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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include <sched.h>
51#include "libbb.h"
52
53#if ENABLE_FEATURE_TASKSET_FANCY
54#define TASKSET_PRINTF_MASK "%s"
55
56static char *from_cpuset(cpu_set_t *mask)
57{
58 int i;
59 char *ret = NULL;
60 char *str = xzalloc((CPU_SETSIZE / 4) + 1);
61
62 for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
63 int val = 0;
64 int off;
65 for (off = 0; off <= 3; ++off)
66 if (CPU_ISSET(i + off, mask))
67 val |= 1 << off;
68 if (!ret && val)
69 ret = str;
70 *str++ = bb_hexdigits_upcase[val] | 0x20;
71 }
72 return ret;
73}
74#else
75#define TASKSET_PRINTF_MASK "%llx"
76static unsigned long long from_cpuset(cpu_set_t *mask)
77{
78 struct BUG_CPU_SETSIZE_is_too_small {
79 char BUG_CPU_SETSIZE_is_too_small[
80 CPU_SETSIZE < sizeof(int) ? -1 : 1];
81 };
82 char *p = (void*)mask;
83
84
85
86
87#if BB_BIG_ENDIAN
88
89 if (CPU_SETSIZE < sizeof(long))
90 p += CPU_SETSIZE - sizeof(int);
91 else if (CPU_SETSIZE < sizeof(long long))
92 p += CPU_SETSIZE - sizeof(long);
93 else
94 p += CPU_SETSIZE - sizeof(long long);
95#endif
96 if (CPU_SETSIZE < sizeof(long))
97 return *(unsigned*)p;
98 if (CPU_SETSIZE < sizeof(long long))
99 return *(unsigned long*)p;
100 return *(unsigned long long*)p;
101}
102#endif
103
104
105int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
106int taskset_main(int argc UNUSED_PARAM, char **argv)
107{
108 cpu_set_t mask;
109 pid_t pid = 0;
110 unsigned opt_p;
111 const char *current_new;
112 char *pid_str;
113 char *aff = aff;
114
115
116
117
118
119
120 opt_complementary = "-1";
121 opt_p = getopt32(argv, "+p");
122 argv += optind;
123
124 if (opt_p) {
125 pid_str = *argv++;
126 if (*argv) {
127 aff = pid_str;
128 pid_str = *argv;
129 }
130
131 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
132 } else {
133 aff = *argv++;
134 if (!*argv)
135 bb_show_usage();
136 }
137
138 current_new = "current\0new";
139 if (opt_p) {
140 print_aff:
141 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
142 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
143 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
144 pid, current_new, from_cpuset(&mask));
145 if (!*argv) {
146
147
148
149 return EXIT_SUCCESS;
150 }
151 *argv = NULL;
152 current_new += 8;
153 }
154
155
156 CPU_ZERO(&mask);
157 if (!ENABLE_FEATURE_TASKSET_FANCY) {
158 unsigned i;
159 unsigned long long m;
160
161
162 m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
163 i = 0;
164 do {
165 if (m & 1)
166 CPU_SET(i, &mask);
167 i++;
168 m >>= 1;
169 } while (m != 0);
170 } else {
171 unsigned i;
172 char *last_byte;
173 char *bin;
174 uint8_t bit_in_byte;
175
176
177 bin = xstrdup(aff);
178
179 if (aff[0] != '0' || (aff[1]|0x20) != 'x') {
180
181 unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
182 bin += strlen(bin);
183 last_byte = bin - 1;
184 while (m) {
185 *--bin = m & 0xff;
186 m >>= 8;
187 }
188 } else {
189
190 last_byte = hex2bin(bin, aff + 2, INT_MAX);
191 if (!last_byte) {
192 bad_aff:
193 bb_error_msg_and_die("bad affinity '%s'", aff);
194 }
195 last_byte--;
196 }
197
198 i = 0;
199 bit_in_byte = 1;
200 while (last_byte >= bin) {
201 if (bit_in_byte & *last_byte) {
202 if (i >= CPU_SETSIZE)
203 goto bad_aff;
204 CPU_SET(i, &mask);
205
206 }
207 i++;
208
209 bit_in_byte = (bit_in_byte << 1);
210 if (!bit_in_byte) {
211 bit_in_byte = 1;
212 last_byte--;
213 }
214 }
215 }
216
217
218 if (sched_setaffinity(pid, sizeof(mask), &mask))
219 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
220
221 if (!argv[0])
222 goto print_aff;
223
224 BB_EXECVP_or_die(argv);
225}
226