From patchwork Mon Aug 24 01:57:10 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander 'lynxis' Couzens X-Patchwork-Id: 509900 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 21520140332 for ; Mon, 24 Aug 2015 11:58:14 +1000 (AEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id D35B9283F0C; Mon, 24 Aug 2015 03:57:02 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00 autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 9755D2808A0 for ; Mon, 24 Aug 2015 03:56:54 +0200 (CEST) X-policyd-weight: using cached result; rate: -7.6 Received: from mail.base45.de (mail.base45.de [80.241.61.77]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Mon, 24 Aug 2015 03:56:51 +0200 (CEST) Received: from port-92-195-113-129.dynamic.qsc.de ([92.195.113.129] helo=lazus.yip) by mail.base45.de with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA256:128) (Exim 4.82) (envelope-from ) id 1ZTh0y-0001IT-Tx; Mon, 24 Aug 2015 03:57:37 +0200 From: Alexander Couzens To: OpenWrt Development List Date: Mon, 24 Aug 2015 03:57:10 +0200 Message-Id: <1440381430-5767-1-git-send-email-lynxis@fe80.eu> X-Mailer: git-send-email 2.5.0 In-Reply-To: References: Subject: [OpenWrt-Devel] [PATCH v3] busybox: lock: implement -n "Fail rather than wait" X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" lock -n is similiar to flock -n. If the lock was already taken, fail with exit code = 1 and write error message to stderr. example: if ! lock -n /tmp/foo ; then echo lock exits. else echo lock was free. But is locked now. fi > lock was free. But is locked now. > lock exists. Signed-off-by: Alexander Couzens --- v1: implement feature v2: rename variable failinsteadwait into try_lock extend description of -n run make package/utils/busybox/refresh v3: drop changelog from commit message remove ret variable package/utils/busybox/patches/220-add_lock_util.patch | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/package/utils/busybox/patches/220-add_lock_util.patch b/package/utils/busybox/patches/220-add_lock_util.patch index f42edcb..9cac9e6 100644 --- a/package/utils/busybox/patches/220-add_lock_util.patch +++ b/package/utils/busybox/patches/220-add_lock_util.patch @@ -1,6 +1,6 @@ --- a/include/applets.src.h +++ b/include/applets.src.h -@@ -211,6 +211,7 @@ IF_LN(APPLET_NOEXEC(ln, ln, BB_DIR_BIN, +@@ -211,6 +211,7 @@ IF_LN(APPLET_NOEXEC(ln, ln, BB_DIR_BIN, IF_LOAD_POLICY(APPLET(load_policy, BB_DIR_USR_SBIN, BB_SUID_DROP)) IF_LOADFONT(APPLET(loadfont, BB_DIR_USR_SBIN, BB_SUID_DROP)) IF_LOADKMAP(APPLET(loadkmap, BB_DIR_SBIN, BB_SUID_DROP)) @@ -35,7 +35,7 @@ lib-$(CONFIG_MICROCOM) += microcom.o --- /dev/null +++ b/miscutils/lock.c -@@ -0,0 +1,135 @@ +@@ -0,0 +1,144 @@ +/* + * Copyright (C) 2006 Felix Fietkau + * @@ -56,6 +56,7 @@ +static int unlock = 0; +static int shared = 0; +static int waitonly = 0; ++static int try_lock = 0; +static int fd; +static char *file; + @@ -65,6 +66,7 @@ + " -s Use shared locking\n" + " -u Unlock\n" + " -w Wait for the lock to become free, don't acquire lock\n" ++ " -n Don't wait for the lock to become free. Fail with exit code\n" + "\n", name); + exit(1); +} @@ -95,6 +97,7 @@ +static int do_lock(void) +{ + int pid; ++ int flags; + char pidstr[8]; + + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) { @@ -104,7 +107,10 @@ + } + } + -+ if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) { ++ flags = shared ? LOCK_SH : LOCK_EX; ++ flags |= try_lock ? LOCK_NB : 0; ++ ++ if (flock(fd, flags) < 0) { + fprintf(stderr, "Can't lock %s\n", file); + return 1; + } @@ -156,6 +162,9 @@ + case 'u': + unlock = 1; + break; ++ case 'n': ++ try_lock = 1; ++ break; + } + } + c--;