]>
Commit | Line | Data |
---|---|---|
8b3a46d2 KZ |
1 | /* Copyright (C) 2003-2006 Red Hat, Inc. |
2 | * Licensed under the terms of the GPL | |
3 | * | |
4 | * Written by Elliot Lee <sopwith@redhat.com> | |
5 | * New personality options & code added by Jindrich Novy <jnovy@redhat.com> | |
6 | * ADD_NO_RANDOMIZE flag added by Arjan van de Ven <arjanv@redhat.com> | |
7 | * Help and MIPS support from Mike Frysinger (vapier@gentoo.org) | |
8 | * Better error handling from Dmitry V. Levin (ldv@altlinux.org) | |
9 | * | |
10 | * based on ideas from the ppc32 util by Guy Streeter (2002-01), based on the | |
11 | * sparc32 util by Jakub Jelinek (1998, 1999) | |
12 | */ | |
13 | ||
14 | #ifndef _GNU_SOURCE | |
15 | # define _GNU_SOURCE | |
16 | #endif | |
17 | ||
18 | #include <syscall.h> | |
19 | #include <linux/personality.h> | |
20 | #include <unistd.h> | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
23 | #include <stdlib.h> | |
24 | #include <errno.h> | |
25 | #include <error.h> | |
26 | #include <sys/utsname.h> | |
a5b47e0a | 27 | #include "nls.h" |
8b3a46d2 KZ |
28 | |
29 | #define set_pers(pers) ((long)syscall(SYS_personality, pers)) | |
30 | ||
31 | struct { | |
32 | char c; | |
33 | const char *name; | |
34 | unsigned int option; | |
35 | } flags[] = { | |
36 | {'R', "ADDR_NO_RANDOMIZE", 0x0040000}, | |
37 | {'F', "FDPIC_FUNCPTRS", 0x0080000}, | |
38 | {'Z', "MMAP_PAGE_ZERO", 0x0100000}, | |
39 | {'L', "ADDR_COMPAT_LAYOUT", 0x0200000}, | |
40 | {'X', "READ_IMPLIES_EXEC", 0x0400000}, | |
41 | {'B', "ADDR_LIMIT_32BIT", 0x0800000}, | |
42 | {'I', "SHORT_INODE", 0x1000000}, | |
43 | {'S', "WHOLE_SECONDS", 0x2000000}, | |
44 | {'T', "STICKY_TIMEOUTS", 0x4000000}, | |
45 | {'3', "ADDR_LIMIT_3GB", 0x8000000} | |
46 | }; | |
47 | ||
48 | static void __attribute__((__noreturn__)) | |
49 | show_help(void) | |
50 | { | |
51 | int f; | |
52 | const char *p = program_invocation_short_name; | |
53 | ||
54 | if (!*p) | |
55 | p = "setarch"; | |
56 | ||
a5b47e0a | 57 | printf(_("Usage: %s%s [options] [program [program arguments]]\n\nOptions:\n"), |
8b3a46d2 KZ |
58 | p, !strcmp(p, "setarch") ? " <arch>" : ""); |
59 | ||
60 | for (f = 0; f < sizeof(flags) / sizeof(flags[0]); f++) | |
a5b47e0a | 61 | printf(_("\t-%c\tEnable %s\n"), flags[f].c, flags[f].name); |
8b3a46d2 | 62 | |
a5b47e0a | 63 | printf(_("\nFor more information see setarch(8).\n")); |
8b3a46d2 KZ |
64 | exit(EXIT_SUCCESS); |
65 | } | |
66 | ||
67 | static void __attribute__((__noreturn__)) | |
68 | show_usage(const char *s) | |
69 | { | |
70 | const char *p = program_invocation_short_name; | |
71 | ||
72 | if (!*p) | |
73 | p = "setarch"; | |
74 | ||
a5b47e0a | 75 | fprintf(stderr, _("%s: %s\nTry `%s --help' for more information.\n"), p, s, p); |
8b3a46d2 KZ |
76 | exit(EXIT_FAILURE); |
77 | } | |
78 | ||
79 | int set_arch(const char *pers, unsigned long options) | |
80 | { | |
81 | struct utsname un; | |
82 | int i; | |
83 | unsigned long pers_value, res; | |
84 | ||
85 | struct { | |
86 | int perval; | |
87 | const char *target_arch, *result_arch; | |
88 | } transitions[] = { | |
89 | {PER_LINUX32, "linux32", NULL}, | |
90 | {PER_LINUX, "linux64", NULL}, | |
91 | #if defined(__powerpc__) || defined(__powerpc64__) | |
92 | {PER_LINUX32, "ppc32", "ppc"}, | |
93 | {PER_LINUX32, "ppc", "ppc"}, | |
94 | {PER_LINUX, "ppc64", "ppc64"}, | |
95 | {PER_LINUX, "ppc64pseries", "ppc64"}, | |
96 | {PER_LINUX, "ppc64iseries", "ppc64"}, | |
97 | #endif | |
98 | #if defined(__x86_64__) || defined(__i386__) || defined(__ia64__) | |
99 | {PER_LINUX32, "i386", "i386"}, | |
100 | {PER_LINUX32, "i486", "i386"}, | |
101 | {PER_LINUX32, "i586", "i386"}, | |
102 | {PER_LINUX32, "i686", "i386"}, | |
103 | {PER_LINUX32, "athlon", "i386"}, | |
104 | #endif | |
105 | #if defined(__x86_64__) || defined(__i386__) | |
106 | {PER_LINUX, "x86_64", "x86_64"}, | |
107 | #endif | |
108 | #if defined(__ia64__) || defined(__i386__) | |
109 | {PER_LINUX, "ia64", "ia64"}, | |
110 | #endif | |
111 | #if defined(__s390x__) || defined(__s390__) | |
112 | {PER_LINUX32, "s390", "s390"}, | |
113 | {PER_LINUX, "s390x", "s390x"}, | |
114 | #endif | |
115 | #if defined(__sparc64__) || defined(__sparc__) | |
116 | {PER_LINUX32, "sparc", "sparc"}, | |
ee7401c5 | 117 | {PER_LINUX32, "sparc32bash", "sparc"}, |
8b3a46d2 KZ |
118 | {PER_LINUX32, "sparc32", "sparc"}, |
119 | {PER_LINUX, "sparc64", "sparc64"}, | |
120 | #endif | |
121 | #if defined(__mips64__) || defined(__mips__) | |
122 | {PER_LINUX32, "mips32", "mips"}, | |
123 | {PER_LINUX32, "mips", "mips"}, | |
124 | {PER_LINUX, "mips64", "mips64"}, | |
125 | #endif | |
126 | {-1, NULL, NULL} | |
127 | }; | |
128 | ||
129 | for(i = 0; transitions[i].perval >= 0; i++) | |
130 | if(!strcmp(pers, transitions[i].target_arch)) | |
131 | break; | |
132 | ||
133 | if(transitions[i].perval < 0) | |
a5b47e0a | 134 | error(EXIT_FAILURE, 0, _("%s: Unrecognized architecture"), pers); |
8b3a46d2 KZ |
135 | |
136 | pers_value = transitions[i].perval | options; | |
137 | res = set_pers(pers_value); | |
138 | if(res == -EINVAL) | |
139 | return 1; | |
140 | ||
141 | uname(&un); | |
142 | if(transitions[i].result_arch && | |
143 | strcmp(un.machine, transitions[i].result_arch)) | |
144 | { | |
145 | if(strcmp(transitions[i].result_arch, "i386") | |
146 | || (strcmp(un.machine, "i486") | |
147 | && strcmp(un.machine, "i586") | |
148 | && strcmp(un.machine, "i686") | |
149 | && strcmp(un.machine, "athlon"))) | |
a5b47e0a | 150 | error(EXIT_FAILURE, 0, _("%s: Unrecognized architecture"), pers); |
8b3a46d2 KZ |
151 | } |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | int main(int argc, char *argv[]) | |
157 | { | |
158 | const char *p; | |
159 | unsigned long options = 0; | |
160 | int verbose = 0; | |
161 | ||
a5b47e0a KZ |
162 | setlocale(LC_ALL, ""); |
163 | bindtextdomain(PACKAGE, LOCALEDIR); | |
164 | textdomain(PACKAGE); | |
165 | ||
8b3a46d2 | 166 | if (argc < 1) |
a5b47e0a | 167 | show_usage(_("Not enough arguments")); |
8b3a46d2 KZ |
168 | |
169 | p = program_invocation_short_name; | |
170 | if (!strcmp(p, "setarch")) { | |
171 | argv++; | |
172 | argc--; | |
173 | if (argc < 1) | |
a5b47e0a | 174 | show_usage(_("Not enough arguments")); |
8b3a46d2 KZ |
175 | p = argv[0]; |
176 | if (!strcmp(p, "-h") || !strcmp(p, "--help")) | |
177 | show_help(); | |
178 | } | |
ee7401c5 DG |
179 | #if defined(__sparc64__) || defined(__sparc__) |
180 | if (!strcmp(p, "sparc32bash")) { | |
181 | if (set_arch(p, NULL)) | |
182 | error(EXIT_FAILURE, errno, "Failed to set personality to %s", p); | |
183 | execl("/bin/bash", NULL); | |
184 | error(EXIT_FAILURE, errno, "/bin/bash"); | |
185 | } | |
186 | #endif | |
8b3a46d2 KZ |
187 | for (argv++, argc--; argc && argv[0][0] == '-'; argv++, argc--) { |
188 | int n, unknown = 1; | |
189 | const char *arg = argv[0]; | |
190 | ||
191 | if (!strcmp(arg, "--help")) | |
192 | show_help(); | |
193 | ||
194 | for (n = 1; arg[n]; n++) { | |
195 | int f; | |
196 | ||
197 | if (arg[n] == 'v') { | |
198 | verbose = 1; | |
199 | continue; | |
200 | } | |
201 | ||
202 | if (arg[n] == 'h') | |
203 | show_help(); | |
204 | ||
205 | for (f = 0; f < sizeof(flags) / sizeof(flags[0]); f++) { | |
206 | if (arg[n] == flags[f].c) { | |
207 | if (verbose) | |
a5b47e0a | 208 | fprintf(stderr, _("Switching on %s.\n"), flags[f].name); |
8b3a46d2 KZ |
209 | options |= flags[f].option; |
210 | unknown = 0; | |
211 | break; | |
212 | } | |
213 | } | |
214 | if (unknown) | |
a5b47e0a | 215 | error(0, 0, _("Unknown option `%c' ignored"), arg[n]); |
8b3a46d2 KZ |
216 | } |
217 | } | |
218 | ||
219 | if (set_arch(p, options)) | |
a5b47e0a | 220 | error(EXIT_FAILURE, errno, _("Failed to set personality to %s"), p); |
8b3a46d2 KZ |
221 | |
222 | if (!argc) { | |
223 | execl("/bin/sh", "-sh", NULL); | |
224 | error(EXIT_FAILURE, errno, "/bin/sh"); | |
225 | } | |
226 | ||
227 | execvp(argv[0], argv); | |
228 | error(EXIT_FAILURE, errno, "%s", argv[0]); | |
229 | return EXIT_FAILURE; | |
230 | } |