From 4d96d1de070fe254f23f6fe0c701a6f31617a73a Mon Sep 17 00:00:00 2001 From: jdp Date: Thu, 9 Jan 1997 19:53:21 +0000 Subject: On failure, return various exit codes from . In particular, return EX_TEMPFAIL if the file was already locked. This makes it easier to distinguish between lock collisions and failures within the command being executed. Also, don't complain if the unlink() fails in the cleanup handler. It doesn't matter anyway, and it obscured the exit status returned from the command that was executed. --- usr.bin/lockf/lockf.1 | 25 +++++++++++++++++++++---- usr.bin/lockf/lockf.c | 26 +++++++++++++------------- 2 files changed, 34 insertions(+), 17 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/lockf/lockf.1 b/usr.bin/lockf/lockf.1 index 6b78644..c55135e 100644 --- a/usr.bin/lockf/lockf.1 +++ b/usr.bin/lockf/lockf.1 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id$ +.\" $Id: lockf.1,v 1.1.1.1 1997/01/08 20:12:59 jdp Exp $ .\" .Dd January 8, 1997 .Os FreeBSD @@ -85,11 +85,28 @@ break a lock that is held by another process. .Sh DIAGNOSTICS If .Nm -is unable to acquire the lock, it returns an exit status of 1. -Otherwise, it returns the exit status produced by +successfully acquires the lock, it returns the exit status produced by .Ar command . +Otherwise, it returns one of the exit codes defined in +.Xr sysexits 3 , +as follows: +.Bl -tag -width F_CANTCREATX +.It Dv EX_TEMPFAIL +The specified lock file was already locked by another process. +.It Dv EX_CANTCREAT +.Nm +was unable to create the lock file, e.g., because of insufficient access +privileges. +.It Dv EX_USAGE +There was an error on the +.Nm +command line. +.It Dv EX_OSERR +A system call (e.g., fork) failed unexpectedly. +.El .Sh SEE ALSO -.Xr flock 2 . +.Xr flock 2 , +.Xr sysexits 3 . .Sh AUTHORS John Polstra, .Aq jdp@polstra.com . diff --git a/usr.bin/lockf/lockf.c b/usr.bin/lockf/lockf.c index 319dc6b..1058138 100644 --- a/usr.bin/lockf/lockf.c +++ b/usr.bin/lockf/lockf.c @@ -22,7 +22,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: lockf.c,v 1.1.1.1 1997/01/08 20:12:59 jdp Exp $ */ #include @@ -34,6 +34,7 @@ #include #include #include +#include #include static int acquire_lock(const char *name); @@ -73,7 +74,7 @@ main(int argc, char **argv) char *endptr; waitsec = strtol(optarg, &endptr, 0); if (*optarg == '\0' || *endptr != '\0' || waitsec < 0) - errx(1, "invalid timeout \"%s\"", optarg); + errx(EX_USAGE, "invalid timeout \"%s\"", optarg); } break; @@ -109,17 +110,17 @@ main(int argc, char **argv) if (lockfd == -1) { /* We failed to acquire the lock. */ if (silent) - exit(1); - errx(1, "%s: already locked", lockname); + exit(EX_TEMPFAIL); + errx(EX_TEMPFAIL, "%s: already locked", lockname); } /* At this point, we own the lock. */ if (atexit(cleanup) == -1) - err(1, "atexit failed"); + err(EX_OSERR, "atexit failed"); if ((child = fork()) == -1) - err(1, "cannot fork"); + err(EX_OSERR, "cannot fork"); if (child == 0) { /* The child process. */ close(lockfd); @@ -135,7 +136,7 @@ main(int argc, char **argv) signal(SIGTERM, killed); if (waitpid(child, &status, 0) == -1) - err(1, "waitpid failed"); + err(EX_OSERR, "waitpid failed"); return WIFEXITED(status) ? WEXITSTATUS(status) : 1; } @@ -152,7 +153,7 @@ acquire_lock(const char *name) if ((fd = open(name, O_RDONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 0666)) == -1) { if (errno == EAGAIN || errno == EINTR) return -1; - err(1, "cannot open %s", name); + err(EX_CANTCREAT, "cannot open %s", name); } return fd; } @@ -163,8 +164,7 @@ acquire_lock(const char *name) static void cleanup(void) { - if (unlink(lockname) == -1 && errno != ENOENT) - err(1, "cannot unlink %s", lockname); + unlink(lockname); } /* @@ -177,7 +177,7 @@ killed(int sig) cleanup(); signal(sig, SIG_DFL); if (kill(getpid(), sig) == -1) - err(1, "kill failed"); + err(EX_OSERR, "kill failed"); } /* @@ -192,7 +192,7 @@ timeout(int sig) static void usage(void) { - errx(1, "usage: lockf [-s] [-t seconds] file command [arguments]"); + errx(EX_USAGE, "usage: lockf [-s] [-t seconds] file command [arguments]"); } /* @@ -206,7 +206,7 @@ wait_for_lock(const char *name) if ((fd = open(name, O_RDONLY|O_EXLOCK)) == -1) { if (errno == ENOENT || errno == EINTR) return; - err(1, "cannot open %s", name); + err(EX_CANTCREAT, "cannot open %s", name); } close(fd); return; -- cgit v1.1