blob: 9d9d70615dc3fbc74bb1af5a4fd7489abfa1c7e3 [file] [log] [blame]
landley13bab2f2006-09-27 00:45:05 -04001/* Toybox infrastructure.
2 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
landley13bab2f2006-09-27 00:45:05 -04004 */
5
landleyc5621502006-09-28 17:18:51 -04006#include "toys.h"
7
Rob Landleyf2311a42006-11-04 17:45:18 -05008// Populate toy_list[].
landleyc5621502006-09-28 17:18:51 -04009
Rob Landleya6d696b2007-01-31 13:31:19 -050010#undef NEWTOY
11#undef OLDTOY
Rob Landleyec0b4822016-06-30 20:41:07 -050012#define NEWTOY(name, opts, flags) {#name, name##_main, OPTSTR_##name, flags},
Rob Landleyf3e56f42014-12-31 21:30:59 -060013#define OLDTOY(name, oldname, flags) \
14 {#name, oldname##_main, OPTSTR_##oldname, flags},
Rob Landleya6d696b2007-01-31 13:31:19 -050015
landleyc5621502006-09-28 17:18:51 -040016struct toy_list toy_list[] = {
Rob Landley55928b12008-01-19 17:43:27 -060017#include "generated/newtoys.h"
landleyc5621502006-09-28 17:18:51 -040018};
19
Rob Landley933919c2013-04-21 12:15:59 -050020// global context for this command.
landleyc5621502006-09-28 17:18:51 -040021
22struct toy_context toys;
Rob Landleyb1aaba12008-01-20 17:25:44 -060023union global_union this;
Rob Landley7b0ea0a2020-10-28 17:03:37 -050024char *toybox_version = TOYBOX_VERSION, toybuf[4096], libbuf[4096];
landleyc5621502006-09-28 17:18:51 -040025
landley4f344e32006-10-05 16:18:03 -040026struct toy_list *toy_find(char *name)
landley13bab2f2006-09-27 00:45:05 -040027{
Rob Landley7aa651a2012-11-13 17:14:08 -060028 int top, bottom, middle;
landley13bab2f2006-09-27 00:45:05 -040029
Rob Landley7771e942018-07-02 23:56:34 -050030 if (!CFG_TOYBOX || strchr(name, '/')) return 0;
Rob Landleydc1af182014-09-27 19:58:18 -050031
Rob Landley28c97102020-02-06 11:01:32 -060032 // Multiplexer name works as prefix, else skip first entry (it's out of order)
Rob Landley0c6000a2021-06-09 19:00:46 -050033 if (!toys.which && strstart(&name, toy_list->name)) return toy_list;
Rob Landley7aa651a2012-11-13 17:14:08 -060034 bottom = 1;
landley13bab2f2006-09-27 00:45:05 -040035
Rob Landley933919c2013-04-21 12:15:59 -050036 // Binary search to find this command.
Rob Landley7aa651a2012-11-13 17:14:08 -060037 top = ARRAY_LEN(toy_list)-1;
38 for (;;) {
39 int result;
Rob Landley2c226852007-11-15 18:30:30 -060040
Rob Landley7aa651a2012-11-13 17:14:08 -060041 middle = (top+bottom)/2;
Rob Landley06897a82019-08-23 10:33:47 -050042 if (middle<bottom || middle>top) return 0;
Rob Landley7aa651a2012-11-13 17:14:08 -060043 result = strcmp(name,toy_list[middle].name);
44 if (!result) return toy_list+middle;
Joyounger77f9c772017-05-24 00:36:35 +080045 if (result<0) top = --middle;
Rob Landley7aa651a2012-11-13 17:14:08 -060046 else bottom = ++middle;
47 }
landley13bab2f2006-09-27 00:45:05 -040048}
49
Rob Landleyfc2224b2007-06-01 14:31:45 -040050// Figure out whether or not anything is using the option parsing logic,
51// because the compiler can't figure out whether or not to optimize it away
Rob Landley8f8c5042012-01-14 23:28:15 -060052// on its' own. NEED_OPTIONS becomes a constant allowing if() to optimize
53// stuff out via dead code elimination.
Rob Landleyfc2224b2007-06-01 14:31:45 -040054
55#undef NEWTOY
56#undef OLDTOY
57#define NEWTOY(name, opts, flags) opts ||
Rob Landleyf3e56f42014-12-31 21:30:59 -060058#define OLDTOY(name, oldname, flags) OPTSTR_##oldname ||
Rob Landley4307a7b2007-06-07 15:20:26 -040059static const int NEED_OPTIONS =
Rob Landley55928b12008-01-19 17:43:27 -060060#include "generated/newtoys.h"
Rob Landleyfc2224b2007-06-01 14:31:45 -0400610; // Ends the opts || opts || opts...
62
Rob Landleyfc0fbe62022-02-12 02:16:56 -060063// Populate help text array
64
65#undef NEWTOY
66#undef OLDTOY
67#define NEWTOY(name,opt,flags) HELP_##name "\0"
68#if CFG_TOYBOX
69#define OLDTOY(name,oldname,flags) "\xff" #oldname "\0"
70#else
71#define OLDTOY(name, oldname, flags) HELP_##oldname "\0"
72#endif
73
74#include "generated/help.h"
75static char *help_data =
76#include "generated/newtoys.h"
77;
78
79void show_help(FILE *out, int full)
80{
81 int i = toys.which-toy_list;
82 char *s, *ss;
83
84 if (!(full&2))
Rob Landley0fd6b762022-03-22 14:29:17 -050085 fprintf(out, "Toybox %s"USE_TOYBOX(" multicall binary")"%s\n\n",
86 toybox_version, (CFG_TOYBOX && i) ? " (see toybox --help)"
87 : " (see https://landley.net/toybox)");
Rob Landleyfc0fbe62022-02-12 02:16:56 -060088
89 if (CFG_TOYBOX_HELP) {
90 for (;;) {
91 s = help_data;
92 while (i--) s += strlen(s) + 1;
93 // If it's an alias, restart search for real name
94 if (*s != 255) break;
95 i = toy_find(++s)-toy_list;
96 }
97
98 if (full) fprintf(out, "%s\n", s);
99 else {
100 strstart(&s, "usage: ");
101 for (ss = s; *ss && *ss!='\n'; ss++);
102 fprintf(out, "%.*s\n", (int)(ss-s), s);
103 }
104 }
105}
106
Rob Landley29e75d52016-10-01 15:52:00 -0500107static void unknown(char *name)
108{
109 toys.exitval = 127;
Rob Landleyb2574792016-10-17 18:32:35 -0500110 toys.which = toy_list;
Rob Landleyefb80602020-08-07 02:25:50 -0500111 help_exit("Unknown command %s", name);
Rob Landley29e75d52016-10-01 15:52:00 -0500112}
113
Rob Landley80f82b22021-09-11 12:48:35 -0500114// Parse --help and --version for (almost) all commands
Rob Landley29e7ed92021-06-18 08:57:07 -0500115void check_help(char **arg)
116{
Rob Landley83d5d7a2022-03-31 16:08:56 -0500117 if (!CFG_TOYBOX_HELP_DASHDASH || !*arg) return;
118 if (!CFG_TOYBOX || toys.which != toy_list)
119 if (toys.which->flags&TOYFLAG_NOHELP) return;
Rob Landley80f82b22021-09-11 12:48:35 -0500120
Rob Landley29e7ed92021-06-18 08:57:07 -0500121 if (!strcmp(*arg, "--help")) {
122 if (CFG_TOYBOX && toys.which == toy_list && arg[1])
123 if (!(toys.which = toy_find(arg[1]))) unknown(arg[1]);
124 show_help(stdout, 1);
125 xexit();
126 }
127
128 if (!strcmp(*arg, "--version")) {
129 xprintf("toybox %s\n", toybox_version);
130 xexit();
131 }
132}
133
Rob Landley3b51a072015-09-27 09:03:41 -0500134// Setup toybox global state for this command.
Rob Landley114541b2020-03-11 22:09:15 -0500135void toy_singleinit(struct toy_list *which, char *argv[])
Rob Landleybb504f32013-07-19 02:03:02 -0500136{
137 toys.which = which;
138 toys.argv = argv;
Rob Landley488f8502019-12-22 20:57:08 -0600139 toys.toycount = ARRAY_LEN(toy_list);
Rob Landleyfc497612014-06-21 13:03:42 -0500140
Rob Landleybb504f32013-07-19 02:03:02 -0500141 if (NEED_OPTIONS && which->options) get_optflags();
142 else {
Rob Landley80f82b22021-09-11 12:48:35 -0500143 check_help(toys.optargs = argv+1);
Rob Landley3b51a072015-09-27 09:03:41 -0500144 for (toys.optc = 0; toys.optargs[toys.optc]; toys.optc++);
Rob Landleybb504f32013-07-19 02:03:02 -0500145 }
Rob Landley488f8502019-12-22 20:57:08 -0600146
Rob Landleya28ad6d2021-04-27 02:40:54 -0500147 if (!(CFG_TOYBOX && which == toy_list) && !(which->flags & TOYFLAG_NOFORK)) {
Rob Landley488f8502019-12-22 20:57:08 -0600148 toys.old_umask = umask(0);
149 if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
Rob Landley75b89012020-12-06 00:02:46 -0600150
Elliott Hughes4786fd62021-01-05 14:14:39 -0800151 // Try user's locale, but merge in the en_US.UTF-8 locale's character
152 // type data if the user's locale isn't UTF-8. (We can't merge in C.UTF-8
153 // because that locale doesn't exist on macOS.)
Rob Landley75b89012020-12-06 00:02:46 -0600154 setlocale(LC_CTYPE, "");
Elliott Hughesbec20282020-12-14 17:32:04 -0800155 if (strcmp("UTF-8", nl_langinfo(CODESET)))
Elliott Hughes4786fd62021-01-05 14:14:39 -0800156 uselocale(newlocale(LC_CTYPE_MASK, "en_US.UTF-8", NULL));
157
Rob Landley4e47b8e2020-12-11 19:50:36 -0600158 setvbuf(stdout, 0, (which->flags & TOYFLAG_LINEBUF) ? _IOLBF : _IONBF, 0);
Rob Landley488f8502019-12-22 20:57:08 -0600159 }
Rob Landleybb504f32013-07-19 02:03:02 -0500160}
161
Rob Landley3b51a072015-09-27 09:03:41 -0500162// Full init needed by multiplexer or reentrant calls, calls singleinit at end
landleycd9dfc32006-10-18 18:38:16 -0400163void toy_init(struct toy_list *which, char *argv[])
164{
Rob Landleyca311f12016-01-30 16:28:13 -0600165 void *oldwhich = toys.which;
166
Rob Landley7aa651a2012-11-13 17:14:08 -0600167 // Drop permissions for non-suid commands.
Rob Landley7aa651a2012-11-13 17:14:08 -0600168 if (CFG_TOYBOX_SUID) {
Rob Landleyca311f12016-01-30 16:28:13 -0600169 if (!toys.which) toys.which = toy_list;
170
Rob Landley7aa651a2012-11-13 17:14:08 -0600171 uid_t uid = getuid(), euid = geteuid();
Rob Landleye0377fb2010-01-05 12:17:05 -0600172
Rob Landley7aa651a2012-11-13 17:14:08 -0600173 if (!(which->flags & TOYFLAG_STAYROOT)) {
Rob Landley5c87c142014-08-31 11:58:39 -0500174 if (uid != euid) {
Patrick Ohly62b53ed2016-02-09 16:43:35 +0100175 if (setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
Rob Landleyca311f12016-01-30 16:28:13 -0600176 euid = uid;
177 toys.wasroot++;
Rob Landley5c87c142014-08-31 11:58:39 -0500178 }
Rob Landleybf1e70f2012-12-27 17:09:17 -0600179 } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
180 error_msg("Not installed suid root");
Rob Landleye0377fb2010-01-05 12:17:05 -0600181
Rob Landley80f82b22021-09-11 12:48:35 -0500182 if ((which->flags & TOYFLAG_NEEDROOT) && euid) {
Rob Landleyd040b012022-04-01 14:19:28 -0500183 toys.which = which;
Rob Landley80f82b22021-09-11 12:48:35 -0500184 check_help(argv+1);
185 help_exit("Not root");
186 }
Rob Landley7aa651a2012-11-13 17:14:08 -0600187 }
Rob Landleye0377fb2010-01-05 12:17:05 -0600188
Rob Landleycaf39c22012-11-16 00:35:46 -0600189 // Free old toys contents (to be reentrant), but leave rebound if any
Rob Landley3b51a072015-09-27 09:03:41 -0500190 // don't blank old optargs if our new argc lives in the old optargs.
191 if (argv<toys.optargs || argv>toys.optargs+toys.optc) free(toys.optargs);
Rob Landleycaf39c22012-11-16 00:35:46 -0600192 memset(&toys, 0, offsetof(struct toy_context, rebound));
Rob Landleyca311f12016-01-30 16:28:13 -0600193 if (oldwhich) memset(&this, 0, sizeof(this));
landleycd9dfc32006-10-18 18:38:16 -0400194
Rob Landley3b51a072015-09-27 09:03:41 -0500195 // Continue to portion of init needed by standalone commands
Rob Landleybb504f32013-07-19 02:03:02 -0500196 toy_singleinit(which, argv);
landleycd9dfc32006-10-18 18:38:16 -0400197}
198
Rob Landley7771e942018-07-02 23:56:34 -0500199// Run an internal toybox command.
200// Only returns if it can't run command internally, otherwise xexit() when done.
Rob Landley664c4172021-02-07 17:19:44 -0600201static void toy_exec_which(struct toy_list *which, char *argv[])
landley4f344e32006-10-05 16:18:03 -0400202{
Rob Landley3b51a072015-09-27 09:03:41 -0500203 // Return if we can't find it (which includes no multiplexer case),
Rob Landleyd5680f02020-04-07 16:54:42 -0500204 if (!which || (which->flags&TOYFLAG_NOFORK)) return;
Rob Landleyc6705af2014-09-09 23:42:25 -0500205
Rob Landley3b51a072015-09-27 09:03:41 -0500206 // Return if stack depth getting noticeable (proxy for leaked heap, etc).
Rob Landley869da8c2016-05-07 00:21:34 -0500207
208 // Compiler writers have decided subtracting char * is undefined behavior,
209 // so convert to integers. (LP64 says sizeof(long)==sizeof(pointer).)
Rob Landley3f988702018-09-21 12:54:56 -0500210 // Signed typecast so stack growth direction is irrelevant: we're measuring
211 // the distance between two pointers on the same stack, hence the labs().
Rob Landley19f7ad42018-09-16 14:17:09 -0500212 if (!CFG_TOYBOX_NORECURSE && toys.stacktop)
Rob Landley3f988702018-09-21 12:54:56 -0500213 if (labs((long)toys.stacktop-(long)&which)>6000) return;
Rob Landley3b51a072015-09-27 09:03:41 -0500214
215 // Return if we need to re-exec to acquire root via suid bit.
Rob Landleyca311f12016-01-30 16:28:13 -0600216 if (toys.which && (which->flags&TOYFLAG_ROOTONLY) && toys.wasroot) return;
Rob Landley7c5ed1c2015-02-15 15:27:43 -0600217
Rob Landleyc6705af2014-09-09 23:42:25 -0500218 // Run command
Rob Landley7aa651a2012-11-13 17:14:08 -0600219 toy_init(which, argv);
Rob Landley5f805332013-08-21 03:03:47 -0500220 if (toys.which) toys.which->toy_main();
Rob Landley953722e2013-06-30 15:58:24 -0500221 xexit();
landley4f344e32006-10-05 16:18:03 -0400222}
223
Rob Landley7771e942018-07-02 23:56:34 -0500224// Lookup internal toybox command to run via argv[0]
225void toy_exec(char *argv[])
226{
Rob Landleyf224ac52020-06-08 00:37:06 -0500227 toy_exec_which(toy_find(*argv), argv);
Rob Landley7771e942018-07-02 23:56:34 -0500228}
229
Rob Landley8f8c5042012-01-14 23:28:15 -0600230// Multiplexer command, first argument is command to run, rest are args to that.
231// If first argument starts with - output list of command install paths.
Rob Landleyefda21c2007-11-29 18:14:37 -0600232void toybox_main(void)
landley4f344e32006-10-05 16:18:03 -0400233{
Rob Landleyf224ac52020-06-08 00:37:06 -0500234 char *toy_paths[] = {"usr/", "bin/", "sbin/", 0}, *s = toys.argv[1];
Rob Landley7aa651a2012-11-13 17:14:08 -0600235 int i, len = 0;
Elliott Hughes1b276a82020-08-13 09:19:11 -0700236 unsigned width = 80;
landley4f344e32006-10-05 16:18:03 -0400237
Rob Landley3b51a072015-09-27 09:03:41 -0500238 // fast path: try to exec immediately.
239 // (Leave toys.which null to disable suid return logic.)
Rob Landleyd040b012022-04-01 14:19:28 -0500240 // Try dereferencing symlinks until we hit a recognized name
Rob Landleyf224ac52020-06-08 00:37:06 -0500241 while (s) {
Rob Landleyd040b012022-04-01 14:19:28 -0500242 char *ss = basename(s);
243 struct toy_list *tl = toy_find(ss);
Rob Landley68757a52019-08-23 10:32:38 -0500244
Rob Landleyd040b012022-04-01 14:19:28 -0500245 if (tl==toy_list && s!=toys.argv[1]) unknown(ss);
246 toy_exec_which(tl, toys.argv+1);
Rob Landleyf224ac52020-06-08 00:37:06 -0500247 s = (0<readlink(s, libbuf, sizeof(libbuf))) ? libbuf : 0;
Rob Landley7771e942018-07-02 23:56:34 -0500248 }
Rob Landley3b51a072015-09-27 09:03:41 -0500249
250 // For early error reporting
Rob Landley6c624482012-11-18 18:52:19 -0600251 toys.which = toy_list;
Rob Landley3b51a072015-09-27 09:03:41 -0500252
Rob Landleyefb80602020-08-07 02:25:50 -0500253 if (toys.argv[1] && strcmp(toys.argv[1], "--long")) unknown(toys.argv[1]);
landley4f344e32006-10-05 16:18:03 -0400254
Elliott Hughes1b276a82020-08-13 09:19:11 -0700255 // Output list of commands.
256 terminal_size(&width, 0);
Rob Landley28c97102020-02-06 11:01:32 -0600257 for (i = 1; i<ARRAY_LEN(toy_list); i++) {
Rob Landley7aa651a2012-11-13 17:14:08 -0600258 int fl = toy_list[i].flags;
259 if (fl & TOYMASK_LOCATION) {
260 if (toys.argv[1]) {
261 int j;
Rob Landley28c97102020-02-06 11:01:32 -0600262 for (j = 0; toy_paths[j]; j++)
Rob Landley7aa651a2012-11-13 17:14:08 -0600263 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
264 }
Rob Landleyd3f335d2014-10-26 12:56:41 -0500265 len += printf("%s",toy_list[i].name);
Elliott Hughes1b276a82020-08-13 09:19:11 -0700266 if (++len > width-15) len = 0;
Rob Landleyd3f335d2014-10-26 12:56:41 -0500267 xputc(len ? ' ' : '\n');
Rob Landley7aa651a2012-11-13 17:14:08 -0600268 }
269 }
270 xputc('\n');
landley4f344e32006-10-05 16:18:03 -0400271}
272
landley13bab2f2006-09-27 00:45:05 -0400273int main(int argc, char *argv[])
274{
Rob Landley72744582020-02-05 19:11:54 -0600275 // don't segfault if our environment is crazy
Rob Landley3b51a072015-09-27 09:03:41 -0500276 if (!*argv) return 127;
Rob Landley67476b12020-12-11 20:10:14 -0600277
Rob Landley3b51a072015-09-27 09:03:41 -0500278 // Snapshot stack location so we can detect recursion depth later.
Rob Landley72744582020-02-05 19:11:54 -0600279 // Nommu has special reentry path, !stacktop = "vfork/exec self happened"
280 if (!CFG_TOYBOX_FORK && (0x80 & **argv)) **argv &= 0x7f;
Rob Landley3b51a072015-09-27 09:03:41 -0500281 else {
Rob Landley72744582020-02-05 19:11:54 -0600282 int stack_start; // here so probe var won't permanently eat stack
Rob Landley3b51a072015-09-27 09:03:41 -0500283
Rob Landley72744582020-02-05 19:11:54 -0600284 toys.stacktop = &stack_start;
Rob Landley3b51a072015-09-27 09:03:41 -0500285 }
Rob Landley3b51a072015-09-27 09:03:41 -0500286
Rob Landley72744582020-02-05 19:11:54 -0600287 // Android before O had non-default SIGPIPE, 7 years = remove in Sep 2024.
Rob Landleye95731e2017-02-10 16:37:42 -0600288 if (CFG_TOYBOX_ON_ANDROID) signal(SIGPIPE, SIG_DFL);
289
Rob Landleyd04dc1f2013-08-30 01:53:31 -0500290 if (CFG_TOYBOX) {
Rob Landley72744582020-02-05 19:11:54 -0600291 // Call the multiplexer with argv[] as its arguments so it can toy_find()
Rob Landleybb504f32013-07-19 02:03:02 -0500292 toys.argv = argv-1;
293 toybox_main();
294 } else {
Rob Landley72744582020-02-05 19:11:54 -0600295 // single command built standalone with no multiplexer is first list entry
Rob Landleybb504f32013-07-19 02:03:02 -0500296 toy_singleinit(toy_list, argv);
297 toy_list->toy_main();
Rob Landleybb504f32013-07-19 02:03:02 -0500298 }
299
Rob Landleyaad492f2015-01-03 16:25:36 -0600300 xexit();
landley13bab2f2006-09-27 00:45:05 -0400301}