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