OSDN Git Service

The "no }" error with find | xargs sed is because toy_init() wasn't blanking the...
[android-x86/external-toybox.git] / main.c
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, opts, 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
26 struct toy_list *toy_find(char *name)
27 {
28   int top, bottom, middle;
29
30   if (!CFG_TOYBOX) 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 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;
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
66 // Subset of init needed by singlemain
67 static void toy_singleinit(struct toy_list *which, char *argv[])
68 {
69   toys.which = which;
70   toys.argv = argv;
71
72   if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "C"+!!(which->flags & TOYFLAG_LOCALE));
73
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;
77     show_help();
78     xexit();
79   }
80
81   if (NEED_OPTIONS && which->options) get_optflags();
82   else {
83     toys.optargs = argv+1;
84     for (toys.optc=0; toys.optargs[toys.optc]; toys.optc++);
85   }
86   toys.old_umask = umask(0);
87   if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
88   toys.signalfd--;
89   toys.toycount = ARRAY_LEN(toy_list);
90 }
91
92 // Setup toybox global state for this command.
93
94 void toy_init(struct toy_list *which, char *argv[])
95 {
96   // Drop permissions for non-suid commands.
97
98   if (CFG_TOYBOX_SUID) {
99     uid_t uid = getuid(), euid = geteuid();
100
101     if (!(which->flags & TOYFLAG_STAYROOT)) {
102       if (uid != euid) {
103         if (!setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
104         else euid = uid;
105       }
106     } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
107       error_msg("Not installed suid root");
108
109     if ((which->flags & TOYFLAG_NEEDROOT) && euid) {
110       toys.exithelp++;
111       error_exit("Not root");
112     }
113   }
114
115   // Free old toys contents (to be reentrant), but leave rebound if any
116
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));
120
121   // Subset of init needed by singlemain.
122   toy_singleinit(which, argv);
123 }
124
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[])
128 {
129   struct toy_list *which;
130
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;
136
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;
139
140   // Run command
141   toy_init(which, argv);
142   if (toys.which) toys.which->toy_main();
143   xexit();
144 }
145
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.
148
149 void toybox_main(void)
150 {
151   static char *toy_paths[]={"usr/","bin/","sbin/",0};
152   int i, len = 0;
153
154   toys.which = toy_list;
155   if (toys.argv[1]) {
156     toys.optc = toys.recursion = 0;
157     toy_exec(toys.argv+1);
158     if (toys.argv[1][0] == '-') goto list;
159
160     error_exit("Unknown command %s",toys.argv[1]);
161   }
162
163 list:
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) {
168       if (toys.argv[1]) {
169         int j;
170         for (j=0; toy_paths[j]; j++)
171           if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
172       }
173       len += printf("%s",toy_list[i].name);
174       if (++len > 65) len = 0;
175       xputc(len ? ' ' : '\n');
176     }
177   }
178   xputc('\n');
179 }
180
181 int main(int argc, char *argv[])
182 {
183   if (CFG_TOYBOX) {
184     // Trim path off of command name
185     *argv = basename(*argv);
186
187     // Call the multiplexer, adjusting this argv[] to be its' argv[1].
188     // (It will adjust it back before calling toy_exec().)
189     toys.argv = argv-1;
190     toybox_main();
191   } else {
192     // a single toybox command built standalone with no multiplexer
193     toy_singleinit(toy_list, argv);
194     toy_list->toy_main();
195   }
196
197   xexit();
198 }