首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用sessionbus时DBUS未能打开总线

使用sessionbus时DBUS未能打开总线
EN

Unix & Linux用户
提问于 2020-02-19 17:29:57
回答 1查看 897关注 0票数 2

我正在尝试创建/启动一个使用会话总线的服务。它不一定是会话总线,我只是想让它工作,不知道两者之间的区别,就像免责声明一样。

基本上,我正在查看sdbus-cpp库并遵循它们的示例。

我的服务档案:

代码语言:javascript
运行
复制
[Unit]
Description=org.sdbuscpp.concatenator

[Service]
Type=simple
ExecStart=/home/john/dev/dbus/server/build/dbus-server
StandardOutput=journal
User=john

[Install]
WantedBy=multi-user.target

我的配置:

代码语言:javascript
运行
复制
<!DOCTYPE busconfig PUBLIC
 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
  <policy user="john">
    <allow own="org.sdbuscpp.concatenator"/>
    <allow send_destination="org.sdbuscpp"/>
    <allow send_interface="org.sdbuscpp.concatenator"/>
  </policy>
</busconfig>

以及来自GitHub的示例代码:

代码语言:javascript
运行
复制
#include <sdbus-c++/sdbus-c++.h>

#include <string>
#include <vector>

// Yeah, global variable is ugly, but this is just an example and we want to access 

// the concatenator instance from within the concatenate method handler to be able
// to emit signals.
sdbus::IObject* g_concatenator{};

void concatenate(sdbus::MethodCall call)
{
    // Deserialize the collection of numbers from the message
    std::vector<int> numbers;
    call >> numbers;

    // Deserialize separator from the message
    std::string separator;
    call >> separator;

    // Return error if there are no numbers in the collection
    if (numbers.empty())
    {
        throw sdbus::Error("org.sdbuscpp.Concatenator.Error", "No numbers provided");
    }

    std::string result;
    for (auto number : numbers)
    {
        result += (result.empty() ? std::string() : separator) + std::to_string(number);
    }

    // Serialize resulting string to the reply and send the reply to the caller
    auto reply = call.createReply();
    reply << result;
    reply.send();

    // Emit 'concatenated' signal
    const char* interfaceName = "org.sdbuscpp.Concatenator";
    auto signal = g_concatenator->createSignal(interfaceName, "concatenated");
    signal << result;
    g_concatenator->emitSignal(signal);
}

int main()
{
    // Create D-Bus connection to the system bus and requests name on it.
    const char* serviceName = "org.sdbuscpp.concatenator";
    // auto connection = sdbus::createSystemBusConnection(serviceName);
    auto connection = sdbus::createSessionBusConnection(serviceName);

    // Create concatenator D-Bus object.
    const char* objectPath = "/org/sdbuscpp/concatenator";
    auto concatenator = sdbus::createObject(*connection, objectPath);

    g_concatenator = concatenator.get();

    // Register D-Bus methods and signals on the concatenator object, and exports the object.
    const char* interfaceName = "org.sdbuscpp.Concatenator";
    concatenator->registerMethod(interfaceName, "concatenate", "ais", "s", &concatenate);
    concatenator->registerSignal(interfaceName, "concatenated", "s");
    concatenator->finishRegistration();

    // Run the I/O event loop on the bus connection.
    connection->enterProcessingLoop();
}

这就是我开始这项服务时得到的结果:

代码语言:javascript
运行
复制
Feb 19 17:59:01 box systemd[1]: Started org.sdbuscpp.concatenator.
Feb 19 17:59:02 box dbus-server[828]: terminate called after throwing an instance of 'sdbus::Error'
Feb 19 17:59:02 box dbus-server[828]:   what():  [org.freedesktop.DBus.Error.FileNotFound] Failed to open bus (No such file or directory)
Feb 19 17:59:02 box systemd[1]: org.sdbuscpp.concatenator.service: Main process exited, code=dumped, status=6/ABRT
Feb 19 17:59:02 box systemd[1]: org.sdbuscpp.concatenator.service: Failed with result 'core-dump'.

如果我将代码更改为使用系统总线,服务就会启动,但是当我发现使用d-脚的服务时,我会在单击它之后得到一条错误消息。

我做错了什么/理解错了什么?我在运行Ubuntu 19.10。

EN

回答 1

Unix & Linux用户

发布于 2022-08-25 08:19:14

我把掘墓人头盔戴上了..。

也许这对其他读者是有帮助的:

执行

代码语言:javascript
运行
复制
dbus-launch

在终端中应该修复所描述的问题。

票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/568543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档