-static int execute_shutdown_or_sleep(
- Manager *m,
- InhibitWhat w,
- const char *unit_name,
- DBusError *error) {
-
- _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
- const char *mode = "replace-irreversibly", *p;
- int r;
- char *c;
-
- assert(m);
- assert(w >= 0);
- assert(w < _INHIBIT_WHAT_MAX);
- assert(unit_name);
-
- bus_manager_log_shutdown(m, w, unit_name);
-
- r = bus_method_call_with_reply(
- m->bus,
- "org.freedesktop.systemd1",
- "/org/freedesktop/systemd1",
- "org.freedesktop.systemd1.Manager",
- "StartUnit",
- &reply,
- error,
- DBUS_TYPE_STRING, &unit_name,
- DBUS_TYPE_STRING, &mode,
- DBUS_TYPE_INVALID);
- if (r < 0)
- return r;
-
- if (!dbus_message_get_args(
- reply,
- error,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID))
- return -EINVAL;
-
- c = strdup(p);
- if (!c)
- return -ENOMEM;
-
- m->action_unit = unit_name;
- free(m->action_job);
- m->action_job = c;
- m->action_what = w;
-
- return 0;
-}
-
-static int delay_shutdown_or_sleep(
- Manager *m,
- InhibitWhat w,
- const char *unit_name) {
-
- assert(m);
- assert(w >= 0);
- assert(w < _INHIBIT_WHAT_MAX);
- assert(unit_name);
-
- m->action_timestamp = now(CLOCK_MONOTONIC);
- m->action_unit = unit_name;
- m->action_what = w;
-
- return 0;
-}
-
-static int bus_manager_can_shutdown_or_sleep(
- Manager *m,
- DBusConnection *connection,
- DBusMessage *message,
- InhibitWhat w,
- const char *action,
- const char *action_multiple_sessions,
- const char *action_ignore_inhibit,
- const char *sleep_verb,
- DBusError *error,
- DBusMessage **_reply) {
-
- bool multiple_sessions, challenge, blocked, b;
- const char *result = NULL;
- _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
- int r;
- unsigned long ul;
-
- assert(m);
- assert(connection);
- assert(message);
- assert(w >= 0);
- assert(w <= _INHIBIT_WHAT_MAX);
- assert(action);
- assert(action_multiple_sessions);
- assert(action_ignore_inhibit);
- assert(error);
- assert(_reply);
-
- if (sleep_verb) {
- r = can_sleep(sleep_verb);
- if (r < 0)
- return r;
- if (r == 0) {
- result = "na";
- goto finish;
- }
- }
-
- ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error);
- if (ul == (unsigned long) -1)
- return -EIO;
-
- r = have_multiple_sessions(m, (uid_t) ul);
- if (r < 0)
- return r;
-
- multiple_sessions = r > 0;
- blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, (uid_t) ul);
-
- if (multiple_sessions) {
- r = verify_polkit(connection, message, action_multiple_sessions, false, &challenge, error);
- if (r < 0)
- return r;
-
- if (r > 0)
- result = "yes";
- else if (challenge)
- result = "challenge";
- else
- result = "no";
- }
-
- if (blocked) {
- r = verify_polkit(connection, message, action_ignore_inhibit, false, &challenge, error);
- if (r < 0)
- return r;
-
- if (r > 0 && !result)
- result = "yes";
- else if (challenge && (!result || streq(result, "yes")))
- result = "challenge";
- else
- result = "no";
- }
-
- if (!multiple_sessions && !blocked) {
- /* If neither inhibit nor multiple sessions
- * apply then just check the normal policy */
-
- r = verify_polkit(connection, message, action, false, &challenge, error);
- if (r < 0)
- return r;
-
- if (r > 0)
- result = "yes";
- else if (challenge)
- result = "challenge";
- else
- result = "no";
- }
-
-finish:
- reply = dbus_message_new_method_return(message);
- if (!reply)
- return -ENOMEM;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_STRING, &result,
- DBUS_TYPE_INVALID);
- if (!b)
- return -ENOMEM;
-
- *_reply = reply;
- reply = NULL;
- return 0;
-}
-
-static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
- static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
- [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
- [INHIBIT_SLEEP] = "PrepareForSleep"
- };
-
- dbus_bool_t active = _active;
- _cleanup_dbus_message_unref_ DBusMessage *message = NULL;
-
- assert(m);
- assert(w >= 0);
- assert(w < _INHIBIT_WHAT_MAX);
- assert(signal_name[w]);
-
- message = dbus_message_new_signal("/org/freedesktop/login1", "org.freedesktop.login1.Manager", signal_name[w]);
- if (!message)
- return -ENOMEM;
-
- if (!dbus_message_append_args(message, DBUS_TYPE_BOOLEAN, &active, DBUS_TYPE_INVALID) ||
- !dbus_connection_send(m->bus, message, NULL))
- return -ENOMEM;
-
- return 0;
-}
-
-int bus_manager_shutdown_or_sleep_now_or_later(
- Manager *m,
- const char *unit_name,
- InhibitWhat w,
- DBusError *error) {
-
- bool delayed;
- int r;
-
- assert(m);
- assert(unit_name);
- assert(w >= 0);
- assert(w <= _INHIBIT_WHAT_MAX);
- assert(!m->action_job);
-
- /* Tell everybody to prepare for shutdown/sleep */
- send_prepare_for(m, w, true);
-
- delayed =
- m->inhibit_delay_max > 0 &&
- manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0);
-
- if (delayed)
- /* Shutdown is delayed, keep in mind what we
- * want to do, and start a timeout */
- r = delay_shutdown_or_sleep(m, w, unit_name);
- else
- /* Shutdown is not delayed, execute it
- * immediately */
- r = execute_shutdown_or_sleep(m, w, unit_name, error);
-
- return r;
-}
-
-static int bus_manager_do_shutdown_or_sleep(
- Manager *m,
- DBusConnection *connection,
- DBusMessage *message,
- const char *unit_name,
- InhibitWhat w,
- const char *action,
- const char *action_multiple_sessions,
- const char *action_ignore_inhibit,
- const char *sleep_verb,
- DBusError *error,
- DBusMessage **_reply) {
-
- dbus_bool_t interactive;
- bool multiple_sessions, blocked;
- DBusMessage *reply = NULL;
- int r;
- unsigned long ul;
-
- assert(m);
- assert(connection);
- assert(message);
- assert(unit_name);
- assert(w >= 0);
- assert(w <= _INHIBIT_WHAT_MAX);
- assert(action);
- assert(action_multiple_sessions);
- assert(action_ignore_inhibit);
- assert(error);
- assert(_reply);
-
- /* Don't allow multiple jobs being executed at the same time */
- if (m->action_what)
- return -EALREADY;
-
- if (!dbus_message_get_args(
- message,
- error,
- DBUS_TYPE_BOOLEAN, &interactive,
- DBUS_TYPE_INVALID))
- return -EINVAL;
-
- if (sleep_verb) {
- r = can_sleep(sleep_verb);
- if (r < 0)
- return r;
-
- if (r == 0)
- return -ENOTSUP;
- }
-
- ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error);
- if (ul == (unsigned long) -1)
- return -EIO;
-
- r = have_multiple_sessions(m, (uid_t) ul);
- if (r < 0)
- return r;
-
- multiple_sessions = r > 0;
- blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, (uid_t) ul);
-
- if (multiple_sessions) {
- r = verify_polkit(connection, message, action_multiple_sessions, interactive, NULL, error);
- if (r < 0)
- return r;
- }
-
- if (blocked) {
- r = verify_polkit(connection, message, action_ignore_inhibit, interactive, NULL, error);
- if (r < 0)
- return r;
- }
-
- if (!multiple_sessions && !blocked) {
- r = verify_polkit(connection, message, action, interactive, NULL, error);
- if (r < 0)
- return r;
- }
-
- r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
- if (r < 0)
- return r;
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- return -ENOMEM;
-
- *_reply = reply;
- return 0;
-}
-
-static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_handle_action, handle_action, HandleAction);
-
-static const BusProperty bus_login_manager_properties[] = {
- { "NAutoVTs", bus_property_append_unsigned, "u", offsetof(Manager, n_autovts) },
- { "KillOnlyUsers", bus_property_append_strv, "as", offsetof(Manager, kill_only_users), true },
- { "KillExcludeUsers", bus_property_append_strv, "as", offsetof(Manager, kill_exclude_users), true },
- { "KillUserProcesses", bus_property_append_bool, "b", offsetof(Manager, kill_user_processes) },
- { "IdleHint", bus_manager_append_idle_hint, "b", 0 },
- { "IdleSinceHint", bus_manager_append_idle_hint_since, "t", 0 },
- { "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", 0 },
- { "BlockInhibited", bus_manager_append_inhibited, "s", 0 },
- { "DelayInhibited", bus_manager_append_inhibited, "s", 0 },
- { "InhibitDelayMaxUSec", bus_property_append_usec, "t", offsetof(Manager, inhibit_delay_max) },
- { "HandlePowerKey", bus_manager_append_handle_action, "s", offsetof(Manager, handle_power_key) },
- { "HandleSuspendKey", bus_manager_append_handle_action, "s", offsetof(Manager, handle_suspend_key) },
- { "HandleHibernateKey", bus_manager_append_handle_action, "s", offsetof(Manager, handle_hibernate_key)},
- { "HandleLidSwitch", bus_manager_append_handle_action, "s", offsetof(Manager, handle_lid_switch) },
- { "IdleAction", bus_manager_append_handle_action, "s", offsetof(Manager, idle_action) },
- { "IdleActionUSec", bus_property_append_usec, "t", offsetof(Manager, idle_action_usec) },
- { "PreparingForShutdown", bus_manager_append_preparing, "b", 0 },
- { "PreparingForSleep", bus_manager_append_preparing, "b", 0 },
- { NULL, }
-};
-
-static DBusHandlerResult manager_message_handler(
- DBusConnection *connection,
- DBusMessage *message,
- void *userdata) {
-
- Manager *m = userdata;
-
- DBusError error;
- _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
- int r;
-
- assert(connection);
- assert(message);
- assert(m);
-
- dbus_error_init(&error);
-
- if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
- const char *name;
- char *p;
- Session *session;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- session = hashmap_get(m->sessions, name);
- if (!session)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = session_bus_path(session);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSessionByPID")) {
- uint32_t pid;
- char *p;
- Session *session;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &pid,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- r = manager_get_session_by_pid(m, pid, &session);
- if (r <= 0)
- return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = session_bus_path(session);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
- uint32_t uid;
- char *p;
- User *user;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &uid,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
- if (!user)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = user_bus_path(user);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUserByPID")) {
- uint32_t pid;
- char *p;
- User *user;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &pid,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- r = manager_get_user_by_pid(m, pid, &user);
- if (r <= 0)
- return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = user_bus_path(user);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
- const char *name;
- char *p;
- Seat *seat;
- bool b;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- seat = hashmap_get(m->seats, name);
- if (!seat)
- return bus_send_error_reply(connection, message, &error, -ENOENT);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- p = seat_bus_path(seat);
- if (!p)
- goto oom;
-
- b = dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &p,
- DBUS_TYPE_INVALID);
- free(p);
-
- if (!b)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
- char *p;
- Session *session;
- Iterator i;
- DBusMessageIter iter, sub;
- const char *empty = "";
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- dbus_message_iter_init_append(reply, &iter);
-
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
- goto oom;
-
- HASHMAP_FOREACH(session, m->sessions, i) {
- DBusMessageIter sub2;
- uint32_t uid;
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- goto oom;
-
- uid = session->user->uid;
-
- p = session_bus_path(session);
- if (!p)
- goto oom;
-
- if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
- free(p);
- goto oom;
- }
-
- free(p);
-
- if (!dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
- }
-
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
- User *user;
- Iterator i;
- DBusMessageIter iter, sub;
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- dbus_message_iter_init_append(reply, &iter);
-
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
- goto oom;
-
- HASHMAP_FOREACH(user, m->users, i) {
- _cleanup_free_ char *p = NULL;
- DBusMessageIter sub2;
- uint32_t uid;