1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <sched.h>
27#include "libbb.h"
28
29#if ENABLE_FEATURE_TASKSET_FANCY
30#define TASKSET_PRINTF_MASK "%s"
31
32static char *from_cpuset(cpu_set_t *mask)
33{
34 int i;
35 char *ret = NULL;
36 char *str = xzalloc((CPU_SETSIZE / 4) + 1);
37
38 for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
39 int val = 0;
40 int off;
41 for (off = 0; off <= 3; ++off)
42 if (CPU_ISSET(i + off, mask))
43 val |= 1 << off;
44 if (!ret && val)
45 ret = str;
46 *str++ = bb_hexdigits_upcase[val] | 0x20;
47 }
48 return ret;
49}
50#else
51#define TASKSET_PRINTF_MASK "%llx"
52static unsigned long long from_cpuset(cpu_set_t *mask)
53{
54 struct BUG_CPU_SETSIZE_is_too_small {
55 char BUG_CPU_SETSIZE_is_too_small[
56 CPU_SETSIZE < sizeof(int) ? -1 : 1];
57 };
58 char *p = (void*)mask;
59
60
61
62
63#if BB_BIG_ENDIAN
64
65 if (CPU_SETSIZE < sizeof(long))
66 p += CPU_SETSIZE - sizeof(int);
67 else if (CPU_SETSIZE < sizeof(long long))
68 p += CPU_SETSIZE - sizeof(long);
69 else
70 p += CPU_SETSIZE - sizeof(long long);
71#endif
72 if (CPU_SETSIZE < sizeof(long))
73 return *(unsigned*)p;
74 if (CPU_SETSIZE < sizeof(long long))
75 return *(unsigned long*)p;
76 return *(unsigned long long*)p;
77}
78#endif
79
80
81int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
82int taskset_main(int argc UNUSED_PARAM, char **argv)
83{
84 cpu_set_t mask;
85 pid_t pid = 0;
86 unsigned opt_p;
87 const char *current_new;
88 char *pid_str;
89 char *aff = aff;
90
91
92
93
94
95
96 opt_complementary = "-1";
97 opt_p = getopt32(argv, "+p");
98 argv += optind;
99
100 if (opt_p) {
101 pid_str = *argv++;
102 if (*argv) {
103 aff = pid_str;
104 pid_str = *argv;
105 }
106
107 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
108 } else {
109 aff = *argv++;
110 if (!*argv)
111 bb_show_usage();
112 }
113
114 current_new = "current\0new";
115 if (opt_p) {
116 print_aff:
117 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
118 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
119 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
120 pid, current_new, from_cpuset(&mask));
121 if (!*argv) {
122
123
124
125 return EXIT_SUCCESS;
126 }
127 *argv = NULL;
128 current_new += 8;
129 }
130
131 {
132 unsigned i;
133
134 unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
135 enum { CNT_BIT = CPU_SETSIZE < sizeof(m)*8 ? CPU_SETSIZE : sizeof(m)*8 };
136
137 CPU_ZERO(&mask);
138 for (i = 0; i < CNT_BIT; i++) {
139 unsigned long long bit = (1ULL << i);
140 if (bit & m)
141 CPU_SET(i, &mask);
142 }
143 }
144
145
146 if (sched_setaffinity(pid, sizeof(mask), &mask))
147 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
148
149 if (!argv[0])
150 goto print_aff;
151
152 BB_EXECVP_or_die(argv);
153}
154