[git commit] chrt: add -R to set SCHED_RESET_ON_FORK

Denys Vlasenko vda.linux at googlemail.com
Sun Oct 4 17:41:03 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=e65b5600381f440332f962a16ab33a5a0381bea1
branch: https://git.busybox.net/busybox/log/?h=master

Add the -R option, as in util-linux's chrt, which ORs the
SCHED_RESET_ON_FORK flag into the policy passed to sched_setscheduler().
Children created by fork() then do not inherit privileged scheduling
policies and priorities.

Provide a fallback definition of SCHED_RESET_ON_FORK for libcs whose
headers lack it, so the #ifdef around the flag masking in the -p query
path is no longer needed.

function                                             old     new   delta
chrt_main                                            474     501     +27
packed_usage                                       36406   36428     +22
.rodata                                           104787  104788      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 50/0)               Total: 50 bytes

Signed-off-by: Michele Dionisio <michele.dionisio at gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 util-linux/chrt.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/util-linux/chrt.c b/util-linux/chrt.c
index f64fa6aa6..112a1c8e5 100644
--- a/util-linux/chrt.c
+++ b/util-linux/chrt.c
@@ -17,7 +17,7 @@
 //kbuild:lib-$(CONFIG_CHRT) += chrt.o
 
 //usage:#define chrt_trivial_usage
-//usage:       "-m | [-rfobi] { -p [PRIO] PID | PRIO PROG ARGS }"
+//usage:       "-m | [-rfobiR] { -p [PRIO] PID | PRIO PROG ARGS }"
 //usage:#define chrt_full_usage "\n\n"
 //usage:       "Change scheduling priority and class (default RR) for a process\n"
 //usage:     "\n	-m	Show min/max priorities"
@@ -27,6 +27,7 @@
 //usage:     "\n	-o	Set SCHED_OTHER class"
 //usage:     "\n	-b	Set SCHED_BATCH class"
 //usage:     "\n	-i	Set SCHED_IDLE class"
+//usage:     "\n	-R	Set SCHED_RESET_ON_FORK"
 //usage:
 //usage:#define chrt_example_usage
 //usage:       "$ chrt -r 4 sleep 900; x=$!\n"
@@ -38,6 +39,9 @@
 #ifndef SCHED_IDLE
 # define SCHED_IDLE 5
 #endif
+#ifndef SCHED_RESET_ON_FORK
+# define SCHED_RESET_ON_FORK 0x40000000
+#endif
 
 //musl has no __MUSL__ or similar define to check for,
 //but its <sys/types.h> has these lines:
@@ -85,6 +89,7 @@ static void show_min_max(int pol)
 #define OPT_o (1<<4)
 #define OPT_b (1<<5)
 #define OPT_i (1<<6)
+#define OPT_R (1<<7)
 
 int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int chrt_main(int argc UNUSED_PARAM, char **argv)
@@ -99,7 +104,7 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
 	int ret;
 
 	opt = getopt32(argv, "^"
-			"+" "mprfobi"
+			"+" "mprfobiR"
 			"\0"
 			/* only one policy accepted: */
 			"r--fobi:f--robi:o--rfbi:b--rfoi:i--rfob"
@@ -162,7 +167,6 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
 #endif
 		if (pol < 0)
 			bb_perror_msg_and_die("can't %cet pid %u's policy", 'g', (int)pid);
-#ifdef SCHED_RESET_ON_FORK
 		/* "Since Linux 2.6.32, the SCHED_RESET_ON_FORK flag
 		 * can be ORed in policy when calling sched_setscheduler().
 		 * As a result of including this flag, children created by
@@ -172,7 +176,6 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
 		 * (TODO: do we want to show it?)
 		 */
 		pol &= ~SCHED_RESET_ON_FORK;
-#endif
 		printf("pid %u's %s scheduling policy: SCHED_%s\n",
 			pid, current_new, policy_name(pol)
 		);
@@ -199,6 +202,8 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
 	sp.sched_priority = xstrtou_range(priority, 0,
 		sched_get_priority_min(policy), sched_get_priority_max(policy)
 	);
+	if (opt & OPT_R)
+		policy |= SCHED_RESET_ON_FORK;
 
 #if LIBC_IS_MUSL
 	ret = syscall(SYS_sched_setscheduler, pid, policy, &sp);


More information about the busybox-cvs mailing list