初识Android下的busybox与toolbox

初识Android下的busybox与toolbox,第1张

概述2020-03-25关键字: 原来Android中的busybox与toolbox是两套程序。 busybox是一个嵌入式领域常用的软件。它是一个命令集工具,像传统的PC端的Linux系统上的大多数命令的实现都被封装在busybox程序中。在嵌入式平台上就可以将这些命令以参数的形式传递给busybox工

2020-03-25

关键字:


 

原来 AndroID 中的 busyBox 与 toolBox 是两套程序。

 

busyBox 是一个嵌入式领域常用的软件。它是一个命令集工具,像传统的PC端的linux系统上的大多数命令的实现都被封装在 busyBox 程序中。在嵌入式平台上就可以将这些命令以参数的形式传递给 busyBox 工具集,进而实现相应的功能。

 

那为什么嵌入式平台不能直接像PC端那样,直接将各个命令所对应的程序预置在板端,而非要包装在一个 busyBox 中呢?其最主要的原因还是因为这些命令所对应的程序加起来太过庞大,嵌入式平台的存储资源通常是比较有限的,为了节约存储空间,就将这些命令集合在一个程序中。那集合以后的程序又凭什么能比PC端那种分散开来的形式体积要小呢?一个主要的原因是因为这些命令中,有不少命令的实现都是相同的。busyBox 就将这些相同的部分提取出来让多个命令共用。如此一来随着命令数量的增多,就能比较大程度地缩减程序体积了。

 

AndroID平台是基于嵌入式linux的。通过串口或者 adb shell 可以像在嵌入式linux平台上那样敲击命令以实现某些目的。

 

AndroID平台的命令系统一般而言可以认为分为两类:

1、busyBox 实现的命令系统

2、toolBox 实现的命令系统

 

toolBox 命令系统是AndroID平台默认的命令系统。

 

例如,我们可以像在PC端的linux系统shell上那样直接敲击常见linux命令:

root@rk312x:/ # lldrwxr-xr-x root     root              1970-01-01 08:00 acctdrwxrwx--- system   cache             1970-01-01 16:00 cache-rwxr-x--- root     root       264120 1970-01-01 08:00 chargerdr-x------ root     root              1970-01-01 08:00 configroot@rk312x:/ # pwd/root@rk312x:/ # cdroot@rk312x:/data #root@rk312x:/data # grep -nr "yes" /system/bin/                                Binary file /system/bin/busyBox matchesBinary file /system/bin/cat matchesBinary file /system/bin/chcon matches

这里的每一条命令,其实都对应着一个应用程序,它们位于 /system/bin 目录下:

root@rk312x:/ # ll /system/bin/                                                lrwxr-xr-x root     shell             2020-03-21 18:10 cat -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:10 chcon -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:10 chmod -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:10 chown -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 clear -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 cmp -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 cp -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 date -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 dd -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 df -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 dmesg -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 du -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 getenforce -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 getevent -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 getprop -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 getsebool -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 grep -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 hd -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 ID -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 ifconfig -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 iftop -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 insmod -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 ioctl -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 ionice -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 kill -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 ln -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 load_policy -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 log -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 ls -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 lsmod -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 lsof -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 md5 -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 mkdir -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 mkswap -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 mount -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 mv -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 nandread -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 netstat -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 newfs_msdos -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 notify -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 printenv -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 ps -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 r -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 readlink -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 renice -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 restorecon -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 rm -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 rmdir -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 rmmod -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 route -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 runcon -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 schedtop -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 sendevent -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 setconsole -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 setenforce -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 setprop -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 setsebool -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 sleep -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 smd -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 start -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 stop -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 swapoff -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 swapon -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 sync -> toolBox-rwxr-xr-x root     root       139048 2020-03-25 22:18 toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 top -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 touch -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 umount -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 uptime -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 vmstat -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 watchprops -> toolBoxlrwxr-xr-x root     shell             2020-03-21 18:05 wipe -> toolBox

只是每一条命令都只是 /system/bin/toolBox 程序的“影分身”而已。

 

由此可见,这个 AndroID 平台的 toolBox 与嵌入式linux平台下的 busyBox 一样,是一个命令工具集。我们在串口控制台或 adb shell 上敲命令的时候其实就是在执行这些程序。而这些程序又由于是 toolBox 的分身,就会将这些程序名称,也即命令内容作为程序启动参数传递给 toolBox 的 main(int argc, char **argv) 函数,随后 toolBox 再根据不同的命令执行不一样的动作。

 

toolBox 的源码位于AndroID工程源码的以下目录:

./system/core/toolBox

 

toolBox 程序的入口函数位于 toolBox.c 中:

 

这份源码本质上并不复杂,有兴趣的同学可以自行去研究研究。同样的,如果你有特殊需求,也完全可以在这里添加自己的命令程序。

 

busyBox 一般来说都是直接预置可执行程序在AndroID系统源码中的,并不像 toolBox 那样会有源代码。

 

不过 busyBox 有官方网站,上面刊载了各版本的源码与文档等资料:

下载地址:http://www.busyBox.net/

 

在 AndroID 中,busyBox 并不像 toolBox 那样将各命令都以软链接的形式释放出来。

 

如果想使用 busyBox 的命令系统,则必须将命令以启动参数的形式传递给 busyBox 程序:

busyBox ls -albusyBox pwdbusyBox find -name "*.apk"

 

我们也可以通过下面这条命令列出 busyBox 支持的所有命令:

busyBox --List

 

busyBox 命令系统与 toolBox 命令系统在表现形式上可能是不一样的,例如,同样是 ls -al 命令,二者所列举的信息却是有所不同的:

root@rk312x:/sdcard # ls -aldrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 .androID_securedrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 Alarmsdrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 DCIMdrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 Downloaddrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 LOST.DIRdrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 MovIEsdrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 Musicdrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 Notificationsdrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 Picturesdrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 podcastsdrwxrwxr-x system   sdcard_rw          1980-01-01 00:00 ringtonesroot@rk312x:/sdcard # root@rk312x:/sdcard # busyBox ls -al                                           total 96drwxrwxr-x   13 system   sdcard_r      8192 Jan  1 08:00 .drwxrwxr-x    9 root     system           0 Jan  1 08:00 ..drwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 .androID_securedrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 Alarmsdrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 DCIMdrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 Downloaddrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 LOST.DIRdrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 MovIEsdrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 Musicdrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 Notificationsdrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 Picturesdrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 podcastsdrwxrwxr-x    2 system   sdcard_r      8192 Jan  1  1980 ringtones

因此,如果我们发现某个命令不太能符合需求,则可以尝试一下使用另一套命令系统。同时,两套命令系统所包含的命令也会有差异。如,toolBox 就不包含 find 命令,而 busyBox 却支持 find 命令。

 

最后再额外提一点:在AndroID平台,无论是串口控制台还是adb的shell,其本质都是在一个登录了的shell程序中运行。就像PC端linux的 shell 机制一样。AndroID平台上默认的shell程序是 /system/bin/sh 程序。在 init.rc 中可以发现串口控制台其实就是一个 /system/bin/sh 程序的别名:

 

而 adb 的 shell 就更不用说了。

 


 

总结

以上是内存溢出为你收集整理的初识Android下的busybox与toolbox全部内容,希望文章能够帮你解决初识Android下的busybox与toolbox所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1064552.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存