dc: do not advertise ops disabled in .config
[platform/upstream/busybox.git] / miscutils / chrt.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * chrt - manipulate real-time attributes of a process
4  * Copyright (c) 2006-2007 Bernhard Reutner-Fischer
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7  */
8
9 //usage:#define chrt_trivial_usage
10 //usage:       "[-prfom] [PRIO] [PID | PROG ARGS]"
11 //usage:#define chrt_full_usage "\n\n"
12 //usage:       "Change scheduling priority and class for a process\n"
13 //usage:     "\nOptions:"
14 //usage:     "\n        -p      Operate on PID"
15 //usage:     "\n        -r      Set SCHED_RR class"
16 //usage:     "\n        -f      Set SCHED_FIFO class"
17 //usage:     "\n        -o      Set SCHED_OTHER class"
18 //usage:     "\n        -m      Show min/max priorities"
19 //usage:
20 //usage:#define chrt_example_usage
21 //usage:       "$ chrt -r 4 sleep 900; x=$!\n"
22 //usage:       "$ chrt -f -p 3 $x\n"
23 //usage:       "You need CAP_SYS_NICE privileges to set scheduling attributes of a process"
24
25 #include <sched.h>
26 #include "libbb.h"
27 #ifndef _POSIX_PRIORITY_SCHEDULING
28 #warning your system may be foobared
29 #endif
30
31 static const struct {
32         int policy;
33         char name[sizeof("SCHED_OTHER")];
34 } policies[] = {
35         {SCHED_OTHER, "SCHED_OTHER"},
36         {SCHED_FIFO, "SCHED_FIFO"},
37         {SCHED_RR, "SCHED_RR"}
38 };
39
40 //TODO: add
41 // -b, SCHED_BATCH
42 // -i, SCHED_IDLE
43
44 static void show_min_max(int pol)
45 {
46         const char *fmt = "%s min/max priority\t: %u/%u\n";
47         int max, min;
48
49         max = sched_get_priority_max(pol);
50         min = sched_get_priority_min(pol);
51         if ((max|min) < 0)
52                 fmt = "%s not supported\n";
53         printf(fmt, policies[pol].name, min, max);
54 }
55
56 #define OPT_m (1<<0)
57 #define OPT_p (1<<1)
58 #define OPT_r (1<<2)
59 #define OPT_f (1<<3)
60 #define OPT_o (1<<4)
61
62 int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
63 int chrt_main(int argc UNUSED_PARAM, char **argv)
64 {
65         pid_t pid = 0;
66         unsigned opt;
67         struct sched_param sp;
68         char *pid_str;
69         char *priority = priority; /* for compiler */
70         const char *current_new;
71         int policy = SCHED_RR;
72
73         /* only one policy accepted */
74         opt_complementary = "r--fo:f--ro:o--rf";
75         opt = getopt32(argv, "+mprfo");
76         if (opt & OPT_m) { /* print min/max and exit */
77                 show_min_max(SCHED_FIFO);
78                 show_min_max(SCHED_RR);
79                 show_min_max(SCHED_OTHER);
80                 fflush_stdout_and_exit(EXIT_SUCCESS);
81         }
82         if (opt & OPT_r)
83                 policy = SCHED_RR;
84         if (opt & OPT_f)
85                 policy = SCHED_FIFO;
86         if (opt & OPT_o)
87                 policy = SCHED_OTHER;
88
89         argv += optind;
90         if (!argv[0])
91                 bb_show_usage();
92         if (opt & OPT_p) {
93                 pid_str = *argv++;
94                 if (*argv) { /* "-p <priority> <pid> [...]" */
95                         priority = pid_str;
96                         pid_str = *argv;
97                 }
98                 /* else "-p <pid>", and *argv == NULL */
99                 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
100         } else {
101                 priority = *argv++;
102                 if (!*argv)
103                         bb_show_usage();
104         }
105
106         current_new = "current\0new";
107         if (opt & OPT_p) {
108                 int pol;
109  print_rt_info:
110                 pol = sched_getscheduler(pid);
111                 if (pol < 0)
112                         bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
113                 printf("pid %d's %s scheduling policy: %s\n",
114                                 pid, current_new, policies[pol].name);
115                 if (sched_getparam(pid, &sp))
116                         bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
117                 printf("pid %d's %s scheduling priority: %d\n",
118                                 (int)pid, current_new, sp.sched_priority);
119                 if (!*argv) {
120                         /* Either it was just "-p <pid>",
121                          * or it was "-p <priority> <pid>" and we came here
122                          * for the second time (see goto below) */
123                         return EXIT_SUCCESS;
124                 }
125                 *argv = NULL;
126                 current_new += 8;
127         }
128
129         /* from the manpage of sched_getscheduler:
130         [...] sched_priority can have a value in the range 0 to 99.
131         [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
132         [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
133         */
134         sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
135
136         if (sched_setscheduler(pid, policy, &sp) < 0)
137                 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
138
139         if (!argv[0]) /* "-p <priority> <pid> [...]" */
140                 goto print_rt_info;
141
142         BB_EXECVP_or_die(argv);
143 }