aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2019-04-05 12:03:48 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2019-04-05 12:03:48 +0200
commitae5ca6fc417161eb514103d3c2b38add18012760 (patch)
tree44f30bb87ef774528e3a00e63afb9da24e554a69
parent29c2dcfe1c934f75c87f5a0bc3bf42b2e73f294c (diff)
downloadbusybox-w32-ae5ca6fc417161eb514103d3c2b38add18012760.tar.gz
busybox-w32-ae5ca6fc417161eb514103d3c2b38add18012760.tar.bz2
busybox-w32-ae5ca6fc417161eb514103d3c2b38add18012760.zip
chrt: do not segfault if policy number is unknown
While at it, improve help text function old new delta packed_usage 33319 33344 +25 policy_name - 22 +22 show_min_max 59 64 +5 chrt_main 432 429 -3 policies 72 - -72 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 2/1 up/down: 52/-75) Total: -23 bytes text data bss dec hex filename 982150 485 7296 989931 f1aeb busybox_old 982183 485 7296 989964 f1b0c busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--util-linux/chrt.c62
1 files changed, 34 insertions, 28 deletions
diff --git a/util-linux/chrt.c b/util-linux/chrt.c
index 28e65457c..ede92310f 100644
--- a/util-linux/chrt.c
+++ b/util-linux/chrt.c
@@ -17,16 +17,16 @@
17//kbuild:lib-$(CONFIG_CHRT) += chrt.o 17//kbuild:lib-$(CONFIG_CHRT) += chrt.o
18 18
19//usage:#define chrt_trivial_usage 19//usage:#define chrt_trivial_usage
20//usage: "[-prfombi] [PRIO] [PID | PROG ARGS]" 20//usage: "-m | -p [PRIO] PID | [-rfobi] PRIO PROG [ARGS]"
21//usage:#define chrt_full_usage "\n\n" 21//usage:#define chrt_full_usage "\n\n"
22//usage: "Change scheduling priority and class for a process\n" 22//usage: "Change scheduling priority and class for a process\n"
23//usage: "\n -m Show min/max priorities"
23//usage: "\n -p Operate on PID" 24//usage: "\n -p Operate on PID"
24//usage: "\n -r Set SCHED_RR class" 25//usage: "\n -r Set SCHED_RR class"
25//usage: "\n -f Set SCHED_FIFO class" 26//usage: "\n -f Set SCHED_FIFO class"
26//usage: "\n -o Set SCHED_OTHER class" 27//usage: "\n -o Set SCHED_OTHER class"
27//usage: "\n -b Set SCHED_BATCH class" 28//usage: "\n -b Set SCHED_BATCH class"
28//usage: "\n -i Set SCHED_IDLE class" 29//usage: "\n -i Set SCHED_IDLE class"
29//usage: "\n -m Show min/max priorities"
30//usage: 30//usage:
31//usage:#define chrt_example_usage 31//usage:#define chrt_example_usage
32//usage: "$ chrt -r 4 sleep 900; x=$!\n" 32//usage: "$ chrt -r 4 sleep 900; x=$!\n"
@@ -39,28 +39,32 @@
39# define SCHED_IDLE 5 39# define SCHED_IDLE 5
40#endif 40#endif
41 41
42static const struct { 42static const char *policy_name(int pol)
43 char name[sizeof("SCHED_OTHER")]; 43{
44} policies[] = { 44 if (pol > 6)
45 { "SCHED_OTHER" }, /* 0:SCHED_OTHER */ 45 return utoa(pol);
46 { "SCHED_FIFO" }, /* 1:SCHED_FIFO */ 46 return nth_string(
47 { "SCHED_RR" }, /* 2:SCHED_RR */ 47 "OTHER" "\0" /* 0:SCHED_OTHER */
48 { "SCHED_BATCH" }, /* 3:SCHED_BATCH */ 48 "FIFO" "\0" /* 1:SCHED_FIFO */
49 { "" }, /* 4:SCHED_ISO */ 49 "RR" "\0" /* 2:SCHED_RR */
50 { "SCHED_IDLE" }, /* 5:SCHED_IDLE */ 50 "BATCH" "\0" /* 3:SCHED_BATCH */
51 /* 6:SCHED_DEADLINE */ 51 "ISO" "\0" /* 4:SCHED_ISO */
52}; 52 "IDLE" "\0" /* 5:SCHED_IDLE */
53 "DEADLINE", /* 6:SCHED_DEADLINE */
54 pol
55 );
56}
53 57
54static void show_min_max(int pol) 58static void show_min_max(int pol)
55{ 59{
56 const char *fmt = "%s min/max priority\t: %u/%u\n"; 60 const char *fmt = "SCHED_%s min/max priority\t: %u/%u\n";
57 int max, min; 61 int max, min;
58 62
59 max = sched_get_priority_max(pol); 63 max = sched_get_priority_max(pol);
60 min = sched_get_priority_min(pol); 64 min = sched_get_priority_min(pol);
61 if ((max|min) < 0) 65 if ((max|min) < 0)
62 fmt = "%s not supported\n"; 66 fmt = "SCHED_%s not supported\n";
63 printf(fmt, policies[pol].name, min, max); 67 printf(fmt, policy_name(pol), min, max);
64} 68}
65 69
66#define OPT_m (1<<0) 70#define OPT_m (1<<0)
@@ -112,11 +116,11 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
112 bb_show_usage(); 116 bb_show_usage();
113 if (opt & OPT_p) { 117 if (opt & OPT_p) {
114 pid_str = *argv++; 118 pid_str = *argv++;
115 if (*argv) { /* "-p <priority> <pid> [...]" */ 119 if (*argv) { /* "-p PRIO PID [...]" */
116 priority = pid_str; 120 priority = pid_str;
117 pid_str = *argv; 121 pid_str = *argv;
118 } 122 }
119 /* else "-p <pid>", and *argv == NULL */ 123 /* else "-p PID", and *argv == NULL */
120 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1); 124 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
121 } else { 125 } else {
122 priority = *argv++; 126 priority = *argv++;
@@ -130,16 +134,18 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
130 print_rt_info: 134 print_rt_info:
131 pol = sched_getscheduler(pid); 135 pol = sched_getscheduler(pid);
132 if (pol < 0) 136 if (pol < 0)
133 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid); 137 bb_perror_msg_and_die("can't %cet pid %u's policy", 'g', (int)pid);
134 printf("pid %d's %s scheduling policy: %s\n", 138 printf("pid %u's %s scheduling policy: SCHED_%s\n",
135 pid, current_new, policies[pol].name); 139 pid, current_new, policy_name(pol)
140 );
136 if (sched_getparam(pid, &sp)) 141 if (sched_getparam(pid, &sp))
137 bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid); 142 bb_perror_msg_and_die("can't get pid %u's attributes", (int)pid);
138 printf("pid %d's %s scheduling priority: %d\n", 143 printf("pid %u's %s scheduling priority: %d\n",
139 (int)pid, current_new, sp.sched_priority); 144 (int)pid, current_new, sp.sched_priority
145 );
140 if (!*argv) { 146 if (!*argv) {
141 /* Either it was just "-p <pid>", 147 /* Either it was just "-p PID",
142 * or it was "-p <priority> <pid>" and we came here 148 * or it was "-p PRIO PID" and we came here
143 * for the second time (see goto below) */ 149 * for the second time (see goto below) */
144 return EXIT_SUCCESS; 150 return EXIT_SUCCESS;
145 } 151 }
@@ -152,9 +158,9 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
152 ); 158 );
153 159
154 if (sched_setscheduler(pid, policy, &sp) < 0) 160 if (sched_setscheduler(pid, policy, &sp) < 0)
155 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid); 161 bb_perror_msg_and_die("can't %cet pid %u's policy", 's', (int)pid);
156 162
157 if (!argv[0]) /* "-p <priority> <pid> [...]" */ 163 if (!argv[0]) /* "-p PRIO PID [...]" */
158 goto print_rt_info; 164 goto print_rt_info;
159 165
160 BB_EXECVP_or_die(argv); 166 BB_EXECVP_or_die(argv);