Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 7fe21c6 | 2008-08-23 23:15:17 +0000 | [diff] [blame] | 3 | * linux32/linux64 allows for changing uname emulation. |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2002 Andi Kleen, SuSE Labs. |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 8 | */ |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 9 | //config:config SETARCH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "setarch (3.4 kb)" |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: select PLATFORM_LINUX |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame^] | 14 | //config: The linux32 utility is used to create a 32bit environment for the |
| 15 | //config: specified program (usually a shell). It only makes sense to have |
| 16 | //config: this util on a system that supports both 64bit and 32bit userland |
| 17 | //config: (like amd64/x86, ppc64/ppc, sparc64/sparc, etc...). |
Denys Vlasenko | c314ca9 | 2016-11-13 22:56:04 +0100 | [diff] [blame] | 18 | //config: |
| 19 | //config:config LINUX32 |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 20 | //config: bool "linux32 (3.2 kb)" |
Denys Vlasenko | c314ca9 | 2016-11-13 22:56:04 +0100 | [diff] [blame] | 21 | //config: default y |
| 22 | //config: select PLATFORM_LINUX |
| 23 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame^] | 24 | //config: Alias to "setarch linux32". |
Denys Vlasenko | c314ca9 | 2016-11-13 22:56:04 +0100 | [diff] [blame] | 25 | //config: |
| 26 | //config:config LINUX64 |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 27 | //config: bool "linux64 (3.2 kb)" |
Denys Vlasenko | c314ca9 | 2016-11-13 22:56:04 +0100 | [diff] [blame] | 28 | //config: default y |
| 29 | //config: select PLATFORM_LINUX |
| 30 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame^] | 31 | //config: Alias to "setarch linux64". |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 32 | |
| 33 | //applet:IF_SETARCH(APPLET(setarch, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 34 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | c314ca9 | 2016-11-13 22:56:04 +0100 | [diff] [blame] | 35 | //applet:IF_LINUX32(APPLET_ODDNAME(linux32, setarch, BB_DIR_BIN, BB_SUID_DROP, linux32)) |
| 36 | //applet:IF_LINUX64(APPLET_ODDNAME(linux64, setarch, BB_DIR_BIN, BB_SUID_DROP, linux64)) |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 37 | |
| 38 | //kbuild:lib-$(CONFIG_SETARCH) += setarch.o |
Denys Vlasenko | c314ca9 | 2016-11-13 22:56:04 +0100 | [diff] [blame] | 39 | //kbuild:lib-$(CONFIG_LINUX32) += setarch.o |
| 40 | //kbuild:lib-$(CONFIG_LINUX64) += setarch.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 41 | |
| 42 | //usage:#define setarch_trivial_usage |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 43 | //usage: "PERSONALITY [-R] PROG ARGS" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 44 | //usage:#define setarch_full_usage "\n\n" |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 45 | //usage: "PERSONALITY may be:" |
| 46 | //usage: "\n"" linux32 Set 32bit uname emulation" |
| 47 | //usage: "\n"" linux64 Set 64bit uname emulation" |
| 48 | //usage: "\n" |
| 49 | //usage: "\n"" -R Disable address space randomization" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 50 | //usage: |
| 51 | //usage:#define linux32_trivial_usage NOUSAGE_STR |
| 52 | //usage:#define linux32_full_usage "" |
| 53 | //usage: |
| 54 | //usage:#define linux64_trivial_usage NOUSAGE_STR |
| 55 | //usage:#define linux64_full_usage "" |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 56 | |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 57 | #include "libbb.h" |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 58 | #include <sys/personality.h> |
| 59 | |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 60 | #ifndef ADDR_NO_RANDOMIZE |
| 61 | # define ADDR_NO_RANDOMIZE 0x0040000 |
| 62 | #endif |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 64 | int setarch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 65 | int setarch_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 66 | { |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 67 | unsigned opts; |
| 68 | unsigned long pers; |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 69 | |
| 70 | /* Figure out what personality we are supposed to switch to ... |
| 71 | * we can be invoked as either: |
Denis Vlasenko | 7fe21c6 | 2008-08-23 23:15:17 +0000 | [diff] [blame] | 72 | * argv[0],argv[1] == "setarch","personality" |
| 73 | * argv[0] == "personality" |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 74 | */ |
Denis Vlasenko | 7fe21c6 | 2008-08-23 23:15:17 +0000 | [diff] [blame] | 75 | if (ENABLE_SETARCH && applet_name[0] == 's' |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 76 | && argv[1] && is_prefixed_with(argv[1], "linux") |
Denis Vlasenko | 7fe21c6 | 2008-08-23 23:15:17 +0000 | [diff] [blame] | 77 | ) { |
Denis Vlasenko | 7fe21c6 | 2008-08-23 23:15:17 +0000 | [diff] [blame] | 78 | argv++; |
Denys Vlasenko | c314ca9 | 2016-11-13 22:56:04 +0100 | [diff] [blame] | 79 | applet_name = argv[0]; |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 80 | } |
Denys Vlasenko | c314ca9 | 2016-11-13 22:56:04 +0100 | [diff] [blame] | 81 | if ((!ENABLE_SETARCH && !ENABLE_LINUX32) || applet_name[5] == '6') |
| 82 | /* linux64 */ |
Denis Vlasenko | 7fe21c6 | 2008-08-23 23:15:17 +0000 | [diff] [blame] | 83 | pers = PER_LINUX; |
Denys Vlasenko | c314ca9 | 2016-11-13 22:56:04 +0100 | [diff] [blame] | 84 | else |
| 85 | if ((!ENABLE_SETARCH && !ENABLE_LINUX64) || applet_name[5] == '3') |
| 86 | /* linux32 */ |
Denis Vlasenko | 7fe21c6 | 2008-08-23 23:15:17 +0000 | [diff] [blame] | 87 | pers = PER_LINUX32; |
| 88 | else |
| 89 | bb_show_usage(); |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 90 | |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 91 | opts = getopt32(argv, "+R"); /* '+': stop at first non-option */ |
| 92 | if (opts) |
| 93 | pers |= ADDR_NO_RANDOMIZE; |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 94 | |
| 95 | /* Try to set personality */ |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 96 | if (personality(pers) < 0) |
| 97 | bb_perror_msg_and_die("personality(0x%lx)", pers); |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 98 | |
Denys Vlasenko | f2ccefb | 2015-10-19 23:26:50 +0200 | [diff] [blame] | 99 | argv += optind; |
| 100 | if (!argv[0]) |
| 101 | (--argv)[0] = (char*)"/bin/sh"; |
| 102 | |
| 103 | /* Try to execute the program */ |
| 104 | BB_EXECVP_or_die(argv); |
Mike Frysinger | 0a6b0bf | 2006-02-21 04:26:52 +0000 | [diff] [blame] | 105 | } |