Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin Add an flock program inspired by the linux one with ...



details:   https://anonhg.NetBSD.org/src/rev/ede984fe3fcd
branches:  trunk
changeset: 782397:ede984fe3fcd
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Nov 01 23:30:19 2012 +0000

description:
Add an flock program inspired by the linux one with the same name.

diffstat:

 usr.bin/Makefile       |    4 +-
 usr.bin/flock/Makefile |    8 +
 usr.bin/flock/flock.1  |   99 ++++++++++++++++++++++
 usr.bin/flock/flock.c  |  211 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 320 insertions(+), 2 deletions(-)

diffs (truncated from 348 to 300 lines):

diff -r c68e74199b4d -r ede984fe3fcd usr.bin/Makefile
--- a/usr.bin/Makefile  Thu Nov 01 22:15:25 2012 +0000
+++ b/usr.bin/Makefile  Thu Nov 01 23:30:19 2012 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.210 2012/08/08 14:13:46 christos Exp $
+#      $NetBSD: Makefile,v 1.211 2012/11/01 23:30:19 christos Exp $
 #      from: @(#)Makefile      8.3 (Berkeley) 1/7/94
 
 .include <bsd.own.mk>
@@ -10,7 +10,7 @@
        column comm compress config crunch csplit ctags cut \
        deroff db dirname du \
        eject elf2aout elf2ecoff env error expand extattr \
-       false fdformat fgen fincore find finger fmt fold fpr from \
+       false fdformat fgen fincore find finger flock fmt fold fpr from \
        fsplit fstat ftp gcore genassym gencat getconf getent getopt gprof \
        head hexdump iconv id indent infocmp innetgr ipcrm ipcs join jot \
        kdump ktrace ktruss lam last lastcomm ldd leave \
diff -r c68e74199b4d -r ede984fe3fcd usr.bin/flock/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/flock/Makefile    Thu Nov 01 23:30:19 2012 +0000
@@ -0,0 +1,8 @@
+#      $NetBSD: Makefile,v 1.1 2012/11/01 23:30:19 christos Exp $
+#      @(#)Makefile    8.1 (Berkeley) 6/6/93
+
+PROG=  flock
+#LDADD+=       -lutil
+#DPADD+=       ${LIBUTIL}
+
+.include <bsd.prog.mk>
diff -r c68e74199b4d -r ede984fe3fcd usr.bin/flock/flock.1
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/flock/flock.1     Thu Nov 01 23:30:19 2012 +0000
@@ -0,0 +1,99 @@
+.\"    $NetBSD: flock.1,v 1.1 2012/11/01 23:30:19 christos Exp $
+.\"
+.\" Copyright (c) 2012 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Christos Zoulas.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\"
+.Dd November 1, 2012
+.Dt flock 1
+.Os
+.Sh NAME
+.Nm flock
+.Nd Provide locking API for shell scripts
+.Sh SYNOPSIS
+.Nm
+.Op Fl dnosxv
+.Op Fl w Ar timeout
+.Ar lockfile|lockdir
+.Op Ar c
+.Ar command
+.Nm
+.Op Fl dnusxv
+.Op Fl w Ar timeout
+.Ar lockfd
+.Sh DESCRIPTION
+The
+.Nm
+utility provides
+.Xr flock 2
+access to the command line or scripts.
+The first form locks a file or directory while the command provided is executed.
+If the file or directory does not exist, then a file is created.
+.Pp
+The second form can use an arbitrary file descriptor that is provided from a
+shell script for example:
+.nf
+(
+       flock -s 100
+       # commands to be executed under the lock
+) 100> /path/to/lockfile
+.if
+.Pp
+The following options are available:
+.Bl -tag -width "XXXXXXXXXXXXXXXXX"
+.It Fl c Ar command
+Pass a command to a the shell.
+.It Fl d Fl Fl debug
+Provide debugging output.
+.It Fl s Fl Fl shared
+Obtain a shared lock.
+.It Fl e Fl x Fl Fl exclusive
+Obtain an exclusive lock.
+.It Fl u Fl Fl unlock
+Unlock an existing lock.
+This is available only for a file descriptor.
+.It Fl n Fl Fl nb Fl Fl nonblock
+Don't block and fail immediately if the lock could not be obtained.
+.If Fl v Fl Fl verbose
+On error print an explanation of the failure.
+.It Fl w Fl Fl wait Fl Fl timeout Ar seconds
+Fail if the lock could not be obtained after
+.Ar seconds .
+Seconds are truncated to integer values.
+.It Fl o Fl Fl close
+Close the file before executing the command.
+This is useful if the child forks and should not be holding the lock.
+.El
+.Sh EXIT STATUS
+.Ex -std
+.Sh SEE ALSO
+.Xr flock 2
+.Sh HISTORY
+A
+.Nm
+utility appeared in
+.Nx 7
diff -r c68e74199b4d -r ede984fe3fcd usr.bin/flock/flock.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/flock/flock.c     Thu Nov 01 23:30:19 2012 +0000
@@ -0,0 +1,211 @@
+/*     $NetBSD: flock.c,v 1.1 2012/11/01 23:30:19 christos Exp $       */
+
+/*-
+ * Copyright (c) 2012 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: flock.c,v 1.1 2012/11/01 23:30:19 christos Exp $");
+
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <unistd.h>
+#include <err.h>
+#include <errno.h>
+#include <getopt.h>
+#include <paths.h>
+
+struct option flock_longopts[] = {
+       { "debug",              no_argument,            0, 'd' },
+       { "help",               no_argument,            0, 'h' },
+       { "nonblock",           no_argument,            0, 'n' },
+       { "nb",                 no_argument,            0, 'n' },
+       { "close",              no_argument,            0, 'o' },
+       { "shared",             no_argument,            0, 's' },
+       { "exclusive",          no_argument,            0, 'x' },
+       { "unlock",             no_argument,            0, 'u' },
+       { "verbose",            no_argument,            0, 'v' },
+       { "command",            required_argument,      0, 'c' },
+       { "wait",               required_argument,      0, 'w' },
+       { "timeout",            required_argument,      0, 'w' },
+       { NULL,                 0,                      0, 0   },
+};
+
+static int verbose = 0;
+
+static __dead void usage(void) 
+{
+       fprintf(stderr, "Usage: %s [-dnosxv] [-w <timeout>] file|directory [-c] "
+           "<command>\n\t%s [-dnusxv] [-w <timeout>] fd\n", getprogname(),
+           getprogname());
+       exit(EXIT_FAILURE);
+}
+
+static __dead void
+sigalrm(int sig)
+{
+       if (verbose) {
+               errno = ETIMEDOUT;
+               errx(EXIT_FAILURE, "");
+       } else
+               exit(EXIT_FAILURE);
+}
+
+static const char *
+lock2name(int l)
+{
+       static char buf[1024];
+       int nb = l & LOCK_NB;
+
+       l &= ~LOCK_NB;
+       if (nb)
+               strlcpy(buf, "LOCK_NB|", sizeof(buf));
+       else
+               buf[0] = '\0';
+
+       switch (l) {
+       case LOCK_SH:
+               strlcat(buf, "LOCK_SH", sizeof(buf));
+               return buf;
+       case LOCK_EX:
+               strlcat(buf, "LOCK_EX", sizeof(buf));
+               return buf;
+       case LOCK_UN:
+               strlcat(buf, "LOCK_UN", sizeof(buf));
+               return buf;
+       default:
+               snprintf(buf, sizeof(buf), "*%d*", l | nb);
+               return buf;
+       }
+}
+
+int
+main(int argc, char *argv[])
+{
+       int c;
+       int lock = 0;
+       double timeout = 0;
+       int cls = 0;
+       int fd = -1;
+       int debug = 0;
+       char *command = NULL;
+
+       setprogname(argv[0]);
+
+       while ((c = getopt_long(argc, argv, "c:dnosxuw:", flock_longopts, NULL))
+           != -1)
+               switch (c) {
+               case 'c':
+                       command = optarg;
+                       break;
+               case 'd':
+                       debug++;
+                       break;
+               case 'e':
+               case 'x':
+                       lock |= LOCK_EX;
+                       break;
+               case 'n':
+                       lock |= LOCK_NB;
+                       break;
+               case 's':
+                       lock |= LOCK_SH;
+                       break;
+               case 'u':
+                       lock |= LOCK_UN;
+                       break;
+               case 'w':
+                       timeout = strtod(optarg, NULL);
+                       break;
+               case 'v':
+                       verbose = 1;
+                       break;
+               case 'o':
+                       cls = 1;
+                       break;
+               default:
+                       usage();
+               }
+
+       argc -= optind;
+       argv += optind;
+
+       if (command) {
+               if (lock == LOCK_UN)



Home | Main Index | Thread Index | Old Index