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) {
114 r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
118 r = fopen_temporary(s->state_file, &f, &temp_path);
124 fchmod(fileno(f), 0644);
127 "# This is private data. Do not parse.\n"
132 "KILL_PROCESSES=%i\n",
133 (unsigned long) s->user->uid,
135 session_is_active(s),
142 session_type_to_string(s->type));
179 if (s->seat && seat_is_vtconsole(s->seat))
187 (unsigned long) s->leader);
192 (unsigned long long) s->audit_id);
196 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
198 unlink(s->state_file);
207 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
212 int session_load(Session *s) {
214 *kill_processes = NULL,
225 r = parse_env_file(s->state_file, NEWLINE,
227 "KILL_PROCESSES", &kill_processes,
228 "CGROUP", &s->cgroup_path,
231 "DISPLAY", &s->display,
232 "REMOTE_HOST", &s->remote_host,
233 "REMOTE_USER", &s->remote_user,
234 "SERVICE", &s->service,
244 k = parse_boolean(remote);
249 if (kill_processes) {
250 k = parse_boolean(kill_processes);
252 s->kill_processes = k;
255 if (seat && !s->seat) {
258 o = hashmap_get(s->manager->seats, seat);
260 seat_attach_session(o, s);
263 if (vtnr && s->seat && seat_is_vtconsole(s->seat)) {
266 k = safe_atoi(vtnr, &v);
267 if (k >= 0 && v >= 1)
274 k = parse_pid(leader, &pid);
275 if (k >= 0 && pid >= 1) {
278 audit_session_from_pid(pid, &s->audit_id);
285 t = session_type_from_string(type);
292 free(kill_processes);
301 int session_activate(Session *s) {
313 if (s->seat->active == s)
316 assert(seat_is_vtconsole(s->seat));
322 old_active = s->seat->active;
325 return seat_apply_acls(s->seat, old_active);
329 static int session_link_x11_socket(Session *s) {
335 assert(s->user->runtime_path);
337 if (s->user->display)
340 if (!s->display || !display_is_local(s->display))
343 k = strspn(s->display+1, "0123456789");
344 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
346 log_error("Out of memory");
350 c = stpcpy(f, "/tmp/.X11-unix/X");
351 memcpy(c, s->display+1, k);
354 if (access(f, F_OK) < 0) {
355 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
360 t = strappend(s->user->runtime_path, "/display");
362 log_error("Out of memory");
367 if (link(f, t) < 0) {
368 if (errno == EEXIST) {
375 if (symlink(f, t) < 0) {
377 if (errno == EEXIST) {
380 if (symlink(f, t) >= 0)
384 log_error("Failed to link %s to %s: %m", f, t);
392 log_info("Linked %s to %s.", f, t);
396 s->user->display = s;
401 static int session_create_one_group(Session *s, const char *controller, const char *path) {
409 r = cg_create_and_attach(controller, path, s->leader);
411 r = cg_create(controller, path);
416 r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid);
418 r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid);
423 static int session_create_cgroup(Session *s) {
430 assert(s->user->cgroup_path);
432 if (!s->cgroup_path) {
433 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
434 log_error("Out of memory");
440 r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
442 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
444 s->cgroup_path = NULL;
450 STRV_FOREACH(k, s->controllers) {
452 if (strv_contains(s->reset_controllers, *k))
455 r = session_create_one_group(s, *k, p);
457 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
460 STRV_FOREACH(k, s->manager->controllers) {
462 if (strv_contains(s->reset_controllers, *k) ||
463 strv_contains(s->controllers, *k))
466 r = session_create_one_group(s, *k, p);
468 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
473 STRV_FOREACH(k, s->reset_controllers) {
474 r = cg_attach(*k, "/", s->leader);
476 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
481 hashmap_put(s->manager->cgroups, s->cgroup_path, s);
486 int session_start(Session *s) {
495 r = user_start(s->user);
499 log_info("New session %s of user %s.", s->id, s->user->name);
502 r = session_create_cgroup(s);
506 /* Create X11 symlink */
507 session_link_x11_socket(s);
509 /* Save session data */
512 dual_timestamp_get(&s->timestamp);
516 session_send_signal(s, true);
519 if (s->seat->active == s)
520 seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
522 seat_send_changed(s->seat, "Sessions\0");
525 user_send_changed(s->user, "Sessions\0");
530 static bool session_shall_kill(Session *s) {
533 if (!s->kill_processes)
536 if (strv_contains(s->manager->kill_exclude_users, s->user->name))
539 if (strv_isempty(s->manager->kill_only_users))
542 return strv_contains(s->manager->kill_only_users, s->user->name);
545 static int session_kill_cgroup(Session *s) {
554 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
556 if (session_shall_kill(s)) {
558 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
560 log_error("Failed to kill session cgroup: %s", strerror(-r));
563 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
565 log_error("Failed to check session cgroup: %s", strerror(-r));
567 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
569 log_error("Failed to delete session cgroup: %s", strerror(-r));
574 STRV_FOREACH(k, s->user->manager->controllers)
575 cg_trim(*k, s->cgroup_path, true);
577 hashmap_remove(s->manager->cgroups, s->cgroup_path);
579 free(s->cgroup_path);
580 s->cgroup_path = NULL;
585 static int session_unlink_x11_socket(Session *s) {
592 if (s->user->display != s)
595 s->user->display = NULL;
597 t = strappend(s->user->runtime_path, "/display");
599 log_error("Out of memory");
606 return r < 0 ? -errno : 0;
609 int session_stop(Session *s) {
615 log_info("Removed session %s.", s->id);
618 k = session_kill_cgroup(s);
622 /* Remove X11 symlink */
623 session_unlink_x11_socket(s);
625 unlink(s->state_file);
626 session_add_to_gc_queue(s);
627 user_add_to_gc_queue(s->user);
630 session_send_signal(s, false);
633 if (s->seat->active == s)
634 seat_set_active(s->seat, NULL);
636 seat_send_changed(s->seat, "Sessions\0");
639 user_send_changed(s->user, "Sessions\0");
646 bool session_is_active(Session *s) {
652 return s->seat->active == s;
655 int session_get_idle_hint(Session *s, dual_timestamp *t) {
666 *t = s->idle_hint_timestamp;
674 if (s->tty[0] != '/') {
675 p = strappend("/dev/", s->tty);
681 if (!startswith(p ? p : s->tty, "/dev/")) {
686 k = lstat(p ? p : s->tty, &st);
692 u = timespec_load(&st.st_atim);
693 n = now(CLOCK_REALTIME);
694 b = u + IDLE_THRESHOLD_USEC < n;
697 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
703 *t = s->idle_hint_timestamp;
708 void session_set_idle_hint(Session *s, bool b) {
711 if (s->idle_hint == b)
715 dual_timestamp_get(&s->idle_hint_timestamp);
717 session_send_changed(s,
720 "IdleSinceHintMonotonic\0");
723 seat_send_changed(s->seat,
726 "IdleSinceHintMonotonic\0");
728 user_send_changed(s->user,
731 "IdleSinceHintMonotonic\0");
733 manager_send_changed(s->manager,
736 "IdleSinceHintMonotonic\0");
739 int session_set_pipe_fd(Session *s, int fd) {
740 struct epoll_event ev;
745 assert(s->pipe_fd < 0);
747 r = hashmap_put(s->manager->pipe_fds, INT_TO_PTR(fd + 1), s);
753 ev.data.u32 = FD_PIPE_BASE + fd;
755 if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
756 assert_se(hashmap_remove(s->manager->pipe_fds, INT_TO_PTR(fd + 1)) == s);
764 void session_unset_pipe_fd(Session *s) {
770 assert_se(hashmap_remove(s->manager->pipe_fds, INT_TO_PTR(s->pipe_fd + 1)) == s);
772 assert_se(epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_DEL, s->pipe_fd, NULL) == 0);
774 close_nointr_nofail(s->pipe_fd);
778 int session_check_gc(Session *s) {
783 if (s->pipe_fd >= 0) {
785 r = pipe_eof(s->pipe_fd);
793 if (s->cgroup_path) {
795 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
806 void session_add_to_gc_queue(Session *s) {
812 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
813 s->in_gc_queue = true;
816 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
817 [SESSION_TTY] = "tty",
818 [SESSION_X11] = "x11",
819 [SESSION_UNSPECIFIED] = "unspecified"
822 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);