]>
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 | |
70eebc40 BH |
44 | /* Options without equivalent short options */ |
45 | enum { | |
77eb13b9 | 46 | OPT_4GB = CHAR_MAX + 1, |
70eebc40 BH |
47 | OPT_UNAME26 |
48 | }; | |
8df90dec KZ |
49 | |
50 | #define turn_on(_flag, _opts) \ | |
51 | do { \ | |
52 | (_opts) |= _flag; \ | |
53 | if (verbose) \ | |
54 | printf(_("Switching on %s.\n"), #_flag); \ | |
55 | } while(0) | |
56 | ||
f29a3d18 | 57 | #ifndef UNAME26 |
70eebc40 BH |
58 | # define UNAME26 0x0020000 |
59 | #endif | |
f29a3d18 | 60 | #ifndef ADDR_NO_RANDOMIZE |
2d281745 KZ |
61 | # define ADDR_NO_RANDOMIZE 0x0040000 |
62 | #endif | |
f29a3d18 | 63 | #ifndef FDPIC_FUNCPTRS |
2d281745 KZ |
64 | # define FDPIC_FUNCPTRS 0x0080000 |
65 | #endif | |
f29a3d18 | 66 | #ifndef MMAP_PAGE_ZERO |
2d281745 KZ |
67 | # define MMAP_PAGE_ZERO 0x0100000 |
68 | #endif | |
f29a3d18 | 69 | #ifndef ADDR_COMPAT_LAYOUT |
2d281745 KZ |
70 | # define ADDR_COMPAT_LAYOUT 0x0200000 |
71 | #endif | |
f29a3d18 | 72 | #ifndef READ_IMPLIES_EXEC |
2d281745 KZ |
73 | # define READ_IMPLIES_EXEC 0x0400000 |
74 | #endif | |
f29a3d18 | 75 | #ifndef ADDR_LIMIT_32BIT |
2d281745 KZ |
76 | # define ADDR_LIMIT_32BIT 0x0800000 |
77 | #endif | |
f29a3d18 | 78 | #ifndef SHORT_INODE |
2d281745 KZ |
79 | # define SHORT_INODE 0x1000000 |
80 | #endif | |
f29a3d18 | 81 | #ifndef WHOLE_SECONDS |
2d281745 KZ |
82 | # define WHOLE_SECONDS 0x2000000 |
83 | #endif | |
f29a3d18 | 84 | #ifndef STICKY_TIMEOUTS |
2d281745 KZ |
85 | # define STICKY_TIMEOUTS 0x4000000 |
86 | #endif | |
f29a3d18 | 87 | #ifndef ADDR_LIMIT_3GB |
2d281745 KZ |
88 | # define ADDR_LIMIT_3GB 0x8000000 |
89 | #endif | |
90 | ||
77eb13b9 | 91 | static void __attribute__((__noreturn__)) show_help(void) |
8b3a46d2 | 92 | { |
77eb13b9 | 93 | fputs(USAGE_HEADER, stdout); |
68243049 BS |
94 | if (!strcmp(program_invocation_short_name, "setarch")) |
95 | printf(_(" %s <arch> [options] [<program> [<argument>...]]\n"), program_invocation_short_name); | |
96 | else | |
97 | printf(_(" %s [options] [<program> [<argument>...]]\n"), program_invocation_short_name); | |
451dbcfa BS |
98 | |
99 | fputs(USAGE_SEPARATOR, stdout); | |
100 | fputs(_("Change the reported architecture and set personality flags.\n"), stdout); | |
101 | ||
77eb13b9 SK |
102 | fputs(USAGE_OPTIONS, stdout); |
103 | fputs(_(" -v, --verbose says what options are being switched on\n"), stdout); | |
104 | fputs(_(" -R, --addr-no-randomize disables randomization of the virtual address space\n"), stdout); | |
105 | fputs(_(" -F, --fdpic-funcptrs makes function pointers point to descriptors\n"), stdout); | |
106 | fputs(_(" -Z, --mmap-page-zero turns on MMAP_PAGE_ZERO\n"), stdout); | |
107 | fputs(_(" -L, --addr-compat-layout changes the way virtual memory is allocated\n"), stdout); | |
108 | fputs(_(" -X, --read-implies-exec turns on READ_IMPLIES_EXEC\n"), stdout); | |
109 | fputs(_(" -B, --32bit turns on ADDR_LIMIT_32BIT\n"), stdout); | |
110 | fputs(_(" -I, --short-inode turns on SHORT_INODE\n"), stdout); | |
111 | fputs(_(" -S, --whole-seconds turns on WHOLE_SECONDS\n"), stdout); | |
112 | fputs(_(" -T, --sticky-timeouts turns on STICKY_TIMEOUTS\n"), stdout); | |
113 | fputs(_(" -3, --3gb limits the used address space to a maximum of 3 GB\n"), stdout); | |
114 | fputs(_(" --4gb ignored (for backward compatibility only)\n"), stdout); | |
115 | fputs(_(" --uname-2.6 turns on UNAME26\n"), stdout); | |
116 | fputs(_(" --list list settable architectures, and exit\n"), stdout); | |
117 | fputs(USAGE_SEPARATOR, stdout); | |
118 | fputs(USAGE_HELP, stdout); | |
119 | fputs(USAGE_VERSION, stdout); | |
120 | printf(USAGE_MAN_TAIL("setarch(8)")); | |
121 | exit(EXIT_SUCCESS); | |
8b3a46d2 KZ |
122 | } |
123 | ||
77eb13b9 | 124 | static void __attribute__((__noreturn__)) show_usage(const char *s) |
8b3a46d2 | 125 | { |
77eb13b9 SK |
126 | if (s) |
127 | errx(EXIT_FAILURE, | |
128 | _("%s\nTry `%s --help' for more information."), s, | |
129 | program_invocation_short_name); | |
130 | else | |
131 | errx(EXIT_FAILURE, _("Try `%s --help' for more information."), | |
132 | program_invocation_short_name); | |
8b3a46d2 KZ |
133 | } |
134 | ||
b446402b | 135 | static void __attribute__((__noreturn__)) |
77eb13b9 | 136 | show_version(void) |
b446402b | 137 | { |
77eb13b9 SK |
138 | printf(UTIL_LINUX_VERSION); |
139 | exit(EXIT_SUCCESS); | |
b446402b | 140 | } |
8df90dec | 141 | |
77eb13b9 | 142 | static int set_arch(const char *pers, unsigned long options, int list) |
8b3a46d2 | 143 | { |
77eb13b9 SK |
144 | struct utsname un; |
145 | int i; | |
146 | unsigned long pers_value; | |
147 | ||
148 | struct { | |
149 | int perval; | |
150 | const char *target_arch, *result_arch; | |
151 | } transitions[] = { | |
152 | {UNAME26, "uname26", NULL}, | |
153 | {PER_LINUX32, "linux32", NULL}, | |
154 | {PER_LINUX, "linux64", NULL}, | |
8b3a46d2 | 155 | #if defined(__powerpc__) || defined(__powerpc64__) |
77eb13b9 SK |
156 | # ifdef __BIG_ENDIAN__ |
157 | {PER_LINUX32, "ppc32", "ppc"}, | |
158 | {PER_LINUX32, "ppc", "ppc"}, | |
159 | {PER_LINUX, "ppc64", "ppc64"}, | |
160 | {PER_LINUX, "ppc64pseries", "ppc64"}, | |
161 | {PER_LINUX, "ppc64iseries", "ppc64"}, | |
162 | # else | |
163 | PER_LINUX32, "ppc32le", "ppcle"}, | |
164 | PER_LINUX32, "ppcle", "ppcle"}, | |
165 | PER_LINUX, "ppc64le", "ppc64le"}, | |
166 | # endif | |
8b3a46d2 KZ |
167 | #endif |
168 | #if defined(__x86_64__) || defined(__i386__) || defined(__ia64__) | |
77eb13b9 SK |
169 | {PER_LINUX32, "i386", "i386"}, |
170 | {PER_LINUX32, "i486", "i386"}, | |
171 | {PER_LINUX32, "i586", "i386"}, | |
172 | {PER_LINUX32, "i686", "i386"}, | |
173 | {PER_LINUX32, "athlon", "i386"}, | |
8b3a46d2 KZ |
174 | #endif |
175 | #if defined(__x86_64__) || defined(__i386__) | |
77eb13b9 | 176 | {PER_LINUX, "x86_64", "x86_64"}, |
8b3a46d2 KZ |
177 | #endif |
178 | #if defined(__ia64__) || defined(__i386__) | |
77eb13b9 | 179 | {PER_LINUX, "ia64", "ia64"}, |
8b3a46d2 | 180 | #endif |
782e198d | 181 | #if defined(__hppa__) |
77eb13b9 SK |
182 | {PER_LINUX32, "parisc32", "parisc"}, |
183 | {PER_LINUX32, "parisc", "parisc"}, | |
184 | {PER_LINUX, "parisc64", "parisc64"}, | |
782e198d | 185 | #endif |
8b3a46d2 | 186 | #if defined(__s390x__) || defined(__s390__) |
77eb13b9 SK |
187 | {PER_LINUX32, "s390", "s390"}, |
188 | {PER_LINUX, "s390x", "s390x"}, | |
8b3a46d2 KZ |
189 | #endif |
190 | #if defined(__sparc64__) || defined(__sparc__) | |
77eb13b9 SK |
191 | {PER_LINUX32, "sparc", "sparc"}, |
192 | {PER_LINUX32, "sparc32bash", "sparc"}, | |
193 | {PER_LINUX32, "sparc32", "sparc"}, | |
194 | {PER_LINUX, "sparc64", "sparc64"}, | |
8b3a46d2 KZ |
195 | #endif |
196 | #if defined(__mips64__) || defined(__mips__) | |
77eb13b9 SK |
197 | {PER_LINUX32, "mips32", "mips"}, |
198 | {PER_LINUX32, "mips", "mips"}, | |
199 | {PER_LINUX, "mips64", "mips64"}, | |
9e19737a BC |
200 | #endif |
201 | #if defined(__alpha__) | |
77eb13b9 SK |
202 | {PER_LINUX, "alpha", "alpha"}, |
203 | {PER_LINUX, "alphaev5", "alpha"}, | |
204 | {PER_LINUX, "alphaev56", "alpha"}, | |
205 | {PER_LINUX, "alphaev6", "alpha"}, | |
206 | {PER_LINUX, "alphaev67", "alpha"}, | |
8b3a46d2 | 207 | #endif |
77eb13b9 SK |
208 | /* place holder, will be filled up at runtime */ |
209 | {-1, NULL, NULL}, | |
210 | {-1, NULL, NULL} | |
211 | }; | |
212 | ||
213 | /* Add the trivial transition {PER_LINUX, machine, machine} if no | |
214 | * such target_arch is hardcoded yet. */ | |
215 | uname(&un); | |
216 | for (i = 0; transitions[i].perval >= 0; i++) | |
217 | if (!strcmp(un.machine, transitions[i].target_arch)) | |
218 | break; | |
219 | if (transitions[i].perval < 0) { | |
220 | unsigned long wrdsz = CHAR_BIT * sizeof(void *); | |
221 | if (wrdsz == 32 || wrdsz == 64) { | |
222 | /* fill up the place holder */ | |
223 | transitions[i].perval = wrdsz == 32 ? PER_LINUX32 : PER_LINUX; | |
224 | transitions[i].target_arch = un.machine; | |
225 | transitions[i].result_arch = un.machine; | |
226 | } | |
ae2c3b5b | 227 | } |
77eb13b9 SK |
228 | if (list) { |
229 | for (i = 0; transitions[i].target_arch != NULL; i++) | |
230 | printf("%s\n", transitions[i].target_arch); | |
231 | return 0; | |
232 | } | |
233 | for (i = 0; transitions[i].perval >= 0; i++) | |
234 | if (!strcmp(pers, transitions[i].target_arch)) | |
235 | break; | |
236 | if (transitions[i].perval < 0) | |
237 | errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), pers); | |
238 | pers_value = transitions[i].perval | options; | |
9ed11cc2 | 239 | if (personality(pers_value) == -EINVAL) |
77eb13b9 SK |
240 | return 1; |
241 | uname(&un); | |
242 | if (transitions[i].result_arch && strcmp(un.machine, transitions[i].result_arch)) { | |
243 | if (strcmp(transitions[i].result_arch, "i386") | |
244 | || (strcmp(un.machine, "i486") | |
245 | && strcmp(un.machine, "i586") | |
246 | && strcmp(un.machine, "i686") | |
247 | && strcmp(un.machine, "athlon"))) | |
248 | errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), pers); | |
249 | } | |
250 | return 0; | |
8b3a46d2 KZ |
251 | } |
252 | ||
253 | int main(int argc, char *argv[]) | |
254 | { | |
77eb13b9 SK |
255 | const char *p; |
256 | unsigned long options = 0; | |
257 | int verbose = 0; | |
258 | int c; | |
259 | ||
260 | /* Options --3gb and --4gb are for compatibitity with an old | |
261 | * Debian setarch implementation. */ | |
262 | static const struct option longopts[] = { | |
263 | {"help", no_argument, NULL, 'h'}, | |
264 | {"version", no_argument, NULL, 'V'}, | |
265 | {"verbose", no_argument, NULL, 'v'}, | |
266 | {"addr-no-randomize", no_argument, NULL, 'R'}, | |
267 | {"fdpic-funcptrs", no_argument, NULL, 'F'}, | |
268 | {"mmap-page-zero", no_argument, NULL, 'Z'}, | |
269 | {"addr-compat-layout", no_argument, NULL, 'L'}, | |
270 | {"read-implies-exec", no_argument, NULL, 'X'}, | |
271 | {"32bit", no_argument, NULL, 'B'}, | |
272 | {"short-inode", no_argument, NULL, 'I'}, | |
273 | {"whole-seconds", no_argument, NULL, 'S'}, | |
274 | {"sticky-timeouts", no_argument, NULL, 'T'}, | |
275 | {"3gb", no_argument, NULL, '3'}, | |
276 | {"4gb", no_argument, NULL, OPT_4GB}, | |
277 | {"uname-2.6", no_argument, NULL, OPT_UNAME26}, | |
278 | {NULL, 0, NULL, 0} | |
279 | }; | |
280 | ||
281 | setlocale(LC_ALL, ""); | |
282 | bindtextdomain(PACKAGE, LOCALEDIR); | |
283 | textdomain(PACKAGE); | |
284 | atexit(close_stdout); | |
285 | ||
286 | if (argc < 1) | |
287 | show_usage(_("Not enough arguments")); | |
288 | ||
289 | p = program_invocation_short_name; | |
290 | if (!strcmp(p, "setarch")) { | |
291 | argc--; | |
292 | if (argc < 1) | |
293 | show_usage(_("Not enough arguments")); | |
294 | p = argv[1]; | |
295 | argv[1] = argv[0]; /* for getopt_long() to get the program name */ | |
296 | argv++; | |
297 | if (!strcmp(p, "-h") || !strcmp(p, "--help")) | |
298 | show_help(); | |
299 | else if (!strcmp(p, "-V") || !strcmp(p, "--version")) | |
300 | show_version(); | |
301 | else if (!strcmp(p, "--list")) { | |
302 | set_arch(argv[0], 0L, 1); | |
303 | return EXIT_SUCCESS; | |
304 | } | |
305 | } | |
306 | #if defined(__sparc64__) || defined(__sparc__) | |
307 | if (!strcmp(p, "sparc32bash")) { | |
308 | if (set_arch(p, 0L, 0)) | |
309 | err(EXIT_FAILURE, _("Failed to set personality to %s"), p); | |
310 | execl("/bin/bash", NULL); | |
311 | err(EXIT_FAILURE, _("failed to execute %s"), "/bin/bash"); | |
312 | } | |
313 | #endif | |
8b3a46d2 | 314 | |
77eb13b9 SK |
315 | while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZ", longopts, NULL)) != -1) { |
316 | switch (c) { | |
317 | case 'h': | |
318 | show_help(); | |
319 | break; | |
320 | case 'V': | |
321 | show_version(); | |
322 | break; | |
323 | case 'v': | |
324 | verbose = 1; | |
325 | break; | |
326 | case 'R': | |
327 | turn_on(ADDR_NO_RANDOMIZE, options); | |
328 | break; | |
329 | case 'F': | |
330 | turn_on(FDPIC_FUNCPTRS, options); | |
331 | break; | |
332 | case 'Z': | |
333 | turn_on(MMAP_PAGE_ZERO, options); | |
334 | break; | |
335 | case 'L': | |
336 | turn_on(ADDR_COMPAT_LAYOUT, options); | |
337 | break; | |
338 | case 'X': | |
339 | turn_on(READ_IMPLIES_EXEC, options); | |
340 | break; | |
341 | case 'B': | |
342 | turn_on(ADDR_LIMIT_32BIT, options); | |
343 | break; | |
344 | case 'I': | |
345 | turn_on(SHORT_INODE, options); | |
346 | break; | |
347 | case 'S': | |
348 | turn_on(WHOLE_SECONDS, options); | |
349 | break; | |
350 | case 'T': | |
351 | turn_on(STICKY_TIMEOUTS, options); | |
352 | break; | |
353 | case '3': | |
354 | turn_on(ADDR_LIMIT_3GB, options); | |
355 | break; | |
356 | case OPT_4GB: /* just ignore this one */ | |
357 | break; | |
358 | case OPT_UNAME26: | |
359 | turn_on(UNAME26, options); | |
360 | break; | |
361 | default: | |
362 | show_usage(NULL); | |
363 | } | |
364 | } | |
8b3a46d2 | 365 | |
77eb13b9 SK |
366 | argc -= optind; |
367 | argv += optind; | |
8df90dec | 368 | |
77eb13b9 SK |
369 | if (set_arch(p, options, 0)) |
370 | err(EXIT_FAILURE, _("Failed to set personality to %s"), p); | |
8b3a46d2 | 371 | |
77eb13b9 SK |
372 | /* flush all output streams before exec */ |
373 | fflush(NULL); | |
34c265f0 | 374 | |
77eb13b9 SK |
375 | if (!argc) { |
376 | execl("/bin/sh", "-sh", NULL); | |
377 | err(EXIT_FAILURE, _("failed to execute %s"), "/bin/sh"); | |
378 | } | |
8b3a46d2 | 379 | |
77eb13b9 SK |
380 | execvp(argv[0], argv); |
381 | err(EXIT_FAILURE, "%s", argv[0]); | |
382 | return EXIT_FAILURE; | |
8b3a46d2 | 383 | } |