[PATCH 4/4] chrt: don't rely on exact values of SCHED_* defines

Povilas Kanapickas povilas at radix.lt
Sat Jan 13 21:30:46 UTC 2018


---
 util-linux/chrt.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/util-linux/chrt.c b/util-linux/chrt.c
index 5c5513aeb..8fd4b7bea 100644
--- a/util-linux/chrt.c
+++ b/util-linux/chrt.c
@@ -36,17 +36,20 @@
 #include <sched.h>
 #include "libbb.h"
 
-static const struct {
-	int policy;
-	char name[sizeof("SCHED_OTHER")];
-} policies[] = {
-	{SCHED_OTHER, "SCHED_OTHER"},
-	{SCHED_FIFO, "SCHED_FIFO"},
-	{SCHED_RR, "SCHED_RR"},
-	{SCHED_BATCH, "SCHED_BATCH"},
-	{0 /* unused */, ""},
-	{SCHED_IDLE, "SCHED_IDLE"}
-};
+static const char* get_policy_name(int pol)
+{
+	if (pol == SCHED_OTHER)
+		return "SCHED_OTHER";
+	if (pol == SCHED_FIFO)
+		return "SCHED_FIFO";
+	if (pol == SCHED_RR)
+		return "SCHED_RR";
+	if (pol == SCHED_BATCH)
+		return "SCHED_BATCH";
+	if (pol == SCHED_IDLE)
+		return "SCHED_IDLE";
+	return "";
+}
 
 static void show_min_max(int pol)
 {
@@ -57,7 +60,7 @@ static void show_min_max(int pol)
 	min = sched_get_priority_min(pol);
 	if ((max|min) < 0)
 		fmt = "%s not supported\n";
-	printf(fmt, policies[pol].name, min, max);
+	printf(fmt, get_policy_name(pol), min, max);
 }
 
 #define OPT_m (1<<0)
@@ -127,7 +130,7 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
 		if (pol < 0)
 			bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
 		printf("pid %d's %s scheduling policy: %s\n",
-				pid, current_new, policies[pol].name);
+				pid, current_new, get_policy_name(pol));
 		if (sched_getparam(pid, &sp))
 			bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
 		printf("pid %d's %s scheduling priority: %d\n",
-- 
2.14.1




More information about the busybox mailing list