1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/epoll.h>
27 #include "logind-session.h"
30 #include "cgroup-util.h"
32 #define IDLE_THRESHOLD_USEC (5*USEC_PER_MINUTE)
34 Session* session_new(Manager *m, User *u, const char *id) {
44 s->state_file = strappend("/run/systemd/sessions/", id);
50 s->id = file_name_from_path(s->state_file);
52 if (hashmap_put(m->sessions, s->id, s) < 0) {
62 LIST_PREPEND(Session, sessions_by_user, u->sessions, s);
67 void session_free(Session *s) {
71 LIST_REMOVE(Session, gc_queue, s->manager->session_gc_queue, s);
74 LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s);
76 if (s->user->display == s)
77 s->user->display = NULL;
81 if (s->seat->active == s)
82 s->seat->active = NULL;
84 LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s);
88 hashmap_remove(s->manager->cgroups, s->cgroup_path);
91 strv_free(s->controllers);
99 hashmap_remove(s->manager->sessions, s->id);
101 session_unset_pipe_fd(s);
107 int session_save(Session *s) {
117 r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
121 r = fopen_temporary(s->state_file, &f, &temp_path);
127 fchmod(fileno(f), 0644);
130 "# This is private data. Do not parse.\n"
135 "KILL_PROCESSES=%i\n",
136 (unsigned long) s->user->uid,
138 session_is_active(s),
145 session_type_to_string(s->type));
182 if (s->seat && seat_is_vtconsole(s->seat))
190 (unsigned long) s->leader);
195 (unsigned long long) s->audit_id);
199 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
201 unlink(s->state_file);
210 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
215 int session_load(Session *s) {
217 *kill_processes = NULL,
228 r = parse_env_file(s->state_file, NEWLINE,
230 "KILL_PROCESSES", &kill_processes,
231 "CGROUP", &s->cgroup_path,
234 "DISPLAY", &s->display,
235 "REMOTE_HOST", &s->remote_host,
236 "REMOTE_USER", &s->remote_user,
237 "SERVICE", &s->service,
247 k = parse_boolean(remote);
252 if (kill_processes) {
253 k = parse_boolean(kill_processes);
255 s->kill_processes = k;
258 if (seat && !s->seat) {
261 o = hashmap_get(s->manager->seats, seat);
263 seat_attach_session(o, s);
266 if (vtnr && s->seat && seat_is_vtconsole(s->seat)) {
269 k = safe_atoi(vtnr, &v);
270 if (k >= 0 && v >= 1)
277 k = parse_pid(leader, &pid);
278 if (k >= 0 && pid >= 1) {
281 audit_session_from_pid(pid, &s->audit_id);
288 t = session_type_from_string(type);
295 free(kill_processes);
304 int session_activate(Session *s) {
316 if (s->seat->active == s)
319 assert(seat_is_vtconsole(s->seat));
325 old_active = s->seat->active;
328 return seat_apply_acls(s->seat, old_active);
331 static int session_link_x11_socket(Session *s) {
337 assert(s->user->runtime_path);
339 if (s->user->display)
342 if (!s->display || !display_is_local(s->display))
345 k = strspn(s->display+1, "0123456789");
346 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
348 log_error("Out of memory");
352 c = stpcpy(f, "/tmp/.X11-unix/X");
353 memcpy(c, s->display+1, k);
356 if (access(f, F_OK) < 0) {
357 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
362 t = strappend(s->user->runtime_path, "/display");
364 log_error("Out of memory");
369 if (link(f, t) < 0) {
370 if (errno == EEXIST) {
377 if (symlink(f, t) < 0) {
379 if (errno == EEXIST) {
382 if (symlink(f, t) >= 0)
386 log_error("Failed to link %s to %s: %m", f, t);
394 log_info("Linked %s to %s.", f, t);
398 s->user->display = s;
403 static int session_create_one_group(Session *s, const char *controller, const char *path) {
411 r = cg_create_and_attach(controller, path, s->leader);
413 r = cg_create(controller, path);
415 r = cg_create(controller, path);
420 r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid);
422 r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid);
427 static int session_create_cgroup(Session *s) {
434 assert(s->user->cgroup_path);
436 if (!s->cgroup_path) {
437 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
438 log_error("Out of memory");
444 r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
446 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
448 s->cgroup_path = NULL;
454 STRV_FOREACH(k, s->controllers) {
456 if (strv_contains(s->reset_controllers, *k))
459 r = session_create_one_group(s, *k, p);
461 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
464 STRV_FOREACH(k, s->manager->controllers) {
466 if (strv_contains(s->reset_controllers, *k) ||
467 strv_contains(s->controllers, *k))
470 r = session_create_one_group(s, *k, p);
472 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
477 STRV_FOREACH(k, s->reset_controllers) {
478 r = cg_attach(*k, "/", s->leader);
480 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
485 hashmap_put(s->manager->cgroups, s->cgroup_path, s);
490 int session_start(Session *s) {
499 r = user_start(s->user);
503 log_info("New session %s of user %s.", s->id, s->user->name);
506 r = session_create_cgroup(s);
510 /* Create X11 symlink */
511 session_link_x11_socket(s);
513 dual_timestamp_get(&s->timestamp);
516 seat_read_active_vt(s->seat);
520 /* Save session data */
524 session_send_signal(s, true);
529 if (s->seat->active == s)
530 seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
532 seat_send_changed(s->seat, "Sessions\0");
535 user_send_changed(s->user, "Sessions\0");
540 static bool session_shall_kill(Session *s) {
543 if (!s->kill_processes)
546 if (strv_contains(s->manager->kill_exclude_users, s->user->name))
549 if (strv_isempty(s->manager->kill_only_users))
552 return strv_contains(s->manager->kill_only_users, s->user->name);
555 static int session_kill_cgroup(Session *s) {
564 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
566 if (session_shall_kill(s)) {
568 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
570 log_error("Failed to kill session cgroup: %s", strerror(-r));
573 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
575 log_error("Failed to check session cgroup: %s", strerror(-r));
577 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
579 log_error("Failed to delete session cgroup: %s", strerror(-r));
584 STRV_FOREACH(k, s->user->manager->controllers)
585 cg_trim(*k, s->cgroup_path, true);
587 hashmap_remove(s->manager->cgroups, s->cgroup_path);
589 free(s->cgroup_path);
590 s->cgroup_path = NULL;
595 static int session_unlink_x11_socket(Session *s) {
602 if (s->user->display != s)
605 s->user->display = NULL;
607 t = strappend(s->user->runtime_path, "/display");
609 log_error("Out of memory");
616 return r < 0 ? -errno : 0;
619 int session_stop(Session *s) {
625 log_info("Removed session %s.", s->id);
628 k = session_kill_cgroup(s);
632 /* Remove X11 symlink */
633 session_unlink_x11_socket(s);
635 unlink(s->state_file);
636 session_add_to_gc_queue(s);
637 user_add_to_gc_queue(s->user);
640 session_send_signal(s, false);
643 if (s->seat->active == s)
644 seat_set_active(s->seat, NULL);
646 seat_send_changed(s->seat, "Sessions\0");
649 user_send_changed(s->user, "Sessions\0");
656 bool session_is_active(Session *s) {
662 return s->seat->active == s;
665 int session_get_idle_hint(Session *s, dual_timestamp *t) {
676 *t = s->idle_hint_timestamp;
684 if (s->tty[0] != '/') {
685 p = strappend("/dev/", s->tty);
691 if (!startswith(p ? p : s->tty, "/dev/")) {
696 k = lstat(p ? p : s->tty, &st);
702 u = timespec_load(&st.st_atim);
703 n = now(CLOCK_REALTIME);
704 b = u + IDLE_THRESHOLD_USEC < n;
707 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
713 *t = s->idle_hint_timestamp;
718 void session_set_idle_hint(Session *s, bool b) {
721 if (s->idle_hint == b)
725 dual_timestamp_get(&s->idle_hint_timestamp);
727 session_send_changed(s,
730 "IdleSinceHintMonotonic\0");
733 seat_send_changed(s->seat,
736 "IdleSinceHintMonotonic\0");
738 user_send_changed(s->user,
741 "IdleSinceHintMonotonic\0");
743 manager_send_changed(s->manager,
746 "IdleSinceHintMonotonic\0");
749 int session_set_pipe_fd(Session *s, int fd) {
750 struct epoll_event ev;
755 assert(s->pipe_fd < 0);
757 r = hashmap_put(s->manager->pipe_fds, INT_TO_PTR(fd + 1), s);
763 ev.data.u32 = FD_PIPE_BASE + fd;
765 if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
766 assert_se(hashmap_remove(s->manager->pipe_fds, INT_TO_PTR(fd + 1)) == s);
774 void session_unset_pipe_fd(Session *s) {
780 assert_se(hashmap_remove(s->manager->pipe_fds, INT_TO_PTR(s->pipe_fd + 1)) == s);
782 assert_se(epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_DEL, s->pipe_fd, NULL) == 0);
784 close_nointr_nofail(s->pipe_fd);
788 int session_check_gc(Session *s) {
793 if (s->pipe_fd >= 0) {
795 r = pipe_eof(s->pipe_fd);
803 if (s->cgroup_path) {
805 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
816 void session_add_to_gc_queue(Session *s) {
822 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
823 s->in_gc_queue = true;
826 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
827 [SESSION_TTY] = "tty",
828 [SESSION_X11] = "x11",
829 [SESSION_UNSPECIFIED] = "unspecified"
832 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);