embedded-scripts.txt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Embedded Shell Scripts in BusyBox
  2. =================================
  3. BusyBox allows applets to be implemented as shell scripts. Since
  4. this obviously requires a shell to interpret the scripts the feature
  5. depends on having a shell built into the binary. Either ash or hush
  6. will do. If both are present ash will be used. Support for embedded
  7. scripts also has to be enabled.
  8. It's unlikely that your applet will be implemented as a pure shell
  9. script: it will probably need some external commands. If these are
  10. to be provided by BusyBox you'll need to ensure they're enabled too.
  11. There are two ways to include scripts in BusyBox: the quick-and-dirty
  12. custom script and the full-featured scripted applet.
  13. Custom Scripts
  14. --------------
  15. When embedded script support is enabled the BusyBox build process
  16. assumes that any files in the directory 'embed' at the top level of
  17. the source tree are scripts to be embedded.
  18. The embed directory isn't present in the BusyBox source tree and
  19. BusyBox itself will never put anything there: it's entirely for the
  20. use of third parties.
  21. Adding a custom script is as simple as running the following sequence
  22. of commands in the BusyBox source directory:
  23. mkdir embed
  24. echo 'echo foo' >embed/foo
  25. make defconfig
  26. make
  27. The resulting binary includes the new applet foo!
  28. Custom scripts have limited opportunities for configuration: the only
  29. control developers have is to put them in the embed directory, or not.
  30. Everything else takes default values. For more control you need the
  31. additional features provided by scripted applets.
  32. Scripted Applets
  33. ----------------
  34. Suppose we want to make a shell script version of the sample applet
  35. from the New Applet HOWTO. First we'd have to write a script (vaguely)
  36. equivalent to the C code:
  37. return $(($RANDOM%256))
  38. This should be placed in the file applets_sh/mu in the source tree.
  39. Next we need the configuration data. This is very similar to the example
  40. code for the native applet:
  41. //config:config MU
  42. //config: bool "MU"
  43. //config: default y
  44. //config: help
  45. //config: Returns an indeterminate value.
  46. //applet:IF_MU(APPLET_SCRIPTED(mu, scripted, BB_DIR_USR_BIN, BB_SUID_DROP, mu))
  47. //usage:#define mu_trivial_usage
  48. //usage: "[-abcde] FILE..."
  49. //usage:#define mu_full_usage
  50. //usage: "Returns an indeterminate value\n"
  51. //usage: "\n -a First function"
  52. //usage: "\n -b Second function"
  53. The only difference is that the applet is specified as being of type
  54. APPLET_SCRIPTED. It would also be useful to include details of any
  55. dependencies the script has. No external commands are used by our mu
  56. script, but it does depend on optional shell features. We can ensure
  57. these are selected by adding this to the configuration:
  58. //config:config MU_DEPENDENCIES
  59. //config: bool "Enable dependencies for mu"
  60. //config: default y
  61. //config: depends on MU
  62. //config: select ASH_RANDOM_SUPPORT
  63. //config: select FEATURE_SH_MATH
  64. //config: help
  65. //config: mu is implemented as a shell script. It requires support
  66. //config: for $RANDOM and arithmetic.
  67. The configuration data should be placed in a C file in an appropriate
  68. subdirectory. There isn't any C code, though! In this case the file
  69. could be miscutils/mu.c.
  70. Scripted applets are just as configurable as applets written in C.
  71. They can be enabled or disabled using the configuration menu; their
  72. install directory can be specified and their usage messages are stored
  73. along with those of all other applets.
  74. Additional Notes
  75. ----------------
  76. The source for embedded scripts can be displayed by running:
  77. busybox --show SCRIPT
  78. This can be disabled by turning off FEATURE_SHOW_SCRIPT in the
  79. configuration, though it won't prevent a determined user from
  80. extracting the source code.
  81. It can be argued that embedded scripts are linked into the BusyBox
  82. binary and are therefore not subject to the 'mere aggregation'
  83. exception in the GPL. If this is the case embedded scripts should
  84. have a licence compatible with BusyBox's GPL v2-only licence.