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
Printable View
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
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.
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?
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.
That presumes you even have permission to do this.Quote:
how to mout them with C.
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.
I have permission to do that
I want to do this with C API not calling a mount program. this is not possible?
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.
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?
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?
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:
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.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
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)