]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - schedutils/taskset.c
libmount: fix comment referring to passno field
[thirdparty/util-linux.git] / schedutils / taskset.c
index 908ac8e2d8535902085a12ebd7f1f06db08bf723..56db817924dcb268c456618930de18f228b7f550 100644 (file)
@@ -1,9 +1,8 @@
 /*
- * taskset.c - command-line utility for setting and retrieving
- *             a task's CPU affinity
+ * taskset.c - set or retrieve a task's CPU affinity
  *
  * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, v2, as
+ * it under the terms of the GNU General Public License, version 2, as
  * published by the Free Software Foundation
  *
  * This program is distributed in the hope that it will be useful,
@@ -46,20 +45,26 @@ struct taskset {
                        get_only:1;     /* print the mask, but not modify */
 };
 
-static void __attribute__((__noreturn__)) usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fprintf(out,
                _("Usage: %s [options] [mask | cpu-list] [pid|cmd [args...]]\n\n"),
                program_invocation_short_name);
 
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Show or change the CPU affinity of a process.\n"), out);
+       fputs(USAGE_SEPARATOR, out);
+
        fprintf(out, _(
                "Options:\n"
                " -a, --all-tasks         operate on all the tasks (threads) for a given pid\n"
                " -p, --pid               operate on existing given pid\n"
                " -c, --cpu-list          display and specify cpus in list format\n"
-               " -h, --help              display this help\n"
-               " -V, --version           output version information\n\n"));
+               ));
+       printf(USAGE_HELP_OPTIONS(25));
 
+       fputs(USAGE_SEPARATOR, out);
        fprintf(out, _(
                "The default behavior is to run a new command:\n"
                "    %1$s 03 sshd -b 1024\n"
@@ -73,9 +78,8 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
                "    e.g. 0-31:2 is equivalent to mask 0x55555555\n"),
                program_invocation_short_name);
 
-       fprintf(out, _("\nFor more information see taskset(1).\n"));
-
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       printf(USAGE_MAN_TAIL("taskset(1)"));
+       exit(EXIT_SUCCESS);
 }
 
 static void print_affinity(struct taskset *ts, int isnew)
@@ -93,10 +97,19 @@ static void print_affinity(struct taskset *ts, int isnew)
        }
 
        if (!str)
-               /* this is internal error... */
-               errx(EXIT_FAILURE, _("conversion from cpuset to string failed"));
+               errx(EXIT_FAILURE, _("internal error: conversion from cpuset to string failed"));
 
-       printf(msg, ts->pid, str);
+       printf(msg, ts->pid ? ts->pid : getpid(), str);
+}
+
+static void __attribute__((__noreturn__)) err_affinity(pid_t pid, int set)
+{
+       char *msg;
+
+       msg = set ? _("failed to set pid %d's affinity") :
+                   _("failed to get pid %d's affinity");
+
+       err(EXIT_FAILURE, msg, pid ? pid : getpid());
 }
 
 static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set)
@@ -104,8 +117,7 @@ static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set)
        /* read the current mask */
        if (ts->pid) {
                if (sched_getaffinity(ts->pid, ts->setsize, ts->set) < 0)
-                       err(EXIT_FAILURE, _("failed to get pid %d's affinity"),
-                           ts->pid);
+                       err_affinity(ts->pid, 1);
                print_affinity(ts, FALSE);
        }
 
@@ -114,14 +126,12 @@ static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set)
 
        /* set new mask */
        if (sched_setaffinity(ts->pid, setsize, set) < 0)
-               err(EXIT_FAILURE, _("failed to set pid %d's affinity"),
-                   ts->pid);
+               err_affinity(ts->pid, 1);
 
        /* re-read the current mask */
        if (ts->pid) {
                if (sched_getaffinity(ts->pid, ts->setsize, ts->set) < 0)
-                       err(EXIT_FAILURE, _("failed to get pid %d's affinity"),
-                           ts->pid);
+                       err_affinity(ts->pid, 0);
                print_affinity(ts, TRUE);
        }
 }
@@ -147,7 +157,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        memset(&ts, 0, sizeof(ts));
 
@@ -157,28 +167,27 @@ int main(int argc, char **argv)
                        all_tasks = 1;
                        break;
                case 'p':
-                       pid = strtol_or_err(argv[argc - 1],
-                                           _("failed to parse pid"));
+                       pid = strtos32_or_err(argv[argc - 1],
+                                           _("invalid PID argument"));
                        break;
                case 'c':
                        ts.use_list = 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);
-                       break;
+                       usage();
                default:
-                       usage(stderr);
-                       break;
+                       errtryhelp(EXIT_FAILURE);
                }
        }
 
        if ((!pid && argc - optind < 2)
-           || (pid && (argc - optind < 1 || argc - optind > 2)))
-               usage(stderr);
+           || (pid && (argc - optind < 1 || argc - optind > 2))) {
+               warnx(_("bad usage"));
+               errtryhelp(EXIT_FAILURE);
+       }
 
        ncpus = get_max_number_of_cpus();
        if (ncpus <= 0)
@@ -218,7 +227,7 @@ int main(int argc, char **argv)
                     argv[optind]);
        }
 
-       if (all_tasks) {
+       if (all_tasks && pid) {
                struct proc_tasks *tasks = proc_open_tasks(pid);
                while (!proc_next_tid(tasks, &ts.pid))
                        do_taskset(&ts, new_setsize, new_set);
@@ -235,7 +244,7 @@ int main(int argc, char **argv)
        if (!pid) {
                argv += optind + 1;
                execvp(argv[0], argv);
-               err(EXIT_FAILURE, _("executing %s failed"), argv[0]);
+               errexec(argv[0]);
        }
 
        return EXIT_SUCCESS;