logind-session.c revision 6a38d04a036475bfc222f40961e2adcaa70a6174
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2011 Lennart Poettering
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer systemd is free software; you can redistribute it and/or modify it
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer under the terms of the GNU Lesser General Public License as published by
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer the Free Software Foundation; either version 2.1 of the License, or
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (at your option) any later version.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer systemd is distributed in the hope that it will be useful, but
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer WITHOUT ANY WARRANTY; without even the implied warranty of
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Lesser General Public License for more details.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer You should have received a copy of the GNU Lesser General Public License
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer along with systemd; If not, see <http://www.gnu.org/licenses/>.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic unsigned long devt_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int devt_compare_func(const void *_a, const void *_b) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald HoyerSession* session_new(Manager *m, const char *id) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer s->state_file = strappend("/run/systemd/sessions/", id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer s->devices = hashmap_new(devt_hash_func, devt_compare_func);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_REMOVE(gc_queue, s->manager->session_gc_queue, s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_REMOVE(sessions_by_user, s->user->sessions, s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_REMOVE(sessions_by_seat, s->seat->sessions, s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer hashmap_remove(s->manager->session_units, s->scope);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_PREPEND(sessions_by_user, u->sessions, s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = fopen_temporary(s->state_file, &f, &temp_path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "# This is private data. Do not parse.\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "ACTIVE=%i\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "STATE=%s\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "REMOTE=%i\n",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (s->type >= 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fprintf(f, "TYPE=%s\n", session_type_to_string(s->type));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fprintf(f, "CLASS=%s\n", session_class_to_string(s->class));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fprintf(f, "REMOTE_HOST=%s\n", s->remote_host);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fprintf(f, "REMOTE_USER=%s\n", s->remote_user);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fprintf(f, "LEADER=%lu\n", (unsigned long) s->leader);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "REALTIME=%llu\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "MONOTONIC=%llu\n",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ferror(f) || rename(temp_path, s->state_file) < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to read %s: %s", s->state_file, strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("UID not specified for session %s", s->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to parse UID value %s for session %s.", uid, s->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer user = hashmap_get(s->manager->users, ULONG_TO_PTR((unsigned long) u));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("User of session %s not known.", s->id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!o || r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Cannot attach session %s to seat %s", s->id, seat);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer audit_session_from_pid(s->leader, &s->audit_id);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* If we open an unopened pipe for reading we will not
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer get an EOF. to trigger an EOF we hence open it for
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer reading, but close it right-away which then will
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer trigger the EOF. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned long long l;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned long long l;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (bus_name_has_owner(s->manager->bus, controller, NULL) > 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* on seats with VTs, we let VTs manage session-switching */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* On seats without VTs, we implement session-switching in logind. We
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * try to pause all session-devices and wait until the session
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * controller acknowledged them. Once all devices are asleep, we simply
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * switch the active session and be done.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * We save the session we want to switch to in seat->pending_switch and
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * seat_complete_switch() will perform the final switch. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* if no devices are running, immediately perform the session switch */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer description = strjoin("Session ", s->id, " of user ", s->user->name, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer scope = strjoin("session-", s->id, ".scope", NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer kill_mode = manager_shall_kill(s->manager, s->user->name) ? "control-group" : "none";
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = manager_start_scope(s->manager, scope, s->leader, s->user->slice, description, "systemd-user-sessions.service", kill_mode, &error, &job);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to start session scope %s: %s %s",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer scope, bus_error_message(&error, r), error.name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer hashmap_put(s->manager->session_units, s->scope, s);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Create cgroup */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_struct(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "MESSAGE=New session %s of user %s.", s->id, s->user->name,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Save session data */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer seat_send_changed(s->seat, "Sessions", "ActiveSession", NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = manager_stop_unit(s->manager, s->scope, &error, &job);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error("Failed to stop session scope: %s", bus_error_message(&error, r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Kill cgroup */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_struct(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Kill session devices */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return true;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int get_tty_atime(const char *tty, usec_t *atime) {
tty = p;
return -ENOENT;
return -errno;
assert(s);
if (s->idle_hint) {
*t = s->idle_hint_timestamp;
return s->idle_hint;
if (s->display)
goto dont_know;
if (s->tty) {
goto found_atime;
if (s->leader > 0) {
goto found_atime;
*t = s->idle_hint_timestamp;
assert(s);
if (s->idle_hint == b)
s->idle_hint = b;
if (s->seat)
assert(s);
session_stop(s);
assert(s);
if (!s->fifo_path) {
return -ENOMEM;
return -errno;
if (s->fifo_fd < 0) {
if (s->fifo_fd < 0)
return -errno;
if (!s->fifo_event_source) {
r = sd_event_add_io(s->manager->event, s->fifo_fd, 0, session_dispatch_fifo, s, &s->fifo_event_source);
return -errno;
assert(s);
if (s->fifo_event_source)
if (s->fifo_fd >= 0) {
if (s->fifo_path) {
assert(s);
if (!s->user)
if (s->fifo_fd >= 0) {
assert(s);
if (s->in_gc_queue)
s->in_gc_queue = true;
assert(s);
if (s->scope_job)
return SESSION_OPENING;
if (s->fifo_fd < 0)
return SESSION_CLOSING;
if (session_is_active(s))
return SESSION_ACTIVE;
return SESSION_ONLINE;
assert(s);
if (!s->scope)
return -ESRCH;
if (!s->vtnr)
if (s->vtfd >= 0)
return s->vtfd;
if (s->vtfd < 0) {
return s->vtfd;
if (s->vtfd >= 0)
int vt, r;
if (vt < 0)
goto error;
goto error;
goto error;
goto error;
if (vt < 0)
assert(s);
if (s->controller) {
if (!name)
session_save(s);
assert(s);
return -EBUSY;
return -ENOMEM;
free(t);
session_swap_controller(s, t);
session_mute_vt(s);
assert(s);
if (!s->controller)