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:
parent
5e2e2222ae
commit
2362bc2cf5
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=357141
@ -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 ,
|
||||
|
@ -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,11 +150,14 @@ 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user