1 /* Toybox infrastructure.
3 * Copyright 2006 Rob Landley <rob@landley.net>
8 // Populate toy_list[].
12 #define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags},
13 #define OLDTOY(name, oldname, flags) \
14 {#name, oldname##_main, OPTSTR_##oldname, flags},
16 struct toy_list toy_list[] = {
17 #include "generated/newtoys.h"
20 // global context for this command.
22 struct toy_context toys;
23 union global_union this;
24 char toybuf[4096], libbuf[4096];
26 struct toy_list *toy_find(char *name)
28 int top, bottom, middle;
30 if (!CFG_TOYBOX) return 0;
32 // If the name starts with "toybox" accept that as a match. Otherwise
33 // skip the first entry, which is out of order.
35 if (!strncmp(name,"toybox",6)) return toy_list;
38 // Binary search to find this command.
40 top = ARRAY_LEN(toy_list)-1;
44 middle = (top+bottom)/2;
45 if (middle<bottom || middle>top) return NULL;
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;
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.
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...
66 // Subset of init needed by singlemain
67 static void toy_singleinit(struct toy_list *which, char *argv[])
72 if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "C"+!!(which->flags & TOYFLAG_LOCALE));
74 if (CFG_TOYBOX_HELP_DASHDASH && argv[1] && !strcmp(argv[1], "--help")) {
75 if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2])
76 if (!(toys.which = toy_find(toys.argv[2]))) return;
81 if (NEED_OPTIONS && which->options) get_optflags();
83 toys.optargs = argv+1;
84 for (toys.optc=0; toys.optargs[toys.optc]; toys.optc++);
86 toys.old_umask = umask(0);
87 if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
89 toys.toycount = ARRAY_LEN(toy_list);
92 // Setup toybox global state for this command.
94 void toy_init(struct toy_list *which, char *argv[])
96 // Drop permissions for non-suid commands.
98 if (CFG_TOYBOX_SUID) {
99 uid_t uid = getuid(), euid = geteuid();
101 if (!(which->flags & TOYFLAG_STAYROOT)) {
103 if (!setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
106 } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
107 error_msg("Not installed suid root");
109 if ((which->flags & TOYFLAG_NEEDROOT) && euid) {
111 error_exit("Not root");
115 // Free old toys contents (to be reentrant), but leave rebound if any
117 if (toys.optargs != toys.argv+1) free(toys.optargs);
118 memset(&toys, 0, offsetof(struct toy_context, rebound));
119 if (toys.recursion > 1) memset(&this, 0, sizeof(this));
121 // Subset of init needed by singlemain.
122 toy_singleinit(which, argv);
125 // Like exec() but runs an internal toybox command instead of another file.
126 // Only returns if it can't run command internally, otherwise exit() when done.
127 void toy_exec(char *argv[])
129 struct toy_list *which;
131 // Return if we can't find it, or need to re-exec to acquire root,
132 // or if stack depth is getting silly.
133 if (!(which = toy_find(argv[0]))) return;
134 if (toys.recursion && (which->flags & TOYFLAG_ROOTONLY) && getuid()) return;
135 if (toys.recursion++ > 5) return;
137 // don't blank old optargs if our new argc lives in the old optargs.
138 if (argv>=toys.optargs && argv<=toys.optargs+toys.optc) toys.optargs = 0;
141 toy_init(which, argv);
142 if (toys.which) toys.which->toy_main();
146 // Multiplexer command, first argument is command to run, rest are args to that.
147 // If first argument starts with - output list of command install paths.
149 void toybox_main(void)
151 static char *toy_paths[]={"usr/","bin/","sbin/",0};
154 toys.which = toy_list;
156 toys.optc = toys.recursion = 0;
157 toy_exec(toys.argv+1);
158 if (toys.argv[1][0] == '-') goto list;
160 error_exit("Unknown command %s",toys.argv[1]);
164 // Output list of command.
165 for (i=1; i<ARRAY_LEN(toy_list); i++) {
166 int fl = toy_list[i].flags;
167 if (fl & TOYMASK_LOCATION) {
170 for (j=0; toy_paths[j]; j++)
171 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
173 len += printf("%s",toy_list[i].name);
174 if (++len > 65) len = 0;
175 xputc(len ? ' ' : '\n');
181 int main(int argc, char *argv[])
184 // Trim path off of command name
185 *argv = basename(*argv);
187 // Call the multiplexer, adjusting this argv[] to be its' argv[1].
188 // (It will adjust it back before calling toy_exec().)
192 // a single toybox command built standalone with no multiplexer
193 toy_singleinit(toy_list, argv);
194 toy_list->toy_main();