2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2020-06-11 18:23:28 +02:00
|
|
|
#include <sys/types.h>
|
2011-05-23 23:55:06 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
#include "sd-daemon.h"
|
2018-08-22 07:53:51 +02:00
|
|
|
#include "sd-device.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2013-11-05 01:10:21 +01:00
|
|
|
#include "bus-error.h"
|
2020-04-21 10:34:05 +02:00
|
|
|
#include "bus-log-control-api.h"
|
2020-01-22 11:39:22 +01:00
|
|
|
#include "bus-polkit.h"
|
2020-05-03 07:52:38 +02:00
|
|
|
#include "bus-util.h"
|
2018-01-11 00:39:12 +01:00
|
|
|
#include "cgroup-util.h"
|
2015-11-03 12:26:12 +01:00
|
|
|
#include "def.h"
|
2018-08-28 09:05:35 +02:00
|
|
|
#include "device-util.h"
|
2015-10-26 20:07:55 +01:00
|
|
|
#include "dirent-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2018-02-16 06:58:33 +01:00
|
|
|
#include "fs-util.h"
|
2019-04-30 15:05:14 +02:00
|
|
|
#include "logind-dbus.h"
|
|
|
|
#include "logind-seat-dbus.h"
|
|
|
|
#include "logind-session-dbus.h"
|
|
|
|
#include "logind-user-dbus.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "logind.h"
|
2018-11-19 20:26:37 +01:00
|
|
|
#include "main-func.h"
|
2018-03-06 16:59:38 +01:00
|
|
|
#include "parse-util.h"
|
2018-01-11 00:39:12 +01:00
|
|
|
#include "process-util.h"
|
2016-02-02 20:07:46 +01:00
|
|
|
#include "selinux-util.h"
|
2020-04-23 10:19:46 +02:00
|
|
|
#include "service-util.h"
|
2015-05-29 20:14:11 +02:00
|
|
|
#include "signal-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "strv.h"
|
2018-08-27 23:16:10 +02:00
|
|
|
#include "terminal-util.h"
|
2019-03-09 02:45:48 +01:00
|
|
|
#include "udev-util.h"
|
2020-06-11 18:23:28 +02:00
|
|
|
#include "user-util.h"
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-04-24 18:13:12 +02:00
|
|
|
static Manager* manager_unref(Manager *m);
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
|
2015-04-11 19:47:12 +02:00
|
|
|
|
2018-04-24 18:13:12 +02:00
|
|
|
static int manager_new(Manager **ret) {
|
|
|
|
_cleanup_(manager_unrefp) Manager *m = NULL;
|
2016-04-09 21:03:19 +02:00
|
|
|
int r;
|
|
|
|
|
2018-04-24 18:13:12 +02:00
|
|
|
assert(ret);
|
|
|
|
|
2018-08-03 20:20:50 +02:00
|
|
|
m = new(Manager, 1);
|
2016-04-09 21:03:19 +02:00
|
|
|
if (!m)
|
2018-04-24 18:13:12 +02:00
|
|
|
return -ENOMEM;
|
2016-04-09 21:03:19 +02:00
|
|
|
|
2018-08-03 20:20:50 +02:00
|
|
|
*m = (Manager) {
|
|
|
|
.console_active_fd = -1,
|
|
|
|
.reserve_vt_fd = -1,
|
2019-04-28 11:06:50 +02:00
|
|
|
.idle_action_not_before_usec = now(CLOCK_MONOTONIC),
|
2018-08-03 20:20:50 +02:00
|
|
|
};
|
2016-04-09 21:03:19 +02:00
|
|
|
|
2014-08-13 01:00:18 +02:00
|
|
|
m->devices = hashmap_new(&string_hash_ops);
|
|
|
|
m->seats = hashmap_new(&string_hash_ops);
|
|
|
|
m->sessions = hashmap_new(&string_hash_ops);
|
2018-08-07 12:08:24 +02:00
|
|
|
m->sessions_by_leader = hashmap_new(NULL);
|
2014-08-13 01:00:18 +02:00
|
|
|
m->users = hashmap_new(NULL);
|
|
|
|
m->inhibitors = hashmap_new(&string_hash_ops);
|
|
|
|
m->buttons = hashmap_new(&string_hash_ops);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2014-08-13 01:00:18 +02:00
|
|
|
m->user_units = hashmap_new(&string_hash_ops);
|
|
|
|
m->session_units = hashmap_new(&string_hash_ops);
|
2012-05-31 19:46:42 +02:00
|
|
|
|
2018-08-07 12:08:24 +02:00
|
|
|
if (!m->devices || !m->seats || !m->sessions || !m->sessions_by_leader || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units)
|
2018-04-24 18:13:12 +02:00
|
|
|
return -ENOMEM;
|
2011-06-29 02:27:16 +02:00
|
|
|
|
2013-11-11 19:34:13 +01:00
|
|
|
r = sd_event_default(&m->event);
|
2014-02-21 21:10:00 +01:00
|
|
|
if (r < 0)
|
2018-04-24 18:13:12 +02:00
|
|
|
return r;
|
2013-11-05 01:10:21 +01:00
|
|
|
|
2018-04-24 18:14:25 +02:00
|
|
|
r = sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-04-24 18:13:12 +02:00
|
|
|
(void) sd_event_set_watchdog(m->event, true);
|
2013-12-11 18:14:52 +01:00
|
|
|
|
2016-04-09 21:03:19 +02:00
|
|
|
manager_reset_config(m);
|
|
|
|
|
2018-04-24 18:13:12 +02:00
|
|
|
*ret = TAKE_PTR(m);
|
|
|
|
return 0;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 18:13:12 +02:00
|
|
|
static Manager* manager_unref(Manager *m) {
|
2011-05-23 23:55:06 +02:00
|
|
|
Session *session;
|
|
|
|
User *u;
|
|
|
|
Device *d;
|
|
|
|
Seat *s;
|
2012-04-16 16:47:33 +02:00
|
|
|
Inhibitor *i;
|
2012-05-30 15:01:51 +02:00
|
|
|
Button *b;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2016-10-20 19:19:46 +02:00
|
|
|
if (!m)
|
2018-04-24 18:13:12 +02:00
|
|
|
return NULL;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
while ((session = hashmap_first(m->sessions)))
|
|
|
|
session_free(session);
|
|
|
|
|
|
|
|
while ((u = hashmap_first(m->users)))
|
|
|
|
user_free(u);
|
|
|
|
|
|
|
|
while ((d = hashmap_first(m->devices)))
|
|
|
|
device_free(d);
|
|
|
|
|
|
|
|
while ((s = hashmap_first(m->seats)))
|
|
|
|
seat_free(s);
|
|
|
|
|
2012-04-16 16:47:33 +02:00
|
|
|
while ((i = hashmap_first(m->inhibitors)))
|
|
|
|
inhibitor_free(i);
|
|
|
|
|
2012-05-30 15:01:51 +02:00
|
|
|
while ((b = hashmap_first(m->buttons)))
|
|
|
|
button_free(b);
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
hashmap_free(m->devices);
|
|
|
|
hashmap_free(m->seats);
|
2012-04-16 16:47:33 +02:00
|
|
|
hashmap_free(m->sessions);
|
2018-08-07 12:08:24 +02:00
|
|
|
hashmap_free(m->sessions_by_leader);
|
2012-04-16 16:47:33 +02:00
|
|
|
hashmap_free(m->users);
|
|
|
|
hashmap_free(m->inhibitors);
|
2012-05-30 15:01:51 +02:00
|
|
|
hashmap_free(m->buttons);
|
logind: add SetBrightness() bus call for setting brightness of leds/backlight devices associated with a seat
This augments the drm/input device management by adding a single method
call for setting the brightness of an "leds" or "backlight" kernel class
device.
This method call requires no privileges to call, but a caller can only
change the brightness on sessions that are currently active, and they
must own the session.
This does not do enumeration of such class devices, feature or range
probing, chnage notification; it doesn't help associating graphics or
input devices with their backlight or leds devices. For all that clients
should go directly to udev/sysfs. The SetBrightness() call is just for
executing the actual change operation, that is otherwise privileged.
Example line:
busctl call org.freedesktop.login1 /org/freedesktop/login1/session/self org.freedesktop.login1.Session SetBrightness ssu "backlight" "intel_backlight" 200
The parameter the SetBrightness() call takes are the kernel subsystem
(i.e. "leds" or "backlight"), the device name, and the brightness
value.
On some hw setting the brightness is slow, and implementation and write
access to the sysfs knobs exposes this slowness. Due to this we'll fork
off a writer process in the background so that logind doesn't have to
block. Moreover, write requestes are coalesced: when a write request is
enqueued while one is already being executed it is queued. When another
write reques is then enqueued the earlier one is replaced by the newer
one, so that only one queued write request per device remains at any
time. Method replies are sent as soon as the first write request that
happens after the request was received is completed.
It is recommended that bus clients turn off the "expect_reply" flag on
the dbus messages they send though, that relieves logind from sending
completion notification and is particularly a good idea if clients
implement reactive UI sliders that send a quick secession of write
requests.
Replaces: #12413
2019-04-28 11:07:56 +02:00
|
|
|
hashmap_free(m->brightness_writers);
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2013-07-02 01:46:30 +02:00
|
|
|
hashmap_free(m->user_units);
|
|
|
|
hashmap_free(m->session_units);
|
2012-05-31 19:46:42 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_event_source_unref(m->idle_action_event_source);
|
2015-04-11 18:44:51 +02:00
|
|
|
sd_event_source_unref(m->inhibit_timeout_source);
|
2015-04-20 15:27:15 +02:00
|
|
|
sd_event_source_unref(m->scheduled_shutdown_timeout_source);
|
2015-04-23 17:11:28 +02:00
|
|
|
sd_event_source_unref(m->nologin_timeout_source);
|
2015-04-22 17:20:42 +02:00
|
|
|
sd_event_source_unref(m->wall_message_timeout_source);
|
2013-11-05 01:10:21 +01:00
|
|
|
|
|
|
|
sd_event_source_unref(m->console_active_event_source);
|
2014-03-03 20:49:33 +01:00
|
|
|
sd_event_source_unref(m->lid_switch_ignore_event_source);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-07 13:49:34 +02:00
|
|
|
#if ENABLE_UTMP
|
|
|
|
sd_event_source_unref(m->utmp_event_source);
|
|
|
|
#endif
|
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(m->console_active_fd);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
sd_device_monitor_unref(m->device_seat_monitor);
|
|
|
|
sd_device_monitor_unref(m->device_monitor);
|
|
|
|
sd_device_monitor_unref(m->device_vcsa_monitor);
|
|
|
|
sd_device_monitor_unref(m->device_button_monitor);
|
2015-08-17 10:45:30 +02:00
|
|
|
|
2015-04-23 17:11:28 +02:00
|
|
|
if (m->unlink_nologin)
|
2018-02-16 06:58:33 +01:00
|
|
|
(void) unlink_or_warn("/run/nologin");
|
2015-04-23 17:11:28 +02:00
|
|
|
|
2014-08-06 07:57:43 +02:00
|
|
|
bus_verify_polkit_async_registry_free(m->polkit_registry);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2019-01-17 15:53:15 +01:00
|
|
|
sd_bus_flush_close_unref(m->bus);
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_event_unref(m->event);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(m->reserve_vt_fd);
|
2012-09-17 12:39:16 +02:00
|
|
|
|
2011-06-29 01:47:55 +02:00
|
|
|
strv_free(m->kill_only_users);
|
|
|
|
strv_free(m->kill_exclude_users);
|
|
|
|
|
2015-04-20 15:27:15 +02:00
|
|
|
free(m->scheduled_shutdown_type);
|
2015-04-22 17:20:42 +02:00
|
|
|
free(m->scheduled_shutdown_tty);
|
|
|
|
free(m->wall_message);
|
2013-01-24 04:56:44 +01:00
|
|
|
free(m->action_job);
|
2018-04-24 18:13:12 +02:00
|
|
|
|
2020-05-26 22:24:02 +02:00
|
|
|
strv_free(m->efi_boot_loader_entries);
|
2020-06-25 16:25:41 +02:00
|
|
|
free(m->efi_loader_entry_one_shot);
|
2020-05-26 22:24:02 +02:00
|
|
|
|
2018-04-24 18:13:12 +02:00
|
|
|
return mfree(m);
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 18:11:09 +01:00
|
|
|
static int manager_enumerate_devices(Manager *m) {
|
2018-08-22 07:53:51 +02:00
|
|
|
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
|
|
|
sd_device *d;
|
2011-05-25 00:55:58 +02:00
|
|
|
int r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
/* Loads devices from udev and creates seats for them as
|
|
|
|
* necessary */
|
|
|
|
|
2018-08-22 07:53:51 +02:00
|
|
|
r = sd_device_enumerator_new(&e);
|
2011-05-25 00:55:58 +02:00
|
|
|
if (r < 0)
|
2013-12-18 03:37:26 +01:00
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-08-22 07:53:51 +02:00
|
|
|
r = sd_device_enumerator_add_match_tag(e, "master-of-seat");
|
2013-12-18 17:12:15 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-08-28 09:05:35 +02:00
|
|
|
FOREACH_DEVICE(e, d) {
|
2011-05-23 23:55:06 +02:00
|
|
|
int k;
|
|
|
|
|
2013-12-18 03:37:26 +01:00
|
|
|
k = manager_process_seat_device(m, d);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-11-08 18:11:09 +01:00
|
|
|
static int manager_enumerate_buttons(Manager *m) {
|
2018-08-22 07:53:51 +02:00
|
|
|
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
|
|
|
sd_device *d;
|
2012-05-30 15:01:51 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
/* Loads buttons from udev */
|
|
|
|
|
2018-02-09 17:37:39 +01:00
|
|
|
if (manager_all_buttons_ignored(m))
|
2012-05-30 21:40:32 +02:00
|
|
|
return 0;
|
|
|
|
|
2018-08-22 07:53:51 +02:00
|
|
|
r = sd_device_enumerator_new(&e);
|
2012-05-30 15:01:51 +02:00
|
|
|
if (r < 0)
|
2013-12-18 03:37:26 +01:00
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2018-08-22 07:53:51 +02:00
|
|
|
r = sd_device_enumerator_add_match_subsystem(e, "input", true);
|
2012-05-30 15:01:51 +02:00
|
|
|
if (r < 0)
|
2013-12-18 03:37:26 +01:00
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2018-08-22 07:53:51 +02:00
|
|
|
r = sd_device_enumerator_add_match_tag(e, "power-switch");
|
2013-12-18 17:12:15 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-08-28 09:05:35 +02:00
|
|
|
FOREACH_DEVICE(e, d) {
|
2012-05-30 15:01:51 +02:00
|
|
|
int k;
|
|
|
|
|
2013-12-18 03:37:26 +01:00
|
|
|
k = manager_process_button_device(m, d);
|
2012-05-30 15:01:51 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-11-08 18:11:09 +01:00
|
|
|
static int manager_enumerate_seats(Manager *m) {
|
2013-06-20 03:45:08 +02:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
2011-05-23 23:55:06 +02:00
|
|
|
struct dirent *de;
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
/* This loads data about seats stored on disk, but does not
|
|
|
|
* actually create any seats. Removes data of seats that no
|
|
|
|
* longer exist. */
|
|
|
|
|
|
|
|
d = opendir("/run/systemd/seats");
|
|
|
|
if (!d) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
2015-11-05 13:44:20 +01:00
|
|
|
return log_error_errno(errno, "Failed to open /run/systemd/seats: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
FOREACH_DIRENT(de, d, return -errno) {
|
2011-05-23 23:55:06 +02:00
|
|
|
Seat *s;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
if (!dirent_is_file(de))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s = hashmap_get(m->seats, de->d_name);
|
|
|
|
if (!s) {
|
2018-02-16 07:00:22 +01:00
|
|
|
if (unlinkat(dirfd(d), de->d_name, 0) < 0)
|
|
|
|
log_warning("Failed to remove /run/systemd/seats/%s: %m",
|
|
|
|
de->d_name);
|
2011-05-23 23:55:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
k = seat_load(s);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
return r;
|
|
|
|
}
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
static int manager_enumerate_linger_users(Manager *m) {
|
2013-06-20 03:45:08 +02:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
2011-05-25 00:55:58 +02:00
|
|
|
struct dirent *de;
|
|
|
|
int r = 0;
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
assert(m);
|
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
d = opendir("/var/lib/systemd/linger");
|
|
|
|
if (!d) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
2015-11-05 13:44:20 +01:00
|
|
|
return log_error_errno(errno, "Failed to open /var/lib/systemd/linger/: %m");
|
2011-05-25 00:55:58 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
FOREACH_DIRENT(de, d, return -errno) {
|
2011-05-25 00:55:58 +02:00
|
|
|
int k;
|
|
|
|
|
2019-09-29 13:43:00 +02:00
|
|
|
dirent_ensure_type(d, de);
|
2011-05-25 00:55:58 +02:00
|
|
|
if (!dirent_is_file(de))
|
2011-05-23 23:55:06 +02:00
|
|
|
continue;
|
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
k = manager_add_user_by_name(m, de->d_name, NULL);
|
2019-07-23 10:17:46 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = log_warning_errno(k, "Couldn't add lingering user %s, ignoring: %m", de->d_name);
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-11-08 18:11:09 +01:00
|
|
|
static int manager_enumerate_users(Manager *m) {
|
2013-06-20 03:45:08 +02:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
2011-05-23 23:55:06 +02:00
|
|
|
struct dirent *de;
|
2011-05-25 00:55:58 +02:00
|
|
|
int r, k;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
/* Add lingering users */
|
2013-06-20 06:51:10 +02:00
|
|
|
r = manager_enumerate_linger_users(m);
|
2011-05-25 00:55:58 +02:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
/* Read in user data stored on disk */
|
2011-05-23 23:55:06 +02:00
|
|
|
d = opendir("/run/systemd/users");
|
|
|
|
if (!d) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
2015-11-05 13:44:20 +01:00
|
|
|
return log_error_errno(errno, "Failed to open /run/systemd/users: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
FOREACH_DIRENT(de, d, return -errno) {
|
2011-05-23 23:55:06 +02:00
|
|
|
User *u;
|
2020-06-11 18:23:28 +02:00
|
|
|
uid_t uid;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
if (!dirent_is_file(de))
|
|
|
|
continue;
|
|
|
|
|
2020-06-11 18:23:28 +02:00
|
|
|
k = parse_uid(de->d_name, &uid);
|
|
|
|
if (k < 0) {
|
|
|
|
r = log_warning_errno(k, "Failed to parse filename /run/systemd/users/%s as UID.", de->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
k = manager_add_user_by_uid(m, uid, &u);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (k < 0) {
|
2019-07-23 10:17:46 +02:00
|
|
|
r = log_warning_errno(k, "Failed to add user by file name %s, ignoring: %m", de->d_name);
|
2011-05-23 23:55:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
user_add_to_gc_queue(u);
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
k = user_load(u);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:59:38 +01:00
|
|
|
static int parse_fdname(const char *fdname, char **session_id, dev_t *dev) {
|
|
|
|
_cleanup_strv_free_ char **parts = NULL;
|
|
|
|
_cleanup_free_ char *id = NULL;
|
2018-10-19 20:00:46 +02:00
|
|
|
unsigned major, minor;
|
2018-03-06 16:59:38 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
parts = strv_split(fdname, "-");
|
|
|
|
if (!parts)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (strv_length(parts) != 5)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!streq(parts[0], "session"))
|
|
|
|
return -EINVAL;
|
2018-05-10 01:34:33 +02:00
|
|
|
|
2018-03-06 16:59:38 +01:00
|
|
|
id = strdup(parts[1]);
|
|
|
|
if (!id)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (!streq(parts[2], "device"))
|
|
|
|
return -EINVAL;
|
2018-05-10 01:34:33 +02:00
|
|
|
|
|
|
|
r = safe_atou(parts[3], &major);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
r = safe_atou(parts[4], &minor);
|
2018-03-06 16:59:38 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
*dev = makedev(major, minor);
|
2018-03-22 16:53:26 +01:00
|
|
|
*session_id = TAKE_PTR(id);
|
2018-03-06 16:59:38 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-02 11:52:58 +02:00
|
|
|
static int deliver_fd(Manager *m, const char *fdname, int fd) {
|
|
|
|
_cleanup_free_ char *id = NULL;
|
|
|
|
SessionDevice *sd;
|
|
|
|
struct stat st;
|
|
|
|
Session *s;
|
|
|
|
dev_t dev;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(fd >= 0);
|
|
|
|
|
|
|
|
r = parse_fdname(fdname, &id, &dev);
|
|
|
|
if (r < 0)
|
|
|
|
return log_debug_errno(r, "Failed to parse fd name %s: %m", fdname);
|
|
|
|
|
|
|
|
s = hashmap_get(m->sessions, id);
|
|
|
|
if (!s)
|
|
|
|
/* If the session doesn't exist anymore, the associated session device attached to this fd
|
|
|
|
* doesn't either. Let's simply close this fd. */
|
|
|
|
return log_debug_errno(SYNTHETIC_ERRNO(ENXIO), "Failed to attach fd for unknown session: %s", id);
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
/* The device is allowed to go away at a random point, in which case fstat() failing is
|
|
|
|
* expected. */
|
|
|
|
return log_debug_errno(errno, "Failed to stat device fd for session %s: %m", id);
|
|
|
|
|
|
|
|
if (!S_ISCHR(st.st_mode) || st.st_rdev != dev)
|
|
|
|
return log_debug_errno(SYNTHETIC_ERRNO(ENODEV), "Device fd doesn't point to the expected character device node");
|
|
|
|
|
|
|
|
sd = hashmap_get(s->devices, &dev);
|
|
|
|
if (!sd)
|
|
|
|
/* Weird, we got an fd for a session device which wasn't recorded in the session state
|
|
|
|
* file... */
|
|
|
|
return log_warning_errno(SYNTHETIC_ERRNO(ENODEV), "Got fd for missing session device [%u:%u] in session %s",
|
|
|
|
major(dev), minor(dev), s->id);
|
|
|
|
|
|
|
|
log_debug("Attaching fd to session device [%u:%u] for session %s",
|
|
|
|
major(dev), minor(dev), s->id);
|
|
|
|
|
|
|
|
session_device_attach_fd(sd, fd, s->was_active);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-16 11:17:09 +01:00
|
|
|
static int manager_attach_fds(Manager *m) {
|
|
|
|
_cleanup_strv_free_ char **fdnames = NULL;
|
2019-04-02 11:52:58 +02:00
|
|
|
int n;
|
2017-03-16 11:17:09 +01:00
|
|
|
|
2019-04-02 11:52:58 +02:00
|
|
|
/* Upon restart, PID1 will send us back all fds of session devices that we previously opened. Each
|
|
|
|
* file descriptor is associated with a given session. The session ids are passed through FDNAMES. */
|
2017-03-16 11:17:09 +01:00
|
|
|
|
|
|
|
n = sd_listen_fds_with_names(true, &fdnames);
|
2019-04-02 11:52:58 +02:00
|
|
|
if (n < 0)
|
|
|
|
return log_warning_errno(n, "Failed to acquire passed fd list: %m");
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
2017-03-16 11:17:09 +01:00
|
|
|
|
2019-04-02 11:52:58 +02:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
int fd = SD_LISTEN_FDS_START + i;
|
2017-03-16 11:17:09 +01:00
|
|
|
|
2019-04-02 11:52:58 +02:00
|
|
|
if (deliver_fd(m, fdnames[i], fd) >= 0)
|
2018-02-26 18:34:43 +01:00
|
|
|
continue;
|
2017-03-16 11:17:09 +01:00
|
|
|
|
2019-04-02 11:52:58 +02:00
|
|
|
/* Hmm, we couldn't deliver the fd to any session device object? If so, let's close the fd */
|
|
|
|
safe_close(fd);
|
2017-03-16 11:17:09 +01:00
|
|
|
|
2019-04-02 11:52:58 +02:00
|
|
|
/* Remove from fdstore as well */
|
|
|
|
(void) sd_notifyf(false,
|
|
|
|
"FDSTOREREMOVE=1\n"
|
|
|
|
"FDNAME=%s", fdnames[i]);
|
2017-03-16 11:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-08 18:11:09 +01:00
|
|
|
static int manager_enumerate_sessions(Manager *m) {
|
2013-06-20 03:45:08 +02:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
|
|
|
struct dirent *de;
|
2017-03-16 11:17:09 +01:00
|
|
|
int r = 0, k;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
assert(m);
|
|
|
|
|
|
|
|
/* Read in session data stored on disk */
|
|
|
|
d = opendir("/run/systemd/sessions");
|
|
|
|
if (!d) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
2015-11-05 13:44:20 +01:00
|
|
|
return log_error_errno(errno, "Failed to open /run/systemd/sessions: %m");
|
2013-06-20 03:45:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FOREACH_DIRENT(de, d, return -errno) {
|
|
|
|
struct Session *s;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
if (!dirent_is_file(de))
|
2011-05-25 00:55:58 +02:00
|
|
|
continue;
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
k = manager_add_session(m, de->d_name, &s);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (k < 0) {
|
2019-07-23 10:17:46 +02:00
|
|
|
r = log_warning_errno(k, "Failed to add session by file name %s, ignoring: %m", de->d_name);
|
2011-05-23 23:55:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
session_add_to_gc_queue(s);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
k = session_load(s);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
2019-04-02 11:52:58 +02:00
|
|
|
/* We might be restarted and PID1 could have sent us back the session device fds we previously
|
|
|
|
* saved. */
|
|
|
|
(void) manager_attach_fds(m);
|
2017-03-16 11:17:09 +01:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-11-08 18:11:09 +01:00
|
|
|
static int manager_enumerate_inhibitors(Manager *m) {
|
2013-06-20 03:45:08 +02:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
2011-05-23 23:55:06 +02:00
|
|
|
struct dirent *de;
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
d = opendir("/run/systemd/inhibit");
|
2011-05-23 23:55:06 +02:00
|
|
|
if (!d) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
2015-11-05 13:44:20 +01:00
|
|
|
return log_error_errno(errno, "Failed to open /run/systemd/inhibit: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
FOREACH_DIRENT(de, d, return -errno) {
|
2011-05-23 23:55:06 +02:00
|
|
|
int k;
|
2013-06-20 03:45:08 +02:00
|
|
|
Inhibitor *i;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
if (!dirent_is_file(de))
|
|
|
|
continue;
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
k = manager_add_inhibitor(m, de->d_name, &i);
|
|
|
|
if (k < 0) {
|
2019-07-23 10:17:46 +02:00
|
|
|
r = log_warning_errno(k, "Couldn't add inhibitor %s, ignoring: %m", de->d_name);
|
2011-05-23 23:55:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
k = inhibitor_load(i);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
static int manager_dispatch_seat_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) {
|
2013-11-05 01:10:21 +01:00
|
|
|
Manager *m = userdata;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
assert(m);
|
2018-10-08 15:47:53 +02:00
|
|
|
assert(device);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
manager_process_seat_device(m, device);
|
2013-12-13 03:30:42 +01:00
|
|
|
return 0;
|
2011-06-28 03:21:14 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
static int manager_dispatch_device_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) {
|
2013-11-05 01:10:21 +01:00
|
|
|
Manager *m = userdata;
|
logind: listen actively for session devices
Session compositors need access to fbdev, DRM and evdev devices if they
control a session. To make logind pass them to sessions, we need to
listen for them actively.
However, we avoid creating new seats for non master-of-seat devices. Only
once a seat is created, we start remembering all other session devices. If
the last master-device is removed (even if there are other non-master
devices still available), we destroy the seat. This is the current
behavior, but we need to explicitly implement it now as there may be
non-master devices in the seat->devices list.
Unlike master devices, we don't care whether our list of non-master
devices is complete. We don't export this list but use it only as cache if
sessions request these devices. Hence, if a session requests a device that
is not in the list, we will simply look it up. However, once a session
requested a device, we must be notified of "remove" udev events. So we
must link the devices somehow into the device-list.
Regarding the implementation, we now sort the device list by the "master"
flag. This guarantees that master devices are at the front and non-master
devices at the tail of the list. Thus, we can easily test whether a seat
has a master device attached.
2013-09-17 17:39:54 +02:00
|
|
|
|
|
|
|
assert(m);
|
2018-10-08 15:47:53 +02:00
|
|
|
assert(device);
|
logind: listen actively for session devices
Session compositors need access to fbdev, DRM and evdev devices if they
control a session. To make logind pass them to sessions, we need to
listen for them actively.
However, we avoid creating new seats for non master-of-seat devices. Only
once a seat is created, we start remembering all other session devices. If
the last master-device is removed (even if there are other non-master
devices still available), we destroy the seat. This is the current
behavior, but we need to explicitly implement it now as there may be
non-master devices in the seat->devices list.
Unlike master devices, we don't care whether our list of non-master
devices is complete. We don't export this list but use it only as cache if
sessions request these devices. Hence, if a session requests a device that
is not in the list, we will simply look it up. However, once a session
requested a device, we must be notified of "remove" udev events. So we
must link the devices somehow into the device-list.
Regarding the implementation, we now sort the device list by the "master"
flag. This guarantees that master devices are at the front and non-master
devices at the tail of the list. Thus, we can easily test whether a seat
has a master device attached.
2013-09-17 17:39:54 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
manager_process_seat_device(m, device);
|
2013-12-13 03:30:42 +01:00
|
|
|
return 0;
|
logind: listen actively for session devices
Session compositors need access to fbdev, DRM and evdev devices if they
control a session. To make logind pass them to sessions, we need to
listen for them actively.
However, we avoid creating new seats for non master-of-seat devices. Only
once a seat is created, we start remembering all other session devices. If
the last master-device is removed (even if there are other non-master
devices still available), we destroy the seat. This is the current
behavior, but we need to explicitly implement it now as there may be
non-master devices in the seat->devices list.
Unlike master devices, we don't care whether our list of non-master
devices is complete. We don't export this list but use it only as cache if
sessions request these devices. Hence, if a session requests a device that
is not in the list, we will simply look it up. However, once a session
requested a device, we must be notified of "remove" udev events. So we
must link the devices somehow into the device-list.
Regarding the implementation, we now sort the device list by the "master"
flag. This guarantees that master devices are at the front and non-master
devices at the tail of the list. Thus, we can easily test whether a seat
has a master device attached.
2013-09-17 17:39:54 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
static int manager_dispatch_vcsa_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) {
|
2013-11-05 01:10:21 +01:00
|
|
|
Manager *m = userdata;
|
2019-03-09 02:45:48 +01:00
|
|
|
const char *name;
|
2011-06-28 03:21:14 +02:00
|
|
|
|
|
|
|
assert(m);
|
2018-10-08 15:47:53 +02:00
|
|
|
assert(device);
|
2011-06-28 03:21:14 +02:00
|
|
|
|
|
|
|
/* Whenever a VCSA device is removed try to reallocate our
|
|
|
|
* VTs, to make sure our auto VTs never go away. */
|
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
if (sd_device_get_sysname(device, &name) >= 0 &&
|
2018-08-22 07:53:51 +02:00
|
|
|
startswith(name, "vcsa") &&
|
2019-03-09 02:45:48 +01:00
|
|
|
device_for_action(device, DEVICE_ACTION_REMOVE))
|
2013-12-13 03:30:42 +01:00
|
|
|
seat_preallocate_vts(m->seat0);
|
2011-06-28 03:21:14 +02:00
|
|
|
|
2013-12-13 03:30:42 +01:00
|
|
|
return 0;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
static int manager_dispatch_button_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) {
|
2013-11-05 01:10:21 +01:00
|
|
|
Manager *m = userdata;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
|
|
|
assert(m);
|
2018-10-08 15:47:53 +02:00
|
|
|
assert(device);
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
manager_process_button_device(m, device);
|
2013-12-13 03:30:42 +01:00
|
|
|
return 0;
|
2012-05-30 15:01:51 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int manager_dispatch_console(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
assert(m);
|
2013-09-17 17:39:59 +02:00
|
|
|
assert(m->seat0);
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(m->console_active_fd == fd);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2013-09-17 17:39:59 +02:00
|
|
|
seat_read_active_vt(m->seat0);
|
2011-05-23 23:55:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-17 12:39:16 +02:00
|
|
|
static int manager_reserve_vt(Manager *m) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
if (m->reserve_vt <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (asprintf(&p, "/dev/tty%u", m->reserve_vt) < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
m->reserve_vt_fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
|
|
|
|
if (m->reserve_vt_fd < 0) {
|
2012-09-17 19:11:48 +02:00
|
|
|
|
|
|
|
/* Don't complain on VT-less systems */
|
|
|
|
if (errno != ENOENT)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_warning_errno(errno, "Failed to pin reserved VT: %m");
|
2012-09-17 12:39:16 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int manager_connect_bus(Manager *m) {
|
|
|
|
int r;
|
2011-06-24 23:25:28 +02:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(m);
|
|
|
|
assert(!m->bus);
|
2011-06-24 23:25:28 +02:00
|
|
|
|
2013-11-11 22:00:48 +01:00
|
|
|
r = sd_bus_default_system(&m->bus);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to connect to system bus: %m");
|
2011-06-24 23:25:28 +02:00
|
|
|
|
2020-04-23 01:00:07 +02:00
|
|
|
r = bus_add_implementation(m->bus, &manager_object, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2020-04-23 01:00:07 +02:00
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2020-04-23 01:00:07 +02:00
|
|
|
r = bus_log_control_api_register(m->bus);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2020-04-23 01:00:07 +02:00
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2020-05-03 07:52:38 +02:00
|
|
|
r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "JobRemoved", match_job_removed, NULL, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2017-12-19 12:29:04 +01:00
|
|
|
return log_error_errno(r, "Failed to request match for JobRemoved: %m");
|
|
|
|
|
2020-05-03 07:52:38 +02:00
|
|
|
r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "UnitRemoved", match_unit_removed, NULL, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2017-12-19 12:29:04 +01:00
|
|
|
return log_error_errno(r, "Failed to request match for UnitRemoved: %m");
|
|
|
|
|
|
|
|
r = sd_bus_match_signal_async(
|
|
|
|
m->bus,
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.DBus.Properties",
|
|
|
|
"PropertiesChanged",
|
|
|
|
match_properties_changed, NULL, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2017-12-19 12:29:04 +01:00
|
|
|
return log_error_errno(r, "Failed to request match for PropertiesChanged: %m");
|
|
|
|
|
2020-05-03 07:52:38 +02:00
|
|
|
r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "Reloading", match_reloading, NULL, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2017-12-19 12:29:04 +01:00
|
|
|
return log_error_errno(r, "Failed to request match for Reloading: %m");
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2020-05-03 07:52:38 +02:00
|
|
|
r = bus_call_method_async(m->bus, NULL, bus_systemd_mgr, "Subscribe", NULL, NULL, NULL);
|
2017-12-20 16:40:47 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to enable subscription: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2017-12-18 22:10:11 +01:00
|
|
|
r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.login1", 0, NULL, NULL);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2017-12-18 22:10:11 +01:00
|
|
|
return log_error_errno(r, "Failed to request name: %m");
|
2011-06-27 23:07:28 +02:00
|
|
|
|
2016-05-04 19:01:56 +02:00
|
|
|
r = sd_bus_attach_event(m->bus, m->event, SD_EVENT_PRIORITY_NORMAL);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to attach bus to event loop: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-11 18:17:54 +02:00
|
|
|
static int manager_vt_switch(sd_event_source *src, const struct signalfd_siginfo *si, void *data) {
|
|
|
|
Manager *m = data;
|
|
|
|
Session *active, *iter;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We got a VT-switch signal and we have to acknowledge it immediately.
|
|
|
|
* Preferably, we'd just use m->seat0->active->vtfd, but unfortunately,
|
|
|
|
* old user-space might run multiple sessions on a single VT, *sigh*.
|
|
|
|
* Therefore, we have to iterate all sessions and find one with a vtfd
|
|
|
|
* on the requested VT.
|
|
|
|
* As only VTs with active controllers have VT_PROCESS set, our current
|
|
|
|
* notion of the active VT might be wrong (for instance if the switch
|
|
|
|
* happens while we setup VT_PROCESS). Therefore, read the current VT
|
|
|
|
* first and then use s->active->vtnr as reference. Note that this is
|
|
|
|
* not racy, as no further VT-switch can happen as long as we're in
|
|
|
|
* synchronous VT_PROCESS mode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert(m->seat0);
|
|
|
|
seat_read_active_vt(m->seat0);
|
|
|
|
|
|
|
|
active = m->seat0->active;
|
|
|
|
if (!active || active->vtnr < 1) {
|
2018-08-27 23:16:10 +02:00
|
|
|
_cleanup_close_ int fd = -1;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/* We are requested to acknowledge the VT-switch signal by the kernel but
|
|
|
|
* there's no registered sessions for the current VT. Normally this
|
|
|
|
* shouldn't happen but something wrong might have happened when we tried
|
|
|
|
* to release the VT. Better be safe than sorry, and try to release the VT
|
|
|
|
* one more time otherwise the user will be locked with the current VT. */
|
|
|
|
|
|
|
|
log_warning("Received VT_PROCESS signal without a registered session, restoring VT.");
|
|
|
|
|
|
|
|
/* At this point we only have the kernel mapping for referring to the
|
|
|
|
* current VT. */
|
|
|
|
fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
|
|
|
|
if (fd < 0) {
|
|
|
|
log_warning_errno(fd, "Failed to open, ignoring: %m");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = vt_release(fd, true);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning_errno(r, "Failed to release VT, ignoring: %m");
|
|
|
|
|
2014-08-11 18:17:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active->vtfd >= 0) {
|
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
|
|
|
session_leave_vt(active);
|
2014-08-11 18:17:54 +02:00
|
|
|
} else {
|
|
|
|
LIST_FOREACH(sessions_by_seat, iter, m->seat0->sessions) {
|
|
|
|
if (iter->vtnr == active->vtnr && iter->vtfd >= 0) {
|
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
|
|
|
session_leave_vt(iter);
|
2014-08-11 18:17:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
static int manager_connect_console(Manager *m) {
|
2013-11-05 01:10:21 +01:00
|
|
|
int r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(m->console_active_fd < 0);
|
|
|
|
|
2018-08-03 20:21:27 +02:00
|
|
|
/* On certain systems (such as S390, Xen, and containers) /dev/tty0 does not exist (as there is no VC), so
|
|
|
|
* don't fail if we can't open it. */
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (access("/dev/tty0", F_OK) < 0)
|
2012-04-13 17:54:33 +02:00
|
|
|
return 0;
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
m->console_active_fd = open("/sys/class/tty/tty0/active", O_RDONLY|O_NOCTTY|O_CLOEXEC);
|
|
|
|
if (m->console_active_fd < 0) {
|
2012-06-15 22:22:24 +02:00
|
|
|
|
2018-08-03 20:21:27 +02:00
|
|
|
/* On some systems /dev/tty0 may exist even though /sys/class/tty/tty0 does not. These are broken, but
|
|
|
|
* common. Let's complain but continue anyway. */
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
log_warning_errno(errno, "System has /dev/tty0 but not /sys/class/tty/tty0/active which is broken, ignoring: %m");
|
2012-06-15 22:22:24 +02:00
|
|
|
return 0;
|
2018-08-03 20:21:27 +02:00
|
|
|
}
|
2012-06-15 22:22:24 +02:00
|
|
|
|
2015-11-05 13:44:20 +01:00
|
|
|
return log_error_errno(errno, "Failed to open /sys/class/tty/tty0/active: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_event_add_io(m->event, &m->console_active_event_source, m->console_active_fd, 0, manager_dispatch_console, m);
|
2018-08-03 20:21:27 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to watch foreground console: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-08-11 18:17:54 +02:00
|
|
|
/*
|
|
|
|
* SIGRTMIN is used as global VT-release signal, SIGRTMIN + 1 is used
|
|
|
|
* as VT-acquire signal. We ignore any acquire-events (yes, we still
|
|
|
|
* have to provide a valid signal-number for it!) and acknowledge all
|
|
|
|
* release events immediately.
|
|
|
|
*/
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (SIGRTMIN + 1 > SIGRTMAX)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Not enough real-time signals available: %u-%u",
|
|
|
|
SIGRTMIN, SIGRTMAX);
|
2014-08-11 18:17:54 +02:00
|
|
|
|
2015-06-15 20:13:23 +02:00
|
|
|
assert_se(ignore_signals(SIGRTMIN + 1, -1) >= 0);
|
|
|
|
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN, -1) >= 0);
|
2014-08-11 18:17:54 +02:00
|
|
|
|
|
|
|
r = sd_event_add_signal(m->event, NULL, SIGRTMIN, manager_vt_switch, m);
|
|
|
|
if (r < 0)
|
2018-08-03 20:21:27 +02:00
|
|
|
return log_error_errno(r, "Failed to subscribe to signal: %m");
|
2014-08-11 18:17:54 +02:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int manager_connect_udev(Manager *m) {
|
2011-05-25 00:55:58 +02:00
|
|
|
int r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
assert(m);
|
2018-10-08 15:47:53 +02:00
|
|
|
assert(!m->device_seat_monitor);
|
|
|
|
assert(!m->device_monitor);
|
|
|
|
assert(!m->device_vcsa_monitor);
|
|
|
|
assert(!m->device_button_monitor);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_new(&m->device_seat_monitor);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_filter_add_match_tag(m->device_seat_monitor, "master-of-seat");
|
2011-05-25 00:55:58 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
r = sd_device_monitor_attach_event(m->device_seat_monitor, m->event);
|
2011-05-25 00:55:58 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
r = sd_device_monitor_start(m->device_seat_monitor, manager_dispatch_seat_udev, m);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
(void) sd_event_source_set_description(sd_device_monitor_get_event_source(m->device_seat_monitor), "logind-seat-monitor");
|
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_new(&m->device_monitor);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
logind: listen actively for session devices
Session compositors need access to fbdev, DRM and evdev devices if they
control a session. To make logind pass them to sessions, we need to
listen for them actively.
However, we avoid creating new seats for non master-of-seat devices. Only
once a seat is created, we start remembering all other session devices. If
the last master-device is removed (even if there are other non-master
devices still available), we destroy the seat. This is the current
behavior, but we need to explicitly implement it now as there may be
non-master devices in the seat->devices list.
Unlike master devices, we don't care whether our list of non-master
devices is complete. We don't export this list but use it only as cache if
sessions request these devices. Hence, if a session requests a device that
is not in the list, we will simply look it up. However, once a session
requested a device, we must be notified of "remove" udev events. So we
must link the devices somehow into the device-list.
Regarding the implementation, we now sort the device list by the "master"
flag. This guarantees that master devices are at the front and non-master
devices at the tail of the list. Thus, we can easily test whether a seat
has a master device attached.
2013-09-17 17:39:54 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "input", NULL);
|
logind: listen actively for session devices
Session compositors need access to fbdev, DRM and evdev devices if they
control a session. To make logind pass them to sessions, we need to
listen for them actively.
However, we avoid creating new seats for non master-of-seat devices. Only
once a seat is created, we start remembering all other session devices. If
the last master-device is removed (even if there are other non-master
devices still available), we destroy the seat. This is the current
behavior, but we need to explicitly implement it now as there may be
non-master devices in the seat->devices list.
Unlike master devices, we don't care whether our list of non-master
devices is complete. We don't export this list but use it only as cache if
sessions request these devices. Hence, if a session requests a device that
is not in the list, we will simply look it up. However, once a session
requested a device, we must be notified of "remove" udev events. So we
must link the devices somehow into the device-list.
Regarding the implementation, we now sort the device list by the "master"
flag. This guarantees that master devices are at the front and non-master
devices at the tail of the list. Thus, we can easily test whether a seat
has a master device attached.
2013-09-17 17:39:54 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "graphics", NULL);
|
logind: listen actively for session devices
Session compositors need access to fbdev, DRM and evdev devices if they
control a session. To make logind pass them to sessions, we need to
listen for them actively.
However, we avoid creating new seats for non master-of-seat devices. Only
once a seat is created, we start remembering all other session devices. If
the last master-device is removed (even if there are other non-master
devices still available), we destroy the seat. This is the current
behavior, but we need to explicitly implement it now as there may be
non-master devices in the seat->devices list.
Unlike master devices, we don't care whether our list of non-master
devices is complete. We don't export this list but use it only as cache if
sessions request these devices. Hence, if a session requests a device that
is not in the list, we will simply look it up. However, once a session
requested a device, we must be notified of "remove" udev events. So we
must link the devices somehow into the device-list.
Regarding the implementation, we now sort the device list by the "master"
flag. This guarantees that master devices are at the front and non-master
devices at the tail of the list. Thus, we can easily test whether a seat
has a master device attached.
2013-09-17 17:39:54 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "drm", NULL);
|
logind: listen actively for session devices
Session compositors need access to fbdev, DRM and evdev devices if they
control a session. To make logind pass them to sessions, we need to
listen for them actively.
However, we avoid creating new seats for non master-of-seat devices. Only
once a seat is created, we start remembering all other session devices. If
the last master-device is removed (even if there are other non-master
devices still available), we destroy the seat. This is the current
behavior, but we need to explicitly implement it now as there may be
non-master devices in the seat->devices list.
Unlike master devices, we don't care whether our list of non-master
devices is complete. We don't export this list but use it only as cache if
sessions request these devices. Hence, if a session requests a device that
is not in the list, we will simply look it up. However, once a session
requested a device, we must be notified of "remove" udev events. So we
must link the devices somehow into the device-list.
Regarding the implementation, we now sort the device list by the "master"
flag. This guarantees that master devices are at the front and non-master
devices at the tail of the list. Thus, we can easily test whether a seat
has a master device attached.
2013-09-17 17:39:54 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
r = sd_device_monitor_attach_event(m->device_monitor, m->event);
|
logind: listen actively for session devices
Session compositors need access to fbdev, DRM and evdev devices if they
control a session. To make logind pass them to sessions, we need to
listen for them actively.
However, we avoid creating new seats for non master-of-seat devices. Only
once a seat is created, we start remembering all other session devices. If
the last master-device is removed (even if there are other non-master
devices still available), we destroy the seat. This is the current
behavior, but we need to explicitly implement it now as there may be
non-master devices in the seat->devices list.
Unlike master devices, we don't care whether our list of non-master
devices is complete. We don't export this list but use it only as cache if
sessions request these devices. Hence, if a session requests a device that
is not in the list, we will simply look it up. However, once a session
requested a device, we must be notified of "remove" udev events. So we
must link the devices somehow into the device-list.
Regarding the implementation, we now sort the device list by the "master"
flag. This guarantees that master devices are at the front and non-master
devices at the tail of the list. Thus, we can easily test whether a seat
has a master device attached.
2013-09-17 17:39:54 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
r = sd_device_monitor_start(m->device_monitor, manager_dispatch_device_udev, m);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
logind: listen actively for session devices
Session compositors need access to fbdev, DRM and evdev devices if they
control a session. To make logind pass them to sessions, we need to
listen for them actively.
However, we avoid creating new seats for non master-of-seat devices. Only
once a seat is created, we start remembering all other session devices. If
the last master-device is removed (even if there are other non-master
devices still available), we destroy the seat. This is the current
behavior, but we need to explicitly implement it now as there may be
non-master devices in the seat->devices list.
Unlike master devices, we don't care whether our list of non-master
devices is complete. We don't export this list but use it only as cache if
sessions request these devices. Hence, if a session requests a device that
is not in the list, we will simply look it up. However, once a session
requested a device, we must be notified of "remove" udev events. So we
must link the devices somehow into the device-list.
Regarding the implementation, we now sort the device list by the "master"
flag. This guarantees that master devices are at the front and non-master
devices at the tail of the list. Thus, we can easily test whether a seat
has a master device attached.
2013-09-17 17:39:54 +02:00
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
(void) sd_event_source_set_description(sd_device_monitor_get_event_source(m->device_monitor), "logind-device-monitor");
|
|
|
|
|
2012-05-30 21:40:32 +02:00
|
|
|
/* Don't watch keys if nobody cares */
|
2018-02-09 17:37:39 +01:00
|
|
|
if (!manager_all_buttons_ignored(m)) {
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_new(&m->device_button_monitor);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_filter_add_match_tag(m->device_button_monitor, "power-switch");
|
2012-05-30 21:40:32 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_button_monitor, "input", NULL);
|
2012-05-30 21:40:32 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
r = sd_device_monitor_attach_event(m->device_button_monitor, m->event);
|
2012-05-30 21:40:32 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
r = sd_device_monitor_start(m->device_button_monitor, manager_dispatch_button_udev, m);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2018-11-10 14:50:11 +01:00
|
|
|
|
|
|
|
(void) sd_event_source_set_description(sd_device_monitor_get_event_source(m->device_button_monitor), "logind-button-monitor");
|
2012-05-30 21:40:32 +02:00
|
|
|
}
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2012-01-03 21:50:02 +01:00
|
|
|
/* Don't bother watching VCSA devices, if nobody cares */
|
2012-05-30 21:40:32 +02:00
|
|
|
if (m->n_autovts > 0 && m->console_active_fd >= 0) {
|
2011-06-28 03:21:14 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_new(&m->device_vcsa_monitor);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-06-28 03:21:14 +02:00
|
|
|
|
2018-10-08 15:47:53 +02:00
|
|
|
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_vcsa_monitor, "vc", NULL);
|
2012-05-30 21:40:32 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-06-28 03:21:14 +02:00
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
r = sd_device_monitor_attach_event(m->device_vcsa_monitor, m->event);
|
2012-05-30 21:40:32 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-06-28 03:21:14 +02:00
|
|
|
|
2018-11-10 14:50:11 +01:00
|
|
|
r = sd_device_monitor_start(m->device_vcsa_monitor, manager_dispatch_vcsa_udev, m);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2018-11-10 14:50:11 +01:00
|
|
|
|
|
|
|
(void) sd_event_source_set_description(sd_device_monitor_get_event_source(m->device_vcsa_monitor), "logind-vcsa-monitor");
|
2012-05-30 21:40:32 +02:00
|
|
|
}
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-11 19:47:12 +02:00
|
|
|
static void manager_gc(Manager *m, bool drop_not_started) {
|
2011-05-25 00:55:58 +02:00
|
|
|
Seat *seat;
|
|
|
|
Session *session;
|
|
|
|
User *user;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
while ((seat = m->seat_gc_queue)) {
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_REMOVE(gc_queue, m->seat_gc_queue, seat);
|
2011-05-25 00:55:58 +02:00
|
|
|
seat->in_gc_queue = false;
|
|
|
|
|
2018-02-15 13:14:35 +01:00
|
|
|
if (seat_may_gc(seat, drop_not_started)) {
|
2014-02-09 02:29:56 +01:00
|
|
|
seat_stop(seat, false);
|
2011-05-25 00:55:58 +02:00
|
|
|
seat_free(seat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((session = m->session_gc_queue)) {
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_REMOVE(gc_queue, m->session_gc_queue, session);
|
2011-05-25 00:55:58 +02:00
|
|
|
session->in_gc_queue = false;
|
|
|
|
|
2014-02-06 18:32:14 +01:00
|
|
|
/* First, if we are not closing yet, initiate stopping */
|
2018-02-15 13:14:35 +01:00
|
|
|
if (session_may_gc(session, drop_not_started) &&
|
2014-02-06 18:32:14 +01:00
|
|
|
session_get_state(session) != SESSION_CLOSING)
|
2018-08-06 19:04:49 +02:00
|
|
|
(void) session_stop(session, false);
|
2014-02-06 18:32:14 +01:00
|
|
|
|
2015-08-06 15:48:17 +02:00
|
|
|
/* Normally, this should make the session referenced
|
|
|
|
* again, if it doesn't then let's get rid of it
|
2014-02-06 18:32:14 +01:00
|
|
|
* immediately */
|
2018-02-15 13:14:35 +01:00
|
|
|
if (session_may_gc(session, drop_not_started)) {
|
2018-08-06 19:04:49 +02:00
|
|
|
(void) session_finalize(session);
|
2011-05-25 00:55:58 +02:00
|
|
|
session_free(session);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((user = m->user_gc_queue)) {
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_REMOVE(gc_queue, m->user_gc_queue, user);
|
2011-05-25 00:55:58 +02:00
|
|
|
user->in_gc_queue = false;
|
|
|
|
|
2014-02-13 18:31:43 +01:00
|
|
|
/* First step: queue stop jobs */
|
2018-02-15 13:14:35 +01:00
|
|
|
if (user_may_gc(user, drop_not_started))
|
2018-08-06 19:04:49 +02:00
|
|
|
(void) user_stop(user, false);
|
2014-02-06 18:32:14 +01:00
|
|
|
|
2014-02-13 18:31:43 +01:00
|
|
|
/* Second step: finalize user */
|
2018-02-15 13:14:35 +01:00
|
|
|
if (user_may_gc(user, drop_not_started)) {
|
2018-08-06 19:04:49 +02:00
|
|
|
(void) user_finalize(user);
|
2011-05-25 00:55:58 +02:00
|
|
|
user_free(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int manager_dispatch_idle_action(sd_event_source *s, uint64_t t, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2012-12-23 22:32:48 +01:00
|
|
|
struct dual_timestamp since;
|
2013-11-05 01:10:21 +01:00
|
|
|
usec_t n, elapse;
|
2012-12-23 22:32:48 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
if (m->idle_action == HANDLE_IGNORE ||
|
2013-11-05 01:10:21 +01:00
|
|
|
m->idle_action_usec <= 0)
|
|
|
|
return 0;
|
2012-12-23 22:32:48 +01:00
|
|
|
|
|
|
|
n = now(CLOCK_MONOTONIC);
|
|
|
|
|
|
|
|
r = manager_get_idle_hint(m, &since);
|
|
|
|
if (r <= 0)
|
2013-02-14 22:16:58 +01:00
|
|
|
/* Not idle. Let's check if after a timeout it might be idle then. */
|
2013-11-05 01:10:21 +01:00
|
|
|
elapse = n + m->idle_action_usec;
|
2012-12-23 22:32:48 +01:00
|
|
|
else {
|
|
|
|
/* Idle! Let's see if it's time to do something, or if
|
|
|
|
* we shall sleep for longer. */
|
|
|
|
|
|
|
|
if (n >= since.monotonic + m->idle_action_usec &&
|
|
|
|
(m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) {
|
2019-06-13 18:11:56 +02:00
|
|
|
log_info("System idle. Doing %s operation.", handle_action_to_string(m->idle_action));
|
2012-12-23 22:32:48 +01:00
|
|
|
|
|
|
|
manager_handle_action(m, 0, m->idle_action, false, false);
|
|
|
|
m->idle_action_not_before_usec = n;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
elapse = MAX(since.monotonic, m->idle_action_not_before_usec) + m->idle_action_usec;
|
2012-12-23 22:32:48 +01:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
if (!m->idle_action_event_source) {
|
2012-12-23 22:32:48 +01:00
|
|
|
|
2014-03-24 02:49:09 +01:00
|
|
|
r = sd_event_add_time(
|
|
|
|
m->event,
|
|
|
|
&m->idle_action_event_source,
|
|
|
|
CLOCK_MONOTONIC,
|
|
|
|
elapse, USEC_PER_SEC*30,
|
|
|
|
manager_dispatch_idle_action, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add idle event source: %m");
|
2012-12-23 22:32:48 +01:00
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
r = sd_event_source_set_priority(m->idle_action_event_source, SD_EVENT_PRIORITY_IDLE+10);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to set idle event source priority: %m");
|
2013-11-05 01:10:21 +01:00
|
|
|
} else {
|
|
|
|
r = sd_event_source_set_time(m->idle_action_event_source, elapse);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to set idle event timer: %m");
|
2012-12-23 22:32:48 +01:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_event_source_set_enabled(m->idle_action_event_source, SD_EVENT_ONESHOT);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to enable idle event timer: %m");
|
2012-12-23 22:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-05 01:10:21 +01:00
|
|
|
|
2016-04-09 21:03:19 +02:00
|
|
|
static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
manager_reset_config(m);
|
|
|
|
r = manager_parse_config_file(m);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning_errno(r, "Failed to parse config file, using defaults: %m");
|
|
|
|
else
|
|
|
|
log_info("Config file reloaded.");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-11 19:47:12 +02:00
|
|
|
static int manager_startup(Manager *m) {
|
2011-05-23 23:55:06 +02:00
|
|
|
int r;
|
2011-05-25 00:55:58 +02:00
|
|
|
Seat *seat;
|
|
|
|
Session *session;
|
|
|
|
User *user;
|
2014-02-21 21:10:00 +01:00
|
|
|
Button *button;
|
2012-04-16 16:47:33 +02:00
|
|
|
Inhibitor *inhibitor;
|
2011-05-25 00:55:58 +02:00
|
|
|
Iterator i;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
2016-04-09 21:03:19 +02:00
|
|
|
r = sd_event_add_signal(m->event, NULL, SIGHUP, manager_dispatch_reload_signal, m);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to register SIGHUP handler: %m");
|
|
|
|
|
2018-08-07 13:49:34 +02:00
|
|
|
/* Connect to utmp */
|
|
|
|
manager_connect_utmp(m);
|
|
|
|
|
2012-01-03 21:50:02 +01:00
|
|
|
/* Connect to console */
|
|
|
|
r = manager_connect_console(m);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2012-01-03 21:50:02 +01:00
|
|
|
/* Connect to udev */
|
|
|
|
r = manager_connect_udev(m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to create udev watchers: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
/* Connect to the bus */
|
|
|
|
r = manager_connect_bus(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
/* Instantiate magic seat 0 */
|
2013-09-17 17:39:59 +02:00
|
|
|
r = manager_add_seat(m, "seat0", &m->seat0);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add seat0: %m");
|
2011-05-25 00:55:58 +02:00
|
|
|
|
2015-03-06 14:37:09 +01:00
|
|
|
r = manager_set_lid_switch_ignore(m, 0 + m->holdoff_timeout_usec);
|
2014-03-03 20:49:33 +01:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "Failed to set up lid switch ignore event source: %m");
|
2014-03-03 20:49:33 +01:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
/* Deserialize state */
|
2013-08-24 16:52:23 +02:00
|
|
|
r = manager_enumerate_devices(m);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "Device enumeration failed: %m");
|
2013-08-24 16:52:23 +02:00
|
|
|
|
|
|
|
r = manager_enumerate_seats(m);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "Seat enumeration failed: %m");
|
2013-08-24 16:52:23 +02:00
|
|
|
|
|
|
|
r = manager_enumerate_users(m);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "User enumeration failed: %m");
|
2013-08-24 16:52:23 +02:00
|
|
|
|
|
|
|
r = manager_enumerate_sessions(m);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "Session enumeration failed: %m");
|
2013-08-24 16:52:23 +02:00
|
|
|
|
|
|
|
r = manager_enumerate_inhibitors(m);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "Inhibitor enumeration failed: %m");
|
2013-08-24 16:52:23 +02:00
|
|
|
|
|
|
|
r = manager_enumerate_buttons(m);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "Button enumeration failed: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2011-06-29 03:48:16 +02:00
|
|
|
/* Remove stale objects before we start them */
|
|
|
|
manager_gc(m, false);
|
|
|
|
|
2012-09-17 12:39:16 +02:00
|
|
|
/* Reserve the special reserved VT */
|
|
|
|
manager_reserve_vt(m);
|
|
|
|
|
2018-08-07 13:49:34 +02:00
|
|
|
/* Read in utmp if it exists */
|
|
|
|
manager_read_utmp(m);
|
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
/* And start everything */
|
|
|
|
HASHMAP_FOREACH(seat, m->seats, i)
|
2018-08-06 19:04:49 +02:00
|
|
|
(void) seat_start(seat);
|
2011-05-25 00:55:58 +02:00
|
|
|
|
|
|
|
HASHMAP_FOREACH(user, m->users, i)
|
2018-08-06 19:04:49 +02:00
|
|
|
(void) user_start(user);
|
2011-05-25 00:55:58 +02:00
|
|
|
|
|
|
|
HASHMAP_FOREACH(session, m->sessions, i)
|
2018-08-06 21:44:45 +02:00
|
|
|
(void) session_start(session, NULL, NULL);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2019-07-23 10:33:39 +02:00
|
|
|
HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
|
|
|
|
(void) inhibitor_start(inhibitor);
|
|
|
|
|
|
|
|
/* Let's see if the inhibitor is dead now, then remove it */
|
|
|
|
if (inhibitor_is_orphan(inhibitor)) {
|
|
|
|
inhibitor_stop(inhibitor);
|
|
|
|
inhibitor_free(inhibitor);
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
HASHMAP_FOREACH(button, m->buttons, i)
|
2014-02-24 16:22:23 +01:00
|
|
|
button_check_switches(button);
|
2014-02-21 21:10:00 +01:00
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
manager_dispatch_idle_action(NULL, 0, m);
|
2012-12-23 22:32:48 +01:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-11 19:47:12 +02:00
|
|
|
static int manager_run(Manager *m) {
|
2013-11-05 01:10:21 +01:00
|
|
|
int r;
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
assert(m);
|
|
|
|
|
|
|
|
for (;;) {
|
2013-11-05 01:10:21 +01:00
|
|
|
r = sd_event_get_state(m->event);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == SD_EVENT_FINISHED)
|
|
|
|
return 0;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2011-06-29 03:48:16 +02:00
|
|
|
manager_gc(m, true);
|
2011-05-25 00:55:58 +02:00
|
|
|
|
2015-06-08 22:58:50 +02:00
|
|
|
r = manager_dispatch_delayed(m, false);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r > 0)
|
|
|
|
continue;
|
|
|
|
|
2015-04-11 18:44:51 +02:00
|
|
|
r = sd_event_run(m->event, (uint64_t) -1);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 14:41:08 +01:00
|
|
|
static int run(int argc, char *argv[]) {
|
2018-04-24 18:13:12 +02:00
|
|
|
_cleanup_(manager_unrefp) Manager *m = NULL;
|
2011-05-24 04:20:35 +02:00
|
|
|
int r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2012-04-03 19:25:29 +02:00
|
|
|
log_set_facility(LOG_AUTH);
|
2018-11-20 11:18:22 +01:00
|
|
|
log_setup_service();
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2020-04-23 10:19:46 +02:00
|
|
|
r = service_parse_argv("systemd-logind.service",
|
|
|
|
"Manager for user logins and devices and privileged operations.",
|
2020-04-28 17:55:34 +02:00
|
|
|
BUS_IMPLEMENTATIONS(&manager_object,
|
|
|
|
&log_control_object),
|
2020-04-23 10:19:46 +02:00
|
|
|
argc, argv);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
2011-08-01 20:52:18 +02:00
|
|
|
|
2020-04-23 10:19:46 +02:00
|
|
|
umask(0022);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2016-03-02 02:35:55 +01:00
|
|
|
r = mac_selinux_init();
|
2018-11-16 14:41:08 +01:00
|
|
|
if (r < 0)
|
2020-06-19 19:29:24 +02:00
|
|
|
return r;
|
2016-02-02 20:07:46 +01:00
|
|
|
|
2018-04-24 18:16:14 +02:00
|
|
|
/* Always create the directories people can create inotify watches in. Note that some applications might check
|
|
|
|
* for the existence of /run/systemd/seats/ to determine whether logind is available, so please always make
|
|
|
|
* sure these directories are created early on and unconditionally. */
|
|
|
|
(void) mkdir_label("/run/systemd/seats", 0755);
|
|
|
|
(void) mkdir_label("/run/systemd/users", 0755);
|
|
|
|
(void) mkdir_label("/run/systemd/sessions", 0755);
|
2013-03-15 16:46:35 +01:00
|
|
|
|
logind: add SetBrightness() bus call for setting brightness of leds/backlight devices associated with a seat
This augments the drm/input device management by adding a single method
call for setting the brightness of an "leds" or "backlight" kernel class
device.
This method call requires no privileges to call, but a caller can only
change the brightness on sessions that are currently active, and they
must own the session.
This does not do enumeration of such class devices, feature or range
probing, chnage notification; it doesn't help associating graphics or
input devices with their backlight or leds devices. For all that clients
should go directly to udev/sysfs. The SetBrightness() call is just for
executing the actual change operation, that is otherwise privileged.
Example line:
busctl call org.freedesktop.login1 /org/freedesktop/login1/session/self org.freedesktop.login1.Session SetBrightness ssu "backlight" "intel_backlight" 200
The parameter the SetBrightness() call takes are the kernel subsystem
(i.e. "leds" or "backlight"), the device name, and the brightness
value.
On some hw setting the brightness is slow, and implementation and write
access to the sysfs knobs exposes this slowness. Due to this we'll fork
off a writer process in the background so that logind doesn't have to
block. Moreover, write requestes are coalesced: when a write request is
enqueued while one is already being executed it is queued. When another
write reques is then enqueued the earlier one is replaced by the newer
one, so that only one queued write request per device remains at any
time. Method replies are sent as soon as the first write request that
happens after the request was received is completed.
It is recommended that bus clients turn off the "expect_reply" flag on
the dbus messages they send though, that relieves logind from sending
completion notification and is particularly a good idea if clients
implement reactive UI sliders that send a quick secession of write
requests.
Replaces: #12413
2019-04-28 11:07:56 +02:00
|
|
|
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGHUP, SIGTERM, SIGINT, SIGCHLD, -1) >= 0);
|
2018-04-24 18:14:25 +02:00
|
|
|
|
2018-04-24 18:13:12 +02:00
|
|
|
r = manager_new(&m);
|
2018-11-16 14:41:08 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to allocate manager object: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-04-24 18:13:12 +02:00
|
|
|
(void) manager_parse_config_file(m);
|
2011-06-29 01:47:55 +02:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
r = manager_startup(m);
|
2018-11-16 14:41:08 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to fully start up daemon: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2017-07-20 16:19:18 +02:00
|
|
|
log_debug("systemd-logind running as pid "PID_FMT, getpid_cached());
|
2018-04-24 18:13:12 +02:00
|
|
|
(void) sd_notify(false,
|
|
|
|
"READY=1\n"
|
|
|
|
"STATUS=Processing requests...");
|
2012-01-04 20:40:04 +01:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
r = manager_run(m);
|
|
|
|
|
2017-07-20 16:19:18 +02:00
|
|
|
log_debug("systemd-logind stopped as pid "PID_FMT, getpid_cached());
|
2018-04-24 18:13:12 +02:00
|
|
|
(void) sd_notify(false,
|
|
|
|
"STOPPING=1\n"
|
|
|
|
"STATUS=Shutting down...");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2018-11-16 14:41:08 +01:00
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
2018-11-16 14:41:08 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|