Home | History | Annotate | Line # | Download | only in pwait
pwait.c revision 1.3
      1  1.3  christos /*	$NetBSD: pwait.c,v 1.3 2015/03/03 19:59:48 christos 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.3  christos __RCSID("$NetBSD: pwait.c,v 1.3 2015/03/03 19:59:48 christos 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.1  christos 
     57  1.3  christos static __dead void
     58  1.1  christos usage(void)
     59  1.1  christos {
     60  1.1  christos 
     61  1.3  christos 	fprintf(stderr, "Usage: %s [-sv] [-t <timeout>] <pid> ...\n",
     62  1.3  christos 	    getprogname());
     63  1.1  christos 	exit(EX_USAGE);
     64  1.1  christos }
     65  1.1  christos 
     66  1.1  christos /*
     67  1.1  christos  * pwait - wait for processes to terminate
     68  1.1  christos  */
     69  1.1  christos int
     70  1.1  christos main(int argc, char *argv[])
     71  1.1  christos {
     72  1.1  christos 	int kq;
     73  1.1  christos 	struct kevent *e;
     74  1.2  christos 	int verbose = 0, childstatus = 0;
     75  1.2  christos 	int opt, duplicate, status;
     76  1.2  christos 	size_t nleft, n, i;
     77  1.2  christos 	pid_t pid;
     78  1.1  christos 	char *s, *end;
     79  1.3  christos 	double timeout = 0;
     80  1.3  christos 	struct timespec ts, *tsp;
     81  1.1  christos 
     82  1.3  christos 	setprogname(argv[0]);
     83  1.3  christos 	while ((opt = getopt(argc, argv, "st:v")) != -1) {
     84  1.1  christos 		switch (opt) {
     85  1.2  christos 		case 's':
     86  1.2  christos 			childstatus = 1;
     87  1.2  christos 			break;
     88  1.3  christos 		case 't':
     89  1.3  christos 			timeout = atof(optarg);
     90  1.3  christos 			if (timeout < 0)
     91  1.3  christos 				timeout = 0;
     92  1.3  christos 			break;
     93  1.1  christos 		case 'v':
     94  1.1  christos 			verbose = 1;
     95  1.1  christos 			break;
     96  1.1  christos 		default:
     97  1.1  christos 			usage();
     98  1.1  christos 			/* NOTREACHED */
     99  1.1  christos 		}
    100  1.1  christos 	}
    101  1.1  christos 
    102  1.1  christos 	argc -= optind;
    103  1.1  christos 	argv += optind;
    104  1.1  christos 
    105  1.1  christos 	if (argc == 0)
    106  1.1  christos 		usage();
    107  1.1  christos 
    108  1.3  christos 	if (timeout != 0) {
    109  1.3  christos 		ts.tv_sec = (time_t)timeout;
    110  1.3  christos 		timeout -= (double)ts.tv_sec;
    111  1.3  christos 		ts.tv_nsec = (long)(timeout * 1000000000L);
    112  1.3  christos 		while (ts.tv_nsec < 0) {
    113  1.3  christos 			ts.tv_sec--;
    114  1.3  christos 			ts.tv_nsec += 1000000000L;
    115  1.3  christos 		}
    116  1.3  christos 		tsp = &ts;
    117  1.3  christos 	} else
    118  1.3  christos 		tsp = NULL;
    119  1.3  christos 
    120  1.1  christos 	kq = kqueue();
    121  1.1  christos 	if (kq == -1)
    122  1.2  christos 		err(EXIT_FAILURE, "kqueue");
    123  1.1  christos 
    124  1.2  christos 	e = malloc((size_t)argc * sizeof(*e));
    125  1.1  christos 	if (e == NULL)
    126  1.2  christos 		err(EXIT_FAILURE, "malloc");
    127  1.1  christos 	nleft = 0;
    128  1.2  christos 	for (n = 0; n < (size_t)argc; n++) {
    129  1.2  christos 		long pidl;
    130  1.1  christos 		s = argv[n];
    131  1.1  christos 		if (!strncmp(s, "/proc/", 6)) /* Undocumented Solaris compat */
    132  1.1  christos 			s += 6;
    133  1.1  christos 		errno = 0;
    134  1.2  christos 		pidl = strtol(s, &end, 10);
    135  1.2  christos 		if (pidl < 0 || *end != '\0' || errno != 0) {
    136  1.1  christos 			warnx("%s: bad process id", s);
    137  1.1  christos 			continue;
    138  1.1  christos 		}
    139  1.2  christos 		pid = (pid_t)pidl;
    140  1.1  christos 		duplicate = 0;
    141  1.1  christos 		for (i = 0; i < nleft; i++)
    142  1.1  christos 			if (e[i].ident == (uintptr_t)pid)
    143  1.1  christos 				duplicate = 1;
    144  1.1  christos 		if (!duplicate) {
    145  1.2  christos 			EV_SET(e + nleft, (uintptr_t)pid, EVFILT_PROC, EV_ADD,
    146  1.2  christos 			    NOTE_EXIT, 0, NULL);
    147  1.1  christos 			if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
    148  1.2  christos 				warn("%jd", (intmax_t)pid);
    149  1.1  christos 			else
    150  1.1  christos 				nleft++;
    151  1.1  christos 		}
    152  1.1  christos 	}
    153  1.1  christos 
    154  1.1  christos 	while (nleft > 0) {
    155  1.3  christos 		int rv;
    156  1.3  christos 
    157  1.3  christos 		switch (rv = kevent(kq, NULL, 0, e, nleft, tsp)) {
    158  1.3  christos 		case 0:
    159  1.3  christos 			if (verbose)
    160  1.3  christos 				printf("timed out\n");
    161  1.3  christos 			if (childstatus)
    162  1.3  christos 				return 255;
    163  1.3  christos 			return EX_OK;
    164  1.3  christos 		case -1:
    165  1.2  christos 			err(EXIT_FAILURE, "kevent");
    166  1.3  christos 		default:
    167  1.3  christos 			n = (size_t)rv;
    168  1.3  christos 			break;
    169  1.3  christos 		}
    170  1.3  christos 
    171  1.2  christos 		for (i = 0; i < n; i++) {
    172  1.2  christos 			status = (int)e[i].data;
    173  1.2  christos 			if (verbose) {
    174  1.1  christos 				if (WIFEXITED(status))
    175  1.1  christos 					printf("%ld: exited with status %d.\n",
    176  1.1  christos 					    (long)e[i].ident,
    177  1.1  christos 					    WEXITSTATUS(status));
    178  1.1  christos 				else if (WIFSIGNALED(status))
    179  1.1  christos 					printf("%ld: killed by signal %d.\n",
    180  1.1  christos 					    (long)e[i].ident,
    181  1.1  christos 					    WTERMSIG(status));
    182  1.1  christos 				else
    183  1.1  christos 					printf("%ld: terminated.\n",
    184  1.1  christos 					    (long)e[i].ident);
    185  1.1  christos 			}
    186  1.2  christos 			if (childstatus)
    187  1.2  christos 				return status;
    188  1.2  christos 		}
    189  1.1  christos 		nleft -= n;
    190  1.1  christos 	}
    191  1.1  christos 
    192  1.2  christos 	return EX_OK;
    193  1.1  christos }
    194