Mercurial > hg > toybox
annotate main.c @ 1676:cbb1aca81eca draft
Make toy_exec() check if argc is in optargs and deal with it there so we don't need a separate xexec_optargs().
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 07 Feb 2015 16:17:44 -0600 |
parents | 10922d83392a |
children | 31475e814232 |
rev | line source |
---|---|
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
1 /* Toybox infrastructure. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
2 * |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
3 * Copyright 2006 Rob Landley <rob@landley.net> |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
4 */ |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
5 |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
6 #include "toys.h" |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
7 |
21
6475d6c46066
Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents:
20
diff
changeset
|
8 // Populate toy_list[]. |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
9 |
88
021fe1a818c3
Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
10 #undef NEWTOY |
021fe1a818c3
Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
11 #undef OLDTOY |
021fe1a818c3
Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
12 #define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags}, |
1634
5fac2769a159
Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents:
1587
diff
changeset
|
13 #define OLDTOY(name, oldname, flags) \ |
5fac2769a159
Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents:
1587
diff
changeset
|
14 {#name, oldname##_main, OPTSTR_##oldname, flags}, |
88
021fe1a818c3
Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents:
65
diff
changeset
|
15 |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
16 struct toy_list toy_list[] = { |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
17 #include "generated/newtoys.h" |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
18 }; |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
19 |
873
d90f14e011b8
Fix some comments from way back when toybox first started (in 2006), when I was still cleaning busybox-isms out of my head...
Rob Landley <rob@landley.net>
parents:
782
diff
changeset
|
20 // global context for this command. |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
21 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
22 struct toy_context toys; |
237
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
23 union global_union this; |
1043
acf7bb2b99e2
Introduce libbuf analogous to toybuf but for use by lib/*.c. Change readfile() semantics to be able to read into an existing buffer, or malloc its own if that's NULL.
Rob Landley <rob@landley.net>
parents:
1032
diff
changeset
|
24 char toybuf[4096], libbuf[4096]; |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
25 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
26 struct toy_list *toy_find(char *name) |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
27 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
28 int top, bottom, middle; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
29 |
1501
c51a4dbe5db7
Don't segfault for --help of single.sh build of OLDTOY commands that use another command's help.
Rob Landley <rob@landley.net>
parents:
1493
diff
changeset
|
30 if (!CFG_TOYBOX) return 0; |
c51a4dbe5db7
Don't segfault for --help of single.sh build of OLDTOY commands that use another command's help.
Rob Landley <rob@landley.net>
parents:
1493
diff
changeset
|
31 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
32 // If the name starts with "toybox" accept that as a match. Otherwise |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
33 // skip the first entry, which is out of order. |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
34 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
35 if (!strncmp(name,"toybox",6)) return toy_list; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
36 bottom = 1; |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
37 |
873
d90f14e011b8
Fix some comments from way back when toybox first started (in 2006), when I was still cleaning busybox-isms out of my head...
Rob Landley <rob@landley.net>
parents:
782
diff
changeset
|
38 // Binary search to find this command. |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
39 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
40 top = ARRAY_LEN(toy_list)-1; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
41 for (;;) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
42 int result; |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
138
diff
changeset
|
43 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
44 middle = (top+bottom)/2; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
45 if (middle<bottom || middle>top) return NULL; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
46 result = strcmp(name,toy_list[middle].name); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
47 if (!result) return toy_list+middle; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
48 if (result<0) top=--middle; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
49 else bottom = ++middle; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
50 } |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
51 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
52 |
122
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
53 // Figure out whether or not anything is using the option parsing logic, |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
54 // because the compiler can't figure out whether or not to optimize it away |
402 | 55 // on its' own. NEED_OPTIONS becomes a constant allowing if() to optimize |
56 // stuff out via dead code elimination. | |
122
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
57 |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
58 #undef NEWTOY |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
59 #undef OLDTOY |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
60 #define NEWTOY(name, opts, flags) opts || |
1634
5fac2769a159
Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents:
1587
diff
changeset
|
61 #define OLDTOY(name, oldname, flags) OPTSTR_##oldname || |
125 | 62 static const int NEED_OPTIONS = |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
197
diff
changeset
|
63 #include "generated/newtoys.h" |
122
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
64 0; // Ends the opts || opts || opts... |
ee49aa0dc731
Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents:
121
diff
changeset
|
65 |
956
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
66 // Subset of init needed by singlemain |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
67 static void toy_singleinit(struct toy_list *which, char *argv[]) |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
68 { |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
69 toys.which = which; |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
70 toys.argv = argv; |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
71 |
1493
74b29c369420
Always call setlocale if I18N is enabled, so nested toy_exec() can switch it back off if necessary.
Rob Landley <rob@landley.net>
parents:
1467
diff
changeset
|
72 if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "C"+!!(which->flags & TOYFLAG_LOCALE)); |
1357
e01ae62fcac5
Forgot to check in main() part of TOYFLAG_LOCALE change.
Rob Landley <rob@landley.net>
parents:
1351
diff
changeset
|
73 |
956
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
74 if (CFG_TOYBOX_HELP_DASHDASH && argv[1] && !strcmp(argv[1], "--help")) { |
1501
c51a4dbe5db7
Don't segfault for --help of single.sh build of OLDTOY commands that use another command's help.
Rob Landley <rob@landley.net>
parents:
1493
diff
changeset
|
75 if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2]) |
1022
71f64e2f24a9
Fix --help option to multiplexer.
Rob Landley <rob@landley.net>
parents:
956
diff
changeset
|
76 if (!(toys.which = toy_find(toys.argv[2]))) return; |
956
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
77 show_help(); |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
78 xexit(); |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
79 } |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
80 |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
81 if (NEED_OPTIONS && which->options) get_optflags(); |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
82 else { |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
83 toys.optargs = argv+1; |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
84 for (toys.optc=0; toys.optargs[toys.optc]; toys.optc++); |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
85 } |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
86 toys.old_umask = umask(0); |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
87 if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask); |
1299
313980d3d78c
Add generic_signal() handler, which sets toys.signal and writes byte to toys.signalfd if set.
Rob Landley <rob@landley.net>
parents:
1236
diff
changeset
|
88 toys.signalfd--; |
1351
0f2b9d0b1f7a
Move toys.toycount initialization _after_ zeroing toys, so help -a works again.
Rob Landley <rob@landley.net>
parents:
1311
diff
changeset
|
89 toys.toycount = ARRAY_LEN(toy_list); |
956
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
90 } |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
91 |
402 | 92 // Setup toybox global state for this command. |
93 | |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
94 void toy_init(struct toy_list *which, char *argv[]) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
95 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
96 // Drop permissions for non-suid commands. |
370 | 97 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
98 if (CFG_TOYBOX_SUID) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
99 uid_t uid = getuid(), euid = geteuid(); |
370 | 100 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
101 if (!(which->flags & TOYFLAG_STAYROOT)) { |
1452
8f9721561211
Give a hint when setuid logic fails.
Rob Landley <rob@landley.net>
parents:
1357
diff
changeset
|
102 if (uid != euid) { |
8f9721561211
Give a hint when setuid logic fails.
Rob Landley <rob@landley.net>
parents:
1357
diff
changeset
|
103 if (!setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root |
8f9721561211
Give a hint when setuid logic fails.
Rob Landley <rob@landley.net>
parents:
1357
diff
changeset
|
104 else euid = uid; |
8f9721561211
Give a hint when setuid logic fails.
Rob Landley <rob@landley.net>
parents:
1357
diff
changeset
|
105 } |
769
098ab2654fa0
TOYBOX_DEBUG warns about lack of suid bit when running a STAYROOT command, but it shouldn't warn just because the multiplexer command "toybox" is stayroot.
Rob Landley <rob@landley.net>
parents:
761
diff
changeset
|
106 } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list) |
098ab2654fa0
TOYBOX_DEBUG warns about lack of suid bit when running a STAYROOT command, but it shouldn't warn just because the multiplexer command "toybox" is stayroot.
Rob Landley <rob@landley.net>
parents:
761
diff
changeset
|
107 error_msg("Not installed suid root"); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
108 |
1311
726d3e67d8c1
The "not root" test happens before looking for --help, so "./sulogin --help" doesn't show it. Instead make the "not root" failure case always show help text.
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
109 if ((which->flags & TOYFLAG_NEEDROOT) && euid) { |
726d3e67d8c1
The "not root" test happens before looking for --help, so "./sulogin --help" doesn't show it. Instead make the "not root" failure case always show help text.
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
110 toys.exithelp++; |
726d3e67d8c1
The "not root" test happens before looking for --help, so "./sulogin --help" doesn't show it. Instead make the "not root" failure case always show help text.
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
111 error_exit("Not root"); |
726d3e67d8c1
The "not root" test happens before looking for --help, so "./sulogin --help" doesn't show it. Instead make the "not root" failure case always show help text.
Rob Landley <rob@landley.net>
parents:
1299
diff
changeset
|
112 } |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
113 } |
370 | 114 |
696
99ca30ad3d2b
Add rebound support to intercept error_exit() and longjmp instead.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
115 // Free old toys contents (to be reentrant), but leave rebound if any |
370 | 116 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
117 if (toys.optargs != toys.argv+1) free(toys.optargs); |
696
99ca30ad3d2b
Add rebound support to intercept error_exit() and longjmp instead.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
118 memset(&toys, 0, offsetof(struct toy_context, rebound)); |
373
5e68c7cab1a4
Make toy_init() reentrant, or else xexec() has funky errors.
Rob Landley <rob@landley.net>
parents:
370
diff
changeset
|
119 |
956
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
120 // Subset of init needed by singlemain. |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
121 toy_singleinit(which, argv); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
122 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
123 |
402 | 124 // Like exec() but runs an internal toybox command instead of another file. |
1467
d1411369baa7
Two problems: 1) Sometimes toy_exec() needs to re-exec to gain dropped root permissions, 2) shouldn't recurse forever without exec, stack depth increases and we may leak other resources. Limit it to ~5 levels.
Rob Landley <rob@landley.net>
parents:
1452
diff
changeset
|
125 // Only returns if it can't run command internally, otherwise exit() when done. |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
126 void toy_exec(char *argv[]) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
127 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
128 struct toy_list *which; |
121
933766b0bd4b
Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents:
93
diff
changeset
|
129 |
1676
cbb1aca81eca
Make toy_exec() check if argc is in optargs and deal with it there so we don't need a separate xexec_optargs().
Rob Landley <rob@landley.net>
parents:
1666
diff
changeset
|
130 // don't blank old optargs if our new argc lives in the old optargs. |
cbb1aca81eca
Make toy_exec() check if argc is in optargs and deal with it there so we don't need a separate xexec_optargs().
Rob Landley <rob@landley.net>
parents:
1666
diff
changeset
|
131 if (argv>=toys.optargs && argv<=toys.optargs+toys.optc) toys.optargs = 0; |
cbb1aca81eca
Make toy_exec() check if argc is in optargs and deal with it there so we don't need a separate xexec_optargs().
Rob Landley <rob@landley.net>
parents:
1666
diff
changeset
|
132 |
1467
d1411369baa7
Two problems: 1) Sometimes toy_exec() needs to re-exec to gain dropped root permissions, 2) shouldn't recurse forever without exec, stack depth increases and we may leak other resources. Limit it to ~5 levels.
Rob Landley <rob@landley.net>
parents:
1452
diff
changeset
|
133 // Return if we can't find it, or need to re-exec to acquire root, |
d1411369baa7
Two problems: 1) Sometimes toy_exec() needs to re-exec to gain dropped root permissions, 2) shouldn't recurse forever without exec, stack depth increases and we may leak other resources. Limit it to ~5 levels.
Rob Landley <rob@landley.net>
parents:
1452
diff
changeset
|
134 // or if stack depth is getting silly. |
895
60a430b7791e
Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents:
873
diff
changeset
|
135 if (!(which = toy_find(argv[0]))) return; |
1467
d1411369baa7
Two problems: 1) Sometimes toy_exec() needs to re-exec to gain dropped root permissions, 2) shouldn't recurse forever without exec, stack depth increases and we may leak other resources. Limit it to ~5 levels.
Rob Landley <rob@landley.net>
parents:
1452
diff
changeset
|
136 if (toys.recursion && (which->flags & TOYFLAG_ROOTONLY) && getuid()) return; |
d1411369baa7
Two problems: 1) Sometimes toy_exec() needs to re-exec to gain dropped root permissions, 2) shouldn't recurse forever without exec, stack depth increases and we may leak other resources. Limit it to ~5 levels.
Rob Landley <rob@landley.net>
parents:
1452
diff
changeset
|
137 if (toys.recursion++ > 5) return; |
d1411369baa7
Two problems: 1) Sometimes toy_exec() needs to re-exec to gain dropped root permissions, 2) shouldn't recurse forever without exec, stack depth increases and we may leak other resources. Limit it to ~5 levels.
Rob Landley <rob@landley.net>
parents:
1452
diff
changeset
|
138 |
d1411369baa7
Two problems: 1) Sometimes toy_exec() needs to re-exec to gain dropped root permissions, 2) shouldn't recurse forever without exec, stack depth increases and we may leak other resources. Limit it to ~5 levels.
Rob Landley <rob@landley.net>
parents:
1452
diff
changeset
|
139 // Run command |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
140 toy_init(which, argv); |
1022
71f64e2f24a9
Fix --help option to multiplexer.
Rob Landley <rob@landley.net>
parents:
956
diff
changeset
|
141 if (toys.which) toys.which->toy_main(); |
938
812e8c5d026f
Add config option for --help support in all commands.
Rob Landley <rob@landley.net>
parents:
895
diff
changeset
|
142 xexit(); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
143 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
144 |
402 | 145 // Multiplexer command, first argument is command to run, rest are args to that. |
146 // If first argument starts with - output list of command install paths. | |
147 | |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
182
diff
changeset
|
148 void toybox_main(void) |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
149 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
150 static char *toy_paths[]={"usr/","bin/","sbin/",0}; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
151 int i, len = 0; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
152 |
704
e45ab88c477d
Init global context to toybox multiplexer early so error_exit() doesn't segfault trying to print current command name, and change TOYBOX_DEBUG+TOYBOX_SUID complaint about not having the suid bit set to warning rather than exit..
Rob Landley <rob@landley.net>
parents:
697
diff
changeset
|
153 toys.which = toy_list; |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
154 if (toys.argv[1]) { |
1587
a35ea98b4972
The "re-exec to regain root permissions" logic broke the error message when calling root-only commands through the multiplexer.
Rob Landley <rob@landley.net>
parents:
1533
diff
changeset
|
155 toys.optc = toys.recursion = 0; |
1022
71f64e2f24a9
Fix --help option to multiplexer.
Rob Landley <rob@landley.net>
parents:
956
diff
changeset
|
156 toy_exec(toys.argv+1); |
71f64e2f24a9
Fix --help option to multiplexer.
Rob Landley <rob@landley.net>
parents:
956
diff
changeset
|
157 if (toys.argv[1][0] == '-') goto list; |
1666 | 158 |
895
60a430b7791e
Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents:
873
diff
changeset
|
159 error_exit("Unknown command %s",toys.argv[1]); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
160 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
161 |
895
60a430b7791e
Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents:
873
diff
changeset
|
162 list: |
873
d90f14e011b8
Fix some comments from way back when toybox first started (in 2006), when I was still cleaning busybox-isms out of my head...
Rob Landley <rob@landley.net>
parents:
782
diff
changeset
|
163 // Output list of command. |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
164 for (i=1; i<ARRAY_LEN(toy_list); i++) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
165 int fl = toy_list[i].flags; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
166 if (fl & TOYMASK_LOCATION) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
167 if (toys.argv[1]) { |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
168 int j; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
169 for (j=0; toy_paths[j]; j++) |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
170 if (fl & (1<<j)) len += printf("%s", toy_paths[j]); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
171 } |
1533
3c77af6f81cc
Cosmetic tweak: no trailing spaces when ./toybox lists command names.
Rob Landley <rob@landley.net>
parents:
1501
diff
changeset
|
172 len += printf("%s",toy_list[i].name); |
3c77af6f81cc
Cosmetic tweak: no trailing spaces when ./toybox lists command names.
Rob Landley <rob@landley.net>
parents:
1501
diff
changeset
|
173 if (++len > 65) len = 0; |
3c77af6f81cc
Cosmetic tweak: no trailing spaces when ./toybox lists command names.
Rob Landley <rob@landley.net>
parents:
1501
diff
changeset
|
174 xputc(len ? ' ' : '\n'); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
175 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
176 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
647
diff
changeset
|
177 xputc('\n'); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
178 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
179 |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
180 int main(int argc, char *argv[]) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
181 { |
1032
40d0c96a8e89
Add scripts/single.sh to build individual non-multiplexed standalone commands.
Rob Landley <rob@landley.net>
parents:
1022
diff
changeset
|
182 if (CFG_TOYBOX) { |
956
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
183 // Trim path off of command name |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
184 *argv = basename(*argv); |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
185 |
956
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
186 // Call the multiplexer, adjusting this argv[] to be its' argv[1]. |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
187 // (It will adjust it back before calling toy_exec().) |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
188 toys.argv = argv-1; |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
189 toybox_main(); |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
190 } else { |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
191 // a single toybox command built standalone with no multiplexer |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
192 toy_singleinit(toy_list, argv); |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
193 toy_list->toy_main(); |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
194 } |
caa05719070f
Start of TOYBOX_SINGLE support, for building standalone commands with no multiplexer.
Rob Landley <rob@landley.net>
parents:
938
diff
changeset
|
195 |
1644
492bd41f8b9a
Move fflush() checking to xexit() and have exit paths in main() call that.
Rob Landley <rob@landley.net>
parents:
1634
diff
changeset
|
196 xexit(); |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
197 } |