LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Device change notification on Linux (https://www.linuxquestions.org/questions/programming-9/device-change-notification-on-linux-883860/)

technogothica 05-31-2011 11:58 PM

Device change notification on Linux
 
Hi all,

On Windows, whenever you plug in or unplug a device such as USB external hard disk or game controller, the application is sent a WM_DEVICECHANGE message.

What is the equivalent approach to detecting device changes on Linux?

I don't need specific details of what changes has occurred, all I want is some notification that something has been plugged in or unplugged.

Thanks in advance.

xylex_blaiste 06-01-2011 02:42 AM

Quote:

Originally Posted by technogothica (Post 4372639)
Hi all,

On Windows, whenever you plug in or unplug a device such as USB external hard disk or game controller, the application is sent a WM_DEVICECHANGE message.

What is the equivalent approach to detecting device changes on Linux?

I don't need specific details of what changes has occurred, all I want is some notification that something has been plugged in or unplugged.

Thanks in advance.

Try this one http://udev-notify.learnfree.eu/. It should run on most Linux distros provided udev is used/installed. Hope that helps.

technogothica 06-01-2011 03:04 AM

Quote:

Originally Posted by xylex_blaiste (Post 4372770)
Try this one http://udev-notify.learnfree.eu/. It should run on most Linux distros provided udev is used/installed. Hope that helps.

Thanks for the link. I'm actually looking for an API based approach for a C/C++ application, so udev-notify is not quite what I'm after.

However, I notice that udev-notify uses udev to monitor device changes, and I also see that there is a libudev library which looks promising.

Is libudev the best (i.e. most efficient and most widely supported) way to check for device changes?

xylex_blaiste 06-01-2011 04:34 AM

I linked udev-notify because it might help you start from somewhere. I was aware your question was programming related but wasn't really sure, hence I dropped that link and know you'd pick it up from where it might lead you (to where its getting its info, from udev). Yes and it's (udev-notify) python, but it (udev) can be for any capable language.

Quote:

Originally Posted by wiki
udev is the device manager for the Linux kernel. Primarily, it manages device nodes in /dev. It is the successor of devfs and hotplug, which means that it handles the /dev directory and all user space actions when adding/removing devices, including firmware load.

I'd say it's the best place to start because it is common for all distros using 2.6.x kernels.

technogothica 06-01-2011 07:24 AM

Quote:

Originally Posted by xylex_blaiste (Post 4372865)
I linked udev-notify because it might help you start from somewhere. I was aware your question was programming related but wasn't really sure, hence I dropped that link and know you'd pick it up from where it might lead you (to where its getting its info, from udev). Yes and it's (udev-notify) python, but it (udev) can be for any capable language.

No worries, you put me on the right track. Very much appreciated :D

Quote:

I'd say it's the best place to start because it is common for all distros using 2.6.x kernels.
If it's guaranteed to work on all current versions of Linux, then it will do the trick! I'm going to give libudev a try tonight. Thanks again!


All times are GMT -5. The time now is 06:36 AM.