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