#include <systemd/sd-bus.h>
int ListActiveInstances(sd_bus *bus, std::vector<LiveInstance> *out) {
sd_bus_error err = SD_BUS_ERROR_NULL;
sd_bus_message *m = nullptr, *reply = nullptr;
int r;
r = sd_bus_message_new_method_call(
bus, &m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnitsByPatterns");
if (r < 0) goto finish;
// in: as states
r = sd_bus_message_append_strv(m, (char *[]){ (char*)"active", nullptr });
if (r < 0) goto finish;
// in: as patterns
r = sd_bus_message_append_strv(m, (char *[]){ (char*)"osmo-gbproxy@*.service", nullptr });
if (r < 0) goto finish;
r = sd_bus_call(bus, m, 0, &err, &reply);
if (r < 0) goto finish;
// out: a(ssssssouso)
r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
if (r < 0) goto finish;
for (;;) {
const char *name, *desc, *load, *active, *sub, *followed;
const char *unit_path, *job_type, *job_path;
uint32_t job_id;
r = sd_bus_message_read(reply, "(ssssssouso)",
&name, &desc, &load, &active, &sub, &followed,
&unit_path, &job_id, &job_type, &job_path);
if (r < 0) goto finish;
if (r == 0) break; // конец массива
LiveInstance li;
if (ParseUnitName(name, &li)) {
out->push_back(std::move(li));
} else {
spdlog::warn("unparsable unit name, skipped: {}", name);
}
}
r = sd_bus_message_exit_container(reply);
finish:
sd_bus_error_free(&err);
sd_bus_message_unref(m);
sd_bus_message_unref(reply);
return r;
}