5 /* run command with bounded time
7 /* \fBtimeout\fR [-\fIsignal\fR] \fItime\fR \fIcommand\fR ...
9 /* \fBtimeout\fR executes a command and imposes an elapsed time limit.
10 /* The command is run in a separate POSIX process group so that the
11 /* right thing happens with commands that spawn child processes.
15 /* Specify an optional signal to send to the controlled process.
16 /* By default, \fBtimeout\fR sends SIGKILL, which cannot be caught
19 /* The elapsed time limit after which the command is terminated.
21 /* The command to be executed.
23 /* The command exit status is the exit status of the command
24 /* (status 1 in case of a usage error).
27 /* This program is part of SATAN.
30 /* System libraries. */
32 #include <sys/types.h>
40 /* Application-specific. */
42 #define perrorexit(s) { perror(s); exit(1); }
44 static int kill_signal = SIGKILL;
45 static char *progname;
46 static char *commandname;
50 fprintf(stderr, "usage: %s [-signal] time command...\n", progname);
54 static void terminate(sig)
57 signal(kill_signal, SIG_DFL);
58 fprintf(stderr, "Timeout: aborting command ``%s'' with signal %d\n",
59 commandname, kill_signal);
77 while (--argc && *++argv && **argv == '-')
78 if ((kill_signal = atoi(*argv + 1)) <= 0)
81 if (argc < 2 || (time_to_run = atoi(argv[0])) <= 0)
84 commandname = argv[1];
87 * Run the command and its watchdog in a separate process group so that
88 * both can be killed off with one signal.
91 switch (child_pid = fork()) {
93 perrorexit("timeout: fork");
94 case 00: /* run controlled command */
95 execvp(argv[1], argv + 1);
97 default: /* become watchdog */
98 (void) signal(SIGHUP, terminate);
99 (void) signal(SIGINT, terminate);
100 (void) signal(SIGQUIT, terminate);
101 (void) signal(SIGTERM, terminate);
102 (void) signal(SIGALRM, terminate);
104 while ((pid = wait(&status)) != -1 && pid != child_pid)
106 return (pid == child_pid ? status : -1);