summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2005-07-09 21:34:44 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2005-07-09 21:34:44 (GMT)
commit252e4d6b9f1a5ad313ebceb65f0411872f542fab (patch)
treee66141d835aae6bdec4ba5fea2c8ffe5607f79cb
parent99f14d557425cb68c39143a7108563227b0a91d7 (diff)
downloadflock-252e4d6b9f1a5ad313ebceb65f0411872f542fab.zip
flock-252e4d6b9f1a5ad313ebceb65f0411872f542fab.tar.gz
flock-252e4d6b9f1a5ad313ebceb65f0411872f542fab.tar.bz2
flock-252e4d6b9f1a5ad313ebceb65f0411872f542fab.tar.xz
New version which incorporates all functionality of Adam Richter's
flock(1) as integrated in util-linux
-rw-r--r--Makefile7
-rw-r--r--flock.128
-rw-r--r--flock.c198
-rw-r--r--version3
4 files changed, 210 insertions, 26 deletions
diff --git a/Makefile b/Makefile
index e33964e..869f0d6 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,8 @@
CC = cc
-CFLAGS = -g -O
+CFLAGS = -g -O -W -Wall
LDFLAGS =
INSTALL = install
+LIBS = -liberty # For xmalloc()
prefix = /usr/local
BINDIR = $(prefix)/bin
MANDIR = $(prefix)/man/man1
@@ -12,7 +13,7 @@ SOURCES = flock.c flock.1 flock.spec Makefile version
all: flock
flock: flock.c version
- $(CC) $(CFLAGS) $(LDFLAGS) -DVERSION=\"$(VERSION)\" -o $@ $<
+ $(CC) $(CFLAGS) $(LDFLAGS) -DVERSION=\"$(VERSION)\" -o $@ $< $(LIBS)
install:
mkdir -m 755 -p $(INSTALLROOT)$(BINDIR) $(INSTALLROOT)$(MANDIR)
@@ -32,4 +33,4 @@ tar:
cp $(SOURCES) flock-$(VERSION)
tar cvvf flock-$(VERSION).tar flock-$(VERSION)
gzip -9 flock-$(VERSION).tar
- rm -rf flock-$(VERSION) \ No newline at end of file
+ rm -rf flock-$(VERSION)
diff --git a/flock.1 b/flock.1
index 8b0d600..8c71ff6 100644
--- a/flock.1
+++ b/flock.1
@@ -29,13 +29,24 @@
.SH NAME
flock \- Manage locks from shell scripts
.SH SYNOPSIS
-\fBflock\fP [\fIOPTIONS\fR] FD
+\fBflock\fP [\fB\-sxon\fP] [\fB\-w\fP \fItimeout\fP] \fIlockfile\fP [\fB\-c\fP] \fIcommand...\fP
+.PP
+\fBflock\fP [\fB\-sxun\fP] [\fB\-w\fP \fItimeout\fP] \fIfd\fP
.SH DESCRIPTION
.PP
This utility manages
.BR flock (2)
-locks from within shell scripts. It is usually used in something
-similar to the following manner:
+locks from within shell scripts or the command line.
+.PP
+The first form wraps the lock around the executing a command, in a manner similar to
+.BR su (1)
+or
+.BR newgrp (1).
+It locks a specified file, which is created (assuming appropriate
+permissions), if it does not already exist.
+.PP
+The second form is conveninent inside shell scripts, and is usually
+used the following manner:
.PP
\fC(
.br
@@ -73,12 +84,21 @@ lock.
Fail (with an exit code of 1) rather than wait if the lock cannot be
immediately acquired.
.TP
+\fB\-w\fP, \fB\-\-timeout\fP \fIseconds\fP
+Fail (with an exit code of 1) if the lock cannot be acquired within
+\fIseconds\fP seconds. Decimal fractional values are allowed.
+.TP
+\fB\-o\fP, \fB\-\-close\fP
+Close the file descriptor on which the lock is held before executing
+\fIcommand\fP. This is useful if \fIcommand\fP spawns a child process
+which should not be hold ing the lock.
+.TP
\fB\-h\fP, \fB\-\-help\fP
Print a help message.
.SH AUTHOR
Written by H. Peter Anvin <hpa@zytor.com>.
.SH COPYRIGHT
-Copyright \(co 2003 H. Peter Anvin.
+Copyright \(co 2003\-2005 H. Peter Anvin.
.br
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
diff --git a/flock.c b/flock.c
index a59f9df..36f1912 100644
--- a/flock.c
+++ b/flock.c
@@ -1,7 +1,7 @@
/* $Id$ */
/* ----------------------------------------------------------------------- *
*
- * Copyright 2003 H. Peter Anvin - All Rights Reserved
+ * Copyright 2003-2005 H. Peter Anvin - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -16,15 +16,27 @@
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
+#include <signal.h>
+#include <ctype.h>
+#include <string.h>
+#include <paths.h>
#include <sysexits.h>
+#include <sys/types.h>
#include <sys/file.h>
+#include <sys/time.h>
+#include <sys/wait.h>
-const struct option long_options[] = {
+extern void *xmalloc(size_t); /* in libiberty.a */
+
+static const struct option long_options[] = {
{ "shared", 0, NULL, 's' },
{ "exclusive", 0, NULL, 'x' },
{ "unlock", 0, NULL, 'u' },
{ "nonblocking", 0, NULL, 'n' },
{ "nb", 0, NULL, 'n' },
+ { "timeout", 1, NULL, 'w' },
+ { "wait", 1, NULL, 'w' },
+ { "close", 0, NULL, 'o' },
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ 0, 0, 0, 0 }
@@ -32,37 +44,90 @@ const struct option long_options[] = {
const char *program;
-void usage(int ex)
+static void usage(int ex)
{
fprintf(stderr,
"flock version " VERSION "\n"
- "Usage: %s [options] fd#\n"
+ "Usage: %s [-seun][-w #] fd#\n"
+ " %s [-sen][-w #] file [-c] command...\n"
" -s --shared Get a shared lock\n"
" -e --exclusive Get an exclusive lock\n"
" -u --unlock Remove a lock\n"
" -n --nonblock Fail rather than wait\n"
+ " -w --timeout Wait for a limited amount of time\n"
+ " -o --close Close file descriptor before running command\n"
+ " -c --command Run a single command string through the shell\n"
" -h --help Display this text\n"
" -V --version Display version\n",
- program);
+ program, program);
exit(ex);
}
+static sig_atomic_t timeout_expired = 0;
+
+static void timeout_handler(int sig)
+{
+ (void)sig;
+
+ timeout_expired = 1;
+}
+
+
+static char * strtotimeval(const char *str, struct timeval *tv)
+{
+ char *s;
+ long fs; /* Fractional seconds */
+ int i;
+
+ tv->tv_sec = strtol(str, &s, 10);
+ fs = 0;
+
+ if ( *s == '.' ) {
+ s++;
+
+ for ( i = 0 ; i < 6 ; i++ ) {
+ if ( !isdigit(*s) )
+ break;
+
+ fs *= 10;
+ fs += *s++ - '0';
+ }
+
+ for ( ; i < 6; i++ )
+ fs *= 10;
+
+ while ( isdigit(*s) )
+ s++;
+ }
+
+ tv->tv_usec = fs;
+ return s;
+}
+
int main(int argc, char *argv[])
{
+ struct itimerval timeout;
+ int have_timeout = 0;
int type = LOCK_EX;
int block = 0;
- int fd;
+ int fd = -1;
int opt, ix;
+ int do_close = 0;
int err;
+ int status;
char *eon;
+ char **cmd_argv = NULL;
+ const char *filename = NULL;
program = argv[0];
if ( argc < 2 )
usage(EX_USAGE);
- while ( (opt = getopt_long(argc, argv, "sexnuh", long_options, &ix)) != EOF ) {
+ memset(&timeout, 0, sizeof timeout);
+
+ while ( (opt = getopt_long(argc, argv, "+sexnouw:h?", long_options, &ix)) != EOF ) {
switch(opt) {
case 's':
type = LOCK_SH;
@@ -74,35 +139,134 @@ int main(int argc, char *argv[])
case 'u':
type = LOCK_UN;
break;
+ case 'o':
+ do_close = 1;
+ break;
case 'n':
block = LOCK_NB;
break;
+ case 'w':
+ have_timeout = 1;
+ eon = strtotimeval(optarg, &timeout.it_value);
+ if ( *eon )
+ usage(EX_USAGE);
+ break;
case 'V':
fprintf(stderr, "flock " VERSION "\n");
exit(0);
default:
- usage(0);
+ usage((optopt != 'h' && optopt != '?') ? EX_USAGE : 0);
break;
}
}
- fd = (int)strtol(argv[optind], &eon, 10);
- if ( *eon || !argv[optind] ) {
- fprintf(stderr, "%s: bad number: %s\n", program, argv[optind]);
- exit(EX_USAGE);
+ if ( argc > optind+1 ) {
+ /* Run command */
+
+ if ( !strcmp(argv[optind+1], "-c") ||
+ !strcmp(argv[optind+1], "--command") ) {
+
+ if ( argc != optind+3 ) {
+ fprintf(stderr, "%s: %s requires exactly one command argument\n",
+ program, argv[optind+1]);
+ exit(EX_USAGE);
+ }
+
+ cmd_argv = xmalloc(4*sizeof(char *));
+
+ cmd_argv[0] = getenv("SHELL");
+ if ( !cmd_argv[0] || !*cmd_argv[0] )
+ cmd_argv[0] = _PATH_BSHELL;
+
+ cmd_argv[1] = "-c";
+ cmd_argv[2] = argv[optind+2];
+ cmd_argv[3] = 0;
+ } else {
+ cmd_argv = &argv[optind+1];
+ }
+
+ filename = argv[optind];
+ fd = open(filename, O_RDONLY|O_CREAT, 0666);
+
+ if ( fd < 0 ) {
+ err = errno;
+ fprintf(stderr, "%s: cannot open lock file %s: %s\n",
+ program, argv[optind], strerror(err));
+ exit((err == ENOMEM||err == EMFILE||err == ENFILE) ? EX_OSERR :
+ (err == EROFS||err == ENOSPC) ? EX_CANTCREAT :
+ EX_NOINPUT);
+ }
+
+ } else {
+ /* Use provided file descriptor */
+
+ fd = (int)strtol(argv[optind], &eon, 10);
+ if ( *eon || !argv[optind] ) {
+ fprintf(stderr, "%s: bad number: %s\n", program, argv[optind]);
+ exit(EX_USAGE);
+ }
+
}
-
+
+ if ( have_timeout ) {
+ struct sigaction sa;
+
+ memset(&sa, 0, sizeof sa);
+
+ sa.sa_handler = timeout_handler;
+ sa.sa_flags = SA_ONESHOT;
+ sigaction(SIGALRM, &sa, NULL);
+
+ setitimer(ITIMER_REAL, &timeout, NULL);
+ }
+
while ( flock(fd, type|block) ) {
switch( (err = errno) ) {
case EWOULDBLOCK: /* -n option set and failed to lock */
exit(1);
- case EINTR: /* Interrupted, try again */
- continue;
+ case EINTR: /* Signal received */
+ if ( timeout_expired )
+ return 1; /* -w option set and failed to lock */
+ continue; /* otherwise try again */
default: /* Other errors */
- fprintf(stderr, "%s: %d: %s\n", program, fd, strerror(err));
+ if ( filename )
+ fprintf(stderr, "%s: %s: %s\n", program, filename, strerror(err));
+ else
+ fprintf(stderr, "%s: %d: %s\n", program, fd, strerror(err));
exit((err == ENOLCK||err == ENOMEM) ? EX_OSERR : EX_DATAERR);
}
}
+
+ status = 0;
- return 0; /* If we get here, we were successful */
+ if ( cmd_argv ) {
+ pid_t w, f;
+
+ f = fork();
+
+ if ( f < 0 ) {
+ err = errno;
+ fprintf(stderr, "%s: fork: %s\n", program, strerror(err));
+ exit(EX_OSERR);
+ } else if ( f == 0 ) {
+ if ( do_close )
+ close(fd);
+ execvp(cmd_argv[0], cmd_argv);
+ _exit(EX_UNAVAILABLE); /* execvp() failed */
+ } else {
+ do {
+ w = waitpid(f, &status, 0);
+ } while ( w != f );
+
+ if ( WIFEXITED(status) )
+ status = WEXITSTATUS(status);
+ else if ( WIFSIGNALED(status) )
+ status = WTERMSIG(status) + 128;
+ else
+ status = EX_OSERR; /* WTF? */
+ }
+ }
+
+ return status;
}
+
diff --git a/version b/version
index 658aed9..cd5ac03 100644
--- a/version
+++ b/version
@@ -1,2 +1 @@
-1.0.1
-
+2.0