sd-bus.h 例子

注意:

sd-dbus 是systemd提供的lib,但是这个lib,只有在systemd>v221版本后才可以使用,centos 219版本太低,所以不能使用。

参考: http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html

#cat print-unit-path.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> #include <systemd/sd-bus.h>
#define _cleanup_(f) __attribute__((cleanup(f))) /* This is equivalent to:
* busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 \
* org.freedesktop.systemd1.Manager GetUnitByPID $$
*
* Compile with 'cc -lsystemd print-unit-path.c'
*/ #define DESTINATION "org.freedesktop.systemd1"
#define PATH "/org/freedesktop/systemd1"
#define INTERFACE "org.freedesktop.systemd1.Manager"
#define MEMBER "GetUnitByPID" static int log_error(int error, const char *message) {
fprintf(stderr, "%s: %s\n", message, strerror(-error));
return error;
} static int print_unit_path(sd_bus *bus) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
int r; // create m
r = sd_bus_message_new_method_call(bus, &m,
DESTINATION, PATH, INTERFACE, MEMBER);
if (r < 0)
return log_error(r, "Failed to create bus message"); r = sd_bus_message_append(m, "u", (unsigned) getpid());
if (r < 0)
return log_error(r, "Failed to append to bus message"); r = sd_bus_call(bus, m, -1, &error, &reply);
if (r < 0)
return log_error(r, "Call failed"); const char *ans;
r = sd_bus_message_read(reply, "o", &ans);
if (r < 0)
return log_error(r, "Failed to read reply"); printf("Unit path is \"%s\".\n", ans); return 0;
} int main(int argc, char **argv) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r; /* Connect to the system bus */
r = sd_bus_open_system(&bus);
if (r < 0)
return log_error(r, "Failed to acquire bus"); print_unit_path(bus);
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <systemd/sd-bus.h> static int method_multiply(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
int64_t x, y;
int r; /* Read the parameters */
r = sd_bus_message_read(m, "xx", &x, &y);
if (r < 0) {
fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r));
return r;
} /* Reply with the response */
return sd_bus_reply_method_return(m, "x", x * y);
} static int method_divide(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
int64_t x, y;
int r; /* Read the parameters */
r = sd_bus_message_read(m, "xx", &x, &y);
if (r < 0) {
fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r));
return r;
} /* Return an error on division by zero */
if (y == 0) {
sd_bus_error_set_const(ret_error, "net.poettering.DivisionByZero", "Sorry, can't allow division by zero.");
return -EINVAL;
} return sd_bus_reply_method_return(m, "x", x / y);
} /* The vtable of our little object, implements the net.poettering.Calculator interface */
static const sd_bus_vtable calculator_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("Multiply", "xx", "x", method_multiply, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("Divide", "xx", "x", method_divide, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END
}; int main(int argc, char *argv[]) {
sd_bus_slot *slot = NULL;
sd_bus *bus = NULL;
int r; /* Connect to the user bus this time */
r = sd_bus_open_user(&bus);
if (r < 0) {
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
goto finish;
} /* Install the object */
r = sd_bus_add_object_vtable(bus,
&slot,
"/net/poettering/Calculator", /* object path */
"net.poettering.Calculator", /* interface name */
calculator_vtable,
NULL);
if (r < 0) {
fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r));
goto finish;
} /* Take a well-known service name so that clients can find us */
r = sd_bus_request_name(bus, "net.poettering.Calculator", 0);
if (r < 0) {
fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r));
goto finish;
} for (;;) {
/* Process requests */
r = sd_bus_process(bus, NULL);
if (r < 0) {
fprintf(stderr, "Failed to process bus: %s\n", strerror(-r));
goto finish;
}
if (r > 0) /* we processed a request, try to process another one, right-away */
continue; /* Wait for the next request to process */
r = sd_bus_wait(bus, (uint64_t) -1);
if (r < 0) {
fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r));
goto finish;
}
} finish:
sd_bus_slot_unref(slot);
sd_bus_unref(bus); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

Refs

http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html

Getting start with dbus in systemd (03) - sd-bus.h 使用例子 (systemd version>=221)的更多相关文章

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

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

  2. systemd 和 如何修改和创建一个 systemd service (Understanding and administering systemd)

    系统中经常会使用到 systemctl 去管理systemd程序,刚刚看了一篇关于 systemd 和 SysV 相关的文章,这里简要记录一下: systemd定义: (英文来解释更为原汁原味) sy ...

  3. [systemd]How To Use Systemctl to Manage Systemd Services and Units

    转自: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services ...

  4. systemd的新特性及常见的systemd unit类型分析

    systemd概述 )systemd是一种新的linux系统服务管理器,用于替换init系统,能够管理系统启动过程和系统服务,一旦启动起来,就将监管整个系统.在centos7系统中,PID1被syst ...

  5. CentOS 7下关于systemd的一些唠叨话二:systemd服务脚本的编写

    CentOS 7继承了RHEL 7的新的特性,例如强大的systemd,而systemd的使用也使得以往系统服务的/etc/init.d的启动脚本的方式就此改变,也大幅提高了系统服务的运行效率.但服务 ...

  6. day63-webservice 03.解析cxf提供的例子

    Path配置: C:\Program Files (x86)\ScanSign;E:\app\zhongzh\product\11.2.0\dbhome_1\bin;D:\app\zhongzh\pr ...

  7. Systemd入门教程:命令篇

    导读 传统的Linux系统启动过程主要由著名的init进程(也被称为SysV init启动系统)来处理,而基于init的启动系统被认为有效率不足的问题,systemd是Linux系统机器的另一种启动方 ...

  8. Linux 系统的总管 Systemd

    目录 1. init的进化,全功能的Systemd 2 1.1 Linux系统中,init主要有3个版本 2 1.2 比较传统的init程序,Systemd的特点有: 2 1.3 Systemd Jo ...

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

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

随机推荐

  1. Java:String和Date、Timestamp之间的转换【转】

    原文地址:http://yunnick.iteye.com/blog/1074495 一.String与Date(java.util.Date)互转 1.1 String -> Date Str ...

  2. Zend Studio如何调试?

    1.安装Zend Studio之前,本机已安装Apache2.如果使用Apache2作为服务器 Window-Preferences-Php-Php Servers 配置好 URL和Server Ro ...

  3. dpdpdpdp~~~!!!

    dpdpdpdpdpdp D你妹个P!  妈的劳资就不信征服不了你!!哼!!

  4. leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  5. BZOJ_4398_福慧双修&&BZOJ_2407_探险_分治+dij

    BZOJ_4398_福慧双修&&BZOJ_2407_探险_分治+dij Description 菩萨为行,福慧双修,智人得果,不忘其本. ——唐朠立<大慈恩寺三藏法师传> ...

  6. 10.16NOIP模拟赛

    /* 我是一个大sb */ #include<iostream> #include<cstdio> #include<cstring> #include<qu ...

  7. P4128 [SHOI2006]有色图

    传送门 数学渣渣看题解看得想死Ծ‸Ծ 首先发现这玩意儿看着很像polya定理 \[L=\frac{1}{|G|}\sum_{i\in G}m^{w(i)}\] 然而polya定理只能用来求点的置换,边 ...

  8. lnmp环境的nginx的tp5配置

    php7.1 server { listen 80; server_name www.tp5.com; access_log /home/wwwroot/access.log combined; er ...

  9. 【计蒜客习题】 取石子游戏(gcd)

    问题描述 蒜头君和花椰妹在玩一个游戏,他们在地上将 n 颗石子排成一排,编号为 1 到 n.开始时,蒜头君随机取出了 2 颗石子扔掉,假设蒜头君取出的 2 颗石子的编号为 a, b.游戏规则如下,蒜头 ...

  10. python自动化测试学习笔记-9测试框架

    学习了这么久的python,我们已经可以自己搭建一个简单的测试和框架了,先从简单的开始,有时我们编写接口的测试用例会用excel进行编写,以下面的接口测试用例模板为例,进行编写: