blob: e4124b044f9a9009dd93579bd7436609cb102f59 [file] [log] [blame]
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko7fe21c62008-08-23 23:15:17 +00003 * linux32/linux64 allows for changing uname emulation.
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +00004 *
5 * Copyright 2002 Andi Kleen, SuSE Labs.
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Pere Orga5bc8c002011-04-11 03:29:49 +02008 */
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +02009//config:config SETARCH
10//config: bool "setarch"
11//config: default y
12//config: select PLATFORM_LINUX
13//config: help
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 Vlasenkoc314ca92016-11-13 22:56:04 +010018//config:
19//config:config LINUX32
20//config: bool "linux32"
21//config: default y
22//config: select PLATFORM_LINUX
23//config: help
24//config: Alias to "setarch linux32".
25//config:
26//config:config LINUX64
27//config: bool "linux64"
28//config: default y
29//config: select PLATFORM_LINUX
30//config: help
31//config: Alias to "setarch linux64".
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020032
33//applet:IF_SETARCH(APPLET(setarch, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko205d48e2017-01-29 14:57:33 +010034// APPLET_ODDNAME:name main location suid_type help
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010035//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 Vlasenkof2ccefb2015-10-19 23:26:50 +020037
38//kbuild:lib-$(CONFIG_SETARCH) += setarch.o
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010039//kbuild:lib-$(CONFIG_LINUX32) += setarch.o
40//kbuild:lib-$(CONFIG_LINUX64) += setarch.o
Pere Orga5bc8c002011-04-11 03:29:49 +020041
42//usage:#define setarch_trivial_usage
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020043//usage: "PERSONALITY [-R] PROG ARGS"
Pere Orga5bc8c002011-04-11 03:29:49 +020044//usage:#define setarch_full_usage "\n\n"
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020045//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 Orga5bc8c002011-04-11 03:29:49 +020050//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 Frysinger0a6b0bf2006-02-21 04:26:52 +000056
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020057#include "libbb.h"
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000058#include <sys/personality.h>
59
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020060#ifndef ADDR_NO_RANDOMIZE
61# define ADDR_NO_RANDOMIZE 0x0040000
62#endif
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000063
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000064int setarch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000065int setarch_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000066{
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020067 unsigned opts;
68 unsigned long pers;
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000069
70 /* Figure out what personality we are supposed to switch to ...
71 * we can be invoked as either:
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000072 * argv[0],argv[1] == "setarch","personality"
73 * argv[0] == "personality"
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000074 */
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000075 if (ENABLE_SETARCH && applet_name[0] == 's'
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020076 && argv[1] && is_prefixed_with(argv[1], "linux")
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000077 ) {
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000078 argv++;
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010079 applet_name = argv[0];
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000080 }
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010081 if ((!ENABLE_SETARCH && !ENABLE_LINUX32) || applet_name[5] == '6')
82 /* linux64 */
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000083 pers = PER_LINUX;
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010084 else
85 if ((!ENABLE_SETARCH && !ENABLE_LINUX64) || applet_name[5] == '3')
86 /* linux32 */
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000087 pers = PER_LINUX32;
88 else
89 bb_show_usage();
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000090
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020091 opts = getopt32(argv, "+R"); /* '+': stop at first non-option */
92 if (opts)
93 pers |= ADDR_NO_RANDOMIZE;
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000094
95 /* Try to set personality */
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020096 if (personality(pers) < 0)
97 bb_perror_msg_and_die("personality(0x%lx)", pers);
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000098
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020099 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 Frysinger0a6b0bf2006-02-21 04:26:52 +0000105}