CVS commit: src/usr.bin/flock

2014-08-18 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Aug 18 09:14:03 UTC 2014

Modified Files:
src/usr.bin/flock: flock.c

Log Message:
make this behave like linux.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/usr.bin/flock/flock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.c
diff -u src/usr.bin/flock/flock.c:1.9 src/usr.bin/flock/flock.c:1.10
--- src/usr.bin/flock/flock.c:1.9	Mon Jan  6 21:07:08 2014
+++ src/usr.bin/flock/flock.c	Mon Aug 18 05:14:03 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: flock.c,v 1.9 2014/01/07 02:07:08 joerg Exp $	*/
+/*	$NetBSD: flock.c,v 1.10 2014/08/18 09:14:03 christos Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include sys/cdefs.h
-__RCSID($NetBSD: flock.c,v 1.9 2014/01/07 02:07:08 joerg Exp $);
+__RCSID($NetBSD: flock.c,v 1.10 2014/08/18 09:14:03 christos Exp $);
 
 #include stdio.h
 #include string.h
@@ -43,6 +43,7 @@ __RCSID($NetBSD: flock.c,v 1.9 2014/01/
 #include errno.h
 #include getopt.h
 #include paths.h
+#include limits.h
 #include time.h
 
 static struct option flock_longopts[] = {
@@ -155,6 +156,7 @@ main(int argc, char *argv[])
 	int fd = -1;
 	int debug = 0;
 	int verbose = 0;
+	long l;
 	char *mcargv[] = {
 	__UNCONST(_PATH_BSHELL), __UNCONST(-c), NULL, NULL
 	};
@@ -207,7 +209,7 @@ main(int argc, char *argv[])
 	argv += optind;
 
 	if ((lock  ~LOCK_NB) == 0)
-		usage(Missing lock type flag);
+		lock |= LOCK_EX;	/* default to exclusive like linux */
 
 	switch (argc) {
 	case 0:
@@ -215,7 +217,13 @@ main(int argc, char *argv[])
 	case 1:
 		if (cls)
 			usage(Close is valid only for descriptors);
-		fd = strtol(argv[0], NULL, 0);	// XXX: error checking
+		errno = 0;
+		l = strtol(argv[0], v, 0);	// XXX: error checking
+		if ((l == LONG_MIN || l == LONG_MAX)  errno == ERANGE)
+			err(EXIT_FAILURE, Bad file descriptor `%s', argv[0]);
+		if (l  INT_MAX || l  0 || *v)
+			errx(EXIT_FAILURE, Bad file descriptor `%s', argv[0]);
+		fd = (int)l;
 		if (debug) {
 			fprintf(stderr, descriptor %s lock %s\n,
 			argv[0], lock2name(lock));



CVS commit: src/usr.bin/flock

2014-08-18 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Aug 18 09:16:35 UTC 2014

Modified Files:
src/usr.bin/flock: flock.c

Log Message:
remove XXX, fix error message


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/usr.bin/flock/flock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.c
diff -u src/usr.bin/flock/flock.c:1.10 src/usr.bin/flock/flock.c:1.11
--- src/usr.bin/flock/flock.c:1.10	Mon Aug 18 05:14:03 2014
+++ src/usr.bin/flock/flock.c	Mon Aug 18 05:16:35 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: flock.c,v 1.10 2014/08/18 09:14:03 christos Exp $	*/
+/*	$NetBSD: flock.c,v 1.11 2014/08/18 09:16:35 christos Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include sys/cdefs.h
-__RCSID($NetBSD: flock.c,v 1.10 2014/08/18 09:14:03 christos Exp $);
+__RCSID($NetBSD: flock.c,v 1.11 2014/08/18 09:16:35 christos Exp $);
 
 #include stdio.h
 #include string.h
@@ -216,9 +216,9 @@ main(int argc, char *argv[])
 		usage(Missing lock file argument);
 	case 1:
 		if (cls)
-			usage(Close is valid only for descriptors);
+			usage(Close is not valid for descriptors);
 		errno = 0;
-		l = strtol(argv[0], v, 0);	// XXX: error checking
+		l = strtol(argv[0], v, 0);
 		if ((l == LONG_MIN || l == LONG_MAX)  errno == ERANGE)
 			err(EXIT_FAILURE, Bad file descriptor `%s', argv[0]);
 		if (l  INT_MAX || l  0 || *v)



CVS commit: src/usr.bin/flock

2014-08-18 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Aug 18 09:45:52 UTC 2014

Modified Files:
src/usr.bin/flock: flock.1

Log Message:
mention that -x is the default.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/usr.bin/flock/flock.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.1
diff -u src/usr.bin/flock/flock.1:1.9 src/usr.bin/flock/flock.1:1.10
--- src/usr.bin/flock/flock.1:1.9	Sat Sep 21 11:01:14 2013
+++ src/usr.bin/flock/flock.1	Mon Aug 18 05:45:52 2014
@@ -1,4 +1,4 @@
-.\	$NetBSD: flock.1,v 1.9 2013/09/21 15:01:14 khorben Exp $
+.\	$NetBSD: flock.1,v 1.10 2014/08/18 09:45:52 christos Exp $
 .\
 .\ Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -28,7 +28,7 @@
 .\ POSSIBILITY OF SUCH DAMAGE.
 .\
 .\
-.Dd November 2, 2012
+.Dd August 18, 2014
 .Dt FLOCK 1
 .Os
 .Sh NAME
@@ -87,6 +87,7 @@ Fail if the lock could not be obtained a
 .Ar seconds .
 .It Fl x , Fl Fl exclusive
 Obtain an exclusive lock.
+This is the default.
 .El
 .Sh EXIT STATUS
 .Ex -std



CVS commit: src/usr.bin/flock

2013-10-29 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Oct 29 16:02:15 UTC 2013

Modified Files:
src/usr.bin/flock: flock.c

Log Message:
PR/48351: Dennis Ferguson: Fix incorrect parsing of flock flags.
XXX: still flock -s 0 fails with EINVAL, why?


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/usr.bin/flock/flock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.c
diff -u src/usr.bin/flock/flock.c:1.7 src/usr.bin/flock/flock.c:1.8
--- src/usr.bin/flock/flock.c:1.7	Thu Feb  7 08:57:40 2013
+++ src/usr.bin/flock/flock.c	Tue Oct 29 12:02:15 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: flock.c,v 1.7 2013/02/07 13:57:40 tron Exp $	*/
+/*	$NetBSD: flock.c,v 1.8 2013/10/29 16:02:15 christos Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include sys/cdefs.h
-__RCSID($NetBSD: flock.c,v 1.7 2013/02/07 13:57:40 tron Exp $);
+__RCSID($NetBSD: flock.c,v 1.8 2013/10/29 16:02:15 christos Exp $);
 
 #include stdio.h
 #include string.h
@@ -149,7 +149,7 @@ int
 main(int argc, char *argv[])
 {
 	int c;
-	int lock = LOCK_EX;
+	int lock = 0;
 	double timeout = 0;
 	int cls = 0;
 	int fd = -1;
@@ -170,7 +170,8 @@ main(int argc, char *argv[])
 			debug++;
 			break;
 		case 'x':
-			if (lock  ~LOCK_NB)
+#define T(l)	(lock  ~LOCK_NB) != (l)  (lock  ~LOCK_NB) != 0
+			if (T(LOCK_EX))
 goto badlock;
 			lock |= LOCK_EX;
 			break;
@@ -178,12 +179,12 @@ main(int argc, char *argv[])
 			lock |= LOCK_NB;
 			break;
 		case 's':
-			if (lock  ~LOCK_NB)
+			if (T(LOCK_SH))
 goto badlock;
 			lock |= LOCK_SH;
 			break;
 		case 'u':
-			if (lock  ~LOCK_NB)
+			if (T(LOCK_UN))
 goto badlock;
 			lock |= LOCK_UN;
 			break;
@@ -205,6 +206,9 @@ main(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
+	if ((lock  ~LOCK_NB) == 0)
+		usage(Missing lock type flag);
+
 	switch (argc) {
 	case 0:
 		usage(Missing lock file argument);



CVS commit: src/usr.bin/flock

2013-09-21 Thread Pierre Pronchery
Module Name:src
Committed By:   khorben
Date:   Sat Sep 21 15:01:14 UTC 2013

Modified Files:
src/usr.bin/flock: flock.1

Log Message:
flock(1) really appeared first in NetBSD 6.1


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/usr.bin/flock/flock.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.1
diff -u src/usr.bin/flock/flock.1:1.8 src/usr.bin/flock/flock.1:1.9
--- src/usr.bin/flock/flock.1:1.8	Sat Nov  3 00:50:04 2012
+++ src/usr.bin/flock/flock.1	Sat Sep 21 15:01:14 2013
@@ -1,4 +1,4 @@
-.\	$NetBSD: flock.1,v 1.8 2012/11/03 00:50:04 wiz Exp $
+.\	$NetBSD: flock.1,v 1.9 2013/09/21 15:01:14 khorben Exp $
 .\
 .\ Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -97,4 +97,4 @@ Obtain an exclusive lock.
 An
 .Nm
 utility appeared in
-.Nx 7.0 .
+.Nx 6.1 .



CVS commit: src/usr.bin/flock

2013-02-07 Thread Matthias Scheler
Module Name:src
Committed By:   tron
Date:   Thu Feb  7 13:57:40 UTC 2013

Modified Files:
src/usr.bin/flock: flock.c

Log Message:
Don't crash if flock is used to lock a file descriptor e.g. via
flock --nb 8.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/usr.bin/flock/flock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.c
diff -u src/usr.bin/flock/flock.c:1.6 src/usr.bin/flock/flock.c:1.7
--- src/usr.bin/flock/flock.c:1.6	Fri Nov  2 17:03:16 2012
+++ src/usr.bin/flock/flock.c	Thu Feb  7 13:57:40 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: flock.c,v 1.6 2012/11/02 17:03:16 christos Exp $	*/
+/*	$NetBSD: flock.c,v 1.7 2013/02/07 13:57:40 tron Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include sys/cdefs.h
-__RCSID($NetBSD: flock.c,v 1.6 2012/11/02 17:03:16 christos Exp $);
+__RCSID($NetBSD: flock.c,v 1.7 2013/02/07 13:57:40 tron Exp $);
 
 #include stdio.h
 #include string.h
@@ -212,9 +212,12 @@ main(int argc, char *argv[])
 		if (cls)
 			usage(Close is valid only for descriptors);
 		fd = strtol(argv[0], NULL, 0);	// XXX: error checking
-		if (debug)
+		if (debug) {
 			fprintf(stderr, descriptor %s lock %s\n,
 			argv[0], lock2name(lock));
+		}
+		break;
+
 	default:
 		if ((lock  LOCK_NB) == LOCK_UN)
 			usage(Unlock is only valid for descriptors);



CVS commit: src/usr.bin/flock

2012-11-02 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Nov  2 12:47:23 UTC 2012

Modified Files:
src/usr.bin/flock: flock.c

Log Message:
- use modern timer functions to handler fractional wait
- add a function to print the full command line
- use sigaction so signals interrupt us


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/usr.bin/flock/flock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.c
diff -u src/usr.bin/flock/flock.c:1.4 src/usr.bin/flock/flock.c:1.5
--- src/usr.bin/flock/flock.c:1.4	Thu Nov  1 22:07:19 2012
+++ src/usr.bin/flock/flock.c	Fri Nov  2 08:47:23 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: flock.c,v 1.4 2012/11/02 02:07:19 wiz Exp $	*/
+/*	$NetBSD: flock.c,v 1.5 2012/11/02 12:47:23 christos Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include sys/cdefs.h
-__RCSID($NetBSD: flock.c,v 1.4 2012/11/02 02:07:19 wiz Exp $);
+__RCSID($NetBSD: flock.c,v 1.5 2012/11/02 12:47:23 christos Exp $);
 
 #include stdio.h
 #include string.h
@@ -43,6 +43,7 @@ __RCSID($NetBSD: flock.c,v 1.4 2012/11/
 #include errno.h
 #include getopt.h
 #include paths.h
+#include time.h
 
 struct option flock_longopts[] = {
 	{ debug,		no_argument,		0, 'd' },
@@ -65,9 +66,9 @@ static sig_atomic_t timeout_expired;
 
 static __dead void usage(void) 
 {
-	fprintf(stderr, Usage: %s [-dnosvx] [-w timeout] lockfile|lockdir [-c command]| 
-	command ...\n\t%s [-dnsuvx] [-w timeout] lockfd\n, getprogname(),
-	getprogname());
+	fprintf(stderr, Usage: %s [-dnosvx] [-w timeout] lockfile|lockdir 
+	[-c command]|command ...\n\t%s [-dnsuvx] [-w timeout] lockfd\n,
+	getprogname(), getprogname());
 	exit(EXIT_FAILURE);
 }
 
@@ -105,6 +106,21 @@ lock2name(int l)
 	}
 }
 
+static char *
+cmdline(char **av)
+{
+	char *v = NULL;
+	while (*av)
+		if (v) {
+			if (asprintf(v, %s %s, v, *av++)  0)
+err(EXIT_FAILURE, malloc);
+		} else {
+			if ((v = strdup(*av++)) == NULL)
+err(EXIT_FAILURE, strdup);
+		}
+	return v;
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -117,11 +133,12 @@ main(int argc, char *argv[])
 	char *mcargv[] = {
 	__UNCONST(_PATH_BSHELL), __UNCONST(-c), NULL, NULL
 	};
-	char **cmdargv = NULL;
+	char **cmdargv = NULL, *v;
+	timer_t tm;
 
 	setprogname(argv[0]);
 
-	while ((c = getopt_long(argc, argv, +dnosuw:x, flock_longopts, NULL))
+	while ((c = getopt_long(argc, argv, +dnosuvw:x, flock_longopts, NULL))
 	!= -1)
 		switch (c) {
 		case 'd':
@@ -182,17 +199,43 @@ main(int argc, char *argv[])
 			(fd = open(argv[0], O_RDWR|O_CREAT, 0600)) == -1)
 err(EXIT_FAILURE, Cannot open `%s', argv[0]);
 		}
-		if (debug)
+		if (debug) {
 			fprintf(stderr, file %s lock %s command %s ...\n,
-			argv[0], lock2name(lock), cmdargv[0]);
+			argv[0], lock2name(lock), v = cmdline(cmdargv));
+			free(v);
+		}
 		break;
 	}
 
 	if (timeout) {
-		signal(SIGALRM, sigalrm);
-		alarm((int)timeout);	// XXX: User timer_create()
+		struct sigevent ev;
+		struct itimerspec it;
+		struct sigaction sa;
+
+		timespecclear(it.it_interval);
+		it.it_value.tv_sec = timeout;
+		it.it_value.tv_nsec = (timeout - it.it_value.tv_sec) *
+			10;
+
+		memset(ev, 0, sizeof(ev));
+		ev.sigev_notify = SIGEV_SIGNAL;
+		ev.sigev_signo = SIGALRM;
+
+		if (timer_create(CLOCK_REALTIME, ev, tm) == -1)
+			err(EXIT_FAILURE, timer_create);
+
+		if (timer_settime(tm, TIMER_RELTIME, it, NULL) == -1)
+			err(EXIT_FAILURE, timer_settime);
+
+		memset(sa, 0, sizeof(sa));
+		sa.sa_handler = sigalrm;
+		sigemptyset(sa.sa_mask);
+		sa.sa_flags = 0;
+		if (sigaction(SIGALRM, sa, NULL) == -1)
+			err(EXIT_FAILURE, sigaction);
+
 		if (debug)
-			fprintf(stderr, alarm %d\n, (int)timeout);
+			fprintf(stderr, alarm %g\n, timeout);
 	}
 
 	while (flock(fd, lock) == -1) {
@@ -205,14 +248,15 @@ main(int argc, char *argv[])
 	}
 
 	if (timeout)
-		alarm(0);
+		timer_delete(tm);
 
 	if (cls)
 		(void)close(fd);
 
 	if (cmdargv != NULL) {
 		execvp(cmdargv[0], cmdargv);
-		err(EXIT_FAILURE, execvp '%s', cmdargv[0]);
+		err(EXIT_FAILURE, execvp '%s', v = cmdline(cmdargv));
+		free(v);
 	}
 	return 0;
 }



CVS commit: src/usr.bin/flock

2012-11-02 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Nov  2 12:47:57 UTC 2012

Modified Files:
src/usr.bin/flock: flock.1

Log Message:
we don't truncate seconds anymore.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/usr.bin/flock/flock.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.1
diff -u src/usr.bin/flock/flock.1:1.5 src/usr.bin/flock/flock.1:1.6
--- src/usr.bin/flock/flock.1:1.5	Thu Nov  1 22:07:19 2012
+++ src/usr.bin/flock/flock.1	Fri Nov  2 08:47:57 2012
@@ -1,4 +1,4 @@
-.\	$NetBSD: flock.1,v 1.5 2012/11/02 02:07:19 wiz Exp $
+.\	$NetBSD: flock.1,v 1.6 2012/11/02 12:47:57 christos Exp $
 .\
 .\ Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -85,7 +85,6 @@ On error print an explanation of the fai
 .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 x , Fl Fl exclusive
 Obtain an exclusive lock.
 .El



CVS commit: src/usr.bin/flock

2012-11-02 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Nov  2 16:32:36 UTC 2012

Modified Files:
src/usr.bin/flock: flock.1

Log Message:
cross reference shlock(1)


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/usr.bin/flock/flock.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.1
diff -u src/usr.bin/flock/flock.1:1.6 src/usr.bin/flock/flock.1:1.7
--- src/usr.bin/flock/flock.1:1.6	Fri Nov  2 08:47:57 2012
+++ src/usr.bin/flock/flock.1	Fri Nov  2 12:32:35 2012
@@ -1,4 +1,4 @@
-.\	$NetBSD: flock.1,v 1.6 2012/11/02 12:47:57 christos Exp $
+.\	$NetBSD: flock.1,v 1.7 2012/11/02 16:32:35 christos Exp $
 .\
 .\ Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -28,7 +28,7 @@
 .\ POSSIBILITY OF SUCH DAMAGE.
 .\
 .\
-.Dd November 1, 2012
+.Dd November 2, 2012
 .Dt FLOCK 1
 .Os
 .Sh NAME
@@ -91,6 +91,7 @@ Obtain an exclusive lock.
 .Sh EXIT STATUS
 .Ex -std
 .Sh SEE ALSO
+.Xr shlock 1
 .Xr flock 2
 .Sh HISTORY
 An



CVS commit: src/usr.bin/flock

2012-11-02 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Nov  2 17:03:16 UTC 2012

Modified Files:
src/usr.bin/flock: flock.c

Log Message:
- better usage messages
- verbose can be local
- add static


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/usr.bin/flock/flock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.c
diff -u src/usr.bin/flock/flock.c:1.5 src/usr.bin/flock/flock.c:1.6
--- src/usr.bin/flock/flock.c:1.5	Fri Nov  2 08:47:23 2012
+++ src/usr.bin/flock/flock.c	Fri Nov  2 13:03:16 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: flock.c,v 1.5 2012/11/02 12:47:23 christos Exp $	*/
+/*	$NetBSD: flock.c,v 1.6 2012/11/02 17:03:16 christos Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include sys/cdefs.h
-__RCSID($NetBSD: flock.c,v 1.5 2012/11/02 12:47:23 christos Exp $);
+__RCSID($NetBSD: flock.c,v 1.6 2012/11/02 17:03:16 christos Exp $);
 
 #include stdio.h
 #include string.h
@@ -45,7 +45,7 @@ __RCSID($NetBSD: flock.c,v 1.5 2012/11/
 #include paths.h
 #include time.h
 
-struct option flock_longopts[] = {
+static struct option flock_longopts[] = {
 	{ debug,		no_argument,		0, 'd' },
 	{ help,		no_argument,		0, 'h' },
 	{ nonblock,		no_argument,		0, 'n' },
@@ -61,18 +61,27 @@ struct option flock_longopts[] = {
 	{ NULL,			0,			0, 0   },
 };
 
-static int verbose = 0;
 static sig_atomic_t timeout_expired;
 
-static __dead void usage(void) 
+static __dead void
+usage(const char *fmt, ...) 
 {
+	if (fmt) {
+		va_list ap;
+		va_start(ap, fmt);
+		fprintf(stderr, %s: , getprogname());
+		vfprintf(stderr, fmt, ap);
+		fputc('\n', stderr);
+		va_end(ap);
+	}
+
 	fprintf(stderr, Usage: %s [-dnosvx] [-w timeout] lockfile|lockdir 
 	[-c command]|command ...\n\t%s [-dnsuvx] [-w timeout] lockfd\n,
 	getprogname(), getprogname());
 	exit(EXIT_FAILURE);
 }
 
-static __dead void
+static void
 sigalrm(int sig)
 {
 	timeout_expired++;
@@ -106,6 +115,21 @@ lock2name(int l)
 	}
 }
 
+static char
+lockchar(int l)
+{
+	switch (l  ~LOCK_NB) {
+	case LOCK_SH:
+		return 's';
+	case LOCK_EX:
+		return 'x';
+	case LOCK_UN:
+		return 'u';
+	default:
+		return '*';
+	}
+}
+
 static char *
 cmdline(char **av)
 {
@@ -130,6 +154,7 @@ main(int argc, char *argv[])
 	int cls = 0;
 	int fd = -1;
 	int debug = 0;
+	int verbose = 0;
 	char *mcargv[] = {
 	__UNCONST(_PATH_BSHELL), __UNCONST(-c), NULL, NULL
 	};
@@ -145,16 +170,22 @@ main(int argc, char *argv[])
 			debug++;
 			break;
 		case 'x':
-			lock = LOCK_EX | (lock  ~LOCK_NB);
+			if (lock  ~LOCK_NB)
+goto badlock;
+			lock |= LOCK_EX;
 			break;
 		case 'n':
 			lock |= LOCK_NB;
 			break;
 		case 's':
-			lock = LOCK_SH | (lock  ~LOCK_NB);
+			if (lock  ~LOCK_NB)
+goto badlock;
+			lock |= LOCK_SH;
 			break;
 		case 'u':
-			lock = LOCK_UN | (lock  ~LOCK_NB);
+			if (lock  ~LOCK_NB)
+goto badlock;
+			lock |= LOCK_UN;
 			break;
 		case 'w':
 			timeout = strtod(optarg, NULL);
@@ -166,7 +197,9 @@ main(int argc, char *argv[])
 			cls = 1;
 			break;
 		default:
-			usage();
+			usage(Invalid option '%c', c);
+		badlock:
+			usage(-%c can't be used with -%c, c, lockchar(lock));
 		}
 
 	argc -= optind;
@@ -174,21 +207,22 @@ main(int argc, char *argv[])
 
 	switch (argc) {
 	case 0:
-		usage();
+		usage(Missing lock file argument);
 	case 1:
 		if (cls)
-			usage();
+			usage(Close is valid only for descriptors);
 		fd = strtol(argv[0], NULL, 0);	// XXX: error checking
 		if (debug)
 			fprintf(stderr, descriptor %s lock %s\n,
 			argv[0], lock2name(lock));
-		if (lock == LOCK_UN)
-			usage();
 	default:
+		if ((lock  LOCK_NB) == LOCK_UN)
+			usage(Unlock is only valid for descriptors);
 		if (strcmp(argv[1], -c) == 0 ||
 		strcmp(argv[1], --command) == 0) {
 			if (argc == 2)
-usage();
+usage(Missing argument to %s, strcmp(argv[1],
+-c) == 0 ? -c : --command);
 			mcargv[2] = argv[2];
 			cmdargv = mcargv;
 		} else



CVS commit: src/usr.bin/flock

2012-11-02 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Sat Nov  3 00:50:05 UTC 2012

Modified Files:
src/usr.bin/flock: flock.1

Log Message:
Separate SEE ALSO entries with comma.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/usr.bin/flock/flock.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.1
diff -u src/usr.bin/flock/flock.1:1.7 src/usr.bin/flock/flock.1:1.8
--- src/usr.bin/flock/flock.1:1.7	Fri Nov  2 16:32:35 2012
+++ src/usr.bin/flock/flock.1	Sat Nov  3 00:50:04 2012
@@ -1,4 +1,4 @@
-.\	$NetBSD: flock.1,v 1.7 2012/11/02 16:32:35 christos Exp $
+.\	$NetBSD: flock.1,v 1.8 2012/11/03 00:50:04 wiz Exp $
 .\
 .\ Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -91,7 +91,7 @@ Obtain an exclusive lock.
 .Sh EXIT STATUS
 .Ex -std
 .Sh SEE ALSO
-.Xr shlock 1
+.Xr shlock 1 ,
 .Xr flock 2
 .Sh HISTORY
 An



CVS commit: src/usr.bin/flock

2012-11-01 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Nov  1 23:34:44 UTC 2012

Modified Files:
src/usr.bin/flock: flock.1

Log Message:
fix usage


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/usr.bin/flock/flock.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.1
diff -u src/usr.bin/flock/flock.1:1.1 src/usr.bin/flock/flock.1:1.2
--- src/usr.bin/flock/flock.1:1.1	Thu Nov  1 19:30:19 2012
+++ src/usr.bin/flock/flock.1	Thu Nov  1 19:34:44 2012
@@ -1,4 +1,4 @@
-.\	$NetBSD: flock.1,v 1.1 2012/11/01 23:30:19 christos Exp $
+.\	$NetBSD: flock.1,v 1.2 2012/11/01 23:34:44 christos Exp $
 .\
 .\ Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -39,7 +39,7 @@
 .Op Fl dnosxv
 .Op Fl w Ar timeout
 .Ar lockfile|lockdir
-.Op Ar c
+.Op Fl c
 .Ar command
 .Nm
 .Op Fl dnusxv



CVS commit: src/usr.bin/flock

2012-11-01 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Nov  2 01:30:47 UTC 2012

Modified Files:
src/usr.bin/flock: flock.1 flock.c

Log Message:
treat a command without -c like an argument vector
default to exclusive lock


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/flock/flock.1
cvs rdiff -u -r1.1 -r1.2 src/usr.bin/flock/flock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.1
diff -u src/usr.bin/flock/flock.1:1.2 src/usr.bin/flock/flock.1:1.3
--- src/usr.bin/flock/flock.1:1.2	Thu Nov  1 19:34:44 2012
+++ src/usr.bin/flock/flock.1	Thu Nov  1 21:30:46 2012
@@ -1,4 +1,4 @@
-.\	$NetBSD: flock.1,v 1.2 2012/11/01 23:34:44 christos Exp $
+.\	$NetBSD: flock.1,v 1.3 2012/11/02 01:30:46 christos Exp $
 .\
 .\ Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -39,8 +39,9 @@
 .Op Fl dnosxv
 .Op Fl w Ar timeout
 .Ar lockfile|lockdir
-.Op Fl c
-.Ar command
+.Op Fl c Ar command
+|
+.Op Ar command ...
 .Nm
 .Op Fl dnusxv
 .Op Fl w Ar timeout

Index: src/usr.bin/flock/flock.c
diff -u src/usr.bin/flock/flock.c:1.1 src/usr.bin/flock/flock.c:1.2
--- src/usr.bin/flock/flock.c:1.1	Thu Nov  1 19:30:19 2012
+++ src/usr.bin/flock/flock.c	Thu Nov  1 21:30:46 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: flock.c,v 1.1 2012/11/01 23:30:19 christos Exp $	*/
+/*	$NetBSD: flock.c,v 1.2 2012/11/02 01:30:46 christos Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include sys/cdefs.h
-__RCSID($NetBSD: flock.c,v 1.1 2012/11/01 23:30:19 christos Exp $);
+__RCSID($NetBSD: flock.c,v 1.2 2012/11/02 01:30:46 christos Exp $);
 
 #include stdio.h
 #include string.h
@@ -61,6 +61,7 @@ struct option flock_longopts[] = {
 };
 
 static int verbose = 0;
+static sig_atomic_t timeout_expired;
 
 static __dead void usage(void) 
 {
@@ -73,11 +74,7 @@ static __dead void usage(void) 
 static __dead void
 sigalrm(int sig)
 {
-	if (verbose) {
-		errno = ETIMEDOUT;
-		errx(EXIT_FAILURE, );
-	} else
-		exit(EXIT_FAILURE);
+	timeout_expired++;
 }
 
 static const char *
@@ -112,36 +109,36 @@ int
 main(int argc, char *argv[])
 {
 	int c;
-	int lock = 0;
+	int lock = LOCK_EX;
 	double timeout = 0;
 	int cls = 0;
 	int fd = -1;
 	int debug = 0;
-	char *command = NULL;
+	char *mcargv[] = {
+	__UNCONST(_PATH_BSHELL), __UNCONST(-c), NULL, NULL
+	};
+	char **cmdargv = NULL;
 
 	setprogname(argv[0]);
 
-	while ((c = getopt_long(argc, argv, c:dnosxuw:, flock_longopts, NULL))
+	while ((c = getopt_long(argc, argv, +dnosxuw:, flock_longopts, NULL))
 	!= -1)
 		switch (c) {
-		case 'c':
-			command = optarg;
-			break;
 		case 'd':
 			debug++;
 			break;
 		case 'e':
 		case 'x':
-			lock |= LOCK_EX;
+			lock = LOCK_EX | (lock  ~LOCK_NB);
 			break;
 		case 'n':
 			lock |= LOCK_NB;
 			break;
 		case 's':
-			lock |= LOCK_SH;
+			lock = LOCK_SH | (lock  ~LOCK_NB);
 			break;
 		case 'u':
-			lock |= LOCK_UN;
+			lock = LOCK_UN | (lock  ~LOCK_NB);
 			break;
 		case 'w':
 			timeout = strtod(optarg, NULL);
@@ -159,26 +156,37 @@ main(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	if (command) {
+	switch (argc) {
+	case 0:
+		usage();
+	case 1:
+		if (cls)
+			usage();
+		fd = strtol(argv[0], NULL, 0);	// XXX: error checking
+		if (debug)
+			fprintf(stderr, descriptor %s lock %s\n,
+			argv[0], lock2name(lock));
 		if (lock == LOCK_UN)
 			usage();
+	default:
+		if (strcmp(argv[1], -c) == 0 ||
+		strcmp(argv[1], --command) == 0) {
+			if (argc == 2)
+usage();
+			mcargv[2] = argv[2];
+			cmdargv = mcargv;
+		} else
+			cmdargv = argv + 1;
+			
 		if ((fd = open(argv[0], O_RDONLY)) == -1) {
 			if (errno != ENOENT || 
 			(fd = open(argv[0], O_RDWR|O_CREAT, 0600)) == -1)
 err(EXIT_FAILURE, Cannot open `%s', argv[0]);
 		}
 		if (debug)
-			fprintf(stderr, file %s lock %s command %s\n,
-			argv[0], lock2name(lock), command);
-
-			
-	} else {
-		if (cls)
-			usage();
-		fd = strtol(argv[0], NULL, 0);	// XXX: error checking
-		if (debug)
-			fprintf(stderr, descriptor %s lock %s\n,
-			argv[0], lock2name(lock));
+			fprintf(stderr, file %s lock %s command %s ...\n,
+			argv[0], lock2name(lock), cmdargv[0]);
+		break;
 	}
 
 	if (timeout) {
@@ -188,7 +196,9 @@ main(int argc, char *argv[])
 			fprintf(stderr, alarm %d\n, (int)timeout);
 	}
 
-	if (flock(fd, lock) == -1) {
+	while (flock(fd, lock) == -1) {
+		if (errno == EINTR  timeout_expired == 0)
+			continue;
 		if (verbose)
 			err(EXIT_FAILURE, flock(%d, %s), fd, lock2name(lock));
 		else
@@ -201,11 +211,9 @@ main(int argc, char *argv[])
 	if (cls)
 		(void)close(fd);
 
-	if (command == NULL)
-		return 0;
-	if (debug)
-		fprintf(stderr, execute %s -c '%s'\n, _PATH_BSHELL, command);
-	execlp(_PATH_BSHELL, sh, -c, command, NULL);
-	err(EXIT_FAILURE, exec %s -c '%s', _PATH_BSHELL, command);
+	if (cmdargv != NULL) {
+		execvp(cmdargv[0], cmdargv);

CVS commit: src/usr.bin/flock

2012-11-01 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Fri Nov  2 02:03:18 UTC 2012

Modified Files:
src/usr.bin/flock: flock.1 flock.c

Log Message:
Sort options and their descriptions. Sync usage with man page.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/flock/flock.1
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/flock/flock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.1
diff -u src/usr.bin/flock/flock.1:1.3 src/usr.bin/flock/flock.1:1.4
--- src/usr.bin/flock/flock.1:1.3	Fri Nov  2 01:30:46 2012
+++ src/usr.bin/flock/flock.1	Fri Nov  2 02:03:18 2012
@@ -1,4 +1,4 @@
-.\	$NetBSD: flock.1,v 1.3 2012/11/02 01:30:46 christos Exp $
+.\	$NetBSD: flock.1,v 1.4 2012/11/02 02:03:18 wiz Exp $
 .\
 .\ Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -29,21 +29,20 @@
 .\
 .\
 .Dd November 1, 2012
-.Dt flock 1
+.Dt FLOCK 1
 .Os
 .Sh NAME
 .Nm flock
 .Nd Provide locking API for shell scripts
 .Sh SYNOPSIS
 .Nm
-.Op Fl dnosxv
+.Op Fl dnosvx
 .Op Fl w Ar timeout
 .Ar lockfile|lockdir
-.Op Fl c Ar command
-|
+.Op Fl c
 .Op Ar command ...
 .Nm
-.Op Fl dnusxv
+.Op Fl dnsuvx
 .Op Fl w Ar timeout
 .Ar lockfd
 .Sh DESCRIPTION
@@ -57,44 +56,44 @@ If the file or directory does not exist,
 .Pp
 The second form can use an arbitrary file descriptor that is provided from a
 shell script for example:
-.nf
+.Bd -literal
 (
 	flock -s 100
 	# commands to be executed under the lock
 ) 100 /path/to/lockfile
-.if
+.Ed
 .Pp
 The following options are available:
 .Bl -tag -width X
 .It Fl c Ar command
 Pass a command to a the shell.
-.It Fl d Fl Fl debug
+.It Fl d , Fl Fl debug
 Provide debugging output.
-.It Fl s Fl Fl shared
+.It Fl n , Fl Fl nb , Fl Fl nonblock
+Don't block and fail immediately if the lock could not be obtained.
+.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.
+.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
+.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
+.It Fl v , Fl Fl verbose
 On error print an explanation of the failure.
-.It Fl w Fl Fl wait Fl Fl timeout Ar seconds
+.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.
+.It Fl x , Fl Fl exclusive
+Obtain an exclusive lock.
 .El
 .Sh EXIT STATUS
 .Ex -std
 .Sh SEE ALSO
 .Xr flock 2
 .Sh HISTORY
-A
+An
 .Nm
 utility appeared in
-.Nx 7
+.Nx 7.0 .

Index: src/usr.bin/flock/flock.c
diff -u src/usr.bin/flock/flock.c:1.2 src/usr.bin/flock/flock.c:1.3
--- src/usr.bin/flock/flock.c:1.2	Fri Nov  2 01:30:46 2012
+++ src/usr.bin/flock/flock.c	Fri Nov  2 02:03:18 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: flock.c,v 1.2 2012/11/02 01:30:46 christos Exp $	*/
+/*	$NetBSD: flock.c,v 1.3 2012/11/02 02:03:18 wiz Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include sys/cdefs.h
-__RCSID($NetBSD: flock.c,v 1.2 2012/11/02 01:30:46 christos Exp $);
+__RCSID($NetBSD: flock.c,v 1.3 2012/11/02 02:03:18 wiz Exp $);
 
 #include stdio.h
 #include string.h
@@ -65,8 +65,8 @@ static sig_atomic_t timeout_expired;
 
 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(),
+	fprintf(stderr, Usage: %s [-dnosvx] [-w timeout] lockfile|lockdir [-c] 
+	command\n\t%s [-dnsuvx] [-w timeout] lockfd\n, getprogname(),
 	getprogname());
 	exit(EXIT_FAILURE);
 }
@@ -121,13 +121,12 @@ main(int argc, char *argv[])
 
 	setprogname(argv[0]);
 
-	while ((c = getopt_long(argc, argv, +dnosxuw:, flock_longopts, NULL))
+	while ((c = getopt_long(argc, argv, +dnosuw:x, flock_longopts, NULL))
 	!= -1)
 		switch (c) {
 		case 'd':
 			debug++;
 			break;
-		case 'e':
 		case 'x':
 			lock = LOCK_EX | (lock  ~LOCK_NB);
 			break;



CVS commit: src/usr.bin/flock

2012-11-01 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Fri Nov  2 02:07:19 UTC 2012

Modified Files:
src/usr.bin/flock: flock.1 flock.c

Log Message:
Use longer -c description.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/usr.bin/flock/flock.1
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/flock/flock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/flock/flock.1
diff -u src/usr.bin/flock/flock.1:1.4 src/usr.bin/flock/flock.1:1.5
--- src/usr.bin/flock/flock.1:1.4	Fri Nov  2 02:03:18 2012
+++ src/usr.bin/flock/flock.1	Fri Nov  2 02:07:19 2012
@@ -1,4 +1,4 @@
-.\	$NetBSD: flock.1,v 1.4 2012/11/02 02:03:18 wiz Exp $
+.\	$NetBSD: flock.1,v 1.5 2012/11/02 02:07:19 wiz Exp $
 .\
 .\ Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -39,7 +39,8 @@
 .Op Fl dnosvx
 .Op Fl w Ar timeout
 .Ar lockfile|lockdir
-.Op Fl c
+.Op Fl c Ar command
+|
 .Op Ar command ...
 .Nm
 .Op Fl dnsuvx

Index: src/usr.bin/flock/flock.c
diff -u src/usr.bin/flock/flock.c:1.3 src/usr.bin/flock/flock.c:1.4
--- src/usr.bin/flock/flock.c:1.3	Fri Nov  2 02:03:18 2012
+++ src/usr.bin/flock/flock.c	Fri Nov  2 02:07:19 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: flock.c,v 1.3 2012/11/02 02:03:18 wiz Exp $	*/
+/*	$NetBSD: flock.c,v 1.4 2012/11/02 02:07:19 wiz Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include sys/cdefs.h
-__RCSID($NetBSD: flock.c,v 1.3 2012/11/02 02:03:18 wiz Exp $);
+__RCSID($NetBSD: flock.c,v 1.4 2012/11/02 02:07:19 wiz Exp $);
 
 #include stdio.h
 #include string.h
@@ -65,8 +65,8 @@ static sig_atomic_t timeout_expired;
 
 static __dead void usage(void) 
 {
-	fprintf(stderr, Usage: %s [-dnosvx] [-w timeout] lockfile|lockdir [-c] 
-	command\n\t%s [-dnsuvx] [-w timeout] lockfd\n, getprogname(),
+	fprintf(stderr, Usage: %s [-dnosvx] [-w timeout] lockfile|lockdir [-c command]| 
+	command ...\n\t%s [-dnsuvx] [-w timeout] lockfd\n, getprogname(),
 	getprogname());
 	exit(EXIT_FAILURE);
 }