Implement -o flag which tells pwait(1) to exit if any of the given processes

has terminated.

Sponsored by:	Fudo Security
This commit is contained in:
Pawel Jakub Dawidek 2020-01-26 11:02:51 +00:00
parent 5e2e2222ae
commit 2362bc2cf5
2 changed files with 21 additions and 8 deletions

View File

@ -32,7 +32,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd March 7, 2017
.Dd January 26, 2020
.Dt PWAIT 1
.Os
.Sh NAME
@ -41,7 +41,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl t Ar duration
.Op Fl v
.Op Fl ov
.Ar pid
\&...
.Sh DESCRIPTION
@ -51,6 +51,8 @@ utility will wait until each of the given processes has terminated.
.Pp
The following option is available:
.Bl -tag -width indent
.It Fl o
Exit when any of the given processes has terminated.
.It Fl t Ar duration
If any process is still running after
.Ar duration ,

View File

@ -53,7 +53,7 @@ static void
usage(void)
{
errx(EX_USAGE, "usage: pwait [-t timeout] [-v] pid ...");
errx(EX_USAGE, "usage: pwait [-t timeout] [-ov] pid ...");
}
/*
@ -65,16 +65,22 @@ main(int argc, char *argv[])
struct itimerval itv;
int kq;
struct kevent *e;
int tflag, verbose;
int oflag, tflag, verbose;
int opt, nleft, n, i, status;
long pid;
char *s, *end;
double timeout;
tflag = verbose = 0;
oflag = 0;
tflag = 0;
verbose = 0;
memset(&itv, 0, sizeof(itv));
while ((opt = getopt(argc, argv, "t:v")) != -1) {
while ((opt = getopt(argc, argv, "t:ov")) != -1) {
switch (opt) {
case 'o':
oflag = 1;
break;
case 't':
tflag = 1;
errno = 0;
@ -144,10 +150,13 @@ main(int argc, char *argv[])
continue;
}
EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) {
warn("%ld", pid);
else
if (oflag)
exit(EX_OK);
} else {
nleft++;
}
}
if (nleft > 0 && tflag) {
@ -187,6 +196,8 @@ main(int argc, char *argv[])
printf("%ld: terminated.\n",
(long)e[i].ident);
}
if (oflag)
exit(EX_OK);
--nleft;
}
}