]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/setarch.c
Merge branch 'sys-utils' of https://github.com/kerolasa/lelux-utiliteetit
[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
39 #define set_pers(pers) ((long)syscall(SYS_personality, pers))
40
41 /* Options without equivalent short options */
42 enum {
43 OPT_4GB = CHAR_MAX + 1,
44 OPT_UNAME26
45 };
46
47 #define turn_on(_flag, _opts) \
48 do { \
49 (_opts) |= _flag; \
50 if (verbose) \
51 printf(_("Switching on %s.\n"), #_flag); \
52 } while(0)
53
54
55 #if !HAVE_DECL_UNAME26
56 # define UNAME26 0x0020000
57 #endif
58 #if !HAVE_DECL_ADDR_NO_RANDOMIZE
59 # define ADDR_NO_RANDOMIZE 0x0040000
60 #endif
61 #if !HAVE_DECL_FDPIC_FUNCPTRS
62 # define FDPIC_FUNCPTRS 0x0080000
63 #endif
64 #if !HAVE_DECL_MMAP_PAGE_ZERO
65 # define MMAP_PAGE_ZERO 0x0100000
66 #endif
67 #if !HAVE_DECL_ADDR_COMPAT_LAYOUT
68 # define ADDR_COMPAT_LAYOUT 0x0200000
69 #endif
70 #if !HAVE_DECL_READ_IMPLIES_EXEC
71 # define READ_IMPLIES_EXEC 0x0400000
72 #endif
73 #if !HAVE_DECL_ADDR_LIMIT_32BIT
74 # define ADDR_LIMIT_32BIT 0x0800000
75 #endif
76 #if !HAVE_DECL_SHORT_INODE
77 # define SHORT_INODE 0x1000000
78 #endif
79 #if !HAVE_DECL_WHOLE_SECONDS
80 # define WHOLE_SECONDS 0x2000000
81 #endif
82 #if !HAVE_DECL_STICKY_TIMEOUTS
83 # define STICKY_TIMEOUTS 0x4000000
84 #endif
85 #if !HAVE_DECL_ADDR_LIMIT_3GB
86 # define ADDR_LIMIT_3GB 0x8000000
87 #endif
88
89 static void __attribute__((__noreturn__))
90 show_help(void)
91 {
92 printf(_("Usage: %s%s [options] [program [program arguments]]\n\nOptions:\n"),
93 program_invocation_short_name, !strcmp(program_invocation_short_name, "setarch") ? " <arch>" : "");
94
95 printf(_(
96 " -v, --verbose says what options are being switched on\n"
97 " -R, --addr-no-randomize disables randomization of the virtual address space\n"
98 " -F, --fdpic-funcptrs makes function pointers point to descriptors\n"
99 " -Z, --mmap-page-zero turns on MMAP_PAGE_ZERO\n"
100 " -L, --addr-compat-layout changes the way virtual memory is allocated\n"
101 " -X, --read-implies-exec turns on READ_IMPLIES_EXEC\n"
102 " -B, --32bit turns on ADDR_LIMIT_32BIT\n"
103 " -I, --short-inode turns on SHORT_INODE\n"
104 " -S, --whole-seconds turns on WHOLE_SECONDS\n"
105 " -T, --sticky-timeouts turns on STICKY_TIMEOUTS\n"
106 " -3, --3gb limits the used address space to a maximum of 3 GB\n"
107 " --4gb ignored (for backward compatibility only)\n"));
108
109 printf(_(
110 " --uname-2.6 turns on UNAME26\n"));
111 printf(USAGE_HELP);
112 printf(USAGE_VERSION);
113
114 printf(_("\nFor more information see setarch(8).\n"));
115 exit(EXIT_SUCCESS);
116 }
117
118 static void __attribute__((__noreturn__))
119 show_usage(const char *s)
120 {
121 errx(EXIT_FAILURE, _("%s\nTry `%s --help' for more information."), s, program_invocation_short_name);
122 }
123
124 static void __attribute__((__noreturn__))
125 show_version(void)
126 {
127 printf(UTIL_LINUX_VERSION);
128 exit(EXIT_SUCCESS);
129 }
130
131 int set_arch(const char *pers, unsigned long options)
132 {
133 struct utsname un;
134 int i;
135 unsigned long pers_value;
136
137 struct {
138 int perval;
139 const char *target_arch, *result_arch;
140 } transitions[] = {
141 {PER_LINUX32, "linux32", NULL},
142 {PER_LINUX, "linux64", NULL},
143 #if defined(__powerpc__) || defined(__powerpc64__)
144 {PER_LINUX32, "ppc32", "ppc"},
145 {PER_LINUX32, "ppc", "ppc"},
146 {PER_LINUX, "ppc64", "ppc64"},
147 {PER_LINUX, "ppc64pseries", "ppc64"},
148 {PER_LINUX, "ppc64iseries", "ppc64"},
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 {-1, NULL, NULL}
191 };
192
193 for(i = 0; transitions[i].perval >= 0; i++)
194 if(!strcmp(pers, transitions[i].target_arch))
195 break;
196
197 if(transitions[i].perval < 0)
198 errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), pers);
199
200 pers_value = transitions[i].perval | options;
201 if (set_pers(pers_value) == -EINVAL)
202 return 1;
203
204 uname(&un);
205 if(transitions[i].result_arch &&
206 strcmp(un.machine, transitions[i].result_arch))
207 {
208 if(strcmp(transitions[i].result_arch, "i386")
209 || (strcmp(un.machine, "i486")
210 && strcmp(un.machine, "i586")
211 && strcmp(un.machine, "i686")
212 && strcmp(un.machine, "athlon")))
213 errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), pers);
214 }
215
216 return 0;
217 }
218
219 int main(int argc, char *argv[])
220 {
221 const char *p;
222 unsigned long options = 0;
223 int verbose = 0;
224 int c;
225
226 /* Options --3gb and --4gb are for compatibitity with an old Debian setarch
227 implementation. */
228 static const struct option longopts[] =
229 {
230 { "help", 0, 0, 'h' },
231 { "version", 0, 0, 'V' },
232 { "verbose", 0, 0, 'v' },
233 { "addr-no-randomize", 0, 0, 'R' },
234 { "fdpic-funcptrs", 0, 0, 'F' },
235 { "mmap-page-zero", 0, 0, 'Z' },
236 { "addr-compat-layout", 0, 0, 'L' },
237 { "read-implies-exec", 0, 0, 'X' },
238 { "32bit", 0, 0, 'B' },
239 { "short-inode", 0, 0, 'I' },
240 { "whole-seconds", 0, 0, 'S' },
241 { "sticky-timeouts", 0, 0, 'T' },
242 { "3gb", 0, 0, '3' },
243 { "4gb", 0, 0, OPT_4GB },
244 { "uname-2.6", 0, 0, OPT_UNAME26 },
245 { NULL, 0, 0, 0 }
246 };
247
248 setlocale(LC_ALL, "");
249 bindtextdomain(PACKAGE, LOCALEDIR);
250 textdomain(PACKAGE);
251
252 if (argc < 1)
253 show_usage(_("Not enough arguments"));
254
255 p = program_invocation_short_name;
256 if (!strcmp(p, "setarch")) {
257 argv++;
258 argc--;
259 if (argc < 1)
260 show_usage(_("Not enough arguments"));
261 p = argv[0];
262 argv[0] = argv[-1]; /* for getopt_long() to get the program name */
263 if (!strcmp(p, "-h") || !strcmp(p, "--help"))
264 show_help();
265 else if (!strcmp(p, "-V") || !strcmp(p, "--version"))
266 show_version();
267 }
268 #if defined(__sparc64__) || defined(__sparc__)
269 if (!strcmp(p, "sparc32bash")) {
270 if (set_arch(p, 0L))
271 err(EXIT_FAILURE, _("Failed to set personality to %s"), p);
272 execl("/bin/bash", NULL);
273 err(EXIT_FAILURE, "/bin/bash");
274 }
275 #endif
276
277 while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZ", longopts, NULL)) != -1) {
278 switch (c) {
279 case 'h':
280 show_help();
281 break;
282 case 'V':
283 show_version();
284 break;
285 case 'v':
286 verbose = 1;
287 break;
288 case 'R':
289 turn_on(ADDR_NO_RANDOMIZE, options);
290 break;
291 case 'F':
292 turn_on(FDPIC_FUNCPTRS, options);
293 break;
294 case 'Z':
295 turn_on(MMAP_PAGE_ZERO, options);
296 break;
297 case 'L':
298 turn_on(ADDR_COMPAT_LAYOUT, options);
299 break;
300 case 'X':
301 turn_on(READ_IMPLIES_EXEC, options);
302 break;
303 case 'B':
304 turn_on(ADDR_LIMIT_32BIT, options);
305 break;
306 case 'I':
307 turn_on(SHORT_INODE, options);
308 break;
309 case 'S':
310 turn_on(WHOLE_SECONDS, options);
311 break;
312 case 'T':
313 turn_on(STICKY_TIMEOUTS, options);
314 break;
315 case '3':
316 turn_on(ADDR_LIMIT_3GB, options);
317 break;
318 case OPT_4GB: /* just ignore this one */
319 break;
320 case OPT_UNAME26:
321 turn_on(UNAME26, options);
322 break;
323 }
324 }
325
326 argc -= optind;
327 argv += optind;
328
329 if (set_arch(p, options))
330 err(EXIT_FAILURE, _("Failed to set personality to %s"), p);
331
332 if (!argc) {
333 execl("/bin/sh", "-sh", NULL);
334 err(EXIT_FAILURE, "/bin/sh");
335 }
336
337 execvp(argv[0], argv);
338 err(EXIT_FAILURE, "%s", argv[0]);
339 return EXIT_FAILURE;
340 }