Module Name:    src
Committed By:   christos
Date:           Tue Mar  3 19:59:49 UTC 2015

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

Log Message:
Add a timeout parameter.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/pwait/pwait.1 src/usr.bin/pwait/pwait.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/pwait/pwait.1
diff -u src/usr.bin/pwait/pwait.1:1.2 src/usr.bin/pwait/pwait.1:1.3
--- src/usr.bin/pwait/pwait.1:1.2	Mon Mar  2 16:53:48 2015
+++ src/usr.bin/pwait/pwait.1	Tue Mar  3 14:59:48 2015
@@ -1,4 +1,4 @@
-.\"	$NetBSD: pwait.1,v 1.2 2015/03/02 21:53:48 christos Exp $
+.\"	$NetBSD: pwait.1,v 1.3 2015/03/03 19:59:48 christos Exp $
 .\"
 .\" Copyright (c) 2004-2009, Jilles Tjoelker
 .\" All rights reserved.
@@ -33,7 +33,7 @@
 .\"
 .\" $FreeBSD: head/bin/pwait/pwait.1 233648 2012-03-29 05:02:12Z eadler $
 .\"
-.Dd March 2, 2015
+.Dd March 3, 2015
 .Dt PWAIT 1
 .Os
 .Sh NAME
@@ -42,6 +42,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl sv
+.Op Fl t Ar timeout
 .Ar pid
 \&...
 .Sh DESCRIPTION
@@ -52,7 +53,13 @@ utility will wait until each of the give
 The following option is available:
 .Bl -tag -width indent
 .It Fl s
-Exit with the status code of the first non-zero exit status pid.
+Exit with the status code of the first non-zero exit status pid, or
+if timed out exit with
+.Dv 255 .
+.It Fl t Ar timeout
+Only wait for
+.Ar timeout
+seconds before exiting.
 .It Fl v
 Print the exit status when each process terminates.
 .El
Index: src/usr.bin/pwait/pwait.c
diff -u src/usr.bin/pwait/pwait.c:1.2 src/usr.bin/pwait/pwait.c:1.3
--- src/usr.bin/pwait/pwait.c:1.2	Mon Mar  2 16:53:48 2015
+++ src/usr.bin/pwait/pwait.c	Tue Mar  3 14:59:48 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: pwait.c,v 1.2 2015/03/02 21:53:48 christos Exp $	*/
+/*	$NetBSD: pwait.c,v 1.3 2015/03/03 19:59:48 christos Exp $	*/
 
 /*-
  * Copyright (c) 2004-2009, Jilles Tjoelker
@@ -37,7 +37,7 @@
 #ifdef __FBSDID
 __FBSDID("$FreeBSD: head/bin/pwait/pwait.c 245506 2013-01-16 18:15:25Z delphij $");
 #endif
-__RCSID("$NetBSD: pwait.c,v 1.2 2015/03/02 21:53:48 christos Exp $");
+__RCSID("$NetBSD: pwait.c,v 1.3 2015/03/03 19:59:48 christos Exp $");
 
 #include <sys/types.h>
 #include <sys/event.h>
@@ -54,11 +54,12 @@ __RCSID("$NetBSD: pwait.c,v 1.2 2015/03/
 #include <sysexits.h>
 #include <unistd.h>
 
-static void
+static __dead void
 usage(void)
 {
 
-	fprintf(stderr, "usage: pwait [-v] pid ...\n");
+	fprintf(stderr, "Usage: %s [-sv] [-t <timeout>] <pid> ...\n",
+	    getprogname());
 	exit(EX_USAGE);
 }
 
@@ -75,12 +76,20 @@ main(int argc, char *argv[])
 	size_t nleft, n, i;
 	pid_t pid;
 	char *s, *end;
+	double timeout = 0;
+	struct timespec ts, *tsp;
 
-	while ((opt = getopt(argc, argv, "sv")) != -1) {
+	setprogname(argv[0]);
+	while ((opt = getopt(argc, argv, "st:v")) != -1) {
 		switch (opt) {
 		case 's':
 			childstatus = 1;
 			break;
+		case 't':
+			timeout = atof(optarg);
+			if (timeout < 0)
+				timeout = 0;
+			break;
 		case 'v':
 			verbose = 1;
 			break;
@@ -96,6 +105,18 @@ main(int argc, char *argv[])
 	if (argc == 0)
 		usage();
 
+	if (timeout != 0) {
+		ts.tv_sec = (time_t)timeout;
+		timeout -= (double)ts.tv_sec;
+		ts.tv_nsec = (long)(timeout * 1000000000L);
+		while (ts.tv_nsec < 0) {
+			ts.tv_sec--;
+			ts.tv_nsec += 1000000000L;
+		}
+		tsp = &ts;
+	} else
+		tsp = NULL;
+
 	kq = kqueue();
 	if (kq == -1)
 		err(EXIT_FAILURE, "kqueue");
@@ -131,9 +152,22 @@ main(int argc, char *argv[])
 	}
 
 	while (nleft > 0) {
-		int rv = kevent(kq, NULL, 0, e, nleft, NULL);
-		if (rv == -1)
+		int rv;
+
+		switch (rv = kevent(kq, NULL, 0, e, nleft, tsp)) {
+		case 0:
+			if (verbose)
+				printf("timed out\n");
+			if (childstatus)
+				return 255;
+			return EX_OK;
+		case -1:
 			err(EXIT_FAILURE, "kevent");
+		default:
+			n = (size_t)rv;
+			break;
+		}
+
 		for (i = 0; i < n; i++) {
 			status = (int)e[i].data;
 			if (verbose) {

Reply via email to