I'm trying to wrap my head around managing a systemd service using DBus calls. However, I can't seem to figure out the correct way to make a method call, since whatever I do it throws a DBus error
// Create connection
auto connection = Gio::DBus::Connection::get_sync(Gio::DBus::BusType::BUS_TYPE_SESSION);
// Create the parameters vector
std::vector<std::string> param_vec;
param_vec.push_back("my_unit_name");
auto param_vec_variant = Glib::Variant<std::vector<std::string>>::create(param_vec);
auto params = Glib::Variant<std::vector<std::string>>::create_tuple(param_vec_variant);
try {
// Make the "GetUnit" dbus call to systemd
auto response = connection->call_sync(
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit",
params
);
}
catch (Gio::DBus::Error err) {
// org.freedesktop.DBus.Error.UnknownMethod being thrown
std::cerr << Gio::DBus::ErrorUtils::get_remote_error(err) << std::endl;
}
Can anyone point out where I'm going wrong?
As Philip pointed out, my problem was with the
connection->call_sync
method, which took the default parameterbus_name = ""
(docs).The
bus_name
for systemd isorg.freedesktop.systemd1
, so the correct solution to this problem would be to add this parameter:However, after further digging I found that it's actually not recommended to use the
Connection
object for DBus calls, and that I should actually be usingProxy
.Eventually I generalised my code using variadic parameters and settled on this function (try-catch block removed for readability):
This can be called like so: