Ubuntu Linux Udev rules: Is it possible to run a program written in C through udev?

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

I am currently running Hello World script when I connect my device. However, he runs it more than once.

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

Current Rule:

SUBSYSTEMS == "usb", ACTION == "add", RUN + = "/USR/local/ben/USB.sh"

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

Script:

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

I tried something like this to run the executable:

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

typing usr / games / blackjack works in the terminal, however it does not work when a USB device is inserted. However, I know that the script works, because I combined them into one file, and the hi world was created.

I also tried to run the executable from my user account, as shown below:

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

However, this does not work either.

Is this a problem with device privileges or is it just impossible to run an executable file?

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

+4
source share
3 answers

Another reason may be that udev seems to be working in its own environment. This is similar to running your program from tty: it starts, but there is no xserver ...

However, this is pure speculation based on my own experiments. It would be nice if someone who knows udev 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 )

+1
source

If you are writing a C program, you can use libudev . It has a simple API that allows you to list and control devices. Here you will find a good tutorial.

+1
source

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

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

I do not know if you typed it correctly on your terminal and incorrectly in your script, or if you just have a different environment. Unless UDEV is specifically designed to recreate your terminal environment, there is no reason why they will be the same.

0
source

Source: https://habr.com/ru/post/1336859/


All Articles