X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogin%2Flogind-session.c;h=10ea5265174dec96976d37a010c7dcea353fd0d7;hp=d3433731e56c48a57e9ade7e72be24351f668674;hb=ec202eae8e84a4c99f054f771cb832046cb8769f;hpb=718db96199eb307751264e4163555662c9a389fa diff --git a/src/login/logind-session.c b/src/login/logind-session.c index d3433731e..10ea52651 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -20,9 +20,13 @@ ***/ #include +#include +#include +#include +#include #include +#include #include -#include #include "sd-id128.h" #include "sd-messages.h" @@ -75,7 +79,7 @@ Session* session_new(Manager *m, const char *id) { return NULL; } - s->id = path_get_file_name(s->state_file); + s->id = basename(s->state_file); if (hashmap_put(m->sessions, s->id, s) < 0) { hashmap_free(s->devices); @@ -86,6 +90,7 @@ Session* session_new(Manager *m, const char *id) { s->manager = m; s->fifo_fd = -1; + s->vtfd = -1; return s; } @@ -224,7 +229,7 @@ int session_save(Session *s) { fprintf(f, "SERVICE=%s\n", s->service); if (s->seat && seat_has_vts(s->seat)) - fprintf(f, "VTNR=%i\n", s->vtnr); + fprintf(f, "VTNR=%u\n", s->vtnr); if (s->leader > 0) fprintf(f, "LEADER=%lu\n", (unsigned long) s->leader); @@ -239,6 +244,9 @@ int session_save(Session *s) { (unsigned long long) s->timestamp.realtime, (unsigned long long) s->timestamp.monotonic); + if (s->controller) + fprintf(f, "CONTROLLER=%s\n", s->controller); + fflush(f); if (ferror(f) || rename(temp_path, s->state_file) < 0) { @@ -263,7 +271,8 @@ int session_load(Session *s) { *class = NULL, *uid = NULL, *realtime = NULL, - *monotonic = NULL; + *monotonic = NULL, + *controller = NULL; int k, r; @@ -287,6 +296,7 @@ int session_load(Session *s) { "UID", &uid, "REALTIME", &realtime, "MONOTONIC", &monotonic, + "CONTROLLER", &controller, NULL); if (r < 0) { @@ -324,21 +334,21 @@ int session_load(Session *s) { s->remote = k; } + if (vtnr) + safe_atou(vtnr, &s->vtnr); + if (seat && !s->seat) { Seat *o; o = hashmap_get(s->manager->seats, seat); if (o) - seat_attach_session(o, s); + r = seat_attach_session(o, s); + if (!o || r < 0) + log_error("Cannot attach session %s to seat %s", s->id, seat); } - if (vtnr && s->seat && seat_has_vts(s->seat)) { - int v; - - k = safe_atoi(vtnr, &v); - if (k >= 0 && v >= 1) - s->vtnr = v; - } + if (!s->seat || !seat_has_vts(s->seat)) + s->vtnr = 0; if (leader) { k = parse_pid(leader, &s->leader); @@ -387,6 +397,13 @@ int session_load(Session *s) { s->timestamp.monotonic = l; } + if (controller) { + if (bus_name_has_owner(s->manager->bus, controller, NULL) > 0) + session_set_controller(s, controller, false); + else + session_restore_vt(s); + } + return r; } @@ -404,7 +421,7 @@ int session_activate(Session *s) { /* on seats with VTs, we let VTs manage session-switching */ if (seat_has_vts(s->seat)) { - if (s->vtnr <= 0) + if (!s->vtnr) return -ENOTSUP; return chvt(s->vtnr); @@ -937,9 +954,6 @@ void session_add_to_gc_queue(Session *s) { SessionState session_get_state(Session *s) { assert(s); - if (s->closing) - return SESSION_CLOSING; - if (s->scope_job) return SESSION_OPENING; @@ -961,12 +975,129 @@ int session_kill(Session *s, KillWho who, int signo) { return manager_kill_unit(s->manager, s->scope, who, signo, NULL); } +static int session_open_vt(Session *s) { + char path[128]; + + if (!s->vtnr) + return -1; + + if (s->vtfd >= 0) + return s->vtfd; + + sprintf(path, "/dev/tty%u", s->vtnr); + s->vtfd = open(path, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY); + if (s->vtfd < 0) { + log_error("cannot open VT %s of session %s: %m", path, s->id); + return -1; + } + + return s->vtfd; +} + +static int session_vt_fn(sd_event_source *source, const struct signalfd_siginfo *si, void *data) { + Session *s = data; + + if (s->vtfd >= 0) + ioctl(s->vtfd, VT_RELDISP, 1); + + return 0; +} + +void session_mute_vt(Session *s) { + int vt, r; + struct vt_mode mode = { 0 }; + sigset_t mask; + + vt = session_open_vt(s); + if (vt < 0) + return; + + r = ioctl(vt, KDSKBMODE, K_OFF); + if (r < 0) + goto error; + + r = ioctl(vt, KDSETMODE, KD_GRAPHICS); + if (r < 0) + goto error; + + sigemptyset(&mask); + sigaddset(&mask, SIGUSR1); + sigprocmask(SIG_BLOCK, &mask, NULL); + + r = sd_event_add_signal(s->manager->event, SIGUSR1, session_vt_fn, s, &s->vt_source); + if (r < 0) + goto error; + + /* Oh, thanks to the VT layer, VT_AUTO does not work with KD_GRAPHICS. + * So we need a dummy handler here which just acknowledges *all* VT + * switch requests. */ + mode.mode = VT_PROCESS; + mode.relsig = SIGUSR1; + mode.acqsig = SIGUSR1; + r = ioctl(vt, VT_SETMODE, &mode); + if (r < 0) + goto error; + + return; + +error: + log_error("cannot mute VT %u for session %s (%d/%d)", s->vtnr, s->id, r, errno); + session_restore_vt(s); +} + +void session_restore_vt(Session *s) { + _cleanup_free_ char *utf8; + int vt, kb = K_XLATE; + struct vt_mode mode = { 0 }; + + vt = session_open_vt(s); + if (vt < 0) + return; + + sd_event_source_unref(s->vt_source); + s->vt_source = NULL; + + ioctl(vt, KDSETMODE, KD_TEXT); + + if (read_one_line_file("/sys/module/vt/parameters/default_utf8", &utf8) >= 0 && *utf8 == '1') + kb = K_UNICODE; + ioctl(vt, KDSKBMODE, kb); + + mode.mode = VT_AUTO; + ioctl(vt, VT_SETMODE, &mode); + + close_nointr_nofail(vt); + s->vtfd = -1; +} + bool session_is_controller(Session *s, const char *sender) { assert(s); return streq_ptr(s->controller, sender); } +static void session_swap_controller(Session *s, char *name) { + SessionDevice *sd; + + if (s->controller) { + manager_drop_busname(s->manager, s->controller); + free(s->controller); + s->controller = NULL; + + /* Drop all devices as they're now unused. Do that after the + * controller is released to avoid sending out useles + * dbus signals. */ + while ((sd = hashmap_first(s->devices))) + session_device_free(sd); + + if (!name) + session_restore_vt(s); + } + + s->controller = name; + session_save(s); +} + int session_set_controller(Session *s, const char *sender, bool force) { char *t; int r; @@ -989,28 +1120,28 @@ int session_set_controller(Session *s, const char *sender, bool force) { return r; } - session_drop_controller(s); + session_swap_controller(s, t); + + /* When setting a session controller, we forcibly mute the VT and set + * it into graphics-mode. Applications can override that by changing + * VT state after calling TakeControl(). However, this serves as a good + * default and well-behaving controllers can now ignore VTs entirely. + * Note that we reset the VT on ReleaseControl() and if the controller + * exits. + * If logind crashes/restarts, we restore the controller during restart + * or reset the VT in case it crashed/exited, too. */ + session_mute_vt(s); - s->controller = t; return 0; } void session_drop_controller(Session *s) { - SessionDevice *sd; - assert(s); if (!s->controller) return; - manager_drop_busname(s->manager, s->controller); - free(s->controller); - s->controller = NULL; - - /* Drop all devices as they're now unused. Do that after the controller - * is released to avoid sending out useles dbus signals. */ - while ((sd = hashmap_first(s->devices))) - session_device_free(sd); + session_swap_controller(s, NULL); } static const char* const session_state_table[_SESSION_STATE_MAX] = {