X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogin%2Flogind-dbus.c;h=3bcb91bf13fea32c47f8bf98a65999812074fe81;hp=6175d57d8c34fc59a22e76d99aa326f528d68696;hb=6524990fdc98370ecba5d9f73e67161e8798c010;hpb=d2e54fae5ca7a0f71b5ac8b356a589ff0a09ea0a diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 6175d57d8..3bcb91bf1 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -31,6 +31,8 @@ #include "path-util.h" #include "polkit.h" #include "special.h" +#include "systemd/sd-id128.h" +#include "systemd/sd-messages.h" #define BUS_MANAGER_INTERFACE \ " \n" \ @@ -81,6 +83,7 @@ " \n" \ " \n" \ " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -98,6 +101,7 @@ " \n" \ " \n" \ " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -141,6 +145,9 @@ " \n" \ " \n" \ " \n" \ + " \n" \ + " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -153,6 +160,9 @@ " \n" \ " \n" \ " \n" \ + " \n" \ + " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -207,8 +217,11 @@ " \n" \ " \n" \ " \n" \ - " \n" \ + " \n" \ + " \n" \ " \n" \ + " \n" \ + " \n" \ " \n" #define INTROSPECTION_BEGIN \ @@ -273,12 +286,28 @@ static int bus_manager_append_inhibited(DBusMessageIter *i, const char *property return 0; } +static int bus_manager_append_preparing(DBusMessageIter *i, const char *property, void *data) { + Manager *m = data; + dbus_bool_t b; + + assert(i); + assert(property); + + if (streq(property, "PreparingForShutdown")) + b = !!(m->delayed_what & INHIBIT_SHUTDOWN); + else + b = !!(m->delayed_what & INHIBIT_SLEEP); + + dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b); + return 0; +} + static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) { Session *session = NULL; User *user = NULL; const char *type, *class, *seat, *tty, *display, *remote_user, *remote_host, *service; uint32_t uid, leader, audit_id = 0; - dbus_bool_t remote, kill_processes; + dbus_bool_t remote, kill_processes, exists; char **controllers = NULL, **reset_controllers = NULL; SessionType t; SessionClass c; @@ -496,6 +525,8 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMess seat = session->seat ? session->seat->id : ""; vtnr = session->vtnr; + exists = true; + b = dbus_message_append_args( reply, DBUS_TYPE_STRING, &session->id, @@ -504,6 +535,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMess DBUS_TYPE_UNIX_FD, &fifo_fd, DBUS_TYPE_STRING, &seat, DBUS_TYPE_UINT32, &vtnr, + DBUS_TYPE_BOOLEAN, &exists, DBUS_TYPE_INVALID); free(p); @@ -620,6 +652,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMess } seat = s ? s->id : ""; + exists = false; b = dbus_message_append_args( reply, DBUS_TYPE_STRING, &session->id, @@ -628,6 +661,7 @@ static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMess DBUS_TYPE_UNIX_FD, &fifo_fd, DBUS_TYPE_STRING, &seat, DBUS_TYPE_UINT32, &vtnr, + DBUS_TYPE_BOOLEAN, &exists, DBUS_TYPE_INVALID); free(p); @@ -701,10 +735,21 @@ static int bus_manager_inhibit(Manager *m, DBusConnection *connection, DBusMessa goto fail; } + /* Delay is only supported for shutdown/sleep */ + if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP))) { + r = -EINVAL; + goto fail; + } + r = verify_polkit(connection, message, - m == INHIBIT_BLOCK ? - "org.freedesktop.login1.inhibit-block" : - "org.freedesktop.login1.inhibit-delay", false, NULL, error); + w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") : + w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") : + w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" : + w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" : + w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" : + w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" : + "org.freedesktop.login1.inhibit-handle-lid-switch", + false, NULL, error); if (r < 0) goto fail; @@ -925,67 +970,38 @@ static int flush_devices(Manager *m) { } static int have_multiple_sessions( - DBusConnection *connection, Manager *m, - DBusMessage *message, - DBusError *error) { + uid_t uid) { - Session *s; + Session *session; + Iterator i; assert(m); - if (hashmap_size(m->sessions) > 1) - return true; - - /* Hmm, there's only one session, but let's make sure it - * actually belongs to the user who is asking. If not, better - * be safe than sorry. */ - - s = hashmap_first(m->sessions); - if (s) { - unsigned long ul; - - ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error); - if (ul == (unsigned long) -1) - return -EIO; - - return s->user->uid != ul; - } + /* Check for other users' sessions. Greeter sessions do not count. */ + HASHMAP_FOREACH(session, m->sessions, i) + if (session->class == SESSION_USER && session->user->uid != uid) + return true; return false; } static int send_start_unit(DBusConnection *connection, const char *unit_name, DBusError *error) { - DBusMessage *message, *reply; const char *mode = "replace"; - assert(connection); assert(unit_name); - message = dbus_message_new_method_call( + return bus_method_call_with_reply ( + connection, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", - "StartUnit"); - if (!message) - return -ENOMEM; - - if (!dbus_message_append_args(message, - DBUS_TYPE_STRING, &unit_name, - DBUS_TYPE_STRING, &mode, - DBUS_TYPE_INVALID)) { - dbus_message_unref(message); - return -ENOMEM; - } - - reply = dbus_connection_send_with_reply_and_block(connection, message, -1, error); - dbus_message_unref(message); - - if (!reply) - return -EIO; - - dbus_message_unref(reply); - return 0; + "StartUnit", + NULL, + NULL, + DBUS_TYPE_STRING, &unit_name, + DBUS_TYPE_STRING, &mode, + DBUS_TYPE_INVALID); } static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) { @@ -1044,6 +1060,7 @@ static int bus_manager_can_shutdown_or_sleep( const char *action_multiple_sessions, const char *action_ignore_inhibit, const char *sleep_type, + const char *sleep_disk_type, DBusError *error, DBusMessage **_reply) { @@ -1051,6 +1068,7 @@ static int bus_manager_can_shutdown_or_sleep( const char *result; DBusMessage *reply = NULL; int r; + unsigned long ul; assert(m); assert(connection); @@ -1068,16 +1086,33 @@ static int bus_manager_can_shutdown_or_sleep( if (r < 0) return r; - result = "na"; - goto finish; + if (r == 0) { + result = "na"; + goto finish; + } + } + + if (sleep_disk_type) { + r = can_sleep_disk(sleep_disk_type); + if (r < 0) + return r; + + if (r == 0) { + result = "na"; + goto finish; + } } - r = have_multiple_sessions(connection, m, message, error); + 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); + 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); @@ -1086,11 +1121,11 @@ static int bus_manager_can_shutdown_or_sleep( if (r > 0) result = "yes"; - else if (challenge) - result = "challenge"; - else - result = "no"; - } + else if (challenge) + result = "challenge"; + else + result = "no"; + } if (blocked) { r = verify_polkit(connection, message, action_ignore_inhibit, false, &challenge, error); @@ -1139,6 +1174,41 @@ finish: return 0; } +static int bus_manager_log_shutdown( + Manager *m, + InhibitWhat w, + const char *unit_name) { + + const char *p, *q; + + assert(m); + assert(unit_name); + + if (w != INHIBIT_SHUTDOWN) + return 0; + + if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) { + p = "MESSAGE=System is powering down."; + q = "SHUTDOWN=power-off"; + } else if (streq(unit_name, SPECIAL_HALT_TARGET)) { + p = "MESSAGE=System is halting."; + q = "SHUTDOWN=halt"; + } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) { + p = "MESSAGE=System is rebooting."; + q = "SHUTDOWN=reboot"; + } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) { + p = "MESSAGE=System is rebooting with kexec."; + q = "SHUTDOWN=kexec"; + } else { + p = "MESSAGE=System is shutting down."; + q = NULL; + } + + return log_struct(LOG_NOTICE, MESSAGE_ID(SD_MESSAGE_SHUTDOWN), + p, + q, NULL); +} + int bus_manager_shutdown_or_sleep_now_or_later( Manager *m, const char *unit_name, @@ -1155,16 +1225,19 @@ int bus_manager_shutdown_or_sleep_now_or_later( delayed = m->inhibit_delay_max > 0 && - manager_is_inhibited(m, w, INHIBIT_DELAY, NULL); + 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 + else { + bus_manager_log_shutdown(m, w, unit_name); + /* Shutdown is not delayed, execute it * immediately */ r = send_start_unit(m->bus, unit_name, error); + } return r; } @@ -1179,6 +1252,7 @@ static int bus_manager_do_shutdown_or_sleep( const char *action_multiple_sessions, const char *action_ignore_inhibit, const char *sleep_type, + const char *sleep_disk_type, DBusError *error, DBusMessage **_reply) { @@ -1186,6 +1260,7 @@ static int bus_manager_do_shutdown_or_sleep( bool multiple_sessions, blocked; DBusMessage *reply = NULL; int r; + unsigned long ul; assert(m); assert(connection); @@ -1215,12 +1290,25 @@ static int bus_manager_do_shutdown_or_sleep( return -ENOTSUP; } - r = have_multiple_sessions(connection, m, message, error); + if (sleep_disk_type) { + r = can_sleep_disk(sleep_disk_type); + 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); + 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); @@ -1269,8 +1357,11 @@ static const BusProperty bus_login_manager_properties[] = { { "DelayInhibited", bus_manager_append_inhibited, "s", 0 }, { "InhibitDelayMaxUSec", bus_property_append_usec, "t", offsetof(Manager, inhibit_delay_max) }, { "HandlePowerKey", bus_manager_append_handle_button, "s", offsetof(Manager, handle_power_key) }, - { "HandleSleepKey", bus_manager_append_handle_button, "s", offsetof(Manager, handle_sleep_key) }, + { "HandleSuspendKey", bus_manager_append_handle_button, "s", offsetof(Manager, handle_suspend_key) }, + { "HandleHibernateKey", bus_manager_append_handle_button, "s", offsetof(Manager, handle_hibernate_key)}, { "HandleLidSwitch", bus_manager_append_handle_button, "s", offsetof(Manager, handle_lid_switch) }, + { "PreparingForShutdown", bus_manager_append_preparing, "b", 0 }, + { "PreparingForSleep", bus_manager_append_preparing, "b", 0 }, { NULL, } }; @@ -1730,6 +1821,18 @@ static DBusHandlerResult manager_message_handler( if (!reply) goto oom; + } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSessions")) { + Session *session; + Iterator i; + + HASHMAP_FOREACH(session, m->sessions, i) + if (session_send_lock(session, true) < 0) + goto oom; + + reply = dbus_message_new_method_return(message); + if (!reply) + goto oom; + } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) { const char *swho; int32_t signo; @@ -1990,7 +2093,7 @@ static DBusHandlerResult manager_message_handler( "org.freedesktop.login1.power-off", "org.freedesktop.login1.power-off-multiple-sessions", "org.freedesktop.login1.power-off-ignore-inhibit", - NULL, + NULL, NULL, &error, &reply); if (r < 0) return bus_send_error_reply(connection, message, &error, r); @@ -2002,7 +2105,7 @@ static DBusHandlerResult manager_message_handler( "org.freedesktop.login1.reboot", "org.freedesktop.login1.reboot-multiple-sessions", "org.freedesktop.login1.reboot-ignore-inhibit", - NULL, + NULL, NULL, &error, &reply); if (r < 0) return bus_send_error_reply(connection, message, &error, r); @@ -2015,7 +2118,7 @@ static DBusHandlerResult manager_message_handler( "org.freedesktop.login1.suspend", "org.freedesktop.login1.suspend-multiple-sessions", "org.freedesktop.login1.suspend-ignore-inhibit", - "mem", + "mem", NULL, &error, &reply); if (r < 0) return bus_send_error_reply(connection, message, &error, r); @@ -2027,7 +2130,20 @@ static DBusHandlerResult manager_message_handler( "org.freedesktop.login1.hibernate", "org.freedesktop.login1.hibernate-multiple-sessions", "org.freedesktop.login1.hibernate-ignore-inhibit", - "disk", + "disk", NULL, + &error, &reply); + if (r < 0) + return bus_send_error_reply(connection, message, &error, r); + + } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "HybridSleep")) { + r = bus_manager_do_shutdown_or_sleep( + m, connection, message, + SPECIAL_HYBRID_SLEEP_TARGET, + INHIBIT_SLEEP, + "org.freedesktop.login1.hibernate", + "org.freedesktop.login1.hibernate-multiple-sessions", + "org.freedesktop.login1.hibernate-ignore-inhibit", + "disk", "suspend", &error, &reply); if (r < 0) return bus_send_error_reply(connection, message, &error, r); @@ -2040,7 +2156,7 @@ static DBusHandlerResult manager_message_handler( "org.freedesktop.login1.power-off", "org.freedesktop.login1.power-off-multiple-sessions", "org.freedesktop.login1.power-off-ignore-inhibit", - NULL, + NULL, NULL, &error, &reply); if (r < 0) return bus_send_error_reply(connection, message, &error, r); @@ -2051,7 +2167,7 @@ static DBusHandlerResult manager_message_handler( "org.freedesktop.login1.reboot", "org.freedesktop.login1.reboot-multiple-sessions", "org.freedesktop.login1.reboot-ignore-inhibit", - NULL, + NULL, NULL, &error, &reply); if (r < 0) return bus_send_error_reply(connection, message, &error, r); @@ -2063,7 +2179,7 @@ static DBusHandlerResult manager_message_handler( "org.freedesktop.login1.suspend", "org.freedesktop.login1.suspend-multiple-sessions", "org.freedesktop.login1.suspend-ignore-inhibit", - "mem", + "mem", NULL, &error, &reply); if (r < 0) return bus_send_error_reply(connection, message, &error, r); @@ -2075,7 +2191,19 @@ static DBusHandlerResult manager_message_handler( "org.freedesktop.login1.hibernate", "org.freedesktop.login1.hibernate-multiple-sessions", "org.freedesktop.login1.hibernate-ignore-inhibit", - "disk", + "disk", NULL, + &error, &reply); + if (r < 0) + return bus_send_error_reply(connection, message, &error, r); + + } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CanHybridSleep")) { + r = bus_manager_can_shutdown_or_sleep( + m, connection, message, + INHIBIT_SLEEP, + "org.freedesktop.login1.hibernate", + "org.freedesktop.login1.hibernate-multiple-sessions", + "org.freedesktop.login1.hibernate-ignore-inhibit", + "disk", "suspend", &error, &reply); if (r < 0) return bus_send_error_reply(connection, message, &error, r); @@ -2239,10 +2367,12 @@ int manager_dispatch_delayed(Manager *manager) { /* Continue delay? */ delayed = manager->delayed_timestamp + manager->inhibit_delay_max > now(CLOCK_MONOTONIC) && - manager_is_inhibited(manager, manager->delayed_what, INHIBIT_DELAY, NULL); + manager_is_inhibited(manager, manager->delayed_what, INHIBIT_DELAY, NULL, false, false, 0); if (delayed) return 0; + bus_manager_log_shutdown(manager, manager->delayed_what, manager->delayed_unit); + /* Reset delay data */ unit_name = manager->delayed_unit; manager->delayed_unit = NULL;