X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogin%2Flogind-dbus.c;h=d88636f7471a4b1afa2da1162f3a574d5222fddb;hp=a0a23788b2fa3740b20e350f167f2e1616ee8955;hb=2bd94bfa67be6bc52fd525396fd87b4a6f323bed;hpb=059696ac014da265ee2d1b062cd0dc7bee3d2001 diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index a0a23788b..d88636f74 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -28,7 +28,7 @@ #include "strv.h" #include "mkdir.h" #include "path-util.h" -#include "special.h" +// #include "special.h" #include "sleep-config.h" #include "fileio-label.h" #include "unit-name.h" @@ -158,7 +158,7 @@ static int property_get_idle_since_hint( sd_bus_error *error) { Manager *m = userdata; - dual_timestamp t; + dual_timestamp t = DUAL_TIMESTAMP_NULL; assert(bus); assert(reply); @@ -243,6 +243,24 @@ static int property_get_scheduled_shutdown( static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction); +static int property_get_docked( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + Manager *m = userdata; + + assert(bus); + assert(reply); + assert(m); + + return sd_bus_message_append(reply, "b", manager_is_docked_or_external_displays(m)); +} + static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_free_ char *p = NULL; Manager *m = userdata; @@ -671,45 +689,26 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus return r; } - manager_get_session_by_pid(m, leader, &session); - if (session) { - _cleanup_free_ char *path = NULL; - _cleanup_close_ int fifo_fd = -1; - - /* Session already exists, client is probably - * something like "su" which changes uid but is still - * the same session */ - - fifo_fd = session_create_fifo(session); - if (fifo_fd < 0) - return fifo_fd; - - path = session_bus_path(session); - if (!path) - return -ENOMEM; - - log_debug("Sending reply about an existing session: " - "id=%s object_path=%s uid=%u runtime_path=%s " - "session_fd=%d seat=%s vtnr=%u", - session->id, - path, - (uint32_t) session->user->uid, - session->user->runtime_path, - fifo_fd, - session->seat ? session->seat->id : "", - (uint32_t) session->vtnr); - - return sd_bus_reply_method_return( - message, "soshusub", - session->id, - path, - session->user->runtime_path, - fifo_fd, - (uint32_t) session->user->uid, - session->seat ? session->seat->id : "", - (uint32_t) session->vtnr, - true); - } + r = manager_get_session_by_pid(m, leader, NULL); + if (r > 0) + return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session"); + + /* + * Old gdm and lightdm start the user-session on the same VT as + * the greeter session. But they destroy the greeter session + * after the user-session and want the user-session to take + * over the VT. We need to support this for + * backwards-compatibility, so make sure we allow new sessions + * on a VT that a greeter is running on. Furthermore, to allow + * re-logins, we have to allow a greeter to take over a used VT for + * the exact same reasons. + */ + if (c != SESSION_GREETER && + vtnr > 0 && + vtnr < m->seat0->position_count && + m->seat0->positions[vtnr] && + m->seat0->positions[vtnr]->class != SESSION_GREETER) + return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session"); audit_session_from_pid(leader, &audit_id); if (audit_id > 0) { @@ -817,19 +816,14 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus session->create_message = sd_bus_message_ref(message); - /* Now, let's wait until the slice unit and stuff got - * created. We send the reply back from - * session_send_create_reply(). */ - - /* Elogind note: replying directly, since we're not actually - starting slices and thus we aren't waiting on systemd. */ + /* Here upstream systemd starts cgroups and the user systemd, + and arranges to reply asynchronously. We reply + directly. */ r = session_send_create_reply(session, NULL); if (r < 0) goto fail; - session_save(session); - return 1; fail: @@ -863,6 +857,8 @@ static int method_release_session(sd_bus_message *message, void *userdata, sd_bu if (r < 0) return r; + session_add_to_gc_queue(session); + return sd_bus_reply_method_return(message, NULL); } @@ -1185,7 +1181,7 @@ static int trigger_device(Manager *m, struct udev_device *d) { if (!t) return -ENOMEM; - write_string_file(t, "change"); + write_string_file(t, "change", WRITE_STRING_FILE_CREATE); } return 0; @@ -1351,29 +1347,33 @@ static int have_multiple_sessions( static int bus_manager_log_shutdown( Manager *m, InhibitWhat w, - const char *unit_name) { + HandleAction action) { const char *p, *q; assert(m); - assert(unit_name); if (w != INHIBIT_SHUTDOWN) return 0; - if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) { + switch (action) { + case HANDLE_POWEROFF: p = "MESSAGE=System is powering down."; q = "SHUTDOWN=power-off"; - } else if (streq(unit_name, SPECIAL_HALT_TARGET)) { + break; + case HANDLE_HALT: p = "MESSAGE=System is halting."; q = "SHUTDOWN=halt"; - } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) { + break; + case HANDLE_REBOOT: p = "MESSAGE=System is rebooting."; q = "SHUTDOWN=reboot"; - } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) { + break; + case HANDLE_KEXEC: p = "MESSAGE=System is rebooting with kexec."; q = "SHUTDOWN=kexec"; - } else { + break; + default: p = "MESSAGE=System is shutting down."; q = NULL; } @@ -1433,24 +1433,50 @@ int manager_set_lid_switch_ignore(Manager *m, usec_t until) { return r; } +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" + }; + + int active = _active; + + assert(m); + assert(w >= 0); + assert(w < _INHIBIT_WHAT_MAX); + assert(signal_name[w]); + + return sd_bus_emit_signal(m->bus, + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + signal_name[w], + "b", + active); +} + static int execute_shutdown_or_sleep( Manager *m, InhibitWhat w, - const char *unit_name, + HandleAction action, sd_bus_error *error) { +/// elogind does not need these, we do it ourselves +#if 0 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; const char *p; char *c; +#endif // 0 int r; assert(m); assert(w >= 0); assert(w < _INHIBIT_WHAT_MAX); - assert(unit_name); - bus_manager_log_shutdown(m, w, unit_name); + bus_manager_log_shutdown(m, w, action); +/// elogind does it directly without depending on systemd running the system +#if 0 r = sd_bus_call_method( m->bus, "org.freedesktop.systemd1", @@ -1459,10 +1485,15 @@ static int execute_shutdown_or_sleep( "StartUnit", error, &reply, - "ss", unit_name, "replace-irreversibly"); + "ss", NULL, "replace-irreversibly"); +#else + r = shutdown_or_sleep(m, action); +#endif // 0 if (r < 0) return r; +/// elogind neither needs a dbus reply, nor supports systemd action jobs +#if 0 r = sd_bus_message_read(reply, "o", &p); if (r < 0) return r; @@ -1475,6 +1506,13 @@ static int execute_shutdown_or_sleep( free(m->action_job); m->action_job = c; m->action_what = w; +#endif // 0 + + if (w == INHIBIT_SLEEP) + /* And we're back. */ + send_prepare_for(m, w, false); + + m->action_what = 0; /* Make sure the lid switch is ignored for a while */ manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec); @@ -1482,25 +1520,23 @@ static int execute_shutdown_or_sleep( return 0; } -static int manager_inhibit_timeout_handler( - sd_event_source *s, - uint64_t usec, - void *userdata) { +int manager_dispatch_delayed(Manager *manager, bool timeout) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; Inhibitor *offending = NULL; - Manager *manager = userdata; int r; assert(manager); - assert(manager->inhibit_timeout_source == s); - if (manager->action_what == 0 || manager->action_job) + if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) ) return 0; if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) { _cleanup_free_ char *comm = NULL, *u = NULL; + if (!timeout) + return 0; + (void) get_process_comm(offending->pid, &comm); u = uid_to_name(offending->uid); @@ -1510,21 +1546,37 @@ static int manager_inhibit_timeout_handler( } /* Actually do the operation */ - r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error); + r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error); if (r < 0) { log_warning("Failed to send delayed message: %s", bus_error_message(&error, r)); - manager->action_unit = NULL; - manager->action_what = 0; + manager->pending_action = HANDLE_IGNORE; + manager->action_what = 0; + return r; } - return 0; + return 1; +} + +static int manager_inhibit_timeout_handler( + sd_event_source *s, + uint64_t usec, + void *userdata) { + + Manager *manager = userdata; + int r; + + assert(manager); + assert(manager->inhibit_timeout_source == s); + + r = manager_dispatch_delayed(manager, true); + return (r < 0) ? r : 0; } static int delay_shutdown_or_sleep( Manager *m, InhibitWhat w, - const char *unit_name) { + HandleAction action) { int r; usec_t timeout_val; @@ -1532,7 +1584,6 @@ static int delay_shutdown_or_sleep( assert(m); assert(w >= 0); assert(w < _INHIBIT_WHAT_MAX); - assert(unit_name); timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max; @@ -1551,37 +1602,15 @@ static int delay_shutdown_or_sleep( return r; } - m->action_unit = unit_name; + m->pending_action = action; m->action_what = w; 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" - }; - - int active = _active; - - assert(m); - assert(w >= 0); - assert(w < _INHIBIT_WHAT_MAX); - assert(signal_name[w]); - - return sd_bus_emit_signal(m->bus, - "/org/freedesktop/login1", - "org.freedesktop.login1.Manager", - signal_name[w], - "b", - active); -} - int bus_manager_shutdown_or_sleep_now_or_later( Manager *m, - const char *unit_name, + HandleAction action, InhibitWhat w, sd_bus_error *error) { @@ -1589,10 +1618,8 @@ int bus_manager_shutdown_or_sleep_now_or_later( 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); @@ -1604,11 +1631,11 @@ int bus_manager_shutdown_or_sleep_now_or_later( 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); + r = delay_shutdown_or_sleep(m, w, action); else /* Shutdown is not delayed, execute it * immediately */ - r = execute_shutdown_or_sleep(m, w, unit_name, error); + r = execute_shutdown_or_sleep(m, w, action, error); return r; } @@ -1678,7 +1705,7 @@ static int verify_shutdown_creds( static int method_do_shutdown_or_sleep( Manager *m, sd_bus_message *message, - const char *unit_name, + HandleAction sleep_action, InhibitWhat w, const char *action, const char *action_multiple_sessions, @@ -1690,7 +1717,6 @@ static int method_do_shutdown_or_sleep( assert(m); assert(message); - assert(unit_name); assert(w >= 0); assert(w <= _INHIBIT_WHAT_MAX); @@ -1716,7 +1742,7 @@ static int method_do_shutdown_or_sleep( if (r != 0) return r; - r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error); + r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error); if (r < 0) return r; @@ -1728,7 +1754,7 @@ static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error return method_do_shutdown_or_sleep( m, message, - SPECIAL_POWEROFF_TARGET, + HANDLE_POWEROFF, INHIBIT_SHUTDOWN, "org.freedesktop.login1.power-off", "org.freedesktop.login1.power-off-multiple-sessions", @@ -1742,7 +1768,7 @@ static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error * return method_do_shutdown_or_sleep( m, message, - SPECIAL_REBOOT_TARGET, + HANDLE_REBOOT, INHIBIT_SHUTDOWN, "org.freedesktop.login1.reboot", "org.freedesktop.login1.reboot-multiple-sessions", @@ -1756,7 +1782,7 @@ static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error return method_do_shutdown_or_sleep( m, message, - SPECIAL_SUSPEND_TARGET, + HANDLE_SUSPEND, INHIBIT_SLEEP, "org.freedesktop.login1.suspend", "org.freedesktop.login1.suspend-multiple-sessions", @@ -1775,7 +1801,7 @@ static int nologin_timeout_handler( log_info("Creating /run/nologin, blocking further logins..."); - r = write_string_file_atomic("/run/nologin", "System is going down."); + r = write_string_file("/run/nologin", "System is going down.", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC); if (r < 0) log_error_errno(r, "Failed to create /run/nologin: %m"); else @@ -1846,7 +1872,7 @@ static int manager_scheduled_shutdown_handler( _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; Manager *m = userdata; - const char *target; + HandleAction action; int r; assert(m); @@ -1855,15 +1881,15 @@ static int manager_scheduled_shutdown_handler( return 0; if (streq(m->scheduled_shutdown_type, "halt")) - target = SPECIAL_HALT_TARGET; + action = HANDLE_HALT; else if (streq(m->scheduled_shutdown_type, "poweroff")) - target = SPECIAL_POWEROFF_TARGET; + action = HANDLE_POWEROFF; else - target = SPECIAL_REBOOT_TARGET; + action = HANDLE_REBOOT; - r = execute_shutdown_or_sleep(m, 0, target, &error); + r = execute_shutdown_or_sleep(m, 0, action, &error); if (r < 0) - return log_error_errno(r, "Unable to execute transition to %s: %m", target); + return log_error_errno(r, "Unable to execute transition to %s: %m", m->scheduled_shutdown_type); return 0; } @@ -1983,10 +2009,14 @@ static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userd m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source); m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source); - free(m->scheduled_shutdown_type); - m->scheduled_shutdown_type = NULL; + m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type); m->scheduled_shutdown_timeout = 0; + if (m->unlink_nologin) { + (void) unlink("/run/nologin"); + m->unlink_nologin = false; + } + if (cancelled) { _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; const char *tty = NULL; @@ -2011,7 +2041,7 @@ static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_erro return method_do_shutdown_or_sleep( m, message, - SPECIAL_HIBERNATE_TARGET, + HANDLE_HIBERNATE, INHIBIT_SLEEP, "org.freedesktop.login1.hibernate", "org.freedesktop.login1.hibernate-multiple-sessions", @@ -2025,7 +2055,7 @@ static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_e return method_do_shutdown_or_sleep( m, message, - SPECIAL_HYBRID_SLEEP_TARGET, + HANDLE_HYBRID_SLEEP, INHIBIT_SLEEP, "org.freedesktop.login1.hibernate", "org.freedesktop.login1.hibernate-multiple-sessions", @@ -2469,6 +2499,7 @@ const sd_bus_vtable manager_vtable[] = { SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0), SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0), SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0), + SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0), SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED), @@ -2498,8 +2529,6 @@ const sd_bus_vtable manager_vtable[] = { SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED), @@ -2507,6 +2536,8 @@ const sd_bus_vtable manager_vtable[] = { SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED), @@ -2524,6 +2555,8 @@ const sd_bus_vtable manager_vtable[] = { SD_BUS_VTABLE_END }; +/// UNNEEDED by elogind +#if 0 static int session_jobs_reply(Session *s, const char *unit, const char *result) { int r = 0; @@ -2563,7 +2596,7 @@ int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *err } if (m->action_job && streq(m->action_job, path)) { - log_info("Operation finished."); + log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what)); /* Tell people that they now may take a lock again */ send_prepare_for(m, m->action_what, false); @@ -2575,31 +2608,25 @@ int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *err } session = hashmap_get(m->session_units, unit); - if (session) { - -/// elogind does not support scope jobs -#if 0 - if (streq_ptr(path, session->scope_job)) - session->scope_job = mfree(session->scope_job); -#endif // 0 - + if (session && streq_ptr(path, session->scope_job)) { + session->scope_job = mfree(session->scope_job); session_jobs_reply(session, unit, result); session_save(session); + user_save(session->user); session_add_to_gc_queue(session); } user = hashmap_get(m->user_units, unit); - if (user) { + if (user && + (streq_ptr(path, user->service_job) || + streq_ptr(path, user->slice_job))) { -/// elogind does not support slice and service jobs -#if 0 if (streq_ptr(path, user->service_job)) user->service_job = mfree(user->service_job); if (streq_ptr(path, user->slice_job)) user->slice_job = mfree(user->slice_job); -#endif // 0 LIST_FOREACH(sessions_by_user, session, user->sessions) session_jobs_reply(session, unit, result); @@ -2698,6 +2725,7 @@ int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error return 0; } +#endif // 0 int manager_send_changed(Manager *manager, const char *property, ...) { char **l; @@ -2834,7 +2862,7 @@ int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, "StartUnit", error, &reply, - "ss", unit, "fail"); + "ss", unit, "replace"); if (r < 0) return r;