Mirror of git://git.busybox.net/busybox with our patches on top
Source
xxxxxxxxxx
run_nofork_applet() saves/inits/restores option parsing, xfunc_error_retval,
NOEXEC and NOFORK applets.
Unix shells traditionally execute some commands internally in the attempt
to dramatically speed up execution. It will be slow as hell if for every
"echo blah" shell will fork and exec /bin/echo. To this end, shells
have to _reimplement_ these commands internally.
Busybox is unique in this regard because it already is a collection
of reimplemented Unix commands, and we can do the same trick
for speeding up busybox shells, and more. NOEXEC and NOFORK applets
are exactly those applets which are eligible for these tricks.
Applet will be subject to NOFORK/NOEXEC tricks only if it is marked
as such in applets.src.h or in their inline "//applet:" directives.
In C, if you want to call a program and wait for it, use
spawn_and_wait(argv), BB_EXECVP(prog,argv) or BB_EXECLP(prog,argv0,...).
They check whether program name is an applet name and optionally
do NOFORK/NOEXEC thing depending on configuration.
Relevant CONFIG options
FEATURE_PREFER_APPLETS
Globally enables NOFORK/NOEXEC tricks for such programs as xargs
and find:
BB_EXECVP(cmd, argv) will try to exec /proc/self/exe
if command's name matches some applet name;
spawn_and_wait(argv) will do NOFORK/NOEXEC tricks
//TODO: the above two things probably should have separate options?
FEATURE_SH_STANDALONE
shells will try to exec /proc/self/exe if command's name matches
some applet name; shells will do NOEXEC trick on NOEXEC applets
//TODO: split (same as for PREFER_APPLETS)
FEATURE_SH_NOFORK
shells will do NOFORK trick on NOFORK applets
NB: shell builtins use these tricks regardless of FEATURE_SH_STANDALONE,
FEATURE_PREFER_APPLETS or FEATURE_SH_NOFORK. In effect, builtins
are "always NOFORK".
NOEXEC
NOEXEC applet should work correctly if another applet forks and then
executes exit(<applet>_main(argc,argv)) in the child. The rules
roughly are:
* do not expect shared global variables/buffers to be in their
"initialized" state. Examples: xfunc_error_retval can be != 1,
bb_common_bufsiz1 can be scribbled over, ...
(although usually xfunc_error_retval's state is not a problem).
* do not expect that stdio wasn't used before. Calling set[v]buf()
can be disastrous.
* ...
NOEXEC applets save only one half of fork+exec overhead.
NOEXEC trick is disabled for NOMMU build.