From patchwork Sat Mar 25 16:34:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luke Shumaker X-Patchwork-Id: 71 Return-Path: Delivered-To: patchwork@archlinux.org Received: from nymeria.archlinux.org by nymeria.archlinux.org (Dovecot) with LMTP id MX4jLjic1lj/MQAAtiB/HQ for ; Sat, 25 Mar 2017 17:35:04 +0100 Received: from nymeria.archlinux.org (localhost.localdomain [127.0.0.1]) by nymeria.archlinux.org (Postfix) with ESMTP id AA68940709; Sat, 25 Mar 2017 17:35:03 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on nymeria.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.5 tests=BAYES_00,RCVD_IN_DNSWL_MED shortcircuit=no autolearn=ham autolearn_force=no version=3.4.1 Received: from luna.archlinux.org (luna.archlinux.org [IPv6:2a01:4f8:160:3033::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by nymeria.archlinux.org (Postfix) with ESMTPS; Sat, 25 Mar 2017 17:35:03 +0100 (CET) Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 09C1F213FB; Sat, 25 Mar 2017 16:35:02 +0000 (UTC) Authentication-Results: luna.archlinux.org; dkim=none Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 7C225213F8 for ; Sat, 25 Mar 2017 16:35:01 +0000 (UTC) Received: from mav.lukeshu.com (team4272.com [IPv6:2001:19f0:5c00:8069:5400:ff:fe26:6a86]) by luna.archlinux.org (Postfix) with ESMTPS for ; Sat, 25 Mar 2017 16:35:01 +0000 (UTC) Received: from build64-par (unknown [IPv6:2601:803:202:9275:21f:e2ff:fe4d:191b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 5A98983EAC; Sat, 25 Mar 2017 12:35:00 -0400 (EDT) From: Luke Shumaker To: arch-projects@lists.archlinux.org Date: Sat, 25 Mar 2017 12:34:39 -0400 Message-Id: <20170325163441.28555-3-lukeshu@parabola.nu> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170325163441.28555-1-lukeshu@parabola.nu> References: <20170325163441.28555-1-lukeshu@parabola.nu> Subject: [arch-projects] [devtools] [PATCH 2/4] lib/common.sh: lock, slock: Allow locks to be inherited. X-BeenThere: arch-projects@archlinux.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Arch Linux projects development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Arch Linux projects development discussion Cc: Luke Shumaker Errors-To: arch-projects-bounces@archlinux.org Sender: "arch-projects" X-UID: 199 Status: X-Keywords: Content-Length: 1642 Allow for locks to be inherited. Inheriting the lock is something that mkarchroot could do previously, but has since lost the ability to do. This allows for the programs to be more compos-able. Do this by instead of unconditionally opening $file on $fd, first check if $file is already open on $fd; and go ahead use it if it is. The naive way of doing this would be to `$(readlink /dev/fd/$fd)` and compare that to `$file`. However, if `$file` is itself a symlink; or there is a symlink somewhere in the path to `$file`, then this could easily fail. Instead, check `[[ "/dev/fd/$fd" -ef "$file" ]]`. Even though the Bash documentation (`help test`) says that `-ef` checks for if the two files are hard links to eachother, because it uses stat(3) (which resolves symlinks) to do this check, it also works with the /dev/fd/ soft links. --- lib/common.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/common.sh b/lib/common.sh index 689772f..63b7795 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -138,7 +138,11 @@ get_full_version() { # usage : lock( $fd, $file, $message ) ## lock() { - eval "exec $1>"'"$2"' + # Only reopen the FD if it wasn't handed to us + if ! [[ "/dev/fd/$1" -ef "$2" ]]; then + eval "exec $1>"'"$2"' + fi + if ! flock -n $1; then stat_busy "$3" flock $1 @@ -150,7 +154,11 @@ lock() { # usage : slock( $fd, $file, $message ) ## slock() { - eval "exec $1>"'"$2"' + # Only reopen the FD if it wasn't handed to us + if ! [[ "/dev/fd/$1" -ef "$2" ]]; then + eval "exec $1>"'"$2"' + fi + if ! flock -sn $1; then stat_busy "$3" flock -s $1