X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogind-session.c;h=011fc8f5b55597082f350c54e9ba4ed44509b959;hp=74f8ad1b0c60b8048c7d6634eb495f0fcdcc06cb;hb=85f248b26653f5322c26735661d63d4e8460c30e;hpb=ed18b08bed983b845c72a83666a7d7db546d89ad diff --git a/src/logind-session.c b/src/logind-session.c index 74f8ad1b0..011fc8f5b 100644 --- a/src/logind-session.c +++ b/src/logind-session.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "logind-session.h" #include "strv.h" @@ -55,7 +57,7 @@ Session* session_new(Manager *m, User *u, const char *id) { } s->manager = m; - s->pipe_fd = -1; + s->fifo_fd = -1; s->user = u; LIST_PREPEND(Session, sessions_by_user, u->sessions, s); @@ -83,6 +85,9 @@ void session_free(Session *s) { LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s); } + if (s->cgroup_path) + hashmap_remove(s->manager->cgroups, s->cgroup_path); + free(s->cgroup_path); strv_free(s->controllers); @@ -94,8 +99,7 @@ void session_free(Session *s) { hashmap_remove(s->manager->sessions, s->id); - if (s->pipe_fd >= 0) - close_nointr_nofail(s->pipe_fd); + session_remove_fifo(s); free(s->state_file); free(s); @@ -108,6 +112,9 @@ int session_save(Session *s) { assert(s); + if (!s->started) + return 0; + r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0); if (r < 0) goto finish; @@ -133,11 +140,21 @@ int session_save(Session *s) { s->remote, s->kill_processes); + if (s->type >= 0) + fprintf(f, + "TYPE=%s\n", + session_type_to_string(s->type)); + if (s->cgroup_path) fprintf(f, "CGROUP=%s\n", s->cgroup_path); + if (s->fifo_path) + fprintf(f, + "FIFO=%s\n", + s->fifo_path); + if (s->seat) fprintf(f, "SEAT=%s\n", @@ -207,7 +224,8 @@ int session_load(Session *s) { *seat = NULL, *vtnr = NULL, *leader = NULL, - *audit_id = NULL; + *audit_id = NULL, + *type = NULL; int k, r; @@ -217,6 +235,7 @@ int session_load(Session *s) { "REMOTE", &remote, "KILL_PROCESSES", &kill_processes, "CGROUP", &s->cgroup_path, + "FIFO", &s->fifo_path, "SEAT", &seat, "TTY", &s->tty, "DISPLAY", &s->display, @@ -225,6 +244,7 @@ int session_load(Session *s) { "SERVICE", &s->service, "VTNR", &vtnr, "LEADER", &leader, + "TYPE", &type, NULL); if (r < 0) @@ -269,6 +289,28 @@ int session_load(Session *s) { } } + if (type) { + SessionType t; + + t = session_type_from_string(type); + if (t >= 0) + s->type = t; + } + + if (s->fifo_path) { + int fd; + + /* If we open an unopened pipe for reading we will not + get an EOF. to trigger an EOF we hence open it for + reading, but close it right-away which then will + trigger the EOF. */ + + fd = session_create_fifo(s); + if (fd >= 0) + close_nointr_nofail(fd); + } + + finish: free(remote); free(kill_processes); @@ -307,15 +349,6 @@ int session_activate(Session *s) { return seat_apply_acls(s->seat, old_active); } -bool x11_display_is_local(const char *display) { - assert(display); - - return - display[0] == ':' && - display[1] >= '0' && - display[1] <= '9'; -} - static int session_link_x11_socket(Session *s) { char *t, *f, *c; size_t k; @@ -327,7 +360,7 @@ static int session_link_x11_socket(Session *s) { if (s->user->display) return 0; - if (!s->display || !x11_display_is_local(s->display)) + if (!s->display || !display_is_local(s->display)) return 0; k = strspn(s->display+1, "0123456789"); @@ -347,13 +380,15 @@ static int session_link_x11_socket(Session *s) { return -ENOENT; } - t = strappend(s->user->runtime_path, "/display"); + t = strappend(s->user->runtime_path, "/X11/display"); if (!t) { log_error("Out of memory"); free(f); return -ENOMEM; } + mkdir_parents(t, 0755); + if (link(f, t) < 0) { if (errno == EEXIST) { unlink(t); @@ -395,9 +430,11 @@ static int session_create_one_group(Session *s, const char *controller, const ch assert(controller); assert(path); - if (s->leader > 0) + if (s->leader > 0) { r = cg_create_and_attach(controller, path, s->leader); - else + if (r < 0) + r = cg_create(controller, path); + } else r = cg_create(controller, path); if (r < 0) @@ -429,9 +466,9 @@ static int session_create_cgroup(Session *s) { r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p); 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", p, strerror(-r)); return r; } @@ -450,6 +487,7 @@ static int session_create_cgroup(Session *s) { STRV_FOREACH(k, s->manager->controllers) { if (strv_contains(s->reset_controllers, *k) || + strv_contains(s->manager->reset_controllers, *k) || strv_contains(s->controllers, *k)) continue; @@ -466,8 +504,22 @@ static int session_create_cgroup(Session *s) { log_warning("Failed to reset controller %s: %s", *k, strerror(-r)); } + + STRV_FOREACH(k, s->manager->reset_controllers) { + + if (strv_contains(s->reset_controllers, *k) || + strv_contains(s->controllers, *k)) + continue; + + r = cg_attach(*k, "/", s->leader); + if (r < 0) + log_warning("Failed to reset controller %s: %s", *k, strerror(-r)); + + } } + hashmap_put(s->manager->cgroups, s->cgroup_path, s); + return 0; } @@ -494,16 +546,22 @@ int session_start(Session *s) { /* Create X11 symlink */ session_link_x11_socket(s); - /* Save session data */ - session_save(s); - dual_timestamp_get(&s->timestamp); + if (s->seat) + seat_read_active_vt(s->seat); + s->started = true; + /* Save session data */ + session_save(s); + user_save(s->user); + session_send_signal(s, true); if (s->seat) { + seat_save(s->seat); + if (s->seat->active == s) seat_send_changed(s->seat, "Sessions\0ActiveSession\0"); else @@ -530,7 +588,7 @@ static bool session_shall_kill(Session *s) { return strv_contains(s->manager->kill_only_users, s->user->name); } -static int session_kill_cgroup(Session *s) { +static int session_terminate_cgroup(Session *s) { int r; char **k; @@ -562,6 +620,8 @@ static int session_kill_cgroup(Session *s) { STRV_FOREACH(k, s->user->manager->controllers) cg_trim(*k, s->cgroup_path, true); + hashmap_remove(s->manager->cgroups, s->cgroup_path); + free(s->cgroup_path); s->cgroup_path = NULL; @@ -580,7 +640,7 @@ static int session_unlink_x11_socket(Session *s) { s->user->display = NULL; - t = strappend(s->user->runtime_path, "/display"); + t = strappend(s->user->runtime_path, "/X11/display"); if (!t) { log_error("Out of memory"); return -ENOMEM; @@ -601,7 +661,7 @@ int session_stop(Session *s) { log_info("Removed session %s.", s->id); /* Kill cgroup */ - k = session_kill_cgroup(s); + k = session_terminate_cgroup(s); if (k < 0) r = k; @@ -722,14 +782,80 @@ void session_set_idle_hint(Session *s, bool b) { "IdleSinceHintMonotonic\0"); } -int session_check_gc(Session *s) { +int session_create_fifo(Session *s) { + int r; + + assert(s); + + /* Create FIFO */ + if (!s->fifo_path) { + r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0); + if (r < 0) + return r; + + if (asprintf(&s->fifo_path, "/run/systemd/sessions/%s.ref", s->id) < 0) + return -ENOMEM; + + if (mkfifo(s->fifo_path, 0600) < 0 && errno != EEXIST) + return -errno; + } + + /* Open reading side */ + if (s->fifo_fd < 0) { + struct epoll_event ev; + + s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY); + if (s->fifo_fd < 0) + return -errno; + + r = hashmap_put(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1), s); + if (r < 0) + return r; + + zero(ev); + ev.events = 0; + ev.data.u32 = FD_FIFO_BASE + s->fifo_fd; + + if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, s->fifo_fd, &ev) < 0) + return -errno; + } + + /* Open writing side */ + r = open(s->fifo_path, O_WRONLY|O_CLOEXEC|O_NDELAY); + if (r < 0) + return -errno; + + return r; +} + +void session_remove_fifo(Session *s) { + assert(s); + + if (s->fifo_fd >= 0) { + assert_se(hashmap_remove(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1)) == 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; + } + + if (s->fifo_path) { + unlink(s->fifo_path); + free(s->fifo_path); + s->fifo_path = NULL; + } +} + +int session_check_gc(Session *s, bool drop_not_started) { int r; assert(s); - if (s->pipe_fd >= 0) { + if (drop_not_started && !s->started) + return 0; + + if (s->fifo_fd >= 0) { - r = pipe_eof(s->pipe_fd); + r = pipe_eof(s->fifo_fd); if (r < 0) return r; @@ -760,10 +886,58 @@ void session_add_to_gc_queue(Session *s) { s->in_gc_queue = true; } +int session_kill(Session *s, KillWho who, int signo) { + int r = 0; + Set *pid_set = NULL; + + assert(s); + + if (!s->cgroup_path) + return -ESRCH; + + if (s->leader <= 0 && who == KILL_LEADER) + return -ESRCH; + + if (s->leader > 0) + if (kill(s->leader, signo) < 0) + r = -errno; + + if (who == KILL_ALL) { + int q; + + pid_set = set_new(trivial_hash_func, trivial_compare_func); + if (!pid_set) + return -ENOMEM; + + if (s->leader > 0) { + q = set_put(pid_set, LONG_TO_PTR(s->leader)); + if (q < 0) + r = q; + } + + q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, signo, false, true, false, pid_set); + if (q < 0) + if (q != -EAGAIN && q != -ESRCH && q != -ENOENT) + r = q; + } + + if (pid_set) + set_free(pid_set); + + return r; +} + static const char* const session_type_table[_SESSION_TYPE_MAX] = { [SESSION_TTY] = "tty", [SESSION_X11] = "x11", - [SESSION_OTHER] = "other" + [SESSION_UNSPECIFIED] = "unspecified" }; DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType); + +static const char* const kill_who_table[_KILL_WHO_MAX] = { + [KILL_LEADER] = "leader", + [KILL_ALL] = "all" +}; + +DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);