2 /* Toybox infrastructure.
4 * Copyright 2006 Rob Landley <rob@landley.net>
9 // Populate toy_list[].
13 #define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags},
14 #define OLDTOY(name, oldname, opts, flags) {#name, oldname##_main, opts, flags},
16 struct toy_list toy_list[] = {
17 #include "generated/newtoys.h"
20 // global context for this applet.
22 struct toy_context toys;
23 union global_union this;
26 struct toy_list *toy_find(char *name)
28 int top, bottom, middle;
30 // If the name starts with "toybox" accept that as a match. Otherwise
31 // skip the first entry, which is out of order.
33 if (!strncmp(name,"toybox",6)) return toy_list;
36 // Binary search to find this applet.
38 top = ARRAY_LEN(toy_list)-1;
42 middle = (top+bottom)/2;
43 if (middle<bottom || middle>top) return NULL;
44 result = strcmp(name,toy_list[middle].name);
45 if (!result) return toy_list+middle;
46 if (result<0) top=--middle;
47 else bottom = ++middle;
51 // Figure out whether or not anything is using the option parsing logic,
52 // because the compiler can't figure out whether or not to optimize it away
53 // on its' own. NEED_OPTIONS becomes a constant allowing if() to optimize
54 // stuff out via dead code elimination.
58 #define NEWTOY(name, opts, flags) opts ||
59 #define OLDTOY(name, oldname, opts, flags) opts ||
60 static const int NEED_OPTIONS =
61 #include "generated/newtoys.h"
62 0; // Ends the opts || opts || opts...
64 // Setup toybox global state for this command.
66 void toy_init(struct toy_list *which, char *argv[])
68 // Drop permissions for non-suid commands.
70 if (CFG_TOYBOX_SUID) {
71 uid_t uid = getuid(), euid = geteuid();
73 if (!(which->flags & TOYFLAG_STAYROOT)) {
74 if (uid != euid) xsetuid(euid=uid);
75 } else if (CFG_TOYBOX_DEBUG && uid)
76 error_exit("Not installed suid root");
78 if ((which->flags & TOYFLAG_NEEDROOT) && euid)
79 error_exit("Not root");
82 // Free old toys contents (to be reentrant)
84 if (toys.optargs != toys.argv+1) free(toys.optargs);
85 memset(&toys, 0, sizeof(struct toy_context));
89 if (NEED_OPTIONS && which->options) get_optflags();
90 else toys.optargs = argv+1;
91 toys.old_umask = umask(0);
92 if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
95 // Like exec() but runs an internal toybox command instead of another file.
96 // Only returns if it can't find the command, otherwise exit() when done.
97 void toy_exec(char *argv[])
99 struct toy_list *which;
101 which = toy_find(argv[0]);
103 toy_init(which, argv);
104 toys.which->toy_main();
108 // Multiplexer command, first argument is command to run, rest are args to that.
109 // If first argument starts with - output list of command install paths.
111 void toybox_main(void)
113 static char *toy_paths[]={"usr/","bin/","sbin/",0};
117 if (toys.argv[1][0]!='-') {
118 toy_exec(toys.argv+1);
119 toys.which = toy_list;
120 error_exit("Unknown command %s",toys.argv[1]);
124 // Output list of applets.
125 for (i=1; i<ARRAY_LEN(toy_list); i++) {
126 int fl = toy_list[i].flags;
127 if (fl & TOYMASK_LOCATION) {
130 for (j=0; toy_paths[j]; j++)
131 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
133 len += printf("%s ",toy_list[i].name);
143 int main(int argc, char *argv[])
145 // Artificial scope to eat less stack for things we call
149 // Trim path off of command name
150 name = strrchr(argv[0], '/');
151 if (!name) name=argv[0];
156 // Call the multiplexer, adjusting this argv[] to be its' argv[1].
157 // (It will adjust it back before calling toy_exec().)