How do I properly launch a dbus-monitor --session as root?
I am working on a script that will detect if and when a Gnome session is "locked" by a user in order to kick that user and prevent people locking screens in a public use lab. However, it seems the only way to do this is to launch a dbus-monitor as follows:
dbus-monitor --session \ "type=signal,interface=org.gnome.ScreenSaver"
Unfortunately I can't get this to work as root, no matter how many things I try. I have tried the following:
1
eval `dbus-launch`
dbus-monitor --session ...
This fails to launch with the same error
2
export $(dbus-launch)
dbus-monitor --session ...
This launches but doesn't successfully monitor the messages.
3
eval `dbus-launch`
export DBUS_SESSION_BUS_ADDRESS
dbus-monitor --session ...
This launches but doesn't monitor the messages
The exact error when I do get the X11 session error is as follows:
Failed to open connection to session message bus: dbus-launch failed to autolaunch D-Bus session: Fd 4 did not have the close-on-exec flag set! Setting the flag.
Xlib: connection to ":0.0" refused by server
Xlib: No protocol specified
Autolaunch error: X11 initialization failed.
So simply, I need to be able to monitor the session org.gnome.ScreenSaver dbus messages remotely (ideally as root, since setting up a new user can be a pain) and can't figure out how to do that. It should also be added that if I'm logged in as a user I can run the dbus-monitor command by itself without any problems (it logs as expected).
More info because it can't hurt.
Kernel: RHEL5
2.6.18-406.el5
DBUS Version:
D-Bus Message Bus Launcher 1.1.2
GNOME Version:
2.16.0
Solution 1:
The problem is that dbus-monitor doesn't know where to connect to as it's running as a different user/session (root). You can get the DBUS ADDRESS from the environment with something like:
DBUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pidof -s gnome-session)/environ)
dbus-monitor --address "$DBUS_ADDRESS" "type=signal,interface=org.gnome.ScreenSaver"
Those commands try to find the running process with pidof -s gnome-session
, then look at the environment and grep for the DBUS_SESSION_BUS_ADDRESS and assign it to DBUS_ADDRESS, then uses the variable to tell dbus-monitor what message bus to monitor.
Keep in mind that if you have multiple sessions it will only work with the "first".