I have a script that has to be run under root, with bindings to dbus.

DBUS_SESSION_BUS_ADDRESS = 'address set under .dbus file ...'

class NewApp(dbus.service.Object):

    def __init__(self):
        busname = dbus.service.BusName('org.newapp', dbus.SessionBus(mainloop=DBusGMainLoop()))
        dbus.service.Object.__init__(self, busname, '/org/newapp')

    @dbus.service.method('org.newapp', out_signature='s')
    def do_stuff(self):
        return 'stuff'

if __name__ == '__main__':
    na = NewApp

Where do_stuff has code that needs to have root access. I have found that when you run the program under root, this creates a separate dbus session and I would need to set the 'DBUS_SESSION_BUS_ADDRESS'. I have thought about separating the script into to two different files and just import the other but, I would like to know how to do it this way too.

This is giving me trouble, I'm not sure where or how to set the address properly. If anyone has any insight I would greatly appreciate it.

  • What are you trying to do exactly? – dobey Dec 24 '12 at 2:28
  • @dobey this application will be doing several things, mainly for monitoring system processes and i/o of those processes. The reason for root is so I can read certain restricted files in '/proc' – tijko Dec 24 '12 at 3:31

If you are writing a script which requires running as root, and needs to be accessed via DBus by users some how, you should be exposing the API via the System bus, not the Session bus.

As an example, see how the NetworkManager system service works.

For further documentation on how permissions work in DBus, see http://www.freedesktop.org/wiki/Software/dbus

| improve this answer | |
  • But that is exactly what you're doing. You're providing a DBus service. And you need to access things that require elevated privileges. If that's not what you want, then you probably shouldn't use DBus to do it. – dobey Dec 24 '12 at 16:38
  • This link isn't exactly what I'm trying to do but, it is similar where the poster wants to connect to an existing dbus session not create a new one under a different user(my user being root) stackoverflow.com/questions/2143785/… – tijko Dec 24 '12 at 17:44
  • That question and its answer are totally wrong. You should never run dbus-launch in your own program like that. – dobey Dec 24 '12 at 18:13
  • Running dbus-launch is what creates a separate session. It doesn't matter who you run it as. Running a program which connects to some service on the session bus, will cause dbus to try and start a new session bus, assuming that it can't contact an existing one, and that it can talk to an X display. Otherwise it will just fail. Also, you cannot talk to a session bus as anyone other than the user that owns the session bus, by default, as doing so would be a security breech. – dobey Dec 24 '12 at 19:24
  • 1
    You misunderstand. It is set by dbus-daemon when it is started in the user's session. You should never need to set it yourself. And even if you did, root does not have permission to talk to the user's session bus. If you want to run a service as root, and have user applications talk to it, you need to host the service on the System bus. – dobey Dec 24 '12 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.