2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2013-11-28 15:10:24 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/kd.h>
|
2015-10-23 18:52:53 +02:00
|
|
|
#include <linux/vt.h>
|
2013-11-28 15:10:24 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ioctl.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <sys/stat.h>
|
2011-05-23 23:55:06 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
#include "sd-messages.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-26 23:32:16 +01:00
|
|
|
#include "audit-util.h"
|
2013-11-05 01:10:21 +01:00
|
|
|
#include "bus-error.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "bus-util.h"
|
2018-11-30 22:08:41 +01:00
|
|
|
#include "env-file.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "escape.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "fileio.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-10-25 14:08:25 +01:00
|
|
|
#include "io-util.h"
|
2019-04-30 15:05:14 +02:00
|
|
|
#include "logind-dbus.h"
|
|
|
|
#include "logind-seat-dbus.h"
|
|
|
|
#include "logind-session-dbus.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "logind-session.h"
|
2019-04-30 15:05:14 +02:00
|
|
|
#include "logind-user-dbus.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "mkdir.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "path-util.h"
|
2018-08-03 18:53:09 +02:00
|
|
|
#include "process-util.h"
|
2018-10-17 20:40:09 +02:00
|
|
|
#include "serialize.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
#include "string-table.h"
|
2018-08-06 21:44:45 +02:00
|
|
|
#include "strv.h"
|
2015-04-10 23:15:59 +02:00
|
|
|
#include "terminal-util.h"
|
2018-11-30 21:05:27 +01:00
|
|
|
#include "tmpfile-util.h"
|
2015-10-25 22:32:30 +01:00
|
|
|
#include "user-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "util.h"
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
#define RELEASE_USEC (20*USEC_PER_SEC)
|
|
|
|
|
|
|
|
static void session_remove_fifo(Session *s);
|
2018-08-23 18:03:04 +02:00
|
|
|
static void session_restore_vt(Session *s);
|
2014-02-06 18:32:14 +01:00
|
|
|
|
2018-08-03 18:53:09 +02:00
|
|
|
int session_new(Session **ret, Manager *m, const char *id) {
|
|
|
|
_cleanup_(session_freep) Session *s = NULL;
|
|
|
|
int r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-03 18:53:09 +02:00
|
|
|
assert(ret);
|
2011-05-23 23:55:06 +02:00
|
|
|
assert(m);
|
|
|
|
assert(id);
|
|
|
|
|
2018-08-03 18:53:09 +02:00
|
|
|
if (!session_id_valid(id))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
s = new(Session, 1);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (!s)
|
2018-08-03 18:53:09 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*s = (Session) {
|
|
|
|
.manager = m,
|
|
|
|
.fifo_fd = -1,
|
|
|
|
.vtfd = -1,
|
|
|
|
.audit_id = AUDIT_SESSION_INVALID,
|
2018-08-07 13:49:34 +02:00
|
|
|
.tty_validity = _TTY_VALIDITY_INVALID,
|
2018-08-03 18:53:09 +02:00
|
|
|
};
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2019-07-11 19:14:16 +02:00
|
|
|
s->state_file = path_join("/run/systemd/sessions", id);
|
2016-10-17 00:28:30 +02:00
|
|
|
if (!s->state_file)
|
2018-08-03 18:53:09 +02:00
|
|
|
return -ENOMEM;
|
logind: introduce session-devices
A session-device is a device that is bound to a seat and used by a
session-controller to run the session. This currently includes DRM, fbdev
and evdev devices. A session-device can be created via RequestDevice() on
the dbus API of the session. You can drop it via ReleaseDevice() again.
Once the session is destroyed or you drop control of the session, all
session-devices are automatically destroyed.
Session devices follow the session "active" state. A device can be
active/running or inactive/paused. Whenever a session is not the active
session, no session-device of it can be active. That is, if a session is
not in foreground, all session-devices are paused.
Whenever a session becomes active, all devices are resumed/activated by
logind. If it fails, a device may stay paused.
With every session-device you request, you also get a file-descriptor
back. logind keeps a copy of this fd and uses kernel specific calls to
pause/resume the file-descriptors. For example, a DRM fd is muted
by logind as long as a given session is not active. Hence, the fd of the
application is also muted. Once the session gets active, logind unmutes
the fd and the application will get DRM access again.
This, however, requires kernel support. DRM devices provide DRM-Master for
synchronization, evdev devices have EVIOCREVOKE (pending on
linux-input-ML). fbdev devices do not provide such synchronization methods
(and never will).
Note that for evdev devices, we call EVIOCREVOKE once a session gets
inactive. However, this cannot be undone (the fd is still valid but mostly
unusable). So we reopen a new fd once the session is activated and send it
together with the ResumeDevice() signal.
With this infrastructure in place, compositors can now run without
CAP_SYS_ADMIN (that is, without being root). They use RequestControl() to
acquire a session and listen for devices via udev_monitor. For every
device they want to open, they call RequestDevice() on logind. This
returns a fd which they can use now. They no longer have to open the
devices themselves or call any privileged ioctls. This is all done by
logind.
Session-switches are still bound to VTs. Hence, compositors will get
notified via the usual VT mechanisms and can cleanup their state. Once the
VT switch is acknowledged as usual, logind will get notified via sysfs and
pause the old-session's devices and resume the devices of the new session.
To allow using this infrastructure with systems without VTs, we provide
notification signals. logind sends PauseDevice("force") dbus signals to
the current session controller for every device that it pauses. And it
sends ResumeDevice signals for every device that it resumes. For
seats with VTs this is sent _after_ the VT switch is acknowledged. Because
the compositor already acknowledged that it cleaned-up all devices.
However, for seats without VTs, this is used to notify the active
compositor that the session is about to be deactivated. That is, logind
sends PauseDevice("force") for each active device and then performs the
session-switch. The session-switch changes the "Active" property of the
session which can be monitored by the compositor. The new session is
activated and the ResumeDevice events are sent.
For seats without VTs, this is a forced session-switch. As this is not
backwards-compatible (xserver actually crashes, weston drops the related
devices, ..) we also provide an acknowledged session-switch. Note that
this is never used for sessions with VTs. You use the acknowledged
VT-switch on these seats.
An acknowledged session switch sends PauseDevice("pause") instead of
PauseDevice("force") to the active session. It schedules a short timeout
and waits for the session to acknowledge each of them with
PauseDeviceComplete(). Once all are acknowledged, or the session ran out
of time, a PauseDevice("force") is sent for all remaining active devices
and the session switch is performed.
Note that this is only partially implemented, yet, as we don't allow
multi-session without VTs, yet. A follow up commit will hook it up and
implemented the acknowledgements+timeout.
The implementation is quite simple. We use major/minor exclusively to
identify devices on the bus. On RequestDevice() we retrieve the
udev_device from the major/minor and search for an existing "Device"
object. If no exists, we create it. This guarantees us that we are
notified whenever the device changes seats or is removed.
We create a new SessionDevice object and link it to the related Session
and Device. Session->devices is a hashtable to lookup SessionDevice
objects via major/minor. Device->session_devices is a linked list so we
can release all linked session-devices once a device vanishes.
Now we only have to hook this up in seat_set_active() so we correctly
change device states during session-switches. As mentioned earlier, these
are forced state-changes as VTs are currently used exclusively for
multi-session implementations.
Everything else are hooks to release all session-devices once the
controller changes or a session is closed or removed.
2013-09-17 23:39:04 +02:00
|
|
|
|
2013-12-07 03:29:55 +01:00
|
|
|
s->id = basename(s->state_file);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-03 18:53:09 +02:00
|
|
|
s->devices = hashmap_new(&devt_hash_ops);
|
|
|
|
if (!s->devices)
|
|
|
|
return -ENOMEM;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-03 18:53:09 +02:00
|
|
|
r = hashmap_put(m->sessions, s->id, s);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-03 18:53:09 +02:00
|
|
|
*ret = TAKE_PTR(s);
|
|
|
|
return 0;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 18:53:09 +02:00
|
|
|
Session* session_free(Session *s) {
|
logind: introduce session-devices
A session-device is a device that is bound to a seat and used by a
session-controller to run the session. This currently includes DRM, fbdev
and evdev devices. A session-device can be created via RequestDevice() on
the dbus API of the session. You can drop it via ReleaseDevice() again.
Once the session is destroyed or you drop control of the session, all
session-devices are automatically destroyed.
Session devices follow the session "active" state. A device can be
active/running or inactive/paused. Whenever a session is not the active
session, no session-device of it can be active. That is, if a session is
not in foreground, all session-devices are paused.
Whenever a session becomes active, all devices are resumed/activated by
logind. If it fails, a device may stay paused.
With every session-device you request, you also get a file-descriptor
back. logind keeps a copy of this fd and uses kernel specific calls to
pause/resume the file-descriptors. For example, a DRM fd is muted
by logind as long as a given session is not active. Hence, the fd of the
application is also muted. Once the session gets active, logind unmutes
the fd and the application will get DRM access again.
This, however, requires kernel support. DRM devices provide DRM-Master for
synchronization, evdev devices have EVIOCREVOKE (pending on
linux-input-ML). fbdev devices do not provide such synchronization methods
(and never will).
Note that for evdev devices, we call EVIOCREVOKE once a session gets
inactive. However, this cannot be undone (the fd is still valid but mostly
unusable). So we reopen a new fd once the session is activated and send it
together with the ResumeDevice() signal.
With this infrastructure in place, compositors can now run without
CAP_SYS_ADMIN (that is, without being root). They use RequestControl() to
acquire a session and listen for devices via udev_monitor. For every
device they want to open, they call RequestDevice() on logind. This
returns a fd which they can use now. They no longer have to open the
devices themselves or call any privileged ioctls. This is all done by
logind.
Session-switches are still bound to VTs. Hence, compositors will get
notified via the usual VT mechanisms and can cleanup their state. Once the
VT switch is acknowledged as usual, logind will get notified via sysfs and
pause the old-session's devices and resume the devices of the new session.
To allow using this infrastructure with systems without VTs, we provide
notification signals. logind sends PauseDevice("force") dbus signals to
the current session controller for every device that it pauses. And it
sends ResumeDevice signals for every device that it resumes. For
seats with VTs this is sent _after_ the VT switch is acknowledged. Because
the compositor already acknowledged that it cleaned-up all devices.
However, for seats without VTs, this is used to notify the active
compositor that the session is about to be deactivated. That is, logind
sends PauseDevice("force") for each active device and then performs the
session-switch. The session-switch changes the "Active" property of the
session which can be monitored by the compositor. The new session is
activated and the ResumeDevice events are sent.
For seats without VTs, this is a forced session-switch. As this is not
backwards-compatible (xserver actually crashes, weston drops the related
devices, ..) we also provide an acknowledged session-switch. Note that
this is never used for sessions with VTs. You use the acknowledged
VT-switch on these seats.
An acknowledged session switch sends PauseDevice("pause") instead of
PauseDevice("force") to the active session. It schedules a short timeout
and waits for the session to acknowledge each of them with
PauseDeviceComplete(). Once all are acknowledged, or the session ran out
of time, a PauseDevice("force") is sent for all remaining active devices
and the session switch is performed.
Note that this is only partially implemented, yet, as we don't allow
multi-session without VTs, yet. A follow up commit will hook it up and
implemented the acknowledgements+timeout.
The implementation is quite simple. We use major/minor exclusively to
identify devices on the bus. On RequestDevice() we retrieve the
udev_device from the major/minor and search for an existing "Device"
object. If no exists, we create it. This guarantees us that we are
notified whenever the device changes seats or is removed.
We create a new SessionDevice object and link it to the related Session
and Device. Session->devices is a hashtable to lookup SessionDevice
objects via major/minor. Device->session_devices is a linked list so we
can release all linked session-devices once a device vanishes.
Now we only have to hook this up in seat_set_active() so we correctly
change device states during session-switches. As mentioned earlier, these
are forced state-changes as VTs are currently used exclusively for
multi-session implementations.
Everything else are hooks to release all session-devices once the
controller changes or a session is closed or removed.
2013-09-17 23:39:04 +02:00
|
|
|
SessionDevice *sd;
|
|
|
|
|
2018-08-03 18:53:09 +02:00
|
|
|
if (!s)
|
|
|
|
return NULL;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
if (s->in_gc_queue)
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_REMOVE(gc_queue, s->manager->session_gc_queue, s);
|
2011-05-25 00:55:58 +02:00
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
s->timer_event_source = sd_event_source_unref(s->timer_event_source);
|
|
|
|
|
2013-09-17 17:39:56 +02:00
|
|
|
session_drop_controller(s);
|
|
|
|
|
logind: introduce session-devices
A session-device is a device that is bound to a seat and used by a
session-controller to run the session. This currently includes DRM, fbdev
and evdev devices. A session-device can be created via RequestDevice() on
the dbus API of the session. You can drop it via ReleaseDevice() again.
Once the session is destroyed or you drop control of the session, all
session-devices are automatically destroyed.
Session devices follow the session "active" state. A device can be
active/running or inactive/paused. Whenever a session is not the active
session, no session-device of it can be active. That is, if a session is
not in foreground, all session-devices are paused.
Whenever a session becomes active, all devices are resumed/activated by
logind. If it fails, a device may stay paused.
With every session-device you request, you also get a file-descriptor
back. logind keeps a copy of this fd and uses kernel specific calls to
pause/resume the file-descriptors. For example, a DRM fd is muted
by logind as long as a given session is not active. Hence, the fd of the
application is also muted. Once the session gets active, logind unmutes
the fd and the application will get DRM access again.
This, however, requires kernel support. DRM devices provide DRM-Master for
synchronization, evdev devices have EVIOCREVOKE (pending on
linux-input-ML). fbdev devices do not provide such synchronization methods
(and never will).
Note that for evdev devices, we call EVIOCREVOKE once a session gets
inactive. However, this cannot be undone (the fd is still valid but mostly
unusable). So we reopen a new fd once the session is activated and send it
together with the ResumeDevice() signal.
With this infrastructure in place, compositors can now run without
CAP_SYS_ADMIN (that is, without being root). They use RequestControl() to
acquire a session and listen for devices via udev_monitor. For every
device they want to open, they call RequestDevice() on logind. This
returns a fd which they can use now. They no longer have to open the
devices themselves or call any privileged ioctls. This is all done by
logind.
Session-switches are still bound to VTs. Hence, compositors will get
notified via the usual VT mechanisms and can cleanup their state. Once the
VT switch is acknowledged as usual, logind will get notified via sysfs and
pause the old-session's devices and resume the devices of the new session.
To allow using this infrastructure with systems without VTs, we provide
notification signals. logind sends PauseDevice("force") dbus signals to
the current session controller for every device that it pauses. And it
sends ResumeDevice signals for every device that it resumes. For
seats with VTs this is sent _after_ the VT switch is acknowledged. Because
the compositor already acknowledged that it cleaned-up all devices.
However, for seats without VTs, this is used to notify the active
compositor that the session is about to be deactivated. That is, logind
sends PauseDevice("force") for each active device and then performs the
session-switch. The session-switch changes the "Active" property of the
session which can be monitored by the compositor. The new session is
activated and the ResumeDevice events are sent.
For seats without VTs, this is a forced session-switch. As this is not
backwards-compatible (xserver actually crashes, weston drops the related
devices, ..) we also provide an acknowledged session-switch. Note that
this is never used for sessions with VTs. You use the acknowledged
VT-switch on these seats.
An acknowledged session switch sends PauseDevice("pause") instead of
PauseDevice("force") to the active session. It schedules a short timeout
and waits for the session to acknowledge each of them with
PauseDeviceComplete(). Once all are acknowledged, or the session ran out
of time, a PauseDevice("force") is sent for all remaining active devices
and the session switch is performed.
Note that this is only partially implemented, yet, as we don't allow
multi-session without VTs, yet. A follow up commit will hook it up and
implemented the acknowledgements+timeout.
The implementation is quite simple. We use major/minor exclusively to
identify devices on the bus. On RequestDevice() we retrieve the
udev_device from the major/minor and search for an existing "Device"
object. If no exists, we create it. This guarantees us that we are
notified whenever the device changes seats or is removed.
We create a new SessionDevice object and link it to the related Session
and Device. Session->devices is a hashtable to lookup SessionDevice
objects via major/minor. Device->session_devices is a linked list so we
can release all linked session-devices once a device vanishes.
Now we only have to hook this up in seat_set_active() so we correctly
change device states during session-switches. As mentioned earlier, these
are forced state-changes as VTs are currently used exclusively for
multi-session implementations.
Everything else are hooks to release all session-devices once the
controller changes or a session is closed or removed.
2013-09-17 23:39:04 +02:00
|
|
|
while ((sd = hashmap_first(s->devices)))
|
|
|
|
session_device_free(sd);
|
|
|
|
|
|
|
|
hashmap_free(s->devices);
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
if (s->user) {
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_REMOVE(sessions_by_user, s->user->sessions, s);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
if (s->user->display == s)
|
|
|
|
s->user->display = NULL;
|
2018-08-07 11:02:00 +02:00
|
|
|
|
|
|
|
user_update_last_session_timer(s->user);
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2011-06-21 21:46:13 +02:00
|
|
|
if (s->seat) {
|
|
|
|
if (s->seat->active == s)
|
|
|
|
s->seat->active = NULL;
|
2013-09-17 23:40:19 +02:00
|
|
|
if (s->seat->pending_switch == s)
|
|
|
|
s->seat->pending_switch = NULL;
|
2011-06-21 21:46:13 +02:00
|
|
|
|
logind: introduce session "positions"
logind has no concept of session ordering. Sessions have a unique name,
some attributes about the capabilities and that's already it. There is
currently no stable+total order on sessions. If we use the logind API to
switch between sessions, we are faced with an unordered list of sessions
we have no clue of.
This used to be no problem on seats with VTs or on seats with only a
single active session. However, with the introduction of multi-session
capability for seats without VTs, we need to find a way to order sessions
in a stable way.
This patch introduces session "positions". A position is a simple integer
assigned to a session which is never changed implicitly (currently, we
also don't change it explicitly, but that may be changed someday). For
seats with VTs, we force the position to be the same as the VTnr. Without
VTs, we simply find the lowest unassigned number and use it as position.
If position-assignment fails or if, for any reason, we decide to not
assign a position to a session, the position is set to 0 (which is treated
as invalid position).
During session_load() or if two sessions have the same VTnr, we may end up
with two sessions with the same position (this shouldn't happen, but lets
be fail-safe in case some other part of the stack fails). This case is
dealt with gracefully by ignoring any session but the first session
assigned to the position. Thus, session->pos is a hint, seat->positions[i]
is the definite position-assignment. Always verify both match in case you
need to modify them!
Additionally, we introduce SwitchTo(unsigned int) on the seat-dbus-API.
You can call it with any integer value != 0 and logind will try to switch
to the request position. If you implement a compositor or any other
session-controller, you simply watch for ctrl+alt+F1 to F12 and call
SwitchTo(Fx). logind will figure a way out deal with this number.
For convenience, we also introduce SwitchToNext/Previous(). It should be
called on ctrl+alt+Left/Right (like the kernel-console used to support).
Note that the public API (SwitchTo*()) is *not* bound to the underlying
logic that is implemented now. We don't export "session-positions" on the
dbus/C API! They are an implementation detail. Instead, the SwitchTo*()
API is supposed to be a hint to let logind choose the session-switching
logic. Any foreground session-controller is free to enumerate/order
existing sessions according to their needs and call Session.Activate()
manually. But the SwitchTo*() API provides a uniform behavior across
session-controllers.
Background: Session-switching keys depend on the active keymap. The XKB
specification provides the XKB_KEY_XF86Switch_VT_1-12 key-symbols which
have to be mapped by all keymaps to allow session-switching. It is usually
bound to ctrl+alt+Fx but may be set differently. A compositor passes any
keyboard input to XKB before passing it to clients. In case a key-press
invokes the XKB_KEY_XF86Switch_VT_x action, the keypress is *not*
forwarded to clients, but instead a session-switch is scheduled.
This actually prevents us from handling these keys outside of the session.
If an active compositor has a keymap with a different mapping of these
keys, and logind itself tries to catch these combinations, we end up with
the key-press sent to the compositor's clients *and* handled by logind.
This is *bad* and we must avoid this. The only situation where a
background process is allowed to handle key-presses is debugging and
emergency-keys. In these cases, we don't care for keymap mismatches and
accept the double-event. Another exception is unmapped keys like
PowerOff/Suspend (even though this one is controversial).
2013-11-30 11:39:48 +01:00
|
|
|
seat_evict_position(s->seat, s);
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_REMOVE(sessions_by_seat, s->seat->sessions, s);
|
2011-06-21 21:46:13 +02:00
|
|
|
}
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
if (s->scope) {
|
|
|
|
hashmap_remove(s->manager->session_units, s->scope);
|
|
|
|
free(s->scope);
|
|
|
|
}
|
|
|
|
|
2018-08-07 12:08:24 +02:00
|
|
|
if (pid_is_valid(s->leader))
|
|
|
|
(void) hashmap_remove_value(s->manager->sessions_by_leader, PID_TO_PTR(s->leader), s);
|
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
free(s->scope_job);
|
2011-06-24 20:41:56 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_bus_message_unref(s->create_message);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
free(s->tty);
|
|
|
|
free(s->display);
|
|
|
|
free(s->remote_host);
|
2011-05-26 02:21:16 +02:00
|
|
|
free(s->remote_user);
|
2011-06-24 18:50:50 +02:00
|
|
|
free(s->service);
|
2014-02-05 20:34:11 +01:00
|
|
|
free(s->desktop);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
hashmap_remove(s->manager->sessions, s->id);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2019-07-23 12:41:05 +02:00
|
|
|
sd_event_source_unref(s->fifo_event_source);
|
|
|
|
safe_close(s->fifo_fd);
|
|
|
|
|
|
|
|
/* Note that we remove neither the state file nor the fifo path here, since we want both to survive
|
|
|
|
* daemon restarts */
|
2011-05-25 00:58:55 +02:00
|
|
|
free(s->state_file);
|
2019-07-23 12:41:05 +02:00
|
|
|
free(s->fifo_path);
|
2018-08-03 18:53:09 +02:00
|
|
|
|
|
|
|
return mfree(s);
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
void session_set_user(Session *s, User *u) {
|
|
|
|
assert(s);
|
|
|
|
assert(!s->user);
|
|
|
|
|
|
|
|
s->user = u;
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_PREPEND(sessions_by_user, u->sessions, s);
|
2018-08-07 11:02:00 +02:00
|
|
|
|
|
|
|
user_update_last_session_timer(u);
|
2013-06-20 03:45:08 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 12:08:24 +02:00
|
|
|
int session_set_leader(Session *s, pid_t pid) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
if (!pid_is_valid(pid))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (s->leader == pid)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = hashmap_put(s->manager->sessions_by_leader, PID_TO_PTR(pid), s);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (pid_is_valid(s->leader))
|
|
|
|
(void) hashmap_remove_value(s->manager->sessions_by_leader, PID_TO_PTR(s->leader), s);
|
|
|
|
|
|
|
|
s->leader = pid;
|
|
|
|
(void) audit_session_from_pid(pid, &s->audit_id);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-16 11:17:09 +01:00
|
|
|
static void session_save_devices(Session *s, FILE *f) {
|
|
|
|
SessionDevice *sd;
|
|
|
|
|
|
|
|
if (!hashmap_isempty(s->devices)) {
|
|
|
|
fprintf(f, "DEVICES=");
|
2020-09-08 11:58:29 +02:00
|
|
|
HASHMAP_FOREACH(sd, s->devices)
|
2017-03-16 11:17:09 +01:00
|
|
|
fprintf(f, "%u:%u ", major(sd->dev), minor(sd->dev));
|
|
|
|
fprintf(f, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
int session_save(Session *s) {
|
2013-06-06 00:44:16 +02:00
|
|
|
_cleanup_free_ char *temp_path = NULL;
|
2013-11-05 01:10:21 +01:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2020-08-29 23:21:20 +02:00
|
|
|
int r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
if (!s->user)
|
|
|
|
return -ESTALE;
|
|
|
|
|
2011-06-27 23:06:34 +02:00
|
|
|
if (!s->started)
|
|
|
|
return 0;
|
|
|
|
|
tree-wide: warn when a directory path already exists but has bad mode/owner/type
When we are attempting to create directory somewhere in the bowels of /var/lib
and get an error that it already exists, it can be quite hard to diagnose what
is wrong (especially for a user who is not aware that the directory must have
the specified owner, and permissions not looser than what was requested). Let's
print a warning in most cases. A warning is appropriate, because such state is
usually a sign of borked installation and needs to be resolved by the adminstrator.
$ build/test-fs-util
Path "/tmp/test-readlink_and_make_absolute" already exists and is not a directory, refusing.
(or)
Directory "/tmp/test-readlink_and_make_absolute" already exists, but has mode 0775 that is too permissive (0755 was requested), refusing.
(or)
Directory "/tmp/test-readlink_and_make_absolute" already exists, but is owned by 1001:1000 (1000:1000 was requested), refusing.
Assertion 'mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0' failed at ../src/test/test-fs-util.c:320, function test_readlink_and_make_absolute(). Aborting.
No functional change except for the new log lines.
2018-03-22 13:03:41 +01:00
|
|
|
r = mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0, MKDIR_WARN_MODE);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (r < 0)
|
2015-07-29 20:31:07 +02:00
|
|
|
goto fail;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
r = fopen_temporary(s->state_file, &f, &temp_path);
|
|
|
|
if (r < 0)
|
2015-07-29 20:31:07 +02:00
|
|
|
goto fail;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-03 20:18:55 +02:00
|
|
|
(void) fchmod(fileno(f), 0644);
|
2011-05-25 00:55:58 +02:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
fprintf(f,
|
|
|
|
"# This is private data. Do not parse.\n"
|
2014-02-04 01:31:53 +01:00
|
|
|
"UID="UID_FMT"\n"
|
2011-05-23 23:55:06 +02:00
|
|
|
"USER=%s\n"
|
|
|
|
"ACTIVE=%i\n"
|
2018-08-03 19:04:35 +02:00
|
|
|
"IS_DISPLAY=%i\n"
|
2012-06-21 16:14:53 +02:00
|
|
|
"STATE=%s\n"
|
2013-07-02 01:46:30 +02:00
|
|
|
"REMOTE=%i\n",
|
2019-08-07 16:22:35 +02:00
|
|
|
s->user->user_record->uid,
|
|
|
|
s->user->user_record->user_name,
|
2011-05-23 23:55:06 +02:00
|
|
|
session_is_active(s),
|
2018-08-03 19:04:35 +02:00
|
|
|
s->user->display == s,
|
2012-06-21 16:14:53 +02:00
|
|
|
session_state_to_string(session_get_state(s)),
|
2013-07-02 01:46:30 +02:00
|
|
|
s->remote);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2011-06-24 23:50:39 +02:00
|
|
|
if (s->type >= 0)
|
2013-06-06 00:44:16 +02:00
|
|
|
fprintf(f, "TYPE=%s\n", session_type_to_string(s->type));
|
2011-06-24 23:50:39 +02:00
|
|
|
|
2020-02-23 04:44:42 +01:00
|
|
|
if (s->original_type >= 0)
|
|
|
|
fprintf(f, "ORIGINAL_TYPE=%s\n", session_type_to_string(s->original_type));
|
|
|
|
|
2012-02-14 21:33:51 +01:00
|
|
|
if (s->class >= 0)
|
2013-06-06 00:44:16 +02:00
|
|
|
fprintf(f, "CLASS=%s\n", session_class_to_string(s->class));
|
2012-02-14 21:33:51 +01:00
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
if (s->scope)
|
|
|
|
fprintf(f, "SCOPE=%s\n", s->scope);
|
|
|
|
if (s->scope_job)
|
|
|
|
fprintf(f, "SCOPE_JOB=%s\n", s->scope_job);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2011-06-29 00:06:04 +02:00
|
|
|
if (s->fifo_path)
|
2013-06-06 00:44:16 +02:00
|
|
|
fprintf(f, "FIFO=%s\n", s->fifo_path);
|
2011-06-29 00:06:04 +02:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
if (s->seat)
|
2013-06-06 00:44:16 +02:00
|
|
|
fprintf(f, "SEAT=%s\n", s->seat->id);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
if (s->tty)
|
2013-06-06 00:44:16 +02:00
|
|
|
fprintf(f, "TTY=%s\n", s->tty);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-07 13:49:34 +02:00
|
|
|
if (s->tty_validity >= 0)
|
|
|
|
fprintf(f, "TTY_VALIDITY=%s\n", tty_validity_to_string(s->tty_validity));
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
if (s->display)
|
2013-06-06 00:44:16 +02:00
|
|
|
fprintf(f, "DISPLAY=%s\n", s->display);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-05-22 03:35:28 +02:00
|
|
|
if (s->remote_host) {
|
|
|
|
_cleanup_free_ char *escaped;
|
|
|
|
|
|
|
|
escaped = cescape(s->remote_host);
|
|
|
|
if (!escaped) {
|
|
|
|
r = -ENOMEM;
|
2015-07-29 20:31:07 +02:00
|
|
|
goto fail;
|
2014-05-22 03:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "REMOTE_HOST=%s\n", escaped);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->remote_user) {
|
|
|
|
_cleanup_free_ char *escaped;
|
|
|
|
|
|
|
|
escaped = cescape(s->remote_user);
|
|
|
|
if (!escaped) {
|
|
|
|
r = -ENOMEM;
|
2015-07-29 20:31:07 +02:00
|
|
|
goto fail;
|
2014-05-22 03:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "REMOTE_USER=%s\n", escaped);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->service) {
|
|
|
|
_cleanup_free_ char *escaped;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-05-22 03:35:28 +02:00
|
|
|
escaped = cescape(s->service);
|
|
|
|
if (!escaped) {
|
|
|
|
r = -ENOMEM;
|
2015-07-29 20:31:07 +02:00
|
|
|
goto fail;
|
2014-05-22 03:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "SERVICE=%s\n", escaped);
|
|
|
|
}
|
2011-05-26 02:21:16 +02:00
|
|
|
|
2014-05-22 03:35:28 +02:00
|
|
|
if (s->desktop) {
|
|
|
|
_cleanup_free_ char *escaped;
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2014-05-22 03:35:28 +02:00
|
|
|
escaped = cescape(s->desktop);
|
|
|
|
if (!escaped) {
|
|
|
|
r = -ENOMEM;
|
2015-07-29 20:31:07 +02:00
|
|
|
goto fail;
|
2014-05-22 03:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "DESKTOP=%s\n", escaped);
|
|
|
|
}
|
2014-02-05 20:34:11 +01:00
|
|
|
|
2013-09-17 17:40:02 +02:00
|
|
|
if (s->seat && seat_has_vts(s->seat))
|
2013-11-28 17:05:34 +01:00
|
|
|
fprintf(f, "VTNR=%u\n", s->vtnr);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
logind: introduce session "positions"
logind has no concept of session ordering. Sessions have a unique name,
some attributes about the capabilities and that's already it. There is
currently no stable+total order on sessions. If we use the logind API to
switch between sessions, we are faced with an unordered list of sessions
we have no clue of.
This used to be no problem on seats with VTs or on seats with only a
single active session. However, with the introduction of multi-session
capability for seats without VTs, we need to find a way to order sessions
in a stable way.
This patch introduces session "positions". A position is a simple integer
assigned to a session which is never changed implicitly (currently, we
also don't change it explicitly, but that may be changed someday). For
seats with VTs, we force the position to be the same as the VTnr. Without
VTs, we simply find the lowest unassigned number and use it as position.
If position-assignment fails or if, for any reason, we decide to not
assign a position to a session, the position is set to 0 (which is treated
as invalid position).
During session_load() or if two sessions have the same VTnr, we may end up
with two sessions with the same position (this shouldn't happen, but lets
be fail-safe in case some other part of the stack fails). This case is
dealt with gracefully by ignoring any session but the first session
assigned to the position. Thus, session->pos is a hint, seat->positions[i]
is the definite position-assignment. Always verify both match in case you
need to modify them!
Additionally, we introduce SwitchTo(unsigned int) on the seat-dbus-API.
You can call it with any integer value != 0 and logind will try to switch
to the request position. If you implement a compositor or any other
session-controller, you simply watch for ctrl+alt+F1 to F12 and call
SwitchTo(Fx). logind will figure a way out deal with this number.
For convenience, we also introduce SwitchToNext/Previous(). It should be
called on ctrl+alt+Left/Right (like the kernel-console used to support).
Note that the public API (SwitchTo*()) is *not* bound to the underlying
logic that is implemented now. We don't export "session-positions" on the
dbus/C API! They are an implementation detail. Instead, the SwitchTo*()
API is supposed to be a hint to let logind choose the session-switching
logic. Any foreground session-controller is free to enumerate/order
existing sessions according to their needs and call Session.Activate()
manually. But the SwitchTo*() API provides a uniform behavior across
session-controllers.
Background: Session-switching keys depend on the active keymap. The XKB
specification provides the XKB_KEY_XF86Switch_VT_1-12 key-symbols which
have to be mapped by all keymaps to allow session-switching. It is usually
bound to ctrl+alt+Fx but may be set differently. A compositor passes any
keyboard input to XKB before passing it to clients. In case a key-press
invokes the XKB_KEY_XF86Switch_VT_x action, the keypress is *not*
forwarded to clients, but instead a session-switch is scheduled.
This actually prevents us from handling these keys outside of the session.
If an active compositor has a keymap with a different mapping of these
keys, and logind itself tries to catch these combinations, we end up with
the key-press sent to the compositor's clients *and* handled by logind.
This is *bad* and we must avoid this. The only situation where a
background process is allowed to handle key-presses is debugging and
emergency-keys. In these cases, we don't care for keymap mismatches and
accept the double-event. Another exception is unmapped keys like
PowerOff/Suspend (even though this one is controversial).
2013-11-30 11:39:48 +01:00
|
|
|
if (!s->vtnr)
|
2015-07-10 15:08:24 +02:00
|
|
|
fprintf(f, "POSITION=%u\n", s->position);
|
logind: introduce session "positions"
logind has no concept of session ordering. Sessions have a unique name,
some attributes about the capabilities and that's already it. There is
currently no stable+total order on sessions. If we use the logind API to
switch between sessions, we are faced with an unordered list of sessions
we have no clue of.
This used to be no problem on seats with VTs or on seats with only a
single active session. However, with the introduction of multi-session
capability for seats without VTs, we need to find a way to order sessions
in a stable way.
This patch introduces session "positions". A position is a simple integer
assigned to a session which is never changed implicitly (currently, we
also don't change it explicitly, but that may be changed someday). For
seats with VTs, we force the position to be the same as the VTnr. Without
VTs, we simply find the lowest unassigned number and use it as position.
If position-assignment fails or if, for any reason, we decide to not
assign a position to a session, the position is set to 0 (which is treated
as invalid position).
During session_load() or if two sessions have the same VTnr, we may end up
with two sessions with the same position (this shouldn't happen, but lets
be fail-safe in case some other part of the stack fails). This case is
dealt with gracefully by ignoring any session but the first session
assigned to the position. Thus, session->pos is a hint, seat->positions[i]
is the definite position-assignment. Always verify both match in case you
need to modify them!
Additionally, we introduce SwitchTo(unsigned int) on the seat-dbus-API.
You can call it with any integer value != 0 and logind will try to switch
to the request position. If you implement a compositor or any other
session-controller, you simply watch for ctrl+alt+F1 to F12 and call
SwitchTo(Fx). logind will figure a way out deal with this number.
For convenience, we also introduce SwitchToNext/Previous(). It should be
called on ctrl+alt+Left/Right (like the kernel-console used to support).
Note that the public API (SwitchTo*()) is *not* bound to the underlying
logic that is implemented now. We don't export "session-positions" on the
dbus/C API! They are an implementation detail. Instead, the SwitchTo*()
API is supposed to be a hint to let logind choose the session-switching
logic. Any foreground session-controller is free to enumerate/order
existing sessions according to their needs and call Session.Activate()
manually. But the SwitchTo*() API provides a uniform behavior across
session-controllers.
Background: Session-switching keys depend on the active keymap. The XKB
specification provides the XKB_KEY_XF86Switch_VT_1-12 key-symbols which
have to be mapped by all keymaps to allow session-switching. It is usually
bound to ctrl+alt+Fx but may be set differently. A compositor passes any
keyboard input to XKB before passing it to clients. In case a key-press
invokes the XKB_KEY_XF86Switch_VT_x action, the keypress is *not*
forwarded to clients, but instead a session-switch is scheduled.
This actually prevents us from handling these keys outside of the session.
If an active compositor has a keymap with a different mapping of these
keys, and logind itself tries to catch these combinations, we end up with
the key-press sent to the compositor's clients *and* handled by logind.
This is *bad* and we must avoid this. The only situation where a
background process is allowed to handle key-presses is debugging and
emergency-keys. In these cases, we don't care for keymap mismatches and
accept the double-event. Another exception is unmapped keys like
PowerOff/Suspend (even though this one is controversial).
2013-11-30 11:39:48 +01:00
|
|
|
|
2017-07-14 18:57:54 +02:00
|
|
|
if (pid_is_valid(s->leader))
|
2014-02-04 01:31:53 +01:00
|
|
|
fprintf(f, "LEADER="PID_FMT"\n", s->leader);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2017-07-14 18:42:17 +02:00
|
|
|
if (audit_session_is_valid(s->audit_id))
|
2013-06-06 00:44:16 +02:00
|
|
|
fprintf(f, "AUDIT=%"PRIu32"\n", s->audit_id);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
if (dual_timestamp_is_set(&s->timestamp))
|
|
|
|
fprintf(f,
|
2014-02-04 01:31:53 +01:00
|
|
|
"REALTIME="USEC_FMT"\n"
|
|
|
|
"MONOTONIC="USEC_FMT"\n",
|
|
|
|
s->timestamp.realtime,
|
|
|
|
s->timestamp.monotonic);
|
2013-06-20 03:45:08 +02:00
|
|
|
|
2017-03-16 11:17:09 +01:00
|
|
|
if (s->controller) {
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
fprintf(f, "CONTROLLER=%s\n", s->controller);
|
2017-03-16 11:17:09 +01:00
|
|
|
session_save_devices(s, f);
|
|
|
|
}
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
|
2015-07-29 20:31:07 +02:00
|
|
|
r = fflush_and_check(f);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
2011-05-25 00:55:58 +02:00
|
|
|
|
2015-07-29 20:31:07 +02:00
|
|
|
if (rename(temp_path, s->state_file) < 0) {
|
2011-05-23 23:55:06 +02:00
|
|
|
r = -errno;
|
2015-07-29 20:31:07 +02:00
|
|
|
goto fail;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2015-07-29 20:31:07 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
(void) unlink(s->state_file);
|
2011-05-25 00:55:58 +02:00
|
|
|
|
2015-07-29 20:31:07 +02:00
|
|
|
if (temp_path)
|
|
|
|
(void) unlink(temp_path);
|
|
|
|
|
|
|
|
return log_error_errno(r, "Failed to save session data %s: %m", s->state_file);
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2017-03-16 11:17:09 +01:00
|
|
|
static int session_load_devices(Session *s, const char *devices) {
|
|
|
|
const char *p;
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
for (p = devices;;) {
|
|
|
|
_cleanup_free_ char *word = NULL;
|
|
|
|
SessionDevice *sd;
|
|
|
|
dev_t dev;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
k = extract_first_word(&p, &word, NULL, 0);
|
|
|
|
if (k == 0)
|
|
|
|
break;
|
|
|
|
if (k < 0) {
|
|
|
|
r = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
k = parse_dev(word, &dev);
|
|
|
|
if (k < 0) {
|
|
|
|
r = k;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The file descriptors for loaded devices will be reattached later. */
|
|
|
|
k = session_device_new(s, dev, false, &sd);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Loading session devices for session %s failed: %m", s->id);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2015-07-29 20:31:07 +02:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
int session_load(Session *s) {
|
2013-06-20 03:45:08 +02:00
|
|
|
_cleanup_free_ char *remote = NULL,
|
2011-06-17 15:59:18 +02:00
|
|
|
*seat = NULL,
|
2018-08-07 13:49:34 +02:00
|
|
|
*tty_validity = NULL,
|
2011-06-17 15:59:18 +02:00
|
|
|
*vtnr = NULL,
|
2015-01-28 18:14:01 +01:00
|
|
|
*state = NULL,
|
2015-07-10 15:08:24 +02:00
|
|
|
*position = NULL,
|
2011-06-17 15:59:18 +02:00
|
|
|
*leader = NULL,
|
2012-02-14 21:33:51 +01:00
|
|
|
*type = NULL,
|
2020-02-23 04:44:42 +01:00
|
|
|
*original_type = NULL,
|
2013-06-20 03:45:08 +02:00
|
|
|
*class = NULL,
|
|
|
|
*uid = NULL,
|
|
|
|
*realtime = NULL,
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
*monotonic = NULL,
|
2017-03-16 11:17:09 +01:00
|
|
|
*controller = NULL,
|
|
|
|
*active = NULL,
|
2018-08-03 19:04:35 +02:00
|
|
|
*devices = NULL,
|
|
|
|
*is_display = NULL;
|
2011-06-17 15:59:18 +02:00
|
|
|
|
|
|
|
int k, r;
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
assert(s);
|
|
|
|
|
2018-11-12 14:04:47 +01:00
|
|
|
r = parse_env_file(NULL, s->state_file,
|
2011-06-17 15:59:18 +02:00
|
|
|
"REMOTE", &remote,
|
2013-07-02 01:46:30 +02:00
|
|
|
"SCOPE", &s->scope,
|
|
|
|
"SCOPE_JOB", &s->scope_job,
|
2011-06-29 00:06:04 +02:00
|
|
|
"FIFO", &s->fifo_path,
|
2011-06-17 15:59:18 +02:00
|
|
|
"SEAT", &seat,
|
|
|
|
"TTY", &s->tty,
|
2018-08-07 13:49:34 +02:00
|
|
|
"TTY_VALIDITY", &tty_validity,
|
2011-06-17 15:59:18 +02:00
|
|
|
"DISPLAY", &s->display,
|
|
|
|
"REMOTE_HOST", &s->remote_host,
|
|
|
|
"REMOTE_USER", &s->remote_user,
|
2011-06-24 18:50:50 +02:00
|
|
|
"SERVICE", &s->service,
|
2014-02-05 20:34:11 +01:00
|
|
|
"DESKTOP", &s->desktop,
|
2011-06-17 15:59:18 +02:00
|
|
|
"VTNR", &vtnr,
|
2015-01-28 18:14:01 +01:00
|
|
|
"STATE", &state,
|
2015-07-10 15:08:24 +02:00
|
|
|
"POSITION", &position,
|
2011-06-17 15:59:18 +02:00
|
|
|
"LEADER", &leader,
|
2011-06-24 23:50:39 +02:00
|
|
|
"TYPE", &type,
|
2020-02-23 04:44:42 +01:00
|
|
|
"ORIGINAL_TYPE", &original_type,
|
2012-02-14 21:33:51 +01:00
|
|
|
"CLASS", &class,
|
2013-06-20 03:45:08 +02:00
|
|
|
"UID", &uid,
|
|
|
|
"REALTIME", &realtime,
|
|
|
|
"MONOTONIC", &monotonic,
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
"CONTROLLER", &controller,
|
2017-03-16 11:17:09 +01:00
|
|
|
"ACTIVE", &active,
|
|
|
|
"DEVICES", &devices,
|
2018-11-12 14:18:03 +01:00
|
|
|
"IS_DISPLAY", &is_display);
|
2011-06-17 15:59:18 +02:00
|
|
|
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to read %s: %m", s->state_file);
|
2013-06-20 03:45:08 +02:00
|
|
|
|
|
|
|
if (!s->user) {
|
|
|
|
uid_t u;
|
|
|
|
User *user;
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!uid)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
|
|
|
|
"UID not specified for session %s",
|
|
|
|
s->id);
|
2013-06-20 03:45:08 +02:00
|
|
|
|
|
|
|
r = parse_uid(uid, &u);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to parse UID value %s for session %s.", uid, s->id);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-01-09 16:25:47 +01:00
|
|
|
user = hashmap_get(s->manager->users, UID_TO_PTR(u));
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!user)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
|
|
|
|
"User of session %s not known.",
|
|
|
|
s->id);
|
2013-06-20 03:45:08 +02:00
|
|
|
|
|
|
|
session_set_user(s, user);
|
|
|
|
}
|
2011-06-17 15:59:18 +02:00
|
|
|
|
|
|
|
if (remote) {
|
|
|
|
k = parse_boolean(remote);
|
|
|
|
if (k >= 0)
|
|
|
|
s->remote = k;
|
|
|
|
}
|
|
|
|
|
2013-11-28 17:25:25 +01:00
|
|
|
if (vtnr)
|
|
|
|
safe_atou(vtnr, &s->vtnr);
|
|
|
|
|
2011-06-21 21:46:13 +02:00
|
|
|
if (seat && !s->seat) {
|
2011-06-17 15:59:18 +02:00
|
|
|
Seat *o;
|
|
|
|
|
|
|
|
o = hashmap_get(s->manager->seats, seat);
|
|
|
|
if (o)
|
2013-11-28 17:25:25 +01:00
|
|
|
r = seat_attach_session(o, s);
|
|
|
|
if (!o || r < 0)
|
|
|
|
log_error("Cannot attach session %s to seat %s", s->id, seat);
|
2011-06-17 15:59:18 +02:00
|
|
|
}
|
|
|
|
|
2013-11-28 17:25:25 +01:00
|
|
|
if (!s->seat || !seat_has_vts(s->seat))
|
|
|
|
s->vtnr = 0;
|
2011-06-17 15:59:18 +02:00
|
|
|
|
2015-07-10 15:08:24 +02:00
|
|
|
if (position && s->seat) {
|
2018-10-19 20:00:46 +02:00
|
|
|
unsigned npos;
|
logind: introduce session "positions"
logind has no concept of session ordering. Sessions have a unique name,
some attributes about the capabilities and that's already it. There is
currently no stable+total order on sessions. If we use the logind API to
switch between sessions, we are faced with an unordered list of sessions
we have no clue of.
This used to be no problem on seats with VTs or on seats with only a
single active session. However, with the introduction of multi-session
capability for seats without VTs, we need to find a way to order sessions
in a stable way.
This patch introduces session "positions". A position is a simple integer
assigned to a session which is never changed implicitly (currently, we
also don't change it explicitly, but that may be changed someday). For
seats with VTs, we force the position to be the same as the VTnr. Without
VTs, we simply find the lowest unassigned number and use it as position.
If position-assignment fails or if, for any reason, we decide to not
assign a position to a session, the position is set to 0 (which is treated
as invalid position).
During session_load() or if two sessions have the same VTnr, we may end up
with two sessions with the same position (this shouldn't happen, but lets
be fail-safe in case some other part of the stack fails). This case is
dealt with gracefully by ignoring any session but the first session
assigned to the position. Thus, session->pos is a hint, seat->positions[i]
is the definite position-assignment. Always verify both match in case you
need to modify them!
Additionally, we introduce SwitchTo(unsigned int) on the seat-dbus-API.
You can call it with any integer value != 0 and logind will try to switch
to the request position. If you implement a compositor or any other
session-controller, you simply watch for ctrl+alt+F1 to F12 and call
SwitchTo(Fx). logind will figure a way out deal with this number.
For convenience, we also introduce SwitchToNext/Previous(). It should be
called on ctrl+alt+Left/Right (like the kernel-console used to support).
Note that the public API (SwitchTo*()) is *not* bound to the underlying
logic that is implemented now. We don't export "session-positions" on the
dbus/C API! They are an implementation detail. Instead, the SwitchTo*()
API is supposed to be a hint to let logind choose the session-switching
logic. Any foreground session-controller is free to enumerate/order
existing sessions according to their needs and call Session.Activate()
manually. But the SwitchTo*() API provides a uniform behavior across
session-controllers.
Background: Session-switching keys depend on the active keymap. The XKB
specification provides the XKB_KEY_XF86Switch_VT_1-12 key-symbols which
have to be mapped by all keymaps to allow session-switching. It is usually
bound to ctrl+alt+Fx but may be set differently. A compositor passes any
keyboard input to XKB before passing it to clients. In case a key-press
invokes the XKB_KEY_XF86Switch_VT_x action, the keypress is *not*
forwarded to clients, but instead a session-switch is scheduled.
This actually prevents us from handling these keys outside of the session.
If an active compositor has a keymap with a different mapping of these
keys, and logind itself tries to catch these combinations, we end up with
the key-press sent to the compositor's clients *and* handled by logind.
This is *bad* and we must avoid this. The only situation where a
background process is allowed to handle key-presses is debugging and
emergency-keys. In these cases, we don't care for keymap mismatches and
accept the double-event. Another exception is unmapped keys like
PowerOff/Suspend (even though this one is controversial).
2013-11-30 11:39:48 +01:00
|
|
|
|
2015-07-10 15:08:24 +02:00
|
|
|
safe_atou(position, &npos);
|
logind: introduce session "positions"
logind has no concept of session ordering. Sessions have a unique name,
some attributes about the capabilities and that's already it. There is
currently no stable+total order on sessions. If we use the logind API to
switch between sessions, we are faced with an unordered list of sessions
we have no clue of.
This used to be no problem on seats with VTs or on seats with only a
single active session. However, with the introduction of multi-session
capability for seats without VTs, we need to find a way to order sessions
in a stable way.
This patch introduces session "positions". A position is a simple integer
assigned to a session which is never changed implicitly (currently, we
also don't change it explicitly, but that may be changed someday). For
seats with VTs, we force the position to be the same as the VTnr. Without
VTs, we simply find the lowest unassigned number and use it as position.
If position-assignment fails or if, for any reason, we decide to not
assign a position to a session, the position is set to 0 (which is treated
as invalid position).
During session_load() or if two sessions have the same VTnr, we may end up
with two sessions with the same position (this shouldn't happen, but lets
be fail-safe in case some other part of the stack fails). This case is
dealt with gracefully by ignoring any session but the first session
assigned to the position. Thus, session->pos is a hint, seat->positions[i]
is the definite position-assignment. Always verify both match in case you
need to modify them!
Additionally, we introduce SwitchTo(unsigned int) on the seat-dbus-API.
You can call it with any integer value != 0 and logind will try to switch
to the request position. If you implement a compositor or any other
session-controller, you simply watch for ctrl+alt+F1 to F12 and call
SwitchTo(Fx). logind will figure a way out deal with this number.
For convenience, we also introduce SwitchToNext/Previous(). It should be
called on ctrl+alt+Left/Right (like the kernel-console used to support).
Note that the public API (SwitchTo*()) is *not* bound to the underlying
logic that is implemented now. We don't export "session-positions" on the
dbus/C API! They are an implementation detail. Instead, the SwitchTo*()
API is supposed to be a hint to let logind choose the session-switching
logic. Any foreground session-controller is free to enumerate/order
existing sessions according to their needs and call Session.Activate()
manually. But the SwitchTo*() API provides a uniform behavior across
session-controllers.
Background: Session-switching keys depend on the active keymap. The XKB
specification provides the XKB_KEY_XF86Switch_VT_1-12 key-symbols which
have to be mapped by all keymaps to allow session-switching. It is usually
bound to ctrl+alt+Fx but may be set differently. A compositor passes any
keyboard input to XKB before passing it to clients. In case a key-press
invokes the XKB_KEY_XF86Switch_VT_x action, the keypress is *not*
forwarded to clients, but instead a session-switch is scheduled.
This actually prevents us from handling these keys outside of the session.
If an active compositor has a keymap with a different mapping of these
keys, and logind itself tries to catch these combinations, we end up with
the key-press sent to the compositor's clients *and* handled by logind.
This is *bad* and we must avoid this. The only situation where a
background process is allowed to handle key-presses is debugging and
emergency-keys. In these cases, we don't care for keymap mismatches and
accept the double-event. Another exception is unmapped keys like
PowerOff/Suspend (even though this one is controversial).
2013-11-30 11:39:48 +01:00
|
|
|
seat_claim_position(s->seat, s, npos);
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:49:34 +02:00
|
|
|
if (tty_validity) {
|
|
|
|
TTYValidity v;
|
|
|
|
|
|
|
|
v = tty_validity_from_string(tty_validity);
|
|
|
|
if (v < 0)
|
|
|
|
log_debug("Failed to parse TTY validity: %s", tty_validity);
|
|
|
|
else
|
|
|
|
s->tty_validity = v;
|
|
|
|
}
|
|
|
|
|
2011-06-17 15:59:18 +02:00
|
|
|
if (leader) {
|
2018-08-07 12:08:24 +02:00
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
r = parse_pid(leader, &pid);
|
|
|
|
if (r < 0)
|
|
|
|
log_debug_errno(r, "Failed to parse leader PID of session: %s", leader);
|
|
|
|
else {
|
|
|
|
r = session_set_leader(s, pid);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning_errno(r, "Failed to set session leader PID, ignoring: %m");
|
|
|
|
}
|
2011-06-17 15:59:18 +02:00
|
|
|
}
|
|
|
|
|
2011-06-24 23:50:39 +02:00
|
|
|
if (type) {
|
|
|
|
SessionType t;
|
|
|
|
|
|
|
|
t = session_type_from_string(type);
|
|
|
|
if (t >= 0)
|
|
|
|
s->type = t;
|
|
|
|
}
|
|
|
|
|
2020-02-23 04:44:42 +01:00
|
|
|
if (original_type) {
|
|
|
|
SessionType ot;
|
|
|
|
|
|
|
|
ot = session_type_from_string(original_type);
|
|
|
|
if (ot >= 0)
|
|
|
|
s->original_type = ot;
|
|
|
|
} else
|
|
|
|
/* Pre-v246 compat: initialize original_type if not set in the state file */
|
|
|
|
s->original_type = s->type;
|
|
|
|
|
2012-02-14 21:33:51 +01:00
|
|
|
if (class) {
|
|
|
|
SessionClass c;
|
|
|
|
|
|
|
|
c = session_class_from_string(class);
|
|
|
|
if (c >= 0)
|
|
|
|
s->class = c;
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:14:01 +01:00
|
|
|
if (state && streq(state, "closing"))
|
|
|
|
s->stopping = true;
|
|
|
|
|
2011-06-29 03:48:53 +02:00
|
|
|
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
|
2015-01-28 18:14:01 +01:00
|
|
|
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. */
|
2011-06-29 03:48:53 +02:00
|
|
|
|
|
|
|
fd = session_create_fifo(s);
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(fd);
|
2011-06-29 03:48:53 +02:00
|
|
|
}
|
|
|
|
|
2016-02-15 23:26:34 +01:00
|
|
|
if (realtime)
|
2018-10-17 20:40:09 +02:00
|
|
|
(void) deserialize_usec(realtime, &s->timestamp.realtime);
|
2016-02-15 23:26:34 +01:00
|
|
|
if (monotonic)
|
2018-10-17 20:40:09 +02:00
|
|
|
(void) deserialize_usec(monotonic, &s->timestamp.monotonic);
|
2011-06-17 15:59:18 +02:00
|
|
|
|
2017-03-16 11:17:09 +01:00
|
|
|
if (active) {
|
|
|
|
k = parse_boolean(active);
|
|
|
|
if (k >= 0)
|
|
|
|
s->was_active = k;
|
|
|
|
}
|
|
|
|
|
2018-08-03 19:04:35 +02:00
|
|
|
if (is_display) {
|
|
|
|
/* Note that when enumerating users are loaded before sessions, hence the display session to use is
|
|
|
|
* something we have to store along with the session and not the user, as in that case we couldn't
|
|
|
|
* apply it at the time we load the user. */
|
|
|
|
|
|
|
|
k = parse_boolean(is_display);
|
|
|
|
if (k < 0)
|
|
|
|
log_warning_errno(k, "Failed to parse IS_DISPLAY session property: %m");
|
|
|
|
else if (k > 0)
|
|
|
|
s->user->display = s;
|
|
|
|
}
|
|
|
|
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
if (controller) {
|
2017-03-16 11:17:09 +01:00
|
|
|
if (bus_name_has_owner(s->manager->bus, controller, NULL) > 0) {
|
2017-04-26 14:20:41 +02:00
|
|
|
session_set_controller(s, controller, false, false);
|
2017-03-16 11:17:09 +01:00
|
|
|
session_load_devices(s, devices);
|
|
|
|
} else
|
2013-11-28 15:10:24 +01:00
|
|
|
session_restore_vt(s);
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
}
|
|
|
|
|
2011-06-17 15:59:18 +02:00
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int session_activate(Session *s) {
|
2018-10-19 20:00:46 +02:00
|
|
|
unsigned num_pending;
|
2013-09-17 23:40:19 +02:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
assert(s);
|
2013-06-20 03:45:08 +02:00
|
|
|
assert(s->user);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
if (!s->seat)
|
2015-03-13 14:08:00 +01:00
|
|
|
return -EOPNOTSUPP;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
if (s->seat->active == s)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-17 23:40:19 +02:00
|
|
|
/* on seats with VTs, we let VTs manage session-switching */
|
|
|
|
if (seat_has_vts(s->seat)) {
|
2018-08-06 19:05:26 +02:00
|
|
|
if (s->vtnr == 0)
|
2015-03-13 14:08:00 +01:00
|
|
|
return -EOPNOTSUPP;
|
2013-09-17 23:40:19 +02:00
|
|
|
|
|
|
|
return chvt(s->vtnr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
num_pending = session_device_try_pause_all(s);
|
|
|
|
if (!num_pending)
|
|
|
|
seat_complete_switch(s->seat);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2013-09-17 23:40:19 +02:00
|
|
|
return 0;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:44:45 +02:00
|
|
|
static int session_start_scope(Session *s, sd_bus_message *properties, sd_bus_error *error) {
|
2011-06-24 18:50:50 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(s);
|
2013-06-20 03:45:08 +02:00
|
|
|
assert(s->user);
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
if (!s->scope) {
|
2018-08-07 03:10:12 +02:00
|
|
|
_cleanup_free_ char *scope = NULL;
|
2015-11-13 18:25:02 +01:00
|
|
|
const char *description;
|
2013-08-13 17:59:28 +02:00
|
|
|
|
2018-08-06 21:44:45 +02:00
|
|
|
s->scope_job = mfree(s->scope_job);
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
scope = strjoin("session-", s->id, ".scope");
|
2013-07-02 17:17:35 +02:00
|
|
|
if (!scope)
|
2013-04-23 04:10:13 +02:00
|
|
|
return log_oom();
|
|
|
|
|
2019-08-07 16:22:35 +02:00
|
|
|
description = strjoina("Session ", s->id, " of user ", s->user->user_record->user_name);
|
2015-11-13 18:25:02 +01:00
|
|
|
|
|
|
|
r = manager_start_scope(
|
|
|
|
s->manager,
|
|
|
|
scope,
|
|
|
|
s->leader,
|
|
|
|
s->user->slice,
|
|
|
|
description,
|
2019-04-07 20:51:44 +02:00
|
|
|
/* These two have StopWhenUnneeded= set, hence add a dep towards them */
|
|
|
|
STRV_MAKE(s->user->runtime_dir_service,
|
|
|
|
s->user->service),
|
|
|
|
/* And order us after some more */
|
|
|
|
STRV_MAKE("systemd-logind.service",
|
|
|
|
"systemd-user-sessions.service",
|
|
|
|
s->user->runtime_dir_service,
|
|
|
|
s->user->service),
|
2019-08-07 16:22:35 +02:00
|
|
|
user_record_home_directory(s->user->user_record),
|
2018-04-17 16:42:44 +02:00
|
|
|
properties,
|
2018-08-06 21:44:45 +02:00
|
|
|
error,
|
|
|
|
&s->scope_job);
|
2018-08-07 03:10:12 +02:00
|
|
|
if (r < 0)
|
2019-04-07 20:51:44 +02:00
|
|
|
return log_error_errno(r, "Failed to start session scope %s: %s",
|
|
|
|
scope, bus_error_message(error, r));
|
2018-08-07 03:10:12 +02:00
|
|
|
|
|
|
|
s->scope = TAKE_PTR(scope);
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 20:51:44 +02:00
|
|
|
(void) hashmap_put(s->manager->session_units, s->scope, s);
|
2013-07-02 17:17:35 +02:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:44:45 +02:00
|
|
|
int session_start(Session *s, sd_bus_message *properties, sd_bus_error *error) {
|
2011-05-23 23:55:06 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(s);
|
2013-06-20 03:45:08 +02:00
|
|
|
|
|
|
|
if (!s->user)
|
|
|
|
return -ESTALE;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-06 21:44:45 +02:00
|
|
|
if (s->stopping)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2011-06-21 21:46:13 +02:00
|
|
|
if (s->started)
|
|
|
|
return 0;
|
|
|
|
|
2011-06-24 19:42:45 +02:00
|
|
|
r = user_start(s->user);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-08-06 21:44:45 +02:00
|
|
|
r = session_start_scope(s, properties, error);
|
2013-07-02 01:46:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-02-05 18:27:43 +01:00
|
|
|
log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO,
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SESSION_START_STR,
|
2012-08-24 22:21:20 +02:00
|
|
|
"SESSION_ID=%s", s->id,
|
2019-08-07 16:22:35 +02:00
|
|
|
"USER_ID=%s", s->user->user_record->user_name,
|
2014-04-25 13:45:15 +02:00
|
|
|
"LEADER="PID_FMT, s->leader,
|
2019-08-07 16:22:35 +02:00
|
|
|
LOG_MESSAGE("New session %s of user %s.", s->id, s->user->user_record->user_name));
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
if (!dual_timestamp_is_set(&s->timestamp))
|
|
|
|
dual_timestamp_get(&s->timestamp);
|
2011-05-25 00:55:58 +02:00
|
|
|
|
2011-06-27 23:07:13 +02:00
|
|
|
if (s->seat)
|
|
|
|
seat_read_active_vt(s->seat);
|
|
|
|
|
2011-06-21 21:46:13 +02:00
|
|
|
s->started = true;
|
|
|
|
|
2014-05-19 02:03:20 +02:00
|
|
|
user_elect_display(s->user);
|
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
/* Save data */
|
2011-06-27 23:07:13 +02:00
|
|
|
session_save(s);
|
2011-06-28 03:52:22 +02:00
|
|
|
user_save(s->user);
|
2014-02-06 18:32:14 +01:00
|
|
|
if (s->seat)
|
|
|
|
seat_save(s->seat);
|
2011-06-27 23:07:13 +02:00
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
/* Send signals */
|
2011-06-21 20:43:34 +02:00
|
|
|
session_send_signal(s, true);
|
2017-02-03 02:39:26 +01:00
|
|
|
user_send_changed(s->user, "Display", NULL);
|
2020-09-11 18:02:07 +02:00
|
|
|
|
|
|
|
if (s->seat && s->seat->active == s)
|
|
|
|
seat_send_changed(s->seat, "ActiveSession", NULL);
|
2011-06-21 21:46:13 +02:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:29:56 +01:00
|
|
|
static int session_stop_scope(Session *s, bool force) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2011-05-23 23:55:06 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
if (!s->scope)
|
|
|
|
return 0;
|
2012-01-31 23:51:16 +01:00
|
|
|
|
2016-07-20 13:42:36 +02:00
|
|
|
/* Let's always abandon the scope first. This tells systemd that we are not interested anymore, and everything
|
2016-10-02 19:37:21 +02:00
|
|
|
* that is left in the scope is "left-over". Informing systemd about this has the benefit that it will log
|
2016-07-20 13:42:36 +02:00
|
|
|
* when killing any processes left after this point. */
|
|
|
|
r = manager_abandon_scope(s->manager, s->scope, &error);
|
2018-08-06 21:44:45 +02:00
|
|
|
if (r < 0) {
|
2016-07-20 13:42:36 +02:00
|
|
|
log_warning_errno(r, "Failed to abandon session scope, ignoring: %s", bus_error_message(&error, r));
|
2018-08-06 21:44:45 +02:00
|
|
|
sd_bus_error_free(&error);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->scope_job = mfree(s->scope_job);
|
2016-07-20 13:42:36 +02:00
|
|
|
|
|
|
|
/* Optionally, let's kill everything that's left now. */
|
2019-08-09 13:46:25 +02:00
|
|
|
if (force ||
|
|
|
|
(s->user->user_record->kill_processes != 0 &&
|
|
|
|
(s->user->user_record->kill_processes > 0 ||
|
|
|
|
manager_shall_kill(s->manager, s->user->user_record->user_name)))) {
|
2016-07-20 11:14:18 +02:00
|
|
|
|
2020-09-11 18:06:59 +02:00
|
|
|
r = manager_stop_unit(s->manager, s->scope, force ? "replace" : "fail", &error, &s->scope_job);
|
2018-08-06 21:44:45 +02:00
|
|
|
if (r < 0) {
|
|
|
|
if (force)
|
|
|
|
return log_error_errno(r, "Failed to stop session scope: %s", bus_error_message(&error, r));
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-06 21:44:45 +02:00
|
|
|
log_warning_errno(r, "Failed to stop session scope, ignoring: %s", bus_error_message(&error, r));
|
|
|
|
}
|
2018-06-01 20:03:21 +02:00
|
|
|
} else {
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-06-01 20:03:21 +02:00
|
|
|
/* With no killing, this session is allowed to persist in "closing" state indefinitely.
|
|
|
|
* Therefore session stop and session removal may be two distinct events.
|
|
|
|
* Session stop is quite significant on its own, let's log it. */
|
|
|
|
log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO,
|
|
|
|
"SESSION_ID=%s", s->id,
|
2019-08-07 16:22:35 +02:00
|
|
|
"USER_ID=%s", s->user->user_record->user_name,
|
2018-06-01 20:03:21 +02:00
|
|
|
"LEADER="PID_FMT, s->leader,
|
2018-06-06 14:44:34 +02:00
|
|
|
LOG_MESSAGE("Session %s logged out. Waiting for processes to exit.", s->id));
|
2018-06-01 20:03:21 +02:00
|
|
|
}
|
|
|
|
|
2012-01-31 23:51:16 +01:00
|
|
|
return 0;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2014-02-09 02:29:56 +01:00
|
|
|
int session_stop(Session *s, bool force) {
|
2013-08-13 17:59:28 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
2018-08-06 21:44:45 +02:00
|
|
|
/* This is called whenever we begin with tearing down a session record. It's called in four cases: explicit API
|
|
|
|
* request via the bus (either directly for the session object or for the seat or user object this session
|
|
|
|
* belongs to; 'force' is true), or due to automatic GC (i.e. scope vanished; 'force' is false), or because the
|
|
|
|
* session FIFO saw an EOF ('force' is false), or because the release timer hit ('force' is false). */
|
|
|
|
|
2013-08-13 17:59:28 +02:00
|
|
|
if (!s->user)
|
|
|
|
return -ESTALE;
|
2018-08-06 21:44:45 +02:00
|
|
|
if (!s->started)
|
|
|
|
return 0;
|
|
|
|
if (s->stopping)
|
|
|
|
return 0;
|
2013-08-13 17:59:28 +02:00
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
s->timer_event_source = sd_event_source_unref(s->timer_event_source);
|
|
|
|
|
2015-07-31 16:52:29 +02:00
|
|
|
if (s->seat)
|
|
|
|
seat_evict_position(s->seat, s);
|
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
/* We are going down, don't care about FIFOs anymore */
|
|
|
|
session_remove_fifo(s);
|
|
|
|
|
2013-08-13 17:59:28 +02:00
|
|
|
/* Kill cgroup */
|
2014-02-09 02:29:56 +01:00
|
|
|
r = session_stop_scope(s, force);
|
2013-08-13 17:59:28 +02:00
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
s->stopping = true;
|
|
|
|
|
2014-05-19 02:03:20 +02:00
|
|
|
user_elect_display(s->user);
|
|
|
|
|
2013-08-13 17:59:28 +02:00
|
|
|
session_save(s);
|
2013-11-05 01:10:21 +01:00
|
|
|
user_save(s->user);
|
2013-08-13 17:59:28 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_finalize(Session *s) {
|
logind: introduce session-devices
A session-device is a device that is bound to a seat and used by a
session-controller to run the session. This currently includes DRM, fbdev
and evdev devices. A session-device can be created via RequestDevice() on
the dbus API of the session. You can drop it via ReleaseDevice() again.
Once the session is destroyed or you drop control of the session, all
session-devices are automatically destroyed.
Session devices follow the session "active" state. A device can be
active/running or inactive/paused. Whenever a session is not the active
session, no session-device of it can be active. That is, if a session is
not in foreground, all session-devices are paused.
Whenever a session becomes active, all devices are resumed/activated by
logind. If it fails, a device may stay paused.
With every session-device you request, you also get a file-descriptor
back. logind keeps a copy of this fd and uses kernel specific calls to
pause/resume the file-descriptors. For example, a DRM fd is muted
by logind as long as a given session is not active. Hence, the fd of the
application is also muted. Once the session gets active, logind unmutes
the fd and the application will get DRM access again.
This, however, requires kernel support. DRM devices provide DRM-Master for
synchronization, evdev devices have EVIOCREVOKE (pending on
linux-input-ML). fbdev devices do not provide such synchronization methods
(and never will).
Note that for evdev devices, we call EVIOCREVOKE once a session gets
inactive. However, this cannot be undone (the fd is still valid but mostly
unusable). So we reopen a new fd once the session is activated and send it
together with the ResumeDevice() signal.
With this infrastructure in place, compositors can now run without
CAP_SYS_ADMIN (that is, without being root). They use RequestControl() to
acquire a session and listen for devices via udev_monitor. For every
device they want to open, they call RequestDevice() on logind. This
returns a fd which they can use now. They no longer have to open the
devices themselves or call any privileged ioctls. This is all done by
logind.
Session-switches are still bound to VTs. Hence, compositors will get
notified via the usual VT mechanisms and can cleanup their state. Once the
VT switch is acknowledged as usual, logind will get notified via sysfs and
pause the old-session's devices and resume the devices of the new session.
To allow using this infrastructure with systems without VTs, we provide
notification signals. logind sends PauseDevice("force") dbus signals to
the current session controller for every device that it pauses. And it
sends ResumeDevice signals for every device that it resumes. For
seats with VTs this is sent _after_ the VT switch is acknowledged. Because
the compositor already acknowledged that it cleaned-up all devices.
However, for seats without VTs, this is used to notify the active
compositor that the session is about to be deactivated. That is, logind
sends PauseDevice("force") for each active device and then performs the
session-switch. The session-switch changes the "Active" property of the
session which can be monitored by the compositor. The new session is
activated and the ResumeDevice events are sent.
For seats without VTs, this is a forced session-switch. As this is not
backwards-compatible (xserver actually crashes, weston drops the related
devices, ..) we also provide an acknowledged session-switch. Note that
this is never used for sessions with VTs. You use the acknowledged
VT-switch on these seats.
An acknowledged session switch sends PauseDevice("pause") instead of
PauseDevice("force") to the active session. It schedules a short timeout
and waits for the session to acknowledge each of them with
PauseDeviceComplete(). Once all are acknowledged, or the session ran out
of time, a PauseDevice("force") is sent for all remaining active devices
and the session switch is performed.
Note that this is only partially implemented, yet, as we don't allow
multi-session without VTs, yet. A follow up commit will hook it up and
implemented the acknowledgements+timeout.
The implementation is quite simple. We use major/minor exclusively to
identify devices on the bus. On RequestDevice() we retrieve the
udev_device from the major/minor and search for an existing "Device"
object. If no exists, we create it. This guarantees us that we are
notified whenever the device changes seats or is removed.
We create a new SessionDevice object and link it to the related Session
and Device. Session->devices is a hashtable to lookup SessionDevice
objects via major/minor. Device->session_devices is a linked list so we
can release all linked session-devices once a device vanishes.
Now we only have to hook this up in seat_set_active() so we correctly
change device states during session-switches. As mentioned earlier, these
are forced state-changes as VTs are currently used exclusively for
multi-session implementations.
Everything else are hooks to release all session-devices once the
controller changes or a session is closed or removed.
2013-09-17 23:39:04 +02:00
|
|
|
SessionDevice *sd;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
if (!s->user)
|
|
|
|
return -ESTALE;
|
|
|
|
|
2011-06-24 19:42:45 +02:00
|
|
|
if (s->started)
|
2014-02-05 18:27:43 +01:00
|
|
|
log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO,
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SESSION_STOP_STR,
|
2012-08-24 22:21:20 +02:00
|
|
|
"SESSION_ID=%s", s->id,
|
2019-08-07 16:22:35 +02:00
|
|
|
"USER_ID=%s", s->user->user_record->user_name,
|
2014-04-25 13:45:15 +02:00
|
|
|
"LEADER="PID_FMT, s->leader,
|
2018-06-04 12:59:22 +02:00
|
|
|
LOG_MESSAGE("Removed session %s.", s->id));
|
2011-06-24 18:50:50 +02:00
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
s->timer_event_source = sd_event_source_unref(s->timer_event_source);
|
|
|
|
|
2015-07-31 16:52:29 +02:00
|
|
|
if (s->seat)
|
|
|
|
seat_evict_position(s->seat, s);
|
|
|
|
|
logind: introduce session-devices
A session-device is a device that is bound to a seat and used by a
session-controller to run the session. This currently includes DRM, fbdev
and evdev devices. A session-device can be created via RequestDevice() on
the dbus API of the session. You can drop it via ReleaseDevice() again.
Once the session is destroyed or you drop control of the session, all
session-devices are automatically destroyed.
Session devices follow the session "active" state. A device can be
active/running or inactive/paused. Whenever a session is not the active
session, no session-device of it can be active. That is, if a session is
not in foreground, all session-devices are paused.
Whenever a session becomes active, all devices are resumed/activated by
logind. If it fails, a device may stay paused.
With every session-device you request, you also get a file-descriptor
back. logind keeps a copy of this fd and uses kernel specific calls to
pause/resume the file-descriptors. For example, a DRM fd is muted
by logind as long as a given session is not active. Hence, the fd of the
application is also muted. Once the session gets active, logind unmutes
the fd and the application will get DRM access again.
This, however, requires kernel support. DRM devices provide DRM-Master for
synchronization, evdev devices have EVIOCREVOKE (pending on
linux-input-ML). fbdev devices do not provide such synchronization methods
(and never will).
Note that for evdev devices, we call EVIOCREVOKE once a session gets
inactive. However, this cannot be undone (the fd is still valid but mostly
unusable). So we reopen a new fd once the session is activated and send it
together with the ResumeDevice() signal.
With this infrastructure in place, compositors can now run without
CAP_SYS_ADMIN (that is, without being root). They use RequestControl() to
acquire a session and listen for devices via udev_monitor. For every
device they want to open, they call RequestDevice() on logind. This
returns a fd which they can use now. They no longer have to open the
devices themselves or call any privileged ioctls. This is all done by
logind.
Session-switches are still bound to VTs. Hence, compositors will get
notified via the usual VT mechanisms and can cleanup their state. Once the
VT switch is acknowledged as usual, logind will get notified via sysfs and
pause the old-session's devices and resume the devices of the new session.
To allow using this infrastructure with systems without VTs, we provide
notification signals. logind sends PauseDevice("force") dbus signals to
the current session controller for every device that it pauses. And it
sends ResumeDevice signals for every device that it resumes. For
seats with VTs this is sent _after_ the VT switch is acknowledged. Because
the compositor already acknowledged that it cleaned-up all devices.
However, for seats without VTs, this is used to notify the active
compositor that the session is about to be deactivated. That is, logind
sends PauseDevice("force") for each active device and then performs the
session-switch. The session-switch changes the "Active" property of the
session which can be monitored by the compositor. The new session is
activated and the ResumeDevice events are sent.
For seats without VTs, this is a forced session-switch. As this is not
backwards-compatible (xserver actually crashes, weston drops the related
devices, ..) we also provide an acknowledged session-switch. Note that
this is never used for sessions with VTs. You use the acknowledged
VT-switch on these seats.
An acknowledged session switch sends PauseDevice("pause") instead of
PauseDevice("force") to the active session. It schedules a short timeout
and waits for the session to acknowledge each of them with
PauseDeviceComplete(). Once all are acknowledged, or the session ran out
of time, a PauseDevice("force") is sent for all remaining active devices
and the session switch is performed.
Note that this is only partially implemented, yet, as we don't allow
multi-session without VTs, yet. A follow up commit will hook it up and
implemented the acknowledgements+timeout.
The implementation is quite simple. We use major/minor exclusively to
identify devices on the bus. On RequestDevice() we retrieve the
udev_device from the major/minor and search for an existing "Device"
object. If no exists, we create it. This guarantees us that we are
notified whenever the device changes seats or is removed.
We create a new SessionDevice object and link it to the related Session
and Device. Session->devices is a hashtable to lookup SessionDevice
objects via major/minor. Device->session_devices is a linked list so we
can release all linked session-devices once a device vanishes.
Now we only have to hook this up in seat_set_active() so we correctly
change device states during session-switches. As mentioned earlier, these
are forced state-changes as VTs are currently used exclusively for
multi-session implementations.
Everything else are hooks to release all session-devices once the
controller changes or a session is closed or removed.
2013-09-17 23:39:04 +02:00
|
|
|
/* Kill session devices */
|
|
|
|
while ((sd = hashmap_first(s->devices)))
|
|
|
|
session_device_free(sd);
|
|
|
|
|
2015-08-06 15:48:17 +02:00
|
|
|
(void) unlink(s->state_file);
|
2011-05-25 00:58:55 +02:00
|
|
|
session_add_to_gc_queue(s);
|
2011-06-24 19:42:45 +02:00
|
|
|
user_add_to_gc_queue(s->user);
|
2011-05-25 00:55:58 +02:00
|
|
|
|
2013-08-13 17:59:28 +02:00
|
|
|
if (s->started) {
|
2011-06-24 19:42:45 +02:00
|
|
|
session_send_signal(s, false);
|
2013-08-13 17:59:28 +02:00
|
|
|
s->started = false;
|
|
|
|
}
|
2013-04-10 09:49:24 +02:00
|
|
|
|
2011-06-21 21:46:13 +02:00
|
|
|
if (s->seat) {
|
|
|
|
if (s->seat->active == s)
|
|
|
|
seat_set_active(s->seat, NULL);
|
|
|
|
|
2012-09-04 02:37:27 +02:00
|
|
|
seat_save(s->seat);
|
2011-06-21 21:46:13 +02:00
|
|
|
}
|
|
|
|
|
2012-09-04 02:37:27 +02:00
|
|
|
user_save(s->user);
|
2017-02-03 02:39:26 +01:00
|
|
|
user_send_changed(s->user, "Display", NULL);
|
2011-06-21 21:46:13 +02:00
|
|
|
|
2015-08-06 15:48:17 +02:00
|
|
|
return 0;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
static int release_timeout_callback(sd_event_source *es, uint64_t usec, void *userdata) {
|
|
|
|
Session *s = userdata;
|
|
|
|
|
|
|
|
assert(es);
|
|
|
|
assert(s);
|
|
|
|
|
2020-09-11 17:59:39 +02:00
|
|
|
session_stop(s, /* force = */ false);
|
2014-02-06 18:32:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:37:39 +01:00
|
|
|
int session_release(Session *s) {
|
2014-02-06 18:32:14 +01:00
|
|
|
assert(s);
|
|
|
|
|
|
|
|
if (!s->started || s->stopping)
|
2015-03-15 21:37:39 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (s->timer_event_source)
|
|
|
|
return 0;
|
|
|
|
|
2020-07-28 11:18:26 +02:00
|
|
|
return sd_event_add_time_relative(
|
|
|
|
s->manager->event,
|
|
|
|
&s->timer_event_source,
|
|
|
|
CLOCK_MONOTONIC,
|
|
|
|
RELEASE_USEC, 0,
|
|
|
|
release_timeout_callback, s);
|
2014-02-06 18:32:14 +01:00
|
|
|
}
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
bool session_is_active(Session *s) {
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
if (!s->seat)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return s->seat->active == s;
|
|
|
|
}
|
|
|
|
|
2012-12-23 22:32:48 +01:00
|
|
|
static int get_tty_atime(const char *tty, usec_t *atime) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
2011-06-17 15:59:18 +02:00
|
|
|
struct stat st;
|
2012-12-23 22:32:48 +01:00
|
|
|
|
|
|
|
assert(tty);
|
|
|
|
assert(atime);
|
|
|
|
|
|
|
|
if (!path_is_absolute(tty)) {
|
2019-07-11 19:14:16 +02:00
|
|
|
p = path_join("/dev", tty);
|
2012-12-23 22:32:48 +01:00
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
tty = p;
|
|
|
|
} else if (!path_startswith(tty, "/dev/"))
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if (lstat(tty, &st) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
*atime = timespec_load(&st.st_atim);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_process_ctty_atime(pid_t pid, usec_t *atime) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(pid > 0);
|
|
|
|
assert(atime);
|
|
|
|
|
|
|
|
r = get_ctty(pid, NULL, &p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return get_tty_atime(p, atime);
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_get_idle_hint(Session *s, dual_timestamp *t) {
|
2020-01-06 20:13:16 +01:00
|
|
|
usec_t atime = 0;
|
2012-12-23 22:32:48 +01:00
|
|
|
int r;
|
2011-06-17 15:59:18 +02:00
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
2020-01-06 20:13:16 +01:00
|
|
|
/* Graphical sessions have an explicit idle hint */
|
|
|
|
if (SESSION_TYPE_IS_GRAPHICAL(s->type)) {
|
2011-06-17 15:59:18 +02:00
|
|
|
if (t)
|
|
|
|
*t = s->idle_hint_timestamp;
|
|
|
|
|
|
|
|
return s->idle_hint;
|
|
|
|
}
|
|
|
|
|
2020-01-06 20:13:16 +01:00
|
|
|
/* For sessions with an explicitly configured tty, let's check its atime */
|
2012-12-23 22:32:48 +01:00
|
|
|
if (s->tty) {
|
|
|
|
r = get_tty_atime(s->tty, &atime);
|
|
|
|
if (r >= 0)
|
|
|
|
goto found_atime;
|
|
|
|
}
|
2011-06-17 15:59:18 +02:00
|
|
|
|
2020-01-06 20:13:16 +01:00
|
|
|
/* For sessions with a leader but no explicitly configured tty, let's check the controlling tty of
|
|
|
|
* the leader */
|
2018-08-07 12:08:24 +02:00
|
|
|
if (pid_is_valid(s->leader)) {
|
2012-12-23 22:32:48 +01:00
|
|
|
r = get_process_ctty_atime(s->leader, &atime);
|
|
|
|
if (r >= 0)
|
|
|
|
goto found_atime;
|
2011-06-17 15:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (t)
|
2020-01-06 20:13:16 +01:00
|
|
|
*t = DUAL_TIMESTAMP_NULL;
|
2011-06-17 15:59:18 +02:00
|
|
|
|
2020-01-06 20:13:16 +01:00
|
|
|
return false;
|
2012-12-23 22:32:48 +01:00
|
|
|
|
|
|
|
found_atime:
|
|
|
|
if (t)
|
|
|
|
dual_timestamp_from_realtime(t, atime);
|
|
|
|
|
|
|
|
if (s->manager->idle_action_usec <= 0)
|
2020-01-06 20:13:16 +01:00
|
|
|
return false;
|
2012-12-23 22:32:48 +01:00
|
|
|
|
2020-01-06 20:13:16 +01:00
|
|
|
return usec_add(atime, s->manager->idle_action_usec) <= now(CLOCK_REALTIME);
|
2011-06-17 15:59:18 +02:00
|
|
|
}
|
|
|
|
|
2020-01-06 20:13:16 +01:00
|
|
|
int session_set_idle_hint(Session *s, bool b) {
|
2011-06-21 19:20:05 +02:00
|
|
|
assert(s);
|
|
|
|
|
2020-01-06 20:13:16 +01:00
|
|
|
if (!SESSION_TYPE_IS_GRAPHICAL(s->type))
|
|
|
|
return -ENOTTY;
|
|
|
|
|
2011-06-21 19:20:05 +02:00
|
|
|
if (s->idle_hint == b)
|
2020-01-06 20:13:16 +01:00
|
|
|
return 0;
|
2011-06-21 19:20:05 +02:00
|
|
|
|
|
|
|
s->idle_hint = b;
|
|
|
|
dual_timestamp_get(&s->idle_hint_timestamp);
|
2011-06-21 21:46:13 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
session_send_changed(s, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic", NULL);
|
2011-06-21 21:46:13 +02:00
|
|
|
|
|
|
|
if (s->seat)
|
2013-11-05 01:10:21 +01:00
|
|
|
seat_send_changed(s->seat, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic", NULL);
|
|
|
|
|
|
|
|
user_send_changed(s->user, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic", NULL);
|
|
|
|
manager_send_changed(s->manager, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic", NULL);
|
2020-01-06 20:13:16 +01:00
|
|
|
|
|
|
|
return 1;
|
2013-11-05 01:10:21 +01:00
|
|
|
}
|
|
|
|
|
2016-05-11 19:34:13 +02:00
|
|
|
int session_get_locked_hint(Session *s) {
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
return s->locked_hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_set_locked_hint(Session *s, bool b) {
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
if (s->locked_hint == b)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s->locked_hint = b;
|
|
|
|
|
|
|
|
session_send_changed(s, "LockedHint", NULL);
|
|
|
|
}
|
|
|
|
|
2020-02-23 04:44:42 +01:00
|
|
|
void session_set_type(Session *s, SessionType t) {
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
if (s->type == t)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s->type = t;
|
|
|
|
session_save(s);
|
|
|
|
|
|
|
|
session_send_changed(s, "Type", NULL);
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int session_dispatch_fifo(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
|
|
|
|
Session *s = userdata;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
assert(s->fifo_fd == fd);
|
|
|
|
|
|
|
|
/* EOF on the FIFO means the session died abnormally. */
|
|
|
|
|
|
|
|
session_remove_fifo(s);
|
2020-09-11 17:59:39 +02:00
|
|
|
session_stop(s, /* force = */ false);
|
2013-11-05 01:10:21 +01:00
|
|
|
|
|
|
|
return 1;
|
2011-06-21 19:20:05 +02:00
|
|
|
}
|
|
|
|
|
2011-06-29 00:06:04 +02:00
|
|
|
int session_create_fifo(Session *s) {
|
|
|
|
int r;
|
|
|
|
|
2011-06-24 23:25:28 +02:00
|
|
|
assert(s);
|
|
|
|
|
2011-06-29 03:48:53 +02:00
|
|
|
/* Create FIFO */
|
2011-06-29 00:06:04 +02:00
|
|
|
if (!s->fifo_path) {
|
tree-wide: warn when a directory path already exists but has bad mode/owner/type
When we are attempting to create directory somewhere in the bowels of /var/lib
and get an error that it already exists, it can be quite hard to diagnose what
is wrong (especially for a user who is not aware that the directory must have
the specified owner, and permissions not looser than what was requested). Let's
print a warning in most cases. A warning is appropriate, because such state is
usually a sign of borked installation and needs to be resolved by the adminstrator.
$ build/test-fs-util
Path "/tmp/test-readlink_and_make_absolute" already exists and is not a directory, refusing.
(or)
Directory "/tmp/test-readlink_and_make_absolute" already exists, but has mode 0775 that is too permissive (0755 was requested), refusing.
(or)
Directory "/tmp/test-readlink_and_make_absolute" already exists, but is owned by 1001:1000 (1000:1000 was requested), refusing.
Assertion 'mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0' failed at ../src/test/test-fs-util.c:320, function test_readlink_and_make_absolute(). Aborting.
No functional change except for the new log lines.
2018-03-22 13:03:41 +01:00
|
|
|
r = mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0, MKDIR_WARN_MODE);
|
2011-06-30 02:16:07 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-08-06 19:05:57 +02:00
|
|
|
s->fifo_path = strjoin("/run/systemd/sessions/", s->id, ".ref");
|
|
|
|
if (!s->fifo_path)
|
2011-06-29 00:06:04 +02:00
|
|
|
return -ENOMEM;
|
2011-06-24 23:25:28 +02:00
|
|
|
|
2011-06-29 00:06:04 +02:00
|
|
|
if (mkfifo(s->fifo_path, 0600) < 0 && errno != EEXIST)
|
|
|
|
return -errno;
|
|
|
|
}
|
2011-06-24 23:25:28 +02:00
|
|
|
|
2011-06-29 00:06:04 +02:00
|
|
|
/* Open reading side */
|
2011-06-29 03:48:53 +02:00
|
|
|
if (s->fifo_fd < 0) {
|
2018-01-24 11:09:29 +01:00
|
|
|
s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
|
2011-06-29 03:48:53 +02:00
|
|
|
if (s->fifo_fd < 0)
|
|
|
|
return -errno;
|
2013-11-05 01:10:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->fifo_event_source) {
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_event_add_io(s->manager->event, &s->fifo_event_source, s->fifo_fd, 0, session_dispatch_fifo, s);
|
2011-06-29 03:48:53 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-10-15 19:26:58 +02:00
|
|
|
/* Let's make sure we noticed dead sessions before we process new bus requests (which might
|
|
|
|
* create new sessions). */
|
2016-05-04 19:01:56 +02:00
|
|
|
r = sd_event_source_set_priority(s->fifo_event_source, SD_EVENT_PRIORITY_NORMAL-10);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-06-29 03:48:53 +02:00
|
|
|
}
|
2011-06-29 00:06:04 +02:00
|
|
|
|
|
|
|
/* Open writing side */
|
2018-01-24 11:09:29 +01:00
|
|
|
r = open(s->fifo_path, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
|
2011-06-29 00:06:04 +02:00
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
2011-06-24 23:25:28 +02:00
|
|
|
|
2011-06-29 00:06:04 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
static void session_remove_fifo(Session *s) {
|
2011-06-29 00:06:04 +02:00
|
|
|
assert(s);
|
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
s->fifo_event_source = sd_event_source_unref(s->fifo_event_source);
|
|
|
|
s->fifo_fd = safe_close(s->fifo_fd);
|
2011-06-29 00:06:04 +02:00
|
|
|
|
|
|
|
if (s->fifo_path) {
|
2018-08-06 19:04:49 +02:00
|
|
|
(void) unlink(s->fifo_path);
|
2015-09-08 18:43:11 +02:00
|
|
|
s->fifo_path = mfree(s->fifo_path);
|
2011-06-29 00:06:04 +02:00
|
|
|
}
|
2011-06-24 23:25:28 +02:00
|
|
|
}
|
|
|
|
|
2018-02-15 13:14:35 +01:00
|
|
|
bool session_may_gc(Session *s, bool drop_not_started) {
|
2018-08-06 19:00:49 +02:00
|
|
|
int r;
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
assert(s);
|
|
|
|
|
2011-06-29 03:48:16 +02:00
|
|
|
if (drop_not_started && !s->started)
|
2018-02-15 13:14:35 +01:00
|
|
|
return true;
|
2011-06-29 00:06:04 +02:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
if (!s->user)
|
2018-02-15 13:14:35 +01:00
|
|
|
return true;
|
2013-06-20 03:45:08 +02:00
|
|
|
|
2011-06-29 00:06:04 +02:00
|
|
|
if (s->fifo_fd >= 0) {
|
2014-02-06 18:32:14 +01:00
|
|
|
if (pipe_eof(s->fifo_fd) <= 0)
|
2018-02-15 13:14:35 +01:00
|
|
|
return false;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 19:00:49 +02:00
|
|
|
if (s->scope_job) {
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
|
|
|
|
r = manager_job_is_active(s->manager, s->scope_job, &error);
|
|
|
|
if (r < 0)
|
|
|
|
log_debug_errno(r, "Failed to determine whether job '%s' is pending, ignoring: %s", s->scope_job, bus_error_message(&error, r));
|
|
|
|
if (r != 0)
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-06 19:00:49 +02:00
|
|
|
if (s->scope) {
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
|
|
|
|
r = manager_unit_is_active(s->manager, s->scope, &error);
|
|
|
|
if (r < 0)
|
|
|
|
log_debug_errno(r, "Failed to determine whether unit '%s' is active, ignoring: %s", s->scope, bus_error_message(&error, r));
|
|
|
|
if (r != 0)
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-02-15 13:14:35 +01:00
|
|
|
return true;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
void session_add_to_gc_queue(Session *s) {
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
if (s->in_gc_queue)
|
|
|
|
return;
|
|
|
|
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_PREPEND(gc_queue, s->manager->session_gc_queue, s);
|
2011-05-25 00:55:58 +02:00
|
|
|
s->in_gc_queue = true;
|
|
|
|
}
|
|
|
|
|
2012-06-21 16:14:53 +02:00
|
|
|
SessionState session_get_state(Session *s) {
|
|
|
|
assert(s);
|
|
|
|
|
2014-02-13 23:03:23 +01:00
|
|
|
/* always check closing first */
|
2014-02-06 18:32:14 +01:00
|
|
|
if (s->stopping || s->timer_event_source)
|
|
|
|
return SESSION_CLOSING;
|
|
|
|
|
2014-02-13 23:03:23 +01:00
|
|
|
if (s->scope_job || s->fifo_fd < 0)
|
2013-08-13 17:59:28 +02:00
|
|
|
return SESSION_OPENING;
|
2013-07-02 01:46:30 +02:00
|
|
|
|
2012-06-21 16:14:53 +02:00
|
|
|
if (session_is_active(s))
|
|
|
|
return SESSION_ACTIVE;
|
|
|
|
|
|
|
|
return SESSION_ONLINE;
|
|
|
|
}
|
|
|
|
|
2011-07-13 19:58:35 +02:00
|
|
|
int session_kill(Session *s, KillWho who, int signo) {
|
|
|
|
assert(s);
|
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
if (!s->scope)
|
2011-07-13 19:58:35 +02:00
|
|
|
return -ESRCH;
|
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
return manager_kill_unit(s->manager, s->scope, who, signo, NULL);
|
2011-07-13 19:58:35 +02:00
|
|
|
}
|
|
|
|
|
2013-11-28 15:10:24 +01:00
|
|
|
static int session_open_vt(Session *s) {
|
2014-02-06 18:32:14 +01:00
|
|
|
char path[sizeof("/dev/tty") + DECIMAL_STR_MAX(s->vtnr)];
|
2013-11-28 15:10:24 +01:00
|
|
|
|
2014-08-08 20:45:43 +02:00
|
|
|
if (s->vtnr < 1)
|
|
|
|
return -ENODEV;
|
2013-11-28 15:10:24 +01:00
|
|
|
|
|
|
|
if (s->vtfd >= 0)
|
|
|
|
return s->vtfd;
|
|
|
|
|
2013-11-28 17:05:34 +01:00
|
|
|
sprintf(path, "/dev/tty%u", s->vtnr);
|
2015-08-18 20:41:24 +02:00
|
|
|
s->vtfd = open_terminal(path, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (s->vtfd < 0)
|
2015-11-05 13:44:06 +01:00
|
|
|
return log_error_errno(s->vtfd, "cannot open VT %s of session %s: %m", path, s->id);
|
2013-11-28 15:10:24 +01:00
|
|
|
|
|
|
|
return s->vtfd;
|
|
|
|
}
|
|
|
|
|
2019-10-15 16:43:17 +02:00
|
|
|
static int session_prepare_vt(Session *s) {
|
2013-11-28 15:10:24 +01:00
|
|
|
int vt, r;
|
2019-07-23 10:13:28 +02:00
|
|
|
struct vt_mode mode = {};
|
2013-11-28 15:10:24 +01:00
|
|
|
|
2014-08-08 20:45:43 +02:00
|
|
|
if (s->vtnr < 1)
|
|
|
|
return 0;
|
|
|
|
|
2013-11-28 15:10:24 +01:00
|
|
|
vt = session_open_vt(s);
|
|
|
|
if (vt < 0)
|
2014-08-08 20:45:43 +02:00
|
|
|
return vt;
|
2013-11-28 15:10:24 +01:00
|
|
|
|
2019-08-07 16:22:35 +02:00
|
|
|
r = fchown(vt, s->user->user_record->uid, -1);
|
2014-08-08 20:45:43 +02:00
|
|
|
if (r < 0) {
|
2015-09-09 11:08:23 +02:00
|
|
|
r = log_error_errno(errno,
|
|
|
|
"Cannot change owner of /dev/tty%u: %m",
|
|
|
|
s->vtnr);
|
2014-04-22 19:27:58 +02:00
|
|
|
goto error;
|
2014-08-08 20:45:43 +02:00
|
|
|
}
|
2014-04-22 19:27:58 +02:00
|
|
|
|
2013-11-28 15:10:24 +01:00
|
|
|
r = ioctl(vt, KDSKBMODE, K_OFF);
|
2014-08-08 20:45:43 +02:00
|
|
|
if (r < 0) {
|
2015-09-09 11:08:23 +02:00
|
|
|
r = log_error_errno(errno,
|
|
|
|
"Cannot set K_OFF on /dev/tty%u: %m",
|
|
|
|
s->vtnr);
|
2013-11-28 15:10:24 +01:00
|
|
|
goto error;
|
2014-08-08 20:45:43 +02:00
|
|
|
}
|
2013-11-28 15:10:24 +01:00
|
|
|
|
|
|
|
r = ioctl(vt, KDSETMODE, KD_GRAPHICS);
|
2014-08-08 20:45:43 +02:00
|
|
|
if (r < 0) {
|
2015-09-09 11:08:23 +02:00
|
|
|
r = log_error_errno(errno,
|
|
|
|
"Cannot set KD_GRAPHICS on /dev/tty%u: %m",
|
|
|
|
s->vtnr);
|
2013-11-28 15:10:24 +01:00
|
|
|
goto error;
|
2014-08-08 20:45:43 +02:00
|
|
|
}
|
2013-11-28 15:10:24 +01:00
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
mode.mode = VT_PROCESS;
|
2014-08-11 18:17:54 +02:00
|
|
|
mode.relsig = SIGRTMIN;
|
|
|
|
mode.acqsig = SIGRTMIN + 1;
|
2013-11-28 15:10:24 +01:00
|
|
|
r = ioctl(vt, VT_SETMODE, &mode);
|
2014-08-08 20:45:43 +02:00
|
|
|
if (r < 0) {
|
2015-09-09 11:08:23 +02:00
|
|
|
r = log_error_errno(errno,
|
|
|
|
"Cannot set VT_PROCESS on /dev/tty%u: %m",
|
|
|
|
s->vtnr);
|
2013-11-28 15:10:24 +01:00
|
|
|
goto error;
|
2014-08-08 20:45:43 +02:00
|
|
|
}
|
2013-11-28 15:10:24 +01:00
|
|
|
|
2014-08-08 20:45:43 +02:00
|
|
|
return 0;
|
2013-11-28 15:10:24 +01:00
|
|
|
|
|
|
|
error:
|
|
|
|
session_restore_vt(s);
|
2014-08-08 20:45:43 +02:00
|
|
|
return r;
|
2013-11-28 15:10:24 +01:00
|
|
|
}
|
|
|
|
|
2018-08-23 18:03:04 +02:00
|
|
|
static void session_restore_vt(Session *s) {
|
2019-10-18 12:44:51 +02:00
|
|
|
int r;
|
2015-08-18 21:19:05 +02:00
|
|
|
|
2019-10-18 12:44:51 +02:00
|
|
|
r = vt_restore(s->vtfd);
|
|
|
|
if (r == -EIO) {
|
|
|
|
int vt, old_fd;
|
2018-08-27 22:13:21 +02:00
|
|
|
|
2019-10-18 12:44:51 +02:00
|
|
|
/* It might happen if the controlling process exited before or while we were
|
|
|
|
* restoring the VT as it would leave the old file-descriptor in a hung-up
|
|
|
|
* state. In this case let's retry with a fresh handle to the virtual terminal. */
|
2018-08-27 22:13:21 +02:00
|
|
|
|
2019-10-18 12:44:51 +02:00
|
|
|
/* We do a little dance to avoid having the terminal be available
|
|
|
|
* for reuse before we've cleaned it up. */
|
|
|
|
old_fd = TAKE_FD(s->vtfd);
|
|
|
|
|
|
|
|
vt = session_open_vt(s);
|
|
|
|
safe_close(old_fd);
|
|
|
|
|
|
|
|
if (vt >= 0)
|
|
|
|
r = vt_restore(vt);
|
|
|
|
}
|
2018-08-27 22:13:21 +02:00
|
|
|
|
2019-01-10 12:17:51 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_warning_errno(r, "Failed to restore VT, ignoring: %m");
|
2014-04-22 19:27:58 +02:00
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
s->vtfd = safe_close(s->vtfd);
|
2013-11-28 15:10:24 +01:00
|
|
|
}
|
|
|
|
|
login: pause devices before acknowledging VT switches
If a session controller does not need synchronous VT switches, we allow
them to pass VT control to logind, which acknowledges all VT switches
unconditionally. This works fine with all sessions using the dbus API,
but causes out-of-sync device use if we switch to legacy sessions that
are notified via VT signals. Those are processed before logind notices
the session-switch via sysfs. Therefore, leaving the old session still
active for a short amount of time.
This, in fact, may cause the legacy session to prepare graphics devices
before the old session was deactivated, and thus, maybe causing the old
session to interfer with graphics device usage.
Fix this by releasing devices immediately before acknowledging VT
switches. This way, sessions without VT handlers are required to support
async session switching (which they do in that case, anyway).
2014-09-19 13:26:39 +02:00
|
|
|
void session_leave_vt(Session *s) {
|
2014-09-23 13:33:53 +02:00
|
|
|
int r;
|
|
|
|
|
login: pause devices before acknowledging VT switches
If a session controller does not need synchronous VT switches, we allow
them to pass VT control to logind, which acknowledges all VT switches
unconditionally. This works fine with all sessions using the dbus API,
but causes out-of-sync device use if we switch to legacy sessions that
are notified via VT signals. Those are processed before logind notices
the session-switch via sysfs. Therefore, leaving the old session still
active for a short amount of time.
This, in fact, may cause the legacy session to prepare graphics devices
before the old session was deactivated, and thus, maybe causing the old
session to interfer with graphics device usage.
Fix this by releasing devices immediately before acknowledging VT
switches. This way, sessions without VT handlers are required to support
async session switching (which they do in that case, anyway).
2014-09-19 13:26:39 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
session_device_pause_all(s);
|
2018-08-27 22:42:00 +02:00
|
|
|
r = vt_release(s->vtfd, false);
|
2014-09-23 13:33:53 +02:00
|
|
|
if (r < 0)
|
2018-08-27 22:42:00 +02:00
|
|
|
log_debug_errno(r, "Cannot release VT of session %s: %m", s->id);
|
login: pause devices before acknowledging VT switches
If a session controller does not need synchronous VT switches, we allow
them to pass VT control to logind, which acknowledges all VT switches
unconditionally. This works fine with all sessions using the dbus API,
but causes out-of-sync device use if we switch to legacy sessions that
are notified via VT signals. Those are processed before logind notices
the session-switch via sysfs. Therefore, leaving the old session still
active for a short amount of time.
This, in fact, may cause the legacy session to prepare graphics devices
before the old session was deactivated, and thus, maybe causing the old
session to interfer with graphics device usage.
Fix this by releasing devices immediately before acknowledging VT
switches. This way, sessions without VT handlers are required to support
async session switching (which they do in that case, anyway).
2014-09-19 13:26:39 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
bool session_is_controller(Session *s, const char *sender) {
|
2013-09-17 17:39:56 +02:00
|
|
|
assert(s);
|
|
|
|
|
|
|
|
return streq_ptr(s->controller, sender);
|
|
|
|
}
|
|
|
|
|
2014-09-01 14:04:44 +02:00
|
|
|
static void session_release_controller(Session *s, bool notify) {
|
|
|
|
_cleanup_free_ char *name = NULL;
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
SessionDevice *sd;
|
|
|
|
|
2014-09-01 14:04:44 +02:00
|
|
|
if (!s->controller)
|
|
|
|
return;
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
|
2014-09-01 14:04:44 +02:00
|
|
|
name = s->controller;
|
2013-11-28 15:10:24 +01:00
|
|
|
|
2020-10-15 19:26:58 +02:00
|
|
|
/* 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. */
|
2014-09-01 14:04:44 +02:00
|
|
|
if (!notify)
|
|
|
|
s->controller = NULL;
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
|
2014-09-01 14:04:44 +02:00
|
|
|
while ((sd = hashmap_first(s->devices)))
|
|
|
|
session_device_free(sd);
|
|
|
|
|
|
|
|
s->controller = NULL;
|
2015-08-05 15:58:39 +02:00
|
|
|
s->track = sd_bus_track_unref(s->track);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int on_bus_track(sd_bus_track *track, void *userdata) {
|
|
|
|
Session *s = userdata;
|
|
|
|
|
|
|
|
assert(track);
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
session_drop_controller(s);
|
|
|
|
|
|
|
|
return 0;
|
logind: restore session-controller after crash
We now save the unique bus-name of a session-controller as CONTROLLER=%s
in the session files. This allows us to restore the controller after a
crash or restart.
Note that we test whether the name is still valid (dbus guarantees that
the name is unique as long as the machine is up and running). If it is,
we know that the controller still exists and can safely restore it. Our
dbus-name-tracking guarantees that we're notified once it exits.
Also note that session-devices are *not* restored. We have no way to know
which devices where used before the crash. We could store all these on
disk, too, or mark them via udev. However, this seems to be rather
cumbersome. Instead, we expect controllers to listen for NewSession
signals for their own session. This is sent on session_load() and they can
then re-request all devices.
The only race I could find is if logind crashes, then the session
controller tries calling ReleaseControl() (which will fail as logind is
down) but keeps the bus-connection valid for other independent requests.
If logind is restarted, it will restore the old controller and thus block
the session.
However, this seems unlikely for several reasons:
- The ReleaseControl() call must occur exactly in the timespan where
logind is dead.
- A process which calls ReleaseControl() usually closes the
bus-connection afterwards. Especially if ReleaseControl() fails, the
process should notice that something is wrong and close the bus.
- A process calling ReleaseControl() usually exits afterwards. There may
be any cleanup pending, but other than that, usual compositors exit.
- If a session-controller calls ReleaseControl(), a session is usually
considered closing. There is no known use-case where we hand-over
session-control in a single session. So we don't care whether the
controller is locked afterwards.
So this seems negligible.
2013-11-28 14:58:57 +01:00
|
|
|
}
|
|
|
|
|
2017-04-26 14:20:41 +02:00
|
|
|
int session_set_controller(Session *s, const char *sender, bool force, bool prepare) {
|
2014-09-01 14:04:44 +02:00
|
|
|
_cleanup_free_ char *name = NULL;
|
2013-09-17 17:39:56 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
assert(sender);
|
|
|
|
|
|
|
|
if (session_is_controller(s, sender))
|
|
|
|
return 0;
|
|
|
|
if (s->controller && !force)
|
|
|
|
return -EBUSY;
|
|
|
|
|
2014-09-01 14:04:44 +02:00
|
|
|
name = strdup(sender);
|
|
|
|
if (!name)
|
2013-09-17 17:39:56 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-08-05 15:58:39 +02:00
|
|
|
s->track = sd_bus_track_unref(s->track);
|
|
|
|
r = sd_bus_track_new(s->manager->bus, &s->track, on_bus_track, s);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_track_add_name(s->track, name);
|
|
|
|
if (r < 0)
|
2013-09-17 17:39:56 +02:00
|
|
|
return r;
|
|
|
|
|
2013-11-28 15:10:24 +01:00
|
|
|
/* 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.
|
|
|
|
* If logind crashes/restarts, we restore the controller during restart
|
2017-04-26 14:20:41 +02:00
|
|
|
* (without preparing the VT since the controller has probably overridden
|
|
|
|
* VT state by now) or reset the VT in case it crashed/exited, too. */
|
|
|
|
if (prepare) {
|
|
|
|
r = session_prepare_vt(s);
|
|
|
|
if (r < 0) {
|
|
|
|
s->track = sd_bus_track_unref(s->track);
|
|
|
|
return r;
|
|
|
|
}
|
2014-08-31 23:34:01 +02:00
|
|
|
}
|
2014-08-08 20:45:43 +02:00
|
|
|
|
2014-09-01 14:04:44 +02:00
|
|
|
session_release_controller(s, true);
|
2018-03-22 16:53:26 +01:00
|
|
|
s->controller = TAKE_PTR(name);
|
2014-09-01 14:04:44 +02:00
|
|
|
session_save(s);
|
2013-11-28 15:10:24 +01:00
|
|
|
|
2013-09-17 17:39:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_drop_controller(Session *s) {
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
if (!s->controller)
|
|
|
|
return;
|
|
|
|
|
2015-08-05 15:58:39 +02:00
|
|
|
s->track = sd_bus_track_unref(s->track);
|
2020-02-23 04:44:42 +01:00
|
|
|
session_set_type(s, s->original_type);
|
2014-09-01 14:04:44 +02:00
|
|
|
session_release_controller(s, false);
|
|
|
|
session_save(s);
|
|
|
|
session_restore_vt(s);
|
2013-09-17 17:39:56 +02:00
|
|
|
}
|
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
static const char* const session_state_table[_SESSION_STATE_MAX] = {
|
|
|
|
[SESSION_OPENING] = "opening",
|
2020-10-15 19:26:58 +02:00
|
|
|
[SESSION_ONLINE] = "online",
|
|
|
|
[SESSION_ACTIVE] = "active",
|
|
|
|
[SESSION_CLOSING] = "closing",
|
2012-06-21 16:14:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(session_state, SessionState);
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
static const char* const session_type_table[_SESSION_TYPE_MAX] = {
|
2014-03-07 21:38:48 +01:00
|
|
|
[SESSION_UNSPECIFIED] = "unspecified",
|
2020-10-15 19:26:58 +02:00
|
|
|
[SESSION_TTY] = "tty",
|
|
|
|
[SESSION_X11] = "x11",
|
|
|
|
[SESSION_WAYLAND] = "wayland",
|
|
|
|
[SESSION_MIR] = "mir",
|
|
|
|
[SESSION_WEB] = "web",
|
2011-05-23 23:55:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);
|
2011-07-13 19:58:35 +02:00
|
|
|
|
2012-02-14 21:33:51 +01:00
|
|
|
static const char* const session_class_table[_SESSION_CLASS_MAX] = {
|
2020-10-15 19:26:58 +02:00
|
|
|
[SESSION_USER] = "user",
|
|
|
|
[SESSION_GREETER] = "greeter",
|
2013-04-09 22:18:16 +02:00
|
|
|
[SESSION_LOCK_SCREEN] = "lock-screen",
|
2020-10-15 19:26:58 +02:00
|
|
|
[SESSION_BACKGROUND] = "background",
|
2012-02-14 21:33:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(session_class, SessionClass);
|
|
|
|
|
2011-07-13 19:58:35 +02:00
|
|
|
static const char* const kill_who_table[_KILL_WHO_MAX] = {
|
|
|
|
[KILL_LEADER] = "leader",
|
2020-10-15 19:26:58 +02:00
|
|
|
[KILL_ALL] = "all",
|
2011-07-13 19:58:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);
|
2018-08-07 13:49:34 +02:00
|
|
|
|
|
|
|
static const char* const tty_validity_table[_TTY_VALIDITY_MAX] = {
|
2020-10-15 19:26:58 +02:00
|
|
|
[TTY_FROM_PAM] = "from-pam",
|
|
|
|
[TTY_FROM_UTMP] = "from-utmp",
|
2018-08-07 13:49:34 +02:00
|
|
|
[TTY_UTMP_INCONSISTENT] = "utmp-inconsistent",
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(tty_validity, TTYValidity);
|