/* * timelimit.c -- Run a Unix command with a time limit * Copyright (c) 2006 by James Dow Allen * * This program can be distributed or modified subject to * the terms of the GNU General Public License (version 2 * or, at your option, any later version) as published by * the Free Software Foundation. */ #include #include #include int Child; void doalarm() { kill(Child, SIGTERM); printf("Time limit exceeded.\n"); exit(2); } struct sigaction Alarm = { doalarm, 0, 0, 0, }; int main(int argc, char **argv) { if (argc < 3) { printf("Usage: timelimit #secs command ... \n"); exit(1); } if (Child = fork()) { sigaction(SIGALRM, &Alarm, (struct sigaction *)0); alarm(atoi(argv[1])); waitpid(Child, (int *)0, 0); exit(0); } else { argv += 2; execvp(*argv, argv); perror(*argv); exit(1); } }