System Utility Programming

Jason R. Fink

(c) Copyright 2010 All Rights Reserved.

0.1.1 Preface

System utility programming can mean a lot of things. In the context of this book utility programming means writing scripts and programs that interact at a very low level with an Operating System. Further defined: shell and compiled programs that will run in the UNIX shell environment . That is not
to say such programs cannot communicate with higher level software - in fact well written tools that reveal data about system details rely heavily upon low level utilities to collect the information for them and report it back. The very essence of a well written utility is that it provides easy to read data that can be used by virtually any other utility or piece of software on the system.

The Operating Systems used in this book are Linux kernel based or BSD derived flavors of UNIX. UNIX-like systems are used for several reasons:

Of course bullet 3 and 1 are tied but it is true; UNIX and UNIX-like systems have been around since their epoch (1969) for a reason: they have a strong foundation and rich development environment. It is more difficult these days to install a system without Perl for example then the reverse. Most free Operating Systems come pre-loaded with compilers, toolchains, interpreters of all sorts and a variety of shells.

In this book system programming will be looked at from a system adminsitrator's point of view; much of what is written hinges on getting information about the system in a format the user wanted. That said none of the programs are especially large (only a few go beyond one or two pages). This book is designed to help lay a rounded foundation for the potential future programmer and not meant to be an end all resource or even a practical reference manual.

This book is released under the Creative Commons License version 3. Per the license text (which can be found at: http://creativecommons.org/licenses/by-nc-nd/3.0/ ):

You are free to copy, distribute and transmit the work under the following conditions:

With the understanding that:

0.1.2 Who Should Read This Book?

Anyone new to the shell scripting or programming world will likely find some useful information here. System administrators looking to expand their skills or perhaps leverage programming to their advantage. System programmers who work on non-UNIX systems would likely find something interesting as well - in the least perhaps a style contrast.

A solid understanding of basic scripting (shell or Perl) and C programming is required. Remember, this book gives examples in several languages but it will not stop to explain every aspect of each language used: there are plenty of other references for that sort of thing.

The basic skillset for this book is:

0.1.3 How to Use This Book

The examples in the book are just that: real world examples the author had to write for one reason or another. In several of the examples it should be obvious that there is a better (and faster) way to do things. The lscpu program comes to mind; a program written in C which reads a variety of data from the Linux /proc filesystem should have simply been a shell script that greps and awks out what it needed, however, it makes for a good example of how to leverage the file I/O capabilities of C and at the time it seemed like a good idea.

The primary motivation of this book is provide the reader with small compact examples they can use as a springboard for their own endeavors. Readers would be advised to also have available programming references (either online or printed) for some of the trickier corners navigated within the texts.

0.1.4 Related References

Following is an evolving list of excellent programming books:

0.1.5 Conventions Used

I decided to keep the format as simple as possible in the book. Any code or command lines are simply indented in their own textarea like so:

        prompt$
        prompt$ wc -l /etc/passwd
        prompt$

Which includes single line commands or code listings. Code listings and commands are separated by context - it will be very obvious to the reader when it is a command or code. For example in this faked up excerpt:

        int x;
        int y;
        char * foo;
        char * bar;

0.1.6 Acknowledgements

First and foremost every and anyone involved in Free Software and/or Open Source. Without all of our efforts the capability to even do most of what is in this book (and a great deal of others) would never have been realized.

Secondly to my family friends for motivating me to work just a little every night.

Thirdly by name some other Authors: