summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2025-10-07 08:39:54 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2025-10-07 10:05:08 +0200
commit3cc24609520e3b4141aed4dec0de9eee64b7bdf6 (patch)
treef3019f4a952a0137235dc0c09fb103fe805ca3e1
parent552003dbd6f9e8d8adc55d969e63b9dedcbed726 (diff)
chrt: support passing `-p 0` to operate on self
Specifying a PID of 0 for the -p option of chrt would previously result in a "number 0... not in range" error. Now, it means instead that the calling process (i.e. chrt itself) should be operated on; this is to be consistent with the behavior of util-linux's version of chrt. function old new delta chrt_main 462 474 +12 Signed-off-by: Zuo An <zuoan.penguin@gmail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--util-linux/chrt.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/util-linux/chrt.c b/util-linux/chrt.c
index 51d08584e..f64fa6aa6 100644
--- a/util-linux/chrt.c
+++ b/util-linux/chrt.c
@@ -17,9 +17,9 @@
//kbuild:lib-$(CONFIG_CHRT) += chrt.o
//usage:#define chrt_trivial_usage
-//usage: "-m | -p [PRIO] PID | [-rfobi] PRIO PROG ARGS"
+//usage: "-m | [-rfobi] { -p [PRIO] PID | PRIO PROG ARGS }"
//usage:#define chrt_full_usage "\n\n"
-//usage: "Change scheduling priority and class for a process\n"
+//usage: "Change scheduling priority and class (default RR) for a process\n"
//usage: "\n -m Show min/max priorities"
//usage: "\n -p Operate on PID"
//usage: "\n -r Set SCHED_RR class"
@@ -133,7 +133,14 @@ int chrt_main(int argc UNUSED_PARAM, char **argv)
pid_str = *argv;
}
/* else "-p PID", and *argv == NULL */
- pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
+ pid = xatoul_range(pid_str, 0, ((unsigned)(pid_t)ULONG_MAX) >> 1);
+
+ /* sched_{get,set}scheduler accept PID 0 to mean the calling process,
+ * but this is needed to display the actual PID like util-linux's chrt
+ */
+ if (pid == 0) {
+ pid = getpid();
+ }
} else {
priority = *argv++;
if (!*argv)