1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <sched.h>
25#include "libbb.h"
26#ifndef _POSIX_PRIORITY_SCHEDULING
27#warning your system may be foobared
28#endif
29
30static const struct {
31 int policy;
32 char name[sizeof("SCHED_OTHER")];
33} policies[] = {
34 {SCHED_OTHER, "SCHED_OTHER"},
35 {SCHED_FIFO, "SCHED_FIFO"},
36 {SCHED_RR, "SCHED_RR"}
37};
38
39
40
41
42
43static void show_min_max(int pol)
44{
45 const char *fmt = "%s min/max priority\t: %u/%u\n";
46 int max, min;
47
48 max = sched_get_priority_max(pol);
49 min = sched_get_priority_min(pol);
50 if ((max|min) < 0)
51 fmt = "%s not supported\n";
52 printf(fmt, policies[pol].name, min, max);
53}
54
55#define OPT_m (1<<0)
56#define OPT_p (1<<1)
57#define OPT_r (1<<2)
58#define OPT_f (1<<3)
59#define OPT_o (1<<4)
60
61int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
62int chrt_main(int argc UNUSED_PARAM, char **argv)
63{
64 pid_t pid = 0;
65 unsigned opt;
66 struct sched_param sp;
67 char *pid_str;
68 char *priority = priority;
69 const char *current_new;
70 int policy = SCHED_RR;
71
72
73 opt_complementary = "r--fo:f--ro:o--rf";
74 opt = getopt32(argv, "+mprfo");
75 if (opt & OPT_m) {
76 show_min_max(SCHED_FIFO);
77 show_min_max(SCHED_RR);
78 show_min_max(SCHED_OTHER);
79 fflush_stdout_and_exit(EXIT_SUCCESS);
80 }
81 if (opt & OPT_r)
82 policy = SCHED_RR;
83 if (opt & OPT_f)
84 policy = SCHED_FIFO;
85 if (opt & OPT_o)
86 policy = SCHED_OTHER;
87
88 argv += optind;
89 if (!argv[0])
90 bb_show_usage();
91 if (opt & OPT_p) {
92 pid_str = *argv++;
93 if (*argv) {
94 priority = pid_str;
95 pid_str = *argv;
96 }
97
98 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
99 } else {
100 priority = *argv++;
101 if (!*argv)
102 bb_show_usage();
103 }
104
105 current_new = "current\0new";
106 if (opt & OPT_p) {
107 int pol;
108 print_rt_info:
109 pol = sched_getscheduler(pid);
110 if (pol < 0)
111 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
112 printf("pid %d's %s scheduling policy: %s\n",
113 pid, current_new, policies[pol].name);
114 if (sched_getparam(pid, &sp))
115 bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
116 printf("pid %d's %s scheduling priority: %d\n",
117 (int)pid, current_new, sp.sched_priority);
118 if (!*argv) {
119
120
121
122 return EXIT_SUCCESS;
123 }
124 *argv = NULL;
125 current_new += 8;
126 }
127
128
129
130
131
132
133 sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
134
135 if (sched_setscheduler(pid, policy, &sp) < 0)
136 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
137
138 if (!argv[0])
139 goto print_rt_info;
140
141 BB_EXECVP_or_die(argv);
142}
143