Stupid udev tricks

udev is the new user space device manager in Linux 2.6. It has a lot of cool features, one of which is the ability to run a script when a device is added or removed. It can also let you assign a fixed device name to a hot-pluggable device (say, oh, a Palm Pilot). By combining these features, you can setup a Linux system to do a Palm hotsync automagically when the hotsync button is pressed on the cradle. Obviously, this is only useful if you have a single user with a Palm Pilot using the computer. It may be possible to make it work for multiple users, however, that’s beyond the scope of this post.

Assigning a fixed name

The first step in setting this up is to get udev to assign a fixed device name to the Palm every time it’s plugged in and the sync button pressed. udev uses a subdirectory called rules.d under /etc/udev to store a set of rules files for assigning names. I called the file on my system 01-palm.rules (all rules files must end in .rules to be recognized. The file contains a single line:

SYSFS{product}="Palm Handheld", NAME="palm%n"

Basically, that tells udev that the device in the /sys file system with a product name of “Palm Handheld” should get the device name “palm”, the %n indicates that the created interfaces should be named “palm0” and “palm1”. The “palm1” interface is the one we’ll actually be dealing with.

Next, we need to set appropriate permissions on the newly created device node. I assigned the created device nodes to the “usb” group (and added myself to that group). To do this, we create a file in the permissions.d subdirectory of /etc/udev/. I called mine 01-palm.permissions, again to be recognized by udev, the permissions files all must end in .permissions. The file contains the single line:

palm*:root:usb:0660

Which tells udev to assign all devices starting with “palm” to the root user and usb group and allow the user and group both read/write access to the device, but not anyone else.

Running the Sync

The first step in automating the hotsync is to configure the software you use for performing the sync to look at /dev/palm1 instead of /dev/tts/USB1 or whatever the previous device name was. In my case, I use jpilot for hotsyncing, so I loaded it up and changed the configuration appropriately. I then performed a sync from with jpilot to insure it was correctly setup. Next, find the command line sync utility for your software, for jpilot, it’s jpilot-sync. Start it and make sure you can sync properly using it.

Now it’s time to make the magic happen. In /etc you’ll find a dev.d directory. Under that directory you can create a subdirectory for a particular device name. In our case, we create palm1. Now, any executable scripts in that directory with a name ending in .dev will be automatically execute when the corresponding device is either added or removed from the system. So, create a small file called sync.dev and place it in /etc/dev.d/palm1. The file should contain:

#!/bin/sh
if [ $ACTION = "add" ]; then
        su - jim -c /usr/bin/jpilot-sync >/dev/null 2>&1
fi

Obviously, you’ll want to replace the “jim” (my username) with the appropriate username on your system. And if you’re not using jpilot, replace jpilot-sync with the appropriate utility. What this script says is, on an ‘add’ action, run the jpilot-sync command as the user jim, directing all output to /dev/null. Save the file and make sure it’s executable by root.

Conclusion

And that’s it. Now when you place your Palm Pilot on the cradle and press the hotsync button it will trigger a series of events in the background. First, udev will create the device nodes and give them the appropriate names and permissions. Next, it will execute the script in /etc/dev.d/palm1/sync.dev. This will run the sync program and let the Palm sync with the PC. Once the sync is complete, the Palm will disconnect from the port and udev will automatically remove the device nodes again. Pretty nifty, no?

Leave a Reply

Your email address will not be published. Required fields are marked *