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 LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s);
83 strv_free(s->controllers);
90 hashmap_remove(s->manager->sessions, s->id);
96 int session_save(Session *s) {
103 r = safe_mkdir("/run/systemd/session", 0755, 0, 0);
107 r = fopen_temporary(s->state_file, &f, &temp_path);
113 fchmod(fileno(f), 0644);
116 "# This is private data. Do not parse.\n"
121 "KILL_PROCESSES=%i\n",
122 (unsigned long) s->user->uid,
124 session_is_active(s),
158 if (s->seat && seat_is_vtconsole(s->seat))
166 (unsigned long) s->leader);
171 (unsigned long long) s->audit_id);
175 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
177 unlink(s->state_file);
186 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
191 int session_load(Session *s) {
193 *kill_processes = NULL,
203 r = parse_env_file(s->state_file, NEWLINE,
205 "KILL_PROCESSES", &kill_processes,
206 "CGROUP", &s->cgroup_path,
209 "DISPLAY", &s->display,
210 "REMOTE_HOST", &s->remote_host,
211 "REMOTE_USER", &s->remote_user,
214 "AUDIT_ID", &audit_id,
221 k = parse_boolean(remote);
226 if (kill_processes) {
227 k = parse_boolean(kill_processes);
229 s->kill_processes = k;
235 o = hashmap_get(s->manager->seats, seat);
237 seat_attach_session(o, s);
240 if (vtnr && s->seat && seat_is_vtconsole(s->seat)) {
243 k = safe_atoi(vtnr, &v);
244 if (k >= 0 && v >= 1)
251 k = parse_pid(leader, &pid);
252 if (k >= 0 && pid >= 1)
259 k = safe_atou32(audit_id, &l);
260 if (k >= 0 && l >= l)
266 free(kill_processes);
275 int session_activate(Session *s) {
287 if (s->seat->active == s)
290 assert(seat_is_vtconsole(s->seat));
296 old_active = s->seat->active;
299 return seat_apply_acls(s->seat, old_active);
302 bool x11_display_is_local(const char *display) {
311 static int session_link_x11_socket(Session *s) {
317 assert(s->user->runtime_path);
319 if (s->user->display)
322 if (!s->display || !x11_display_is_local(s->display))
325 k = strspn(s->display+1, "0123456789");
326 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
328 log_error("Out of memory");
332 c = stpcpy(f, "/tmp/.X11-unix/X");
333 memcpy(c, s->display+1, k);
336 if (access(f, F_OK) < 0) {
337 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
342 t = strappend(s->user->runtime_path, "/display");
344 log_error("Out of memory");
349 if (link(f, t) < 0) {
350 if (errno == EEXIST) {
357 if (symlink(f, t) < 0) {
359 if (errno == EEXIST) {
362 if (symlink(f, t) >= 0)
366 log_error("Failed to link %s to %s: %m", f, t);
374 log_info("Linked %s to %s.", f, t);
378 s->user->display = s;
383 static int session_create_cgroup(Session *s) {
390 assert(s->user->cgroup_path);
392 if (!s->cgroup_path) {
393 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
394 log_error("Out of memory");
401 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, p, s->leader);
403 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
407 s->cgroup_path = NULL;
408 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
414 STRV_FOREACH(k, s->manager->controllers) {
416 r = cg_create_and_attach(*k, p, s->leader);
418 r = cg_create(*k, p);
421 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
427 int session_start(Session *s) {
434 r = session_create_cgroup(s);
438 /* Create X11 symlink */
439 session_link_x11_socket(s);
441 /* Save session data */
444 dual_timestamp_get(&s->timestamp);
449 static bool session_shall_kill(Session *s) {
452 return s->kill_processes;
455 static int session_kill_cgroup(Session *s) {
464 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
466 if (session_shall_kill(s)) {
468 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
470 log_error("Failed to kill session cgroup: %s", strerror(-r));
473 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
475 log_error("Failed to check session cgroup: %s", strerror(-r));
477 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
479 log_error("Failed to delete session cgroup: %s", strerror(-r));
484 STRV_FOREACH(k, s->user->manager->controllers)
485 cg_trim(*k, s->cgroup_path, true);
487 free(s->cgroup_path);
488 s->cgroup_path = NULL;
493 static int session_unlink_x11_socket(Session *s) {
500 if (s->user->display != s)
503 s->user->display = NULL;
505 t = strappend(s->user->runtime_path, "/display");
507 log_error("Out of memory");
514 return r < 0 ? -errno : 0;
517 int session_stop(Session *s) {
523 k = session_kill_cgroup(s);
527 /* Remove X11 symlink */
528 session_unlink_x11_socket(s);
530 unlink(s->state_file);
531 session_add_to_gc_queue(s);
536 bool session_is_active(Session *s) {
542 return s->seat->active == s;
545 int session_get_idle_hint(Session *s, dual_timestamp *t) {
556 *t = s->idle_hint_timestamp;
564 if (s->tty[0] != '/') {
565 p = strappend("/dev/", s->tty);
571 if (!startswith(p ? p : s->tty, "/dev/")) {
576 k = lstat(p ? p : s->tty, &st);
582 u = timespec_load(&st.st_atim);
583 n = now(CLOCK_REALTIME);
584 b = u + IDLE_THRESHOLD_USEC < n;
587 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
593 *t = s->idle_hint_timestamp;
598 int session_check_gc(Session *s) {
603 if (s->pipe_fd >= 0) {
605 r = pipe_eof(s->pipe_fd);
613 if (s->cgroup_path) {
615 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
626 void session_add_to_gc_queue(Session *s) {
632 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
633 s->in_gc_queue = true;
636 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
637 [SESSION_TTY] = "tty",
638 [SESSION_X11] = "x11"
641 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);