4

How to programmatically detect when a device raises an interrupt? This can be when a device is connected or disconnected.

And also this case: for example: when a finger is held over a fingerprint scanner, an interrupt is raised. How to detect and possibly trap this interrupt?

I want to write an application using Gtkmm such that when an event occurs like a CD being inserted or a pendrive being plugged in, I catch the interrupt these devices raises and use it to do something in my application, involving these devices.

If it cannot be done in Gtkmm, can I trap the Interrupt at a lower level and inform the Gtkmm application?

I was checking out how GParted behaves. It was initially showing /dev/sda and when I connected my pendrive, it automatically opened files application. When I checked GParted, pendrive was not present in the drop down menu of devices. It appeared only when I selected “Refresh Devices” in the GParted menu or Ctrl+R.

  • There's no gtkmm tag because questions about that belong on Stack Overflow. However, the GTK library doesn't include this kind of functionality, and outside of that there's no callback you can register with the system either. – goldilocks Jul 16 '14 at 14:40
  • @goldilocks, should I delete this question and ask the same on Stack Overflow? – user2555595 Jul 16 '14 at 14:43
  • You could, although since as I've said there's no such functionality available in GTK or any other C or C++ library, the question is really, "How can I detect programmatically when a device has been plugged in?", which is okay here. I'll have a partial answer for you in a minute. – goldilocks Jul 16 '14 at 14:49
  • @goldilocks, ok, I will change the question to what you suggested – user2555595 Jul 16 '14 at 14:55
  • This is a partial answer as it only answers detecting when a device is connected or disconnected. I found a related question here. So using Inotify and monitoring the files mentioned by goldilocks will work for filesystem changes. – user2555595 Jul 19 '14 at 7:26
3

I can try to trap the Interrupt at a lower level and inform the gtkmm application.

No, that is a kernel space activity. Fortunately, the kernel does report the outcome of certain events via interfaces accessible from userland.

It's a little ambiguous in your question whether you want to detect when a block device is attached, or when a filesystem is mounted (although it seems to be more the former). If your system uses automounting (they usually do by default), it will mount filesystems from block devices when they are attached, otherwise you have to do it manually (e.g., with mount).

Either way, you want to poll/parse/scan a kernel file node based interface. I've done this before in an application (a C++ GTK one, in fact) that tracks both attached block devices and mounted filesystems via /dev/ and /etc/mtab. This is a straightforward, language agnostic method. Some people find it a little distasteful at first because it involves reading files/directories, but these interfaces do not actually exist on disk, so there is no heavy I/O overhead, and remember: read() is a system call. Reading the file nodes in kernel interfaces amounts to the same thing as a listAttachedDevices() style API, except again, it is language agnostic. When you go to read from these nodes, the kernel passes you the information they represent directly.

The /dev directory lists attached devices as special device node files -- e.g. /dev/sda. These are added and removed by the kernel as devices are plugged in and out, so if you track it by polling at intervals (say every 5 seconds), you can detect what's new and what's gone. The only complication here is that since there's no callback style API, you have to create your own thread for this if you do want a continuous check (perhaps why gparted requires you to click Refresh Devices instead).

A probably better alternative to /dev would be the stuff in /sys/block. Note that there is a significant difference between /dev and /proc (see below) or /sys in so far as the nodes in the latter contain information about things such as devices, whereas the nodes in /dev are an actual connection to the device (so if you scan /dev, don't bother reading the individual files, just note they exist).

/etc/mtab now-a-days is a symlink (see also the -s switch in man ln) to /proc/self/mounts; /proc is a major swiss army knife kernel interface (see man proc). This lists mounted filesystems; if you use automounting things will appear and disappear from there when stuff is plugged in/out. The information in /proc and /sys is usually in the form of ASCII text, so you can look at these files with cat, etc, and parse it with string(stream) functions.

WRT to other kinds of devices, such as a fingerprint scanner, /sys is a good place to start -- /sys/dev contains a block and a char directory. Block devices are usually storage; the information on them can be randomly accessed. Char devices exchange information with the system in a stream, which would include things like scanners, cameras, HID stuff (human interface device, e.g. mice and keyboards). I notice that gtkmm does have some high level stuff for attached HID things, presumably since these are significant in interacting with the GUI.

  • thanks a lot. A few things went over my head :). Need to read about those in a little bit more detail. – user2555595 Jul 16 '14 at 16:09
0

I approve of the answer by goldilocks. But instead of using the system call read to check for filesystem changes, one can use inotify.

Its man page is here and here.

There is an excellent explanation and example by the creators (developers) of inotify here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.