About the Android toolbox binary

1. What is Android toolbox ?

While browsing the filesystem of my Android emulator, I was surprised to discover that most of the items in the /system/bin folder are actually symlinks to the toolbox binary:


C:\android-sdk\platform-tools>adb shell
# ls -l /system/bin
ls -l /system/bin
[SNIP]
lrwxr-xr-x root shell 2012-08-23 07:01 cat -> toolbox
-rwxr-xr-x root shell 129416 2012-08-23 07:00 check_prereq
lrwxr-xr-x root shell 2012-08-23 07:01 chmod -> toolbox
lrwxr-xr-x root shell 2012-08-23 07:01 chown -> toolbox
[SNIP]
-rwxr-xr-x root shell 181002 2012-08-23 07:01 toolbox
[SNIP]
#

So… what is this toolbox binary ?

Toolbox is like a “swiss army knife”: it allows you to run multiple commands from a single binary. So instead of having different binaries for each standard Unix command (like ls or rm), you can have a single binary (toolbox) that can execute all these commands.

To use it, simply execute “toolbox XX” instead of “XX”. So to list the content of the “/system/bin” folder, I could have used “toolbox ls -l /system/bin”:


# toolbox ls -l /system/bin
toolbox ls -l /system/bin
[SNIP]
lrwxr-xr-x root shell 2012-08-23 07:01 cat -> toolbox
-rwxr-xr-x root shell 129416 2012-08-23 07:00 check_prereq
lrwxr-xr-x root shell 2012-08-23 07:01 chmod -> toolbox
lrwxr-xr-x root shell 2012-08-23 07:01 chown -> toolbox
[SNIP]
-rwxr-xr-x root shell 181002 2012-08-23 07:01 toolbox
[SNIP]
#

Check the toolbox git repository to see which commands can be used.

A popular alternative to toolbox is BusyBox, which provides even more functionnalities.

2. How do toolbox symlinks work ?

As explained above, the “ls” command is actually a symlink that references toolbox:


# ls -l /system/bin/ls
ls -l /system/bin/ls
lrwxr-xr-x root shell 2012-08-23 07:01 ls -> toolbox

The question is: since we have dozens of symlinks that reference toolbox…how does toolbox know which symlink was originally executed ?

It turns out that is actually easy to know, and I’m going to write a Linux bash script showing how you can reproduce this behavior.

First, I have a “script.sh” script and 2 symlinks (named “cmd1″ and “cmd2″) that reference this script:


[laurent@localhost toolbox_example]$ ll
total 4
lrwxrwxrwx. 1 laurent laurent 9 Nov 22 11:18 cmd1 -> script.sh
lrwxrwxrwx. 1 laurent laurent 9 Nov 22 11:18 cmd2 -> script.sh
-rwxrwxr-x. 1 laurent laurent 211 Nov 22 11:22 script.sh

And here is the content of my script:


[laurent@localhost toolbox_example]$ cat script.sh
#!/usr/bin/bash
command=`basename $0`
case "$command" in
cmd1) echo "executing cmd1 with args=[$*]"
 ;;
cmd2) echo "executing cmd2 with args=[$*]"
 ;;
*) echo "Invalid option"
 ;;
esac

Let’s see this in action:


[laurent@localhost toolbox_example]$ ./cmd1 arg1
executing cmd1 with args=[arg1]
[laurent@localhost toolbox_example]$ ./cmd2 arg2
executing cmd2 with args=[arg2]

Of course, this concept can be easily ported to C/C++.

Any questions ?

Laurent KUBASKI

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: