logind-session.c revision 8c8c43515cee56dfc2298998a9e5958308c46f99
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/***
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering This file is part of systemd.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2011 Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (at your option) any later version.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is distributed in the hope that it will be useful, but
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Lesser General Public License for more details.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering***/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering#include <errno.h>
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering#include <string.h>
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include <unistd.h>
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include <sys/epoll.h>
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include <fcntl.h>
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "strv.h"
3c70e3bb022f0de3317f3600c9366a2f4597339eLennart Poettering#include "util.h"
3c70e3bb022f0de3317f3600c9366a2f4597339eLennart Poettering#include "mkdir.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "path-util.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "cgroup-util.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "logind-session.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define IDLE_THRESHOLD_USEC (5*USEC_PER_MINUTE)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel MackSession* session_new(Manager *m, User *u, const char *id) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Session *s;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(m);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s = new0(Session, 1);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!s)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->state_file = strappend("/run/systemd/sessions/", id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!s->state_file) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->id = path_get_file_name(s->state_file);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering if (hashmap_put(m->sessions, s->id, s) < 0) {
4f10118016f9b2fd7e1d26c9ef7d91eb33fba694Lennart Poettering free(s->state_file);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->manager = m;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->fifo_fd = -1;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->user = u;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering LIST_PREPEND(Session, sessions_by_user, u->sessions, s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return s;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid session_free(Session *s) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->in_gc_queue)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering LIST_REMOVE(Session, gc_queue, s->manager->session_gc_queue, s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->user) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->user->display == s)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->user->display = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->seat) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->seat->active == s)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->seat->active = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->cgroup_path)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering hashmap_remove(s->manager->session_cgroups, s->cgroup_path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(s->cgroup_path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering strv_free(s->controllers);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(s->tty);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(s->display);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(s->remote_host);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(s->remote_user);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(s->service);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering hashmap_remove(s->manager->sessions, s->id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering session_remove_fifo(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(s->state_file);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint session_save(Session *s) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering FILE *f;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r = 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering char *temp_path;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (!s->started)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto finish;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = fopen_temporary(s->state_file, &f, &temp_path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto finish;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s->user);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering fchmod(fileno(f), 0644);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "# This is private data. Do not parse.\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "UID=%lu\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "USER=%s\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "ACTIVE=%i\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "REMOTE=%i\n"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "KILL_PROCESSES=%i\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (unsigned long) s->user->uid,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->user->name,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering session_is_active(s),
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->remote,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->kill_processes);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->type >= 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "TYPE=%s\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering session_type_to_string(s->type));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->class >= 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "CLASS=%s\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering session_class_to_string(s->class));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->cgroup_path)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "CGROUP=%s\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->cgroup_path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->fifo_path)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "FIFO=%s\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->fifo_path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->seat)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "SEAT=%s\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->seat->id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->tty)
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack fprintf(f,
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack "TTY=%s\n",
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack s->tty);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (s->display)
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack fprintf(f,
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack "DISPLAY=%s\n",
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack s->display);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (s->remote_host)
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack fprintf(f,
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack "REMOTE_HOST=%s\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->remote_host);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->remote_user)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "REMOTE_USER=%s\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->remote_user);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->service)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "SERVICE=%s\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->service);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->seat && seat_can_multi_session(s->seat))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "VTNR=%i\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->vtnr);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->leader > 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "LEADER=%lu\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (unsigned long) s->leader);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering if (s->audit_id > 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fprintf(f,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "AUDIT=%llu\n",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (unsigned long long) s->audit_id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fflush(f);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (ferror(f) || rename(temp_path, s->state_file) < 0) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = -errno;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering unlink(s->state_file);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering unlink(temp_path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fclose(f);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(temp_path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringfinish:
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint session_load(Session *s) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering char *remote = NULL,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *kill_processes = NULL,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *seat = NULL,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *vtnr = NULL,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *leader = NULL,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *audit_id = NULL,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *type = NULL,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering *class = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int k, r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = parse_env_file(s->state_file, NEWLINE,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "REMOTE", &remote,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "KILL_PROCESSES", &kill_processes,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "CGROUP", &s->cgroup_path,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "FIFO", &s->fifo_path,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "SEAT", &seat,
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering "TTY", &s->tty,
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack "DISPLAY", &s->display,
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering "REMOTE_HOST", &s->remote_host,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "REMOTE_USER", &s->remote_user,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "SERVICE", &s->service,
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering "VTNR", &vtnr,
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack "LEADER", &leader,
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering "TYPE", &type,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering "CLASS", &class,
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto finish;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (remote) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering k = parse_boolean(remote);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (k >= 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->remote = k;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (kill_processes) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering k = parse_boolean(kill_processes);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (k >= 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->kill_processes = k;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (seat && !s->seat) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Seat *o;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering o = hashmap_get(s->manager->seats, seat);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (o)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering seat_attach_session(o, s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (vtnr && s->seat && seat_can_multi_session(s->seat)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int v;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering k = safe_atoi(vtnr, &v);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (k >= 0 && v >= 1)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->vtnr = v;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
31938a8560a664c32a9d72f1fc2d4347b232e6e9Michal Schmidt
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (leader) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering k = parse_pid(leader, &s->leader);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (k >= 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering audit_session_from_pid(s->leader, &s->audit_id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (type) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering SessionType t;
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek t = session_type_from_string(type);
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek if (t >= 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->type = t;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (class) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering SessionClass c;
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek c = session_class_from_string(class);
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt if (c >= 0)
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt s->class = c;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->fifo_path) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int fd;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* If we open an unopened pipe for reading we will not
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering get an EOF. to trigger an EOF we hence open it for
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering reading, but close it right-away which then will
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering trigger the EOF. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering fd = session_create_fifo(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (fd >= 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering close_nointr_nofail(fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringfinish:
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(remote);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(kill_processes);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(seat);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(vtnr);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(leader);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(audit_id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(class);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint session_activate(Session *s) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->vtnr < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -ENOTSUP;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!s->seat)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -ENOTSUP;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->seat->active == s)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(seat_is_vtconsole(s->seat));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = chvt(s->vtnr);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return seat_set_active(s->seat, s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int session_link_x11_socket(Session *s) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering char *t, *f, *c;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering size_t k;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s->user);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s->user->runtime_path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->user->display)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!s->display || !display_is_local(s->display))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering k = strspn(s->display+1, "0123456789");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering f = new(char, sizeof("/tmp/.X11-unix/X") + k);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!f) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Out of memory");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -ENOMEM;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering c = stpcpy(f, "/tmp/.X11-unix/X");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering memcpy(c, s->display+1, k);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering c[k] = 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (access(f, F_OK) < 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_warning("Session %s has display %s with non-existing socket %s.", s->id, s->display, f);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(f);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -ENOENT;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Note that this cannot be in a subdir to avoid
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * vulnerabilities since we are privileged but the runtime
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * path is owned by the user */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering t = strappend(s->user->runtime_path, "/X11-display");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (!t) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Out of memory");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(f);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -ENOMEM;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (link(f, t) < 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (errno == EEXIST) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering unlink(t);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (link(f, t) >= 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto done;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (symlink(f, t) < 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (errno == EEXIST) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering unlink(t);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (symlink(f, t) >= 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering goto done;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Failed to link %s to %s: %m", f, t);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(f);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(t);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -errno;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringdone:
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_info("Linked %s to %s.", f, t);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(f);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(t);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->user->display = s;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int session_create_one_group(Session *s, const char *controller, const char *path) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(controller);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->leader > 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = cg_create_and_attach(controller, path, s->leader);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = cg_create(controller, path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering } else
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = cg_create(controller, path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
db2cb23b5b179707000d28a11efb3d888d06ee80Umut Tezduyar Lindskog return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid, -1);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r >= 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid);
31938a8560a664c32a9d72f1fc2d4347b232e6e9Michal Schmidt
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int session_create_cgroup(Session *s) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering char **k;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering char *p;
31938a8560a664c32a9d72f1fc2d4347b232e6e9Michal Schmidt int r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s->user);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s->user->cgroup_path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (!s->cgroup_path) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Out of memory");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -ENOMEM;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering } else
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering p = s->cgroup_path;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->cgroup_path = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack s->cgroup_path = p;
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
31938a8560a664c32a9d72f1fc2d4347b232e6e9Michal Schmidt STRV_FOREACH(k, s->controllers) {
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (strv_contains(s->reset_controllers, *k))
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack continue;
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack r = session_create_one_group(s, *k, p);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering STRV_FOREACH(k, s->manager->controllers) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (strv_contains(s->reset_controllers, *k) ||
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering strv_contains(s->manager->reset_controllers, *k) ||
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering strv_contains(s->controllers, *k))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering continue;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = session_create_one_group(s, *k, p);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->leader > 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
8d0e0ddda6501479eb69164687c83c1a7667b33aJan Engelhardt STRV_FOREACH(k, s->reset_controllers) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = cg_attach(*k, "/", s->leader);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
31938a8560a664c32a9d72f1fc2d4347b232e6e9Michal Schmidt STRV_FOREACH(k, s->manager->reset_controllers) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (strv_contains(s->reset_controllers, *k) ||
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering strv_contains(s->controllers, *k))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering continue;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
8d0e0ddda6501479eb69164687c83c1a7667b33aJan Engelhardt r = cg_attach(*k, "/", s->leader);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
31938a8560a664c32a9d72f1fc2d4347b232e6e9Michal Schmidt }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering hashmap_put(s->manager->session_cgroups, s->cgroup_path, s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint session_start(Session *s) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s->user);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->started)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = user_start(s->user);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_full(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "New session %s of user %s.", s->id, s->user->name);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack /* Create cgroup */
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack r = session_create_cgroup(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Create X11 symlink */
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering session_link_x11_socket(s);
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering dual_timestamp_get(&s->timestamp);
ff975efb2e88dcd5221a2f0d76c4c87e85b821a8Lennart Poettering
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering if (s->seat)
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering seat_read_active_vt(s->seat);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->started = true;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Save session data */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering session_save(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering user_save(s->user);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering session_send_signal(s, true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->seat) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering seat_save(s->seat);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->seat->active == s)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering else
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering seat_send_changed(s->seat, "Sessions\0");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering user_send_changed(s->user, "Sessions\0");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic bool session_shall_kill(Session *s) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!s->kill_processes)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return false;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (strv_contains(s->manager->kill_exclude_users, s->user->name))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return false;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (strv_isempty(s->manager->kill_only_users))
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return true;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return strv_contains(s->manager->kill_only_users, s->user->name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int session_terminate_cgroup(Session *s) {
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering char **k;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!s->cgroup_path)
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (session_shall_kill(s)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error("Failed to kill session cgroup: %s", strerror(-r));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } else {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->leader > 0) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Session *t;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* We still send a HUP to the leader process,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * even if we are not supposed to kill the
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * whole cgroup. But let's first check the
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * leader still exists and belongs to our
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * session... */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = manager_get_session_by_pid(s->manager, s->leader, &t);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r > 0 && t == s) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering kill(s->leader, SIGTERM); /* for normal processes */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering kill(s->leader, SIGHUP); /* for shells */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering kill(s->leader, SIGCONT); /* in case they are stopped */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack log_error("Failed to check session cgroup: %s", strerror(-r));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering else if (r > 0) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Failed to delete session cgroup: %s", strerror(-r));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering STRV_FOREACH(k, s->user->manager->controllers)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering cg_trim(*k, s->cgroup_path, true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering hashmap_remove(s->manager->session_cgroups, s->cgroup_path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(s->cgroup_path);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->cgroup_path = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int session_unlink_x11_socket(Session *s) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering char *t;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s->user);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->user->display != s)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->user->display = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering t = strappend(s->user->runtime_path, "/X11-display");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!t) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error("Out of memory");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -ENOMEM;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = unlink(t);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(t);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r < 0 ? -errno : 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint session_stop(Session *s) {
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering int r = 0, k;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->started)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_full(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "Removed session %s.", s->id);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Kill cgroup */
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering k = session_terminate_cgroup(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (k < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = k;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Remove X11 symlink */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering session_unlink_x11_socket(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering unlink(s->state_file);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering session_add_to_gc_queue(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering user_add_to_gc_queue(s->user);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->started)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering session_send_signal(s, false);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->seat) {
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering if (s->seat->active == s)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering seat_set_active(s->seat, NULL);
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering seat_send_changed(s->seat, "Sessions\0");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering user_send_changed(s->user, "Sessions\0");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->started = false;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringbool session_is_active(Session *s) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!s->seat)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return true;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return s->seat->active == s;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint session_get_idle_hint(Session *s, dual_timestamp *t) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering char *p;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering struct stat st;
94e15fdc4d9d96fa6607bfb4eaaea164a3aec417David Herrmann usec_t u, n;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering bool b;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering int k;
e24e415e5f3af2fe86d2be9a1f1a2e8d5f8c96bfDaniel Mack
e24e415e5f3af2fe86d2be9a1f1a2e8d5f8c96bfDaniel Mack assert(s);
e24e415e5f3af2fe86d2be9a1f1a2e8d5f8c96bfDaniel Mack
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (s->idle_hint) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (t)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering *t = s->idle_hint_timestamp;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering return s->idle_hint;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering }
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (isempty(s->tty))
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering goto dont_know;
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering if (s->tty[0] != '/') {
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering p = strappend("/dev/", s->tty);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (!p)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering return -ENOMEM;
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering } else
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering p = NULL;
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering if (!startswith(p ? p : s->tty, "/dev/")) {
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering free(p);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering goto dont_know;
94e15fdc4d9d96fa6607bfb4eaaea164a3aec417David Herrmann }
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering k = lstat(p ? p : s->tty, &st);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering free(p);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering if (k < 0)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering goto dont_know;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering u = timespec_load(&st.st_atim);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering n = now(CLOCK_REALTIME);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering b = u + IDLE_THRESHOLD_USEC < n;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (t)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering return b;
a9c8343e83ec09f80a76930573b2592f97ae4283Daniel Mack
a9c8343e83ec09f80a76930573b2592f97ae4283Daniel Mackdont_know:
a9c8343e83ec09f80a76930573b2592f97ae4283Daniel Mack if (t)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering *t = s->idle_hint_timestamp;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering return 0;
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering}
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poetteringvoid session_set_idle_hint(Session *s, bool b) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering assert(s);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (s->idle_hint == b)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering return;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering s->idle_hint = b;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering dual_timestamp_get(&s->idle_hint_timestamp);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering session_send_changed(s,
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering "IdleHint\0"
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering "IdleSinceHint\0"
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering "IdleSinceHintMonotonic\0");
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (s->seat)
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering seat_send_changed(s->seat,
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering "IdleHint\0"
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering "IdleSinceHint\0"
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering "IdleSinceHintMonotonic\0");
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering user_send_changed(s->user,
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering "IdleHint\0"
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering "IdleSinceHint\0"
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering "IdleSinceHintMonotonic\0");
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering manager_send_changed(s->manager,
a9c8343e83ec09f80a76930573b2592f97ae4283Daniel Mack "IdleHint\0"
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering "IdleSinceHint\0"
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering "IdleSinceHintMonotonic\0");
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering}
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poetteringint session_create_fifo(Session *s) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert(s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Create FIFO */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!s->fifo_path) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering if (asprintf(&s->fifo_path, "/run/systemd/sessions/%s.ref", s->id) < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -ENOMEM;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering if (mkfifo(s->fifo_path, 0600) < 0 && errno != EEXIST)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -errno;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Open reading side */
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (s->fifo_fd < 0) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering struct epoll_event ev;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->fifo_fd < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -errno;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = hashmap_put(s->manager->session_fds, INT_TO_PTR(s->fifo_fd + 1), s);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering zero(ev);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering ev.events = 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering ev.data.u32 = FD_OTHER_BASE + s->fifo_fd;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, s->fifo_fd, &ev) < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -errno;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Open writing side */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = open(s->fifo_path, O_WRONLY|O_CLOEXEC|O_NDELAY);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return -errno;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
deffddf1df29a5ed047feff3a0f2b765006fb71bLukas Nykrynvoid session_remove_fifo(Session *s) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->fifo_fd >= 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert_se(hashmap_remove(s->manager->session_fds, INT_TO_PTR(s->fifo_fd + 1)) == s);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering assert_se(epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_DEL, s->fifo_fd, NULL) == 0);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering close_nointr_nofail(s->fifo_fd);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering s->fifo_fd = -1;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->fifo_path) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering unlink(s->fifo_path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering free(s->fifo_path);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->fifo_path = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint session_check_gc(Session *s, bool drop_not_started) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (drop_not_started && !s->started)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->fifo_fd >= 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = pipe_eof(s->fifo_fd);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r == 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 1;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->cgroup_path) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r < 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return r;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r <= 0)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 1;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering }
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringvoid session_add_to_gc_queue(Session *s) {
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering assert(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (s->in_gc_queue)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering s->in_gc_queue = true;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering}
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringint session_kill(Session *s, KillWho who, int signo) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering int r = 0;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering Set *pid_set = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert(s);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (!s->cgroup_path)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -ESRCH;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->leader <= 0 && who == KILL_LEADER)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -ESRCH;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->leader > 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (kill(s->leader, signo) < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = -errno;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (who == KILL_ALL) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int q;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering pid_set = set_new(trivial_hash_func, trivial_compare_func);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!pid_set)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return -ENOMEM;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (s->leader > 0) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering q = set_put(pid_set, LONG_TO_PTR(s->leader));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (q < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = q;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, signo, false, true, false, pid_set);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (q < 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = q;
2f671520ebade4877cbf6aca3572a5f8c4e1871dLennart Poettering }
2f671520ebade4877cbf6aca3572a5f8c4e1871dLennart Poettering
2f671520ebade4877cbf6aca3572a5f8c4e1871dLennart Poettering if (pid_set)
2f671520ebade4877cbf6aca3572a5f8c4e1871dLennart Poettering set_free(pid_set);
2f671520ebade4877cbf6aca3572a5f8c4e1871dLennart Poettering
2f671520ebade4877cbf6aca3572a5f8c4e1871dLennart Poettering return r;
2f671520ebade4877cbf6aca3572a5f8c4e1871dLennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic const char* const session_type_table[_SESSION_TYPE_MAX] = {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering [SESSION_TTY] = "tty",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [SESSION_X11] = "x11",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [SESSION_UNSPECIFIED] = "unspecified"
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering};
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic const char* const session_class_table[_SESSION_CLASS_MAX] = {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [SESSION_USER] = "user",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [SESSION_GREETER] = "greeter",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [SESSION_LOCK_SCREEN] = "lock-screen"
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering};
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(session_class, SessionClass);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic const char* const kill_who_table[_KILL_WHO_MAX] = {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [KILL_LEADER] = "leader",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering [KILL_ALL] = "all"
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering};
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering