From da6f4aa5d906c32cf13f393e3cc0f91cc5dcf9c1 Mon Sep 17 00:00:00 2001 From: Matthew Vernon Date: Wed, 3 May 2017 14:30:02 +0100 Subject: [PATCH 1/1] with-lock-ex: Provide -t (timeout) option Signed-off-by: Ian Jackson Signed-off-by: Matthew Vernon --- v4: Initial capital and final full-stop on sentences Remove code that attempted to reduce post-lock timeout race Move timer clearing to outside the retry loop v3: split off from conversion to getopt --- cprogs/with-lock-ex.c | 78 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/cprogs/with-lock-ex.c b/cprogs/with-lock-ex.c index d1b23db..f69949e 100644 --- a/cprogs/with-lock-ex.c +++ b/cprogs/with-lock-ex.c @@ -2,7 +2,7 @@ * File locker * * Usage: - * with-lock-ex - ... + * with-lock-ex - [-t ] ... * with-lock-ex -l * * modes are @@ -13,6 +13,11 @@ * or "write "; lockfile opened for reading; * no command may be specified) * + * If -t is specified, then with-lock-ex will wait for up to + * 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 @@ -56,6 +61,9 @@ #include #include #include +#include +#include +#include static const char *cmd; @@ -78,11 +86,29 @@ static void badusage(void) { static int mode; +/* This signal handler uses unsafe functions, so MUST NOT be callable + * during an unsafe function, as that is Undefined Behaviour + */ +static void alrm_handler(int signum) { + if (mode=='q') { + exit(0); + } else { + fprintf(stderr, + "with-lock-ex %s: timer expired while trying to acquire lock\n", + cmd); + exit(255); + } +} + int main(int argc, char **argv) { - int fd, um, c; + int fd, um, c, r; struct stat stab, fstab; - long cloexec; + long cloexec, secs=0; struct flock fl; + char *endptr; + sigset_t sigs, oldsigs; + struct sigaction siga; + struct itimerval itv; mode= 'x'; while ((c= getopt(argc,argv,"+wfqlt:")) != -1) { @@ -94,14 +120,41 @@ int main(int argc, char **argv) { if (mode != 'x') badusage(); mode= c; break; + case 't': + errno = 0; + secs = strtol(optarg, &endptr, 0); + if (*endptr || endptr==optarg || errno==ERANGE) + fail("parsing timeout value"); + if (secs < 0) { + fprintf(stderr,"timeout value must be >=0\n"); + exit(255); + } + break; default: badusage(); } } + if (secs && (mode=='l' || mode=='w')) { + fputs("-t only allowed with -q or -f.\n", stderr); + exit(255); + } + argv += optind-1; argc -= optind-1; if (argc < 2) badusage(); + if (secs) { + if (sigemptyset(&sigs)) fail("Initialising signal set"); + if (sigaddset(&sigs,SIGALRM)) fail("Adding SIGALRM to signal set"); + if (sigprocmask(SIG_BLOCK,&sigs,&oldsigs)) fail("Blocking SIGALRM"); + memset(&siga,0,sizeof(siga)); + siga.sa_handler=alrm_handler; + if (sigaction(SIGALRM,&siga,NULL)) 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"); @@ -118,11 +171,16 @@ int main(int argc, char **argv) { fl.l_whence= SEEK_SET; fl.l_start= 0; fl.l_len= mode=='l' ? 0 : 1; - if (fcntl(fd, + if (secs) sigprocmask(SIG_UNBLOCK,&sigs,NULL); + r = fcntl(fd, mode=='l' ? F_GETLK : - mode=='w' ? F_SETLKW : + mode=='w' || secs > 0 ? F_SETLKW : F_SETLK, - &fl) != -1) break; + &fl); + if (secs) sigprocmask(SIG_BLOCK,&sigs,NULL); + if (!r) { + break; + } if (mode=='q' && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EBUSY)) exit(0); @@ -151,6 +209,14 @@ int main(int argc, char **argv) { close(fd); } + if (secs) { + itv.it_value.tv_sec=0; + if (setitimer(ITIMER_REAL,&itv,NULL)) fail("Clearing timer"); + sigprocmask(SIG_SETMASK,&oldsigs,NULL); + siga.sa_handler=SIG_DFL; + sigaction(SIGALRM,&siga,NULL); + } + cloexec= fcntl(fd, F_GETFD); if (cloexec==-1) fail("fcntl F_GETFD"); cloexec &= ~1; if (fcntl(fd, F_SETFD, cloexec)==-1) fail("fcntl F_SETFD"); -- 2.30.2