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/>.
26 #include "logind-session.h"
29 #include "cgroup-util.h"
31 #define IDLE_THRESHOLD_USEC (5*USEC_PER_MINUTE)
33 Session* session_new(Manager *m, User *u, const char *id) {
43 s->state_file = strappend("/run/systemd/sessions/", id);
49 s->id = file_name_from_path(s->state_file);
51 if (hashmap_put(m->sessions, s->id, s) < 0) {
61 LIST_PREPEND(Session, sessions_by_user, u->sessions, s);
66 void session_free(Session *s) {
70 LIST_REMOVE(Session, gc_queue, s->manager->session_gc_queue, s);
73 LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s);
75 if (s->user->display == s)
76 s->user->display = NULL;
80 if (s->seat->active == s)
81 s->seat->active = NULL;
83 LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s);
87 hashmap_remove(s->manager->cgroups, s->cgroup_path);
90 strv_free(s->controllers);
98 hashmap_remove(s->manager->sessions, s->id);
101 close_nointr_nofail(s->pipe_fd);
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),
174 if (s->seat && seat_is_vtconsole(s->seat))
182 (unsigned long) s->leader);
187 (unsigned long long) s->audit_id);
191 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
193 unlink(s->state_file);
202 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
207 int session_load(Session *s) {
209 *kill_processes = NULL,
219 r = parse_env_file(s->state_file, NEWLINE,
221 "KILL_PROCESSES", &kill_processes,
222 "CGROUP", &s->cgroup_path,
225 "DISPLAY", &s->display,
226 "REMOTE_HOST", &s->remote_host,
227 "REMOTE_USER", &s->remote_user,
228 "SERVICE", &s->service,
237 k = parse_boolean(remote);
242 if (kill_processes) {
243 k = parse_boolean(kill_processes);
245 s->kill_processes = k;
248 if (seat && !s->seat) {
251 o = hashmap_get(s->manager->seats, seat);
253 seat_attach_session(o, s);
256 if (vtnr && s->seat && seat_is_vtconsole(s->seat)) {
259 k = safe_atoi(vtnr, &v);
260 if (k >= 0 && v >= 1)
267 k = parse_pid(leader, &pid);
268 if (k >= 0 && pid >= 1) {
271 audit_session_from_pid(pid, &s->audit_id);
277 free(kill_processes);
286 int session_activate(Session *s) {
298 if (s->seat->active == s)
301 assert(seat_is_vtconsole(s->seat));
307 old_active = s->seat->active;
310 return seat_apply_acls(s->seat, old_active);
313 bool x11_display_is_local(const char *display) {
322 static int session_link_x11_socket(Session *s) {
328 assert(s->user->runtime_path);
330 if (s->user->display)
333 if (!s->display || !x11_display_is_local(s->display))
336 k = strspn(s->display+1, "0123456789");
337 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
339 log_error("Out of memory");
343 c = stpcpy(f, "/tmp/.X11-unix/X");
344 memcpy(c, s->display+1, k);
347 if (access(f, F_OK) < 0) {
348 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
353 t = strappend(s->user->runtime_path, "/display");
355 log_error("Out of memory");
360 if (link(f, t) < 0) {
361 if (errno == EEXIST) {
368 if (symlink(f, t) < 0) {
370 if (errno == EEXIST) {
373 if (symlink(f, t) >= 0)
377 log_error("Failed to link %s to %s: %m", f, t);
385 log_info("Linked %s to %s.", f, t);
389 s->user->display = s;
394 static int session_create_one_group(Session *s, const char *controller, const char *path) {
402 r = cg_create_and_attach(controller, path, s->leader);
404 r = cg_create(controller, path);
409 r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid);
411 r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid);
416 static int session_create_cgroup(Session *s) {
423 assert(s->user->cgroup_path);
425 if (!s->cgroup_path) {
426 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
427 log_error("Out of memory");
433 r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
436 s->cgroup_path = NULL;
437 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
443 STRV_FOREACH(k, s->controllers) {
445 if (strv_contains(s->reset_controllers, *k))
448 r = session_create_one_group(s, *k, p);
450 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
453 STRV_FOREACH(k, s->manager->controllers) {
455 if (strv_contains(s->reset_controllers, *k) ||
456 strv_contains(s->controllers, *k))
459 r = session_create_one_group(s, *k, p);
461 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
466 STRV_FOREACH(k, s->reset_controllers) {
467 r = cg_attach(*k, "/", s->leader);
469 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
474 hashmap_put(s->manager->cgroups, s->cgroup_path, s);
479 int session_start(Session *s) {
488 r = user_start(s->user);
492 log_info("New session %s of user %s.", s->id, s->user->name);
495 r = session_create_cgroup(s);
499 /* Create X11 symlink */
500 session_link_x11_socket(s);
502 /* Save session data */
505 dual_timestamp_get(&s->timestamp);
509 session_send_signal(s, true);
512 if (s->seat->active == s)
513 seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
515 seat_send_changed(s->seat, "Sessions\0");
518 user_send_changed(s->user, "Sessions\0");
523 static bool session_shall_kill(Session *s) {
526 if (!s->kill_processes)
529 if (strv_contains(s->manager->kill_exclude_users, s->user->name))
532 if (strv_isempty(s->manager->kill_only_users))
535 return strv_contains(s->manager->kill_only_users, s->user->name);
538 static int session_kill_cgroup(Session *s) {
547 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
549 if (session_shall_kill(s)) {
551 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
553 log_error("Failed to kill session cgroup: %s", strerror(-r));
556 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
558 log_error("Failed to check session cgroup: %s", strerror(-r));
560 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
562 log_error("Failed to delete session cgroup: %s", strerror(-r));
567 STRV_FOREACH(k, s->user->manager->controllers)
568 cg_trim(*k, s->cgroup_path, true);
570 hashmap_remove(s->manager->cgroups, s->cgroup_path);
572 free(s->cgroup_path);
573 s->cgroup_path = NULL;
578 static int session_unlink_x11_socket(Session *s) {
585 if (s->user->display != s)
588 s->user->display = NULL;
590 t = strappend(s->user->runtime_path, "/display");
592 log_error("Out of memory");
599 return r < 0 ? -errno : 0;
602 int session_stop(Session *s) {
608 log_info("Removed session %s.", s->id);
611 k = session_kill_cgroup(s);
615 /* Remove X11 symlink */
616 session_unlink_x11_socket(s);
618 unlink(s->state_file);
619 session_add_to_gc_queue(s);
620 user_add_to_gc_queue(s->user);
623 session_send_signal(s, false);
626 if (s->seat->active == s)
627 seat_set_active(s->seat, NULL);
629 seat_send_changed(s->seat, "Sessions\0");
632 user_send_changed(s->user, "Sessions\0");
639 bool session_is_active(Session *s) {
645 return s->seat->active == s;
648 int session_get_idle_hint(Session *s, dual_timestamp *t) {
659 *t = s->idle_hint_timestamp;
667 if (s->tty[0] != '/') {
668 p = strappend("/dev/", s->tty);
674 if (!startswith(p ? p : s->tty, "/dev/")) {
679 k = lstat(p ? p : s->tty, &st);
685 u = timespec_load(&st.st_atim);
686 n = now(CLOCK_REALTIME);
687 b = u + IDLE_THRESHOLD_USEC < n;
690 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
696 *t = s->idle_hint_timestamp;
701 void session_set_idle_hint(Session *s, bool b) {
704 if (s->idle_hint == b)
708 dual_timestamp_get(&s->idle_hint_timestamp);
710 session_send_changed(s,
713 "IdleSinceHintMonotonic\0");
716 seat_send_changed(s->seat,
719 "IdleSinceHintMonotonic\0");
721 user_send_changed(s->user,
724 "IdleSinceHintMonotonic\0");
726 manager_send_changed(s->manager,
729 "IdleSinceHintMonotonic\0");
732 int session_check_gc(Session *s) {
737 if (s->pipe_fd >= 0) {
739 r = pipe_eof(s->pipe_fd);
747 if (s->cgroup_path) {
749 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
760 void session_add_to_gc_queue(Session *s) {
766 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
767 s->in_gc_queue = true;
770 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
771 [SESSION_TTY] = "tty",
772 [SESSION_X11] = "x11",
773 [SESSION_OTHER] = "other"
776 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);