Hi Folks
I have been using a small stolen script which locks the screen if i unplug a usb-key.
It used to work just fine. And i cannot remember when it stopped working.
I am using:
- Fedora 17
- with 3.6.x Kernel
- gnome-screensaver 3.4.1
I used gnome-screensaver, but xscreensaver wont work either.
The screen locking itself works just fine if enter
Code:
gnome-screensaver-command --lock
or, for dbus
Code:
dbus-send --session --dest=org.gnome.ScreenSaver --type=method_call --print-reply /ScreenSaver org.gnome.ScreenSaver.Lock
directly on the shell.
I use the same command in my script.
The udev rule which actually starts the script does still work.
I can see in the log output that it tries to lock the screen.
So i start gnome-screensaver in foreground with debug and i see a dbus message, which is not there if i enter the "gnome-screensaver-command --lock" directly on the shell.
Code:
[listener_dbus_handle_system_message] gs-listener-dbus.c:854 (11:40:08): obj_path=/org/freedesktop/login1/user/1000 interface=org.freedesktop.DBus.Properties method=PropertiesChanged destination=(null)
I am wondering what is required to pass this command from a script, running as root via udev.
So this would be my firtst question, requirements.
I spent some time with google and figured out that you need or may need these things:
- DBUS_SESSION_BUS_ADDRESS
- username of the user running the gnome session which you want to lock
- the DISPLAY
- content of .Xauthority (mit-magic-cookie ??? and what not)
#1
It is said that one can find the DBUS_SESSION_BUS_ADDRESS in the environment of the gnome-screensaver process. so lets see:
Code:
grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps ax | grep gnome-screensaver | grep -v grep | awk {'print $1'})/environ
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-WTEFs92yxR,guid=64245ee4478d308c5aad048235643089313
#2
ok, for testing reasons i know who i am and hard-coded it into the script
#3
The script will get the Display form /tmp/.X11-unix/. and since there is only one X and i am the only user at the localhost, i believe it if the result is "0"
#4
There used to be a .Xauthority in my home. (i dont remember which century that was)
Now, there is apparently something like
/var/run/gdm/auth-for-user-73sgPH/database
which might be the same, if you trust "xauth -v"
So,
i think i have everything, maybe even too much.
But i cant firure out why the screensaver does not lock the screen anymore.
Do you guys have any idea what i can try ?
Or what is missing ?
Here is the script:
(It got a little messy, sorry)
Code:
# set static username if script is not able to get the correct name
username=logged-in-user
getXuser() {
user=$username
# ~/.Xauthority is manually created link from /var/run/gdm/auth-for-user-xxxx/database
if [ x"$user" != x"" ]; then
userhome=`getent passwd $user | cut -d: -f6`
logger "DEBUG: $(date) userhome: '$userhome'"
export XAUTHORITY=$userhome/.Xauthority
else
export XAUTHORITY=""
fi
logger "DEBUG: $(date) Xauthority file: $XAUTHORITY"
logger "DEBUG: $(date) user found: '$user'"
}
get_dbus_session() {
export "$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps ax | grep gnome-screensaver | grep -v grep | awk {'print $1'})/environ)"
}
if [ "$1" = "lock" ] ; then
for x in /tmp/.X11-unix/*; do
displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
logger "DEBUG: $(date) displaynum: '$displaynum'"
getXuser
if [ x"$XAUTHORITY" != x"" ]; then
# extract current state
export DISPLAY=":$displaynum"
fi
logger "DEBUG: $(date) Display:\"$DISPLAY\""
done
userhome=`getent passwd $user | cut -d: -f6
get_dbus_session`
export XAUTHORITY=$userhome/.Xauthority
logger "DEBUG: $(date) userhome: '$userhome'"
logger "DEBUG: $(date) Xauthority file: $XAUTHORITY"
logger "DEBUG: $(date) dbus session bus address: $DBUS_SESSION_BUS_ADDRESS"
su $user -c "/usr/bin/gnome-screensaver-command --lock"
su $user -c"dbus-send --session --dest=org.gnome.ScreenSaver --type=method_call --print-reply /ScreenSaver org.gnome.ScreenSaver.Lock"
elif [ "$1" = "unlock" ]; then
for x in /tmp/.X11-unix/*; do
displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
getXuser
if [ x"$XAUTHORITY" != x"" ]; then
# extract current state
export DISPLAY=":$displaynum"
fi
logger "DEBUG: $(date) Display: $DISPLAY"
done
get_dbus_session
su $user -c "/usr/bin/gnome-screensaver-command -d"
fi ;
exit 0