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