使用sessionbus时DBUS无法打开总线

使用sessionbus时DBUS无法打开总线

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

基本上我正在研究sdbus-cpp图书馆并遵循他们的例子。

我的服务文件:

[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

我的配置:

<!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 的示例代码:

#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();
}

这是我启动服务时得到的信息:

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-feet 找到该服务时,单击它后会收到一条错误消息。

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

答案1

我戴上了掘墓人头盔......
也许这对其他读者有帮助:
执行

dbus-launch

在终端中应该可以解决所描述的问题。

相关内容