Index: stable/10/etc/mtree/BSD.tests.dist =================================================================== --- stable/10/etc/mtree/BSD.tests.dist (revision 287391) +++ stable/10/etc/mtree/BSD.tests.dist (revision 287392) @@ -1,400 +1,402 @@ # $FreeBSD$ # # Please see the file src/etc/mtree/README before making changes to this file. # /set type=dir uname=root gname=wheel mode=0755 . bin chown .. date .. expr .. mv .. pax .. pkill .. sh builtins .. errors .. execution .. expansion .. parameters .. parser .. set-e .. .. sleep .. test .. .. cddl lib .. sbin .. usr.bin .. usr.sbin .. .. etc rc.d .. .. games .. gnu lib .. usr.bin diff .. .. .. lib atf libatf-c detail .. .. libatf-c++ detail .. .. test-programs .. .. libc c063 .. db .. gen execve .. posix_spawn .. .. hash data .. .. inet .. locale .. net getaddrinfo data .. .. .. regex data .. .. ssp .. stdio .. stdlib .. string .. sys .. time .. tls dso .. .. termios .. ttyio .. .. libcrypt .. libmp .. librt .. libthr dlopen .. .. libutil .. msun .. .. libexec atf atf-check .. atf-sh .. .. .. sbin dhclient .. devd .. growfs .. mdconfig .. .. secure lib .. libexec .. usr.bin .. usr.sbin .. .. share examples tests atf .. plain .. .. .. .. sys aio .. fifo .. file .. kern execve .. .. kqueue .. mqueue .. netinet .. pjdfstest chflags .. chmod .. chown .. ftruncate .. granular .. link .. mkdir .. mkfifo .. mknod .. open .. rename .. rmdir .. symlink .. truncate .. unlink .. .. vm .. .. usr.bin apply .. basename .. bmake archives fmt_44bsd .. fmt_44bsd_mod .. fmt_oldbsd .. .. basic t0 .. t1 .. t2 .. t3 .. .. execution ellipsis .. empty .. joberr .. plus .. .. shell builtin .. meta .. path .. path_select .. replace .. select .. .. suffixes basic .. src_wild1 .. src_wild2 .. .. syntax directive-t0 .. enl .. funny-targets .. semi .. .. sysmk t0 2 1 .. .. mk .. .. t1 2 1 .. .. mk .. .. t2 2 1 .. .. mk .. .. .. + timeout + .. variables modifier_M .. modifier_t .. opt_V .. t0 .. .. .. calendar .. cmp .. comm .. cut .. dirname .. file2c .. grep .. gzip .. join .. jot .. lastcomm .. m4 .. ncal .. opensm .. printf .. sed regress.multitest.out .. .. tr .. truncate .. uudecode .. uuencode .. xargs .. yacc yacc .. .. .. usr.sbin etcupdate .. newsyslog .. nmtree .. pw .. sa .. .. .. # vim: set expandtab ts=4 sw=4: Index: stable/10/usr.bin/Makefile =================================================================== --- stable/10/usr.bin/Makefile (revision 287391) +++ stable/10/usr.bin/Makefile (revision 287392) @@ -1,410 +1,411 @@ # From: @(#)Makefile 8.3 (Berkeley) 1/7/94 # $FreeBSD$ .include # XXX MISSING: deroff diction graph learn plot # spell spline struct xsend # XXX Use GNU versions: diff ld patch # Moved to secure: bdes # SUBDIR= alias \ apply \ asa \ awk \ banner \ basename \ brandelf \ bsdiff \ bzip2 \ bzip2recover \ cap_mkdb \ chat \ chpass \ cksum \ ${_clang} \ cmp \ col \ colldef \ colrm \ column \ comm \ compress \ cpuset \ csplit \ ctlstat \ cut \ dirname \ dpv \ du \ elf2aout \ elfdump \ enigma \ env \ expand \ false \ fetch \ find \ fmt \ fold \ fstat \ fsync \ gcore \ gencat \ getconf \ getent \ getopt \ grep \ gzip \ head \ hexdump \ ${_iconv} \ id \ ipcrm \ ipcs \ join \ jot \ ${_kdump} \ keylogin \ keylogout \ killall \ ktrace \ ktrdump \ lam \ lastcomm \ ldd \ leave \ less \ lessecho \ lesskey \ limits \ locale \ lock \ lockf \ logger \ login \ logins \ logname \ look \ lorder \ lsvfs \ lzmainfo \ m4 \ ${_makewhatis} \ ${_man} \ mandoc \ mesg \ minigzip \ ministat \ ${_mkcsmapper} \ mkdep \ ${_mkesdb} \ mkfifo \ mkimg \ mklocale \ mktemp \ mkulzma \ mkuzip \ mt \ ncal \ netstat \ newgrp \ nfsstat \ nice \ nl \ nohup \ opieinfo \ opiekey \ opiepasswd \ pagesize \ passwd \ paste \ patch \ pathchk \ perror \ pr \ printenv \ printf \ procstat \ protect \ rctl \ renice \ rev \ revoke \ rpcinfo \ rs \ rup \ rusers \ rwall \ script \ sed \ send-pr \ seq \ shar \ showmount \ sockstat \ sort \ split \ stat \ stdbuf \ su \ systat \ tabs \ tail \ tar \ tcopy \ tee \ ${_tests} \ time \ + timeout \ tip \ top \ touch \ tput \ tr \ true \ truncate \ ${_truss} \ tset \ tsort \ tty \ uname \ unexpand \ uniq \ unzip \ units \ unvis \ uudecode \ uuencode \ vi \ vis \ vmstat \ w \ wall \ wc \ what \ whereis \ which \ whois \ write \ xargs \ xinstall \ ${_xlint} \ ${_xstr} \ xz \ xzdec \ ${_yacc} \ yes \ ${_ypcat} \ ${_ypmatch} \ ${_ypwhich} # NB: keep these sorted by MK_* knobs .if ${MK_AT} != "no" SUBDIR+= at .endif .if ${MK_ATM} != "no" SUBDIR+= atm .endif .if ${MK_BLUETOOTH} != "no" SUBDIR+= bluetooth .endif .if ${MK_BSD_CPIO} != "no" SUBDIR+= cpio .endif .if ${MK_CALENDAR} != "no" SUBDIR+= calendar .endif .if ${MK_CLANG} != "no" _clang= clang .endif .if ${MK_EE} != "no" SUBDIR+= ee .endif .if ${MK_FILE} != "no" SUBDIR+= file .endif .if ${MK_FINGER} != "no" SUBDIR+= finger .endif .if ${MK_FTP} != "no" SUBDIR+= ftp .endif .if ${MK_GPL_DTC} != "yes" SUBDIR+= dtc .endif .if ${MK_GROFF} != "no" SUBDIR+= vgrind .endif .if ${MK_HESIOD} != "no" SUBDIR+= hesinfo .endif .if ${MK_ICONV} != "no" _iconv= iconv _mkcsmapper= mkcsmapper _mkesdb= mkesdb .endif .if ${MK_ISCSI} != "no" SUBDIR+= iscsictl .endif .if ${MK_KDUMP} != "no" SUBDIR+= kdump SUBDIR+= truss .endif .if ${MK_KERBEROS_SUPPORT} != "no" SUBDIR+= compile_et .endif .if ${MK_LDNS_UTILS} != "no" SUBDIR+= drill SUBDIR+= host .endif .if ${MK_LIBTHR} != "no" SUBDIR+= csup .endif .if ${MK_LOCATE} != "no" SUBDIR+= locate .endif # XXX msgs? .if ${MK_MAIL} != "no" SUBDIR+= biff SUBDIR+= from SUBDIR+= mail SUBDIR+= msgs .endif .if ${MK_MAKE} != "no" .if ${MK_BMAKE} != "no" SUBDIR+= bmake .else SUBDIR+= make .endif .endif .if ${MK_MAN_UTILS} != "no" SUBDIR+= catman _makewhatis= makewhatis _man= man .endif .if ${MK_NETCAT} != "no" SUBDIR+= nc .endif .if ${MK_NIS} != "no" SUBDIR+= ypcat SUBDIR+= ypmatch SUBDIR+= ypwhich .endif .if ${MK_OPENSSH} != "no" SUBDIR+= ssh-copy-id .endif .if ${MK_OPENSSL} != "no" SUBDIR+= bc SUBDIR+= chkey SUBDIR+= dc SUBDIR+= newkey .endif .if ${MK_QUOTAS} != "no" SUBDIR+= quota .endif .if ${MK_RCMDS} != "no" SUBDIR+= rlogin SUBDIR+= rsh SUBDIR+= ruptime SUBDIR+= rwho .endif .if ${MK_SENDMAIL} != "no" SUBDIR+= vacation .endif .if ${MK_TALK} != "no" SUBDIR+= talk .endif .if ${MK_TELNET} != "no" SUBDIR+= telnet .endif .if ${MK_TESTS} != "no" _tests= tests .endif .if ${MK_TEXTPROC} != "no" SUBDIR+= checknr SUBDIR+= colcrt SUBDIR+= ul .endif .if ${MK_TFTP} != "no" SUBDIR+= tftp .endif .if ${MK_TOOLCHAIN} != "no" SUBDIR+= ar SUBDIR+= c89 SUBDIR+= c99 SUBDIR+= ctags SUBDIR+= file2c SUBDIR+= gprof SUBDIR+= indent SUBDIR+= lex SUBDIR+= mkstr SUBDIR+= rpcgen SUBDIR+= unifdef SUBDIR+= xlint SUBDIR+= xstr SUBDIR+= yacc .endif .if ${MK_VT} != "no" SUBDIR+= vtfontcvt .endif .if ${MK_USB} != "no" SUBDIR+= usbhidaction SUBDIR+= usbhidctl .endif .if ${MK_UTMPX} != "no" SUBDIR+= last SUBDIR+= users SUBDIR+= who .endif .if ${MK_SVN} == "yes" || ${MK_SVNLITE} == "yes" SUBDIR+= svn .endif .include SUBDIR:= ${SUBDIR:O} SUBDIR_PARALLEL= .include Index: stable/10/usr.bin/timeout/Makefile =================================================================== --- stable/10/usr.bin/timeout/Makefile (nonexistent) +++ stable/10/usr.bin/timeout/Makefile (revision 287392) @@ -0,0 +1,11 @@ +# $FreeBSD$ + +.include + +PROG= timeout + +.if ${MK_TESTS} != "no" +SUBDIR+= tests +.endif + +.include Property changes on: stable/10/usr.bin/timeout/Makefile ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: stable/10/usr.bin/timeout/tests/Makefile =================================================================== --- stable/10/usr.bin/timeout/tests/Makefile (nonexistent) +++ stable/10/usr.bin/timeout/tests/Makefile (revision 287392) @@ -0,0 +1,7 @@ +# $FreeBSD$ + +TESTSDIR= ${TESTSBASE}/usr.bin/timeout + +ATF_TESTS_SH= timeout + +.include Property changes on: stable/10/usr.bin/timeout/tests/Makefile ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: stable/10/usr.bin/timeout/tests/timeout.sh =================================================================== --- stable/10/usr.bin/timeout/tests/timeout.sh (nonexistent) +++ stable/10/usr.bin/timeout/tests/timeout.sh (revision 287392) @@ -0,0 +1,215 @@ +# $FreeBSD$ + +atf_test_case nominal +nominal_head() +{ + atf_set "descr" "Basic tests on timeout(1) utility" +} + +nominal_body() +{ + atf_check \ + -o empty \ + -e empty \ + -s exit:0 \ + -x timeout 5 true +} + +atf_test_case time_unit +time_unit_head() +{ + atf_set "descr" "Test parsing the default time unit" +} + +time_unit_body() +{ + atf_check \ + -o empty \ + -e empty \ + -s exit:0 \ + -x timeout 1d true + + atf_check \ + -o empty \ + -e empty \ + -s exit:0 \ + -x timeout 1h true + + atf_check \ + -o empty \ + -e empty \ + -s exit:0 \ + -x timeout 1m true + + atf_check \ + -o empty \ + -e empty \ + -s exit:0 \ + -x timeout 1s true +} + +atf_test_case no_timeout +no_timeout_head() +{ + atf_set "descr" "Test disabled timeout" +} + +no_timeout_body() +{ + atf_check \ + -o empty \ + -e empty \ + -s exit:0 \ + -x timeout 0 true +} + +atf_test_case exit_numbers +exit_numbers_head() +{ + atf_set "descr" "Test exit numbers" +} + +exit_numbers_body() +{ + atf_check \ + -o empty \ + -e empty \ + -s exit:2 \ + -x timeout 5 sh -c \'exit 2\' + + atf_check \ + -o empty \ + -e empty \ + -s exit:124 \ + -x timeout .1 sleep 1 + + # With preserv status exit shoudl be 128 + TERM aka 143 + atf_check \ + -o empty \ + -e empty \ + -s exit:143 \ + -x timeout --preserve-status .1 sleep 10 + + atf_check \ + -o empty \ + -e empty \ + -s exit:124 \ + -x timeout -s1 -k1 .1 sleep 10 + + atf_check \ + -o empty \ + -e empty \ + -s exit:0 \ + -x sh -c 'trap "" CHLD; exec timeout 10 true' +} + +atf_test_case with_a_child +with_a_child_head() +{ + atf_set "descr" "When starting with a child (coreutils bug#9098)" +} + +with_a_child_body() +{ + out=$(sleep .1 & exec timeout .5 sh -c 'sleep 2; echo foo') + status=$? + test "$out" = "" && test $status = 124 || atf_fail + +} + +atf_test_case invalid_timeout +invalid_timeout_head() +{ + atf_set "descr" "Invalid timeout" +} + +invalid_timeout_body() +{ + atf_check \ + -o empty \ + -e inline:"timeout: invalid duration\n" \ + -s exit:125 \ + -x timeout invalid sleep 0 + + atf_check \ + -o empty \ + -e inline:"timeout: invalid duration\n" \ + -s exit:125 \ + -x timeout --kill-after=invalid 1 sleep 0 + + atf_check \ + -o empty \ + -e inline:"timeout: invalid duration\n" \ + -s exit:125 \ + -x timeout 42D sleep 0 + + atf_check \ + -o empty \ + -e inline:"timeout: invalid duration\n" \ + -s exit:125 \ + -x timeout 999999999999999999999999999999999999999999999999999999999999d sleep 0 + + atf_check \ + -o empty \ + -e inline:"timeout: invalid duration\n" \ + -s exit:125 \ + -x timeout 2.34e+5d sleep 0 +} + +atf_test_case invalid_signal +invalid_signal_head() +{ + atf_set "descr" "Invalid signal" +} + +invalid_signal_body() +{ + atf_check \ + -o empty \ + -e inline:"timeout: invalid signal\n" \ + -s exit:125 \ + -x timeout --signal=invalid 1 sleep 0 +} + +atf_test_case invalid_command +invalid_command_head() +{ + atf_set "descr" "Invalid command" +} + +invalid_command_body() +{ + atf_check \ + -o empty \ + -e inline:"timeout: exec(.): Permission denied\n" \ + -s exit:126 \ + -x timeout 10 . +} + +atf_test_case no_such_command +no_such_command_head() +{ + atf_set "descr" "No such command" +} + +no_such_command_body() +{ + atf_check \ + -o empty \ + -e inline:"timeout: exec(enoexists): No such file or directory\n" \ + -s exit:127 \ + -x timeout 10 enoexists +} + +atf_init_test_cases() +{ + atf_add_test_case nominal + atf_add_test_case time_unit + atf_add_test_case no_timeout + atf_add_test_case exit_numbers + atf_add_test_case with_a_child + atf_add_test_case invalid_timeout + atf_add_test_case invalid_signal + atf_add_test_case invalid_command + atf_add_test_case no_such_command +} Property changes on: stable/10/usr.bin/timeout/tests/timeout.sh ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: stable/10/usr.bin/timeout/timeout.1 =================================================================== --- stable/10/usr.bin/timeout/timeout.1 (nonexistent) +++ stable/10/usr.bin/timeout/timeout.1 (revision 287392) @@ -0,0 +1,129 @@ +.\" Copyright (c) 2014 Baptiste Daroussin +.\" 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 AUTHOR 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 AUTHOR 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. +.\" +.\" $FreeBSD$ +.\" +.Dd Oct 28, 2014 +.Dt TIMEOUT 1 +.Os +.Sh NAME +.Nm timeout +.Nd run a command with a time limit +.Sh SYNOPSIS +.Nm +.Op Fl -signal Ar sig | Fl s Ar sig +.Op Fl -preserve-status +.Op Fl -kill-after Ar time | Fl k Ar time +.Op Fl -foreground +.Ao Ar duration Ac +.Ao Ar command Ac +.Ao Ar args ... Ac +.Sh DESCRIPTION +.Nm +starts the +.Ar command +with its +.Ar args. +If +.Ar command +is still running after +.Ar duration , +it is killed. +By default, +.Ar SIGTERM. +is sent. +.Bl -tag -width "-k time, --kill-after time" +.It Fl -preserve-status +Always exits with the same status as +.Ar command +even if it times out. +.It Fl -foreground +Do not propagate timeout to the +.Ar command +children. +.It Fl s Ar sig , Fl -signal Ar sig +Specify the signal to send on timeout. +By default, +.Ar SIGTERM . +is sent. +.It Fl k Ar time , Fl -kill-after Ar time +Send a second kill signal if +.Ar command +is still running after +.Ar time +after the first signal was sent. +.El +.Sh DURATION FORMAT +.Ar duration +and +.Ar time +can be integer or decimal numbers. +Values without unit symbols are interpreted as seconds. +.Pp +Supported unit symbols are: +.Bl -tag -width indent -compact +.It s +seconds +.It m +minutes +.It h +hours +.It d +days +.El +.Sh EXIT STATUS +If the timeout was not reached, the exit status of +.Ar command +is returned. +.Pp +If the timeout was reached and +.Fl -preserve-status +is set, the exit status of +.Ar command +is returned. +If +.Fl -preserve-status +is not set, an exit status of 124 is returned. +.Pp +If +.Ar command +exits after receiving a signal, the exit status returned is the signal number +plus 128. +.Pp +If +.Ar command +is an invalid command, the exit status returned is 126. +.Pp +If +.Ar command +is a non existing command, the exit status returned is 127. +.Pp +If an invalid parameter is passed to +.Fl s +or +.Fl k , +the exit status return is 125. +.Sh SEE ALSO +.Xr kill 1 , +.Xr signal 3 Property changes on: stable/10/usr.bin/timeout/timeout.1 ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: stable/10/usr.bin/timeout/timeout.c =================================================================== --- stable/10/usr.bin/timeout/timeout.c (nonexistent) +++ stable/10/usr.bin/timeout/timeout.c (revision 287392) @@ -0,0 +1,362 @@ +/*- + * Copyright (c) 2014 Baptiste Daroussin + * Copyright (c) 2014 Vsevolod Stakhov + * 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 + * in this position and unchanged. + * 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 AUTHOR(S) ``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 AUTHOR(S) 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define EXIT_TIMEOUT 124 + +static sig_atomic_t sig_chld = 0; +static sig_atomic_t sig_term = 0; +static sig_atomic_t sig_alrm = 0; +static sig_atomic_t sig_ign = 0; + +static void +usage(void) +{ + + fprintf(stderr, "Usage: %s [--signal sig | -s sig] [--preserve-status]" + " [--kill-after time | -k time] [--foreground] " + " \n", getprogname()); + + exit(EX_USAGE); +} + +static double +parse_duration(const char *duration) +{ + double ret; + char *end; + + ret = strtod(duration, &end); + if (ret == 0 && end == duration) + errx(125, "invalid duration"); + + if (end == NULL || *end == '\0') + return (ret); + + if (end != NULL && *(end + 1) != '\0') + errx(EX_USAGE, "invalid duration"); + + switch (*end) { + case 's': + break; + case 'm': + ret *= 60; + break; + case 'h': + ret *= 60 * 60; + break; + case 'd': + ret *= 60 * 60 * 24; + break; + default: + errx(125, "invalid duration"); + } + + if (ret < 0 || ret >= 100000000UL) + errx(125, "invalid duration"); + + return (ret); +} + +static int +parse_signal(const char *str) +{ + int sig, i; + const char *errstr; + + sig = strtonum(str, 0, sys_nsig, &errstr); + + if (errstr == NULL) + return (sig); + if (strncasecmp(str, "SIG", 3) == 0) + str += 3; + + for (i = 1; i < sys_nsig; i++) { + if (strcasecmp(str, sys_signame[i]) == 0) + return (i); + } + + errx(125, "invalid signal"); +} + +static void +sig_handler(int signo) +{ + if (sig_ign != 0 && signo == sig_ign) { + sig_ign = 0; + return; + } + + switch(signo) { + case 0: + case SIGINT: + case SIGHUP: + case SIGQUIT: + case SIGTERM: + sig_term = signo; + break; + case SIGCHLD: + sig_chld = 1; + break; + case SIGALRM: + sig_alrm = 1; + break; + } +} + +static void +set_interval(double iv) +{ + struct itimerval tim; + + memset(&tim, 0, sizeof(tim)); + tim.it_value.tv_sec = (time_t)iv; + iv -= (time_t)iv; + tim.it_value.tv_usec = (suseconds_t)(iv * 1000000UL); + + if (setitimer(ITIMER_REAL, &tim, NULL) == -1) + err(EX_OSERR, "setitimer()"); +} + +int +main(int argc, char **argv) +{ + int ch; + unsigned long i; + int foreground, preserve; + int error, pstat, status; + int killsig = SIGTERM; + pid_t pid, cpid; + double first_kill; + double second_kill; + bool timedout = false; + bool do_second_kill = false; + bool child_done = false; + struct sigaction signals; + struct procctl_reaper_status info; + struct procctl_reaper_kill killemall; + int signums[] = { + -1, + SIGTERM, + SIGINT, + SIGHUP, + SIGCHLD, + SIGALRM, + SIGQUIT, + }; + + foreground = preserve = 0; + second_kill = 0; + + const struct option longopts[] = { + { "preserve-status", no_argument, &preserve, 1 }, + { "foreground", no_argument, &foreground, 1 }, + { "kill-after", required_argument, NULL, 'k'}, + { "signal", required_argument, NULL, 's'}, + { "help", no_argument, NULL, 'h'}, + { NULL, 0, NULL, 0 } + }; + + while ((ch = getopt_long(argc, argv, "+k:s:h", longopts, NULL)) != -1) { + switch (ch) { + case 'k': + do_second_kill = true; + second_kill = parse_duration(optarg); + break; + case 's': + killsig = parse_signal(optarg); + break; + case 0: + break; + case 'h': + default: + usage(); + break; + } + } + + argc -= optind; + argv += optind; + + if (argc < 2) + usage(); + + first_kill = parse_duration(argv[0]); + argc--; + argv++; + + if (!foreground) { + /* Aquire a reaper */ + if (procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == -1) + err(EX_OSERR, "Fail to acquire the reaper"); + } + + memset(&signals, 0, sizeof(signals)); + sigemptyset(&signals.sa_mask); + + if (killsig != SIGKILL && killsig != SIGSTOP) + signums[0] = killsig; + + for (i = 0; i < sizeof(signums) / sizeof(signums[0]); i ++) + sigaddset(&signals.sa_mask, signums[i]); + + signals.sa_handler = sig_handler; + signals.sa_flags = SA_RESTART; + + for (i = 0; i < sizeof(signums) / sizeof(signums[0]); i ++) + if (signums[i] != -1 && signums[i] != 0 && + sigaction(signums[i], &signals, NULL) == -1) + err(EX_OSERR, "sigaction()"); + + signal(SIGTTIN, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + + pid = fork(); + if (pid == -1) + err(EX_OSERR, "fork()"); + else if (pid == 0) { + /* child process */ + signal(SIGTTIN, SIG_DFL); + signal(SIGTTOU, SIG_DFL); + + error = execvp(argv[0], argv); + if (error == -1) { + if (errno == ENOENT) + err(127, "exec(%s)", argv[0]); + else + err(126, "exec(%s)", argv[0]); + } + } + + if (sigprocmask(SIG_BLOCK, &signals.sa_mask, NULL) == -1) + err(EX_OSERR, "sigprocmask()"); + + /* parent continues here */ + set_interval(first_kill); + + for (;;) { + sigemptyset(&signals.sa_mask); + sigsuspend(&signals.sa_mask); + + if (sig_chld) { + sig_chld = 0; + + while ((cpid = waitpid(-1, &status, WNOHANG)) != 0) { + if (cpid < 0) { + if (errno == EINTR) + continue; + else + break; + } else if (cpid == pid) { + pstat = status; + child_done = true; + } + } + if (child_done) { + if (foreground) { + break; + } else { + procctl(P_PID, getpid(), + PROC_REAP_STATUS, &info); + if (info.rs_children == 0) + break; + } + } + } else if (sig_alrm) { + sig_alrm = 0; + + timedout = true; + if (!foreground) { + killemall.rk_sig = killsig; + killemall.rk_flags = 0; + procctl(P_PID, getpid(), PROC_REAP_KILL, + &killemall); + } else + kill(pid, killsig); + + if (do_second_kill) { + set_interval(second_kill); + second_kill = 0; + sig_ign = killsig; + killsig = SIGKILL; + } else + break; + + } else if (sig_term) { + if (!foreground) { + killemall.rk_sig = sig_term; + killemall.rk_flags = 0; + procctl(P_PID, getpid(), PROC_REAP_KILL, + &killemall); + } else + kill(pid, sig_term); + + if (do_second_kill) { + set_interval(second_kill); + second_kill = 0; + sig_ign = killsig; + killsig = SIGKILL; + } else + break; + } + } + + while (!child_done && wait(&pstat) == -1) { + if (errno != EINTR) + err(EX_OSERR, "waitpid()"); + } + + if (!foreground) + procctl(P_PID, getpid(), PROC_REAP_RELEASE, NULL); + + if (WEXITSTATUS(pstat)) + pstat = WEXITSTATUS(pstat); + else if(WIFSIGNALED(pstat)) + pstat = 128 + WTERMSIG(pstat); + + if (timedout && !preserve) + pstat = EXIT_TIMEOUT; + + return (pstat); +} Property changes on: stable/10/usr.bin/timeout/timeout.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: stable/10 =================================================================== --- stable/10 (revision 287391) +++ stable/10 (revision 287392) Property changes on: stable/10 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r268745-268751,268763,268861,273769,273771,276771,278810