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);
332 static int session_link_x11_socket(Session *s) {
338 assert(s->user->runtime_path);
340 if (s->user->display)
343 if (!s->display || !display_is_local(s->display))
346 k = strspn(s->display+1, "0123456789");
347 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
349 log_error("Out of memory");
353 c = stpcpy(f, "/tmp/.X11-unix/X");
354 memcpy(c, s->display+1, k);
357 if (access(f, F_OK) < 0) {
358 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
363 t = strappend(s->user->runtime_path, "/display");
365 log_error("Out of memory");
370 if (link(f, t) < 0) {
371 if (errno == EEXIST) {
378 if (symlink(f, t) < 0) {
380 if (errno == EEXIST) {
383 if (symlink(f, t) >= 0)
387 log_error("Failed to link %s to %s: %m", f, t);
395 log_info("Linked %s to %s.", f, t);
399 s->user->display = s;
404 static int session_create_one_group(Session *s, const char *controller, const char *path) {
412 r = cg_create_and_attach(controller, path, s->leader);
414 r = cg_create(controller, path);
416 r = cg_create(controller, path);
421 r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid);
423 r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid);
428 static int session_create_cgroup(Session *s) {
435 assert(s->user->cgroup_path);
437 if (!s->cgroup_path) {
438 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
439 log_error("Out of memory");
445 r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
447 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
449 s->cgroup_path = NULL;
455 STRV_FOREACH(k, s->controllers) {
457 if (strv_contains(s->reset_controllers, *k))
460 r = session_create_one_group(s, *k, p);
462 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
465 STRV_FOREACH(k, s->manager->controllers) {
467 if (strv_contains(s->reset_controllers, *k) ||
468 strv_contains(s->controllers, *k))
471 r = session_create_one_group(s, *k, p);
473 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
478 STRV_FOREACH(k, s->reset_controllers) {
479 r = cg_attach(*k, "/", s->leader);
481 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
486 hashmap_put(s->manager->cgroups, s->cgroup_path, s);
491 int session_start(Session *s) {
500 r = user_start(s->user);
504 log_info("New session %s of user %s.", s->id, s->user->name);
507 r = session_create_cgroup(s);
511 /* Create X11 symlink */
512 session_link_x11_socket(s);
514 /* Save session data */
517 dual_timestamp_get(&s->timestamp);
521 session_send_signal(s, true);
524 if (s->seat->active == s)
525 seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
527 seat_send_changed(s->seat, "Sessions\0");
530 user_send_changed(s->user, "Sessions\0");
535 static bool session_shall_kill(Session *s) {
538 if (!s->kill_processes)
541 if (strv_contains(s->manager->kill_exclude_users, s->user->name))
544 if (strv_isempty(s->manager->kill_only_users))
547 return strv_contains(s->manager->kill_only_users, s->user->name);
550 static int session_kill_cgroup(Session *s) {
559 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
561 if (session_shall_kill(s)) {
563 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
565 log_error("Failed to kill session cgroup: %s", strerror(-r));
568 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
570 log_error("Failed to check session cgroup: %s", strerror(-r));
572 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
574 log_error("Failed to delete session cgroup: %s", strerror(-r));
579 STRV_FOREACH(k, s->user->manager->controllers)
580 cg_trim(*k, s->cgroup_path, true);
582 hashmap_remove(s->manager->cgroups, s->cgroup_path);
584 free(s->cgroup_path);
585 s->cgroup_path = NULL;
590 static int session_unlink_x11_socket(Session *s) {
597 if (s->user->display != s)
600 s->user->display = NULL;
602 t = strappend(s->user->runtime_path, "/display");
604 log_error("Out of memory");
611 return r < 0 ? -errno : 0;
614 int session_stop(Session *s) {
620 log_info("Removed session %s.", s->id);
623 k = session_kill_cgroup(s);
627 /* Remove X11 symlink */
628 session_unlink_x11_socket(s);
630 unlink(s->state_file);
631 session_add_to_gc_queue(s);
632 user_add_to_gc_queue(s->user);
635 session_send_signal(s, false);
638 if (s->seat->active == s)
639 seat_set_active(s->seat, NULL);
641 seat_send_changed(s->seat, "Sessions\0");
644 user_send_changed(s->user, "Sessions\0");
651 bool session_is_active(Session *s) {
657 return s->seat->active == s;
660 int session_get_idle_hint(Session *s, dual_timestamp *t) {
671 *t = s->idle_hint_timestamp;
679 if (s->tty[0] != '/') {
680 p = strappend("/dev/", s->tty);
686 if (!startswith(p ? p : s->tty, "/dev/")) {
691 k = lstat(p ? p : s->tty, &st);
697 u = timespec_load(&st.st_atim);
698 n = now(CLOCK_REALTIME);
699 b = u + IDLE_THRESHOLD_USEC < n;
702 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
708 *t = s->idle_hint_timestamp;
713 void session_set_idle_hint(Session *s, bool b) {
716 if (s->idle_hint == b)
720 dual_timestamp_get(&s->idle_hint_timestamp);
722 session_send_changed(s,
725 "IdleSinceHintMonotonic\0");
728 seat_send_changed(s->seat,
731 "IdleSinceHintMonotonic\0");
733 user_send_changed(s->user,
736 "IdleSinceHintMonotonic\0");
738 manager_send_changed(s->manager,
741 "IdleSinceHintMonotonic\0");
744 int session_set_pipe_fd(Session *s, int fd) {
745 struct epoll_event ev;
750 assert(s->pipe_fd < 0);
752 r = hashmap_put(s->manager->pipe_fds, INT_TO_PTR(fd + 1), s);
758 ev.data.u32 = FD_PIPE_BASE + fd;
760 if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
761 assert_se(hashmap_remove(s->manager->pipe_fds, INT_TO_PTR(fd + 1)) == s);
769 void session_unset_pipe_fd(Session *s) {
775 assert_se(hashmap_remove(s->manager->pipe_fds, INT_TO_PTR(s->pipe_fd + 1)) == s);
777 assert_se(epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_DEL, s->pipe_fd, NULL) == 0);
779 close_nointr_nofail(s->pipe_fd);
783 int session_check_gc(Session *s) {
788 if (s->pipe_fd >= 0) {
790 r = pipe_eof(s->pipe_fd);
798 if (s->cgroup_path) {
800 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
811 void session_add_to_gc_queue(Session *s) {
817 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
818 s->in_gc_queue = true;
821 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
822 [SESSION_TTY] = "tty",
823 [SESSION_X11] = "x11",
824 [SESSION_UNSPECIFIED] = "unspecified"
827 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);