+/* Ask elogind, which might grant access to unprivileged users
+ * through PolicyKit */
+static int reboot_with_logind(sd_bus *bus, enum action a) {
+ _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+ const char *method;
+ int r;
+ static const char *table[_ACTION_MAX] = {
+ [ACTION_REBOOT] = "The system is going down for reboot NOW!",
+ [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
+ [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
+ };
+
+ if (!bus)
+ return -EIO;
+
+ polkit_agent_open_if_enabled();
+
+ switch (a) {
+
+ case ACTION_POWEROFF:
+ method = "PowerOff";
+ break;
+
+ case ACTION_REBOOT:
+ method = "Reboot";
+ break;
+
+ case ACTION_SUSPEND:
+ method = "Suspend";
+ break;
+
+ case ACTION_HIBERNATE:
+ method = "Hibernate";
+ break;
+
+ case ACTION_HYBRID_SLEEP:
+ method = "HybridSleep";
+ break;
+
+ case ACTION_CANCEL_SHUTDOWN:
+ method = "CancelScheduledShutdown";
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ if (table[a]) {
+ r = sd_bus_call_method(
+ bus,
+ "org.freedesktop.login1",
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ "SetWallMessage",
+ &error,
+ NULL,
+ "sb",
+ table[a],
+ true);
+
+ if (r < 0) {
+ log_warning_errno(r, "Failed to set wall message, ignoring: %s",
+ bus_error_message(&error, r));
+ sd_bus_error_free(&error);
+ }
+ }
+
+
+ r = sd_bus_call_method(
+ bus,
+ "org.freedesktop.login1",
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ method,
+ &error,
+ NULL,
+ "b", arg_ask_password);
+ if (r < 0)
+ log_error("Failed to execute operation: %s", bus_error_message(&error, r));
+
+ return r;
+}
+
+static const struct {
+ HandleAction action;
+ const char* verb;
+} action_table[_ACTION_MAX] = {
+ [ACTION_POWEROFF] = { HANDLE_POWEROFF, "poweroff", },
+ [ACTION_REBOOT] = { HANDLE_REBOOT, "reboot", },
+ [ACTION_SUSPEND] = { HANDLE_SUSPEND, "suspend", },
+ [ACTION_HIBERNATE] = { HANDLE_HIBERNATE, "hibernate", },
+ [ACTION_HYBRID_SLEEP] = { HANDLE_HYBRID_SLEEP, "hybrid-sleep" },
+};
+
+static enum action verb_to_action(const char *verb) {
+ enum action i;
+
+ for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
+ if (streq_ptr(action_table[i].verb, verb))
+ return i;
+
+ return _ACTION_INVALID;
+}
+
+static int check_inhibitors(sd_bus *bus, enum action a) {
+ _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;