Thread: How to detect usb storage arrival and its mount path from C

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Question How to detect usb storage arrival and its mount path from C

    Hi, I am a newbi to linux,
    i wana to write a program in C that can detect arrival of a usb flash memory. I want also to find the actual mount point of new inserted flash disk. can anyone help me?
    thanks a lot

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This is a bit tricky.

    Detecting a new flash drive means registering with dbus to receive hardware availability events from udev. You can identify flash drives by their ID. (Not entirely sure how that works.)

    Finding the mount point is trickier, because udev doesn't mount anything. The drive could be mounted by automountd (but not necessarily immediately - automountd supports mounting on-demand, i.e. when the user first views the directory), or by KDE's udev listener (part of kioslaves, I think), or by whatever Gnome, Xfce, or some other desktop environment use for the same purpose. Or it could even be some homegrown solution, though that is thankfully rare.

    The problem is, I don't think these daemons give notification when they do mount a volume, or at least not in a uniform way. You could google for such a thing of course - "VolumeMount" looks promising, by my short seach - but I don't know how much success you'll have.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Does the USB drive already automount so you can find it without C? (I'm presuming it does)

    That being the case, do you really need to detect the automount in a C program (as if it were something that could happen by surprise, without a user or anything being able to predict it?)

    Why do want to do this this way?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    thank a lot for replies. this is the problem description:
    i want to write a C program under Linux (one customized embedded Linux) that should read some data from USB flash disk and write on. i have done this work under windows XP by using deviceIoControl and CreateFile. There is not any idea about HAL?
    I can change the way i am communicating USB Flash but now i want to now at least how can enumerate all USB flash Storage (or all removable storage) on my system and how to mout them with C.
    In the other word, haw can i know how to refer to my flash? (/dev/sda1 or /dev/sdb1 or /dev/sdc1 or ...) when this problem is solved the next step is that how to Mount this device under C API?
    Thanks a lot.
    Last edited by mostafa; 10-12-2008 at 10:21 AM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    how to mout them with C.
    That presumes you even have permission to do this.

    There are various ways. You can call the mount program. You can send a dbus message. (Something listens and mounts, but I forgot which program that is.) You could even compose the system call yourself.

    I recommend using /dev/disk/by-uuid or /dev/disk/by-id. Udev puts symlinks there to all partitions and disks of the system, identified by filesystem UUID (set when formatting the stick) or the device ID (may or may not incorporate a serial number). This way, you can definitely identify your disk.

    Then you could canonicalize the device name and look it up in the information about mounted devices. If it's there, you can access it directly. If not, you have to mount it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Quote Originally Posted by CornedBee View Post
    That presumes you even have permission to do this.
    I have permission to do that

    Quote Originally Posted by CornedBee View Post
    There are various ways. You can call the mount program. You can send a dbus message. (Something listens and mounts, but I forgot which program that is.) You could even compose the system call yourself.
    I want to do this with C API not calling a mount program. this is not possible?

    Quote Originally Posted by CornedBee View Post
    I recommend using /dev/disk/by-uuid or /dev/disk/by-id. Udev puts symlinks there to all partitions and disks of the system, identified by filesystem UUID (set when formatting the stick) or the device ID (may or may not incorporate a serial number). This way, you can definitely identify your disk.
    My program will be used in places that users may use multiple different Flash disks that i don't know UUID of them. I don't know device name of USB flash disk.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    mount is written in C, so obviously it's possible. You could read its source code to find out how.

    Could you describe at a high level what you need to accomplish?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    mount is written in C, so obviously it's possible. You could read its source code to find out how.

    Could you describe at a high level what you need to accomplish?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Just as a note, mostafa, if you haven't noticed this before -- the kernel does (or can, it could be my configuration) detect USB plug-ins independent of HAL or anything else.

    I'm sure of this because while I do not use an automouter, if I plug a flash drive in after the kernel has booted but before it mounts the filesystem (it will does this after the filesystem is mounted to, but it's hard to notice), it outputs a little message to stderr (in the midst of it's normal boot time stuff) about having detected this USB device (this ends up in dmesg or /var/log/messages). Obviously, this does not mean mounting it, but it could be something to investigate.

    from /var/log/messages:
    Code:
    Oct 13 11:19:04 akashiraffee kernel: usb 1-3: new high speed USB device using ehci_hcd and address 2
    Oct 13 11:19:04 akashiraffee kernel: usb 1-3: configuration #1 chosen from 1 choice
    Oct 13 11:19:04 akashiraffee kernel: scsi0 : SCSI emulation for USB Mass Storage devices
    Oct 13 11:19:09 akashiraffee kernel: scsi 0:0:0:0: Direct-Access              USB Flash Memory 1.00 PQ: 0 ANSI: 2
    Oct 13 11:19:10 akashiraffee kernel: sd 0:0:0:0: [sda] 3953664 512-byte hardware sectors (2024 MB)
    Oct 13 11:19:10 akashiraffee kernel: sd 0:0:0:0: [sda] Write Protect is off
    Oct 13 11:19:10 akashiraffee kernel: sd 0:0:0:0: [sda] Assuming drive cache: write through
    Oct 13 11:19:10 akashiraffee kernel: sd 0:0:0:0: [sda] 3953664 512-byte hardware sectors (2024 MB)
    Oct 13 11:19:10 akashiraffee kernel: sd 0:0:0:0: [sda] Write Protect is off
    Oct 13 11:19:10 akashiraffee kernel: sd 0:0:0:0: [sda] Assuming drive cache: write through
    Oct 13 11:19:10 akashiraffee kernel:  sda: sda1 sda2
    Oct 13 11:19:10 akashiraffee kernel: sd 0:0:0:0: [sda] Attached SCSI removable disk
    To reiterate: this happens immediately when the flash device is plugged in if USB support is compiled into the kernel. The drive HAS NOT been mounted yet.
    Last edited by MK27; 10-13-2008 at 09:34 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Its easy enough to know how is kernel is built. When you pop-in a USB drive, what happens? Does it automatically mount? Does it automatically detect? Does it do anything? (I mean outside the light on the drive turning on)

Popular pages Recent additions subscribe to a feed

Tags for this Thread