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#define FOR_chrt
28#include "toys.h"
29
30GLOBALS(
31 long pid;
32)
33
34#ifndef _POSIX_PRIORITY_SCHEDULING
35#warning "musl-libc intentionally broke sched_get_priority_min() and friends in commit 1e21e78bf7a5 because its maintainer didn't like those Linux system calls"
36#endif
37
38char *polnames[] = {
39 "SCHED_OTHER", "SCHED_FIFO", "SCHED_RR", "SCHED_BATCH", 0, "SCHED_IDLE",
40 "SCHED_DEADLINE"
41};
42
43void chrt_main(void)
44{
45 int pol, pri;
46
47
48 if (toys.optflags&FLAG_m) {
49 for (pol = 0; pol<ARRAY_LEN(polnames); pol++) if (polnames[pol])
50 printf("%s min/max priority\t: %d/%d\n", polnames[pol],
51 sched_get_priority_min(pol), sched_get_priority_max(pol));
52
53 return;
54 }
55
56
57 if (toys.optflags==FLAG_p && !*toys.optargs) {
58 char *s = "???", *R = "";
59
60 if (-1==(pol = sched_getscheduler(TT.pid))) perror_exit("pid %ld", TT.pid);
61 if (pol & SCHED_RESET_ON_FORK) R = "|SCHED_RESET_ON_FORK";
62 if ((pol &= ~SCHED_RESET_ON_FORK)<ARRAY_LEN(polnames)) s = polnames[pol];
63 printf("pid %ld's current scheduling policy: %s%s\n", TT.pid, s, R);
64
65 if (sched_getparam(TT.pid, (void *)&pri)) perror_exit("sched_getparam");
66 printf("pid %ld's current scheduling priority: %d\n", TT.pid, pri);
67
68 return;
69 }
70
71 if (!*toys.optargs) help_exit("no PRIORITY");
72 if (!toys.optargs[1] == !(toys.optflags&FLAG_p))
73 help_exit("need 1 of -p or COMMAND");
74
75
76 if (-1==(pol = highest_bit(toys.optflags&0x2f))) pol = SCHED_RR;
77 pri = atolx_range(*toys.optargs, sched_get_priority_min(pol),
78 sched_get_priority_max(pol));
79 if (toys.optflags&FLAG_R) pol |= SCHED_RESET_ON_FORK;
80
81 if (sched_setscheduler(TT.pid, pol, (void *)&pri))
82 perror_exit("sched_setscheduler");
83
84 if (*(toys.optargs+1)) xexec(toys.optargs+1);
85}
86