[OpenWrt-Devel] [PATCH v2] busybox: lock: implement -n "Fail rather than wait"
Jonas Gorski
jogo at openwrt.org
Fri Aug 7 12:19:11 EDT 2015
On Fri, Aug 7, 2015 at 5:40 PM, Alexander Couzens <lynxis at fe80.eu> wrote:
> 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.
>
> v1: implement feature
> v2: rename variable failinsteadwait into try_lock
> extend description of -n
> run make package/utils/busybox/refresh
>
> Signed-off-by: Alexander Couzens <lynxis at fe80.eu>
> ---
> package/utils/busybox/patches/220-add_lock_util.patch | 16 +++++++++++++---
> 1 file changed, 13 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..c261a9e 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,145 @@
> +/*
> + * Copyright (C) 2006 Felix Fietkau <nbd at openwrt.org>
> + *
> @@ -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,8 @@
> +static int do_lock(void)
> +{
> + int pid;
> ++ int ret;
> ++ int flags;
> + char pidstr[8];
> +
> + if ((fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0700)) < 0) {
> @@ -104,7 +108,10 @@
> + }
> + }
> +
> -+ if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
> ++ flags = shared ? LOCK_SH : LOCK_EX;
> ++ flags |= try_lock ? LOCK_NB : 0;
> ++
> ++ if ((ret = flock(fd, flags)) < 0) {
> + fprintf(stderr, "Can't lock %s\n", file);
> + return 1;
You don't use the value of ret, did you intend to print it? Else it is
unneeded and can be dropped.
> + }
> @@ -156,6 +163,9 @@
> + case 'u':
> + unlock = 1;
> + break;
> ++ case 'n':
> ++ try_lock = 1;
> ++ break;
> + }
> + }
> + c--;
Jonas
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
More information about the openwrt-devel
mailing list