1 /* Toybox infrastructure.
2 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
4 */
5
6 #include "toys.h"
7
8 // Populate toy_list[].
9
10 #undef NEWTOY
11 #undef OLDTOY
12 #define NEWTOY(name, opts, flags) {#name, name##_main, OPTSTR_##name, flags},
13 #define OLDTOY(name, oldname, flags) \
14 {#name, oldname##_main, OPTSTR_##oldname, flags},
15
16 struct toy_list toy_list[] = {
17 #include "generated/newtoys.h"
18 };
19
20 // global context for this command.
21
22 struct toy_context toys;
23 union global_union this;
24 char toybuf[4096], libbuf[4096];
25
toy_find(char * name)26 struct toy_list *toy_find(char *name)
27 {
28 int top, bottom, middle;
29
30 if (!CFG_TOYBOX || strchr(name, '/')) return 0;
31
32 // If the name starts with "toybox" accept that as a match. Otherwise
33 // skip the first entry, which is out of order.
34
35 if (!strncmp(name, "toybox", 6)) return toy_list;
36 bottom = 1;
37
38 // Binary search to find this command.
39
40 top = ARRAY_LEN(toy_list)-1;
41 for (;;) {
42 int result;
43
44 middle = (top+bottom)/2;
45 if (middle<bottom || middle>top) return 0;
46 result = strcmp(name,toy_list[middle].name);
47 if (!result) return toy_list+middle;
48 if (result<0) top = --middle;
49 else bottom = ++middle;
50 }
51 }
52
53 // Figure out whether or not anything is using the option parsing logic,
54 // because the compiler can't figure out whether or not to optimize it away
55 // on its' own. NEED_OPTIONS becomes a constant allowing if() to optimize
56 // stuff out via dead code elimination.
57
58 #undef NEWTOY
59 #undef OLDTOY
60 #define NEWTOY(name, opts, flags) opts ||
61 #define OLDTOY(name, oldname, flags) OPTSTR_##oldname ||
62 static const int NEED_OPTIONS =
63 #include "generated/newtoys.h"
64 0; // Ends the opts || opts || opts...
65
unknown(char * name)66 static void unknown(char *name)
67 {
68 toys.exitval = 127;
69 toys.which = toy_list;
70 error_exit("Unknown command %s", name);
71 }
72
73 // Setup toybox global state for this command.
toy_singleinit(struct toy_list * which,char * argv[])74 static void toy_singleinit(struct toy_list *which, char *argv[])
75 {
76 toys.which = which;
77 toys.argv = argv;
78
79 if (CFG_TOYBOX_I18N) setlocale(LC_CTYPE, "C.UTF-8");
80 setlinebuf(stdout);
81
82 // Parse --help and --version for (almost) all commands
83 if (CFG_TOYBOX_HELP_DASHDASH && !(which->flags & TOYFLAG_NOHELP) && argv[1]) {
84 if (!strcmp(argv[1], "--help")) {
85 if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2])
86 if (!(toys.which = toy_find(toys.argv[2]))) unknown(toys.argv[2]);
87 show_help(stdout);
88 xexit();
89 }
90
91 if (!strcmp(argv[1], "--version")) {
92 xputs("toybox "TOYBOX_VERSION);
93 xexit();
94 }
95 }
96
97 if (NEED_OPTIONS && which->options) get_optflags();
98 else {
99 toys.optargs = argv+1;
100 for (toys.optc = 0; toys.optargs[toys.optc]; toys.optc++);
101 }
102 toys.old_umask = umask(0);
103 if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
104 toys.signalfd--;
105 toys.toycount = ARRAY_LEN(toy_list);
106 }
107
108 // Full init needed by multiplexer or reentrant calls, calls singleinit at end
toy_init(struct toy_list * which,char * argv[])109 void toy_init(struct toy_list *which, char *argv[])
110 {
111 void *oldwhich = toys.which;
112
113 // Drop permissions for non-suid commands.
114
115 if (CFG_TOYBOX_SUID) {
116 if (!toys.which) toys.which = toy_list;
117
118 uid_t uid = getuid(), euid = geteuid();
119
120 if (!(which->flags & TOYFLAG_STAYROOT)) {
121 if (uid != euid) {
122 if (setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
123 euid = uid;
124 toys.wasroot++;
125 }
126 } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
127 error_msg("Not installed suid root");
128
129 if ((which->flags & TOYFLAG_NEEDROOT) && euid) help_exit("Not root");
130 }
131
132 // Free old toys contents (to be reentrant), but leave rebound if any
133 // don't blank old optargs if our new argc lives in the old optargs.
134 if (argv<toys.optargs || argv>toys.optargs+toys.optc) free(toys.optargs);
135 memset(&toys, 0, offsetof(struct toy_context, rebound));
136 if (oldwhich) memset(&this, 0, sizeof(this));
137
138 // Continue to portion of init needed by standalone commands
139 toy_singleinit(which, argv);
140 }
141
142 // Run an internal toybox command.
143 // Only returns if it can't run command internally, otherwise xexit() when done.
toy_exec_which(struct toy_list * which,char * argv[])144 void toy_exec_which(struct toy_list *which, char *argv[])
145 {
146 // Return if we can't find it (which includes no multiplexer case),
147 if (!which) return;
148
149 // Return if stack depth getting noticeable (proxy for leaked heap, etc).
150
151 // Compiler writers have decided subtracting char * is undefined behavior,
152 // so convert to integers. (LP64 says sizeof(long)==sizeof(pointer).)
153 // Signed typecast so stack growth direction is irrelevant: we're measuring
154 // the distance between two pointers on the same stack, hence the labs().
155 if (!CFG_TOYBOX_NORECURSE && toys.stacktop)
156 if (labs((long)toys.stacktop-(long)&which)>6000) return;
157
158 // Return if we need to re-exec to acquire root via suid bit.
159 if (toys.which && (which->flags&TOYFLAG_ROOTONLY) && toys.wasroot) return;
160
161 // Run command
162 toy_init(which, argv);
163 if (toys.which) toys.which->toy_main();
164 xexit();
165 }
166
167 // Lookup internal toybox command to run via argv[0]
toy_exec(char * argv[])168 void toy_exec(char *argv[])
169 {
170 toy_exec_which(toy_find(basename(*argv)), argv);
171 }
172
173 // Multiplexer command, first argument is command to run, rest are args to that.
174 // If first argument starts with - output list of command install paths.
toybox_main(void)175 void toybox_main(void)
176 {
177 static char *toy_paths[]={"usr/","bin/","sbin/",0};
178 int i, len = 0;
179
180 // fast path: try to exec immediately.
181 // (Leave toys.which null to disable suid return logic.)
182 // Try dereferencing one layer of symlink
183 if (toys.argv[1]) {
184 toy_exec(toys.argv+1);
185 if (0<readlink(toys.argv[1], libbuf, sizeof(libbuf))) {
186 struct toy_list *tl= toy_find(basename(libbuf));
187
188 if (tl == toy_list) unknown(basename(toys.argv[1]));
189 else toy_exec_which(tl, toys.argv+1);
190 }
191 }
192
193 // For early error reporting
194 toys.which = toy_list;
195
196 if (toys.argv[1] && toys.argv[1][0] != '-') unknown(toys.argv[1]);
197
198 // Output list of command.
199 for (i=1; i<ARRAY_LEN(toy_list); i++) {
200 int fl = toy_list[i].flags;
201 if (fl & TOYMASK_LOCATION) {
202 if (toys.argv[1]) {
203 int j;
204 for (j=0; toy_paths[j]; j++)
205 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
206 }
207 len += printf("%s",toy_list[i].name);
208 if (++len > 65) len = 0;
209 xputc(len ? ' ' : '\n');
210 }
211 }
212 xputc('\n');
213 }
214
main(int argc,char * argv[])215 int main(int argc, char *argv[])
216 {
217 if (!*argv) return 127;
218
219 // Snapshot stack location so we can detect recursion depth later.
220 // This is its own block so probe doesn't permanently consume stack.
221 else {
222 int stack;
223
224 toys.stacktop = &stack;
225 }
226
227 // Up to and including Android M, bionic's dynamic linker added a handler to
228 // cause a crash dump on SIGPIPE. That was removed in Android N, but adbd
229 // was still setting the SIGPIPE disposition to SIG_IGN, and its children
230 // were inheriting that. In Android O, adbd is fixed, but manually asking
231 // for the default disposition is harmless, and it'll be a long time before
232 // no one's using anything older than O!
233 if (CFG_TOYBOX_ON_ANDROID) signal(SIGPIPE, SIG_DFL);
234
235 // If nommu can't fork, special reentry path.
236 // Use !stacktop to signal "vfork happened", both before and after xexec()
237 if (!CFG_TOYBOX_FORK) {
238 if (0x80 & **argv) {
239 **argv &= 0x7f;
240 toys.stacktop = 0;
241 }
242 }
243
244 if (CFG_TOYBOX) {
245 // Call the multiplexer, adjusting this argv[] to be its' argv[1].
246 // (It will adjust it back before calling toy_exec().)
247 toys.argv = argv-1;
248 toybox_main();
249 } else {
250 // a single toybox command built standalone with no multiplexer
251 toy_singleinit(toy_list, argv);
252 toy_list->toy_main();
253 }
254
255 xexit();
256 }
257