]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/setarch.c
setarch: use kernel address size if possible
[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"
8b3a46d2 39
9ed11cc2
SK
40#ifndef HAVE_PERSONALITY
41# include <syscall.h>
42# define personality(pers) ((long)syscall(SYS_personality, pers))
43#endif
8b3a46d2 44
8df90dec
KZ
45#define turn_on(_flag, _opts) \
46 do { \
47 (_opts) |= _flag; \
48 if (verbose) \
49 printf(_("Switching on %s.\n"), #_flag); \
50 } while(0)
51
f29a3d18 52#ifndef UNAME26
70eebc40
BH
53# define UNAME26 0x0020000
54#endif
f29a3d18 55#ifndef ADDR_NO_RANDOMIZE
2d281745
KZ
56# define ADDR_NO_RANDOMIZE 0x0040000
57#endif
f29a3d18 58#ifndef FDPIC_FUNCPTRS
2d281745
KZ
59# define FDPIC_FUNCPTRS 0x0080000
60#endif
f29a3d18 61#ifndef MMAP_PAGE_ZERO
2d281745
KZ
62# define MMAP_PAGE_ZERO 0x0100000
63#endif
f29a3d18 64#ifndef ADDR_COMPAT_LAYOUT
2d281745
KZ
65# define ADDR_COMPAT_LAYOUT 0x0200000
66#endif
f29a3d18 67#ifndef READ_IMPLIES_EXEC
2d281745
KZ
68# define READ_IMPLIES_EXEC 0x0400000
69#endif
f29a3d18 70#ifndef ADDR_LIMIT_32BIT
2d281745
KZ
71# define ADDR_LIMIT_32BIT 0x0800000
72#endif
f29a3d18 73#ifndef SHORT_INODE
2d281745
KZ
74# define SHORT_INODE 0x1000000
75#endif
f29a3d18 76#ifndef WHOLE_SECONDS
2d281745
KZ
77# define WHOLE_SECONDS 0x2000000
78#endif
f29a3d18 79#ifndef STICKY_TIMEOUTS
2d281745
KZ
80# define STICKY_TIMEOUTS 0x4000000
81#endif
f29a3d18 82#ifndef ADDR_LIMIT_3GB
2d281745
KZ
83# define ADDR_LIMIT_3GB 0x8000000
84#endif
85
5edb0ea6 86
1db8bf93
KZ
87struct arch_domain {
88 int perval; /* PER_* */
89 const char *target_arch;
90 const char *result_arch;
91};
92
93
cbfa1442 94static void __attribute__((__noreturn__)) usage(int archwrapper)
8b3a46d2 95{
77eb13b9 96 fputs(USAGE_HEADER, stdout);
5edb0ea6 97 if (!archwrapper)
05541825 98 printf(_(" %s [<arch>] [options] [<program> [<argument>...]]\n"), program_invocation_short_name);
68243049
BS
99 else
100 printf(_(" %s [options] [<program> [<argument>...]]\n"), program_invocation_short_name);
451dbcfa
BS
101
102 fputs(USAGE_SEPARATOR, stdout);
103 fputs(_("Change the reported architecture and set personality flags.\n"), stdout);
104
77eb13b9 105 fputs(USAGE_OPTIONS, stdout);
77eb13b9 106 fputs(_(" -B, --32bit turns on ADDR_LIMIT_32BIT\n"), stdout);
26b0b857 107 fputs(_(" -F, --fdpic-funcptrs makes function pointers point to descriptors\n"), stdout);
77eb13b9 108 fputs(_(" -I, --short-inode turns on SHORT_INODE\n"), stdout);
26b0b857
BS
109 fputs(_(" -L, --addr-compat-layout changes the way virtual memory is allocated\n"), stdout);
110 fputs(_(" -R, --addr-no-randomize disables randomization of the virtual address space\n"), stdout);
77eb13b9
SK
111 fputs(_(" -S, --whole-seconds turns on WHOLE_SECONDS\n"), stdout);
112 fputs(_(" -T, --sticky-timeouts turns on STICKY_TIMEOUTS\n"), stdout);
26b0b857
BS
113 fputs(_(" -X, --read-implies-exec turns on READ_IMPLIES_EXEC\n"), stdout);
114 fputs(_(" -Z, --mmap-page-zero turns on MMAP_PAGE_ZERO\n"), stdout);
77eb13b9
SK
115 fputs(_(" -3, --3gb limits the used address space to a maximum of 3 GB\n"), stdout);
116 fputs(_(" --4gb ignored (for backward compatibility only)\n"), stdout);
117 fputs(_(" --uname-2.6 turns on UNAME26\n"), stdout);
26b0b857 118 fputs(_(" -v, --verbose say what options are being switched on\n"), stdout);
5edb0ea6
KZ
119
120 if (!archwrapper)
121 fputs(_(" --list list settable architectures, and exit\n"), stdout);
26b0b857 122
77eb13b9 123 fputs(USAGE_SEPARATOR, stdout);
f45f3ec3 124 printf(USAGE_HELP_OPTIONS(26));
77eb13b9 125 printf(USAGE_MAN_TAIL("setarch(8)"));
26b0b857 126
77eb13b9 127 exit(EXIT_SUCCESS);
8b3a46d2
KZ
128}
129
1db8bf93
KZ
130/*
131 * Returns inilialized list of all available execution domains.
132 */
133static struct arch_domain *init_arch_domains(void)
8b3a46d2 134{
33e87bf1 135 static struct utsname un;
1db8bf93 136 size_t i;
77eb13b9 137
1db8bf93
KZ
138 static struct arch_domain transitions[] =
139 {
77eb13b9
SK
140 {UNAME26, "uname26", NULL},
141 {PER_LINUX32, "linux32", NULL},
142 {PER_LINUX, "linux64", NULL},
8b3a46d2 143#if defined(__powerpc__) || defined(__powerpc64__)
77eb13b9
SK
144# ifdef __BIG_ENDIAN__
145 {PER_LINUX32, "ppc32", "ppc"},
146 {PER_LINUX32, "ppc", "ppc"},
147 {PER_LINUX, "ppc64", "ppc64"},
148 {PER_LINUX, "ppc64pseries", "ppc64"},
149 {PER_LINUX, "ppc64iseries", "ppc64"},
150# else
e60b6df5
KZ
151 {PER_LINUX32, "ppc32", "ppcle"},
152 {PER_LINUX32, "ppc", "ppcle"},
d9ca5dd4
RM
153 {PER_LINUX32, "ppc32le", "ppcle"},
154 {PER_LINUX32, "ppcle", "ppcle"},
155 {PER_LINUX, "ppc64le", "ppc64le"},
77eb13b9 156# endif
8b3a46d2
KZ
157#endif
158#if defined(__x86_64__) || defined(__i386__) || defined(__ia64__)
77eb13b9
SK
159 {PER_LINUX32, "i386", "i386"},
160 {PER_LINUX32, "i486", "i386"},
161 {PER_LINUX32, "i586", "i386"},
162 {PER_LINUX32, "i686", "i386"},
163 {PER_LINUX32, "athlon", "i386"},
8b3a46d2
KZ
164#endif
165#if defined(__x86_64__) || defined(__i386__)
77eb13b9 166 {PER_LINUX, "x86_64", "x86_64"},
8b3a46d2
KZ
167#endif
168#if defined(__ia64__) || defined(__i386__)
77eb13b9 169 {PER_LINUX, "ia64", "ia64"},
8b3a46d2 170#endif
782e198d 171#if defined(__hppa__)
77eb13b9
SK
172 {PER_LINUX32, "parisc32", "parisc"},
173 {PER_LINUX32, "parisc", "parisc"},
174 {PER_LINUX, "parisc64", "parisc64"},
782e198d 175#endif
8b3a46d2 176#if defined(__s390x__) || defined(__s390__)
77eb13b9
SK
177 {PER_LINUX32, "s390", "s390"},
178 {PER_LINUX, "s390x", "s390x"},
8b3a46d2
KZ
179#endif
180#if defined(__sparc64__) || defined(__sparc__)
77eb13b9
SK
181 {PER_LINUX32, "sparc", "sparc"},
182 {PER_LINUX32, "sparc32bash", "sparc"},
183 {PER_LINUX32, "sparc32", "sparc"},
184 {PER_LINUX, "sparc64", "sparc64"},
8b3a46d2
KZ
185#endif
186#if defined(__mips64__) || defined(__mips__)
77eb13b9
SK
187 {PER_LINUX32, "mips32", "mips"},
188 {PER_LINUX32, "mips", "mips"},
189 {PER_LINUX, "mips64", "mips64"},
9e19737a
BC
190#endif
191#if defined(__alpha__)
77eb13b9
SK
192 {PER_LINUX, "alpha", "alpha"},
193 {PER_LINUX, "alphaev5", "alpha"},
194 {PER_LINUX, "alphaev56", "alpha"},
195 {PER_LINUX, "alphaev6", "alpha"},
196 {PER_LINUX, "alphaev67", "alpha"},
146900d4
AS
197#endif
198#if defined(__e2k__)
199 {PER_LINUX, "e2k", "e2k"},
200 {PER_LINUX, "e2kv4", "e2k"},
201 {PER_LINUX, "e2kv5", "e2k"},
202 {PER_LINUX, "e2kv6", "e2k"},
203 {PER_LINUX, "e2k4c", "e2k"},
204 {PER_LINUX, "e2k8c", "e2k"},
205 {PER_LINUX, "e2k1cp", "e2k"},
206 {PER_LINUX, "e2k8c2", "e2k"},
207 {PER_LINUX, "e2k12c", "e2k"},
208 {PER_LINUX, "e2k16c", "e2k"},
209 {PER_LINUX, "e2k2c3", "e2k"},
9fd01b10
AG
210#endif
211#if defined(__arm__) || defined(__aarch64__)
212# ifdef __BIG_ENDIAN__
213 {PER_LINUX32, "armv7b", "arm"},
214 {PER_LINUX32, "armv8b", "arm"},
215# else
216 {PER_LINUX32, "armv7l", "arm"},
217 {PER_LINUX32, "armv8l", "arm"},
218# endif
219 {PER_LINUX32, "armh", "arm"},
220 {PER_LINUX32, "arm", "arm"},
221 {PER_LINUX, "arm64", "aarch64"},
222 {PER_LINUX, "aarch64", "aarch64"},
8b3a46d2 223#endif
77eb13b9
SK
224 /* place holder, will be filled up at runtime */
225 {-1, NULL, NULL},
226 {-1, NULL, NULL}
227 };
228
229 /* Add the trivial transition {PER_LINUX, machine, machine} if no
230 * such target_arch is hardcoded yet. */
231 uname(&un);
232 for (i = 0; transitions[i].perval >= 0; i++)
233 if (!strcmp(un.machine, transitions[i].target_arch))
234 break;
235 if (transitions[i].perval < 0) {
035fc2d9
TW
236 int wrdsz = sysfs_get_address_bits(NULL);
237 if (wrdsz < 0)
238 wrdsz = CHAR_BIT * sizeof(void *);
77eb13b9
SK
239 if (wrdsz == 32 || wrdsz == 64) {
240 /* fill up the place holder */
241 transitions[i].perval = wrdsz == 32 ? PER_LINUX32 : PER_LINUX;
242 transitions[i].target_arch = un.machine;
243 transitions[i].result_arch = un.machine;
244 }
ae2c3b5b 245 }
1db8bf93
KZ
246
247 return transitions;
248}
249
250/*
251 * List all execution domains from transitions
252 */
253static void list_arch_domains(struct arch_domain *doms)
254{
255 struct arch_domain *d;
256
257 for (d = doms; d->target_arch != NULL; d++)
258 printf("%s\n", d->target_arch);
259}
260
261static struct arch_domain *get_arch_domain(struct arch_domain *doms, const char *pers)
262{
263 struct arch_domain *d;
264
cf5debba 265 for (d = doms; d && d->perval >= 0; d++) {
1db8bf93 266 if (!strcmp(pers, d->target_arch))
77eb13b9 267 break;
ae706576 268 }
1db8bf93
KZ
269
270 return !d || d->perval < 0 ? NULL : d;
271}
272
7f48cfb0 273static void verify_arch_domain(struct arch_domain *doms, struct arch_domain *target, const char *wanted)
1db8bf93
KZ
274{
275 struct utsname un;
276
7f48cfb0 277 if (!doms || !target || !target->result_arch)
1db8bf93
KZ
278 return;
279
77eb13b9 280 uname(&un);
7f48cfb0
AG
281
282 if (!strcmp(un.machine, target->result_arch))
283 return;
284
9fd01b10
AG
285 if (!strcmp(target->result_arch, "i386") ||
286 !strcmp(target->result_arch, "arm")) {
7f48cfb0
AG
287 struct arch_domain *dom;
288 for (dom = doms; dom->target_arch != NULL; dom++) {
289 if (!dom->result_arch || strcmp(dom->result_arch, target->result_arch))
290 continue;
291 if (!strcmp(dom->target_arch, un.machine))
292 return;
293 }
77eb13b9 294 }
7f48cfb0
AG
295
296 errx(EXIT_FAILURE, _("Kernel cannot set architecture to %s"), wanted);
8b3a46d2
KZ
297}
298
299int main(int argc, char *argv[])
300{
5edb0ea6 301 const char *arch = NULL;
77eb13b9
SK
302 unsigned long options = 0;
303 int verbose = 0;
cbfa1442 304 int archwrapper;
77eb13b9 305 int c;
7f48cfb0 306 struct arch_domain *doms = NULL, *target = NULL;
1db8bf93 307 unsigned long pers_value = 0;
3fa06e04 308 char *shell = NULL, *shell_arg = NULL;
77eb13b9 309
5edb0ea6
KZ
310 /* Options without equivalent short options */
311 enum {
312 OPT_4GB = CHAR_MAX + 1,
313 OPT_UNAME26,
314 OPT_LIST
315 };
316
9e930041 317 /* Options --3gb and --4gb are for compatibility with an old
77eb13b9
SK
318 * Debian setarch implementation. */
319 static const struct option longopts[] = {
320 {"help", no_argument, NULL, 'h'},
321 {"version", no_argument, NULL, 'V'},
322 {"verbose", no_argument, NULL, 'v'},
323 {"addr-no-randomize", no_argument, NULL, 'R'},
324 {"fdpic-funcptrs", no_argument, NULL, 'F'},
325 {"mmap-page-zero", no_argument, NULL, 'Z'},
326 {"addr-compat-layout", no_argument, NULL, 'L'},
327 {"read-implies-exec", no_argument, NULL, 'X'},
328 {"32bit", no_argument, NULL, 'B'},
329 {"short-inode", no_argument, NULL, 'I'},
330 {"whole-seconds", no_argument, NULL, 'S'},
331 {"sticky-timeouts", no_argument, NULL, 'T'},
332 {"3gb", no_argument, NULL, '3'},
333 {"4gb", no_argument, NULL, OPT_4GB},
334 {"uname-2.6", no_argument, NULL, OPT_UNAME26},
5edb0ea6 335 {"list", no_argument, NULL, OPT_LIST},
77eb13b9
SK
336 {NULL, 0, NULL, 0}
337 };
338
339 setlocale(LC_ALL, "");
340 bindtextdomain(PACKAGE, LOCALEDIR);
341 textdomain(PACKAGE);
2c308875 342 close_stdout_atexit();
77eb13b9 343
7a87a774
RM
344 if (argc < 1) {
345 warnx(_("Not enough arguments"));
346 errtryhelp(EXIT_FAILURE);
347 }
5edb0ea6 348 archwrapper = strcmp(program_invocation_short_name, "setarch") != 0;
cbfa1442 349 if (archwrapper) {
5edb0ea6 350 arch = program_invocation_short_name; /* symlinks to setarch */
3fa06e04
KZ
351
352 /* Don't use ifdef sparc here, we get "Unrecognized architecture"
353 * error message later if necessary */
cbfa1442 354 if (strcmp(arch, "sparc32bash") == 0) {
3fa06e04
KZ
355 shell = "/bin/bash";
356 shell_arg = "";
357 goto set_arch;
cbfa1442 358 }
cbfa1442 359 } else {
00b5ef70 360 if (1 < argc && *argv[1] != '-') {
5edb0ea6
KZ
361 arch = argv[1];
362 argv[1] = argv[0]; /* for getopt_long() to get the program name */
363 argv++;
364 argc--;
77eb13b9
SK
365 }
366 }
5edb0ea6 367
77eb13b9
SK
368 while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZ", longopts, NULL)) != -1) {
369 switch (c) {
77eb13b9
SK
370 case 'v':
371 verbose = 1;
372 break;
373 case 'R':
374 turn_on(ADDR_NO_RANDOMIZE, options);
375 break;
376 case 'F':
377 turn_on(FDPIC_FUNCPTRS, options);
378 break;
379 case 'Z':
380 turn_on(MMAP_PAGE_ZERO, options);
381 break;
382 case 'L':
383 turn_on(ADDR_COMPAT_LAYOUT, options);
384 break;
385 case 'X':
386 turn_on(READ_IMPLIES_EXEC, options);
387 break;
388 case 'B':
389 turn_on(ADDR_LIMIT_32BIT, options);
390 break;
391 case 'I':
392 turn_on(SHORT_INODE, options);
393 break;
394 case 'S':
395 turn_on(WHOLE_SECONDS, options);
396 break;
397 case 'T':
398 turn_on(STICKY_TIMEOUTS, options);
399 break;
400 case '3':
401 turn_on(ADDR_LIMIT_3GB, options);
402 break;
403 case OPT_4GB: /* just ignore this one */
404 break;
405 case OPT_UNAME26:
406 turn_on(UNAME26, options);
407 break;
5edb0ea6
KZ
408 case OPT_LIST:
409 if (!archwrapper) {
1db8bf93 410 list_arch_domains(init_arch_domains());
b2b5b502 411 return EXIT_SUCCESS;
5edb0ea6
KZ
412 } else
413 warnx(_("unrecognized option '--list'"));
9a8fada1 414 /* fallthrough */
2c308875 415
77eb13b9 416 default:
677ec86c 417 errtryhelp(EXIT_FAILURE);
2c308875
KZ
418 case 'h':
419 usage(archwrapper);
420 case 'V':
421 print_version(EXIT_SUCCESS);
77eb13b9
SK
422 }
423 }
8b3a46d2 424
05541825
KZ
425 if (!arch && !options)
426 errx(EXIT_FAILURE, _("no architecture argument or personality flags specified"));
5edb0ea6 427
77eb13b9
SK
428 argc -= optind;
429 argv += optind;
8df90dec 430
3fa06e04 431set_arch:
05541825
KZ
432 /* get execution domain (architecture) */
433 if (arch) {
434 doms = init_arch_domains();
435 target = get_arch_domain(doms, arch);
1db8bf93 436
05541825
KZ
437 if (!target)
438 errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), arch);
439 pers_value = target->perval;
440 }
441
442 /* add personality flags */
443 pers_value |= options;
1db8bf93 444
05541825 445 /* call kernel */
1db8bf93
KZ
446 if (personality(pers_value) < 0) {
447 /*
448 * Depending on architecture and kernel version, personality
449 * syscall is either capable or incapable of returning an error.
450 * If the return value is not an error, then it's the previous
451 * personality value, which can be an arbitrary value
452 * undistinguishable from an error value.
453 * To make things clear, a second call is needed.
454 */
455 if (personality(pers_value) < 0)
456 err(EXIT_FAILURE, _("failed to set personality to %s"), arch);
457 }
458
05541825
KZ
459 /* make sure architecture is set as expected */
460 if (arch)
7f48cfb0 461 verify_arch_domain(doms, target, arch);
8b3a46d2 462
3fa06e04
KZ
463 if (!argc) {
464 shell = "/bin/sh";
465 shell_arg = "-sh";
466 }
9508e297 467 if (verbose) {
3fa06e04 468 printf(_("Execute command `%s'.\n"), shell ? shell : argv[0]);
9508e297
RM
469 /* flush all output streams before exec */
470 fflush(NULL);
471 }
34c265f0 472
3fa06e04
KZ
473 /* Execute shell */
474 if (shell) {
1b10fa0e 475 execl(shell, shell_arg, (char *)NULL);
3fa06e04 476 errexec(shell);
77eb13b9 477 }
8b3a46d2 478
3fa06e04 479 /* Execute on command line specified command */
77eb13b9 480 execvp(argv[0], argv);
fd777151 481 errexec(argv[0]);
8b3a46d2 482}