Find the answer to your Linux question:
Results 1 to 4 of 4
Hello to all I make a app (in C language) that need to autodetect new flash drives that are plugged in USB ports. I need to mount that drive and ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    3

    Autodetect new usb pen drive

    Hello to all

    I make a app (in C language) that need to autodetect new flash drives that are plugged in USB ports. I need to mount that drive and launch a app from the pen drive. I can't find any code (any libs) that are help me to automaticaly detect new pen drives. Here are any ideeas?
    Currently I use /proc/partitions (I load in memory a structure at startup...and at every 5 seconds I compare the data from memory with /proc/partitios)

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Use the inotify tools and/or api's to see when the directory /media gets a new entry. That will show you when any new removable drive is plugged into the system and auto-mounted by the OS.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    3
    Rubberman the problem is that we don't have automount (we don't want to use).
    It will be on some embeded system. The executed file should be load only in some situations. We don't want not even to automount...only on some situations (for example, only when is inserted a pen with a special serial number)
    And for this I need a library.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Even if you don't have automount active, it will create a new device entry in /dev, so you can use the inotify C language APIs to detect a new drive type of device (/dev/sdx) when it is plugged in or unplugged. Also, you can monitor changes in /proc/bus/usb/devices and then read that to see if the newly plugged in USB device is a storage device. For example, I just plugged in a USB thumb drive into my USB hub and the following entry was added to /proc/bus/usb/devices:
    Code:
    T:  Bus=01 Lev=02 Prnt=14 Port=02 Cnt=02 Dev#= 19 Spd=480 MxCh= 0
    D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
    P:  Vendor=05dc ProdID=a440 Rev=30.00
    S:  Manufacturer=Lexar Media
    S:  Product=JD Lightning
    S:  SerialNumber=33000001937000002541
    C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=500mA
    I:  If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
    E:  Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
    E:  Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=31875us
    As you can see, even the serial number is there, so you can then use that to determine if you want to allow any operations on this device.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...