Shortly after I got started with embedded systems, it took me half a day to look at busybox. Next I will perform a superficial analysis on the principle of busybox (I have not found any relevant content on the Internet ).
1. IntroductionBusybox is a Swiss Army knife developed in Embedded mode. It is practical, short, and stable.
- Busybox uses the fact that the Code in the Linux Utility is a large number of duplicates to reorganize the duplicated code and put it into a file, reducing the number of times of inclusion.
- Busybox simplifies unnecessary complex functions to reduce the occupied space.
- Busybox is fully customizable and provides a flexible and scalable structure.
2. Basic busybox usageYou can see two usage methods in the original code docs:
Busybox [Arg...] For example:/bin/busybox ls, the LS function will be executed.
[Arg...] For example: ln-S/bin/busybox ls./ls will also execute the LS command
3. busybox source code analysisBusybox source code is a relatively large project, but the project can be divided into three parts as a whole:
The busybox architecture provides basic support for running busybox. The main code is in the applet. Busybox. C contains the main function of busybox entry, which calls the applet for processing call parameters. the run_applet_by_name function in C. Based on the applet name, find the corresponding applet, execute the function directed to bb_applet-> main, and exit directly. The function executed by bb_applet-> main is the command to be executed through the command. In run_applet_by_name, The called find_applet_by_name uses bsearch to search for applets, and returns the applet. Applets definition in include/applets. H, which is a constant array.
- (2) busybox utility Library
The Reusable functions of busybox are defined in the files under libbb. Other applets call these functions to achieve their own goals.
- (3) extended busybox Applet
Busybox itself does not have much practical value. More importantly, the applet of busybox provides us with practical functions. Busybox applets is scattered in various directories of the source code by function. Busybox itself is also an applet. its definition is in busybox. in C, the entry point is busybox_main. example: CP command implementation, CP is placed under coreutils CP. in C, we can see that there is only one function in the file, cp_main (). This function is the entry address of the CP command, and the most critical step of copy is the implementation of do_copy, it is implemented by calling copy_file. The copy_file function is used by multiple commands, such as mV. CP. It is stored in copyfile. C of libbb.
4. script analysis in the busybox Source
(1) applets/busybox. mkll. This script analyzes the include/config. h and inlcude/applets. H files to get links to the configured files.
(2) applets/install. Sh. The script creates a link file based on the busybox. Link generated by busybox. mkll.
(3) makefile in the source code Directory provides make menuconfig to configure the source code, generate the. config file, and make will generate busybox.
5. Extended busybox Functions
See docs/new_applet-HOWTO.txt documentation
(1) Compile the applet code in the appropriate directory
(2) Add the corresponding applet configuration in the makefile. In file in the directory
(3) Add the graphical configuration of the applet In the config. In file in the directory.
(4) Add the corresponding usage description in include/usage. h.
(5) Add the corresponding Applet in inlucde/applet. H. Make sure that it is correctly sorted by the parent.
6. OthersBusybox itself is not configured for many times in order to reduce the time. For example, if you want to change the telnetd port, you usually need to repair the source code and then re-compile it into busybox.
Related: http://kevinasia.spaces.live.com/blog/cns! 30c77c959067468f! 169. Entry