2 * Copyright (C) 2003-2007 Red Hat, Inc.
4 * This file is part of util-linux-ng.
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.
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.
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)
23 * based on ideas from the ppc32 util by Guy Streeter (2002-01), based on the
24 * sparc32 util by Jakub Jelinek (1998, 1999)
28 #include <linux/personality.h>
37 #include <sys/utsname.h>
40 #define set_pers(pers) ((long)syscall(SYS_personality, pers))
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)
46 #define turn_on(_flag, _opts) \
50 printf(_("Switching on %s.\n"), #_flag); \
54 #if !HAVE_DECL_ADDR_NO_RANDOMIZE
55 # define ADDR_NO_RANDOMIZE 0x0040000
57 #if !HAVE_DECL_FDPIC_FUNCPTRS
58 # define FDPIC_FUNCPTRS 0x0080000
60 #if !HAVE_DECL_MMAP_PAGE_ZERO
61 # define MMAP_PAGE_ZERO 0x0100000
63 #if !HAVE_DECL_ADDR_COMPAT_LAYOUT
64 # define ADDR_COMPAT_LAYOUT 0x0200000
66 #if !HAVE_DECL_READ_IMPLIES_EXEC
67 # define READ_IMPLIES_EXEC 0x0400000
69 #if !HAVE_DECL_ADDR_LIMIT_32BIT
70 # define ADDR_LIMIT_32BIT 0x0800000
72 #if !HAVE_DECL_SHORT_INODE
73 # define SHORT_INODE 0x1000000
75 #if !HAVE_DECL_WHOLE_SECONDS
76 # define WHOLE_SECONDS 0x2000000
78 #if !HAVE_DECL_STICKY_TIMEOUTS
79 # define STICKY_TIMEOUTS 0x4000000
81 #if !HAVE_DECL_ADDR_LIMIT_3GB
82 # define ADDR_LIMIT_3GB 0x8000000
85 /* Options --3gb and --4gb are for compatibitity with an old Debian setarch
87 struct option longopts[] =
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 },
105 static void __attribute__((__noreturn__))
108 const char *p = program_invocation_short_name;
113 printf(_("Usage: %s%s [options] [program [program arguments]]\n\nOptions:\n"),
114 p, !strcmp(p, "setarch") ? " <arch>" : "");
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"));
131 printf(_("\nFor more information see setarch(8).\n"));
135 static void __attribute__((__noreturn__))
136 show_usage(const char *s)
138 const char *p = program_invocation_short_name;
143 fprintf(stderr, _("%s: %s\nTry `%s --help' for more information.\n"), p, s, p);
148 int set_arch(const char *pers, unsigned long options)
152 unsigned long pers_value, res;
156 const char *target_arch, *result_arch;
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"},
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"},
174 #if defined(__x86_64__) || defined(__i386__)
175 {PER_LINUX, "x86_64", "x86_64"},
177 #if defined(__ia64__) || defined(__i386__)
178 {PER_LINUX, "ia64", "ia64"},
180 #if defined(__hppa__)
181 {PER_LINUX32, "parisc32", "parisc"},
182 {PER_LINUX32, "parisc", "parisc"},
183 {PER_LINUX, "parisc64", "parisc64"},
185 #if defined(__s390x__) || defined(__s390__)
186 {PER_LINUX32, "s390", "s390"},
187 {PER_LINUX, "s390x", "s390x"},
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"},
195 #if defined(__mips64__) || defined(__mips__)
196 {PER_LINUX32, "mips32", "mips"},
197 {PER_LINUX32, "mips", "mips"},
198 {PER_LINUX, "mips64", "mips64"},
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"},
210 for(i = 0; transitions[i].perval >= 0; i++)
211 if(!strcmp(pers, transitions[i].target_arch))
214 if(transitions[i].perval < 0)
215 error(EXIT_FAILURE, 0, _("%s: Unrecognized architecture"), pers);
217 pers_value = transitions[i].perval | options;
218 res = set_pers(pers_value);
223 if(transitions[i].result_arch &&
224 strcmp(un.machine, transitions[i].result_arch))
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);
237 int main(int argc, char *argv[])
240 unsigned long options = 0;
244 setlocale(LC_ALL, "");
245 bindtextdomain(PACKAGE, LOCALEDIR);
249 show_usage(_("Not enough arguments"));
251 p = program_invocation_short_name;
252 if (!strcmp(p, "setarch")) {
256 show_usage(_("Not enough arguments"));
258 argv[0] = argv[-1]; /* for getopt_long() to get the program name */
259 if (!strcmp(p, "-h") || !strcmp(p, "--help"))
262 #if defined(__sparc64__) || defined(__sparc__)
263 if (!strcmp(p, "sparc32bash")) {
265 error(EXIT_FAILURE, errno, _("Failed to set personality to %s"), p);
266 execl("/bin/bash", NULL);
267 error(EXIT_FAILURE, errno, "/bin/bash");
271 while ((c = getopt_long(argc, argv, "+hv3BFILRSTXZ", longopts, NULL)) != -1) {
280 turn_on(ADDR_NO_RANDOMIZE, options);
283 turn_on(FDPIC_FUNCPTRS, options);
286 turn_on(MMAP_PAGE_ZERO, options);
289 turn_on(ADDR_COMPAT_LAYOUT, options);
292 turn_on(READ_IMPLIES_EXEC, options);
295 turn_on(ADDR_LIMIT_32BIT, options);
298 turn_on(SHORT_INODE, options);
301 turn_on(WHOLE_SECONDS, options);
304 turn_on(STICKY_TIMEOUTS, options);
307 turn_on(ADDR_LIMIT_3GB, options);
309 case OPT_4GB: /* just ignore this one */
317 if (set_arch(p, options))
318 error(EXIT_FAILURE, errno, _("Failed to set personality to %s"), p);
321 execl("/bin/sh", "-sh", NULL);
322 error(EXIT_FAILURE, errno, "/bin/sh");
325 execvp(argv[0], argv);
326 error(EXIT_FAILURE, errno, "%s", argv[0]);