00001 #include <errno.h>
00002
00003 #include <limits.h>
00004 #include <math.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <sys/time.h>
00009 #include <unistd.h>
00010
00011 int
00012 main (int argc, char *argv[])
00013 {
00014 const char *program_name = argv[0];
00015 double timeout;
00016
00017 if (argc < 3)
00018 {
00019 fprintf (stderr,
00020 "setitimer-helper: runs a program with a virtual CPU limit\n"
00021 "usage: %s TIMEOUT PROGRAM [ARG...]\n"
00022 " where TIMEOUT is the virtual CPU limit, in seconds,\n"
00023 " and remaining arguments specify the program to run\n"
00024 " and its argument.\n",
00025 program_name);
00026 return EXIT_FAILURE;
00027 }
00028
00029 timeout = strtod (argv[1], NULL);
00030 if (timeout >= 0.0 && timeout < LONG_MAX)
00031 {
00032 struct itimerval it;
00033
00034 it.it_interval.tv_sec = 0;
00035 it.it_interval.tv_usec = 0;
00036 it.it_value.tv_sec = timeout;
00037 it.it_value.tv_usec = (timeout - floor (timeout)) * 1000000;
00038 if (setitimer (ITIMER_VIRTUAL, &it, NULL) < 0)
00039 fprintf (stderr, "%s: setitimer: %s\n",
00040 program_name, strerror (errno));
00041 }
00042 else
00043 fprintf (stderr, "%s: invalid timeout value \"%s\"\n",
00044 program_name, argv[1]);
00045
00046 execvp (argv[2], &argv[2]);
00047 fprintf (stderr, "%s: couldn't exec \"%s\": %s\n",
00048 program_name, argv[2], strerror (errno));
00049 return EXIT_FAILURE;
00050 }