summaryrefslogtreecommitdiffstats
path: root/src/utils/setitimer-helper.c
blob: 772d736a26057ff095581a41c6b320e96b30a80f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

int
main (int argc, char *argv[]) 
{
  const char *program_name = argv[0];
  double timeout;

  if (argc < 3)
    {
      fprintf (stderr,
               "setitimer-helper: runs a program with a virtual CPU limit\n"
               "usage: %s TIMEOUT PROGRAM [ARG...]\n"
               "  where TIMEOUT is the virtual CPU limit, in seconds,\n"
               "    and remaining arguments specify the program to run\n"
               "    and its argument.\n",
               program_name);
      return EXIT_FAILURE;
    }

  timeout = strtod (argv[1], NULL);
  if (timeout >= 0.0 && timeout < LONG_MAX)
    {
      struct itimerval it;

      it.it_interval.tv_sec = 0;
      it.it_interval.tv_usec = 0;
      it.it_value.tv_sec = timeout;
      it.it_value.tv_usec = (timeout - floor (timeout)) * 1000000;
      if (setitimer (ITIMER_VIRTUAL, &it, NULL) < 0)
        fprintf (stderr, "%s: setitimer: %s\n",
                 program_name, strerror (errno));
    }
  else
    fprintf (stderr, "%s: invalid timeout value \"%s\"\n",
             program_name, argv[1]);
  
  execvp (argv[2], &argv[2]);
  fprintf (stderr, "%s: couldn't exec \"%s\": %s\n",
           program_name, argv[2], strerror (errno));
  return EXIT_FAILURE;
}