]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/setarch.c
misc: consolidate macro style USAGE_HELP_OPTIONS
[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"
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
KZ
85static int archwrapper;
86
7a87a774 87static void __attribute__((__noreturn__)) usage(void)
8b3a46d2 88{
77eb13b9 89 fputs(USAGE_HEADER, stdout);
5edb0ea6 90 if (!archwrapper)
68243049
BS
91 printf(_(" %s <arch> [options] [<program> [<argument>...]]\n"), program_invocation_short_name);
92 else
93 printf(_(" %s [options] [<program> [<argument>...]]\n"), program_invocation_short_name);
451dbcfa
BS
94
95 fputs(USAGE_SEPARATOR, stdout);
96 fputs(_("Change the reported architecture and set personality flags.\n"), stdout);
97
77eb13b9 98 fputs(USAGE_OPTIONS, stdout);
77eb13b9 99 fputs(_(" -B, --32bit turns on ADDR_LIMIT_32BIT\n"), stdout);
26b0b857 100 fputs(_(" -F, --fdpic-funcptrs makes function pointers point to descriptors\n"), stdout);
77eb13b9 101 fputs(_(" -I, --short-inode turns on SHORT_INODE\n"), stdout);
26b0b857
BS
102 fputs(_(" -L, --addr-compat-layout changes the way virtual memory is allocated\n"), stdout);
103 fputs(_(" -R, --addr-no-randomize disables randomization of the virtual address space\n"), stdout);
77eb13b9
SK
104 fputs(_(" -S, --whole-seconds turns on WHOLE_SECONDS\n"), stdout);
105 fputs(_(" -T, --sticky-timeouts turns on STICKY_TIMEOUTS\n"), stdout);
26b0b857
BS
106 fputs(_(" -X, --read-implies-exec turns on READ_IMPLIES_EXEC\n"), stdout);
107 fputs(_(" -Z, --mmap-page-zero turns on MMAP_PAGE_ZERO\n"), stdout);
77eb13b9
SK
108 fputs(_(" -3, --3gb limits the used address space to a maximum of 3 GB\n"), stdout);
109 fputs(_(" --4gb ignored (for backward compatibility only)\n"), stdout);
110 fputs(_(" --uname-2.6 turns on UNAME26\n"), stdout);
26b0b857 111 fputs(_(" -v, --verbose say what options are being switched on\n"), stdout);
5edb0ea6
KZ
112
113 if (!archwrapper)
114 fputs(_(" --list list settable architectures, and exit\n"), stdout);
26b0b857 115
77eb13b9 116 fputs(USAGE_SEPARATOR, stdout);
f45f3ec3 117 printf(USAGE_HELP_OPTIONS(26));
77eb13b9 118 printf(USAGE_MAN_TAIL("setarch(8)"));
26b0b857 119
77eb13b9 120 exit(EXIT_SUCCESS);
8b3a46d2
KZ
121}
122
b446402b 123static void __attribute__((__noreturn__))
77eb13b9 124 show_version(void)
b446402b 125{
77eb13b9
SK
126 printf(UTIL_LINUX_VERSION);
127 exit(EXIT_SUCCESS);
b446402b 128}
8df90dec 129
77eb13b9 130static int set_arch(const char *pers, unsigned long options, int list)
8b3a46d2 131{
77eb13b9
SK
132 struct utsname un;
133 int i;
134 unsigned long pers_value;
135
136 struct {
137 int perval;
138 const char *target_arch, *result_arch;
139 } transitions[] = {
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
d9ca5dd4
RM
151 {PER_LINUX32, "ppc32le", "ppcle"},
152 {PER_LINUX32, "ppcle", "ppcle"},
153 {PER_LINUX, "ppc64le", "ppc64le"},
77eb13b9 154# endif
8b3a46d2
KZ
155#endif
156#if defined(__x86_64__) || defined(__i386__) || defined(__ia64__)
77eb13b9
SK
157 {PER_LINUX32, "i386", "i386"},
158 {PER_LINUX32, "i486", "i386"},
159 {PER_LINUX32, "i586", "i386"},
160 {PER_LINUX32, "i686", "i386"},
161 {PER_LINUX32, "athlon", "i386"},
8b3a46d2
KZ
162#endif
163#if defined(__x86_64__) || defined(__i386__)
77eb13b9 164 {PER_LINUX, "x86_64", "x86_64"},
8b3a46d2
KZ
165#endif
166#if defined(__ia64__) || defined(__i386__)
77eb13b9 167 {PER_LINUX, "ia64", "ia64"},
8b3a46d2 168#endif
782e198d 169#if defined(__hppa__)
77eb13b9
SK
170 {PER_LINUX32, "parisc32", "parisc"},
171 {PER_LINUX32, "parisc", "parisc"},
172 {PER_LINUX, "parisc64", "parisc64"},
782e198d 173#endif
8b3a46d2 174#if defined(__s390x__) || defined(__s390__)
77eb13b9
SK
175 {PER_LINUX32, "s390", "s390"},
176 {PER_LINUX, "s390x", "s390x"},
8b3a46d2
KZ
177#endif
178#if defined(__sparc64__) || defined(__sparc__)
77eb13b9
SK
179 {PER_LINUX32, "sparc", "sparc"},
180 {PER_LINUX32, "sparc32bash", "sparc"},
181 {PER_LINUX32, "sparc32", "sparc"},
182 {PER_LINUX, "sparc64", "sparc64"},
8b3a46d2
KZ
183#endif
184#if defined(__mips64__) || defined(__mips__)
77eb13b9
SK
185 {PER_LINUX32, "mips32", "mips"},
186 {PER_LINUX32, "mips", "mips"},
187 {PER_LINUX, "mips64", "mips64"},
9e19737a
BC
188#endif
189#if defined(__alpha__)
77eb13b9
SK
190 {PER_LINUX, "alpha", "alpha"},
191 {PER_LINUX, "alphaev5", "alpha"},
192 {PER_LINUX, "alphaev56", "alpha"},
193 {PER_LINUX, "alphaev6", "alpha"},
194 {PER_LINUX, "alphaev67", "alpha"},
8b3a46d2 195#endif
77eb13b9
SK
196 /* place holder, will be filled up at runtime */
197 {-1, NULL, NULL},
198 {-1, NULL, NULL}
199 };
200
201 /* Add the trivial transition {PER_LINUX, machine, machine} if no
202 * such target_arch is hardcoded yet. */
203 uname(&un);
204 for (i = 0; transitions[i].perval >= 0; i++)
205 if (!strcmp(un.machine, transitions[i].target_arch))
206 break;
207 if (transitions[i].perval < 0) {
208 unsigned long wrdsz = CHAR_BIT * sizeof(void *);
209 if (wrdsz == 32 || wrdsz == 64) {
210 /* fill up the place holder */
211 transitions[i].perval = wrdsz == 32 ? PER_LINUX32 : PER_LINUX;
212 transitions[i].target_arch = un.machine;
213 transitions[i].result_arch = un.machine;
214 }
ae2c3b5b 215 }
77eb13b9
SK
216 if (list) {
217 for (i = 0; transitions[i].target_arch != NULL; i++)
218 printf("%s\n", transitions[i].target_arch);
219 return 0;
220 }
221 for (i = 0; transitions[i].perval >= 0; i++)
222 if (!strcmp(pers, transitions[i].target_arch))
223 break;
224 if (transitions[i].perval < 0)
225 errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), pers);
226 pers_value = transitions[i].perval | options;
ae706576
DL
227 if (personality(pers_value) < 0) {
228 /*
229 * Depending on architecture and kernel version, personality
230 * syscall is either capable or incapable of returning an error.
231 * If the return value is not an error, then it's the previous
232 * personality value, which can be an arbitrary value
233 * undistinguishable from an error value.
234 * To make things clear, a second call is needed.
235 */
236 if (personality(pers_value) < 0)
237 return 1;
238 }
77eb13b9
SK
239 uname(&un);
240 if (transitions[i].result_arch && strcmp(un.machine, transitions[i].result_arch)) {
241 if (strcmp(transitions[i].result_arch, "i386")
242 || (strcmp(un.machine, "i486")
243 && strcmp(un.machine, "i586")
244 && strcmp(un.machine, "i686")
245 && strcmp(un.machine, "athlon")))
1e098672 246 errx(EXIT_FAILURE, _("Kernel cannot set architecture to %s"), pers);
77eb13b9
SK
247 }
248 return 0;
8b3a46d2
KZ
249}
250
251int main(int argc, char *argv[])
252{
5edb0ea6 253 const char *arch = NULL;
77eb13b9
SK
254 unsigned long options = 0;
255 int verbose = 0;
256 int c;
257
5edb0ea6
KZ
258 /* Options without equivalent short options */
259 enum {
260 OPT_4GB = CHAR_MAX + 1,
261 OPT_UNAME26,
262 OPT_LIST
263 };
264
9e930041 265 /* Options --3gb and --4gb are for compatibility with an old
77eb13b9
SK
266 * Debian setarch implementation. */
267 static const struct option longopts[] = {
268 {"help", no_argument, NULL, 'h'},
269 {"version", no_argument, NULL, 'V'},
270 {"verbose", no_argument, NULL, 'v'},
271 {"addr-no-randomize", no_argument, NULL, 'R'},
272 {"fdpic-funcptrs", no_argument, NULL, 'F'},
273 {"mmap-page-zero", no_argument, NULL, 'Z'},
274 {"addr-compat-layout", no_argument, NULL, 'L'},
275 {"read-implies-exec", no_argument, NULL, 'X'},
276 {"32bit", no_argument, NULL, 'B'},
277 {"short-inode", no_argument, NULL, 'I'},
278 {"whole-seconds", no_argument, NULL, 'S'},
279 {"sticky-timeouts", no_argument, NULL, 'T'},
280 {"3gb", no_argument, NULL, '3'},
281 {"4gb", no_argument, NULL, OPT_4GB},
282 {"uname-2.6", no_argument, NULL, OPT_UNAME26},
5edb0ea6 283 {"list", no_argument, NULL, OPT_LIST},
77eb13b9
SK
284 {NULL, 0, NULL, 0}
285 };
286
287 setlocale(LC_ALL, "");
288 bindtextdomain(PACKAGE, LOCALEDIR);
289 textdomain(PACKAGE);
290 atexit(close_stdout);
291
7a87a774
RM
292 if (argc < 1) {
293 warnx(_("Not enough arguments"));
294 errtryhelp(EXIT_FAILURE);
295 }
5edb0ea6
KZ
296 archwrapper = strcmp(program_invocation_short_name, "setarch") != 0;
297 if (archwrapper)
298 arch = program_invocation_short_name; /* symlinks to setarch */
299 else {
00b5ef70 300 if (1 < argc && *argv[1] != '-') {
5edb0ea6
KZ
301 arch = argv[1];
302 argv[1] = argv[0]; /* for getopt_long() to get the program name */
303 argv++;
304 argc--;
77eb13b9
SK
305 }
306 }
5edb0ea6 307
77eb13b9 308#if defined(__sparc64__) || defined(__sparc__)
5edb0ea6
KZ
309 if (archwrapper && strcmp(arch, "sparc32bash") == 0) {
310 if (set_arch(arch, 0L, 0))
fcca5f1e 311 err(EXIT_FAILURE, _("Failed to set personality to %s"), arch);
77eb13b9
SK
312 execl("/bin/bash", NULL);
313 err(EXIT_FAILURE, _("failed to execute %s"), "/bin/bash");
314 }
315#endif
8b3a46d2 316
77eb13b9
SK
317 while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZ", longopts, NULL)) != -1) {
318 switch (c) {
319 case 'h':
7a87a774 320 usage();
77eb13b9
SK
321 break;
322 case 'V':
323 show_version();
324 break;
325 case 'v':
326 verbose = 1;
327 break;
328 case 'R':
329 turn_on(ADDR_NO_RANDOMIZE, options);
330 break;
331 case 'F':
332 turn_on(FDPIC_FUNCPTRS, options);
333 break;
334 case 'Z':
335 turn_on(MMAP_PAGE_ZERO, options);
336 break;
337 case 'L':
338 turn_on(ADDR_COMPAT_LAYOUT, options);
339 break;
340 case 'X':
341 turn_on(READ_IMPLIES_EXEC, options);
342 break;
343 case 'B':
344 turn_on(ADDR_LIMIT_32BIT, options);
345 break;
346 case 'I':
347 turn_on(SHORT_INODE, options);
348 break;
349 case 'S':
350 turn_on(WHOLE_SECONDS, options);
351 break;
352 case 'T':
353 turn_on(STICKY_TIMEOUTS, options);
354 break;
355 case '3':
356 turn_on(ADDR_LIMIT_3GB, options);
357 break;
358 case OPT_4GB: /* just ignore this one */
359 break;
360 case OPT_UNAME26:
361 turn_on(UNAME26, options);
362 break;
5edb0ea6
KZ
363 case OPT_LIST:
364 if (!archwrapper) {
365 set_arch(NULL, 0, 1);
b2b5b502 366 return EXIT_SUCCESS;
5edb0ea6
KZ
367 } else
368 warnx(_("unrecognized option '--list'"));
9a8fada1 369 /* fallthrough */
77eb13b9 370 default:
677ec86c 371 errtryhelp(EXIT_FAILURE);
77eb13b9
SK
372 }
373 }
8b3a46d2 374
5edb0ea6
KZ
375 if (!arch)
376 errx(EXIT_FAILURE, _("no architecture argument specified"));
377
77eb13b9
SK
378 argc -= optind;
379 argv += optind;
8df90dec 380
5edb0ea6
KZ
381 if (set_arch(arch, options, 0))
382 err(EXIT_FAILURE, _("failed to set personality to %s"), arch);
8b3a46d2 383
9508e297
RM
384 if (verbose) {
385 printf(_("Execute command `%s'.\n"), argc ? argv[0] : "/bin/sh");
386 /* flush all output streams before exec */
387 fflush(NULL);
388 }
34c265f0 389
77eb13b9
SK
390 if (!argc) {
391 execl("/bin/sh", "-sh", NULL);
392 err(EXIT_FAILURE, _("failed to execute %s"), "/bin/sh");
393 }
8b3a46d2 394
77eb13b9
SK
395 execvp(argv[0], argv);
396 err(EXIT_FAILURE, "%s", argv[0]);
397 return EXIT_FAILURE;
8b3a46d2 398}