2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2011-05-23 23:55:06 +02:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2011 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2011-05-23 23:55:06 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2011-05-23 23:55:06 +02:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-11-17 06:52:45 +01:00
|
|
|
#include "libudev.h"
|
2013-11-05 01:10:21 +01:00
|
|
|
#include "sd-daemon.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"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "bus-util.h"
|
2018-01-11 00:39:12 +01:00
|
|
|
#include "cgroup-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "conf-parser.h"
|
2015-11-03 12:26:12 +01:00
|
|
|
#include "def.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"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "logind.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"
|
2015-05-29 20:14:11 +02:00
|
|
|
#include "signal-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "strv.h"
|
|
|
|
#include "udev-util.h"
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2015-04-11 19:47:12 +02:00
|
|
|
static void manager_free(Manager *m);
|
|
|
|
|
2016-04-09 21:03:19 +02:00
|
|
|
static void manager_reset_config(Manager *m) {
|
2011-05-23 23:55:06 +02:00
|
|
|
m->n_autovts = 6;
|
2012-09-17 12:39:16 +02:00
|
|
|
m->reserve_vt = 6;
|
2014-03-14 01:38:19 +01:00
|
|
|
m->remove_ipc = true;
|
2012-05-05 00:34:48 +02:00
|
|
|
m->inhibit_delay_max = 5 * USEC_PER_SEC;
|
2012-09-19 13:10:10 +02:00
|
|
|
m->handle_power_key = HANDLE_POWEROFF;
|
2012-09-21 15:44:14 +02:00
|
|
|
m->handle_suspend_key = HANDLE_SUSPEND;
|
|
|
|
m->handle_hibernate_key = HANDLE_HIBERNATE;
|
2012-09-19 13:10:10 +02:00
|
|
|
m->handle_lid_switch = HANDLE_SUSPEND;
|
2018-02-09 17:37:39 +01:00
|
|
|
m->handle_lid_switch_ep = _HANDLE_ACTION_INVALID;
|
2014-08-26 22:08:02 +02:00
|
|
|
m->handle_lid_switch_docked = HANDLE_IGNORE;
|
2016-04-09 21:03:19 +02:00
|
|
|
m->power_key_ignore_inhibited = false;
|
|
|
|
m->suspend_key_ignore_inhibited = false;
|
|
|
|
m->hibernate_key_ignore_inhibited = false;
|
2012-09-19 13:10:10 +02:00
|
|
|
m->lid_switch_ignore_inhibited = true;
|
2016-04-09 21:03:19 +02:00
|
|
|
|
2015-03-06 14:37:09 +01:00
|
|
|
m->holdoff_timeout_usec = 30 * USEC_PER_SEC;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2012-12-23 22:32:48 +01:00
|
|
|
m->idle_action_usec = 30 * USEC_PER_MINUTE;
|
|
|
|
m->idle_action = HANDLE_IGNORE;
|
|
|
|
|
2016-06-08 20:45:32 +02:00
|
|
|
m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */
|
2016-08-19 04:57:53 +02:00
|
|
|
m->user_tasks_max = system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE, 100U); /* 33% */
|
2016-05-05 22:49:25 +02:00
|
|
|
m->sessions_max = 8192;
|
|
|
|
m->inhibitors_max = 8192;
|
2014-03-04 19:20:21 +01:00
|
|
|
|
2016-04-12 04:51:31 +02:00
|
|
|
m->kill_user_processes = KILL_USER_PROCESSES;
|
2016-04-09 21:03:19 +02:00
|
|
|
|
|
|
|
m->kill_only_users = strv_free(m->kill_only_users);
|
|
|
|
m->kill_exclude_users = strv_free(m->kill_exclude_users);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Manager *manager_new(void) {
|
|
|
|
Manager *m;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
m = new0(Manager, 1);
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
m->console_active_fd = -1;
|
|
|
|
m->reserve_vt_fd = -1;
|
|
|
|
|
|
|
|
m->idle_action_not_before_usec = now(CLOCK_MONOTONIC);
|
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
2015-08-05 15:58:39 +02:00
|
|
|
if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units)
|
2014-02-21 21:10:00 +01:00
|
|
|
goto fail;
|
2011-06-29 02:27:16 +02:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
m->udev = udev_new();
|
2014-02-21 21:10:00 +01:00
|
|
|
if (!m->udev)
|
|
|
|
goto fail;
|
2011-05-23 23:55:06 +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)
|
|
|
|
goto fail;
|
2013-11-05 01:10:21 +01:00
|
|
|
|
2013-12-11 18:14:52 +01:00
|
|
|
sd_event_set_watchdog(m->event, true);
|
|
|
|
|
2016-04-09 21:03:19 +02:00
|
|
|
manager_reset_config(m);
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
return m;
|
2014-02-21 21:10:00 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
manager_free(m);
|
|
|
|
return NULL;
|
2011-05-23 23:55:06 +02:00
|
|
|
}
|
|
|
|
|
2015-04-11 19:47:12 +02:00
|
|
|
static void manager_free(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)
|
|
|
|
return;
|
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);
|
|
|
|
hashmap_free(m->users);
|
|
|
|
hashmap_free(m->inhibitors);
|
2012-05-30 15:01:51 +02:00
|
|
|
hashmap_free(m->buttons);
|
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);
|
|
|
|
sd_event_source_unref(m->udev_seat_event_source);
|
|
|
|
sd_event_source_unref(m->udev_device_event_source);
|
|
|
|
sd_event_source_unref(m->udev_vcsa_event_source);
|
|
|
|
sd_event_source_unref(m->udev_button_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
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(m->console_active_fd);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2015-08-17 10:45:30 +02:00
|
|
|
udev_monitor_unref(m->udev_seat_monitor);
|
|
|
|
udev_monitor_unref(m->udev_device_monitor);
|
|
|
|
udev_monitor_unref(m->udev_vcsa_monitor);
|
|
|
|
udev_monitor_unref(m->udev_button_monitor);
|
|
|
|
|
|
|
|
udev_unref(m->udev);
|
2011-05-23 23:55:06 +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
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
sd_bus_unref(m->bus);
|
|
|
|
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);
|
2011-05-23 23:55:06 +02:00
|
|
|
free(m);
|
|
|
|
}
|
|
|
|
|
2013-11-08 18:11:09 +01:00
|
|
|
static int manager_enumerate_devices(Manager *m) {
|
2011-05-23 23:55:06 +02:00
|
|
|
struct udev_list_entry *item = NULL, *first = NULL;
|
2013-12-18 03:37:26 +01:00
|
|
|
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
|
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 */
|
|
|
|
|
|
|
|
e = udev_enumerate_new(m->udev);
|
2013-12-18 03:37:26 +01:00
|
|
|
if (!e)
|
|
|
|
return -ENOMEM;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2013-02-09 01:25:55 +01:00
|
|
|
r = udev_enumerate_add_match_tag(e, "master-of-seat");
|
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
|
|
|
|
2013-12-18 17:12:15 +01:00
|
|
|
r = udev_enumerate_add_match_is_initialized(e);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
r = udev_enumerate_scan_devices(e);
|
|
|
|
if (r < 0)
|
2013-12-18 03:37:26 +01:00
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
first = udev_enumerate_get_list_entry(e);
|
|
|
|
udev_list_entry_foreach(item, first) {
|
2013-12-18 03:37:26 +01:00
|
|
|
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
|
2011-05-23 23:55:06 +02:00
|
|
|
int k;
|
|
|
|
|
|
|
|
d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
|
2013-12-18 03:37:26 +01:00
|
|
|
if (!d)
|
|
|
|
return -ENOMEM;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
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) {
|
2013-12-18 03:37:26 +01:00
|
|
|
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
|
2012-05-30 15:01:51 +02:00
|
|
|
struct udev_list_entry *item = NULL, *first = NULL;
|
|
|
|
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;
|
|
|
|
|
2012-05-30 15:01:51 +02:00
|
|
|
e = udev_enumerate_new(m->udev);
|
2013-12-18 03:37:26 +01:00
|
|
|
if (!e)
|
|
|
|
return -ENOMEM;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
|
|
|
r = udev_enumerate_add_match_subsystem(e, "input");
|
|
|
|
if (r < 0)
|
2013-12-18 03:37:26 +01:00
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
|
|
|
r = udev_enumerate_add_match_tag(e, "power-switch");
|
|
|
|
if (r < 0)
|
2013-12-18 03:37:26 +01:00
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2013-12-18 17:12:15 +01:00
|
|
|
r = udev_enumerate_add_match_is_initialized(e);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2012-05-30 15:01:51 +02:00
|
|
|
r = udev_enumerate_scan_devices(e);
|
|
|
|
if (r < 0)
|
2013-12-18 03:37:26 +01:00
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
|
|
|
first = udev_enumerate_get_list_entry(e);
|
|
|
|
udev_list_entry_foreach(item, first) {
|
2013-12-18 03:37:26 +01:00
|
|
|
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
|
2012-05-30 15:01:51 +02:00
|
|
|
int k;
|
|
|
|
|
|
|
|
d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
|
2013-12-18 03:37:26 +01:00
|
|
|
if (!d)
|
|
|
|
return -ENOMEM;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (k < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_notice_errno(k, "Couldn't add lingering user %s: %m", de->d_name);
|
2011-05-25 00:55:58 +02:00
|
|
|
r = k;
|
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;
|
|
|
|
|
|
|
|
if (!dirent_is_file(de))
|
|
|
|
continue;
|
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
k = manager_add_user_by_name(m, de->d_name, &u);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (k < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(k, "Failed to add user by file name %s: %m", de->d_name);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2013-06-20 03:45:08 +02:00
|
|
|
r = k;
|
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;
|
|
|
|
unsigned int major, minor;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
parts = strv_split(fdname, "-");
|
|
|
|
if (!parts)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (strv_length(parts) != 5)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!streq(parts[0], "session"))
|
|
|
|
return -EINVAL;
|
|
|
|
id = strdup(parts[1]);
|
|
|
|
if (!id)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (!streq(parts[2], "device"))
|
|
|
|
return -EINVAL;
|
|
|
|
r = safe_atou(parts[3], &major) ||
|
|
|
|
safe_atou(parts[4], &minor);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-16 11:17:09 +01:00
|
|
|
static int manager_attach_fds(Manager *m) {
|
|
|
|
_cleanup_strv_free_ char **fdnames = NULL;
|
|
|
|
int n, i, fd;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
|
|
|
n = sd_listen_fds_with_names(true, &fdnames);
|
|
|
|
if (n <= 0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2018-03-06 16:59:38 +01:00
|
|
|
_cleanup_free_ char *id = NULL;
|
|
|
|
dev_t dev;
|
2017-03-16 11:17:09 +01:00
|
|
|
struct stat st;
|
|
|
|
SessionDevice *sd;
|
|
|
|
Session *s;
|
2018-03-06 16:59:38 +01:00
|
|
|
int r;
|
2017-03-16 11:17:09 +01:00
|
|
|
|
|
|
|
fd = SD_LISTEN_FDS_START + i;
|
|
|
|
|
2018-03-06 16:59:38 +01:00
|
|
|
r = parse_fdname(fdnames[i], &id, &dev);
|
|
|
|
if (r < 0) {
|
|
|
|
log_debug_errno(r, "Failed to parse fd name %s: %m", fdnames[i]);
|
|
|
|
close_nointr(fd);
|
2017-03-16 11:17:09 +01:00
|
|
|
continue;
|
2018-03-06 16:59:38 +01:00
|
|
|
}
|
2017-03-16 11:17:09 +01:00
|
|
|
|
|
|
|
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. */
|
|
|
|
log_debug("Failed to attach fd for unknown session: %s", id);
|
|
|
|
close_nointr(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0) {
|
|
|
|
/* The device is allowed to go away at a random point, in which
|
|
|
|
* case fstat failing is expected. */
|
|
|
|
log_debug_errno(errno, "Failed to stat device fd for session %s: %m", id);
|
|
|
|
close_nointr(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:59:38 +01:00
|
|
|
if (!S_ISCHR(st.st_mode) || st.st_rdev != dev) {
|
|
|
|
log_debug("Device fd doesn't point to the expected character device node");
|
2018-02-26 18:34:43 +01:00
|
|
|
close_nointr(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:59:38 +01:00
|
|
|
sd = hashmap_get(s->devices, &dev);
|
2017-03-16 11:17:09 +01:00
|
|
|
if (!sd) {
|
2018-02-26 18:31:06 +01:00
|
|
|
/* Weird, we got an fd for a session device which wasn't
|
2018-03-06 16:59:38 +01:00
|
|
|
* recorded in the session state file... */
|
2017-03-16 11:17:09 +01:00
|
|
|
log_warning("Got fd for missing session device [%u:%u] in session %s",
|
2018-03-06 16:59:38 +01:00
|
|
|
major(dev), minor(dev), s->id);
|
2017-03-16 11:17:09 +01:00
|
|
|
close_nointr(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug("Attaching fd to session device [%u:%u] for session %s",
|
2018-03-06 16:59:38 +01:00
|
|
|
major(dev), minor(dev), s->id);
|
2017-03-16 11:17:09 +01:00
|
|
|
|
|
|
|
session_device_attach_fd(sd, fd, s->was_active);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-16 04:26:56 +02:00
|
|
|
if (!session_id_valid(de->d_name)) {
|
|
|
|
log_warning("Invalid session file name '%s', ignoring.", de->d_name);
|
|
|
|
r = -EINVAL;
|
|
|
|
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) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(k, "Failed to add session by file name %s: %m", de->d_name);
|
2011-05-23 23:55:06 +02:00
|
|
|
r = k;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-16 11:17:09 +01:00
|
|
|
/* We might be restarted and PID1 could have sent us back the
|
|
|
|
* session device fds we previously saved. */
|
|
|
|
k = manager_attach_fds(m);
|
|
|
|
if (k < 0)
|
|
|
|
log_warning_errno(k, "Failed to reattach session device fds: %m");
|
|
|
|
|
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) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_notice_errno(k, "Couldn't add inhibitor %s: %m", de->d_name);
|
2013-06-20 03:45:08 +02:00
|
|
|
r = k;
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int manager_dispatch_seat_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
2013-12-13 03:30:42 +01:00
|
|
|
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
|
2013-11-05 01:10:21 +01:00
|
|
|
Manager *m = userdata;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
2011-06-28 03:21:14 +02:00
|
|
|
d = udev_monitor_receive_device(m->udev_seat_monitor);
|
2011-05-23 23:55:06 +02:00
|
|
|
if (!d)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-12-13 03:30:42 +01:00
|
|
|
manager_process_seat_device(m, d);
|
|
|
|
return 0;
|
2011-06-28 03:21:14 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int manager_dispatch_device_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
2013-12-13 03:30:42 +01:00
|
|
|
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
|
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);
|
|
|
|
|
|
|
|
d = udev_monitor_receive_device(m->udev_device_monitor);
|
|
|
|
if (!d)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-12-13 03:30:42 +01:00
|
|
|
manager_process_seat_device(m, d);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int manager_dispatch_vcsa_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
2013-12-13 03:30:42 +01:00
|
|
|
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
|
2013-11-05 01:10:21 +01:00
|
|
|
Manager *m = userdata;
|
2011-06-28 03:21:14 +02:00
|
|
|
const char *name;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
d = udev_monitor_receive_device(m->udev_vcsa_monitor);
|
|
|
|
if (!d)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
name = udev_device_get_sysname(d);
|
|
|
|
|
|
|
|
/* Whenever a VCSA device is removed try to reallocate our
|
|
|
|
* VTs, to make sure our auto VTs never go away. */
|
|
|
|
|
|
|
|
if (name && startswith(name, "vcsa") && streq_ptr(udev_device_get_action(d), "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
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int manager_dispatch_button_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
2013-12-13 03:30:42 +01:00
|
|
|
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
|
2013-11-05 01:10:21 +01:00
|
|
|
Manager *m = userdata;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
d = udev_monitor_receive_device(m->udev_button_monitor);
|
|
|
|
if (!d)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-12-13 03:30:42 +01:00
|
|
|
manager_process_button_device(m, d);
|
|
|
|
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
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/login1", "org.freedesktop.login1.Manager", manager_vtable, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add manager object vtable: %m");
|
2012-04-16 16:47:33 +02:00
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/seat", "org.freedesktop.login1.Seat", seat_vtable, seat_object_find, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add seat object vtable: %m");
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/seat", seat_node_enumerator, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add seat enumerator: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/session", "org.freedesktop.login1.Session", session_vtable, session_object_find, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add session object vtable: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/session", session_node_enumerator, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add session enumerator: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/user", "org.freedesktop.login1.User", user_vtable, user_object_find, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add user object vtable: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/user", user_node_enumerator, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add user enumerator: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2017-12-19 12:29:04 +01:00
|
|
|
r = sd_bus_match_signal_async(
|
|
|
|
m->bus,
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"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");
|
|
|
|
|
|
|
|
r = sd_bus_match_signal_async(
|
|
|
|
m->bus,
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"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");
|
|
|
|
|
|
|
|
r = sd_bus_match_signal_async(
|
|
|
|
m->bus,
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"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
|
|
|
|
2017-12-20 16:40:47 +01:00
|
|
|
r = sd_bus_call_method_async(
|
2013-07-02 01:46:30 +02:00
|
|
|
m->bus,
|
2017-12-20 16:40:47 +01:00
|
|
|
NULL,
|
2013-07-02 01:46:30 +02:00
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"Subscribe",
|
2017-12-20 16:40:47 +01:00
|
|
|
NULL, NULL,
|
|
|
|
NULL);
|
|
|
|
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) {
|
|
|
|
log_warning("Received VT_PROCESS signal without a registered session on that VT.");
|
|
|
|
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);
|
|
|
|
|
2012-04-13 17:54:33 +02:00
|
|
|
/* On certain architectures (S390 and Xen, and containers),
|
|
|
|
/dev/tty0 does not exist, 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
|
|
|
|
|
|
|
/* On some systems the device node /dev/tty0 may exist
|
|
|
|
* even though /sys/class/tty/tty0 does not. */
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
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);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to watch foreground console");
|
|
|
|
return r;
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (SIGRTMIN + 1 > SIGRTMAX) {
|
|
|
|
log_error("Not enough real-time signals available: %u-%u", SIGRTMIN, SIGRTMAX);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return r;
|
|
|
|
|
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);
|
2011-06-28 03:21:14 +02:00
|
|
|
assert(!m->udev_seat_monitor);
|
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->udev_device_monitor);
|
2011-06-28 03:21:14 +02:00
|
|
|
assert(!m->udev_vcsa_monitor);
|
2012-05-30 15:01:51 +02:00
|
|
|
assert(!m->udev_button_monitor);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2011-06-28 03:21:14 +02:00
|
|
|
m->udev_seat_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
|
|
|
|
if (!m->udev_seat_monitor)
|
2011-05-23 23:55:06 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-02-09 01:25:55 +01:00
|
|
|
r = udev_monitor_filter_add_match_tag(m->udev_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
|
|
|
|
2011-06-28 03:21:14 +02:00
|
|
|
r = udev_monitor_enable_receiving(m->udev_seat_monitor);
|
2011-05-25 00:55:58 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_event_add_io(m->event, &m->udev_seat_event_source, udev_monitor_get_fd(m->udev_seat_monitor), EPOLLIN, 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
|
|
|
|
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
|
|
|
m->udev_device_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
|
|
|
|
if (!m->udev_device_monitor)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_device_monitor, "input", NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_device_monitor, "graphics", NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_device_monitor, "drm", NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = udev_monitor_enable_receiving(m->udev_device_monitor);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_event_add_io(m->event, &m->udev_device_event_source, udev_monitor_get_fd(m->udev_device_monitor), EPOLLIN, 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
|
|
|
|
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)) {
|
2012-05-30 21:40:32 +02:00
|
|
|
m->udev_button_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
|
|
|
|
if (!m->udev_button_monitor)
|
|
|
|
return -ENOMEM;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2012-05-30 21:40:32 +02:00
|
|
|
r = udev_monitor_filter_add_match_tag(m->udev_button_monitor, "power-switch");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2012-05-30 21:40:32 +02:00
|
|
|
r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_button_monitor, "input", NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2012-05-30 21:40:32 +02:00
|
|
|
r = udev_monitor_enable_receiving(m->udev_button_monitor);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-05-30 15:01:51 +02:00
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_event_add_io(m->event, &m->udev_button_event_source, udev_monitor_get_fd(m->udev_button_monitor), EPOLLIN, manager_dispatch_button_udev, m);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
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
|
|
|
|
2012-05-30 21:40:32 +02:00
|
|
|
m->udev_vcsa_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
|
|
|
|
if (!m->udev_vcsa_monitor)
|
|
|
|
return -ENOMEM;
|
2011-06-28 03:21:14 +02:00
|
|
|
|
2012-05-30 21:40:32 +02:00
|
|
|
r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_vcsa_monitor, "vc", NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-06-28 03:21:14 +02:00
|
|
|
|
2012-05-30 21:40:32 +02:00
|
|
|
r = udev_monitor_enable_receiving(m->udev_vcsa_monitor);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-06-28 03:21:14 +02:00
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_event_add_io(m->event, &m->udev_vcsa_event_source, udev_monitor_get_fd(m->udev_vcsa_monitor), EPOLLIN, manager_dispatch_vcsa_udev, m);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
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)
|
2014-02-09 02:29:56 +01:00
|
|
|
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)) {
|
2013-08-13 17:59:28 +02:00
|
|
|
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))
|
2014-02-09 02:29:56 +01:00
|
|
|
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)) {
|
2013-08-13 17:59:28 +02:00
|
|
|
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)) {
|
|
|
|
log_info("System idle. Taking action.");
|
|
|
|
|
|
|
|
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_parse_config_file(Manager *m) {
|
|
|
|
assert(m);
|
|
|
|
|
2016-09-10 12:02:40 +02:00
|
|
|
return config_parse_many_nulstr(PKGSYSCONFDIR "/logind.conf",
|
2017-04-19 08:48:29 +02:00
|
|
|
CONF_PATHS_NULSTR("systemd/logind.conf.d"),
|
|
|
|
"Login\0",
|
|
|
|
config_item_perf_lookup, logind_gperf_lookup,
|
2017-11-09 00:26:11 +01:00
|
|
|
CONFIG_PARSE_WARN, m);
|
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
|
|
|
assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGHUP, -1) >= 0);
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
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);
|
|
|
|
|
2011-05-25 00:55:58 +02:00
|
|
|
/* And start everything */
|
|
|
|
HASHMAP_FOREACH(seat, m->seats, i)
|
|
|
|
seat_start(seat);
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(user, m->users, i)
|
|
|
|
user_start(user);
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(session, m->sessions, i)
|
|
|
|
session_start(session);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
2012-04-16 16:47:33 +02:00
|
|
|
HASHMAP_FOREACH(inhibitor, m->inhibitors, i)
|
|
|
|
inhibitor_start(inhibitor);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
Manager *m = NULL;
|
2011-05-24 04:20:35 +02:00
|
|
|
int r;
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
log_set_target(LOG_TARGET_AUTO);
|
2012-04-03 19:25:29 +02:00
|
|
|
log_set_facility(LOG_AUTH);
|
2011-05-23 23:55:06 +02:00
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
2011-08-01 20:52:18 +02:00
|
|
|
umask(0022);
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
if (argc != 1) {
|
|
|
|
log_error("This program takes no arguments.");
|
|
|
|
r = -EINVAL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2016-03-02 02:35:55 +01:00
|
|
|
r = mac_selinux_init();
|
2016-02-02 20:07:46 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "Could not initialize labelling: %m");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-03-15 16:46:35 +01:00
|
|
|
/* Always create the directories people can create inotify
|
|
|
|
* watches in. Note that some applications might check for the
|
2013-04-15 04:37:54 +02:00
|
|
|
* existence of /run/systemd/seats/ to determine whether
|
2013-03-15 16:46:35 +01:00
|
|
|
* logind is available, so please always make sure this check
|
|
|
|
* stays in. */
|
|
|
|
mkdir_label("/run/systemd/seats", 0755);
|
|
|
|
mkdir_label("/run/systemd/users", 0755);
|
|
|
|
mkdir_label("/run/systemd/sessions", 0755);
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
m = manager_new();
|
|
|
|
if (!m) {
|
2012-07-25 23:55:59 +02:00
|
|
|
r = log_oom();
|
2011-05-23 23:55:06 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2011-06-29 01:47:55 +02:00
|
|
|
manager_parse_config_file(m);
|
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
r = manager_startup(m);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to fully start up daemon: %m");
|
2011-05-23 23:55:06 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2017-07-20 16:19:18 +02:00
|
|
|
log_debug("systemd-logind running as pid "PID_FMT, getpid_cached());
|
2012-01-04 20:40:04 +01:00
|
|
|
|
|
|
|
sd_notify(false,
|
|
|
|
"READY=1\n"
|
|
|
|
"STATUS=Processing requests...");
|
|
|
|
|
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());
|
2012-01-04 20:40:04 +01:00
|
|
|
|
2011-05-23 23:55:06 +02:00
|
|
|
finish:
|
2012-01-04 20:40:04 +01:00
|
|
|
sd_notify(false,
|
2014-08-21 17:19:28 +02:00
|
|
|
"STOPPING=1\n"
|
2012-01-04 20:40:04 +01:00
|
|
|
"STATUS=Shutting down...");
|
|
|
|
|
2015-08-17 10:45:30 +02:00
|
|
|
manager_free(m);
|
2011-05-23 23:55:06 +02:00
|
|
|
|
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|