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