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 strv_free(s->controllers);
95 hashmap_remove(s->manager->sessions, s->id);
98 close_nointr_nofail(s->pipe_fd);
104 int session_save(Session *s) {
111 r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
115 r = fopen_temporary(s->state_file, &f, &temp_path);
121 fchmod(fileno(f), 0644);
124 "# This is private data. Do not parse.\n"
129 "KILL_PROCESSES=%i\n",
130 (unsigned long) s->user->uid,
132 session_is_active(s),
171 if (s->seat && seat_is_vtconsole(s->seat))
179 (unsigned long) s->leader);
184 (unsigned long long) s->audit_id);
188 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
190 unlink(s->state_file);
199 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
204 int session_load(Session *s) {
206 *kill_processes = NULL,
216 r = parse_env_file(s->state_file, NEWLINE,
218 "KILL_PROCESSES", &kill_processes,
219 "CGROUP", &s->cgroup_path,
222 "DISPLAY", &s->display,
223 "REMOTE_HOST", &s->remote_host,
224 "REMOTE_USER", &s->remote_user,
225 "SERVICE", &s->service,
234 k = parse_boolean(remote);
239 if (kill_processes) {
240 k = parse_boolean(kill_processes);
242 s->kill_processes = k;
245 if (seat && !s->seat) {
248 o = hashmap_get(s->manager->seats, seat);
250 seat_attach_session(o, s);
253 if (vtnr && s->seat && seat_is_vtconsole(s->seat)) {
256 k = safe_atoi(vtnr, &v);
257 if (k >= 0 && v >= 1)
264 k = parse_pid(leader, &pid);
265 if (k >= 0 && pid >= 1) {
268 audit_session_from_pid(pid, &s->audit_id);
274 free(kill_processes);
283 int session_activate(Session *s) {
295 if (s->seat->active == s)
298 assert(seat_is_vtconsole(s->seat));
304 old_active = s->seat->active;
307 return seat_apply_acls(s->seat, old_active);
310 bool x11_display_is_local(const char *display) {
319 static int session_link_x11_socket(Session *s) {
325 assert(s->user->runtime_path);
327 if (s->user->display)
330 if (!s->display || !x11_display_is_local(s->display))
333 k = strspn(s->display+1, "0123456789");
334 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
336 log_error("Out of memory");
340 c = stpcpy(f, "/tmp/.X11-unix/X");
341 memcpy(c, s->display+1, k);
344 if (access(f, F_OK) < 0) {
345 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
350 t = strappend(s->user->runtime_path, "/display");
352 log_error("Out of memory");
357 if (link(f, t) < 0) {
358 if (errno == EEXIST) {
365 if (symlink(f, t) < 0) {
367 if (errno == EEXIST) {
370 if (symlink(f, t) >= 0)
374 log_error("Failed to link %s to %s: %m", f, t);
382 log_info("Linked %s to %s.", f, t);
386 s->user->display = s;
391 static int session_create_one_group(Session *s, const char *controller, const char *path) {
399 r = cg_create_and_attach(controller, path, s->leader);
401 r = cg_create(controller, path);
406 r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid);
408 r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid);
413 static int session_create_cgroup(Session *s) {
420 assert(s->user->cgroup_path);
422 if (!s->cgroup_path) {
423 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
424 log_error("Out of memory");
430 r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
433 s->cgroup_path = NULL;
434 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
440 STRV_FOREACH(k, s->controllers) {
442 if (strv_contains(s->reset_controllers, *k))
445 r = session_create_one_group(s, *k, p);
447 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
450 STRV_FOREACH(k, s->manager->controllers) {
452 if (strv_contains(s->reset_controllers, *k) ||
453 strv_contains(s->controllers, *k))
456 r = session_create_one_group(s, *k, p);
458 log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
463 STRV_FOREACH(k, s->reset_controllers) {
464 r = cg_attach(*k, "/", s->leader);
466 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
474 int session_start(Session *s) {
483 r = user_start(s->user);
487 log_info("New session %s of user %s.", s->id, s->user->name);
490 r = session_create_cgroup(s);
494 /* Create X11 symlink */
495 session_link_x11_socket(s);
497 /* Save session data */
500 dual_timestamp_get(&s->timestamp);
504 session_send_signal(s, true);
507 if (s->seat->active == s)
508 seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
510 seat_send_changed(s->seat, "Sessions\0");
513 user_send_changed(s->user, "Sessions\0");
518 static bool session_shall_kill(Session *s) {
521 if (!s->kill_processes)
524 if (strv_contains(s->manager->kill_exclude_users, s->user->name))
527 if (strv_isempty(s->manager->kill_only_users))
530 return strv_contains(s->manager->kill_only_users, s->user->name);
533 static int session_kill_cgroup(Session *s) {
542 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
544 if (session_shall_kill(s)) {
546 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
548 log_error("Failed to kill session cgroup: %s", strerror(-r));
551 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
553 log_error("Failed to check session cgroup: %s", strerror(-r));
555 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
557 log_error("Failed to delete session cgroup: %s", strerror(-r));
562 STRV_FOREACH(k, s->user->manager->controllers)
563 cg_trim(*k, s->cgroup_path, true);
565 free(s->cgroup_path);
566 s->cgroup_path = NULL;
571 static int session_unlink_x11_socket(Session *s) {
578 if (s->user->display != s)
581 s->user->display = NULL;
583 t = strappend(s->user->runtime_path, "/display");
585 log_error("Out of memory");
592 return r < 0 ? -errno : 0;
595 int session_stop(Session *s) {
601 log_info("Removed session %s.", s->id);
604 k = session_kill_cgroup(s);
608 /* Remove X11 symlink */
609 session_unlink_x11_socket(s);
611 unlink(s->state_file);
612 session_add_to_gc_queue(s);
613 user_add_to_gc_queue(s->user);
616 session_send_signal(s, false);
619 if (s->seat->active == s)
620 seat_set_active(s->seat, NULL);
622 seat_send_changed(s->seat, "Sessions\0");
625 user_send_changed(s->user, "Sessions\0");
632 bool session_is_active(Session *s) {
638 return s->seat->active == s;
641 int session_get_idle_hint(Session *s, dual_timestamp *t) {
652 *t = s->idle_hint_timestamp;
660 if (s->tty[0] != '/') {
661 p = strappend("/dev/", s->tty);
667 if (!startswith(p ? p : s->tty, "/dev/")) {
672 k = lstat(p ? p : s->tty, &st);
678 u = timespec_load(&st.st_atim);
679 n = now(CLOCK_REALTIME);
680 b = u + IDLE_THRESHOLD_USEC < n;
683 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
689 *t = s->idle_hint_timestamp;
694 void session_set_idle_hint(Session *s, bool b) {
697 if (s->idle_hint == b)
701 dual_timestamp_get(&s->idle_hint_timestamp);
703 session_send_changed(s,
706 "IdleSinceHintMonotonic\0");
709 seat_send_changed(s->seat,
712 "IdleSinceHintMonotonic\0");
714 user_send_changed(s->user,
717 "IdleSinceHintMonotonic\0");
719 manager_send_changed(s->manager,
722 "IdleSinceHintMonotonic\0");
725 int session_check_gc(Session *s) {
730 if (s->pipe_fd >= 0) {
732 r = pipe_eof(s->pipe_fd);
740 if (s->cgroup_path) {
742 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
753 void session_add_to_gc_queue(Session *s) {
759 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
760 s->in_gc_queue = true;
763 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
764 [SESSION_TTY] = "tty",
765 [SESSION_X11] = "x11",
766 [SESSION_OTHER] = "other"
769 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);