- revert r15563 (pull current version of taskset off the busybox_scratch branch)
[oweals/busybox.git] / miscutils / taskset.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * taskset - retrieve or set a processes' CPU affinity
4  * Copyright (c) 2006 Bernhard Fischer
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7  */
8
9 #include "busybox.h"
10 #include <sched.h>
11 #include <unistd.h>
12 #include <getopt.h> /* optind */
13
14 #if ENABLE_FEATURE_TASKSET_FANCY
15 #define TASKSET_PRINTF_MASK "%s"
16 #define from_cpuset(x) __from_cpuset(&x)
17 /* craft a string from the mask */
18 static char *__from_cpuset(cpu_set_t *mask) {
19         int i;
20         char *ret = 0, *str = xzalloc(9);
21
22         for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
23                 char val = 0;
24                 int off;
25                 for (off = 0; off <= 3; ++off)
26                         if (CPU_ISSET(i+off, mask))
27                                 val |= 1<<off;
28
29                 if (!ret && val)
30                         ret = str;
31                 *str++ = (val-'0'<=9) ? (val+48) : (val+87);
32         }
33         return ret;
34 }
35 #else
36 #define TASKSET_PRINTF_MASK "%x"
37 #define from_cpuset(mask) mask
38 #endif
39
40 #define TASKSET_OPT_p (1)
41
42 int taskset_main(int argc, char** argv)
43 {
44         cpu_set_t mask, new_mask;
45         pid_t pid = 0;
46         unsigned long ul;
47         const char *state = "current\0new";
48         char *p_opt = NULL, *aff = NULL;
49
50         ul = getopt32(argc, argv, "+p:", &p_opt);
51
52         if (ul & TASKSET_OPT_p) {
53                 if (argc == optind+1) { /* -p <aff> <pid> */
54                         aff = p_opt;
55                         p_opt = argv[optind];
56                 }
57                 argv += optind; /* me -p <arg> */
58                 pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */
59         } else
60                 aff = *++argv; /* <aff> <cmd...> */
61         if (aff) {
62                 unsigned i = 0;
63                 unsigned long l = xstrtol_range(aff, 16, 1, ULONG_MAX);
64
65                 CPU_ZERO(&new_mask);
66                 while (i < CPU_SETSIZE && l >= (1<<i)) {
67                         if ((1<<i) & l)
68                                 CPU_SET(i, &new_mask);
69                         ++i;
70                 }
71         }
72
73         if (ul & TASKSET_OPT_p) {
74 print_aff:
75                 if (sched_getaffinity(pid, sizeof (mask), &mask) < 0)
76                         bb_perror_msg_and_die("Failed to %cet pid %d's affinity", 'g', pid);
77                 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
78                                 pid, state, from_cpuset(mask));
79                 if (!*argv) /* no new affinity given or we did print already, done. */
80                         return EXIT_SUCCESS;
81         }
82
83         if (sched_setaffinity(pid, sizeof (new_mask), &new_mask))
84                 bb_perror_msg_and_die("Failed to %cet pid %d's affinity", 's', pid);
85         if (ul & TASKSET_OPT_p) {
86                 state += 8;
87                 ++argv;
88                 goto print_aff;
89         }
90         ++argv;
91         execvp(*argv, argv);
92         bb_perror_msg_and_die("%s", *argv);
93 }
94 #undef TASKSET_OPT_p
95 #undef TASKSET_PRINTF_MASK
96 #undef from_cpuset