Skip to contentSkip to conversation

chrt: Allow optional priority for non‑prio policies without --pid

PATCH Superseded Madadi Vineeth Reddy 2 participants 3 messages Jul 18, 2025 – Jul 21, 2025 Util-Linux package

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 chrt: Allow optional priority for non‑prio policies without --pid

This extends commit e7a2d62434c2
("chrt: Make priority optional for policies that don't use it") so that priority arguments are optional even when --pid is not specified.

Before this patch:

$ chrt --other ls -lh
chrt: invalid priority argument: 'ls'
-> only "chrt --other 0 ls -lh" would work

After this patch:

$ chrt --other ls -lh
$ chrt --other 0 ls -lh
-> both now work

If an out‑of‑range priority is given, it reports an error:

$ chrt --other 1 ls -lh
unsupported priority value for the policy: 1 (see --max for valid range)
Fixes e7a2d62434c2 ("chrt: Make priority optional for policies that don't use it")
---
 schedutils/chrt.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
schedutils/chrt.c +29 -4
index 10ba7fbf6..44b9b9e20 100644
@@ -395,6 +395,18 @@ static void set_sched(struct chrt_ctl *ctl)
 	ctl->altered = 1;
 }
 
+static bool is_number(const char *s)
+{
+	if (!s || *s == '\0')
+		return false;
+
+	for (const char *p = s; *p; p++) {
+		if (!isdigit((unsigned char)*p))
+			return false;
+	}
+	return true;
+}
+
 int main(int argc, char **argv)
 {
 	struct chrt_ctl _ctl = { .pid = -1, .policy = SCHED_RR }, *ctl = &_ctl;
@@ -503,7 +515,7 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (argc - optind < (ctl->pid == 0 ? 1 : 2)) {
+	if (argc - optind < 1) {
 		warnx(_("too few arguments"));
 		errtryhelp(EXIT_FAILURE);
 	}
@@ -527,7 +539,10 @@ int main(int argc, char **argv)
 	if (ctl->verbose)
 		show_sched_info(ctl);
 
-	if (argc - optind > 1) {
+	bool have_prio = need_prio ||
+		(ctl->pid == -1 ? (optind < argc && is_number(argv[optind])) : (argc - optind > 1));
+
+	if (have_prio) {
 		errno = 0;
 		ctl->priority = strtos32_or_err(argv[optind], _("invalid priority argument"));
 	} else
@@ -568,9 +583,19 @@ int main(int argc, char **argv)
 		show_sched_info(ctl);
 
 	if (!ctl->pid) {
-		argv += optind + 1;
-		if (strcmp(argv[0], "--") == 0)
+		argv += optind;
+
+		if (need_prio)
+			argv++;
+		else if (argv[0] && is_number(argv[0]))
+			argv++;
+
+		if (argv[0] && strcmp(argv[0], "--") == 0)
 			argv++;
+
+		if (!argv[0])
+			errx(EXIT_FAILURE, "Missing command to execute");
+
 		execvp(argv[0], argv);
 		errexec(argv[0]);
 	}
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 9 lines · Madadi Vineeth Reddy
schedutils/chrt.c +1 -0
index 10ba7fbf6..44b9b9e20 100644
@@ -395,6 +395,18 @@ static void set_sched(struct chrt_ctl *ctl)
 	ctl->altered = 1;
 }
 
+static bool is_number(const char *s)

schedutils/chrt.c static void set_sched(struct chrt_ctl *ctl) · line 408

Quoted 10 lines
schedutils/chrt.c +10 -0
+{
+	if (!s || *s == '\0')
+		return false;
+
+	for (const char *p = s; *p; p++) {
+		if (!isdigit((unsigned char)*p))
+			return false;
+	}
+	return true;
+}

It seems you can replace this function with isdigit_string() from include/strutils.h.

Quoted 33 lines
schedutils/chrt.c +8 -4
+
 int main(int argc, char **argv)
 {
 	struct chrt_ctl _ctl = { .pid = -1, .policy = SCHED_RR }, *ctl = &_ctl;
@@ -503,7 +515,7 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (argc - optind < (ctl->pid == 0 ? 1 : 2)) {
+	if (argc - optind < 1) {
 		warnx(_("too few arguments"));
 		errtryhelp(EXIT_FAILURE);
 	}
@@ -527,7 +539,10 @@ int main(int argc, char **argv)
 	if (ctl->verbose)
 		show_sched_info(ctl);
 
-	if (argc - optind > 1) {
+	bool have_prio = need_prio ||
+		(ctl->pid == -1 ? (optind < argc && is_number(argv[optind])) : (argc - optind > 1));
+
+	if (have_prio) {
 		errno = 0;
 		ctl->priority = strtos32_or_err(argv[optind], _("invalid priority argument"));
 	} else
@@ -568,9 +583,19 @@ int main(int argc, char **argv)
 		show_sched_info(ctl);
 
 	if (!ctl->pid) {
-		argv += optind + 1;
-		if (strcmp(argv[0], "--") == 0)
+		argv += optind;
+

schedutils/chrt.c int main(int argc, char **argv) · line 597

Quoted 10 lines
schedutils/chrt.c +9 -0
+		if (need_prio)
+			argv++;
+		else if (argv[0] && is_number(argv[0]))
+			argv++;
+
+		if (argv[0] && strcmp(argv[0], "--") == 0)
 			argv++;
+
+		if (!argv[0])
+			errx(EXIT_FAILURE, "Missing command to execute");

You need to use _() for translation, ideally with string already used in other tools. For example, _("no command specified").

Thanks!
Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#2 lore raw

Hi Karel,

Quoted 9 lines · Karel Zak
Quoted 8 lines · Madadi Vineeth Reddy
schedutils/chrt.c +0 -0
index 10ba7fbf6..44b9b9e20 100644
@@ -395,6 +395,18 @@ static void set_sched(struct chrt_ctl *ctl)
 	ctl->altered = 1;
 }
 
Quoted 15 lines

schedutils/chrt.c static void set_sched(struct chrt_ctl *ctl) · line 408

Quoted 11 lines
schedutils/chrt.c +11 -0
+static bool is_number(const char *s)
+{
+	if (!s || *s == '\0')
+		return false;
+
+	for (const char *p = s; *p; p++) {
+		if (!isdigit((unsigned char)*p))
+			return false;
+	}
+	return true;
+}

It seems you can replace this function with isdigit_string() from include/strutils.h.

Will replace it.

Quoted 38 lines
Quoted 38 lines
schedutils/chrt.c +13 -4
+
 int main(int argc, char **argv)
 {
 	struct chrt_ctl _ctl = { .pid = -1, .policy = SCHED_RR }, *ctl = &_ctl;
@@ -503,7 +515,7 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (argc - optind < (ctl->pid == 0 ? 1 : 2)) {
+	if (argc - optind < 1) {
 		warnx(_("too few arguments"));
 		errtryhelp(EXIT_FAILURE);
 	}
@@ -527,7 +539,10 @@ int main(int argc, char **argv)
 	if (ctl->verbose)
 		show_sched_info(ctl);
 
-	if (argc - optind > 1) {
+	bool have_prio = need_prio ||
+		(ctl->pid == -1 ? (optind < argc && is_number(argv[optind])) : (argc - optind > 1));
+
+	if (have_prio) {
 		errno = 0;
 		ctl->priority = strtos32_or_err(argv[optind], _("invalid priority argument"));
 	} else
@@ -568,9 +583,19 @@ int main(int argc, char **argv)
 		show_sched_info(ctl);
 
 	if (!ctl->pid) {
-		argv += optind + 1;
-		if (strcmp(argv[0], "--") == 0)
+		argv += optind;
+
+		if (need_prio)
+			argv++;
+		else if (argv[0] && is_number(argv[0]))
+			argv++;
+
Quoted 8 lines

schedutils/chrt.c int main(int argc, char **argv) · line 597

Quoted 5 lines
schedutils/chrt.c +4 -0
+		if (argv[0] && strcmp(argv[0], "--") == 0)
 			argv++;
+
+		if (!argv[0])
+			errx(EXIT_FAILURE, "Missing command to execute");

You need to use _() for translation, ideally with string already used in other tools. For example, _("no command specified").

Sure. Will fix it in v2. Thanks for taking a look.

--
Madadi Vineeth Reddy

Quoted 4 lines

Thanks!
Karel