Home | History | Annotate | Line # | Download | only in pwait
pwait.c revision 1.1
      1  1.1  christos /*	$NetBSD: pwait.c,v 1.1 2015/03/02 21:43:39 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.1  christos __RCSID("$NetBSD: pwait.c,v 1.1 2015/03/02 21:43:39 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.1  christos static void
     58  1.1  christos usage(void)
     59  1.1  christos {
     60  1.1  christos 
     61  1.1  christos 	fprintf(stderr, "usage: pwait [-v] pid ...\n");
     62  1.1  christos 	exit(EX_USAGE);
     63  1.1  christos }
     64  1.1  christos 
     65  1.1  christos /*
     66  1.1  christos  * pwait - wait for processes to terminate
     67  1.1  christos  */
     68  1.1  christos int
     69  1.1  christos main(int argc, char *argv[])
     70  1.1  christos {
     71  1.1  christos 	int kq;
     72  1.1  christos 	struct kevent *e;
     73  1.1  christos 	int verbose = 0;
     74  1.1  christos 	int opt, nleft, n, i, duplicate, status;
     75  1.1  christos 	long pid;
     76  1.1  christos 	char *s, *end;
     77  1.1  christos 
     78  1.1  christos 	while ((opt = getopt(argc, argv, "v")) != -1) {
     79  1.1  christos 		switch (opt) {
     80  1.1  christos 		case 'v':
     81  1.1  christos 			verbose = 1;
     82  1.1  christos 			break;
     83  1.1  christos 		default:
     84  1.1  christos 			usage();
     85  1.1  christos 			/* NOTREACHED */
     86  1.1  christos 		}
     87  1.1  christos 	}
     88  1.1  christos 
     89  1.1  christos 	argc -= optind;
     90  1.1  christos 	argv += optind;
     91  1.1  christos 
     92  1.1  christos 	if (argc == 0)
     93  1.1  christos 		usage();
     94  1.1  christos 
     95  1.1  christos 	kq = kqueue();
     96  1.1  christos 	if (kq == -1)
     97  1.1  christos 		err(1, "kqueue");
     98  1.1  christos 
     99  1.1  christos 	e = malloc(argc * sizeof(struct kevent));
    100  1.1  christos 	if (e == NULL)
    101  1.1  christos 		err(1, "malloc");
    102  1.1  christos 	nleft = 0;
    103  1.1  christos 	for (n = 0; n < argc; n++) {
    104  1.1  christos 		s = argv[n];
    105  1.1  christos 		if (!strncmp(s, "/proc/", 6)) /* Undocumented Solaris compat */
    106  1.1  christos 			s += 6;
    107  1.1  christos 		errno = 0;
    108  1.1  christos 		pid = strtol(s, &end, 10);
    109  1.1  christos 		if (pid < 0 || *end != '\0' || errno != 0) {
    110  1.1  christos 			warnx("%s: bad process id", s);
    111  1.1  christos 			continue;
    112  1.1  christos 		}
    113  1.1  christos 		duplicate = 0;
    114  1.1  christos 		for (i = 0; i < nleft; i++)
    115  1.1  christos 			if (e[i].ident == (uintptr_t)pid)
    116  1.1  christos 				duplicate = 1;
    117  1.1  christos 		if (!duplicate) {
    118  1.1  christos 			EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT,
    119  1.1  christos 			    0, NULL);
    120  1.1  christos 			if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
    121  1.1  christos 				warn("%ld", pid);
    122  1.1  christos 			else
    123  1.1  christos 				nleft++;
    124  1.1  christos 		}
    125  1.1  christos 	}
    126  1.1  christos 
    127  1.1  christos 	while (nleft > 0) {
    128  1.1  christos 		n = kevent(kq, NULL, 0, e, nleft, NULL);
    129  1.1  christos 		if (n == -1)
    130  1.1  christos 			err(1, "kevent");
    131  1.1  christos 		if (verbose)
    132  1.1  christos 			for (i = 0; i < n; i++) {
    133  1.1  christos 				status = e[i].data;
    134  1.1  christos 				if (WIFEXITED(status))
    135  1.1  christos 					printf("%ld: exited with status %d.\n",
    136  1.1  christos 					    (long)e[i].ident,
    137  1.1  christos 					    WEXITSTATUS(status));
    138  1.1  christos 				else if (WIFSIGNALED(status))
    139  1.1  christos 					printf("%ld: killed by signal %d.\n",
    140  1.1  christos 					    (long)e[i].ident,
    141  1.1  christos 					    WTERMSIG(status));
    142  1.1  christos 				else
    143  1.1  christos 					printf("%ld: terminated.\n",
    144  1.1  christos 					    (long)e[i].ident);
    145  1.1  christos 			}
    146  1.1  christos 		nleft -= n;
    147  1.1  christos 	}
    148  1.1  christos 
    149  1.1  christos 	exit(EX_OK);
    150  1.1  christos }
    151