Bash script udev play sound - linux

I have a problem with my script. I added a rule which detect if a unique USB if plugged in. These part works fine. My idea was after I did some things on the stick, I make a sound so the user know when he can unplugged the stick.
I like to use espeak and tried it without the rule and it is working, but only without the trigger.
Here is my usb.sh:
echo "Pluged in" >>/tmp/usb.log
espeak "Backup done"
And here is the rule i added:
ACTION=="add",KERNEL=="sdb1", SUBSYSTEM=="block" ,ATTRS{idVendor}=="0951", ATTRS{idProduct}=="1666" ,RUN+="/home/user/usb/usb.sh"

Related

How to make Linux ignore a keyboard while keeping it available for my program to read?

I am building some kind of kiosk system and I bought this USB DIY keyboard for it: https://www.amazon.com/gp/product/B07QPXQQ7L
This allows me to have a lot of buttons and they behave like keyboard keys.
I'm writing a program (Perl) that will take the input from that keyboard and do things based on that.
The problem is that I need to have the rest of the system (both X and the TTYs) ignore that keyboard so that it won't type random things in the terminal or in the window manager. In other words, the system should disregard it but the device itself must still be available in /dev/input/...
I don't need a real keyboard to control the machine because I connect via VNC and SSH.
Bonus points if you know how to read from a /dev/input/... keyboard and end up with letters typed just like with STDIN in a terminal.
Thanks!
I found the solution here where someone wanted the exact same thing in the case of a barcode reader:
https://serverfault.com/questions/385260/bind-usb-keyboard-exclusively-to-specific-application/976557#976557
SUBSYSTEM=="input", ACTION=="add", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", RUN+="/bin/sh -c 'echo remove > /sys$env{DEVPATH}/uevent'"
ACTION=="add", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", SYMLINK+="diykeyboard"
And then replace xxxx and yyyy by the Vendor and Product ID as found in lsusb. So in my case 1c4f and 0002:
Bus 001 Device 003: ID 1c4f:0002 SiGma Micro Keyboard TRACER Gamma Ivory
The udevadm control --reload thing didn't do it for me, I had to reboot.
Then in theory the data typed on the keyboard should be available at /dev/diykeyboard (the SYMLINK variable).
Now in my case unfortunately there are multiple events that match this vendor+product, and to match the right one I needed to add DEVPATH=="*:1.0/*", KERNEL=="event*" in the second line where it creates the SYMLINK. And then surprise it did not create the link in /dev so I had to do something dirty, create a link myself with ln:
SUBSYSTEM=="input", ACTION=="add", ATTRS{idVendor}=="1c4f", ATTRS{idProduct}=="0002", RUN+="/bin/sh -c 'echo remove > /sys$env{DEVPATH}/uevent'"
SUBSYSTEM=="input", ACTION=="add", ATTRS{idVendor}=="1c4f", ATTRS{idProduct}=="0002", DEVPATH=="*:1.0/*", KERNEL=="event*", RUN+="/bin/sh -c 'ln -sf /dev/input/$kernel /diykeyboard'"
(don't create the link in /tmp since udev happens before the mounting of /tmp at boot)
From there I can read from /diykeyboard (which usually points to /dev/input/event0) either with evtest which shows the keys typed, or directly with my program and then decoding the scancodes.

notify-send not working (in script) executed from udev

I want a notification in desktop that a particular USB device is inserted.Hence the following is the udev rule.
KERNEL=="sd*",SUBSYSTEMS=="usb",ACTION=="add",RUN+="/home/username/Desktop/notify_script"
notify_script is as follows
#!/bin/sh
su username -c 'notify-send "USB Inserted"'
echo USB_inserted >> /home/username/Desktop/test_file
Problem :
The above script works perfectly if it is executed as root from command line but, notify-send in script is not working if executed from udev.
Question:
How to make notify-send work from udev? or
Is there any other way by which I can achieve notification ?
Is it possible to invoke any GUI from udev?
Thank you.
The main problem is, that the udev-rule will not run in any xorg-related environment per default, thus not knowing which DISPLAY to use. Therefore it will always fail, if You want to echo something into a terminal like a gnome-terminal for example. The script, which shall be executed on the udev-rule-match, must prior to any ui-related execution first export the DISPLAY.
This is done via
export DISPLAY=:0
I assume, that this also will be the problem, and notify-send will just run against the wall.
I am actually also playing with udev-rules, and I managed it to work, though i am acting as root, similar to my answer and this one found already here :
https://unix.stackexchange.com/questions/80882/udev-running-a-shellscript-that-accesses-an-x-display
And also here
Scripts launched from udev do not have DISPLAY access anymore?
You might want also to check zenity. Very helpful for small notifications

Udev Rule no longer works

I wrote a simple udev rule to mount my compact flash card reader to a writeable device node. The rule was working earlier, but seems to have stopped for some reason, I was editing the file before, but now it just contains:
KERNEL=="/dev/sd*", ATTRS{model}=="Compact Flash ", SYMLINK+="compactflash-%k", MODE=="0666"
The file is called 90-cf-rule.rules and is located in /etc/udev/rules.d. If someone could point out what's wrong I'd be really grateful.
It looks like udevd is still running, and I run udevadm trigger after editing the rules file. I've tried plugging and unplugging the device. I'm also certain that the model matches as I copied and pasted from udevadm info and have had this rule working!
The correct udev filter is:
KERNEL=="sd*", ATTRS{model}=="Compact Flash ", SYMLINK+="compactflash-%k", MODE=="0666"
ie. The /dev/ is not required in front of the KERNEL argument. I must have put this in while editing the file earlier.

Handling input device plug/unplug while reading from it

I have a bluetooth remote that is paired with my linux box, when active, this remote is visible at /dev/input/event13.
I have a small C program that open this device and read directly from it, which works fine.
Now, my problem is that this remote goes to sleep after a few minutes of inactivity and /dev/input/event13 disappears. It reappears as soon as I press a key on my remote.
I put the output of udevadm here: https://gist.github.com/9fff2f0d7edef1050060.
I use the following code (small ruby ext I wrote), to read from the device: https://gist.github.com/b403d538eb6a8627e2bd.
I thought of writing an udev rule that would start my program when my remote is added and stop it when it is removed. I read the udev documentation, but I couldn't figure it out how to do it. I'm open for suggestion.
After some digging and a lot of frustration I did the following:
I put into /etc/udev/rules.d/99-rmote.rules
KERNEL=="event*", SUBSYSTEM=="input", ACTION=="add|remove", ATTRS{name}=="TiVo Keyboard Remote", RUN+="/home/kuon/handleConnect.rb"
And in handleConnect.rb I check the ACTION environment variable and I start/stop my daemon accordingly. Quite simple in the end and it works well.

auto permissions on udev'd device files?

This is a dupe from SuperUser.com . Folks over there weren't smart enough or willing to help me out; maybe it's more a programmer question than an administrator one:
I have an app that reads input from 4 (four) mice that are plugged in via USB in addition to the built-in touchpad. This is no problem for Ubuntu 9.10: hald notices the new devices and udev's them brand new entries called /dev/input/mouse4 ... mouse7.
My app runs as a normal user app. The files in /dev belong to root and aren't readable to anyone else.
I don't have a problem doing chmod a+r mouse? once, but the devices come and go with every reboot and every time the dang rodents are plugged in or out.
Can someone please tell me a script or something to manipulate so my chmod happens automagically?
This information is easily discoverable, I just had to look at /etc/udev/rules.d/91-permissions.rules on a Debian configuration. :-)
Some interesting examples pasted from there:
# default permissions for block devices
SUBSYSTEM=="block", GROUP="disk"
Looks like there's this "GROUP" thing to determine the group owner...
And, you can chmod stuff apparently:
KERNEL=="pty*", MODE="0666", GROUP="tty"
Note the MODE part...
I'm sure RTFM-ing for the udev config file format will give you even more information. :-)

Resources