Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a d-bus activated systemd service?

Tags:

systemd

dbus

Does anyone have an example, or a link to an example of how to define a systemd .service which is activated by d-bus?

My understanding is that if I create a test.service file here:

/usr/share/dbus-1/services/test.service

With the following contents:

[D-BUS Service]
Name=org.me.test
Exec="/tmp/testamundo.sh"

Can the service now be started/stopped via d-bus calls to systemd.Manager? If so, how?

like image 384
RandomUser Avatar asked Jul 29 '15 13:07

RandomUser


1 Answers

Lets take a look at one of the services coming with systemd, hostnamed.

# cat /usr/share/dbus-1/system-services/org.freedesktop.hostname1.service

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[D-BUS Service]
Name=org.freedesktop.hostname1
Exec=/bin/false
User=root
SystemdService=dbus-org.freedesktop.hostname1.service

The magic is SystemdService= directive. The service specified with SystemdService= is what dbus-daemon asks systemd to activate.

We are expecting a service called dbus-org.freedesktop.hostname1.service in systemd service directory.

# readlink /usr/lib/systemd/system/dbus-org.freedesktop.hostname1.service
systemd-hostnamed.service

There you go, this way a dbus service org.freedesktop.hostname1.service tells systemd to activate a systemd service systemd-hostnamed.service.

And the systemd service looks like

# cat /usr/lib/systemd/system/systemd-hostnamed.service
...
...
[Service]
BusName=org.freedesktop.hostname1
...
...

The magic on the systemd service file is BusName= directive. This directive tells systemd to wait until the given bus name to appear on the bus before proceeding.

Note: A dbus service has completely different syntax than systemd service. You need both to be able to have a dbus activated daemon.

like image 136
Umut Avatar answered Sep 21 '22 17:09

Umut