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>
28 #include "logind-session.h"
31 #include "cgroup-util.h"
33 #define IDLE_THRESHOLD_USEC (5*USEC_PER_MINUTE)
35 Session* session_new(Manager *m, User *u, const char *id) {
45 s->state_file = strappend("/run/systemd/sessions/", id);
51 s->id = file_name_from_path(s->state_file);
53 if (hashmap_put(m->sessions, s->id, s) < 0) {
63 LIST_PREPEND(Session, sessions_by_user, u->sessions, s);
68 void session_free(Session *s) {
72 LIST_REMOVE(Session, gc_queue, s->manager->session_gc_queue, s);
75 LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s);
77 if (s->user->display == s)
78 s->user->display = NULL;
82 if (s->seat->active == s)
83 s->seat->active = NULL;
85 LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s);
89 hashmap_remove(s->manager->cgroups, s->cgroup_path);
92 strv_free(s->controllers);
100 hashmap_remove(s->manager->sessions, s->id);
102 session_remove_fifo(s);
108 int session_save(Session *s) {
118 r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
122 r = fopen_temporary(s->state_file, &f, &temp_path);
128 fchmod(fileno(f), 0644);
131 "# This is private data. Do not parse.\n"
136 "KILL_PROCESSES=%i\n",
137 (unsigned long) s->user->uid,
139 session_is_active(s),
146 session_type_to_string(s->type));
188 if (s->seat && seat_is_vtconsole(s->seat))
196 (unsigned long) s->leader);
201 (unsigned long long) s->audit_id);
205 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
207 unlink(s->state_file);
216 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
221 int session_load(Session *s) {
223 *kill_processes = NULL,
234 r = parse_env_file(s->state_file, NEWLINE,
236 "KILL_PROCESSES", &kill_processes,
237 "CGROUP", &s->cgroup_path,
238 "FIFO", &s->fifo_path,
241 "DISPLAY", &s->display,
242 "REMOTE_HOST", &s->remote_host,
243 "REMOTE_USER", &s->remote_user,
244 "SERVICE", &s->service,
254 k = parse_boolean(remote);
259 if (kill_processes) {
260 k = parse_boolean(kill_processes);
262 s->kill_processes = k;
265 if (seat && !s->seat) {
268 o = hashmap_get(s->manager->seats, seat);
270 seat_attach_session(o, s);
273 if (vtnr && s->seat && seat_is_vtconsole(s->seat)) {
276 k = safe_atoi(vtnr, &v);
277 if (k >= 0 && v >= 1)
284 k = parse_pid(leader, &pid);
285 if (k >= 0 && pid >= 1) {
288 audit_session_from_pid(pid, &s->audit_id);
295 t = session_type_from_string(type);
303 /* If we open an unopened pipe for reading we will not
304 get an EOF. to trigger an EOF we hence open it for
305 reading, but close it right-away which then will
308 fd = session_create_fifo(s);
310 close_nointr_nofail(fd);
316 free(kill_processes);
325 int session_activate(Session *s) {
337 if (s->seat->active == s)
340 assert(seat_is_vtconsole(s->seat));
346 old_active = s->seat->active;
349 return seat_apply_acls(s->seat, old_active);
352 static int session_link_x11_socket(Session *s) {
358 assert(s->user->runtime_path);
360 if (s->user->display)
363 if (!s->display || !display_is_local(s->display))
366 k = strspn(s->display+1, "0123456789");
367 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
369 log_error("Out of memory");
373 c = stpcpy(f, "/tmp/.X11-unix/X");
374 memcpy(c, s->display+1, k);
377 if (access(f, F_OK) < 0) {
378 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
383 t = strappend(s->user->runtime_path, "/X11/display");
385 log_error("Out of memory");
390 mkdir_parents(t, 0755);
392 if (link(f, t) < 0) {
393 if (errno == EEXIST) {
400 if (symlink(f, t) < 0) {
402 if (errno == EEXIST) {
405 if (symlink(f, t) >= 0)
409 log_error("Failed to link %s to %s: %m", f, t);
417 log_info("Linked %s to %s.", f, t);
421 s->user->display = s;
426 static int session_create_one_group(Session *s, const char *controller, const char *path) {
434 r = cg_create_and_attach(controller, path, s->leader);
436 r = cg_create(controller, path);
438 r = cg_create(controller, path);
443 r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid);
445 r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid);
450 static int session_create_cgroup(Session *s) {
457 assert(s->user->cgroup_path);
459 if (!s->cgroup_path) {
460 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
461 log_error("Out of memory");
467 r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
469 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
471 s->cgroup_path = NULL;
477 STRV_FOREACH(k, s->controllers) {
479 if (strv_contains(s->reset_controllers, *k))
482 r = session_create_one_group(s, *k, p);
484 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
487 STRV_FOREACH(k, s->manager->controllers) {
489 if (strv_contains(s->reset_controllers, *k) ||
490 strv_contains(s->manager->reset_controllers, *k) ||
491 strv_contains(s->controllers, *k))
494 r = session_create_one_group(s, *k, p);
496 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
501 STRV_FOREACH(k, s->reset_controllers) {
502 r = cg_attach(*k, "/", s->leader);
504 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
508 STRV_FOREACH(k, s->manager->reset_controllers) {
510 if (strv_contains(s->reset_controllers, *k) ||
511 strv_contains(s->controllers, *k))
514 r = cg_attach(*k, "/", s->leader);
516 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
521 hashmap_put(s->manager->cgroups, s->cgroup_path, s);
526 int session_start(Session *s) {
535 r = user_start(s->user);
539 log_info("New session %s of user %s.", s->id, s->user->name);
542 r = session_create_cgroup(s);
546 /* Create X11 symlink */
547 session_link_x11_socket(s);
549 dual_timestamp_get(&s->timestamp);
552 seat_read_active_vt(s->seat);
556 /* Save session data */
560 session_send_signal(s, true);
565 if (s->seat->active == s)
566 seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
568 seat_send_changed(s->seat, "Sessions\0");
571 user_send_changed(s->user, "Sessions\0");
576 static bool session_shall_kill(Session *s) {
579 if (!s->kill_processes)
582 if (strv_contains(s->manager->kill_exclude_users, s->user->name))
585 if (strv_isempty(s->manager->kill_only_users))
588 return strv_contains(s->manager->kill_only_users, s->user->name);
591 static int session_terminate_cgroup(Session *s) {
600 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
602 if (session_shall_kill(s)) {
604 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
606 log_error("Failed to kill session cgroup: %s", strerror(-r));
609 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
611 log_error("Failed to check session cgroup: %s", strerror(-r));
613 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
615 log_error("Failed to delete session cgroup: %s", strerror(-r));
620 STRV_FOREACH(k, s->user->manager->controllers)
621 cg_trim(*k, s->cgroup_path, true);
623 hashmap_remove(s->manager->cgroups, s->cgroup_path);
625 free(s->cgroup_path);
626 s->cgroup_path = NULL;
631 static int session_unlink_x11_socket(Session *s) {
638 if (s->user->display != s)
641 s->user->display = NULL;
643 t = strappend(s->user->runtime_path, "/X11/display");
645 log_error("Out of memory");
652 return r < 0 ? -errno : 0;
655 int session_stop(Session *s) {
661 log_info("Removed session %s.", s->id);
664 k = session_terminate_cgroup(s);
668 /* Remove X11 symlink */
669 session_unlink_x11_socket(s);
671 unlink(s->state_file);
672 session_add_to_gc_queue(s);
673 user_add_to_gc_queue(s->user);
676 session_send_signal(s, false);
679 if (s->seat->active == s)
680 seat_set_active(s->seat, NULL);
682 seat_send_changed(s->seat, "Sessions\0");
685 user_send_changed(s->user, "Sessions\0");
692 bool session_is_active(Session *s) {
698 return s->seat->active == s;
701 int session_get_idle_hint(Session *s, dual_timestamp *t) {
712 *t = s->idle_hint_timestamp;
720 if (s->tty[0] != '/') {
721 p = strappend("/dev/", s->tty);
727 if (!startswith(p ? p : s->tty, "/dev/")) {
732 k = lstat(p ? p : s->tty, &st);
738 u = timespec_load(&st.st_atim);
739 n = now(CLOCK_REALTIME);
740 b = u + IDLE_THRESHOLD_USEC < n;
743 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
749 *t = s->idle_hint_timestamp;
754 void session_set_idle_hint(Session *s, bool b) {
757 if (s->idle_hint == b)
761 dual_timestamp_get(&s->idle_hint_timestamp);
763 session_send_changed(s,
766 "IdleSinceHintMonotonic\0");
769 seat_send_changed(s->seat,
772 "IdleSinceHintMonotonic\0");
774 user_send_changed(s->user,
777 "IdleSinceHintMonotonic\0");
779 manager_send_changed(s->manager,
782 "IdleSinceHintMonotonic\0");
785 int session_create_fifo(Session *s) {
792 r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
796 if (asprintf(&s->fifo_path, "/run/systemd/sessions/%s.ref", s->id) < 0)
799 if (mkfifo(s->fifo_path, 0600) < 0 && errno != EEXIST)
803 /* Open reading side */
804 if (s->fifo_fd < 0) {
805 struct epoll_event ev;
807 s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
811 r = hashmap_put(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1), s);
817 ev.data.u32 = FD_FIFO_BASE + s->fifo_fd;
819 if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, s->fifo_fd, &ev) < 0)
823 /* Open writing side */
824 r = open(s->fifo_path, O_WRONLY|O_CLOEXEC|O_NDELAY);
831 void session_remove_fifo(Session *s) {
834 if (s->fifo_fd >= 0) {
835 assert_se(hashmap_remove(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1)) == s);
836 assert_se(epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_DEL, s->fifo_fd, NULL) == 0);
837 close_nointr_nofail(s->fifo_fd);
842 unlink(s->fifo_path);
848 int session_check_gc(Session *s, bool drop_not_started) {
853 if (drop_not_started && !s->started)
856 if (s->fifo_fd >= 0) {
858 r = pipe_eof(s->fifo_fd);
866 if (s->cgroup_path) {
868 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
879 void session_add_to_gc_queue(Session *s) {
885 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
886 s->in_gc_queue = true;
889 int session_kill(Session *s, KillWho who, int signo) {
898 if (s->leader <= 0 && who == KILL_LEADER)
902 if (kill(s->leader, signo) < 0)
905 if (who == KILL_ALL) {
908 pid_set = set_new(trivial_hash_func, trivial_compare_func);
913 q = set_put(pid_set, LONG_TO_PTR(s->leader));
918 q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, signo, false, true, false, pid_set);
920 if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
930 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
931 [SESSION_TTY] = "tty",
932 [SESSION_X11] = "x11",
933 [SESSION_UNSPECIFIED] = "unspecified"
936 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);
938 static const char* const kill_who_table[_KILL_WHO_MAX] = {
939 [KILL_LEADER] = "leader",
943 DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);