logind-session.c revision e6494a07cbbfa93dd83782fa1f8554aa4d2353bd
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "sd-messages.h"
#include "util.h"
#include "mkdir.h"
#include "path-util.h"
#include "fileio.h"
#include "audit.h"
#include "bus-util.h"
#include "bus-error.h"
#include "logind-session.h"
#include "formats-util.h"
#include "terminal-util.h"
static void session_remove_fifo(Session *s);
Session *s;
assert(m);
if (!s)
return NULL;
if (!s->state_file) {
free(s);
return NULL;
}
if (!s->devices) {
free(s->state_file);
free(s);
return NULL;
}
hashmap_free(s->devices);
free(s->state_file);
free(s);
return NULL;
}
s->manager = m;
s->fifo_fd = -1;
s->vtfd = -1;
return s;
}
void session_free(Session *s) {
assert(s);
if (s->in_gc_queue)
hashmap_free(s->devices);
if (s->user) {
}
if (s->seat) {
if (s->seat->pending_switch == s)
seat_evict_position(s->seat, s);
}
if (s->scope) {
}
free(s->remote_host);
free(s->remote_user);
free(s->state_file);
free(s);
}
assert(s);
s->user = u;
}
int session_save(Session *s) {
int r = 0;
assert(s);
if (!s->user)
return -ESTALE;
if (!s->started)
return 0;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
fprintf(f,
"# This is private data. Do not parse.\n"
"USER=%s\n"
"ACTIVE=%i\n"
"STATE=%s\n"
"REMOTE=%i\n",
s->remote);
if (s->type >= 0)
if (s->class >= 0)
if (s->scope)
if (s->scope_job)
if (s->fifo_path)
if (s->seat)
if (s->tty)
if (s->display)
if (s->remote_host) {
_cleanup_free_ char *escaped;
if (!escaped) {
r = -ENOMEM;
goto finish;
}
}
if (s->remote_user) {
_cleanup_free_ char *escaped;
if (!escaped) {
r = -ENOMEM;
goto finish;
}
}
if (s->service) {
_cleanup_free_ char *escaped;
if (!escaped) {
r = -ENOMEM;
goto finish;
}
}
if (s->desktop) {
_cleanup_free_ char *escaped;
if (!escaped) {
r = -ENOMEM;
goto finish;
}
}
if (!s->vtnr)
if (s->leader > 0)
if (s->audit_id > 0)
if (dual_timestamp_is_set(&s->timestamp))
fprintf(f,
if (s->controller)
fflush(f);
r = -errno;
unlink(s->state_file);
}
if (r < 0)
return r;
}
int session_load(Session *s) {
*controller = NULL;
int k, r;
assert(s);
"REMOTE", &remote,
"SCOPE", &s->scope,
"SCOPE_JOB", &s->scope_job,
"FIFO", &s->fifo_path,
"SEAT", &seat,
"TTY", &s->tty,
"DISPLAY", &s->display,
"REMOTE_HOST", &s->remote_host,
"REMOTE_USER", &s->remote_user,
"SERVICE", &s->service,
"DESKTOP", &s->desktop,
"VTNR", &vtnr,
"STATE", &state,
"POSITION", &position,
"LEADER", &leader,
"TYPE", &type,
"CLASS", &class,
"UID", &uid,
"REALTIME", &realtime,
"MONOTONIC", &monotonic,
"CONTROLLER", &controller,
NULL);
if (r < 0)
if (!s->user) {
uid_t u;
if (!uid) {
return -ENOENT;
}
if (r < 0) {
return r;
}
if (!user) {
return -ENOENT;
}
session_set_user(s, user);
}
if (remote) {
k = parse_boolean(remote);
if (k >= 0)
s->remote = k;
}
if (vtnr)
Seat *o;
if (o)
r = seat_attach_session(o, s);
if (!o || r < 0)
}
s->vtnr = 0;
unsigned int npos;
}
if (leader) {
if (k >= 0)
}
if (type) {
SessionType t;
t = session_type_from_string(type);
if (t >= 0)
s->type = t;
}
if (class) {
SessionClass c;
if (c >= 0)
s->class = c;
}
s->stopping = true;
if (s->fifo_path) {
int fd;
/* If we open an unopened pipe for reading we will not
get an EOF. to trigger an EOF we hence open it for
writing, but close it right away which then will
trigger the EOF. This will happen immediately if no
other process has the FIFO open for writing, i. e.
when the session died before logind (re)started. */
fd = session_create_fifo(s);
safe_close(fd);
}
if (realtime) {
unsigned long long l;
}
if (monotonic) {
unsigned long long l;
}
if (controller) {
session_set_controller(s, controller, false);
else
}
return r;
}
int session_activate(Session *s) {
unsigned int num_pending;
assert(s);
if (!s->seat)
return -EOPNOTSUPP;
return 0;
/* on seats with VTs, we let VTs manage session-switching */
if (seat_has_vts(s->seat)) {
if (!s->vtnr)
return -EOPNOTSUPP;
}
/* On seats without VTs, we implement session-switching in logind. We
* try to pause all session-devices and wait until the session
* controller acknowledged them. Once all devices are asleep, we simply
* switch the active session and be done.
* We save the session we want to switch to in seat->pending_switch and
* seat_complete_switch() will perform the final switch. */
s->seat->pending_switch = s;
/* if no devices are running, immediately perform the session switch */
if (!num_pending)
seat_complete_switch(s->seat);
return 0;
}
static int session_start_scope(Session *s) {
int r;
assert(s);
if (!s->scope) {
if (!description)
return log_oom();
if (!scope)
return log_oom();
r = manager_start_scope(s->manager, scope, s->leader, s->user->slice, description, "systemd-logind.service", "systemd-user-sessions.service", &error, &job);
if (r < 0) {
log_error("Failed to start session scope %s: %s %s",
return r;
} else {
}
}
if (s->scope)
return 0;
}
int session_start(Session *s) {
int r;
assert(s);
if (!s->user)
return -ESTALE;
if (s->started)
return 0;
r = user_start(s->user);
if (r < 0)
return r;
/* Create cgroup */
r = session_start_scope(s);
if (r < 0)
return r;
"SESSION_ID=%s", s->id,
NULL);
if (!dual_timestamp_is_set(&s->timestamp))
dual_timestamp_get(&s->timestamp);
if (s->seat)
seat_read_active_vt(s->seat);
s->started = true;
user_elect_display(s->user);
/* Save data */
session_save(s);
if (s->seat)
/* Send signals */
session_send_signal(s, true);
if (s->seat) {
else
}
return 0;
}
int r;
assert(s);
if (!s->scope)
return 0;
if (r < 0) {
return r;
}
} else {
if (r < 0) {
return r;
}
}
return 0;
}
int r;
assert(s);
if (!s->user)
return -ESTALE;
/* We are going down, don't care about FIFOs anymore */
/* Kill cgroup */
r = session_stop_scope(s, force);
s->stopping = true;
user_elect_display(s->user);
session_save(s);
return r;
}
int session_finalize(Session *s) {
int r = 0;
assert(s);
if (!s->user)
return -ESTALE;
if (s->started)
"SESSION_ID=%s", s->id,
NULL);
/* Kill session devices */
unlink(s->state_file);
user_add_to_gc_queue(s->user);
if (s->started) {
session_send_signal(s, false);
s->started = false;
}
if (s->seat) {
}
return r;
}
assert(s);
session_stop(s, false);
return 0;
}
int session_release(Session *s) {
assert(s);
return 0;
if (s->timer_event_source)
return 0;
&s->timer_event_source,
}
bool session_is_active(Session *s) {
assert(s);
if (!s->seat)
return true;
}
_cleanup_free_ char *p = NULL;
if (!path_is_absolute(tty)) {
if (!p)
return -ENOMEM;
tty = p;
return -ENOENT;
return -errno;
return 0;
}
_cleanup_free_ char *p = NULL;
int r;
if (r < 0)
return r;
return get_tty_atime(p, atime);
}
int r;
assert(s);
/* Explicit idle hint is set */
if (s->idle_hint) {
if (t)
*t = s->idle_hint_timestamp;
return s->idle_hint;
}
/* Graphical sessions should really implement a real
* idle hint logic */
if (s->display)
goto dont_know;
/* For sessions with an explicitly configured tty, let's check
* its atime */
if (s->tty) {
if (r >= 0)
goto found_atime;
}
/* For sessions with a leader but no explicitly configured
* tty, let's check the controlling tty of the leader */
if (s->leader > 0) {
if (r >= 0)
goto found_atime;
}
if (t)
*t = s->idle_hint_timestamp;
return 0;
if (t)
n = now(CLOCK_REALTIME);
if (s->manager->idle_action_usec <= 0)
return 0;
}
void session_set_idle_hint(Session *s, bool b) {
assert(s);
if (s->idle_hint == b)
return;
s->idle_hint = b;
if (s->seat)
}
assert(s);
/* EOF on the FIFO means the session died abnormally. */
session_stop(s, false);
return 1;
}
int session_create_fifo(Session *s) {
int r;
assert(s);
/* Create FIFO */
if (!s->fifo_path) {
if (r < 0)
return r;
return -ENOMEM;
return -errno;
}
/* Open reading side */
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_event_source, s->fifo_fd, 0, session_dispatch_fifo, s);
if (r < 0)
return r;
if (r < 0)
return r;
}
/* Open writing side */
if (r < 0)
return -errno;
return r;
}
static void session_remove_fifo(Session *s) {
assert(s);
if (s->fifo_path) {
}
}
assert(s);
if (drop_not_started && !s->started)
return false;
if (!s->user)
return false;
if (s->fifo_fd >= 0) {
return true;
}
return true;
return true;
return false;
}
void session_add_to_gc_queue(Session *s) {
assert(s);
if (s->in_gc_queue)
return;
s->in_gc_queue = true;
}
assert(s);
/* always check closing first */
if (s->stopping || s->timer_event_source)
return SESSION_CLOSING;
return SESSION_OPENING;
if (session_is_active(s))
return SESSION_ACTIVE;
return SESSION_ONLINE;
}
assert(s);
if (!s->scope)
return -ESRCH;
}
static int session_open_vt(Session *s) {
if (s->vtnr < 1)
return -ENODEV;
if (s->vtfd >= 0)
return s->vtfd;
if (s->vtfd < 0)
return s->vtfd;
}
int session_prepare_vt(Session *s) {
int vt, r;
if (s->vtnr < 1)
return 0;
vt = session_open_vt(s);
if (vt < 0)
return vt;
if (r < 0) {
r = -errno;
goto error;
}
if (r < 0) {
r = -errno;
goto error;
}
if (r < 0) {
r = -errno;
goto error;
}
/* Oh, thanks to the VT layer, VT_AUTO does not work with KD_GRAPHICS.
* So we need a dummy handler here which just acknowledges *all* VT
* switch requests. */
if (r < 0) {
r = -errno;
goto error;
}
return 0;
return r;
}
void session_restore_vt(Session *s) {
vt = session_open_vt(s);
if (vt < 0)
return;
}
void session_leave_vt(Session *s) {
int r;
assert(s);
/* This is called whenever we get a VT-switch signal from the kernel.
* We acknowledge all of them unconditionally. Note that session are
* free to overwrite those handlers and we only register them for
* sessions with controllers. Legacy sessions are not affected.
* However, if we switch from a non-legacy to a legacy session, we must
* make sure to pause all device before acknowledging the switch. We
* process the real switch only after we are notified via sysfs, so the
* legacy session might have already started using the devices. If we
* don't pause the devices before the switch, we might confuse the
* session we switch to. */
if (s->vtfd < 0)
return;
if (r < 0)
}
assert(s);
}
if (!s->controller)
return;
name = s->controller;
/* By resetting the controller before releasing the devices, we won't
* send notification signals. This avoids sending useless notifications
* if the controller is released on disconnects. */
if (!notify)
s->controller = NULL;
s->controller = NULL;
}
int r;
assert(s);
if (session_is_controller(s, sender))
return 0;
if (s->controller && !force)
return -EBUSY;
if (!name)
return -ENOMEM;
if (r)
return r;
/* When setting a session controller, we forcibly mute the VT and set
* it into graphics-mode. Applications can override that by changing
* VT state after calling TakeControl(). However, this serves as a good
* default and well-behaving controllers can now ignore VTs entirely.
* Note that we reset the VT on ReleaseControl() and if the controller
* exits.
r = session_prepare_vt(s);
if (r < 0) {
return r;
}
session_release_controller(s, true);
s->controller = name;
session_save(s);
return 0;
}
void session_drop_controller(Session *s) {
assert(s);
if (!s->controller)
return;
session_release_controller(s, false);
session_save(s);
}
static const char* const session_state_table[_SESSION_STATE_MAX] = {
[SESSION_OPENING] = "opening",
[SESSION_ONLINE] = "online",
[SESSION_ACTIVE] = "active",
[SESSION_CLOSING] = "closing"
};
static const char* const session_type_table[_SESSION_TYPE_MAX] = {
[SESSION_UNSPECIFIED] = "unspecified",
[SESSION_TTY] = "tty",
[SESSION_X11] = "x11",
[SESSION_WAYLAND] = "wayland",
[SESSION_MIR] = "mir",
[SESSION_WEB] = "web",
};
static const char* const session_class_table[_SESSION_CLASS_MAX] = {
[SESSION_USER] = "user",
[SESSION_GREETER] = "greeter",
[SESSION_LOCK_SCREEN] = "lock-screen",
[SESSION_BACKGROUND] = "background"
};
static const char* const kill_who_table[_KILL_WHO_MAX] = {
[KILL_LEADER] = "leader",
[KILL_ALL] = "all"
};