LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2008, 12:41 AM   #1
minge.zu
LQ Newbie
 
Registered: Oct 2008
Location: Beijing
Posts: 17

Rep: Reputation: 0
Question how to detect USB be forcely unplug


Hi,everyone.I am newbie in Linux programming.
Now i have a problem.in my program,i have to know the flash disk whether is being unplugged.but i don't know how to do.is there any system message can be captured? or any API to check system conditions?
any advices are appreciated!
 
Old 10-24-2008, 02:08 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
dmesg (or /var/log/dmesg) will show output when devices are added or removed, but the output can be hard to process for specific changes.

I don't know if this is the best way, but the hal daemon monitors and reports all system hardware changes. "lshal --monitor" will show you what changes are made to the hal listings in real time as a device is added or removed, or even mounted and unmounted.

You can then use lshal with grep or something to test the state of the device in a script. Or alternately, you can use ivman, a daemon that lets you run commands on hal events. For example, you could have ivman create a text file when a device is added, and delete it when the device is removed. Then you can test for existence of that file.

I only know a little about scripting, so this is the extent of what I know, and there are probably better ways to do this in other programming languages. But hopefully this will give you some ideas.
 
Old 10-24-2008, 02:43 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
HAL communicates with userland programs like KDE or Gnome via DBUS. You might want to study the python scripts that many systems have for examples on how to register with DBUS/HAL to receive event messages.
 
Old 10-28-2008, 05:14 AM   #4
minge.zu
LQ Newbie
 
Registered: Oct 2008
Location: Beijing
Posts: 17

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by David the H. View Post
dmesg (or /var/log/dmesg) will show output when devices are added or removed, but the output can be hard to process for specific changes.

I don't know if this is the best way, but the hal daemon monitors and reports all system hardware changes. "lshal --monitor" will show you what changes are made to the hal listings in real time as a device is added or removed, or even mounted and unmounted.

You can then use lshal with grep or something to test the state of the device in a script. Or alternately, you can use ivman, a daemon that lets you run commands on hal events. For example, you could have ivman create a text file when a device is added, and delete it when the device is removed. Then you can test for existence of that file.

I only know a little about scripting, so this is the extent of what I know, and there are probably better ways to do this in other programming languages. But hopefully this will give you some ideas.
thank you very much!I have taken a try as your advice.some informations can be captured with the commands `dmesg`,`lshal` and so on when unplug event takes place.but the difficult is that i can't determine which device is what i need...
 
Old 10-28-2008, 05:21 AM   #5
minge.zu
LQ Newbie
 
Registered: Oct 2008
Location: Beijing
Posts: 17

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by jschiwal View Post
HAL communicates with userland programs like KDE or Gnome via DBUS. You might want to study the python scripts that many systems have for examples on how to register with DBUS/HAL to receive event messages.
thank you! I am studying the python scripts related with DBUS/HAL.
i used:
Code:
bus.add_signal_receiver(device_removed, 'DeviceRemoved',
                                     'org.freedesktop.Hal.Manager',
                                     'org.freedesktop.Hal',
                                     '/org/freedesktop/Hal/Manager')
that will inform me when device removed.but the same problem is that i can't determine whether the device is what i need.maybe 'GetProperty(...) is useful.but i have no idea how to use it do you have any clue?
 
  


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
how can I safely unplug an usb device? smau Linux - Hardware 5 03-14-2006 06:30 AM
USB keyboard not detected until I unplug it ??? glenn69 Linux - Hardware 0 11-08-2005 11:13 AM
Why do I have to unplug my USB adapter to get it to work? JakeMH Linux - Wireless Networking 2 05-17-2005 04:25 PM
USB plug & unplug JrLz Slackware 6 12-09-2004 08:07 AM
USB hot unplug ( hotplugctl ) earcam Linux - Hardware 0 08-14-2004 01:39 AM


All times are GMT -5. The time now is 08:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration