aboutsummaryrefslogtreecommitdiffstats
path: root/slack.c
blob: 28b51717c08c5a9525c0ccc119be5b83697d08b2 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/prctl.h>



#define NS  (1L)
#define US  (1000L)
#define MS  (1000L * 1000L)
#define S   (1000L * 1000L * 1000L)


static void usage(void)
{
  printf("USAGE: slack (get | GET | ((reset | <interval>) [--fatal] [--] <command...>))\n");
  printf("\n");
  printf("The <interval> must be a positive number optionally with a suffix:\n");
  printf("  ns  n           nanoseconds (default)\n");
  printf("  µs  µ  us  u    microseconds\n");
  printf("  ms  m           milliseconds\n");
  printf("  s               seconds\n");
  printf("\n");
  printf("If `GET' is used rather than `get', the timer slack value will be printed in\n");
  printf("nanoseconds without suffix. `GET' and `get' prints the current timer slack\n");
  printf("value and exits.\n");
  printf("\n");
  printf("`reset` reset the timer slack value to the default (the time slack value for\n");
  printf("the init process (PID 1) has.)\n");
  printf("\n");
  printf("Unless `--fatal` is used, the program will run <command> even if the timer\n");
  printf("slack could not be set.\n");
  printf("\n");
}


int main(int argc, char** argv)
{
  long slackvalue, r;
  int i, j, fatal = 0;
  char** exec_argv;
  
  if ((argc < 2) || !strcmp(argv[1], "--help") || !strcmp(argv[1], "help"))
    return usage(), argc < 2 ? 1 : 0;
  
  if (!strcmp(argv[1], "get") || !strcmp(argv[1], "GET"))
    {
      const char* suffix;
      if (r = prctl(PR_GET_TIMERSLACK), r < 0)
	perror(*argv);
      else
	{
	  if (!strcmp(argv[1], "GET"))
	    suffix = "";
	  else if (r %  S == 0)  suffix =  "s", r /=  S;
	  else if (r % MS == 0)  suffix = "ms", r /= MS;
	  else if (r % US == 0)  suffix = "µs", r /= US;
	  else                   suffix = "ns";
	  printf("%li%s\n", r, suffix);
	}
      return r > 0 ? 0 : 1;
    }
  
  for (i = 2; i < argc; i++)
    if (!strcmp(argv[i], "--fatal"))
      fatal = 1;
    else if (!strcmp(argv[i], "--"))
      {
	i++;
	break;
      }
    else if (strstr(argv[i], "-") == argv[i])
      return usage(), 1;
    else
      break;
  
  if (i == argc)
    return usage(), 1;
  
  exec_argv = malloc((argc - i + 1) * sizeof(char*));
  if (exec_argv == NULL)
    return perror(*argv), 2;
  exec_argv[argc - i] = NULL;
  for (j = 0; i < argc; j++, i++)
    exec_argv[j] = argv[i];
  
  if (!strcmp(argv[1], "reset"))
    slackvalue = 0;
  else
    {
      char* end;
      
      slackvalue = strtol(argv[1], &end, 10);
      if (end == argv[1])
	return fprintf(stderr, "No slack value specified."), free(exec_argv), 1;
      
      if      (!strcmp(end, "n") || !strcmp(end, "ns") || !strcmp(end, ""))  slackvalue *= NS;
      else if (!strcmp(end, "µ") || !strcmp(end, "µs"))                      slackvalue *= US;
      else if (!strcmp(end, "u") || !strcmp(end, "us"))                      slackvalue *= US;
      else if (!strcmp(end, "m") || !strcmp(end, "ms"))                      slackvalue *= MS;
      else if (!strcmp(end, "s"))                                            slackvalue *=  S;
      else
	return fprintf(stderr, "Unrecognised suffix for slack value."), free(exec_argv), 1;
      
      if (slackvalue <= 0)
	return fprintf(stderr, "Invalid slack value: %lins", slackvalue), free(exec_argv), 1;
    }
  
  if (r = prctl(PR_SET_TIMERSLACK, slackvalue), r < 0)
    if (perror(*argv), fatal)
      return free(exec_argv), 2;
  
  execvp(*exec_argv, exec_argv);
  perror(*argv);
  free(exec_argv);
  return 2;
}