I am developing a application to don't forget the pendrive.
This app must lock the shutdown if a pendrive is connected to the machine. As this form, if the user wants to shutdown the system while a pendrive is connected, the system shows a notification to alert about it must disconnect the pendrive to unlock shutdown.
To detect the shutdown event, I set a polkit rule what call a script to check if any pendrive are connected to the system.
If there are any pendrive connected, the polkit rule calls to notify-send through the script send_notify.sh
, which execute this command:
notify-send "Pendrive-Reminder" "Extract Pendrive to enable shutdown" -t 5000
The polkit rule is this:
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.consolekit.system.stop" ||
action.id == "org.freedesktop.login1.power-off" ||
action.id == "org.freedesktop.login1.power-off-multiple-sessions" ||
action.id == "org.xfce.session.xfsm-shutdown-helper")
{
try{
polkit.spawn(["/usr/bin/pendrive-reminder/check_pendrive.sh", subject.user]);
return polkit.Result.YES;
}catch(error){
polkit.spawn(["/usr/bin/pendrive-reminder/send_notify.sh", subject.user]);
return polkit.Result.NO;
}
}
}
But. after put this polkit rule and press shutdown button, my user don't receive any notification.
I debug the rule and I checked that second script It's executed, but the notify-send
don't shows the notification to my user.
How can I solve this?
UPDATE:
I tried to modify the script as this:
#!/bin/bash
user=$1
XAUTHORITY="/home/$user/.Xauthority"
DISPLAY=$( who | grep -m1 $user.*\( | awk '{print $5}' | sed 's/[(|)]//g')
notify-send "Extract Pendrive to enable shutdown" -t 5000
exit 0
The user is passed as parameter by pòlkit
But the problem continues
UPDATE: I've just seen this bug https://bugs.launchpad.net/ubuntu/+source/libnotify/+bug/160598 that don't allows to send notifications as root.
Later I'll test to modify workaround changing user
UPDATE2: After change code to this. the problem continues:
#!/bin/bash
export XAUTHORITY="/home/$user/.Xauthority"
export DISPLAY=$(cat "/tmp/display.$user")
user=$1
su $user -c 'notify-send "Pendrive Reminder" "Shutdown lock enabled. Disconnect pendrive to enable shutdown" -u critical'
So, the second script is called correctly
– AlmuHS Feb 19 '18 at 20:49export DISPLAY XAUTHORITY
before notify-send. – mviereck Feb 19 '18 at 22:10echo $DISPLAY $XAUTHORITY > /home/$user/x.txt
and check the content ofx.txt
. Also: Does the script run as user$user
? There may be access restrictions to.Xauthority
forbidding access for other users than$user
. – mviereck Feb 19 '18 at 22:18xmessage "yourtext"
to make sure its not a problem with notify-send itself. – mviereck Feb 19 '18 at 22:20echo $DISPLAY $XAUTHORITY > /home/$user/x.txt
from my script, I check that x.txt file don't exists after press shutdown button. – AlmuHS Feb 19 '18 at 22:26/home/$user
. Tryecho $DISPLAY $XAUTHORITY $user > /tmp/x.txt
– mviereck Feb 19 '18 at 22:30echo $user > /tmp/user
and this file was created correcly with the username in it.So feels the second problem
– AlmuHS Feb 19 '18 at 22:32echo $DISPLAY $XAUTHORITY $(id) > /tmp/x.txt
? – mviereck Feb 19 '18 at 23:1315:30 /home/almu/.Xauthority uid=102(polkitd) gid=105(polkitd) groups=105(polkitd)
– AlmuHS Feb 19 '18 at 23:20In this version, I have two scripts linked to 2 udev events. The first script, furthermore creates the usb file, add the pkla polkit file; and the second, after remove id from file, if file is empty, also remove pkla polkit file.
But this version don't allow show notifications
– AlmuHS Feb 19 '18 at 23:2315:30
is not a valid value forDISPLAY
. Most times it is:0
or:1
. Another issue: most probablypolkitd
is not allowed to read.Xauthority
. I'll add a workaround to my answer. – mviereck Feb 19 '18 at 23:25By this reason, my rule file capture all possible shutdown events of each desktop environment and system.
The program try to be a reminder, with objective that the user don't forget its pendrive. By this reason, furthermore to lock shutdown when user connect pendrive, is needed a notify when the user try to shutdown
– AlmuHS Feb 20 '18 at 20:19/tmp/display.$user
exists. Therefore you need the autostart script provided in my answer. Instead ofXAUTHORITY
it works withxhost
. You need to read and understand the script snippets to use them. – mviereck Feb 23 '18 at 13:21