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