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