4

I'm currently working on a project to run an program written in C when a USB device is plugged in. Is this possible with udev rules?

I've currently got it to run a Hello World script when I plug in my device. However, it runs it more than once.

Current path:/etc/udev/rules.d/98-local.rules

Current rule:

SUBSYSTEMS=="usb", ACTION=="add", RUN+="/usr/local/bin/USB.sh"

Script's path: /usr/local/bin/USB.sh

Script:

 #!/bin/bash
echo 'Hello World!' >>"/home/<username>/Desktop/udev.out"
exit

I've tried something like this to get the executable to run:

 #!/bin/bash
usr/games/blackjack
exit

typing usr/games/blackjack works in the terminal however it doesn't work when the USB device is inserted. However, I know the script is running because I've had them combined in the same file, and the hello world has been created.

I have also tried running the executable from my user account, as follows:

SUBSYSTEMS=="usb", ACTION=='add", RUN+="/bin/su tyler -c '/usr/local/bin/USB.sh'"

However, this doesn't work either.

Is it a problem with device privileges or is it just not possible to run an executable?

*note: I've read the udev rule explanations at http://reactivated.net/writing_udev_rules.html extensively.

ismail
  • 46,010
  • 9
  • 86
  • 95
Tyler
  • 41
  • 1
  • 2
  • How are you testing to see if the program ran? – Ben Voigt Jan 24 '11 at 22:52
  • well, I want an interactive program to launch this way, so in they case of this game, it would launch and I'd be able to play it. I've got about 10+ hours into this, im starting to think it isn't possible. – Tyler Jan 27 '11 at 21:14
  • Both your bash scripts have an extra space in front of the shebang (` #!/bin/bash`). That probably doesn't matter in this case (the scripts will be invoked with the default `/bin/sh`), but you should fix it. – Keith Thompson Aug 31 '11 at 15:50

3 Answers3

2

If you write program in C, you can use libudev. It has simple API which allows to enumerate and monitor devices. Here you'll find nice tutorial.

Maciej
  • 3,685
  • 1
  • 19
  • 14
1

Another reason might be that udev seems to run in it's own environment. It's like you would run your program from a tty: It does start, but there's no xserver...

However, this is pure speculation based on my own experiments. It would be nice if someone who knows udev better could confirm this.

EDIT: add the following to your script:

set -x
xhost local:YOURUSERNAME
export DISPLAY=:0.0

And maybe reload your rules with udevadm control --reload-rules.

(Source: http://ubuntuforums.org/showthread.php?t=994233)

iliis
  • 878
  • 8
  • 19
0

If you can get a shell script to run, you can get an executable to run. It doesn't matter if it was created by C or not.

You seem to be missing a slash. That path almost certainly should be /usr/games/blackjack, not user/games/blackjack.

I don't know if you've typed it properly in your terminal and improperly in your script, or if you simply have your environment different. Unless the UDEV system is deliberately designed to recreate your terminal environment, there's no reason why they'd be the same.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
  • 1
    This should help launch the process, but it also looks like Tyler is expecting the program to run interactively, which is going to need a lot more care than just getting the path right, if it's possible at all. – Ben Voigt Jan 24 '11 at 22:51
  • Ben is correct. PS. Ben can you tell me what kind of care you are talking about? Perhaps to point me in the right direction? I'm sort of at a loss. – Tyler Jan 27 '11 at 21:15