]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/setarch.c
Make the ways of using output stream consistent in usage()
[thirdparty/util-linux.git] / sys-utils / setarch.c
1 /*
2 * Copyright (C) 2003-2007 Red Hat, Inc.
3 *
4 * This file is part of util-linux.
5 *
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 *
17 * Written by Elliot Lee <sopwith@redhat.com>
18 * New personality options & code added by Jindrich Novy <jnovy@redhat.com>
19 * ADD_NO_RANDOMIZE flag added by Arjan van de Ven <arjanv@redhat.com>
20 * Help and MIPS support from Mike Frysinger (vapier@gentoo.org)
21 * Better error handling from Dmitry V. Levin (ldv@altlinux.org)
22 *
23 * based on ideas from the ppc32 util by Guy Streeter (2002-01), based on the
24 * sparc32 util by Jakub Jelinek (1998, 1999)
25 */
26
27 #include <sys/personality.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <getopt.h>
33 #include <limits.h>
34 #include <sys/utsname.h>
35 #include "nls.h"
36 #include "c.h"
37 #include "closestream.h"
38 #include "sysfs.h"
39 #include "strutils.h"
40
41 #ifndef HAVE_PERSONALITY
42 # include <syscall.h>
43 # define personality(pers) ((long)syscall(SYS_personality, pers))
44 #endif
45
46 #define turn_on(_flag, _opts) \
47 do { \
48 (_opts) |= _flag; \
49 if (verbose) \
50 printf(_("Switching on %s.\n"), #_flag); \
51 } while(0)
52
53 #ifndef UNAME26
54 # define UNAME26 0x0020000
55 #endif
56 #ifndef ADDR_NO_RANDOMIZE
57 # define ADDR_NO_RANDOMIZE 0x0040000
58 #endif
59 #ifndef FDPIC_FUNCPTRS
60 # define FDPIC_FUNCPTRS 0x0080000
61 #endif
62 #ifndef MMAP_PAGE_ZERO
63 # define MMAP_PAGE_ZERO 0x0100000
64 #endif
65 #ifndef ADDR_COMPAT_LAYOUT
66 # define ADDR_COMPAT_LAYOUT 0x0200000
67 #endif
68 #ifndef READ_IMPLIES_EXEC
69 # define READ_IMPLIES_EXEC 0x0400000
70 #endif
71 #ifndef ADDR_LIMIT_32BIT
72 # define ADDR_LIMIT_32BIT 0x0800000
73 #endif
74 #ifndef SHORT_INODE
75 # define SHORT_INODE 0x1000000
76 #endif
77 #ifndef WHOLE_SECONDS
78 # define WHOLE_SECONDS 0x2000000
79 #endif
80 #ifndef STICKY_TIMEOUTS
81 # define STICKY_TIMEOUTS 0x4000000
82 #endif
83 #ifndef ADDR_LIMIT_3GB
84 # define ADDR_LIMIT_3GB 0x8000000
85 #endif
86
87 /* fallback for old glibc-headers 2.17 */
88 #ifndef PER_LINUX_FDPIC
89 # define PER_LINUX_FDPIC (PER_LINUX | FDPIC_FUNCPTRS)
90 #endif
91
92 #define ALL_PERSONALITIES \
93 X(PER_LINUX) \
94 X(PER_LINUX_32BIT) \
95 X(PER_LINUX_FDPIC) \
96 X(PER_SVR4) \
97 X(PER_SVR3) \
98 X(PER_SCOSVR3) \
99 X(PER_OSR5) \
100 X(PER_WYSEV386) \
101 X(PER_ISCR4) \
102 X(PER_BSD) \
103 X(PER_SUNOS) \
104 X(PER_XENIX) \
105 X(PER_LINUX32) \
106 X(PER_LINUX32_3GB) \
107 X(PER_IRIX32) \
108 X(PER_IRIXN32) \
109 X(PER_IRIX64) \
110 X(PER_RISCOS) \
111 X(PER_SOLARIS) \
112 X(PER_UW7) \
113 X(PER_OSF4) \
114 X(PER_HPUX) \
115
116
117 #define ALL_OPTIONS \
118 X(UNAME26) \
119 X(ADDR_NO_RANDOMIZE) \
120 X(FDPIC_FUNCPTRS) \
121 X(MMAP_PAGE_ZERO) \
122 X(ADDR_COMPAT_LAYOUT) \
123 X(READ_IMPLIES_EXEC) \
124 X(ADDR_LIMIT_32BIT) \
125 X(SHORT_INODE) \
126 X(WHOLE_SECONDS) \
127 X(STICKY_TIMEOUTS) \
128 X(ADDR_LIMIT_3GB) \
129
130
131 struct arch_domain {
132 int perval; /* PER_* */
133 const char *target_arch;
134 const char *result_arch;
135 };
136
137
138 static void __attribute__((__noreturn__)) usage(int archwrapper)
139 {
140 fputs(USAGE_HEADER, stdout);
141 if (!archwrapper)
142 fprintf(stdout, _(" %s [<arch>] [options] [<program> [<argument>...]]\n"), program_invocation_short_name);
143 else
144 fprintf(stdout, _(" %s [options] [<program> [<argument>...]]\n"), program_invocation_short_name);
145
146 fputs(USAGE_SEPARATOR, stdout);
147 fputs(_("Change the reported architecture and set personality flags.\n"), stdout);
148
149 fputs(USAGE_OPTIONS, stdout);
150 fputs(_(" -B, --32bit turns on ADDR_LIMIT_32BIT\n"), stdout);
151 fputs(_(" -F, --fdpic-funcptrs makes function pointers point to descriptors\n"), stdout);
152 fputs(_(" -I, --short-inode turns on SHORT_INODE\n"), stdout);
153 fputs(_(" -L, --addr-compat-layout changes the way virtual memory is allocated\n"), stdout);
154 fputs(_(" -R, --addr-no-randomize disables randomization of the virtual address space\n"), stdout);
155 fputs(_(" -S, --whole-seconds turns on WHOLE_SECONDS\n"), stdout);
156 fputs(_(" -T, --sticky-timeouts turns on STICKY_TIMEOUTS\n"), stdout);
157 fputs(_(" -X, --read-implies-exec turns on READ_IMPLIES_EXEC\n"), stdout);
158 fputs(_(" -Z, --mmap-page-zero turns on MMAP_PAGE_ZERO\n"), stdout);
159 fputs(_(" -3, --3gb limits the used address space to a maximum of 3 GB\n"), stdout);
160 fputs(_(" --4gb ignored (for backward compatibility only)\n"), stdout);
161 fputs(_(" --uname-2.6 turns on UNAME26\n"), stdout);
162 fputs(_(" -v, --verbose say what options are being switched on\n"), stdout);
163
164 if (!archwrapper) {
165 fputs(_(" --list list settable architectures, and exit\n"), stdout);
166 fputs(_(" --show[=personality] show current or specific personality and exit\n"), stdout);
167 }
168
169 fputs(USAGE_SEPARATOR, stdout);
170 fprintf(stdout, USAGE_HELP_OPTIONS(26));
171 fprintf(stdout, USAGE_MAN_TAIL("setarch(8)"));
172
173 exit(EXIT_SUCCESS);
174 }
175
176 /*
177 * Returns inilialized list of all available execution domains.
178 */
179 static struct arch_domain *init_arch_domains(void)
180 {
181 static struct utsname un;
182 size_t i;
183
184 static struct arch_domain transitions[] =
185 {
186 {UNAME26, "uname26", NULL},
187 {PER_LINUX32, "linux32", NULL},
188 {PER_LINUX, "linux64", NULL},
189 #if defined(__powerpc__) || defined(__powerpc64__)
190 # ifdef __BIG_ENDIAN__
191 {PER_LINUX32, "ppc32", "ppc"},
192 {PER_LINUX32, "ppc", "ppc"},
193 {PER_LINUX, "ppc64", "ppc64"},
194 {PER_LINUX, "ppc64pseries", "ppc64"},
195 {PER_LINUX, "ppc64iseries", "ppc64"},
196 # else
197 {PER_LINUX32, "ppc32", "ppcle"},
198 {PER_LINUX32, "ppc", "ppcle"},
199 {PER_LINUX32, "ppc32le", "ppcle"},
200 {PER_LINUX32, "ppcle", "ppcle"},
201 {PER_LINUX, "ppc64le", "ppc64le"},
202 # endif
203 #endif
204 #if defined(__x86_64__) || defined(__i386__) || defined(__ia64__)
205 {PER_LINUX32, "i386", "i386"},
206 {PER_LINUX32, "i486", "i386"},
207 {PER_LINUX32, "i586", "i386"},
208 {PER_LINUX32, "i686", "i386"},
209 {PER_LINUX32, "athlon", "i386"},
210 #endif
211 #if defined(__x86_64__) || defined(__i386__)
212 {PER_LINUX, "x86_64", "x86_64"},
213 #endif
214 #if defined(__ia64__) || defined(__i386__)
215 {PER_LINUX, "ia64", "ia64"},
216 #endif
217 #if defined(__hppa__)
218 {PER_LINUX32, "parisc32", "parisc"},
219 {PER_LINUX32, "parisc", "parisc"},
220 {PER_LINUX, "parisc64", "parisc64"},
221 #endif
222 #if defined(__s390x__) || defined(__s390__)
223 {PER_LINUX32, "s390", "s390"},
224 {PER_LINUX, "s390x", "s390x"},
225 #endif
226 #if defined(__sparc64__) || defined(__sparc__)
227 {PER_LINUX32, "sparc", "sparc"},
228 {PER_LINUX32, "sparc32bash", "sparc"},
229 {PER_LINUX32, "sparc32", "sparc"},
230 {PER_LINUX, "sparc64", "sparc64"},
231 #endif
232 #if defined(__mips64__) || defined(__mips__)
233 {PER_LINUX32, "mips32", "mips"},
234 {PER_LINUX32, "mips", "mips"},
235 {PER_LINUX, "mips64", "mips64"},
236 #endif
237 #if defined(__alpha__)
238 {PER_LINUX, "alpha", "alpha"},
239 {PER_LINUX, "alphaev5", "alpha"},
240 {PER_LINUX, "alphaev56", "alpha"},
241 {PER_LINUX, "alphaev6", "alpha"},
242 {PER_LINUX, "alphaev67", "alpha"},
243 #endif
244 #if defined(__loongarch__)
245 {PER_LINUX, "loongarch", "loongarch64"},
246 {PER_LINUX, "loongarch64", "loongarch64"},
247 #endif
248 #if defined(__e2k__)
249 {PER_LINUX, "e2k", "e2k"},
250 {PER_LINUX, "e2kv4", "e2k"},
251 {PER_LINUX, "e2kv5", "e2k"},
252 {PER_LINUX, "e2kv6", "e2k"},
253 {PER_LINUX, "e2k4c", "e2k"},
254 {PER_LINUX, "e2k8c", "e2k"},
255 {PER_LINUX, "e2k1cp", "e2k"},
256 {PER_LINUX, "e2k8c2", "e2k"},
257 {PER_LINUX, "e2k12c", "e2k"},
258 {PER_LINUX, "e2k16c", "e2k"},
259 {PER_LINUX, "e2k2c3", "e2k"},
260 #endif
261 #if defined(__arm__) || defined(__aarch64__)
262 # ifdef __BIG_ENDIAN__
263 {PER_LINUX32, "armv7b", "arm"},
264 {PER_LINUX32, "armv8b", "arm"},
265 # else
266 {PER_LINUX32, "armv7l", "arm"},
267 {PER_LINUX32, "armv8l", "arm"},
268 # endif
269 {PER_LINUX32, "armh", "arm"},
270 {PER_LINUX32, "arm", "arm"},
271 {PER_LINUX, "arm64", "aarch64"},
272 {PER_LINUX, "aarch64", "aarch64"},
273 #endif
274 #if defined(__riscv)
275 {PER_LINUX32, "riscv32", "riscv32"},
276 {PER_LINUX32, "rv32", "riscv32"},
277 {PER_LINUX, "riscv64", "riscv64"},
278 {PER_LINUX, "rv64", "riscv64"},
279 #endif
280 /* place holder, will be filled up at runtime */
281 {-1, NULL, NULL},
282 {-1, NULL, NULL}
283 };
284
285 /* Add the trivial transition {PER_LINUX, machine, machine} if no
286 * such target_arch is hardcoded yet. */
287 uname(&un);
288 for (i = 0; transitions[i].perval >= 0; i++)
289 if (!strcmp(un.machine, transitions[i].target_arch))
290 break;
291 if (transitions[i].perval < 0) {
292 int wrdsz = sysfs_get_address_bits(NULL);
293 if (wrdsz < 0)
294 wrdsz = CHAR_BIT * sizeof(void *);
295 if (wrdsz == 32 || wrdsz == 64) {
296 /* fill up the place holder */
297 transitions[i].perval = wrdsz == 32 ? PER_LINUX32 : PER_LINUX;
298 transitions[i].target_arch = un.machine;
299 transitions[i].result_arch = un.machine;
300 }
301 }
302
303 return transitions;
304 }
305
306 /*
307 * List all execution domains from transitions
308 */
309 static void list_arch_domains(struct arch_domain *doms)
310 {
311 struct arch_domain *d;
312
313 for (d = doms; d->target_arch != NULL; d++)
314 printf("%s\n", d->target_arch);
315 }
316
317 static struct arch_domain *get_arch_domain(struct arch_domain *doms, const char *pers)
318 {
319 struct arch_domain *d;
320
321 for (d = doms; d && d->perval >= 0; d++) {
322 if (!strcmp(pers, d->target_arch))
323 break;
324 }
325
326 return !d || d->perval < 0 ? NULL : d;
327 }
328
329 static void verify_arch_domain(struct arch_domain *doms, struct arch_domain *target, const char *wanted)
330 {
331 struct utsname un;
332
333 if (!doms || !target || !target->result_arch)
334 return;
335
336 uname(&un);
337
338 if (!strcmp(un.machine, target->result_arch))
339 return;
340
341 if (!strcmp(target->result_arch, "i386") ||
342 !strcmp(target->result_arch, "arm")) {
343 struct arch_domain *dom;
344 for (dom = doms; dom->target_arch != NULL; dom++) {
345 if (!dom->result_arch || strcmp(dom->result_arch, target->result_arch))
346 continue;
347 if (!strcmp(dom->target_arch, un.machine))
348 return;
349 }
350 }
351
352 errx(EXIT_FAILURE, _("Kernel cannot set architecture to %s"), wanted);
353 }
354
355 static const struct { int value; const char * const name; } all_personalities[] = {
356 #define X(opt) { .value = opt, .name = #opt },
357 ALL_PERSONALITIES
358 #undef X
359 };
360
361 static const struct { int value; const char * const name; } all_options[] = {
362 #define X(opt) { .value = opt, .name = #opt },
363 ALL_OPTIONS
364 #undef X
365 };
366
367 static void show_personality(int pers)
368 {
369 int options;
370 size_t i;
371
372 /* Test for exact matches including options */
373 for (i = 0; i < ARRAY_SIZE(all_personalities); i++) {
374 if (pers == all_personalities[i].value) {
375 printf("%s\n", all_personalities[i].name);
376 return;
377 }
378 }
379
380 options = pers & ~PER_MASK;
381 pers &= PER_MASK;
382
383 /* Second test for type-only matches */
384 for (i = 0; i < ARRAY_SIZE(all_personalities); i++) {
385 if (pers == all_personalities[i].value) {
386 printf("%s", all_personalities[i].name);
387 break;
388 }
389 }
390
391 if (i == ARRAY_SIZE(all_personalities))
392 printf("0x%02x", pers);
393
394 if (options) {
395 printf(" (");
396
397 for (i = 0; i < ARRAY_SIZE(all_options); i++) {
398 if (options & all_options[i].value) {
399 printf("%s", all_options[i].name);
400
401 options &= ~all_options[i].value;
402 if (options)
403 printf(" ");
404 }
405 }
406 if (options)
407 printf("0x%08x", options);
408 printf(")");
409 }
410 printf("\n");
411 }
412
413 static void show_current_personality(void)
414 {
415 int pers = personality(0xffffffff);
416 if (pers == -1)
417 err(EXIT_FAILURE, _("Can not get current kernel personality"));
418
419 show_personality(pers);
420 }
421
422 int main(int argc, char *argv[])
423 {
424 const char *arch = NULL;
425 unsigned long options = 0;
426 int verbose = 0;
427 int archwrapper;
428 int c;
429 struct arch_domain *doms = NULL, *target = NULL;
430 unsigned long pers_value = 0;
431 char *shell = NULL, *shell_arg = NULL;
432
433 /* Options without equivalent short options */
434 enum {
435 OPT_4GB = CHAR_MAX + 1,
436 OPT_UNAME26,
437 OPT_LIST,
438 OPT_SHOW,
439 };
440
441 /* Options --3gb and --4gb are for compatibility with an old
442 * Debian setarch implementation. */
443 static const struct option longopts[] = {
444 {"help", no_argument, NULL, 'h'},
445 {"version", no_argument, NULL, 'V'},
446 {"verbose", no_argument, NULL, 'v'},
447 {"addr-no-randomize", no_argument, NULL, 'R'},
448 {"fdpic-funcptrs", no_argument, NULL, 'F'},
449 {"mmap-page-zero", no_argument, NULL, 'Z'},
450 {"addr-compat-layout", no_argument, NULL, 'L'},
451 {"read-implies-exec", no_argument, NULL, 'X'},
452 {"32bit", no_argument, NULL, 'B'},
453 {"short-inode", no_argument, NULL, 'I'},
454 {"whole-seconds", no_argument, NULL, 'S'},
455 {"sticky-timeouts", no_argument, NULL, 'T'},
456 {"3gb", no_argument, NULL, '3'},
457 {"4gb", no_argument, NULL, OPT_4GB},
458 {"uname-2.6", no_argument, NULL, OPT_UNAME26},
459 {"list", no_argument, NULL, OPT_LIST},
460 {"show", optional_argument, NULL, OPT_SHOW},
461 {NULL, 0, NULL, 0}
462 };
463
464 setlocale(LC_ALL, "");
465 bindtextdomain(PACKAGE, LOCALEDIR);
466 textdomain(PACKAGE);
467 close_stdout_atexit();
468
469 if (argc < 1) {
470 warnx(_("Not enough arguments"));
471 errtryhelp(EXIT_FAILURE);
472 }
473 archwrapper = strcmp(program_invocation_short_name, "setarch") != 0;
474 if (archwrapper) {
475 arch = program_invocation_short_name; /* symlinks to setarch */
476
477 /* Don't use ifdef sparc here, we get "Unrecognized architecture"
478 * error message later if necessary */
479 if (strcmp(arch, "sparc32bash") == 0) {
480 shell = "/bin/bash";
481 shell_arg = "";
482 goto set_arch;
483 }
484 } else {
485 if (1 < argc && *argv[1] != '-') {
486 arch = argv[1];
487 argv[1] = argv[0]; /* for getopt_long() to get the program name */
488 argv++;
489 argc--;
490 }
491 }
492
493 while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZ", longopts, NULL)) != -1) {
494 switch (c) {
495 case 'v':
496 verbose = 1;
497 break;
498 case 'R':
499 turn_on(ADDR_NO_RANDOMIZE, options);
500 break;
501 case 'F':
502 turn_on(FDPIC_FUNCPTRS, options);
503 break;
504 case 'Z':
505 turn_on(MMAP_PAGE_ZERO, options);
506 break;
507 case 'L':
508 turn_on(ADDR_COMPAT_LAYOUT, options);
509 break;
510 case 'X':
511 turn_on(READ_IMPLIES_EXEC, options);
512 break;
513 case 'B':
514 turn_on(ADDR_LIMIT_32BIT, options);
515 break;
516 case 'I':
517 turn_on(SHORT_INODE, options);
518 break;
519 case 'S':
520 turn_on(WHOLE_SECONDS, options);
521 break;
522 case 'T':
523 turn_on(STICKY_TIMEOUTS, options);
524 break;
525 case '3':
526 turn_on(ADDR_LIMIT_3GB, options);
527 break;
528 case OPT_4GB: /* just ignore this one */
529 break;
530 case OPT_UNAME26:
531 turn_on(UNAME26, options);
532 break;
533 case OPT_LIST:
534 if (!archwrapper) {
535 list_arch_domains(init_arch_domains());
536 return EXIT_SUCCESS;
537 } else
538 warnx(_("unrecognized option '--list'"));
539 goto error_getopts;
540 case OPT_SHOW:
541 if (!archwrapper) {
542 if (!optarg || strcmp(optarg, "current") == 0)
543 show_current_personality();
544 else
545 show_personality(str2num_or_err(
546 optarg, 16,
547 _("could not parse personality"),
548 0, INT_MAX));
549 return EXIT_SUCCESS;
550 } else
551 warnx(_("unrecognized option '--show'"));
552 goto error_getopts;
553
554 error_getopts:
555 default:
556 errtryhelp(EXIT_FAILURE);
557 case 'h':
558 usage(archwrapper);
559 case 'V':
560 print_version(EXIT_SUCCESS);
561 }
562 }
563
564 if (!arch && !options)
565 errx(EXIT_FAILURE, _("no architecture argument or personality flags specified"));
566
567 argc -= optind;
568 argv += optind;
569
570 set_arch:
571 /* get execution domain (architecture) */
572 if (arch) {
573 doms = init_arch_domains();
574 target = get_arch_domain(doms, arch);
575
576 if (!target)
577 errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), arch);
578 pers_value = target->perval;
579 }
580
581 /* add personality flags */
582 pers_value |= options;
583
584 /* call kernel */
585 if (personality(pers_value) < 0) {
586 /*
587 * Depending on architecture and kernel version, personality
588 * syscall is either capable or incapable of returning an error.
589 * If the return value is not an error, then it's the previous
590 * personality value, which can be an arbitrary value
591 * undistinguishable from an error value.
592 * To make things clear, a second call is needed.
593 */
594 if (personality(pers_value) < 0)
595 err(EXIT_FAILURE, _("failed to set personality to %s"), arch);
596 }
597
598 /* make sure architecture is set as expected */
599 if (arch)
600 verify_arch_domain(doms, target, arch);
601
602 if (!argc) {
603 shell = "/bin/sh";
604 shell_arg = "-sh";
605 }
606 if (verbose) {
607 printf(_("Execute command `%s'.\n"), shell ? shell : argv[0]);
608 /* flush all output streams before exec */
609 fflush(NULL);
610 }
611
612 /* Execute shell */
613 if (shell) {
614 execl(shell, shell_arg, (char *)NULL);
615 errexec(shell);
616 }
617
618 /* Execute on command line specified command */
619 execvp(argv[0], argv);
620 errexec(argv[0]);
621 }