]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - schedutils/ionice.c
rev: be careful with close()
[thirdparty/util-linux.git] / schedutils / ionice.c
index a3edf551239927d86a960b04b9d0909b40231970..ae2c73ea5b5b2f928bebbee52e26644f3c4a8a74 100644 (file)
@@ -51,7 +51,7 @@ enum {
 #define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK)
 #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
 
-const char *to_prio[] = {
+static const char *to_prio[] = {
        [IOPRIO_CLASS_NONE] = "none",
        [IOPRIO_CLASS_RT]   = "realtime",
        [IOPRIO_CLASS_BE]   = "best-effort",
@@ -98,32 +98,34 @@ static void ioprio_setid(int which, int ioclass, int data, int who)
                err(EXIT_FAILURE, _("ioprio_set failed"));
 }
 
-static void __attribute__ ((__noreturn__)) usage(FILE * out)
+static void __attribute__((__noreturn__)) usage(void)
 {
-       fprintf(out,
-              _("\n"
-                "%1$s - sets or gets process io scheduling class and priority.\n"
-                "\n"
-                "Usage:\n"
-                "  %1$s [OPTION] -p PID [PID...]\n"
-                "  %1$s [OPTION] -P PGID [PGID...]\n"
-                "  %1$s [OPTION] -u UID [UID...]\n"
-                "  %1$s [OPTION] COMMAND\n"
-                "\n"
-                "Options:\n"
-                "  -c, --class <class>   scheduling class name or number\n"
-                "                           0: none, 1: realtime, 2: best-effort, 3: idle\n"
-                "  -n, --classdata <num> scheduling class data\n"
-                "                           0-7 for realtime and best-effort classes\n"
-                "  -p, --pid <pid>       view or modify already running process\n"
-                "  -P, --pgid <pgrp>     view or modify already running process group\n"
-                "  -t, --ignore          ignore failures\n"
-                "  -u, --uid <uid>       view or modify already running processes owned by a user\n"
-                "  -V, --version         output version information and exit\n"
-                "  -h, --help            display this help and exit\n\n"),
-               program_invocation_short_name);
-
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
+       fprintf(out,  _(" %1$s [options] -p <pid>...\n"
+                       " %1$s [options] -P <pgid>...\n"
+                       " %1$s [options] -u <uid>...\n"
+                       " %1$s [options] <command>\n"), program_invocation_short_name);
+
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Show or change the I/O-scheduling class and priority of a process.\n"), out);
+
+       fputs(USAGE_OPTIONS, out);
+       fputs(_(" -c, --class <class>    name or number of scheduling class,\n"
+               "                          0: none, 1: realtime, 2: best-effort, 3: idle\n"), out);
+       fputs(_(" -n, --classdata <num>  priority (0..7) in the specified scheduling class,\n"
+               "                          only for the realtime and best-effort classes\n"), out);
+       fputs(_(" -p, --pid <pid>...     act on these already running processes\n"), out);
+       fputs(_(" -P, --pgid <pgrp>...   act on already running processes in these groups\n"), out);
+       fputs(_(" -t, --ignore           ignore failures\n"), out);
+       fputs(_(" -u, --uid <uid>...     act on already running processes owned by these users\n"), out);
+
+       fputs(USAGE_SEPARATOR, out);
+       printf(USAGE_HELP_OPTIONS(24));
+
+       printf(USAGE_MAN_TAIL("ionice(1)"));
+
+       exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char **argv)
@@ -147,7 +149,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while ((c = getopt_long(argc, argv, "+n:c:p:P:u:tVh", longopts, NULL)) != EOF)
                switch (c) {
@@ -171,7 +173,7 @@ int main(int argc, char **argv)
                case 'p':
                        if (who)
                                errx(EXIT_FAILURE,
-                                    _("can handle one of pid, pgid or uid at once"));
+                                    _("can handle only one of pid, pgid or uid at once"));
                        invalid_msg = _("invalid PID argument");
                        which = strtos32_or_err(optarg, invalid_msg);
                        who = IOPRIO_WHO_PROCESS;
@@ -179,7 +181,7 @@ int main(int argc, char **argv)
                case 'P':
                        if (who)
                                errx(EXIT_FAILURE,
-                                    _("can handle one of pid, pgid or uid at once"));
+                                    _("can handle only one of pid, pgid or uid at once"));
                        invalid_msg = _("invalid PGID argument");
                        which = strtos32_or_err(optarg, invalid_msg);
                        who = IOPRIO_WHO_PGRP;
@@ -187,21 +189,21 @@ int main(int argc, char **argv)
                case 'u':
                        if (who)
                                errx(EXIT_FAILURE,
-                                    _("can handle one of pid, pgid or uid at once"));
+                                    _("can handle only one of pid, pgid or uid at once"));
+                       invalid_msg = _("invalid UID argument");
                        which = strtos32_or_err(optarg, invalid_msg);
                        who = IOPRIO_WHO_USER;
                        break;
                case 't':
                        tolerant = 1;
                        break;
+
                case 'V':
-                       printf(_("%s from %s\n"),
-                               program_invocation_short_name, PACKAGE_STRING);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'h':
-                       usage(stdout);
+                       usage();
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
 
        switch (ioclass) {
@@ -255,10 +257,11 @@ int main(int argc, char **argv)
                 */
                ioprio_setid(0, ioclass, data, IOPRIO_WHO_PROCESS);
                execvp(argv[optind], &argv[optind]);
-               err(EXIT_FAILURE, _("failed to execute %s"), argv[optind]);
-       } else
-               usage(stderr);
-
+               errexec(argv[optind]);
+       } else {
+               warnx(_("bad usage"));
+               errtryhelp(EXIT_FAILURE);
+       }
 
        return EXIT_SUCCESS;
 }