bool BaseSystemdPlugin::talkToSystemdManager()

in src/oomd/plugins/systemd/BaseSystemdPlugin.cpp [34:94]


bool BaseSystemdPlugin::talkToSystemdManager(
    const std::string& method,
    const std::string& service,
    const std::string& mode) {
  ::sd_bus_error error = SD_BUS_ERROR_NULL;
  ::sd_bus_message* m = nullptr;
  ::sd_bus* bus = nullptr;
  const char* path;
  int r;

  OOMD_SCOPE_EXIT {
    ::sd_bus_error_free(&error);
    ::sd_bus_message_unref(m);
    ::sd_bus_close(bus);
    ::sd_bus_unref(bus);
  };

  /* Connect to the system bus */
  r = ::sd_bus_open_system(&bus);
  if (r < 0) {
    OLOG << "Failed to connect to system bus: " << strerror(-r);
    return false;
  }

  if (bus == nullptr) {
    OLOG << "Failed to connect to system bus: bus is null";
    return false;
  };

  /* Issue the method call and store the respons message in m */
  r = ::sd_bus_call_method(
      bus,
      "org.freedesktop.systemd1", /* service to contact */
      "/org/freedesktop/systemd1", /* object path */
      "org.freedesktop.systemd1.Manager", /* interface name */
      method.c_str(), /* method name */
      &error, /* object to return error in */
      &m, /* return message on success */
      "ss", /* input signature */
      service.c_str(), /* first argument */
      mode.c_str()); /* second argument */
  if (r < 0) {
    OLOG << "Failed to issue method call: " << error.message;
    return false;
  }

  if (m == nullptr) {
    OLOG << "Failed to issue method call: return message is null";
    return false;
  }

  /* Parse the response message */
  r = ::sd_bus_message_read(m, "o", &path);
  if (r < 0) {
    OLOG << "Failed to parse response message: " << strerror(-r);
    return false;
  }

  OLOG << "Queued service job as " << path;
  return true;
}