Your message dated Fri, 20 Apr 2018 16:34:17 +0000
with message-id <[email protected]>
and subject line Bug#858555: fixed in chiark-utils 6.0.0
has caused the Debian Bug report #858555,
regarding chiark-utils-bin: [patch] add timeout option to with-lock-ex
to be marked as done.

This means that you claim that the problem has been dealt with.
If this is not the case it is now your responsibility to reopen the
Bug report if necessary, and/or fix the problem forthwith.

(NB: If you are a system administrator and have no idea what this
message is talking about, this may indicate a serious mail system
misconfiguration somewhere. Please contact [email protected]
immediately.)


-- 
858555: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=858555
Debian Bug Tracking System
Contact [email protected] with problems
--- Begin Message ---
Package: chiark-utils-bin
Version: 4.2.0
Severity: wishlist
Tags: upstream patch

Hi,

I wanted a timeout option to with-lock-ex. The attached patch provides
this - you can see -t <secs> after -q or -f and with-lock-ex will then
wait that long while trying to get the lock before quitting.

I have built and tested the attached patch against the current master
branch in git.

-- System Information:
Debian Release: 7.11
  APT prefers oldstable-updates
  APT policy: (500, 'oldstable-updates'), (500, 'oldstable')
Architecture: i386 (i686)

Kernel: Linux 3.2.0-4-686-pae (SMP w/4 CPU cores)
Locale: LANG=en_GB.UTF-8, LC_CTYPE=en_GB.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages chiark-utils-bin depends on:
ii  libc6  2.13-38+deb7u11

Versions of packages chiark-utils-bin recommends:
ii  libc6       2.13-38+deb7u11
ii  libgmp10    2:5.0.5+dfsg-2
ii  libnettle4  2.4-3+deb7u1

Versions of packages chiark-utils-bin suggests:
ii  libx11-6  2:1.5.0-1+deb7u4

-- no debconf information
diff --git a/cprogs/with-lock-ex.c b/cprogs/with-lock-ex.c
index 2b7b45b..560711f 100644
--- a/cprogs/with-lock-ex.c
+++ b/cprogs/with-lock-ex.c
@@ -2,7 +2,7 @@
  * File locker
  *
  * Usage:
- *  with-lock-ex -<mode> <lockfile> <command> <args>...
+ *  with-lock-ex -<mode> [-t <secs>] <lockfile> <command> <args>...
  *  with-lock-ex -l      <lockfile>
  *
  * modes are
@@ -13,6 +13,11 @@
  *         or "write <pid>"; lockfile opened for reading;
  *         no command may be specified)
  *
+ * if -t is specified, then with-lock-ex will wait for up to <secs>
+ * seconds to acquire the lock, and then fail or silently do nothing
+ * (depending on whether -f or -q is specified). You cannot specify
+ * a timeout for modes l or w
+ *
  * with-lock-ex will open and lock the lockfile for writing and
  * then feed the remainder of its arguments to exec(2); when
  * that process terminates the fd will be closed and the file
@@ -32,6 +37,10 @@
 #include <unistd.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <limits.h>
+#include <time.h>
+#include <signal.h>
+#include <sys/time.h>
 
 static const char *cmd;
 
@@ -42,12 +51,22 @@ static void fail(const char *why) {
   exit(255);
 }
 
+volatile int alarmed = 0;
+
+static void alrm_handler(int signum) {
+  alarmed = 1;
+}
+
 int main(int argc, char **argv) {
   int fd, mode, um;
   struct stat stab, fstab;
-  long cloexec;
+  long cloexec, secs=-1;
   struct flock fl;
   const char *p;
+  char *endptr;
+  time_t tstart=0, tend=0;
+  struct sigaction siga, oldsiga;
+  struct itimerval itv;
 
   if (argc >= 3 && !strcmp((p= strrchr(argv[0],'/')) ? ++p : argv[0], "with-lock")) {
     mode= 'f';
@@ -56,7 +75,7 @@ int main(int argc, char **argv) {
 	      && mode != 'l') ||
 	     (mode != 'l' && argc < 4) ||
 	     (mode == 'l' && argc != 3)) {
-    fputs("usage: with-lock-ex -w|-q|-f <lockfile> <command> <args>...\n"
+    fputs("usage: with-lock-ex -w|-q|-f [-t <secs>] <lockfile> <command> <args>...\n"
 	  "       with-lock-ex -l       <lockfile>\n"
 	  "       with-lock             <lockfile> <command> <args>...\n",
 	  stderr);
@@ -64,6 +83,39 @@ int main(int argc, char **argv) {
   } else {
     argv++; argc--;
   }
+  if (0 == strncmp(argv[1],"-t",2)) {
+    errno = 0;
+    secs = strtol(argv[2], &endptr, 0);
+    if ((errno == ERANGE && (secs == LONG_MAX || secs == LONG_MIN))
+	|| (errno !=0 && secs == 0)) {
+      fail("parsing timeout value");
+    }
+    if (secs <= 0) {
+      fputs("timeout value must be greater than zero.\n",
+	    stderr);
+      exit(255);
+    }
+    tstart = time(NULL); if (tstart==(time_t)-1) fail("getting current time");
+    if (mode=='l' || mode=='w') {
+      fputs("-t only allowed with -q or -f.\n", stderr);
+      exit(255);
+    }
+    argv+= 2; argc-= 2;
+    if (argc < 2) {
+      fputs("Insufficient arguments following -t\n"
+	    "Usage: with-lock-ex -f|q -t <secs> <lockfile> <command> <args>...\n",
+	    stderr);
+      exit(255);
+    }
+    /*Install SIGALRM handler, and set a timer for secs seconds from now*/
+    memset(&siga,0,sizeof(siga));
+    siga.sa_handler=alrm_handler;
+    if (sigaction(SIGALRM,&siga,&oldsiga)) fail("Installing SIGALRM handler");
+    memset(&itv,0,sizeof(itv));
+    itv.it_value.tv_sec=secs;
+    if (setitimer(ITIMER_REAL,&itv,NULL)) fail("Setting timer");
+  }
+
   cmd= argv[2];
   um= umask(0777); if (um==-1) fail("find umask");
   if (umask(um)==-1) fail("reset umask");
@@ -82,9 +134,21 @@ int main(int argc, char **argv) {
       fl.l_len= mode=='l' ? 0 : 1;
       if (fcntl(fd,
 		mode=='l' ? F_GETLK :
-		mode=='w' ? F_SETLKW :
+		mode=='w' || secs > 0 ? F_SETLKW :
 		F_SETLK,
 		&fl) != -1) break;
+      if (alarmed==1) {
+	tend = time(NULL); if (tend==(time_t)-1) fail("getting current time");
+	if (tend-tstart >= secs) {
+	  if (mode=='q') exit(0);
+	  else {
+	    fprintf(stderr,
+		    "with-lock-ex %s: timer expired while trying to acquire lock\n",
+		    cmd);
+	    exit(255);
+	  }
+	} else alarmed=0;
+      }
       if (mode=='q' &&
 	  (errno == EAGAIN || errno == EWOULDBLOCK || errno == EBUSY))
 	exit(0);
@@ -102,6 +166,11 @@ int main(int argc, char **argv) {
       if (ferror(stdout)) fail("print to stdout\n");
       exit(0);
     }
+    if (secs > 0) {
+      itv.it_value.tv_sec=0;
+      if (setitimer(ITIMER_REAL,&itv,NULL)) fail("Clearing timer");
+      if (sigaction(SIGALRM,&oldsiga,NULL)) fail("Restoring SIGALRM handler");
+    }
 
     if (fstat(fd, &fstab)) fail("could not fstat lock fd");
     if (stat(argv[1], &stab)) {

--- End Message ---
--- Begin Message ---
Source: chiark-utils
Source-Version: 6.0.0

We believe that the bug you reported is fixed in the latest version of
chiark-utils, which is due to be installed in the Debian FTP archive.

A summary of the changes between this version and the previous one is
attached.

Thank you for reporting the bug, which will now be closed.  If you
have further comments please address them to [email protected],
and the maintainer will reopen the bug report if appropriate.

Debian distribution maintenance software
pp.
Ian Jackson <[email protected]> (supplier of updated chiark-utils 
package)

(This message was generated automatically at their request; if you
believe that there is a problem with it please contact the archive
administrators by mailing [email protected])


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Format: 1.8
Date: Fri, 20 Apr 2018 16:56:04 +0100
Source: chiark-utils
Binary: chiark-backup chiark-scripts chiark-rwbuffer chiark-utils-bin 
chiark-really
Architecture: source
Version: 6.0.0
Distribution: unstable
Urgency: medium
Maintainer: Ian Jackson <[email protected]>
Changed-By: Ian Jackson <[email protected]>
Description:
 chiark-backup - backup system for small systems and networks
 chiark-really - really - a tool for gaining privilege (simple, realistic sudo)
 chiark-rwbuffer - readbuffer/writebuffer: prevents tape drive seesawing, etc.
 chiark-scripts - chiark system administration scripts
 chiark-utils-bin - chiark system administration utilities
Closes: 858555
Changes:
 chiark-utils (6.0.0) unstable; urgency=medium
 .
   xbatmon-simple:
   * Minor improvements to the .txt doc.
 .
 chiark-utils (5.0.3~citrix3) unstable; urgency=medium
 .
   fishdescriptor:
   * New utility.
 .
 chiark-utils (5.0.3~citrix2) unstable; urgency=medium
 .
   with-lock-ex:
   * Switch to using getopt.
   * New -t (timeout) option.  Closes:#858555.
 .
   git-cache-proxy:
   * New --serve-timeout option.
   * Radically increase several timeouts.
 .
   Copyright faff:
   * Add DEVELOPER-CERTIFICATE file and start using S-o-b practice.
   * Convert with-lock-ex to MIT, since the PD dedication is not
     effective in some jurisdictions.
 .
 chiark-utils (5.0.3~citrix1) unstable; urgency=medium
 .
   * git-cache-proxy: Do not spuriously timeout on too many simultaneous
     invocations (specifically, do not timeout when reacquiring the lock
     before serving).
Checksums-Sha1:
 d10a31b39eab97aae00a1c98706155fde9d02692 1514 chiark-utils_6.0.0.dsc
 198e80eec70c6f5bc0b166a600c76438e6c0f2ae 174546 chiark-utils_6.0.0.tar.gz
Checksums-Sha256:
 dd192e979a0f5a729a3f3d7b7a3c69124eaf28d87d031036825033f45e9ed96b 1514 
chiark-utils_6.0.0.dsc
 793d7fa232ad9fca5d2ac7ae91ba5d4e32807490be45d0aebf694d1c3ac6528e 174546 
chiark-utils_6.0.0.tar.gz
Files:
 06a21737069ee50f7dfc440502ea478a 1514 admin extra chiark-utils_6.0.0.dsc
 acf8bdcbf3b263b8c60d56da2a86b4ec 174546 admin extra chiark-utils_6.0.0.tar.gz

-----BEGIN PGP SIGNATURE-----

iQEzBAEBCAAdFiEEVZrkbC1rbTJl58uh4+M5I0i1DTkFAlraEFQACgkQ4+M5I0i1
DTnGEAgAmiz84caoh3bgDmHDleOt0rdOZgdRmgL1+lkuzh/AjOnh47IiMZy+a0J5
XuRSlSlrlbwibiD6AHZ4hWxi36poOY0b/GMGXhFvlVlCAD54ePyIBOtJ2AI5hR3Z
OTQiDc8XE6zw8QM+4aTCD5iHecPgflAea11ZirTuV6guReTufc/YmQwJ+pvhd1ve
asqtRwTWdb9VNn7c7lmCs/PttRSn8cRyvTQXytl9EZ3EtKuZb1G2n+nBZYjK+0Wo
dVQXIsuHbbbi7F63ZyJzWUrWM7aTY1U4nLcdk5eak2a7K1Dvu5hPwTxOcUxT3BEE
RPba8+g3HwCl+j/vvwqAC/UGrLL8fA==
=pm8o
-----END PGP SIGNATURE-----

--- End Message ---

Reply via email to