D-Bus service activation

本文探讨了D-Bus服务的启动流程,包括Sessionbus与Systembus服务的区别,以及如何利用dbusdaemonlaunchhelper自动启动服务。通过创建特定的.service文件并配置相关属性,如Name、Exec、User等,程序员可以轻松实现服务的自动激活。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 最近更新: 2010-05-06

D-Bus service activation

當一名程序員實作了一個 daemon 時,他將會面臨一個關於系統啟動流程的問題。他要如何安排啟動流程,才能正常地啟動他的 daemon? 要放在 init.d 嗎?還是放在 rc.local?再者,每種 Linux 或 BSD 系統的啟動流程都有差異,更是為這項工作增加不少負擔。而 D-Bus service 的運作模式基本上也是一個 daemon ,所以當程序員實作了一個 D-Bus service 後,按理也是要為安排啟動流程煩惱。

所幸 D-Bus 有一個 dbus daemon launch helper 的功能,可以免除程序員安排 D-Bus service 啟動程序的困惱。只要按照 D-Bus 規格上說明的方式,寫好 .service 文件,dbus daemon lauhcn helper 就會在使用者呼叫指定 D-Bus service 時,自動啟動該 D-Bus service 程序。程序員不必煩惱 D-Bus service 程序的啟動問題。

Session bus service

Session bus service 是跟隨使用者桌面環境期間的行程,所以它執行時的 UID,GID 為此桌面期間的使用者。

假設有一個 Session bus service ,其 service name 為 blog.rock.Hello ,程式名稱是 /usr/local/bin/HelloService。那麼我們可以編輯一個與 service name 同名的 blog.rock.Hello.service 文件,其內容如下:

#filename: blog.rock.Hello.service
[D-BUS Service]
Name=blog.rock.Hello
Exec=/usr/local/bin/HelloService

最後將此文件複製到 /usr/share/dbus-1/services 目錄下。 Dbus daemon launch helper 就會知道該如何啟動這個程序。

D-Bus 規格文件上有一個疏失,規格文件上指出 .serivce 文件中,可以用 Names 指定一或多個 serivce name。但實際上無效。應該用 Name ,且指定與 .service 文件同名的 service name。

System bus service

System bus service 屬於系統層級的行程,它執行時的預設身份將會是 root ,或者你也可以指定執行身份。因此,dbus daemon lauhcn helper 啟動 System bus service 程序時,比 Session bus service 多了一道安全機制。

假設有一個 System bus service ,其 service name 為 blog.rock.sys ,程式名稱是 /usr/local/bin/rock_sys。那麼我們可以編輯一個與 service name 同名的 blog.rock.sys.service 文件,其內容如下:

#filename: blog.rock.sys.service
[D-BUS Service]
Name=blog.rock.sys
Exec=/usr/local/bin/rock_sys
User=root

System bus service 的 .service 文件,多一個 User 欄位,指定此服務行程的身份。

將此文件複製到 /usr/share/dbus-1/system-services 目錄下。

接著,配合 dbus daemon lauhcn helper 啟動 System bus service 程序的安全機制,我們還要新增一份與 service 同名的組態文件。在此例中,此文件名稱是 blog.rock.sys.conf 。最少內容如下:

<!DOCTYPE busconfig PUBLIC
 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
  <!-- Only root could launch this service -->
  <policy user="root">
    <allow own="blog.rock.sys"/>
  </policy>

  <!-- Allow anyone to invoke methods of interface -->
  <policy context="default">
    <allow send_destination="blog.rock.sys"
           send_interface="org.freedesktop.DBus.Introspectable"/>
    <allow send_destination="blog.rock.sys"
           send_interface="blog.rock.sys"/>
  </policy>
</busconfig>

將此文件複製到 /etc/dbus-1/system.d。安排好這兩份文件後,Dbus daemon launch helper 就會知道該如何啟動這個程序。

[root@Docker-register ~]# systemctl list-units --type=service --state=running UNIT LOAD ACTIVE SUB DESCRIPTION auditd.service loaded active running Security Auditing Service chronyd.service loaded active running NTP client/server couchbase-server.service loaded active running Couchbase Server crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus docker.service loaded active running Docker Application Container Engine getty@tty1.service loaded active running Getty on tty1 gssproxy.service loaded active running GSSAPI Proxy Daemon irqbalance.service loaded active running irqbalance daemon lvm2-lvmetad.service loaded active running LVM2 metadata daemon metricbeat.service loaded active running Metricbeat is a lightweight shipper for metrics. NetworkManager.service loaded active running Network Manager polkit.service loaded active running Authorization Manager postfix.service loaded active running Postfix Mail Transport Agent rhnsd.service loaded active running LSB: Starts the Spacewalk Daemon rhsmcertd.service loaded active running Enable periodic update of entitlement certificates. rpcbind.service loaded active running RPC bind service rsyslog.service loaded active running System Logging Service sshd.service loaded active running OpenSSH server daemon systemd-journald.service loaded active running Journal Service systemd-logind.service loaded active running Login Service systemd-udevd.service loaded active running udev Kernel Device Manager tuned.service loaded active running Dynamic System Tuning Daemon vgauthd.service loaded active running VGAuth Service for open-vm-tools vmtoolsd.service loaded active running Service for virtual machines hosted on VMware zabbix-agent.service loaded active running Zabbix Agent LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type. 26 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'.
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值