]>
Commit | Line | Data |
---|---|---|
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 | ||
9ed11cc2 | 27 | #include <sys/personality.h> |
8b3a46d2 KZ |
28 | #include <unistd.h> |
29 | #include <stdio.h> | |
30 | #include <string.h> | |
31 | #include <stdlib.h> | |
8df90dec KZ |
32 | #include <getopt.h> |
33 | #include <limits.h> | |
8b3a46d2 | 34 | #include <sys/utsname.h> |
a5b47e0a | 35 | #include "nls.h" |
c60103a9 | 36 | #include "c.h" |
efb8854f | 37 | #include "closestream.h" |
035fc2d9 | 38 | #include "sysfs.h" |
03a254f0 | 39 | #include "strutils.h" |
e6446e13 | 40 | #include "procfs.h" |
8b3a46d2 | 41 | |
9ed11cc2 SK |
42 | #ifndef HAVE_PERSONALITY |
43 | # include <syscall.h> | |
44 | # define personality(pers) ((long)syscall(SYS_personality, pers)) | |
45 | #endif | |
8b3a46d2 | 46 | |
8df90dec KZ |
47 | #define turn_on(_flag, _opts) \ |
48 | do { \ | |
49 | (_opts) |= _flag; \ | |
50 | if (verbose) \ | |
51 | printf(_("Switching on %s.\n"), #_flag); \ | |
52 | } while(0) | |
53 | ||
f29a3d18 | 54 | #ifndef UNAME26 |
70eebc40 BH |
55 | # define UNAME26 0x0020000 |
56 | #endif | |
f29a3d18 | 57 | #ifndef ADDR_NO_RANDOMIZE |
2d281745 KZ |
58 | # define ADDR_NO_RANDOMIZE 0x0040000 |
59 | #endif | |
f29a3d18 | 60 | #ifndef FDPIC_FUNCPTRS |
2d281745 KZ |
61 | # define FDPIC_FUNCPTRS 0x0080000 |
62 | #endif | |
f29a3d18 | 63 | #ifndef MMAP_PAGE_ZERO |
2d281745 KZ |
64 | # define MMAP_PAGE_ZERO 0x0100000 |
65 | #endif | |
f29a3d18 | 66 | #ifndef ADDR_COMPAT_LAYOUT |
2d281745 KZ |
67 | # define ADDR_COMPAT_LAYOUT 0x0200000 |
68 | #endif | |
f29a3d18 | 69 | #ifndef READ_IMPLIES_EXEC |
2d281745 KZ |
70 | # define READ_IMPLIES_EXEC 0x0400000 |
71 | #endif | |
f29a3d18 | 72 | #ifndef ADDR_LIMIT_32BIT |
2d281745 KZ |
73 | # define ADDR_LIMIT_32BIT 0x0800000 |
74 | #endif | |
f29a3d18 | 75 | #ifndef SHORT_INODE |
2d281745 KZ |
76 | # define SHORT_INODE 0x1000000 |
77 | #endif | |
f29a3d18 | 78 | #ifndef WHOLE_SECONDS |
2d281745 KZ |
79 | # define WHOLE_SECONDS 0x2000000 |
80 | #endif | |
f29a3d18 | 81 | #ifndef STICKY_TIMEOUTS |
2d281745 KZ |
82 | # define STICKY_TIMEOUTS 0x4000000 |
83 | #endif | |
f29a3d18 | 84 | #ifndef ADDR_LIMIT_3GB |
2d281745 KZ |
85 | # define ADDR_LIMIT_3GB 0x8000000 |
86 | #endif | |
87 | ||
2756f004 KZ |
88 | /* fallback for old glibc-headers 2.17 */ |
89 | #ifndef PER_LINUX_FDPIC | |
90 | # define PER_LINUX_FDPIC (PER_LINUX | FDPIC_FUNCPTRS) | |
91 | #endif | |
92 | ||
beef18da FF |
93 | /* fallback for old uclibc-headers < 1.0.39 */ |
94 | #ifndef PER_LINUX32_3GB | |
95 | # define PER_LINUX32_3GB (PER_LINUX32 | ADDR_LIMIT_3GB) | |
96 | #endif | |
97 | ||
03a254f0 TW |
98 | #define ALL_PERSONALITIES \ |
99 | X(PER_LINUX) \ | |
100 | X(PER_LINUX_32BIT) \ | |
101 | X(PER_LINUX_FDPIC) \ | |
102 | X(PER_SVR4) \ | |
103 | X(PER_SVR3) \ | |
104 | X(PER_SCOSVR3) \ | |
105 | X(PER_OSR5) \ | |
106 | X(PER_WYSEV386) \ | |
107 | X(PER_ISCR4) \ | |
108 | X(PER_BSD) \ | |
109 | X(PER_SUNOS) \ | |
110 | X(PER_XENIX) \ | |
111 | X(PER_LINUX32) \ | |
112 | X(PER_LINUX32_3GB) \ | |
113 | X(PER_IRIX32) \ | |
114 | X(PER_IRIXN32) \ | |
115 | X(PER_IRIX64) \ | |
116 | X(PER_RISCOS) \ | |
117 | X(PER_SOLARIS) \ | |
118 | X(PER_UW7) \ | |
119 | X(PER_OSF4) \ | |
120 | X(PER_HPUX) \ | |
121 | ||
122 | ||
123 | #define ALL_OPTIONS \ | |
124 | X(UNAME26) \ | |
125 | X(ADDR_NO_RANDOMIZE) \ | |
126 | X(FDPIC_FUNCPTRS) \ | |
127 | X(MMAP_PAGE_ZERO) \ | |
128 | X(ADDR_COMPAT_LAYOUT) \ | |
129 | X(READ_IMPLIES_EXEC) \ | |
130 | X(ADDR_LIMIT_32BIT) \ | |
131 | X(SHORT_INODE) \ | |
132 | X(WHOLE_SECONDS) \ | |
133 | X(STICKY_TIMEOUTS) \ | |
134 | X(ADDR_LIMIT_3GB) \ | |
135 | ||
5edb0ea6 | 136 | |
1db8bf93 KZ |
137 | struct arch_domain { |
138 | int perval; /* PER_* */ | |
139 | const char *target_arch; | |
140 | const char *result_arch; | |
141 | }; | |
142 | ||
143 | ||
cbfa1442 | 144 | static void __attribute__((__noreturn__)) usage(int archwrapper) |
8b3a46d2 | 145 | { |
77eb13b9 | 146 | fputs(USAGE_HEADER, stdout); |
5edb0ea6 | 147 | if (!archwrapper) |
bad4c729 | 148 | fprintf(stdout, _(" %s [<arch>] [options] [<program> [<argument>...]]\n"), program_invocation_short_name); |
68243049 | 149 | else |
bad4c729 | 150 | fprintf(stdout, _(" %s [options] [<program> [<argument>...]]\n"), program_invocation_short_name); |
451dbcfa BS |
151 | |
152 | fputs(USAGE_SEPARATOR, stdout); | |
153 | fputs(_("Change the reported architecture and set personality flags.\n"), stdout); | |
154 | ||
77eb13b9 | 155 | fputs(USAGE_OPTIONS, stdout); |
77eb13b9 | 156 | fputs(_(" -B, --32bit turns on ADDR_LIMIT_32BIT\n"), stdout); |
26b0b857 | 157 | fputs(_(" -F, --fdpic-funcptrs makes function pointers point to descriptors\n"), stdout); |
77eb13b9 | 158 | fputs(_(" -I, --short-inode turns on SHORT_INODE\n"), stdout); |
26b0b857 BS |
159 | fputs(_(" -L, --addr-compat-layout changes the way virtual memory is allocated\n"), stdout); |
160 | fputs(_(" -R, --addr-no-randomize disables randomization of the virtual address space\n"), stdout); | |
77eb13b9 SK |
161 | fputs(_(" -S, --whole-seconds turns on WHOLE_SECONDS\n"), stdout); |
162 | fputs(_(" -T, --sticky-timeouts turns on STICKY_TIMEOUTS\n"), stdout); | |
26b0b857 BS |
163 | fputs(_(" -X, --read-implies-exec turns on READ_IMPLIES_EXEC\n"), stdout); |
164 | fputs(_(" -Z, --mmap-page-zero turns on MMAP_PAGE_ZERO\n"), stdout); | |
77eb13b9 SK |
165 | fputs(_(" -3, --3gb limits the used address space to a maximum of 3 GB\n"), stdout); |
166 | fputs(_(" --4gb ignored (for backward compatibility only)\n"), stdout); | |
167 | fputs(_(" --uname-2.6 turns on UNAME26\n"), stdout); | |
26b0b857 | 168 | fputs(_(" -v, --verbose say what options are being switched on\n"), stdout); |
5edb0ea6 | 169 | |
03a254f0 | 170 | if (!archwrapper) { |
5edb0ea6 | 171 | fputs(_(" --list list settable architectures, and exit\n"), stdout); |
03a254f0 | 172 | fputs(_(" --show[=personality] show current or specific personality and exit\n"), stdout); |
e6446e13 | 173 | fputs(_(" -p, --pid=PID show the personality of the process using with --show\n"), stdout); |
03a254f0 | 174 | } |
26b0b857 | 175 | |
77eb13b9 | 176 | fputs(USAGE_SEPARATOR, stdout); |
bad4c729 MY |
177 | fprintf(stdout, USAGE_HELP_OPTIONS(26)); |
178 | fprintf(stdout, USAGE_MAN_TAIL("setarch(8)")); | |
26b0b857 | 179 | |
77eb13b9 | 180 | exit(EXIT_SUCCESS); |
8b3a46d2 KZ |
181 | } |
182 | ||
1db8bf93 KZ |
183 | /* |
184 | * Returns inilialized list of all available execution domains. | |
185 | */ | |
186 | static struct arch_domain *init_arch_domains(void) | |
8b3a46d2 | 187 | { |
33e87bf1 | 188 | static struct utsname un; |
1db8bf93 | 189 | size_t i; |
77eb13b9 | 190 | |
1db8bf93 KZ |
191 | static struct arch_domain transitions[] = |
192 | { | |
77eb13b9 SK |
193 | {UNAME26, "uname26", NULL}, |
194 | {PER_LINUX32, "linux32", NULL}, | |
195 | {PER_LINUX, "linux64", NULL}, | |
8b3a46d2 | 196 | #if defined(__powerpc__) || defined(__powerpc64__) |
77eb13b9 SK |
197 | # ifdef __BIG_ENDIAN__ |
198 | {PER_LINUX32, "ppc32", "ppc"}, | |
199 | {PER_LINUX32, "ppc", "ppc"}, | |
200 | {PER_LINUX, "ppc64", "ppc64"}, | |
201 | {PER_LINUX, "ppc64pseries", "ppc64"}, | |
202 | {PER_LINUX, "ppc64iseries", "ppc64"}, | |
203 | # else | |
e60b6df5 KZ |
204 | {PER_LINUX32, "ppc32", "ppcle"}, |
205 | {PER_LINUX32, "ppc", "ppcle"}, | |
d9ca5dd4 RM |
206 | {PER_LINUX32, "ppc32le", "ppcle"}, |
207 | {PER_LINUX32, "ppcle", "ppcle"}, | |
208 | {PER_LINUX, "ppc64le", "ppc64le"}, | |
77eb13b9 | 209 | # endif |
8b3a46d2 KZ |
210 | #endif |
211 | #if defined(__x86_64__) || defined(__i386__) || defined(__ia64__) | |
77eb13b9 SK |
212 | {PER_LINUX32, "i386", "i386"}, |
213 | {PER_LINUX32, "i486", "i386"}, | |
214 | {PER_LINUX32, "i586", "i386"}, | |
215 | {PER_LINUX32, "i686", "i386"}, | |
216 | {PER_LINUX32, "athlon", "i386"}, | |
8b3a46d2 KZ |
217 | #endif |
218 | #if defined(__x86_64__) || defined(__i386__) | |
77eb13b9 | 219 | {PER_LINUX, "x86_64", "x86_64"}, |
8b3a46d2 KZ |
220 | #endif |
221 | #if defined(__ia64__) || defined(__i386__) | |
77eb13b9 | 222 | {PER_LINUX, "ia64", "ia64"}, |
8b3a46d2 | 223 | #endif |
782e198d | 224 | #if defined(__hppa__) |
77eb13b9 SK |
225 | {PER_LINUX32, "parisc32", "parisc"}, |
226 | {PER_LINUX32, "parisc", "parisc"}, | |
227 | {PER_LINUX, "parisc64", "parisc64"}, | |
782e198d | 228 | #endif |
8b3a46d2 | 229 | #if defined(__s390x__) || defined(__s390__) |
77eb13b9 SK |
230 | {PER_LINUX32, "s390", "s390"}, |
231 | {PER_LINUX, "s390x", "s390x"}, | |
8b3a46d2 KZ |
232 | #endif |
233 | #if defined(__sparc64__) || defined(__sparc__) | |
77eb13b9 SK |
234 | {PER_LINUX32, "sparc", "sparc"}, |
235 | {PER_LINUX32, "sparc32bash", "sparc"}, | |
236 | {PER_LINUX32, "sparc32", "sparc"}, | |
237 | {PER_LINUX, "sparc64", "sparc64"}, | |
8b3a46d2 KZ |
238 | #endif |
239 | #if defined(__mips64__) || defined(__mips__) | |
77eb13b9 SK |
240 | {PER_LINUX32, "mips32", "mips"}, |
241 | {PER_LINUX32, "mips", "mips"}, | |
242 | {PER_LINUX, "mips64", "mips64"}, | |
9e19737a BC |
243 | #endif |
244 | #if defined(__alpha__) | |
77eb13b9 SK |
245 | {PER_LINUX, "alpha", "alpha"}, |
246 | {PER_LINUX, "alphaev5", "alpha"}, | |
247 | {PER_LINUX, "alphaev56", "alpha"}, | |
248 | {PER_LINUX, "alphaev6", "alpha"}, | |
249 | {PER_LINUX, "alphaev67", "alpha"}, | |
146900d4 | 250 | #endif |
f791eea8 EL |
251 | #if defined(__loongarch__) |
252 | {PER_LINUX, "loongarch", "loongarch64"}, | |
253 | {PER_LINUX, "loongarch64", "loongarch64"}, | |
254 | #endif | |
146900d4 AS |
255 | #if defined(__e2k__) |
256 | {PER_LINUX, "e2k", "e2k"}, | |
257 | {PER_LINUX, "e2kv4", "e2k"}, | |
258 | {PER_LINUX, "e2kv5", "e2k"}, | |
259 | {PER_LINUX, "e2kv6", "e2k"}, | |
260 | {PER_LINUX, "e2k4c", "e2k"}, | |
261 | {PER_LINUX, "e2k8c", "e2k"}, | |
262 | {PER_LINUX, "e2k1cp", "e2k"}, | |
263 | {PER_LINUX, "e2k8c2", "e2k"}, | |
264 | {PER_LINUX, "e2k12c", "e2k"}, | |
265 | {PER_LINUX, "e2k16c", "e2k"}, | |
266 | {PER_LINUX, "e2k2c3", "e2k"}, | |
9fd01b10 AG |
267 | #endif |
268 | #if defined(__arm__) || defined(__aarch64__) | |
269 | # ifdef __BIG_ENDIAN__ | |
270 | {PER_LINUX32, "armv7b", "arm"}, | |
271 | {PER_LINUX32, "armv8b", "arm"}, | |
272 | # else | |
273 | {PER_LINUX32, "armv7l", "arm"}, | |
274 | {PER_LINUX32, "armv8l", "arm"}, | |
275 | # endif | |
276 | {PER_LINUX32, "armh", "arm"}, | |
277 | {PER_LINUX32, "arm", "arm"}, | |
278 | {PER_LINUX, "arm64", "aarch64"}, | |
279 | {PER_LINUX, "aarch64", "aarch64"}, | |
a3c05171 MB |
280 | #endif |
281 | #if defined(__riscv) | |
282 | {PER_LINUX32, "riscv32", "riscv32"}, | |
283 | {PER_LINUX32, "rv32", "riscv32"}, | |
284 | {PER_LINUX, "riscv64", "riscv64"}, | |
285 | {PER_LINUX, "rv64", "riscv64"}, | |
8b3a46d2 | 286 | #endif |
77eb13b9 SK |
287 | /* place holder, will be filled up at runtime */ |
288 | {-1, NULL, NULL}, | |
289 | {-1, NULL, NULL} | |
290 | }; | |
291 | ||
292 | /* Add the trivial transition {PER_LINUX, machine, machine} if no | |
293 | * such target_arch is hardcoded yet. */ | |
294 | uname(&un); | |
295 | for (i = 0; transitions[i].perval >= 0; i++) | |
296 | if (!strcmp(un.machine, transitions[i].target_arch)) | |
297 | break; | |
298 | if (transitions[i].perval < 0) { | |
035fc2d9 TW |
299 | int wrdsz = sysfs_get_address_bits(NULL); |
300 | if (wrdsz < 0) | |
301 | wrdsz = CHAR_BIT * sizeof(void *); | |
77eb13b9 SK |
302 | if (wrdsz == 32 || wrdsz == 64) { |
303 | /* fill up the place holder */ | |
304 | transitions[i].perval = wrdsz == 32 ? PER_LINUX32 : PER_LINUX; | |
305 | transitions[i].target_arch = un.machine; | |
306 | transitions[i].result_arch = un.machine; | |
307 | } | |
ae2c3b5b | 308 | } |
1db8bf93 KZ |
309 | |
310 | return transitions; | |
311 | } | |
312 | ||
313 | /* | |
314 | * List all execution domains from transitions | |
315 | */ | |
316 | static void list_arch_domains(struct arch_domain *doms) | |
317 | { | |
318 | struct arch_domain *d; | |
319 | ||
320 | for (d = doms; d->target_arch != NULL; d++) | |
321 | printf("%s\n", d->target_arch); | |
322 | } | |
323 | ||
324 | static struct arch_domain *get_arch_domain(struct arch_domain *doms, const char *pers) | |
325 | { | |
326 | struct arch_domain *d; | |
327 | ||
cf5debba | 328 | for (d = doms; d && d->perval >= 0; d++) { |
1db8bf93 | 329 | if (!strcmp(pers, d->target_arch)) |
77eb13b9 | 330 | break; |
ae706576 | 331 | } |
1db8bf93 KZ |
332 | |
333 | return !d || d->perval < 0 ? NULL : d; | |
334 | } | |
335 | ||
7f48cfb0 | 336 | static void verify_arch_domain(struct arch_domain *doms, struct arch_domain *target, const char *wanted) |
1db8bf93 KZ |
337 | { |
338 | struct utsname un; | |
339 | ||
7f48cfb0 | 340 | if (!doms || !target || !target->result_arch) |
1db8bf93 KZ |
341 | return; |
342 | ||
77eb13b9 | 343 | uname(&un); |
7f48cfb0 AG |
344 | |
345 | if (!strcmp(un.machine, target->result_arch)) | |
346 | return; | |
347 | ||
9fd01b10 AG |
348 | if (!strcmp(target->result_arch, "i386") || |
349 | !strcmp(target->result_arch, "arm")) { | |
7f48cfb0 AG |
350 | struct arch_domain *dom; |
351 | for (dom = doms; dom->target_arch != NULL; dom++) { | |
352 | if (!dom->result_arch || strcmp(dom->result_arch, target->result_arch)) | |
353 | continue; | |
354 | if (!strcmp(dom->target_arch, un.machine)) | |
355 | return; | |
356 | } | |
77eb13b9 | 357 | } |
7f48cfb0 AG |
358 | |
359 | errx(EXIT_FAILURE, _("Kernel cannot set architecture to %s"), wanted); | |
8b3a46d2 KZ |
360 | } |
361 | ||
03a254f0 TW |
362 | static const struct { int value; const char * const name; } all_personalities[] = { |
363 | #define X(opt) { .value = opt, .name = #opt }, | |
364 | ALL_PERSONALITIES | |
365 | #undef X | |
366 | }; | |
367 | ||
368 | static const struct { int value; const char * const name; } all_options[] = { | |
369 | #define X(opt) { .value = opt, .name = #opt }, | |
370 | ALL_OPTIONS | |
371 | #undef X | |
372 | }; | |
373 | ||
374 | static void show_personality(int pers) | |
375 | { | |
376 | int options; | |
377 | size_t i; | |
378 | ||
379 | /* Test for exact matches including options */ | |
380 | for (i = 0; i < ARRAY_SIZE(all_personalities); i++) { | |
381 | if (pers == all_personalities[i].value) { | |
382 | printf("%s\n", all_personalities[i].name); | |
383 | return; | |
384 | } | |
385 | } | |
386 | ||
387 | options = pers & ~PER_MASK; | |
388 | pers &= PER_MASK; | |
389 | ||
390 | /* Second test for type-only matches */ | |
391 | for (i = 0; i < ARRAY_SIZE(all_personalities); i++) { | |
392 | if (pers == all_personalities[i].value) { | |
393 | printf("%s", all_personalities[i].name); | |
394 | break; | |
395 | } | |
396 | } | |
397 | ||
398 | if (i == ARRAY_SIZE(all_personalities)) | |
399 | printf("0x%02x", pers); | |
400 | ||
401 | if (options) { | |
402 | printf(" ("); | |
403 | ||
404 | for (i = 0; i < ARRAY_SIZE(all_options); i++) { | |
405 | if (options & all_options[i].value) { | |
406 | printf("%s", all_options[i].name); | |
407 | ||
408 | options &= ~all_options[i].value; | |
409 | if (options) | |
410 | printf(" "); | |
411 | } | |
412 | } | |
413 | if (options) | |
414 | printf("0x%08x", options); | |
415 | printf(")"); | |
416 | } | |
417 | printf("\n"); | |
418 | } | |
419 | ||
420 | static void show_current_personality(void) | |
421 | { | |
422 | int pers = personality(0xffffffff); | |
423 | if (pers == -1) | |
424 | err(EXIT_FAILURE, _("Can not get current kernel personality")); | |
425 | ||
426 | show_personality(pers); | |
427 | } | |
428 | ||
e6446e13 MY |
429 | static void show_process_personality(pid_t pid) |
430 | { | |
431 | int pers; | |
432 | char *pers_str; | |
433 | ||
434 | pers_str = pid_get_personality(pid); | |
435 | if (pers_str == NULL) | |
436 | err(EXIT_FAILURE, | |
437 | _("Can not get the personality for process(%d)"), | |
438 | pid); | |
439 | pers = str2num_or_err(pers_str, 16, | |
440 | _("could not parse personality"), | |
441 | 0, INT_MAX); | |
442 | free(pers_str); | |
443 | ||
444 | show_personality(pers); | |
445 | } | |
446 | ||
8b3a46d2 KZ |
447 | int main(int argc, char *argv[]) |
448 | { | |
5edb0ea6 | 449 | const char *arch = NULL; |
77eb13b9 SK |
450 | unsigned long options = 0; |
451 | int verbose = 0; | |
cbfa1442 | 452 | int archwrapper; |
77eb13b9 | 453 | int c; |
7f48cfb0 | 454 | struct arch_domain *doms = NULL, *target = NULL; |
1db8bf93 | 455 | unsigned long pers_value = 0; |
3fa06e04 | 456 | char *shell = NULL, *shell_arg = NULL; |
e6446e13 MY |
457 | int do_show = 0; |
458 | pid_t target_pid = 0; | |
77eb13b9 | 459 | |
5edb0ea6 KZ |
460 | /* Options without equivalent short options */ |
461 | enum { | |
462 | OPT_4GB = CHAR_MAX + 1, | |
463 | OPT_UNAME26, | |
03a254f0 TW |
464 | OPT_LIST, |
465 | OPT_SHOW, | |
5edb0ea6 KZ |
466 | }; |
467 | ||
9e930041 | 468 | /* Options --3gb and --4gb are for compatibility with an old |
77eb13b9 SK |
469 | * Debian setarch implementation. */ |
470 | static const struct option longopts[] = { | |
03a254f0 TW |
471 | {"help", no_argument, NULL, 'h'}, |
472 | {"version", no_argument, NULL, 'V'}, | |
473 | {"verbose", no_argument, NULL, 'v'}, | |
474 | {"addr-no-randomize", no_argument, NULL, 'R'}, | |
475 | {"fdpic-funcptrs", no_argument, NULL, 'F'}, | |
476 | {"mmap-page-zero", no_argument, NULL, 'Z'}, | |
477 | {"addr-compat-layout", no_argument, NULL, 'L'}, | |
478 | {"read-implies-exec", no_argument, NULL, 'X'}, | |
479 | {"32bit", no_argument, NULL, 'B'}, | |
480 | {"short-inode", no_argument, NULL, 'I'}, | |
481 | {"whole-seconds", no_argument, NULL, 'S'}, | |
482 | {"sticky-timeouts", no_argument, NULL, 'T'}, | |
483 | {"3gb", no_argument, NULL, '3'}, | |
484 | {"4gb", no_argument, NULL, OPT_4GB}, | |
485 | {"uname-2.6", no_argument, NULL, OPT_UNAME26}, | |
486 | {"list", no_argument, NULL, OPT_LIST}, | |
487 | {"show", optional_argument, NULL, OPT_SHOW}, | |
e6446e13 | 488 | {"pid", required_argument, NULL, 'p'}, |
03a254f0 | 489 | {NULL, 0, NULL, 0} |
77eb13b9 SK |
490 | }; |
491 | ||
492 | setlocale(LC_ALL, ""); | |
493 | bindtextdomain(PACKAGE, LOCALEDIR); | |
494 | textdomain(PACKAGE); | |
2c308875 | 495 | close_stdout_atexit(); |
77eb13b9 | 496 | |
7a87a774 RM |
497 | if (argc < 1) { |
498 | warnx(_("Not enough arguments")); | |
499 | errtryhelp(EXIT_FAILURE); | |
500 | } | |
5edb0ea6 | 501 | archwrapper = strcmp(program_invocation_short_name, "setarch") != 0; |
cbfa1442 | 502 | if (archwrapper) { |
5edb0ea6 | 503 | arch = program_invocation_short_name; /* symlinks to setarch */ |
3fa06e04 KZ |
504 | |
505 | /* Don't use ifdef sparc here, we get "Unrecognized architecture" | |
506 | * error message later if necessary */ | |
cbfa1442 | 507 | if (strcmp(arch, "sparc32bash") == 0) { |
3fa06e04 KZ |
508 | shell = "/bin/bash"; |
509 | shell_arg = ""; | |
510 | goto set_arch; | |
cbfa1442 | 511 | } |
cbfa1442 | 512 | } else { |
00b5ef70 | 513 | if (1 < argc && *argv[1] != '-') { |
5edb0ea6 KZ |
514 | arch = argv[1]; |
515 | argv[1] = argv[0]; /* for getopt_long() to get the program name */ | |
516 | argv++; | |
517 | argc--; | |
77eb13b9 SK |
518 | } |
519 | } | |
5edb0ea6 | 520 | |
e6446e13 | 521 | while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZp:", longopts, NULL)) != -1) { |
77eb13b9 | 522 | switch (c) { |
77eb13b9 SK |
523 | case 'v': |
524 | verbose = 1; | |
525 | break; | |
526 | case 'R': | |
527 | turn_on(ADDR_NO_RANDOMIZE, options); | |
528 | break; | |
529 | case 'F': | |
530 | turn_on(FDPIC_FUNCPTRS, options); | |
531 | break; | |
532 | case 'Z': | |
533 | turn_on(MMAP_PAGE_ZERO, options); | |
534 | break; | |
535 | case 'L': | |
536 | turn_on(ADDR_COMPAT_LAYOUT, options); | |
537 | break; | |
538 | case 'X': | |
539 | turn_on(READ_IMPLIES_EXEC, options); | |
540 | break; | |
541 | case 'B': | |
542 | turn_on(ADDR_LIMIT_32BIT, options); | |
543 | break; | |
544 | case 'I': | |
545 | turn_on(SHORT_INODE, options); | |
546 | break; | |
547 | case 'S': | |
548 | turn_on(WHOLE_SECONDS, options); | |
549 | break; | |
550 | case 'T': | |
551 | turn_on(STICKY_TIMEOUTS, options); | |
552 | break; | |
553 | case '3': | |
554 | turn_on(ADDR_LIMIT_3GB, options); | |
555 | break; | |
556 | case OPT_4GB: /* just ignore this one */ | |
557 | break; | |
558 | case OPT_UNAME26: | |
559 | turn_on(UNAME26, options); | |
560 | break; | |
5edb0ea6 KZ |
561 | case OPT_LIST: |
562 | if (!archwrapper) { | |
1db8bf93 | 563 | list_arch_domains(init_arch_domains()); |
b2b5b502 | 564 | return EXIT_SUCCESS; |
5edb0ea6 KZ |
565 | } else |
566 | warnx(_("unrecognized option '--list'")); | |
03a254f0 | 567 | goto error_getopts; |
e6446e13 MY |
568 | case 'p': |
569 | if (!archwrapper) { | |
c8dc60f9 | 570 | target_pid = strtopid_or_err(optarg, _("invalid PID argument")); |
e6446e13 MY |
571 | if (target_pid <= 0) |
572 | errx(EXIT_FAILURE, _("out of range value for pid specification: %d"), | |
573 | target_pid); | |
574 | break; | |
575 | } else | |
576 | warnx(_("unrecognized option '%s'"), argv[optind - 1]); | |
577 | goto error_getopts; | |
03a254f0 TW |
578 | case OPT_SHOW: |
579 | if (!archwrapper) { | |
e6446e13 MY |
580 | if (!optarg) { |
581 | do_show = 1; | |
582 | break; | |
583 | } else if (strcmp(optarg, "current") == 0) | |
03a254f0 TW |
584 | show_current_personality(); |
585 | else | |
586 | show_personality(str2num_or_err( | |
587 | optarg, 16, | |
588 | _("could not parse personality"), | |
589 | 0, INT_MAX)); | |
590 | return EXIT_SUCCESS; | |
591 | } else | |
592 | warnx(_("unrecognized option '--show'")); | |
593 | goto error_getopts; | |
2c308875 | 594 | |
03a254f0 | 595 | error_getopts: |
77eb13b9 | 596 | default: |
677ec86c | 597 | errtryhelp(EXIT_FAILURE); |
2c308875 KZ |
598 | case 'h': |
599 | usage(archwrapper); | |
600 | case 'V': | |
601 | print_version(EXIT_SUCCESS); | |
77eb13b9 SK |
602 | } |
603 | } | |
8b3a46d2 | 604 | |
e6446e13 MY |
605 | /* We could not find --pid option though we saw --show option. */ |
606 | if (do_show) { | |
607 | if (target_pid) | |
608 | show_process_personality(target_pid); | |
609 | else | |
610 | show_current_personality(); | |
611 | return EXIT_SUCCESS; | |
612 | } | |
613 | if (target_pid) | |
614 | errx(EXIT_FAILURE, _("use -p/--pid option with --show option")); | |
615 | ||
05541825 KZ |
616 | if (!arch && !options) |
617 | errx(EXIT_FAILURE, _("no architecture argument or personality flags specified")); | |
5edb0ea6 | 618 | |
77eb13b9 SK |
619 | argc -= optind; |
620 | argv += optind; | |
8df90dec | 621 | |
3fa06e04 | 622 | set_arch: |
05541825 KZ |
623 | /* get execution domain (architecture) */ |
624 | if (arch) { | |
625 | doms = init_arch_domains(); | |
626 | target = get_arch_domain(doms, arch); | |
1db8bf93 | 627 | |
05541825 KZ |
628 | if (!target) |
629 | errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), arch); | |
630 | pers_value = target->perval; | |
631 | } | |
632 | ||
633 | /* add personality flags */ | |
634 | pers_value |= options; | |
1db8bf93 | 635 | |
05541825 | 636 | /* call kernel */ |
1db8bf93 KZ |
637 | if (personality(pers_value) < 0) { |
638 | /* | |
639 | * Depending on architecture and kernel version, personality | |
640 | * syscall is either capable or incapable of returning an error. | |
641 | * If the return value is not an error, then it's the previous | |
642 | * personality value, which can be an arbitrary value | |
643 | * undistinguishable from an error value. | |
644 | * To make things clear, a second call is needed. | |
645 | */ | |
646 | if (personality(pers_value) < 0) | |
647 | err(EXIT_FAILURE, _("failed to set personality to %s"), arch); | |
648 | } | |
649 | ||
05541825 KZ |
650 | /* make sure architecture is set as expected */ |
651 | if (arch) | |
7f48cfb0 | 652 | verify_arch_domain(doms, target, arch); |
8b3a46d2 | 653 | |
3fa06e04 KZ |
654 | if (!argc) { |
655 | shell = "/bin/sh"; | |
656 | shell_arg = "-sh"; | |
657 | } | |
9508e297 | 658 | if (verbose) { |
3fa06e04 | 659 | printf(_("Execute command `%s'.\n"), shell ? shell : argv[0]); |
9508e297 RM |
660 | /* flush all output streams before exec */ |
661 | fflush(NULL); | |
662 | } | |
34c265f0 | 663 | |
3fa06e04 KZ |
664 | /* Execute shell */ |
665 | if (shell) { | |
1b10fa0e | 666 | execl(shell, shell_arg, (char *)NULL); |
3fa06e04 | 667 | errexec(shell); |
77eb13b9 | 668 | } |
8b3a46d2 | 669 | |
3fa06e04 | 670 | /* Execute on command line specified command */ |
77eb13b9 | 671 | execvp(argv[0], argv); |
fd777151 | 672 | errexec(argv[0]); |
8b3a46d2 | 673 | } |