0
  1. I am writing a C program in Linux user space for an HMI. I want to detect the pendrive when inserted into the USB port on my SBC. I am running Lubuntu on it. So it is not having udev libraries. When I try to install udev on SBC, it is asking for dependencies and version compatibility issues are coming. Is there any other way to detect the Pendrive insertion from user space.
  2. When I mount a device ex: /dev/sdc1(pendrive) to a particular folder ex: /mnt/vj, its being mounted properly. If I remove the pendrive without unmounting it then when next time pendrive inserted its being detected as /dev/sdd1 . How to fix the logical name for a pendrive in such cases. I want it to be /dev/sdc1 always. Is it possible?

Thanks in anticipation.

  • please share what did you do! – Mahsum Akbas Oct 29 '15 at 08:55
  • As of now, I have written some glue logic which is not worth full. I am just monitoring the /sys/block folder periodically using system() system call. whenever there is a change in the folder I am mounting the detected device to a defined location. – user5501409 Oct 29 '15 at 09:02

2 Answers2

1
  1. You can implement your own event listening daemon instead of udev. Youhave to create a netlink socket of type NETLINK_KOBJECT_UEVENT. By parsing the events, you will be able to detect the insertion of your drive.

  2. It is not possible to ensure the name is always the same but you can probably create a symlink to the proper block device after detecting the event.

Alexandre Belloni
  • 2,244
  • 13
  • 12
  • Thank you.. I am able to detect the USB plug in using Netlink sock. How to get to know whether its a pendrive or a USB keyboard or mouse inserted.? And how to get the logical name of the drive(i.e., /dev/sdX)? – user5501409 Oct 30 '15 at 11:49
  • Part of the event, you get the major and the minor number. The major should be 8. each disk can have up to 15 partition plus the whole disk, that makes 16. So sda and sda[1-15] are minors from 0 to 15, sdb has 16 to 31, ... – Alexandre Belloni Oct 30 '15 at 14:05
0

Check the link ubuntu 12.04 libudev-dev won't install because of dependencies that should mostly resolve your udev installation/dependencies issue if related to it.

udev is one of the easiest ways for detecting hardware plug-in and fetching of device information. Checkout libudev that is part of udev (Device manager of Linux kernel). Apart from managing device nodes in the /dev directory while hardware devices are added into the system or removed from it, the udev also handles all related user-space events that are raised during various operations such as addition/removal.

libudev allows access to device information and also provides a monitoring interface like udev_monitor that connects to device event source. udev_monitor_get_fd provides file descriptor that can be used with select system call for monitoring.

Check this link that has information related to usage of libudev http://www.signal11.us/oss/udev/

Community
  • 1
  • 1
Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65