android toolbox类似busybux方法让一个可执行程序看起来就像是很多可执行程序一样。
请参考本人转的busybox文章:《BusyBox 简化嵌入式 Linux 系统》。
http://blog.csdn.net/tommy_wxie/article/details/7340639
该文章解析busybox实现原理如下:
清单 1. C 的 main 函数
int main( int argc, char *argv[] ) |
在这个定义中,argc
是传递进来的参数的个数(参数数量),而 argv
是一个字符串数组,代表从命令行传递进来的参数(参数向量)。argv
的索引 0 是从命令行调用的程序名。
清单 2 给出的这个简单 C 程序展示了 BusyBox 的调用。它只简单地打印 argv
向量的内容。
argv[0]
来确定调用哪个应用程序
// test.c #include <stdio.h> int main( int argc, char *argv[] ) { int i; for (i = 0 ; i < argc ; i++) { printf("argv[%d] = %s\n", i, argv[i]); } return 0; } |
调用这个程序会显示所调用的第一个参数是该程序的名字。我们可以对这个可执行程序重新进行命名,此时再调用就会得到该程序的新名字。另外,我们可以创建一个到可执行程序的符号链接,在执行这个符号链接时,就可以看到这个符号链接的名字。
$ <strong>gcc -Wall -o test test.c</strong> $ <strong>./test arg1 arg2</strong> argv[0] = ./test argv[1] = arg1 argv[2] = arg2 $ <strong>mv test newtest</strong> $ <strong>./newtest arg1</strong> argv[0] = ./newtest argv[1] = arg1 $ <strong>ln -s newtest linktest</strong> $ <strong>./linktest arg</strong> argv[0] = ./linktest argv[1] = arg |
BusyBox 使用了符号链接以便使一个可执行程序看起来像很多程序一样。对于 BusyBox 中包含的每个工具来说,都会这样创建一个符号链接,这样就可以使用这些符号链接来调用 BusyBox 了。BusyBox 然后可以通过argv[0]
来调用内部工具。
如下:
out/target/product/xxxx/obj/EXECUTABLES/toolbox_intermediates/tools.h
/* file generated automatically */
TOOL(ls)
TOOL(mount)
TOOL(cat)
TOOL(ps)
TOOL(kill)
TOOL(ln)
TOOL(insmod)
TOOL(rmmod)
TOOL(lsmod)
TOOL(ifconfig)
TOOL(setconsole)
TOOL(rm)
TOOL(mkdir)
TOOL(rmdir)
TOOL(getevent)
TOOL(sendevent)
TOOL(date)
TOOL(wipe)
TOOL(sync)
参考:Android.mk
TOOLS_H := $(intermediates)/tools.h
$(TOOLS_H): PRIVATE_TOOLS := $(ALL_TOOLS)
$(TOOLS_H): PRIVATE_CUSTOM_TOOL = echo "/* file generated automatically */" > $@ ; for t in $(PRIVATE_TOOLS) ; do echo "TOOL($$t)" >> $@ ; done
二、自动生成命令工具链接到toolbox
adb shell 查看这些工具命都是链接到toolbox
root@S850:/system/bin # ll touch
lrwxr-xr-x root shell 2014-04-01 21:44 touch -> toolbox
root@S850:/system/bin # ll rm
lrwxr-xr-x root shell 2014-04-01 21:44 rm -> toolbox
root@S850:/system/bin # ll mv
lrwxr-xr-x root shell 2014-04-01 21:44 mv -> toolbox
参考:Android.mk
# Make #!/system/bin/toolbox launchers for each tool.
#
SYMLINKS := $(addprefix $(TARGET_OUT)/bin/,$(ALL_TOOLS))
$(SYMLINKS): TOOLBOX_BINARY := $(LOCAL_MODULE)
$(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk
@echo "Symlink: $@ -> $(TOOLBOX_BINARY)"
@mkdir -p $(dir $@)
@rm -rf $@
$(hide) ln -sf $(TOOLBOX_BINARY) $@
ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS)
# We need this so that the installed files could be picked up based on the
# local module name
ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \
$(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS)
argv[0]
来调用内部工具