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