Add a command to the BusyBox

Source: Internet
Author: User

Note: The personal use of the BusyBox is 1.21.0 which kbuild config.in usage.h applets.h are all by Gen Kbuild.src, so modify the files generated by the file instead of CONFIG.SRC D and so on. There are instructions in the comments, other references to the other commands to write.

The first of these methods

Adding a new command to BusyBox is simple because it has a well-defined architecture. The first step is to select a location for the source code for the new command. We want to select a location based on the type of command (network, shell, etc.) and keep it consistent with other commands. This is important because the new command will eventually appear in the Menuconfig configuration menu (in the following example, the Miscellaneous Utilities menu).

For this example, I called the new command Newcmd and put it in the./miscutils directory. The source code for this new command is shown in Listing 13.
listing 13. Source code for new commands integrated into BusyBox

#include "busybox.h"

int newcmd_main (int argc, char *argv[])
{
  int i;

  printf ("Newcmd called:\n");

  for (i = 0; i < argc i++) {

    printf ("arg[%d] =%s\n", I, Argv[i]);

  return 0;
}

Next, we will add the source code of this new command to the makefile.in in the selected subdirectory. In this case, I have updated the./miscutils/makefile.in file. Add new commands in alphabetical order to maintain consistency with existing commands:
listing 14. Add a command to the makefile.in

miscutils-$ (CONFIG_MT)          + = MT.O
miscutils-$ (config_newcmd)   + = NEWCMD.O
miscutils-$ ( Config_runlevel)    + = RUNLEVEL.O

Next, update the configuration files in the./miscutils directory to make the new commands visible during configuration. This file is named Config.in, and the new commands are added alphabetically:
listing 15. Add a command to the config.in

Config config_newcmd
 bool "Newcmd"
 default n
 help
   Newcmd is a new test command.

This structure defines a new configuration item (through the CONFIG keyword) and a configuration option (Config_newcmd). The new commands can be enabled or disabled, so we use the bool (Boolean) value for the configured menu properties. This command is disabled by default (n means no) and we can finally put a short help description. In the./scripts/config/kconfig-language.txt file of the source tree, we can see the complete grammar of the configuration syntax.

Next you need to update the./include/applets.h file so that it contains this new command. Add the following line to this file, remember to follow the alphabetical order. It is important to maintain this order, otherwise our commands will not be found.
listing 16. Add a command to the Applets.h

Use_newcmd (APPLET (Newcmd, Newcmd_main, _bb_dir_user_bin, _bb_suid_never))

This defines the command name (newcmd), the function name (Newcmd_main) in the BusyBox source code, where it should create a link for the new command (in which case it is in the/usr/bin directory), and finally whether the command has the right to set the user ID (in this case NO).

The penultimate step is to add detailed help information to the./include/usage.h file. As you can see from the example in this file, the use of information can be very detailed. In this case, I've only added a little bit of information so that I can compile this new command:
listing 17. Add Help information to usage.h

#define Newcmd_trivial_usage "None"
#define Newcmd_full_usage "None"

The final step is to enable the new command (through make menuconfig, and then enable this option in the Miscellaneousutilities menu) and then compile the BusyBox with do.

With the new BusyBox, we can test this new command, as shown in Listing 18.
listing 18. Test new command

./busybox newcmd arg1
Newcmd called:
arg[0] = newcmd
arg[1] = arg1
./busybox newcmd--help busybox v1.1.1
( 2006.04.12-13:47+0000) multi-call binary

usage:newcmd none

None

That's it. BusyBox developers have developed an excellent but very easy to scale tool.

The second method

A few days to add a new command to the BusyBox (1.16.1 version) Finally, the progress of Ah, the use of different versions of the Internet is really not the same. In fact, as long as in the relevant documents to see other commands in the writing can be based on gourd painting ladle, I have been busy this long. Every time there are problems Google Baidu, but the essence of the same, the solution is not I encountered problems, very frustrated. Very happy ~ finally has the result, very simple procedure very simple method Oh ~
For example: The new command is called Newcmd, and it is placed in the./miscutils directory first, write or rewrite the source code for the new command, ensuring that the command corresponds to the main function: int cmdname_main (int argc, char **argv );
#include "libbb.h" #include <stdio.h>
int newcmd_main (int argc, char **argv) {int i;   printf ("Newcmd called:\n");   for (i = 0; i < argc i++) {printf ("arg[%d] \ n", i); return 0; }
Then, update the configuration file in the directory where the new command is added so that the command is visible when the configuration is changed.      The configuration file name in the BusyBox1.16.1 version is config.in. Added in this example: Config newcmd bool "newcmd" Default N Help Newcmd is a new test command.
Again, add the command to Applets.h, in this case: If_newcmd (APPLET (Newcmd, _bb_dir_usr_bin, _bb_suid_drop)) This is because the file has a defined macro # define applet (A,b, c) {#a, a# #_main, b,c}.
Next, add help information to the usage.h, in this case: #define NEWCMD_TRIVIAL_USAGE \ None #define Newcmd_full_usage \ None to display when using the command is: $./busybox newcmd--help busybox v1.1.1 (2006.04.12-13:47+0000) multi-call binary usage:newcmd None None
Finally, to change the compiled version of makefile,busybox1.16.1 in the directory where the new add command is named Kbuild, add the statement in this example: lib-$ (config_newcmd) +=NEWCMD.O
This completes the process of adding a new command, and then simply configure, compile, and install it.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.