Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/pwait Add a timeout parameter.



details:   https://anonhg.NetBSD.org/src/rev/9d085748a5f2
branches:  trunk
changeset: 336496:9d085748a5f2
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Mar 03 19:59:48 2015 +0000

description:
Add a timeout parameter.

diffstat:

 usr.bin/pwait/pwait.1 |  13 ++++++++++---
 usr.bin/pwait/pwait.c |  48 +++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 51 insertions(+), 10 deletions(-)

diffs (140 lines):

diff -r aafe32306693 -r 9d085748a5f2 usr.bin/pwait/pwait.1
--- a/usr.bin/pwait/pwait.1     Tue Mar 03 18:04:33 2015 +0000
+++ b/usr.bin/pwait/pwait.1     Tue Mar 03 19:59:48 2015 +0000
@@ -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 @@
 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
diff -r aafe32306693 -r 9d085748a5f2 usr.bin/pwait/pwait.c
--- a/usr.bin/pwait/pwait.c     Tue Mar 03 18:04:33 2015 +0000
+++ b/usr.bin/pwait/pwait.c     Tue Mar 03 19:59:48 2015 +0000
@@ -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 @@
 #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 @@
        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 @@
        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 @@
        }
 
        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) {



Home | Main Index | Thread Index | Old Index