セッションバスを使用すると、DBUSはバスを開くことができません。

セッションバスを使用すると、DBUSはバスを開くことができません。

セッションバスを使用するサービスを作成/開始しようとしています。セッションバスである必要はなく、ただ働きたいと思い、免責事項のように2つの違いを知りません。

基本的に私が働いているのは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

これにより、端末で説明されている問題が解決されます。

おすすめ記事