Is there a clean way to detect or receive events when a user inserts or removes a CD on a Linux platform?
-
1In what programming language? – Dave Jarvis Feb 25 '11 at 02:06
3 Answers
Udev monitors hardware and forwards events to dbus. You just need some dbus listener. A quick check using the dbus-monitor tool shows this in my system:
dbus-monitor --system
signal sender=:1.15 -> dest=(null destination) serial=144 path=/org/freedesktop/UDisks; interface=org.freedesktop.UDisks; member=DeviceChanged
object path "/org/freedesktop/UDisks/devices/sr0"
This is the DeviceChanged event from Udisks, and the device path is included.
So, in whatever programming language you want that supports dbus bindings you can listen for those (system bus) events.

- 42,110
- 11
- 57
- 76
-
udev forwards events to hotplug. You apparently have hotplug attached to dbus, but this isn't true of all linux distributions. – Ben Voigt Feb 25 '11 at 04:06
-
@Ben No, the latest udev forwards to dbus, I have no hotplug on my system. This is true for the newest udev/opendesktop installations. That's what I have with my Gentoo installation. But it's true that other distros may not yet have this. – Keith Feb 25 '11 at 04:15
-
That's funny, I don't see a dependency from udev on dbus, or any USE flag. Must be an indirect dependency. – Ben Voigt Feb 25 '11 at 04:54
-
2@Ben @Keith: udisks is not part of udev. Rather, udisks is a D-Bus service which manages disks, including handling information coming from udev. – ephemient Feb 25 '11 at 06:53
Traditionally there has been HAL (Hardware Abstraction Layer) for this, but the web page says
HAL is in maintenance mode - no new features are added. All future development focuses on udisks, UPower and other parts of the stack. See Software/DeviceKit for more information.
and the DeviceKit page lists
udisks, a D-Bus interface for dealing with storage devices
So udisks should probably be what you are asking for.

- 26,565
- 10
- 94
- 165
-
udisks, if it's available, is definitely the way to go. It presents as a DBus interface (accessible from most any reasonable programming language), and has a command line tool (udisks) which can be parsed by pretty much anything else. – dannysauer Mar 10 '12 at 17:24
The best way I was able to find was Halevt. Halevt is apparently a higher level abstraction than using HAL directly. It uses an XML based configuration file that may or may not be to your liking. The configuration file properties documentation is somewhat lacking. A list of all the supported properties are listed here:
http://www.marcuscom.com/hal-spec/hal-spec.html
Also, the link to Halevt: http://www.nongnu.org/halevt/

- 9
- 1