-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtsched.c
More file actions
57 lines (51 loc) · 1.12 KB
/
rtsched.c
File metadata and controls
57 lines (51 loc) · 1.12 KB
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
/**
* Linux-specific
* Run a program with a specific realtime scheduler and priority
* Executable should be made set-user-id root.
* $./rtsched policy(r|f) priority command args...
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>
#include <string.h>
static void exit_usage() {
setuid(getuid());
puts("Usage: ./rtsched policy priority command arg");
exit(EXIT_FAILURE);
}
static void exit_error() {
setuid(getuid());
printf("rtsched: %s\n", strerror(errno));
exit(errno);
}
int main(int argc, char **argv) {
int policy;
struct sched_param param;
char *ep;
// Temporarily drop priv
if (seteuid(getuid()))
exit_error();
if (argc < 4)
exit_usage();
param.sched_priority = strtol(argv[2], &ep, 10);
if (*ep != '\0')
exit_usage();
if (*argv[1] == 'f')
policy = SCHED_FIFO;
else if (*argv[1] == 'r')
policy = SCHED_RR;
else
exit_usage();
// Restore priv
if (seteuid(0))
exit_error();
if (sched_setscheduler(0, policy, ¶m) == -1)
exit_error();
// Permenantly drop priv
if (setuid(getuid()))
exit_error();
execvp(argv[3], argv + 3);
exit_error();
}