]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/chcpu.c
misc: use %m in format string instead of %s and strerror(errno)
[thirdparty/util-linux.git] / sys-utils / chcpu.c
1 /*
2 * chcpu - CPU configuration tool
3 *
4 * Copyright IBM Corp. 2011
5 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it would be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <getopt.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/utsname.h>
31 #include <unistd.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include "cpuset.h"
37 #include "nls.h"
38 #include "xalloc.h"
39 #include "c.h"
40 #include "strutils.h"
41 #include "bitops.h"
42 #include "path.h"
43
44 #define _PATH_SYS_CPU "/sys/devices/system/cpu"
45 #define _PATH_SYS_CPU_ONLINE _PATH_SYS_CPU "/online"
46 #define _PATH_SYS_CPU_RESCAN _PATH_SYS_CPU "/rescan"
47 #define _PATH_SYS_CPU_DISPATCH _PATH_SYS_CPU "/dispatching"
48
49 static cpu_set_t *onlinecpus;
50 static size_t maxcpus;
51
52 #define is_cpu_online(cpu) (CPU_ISSET_S((cpu), CPU_ALLOC_SIZE(maxcpus), onlinecpus))
53 #define num_online_cpus() (CPU_COUNT_S(CPU_ALLOC_SIZE(maxcpus), onlinecpus))
54
55 enum {
56 CMD_CPU_ENABLE = 0,
57 CMD_CPU_DISABLE,
58 CMD_CPU_CONFIGURE,
59 CMD_CPU_DECONFIGURE,
60 CMD_CPU_RESCAN,
61 CMD_CPU_DISPATCH_HORIZONTAL,
62 CMD_CPU_DISPATCH_VERTICAL,
63 };
64
65 static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
66 {
67 unsigned int cpu;
68 int online, rc;
69 int configured = -1;
70
71 for (cpu = 0; cpu < setsize; cpu++) {
72 if (!CPU_ISSET(cpu, cpu_set))
73 continue;
74 if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) {
75 printf(_("CPU %d does not exist\n"), cpu);
76 continue;
77 }
78 if (!path_exist(_PATH_SYS_CPU "/cpu%d/online", cpu)) {
79 printf(_("CPU %d is not hot pluggable\n"), cpu);
80 continue;
81 }
82 online = path_getnum(_PATH_SYS_CPU "/cpu%d/online", cpu);
83 if ((online == 1) && (enable == 1)) {
84 printf(_("CPU %d is already enabled\n"), cpu);
85 continue;
86 }
87 if ((online == 0) && (enable == 0)) {
88 printf(_("CPU %d is already disabled\n"), cpu);
89 continue;
90 }
91 if (path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu))
92 configured = path_getnum(_PATH_SYS_CPU "/cpu%d/configure", cpu);
93 if (enable) {
94 rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/online", cpu);
95 if ((rc == -1) && (configured == 0))
96 printf(_("CPU %d enable failed "
97 "(CPU is deconfigured)\n"), cpu);
98 else if (rc == -1)
99 printf(_("CPU %d enable failed (%m)\n"), cpu);
100 else
101 printf(_("CPU %d enabled\n"), cpu);
102 } else {
103 if (onlinecpus && num_online_cpus() == 1) {
104 printf(_("CPU %d disable failed "
105 "(last enabled CPU)\n"), cpu);
106 continue;
107 }
108 rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/online", cpu);
109 if (rc == -1)
110 printf(_("CPU %d disable failed (%m)\n"), cpu);
111 else {
112 printf(_("CPU %d disabled\n"), cpu);
113 if (onlinecpus)
114 CPU_CLR(cpu, onlinecpus);
115 }
116 }
117 }
118 return EXIT_SUCCESS;
119 }
120
121 static int cpu_rescan(void)
122 {
123 if (!path_exist(_PATH_SYS_CPU_RESCAN))
124 errx(EXIT_FAILURE, _("This system does not support rescanning of CPUs"));
125 if (path_writestr("1", _PATH_SYS_CPU_RESCAN) == -1)
126 err(EXIT_FAILURE, _("Failed to trigger rescan of CPUs"));
127 printf(_("Triggered rescan of CPUs\n"));
128 return EXIT_SUCCESS;
129 }
130
131 static int cpu_set_dispatch(int mode)
132 {
133 if (!path_exist(_PATH_SYS_CPU_DISPATCH))
134 errx(EXIT_FAILURE, _("This system does not support setting "
135 "the dispatching mode of CPUs"));
136 if (mode == 0) {
137 if (path_writestr("0", _PATH_SYS_CPU_DISPATCH) == -1)
138 err(EXIT_FAILURE, _("Failed to set horizontal dispatch mode"));
139 printf(_("Succesfully set horizontal dispatching mode\n"));
140 } else {
141 if (path_writestr("1", _PATH_SYS_CPU_DISPATCH) == -1)
142 err(EXIT_FAILURE, _("Failed to set vertical dispatch mode"));
143 printf(_("Succesfully set vertical dispatching mode\n"));
144 }
145 return EXIT_SUCCESS;
146 }
147
148 static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
149 {
150 unsigned int cpu;
151 int rc, current;
152
153 for (cpu = 0; cpu < setsize; cpu++) {
154 if (!CPU_ISSET(cpu, cpu_set))
155 continue;
156 if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) {
157 printf(_("CPU %d does not exist\n"), cpu);
158 continue;
159 }
160 if (!path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) {
161 printf(_("CPU %d is not configurable\n"), cpu);
162 continue;
163 }
164 current = path_getnum(_PATH_SYS_CPU "/cpu%d/configure", cpu);
165 if ((current == 1) && (configure == 1)) {
166 printf(_("CPU %d is already configured\n"), cpu);
167 continue;
168 }
169 if ((current == 0) && (configure == 0)) {
170 printf(_("CPU %d is already deconfigured\n"), cpu);
171 continue;
172 }
173 if ((current == 1) && (configure == 0) && onlinecpus &&
174 is_cpu_online(cpu)) {
175 printf(_("CPU %d deconfigure failed "
176 "(CPU is enabled)\n"), cpu);
177 continue;
178 }
179 if (configure) {
180 rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/configure", cpu);
181 if (rc == -1)
182 printf(_("CPU %d configure failed (%m)\n"), cpu);
183 else
184 printf(_("CPU %d configured\n"), cpu);
185 } else {
186 rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/configure", cpu);
187 if (rc == -1)
188 printf(_("CPU %d deconfigure failed (%m)\n"), cpu);
189 else
190 printf(_("CPU %d deconfigured\n"), cpu);
191 }
192 }
193 return EXIT_SUCCESS;
194 }
195
196 static void cpu_parse(char *cpu_string, cpu_set_t *cpu_set, size_t setsize)
197 {
198 int rc;
199
200 rc = cpulist_parse(cpu_string, cpu_set, setsize, 1);
201 if (rc == 0)
202 return;
203 if (rc == 2)
204 errx(EXIT_FAILURE, _("invalid CPU number in CPU list: %s"), cpu_string);
205 errx(EXIT_FAILURE, _("failed to parse CPU list: %s"), cpu_string);
206 }
207
208 static void __attribute__((__noreturn__)) usage(FILE *out)
209 {
210 fprintf(out, _(
211 "\nUsage:\n"
212 " %s [options]\n"), program_invocation_short_name);
213
214 puts(_( "\nOptions:\n"
215 " -h, --help print this help\n"
216 " -e, --enable <cpu-list> enable cpus\n"
217 " -d, --disable <cpu-list> disable cpus\n"
218 " -c, --configure <cpu-list> configure cpus\n"
219 " -g, --deconfigure <cpu-list> deconfigure cpus\n"
220 " -p, --dispatch <mode> set dispatching mode\n"
221 " -r, --rescan trigger rescan of cpus\n"
222 " -V, --version output version information and exit\n"));
223
224 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
225 }
226
227 int main(int argc, char *argv[])
228 {
229 cpu_set_t *cpu_set;
230 size_t setsize;
231 int cmd = -1;
232 int c;
233
234 static const struct option longopts[] = {
235 { "configure", required_argument, 0, 'c' },
236 { "deconfigure",required_argument, 0, 'g' },
237 { "disable", required_argument, 0, 'd' },
238 { "dispatch", required_argument, 0, 'p' },
239 { "enable", required_argument, 0, 'e' },
240 { "help", no_argument, 0, 'h' },
241 { "rescan", no_argument, 0, 'r' },
242 { "version", no_argument, 0, 'V' },
243 { NULL, 0, 0, 0 }
244 };
245
246 setlocale(LC_ALL, "");
247 bindtextdomain(PACKAGE, LOCALEDIR);
248 textdomain(PACKAGE);
249
250 maxcpus = get_max_number_of_cpus();
251 if (maxcpus <= 0)
252 errx(EXIT_FAILURE, _("cannot determine NR_CPUS; aborting"));
253 if (path_exist(_PATH_SYS_CPU_ONLINE))
254 onlinecpus = path_cpulist(maxcpus, _PATH_SYS_CPU_ONLINE);
255 setsize = CPU_ALLOC_SIZE(maxcpus);
256 cpu_set = CPU_ALLOC(maxcpus);
257 if (!cpu_set)
258 err(EXIT_FAILURE, _("cpuset_alloc failed"));
259
260 while ((c = getopt_long(argc, argv, "c:d:e:g:hp:rV", longopts, NULL)) != -1) {
261 if (cmd != -1 && strchr("cdegpr", c))
262 errx(EXIT_FAILURE,
263 _("configure, deconfigure, disable, dispatch, enable "
264 "and rescan are mutually exclusive"));
265 switch (c) {
266 case 'c':
267 cmd = CMD_CPU_CONFIGURE;
268 cpu_parse(argv[optind - 1], cpu_set, setsize);
269 break;
270 case 'd':
271 cmd = CMD_CPU_DISABLE;
272 cpu_parse(argv[optind - 1], cpu_set, setsize);
273 break;
274 case 'e':
275 cmd = CMD_CPU_ENABLE;
276 cpu_parse(argv[optind - 1], cpu_set, setsize);
277 break;
278 case 'g':
279 cmd = CMD_CPU_DECONFIGURE;
280 cpu_parse(argv[optind - 1], cpu_set, setsize);
281 break;
282 case 'h':
283 usage(stdout);
284 case 'p':
285 if (strcmp("horizontal", argv[optind - 1]) == 0)
286 cmd = CMD_CPU_DISPATCH_HORIZONTAL;
287 else if (strcmp("vertical", argv[optind - 1]) == 0)
288 cmd = CMD_CPU_DISPATCH_VERTICAL;
289 else
290 errx(EXIT_FAILURE, _("unsupported argument: %s"),
291 argv[optind -1 ]);
292 break;
293 case 'r':
294 cmd = CMD_CPU_RESCAN;
295 break;
296 case 'V':
297 printf(_("%s from %s\n"), program_invocation_short_name,
298 PACKAGE_STRING);
299 return EXIT_SUCCESS;
300 default:
301 usage(stderr);
302 }
303 }
304
305 if ((argc == 1) || (argc != optind))
306 usage(stderr);
307
308 switch (cmd) {
309 case CMD_CPU_ENABLE:
310 return cpu_enable(cpu_set, maxcpus, 1);
311 case CMD_CPU_DISABLE:
312 return cpu_enable(cpu_set, maxcpus, 0);
313 case CMD_CPU_CONFIGURE:
314 return cpu_configure(cpu_set, maxcpus, 1);
315 case CMD_CPU_DECONFIGURE:
316 return cpu_configure(cpu_set, maxcpus, 0);
317 case CMD_CPU_RESCAN:
318 return cpu_rescan();
319 case CMD_CPU_DISPATCH_HORIZONTAL:
320 return cpu_set_dispatch(0);
321 case CMD_CPU_DISPATCH_VERTICAL:
322 return cpu_set_dispatch(1);
323 }
324 return EXIT_SUCCESS;
325 }