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 Session* session_new(Manager *m, User *u, const char *id) {
41 s->state_file = strappend("/run/systemd/session/", id);
47 s->id = file_name_from_path(s->state_file);
49 if (hashmap_put(m->sessions, s->id, s) < 0) {
59 LIST_PREPEND(Session, sessions_by_user, u->sessions, s);
64 void session_free(Session *s) {
68 LIST_REMOVE(Session, gc_queue, s->manager->session_gc_queue, s);
71 LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s);
73 if (s->user->display == s)
74 s->user->display = NULL;
78 LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s);
81 strv_free(s->controllers);
88 hashmap_remove(s->manager->sessions, s->id);
94 int session_save(Session *s) {
101 r = safe_mkdir("/run/systemd/session", 0755, 0, 0);
105 r = fopen_temporary(s->state_file, &f, &temp_path);
111 fchmod(fileno(f), 0644);
114 "# This is private data. Do not parse.\n"
119 "KILL_PROCESSES=%i\n",
120 (unsigned long) s->user->uid,
122 session_is_active(s),
156 if (s->seat && s->seat->manager->vtconsole == s->seat)
164 (unsigned long) s->leader);
169 (unsigned long long) s->audit_id);
173 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
175 unlink(s->state_file);
184 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
189 int session_load(Session *s) {
195 int session_activate(Session *s) {
207 if (s->seat->active == s)
210 assert(s->manager->vtconsole == s->seat);
216 old_active = s->seat->active;
219 return seat_apply_acls(s->seat, old_active);
222 bool x11_display_is_local(const char *display) {
231 static int session_link_x11_socket(Session *s) {
237 assert(s->user->runtime_path);
239 if (s->user->display)
242 if (!s->display || !x11_display_is_local(s->display))
245 k = strspn(s->display+1, "0123456789");
246 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
248 log_error("Out of memory");
252 c = stpcpy(f, "/tmp/.X11-unix/X");
253 memcpy(c, s->display+1, k);
256 if (access(f, F_OK) < 0) {
257 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
262 t = strappend(s->user->runtime_path, "/display");
264 log_error("Out of memory");
269 if (link(f, t) < 0) {
270 if (errno == EEXIST) {
277 if (symlink(f, t) < 0) {
279 if (errno == EEXIST) {
282 if (symlink(f, t) >= 0)
286 log_error("Failed to link %s to %s: %m", f, t);
294 log_info("Linked %s to %s.", f, t);
298 s->user->display = s;
303 static int session_create_cgroup(Session *s) {
310 assert(s->user->cgroup_path);
312 if (!s->cgroup_path) {
313 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
314 log_error("Out of memory");
321 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, p, s->leader);
323 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
327 s->cgroup_path = NULL;
328 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
334 STRV_FOREACH(k, s->manager->controllers) {
336 r = cg_create_and_attach(*k, p, s->leader);
338 r = cg_create(*k, p);
341 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
347 int session_start(Session *s) {
354 r = session_create_cgroup(s);
358 /* Create X11 symlink */
359 session_link_x11_socket(s);
361 /* Save session data */
364 dual_timestamp_get(&s->timestamp);
369 static bool session_shall_kill(Session *s) {
372 return s->kill_processes;
375 static int session_kill_cgroup(Session *s) {
384 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
386 if (session_shall_kill(s)) {
388 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
390 log_error("Failed to kill session cgroup: %s", strerror(-r));
393 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
395 log_error("Failed to check session cgroup: %s", strerror(-r));
397 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
399 log_error("Failed to delete session cgroup: %s", strerror(-r));
404 STRV_FOREACH(k, s->user->manager->controllers)
405 cg_trim(*k, s->cgroup_path, true);
407 free(s->cgroup_path);
408 s->cgroup_path = NULL;
413 static int session_unlink_x11_socket(Session *s) {
420 if (s->user->display != s)
423 s->user->display = NULL;
425 t = strappend(s->user->runtime_path, "/display");
427 log_error("Out of memory");
434 return r < 0 ? -errno : 0;
437 int session_stop(Session *s) {
443 k = session_kill_cgroup(s);
447 /* Remove X11 symlink */
448 session_unlink_x11_socket(s);
450 unlink(s->state_file);
451 session_add_to_gc_queue(s);
456 bool session_is_active(Session *s) {
462 return s->seat->active == s;
465 int session_check_gc(Session *s) {
470 if (s->pipe_fd >= 0) {
472 r = pipe_eof(s->pipe_fd);
480 if (s->cgroup_path) {
482 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
493 void session_add_to_gc_queue(Session *s) {
499 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
500 s->in_gc_queue = true;
503 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
504 [SESSION_TTY] = "tty",
505 [SESSION_X11] = "x11"
508 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);