blob: c58f5a83f139ef0fe78bcfff3f2f78a238df8f5c [file] [log] [blame]
Denis Vlasenko0d058362007-04-11 16:16:41 +00001 NOEXEC and NOFORK applets.
2
3Unix shells traditionally execute some commands internally in the attempt
4to dramatically speed up execution. It will be slow as hell if for every
Denis Vlasenkobcd5fc12008-01-06 06:27:17 +00005"echo blah" shell will fork and exec /bin/echo. To this end, shells
Denis Vlasenko0d058362007-04-11 16:16:41 +00006have to _reimplement_ these commands internally.
7
8Busybox is unique in this regard because it already is a collection
9of reimplemented Unix commands, and we can do the same trick
10for speeding up busybox shells, and more. NOEXEC and NOFORK applets
11are exactly those applets which are eligible for these tricks.
12
13Applet will be subject to NOFORK/NOEXEC tricks if it is marked as such
Denis Vlasenkobcd5fc12008-01-06 06:27:17 +000014in applets.h. FEATURE_PREFER_APPLETS is a config option which
Denis Vlasenko4b924f32007-05-30 00:29:55 +000015globally enables usage of NOFORK/NOEXEC tricks.
Denis Vlasenkobcd5fc12008-01-06 06:27:17 +000016If it is enabled, FEATURE_SH_STANDALONE can be enabled too,
17and then shells will use NOFORK/NOEXEC tricks for ordinary commands.
18NB: shell builtins use these tricks regardless of FEATURE_SH_STANDALONE
19or FEATURE_PREFER_APPLETS.
Denis Vlasenko0d058362007-04-11 16:16:41 +000020
Denis Vlasenkobcd5fc12008-01-06 06:27:17 +000021In C, if you want to call a program and wait for it, use
22spawn_and_wait(argv), BB_EXECVP(prog,argv) or BB_EXECLP(prog,argv0,...).
23They check whether program name is an applet name and optionally
24do NOFORK/NOEXEC thing depending on configuration.
Denis Vlasenko0d058362007-04-11 16:16:41 +000025
Denis Vlasenkobcd5fc12008-01-06 06:27:17 +000026
27 NOEXEC
Denis Vlasenko0d058362007-04-11 16:16:41 +000028
29NOEXEC applet should work correctly if another applet forks and then
30executes exit(<applet>_main(argc,argv)) in the child. The rules
31roughly are:
32
33* do not expect shared global variables/buffers to be in their
34 "initialized" state. Examples: xfunc_error_retval can be != 1,
35 bb_common_bufsiz1 can be scribbled over, ...
36* do not expect that stdio wasn't used before. Calling set[v]buf()
37 can be disastrous.
38* ...
39
40NOEXEC applets save only one half of fork+exec overhead.
Denis Vlasenkobcd5fc12008-01-06 06:27:17 +000041NOEXEC trick is disabled for NOMMU build.
Denis Vlasenko0d058362007-04-11 16:16:41 +000042
Denis Vlasenkobcd5fc12008-01-06 06:27:17 +000043
44 NOFORK
Denis Vlasenko0d058362007-04-11 16:16:41 +000045
46NOFORK applet should work correctly if another applet simply runs
Denys Vlasenkob72baeb2011-02-02 18:38:57 +010047<applet>_main(argc,argv) and then continues with its business.
48xargs, find, shells do it (grep for "spawn_and_wait" and
49"run_nofork_applet" to find more users).
50
51This poses much more serious limitations on what applet can do:
Denis Vlasenko0d058362007-04-11 16:16:41 +000052
53* all NOEXEC limitations apply.
54* do not ever exit() or exec().
55 - xfuncs are okay. They are using special trick to return
56 to the caller applet instead of dying when they detect "x" condition.
57 - you may "exit" to caller applet by calling xfunc_die(). Return value
58 is taken from xfunc_error_retval.
59 - fflush_stdout_and_exit(n) is ok to use.
60* do not use shared global data, or save/restore shared global data
Denys Vlasenkob72baeb2011-02-02 18:38:57 +010061 (e.g. bb_common_bufsiz1) prior to returning.
Denis Vlasenko0d058362007-04-11 16:16:41 +000062 - getopt32() is ok to use. You do not need to save/restore option_mask32,
63 it is already done by core code.
64* if you allocate memory, you can use xmalloc() only on the very first
65 allocation. All other allocations should use malloc[_or_warn]().
66 After first allocation, you cannot use any xfuncs.
Denis Vlasenkobcd5fc12008-01-06 06:27:17 +000067 Otherwise, failing xfunc will return to caller applet
68 without freeing malloced data!
Denis Vlasenko0d058362007-04-11 16:16:41 +000069* All allocated data, opened files, signal handlers, termios settings,
70 O_NONBLOCK flags etc should be freed/closed/restored prior to return.
71* ...
72
73NOFORK applets give the most of speed advantage, but are trickiest
74to implement. In order to minimize amount of bugs and maintenance,
75prime candidates for NOFORK-ification are those applets which
76are small and easy to audit, and those which are more likely to be
77frequently executed from shell/find/xargs, particularly in shell
78script loops. Applets which mess with signal handlers, termios etc
79are probably not worth the effort.
80
81Any NOFORK applet is also a NOEXEC applet.
Denys Vlasenkob72baeb2011-02-02 18:38:57 +010082
83
84 Relevant CONFIG options
85
86FEATURE_PREFER_APPLETS
87 BB_EXECVP(cmd, argv) will try to exec /proc/self/exe
88 if command's name matches some applet name
89 applet tables will contain NOFORK/NOEXEC bits
90 spawn_and_wait(argv) will do NOFORK/NOEXEC tricks
91
92FEATURE_SH_STANDALONE (needs FEATURE_PREFER_APPLETS=y)
93 shells will try to exec /proc/self/exe if command's name matches
94 some applet name
95 shells will do NOEXEC trick on NOEXEC applets
96
97FEATURE_SH_NOFORK (needs FEATURE_PREFER_APPLETS=y)
98 shells will do NOFORK trick on NOFORK applets