X-Git-Url: http://git.grml.org/?a=blobdiff_plain;f=timeout.c;h=f6fb0cf9e8be98447fed685a28e36cc4e4c7505e;hb=a3ead6351450de49ebc411278e2b960956aacfca;hp=8cda986b275719ede151cd4f835205c09bcffd95;hpb=80fced30b0188cc47c32f4bbdece9e80d4263bf7;p=grml-terminalserver.git diff --git a/timeout.c b/timeout.c index 8cda986..f6fb0cf 100644 --- a/timeout.c +++ b/timeout.c @@ -1,35 +1,37 @@ /*++ -/* NAME -/* timeout 1 -/* SUMMARY -/* run command with bounded time -/* SYNOPSIS -/* \fBtimeout\fR [-\fIsignal\fR] \fItime\fR \fIcommand\fR ... -/* DESCRIPTION -/* \fBtimeout\fR executes a command and imposes an elapsed time limit. -/* The command is run in a separate POSIX process group so that the -/* right thing happens with commands that spawn child processes. -/* -/* Arguments: -/* .IP \fI-signal\fR -/* Specify an optional signal to send to the controlled process. -/* By default, \fBtimeout\fR sends SIGKILL, which cannot be caught -/* or ignored. -/* .IP \fItime\fR -/* The elapsed time limit after which the command is terminated. -/* .IP \fIcommand\fR -/* The command to be executed. -/* DIAGNOSTICS -/* The command exit status is the exit status of the command -/* (status 1 in case of a usage error). -/* AUTHOR(S) -/* Wietse Venema -/* This program is part of SATAN. -/*--*/ + * NAME + * timeout 1 + * SUMMARY + * run command with bounded time + * SYNOPSIS + * \fBtimeout\fR [-\fIsignal\fR] \fItime\fR \fIcommand\fR ... + * DESCRIPTION + * \fBtimeout\fR executes a command and imposes an elapsed time limit. + * The command is run in a separate POSIX process group so that the + * right thing happens with commands that spawn child processes. + * + * Arguments: + * .IP \fI-signal\fR + * Specify an optional signal to send to the controlled process. + * By default, \fBtimeout\fR sends SIGKILL, which cannot be caught + * or ignored. + * .IP \fItime\fR + * The elapsed time limit after which the command is terminated. + * .IP \fIcommand\fR + * The command to be executed. + * DIAGNOSTICS + * The command exit status is the exit status of the command + * (status 1 in case of a usage error). + * AUTHOR(S) + * Wietse Venema + * This program is part of SATAN. + *-- + */ /* System libraries. */ #include +#include #include #include #include @@ -40,31 +42,59 @@ extern int optind; /* Application-specific. */ #define perrorexit(s) { perror(s); exit(1); } +#define WRITE(x) write(2, x, strlen(x)) static int kill_signal = SIGKILL; static char *progname; static char *commandname; +// fmt_ulong and fmt_long from libowfat {{{ +unsigned int fmt_ulong(char *dest,unsigned long i) { + register unsigned long len,tmp,len2; + /* first count the number of bytes needed */ + for (len=1, tmp=i; tmp>9; ++len) tmp/=10; + if (dest) + for (tmp=i, dest+=len, len2=len+1; --len2; tmp/=10) + *--dest = (tmp%10)+'0'; + return len; +} + +unsigned int fmt_long(char *dest,long int i) { + if (i<0) { + if (dest) *dest++='-'; + return fmt_ulong(dest,-i)+1; + } else + return fmt_ulong(dest,i); +} +// }}} + static void usage() { - fprintf(stderr, "usage: %s [-signal] time command...\n", progname); + //fprintf(stderr, "usage: %s [-signal] time command...\n", progname); + WRITE("usage: "); + WRITE(progname); + WRITE(" [-signal] time command...\n"); exit(1); } -static void terminate(sig) -int sig; +static void terminate(int sig) { signal(kill_signal, SIG_DFL); - fprintf(stderr, "Timeout: aborting command ``%s'' with signal %d\n", - commandname, kill_signal); + //fprintf(stderr, "Timeout: aborting command ``%s'' with signal %d\n", + // commandname, kill_signal); + char kill_signal_string[22]; + fmt_long(kill_signal_string, kill_signal); + WRITE("Timeout: aborting command ``"); + WRITE(commandname); + WRITE("'' with signal "); + WRITE(kill_signal_string); + WRITE("\n"); kill(0, kill_signal); } -int main(argc, argv) -int argc; -char **argv; +int main(int argc, char** argv) { - int time_to_run; + int time_to_run = 0; pid_t pid; pid_t child_pid; int status; @@ -91,7 +121,7 @@ char **argv; switch (child_pid = fork()) { case -1: /* error */ perrorexit("timeout: fork"); - case 00: /* run controlled command */ + case 0: /* run controlled command */ execvp(argv[1], argv + 1); perrorexit(argv[1]); default: /* become watchdog */ @@ -106,3 +136,5 @@ char **argv; return (pid == child_pid ? status : -1); } } + +// vim: foldmethod=marker