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);
87 hashmap_remove(s->manager->sessions, s->id);
90 unlink(s->state_file);
97 int session_save(Session *s) {
104 r = safe_mkdir("/run/systemd/session", 0755, 0, 0);
108 r = fopen_temporary(s->state_file, &f, &temp_path);
114 fchmod(fileno(f), 0644);
117 "# This is private data. Do not parse.\n"
122 "KILL_PROCESSES=%i\n",
123 (unsigned long) s->user->uid,
125 session_is_active(s),
154 if (s->seat && s->seat->manager->vtconsole == s->seat)
162 (unsigned long) s->leader);
167 (unsigned long long) s->audit_id);
171 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
173 unlink(s->state_file);
182 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
187 int session_load(Session *s) {
193 int session_activate(Session *s) {
205 if (s->seat->active == s)
208 assert(s->manager->vtconsole == s->seat);
214 old_active = s->seat->active;
217 return seat_apply_acls(s->seat, old_active);
220 bool x11_display_is_local(const char *display) {
229 static int session_link_x11_socket(Session *s) {
235 assert(s->user->runtime_path);
237 if (s->user->display)
240 if (!s->display || !x11_display_is_local(s->display))
243 k = strspn(s->display+1, "0123456789");
244 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
246 log_error("Out of memory");
250 c = stpcpy(f, "/tmp/.X11-unix/X");
251 memcpy(c, s->display+1, k);
254 if (access(f, F_OK) < 0) {
255 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
260 t = strappend(s->user->runtime_path, "/display");
262 log_error("Out of memory");
267 if (link(f, t) < 0) {
268 if (errno == EEXIST) {
275 if (symlink(f, t) < 0) {
277 if (errno == EEXIST) {
280 if (symlink(f, t) >= 0)
284 log_error("Failed to link %s to %s: %m", f, t);
292 log_info("Linked %s to %s.", f, t);
296 s->user->display = s;
301 static int session_create_cgroup(Session *s) {
308 assert(s->user->cgroup_path);
310 if (!s->cgroup_path) {
311 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
312 log_error("Out of memory");
319 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, p, s->leader);
321 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
325 s->cgroup_path = NULL;
326 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
332 STRV_FOREACH(k, s->manager->controllers) {
334 r = cg_create_and_attach(*k, p, s->leader);
336 r = cg_create(*k, p);
339 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
345 int session_start(Session *s) {
352 r = session_create_cgroup(s);
356 /* Create X11 symlink */
357 session_link_x11_socket(s);
359 /* Save session data */
362 dual_timestamp_get(&s->timestamp);
367 static bool session_shall_kill(Session *s) {
370 return s->kill_processes;
373 static int session_kill_cgroup(Session *s) {
382 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
384 if (session_shall_kill(s)) {
386 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
388 log_error("Failed to kill session cgroup: %s", strerror(-r));
391 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
393 log_error("Failed to check session cgroup: %s", strerror(-r));
395 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
397 log_error("Failed to delete session cgroup: %s", strerror(-r));
402 STRV_FOREACH(k, s->user->manager->controllers)
403 cg_trim(*k, s->cgroup_path, true);
405 free(s->cgroup_path);
406 s->cgroup_path = NULL;
411 static int session_unlink_x11_socket(Session *s) {
418 if (s->user->display != s)
421 s->user->display = NULL;
423 t = strappend(s->user->runtime_path, "/display");
425 log_error("Out of memory");
432 return r < 0 ? -errno : 0;
435 int session_stop(Session *s) {
441 k = session_kill_cgroup(s);
445 /* Remove X11 symlink */
446 session_unlink_x11_socket(s);
453 bool session_is_active(Session *s) {
459 return s->seat->active == s;
462 int session_check_gc(Session *s) {
467 if (s->pipe_fd >= 0) {
469 r = pipe_eof(s->pipe_fd);
477 if (s->cgroup_path) {
479 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
490 void session_add_to_gc_queue(Session *s) {
496 LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
497 s->in_gc_queue = true;
500 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
501 [SESSION_TERMINAL] = "terminal",
502 [SESSION_X11] = "x11"
505 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);