]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/setarch.c
setarch: differentiate between "unrecognized" and "cannot set"
[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 /* Options without equivalent short options */
45 enum {
46 OPT_4GB = CHAR_MAX + 1,
47 OPT_UNAME26
48 };
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
57 #ifndef UNAME26
58 # define UNAME26 0x0020000
59 #endif
60 #ifndef ADDR_NO_RANDOMIZE
61 # define ADDR_NO_RANDOMIZE 0x0040000
62 #endif
63 #ifndef FDPIC_FUNCPTRS
64 # define FDPIC_FUNCPTRS 0x0080000
65 #endif
66 #ifndef MMAP_PAGE_ZERO
67 # define MMAP_PAGE_ZERO 0x0100000
68 #endif
69 #ifndef ADDR_COMPAT_LAYOUT
70 # define ADDR_COMPAT_LAYOUT 0x0200000
71 #endif
72 #ifndef READ_IMPLIES_EXEC
73 # define READ_IMPLIES_EXEC 0x0400000
74 #endif
75 #ifndef ADDR_LIMIT_32BIT
76 # define ADDR_LIMIT_32BIT 0x0800000
77 #endif
78 #ifndef SHORT_INODE
79 # define SHORT_INODE 0x1000000
80 #endif
81 #ifndef WHOLE_SECONDS
82 # define WHOLE_SECONDS 0x2000000
83 #endif
84 #ifndef STICKY_TIMEOUTS
85 # define STICKY_TIMEOUTS 0x4000000
86 #endif
87 #ifndef ADDR_LIMIT_3GB
88 # define ADDR_LIMIT_3GB 0x8000000
89 #endif
90
91 static void __attribute__((__noreturn__)) show_help(void)
92 {
93 fputs(USAGE_HEADER, stdout);
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);
98
99 fputs(USAGE_SEPARATOR, stdout);
100 fputs(_("Change the reported architecture and set personality flags.\n"), stdout);
101
102 fputs(USAGE_OPTIONS, stdout);
103 fputs(_(" -B, --32bit turns on ADDR_LIMIT_32BIT\n"), stdout);
104 fputs(_(" -F, --fdpic-funcptrs makes function pointers point to descriptors\n"), stdout);
105 fputs(_(" -I, --short-inode turns on SHORT_INODE\n"), stdout);
106 fputs(_(" -L, --addr-compat-layout changes the way virtual memory is allocated\n"), stdout);
107 fputs(_(" -R, --addr-no-randomize disables randomization of the virtual address space\n"), stdout);
108 fputs(_(" -S, --whole-seconds turns on WHOLE_SECONDS\n"), stdout);
109 fputs(_(" -T, --sticky-timeouts turns on STICKY_TIMEOUTS\n"), stdout);
110 fputs(_(" -X, --read-implies-exec turns on READ_IMPLIES_EXEC\n"), stdout);
111 fputs(_(" -Z, --mmap-page-zero turns on MMAP_PAGE_ZERO\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(_(" -v, --verbose say what options are being switched on\n"), stdout);
116 fputs(_(" --list list settable architectures, and exit\n"), stdout);
117
118 fputs(USAGE_SEPARATOR, stdout);
119 fputs(USAGE_HELP, stdout);
120 fputs(USAGE_VERSION, stdout);
121 printf(USAGE_MAN_TAIL("setarch(8)"));
122
123 exit(EXIT_SUCCESS);
124 }
125
126 static void __attribute__((__noreturn__)) show_usage(const char *s)
127 {
128 if (s)
129 errx(EXIT_FAILURE,
130 _("%s\nTry `%s --help' for more information."), s,
131 program_invocation_short_name);
132 else
133 errx(EXIT_FAILURE, _("Try `%s --help' for more information."),
134 program_invocation_short_name);
135 }
136
137 static void __attribute__((__noreturn__))
138 show_version(void)
139 {
140 printf(UTIL_LINUX_VERSION);
141 exit(EXIT_SUCCESS);
142 }
143
144 static int set_arch(const char *pers, unsigned long options, int list)
145 {
146 struct utsname un;
147 int i;
148 unsigned long pers_value;
149
150 struct {
151 int perval;
152 const char *target_arch, *result_arch;
153 } transitions[] = {
154 {UNAME26, "uname26", NULL},
155 {PER_LINUX32, "linux32", NULL},
156 {PER_LINUX, "linux64", NULL},
157 #if defined(__powerpc__) || defined(__powerpc64__)
158 # ifdef __BIG_ENDIAN__
159 {PER_LINUX32, "ppc32", "ppc"},
160 {PER_LINUX32, "ppc", "ppc"},
161 {PER_LINUX, "ppc64", "ppc64"},
162 {PER_LINUX, "ppc64pseries", "ppc64"},
163 {PER_LINUX, "ppc64iseries", "ppc64"},
164 # else
165 PER_LINUX32, "ppc32le", "ppcle"},
166 PER_LINUX32, "ppcle", "ppcle"},
167 PER_LINUX, "ppc64le", "ppc64le"},
168 # endif
169 #endif
170 #if defined(__x86_64__) || defined(__i386__) || defined(__ia64__)
171 {PER_LINUX32, "i386", "i386"},
172 {PER_LINUX32, "i486", "i386"},
173 {PER_LINUX32, "i586", "i386"},
174 {PER_LINUX32, "i686", "i386"},
175 {PER_LINUX32, "athlon", "i386"},
176 #endif
177 #if defined(__x86_64__) || defined(__i386__)
178 {PER_LINUX, "x86_64", "x86_64"},
179 #endif
180 #if defined(__ia64__) || defined(__i386__)
181 {PER_LINUX, "ia64", "ia64"},
182 #endif
183 #if defined(__hppa__)
184 {PER_LINUX32, "parisc32", "parisc"},
185 {PER_LINUX32, "parisc", "parisc"},
186 {PER_LINUX, "parisc64", "parisc64"},
187 #endif
188 #if defined(__s390x__) || defined(__s390__)
189 {PER_LINUX32, "s390", "s390"},
190 {PER_LINUX, "s390x", "s390x"},
191 #endif
192 #if defined(__sparc64__) || defined(__sparc__)
193 {PER_LINUX32, "sparc", "sparc"},
194 {PER_LINUX32, "sparc32bash", "sparc"},
195 {PER_LINUX32, "sparc32", "sparc"},
196 {PER_LINUX, "sparc64", "sparc64"},
197 #endif
198 #if defined(__mips64__) || defined(__mips__)
199 {PER_LINUX32, "mips32", "mips"},
200 {PER_LINUX32, "mips", "mips"},
201 {PER_LINUX, "mips64", "mips64"},
202 #endif
203 #if defined(__alpha__)
204 {PER_LINUX, "alpha", "alpha"},
205 {PER_LINUX, "alphaev5", "alpha"},
206 {PER_LINUX, "alphaev56", "alpha"},
207 {PER_LINUX, "alphaev6", "alpha"},
208 {PER_LINUX, "alphaev67", "alpha"},
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 if (list) {
231 for (i = 0; transitions[i].target_arch != NULL; i++)
232 printf("%s\n", transitions[i].target_arch);
233 return 0;
234 }
235 for (i = 0; transitions[i].perval >= 0; i++)
236 if (!strcmp(pers, transitions[i].target_arch))
237 break;
238 if (transitions[i].perval < 0)
239 errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), pers);
240 pers_value = transitions[i].perval | options;
241 if (personality(pers_value) == -EINVAL)
242 return 1;
243 uname(&un);
244 if (transitions[i].result_arch && strcmp(un.machine, transitions[i].result_arch)) {
245 if (strcmp(transitions[i].result_arch, "i386")
246 || (strcmp(un.machine, "i486")
247 && strcmp(un.machine, "i586")
248 && strcmp(un.machine, "i686")
249 && strcmp(un.machine, "athlon")))
250 errx(EXIT_FAILURE, _("Kernel cannot set architecture to %s"), pers);
251 }
252 return 0;
253 }
254
255 int main(int argc, char *argv[])
256 {
257 const char *p;
258 unsigned long options = 0;
259 int verbose = 0;
260 int c;
261
262 /* Options --3gb and --4gb are for compatibitity with an old
263 * Debian setarch implementation. */
264 static const struct option longopts[] = {
265 {"help", no_argument, NULL, 'h'},
266 {"version", no_argument, NULL, 'V'},
267 {"verbose", no_argument, NULL, 'v'},
268 {"addr-no-randomize", no_argument, NULL, 'R'},
269 {"fdpic-funcptrs", no_argument, NULL, 'F'},
270 {"mmap-page-zero", no_argument, NULL, 'Z'},
271 {"addr-compat-layout", no_argument, NULL, 'L'},
272 {"read-implies-exec", no_argument, NULL, 'X'},
273 {"32bit", no_argument, NULL, 'B'},
274 {"short-inode", no_argument, NULL, 'I'},
275 {"whole-seconds", no_argument, NULL, 'S'},
276 {"sticky-timeouts", no_argument, NULL, 'T'},
277 {"3gb", no_argument, NULL, '3'},
278 {"4gb", no_argument, NULL, OPT_4GB},
279 {"uname-2.6", no_argument, NULL, OPT_UNAME26},
280 {NULL, 0, NULL, 0}
281 };
282
283 setlocale(LC_ALL, "");
284 bindtextdomain(PACKAGE, LOCALEDIR);
285 textdomain(PACKAGE);
286 atexit(close_stdout);
287
288 if (argc < 1)
289 show_usage(_("Not enough arguments"));
290
291 p = program_invocation_short_name;
292 if (!strcmp(p, "setarch")) {
293 argc--;
294 if (argc < 1)
295 show_usage(_("Not enough arguments"));
296 p = argv[1];
297 argv[1] = argv[0]; /* for getopt_long() to get the program name */
298 argv++;
299 if (!strcmp(p, "-h") || !strcmp(p, "--help"))
300 show_help();
301 else if (!strcmp(p, "-V") || !strcmp(p, "--version"))
302 show_version();
303 else if (!strcmp(p, "--list")) {
304 set_arch(argv[0], 0L, 1);
305 return EXIT_SUCCESS;
306 }
307 }
308 #if defined(__sparc64__) || defined(__sparc__)
309 if (!strcmp(p, "sparc32bash")) {
310 if (set_arch(p, 0L, 0))
311 err(EXIT_FAILURE, _("Failed to set personality to %s"), p);
312 execl("/bin/bash", NULL);
313 err(EXIT_FAILURE, _("failed to execute %s"), "/bin/bash");
314 }
315 #endif
316
317 while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZ", longopts, NULL)) != -1) {
318 switch (c) {
319 case 'h':
320 show_help();
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;
363 default:
364 show_usage(NULL);
365 }
366 }
367
368 argc -= optind;
369 argv += optind;
370
371 if (set_arch(p, options, 0))
372 err(EXIT_FAILURE, _("Failed to set personality to %s"), p);
373
374 /* flush all output streams before exec */
375 fflush(NULL);
376
377 if (!argc) {
378 execl("/bin/sh", "-sh", NULL);
379 err(EXIT_FAILURE, _("failed to execute %s"), "/bin/sh");
380 }
381
382 execvp(argv[0], argv);
383 err(EXIT_FAILURE, "%s", argv[0]);
384 return EXIT_FAILURE;
385 }