大网数据广告活动
服务器运维问答

One Dbus message triggers multiple systemd services

· 52CE 我爱测

How can I trigger multiple systemd services when a dbus signal is lauched. I'm trying to do so for org.freedesktop.hostname1 by creating a service like: [Unit] Description=Set host name [Service] ExecStart=/home/administrator/set…

问题原文

原帖链接 · jlanza · CC BY-SA 4.0

How can I trigger multiple systemd services when a dbus signal is lauched.

I'm trying to do so for org.freedesktop.hostname1 by creating a service like:

[Unit]
Description=Set host name

[Service]
ExecStart=/home/administrator/set-hostname
BusName=org.freedesktop.hostname1

After enabling it and run netplan apply the script is not executed.

TA.

已采纳解答

原帖链接 · u1686_grawity · CC BY-SA 4.0

I assume that by 'triggering' a service you want a new service to start running (which is something that systemd can do) instead of making an already running service do something (which is completely out of the init system's reach).

D-Bus basics

Each D-Bus message has exactly one 'recipient' field (which may be either a well-known name such as org.example, an unique name such as :1.234, or the broadcast address).

D-Bus signals (sent to publish events that already happened) are usually broadcast to the entire bus, so they cannot cause any services to be started. If you're thinking about unicast messages sent to a specific service, those are usually method_calls, not signals.

D-Bus service autostart

The bus daemon will automatically start services when:

  1. there is a message sent to a specific well-known name,
  2. and that well-known service isn't currently "claimed" on the bus,
  3. and that well-known name has a corresponding D-Bus .service file (not to be confused with systemd .service files) under /usr/share/dbus-1/system-services/.

It's important to note from point #2 that only one service may claim a well-known name and receive messages destined for that name. As long as the real systemd-hostnamed is already running and has claimed org.freedesktop.hostname1, it will receive those messages – other processes cannot do the same.

The same point #2 also means that autostart will not happen for names that a service is currently holding. The message will simply be delivered to that already-running service.

In point #3, file's name must exactly match the well-known bus name; i.e. you cannot have multiple services competing for autolaunch under the same name. Therefore, as far as dbus-daemon cares, each message can only start one service at most.

Autostart via systemd

If the D-Bus .service file (under /usr/share/dbus-1/…/) contains a SystemdService= option, dbus-daemon will attempt to start that service via systemd instead of traditionally forking/execing whatever is listed in Exec=.

(Often the systemd service installs an Alias named dbus-[busname].service – this is only a convention to allow for systemctl enabling/disabling, and the syntax has no special meaning beyond that.)

Since systemd services can have dependencies, you now have a workaround for the "each message can only start one service" limitation: the service being directly activated can pull in other services as dependencies. However, everything explained before still applies – once the 'real' service is running, further messages will reach it directly and will not trigger autostart.

Conclusion

It seems like you're trying to avoid systemd-hostnamed completely, and whenever netplan calls hostnamed's SetHostname() method, you want your own program to receive it.

This is not possible with a simple shell script. You cannot receive D-Bus messages via stdin, nor via environment variables, nor via command-line arguments. Your script will only know that it was started for some reason, but it will have no clue as to what it's supposed to be doing exactly.

But it is possible in general – as long as you use a programming language that has a D-Bus module. You "just" need to connect to the system bus, claim the name, receive messages, and handle them the same way systemd-hostnamed does. Many dbus modules come with "skeleton D-Bus service" examples.

(In fact there already are third-party reimplementations of various systemd daemons, e.g. LoginKit, systembsd, systemd-shim.)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-08-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。