]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/setarch.c
merge coreutils' last GPL2 version of su.c
[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
56 #if !HAVE_DECL_UNAME26
57 # define UNAME26 0x0020000
58 #endif
59 #if !HAVE_DECL_ADDR_NO_RANDOMIZE
60 # define ADDR_NO_RANDOMIZE 0x0040000
61 #endif
62 #if !HAVE_DECL_FDPIC_FUNCPTRS
63 # define FDPIC_FUNCPTRS 0x0080000
64 #endif
65 #if !HAVE_DECL_MMAP_PAGE_ZERO
66 # define MMAP_PAGE_ZERO 0x0100000
67 #endif
68 #if !HAVE_DECL_ADDR_COMPAT_LAYOUT
69 # define ADDR_COMPAT_LAYOUT 0x0200000
70 #endif
71 #if !HAVE_DECL_READ_IMPLIES_EXEC
72 # define READ_IMPLIES_EXEC 0x0400000
73 #endif
74 #if !HAVE_DECL_ADDR_LIMIT_32BIT
75 # define ADDR_LIMIT_32BIT 0x0800000
76 #endif
77 #if !HAVE_DECL_SHORT_INODE
78 # define SHORT_INODE 0x1000000
79 #endif
80 #if !HAVE_DECL_WHOLE_SECONDS
81 # define WHOLE_SECONDS 0x2000000
82 #endif
83 #if !HAVE_DECL_STICKY_TIMEOUTS
84 # define STICKY_TIMEOUTS 0x4000000
85 #endif
86 #if !HAVE_DECL_ADDR_LIMIT_3GB
87 # define ADDR_LIMIT_3GB 0x8000000
88 #endif
89
90 static void __attribute__((__noreturn__))
91 show_help(void)
92 {
93 printf(USAGE_HEADER);
94 printf(_(" %s%s [options] [program [program arguments]]\n"),
95 program_invocation_short_name,
96 !strcmp(program_invocation_short_name, "setarch") ? " <arch>" : "");
97
98 printf(USAGE_OPTIONS);
99 printf(_(
100 " -v, --verbose says what options are being switched on\n"
101 " -R, --addr-no-randomize disables randomization of the virtual address space\n"
102 " -F, --fdpic-funcptrs makes function pointers point to descriptors\n"
103 " -Z, --mmap-page-zero turns on MMAP_PAGE_ZERO\n"
104 " -L, --addr-compat-layout changes the way virtual memory is allocated\n"
105 " -X, --read-implies-exec turns on READ_IMPLIES_EXEC\n"
106 " -B, --32bit turns on ADDR_LIMIT_32BIT\n"
107 " -I, --short-inode turns on SHORT_INODE\n"
108 " -S, --whole-seconds turns on WHOLE_SECONDS\n"
109 " -T, --sticky-timeouts turns on STICKY_TIMEOUTS\n"
110 " -3, --3gb limits the used address space to a maximum of 3 GB\n"
111 " --4gb ignored (for backward compatibility only)\n"
112 " --uname-2.6 turns on UNAME26\n"));
113
114 printf(USAGE_SEPARATOR);
115 printf(USAGE_HELP);
116 printf(USAGE_VERSION);
117 printf(USAGE_MAN_TAIL("setarch(8)"));
118
119 exit(EXIT_SUCCESS);
120 }
121
122 static void __attribute__((__noreturn__))
123 show_usage(const char *s)
124 {
125 errx(EXIT_FAILURE, _("%s\nTry `%s --help' for more information."), s, 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 int set_arch(const char *pers, unsigned long options)
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 {PER_LINUX32, "linux32", NULL},
146 {PER_LINUX, "linux64", NULL},
147 #if defined(__powerpc__) || defined(__powerpc64__)
148 {PER_LINUX32, "ppc32", "ppc"},
149 {PER_LINUX32, "ppc", "ppc"},
150 {PER_LINUX, "ppc64", "ppc64"},
151 {PER_LINUX, "ppc64pseries", "ppc64"},
152 {PER_LINUX, "ppc64iseries", "ppc64"},
153 #endif
154 #if defined(__x86_64__) || defined(__i386__) || defined(__ia64__)
155 {PER_LINUX32, "i386", "i386"},
156 {PER_LINUX32, "i486", "i386"},
157 {PER_LINUX32, "i586", "i386"},
158 {PER_LINUX32, "i686", "i386"},
159 {PER_LINUX32, "athlon", "i386"},
160 #endif
161 #if defined(__x86_64__) || defined(__i386__)
162 {PER_LINUX, "x86_64", "x86_64"},
163 #endif
164 #if defined(__ia64__) || defined(__i386__)
165 {PER_LINUX, "ia64", "ia64"},
166 #endif
167 #if defined(__hppa__)
168 {PER_LINUX32, "parisc32", "parisc"},
169 {PER_LINUX32, "parisc", "parisc"},
170 {PER_LINUX, "parisc64", "parisc64"},
171 #endif
172 #if defined(__s390x__) || defined(__s390__)
173 {PER_LINUX32, "s390", "s390"},
174 {PER_LINUX, "s390x", "s390x"},
175 #endif
176 #if defined(__sparc64__) || defined(__sparc__)
177 {PER_LINUX32, "sparc", "sparc"},
178 {PER_LINUX32, "sparc32bash", "sparc"},
179 {PER_LINUX32, "sparc32", "sparc"},
180 {PER_LINUX, "sparc64", "sparc64"},
181 #endif
182 #if defined(__mips64__) || defined(__mips__)
183 {PER_LINUX32, "mips32", "mips"},
184 {PER_LINUX32, "mips", "mips"},
185 {PER_LINUX, "mips64", "mips64"},
186 #endif
187 #if defined(__alpha__)
188 {PER_LINUX, "alpha", "alpha"},
189 {PER_LINUX, "alphaev5", "alpha"},
190 {PER_LINUX, "alphaev56", "alpha"},
191 {PER_LINUX, "alphaev6", "alpha"},
192 {PER_LINUX, "alphaev67", "alpha"},
193 #endif
194 {-1, NULL, NULL}
195 };
196
197 for(i = 0; transitions[i].perval >= 0; i++)
198 if(!strcmp(pers, transitions[i].target_arch))
199 break;
200
201 if(transitions[i].perval < 0)
202 errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), pers);
203
204 pers_value = transitions[i].perval | options;
205 if (set_pers(pers_value) == -EINVAL)
206 return 1;
207
208 uname(&un);
209 if(transitions[i].result_arch &&
210 strcmp(un.machine, transitions[i].result_arch))
211 {
212 if(strcmp(transitions[i].result_arch, "i386")
213 || (strcmp(un.machine, "i486")
214 && strcmp(un.machine, "i586")
215 && strcmp(un.machine, "i686")
216 && strcmp(un.machine, "athlon")))
217 errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), pers);
218 }
219
220 return 0;
221 }
222
223 int main(int argc, char *argv[])
224 {
225 const char *p;
226 unsigned long options = 0;
227 int verbose = 0;
228 int c;
229
230 /* Options --3gb and --4gb are for compatibitity with an old Debian setarch
231 implementation. */
232 static const struct option longopts[] =
233 {
234 { "help", 0, 0, 'h' },
235 { "version", 0, 0, 'V' },
236 { "verbose", 0, 0, 'v' },
237 { "addr-no-randomize", 0, 0, 'R' },
238 { "fdpic-funcptrs", 0, 0, 'F' },
239 { "mmap-page-zero", 0, 0, 'Z' },
240 { "addr-compat-layout", 0, 0, 'L' },
241 { "read-implies-exec", 0, 0, 'X' },
242 { "32bit", 0, 0, 'B' },
243 { "short-inode", 0, 0, 'I' },
244 { "whole-seconds", 0, 0, 'S' },
245 { "sticky-timeouts", 0, 0, 'T' },
246 { "3gb", 0, 0, '3' },
247 { "4gb", 0, 0, OPT_4GB },
248 { "uname-2.6", 0, 0, OPT_UNAME26 },
249 { NULL, 0, 0, 0 }
250 };
251
252 setlocale(LC_ALL, "");
253 bindtextdomain(PACKAGE, LOCALEDIR);
254 textdomain(PACKAGE);
255 atexit(close_stdout);
256
257 if (argc < 1)
258 show_usage(_("Not enough arguments"));
259
260 p = program_invocation_short_name;
261 if (!strcmp(p, "setarch")) {
262 argv++;
263 argc--;
264 if (argc < 1)
265 show_usage(_("Not enough arguments"));
266 p = argv[0];
267 argv[0] = argv[-1]; /* for getopt_long() to get the program name */
268 if (!strcmp(p, "-h") || !strcmp(p, "--help"))
269 show_help();
270 else if (!strcmp(p, "-V") || !strcmp(p, "--version"))
271 show_version();
272 }
273 #if defined(__sparc64__) || defined(__sparc__)
274 if (!strcmp(p, "sparc32bash")) {
275 if (set_arch(p, 0L))
276 err(EXIT_FAILURE, _("Failed to set personality to %s"), p);
277 execl("/bin/bash", NULL);
278 err(EXIT_FAILURE, "/bin/bash");
279 }
280 #endif
281
282 while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZ", longopts, NULL)) != -1) {
283 switch (c) {
284 case 'h':
285 show_help();
286 break;
287 case 'V':
288 show_version();
289 break;
290 case 'v':
291 verbose = 1;
292 break;
293 case 'R':
294 turn_on(ADDR_NO_RANDOMIZE, options);
295 break;
296 case 'F':
297 turn_on(FDPIC_FUNCPTRS, options);
298 break;
299 case 'Z':
300 turn_on(MMAP_PAGE_ZERO, options);
301 break;
302 case 'L':
303 turn_on(ADDR_COMPAT_LAYOUT, options);
304 break;
305 case 'X':
306 turn_on(READ_IMPLIES_EXEC, options);
307 break;
308 case 'B':
309 turn_on(ADDR_LIMIT_32BIT, options);
310 break;
311 case 'I':
312 turn_on(SHORT_INODE, options);
313 break;
314 case 'S':
315 turn_on(WHOLE_SECONDS, options);
316 break;
317 case 'T':
318 turn_on(STICKY_TIMEOUTS, options);
319 break;
320 case '3':
321 turn_on(ADDR_LIMIT_3GB, options);
322 break;
323 case OPT_4GB: /* just ignore this one */
324 break;
325 case OPT_UNAME26:
326 turn_on(UNAME26, options);
327 break;
328 }
329 }
330
331 argc -= optind;
332 argv += optind;
333
334 if (set_arch(p, options))
335 err(EXIT_FAILURE, _("Failed to set personality to %s"), p);
336
337 if (!argc) {
338 execl("/bin/sh", "-sh", NULL);
339 err(EXIT_FAILURE, "/bin/sh");
340 }
341
342 execvp(argv[0], argv);
343 err(EXIT_FAILURE, "%s", argv[0]);
344 return EXIT_FAILURE;
345 }