I have written battery driver and that driver sends an uevent
when a power supply is changed, i.e. from AC to battery
When I run udevadm monitor
I see an KERNEL and UDEV CHANGE
events from power_supply
subsystem upon removing AC supply.
My requirement is to notify the user application when there is an CHANGE
event from power_supply
subsystem. I do not want to poll for kernel message or netlink socket from user application.
Is it possible for me to register/listen for this particular
uevent
from user application and gets something like callback function when an event occurs?
It is possible and there is a great reference here. In the reference they use a change rule for a similar application:
# Rule for when switching to battery
ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/USERNAME/.Xauthority" RUN+="/usr/bin/su USERNAME_TO_RUN_SCRIPT_AS -c /usr/local/bin/brightness_notification.sh"
The ACTION=="change"
registers a rule for a change event. The SUBSYSTEM
and ATTR
parameters specify the rule to only apply to a certain device or devices (you can get these for your device from udevadm). The RUN
parameter is how you run a userspace executable when the event is seen. In the case below they also use the ENV
parameter to pass a environment variable to the executable and they also use the su
command in RUN
to run the executable as another user, but this is probably not necessary for most applications.