2

I'm trying to create/start a service that uses the session bus. It doesn't exactly have to be the session bus, I just want to get it working and don't know the difference between the two, just as a disclaimer.

Basically I'm looking into the sdbus-cpp library and follow their examples.

My service file:

[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

My configuration:

<!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>

And the example code from 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();
}

This is what I get when starting the service:

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'.

If I change my code to use the system bus, the service starts, but when I find the service using d-feet I get an error message after clicking on it.

What am I doing/understanding wrong? I'm running Ubuntu 19.10.

1 Answer 1

0

I put my gravedigger helmet on...
Maybe this is helpfull for other readers:
Executing

dbus-launch

in a terminal should fix the described problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .