4
votes

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.

3
How are you testing to see if the program ran?Ben Voigt
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
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

3 Answers

2
votes

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.

1
votes

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)

0
votes

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.