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