Re: [OpenWrt-Devel] [PATCH] busybox: lock: implement -n "Fail rather than wait"

2015-08-13 Thread Bastian Bittorf
* Alexander Couzens  [06.08.2015 19:05]:
> lock -n is similiar to flock -n. If the lock was already taken,
> fail with exit code = 1 and write error message to stderr.

sorry, forget to ask:
is this "more" atomic than 'mkdir mypath || return'?

bye, bastian
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


Re: [OpenWrt-Devel] [PATCH] busybox: lock: implement -n "Fail rather than wait"

2015-08-06 Thread Felix Fietkau
On 2015-08-06 17:28, Alexander Couzens 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.
> 
> Signed-off-by: Alexander Couzens 
> ---
>  package/utils/busybox/patches/220-add_lock_util.patch | 18 +++---
>  1 file changed, 15 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..08f489a 100644
> --- a/package/utils/busybox/patches/220-add_lock_util.patch
> +++ b/package/utils/busybox/patches/220-add_lock_util.patch
> @@ -33,9 +33,11 @@
>   lib-$(CONFIG_MAKEDEVS)+= makedevs.o
>   lib-$(CONFIG_MAN) += man.o
>   lib-$(CONFIG_MICROCOM)+= microcom.o
> +Index: busybox-1.23.2/miscutils/lock.c
> +===
>  --- /dev/null
> -+++ b/miscutils/lock.c
> -@@ -0,0 +1,135 @@
>  busybox-1.23.2/miscutils/lock.c
> +@@ -0,0 +1,145 @@
Please use the right refresh method or run make package/busybox/refresh.

> @@ -65,6 +68,7 @@
>  +"   -s  Use shared locking\n"
>  +"   -u  Unlock\n"
>  +"   -w  Wait for the lock to become free, don't 
> acquire lock\n"
> ++"   -n  Fail rather than wait\n"
>  +"\n", name);
>  +exit(1);
>  +}
> @@ -104,7 +110,10 @@
>  +}
>  +}
>  +
> -+if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
> ++flags = shared ? LOCK_SH : LOCK_EX;
> ++flags |= failinsteadwait ? LOCK_NB : 0;
> ++
> ++if ((ret = flock(fd, flags)) < 0) {
>  +fprintf(stderr, "Can't lock %s\n", file);
>  +return 1;
>  +}
"failinsteadwait" sounds a bit quirky, how about something like
"try_lock" instead. Same applies for the description.

- Felix
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


[OpenWrt-Devel] [PATCH] busybox: lock: implement -n "Fail rather than wait"

2015-08-06 Thread Alexander Couzens
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 
---
 package/utils/busybox/patches/220-add_lock_util.patch | 18 +++---
 1 file changed, 15 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..08f489a 100644
--- a/package/utils/busybox/patches/220-add_lock_util.patch
+++ b/package/utils/busybox/patches/220-add_lock_util.patch
@@ -33,9 +33,11 @@
  lib-$(CONFIG_MAKEDEVS)+= makedevs.o
  lib-$(CONFIG_MAN) += man.o
  lib-$(CONFIG_MICROCOM)+= microcom.o
+Index: busybox-1.23.2/miscutils/lock.c
+===
 --- /dev/null
-+++ b/miscutils/lock.c
-@@ -0,0 +1,135 @@
 busybox-1.23.2/miscutils/lock.c
+@@ -0,0 +1,145 @@
 +/*
 + * Copyright (C) 2006 Felix Fietkau 
 + *
@@ -56,6 +58,7 @@
 +static int unlock = 0;
 +static int shared = 0;
 +static int waitonly = 0;
++static int failinsteadwait = 0;
 +static int fd;
 +static char *file;
 +
@@ -65,6 +68,7 @@
 +  "   -s  Use shared locking\n"
 +  "   -u  Unlock\n"
 +  "   -w  Wait for the lock to become free, don't 
acquire lock\n"
++  "   -n  Fail rather than wait\n"
 +  "\n", name);
 +  exit(1);
 +}
@@ -95,6 +99,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 +110,10 @@
 +  }
 +  }
 +
-+  if (flock(fd, (shared ? LOCK_SH : LOCK_EX)) < 0) {
++  flags = shared ? LOCK_SH : LOCK_EX;
++  flags |= failinsteadwait ? LOCK_NB : 0;
++
++  if ((ret = flock(fd, flags)) < 0) {
 +  fprintf(stderr, "Can't lock %s\n", file);
 +  return 1;
 +  }
@@ -156,6 +165,9 @@
 +  case 'u':
 +  unlock = 1;
 +  break;
++  case 'n':
++  failinsteadwait = 1;
++  break;
 +  }
 +  }
 +  c--;
-- 
2.5.0
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel