Checking for and handling BusyBox

When developing software in shell (lol) you must consider the different platforms that you are running on, and the versions of the common utilities you may be using. When it comes to software running on embedded devices, it’s common for these to be provided by a monolithic BusyBox binary has common tools in /bin/ symlinked to BusyBox. BusyBox determines which utility you are calling by checking the $0 cmd argument positional.

The problem

Some platforms use soft links, and some use hard. How do we determine whether the utility we are calling is the real one, or a BusyBox function?

Detection strategies

  1. Check for symlink using readlink

  2. List the inode, it will match if this is a hardlink

On some systems, the links to Busybox are hardlinks

1
2
admin@(none):/jffs/scripts/mosshe# ls -i $(which busybox)
5087 /jffs/bin/busybox
1
2
admin@(none):/jffs/scripts/mosshe# ls -i $(which ls)
5087 /jffs/bin/ls

The resulting detection function

Features

  • checks for hardlink
  • checks for softlink
  • has builtin test

The one blind spot here is that stat may not be available on the given system. This information is also available in the ls command, but that cmd output can change and will be non-standard if it’s BusyBox, and relying on that is risky. I’ll probably implement it anyway.