3

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
Merglyn
  • 33
  • 1
  • 1
  • 3

1 Answers1

6

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".

Pablo Martinez
  • 2,326
  • 16
  • 13
  • I'll try this monday and let you know if it works, but that seems about right to me. Thanks. – Merglyn Jun 19 '15 at 20:42
  • Our version of DBUS doesn't have the --address option, so I exported the result instead, and got the following error: `Failed to open connection to session message bus: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.` I was running as root. Any ideas? – Merglyn Jun 23 '15 at 15:14
  • Should add that there only is one session open when I run pidof gnome-session without -s, so it's the right one. – Merglyn Jun 23 '15 at 15:18
  • If you know the user you could use one quick and dirty trick, use sudo as the user: `sudo -u *username* -E dbus-monitor --session "type=signal,interface=org.gnome.ScreenSaver"`(the -E option to preserve the environment so dbus-monitor can access the exported DBUS_ADDRESS. If that doesn't work you may want to add ``to the in the session.conf file for DBUS (in Fedora is in /etc/dbus-1/session.conf.I hope something of that works. – Pablo Martinez Jun 23 '15 at 16:06