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) {
187 if (s->seat->active == s)
190 assert(s->manager->vtconsole == s->seat);
196 old_active = s->seat->active;
199 seat_apply_acls(s->seat, old_active);
200 manager_spawn_autovt(s->manager, s->vtnr);
205 bool x11_display_is_local(const char *display) {
214 static int session_link_x11_socket(Session *s) {
220 assert(s->user->runtime_path);
222 if (s->user->display)
225 if (!s->display || !x11_display_is_local(s->display))
228 k = strspn(s->display+1, "0123456789");
229 f = new(char, sizeof("/tmp/.X11-unix/X") + k);
231 log_error("Out of memory");
235 c = stpcpy(f, "/tmp/.X11-unix/X");
236 memcpy(c, s->display+1, k);
239 if (access(f, F_OK) < 0) {
240 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
245 t = strappend(s->user->runtime_path, "/display");
247 log_error("Out of memory");
252 if (link(f, t) < 0) {
253 if (errno == EEXIST) {
260 if (symlink(f, t) < 0) {
262 if (errno == EEXIST) {
265 if (symlink(f, t) >= 0)
269 log_error("Failed to link %s to %s: %m", f, t);
277 log_info("Linked %s to %s.", f, t);
281 s->user->display = s;
286 static int session_create_cgroup(Session *s) {
293 assert(s->user->cgroup_path);
295 if (!s->cgroup_path) {
296 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
297 log_error("Out of memory");
304 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, p, s->leader);
306 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
310 s->cgroup_path = NULL;
311 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
317 STRV_FOREACH(k, s->manager->controllers) {
319 r = cg_create_and_attach(*k, p, s->leader);
321 r = cg_create(*k, p);
324 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
330 int session_start(Session *s) {
336 /* Create user first */
337 r = user_start(s->user);
342 r = session_create_cgroup(s);
346 /* Create X11 symlink */
347 session_link_x11_socket(s);
351 static bool session_shall_kill(Session *s) {
354 return s->kill_processes;
357 static int session_kill_cgroup(Session *s) {
366 cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
368 if (session_shall_kill(s)) {
370 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
372 log_error("Failed to kill session cgroup: %s", strerror(-r));
375 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
377 log_error("Failed to check session cgroup: %s", strerror(-r));
379 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
381 log_error("Failed to delete session cgroup: %s", strerror(-r));
386 STRV_FOREACH(k, s->user->manager->controllers)
387 cg_trim(*k, s->cgroup_path, true);
389 free(s->cgroup_path);
390 s->cgroup_path = NULL;
395 static int session_unlink_x11_socket(Session *s) {
402 if (s->user->display != s)
405 s->user->display = NULL;
407 t = strappend(s->user->runtime_path, "/display");
409 log_error("Out of memory");
416 return r < 0 ? -errno : 0;
419 int session_stop(Session *s) {
425 k = session_kill_cgroup(s);
429 /* Remove X11 symlink */
430 session_unlink_x11_socket(s);
435 bool session_is_active(Session *s) {
441 return s->seat->active == s;
444 int session_check_gc(Session *s) {
449 if (s->pipe_fd >= 0) {
451 r = pipe_eof(s->pipe_fd);
459 if (s->cgroup_path) {
461 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
472 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
473 [SESSION_TERMINAL] = "terminal",
474 [SESSION_X11] = "x11"
477 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);