aboutsummaryrefslogtreecommitdiff
path: root/miscutils/taskset.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-07-11 13:57:08 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-07-11 13:57:08 +0000
commit0e52541917a80a4c5aee9d32fcc81cf9967f2aed (patch)
tree26351601d9b046a8834e9a462dc261c21877de9a /miscutils/taskset.c
parent88643a86d07f31779b96f0beef23050b951e2fd1 (diff)
downloadbusybox-w32-0e52541917a80a4c5aee9d32fcc81cf9967f2aed.tar.gz
busybox-w32-0e52541917a80a4c5aee9d32fcc81cf9967f2aed.tar.bz2
busybox-w32-0e52541917a80a4c5aee9d32fcc81cf9967f2aed.zip
taskset: fix some careless code in both fancy and non-fancy cases.
-5 bytes for fancy, +5 for non-fancy
Diffstat (limited to 'miscutils/taskset.c')
-rw-r--r--miscutils/taskset.c48
1 files changed, 35 insertions, 13 deletions
diff --git a/miscutils/taskset.c b/miscutils/taskset.c
index 3175af10d..b43d42e90 100644
--- a/miscutils/taskset.c
+++ b/miscutils/taskset.c
@@ -11,31 +11,53 @@
11 11
12#if ENABLE_FEATURE_TASKSET_FANCY 12#if ENABLE_FEATURE_TASKSET_FANCY
13#define TASKSET_PRINTF_MASK "%s" 13#define TASKSET_PRINTF_MASK "%s"
14#define from_cpuset(x) __from_cpuset(&x)
15/* craft a string from the mask */ 14/* craft a string from the mask */
16static char *__from_cpuset(cpu_set_t *mask) 15static char *from_cpuset(cpu_set_t *mask)
17{ 16{
18 int i; 17 int i;
19 char *ret = 0, *str = xzalloc(9); 18 char *ret = NULL;
19 char *str = xzalloc((CPU_SETSIZE / 4) + 1); /* we will leak it */
20 20
21 for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) { 21 for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
22 char val = 0; 22 int val = 0;
23 int off; 23 int off;
24 for (off = 0; off <= 3; ++off) 24 for (off = 0; off <= 3; ++off)
25 if (CPU_ISSET(i+off, mask)) 25 if (CPU_ISSET(i + off, mask))
26 val |= 1<<off; 26 val |= 1 << off;
27
28 if (!ret && val) 27 if (!ret && val)
29 ret = str; 28 ret = str;
30 *str++ = (val-'0'<=9) ? (val+48) : (val+87); 29 *str++ = bb_hexdigits_upcase[val] | 0x20;
31 } 30 }
32 return ret; 31 return ret;
33} 32}
34#else 33#else
35#define TASKSET_PRINTF_MASK "%x" 34#define TASKSET_PRINTF_MASK "%llx"
36/* (void*) cast is for battling gcc: */ 35static unsigned long long from_cpuset(cpu_set_t *mask)
37/* "dereferencing type-punned pointer will break strict-aliasing rules" */ 36{
38#define from_cpuset(mask) (*(unsigned*)(void*)&(mask)) 37 struct BUG_CPU_SETSIZE_is_too_small {
38 char BUG_CPU_SETSIZE_is_too_small[
39 CPU_SETSIZE < sizeof(int) ? -1 : 1];
40 };
41 char *p = (void*)mask;
42
43 /* Take the least significant bits. Careful!
44 * Consider both CPU_SETSIZE=4 and CPU_SETSIZE=1024 cases
45 */
46#if BB_BIG_ENDIAN
47 /* For big endian, it means LAST bits */
48 if (CPU_SETSIZE < sizeof(long))
49 p += CPU_SETSIZE - sizeof(int);
50 else if (CPU_SETSIZE < sizeof(long long))
51 p += CPU_SETSIZE - sizeof(long);
52 else
53 p += CPU_SETSIZE - sizeof(long long);
54#endif
55 if (CPU_SETSIZE < sizeof(long))
56 return *(unsigned*)p;
57 if (CPU_SETSIZE < sizeof(long long))
58 return *(unsigned long*)p;
59 return *(unsigned long long*)p;
60}
39#endif 61#endif
40 62
41 63
@@ -78,7 +100,7 @@ int taskset_main(int argc UNUSED_PARAM, char **argv)
78 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0) 100 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
79 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid); 101 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
80 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", 102 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
81 pid, current_new, from_cpuset(mask)); 103 pid, current_new, from_cpuset(&mask));
82 if (!*argv) { 104 if (!*argv) {
83 /* Either it was just "-p <pid>", 105 /* Either it was just "-p <pid>",
84 * or it was "-p <aff> <pid>" and we came here 106 * or it was "-p <aff> <pid>" and we came here