Let's assume this kind of situation: we have one user logged in, executing some application through sudo
or su
. This user has got a dbus-daemon
running.
However, when an application running with root privileges tries to access D-Bus, it just spawns another dbus-daemon
, owned by root user. That's not a desired situation.
Is there a way to gain access to D-Bus session of user who ran the application through sudo
or su
?
dbus-run-session is used to start a session bus instance of dbus-daemon from a shell script, and start a specified program in that session. The dbus-daemon will run for as long as the program does, after which it will terminate.
dbus/session-bus for the session bus address based on the machine id and DISPLAY environment variable. This contains the location of the session bus that we need and might be sufficient to accomplishing the goal of not requiring the DBUS_SESSION_BUS_ADDRESS to be set in the environment.
D-Bus is an inter-process communication (IPC) mechanism initially designed to replace the software component communications systems used by the GNOME and KDE Linux desktop environments (CORBA and DCOP respectively).
D-Bus is first a library that provides one-to-one communication between any two applications; dbus-daemon is an application that uses this library to implement a message bus daemon. Multiple programs connect to the message bus daemon and can exchange messages with one another.
If you're on a systemd distro, the relatively new machinectl shell
command can do the work of su
/sudo
, and it will also set session variables like XDG_RUNTIME_DIR
and DBUS_SESSION_BUS_ADDRESS
. So for example, if I want to run systemctl --user
as user test
, the normal approach will fail:
$ sudo --user=test systemctl --user
Failed to connect to bus: No such file or directory
But this way works:
$ sudo machinectl shell --uid=test .host -- /usr/bin/systemctl --user
If you need to "reach back" into the user session that invoked a sudo script, you could use the SUDO_USER
/SUDO_UID
to hack something together.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With