1
2
3
4
5
6#include "toys.h"
7
8
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
16struct toy_list toy_list[] = {
17#include "generated/newtoys.h"
18};
19
20
21
22struct toy_context toys;
23union global_union this;
24char *toybox_version = TOYBOX_VERSION, toybuf[4096], libbuf[4096];
25
26struct toy_list *toy_find(char *name)
27{
28 int top, bottom, middle;
29
30 if (!CFG_TOYBOX || strchr(name, '/')) return 0;
31
32
33 if (!toys.which && strstart(&name, "toybox")) return toy_list;
34 bottom = 1;
35
36
37 top = ARRAY_LEN(toy_list)-1;
38 for (;;) {
39 int result;
40
41 middle = (top+bottom)/2;
42 if (middle<bottom || middle>top) return 0;
43 result = strcmp(name,toy_list[middle].name);
44 if (!result) return toy_list+middle;
45 if (result<0) top = --middle;
46 else bottom = ++middle;
47 }
48}
49
50
51
52
53
54
55#undef NEWTOY
56#undef OLDTOY
57#define NEWTOY(name, opts, flags) opts ||
58#define OLDTOY(name, oldname, flags) OPTSTR_##oldname ||
59static const int NEED_OPTIONS =
60#include "generated/newtoys.h"
610;
62
63static void unknown(char *name)
64{
65 toys.exitval = 127;
66 toys.which = toy_list;
67 help_exit("Unknown command %s", name);
68}
69
70
71void toy_singleinit(struct toy_list *which, char *argv[])
72{
73 toys.which = which;
74 toys.argv = argv;
75 toys.toycount = ARRAY_LEN(toy_list);
76
77
78 if (CFG_TOYBOX_HELP_DASHDASH && !(which->flags & TOYFLAG_NOHELP) && argv[1]) {
79 if (!strcmp(argv[1], "--help")) {
80 if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2])
81 if (!(toys.which = toy_find(toys.argv[2]))) unknown(toys.argv[2]);
82 show_help(stdout, 1);
83 xexit();
84 }
85
86 if (!strcmp(argv[1], "--version")) {
87 xprintf("toybox %s\n", toybox_version);
88 xexit();
89 }
90 }
91
92 if (NEED_OPTIONS && which->options) get_optflags();
93 else {
94 toys.optargs = argv+1;
95 for (toys.optc = 0; toys.optargs[toys.optc]; toys.optc++);
96 }
97
98 if (!(CFG_TOYBOX && which == toy_list) && !(which->flags & TOYFLAG_NOFORK)) {
99 toys.old_umask = umask(0);
100 if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
101
102
103
104
105 setlocale(LC_CTYPE, "");
106 if (strcmp("UTF-8", nl_langinfo(CODESET)))
107 uselocale(newlocale(LC_CTYPE_MASK, "en_US.UTF-8", NULL));
108
109 setvbuf(stdout, 0, (which->flags & TOYFLAG_LINEBUF) ? _IOLBF : _IONBF, 0);
110 }
111}
112
113
114void toy_init(struct toy_list *which, char *argv[])
115{
116 void *oldwhich = toys.which;
117
118
119
120 if (CFG_TOYBOX_SUID) {
121 if (!toys.which) toys.which = toy_list;
122
123 uid_t uid = getuid(), euid = geteuid();
124
125 if (!(which->flags & TOYFLAG_STAYROOT)) {
126 if (uid != euid) {
127 if (setuid(uid)) perror_exit("setuid %d->%d", euid, uid);
128 euid = uid;
129 toys.wasroot++;
130 }
131 } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
132 error_msg("Not installed suid root");
133
134 if ((which->flags & TOYFLAG_NEEDROOT) && euid) help_exit("Not root");
135 }
136
137
138
139 if (argv<toys.optargs || argv>toys.optargs+toys.optc) free(toys.optargs);
140 memset(&toys, 0, offsetof(struct toy_context, rebound));
141 if (oldwhich) memset(&this, 0, sizeof(this));
142
143
144 toy_singleinit(which, argv);
145}
146
147
148
149static void toy_exec_which(struct toy_list *which, char *argv[])
150{
151
152 if (!which || (which->flags&TOYFLAG_NOFORK)) return;
153
154
155
156
157
158
159
160 if (!CFG_TOYBOX_NORECURSE && toys.stacktop)
161 if (labs((long)toys.stacktop-(long)&which)>6000) return;
162
163
164 if (toys.which && (which->flags&TOYFLAG_ROOTONLY) && toys.wasroot) return;
165
166
167 toy_init(which, argv);
168 if (toys.which) toys.which->toy_main();
169 xexit();
170}
171
172
173void toy_exec(char *argv[])
174{
175 toy_exec_which(toy_find(*argv), argv);
176}
177
178
179
180void toybox_main(void)
181{
182 char *toy_paths[] = {"usr/", "bin/", "sbin/", 0}, *s = toys.argv[1];
183 int i, len = 0;
184 unsigned width = 80;
185
186
187
188
189 while (s) {
190 struct toy_list *tl = toy_find(basename(s));
191
192 if (tl==toy_list && s!=toys.argv[1]) unknown(basename(s));
193 toy_exec_which(toy_find(basename(s)), toys.argv+1);
194 s = (0<readlink(s, libbuf, sizeof(libbuf))) ? libbuf : 0;
195 }
196
197
198 toys.which = toy_list;
199
200 if (toys.argv[1] && strcmp(toys.argv[1], "--long")) unknown(toys.argv[1]);
201
202
203 terminal_size(&width, 0);
204 for (i = 1; i<ARRAY_LEN(toy_list); i++) {
205 int fl = toy_list[i].flags;
206 if (fl & TOYMASK_LOCATION) {
207 if (toys.argv[1]) {
208 int j;
209 for (j = 0; toy_paths[j]; j++)
210 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
211 }
212 len += printf("%s",toy_list[i].name);
213 if (++len > width-15) len = 0;
214 xputc(len ? ' ' : '\n');
215 }
216 }
217 xputc('\n');
218}
219
220int main(int argc, char *argv[])
221{
222
223 if (!*argv) return 127;
224
225
226
227 if (!CFG_TOYBOX_FORK && (0x80 & **argv)) **argv &= 0x7f;
228 else {
229 int stack_start;
230
231 toys.stacktop = &stack_start;
232 }
233
234
235 if (CFG_TOYBOX_ON_ANDROID) signal(SIGPIPE, SIG_DFL);
236
237 if (CFG_TOYBOX) {
238
239 toys.argv = argv-1;
240 toybox_main();
241 } else {
242
243 toy_singleinit(toy_list, argv);
244 toy_list->toy_main();
245 }
246
247 xexit();
248}
249