LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2006, 04:25 PM   #1
wanglinux2007
LQ Newbie
 
Registered: Dec 2006
Posts: 6

Rep: Reputation: 0
How to write code to detect a memory stick when it is pluged in?


Hi,

I want to write c code under Linux7.1, which implements the following:
The program will go to execuate a specific task once it detects a USB memory stick is pluged in.

When I plug in a USB memory stick, I can check /var/log/messages and see it is detected by the system. But, I don't know how to do this in c code -- detect a memory stick when it is pluged in. Is there any command I can use? I have no clue about this. Could anybody help me? Thanks a lot!

wanglinux2007
 
Old 12-08-2006, 04:43 PM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch buster
Posts: 1,725

Rep: Reputation: 107Reputation: 107
Hi,

I personally have not done this, however
#1. if you are using udev, rule "RUN" can be used to invoke something. see man udev
#2. if you are using hotplug, I think we can tweak some of these files, but I am not sure

Happy Penguins!
 
Old 12-08-2006, 05:35 PM   #3
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
wanglinux2007:
Which distro are you running? Even if hotplug is installed/enabled/running, there's not always a guarantee that it'll detect any new devices, try adding this to /etc/fstab (it works for me, and I'm running Mandriva 2007):
Code:
/dev/sda1 /mnt/removable auto user,users,noauto,exec 0 0
Depending on your distro, the device reserved for "special" devices may change; for Mandriva (and Mandriva-based distro's) it's /dev/sdaN (where n is a number).
Also, make sure that /mnt/removable exists (if it doesn't, as root, type "mkdir /mnt/removable" - without quotes - at the command line), and that all users have read/write access to it (As root, "chmod a+rw /mnt/removable").

Hope that works.
 
Old 12-11-2006, 08:19 AM   #4
wanglinux2007
LQ Newbie
 
Registered: Dec 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Kaz2100, indienick,

Thank you very much for the advice. I found that udev requires a 2.6 kernel to run, but I have linux kernel 2.4. I think I will try to see if I can use hotplug to do this.

wanglinux2007
 
Old 12-11-2006, 10:44 AM   #5
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
You're very welcome, wanglinux2007.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to read/write usb 256mb memory stick Aju Linux - Hardware 13 03-09-2009 03:24 AM
USB memory, Compact Flash, Memory Stick energiza Linux - Hardware 2 08-22-2006 09:29 PM
Missing memory in USB stick, flash memory, removable hd etc bamboo_spider Linux - Newbie 3 06-14-2006 05:39 PM
Can't WRITE to USB stick, Mount OK NO WRITE! ClericPreston Linux - Newbie 4 12-04-2004 10:43 AM
Memory Stick Problems, can't detect module sg? GT_Onizuka Linux - Hardware 3 02-04-2004 07:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration