i want to create a program that should run as auto . when i insert a pendrive it shall execute at the terminal
plz can somebody help me to program in C
i want to create a program that should run as auto . when i insert a pendrive it shall execute at the terminal
plz can somebody help me to program in C
You can tail /var/log/messages looking for insertion messages. They'll vary from device to device, but generally look somewhat like this:
[183532.373402] sda: sda1
[196936.098729] usb 1-3: USB disconnect, address 3
[196936.440424] usb 1-3: new high speed USB device using ehci_hcd and address 6
[196936.586043] usb 1-3: New USB device found, idVendor=0781, idProduct=9219
[196936.586052] usb 1-3: New USB device strings: Mfr=3, Product=4, SerialNumber=5
[196936.586057] usb 1-3: Product: ImageMate CF Reader/Writer
[196936.586061] usb 1-3: Manufacturer: SanDisk
[196936.586065] usb 1-3: SerialNumber: 0302855445
[196936.586286] usb 1-3: configuration #1 chosen from 1 choice
[196936.592552] scsi1 : SCSI emulation for USB Mass Storage devices
[196936.593074] usb-storage: device found at 6
[196936.593078] usb-storage: waiting for device to settle before scanning
[196941.592515] usb-storage: device scan complete
The rest is just a smop.
if I got you right, you mean something like Windows' AUTORUN.INF
, don't you?
in this case, I guess it's very unlikely that you'll find what you looking for, it's quite often considered as dangerous, because you can run whatever you want, virus and other malware included!
I think KDE looks for autorun.sh
in the root of the plugged-in device, but I am not certain.
When you plug in a USB (or often many other types of removable) storage devices the kernel, after recognizing what it is, notifies a program called udevd. udevd looks through some rules (stored in files under /etc/udev/rules.d/, /lib/udev/rules.d/, or /dev/.udev/rules.d ) for rules that match the new device and executes the ones that do. One of the rules for removable block devices runs a program to mount the filesystem(s) on that drive (possibly creating mount points/directories).
Gnome, KDE, or similar desktop interface get notified of this via D-BUS that a new device has been installed and mounted (I'm not 100% about the exact steps here).
Anyway, in order to run a program on a newly inserted drive you are going to need a some program to do for you that is already running. You could try altering your udev rules or the script that mounts new drives to do this for you or write a new daemon that gets notified the way that desktop management systems like gnome or KDE get notified to run it, but there is no facility that I know if that already does this. Doing so is a security risk since it allows anyone with access to a USB (or similar) plug to run any program on a computer. I could walk up to someone using a laptop in a cafe and very quickly run a program on their computer without their consent. Or if I gave someone a USB drive and said "this has my vacation photos on it" and they put it in their Linux computer a malicious autorun program on the drive would be run without their consent.
If such functionality were already part of some Linux distribution (it may be, I don't know) it should definitely require a user to agree before it is run, but even then it would have to be limited to running with that user's UID and GID (ignoring possibility of even more complicated SELinux security), and with a working directory that was safe for that user to have. And this totally ignores the issues with mobile filesystems' ownerships getting mixed up and SUID bits.
This probably is not the right way to go about solving whatever problem you are trying to solve.