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/session/", 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);
94 hashmap_remove(s->manager->sessions, s->id);
100 int session_save(Session *s) {
107 r = safe_mkdir("/run/systemd/session", 0755, 0, 0);
111 r = fopen_temporary(s->state_file, &f, &temp_path);
117 fchmod(fileno(f), 0644);
120 "# This is private data. Do not parse.\n"
125 "KILL_PROCESSES=%i\n",
126 (unsigned long) s->user->uid,
128 session_is_active(s),
162 if (s->seat && seat_is_vtconsole(s->seat))
170 (unsigned long) s->leader);
175 (unsigned long long) s->audit_id);
179 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
181 unlink(s->state_file);
190 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
195 int session_load(Session *s) {
197 *kill_processes = NULL,
207 r = parse_env_file(s->state_file, NEWLINE,
209 "KILL_PROCESSES", &kill_processes,
210 "CGROUP", &s->cgroup_path,
213 "DISPLAY", &s->display,
214 "REMOTE_HOST", &s->remote_host,
215 "REMOTE_USER", &s->remote_user,
218 "AUDIT_ID", &audit_id,
225 k = parse_boolean(remote);
230 if (kill_processes) {
231 k = parse_boolean(kill_processes);
233 s->kill_processes = k;
236 if (seat && !s->seat) {
239 o = hashmap_get(s->manager->seats, seat);
241 seat_attach_session(o, s);
244 if (vtnr && s->seat && seat_is_vtconsole(s->seat)) {
247 k = safe_atoi(vtnr, &v);
248 if (k >= 0 && v >= 1)
255 k = parse_pid(leader, &pid);
256 if (k >= 0 && pid >= 1)
263 k = safe_atou32(audit_id, &l);
264 if (k >= 0 && l >= l)
270 free(kill_processes);
279 int session_activate(Session *s) {
291 if (s->seat->active == s)
294 assert(seat_is_vtconsole(s->seat));
300 old_active = s->seat->active;
303 return seat_apply_acls(s->seat, old_active);
306 bool x11_display_is_local(const char *display) {
315 static int session_link_x11_socket(Session *s) {
321 assert(s->user->runtime_path);
323 if (s->user->display)
326 if (!s->display || !x11_display_is_local(s->display))
329 k = strspn(s->display+1, "0123456789");
330 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
332 log_error("Out of memory");
336 c = stpcpy(f, "/tmp/.X11-unix/X");
337 memcpy(c, s->display+1, k);
340 if (access(f, F_OK) < 0) {
341 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
346 t = strappend(s->user->runtime_path, "/display");
348 log_error("Out of memory");
353 if (link(f, t) < 0) {
354 if (errno == EEXIST) {
361 if (symlink(f, t) < 0) {
363 if (errno == EEXIST) {
366 if (symlink(f, t) >= 0)
370 log_error("Failed to link %s to %s: %m", f, t);
378 log_info("Linked %s to %s.", f, t);
382 s->user->display = s;
387 static int session_create_cgroup(Session *s) {
394 assert(s->user->cgroup_path);
396 if (!s->cgroup_path) {
397 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
398 log_error("Out of memory");
405 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, p, s->leader);
407 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
411 s->cgroup_path = NULL;
412 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
418 STRV_FOREACH(k, s->manager->controllers) {
420 r = cg_create_and_attach(*k, p, s->leader);
422 r = cg_create(*k, p);
425 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
431 int session_start(Session *s) {
441 r = session_create_cgroup(s);
445 /* Create X11 symlink */
446 session_link_x11_socket(s);
448 /* Save session data */
451 dual_timestamp_get(&s->timestamp);
455 session_send_signal(s, true);
458 if (s->seat->active == s)
459 seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
461 seat_send_changed(s->seat, "Sessions\0");
464 user_send_changed(s->user, "Sessions\0");
469 static bool session_shall_kill(Session *s) {
472 return s->kill_processes;
475 static int session_kill_cgroup(Session *s) {
484 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
486 if (session_shall_kill(s)) {
488 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
490 log_error("Failed to kill session cgroup: %s", strerror(-r));
493 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
495 log_error("Failed to check session cgroup: %s", strerror(-r));
497 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
499 log_error("Failed to delete session cgroup: %s", strerror(-r));
504 STRV_FOREACH(k, s->user->manager->controllers)
505 cg_trim(*k, s->cgroup_path, true);
507 free(s->cgroup_path);
508 s->cgroup_path = NULL;
513 static int session_unlink_x11_socket(Session *s) {
520 if (s->user->display != s)
523 s->user->display = NULL;
525 t = strappend(s->user->runtime_path, "/display");
527 log_error("Out of memory");
534 return r < 0 ? -errno : 0;
537 int session_stop(Session *s) {
546 k = session_kill_cgroup(s);
550 /* Remove X11 symlink */
551 session_unlink_x11_socket(s);
553 unlink(s->state_file);
554 session_add_to_gc_queue(s);
556 session_send_signal(s, false);
559 if (s->seat->active == s)
560 seat_set_active(s->seat, NULL);
562 seat_send_changed(s->seat, "Sessions\0");
565 user_send_changed(s->user, "Sessions\0");
572 bool session_is_active(Session *s) {
578 return s->seat->active == s;
581 int session_get_idle_hint(Session *s, dual_timestamp *t) {
592 *t = s->idle_hint_timestamp;
600 if (s->tty[0] != '/') {
601 p = strappend("/dev/", s->tty);
607 if (!startswith(p ? p : s->tty, "/dev/")) {
612 k = lstat(p ? p : s->tty, &st);
618 u = timespec_load(&st.st_atim);
619 n = now(CLOCK_REALTIME);
620 b = u + IDLE_THRESHOLD_USEC < n;
623 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
629 *t = s->idle_hint_timestamp;
634 void session_set_idle_hint(Session *s, bool b) {
637 if (s->idle_hint == b)
641 dual_timestamp_get(&s->idle_hint_timestamp);
643 session_send_changed(s,
646 "IdleSinceHintMonotonic\0");
649 seat_send_changed(s->seat,
652 "IdleSinceHintMonotonic\0");
654 user_send_changed(s->user,
657 "IdleSinceHintMonotonic\0");
659 manager_send_changed(s->manager,
662 "IdleSinceHintMonotonic\0");
665 int session_check_gc(Session *s) {
670 if (s->pipe_fd >= 0) {
672 r = pipe_eof(s->pipe_fd);
680 if (s->cgroup_path) {
682 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
693 void session_add_to_gc_queue(Session *s) {
699 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
700 s->in_gc_queue = true;
703 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
704 [SESSION_TTY] = "tty",
705 [SESSION_X11] = "x11"
708 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);