Home | History | Annotate | Line # | Download | only in pwait
      1  1.6  dholland /*	$NetBSD: pwait.c,v 1.6 2017/07/30 20:37:35 dholland Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2004-2009, Jilles Tjoelker
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with
      8  1.1  christos  * or without modification, are permitted provided that the
      9  1.1  christos  * following conditions are met:
     10  1.1  christos  *
     11  1.1  christos  * 1. Redistributions of source code must retain the above
     12  1.1  christos  *    copyright notice, this list of conditions and the
     13  1.1  christos  *    following disclaimer.
     14  1.1  christos  * 2. Redistributions in binary form must reproduce the
     15  1.1  christos  *    above copyright notice, this list of conditions and
     16  1.1  christos  *    the following disclaimer in the documentation and/or
     17  1.1  christos  *    other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     20  1.1  christos  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
     21  1.1  christos  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22  1.1  christos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23  1.1  christos  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.1  christos  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
     25  1.1  christos  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  1.1  christos  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28  1.1  christos  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     29  1.1  christos  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     31  1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     32  1.1  christos  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     33  1.1  christos  * OF SUCH DAMAGE.
     34  1.1  christos  */
     35  1.1  christos 
     36  1.1  christos #include <sys/cdefs.h>
     37  1.1  christos #ifdef __FBSDID
     38  1.1  christos __FBSDID("$FreeBSD: head/bin/pwait/pwait.c 245506 2013-01-16 18:15:25Z delphij $");
     39  1.1  christos #endif
     40  1.6  dholland __RCSID("$NetBSD: pwait.c,v 1.6 2017/07/30 20:37:35 dholland Exp $");
     41  1.1  christos 
     42  1.1  christos #include <sys/types.h>
     43  1.1  christos #include <sys/event.h>
     44  1.1  christos #include <sys/time.h>
     45  1.1  christos #include <sys/wait.h>
     46  1.1  christos 
     47  1.1  christos #include <err.h>
     48  1.1  christos #include <errno.h>
     49  1.1  christos #include <fcntl.h>
     50  1.1  christos #include <signal.h>
     51  1.1  christos #include <stdio.h>
     52  1.1  christos #include <stdlib.h>
     53  1.1  christos #include <string.h>
     54  1.1  christos #include <sysexits.h>
     55  1.1  christos #include <unistd.h>
     56  1.6  dholland #include <assert.h>
     57  1.1  christos 
     58  1.3  christos static __dead void
     59  1.1  christos usage(void)
     60  1.1  christos {
     61  1.1  christos 
     62  1.5  christos 	fprintf(stderr, "Usage: %s [-isv] [-t <timeout>] <pid> ...\n",
     63  1.3  christos 	    getprogname());
     64  1.1  christos 	exit(EX_USAGE);
     65  1.1  christos }
     66  1.1  christos 
     67  1.1  christos /*
     68  1.1  christos  * pwait - wait for processes to terminate
     69  1.1  christos  */
     70  1.1  christos int
     71  1.1  christos main(int argc, char *argv[])
     72  1.1  christos {
     73  1.1  christos 	int kq;
     74  1.1  christos 	struct kevent *e;
     75  1.2  christos 	int verbose = 0, childstatus = 0;
     76  1.5  christos 	int opt, duplicate, status, immediately = 0;
     77  1.2  christos 	size_t nleft, n, i;
     78  1.2  christos 	pid_t pid;
     79  1.1  christos 	char *s, *end;
     80  1.3  christos 	double timeout = 0;
     81  1.3  christos 	struct timespec ts, *tsp;
     82  1.1  christos 
     83  1.3  christos 	setprogname(argv[0]);
     84  1.5  christos 	while ((opt = getopt(argc, argv, "ist:v")) != -1) {
     85  1.1  christos 		switch (opt) {
     86  1.5  christos 		case 'i':
     87  1.5  christos 			immediately = 1;
     88  1.5  christos 			break;
     89  1.2  christos 		case 's':
     90  1.2  christos 			childstatus = 1;
     91  1.2  christos 			break;
     92  1.3  christos 		case 't':
     93  1.3  christos 			timeout = atof(optarg);
     94  1.3  christos 			if (timeout < 0)
     95  1.3  christos 				timeout = 0;
     96  1.3  christos 			break;
     97  1.1  christos 		case 'v':
     98  1.1  christos 			verbose = 1;
     99  1.1  christos 			break;
    100  1.1  christos 		default:
    101  1.1  christos 			usage();
    102  1.1  christos 			/* NOTREACHED */
    103  1.1  christos 		}
    104  1.1  christos 	}
    105  1.1  christos 
    106  1.1  christos 	argc -= optind;
    107  1.1  christos 	argv += optind;
    108  1.1  christos 
    109  1.1  christos 	if (argc == 0)
    110  1.1  christos 		usage();
    111  1.1  christos 
    112  1.3  christos 	if (timeout != 0) {
    113  1.3  christos 		ts.tv_sec = (time_t)timeout;
    114  1.3  christos 		timeout -= (double)ts.tv_sec;
    115  1.3  christos 		ts.tv_nsec = (long)(timeout * 1000000000L);
    116  1.3  christos 		while (ts.tv_nsec < 0) {
    117  1.3  christos 			ts.tv_sec--;
    118  1.3  christos 			ts.tv_nsec += 1000000000L;
    119  1.3  christos 		}
    120  1.3  christos 		tsp = &ts;
    121  1.3  christos 	} else
    122  1.3  christos 		tsp = NULL;
    123  1.3  christos 
    124  1.1  christos 	kq = kqueue();
    125  1.1  christos 	if (kq == -1)
    126  1.2  christos 		err(EXIT_FAILURE, "kqueue");
    127  1.1  christos 
    128  1.2  christos 	e = malloc((size_t)argc * sizeof(*e));
    129  1.1  christos 	if (e == NULL)
    130  1.2  christos 		err(EXIT_FAILURE, "malloc");
    131  1.1  christos 	nleft = 0;
    132  1.2  christos 	for (n = 0; n < (size_t)argc; n++) {
    133  1.2  christos 		long pidl;
    134  1.1  christos 		s = argv[n];
    135  1.1  christos 		if (!strncmp(s, "/proc/", 6)) /* Undocumented Solaris compat */
    136  1.1  christos 			s += 6;
    137  1.1  christos 		errno = 0;
    138  1.2  christos 		pidl = strtol(s, &end, 10);
    139  1.2  christos 		if (pidl < 0 || *end != '\0' || errno != 0) {
    140  1.1  christos 			warnx("%s: bad process id", s);
    141  1.1  christos 			continue;
    142  1.1  christos 		}
    143  1.2  christos 		pid = (pid_t)pidl;
    144  1.1  christos 		duplicate = 0;
    145  1.1  christos 		for (i = 0; i < nleft; i++)
    146  1.1  christos 			if (e[i].ident == (uintptr_t)pid)
    147  1.1  christos 				duplicate = 1;
    148  1.1  christos 		if (!duplicate) {
    149  1.2  christos 			EV_SET(e + nleft, (uintptr_t)pid, EVFILT_PROC, EV_ADD,
    150  1.4  christos 			    NOTE_EXIT, 0, 0);
    151  1.1  christos 			if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
    152  1.2  christos 				warn("%jd", (intmax_t)pid);
    153  1.1  christos 			else
    154  1.1  christos 				nleft++;
    155  1.1  christos 		}
    156  1.1  christos 	}
    157  1.1  christos 
    158  1.1  christos 	while (nleft > 0) {
    159  1.3  christos 		int rv;
    160  1.3  christos 
    161  1.3  christos 		switch (rv = kevent(kq, NULL, 0, e, nleft, tsp)) {
    162  1.3  christos 		case 0:
    163  1.3  christos 			if (verbose)
    164  1.3  christos 				printf("timed out\n");
    165  1.3  christos 			if (childstatus)
    166  1.3  christos 				return 255;
    167  1.3  christos 			return EX_OK;
    168  1.3  christos 		case -1:
    169  1.2  christos 			err(EXIT_FAILURE, "kevent");
    170  1.3  christos 		default:
    171  1.3  christos 			n = (size_t)rv;
    172  1.3  christos 			break;
    173  1.3  christos 		}
    174  1.3  christos 
    175  1.6  dholland 		assert(n > 0);
    176  1.2  christos 		for (i = 0; i < n; i++) {
    177  1.2  christos 			status = (int)e[i].data;
    178  1.2  christos 			if (verbose) {
    179  1.1  christos 				if (WIFEXITED(status))
    180  1.1  christos 					printf("%ld: exited with status %d.\n",
    181  1.1  christos 					    (long)e[i].ident,
    182  1.1  christos 					    WEXITSTATUS(status));
    183  1.1  christos 				else if (WIFSIGNALED(status))
    184  1.1  christos 					printf("%ld: killed by signal %d.\n",
    185  1.1  christos 					    (long)e[i].ident,
    186  1.1  christos 					    WTERMSIG(status));
    187  1.1  christos 				else
    188  1.1  christos 					printf("%ld: terminated.\n",
    189  1.1  christos 					    (long)e[i].ident);
    190  1.1  christos 			}
    191  1.2  christos 			if (childstatus)
    192  1.2  christos 				return status;
    193  1.2  christos 		}
    194  1.1  christos 		nleft -= n;
    195  1.6  dholland 		if (immediately)
    196  1.5  christos 			break;
    197  1.1  christos 	}
    198  1.1  christos 
    199  1.2  christos 	return EX_OK;
    200  1.1  christos }
    201