nofork_noexec.txt 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. NOEXEC and NOFORK applets.
  2. Unix shells traditionally execute some commands internally in the attempt
  3. to dramatically speed up execution. It will be slow as hell if for every
  4. "echo blah" shell will fork and exec /bin/echo. For this end, shells
  5. have to _reimplement_ these commands internally.
  6. Busybox is unique in this regard because it already is a collection
  7. of reimplemented Unix commands, and we can do the same trick
  8. for speeding up busybox shells, and more. NOEXEC and NOFORK applets
  9. are exactly those applets which are eligible for these tricks.
  10. Applet will be subject to NOFORK/NOEXEC tricks if it is marked as such
  11. in applets.h. CONFIG_FEATURE_PREFER_APPLETS is a config option which
  12. globally enables usage of NOFORK/NOEXEC tricks.
  13. If you want to call a program and wait for it, use spawn_and_wait(argv).
  14. It will check whether argv[0] is an applet name and will optionally
  15. do NOFORK/NOEXEC thing.
  16. NOEXEC
  17. NOEXEC applet should work correctly if another applet forks and then
  18. executes exit(<applet>_main(argc,argv)) in the child. The rules
  19. roughly are:
  20. * do not expect shared global variables/buffers to be in their
  21. "initialized" state. Examples: xfunc_error_retval can be != 1,
  22. bb_common_bufsiz1 can be scribbled over, ...
  23. * do not expect that stdio wasn't used before. Calling set[v]buf()
  24. can be disastrous.
  25. * ...
  26. NOEXEC applets save only one half of fork+exec overhead.
  27. NOEXEC trick is disabled for NOMMU compile.
  28. NOFORK
  29. NOFORK applet should work correctly if another applet simply runs
  30. <applet>_main(argc,argv) and then continues with its business (xargs,
  31. find, shells can do it). This poses much more serious limitations
  32. on what applet can/cannot do:
  33. * all NOEXEC limitations apply.
  34. * do not ever exit() or exec().
  35. - xfuncs are okay. They are using special trick to return
  36. to the caller applet instead of dying when they detect "x" condition.
  37. - you may "exit" to caller applet by calling xfunc_die(). Return value
  38. is taken from xfunc_error_retval.
  39. - fflush_stdout_and_exit(n) is ok to use.
  40. * do not use shared global data, or save/restore shared global data
  41. prior to returning. (e.g. bb_common_bufsiz1 is off-limits).
  42. - getopt32() is ok to use. You do not need to save/restore option_mask32,
  43. it is already done by core code.
  44. * if you allocate memory, you can use xmalloc() only on the very first
  45. allocation. All other allocations should use malloc[_or_warn]().
  46. After first allocation, you cannot use any xfuncs.
  47. * All allocated data, opened files, signal handlers, termios settings,
  48. O_NONBLOCK flags etc should be freed/closed/restored prior to return.
  49. * ...
  50. NOFORK applets give the most of speed advantage, but are trickiest
  51. to implement. In order to minimize amount of bugs and maintenance,
  52. prime candidates for NOFORK-ification are those applets which
  53. are small and easy to audit, and those which are more likely to be
  54. frequently executed from shell/find/xargs, particularly in shell
  55. script loops. Applets which mess with signal handlers, termios etc
  56. are probably not worth the effort.
  57. Any NOFORK applet is also a NOEXEC applet.