Before we continue with our Home Server Scripting Series, let's throw in a simple but useful DTrace hack.

One of the most typical uses for a home server is to serve music or videos to home entertainment equipment. In my case, I'm using the Firefly Media Server to serve music to my Roku Soundbridge and Mediatomb for videos.

The Media Server Update Problem

Whenever I upload new music or videos to my OpenSolaris home server (typically by rsync-ing my laptop home directory), both Firefly and Mediatomb need to be restarted so they detect that new files are sitting in their directories, waiting to be served.

The typical way to solve this in the Linux world is to use inotify, and it seems to work quite well. But inotify is a Linux-only API and it is therefore not portable.

The OpenSolaris way to detect changes in the file system is through file notification events. Darren Moffat has written a nice tutorial on how to use file notification events in your code. But this code has not been adopted by neither Firefly nor Mediatomb (yet?).

A Simple Media Server Update Solution

There's another way, more simple than coding file notification events and easy to use for any kind of media server software: DTrace.

In the following script, we'll ask DTrace to watch for open(2) events and filter out those that match the directories where we store our media. Then, when new files come in or are changed we'll send SIGHUP signals to our media server processes so they can re-scan their content directories.

Some Useful DTrace Principles

While putting together this DTrace script, the following things came up which are probably useful to know for a variety of similar D scripts:
  1. DTrace already knows about the standard libc and kernel functions, so, when watching out for writes, we can just specify the O_RDWRITE and O_WRONLY flags to open(2) verbatim without knowing their actual values.
  2. To match the media directory with the path given to open(2), we need to look at arg0, which is a pointer to the given file name. But the file name resides in user space while DTrace operates in kernel space. Therefore, we need to use the DTrace copyin function to copy the file name across memory spaces.
    This is a common DTrace technique described in the User Process Tracing chapter of the DTrace documentation.
  3. Another common thing to keep in mind: At the moment of function entry, the file name may reside on a memory page that is not yet accessible, so we can't necessarily read it yet.
    We therefore capture the value of arg0 at function entry time, wait until the function has completed, then read out the file name which will then be accessible for sure. Again, this is described in the same chapter of the DTrace docs.
  4. When trying to match the arguments to open(2) with the media directory we want to watch, we need to test for two cases: 1) arg0 contains the full path, or 2) arg0 is relative to the current working directory. In the latter case, we need to construct the full path, then test against it.
  5. If we add half a dozen CDs and 100 songs to our media library, we don't want to force a re-scan 100 times. We'll therefore use a flag that indicates that we have detected some new files, then test against the flag periodically (say, every minute or so) and only then signal the media server process to re-scan.
  6. In order to make the burden on the system as light as possible, we'll try to do as much of the testing as possible inside of DTrace predicates and only use action statements where necessary. Also, DTrace supports short-circuiting of logical AND tests, so we'll place the most likely or common tests at the beginning of our predicates.
  7. We'll use the DTrace system() action to signal our media server processes. This is a destructive action (well, not really, but in DTrace parlance...) which means we'll need to use the -w flag to dtrace(1M) which enables destructive actions.
The Automatic Re-Scan DTrace Script

With that in mind, let's look at the final DTrace script:

#!/usr/sbin/dtrace -wqs /* * dirtrap.d * * Detect when new files have been written to a given directory. * Start a command when this happens. */ BEGIN /* Initialize stuff. */{ dir = $1; dirlen = strlen(dir); flag = 0;} syscall::open*:entry /* Take note of the arg0 pointer for later use. *// !flag && (arg1 & (O_WRONLY | O_RDWR)) /{ self->file = arg0} syscall::open*:return/ !flag && /* Exit if we've already found a hit. */ self->file && /* Exit if we didn't take an arg0 note. */ ( /* More fancy directory testing. */ (copyinstr(self->file, dirlen) == dir) || ( copyinstr(self->file, 1) != "/" && ( substr( strjoin(strjoin(cwd,"/"),copyinstr(self->file)), 0, dirlen ) == dir ) ) )/{ flag = 1; self->file = 0;} tick-60s/flag/ /* Periodic check if we need to do something. */{ system($2); flag = 0;}
Usage and Testing

The script accepts two arguments: A directory to watch and a command that is executed when a file is opened for writing inside the directory. A typical use of this script looks like this:

-bash-3.2$ pfexec ./dirtrap.d /home/constant/PowerBookHome/Music/iTunes "pkill -HUP mt-daapd"
And after having touched a file inside our directory with a simple:

-bash-3.2$ echo "test" >/home/constant/PowerBookHome/Music/iTunes/testfile
We see this after a couple of seconds in the mt-daapd log file:

Apr 19 22:38:03 mt-daapd mt-daapd[7398]: [ID 702911 daemon.alert] Got HUP signal.Apr 19 22:38:03 mt-daapd mt-daapd[7398]: [ID 702911 daemon.alert] Rotated logsApr 19 22:38:03 mt-daapd mt-daapd[7398]: [ID 702911 daemon.alert] Rescanning database2010-04-19 22:38:03 (00000001): Rotated logs2010-04-19 22:38:03 (00000001): Rescanning databaseApr 19 22:38:12 mt-daapd mt-daapd[7398]: [ID 702911 daemon.alert] Starting playlist scan2010-04-19 22:38:12 (00000001): Starting playlist scanApr 19 22:38:22 mt-daapd mt-daapd[7398]: [ID 702911 daemon.alert] Updating playlists2010-04-19 22:38:22 (00000001): Updating playlistsApr 19 22:38:24 mt-daapd mt-daapd[7398]: [ID 702911 daemon.alert] Scanned 7175 songs (was 7175) in 21 seconds2010-04-19 22:38:24 (00000001): Scanned 7175 songs (was 7175) in 21 seconds
Looks like this script is doing its thing. Now I'm waiting for my next load of CDs to arrive so I can rip and rsync them into my auto-updating OpenSolaris home media server!

Conclusion

DTrace can be an amazing help for any system administrator, even on a home server. In this case, we've fixed a common problem that other OSes would need a whole new API (like inotify) to address this problem with!

Learning DTrace isn't that hard if you look at existing D scripts, check out the documentation and are not afraid to read the occasional line of OpenSolaris source code.

Other Useful DTrace Home Server Scripts

This is just the beginning. In fact, if you look around, there's a wealth of opportunities to leverage DTrace in your day to day work, even in your home server. Here are a few famous examples:
Your Turn

But that's not enough. Googling "DTrace home server" doesn't reveal much at the moment. So now it's your turn:
  • If you haven't played with DTrace start exploring it now!
  • Did you write yourself some useful D scripts already? Blog them! Or share them in the comments below!
  • What other situations can you imagine where DTrace would be helpful to you home server?
As always, feel free to share your experiences and comments below.




Read More about [How To Automatically Update Your Home Media Server Library With DTrace...