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 dual_timestamp_get(&s->timestamp);
64 void session_free(Session *s) {
68 LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s);
70 if (s->user->display == s)
71 s->user->display = NULL;
75 LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s);
78 strv_free(s->controllers);
84 hashmap_remove(s->manager->sessions, s->id);
90 int session_save(Session *s) {
96 r = safe_mkdir("/run/systemd/session", 0755, 0, 0);
100 f = fopen(s->state_file, "we");
107 "# This is private data. Do not parse.\n"
112 "KILL_PROCESSES=%i\n",
113 (unsigned long) s->user->uid,
115 session_is_active(s),
144 if (s->seat && s->seat->manager->vtconsole == s->seat)
152 (unsigned long) s->leader);
157 (unsigned long long) s->audit_id);
162 unlink(s->state_file);
169 int session_load(Session *s) {
175 int session_activate(Session *s) {
186 if (s->seat->active == s)
189 assert(s->manager->vtconsole == s->seat);
197 return seat_apply_acls(s->seat);
200 bool x11_display_is_local(const char *display) {
209 static int session_link_x11_socket(Session *s) {
215 assert(s->user->runtime_path);
217 if (s->user->display)
220 if (!s->display || !x11_display_is_local(s->display))
223 k = strspn(s->display+1, "0123456789");
224 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
226 log_error("Out of memory");
230 c = stpcpy(f, "/tmp/.X11-unix/X");
231 memcpy(c, s->display+1, k);
234 if (access(f, F_OK) < 0) {
235 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
240 t = strappend(s->user->runtime_path, "/display");
242 log_error("Out of memory");
247 if (link(f, t) < 0) {
248 if (errno == EEXIST) {
255 if (symlink(f, t) < 0) {
257 if (errno == EEXIST) {
260 if (symlink(f, t) >= 0)
264 log_error("Failed to link %s to %s: %m", f, t);
272 log_info("Linked %s to %s.", f, t);
276 s->user->display = s;
281 static int session_create_cgroup(Session *s) {
288 assert(s->user->cgroup_path);
290 if (!s->cgroup_path) {
291 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
292 log_error("Out of memory");
299 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, p, s->leader);
301 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
305 s->cgroup_path = NULL;
306 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
312 STRV_FOREACH(k, s->manager->controllers) {
314 r = cg_create_and_attach(*k, p, s->leader);
316 r = cg_create(*k, p);
319 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
325 int session_start(Session *s) {
331 /* Create user first */
332 r = user_start(s->user);
337 r = session_create_cgroup(s);
341 /* Create X11 symlink */
342 session_link_x11_socket(s);
346 static bool session_shall_kill(Session *s) {
349 return s->kill_processes;
352 static int session_kill_cgroup(Session *s) {
361 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
363 if (session_shall_kill(s)) {
365 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
367 log_error("Failed to kill session cgroup: %s", strerror(-r));
370 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
372 log_error("Failed to check session cgroup: %s", strerror(-r));
374 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
376 log_error("Failed to delete session cgroup: %s", strerror(-r));
381 STRV_FOREACH(k, s->user->manager->controllers)
382 cg_trim(*k, s->cgroup_path, true);
384 free(s->cgroup_path);
385 s->cgroup_path = NULL;
390 static int session_unlink_x11_socket(Session *s) {
397 if (s->user->display != s)
400 s->user->display = NULL;
402 t = strappend(s->user->runtime_path, "/display");
404 log_error("Out of memory");
411 return r < 0 ? -errno : 0;
414 int session_stop(Session *s) {
420 k = session_kill_cgroup(s);
424 /* Remove X11 symlink */
425 session_unlink_x11_socket(s);
430 bool session_is_active(Session *s) {
436 return s->seat->active == s;
439 int session_check_gc(Session *s) {
444 if (s->pipe_fd >= 0) {
446 r = pipe_eof(s->pipe_fd);
454 if (s->cgroup_path) {
456 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
467 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
468 [SESSION_TERMINAL] = "terminal",
469 [SESSION_X11] = "x11"
472 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);