Plan 9's Advantage

Posted by Daniel Lyons on August 7, 2009

Plan 9 is Unix sans castration.

The central idea behind Plan 9 is that a filesystem is a dandy interface to all kinds of things. Take a look at the list of system calls in Plan 9, all 51 of them.

What kinds of things can be filesystems that typically aren't? All your processes—post a note (remember signals in Unix?) by writing to their control file, see what files they have open, etc. Your network interface. Need NAT? Mount the gateway's network devices locally. Get a connection and listen or connect to a remote host by opening a file and writing to it. Access a database through the filesystem. Access the web through the filesystem. Access FTP or any other remote filesystem through the local filesystem. Manipulate this window on the screen through the filesystem. Draw graphics through the filesystem. Access your keychain through the filesystem. Access your sound card through the filesystem.

Exposing everything through the filesystem has three principle benefits:

  1. Now any aspect of your machine's hardware, display, or any running software can be accessed from other places on the network. Or vice versa.
  2. Want to improve security by jailing a process? Remove the interfacing files from the namespace of that process.
  3. Access to your abstraction is democratized and language-agnostic.

The first two points get made a lot in Plan 9 circles and are the backbone of a secure distributed environment. But the third point is one that's been fascinating me. The echo service on Plan 9 is a shell script that runs cat. You can use regular shell scripts to connect programs and devices across machines. If you find a filesystem that provides access to a given device, protocol, or whatever, every language on your machine gets uniform access to that device, protocol or whatever without any bias towards a particular one.

This also means that in Plan 9, writing a filesystem is better in many cases than writing a library. A library can only be used from C; a filesystem can be used from C, the shell, awk or any other language. Furthermore, a filesystem can be exported over the network or imported from another host. Hooking up a file to another file doesn't sound very interesting, but if one file is the output of a progress bar application and the other is your soundcard's control file, it becomes interesting. Or if one file is your mail and the other is your Jabber account, and between them is a little AWK script that pulls out the subject. Or if one file is your hard drive's SMART status and the other is your SMS gateway. The mere thought of being able to combine these things with trivial shell scripts in the grand Unix tradition is quite alluring.