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