OSDN Git Service

Replace TOY_LIST_LEN with more generic ARRAY_LEN()
[android-x86/external-toybox.git] / main.c
1 /* vi: set ts=4 :*/
2 /* Toybox infrastructure.
3  *
4  * Copyright 2006 Rob Landley <rob@landley.net>
5  */
6
7 #include "toys.h"
8
9 // Populate toy_list[].
10
11 #undef NEWTOY
12 #undef OLDTOY
13 #define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags},
14 #define OLDTOY(name, oldname, opts, flags) {#name, oldname##_main, opts, flags},
15
16 struct toy_list toy_list[] = {
17 #include "generated/newtoys.h"
18 };
19
20 // global context for this applet.
21
22 struct toy_context toys;
23 union global_union this;
24 char toybuf[4096];
25
26 struct toy_list *toy_find(char *name)
27 {
28         int top, bottom, middle;
29
30         // If the name starts with "toybox" accept that as a match.  Otherwise
31         // skip the first entry, which is out of order.
32
33         if (!strncmp(name,"toybox",6)) return toy_list;
34         bottom = 1;
35
36         // Binary search to find this applet.
37
38         top = ARRAY_LEN(toy_list)-1;
39         for (;;) {
40                 int result;
41
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;
48         }
49 }
50
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.
55
56 #undef NEWTOY
57 #undef OLDTOY
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...
63
64 // Setup toybox global state for this command.
65
66 void toy_init(struct toy_list *which, char *argv[])
67 {
68         // Drop permissions for non-suid commands.
69
70         if (CFG_TOYBOX_SUID) {
71                 uid_t uid = getuid(), euid = geteuid();
72
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");
77
78                 if ((which->flags & TOYFLAG_NEEDROOT) && euid)
79                         error_exit("Not root");
80         }
81
82         // Free old toys contents (to be reentrant)
83
84         if (toys.optargs != toys.argv+1) free(toys.optargs);
85         memset(&toys, 0, sizeof(struct toy_context));
86
87         toys.which = which;
88         toys.argv = argv;
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);
93 }
94
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[])
98 {
99         struct toy_list *which;
100
101         which = toy_find(argv[0]);
102         if (!which) return;
103         toy_init(which, argv);
104         toys.which->toy_main();
105         exit(toys.exitval);
106 }
107
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.
110
111 void toybox_main(void)
112 {
113         static char *toy_paths[]={"usr/","bin/","sbin/",0};
114         int i, len = 0;
115
116         if (toys.argv[1]) {
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]);
121                 }
122         }
123
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) {
128                         if (toys.argv[1]) {
129                                 int j;
130                                 for (j=0; toy_paths[j]; j++)
131                                         if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
132                         }
133                         len += printf("%s ",toy_list[i].name);
134                         if (len>65) {
135                                 xputc('\n');
136                                 len=0;
137                         }
138                 }
139         }
140         xputc('\n');
141 }
142
143 int main(int argc, char *argv[])
144 {
145         // Artificial scope to eat less stack for things we call
146         {
147                 char *name;
148
149                 // Trim path off of command name
150                 name = strrchr(argv[0], '/');
151                 if (!name) name=argv[0];
152                 else name++;
153                 argv[0] = name;
154         }
155
156         // Call the multiplexer, adjusting this argv[] to be its' argv[1].
157         // (It will adjust it back before calling toy_exec().)
158         toys.argv = argv-1;
159         toybox_main();
160         return 0;
161 }