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