diff options
author | Bryan Drewery <bryan@shatow.net> | 2017-02-21 06:56:28 +0300 |
---|---|---|
committer | Bryan Drewery <bryan@shatow.net> | 2017-06-20 20:13:23 +0300 |
commit | 12981645baa971d23995da2f666f4463dea3e5ab (patch) | |
tree | a22224809c912c72438ac9f66d95e7f01f13411d | |
parent | 5598cc27c7d1e30e8da1d4c3b2336b1917454db9 (diff) |
Import pwait(1) from base r314944
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | Makefile.in | 39 | ||||
-rw-r--r-- | src/libexec/poudriere/pwait/pwait.c | 195 |
4 files changed, 228 insertions, 9 deletions
@@ -25,6 +25,7 @@ /poudriere_statsd /poudriere /ptsort +/pwait /rename /rexec /rm diff --git a/Makefile.am b/Makefile.am index fb1986b3..c069bdcc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -112,6 +112,7 @@ pkglibexec_PROGRAMS= \ poudriered \ rename \ locked_mkdir \ + pwait \ @SH@ \ @RM@ \ timeout \ @@ -143,6 +144,7 @@ dirwatch_SOURCES= src/libexec/poudriere/dirwatch/dirwatch.c locked_mkdir_SOURCES= src/libexec/poudriere/locked_mkdir/locked_mkdir.c nc_SOURCES= src/libexec/poudriere/nc/nc.c rm_SOURCES= src/libexec/poudriere/rm/rm.c +pwait_SOURCES= src/libexec/poudriere/pwait/pwait.c timeout_SOURCES= src/libexec/poudriere/timeout/timeout.c timestamp_SOURCES= src/libexec/poudriere/timestamp/timestamp.c tsort_SOURCES= src/libexec/poudriere/tsort/tsort.c diff --git a/Makefile.in b/Makefile.in index d0c949f6..117258f5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -92,7 +92,7 @@ PRE_UNINSTALL = : POST_UNINSTALL = : pkglibexec_PROGRAMS = clock$(EXEEXT) cpdup$(EXEEXT) dirempty$(EXEEXT) \ dirwatch$(EXEEXT) nc$(EXEEXT) poudriered$(EXEEXT) \ - rename$(EXEEXT) locked_mkdir$(EXEEXT) @SH@ @RM@ \ + rename$(EXEEXT) locked_mkdir$(EXEEXT) pwait$(EXEEXT) @SH@ @RM@ \ timeout$(EXEEXT) timestamp$(EXEEXT) tsort$(EXEEXT) \ jexecd$(EXEEXT) ptsort$(EXEEXT) rexec$(EXEEXT) EXTRA_PROGRAMS = rm$(EXEEXT) sh$(EXEEXT) @@ -198,6 +198,9 @@ poudriered_LINK = $(CCLD) $(poudriered_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ am_ptsort_OBJECTS = external/ptsort/bin/ptsort-ptsort.$(OBJEXT) ptsort_OBJECTS = $(am_ptsort_OBJECTS) ptsort_DEPENDENCIES = libptsort.a +am_pwait_OBJECTS = src/libexec/poudriere/pwait/pwait.$(OBJEXT) +pwait_OBJECTS = $(am_pwait_OBJECTS) +pwait_LDADD = $(LDADD) am_rename_OBJECTS = src/libexec/poudriere/rename/rename.$(OBJEXT) rename_OBJECTS = $(am_rename_OBJECTS) rename_LDADD = $(LDADD) @@ -317,16 +320,16 @@ SOURCES = $(libnv_a_SOURCES) $(libptsort_a_SOURCES) \ $(libucl_a_SOURCES) $(clock_SOURCES) $(cpdup_SOURCES) \ $(dirempty_SOURCES) $(dirwatch_SOURCES) $(jexecd_SOURCES) \ $(locked_mkdir_SOURCES) $(nc_SOURCES) $(poudriered_SOURCES) \ - $(ptsort_SOURCES) $(rename_SOURCES) $(rexec_SOURCES) \ - $(rm_SOURCES) $(sh_SOURCES) $(timeout_SOURCES) \ - $(timestamp_SOURCES) $(tsort_SOURCES) + $(ptsort_SOURCES) $(pwait_SOURCES) $(rename_SOURCES) \ + $(rexec_SOURCES) $(rm_SOURCES) $(sh_SOURCES) \ + $(timeout_SOURCES) $(timestamp_SOURCES) $(tsort_SOURCES) DIST_SOURCES = $(libnv_a_SOURCES) $(libptsort_a_SOURCES) \ $(libucl_a_SOURCES) $(clock_SOURCES) $(cpdup_SOURCES) \ $(dirempty_SOURCES) $(dirwatch_SOURCES) $(jexecd_SOURCES) \ $(locked_mkdir_SOURCES) $(nc_SOURCES) $(poudriered_SOURCES) \ - $(ptsort_SOURCES) $(rename_SOURCES) $(rexec_SOURCES) \ - $(rm_SOURCES) $(sh_SOURCES) $(timeout_SOURCES) \ - $(timestamp_SOURCES) $(tsort_SOURCES) + $(ptsort_SOURCES) $(pwait_SOURCES) $(rename_SOURCES) \ + $(rexec_SOURCES) $(rm_SOURCES) $(sh_SOURCES) \ + $(timeout_SOURCES) $(timestamp_SOURCES) $(tsort_SOURCES) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -597,6 +600,7 @@ dirwatch_SOURCES = src/libexec/poudriere/dirwatch/dirwatch.c locked_mkdir_SOURCES = src/libexec/poudriere/locked_mkdir/locked_mkdir.c nc_SOURCES = src/libexec/poudriere/nc/nc.c rm_SOURCES = src/libexec/poudriere/rm/rm.c +pwait_SOURCES = src/libexec/poudriere/pwait/pwait.c timeout_SOURCES = src/libexec/poudriere/timeout/timeout.c timestamp_SOURCES = src/libexec/poudriere/timestamp/timestamp.c tsort_SOURCES = src/libexec/poudriere/tsort/tsort.c @@ -963,6 +967,19 @@ external/ptsort/bin/ptsort-ptsort.$(OBJEXT): \ ptsort$(EXEEXT): $(ptsort_OBJECTS) $(ptsort_DEPENDENCIES) $(EXTRA_ptsort_DEPENDENCIES) @rm -f ptsort$(EXEEXT) $(AM_V_CCLD)$(LINK) $(ptsort_OBJECTS) $(ptsort_LDADD) $(LIBS) +src/libexec/poudriere/pwait/$(am__dirstamp): + @$(MKDIR_P) src/libexec/poudriere/pwait + @: > src/libexec/poudriere/pwait/$(am__dirstamp) +src/libexec/poudriere/pwait/$(DEPDIR)/$(am__dirstamp): + @$(MKDIR_P) src/libexec/poudriere/pwait/$(DEPDIR) + @: > src/libexec/poudriere/pwait/$(DEPDIR)/$(am__dirstamp) +src/libexec/poudriere/pwait/pwait.$(OBJEXT): \ + src/libexec/poudriere/pwait/$(am__dirstamp) \ + src/libexec/poudriere/pwait/$(DEPDIR)/$(am__dirstamp) + +pwait$(EXEEXT): $(pwait_OBJECTS) $(pwait_DEPENDENCIES) $(EXTRA_pwait_DEPENDENCIES) + @rm -f pwait$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(pwait_OBJECTS) $(pwait_LDADD) $(LIBS) src/libexec/poudriere/rename/$(am__dirstamp): @$(MKDIR_P) src/libexec/poudriere/rename @: > src/libexec/poudriere/rename/$(am__dirstamp) @@ -1238,6 +1255,7 @@ mostlyclean-compile: -rm -f src/libexec/poudriere/jexecd/*.$(OBJEXT) -rm -f src/libexec/poudriere/locked_mkdir/*.$(OBJEXT) -rm -f src/libexec/poudriere/nc/*.$(OBJEXT) + -rm -f src/libexec/poudriere/pwait/*.$(OBJEXT) -rm -f src/libexec/poudriere/rename/*.$(OBJEXT) -rm -f src/libexec/poudriere/rexec/*.$(OBJEXT) -rm -f src/libexec/poudriere/rm/*.$(OBJEXT) @@ -1309,6 +1327,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@src/libexec/poudriere/locked_mkdir/$(DEPDIR)/locked_mkdir.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/libexec/poudriere/locked_mkdir/$(DEPDIR)/sh-locked_mkdir.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/libexec/poudriere/nc/$(DEPDIR)/nc.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@src/libexec/poudriere/pwait/$(DEPDIR)/pwait.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/libexec/poudriere/rename/$(DEPDIR)/rename.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/libexec/poudriere/rexec/$(DEPDIR)/rexec-rexec.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/libexec/poudriere/rm/$(DEPDIR)/rm.Po@am__quote@ @@ -2743,6 +2762,8 @@ distclean-generic: -rm -f src/libexec/poudriere/locked_mkdir/$(am__dirstamp) -rm -f src/libexec/poudriere/nc/$(DEPDIR)/$(am__dirstamp) -rm -f src/libexec/poudriere/nc/$(am__dirstamp) + -rm -f src/libexec/poudriere/pwait/$(DEPDIR)/$(am__dirstamp) + -rm -f src/libexec/poudriere/pwait/$(am__dirstamp) -rm -f src/libexec/poudriere/rename/$(DEPDIR)/$(am__dirstamp) -rm -f src/libexec/poudriere/rename/$(am__dirstamp) -rm -f src/libexec/poudriere/rexec/$(DEPDIR)/$(am__dirstamp) @@ -2770,7 +2791,7 @@ clean-am: clean-generic clean-noinstLIBRARIES clean-pkglibexecPROGRAMS \ distclean: distclean-am -rm -f $(am__CONFIG_DISTCLEAN_FILES) - -rm -rf external/libnv/$(DEPDIR) external/libucl/src/$(DEPDIR) external/ptsort/bin/$(DEPDIR) external/ptsort/lib/$(DEPDIR) external/sh/$(DEPDIR) external/sh/bltin/$(DEPDIR) external/sh_compat/$(DEPDIR) src/libexec/poudriere/clock/$(DEPDIR) src/libexec/poudriere/cpdup/$(DEPDIR) src/libexec/poudriere/dirempty/$(DEPDIR) src/libexec/poudriere/dirwatch/$(DEPDIR) src/libexec/poudriere/jexecd/$(DEPDIR) src/libexec/poudriere/locked_mkdir/$(DEPDIR) src/libexec/poudriere/nc/$(DEPDIR) src/libexec/poudriere/rename/$(DEPDIR) src/libexec/poudriere/rexec/$(DEPDIR) src/libexec/poudriere/rm/$(DEPDIR) src/libexec/poudriere/timeout/$(DEPDIR) src/libexec/poudriere/timestamp/$(DEPDIR) src/libexec/poudriere/tsort/$(DEPDIR) src/poudriere-sh/$(DEPDIR) src/poudriered/$(DEPDIR) + -rm -rf external/libnv/$(DEPDIR) external/libucl/src/$(DEPDIR) external/ptsort/bin/$(DEPDIR) external/ptsort/lib/$(DEPDIR) external/sh/$(DEPDIR) external/sh/bltin/$(DEPDIR) external/sh_compat/$(DEPDIR) src/libexec/poudriere/clock/$(DEPDIR) src/libexec/poudriere/cpdup/$(DEPDIR) src/libexec/poudriere/dirempty/$(DEPDIR) src/libexec/poudriere/dirwatch/$(DEPDIR) src/libexec/poudriere/jexecd/$(DEPDIR) src/libexec/poudriere/locked_mkdir/$(DEPDIR) src/libexec/poudriere/nc/$(DEPDIR) src/libexec/poudriere/pwait/$(DEPDIR) src/libexec/poudriere/rename/$(DEPDIR) src/libexec/poudriere/rexec/$(DEPDIR) src/libexec/poudriere/rm/$(DEPDIR) src/libexec/poudriere/timeout/$(DEPDIR) src/libexec/poudriere/timestamp/$(DEPDIR) src/libexec/poudriere/tsort/$(DEPDIR) src/poudriere-sh/$(DEPDIR) src/poudriered/$(DEPDIR) -rm -f Makefile distclean-am: clean-am distclean-compile distclean-generic \ distclean-hdr distclean-tags @@ -2825,7 +2846,7 @@ installcheck-am: maintainer-clean: maintainer-clean-am -rm -f $(am__CONFIG_DISTCLEAN_FILES) -rm -rf $(top_srcdir)/autom4te.cache - -rm -rf external/libnv/$(DEPDIR) external/libucl/src/$(DEPDIR) external/ptsort/bin/$(DEPDIR) external/ptsort/lib/$(DEPDIR) external/sh/$(DEPDIR) external/sh/bltin/$(DEPDIR) external/sh_compat/$(DEPDIR) src/libexec/poudriere/clock/$(DEPDIR) src/libexec/poudriere/cpdup/$(DEPDIR) src/libexec/poudriere/dirempty/$(DEPDIR) src/libexec/poudriere/dirwatch/$(DEPDIR) src/libexec/poudriere/jexecd/$(DEPDIR) src/libexec/poudriere/locked_mkdir/$(DEPDIR) src/libexec/poudriere/nc/$(DEPDIR) src/libexec/poudriere/rename/$(DEPDIR) src/libexec/poudriere/rexec/$(DEPDIR) src/libexec/poudriere/rm/$(DEPDIR) src/libexec/poudriere/timeout/$(DEPDIR) src/libexec/poudriere/timestamp/$(DEPDIR) src/libexec/poudriere/tsort/$(DEPDIR) src/poudriere-sh/$(DEPDIR) src/poudriered/$(DEPDIR) + -rm -rf external/libnv/$(DEPDIR) external/libucl/src/$(DEPDIR) external/ptsort/bin/$(DEPDIR) external/ptsort/lib/$(DEPDIR) external/sh/$(DEPDIR) external/sh/bltin/$(DEPDIR) external/sh_compat/$(DEPDIR) src/libexec/poudriere/clock/$(DEPDIR) src/libexec/poudriere/cpdup/$(DEPDIR) src/libexec/poudriere/dirempty/$(DEPDIR) src/libexec/poudriere/dirwatch/$(DEPDIR) src/libexec/poudriere/jexecd/$(DEPDIR) src/libexec/poudriere/locked_mkdir/$(DEPDIR) src/libexec/poudriere/nc/$(DEPDIR) src/libexec/poudriere/pwait/$(DEPDIR) src/libexec/poudriere/rename/$(DEPDIR) src/libexec/poudriere/rexec/$(DEPDIR) src/libexec/poudriere/rm/$(DEPDIR) src/libexec/poudriere/timeout/$(DEPDIR) src/libexec/poudriere/timestamp/$(DEPDIR) src/libexec/poudriere/tsort/$(DEPDIR) src/poudriere-sh/$(DEPDIR) src/poudriered/$(DEPDIR) -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic diff --git a/src/libexec/poudriere/pwait/pwait.c b/src/libexec/poudriere/pwait/pwait.c new file mode 100644 index 00000000..2b8374b1 --- /dev/null +++ b/src/libexec/poudriere/pwait/pwait.c @@ -0,0 +1,195 @@ +/*- + * Copyright (c) 2004-2009, Jilles Tjoelker + * All rights reserved. + * + * Redistribution and use in source and binary forms, with + * or without modification, are permitted provided that the + * following conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD: head/bin/pwait/pwait.c 314886 2017-03-07 22:16:55Z bdrewery $"); + +#include <sys/types.h> +#include <sys/event.h> +#include <sys/time.h> +#include <sys/wait.h> + +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sysexits.h> +#include <unistd.h> + +static void +usage(void) +{ + + fprintf(stderr, "usage: pwait [-t timeout] [-v] pid ...\n"); + exit(EX_USAGE); +} + +/* + * pwait - wait for processes to terminate + */ +int +main(int argc, char *argv[]) +{ + struct itimerval itv; + int kq; + struct kevent *e; + int tflag, verbose; + int opt, nleft, n, i, duplicate, status; + long pid; + char *s, *end; + double timeout; + + tflag = verbose = 0; + memset(&itv, 0, sizeof(itv)); + while ((opt = getopt(argc, argv, "t:v")) != -1) { + switch (opt) { + case 't': + tflag = 1; + errno = 0; + timeout = strtod(optarg, &end); + if (end == optarg || errno == ERANGE || + timeout < 0) + errx(EX_DATAERR, "timeout value"); + switch(*end) { + case 0: + case 's': + break; + case 'h': + timeout *= 60; + /* FALLTHROUGH */ + case 'm': + timeout *= 60; + break; + default: + errx(EX_DATAERR, "timeout unit"); + } + if (timeout > 100000000L) + errx(EX_DATAERR, "timeout value"); + itv.it_value.tv_sec = (time_t)timeout; + timeout -= (time_t)timeout; + itv.it_value.tv_usec = + (suseconds_t)(timeout * 1000000UL); + break; + case 'v': + verbose = 1; + break; + default: + usage(); + /* NOTREACHED */ + } + } + + argc -= optind; + argv += optind; + + if (argc == 0) + usage(); + + kq = kqueue(); + if (kq == -1) + err(1, "kqueue"); + + e = malloc((argc + tflag) * sizeof(struct kevent)); + if (e == NULL) + err(1, "malloc"); + nleft = 0; + for (n = 0; n < argc; n++) { + s = argv[n]; + if (!strncmp(s, "/proc/", 6)) /* Undocumented Solaris compat */ + s += 6; + errno = 0; + pid = strtol(s, &end, 10); + if (pid < 0 || *end != '\0' || errno != 0) { + warnx("%s: bad process id", s); + continue; + } + duplicate = 0; + for (i = 0; i < nleft; i++) + if (e[i].ident == (uintptr_t)pid) + duplicate = 1; + if (!duplicate) { + EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, + 0, NULL); + if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) + warn("%ld", pid); + else + nleft++; + } + } + + if (tflag) { + /* + * Explicitly detect SIGALRM so that an exit status of 124 + * can be returned rather than 142. + */ + EV_SET(e + nleft, SIGALRM, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); + if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) + err(EX_OSERR, "kevent"); + /* Ignore SIGALRM to not interrupt kevent(2). */ + signal(SIGALRM, SIG_IGN); + if (setitimer(ITIMER_REAL, &itv, NULL) == -1) + err(EX_OSERR, "setitimer"); + } + while (nleft > 0) { + n = kevent(kq, NULL, 0, e, nleft + tflag, NULL); + if (n == -1) + err(1, "kevent"); + for (i = 0; i < n; i++) { + if (e[i].filter == EVFILT_SIGNAL) { + if (verbose) + printf("timeout\n"); + return (124); + } + if (verbose) { + status = e[i].data; + if (WIFEXITED(status)) + printf("%ld: exited with status %d.\n", + (long)e[i].ident, + WEXITSTATUS(status)); + else if (WIFSIGNALED(status)) + printf("%ld: killed by signal %d.\n", + (long)e[i].ident, + WTERMSIG(status)); + else + printf("%ld: terminated.\n", + (long)e[i].ident); + } + --nleft; + } + } + + exit(EX_OK); +} |