X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flogin%2Flogind-session.c;h=760425329b70253eb2d8bf1523211cc2143cc471;hb=4ad490007b70e6ac18d3cb04fa2ed92eba1451fa;hp=16d4955d5d94bc1f69151140ad689026149ec12b;hpb=0d0f0c50d3a1d90f03972a6abb82e6413daaa583;p=elogind.git diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 16d4955d5..760425329 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -25,16 +25,17 @@ #include #include +#include "systemd/sd-id128.h" +#include "systemd/sd-messages.h" #include "strv.h" #include "util.h" #include "mkdir.h" #include "path-util.h" #include "cgroup-util.h" #include "logind-session.h" +#include "fileio.h" -#define IDLE_THRESHOLD_USEC (5*USEC_PER_MINUTE) - -Session* session_new(Manager *m, User *u, const char *id) { +Session* session_new(Manager *m, const char *id) { Session *s; assert(m); @@ -60,9 +61,6 @@ Session* session_new(Manager *m, User *u, const char *id) { s->manager = m; s->fifo_fd = -1; - s->user = u; - - LIST_PREPEND(Session, sessions_by_user, u->sessions, s); return s; } @@ -98,6 +96,7 @@ void session_free(Session *s) { free(s->remote_host); free(s->remote_user); free(s->service); + free(s->slice); hashmap_remove(s->manager->sessions, s->id); session_remove_fifo(s); @@ -106,13 +105,24 @@ void session_free(Session *s) { free(s); } +void session_set_user(Session *s, User *u) { + assert(s); + assert(!s->user); + + s->user = u; + LIST_PREPEND(Session, sessions_by_user, u->sessions, s); +} + int session_save(Session *s) { - FILE *f; + _cleanup_fclose_ FILE *f = NULL; + _cleanup_free_ char *temp_path = NULL; int r = 0; - char *temp_path; assert(s); + if (!s->user) + return -ESTALE; + if (!s->started) return 0; @@ -144,69 +154,53 @@ int session_save(Session *s) { s->kill_processes); if (s->type >= 0) - fprintf(f, - "TYPE=%s\n", - session_type_to_string(s->type)); + fprintf(f, "TYPE=%s\n", session_type_to_string(s->type)); if (s->class >= 0) - fprintf(f, - "CLASS=%s\n", - session_class_to_string(s->class)); + fprintf(f, "CLASS=%s\n", session_class_to_string(s->class)); if (s->cgroup_path) - fprintf(f, - "CGROUP=%s\n", - s->cgroup_path); + fprintf(f, "CGROUP=%s\n", s->cgroup_path); if (s->fifo_path) - fprintf(f, - "FIFO=%s\n", - s->fifo_path); + fprintf(f, "FIFO=%s\n", s->fifo_path); if (s->seat) - fprintf(f, - "SEAT=%s\n", - s->seat->id); + fprintf(f, "SEAT=%s\n", s->seat->id); if (s->tty) - fprintf(f, - "TTY=%s\n", - s->tty); + fprintf(f, "TTY=%s\n", s->tty); if (s->display) - fprintf(f, - "DISPLAY=%s\n", - s->display); + fprintf(f, "DISPLAY=%s\n", s->display); if (s->remote_host) - fprintf(f, - "REMOTE_HOST=%s\n", - s->remote_host); + fprintf(f, "REMOTE_HOST=%s\n", s->remote_host); if (s->remote_user) - fprintf(f, - "REMOTE_USER=%s\n", - s->remote_user); + fprintf(f, "REMOTE_USER=%s\n", s->remote_user); if (s->service) - fprintf(f, - "SERVICE=%s\n", - s->service); + fprintf(f, "SERVICE=%s\n", s->service); + + if (s->seat) + fprintf(f, "SLICE=%s\n", s->slice); if (s->seat && seat_can_multi_session(s->seat)) - fprintf(f, - "VTNR=%i\n", - s->vtnr); + fprintf(f, "VTNR=%i\n", s->vtnr); if (s->leader > 0) - fprintf(f, - "LEADER=%lu\n", - (unsigned long) s->leader); + fprintf(f, "LEADER=%lu\n", (unsigned long) s->leader); if (s->audit_id > 0) + fprintf(f, "AUDIT=%"PRIu32"\n", s->audit_id); + + if (dual_timestamp_is_set(&s->timestamp)) fprintf(f, - "AUDIT=%llu\n", - (unsigned long long) s->audit_id); + "REALTIME=%llu\n" + "MONOTONIC=%llu\n", + (unsigned long long) s->timestamp.realtime, + (unsigned long long) s->timestamp.monotonic); fflush(f); @@ -216,9 +210,6 @@ int session_save(Session *s) { unlink(temp_path); } - fclose(f); - free(temp_path); - finish: if (r < 0) log_error("Failed to save session data for %s: %s", s->id, strerror(-r)); @@ -227,14 +218,17 @@ finish: } int session_load(Session *s) { - char *remote = NULL, + _cleanup_free_ char *remote = NULL, *kill_processes = NULL, *seat = NULL, *vtnr = NULL, *leader = NULL, *audit_id = NULL, *type = NULL, - *class = NULL; + *class = NULL, + *uid = NULL, + *realtime = NULL, + *monotonic = NULL; int k, r; @@ -251,14 +245,44 @@ int session_load(Session *s) { "REMOTE_HOST", &s->remote_host, "REMOTE_USER", &s->remote_user, "SERVICE", &s->service, + "SLICE", &s->slice, "VTNR", &vtnr, "LEADER", &leader, "TYPE", &type, "CLASS", &class, + "UID", &uid, + "REALTIME", &realtime, + "MONOTONIC", &monotonic, NULL); - if (r < 0) - goto finish; + if (r < 0) { + log_error("Failed to read %s: %s", s->state_file, strerror(-r)); + return r; + } + + if (!s->user) { + uid_t u; + User *user; + + if (!uid) { + log_error("UID not specified for session %s", s->id); + return -ENOENT; + } + + r = parse_uid(uid, &u); + if (r < 0) { + log_error("Failed to parse UID value %s for session %s.", uid, s->id); + return r; + } + + user = hashmap_get(s->manager->users, ULONG_TO_PTR((unsigned long) u)); + if (!user) { + log_error("User of session %s not known.", s->id); + return -ENOENT; + } + + session_set_user(s, user); + } if (remote) { k = parse_boolean(remote); @@ -323,14 +347,17 @@ int session_load(Session *s) { close_nointr_nofail(fd); } -finish: - free(remote); - free(kill_processes); - free(seat); - free(vtnr); - free(leader); - free(audit_id); - free(class); + if (realtime) { + unsigned long long l; + if (sscanf(realtime, "%llu", &l) > 0) + s->timestamp.realtime = l; + } + + if (monotonic) { + unsigned long long l; + if (sscanf(monotonic, "%llu", &l) > 0) + s->timestamp.monotonic = l; + } return r; } @@ -339,6 +366,7 @@ int session_activate(Session *s) { int r; assert(s); + assert(s->user); if (s->vtnr < 0) return -ENOTSUP; @@ -435,20 +463,21 @@ static int session_create_one_group(Session *s, const char *controller, const ch int r; assert(s); - assert(controller); + assert(s->user); assert(path); - if (s->leader > 0) { + if (s->leader > 0) r = cg_create_and_attach(controller, path, s->leader); - if (r < 0) - r = cg_create(controller, path); - } else - r = cg_create(controller, path); + else + r = -EINVAL; - if (r < 0) - return r; + if (r < 0) { + r = cg_create(controller, path); + if (r < 0) + return r; + } - r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid, -1); + r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid); if (r >= 0) r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid); @@ -457,7 +486,6 @@ static int session_create_one_group(Session *s, const char *controller, const ch static int session_create_cgroup(Session *s) { char **k; - char *p; int r; assert(s); @@ -465,29 +493,51 @@ static int session_create_cgroup(Session *s) { assert(s->user->cgroup_path); if (!s->cgroup_path) { - if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) + _cleanup_free_ char *name = NULL, *escaped = NULL; + + name = strappend(s->id, ".session"); + if (!name) return log_oom(); - } else - p = s->cgroup_path; - r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p); + escaped = cg_escape(name); + if (!escaped) + return log_oom(); + + if (s->slice) { + _cleanup_free_ char *slice = NULL; + + r = cg_slice_to_path(s->slice, &slice); + if (r < 0) + return r; + + s->cgroup_path = strjoin(s->manager->cgroup_root, "/", slice, "/", escaped, NULL); + } else + s->cgroup_path = strjoin(s->user->cgroup_path, "/", escaped, NULL); + + if (!s->cgroup_path) + return log_oom(); + } + + if (!s->slice) { + s->slice = strdup(s->user->slice); + if (!s->slice) + return log_oom(); + } + + r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path); if (r < 0) { - log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r)); - free(p); - s->cgroup_path = NULL; + log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", s->cgroup_path, strerror(-r)); return r; } - s->cgroup_path = p; - STRV_FOREACH(k, s->controllers) { if (strv_contains(s->reset_controllers, *k)) continue; - r = session_create_one_group(s, *k, p); + r = session_create_one_group(s, *k, s->cgroup_path); if (r < 0) - log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r)); + log_warning("Failed to create %s:%s: %s", *k, s->cgroup_path, strerror(-r)); } STRV_FOREACH(k, s->manager->controllers) { @@ -497,9 +547,9 @@ static int session_create_cgroup(Session *s) { strv_contains(s->controllers, *k)) continue; - r = session_create_one_group(s, *k, p); + r = session_create_one_group(s, *k, s->cgroup_path); if (r < 0) - log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r)); + log_warning("Failed to create %s:%s: %s", *k, s->cgroup_path, strerror(-r)); } if (s->leader > 0) { @@ -520,11 +570,12 @@ static int session_create_cgroup(Session *s) { r = cg_attach(*k, "/", s->leader); if (r < 0) log_warning("Failed to reset controller %s: %s", *k, strerror(-r)); - } } - hashmap_put(s->manager->session_cgroups, s->cgroup_path, s); + r = hashmap_put(s->manager->session_cgroups, s->cgroup_path, s); + if (r < 0) + log_warning("Failed to create mapping between cgroup and session"); return 0; } @@ -533,7 +584,9 @@ int session_start(Session *s) { int r; assert(s); - assert(s->user); + + if (!s->user) + return -ESTALE; if (s->started) return 0; @@ -542,8 +595,13 @@ int session_start(Session *s) { if (r < 0) return r; - log_full(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG, - "New session %s of user %s.", s->id, s->user->name); + log_struct(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG, + MESSAGE_ID(SD_MESSAGE_SESSION_START), + "SESSION_ID=%s", s->id, + "USER_ID=%s", s->user->name, + "LEADER=%lu", (unsigned long) s->leader, + "MESSAGE=New session %s of user %s.", s->id, s->user->name, + NULL); /* Create cgroup */ r = session_create_cgroup(s); @@ -553,7 +611,8 @@ int session_start(Session *s) { /* Create X11 symlink */ session_link_x11_socket(s); - dual_timestamp_get(&s->timestamp); + if (!dual_timestamp_is_set(&s->timestamp)) + dual_timestamp_get(&s->timestamp); if (s->seat) seat_read_active_vt(s->seat); @@ -678,9 +737,17 @@ int session_stop(Session *s) { assert(s); + if (!s->user) + return -ESTALE; + if (s->started) - log_full(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG, - "Removed session %s.", s->id); + log_struct(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG, + MESSAGE_ID(SD_MESSAGE_SESSION_STOP), + "SESSION_ID=%s", s->id, + "USER_ID=%s", s->user->name, + "LEADER=%lu", (unsigned long) s->leader, + "MESSAGE=Removed session %s.", s->id, + NULL); /* Kill cgroup */ k = session_terminate_cgroup(s); @@ -697,16 +764,18 @@ int session_stop(Session *s) { if (s->started) session_send_signal(s, false); + s->started = false; + if (s->seat) { if (s->seat->active == s) seat_set_active(s->seat, NULL); seat_send_changed(s->seat, "Sessions\0"); + seat_save(s->seat); } user_send_changed(s->user, "Sessions\0"); - - s->started = false; + user_save(s->user); return r; } @@ -720,15 +789,50 @@ bool session_is_active(Session *s) { return s->seat->active == s; } -int session_get_idle_hint(Session *s, dual_timestamp *t) { - char *p; +static int get_tty_atime(const char *tty, usec_t *atime) { + _cleanup_free_ char *p = NULL; struct stat st; - usec_t u, n; - bool b; - int k; + + assert(tty); + assert(atime); + + if (!path_is_absolute(tty)) { + p = strappend("/dev/", tty); + if (!p) + return -ENOMEM; + + tty = p; + } else if (!path_startswith(tty, "/dev/")) + return -ENOENT; + + if (lstat(tty, &st) < 0) + return -errno; + + *atime = timespec_load(&st.st_atim); + return 0; +} + +static int get_process_ctty_atime(pid_t pid, usec_t *atime) { + _cleanup_free_ char *p = NULL; + int r; + + assert(pid > 0); + assert(atime); + + r = get_ctty(pid, NULL, &p); + if (r < 0) + return r; + + return get_tty_atime(p, atime); +} + +int session_get_idle_hint(Session *s, dual_timestamp *t) { + usec_t atime = 0, n; + int r; assert(s); + /* Explicit idle hint is set */ if (s->idle_hint) { if (t) *t = s->idle_hint_timestamp; @@ -736,41 +840,65 @@ int session_get_idle_hint(Session *s, dual_timestamp *t) { return s->idle_hint; } - if (isempty(s->tty)) + /* Graphical sessions should really implement a real + * idle hint logic */ + if (s->display) goto dont_know; - if (s->tty[0] != '/') { - p = strappend("/dev/", s->tty); - if (!p) - return -ENOMEM; - } else - p = NULL; + /* For sessions with an explicitly configured tty, let's check + * its atime */ + if (s->tty) { + r = get_tty_atime(s->tty, &atime); + if (r >= 0) + goto found_atime; + } - if (!startswith(p ? p : s->tty, "/dev/")) { - free(p); - goto dont_know; + /* For sessions with a leader but no explicitly configured + * tty, let's check the controlling tty of the leader */ + if (s->leader > 0) { + r = get_process_ctty_atime(s->leader, &atime); + if (r >= 0) + goto found_atime; } - k = lstat(p ? p : s->tty, &st); - free(p); + /* For other TTY sessions, let's find the most recent atime of + * the ttys of any of the processes of the session */ + if (s->cgroup_path) { + _cleanup_fclose_ FILE *f = NULL; - if (k < 0) - goto dont_know; + if (cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, &f) >= 0) { + pid_t pid; - u = timespec_load(&st.st_atim); - n = now(CLOCK_REALTIME); - b = u + IDLE_THRESHOLD_USEC < n; + atime = 0; + while (cg_read_pid(f, &pid) > 0) { + usec_t a; - if (t) - dual_timestamp_from_realtime(t, u + b*IDLE_THRESHOLD_USEC); + if (get_process_ctty_atime(pid, &a) >= 0) + if (atime == 0 || atime < a) + atime = a; + } - return b; + if (atime != 0) + goto found_atime; + } + } dont_know: if (t) *t = s->idle_hint_timestamp; return 0; + +found_atime: + if (t) + dual_timestamp_from_realtime(t, atime); + + n = now(CLOCK_REALTIME); + + if (s->manager->idle_action_usec <= 0) + return 0; + + return atime + s->manager->idle_action_usec <= n; } void session_set_idle_hint(Session *s, bool b) { @@ -824,7 +952,7 @@ int session_create_fifo(Session *s) { /* Open reading side */ if (s->fifo_fd < 0) { - struct epoll_event ev; + struct epoll_event ev = {}; s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY); if (s->fifo_fd < 0) @@ -834,7 +962,6 @@ int session_create_fifo(Session *s) { if (r < 0) return r; - zero(ev); ev.events = 0; ev.data.u32 = FD_OTHER_BASE + s->fifo_fd; @@ -858,6 +985,9 @@ void session_remove_fifo(Session *s) { assert_se(epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_DEL, s->fifo_fd, NULL) == 0); close_nointr_nofail(s->fifo_fd); s->fifo_fd = -1; + + session_save(s); + user_save(s->user); } if (s->fifo_path) { @@ -875,6 +1005,9 @@ int session_check_gc(Session *s, bool drop_not_started) { if (drop_not_started && !s->started) return 0; + if (!s->user) + return 0; + if (s->fifo_fd >= 0) { r = pipe_eof(s->fifo_fd); @@ -921,8 +1054,8 @@ SessionState session_get_state(Session *s) { } int session_kill(Session *s, KillWho who, int signo) { + _cleanup_set_free_ Set *pid_set = NULL; int r = 0; - Set *pid_set = NULL; assert(s); @@ -941,7 +1074,7 @@ int session_kill(Session *s, KillWho who, int signo) { pid_set = set_new(trivial_hash_func, trivial_compare_func); if (!pid_set) - return -ENOMEM; + return log_oom(); if (s->leader > 0) { q = set_put(pid_set, LONG_TO_PTR(s->leader)); @@ -955,9 +1088,6 @@ int session_kill(Session *s, KillWho who, int signo) { r = q; } - if (pid_set) - set_free(pid_set); - return r; } @@ -980,7 +1110,8 @@ DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType); static const char* const session_class_table[_SESSION_CLASS_MAX] = { [SESSION_USER] = "user", [SESSION_GREETER] = "greeter", - [SESSION_LOCK_SCREEN] = "lock-screen" + [SESSION_LOCK_SCREEN] = "lock-screen", + [SESSION_BACKGROUND] = "background" }; DEFINE_STRING_TABLE_LOOKUP(session_class, SessionClass);