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