busybox添加自定义applet
本文基于:busybox-1.32.0
添加自定义applet的说明请参考busybox源码下的 docs/new_applet-HOWTO.txt 文档
1. 创建applet目录并编写代码
~/projects/busybox-1.32.0$ mkdir myapp
~/projects/busybox-1.32.0$ cd myapp
~/projects/busybox-1.32.0/myapp$ vi myapp.c
myapp.c
#include "busybox.h"
int myapp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int myapp_main(int argc, char *argv[])
{
printf("Hello myapp!!!\n");
return 0;
}
Config.src
menu "Myapp"
config MYAPP
bool "my app"
default y
select PLATFORM_LINUX
help
This is a demo app
INSERT
endmenu
添加完该文件的目的是在make menuconfig菜单里能够看到myapp选项,并在.config文件里生成对应的CONFIG_MYAPP=y。
到目前为止,make还无法知道myapp.c的存在,需要添加Kbuild.src文件并将myapp加入其中:
Kbuild.src
lib-y:=
lib-$(CONFIG_MYAPP) += myapp.o
INSERT
2. 将myapp添加到Makefile
步骤1只是完成了myapp代码及编译相关文件的编写,但并未告知到busybox的Makefiel, 所以我们要讲步骤1中新增的myapp加入到Makefile:
~/tools/busybox-1.32.0$ vi Makefile
~/tools/busybox-1.32.0$ vi Config.in

3. 在include/usage.src.h 中增加相应的 usage 说明
~/tools/busybox-1.32.0$ git diff include/usage.src.h
diff --git a/include/usage.src.h b/include/usage.src.h
index d22efd3..e2d12ad 100644
--- a/include/usage.src.h
+++ b/include/usage.src.h
@@ -25,6 +25,9 @@
" (default "CONFIG_FEATURE_DEFAULT_PASSWD_ALGO")"
#endif
+#define myapp_trivial_usage "None"
+#define myapp_full_usage "None"
+
INSERT
#define busybox_notes_usage \

4. 在 include/applets.src.h中增加相应的 applet
~/tools/busybox-1.32.0$ git diff include/applets.src.h
diff --git a/include/applets.src.h b/include/applets.src.h
index 60968ce..e39a13e 100644
--- a/include/applets.src.h
+++ b/include/applets.src.h
@@ -85,6 +85,7 @@ s - suid type:
# define BB_DIR_USR_SBIN BB_DIR_SBIN
#endif
+IF_MYAPP(APPLET(myapp, BB_DIR_SBIN, BB_SUID_DROP))
INSERT
这会在对应的include/applets.h文件中生成myapp相关的信息:
5. 编译生成myapp
生成新的.config文件,该文件里会包含:
CONFIG_MYAPP=y
再make && make install即可生成myapp.
~/tools/busybox-1.32.0/_install/sbin$ ll myapp
lrwxrwxrwx 1 zhangsan zhangsan 14 Sep 11 13:38 myapp -> ../bin/busybox*
6. 运行myapp