Getting start with dbus in systemd (02)

创建一个私有的dbus-daemon (session)

环境

这里我们会有两个app: app1(client),app2(server), 然后,再启动一个“dbus-daemon (session)”

我们期望,app1 和 app2 之间的通信,可以使用 刚才启动的 “dbus-daemon”

代码在:

git clone https://github.com/muahao/hello-dbus3-0.1.git

环境:

  • centos 7

依赖:

  • dbus-x11-1.6.12-13.1.rhel7.x86_64 (提供dbus-launch)
  • dbus-glib-0.100-7.1.rhel7.x86_64(代码实现依赖)

启动一个dbus-daemon (session)

方式1: 使用dbus-launch 创建一个dbus-daemon:

#yum install -y dbus-x11-1:1.6.12-13.1.alios7.x86_64

#dbus-launch
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-7Q7Spuq5IH,guid=079edc76e4c5c6433d3507855c5260ce
DBUS_SESSION_BUS_PID=121376

方式2: 手动启动

#DBUS_VERBOSE=1 dbus-daemon --session --print-address
unix:abstract=/tmp/dbus-jXwkggHTo2,guid=dc666ee7ba7ddf788efd8c485c526564

两个方式的目的,不仅仅是启动dbus-daemon, 更重要的是,获得address.

注意,这里会反馈一个地址, unix:abstract=/tmp/dbus-7Q7Spuq5IH,guid=079edc76e4c5c6433d3507855c5260ce , 所以,你需要保证 你的环境变量 DBUS_SESSION_BUS_ADDRESS的值就是这个地址。

其实dbus-daemon是有地址的,而且有一个环境变量来表示它--DBUS_SESSION_BUS_ADDRESS,可以用命令env查看到。我们的程序,也就就是依靠这个环境变量来确认使用哪一个dbus-daemon的。

当我们登录进桌面环境的时候,系统启动脚本会调用到dbus-launch来启动一个dbus-daemon,同时会把这个dbus-daemon的地址赋予环境变量DBUS_SESSION_BUS_ADDRESS

#ps axu | grep dbus-daemon  #新增一个dbus-daemon
dbus 91405 0.0 0.0 24312 2728 ? Ss Jan30 0:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root 121376 0.0 0.0 24312 228 ? Ss 10:43 0:00 /bin/dbus-daemon --fork --print-pid 4 --print-address 6 --session

设置环境变量DBUS_SESSION_BUS_ADDRESS

设置环境变量DBUS_SESSION_BUS_ADDRESS到为刚才启动的dbus-daemon 的地址:

#DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-7Q7Spuq5IH,guid=079edc76e4c5c6433d3507855c5260ce

启动server:

这里会有一个报错:

#./example-service
Couldn't connect to session bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11

Fix: https://www.cnblogs.com/chutianyao/p/3770627.html

#eval `dbus-launch --sh-syntax`

正常运行:

server:

#./example-service
service running

client:

#./example-client
sum is 1099
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.freedesktop.DBus.Introspectable">
<method name="Introspect">
<arg name="data" direction="out" type="s"/>
</method>
</interface>
<interface name="org.freedesktop.DBus.Properties">
<method name="Get">
<arg name="interface" direction="in" type="s"/>
<arg name="propname" direction="in" type="s"/>
<arg name="value" direction="out" type="v"/>
</method>
<method name="Set">
<arg name="interface" direction="in" type="s"/>
<arg name="propname" direction="in" type="s"/>
<arg name="value" direction="in" type="v"/>
</method>
<method name="GetAll">
<arg name="interface" direction="in" type="s"/>
<arg name="props" direction="out" type="a{sv}"/>
</method>
</interface>
<interface name="org.fmddlmyy.Test.Basic">
<method name="Add">
<arg name="arg0" type="i" direction="in"/>
<arg name="arg1" type="i" direction="in"/>
<arg name="ret" type="i" direction="out"/>
</method>
</interface>
</node>

Ref:

https://blog.csdn.net/jack0106/article/details/5588057

http://www.fmddlmyy.cn/text49.html

Getting start with dbus in systemd (02) - How to create a private dbus-daemon的更多相关文章

  1. Getting start with dbus in systemd (01) - Interface, method, path

    Getting start with dbus in systemd (01) 基本概念 几个概念 dbus name: connetion: 如下,第一行,看到的就是 "dbus name ...

  2. Getting start with dbus in systemd (03) - sd-bus.h 使用例子 (systemd version>=221)

    sd-bus.h 例子 注意: sd-dbus 是systemd提供的lib,但是这个lib,只有在systemd>v221版本后才可以使用,centos 219版本太低,所以不能使用. 参考: ...

  3. CentOS 7下关于systemd的一些唠叨话一:systemd的特点和使用

    摘要 近年来,Linux 系统的 init 进程经历了两次重大的演进,传统的 sysvinit 已经逐渐淡出历史舞台,新的 UpStart 和 systemd 各有特点,越来越多的 Linux 发行版 ...

  4. 浅析 Linux 初始化 init 系统,第 1 部分: sysvinit 第 2 部分: UpStart 第 3 部分: Systemd

    浅析 Linux 初始化 init 系统,第 1 部分: sysvinit  第 2 部分: UpStart 第 3 部分: Systemd http://www.ibm.com/developerw ...

  5. 【转发】CentOS 7 巨大变动之 systemd 取代 SysV的Init

    1 systemd是什么 首先systmed是一个用户空间的程序,属于应用程序,不属于Linux内核范畴,Linux内核的主要特征在所有发行版中是统一的,厂商可以自由改变的是用户空间的应用程序.   ...

  6. [Z] 深入浅出 Systemd

    1. Systemd 的简介和特点 Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度.systemd 和 u ...

  7. D-Bus,kdbus和Binder

    http://blog.sina.com.cn/s/blog_4af327e10101irie.html 材料来自:The unveiling of kdbus 和 Kdbus Details .后一 ...

  8. Systemd 添加自定义服务(开机自启动)

    Systemd 简介:https://fedoraproject.org/wiki/Systemd/zh-cn 一.service unit 常用命令,以 mysql 服务为例 # 开机启动 syst ...

  9. CentOS 7 之 Systemd 入门教程:实战篇

    开机启动对于那些支持 Systemd 的软件,安装的时候,会自动在/usr/lib/systemd/system目录添加一个配置文件. 如果你想让该软件开机启动,就执行下面的命令(以httpd.ser ...

随机推荐

  1. Java ServerSocket的服务端代码介绍

    转自:http://developer.51cto.com/art/201003/190007.htm 所谓Java ServerSocket通常也称作"套接字",有不少的时候需要 ...

  2. 2-6 Opencv模块组织结构

    https://opencv.org/releases.html https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.4 ...

  3. FluentData - 轻量级.NET ORM持久化技术解决方案

    官方地址:http://fluentdata.codeplex.com/ 官方教程:http://fluentdata.codeplex.com/documentation FluentData入门 ...

  4. JNI编程(一) —— 编写一个最简单的JNI程序(转载)

    转自:http://chnic.iteye.com/blog/198745 忙了好一段时间,总算得了几天的空闲.貌似很久没更新blog了,实在罪过.其实之前一直想把JNI的相关东西整理一下的,就从今天 ...

  5. Mybatis invalid comparison: java.util.Date and java.lang.String

    Mybatis的实体类是java.utils.Date类型,而在Mybatis的XML文件中,使用if判断了,不需要判断是否等于空字符串这种判断,需要人真一些 相关:https://blog.csdn ...

  6. vue中的列表项删除操作

    <script> Vue({ data: { orders: [] }, created() { $.get( { url: 'orders', dataType: 'json' }) . ...

  7. JavaScript实现对象的深度克隆及typeof和instanceof【简洁】【分享】

    JavaScript实现对象的深度克隆 代码实现如下: <!DOCTYPE html> <html lang="en"> <head> < ...

  8. 3198: [Sdoi2013]spring【容斥原理+hash】

    容斥是ans= 至少k位置相等对数C(k,k)-至少k+1位置相等对数C(k+1,k)+至少k+2位置相等对数*C(k+2,k) -- 然后对数的话2^6枚举状态然后用hash表统计即可 至于为什么要 ...

  9. bzoj 1600: [Usaco2008 Oct]建造栅栏【dp】

    要求三边和大于第四边,所以任意一条边的长度都是小于n/2 设f[i][j]为前i条长为j,转移的时候用n/2限制 #include<iostream> #include<cstdio ...

  10. P3162 [CQOI2012]组装

    传送门 退火大法好 我并不会正解于是只好打退火了--其他没啥好讲--只要对每一种颜色开一个vector,存一下所有这个颜色的位置,判定的时候可以去所有的颜色里二分找到前缀和后缀,把和当前点距离小的加入 ...