]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
chrt: do not try to interpret any other option as a PID either
authorBenno Schulenberg <bensberg@telfort.nl>
Thu, 3 Jul 2025 14:47:52 +0000 (16:47 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 7 Jul 2025 11:33:11 +0000 (13:33 +0200)
When doing, for example, `chrt --pid --max`, it would report:

  chrt: invalid PID argument: '--max'

This mistakenly gave the impression that the PID argument has to follow
directly after the --pid option.

Avoid this by delaying the parsing of a PID until after all options have
been parsed.  Temporarily set 'ctl->pid' to zero to indicate that a PID
needs to be read.

After this change, `chrt --pid --max` will simply report the minimum and
maximum valid priorities.  And `chrt --pid -v`:

  chrt: too few arguments

Also, add a missing call of gettext() for the other error message.

CC: Madadi Vineeth Reddy <vineethr@linux.ibm.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
schedutils/chrt.c

index 731f995bbdfc7f7b52e36f4472df4a6fba00dd81..10ba7fbf65ac46a4663b53b89798f7e033bfe512 100644 (file)
@@ -474,11 +474,7 @@ int main(int argc, char **argv)
                        policy_given = true;
                        break;
                case 'p':
-                       if (argc - optind == 0)
-                               errx(EXIT_FAILURE, _("too few arguments"));
-                       errno = 0;
-                       /* strtopid_or_err() is not suitable here; 0 can be passed.*/
-                       ctl->pid = strtos32_or_err(argv[argc - 1], _("invalid PID argument"));
+                       ctl->pid = 0;  /* indicate that a PID is expected */
                        break;
                case 'r':
                        ctl->policy = SCHED_RR;
@@ -507,20 +503,22 @@ int main(int argc, char **argv)
                }
        }
 
-       if (argc - optind < (ctl->pid > -1 ? 1 : 2)) {
+       if (argc - optind < (ctl->pid == 0 ? 1 : 2)) {
                warnx(_("too few arguments"));
                errtryhelp(EXIT_FAILURE);
        }
 
-       /* pid exists but priority not given */
-       if (ctl->pid > -1 && argc - optind == 1) {
-               /* Error if priority is missing for a policy that requires it */
-               if (policy_given && need_prio)
-                       errx(EXIT_FAILURE, ("policy %s requires a priority argument"),
+       /* If option --pid was given, parse the very last argument as a PID. */
+       if (ctl->pid == 0) {
+               if (need_prio && argc - optind < 2)
+                       errx(EXIT_FAILURE, _("policy %s requires a priority argument"),
                                                get_policy_name(ctl->policy));
+               errno = 0;
+               /* strtopid_or_err() is not suitable here, as 0 can be passed. */
+               ctl->pid = strtos32_or_err(argv[argc - 1], _("invalid PID argument"));
 
-               /* If no policy specified, show current settings */
-               if (!policy_given) {
+               /* If no policy nor priority was given, show current settings. */
+               if (!policy_given && argc - optind == 1) {
                        show_sched_info(ctl);
                        return EXIT_SUCCESS;
                }