patch: shrink by Pascal Bellard <pascal.bellard AT ads-lu.com> (-80 bytes)
[platform/upstream/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 <sched.h>
10 #include "libbb.h"
11
12 #if ENABLE_FEATURE_TASKSET_FANCY
13 #define TASKSET_PRINTF_MASK "%s"
14 #define from_cpuset(x) __from_cpuset(&x)
15 /* craft a string from the mask */
16 static char *__from_cpuset(cpu_set_t *mask)
17 {
18         int i;
19         char *ret = 0, *str = xzalloc(9);
20
21         for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
22                 char val = 0;
23                 int off;
24                 for (off = 0; off <= 3; ++off)
25                         if (CPU_ISSET(i+off, mask))
26                                 val |= 1<<off;
27
28                 if (!ret && val)
29                         ret = str;
30                 *str++ = (val-'0'<=9) ? (val+48) : (val+87);
31         }
32         return ret;
33 }
34 #else
35 #define TASKSET_PRINTF_MASK "%x"
36 /* (void*) cast is for battling gcc: */
37 /* "dereferencing type-punned pointer will break strict-aliasing rules" */
38 #define from_cpuset(mask) (*(unsigned*)(void*)&(mask))
39 #endif
40
41
42 int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
43 int taskset_main(int argc ATTRIBUTE_UNUSED, char **argv)
44 {
45         cpu_set_t mask;
46         pid_t pid = 0;
47         unsigned opt_p;
48         const char *current_new;
49         char *pid_str;
50         char *aff = aff; /* for compiler */
51
52         /* NB: we mimic util-linux's taskset: -p does not take
53          * an argument, i.e., "-pN" is NOT valid, only "-p N"!
54          * Indeed, util-linux-2.13-pre7 uses:
55          * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
56
57         opt_complementary = "-1"; /* at least 1 arg */
58         opt_p = getopt32(argv, "+p");
59         argv += optind;
60
61         if (opt_p) {
62                 pid_str = *argv++;
63                 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
64                         aff = pid_str;
65                         pid_str = *argv; /* NB: *argv != NULL in this case */
66                 }
67                 /* else it was just "-p <pid>", and *argv == NULL */
68                 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
69         } else {
70                 aff = *argv++; /* <aff> <cmd...> */
71                 if (!*argv)
72                         bb_show_usage();
73         }
74
75         current_new = "current\0new";
76         if (opt_p) {
77  print_aff:
78                 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
79                         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",
81                                 pid, current_new, from_cpuset(mask));
82                 if (!*argv) {
83                         /* Either it was just "-p <pid>",
84                          * or it was "-p <aff> <pid>" and we came here
85                          * for the second time (see goto below) */
86                         return EXIT_SUCCESS;
87                 }
88                 *argv = NULL;
89                 current_new += 8; /* "new" */
90         }
91
92         { /* Affinity was specified, translate it into cpu_set_t */
93                 unsigned i;
94                 /* Do not allow zero mask: */
95                 unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
96                 CPU_ZERO(&mask);
97                 for (i = 0; i < CPU_SETSIZE; i++) {
98                         unsigned long long bit = (1ULL << i);
99                         if (bit & m)
100                                 CPU_SET(i, &mask);
101                 }
102         }
103
104         /* Set pid's or our own (pid==0) affinity */
105         if (sched_setaffinity(pid, sizeof(mask), &mask))
106                 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
107
108         if (!*argv) /* "-p <aff> <pid> [...ignored...]" */
109                 goto print_aff; /* print new affinity and exit */
110
111         BB_EXECVP(*argv, argv);
112         bb_simple_perror_msg_and_die(*argv);
113 }