From: Michael Gebetsroither Date: Mon, 27 Nov 2006 23:43:41 +0000 (+0100) Subject: slimmed down timeout.c and cleanly compile it from source through Makefile X-Git-Tag: 0.79~5 X-Git-Url: http://git.grml.org/?a=commitdiff_plain;h=ee1cae18ac61fb59f85e01ed833404ce985dff7a;p=grml-terminalserver.git slimmed down timeout.c and cleanly compile it from source through Makefile --- diff --git a/Makefile b/Makefile index 78296ab..ce2f508 100644 --- a/Makefile +++ b/Makefile @@ -2,14 +2,22 @@ install_ = "install" name = "grml-terminalserver" +#ifndef CFLAGS +CFLAGS = -Wall -O2 +#endif + etc = ${DESTDIR}/etc/grml/terminalserver usr = ${DESTDIR}/usr usrbin = $(usr)/bin usrsbin = $(usr)/sbin usrshare = $(usr)/share/$(name) +bin: timeout + +timeout: timeout.c + diet gcc $(CFLAGS) $^ -o $@ -install: +install: bin $(install_) -d -m 755 $(etc) $(install_) -m 644 config $(etc) @@ -33,4 +41,4 @@ install: $(install_) -m 755 grml-terminalserver-config $(usrsbin) clean: - true + rm -f timeout diff --git a/debian/TODO b/debian/TODO index 24d7bad..fa3f0b6 100644 --- a/debian/TODO +++ b/debian/TODO @@ -5,5 +5,4 @@ TODOs for grml-terminalserver * DON'T delete /var/lib/tftpdboot on a !grml-system * config vars to use external services (dhcp, tftp, nfs) * make pxelinux configurable (to use another pxe-boot-manager) (MAYBE) - * default dhcp range not from 0.1-254 but from 0.2-253 * build binaries (timeout, udhcpc,...) from source diff --git a/timeout b/timeout deleted file mode 100755 index e5c8241..0000000 Binary files a/timeout and /dev/null differ diff --git a/timeout.c b/timeout.c index 8cda986..136bc00 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=maker