9series: Introduction and chmod

Sakis Kasampallis | Jan 29, 2012 min read

Introduction to Plan 9 and writing a simple chmod utility 

I decided to write down some notes about my experiences while playing with Plan 9. Plan 9 is a free software (OSI certified) distributed operating system developed in a place that I would like to visit someday: Bell Labs. Many great programmers have contributed to Plan 9. Among them are the well-known from the UNIX world Ken Thompson and Rob Pike. Yet another respected programming figure, Brian Kernighan, claims to be just a casual user of the system (or at least he did in 1995).

There are many novel ideas behind Plan 9. Some of them have already been reimplemented in other operating systems: The /proc and other in memory pseudo file systems, private group per user, native UTF-8 support, append-only file permissions, and treating FTP directories as local. But there are some Plan 9 features that are simply impossible to implement in the rest systems; or if they are implemented it is done in a very ugly/hacky way, because their design is not flexible enough. To mention a few: Namespaces, no need for root/superuser or sudo-like hacks, every compilation is by default a cross compilation, device files are controlled using clear-text.

Coding in Plan 9

Although Plan 9 provides an ANSI/POSIX compatibility layer (APE), it doesn't make sense to use it for other than porting purposes.The native C API of Plan 9 is neither POSIX nor ANSI compatible, but IMHO it's cleaner and simpler to use. As an example, let's look into the implementation of a simplified chmod utility.

A few comments about the interesting parts of the code. Plan 9 has simple built-in support for handling command line arguments using the ARGBEGIN/ARGEND block. argv0 is a convenient name that can be used for referring to the name of the executable. Applying a new permission mask to a file in Plan 9 is just a matter of updating the mode flag of the file's Dir structure. The result is a nice utility using only a few lines of code. Here's how it can be used:


And that concludes this brief introduction to Plan 9. Until next time... Happy Coding!

References