diff options
| author | manuel <manuel@mausz.at> | 2012-03-27 11:51:08 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2012-03-27 11:51:08 +0200 |
| commit | 4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b (patch) | |
| tree | 868c52e06f207b5ec8a3cc141f4b8b2bdfcc165c /utils/setitimer-helper.c | |
| parent | eae0bd57f0a26314a94785061888d193d186944a (diff) | |
| download | progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.gz progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.bz2 progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.zip | |
reorganize file structure to match the upstream requirements
Diffstat (limited to 'utils/setitimer-helper.c')
| -rw-r--r-- | utils/setitimer-helper.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/utils/setitimer-helper.c b/utils/setitimer-helper.c new file mode 100644 index 0000000..772d736 --- /dev/null +++ b/utils/setitimer-helper.c | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | #include <errno.h> | ||
| 2 | #include <limits.h> | ||
| 3 | #include <math.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <string.h> | ||
| 7 | #include <sys/time.h> | ||
| 8 | #include <unistd.h> | ||
| 9 | |||
| 10 | int | ||
| 11 | main (int argc, char *argv[]) | ||
| 12 | { | ||
| 13 | const char *program_name = argv[0]; | ||
| 14 | double timeout; | ||
| 15 | |||
| 16 | if (argc < 3) | ||
| 17 | { | ||
| 18 | fprintf (stderr, | ||
| 19 | "setitimer-helper: runs a program with a virtual CPU limit\n" | ||
| 20 | "usage: %s TIMEOUT PROGRAM [ARG...]\n" | ||
| 21 | " where TIMEOUT is the virtual CPU limit, in seconds,\n" | ||
| 22 | " and remaining arguments specify the program to run\n" | ||
| 23 | " and its argument.\n", | ||
| 24 | program_name); | ||
| 25 | return EXIT_FAILURE; | ||
| 26 | } | ||
| 27 | |||
| 28 | timeout = strtod (argv[1], NULL); | ||
| 29 | if (timeout >= 0.0 && timeout < LONG_MAX) | ||
| 30 | { | ||
| 31 | struct itimerval it; | ||
| 32 | |||
| 33 | it.it_interval.tv_sec = 0; | ||
| 34 | it.it_interval.tv_usec = 0; | ||
| 35 | it.it_value.tv_sec = timeout; | ||
| 36 | it.it_value.tv_usec = (timeout - floor (timeout)) * 1000000; | ||
| 37 | if (setitimer (ITIMER_VIRTUAL, &it, NULL) < 0) | ||
| 38 | fprintf (stderr, "%s: setitimer: %s\n", | ||
| 39 | program_name, strerror (errno)); | ||
| 40 | } | ||
| 41 | else | ||
| 42 | fprintf (stderr, "%s: invalid timeout value \"%s\"\n", | ||
| 43 | program_name, argv[1]); | ||
| 44 | |||
| 45 | execvp (argv[2], &argv[2]); | ||
| 46 | fprintf (stderr, "%s: couldn't exec \"%s\": %s\n", | ||
| 47 | program_name, argv[2], strerror (errno)); | ||
| 48 | return EXIT_FAILURE; | ||
| 49 | } | ||
