Lars Wirzenius: Anecdotes, 2005


Thursday, September 29, 2005

Anecdotes: Buggy zero length /bin/true

Back when I was young, and Usenet was only about a decade old, I read somewhere the story of two bugs being filed against a zero length implementation of /bin/true. I have since not been able to find the story again, but let me re-tell it this once in my web log, perhaps someone will find the original story, or proof that I hallucinated.

Some technical background: /bin/true is a Unix command that does nothing, and then returns an exit code of zero, which is interpreted by the shell as "success". It can be implemented in various ways, such as this:

exit 0

This is a shell script that does nothing, except exit with an exit code of zero. Works perfectly. It can, however, be shortened. The second line is not necessary: if there is no explicit exit command, the shell will exit with the exit code of the last command it executed, or zero if it didn't execute any commands. We can thus just remove the line. The first line instructs the Unix kernel to start the default shell to execute the file; as it happens, the default shell is the default interpreter, if the kernel can't find anything else to run a file with, and so we can remove the first line as well. We get a file with a filename, execute permissions, and no content. This is a working implementation of /bin/true. It has, however, two bad bugs.

The first bug is that it is too slow. Starting up a shell can take quite a lot of time, relatively speaking, and since /bin/true happens to be a command that gets executed quite often in a Unix system, the startup time of the shell can have a measureable impact on system performance.

The other bug is that the shell takes up too much memory. Especially back when I read the story fifteen years ago, and many slightly older systems still didn't use very sophisticated virtual memory systems, the memory used by the shell could, again, have a significant impact, at least on heavily used systems.

The solution is to rewrite the zero length shell script as a short C program that, ta-daa, does nothing.

Tuesday, April 26, 2005

Anecdotes: -bash: cd: /home/liw: No such file or directory

Public confession of the embarrassing things you do is good for your soul and builds your character.

I have a chroot environment in /srv/chroot/sid for testing various things in. It's something where I can mess more freely than what pbuilder allows. Today I wanted to clean it up, so I figured I'd remove it and start from scratch: rm -rf /srv/chroot/sid. Unfortunately, I had forgotten I had bind-mounted /home inside the chroot, so I managed to remove my home directory as well. Oops.

Luckily, I only use the machine for testing and compilation, so nothing important was actually lost. A few directories with unpacked and built source trees for some packages that will take a few hours to rebuild, such as glibc, but the machine can just do that on its own while I, for example, sleep. Still, I should have been more careful.