| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-bus.h" |
| 7 |
#include "sd-daemon.h" |
| 8 |
#include "sd-device.h" |
| 9 |
#include "sd-event.h" |
| 10 |
|
| 11 |
#include "alloc-util.h" |
| 12 |
#include "bus-locator.h" |
| 13 |
#include "bus-log-control-api.h" |
| 14 |
#include "bus-object.h" |
| 15 |
#include "common-signal.h" |
| 16 |
#include "daemon-util.h" |
| 17 |
#include "device-util.h" |
| 18 |
#include "devnum-util.h" |
| 19 |
#include "dirent-util.h" |
| 20 |
#include "errno-util.h" |
| 21 |
#include "escape.h" |
| 22 |
#include "fd-util.h" |
| 23 |
#include "format-util.h" |
| 24 |
#include "fs-util.h" |
| 25 |
#include "hashmap.h" |
| 26 |
#include "label-util.h" |
| 27 |
#include "logind-session.h" |
| 28 |
#include "logind.h" |
| 29 |
#include "logind-button.h" |
| 30 |
#include "logind-dbus.h" |
| 31 |
#include "logind-device.h" |
| 32 |
#include "logind-seat.h" |
| 33 |
#include "logind-session-device.h" |
| 34 |
#include "logind-user.h" |
| 35 |
#include "logind-utmp.h" |
| 36 |
#include "logind-varlink.h" |
| 37 |
#include "main-func.h" |
| 38 |
#include "mkdir.h" |
| 39 |
#include "parse-util.h" |
| 40 |
#include "process-util.h" |
| 41 |
#include "service-util.h" |
| 42 |
#include "signal-util.h" |
| 43 |
#include "stat-util.h" |
| 44 |
#include "strv.h" |
| 45 |
#include "terminal-util.h" |
| 46 |
#include "udev-util.h" |
| 47 |
#include "user-util.h" |
| 48 |
|
| 49 |
static Manager* manager_free(Manager *m); |
| 50 |
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free); |
| 51 |
|
| 52 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(device_hash_ops, char, string_hash_func, string_compare_func, Device, device_free); |
| 53 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(seat_hash_ops, char, string_hash_func, string_compare_func, Seat, seat_free); |
| 54 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(session_hash_ops, char, string_hash_func, string_compare_func, Session, session_free); |
| 55 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(user_hash_ops, void, trivial_hash_func, trivial_compare_func, User, user_free); |
| 56 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(inhibitor_hash_ops, char, string_hash_func, string_compare_func, Inhibitor, inhibitor_free); |
| 57 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(button_hash_ops, char, string_hash_func, string_compare_func, Button, button_free); |
| 58 |
|
| 59 |
static int manager_new(Manager **ret) { |
| 60 |
_cleanup_(manager_freep) Manager *m = NULL; |
| 61 |
int r; |
| 62 |
|
| 63 |
assert(ret); |
| 64 |
|
| 65 |
m = new(Manager, 1); |
| 66 |
if (!m) |
| 67 |
return -ENOMEM; |
| 68 |
|
| 69 |
*m = (Manager) { |
| 70 |
.console_active_fd = -EBADF, |
| 71 |
.reserve_vt_fd = -EBADF, |
| 72 |
.idle_action_not_before_usec = now(CLOCK_MONOTONIC), |
| 73 |
.scheduled_shutdown_action = _HANDLE_ACTION_INVALID, |
| 74 |
|
| 75 |
.devices = hashmap_new(&device_hash_ops), |
| 76 |
.seats = hashmap_new(&seat_hash_ops), |
| 77 |
.sessions = hashmap_new(&session_hash_ops), |
| 78 |
.users = hashmap_new(&user_hash_ops), |
| 79 |
.inhibitors = hashmap_new(&inhibitor_hash_ops), |
| 80 |
.buttons = hashmap_new(&button_hash_ops), |
| 81 |
|
| 82 |
.user_units = hashmap_new(&string_hash_ops), |
| 83 |
.session_units = hashmap_new(&string_hash_ops), |
| 84 |
}; |
| 85 |
|
| 86 |
if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units) |
| 87 |
return -ENOMEM; |
| 88 |
|
| 89 |
r = sd_event_default(&m->event); |
| 90 |
if (r < 0) |
| 91 |
return r; |
| 92 |
|
| 93 |
r = sd_event_set_signal_exit(m->event, true); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
|
| 97 |
r = sd_event_add_signal(m->event, /* ret= */ NULL, (SIGRTMIN+18)|SD_EVENT_SIGNAL_PROCMASK, sigrtmin18_handler, /* userdata= */ NULL); |
| 98 |
if (r < 0) |
| 99 |
return r; |
| 100 |
|
| 101 |
r = sd_event_add_memory_pressure(m->event, NULL, NULL, NULL); |
| 102 |
if (r < 0) |
| 103 |
log_debug_errno(r, "Failed to allocate memory pressure event source, ignoring: %m"); |
| 104 |
|
| 105 |
(void) sd_event_set_watchdog(m->event, true); |
| 106 |
|
| 107 |
dual_timestamp_now(&m->init_ts); |
| 108 |
|
| 109 |
manager_reset_config(m); |
| 110 |
|
| 111 |
*ret = TAKE_PTR(m); |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
static Manager* manager_free(Manager *m) { |
| 116 |
if (!m) |
| 117 |
return NULL; |
| 118 |
|
| 119 |
hashmap_free(m->devices); |
| 120 |
hashmap_free(m->seats); |
| 121 |
hashmap_free(m->sessions); |
| 122 |
|
| 123 |
/* All records should have been removed by session_free */ |
| 124 |
assert(hashmap_isempty(m->sessions_by_leader)); |
| 125 |
hashmap_free(m->sessions_by_leader); |
| 126 |
|
| 127 |
hashmap_free(m->users); |
| 128 |
hashmap_free(m->inhibitors); |
| 129 |
hashmap_free(m->buttons); |
| 130 |
hashmap_free(m->brightness_writers); |
| 131 |
|
| 132 |
hashmap_free(m->user_units); |
| 133 |
hashmap_free(m->session_units); |
| 134 |
|
| 135 |
sd_event_source_unref(m->idle_action_event_source); |
| 136 |
sd_event_source_unref(m->inhibit_timeout_source); |
| 137 |
sd_event_source_unref(m->scheduled_shutdown_timeout_source); |
| 138 |
sd_event_source_unref(m->nologin_timeout_source); |
| 139 |
sd_event_source_unref(m->wall_message_timeout_source); |
| 140 |
|
| 141 |
sd_event_source_unref(m->console_active_event_source); |
| 142 |
sd_event_source_unref(m->lid_switch_ignore_event_source); |
| 143 |
|
| 144 |
sd_event_source_unref(m->reboot_key_long_press_event_source); |
| 145 |
|
| 146 |
#if ENABLE_UTMP |
| 147 |
sd_event_source_unref(m->utmp_event_source); |
| 148 |
#endif |
| 149 |
|
| 150 |
safe_close(m->console_active_fd); |
| 151 |
|
| 152 |
sd_device_monitor_unref(m->device_seat_monitor); |
| 153 |
sd_device_monitor_unref(m->device_monitor); |
| 154 |
sd_device_monitor_unref(m->device_vcsa_monitor); |
| 155 |
sd_device_monitor_unref(m->device_button_monitor); |
| 156 |
sd_device_monitor_unref(m->device_uaccess_monitor); |
| 157 |
|
| 158 |
if (m->unlink_nologin) |
| 159 |
(void) unlink_or_warn("/run/nologin"); |
| 160 |
|
| 161 |
hashmap_free(m->polkit_registry); |
| 162 |
|
| 163 |
manager_varlink_done(m); |
| 164 |
|
| 165 |
sd_bus_flush_close_unref(m->bus); |
| 166 |
sd_event_unref(m->event); |
| 167 |
|
| 168 |
safe_close(m->reserve_vt_fd); |
| 169 |
|
| 170 |
strv_free(m->kill_only_users); |
| 171 |
strv_free(m->kill_exclude_users); |
| 172 |
|
| 173 |
free(m->scheduled_shutdown_tty); |
| 174 |
free(m->wall_message); |
| 175 |
free(m->action_job); |
| 176 |
|
| 177 |
strv_free(m->efi_boot_loader_entries); |
| 178 |
free(m->efi_loader_entry_one_shot); |
| 179 |
|
| 180 |
return mfree(m); |
| 181 |
} |
| 182 |
|
| 183 |
static int manager_enumerate_devices(Manager *m) { |
| 184 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 185 |
int r; |
| 186 |
|
| 187 |
assert(m); |
| 188 |
|
| 189 |
/* Loads devices from udev and creates seats for them as |
| 190 |
* necessary */ |
| 191 |
|
| 192 |
r = sd_device_enumerator_new(&e); |
| 193 |
if (r < 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
r = sd_device_enumerator_add_match_tag(e, "master-of-seat"); |
| 197 |
if (r < 0) |
| 198 |
return r; |
| 199 |
|
| 200 |
r = 0; |
| 201 |
|
| 202 |
FOREACH_DEVICE(e, d) { |
| 203 |
if (device_is_processed(d) <= 0) |
| 204 |
continue; |
| 205 |
RET_GATHER(r, manager_process_seat_device(m, d)); |
| 206 |
} |
| 207 |
|
| 208 |
return r; |
| 209 |
} |
| 210 |
|
| 211 |
static int manager_enumerate_buttons(Manager *m) { |
| 212 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 213 |
int r; |
| 214 |
|
| 215 |
assert(m); |
| 216 |
|
| 217 |
/* Loads buttons from udev */ |
| 218 |
|
| 219 |
if (manager_all_buttons_ignored(m)) |
| 220 |
return 0; |
| 221 |
|
| 222 |
r = sd_device_enumerator_new(&e); |
| 223 |
if (r < 0) |
| 224 |
return r; |
| 225 |
|
| 226 |
r = sd_device_enumerator_add_match_subsystem(e, "input", true); |
| 227 |
if (r < 0) |
| 228 |
return r; |
| 229 |
|
| 230 |
r = sd_device_enumerator_add_match_tag(e, "power-switch"); |
| 231 |
if (r < 0) |
| 232 |
return r; |
| 233 |
|
| 234 |
r = 0; |
| 235 |
|
| 236 |
FOREACH_DEVICE(e, d) { |
| 237 |
if (device_is_processed(d) <= 0) |
| 238 |
continue; |
| 239 |
RET_GATHER(r, manager_process_button_device(m, d, /* ret_button= */ NULL)); |
| 240 |
} |
| 241 |
|
| 242 |
return r; |
| 243 |
} |
| 244 |
|
| 245 |
static int manager_enumerate_seats(Manager *m) { |
| 246 |
_cleanup_closedir_ DIR *d = NULL; |
| 247 |
int r = 0; |
| 248 |
|
| 249 |
assert(m); |
| 250 |
|
| 251 |
/* This loads data about seats stored on disk, but does not |
| 252 |
* actually create any seats. Removes data of seats that no |
| 253 |
* longer exist. */ |
| 254 |
|
| 255 |
d = opendir("/run/systemd/seats"); |
| 256 |
if (!d) { |
| 257 |
if (errno == ENOENT) |
| 258 |
return 0; |
| 259 |
|
| 260 |
return log_error_errno(errno, "Failed to open %s: %m", "/run/systemd/seats/"); |
| 261 |
} |
| 262 |
|
| 263 |
FOREACH_DIRENT(de, d, return -errno) { |
| 264 |
Seat *s; |
| 265 |
|
| 266 |
if (!dirent_is_file(de)) |
| 267 |
continue; |
| 268 |
|
| 269 |
s = hashmap_get(m->seats, de->d_name); |
| 270 |
if (!s) { |
| 271 |
if (unlinkat(dirfd(d), de->d_name, 0) < 0) |
| 272 |
log_warning_errno(errno, "Failed to remove /run/systemd/seats/%s, ignoring: %m", |
| 273 |
de->d_name); |
| 274 |
continue; |
| 275 |
} |
| 276 |
|
| 277 |
RET_GATHER(r, seat_load(s)); |
| 278 |
} |
| 279 |
|
| 280 |
return r; |
| 281 |
} |
| 282 |
|
| 283 |
static int manager_enumerate_linger_users(Manager *m) { |
| 284 |
_cleanup_closedir_ DIR *d = NULL; |
| 285 |
int r = 0; |
| 286 |
|
| 287 |
assert(m); |
| 288 |
|
| 289 |
d = opendir("/var/lib/systemd/linger"); |
| 290 |
if (!d) { |
| 291 |
if (errno == ENOENT) |
| 292 |
return 0; |
| 293 |
|
| 294 |
return log_error_errno(errno, "Failed to open %s: %m", "/var/lib/systemd/linger/"); |
| 295 |
} |
| 296 |
|
| 297 |
FOREACH_DIRENT(de, d, return -errno) { |
| 298 |
_cleanup_free_ char *n = NULL; |
| 299 |
int k; |
| 300 |
|
| 301 |
if (!dirent_is_file(de)) |
| 302 |
continue; |
| 303 |
|
| 304 |
k = cunescape(de->d_name, 0, &n); |
| 305 |
if (k < 0) { |
| 306 |
RET_GATHER(r, log_warning_errno(k, "Failed to unescape username '%s', ignoring: %m", de->d_name)); |
| 307 |
continue; |
| 308 |
} |
| 309 |
|
| 310 |
k = manager_add_user_by_name(m, n, NULL); |
| 311 |
if (k < 0) |
| 312 |
RET_GATHER(r, log_warning_errno(k, "Couldn't add lingering user %s, ignoring: %m", de->d_name)); |
| 313 |
} |
| 314 |
|
| 315 |
return r; |
| 316 |
} |
| 317 |
|
| 318 |
static int manager_enumerate_users(Manager *m) { |
| 319 |
_cleanup_closedir_ DIR *d = NULL; |
| 320 |
int r; |
| 321 |
|
| 322 |
assert(m); |
| 323 |
|
| 324 |
/* Add lingering users */ |
| 325 |
r = manager_enumerate_linger_users(m); |
| 326 |
|
| 327 |
/* Read in user data stored on disk */ |
| 328 |
d = opendir("/run/systemd/users"); |
| 329 |
if (!d) { |
| 330 |
if (errno == ENOENT) |
| 331 |
return 0; |
| 332 |
|
| 333 |
return log_error_errno(errno, "Failed to open %s: %m", "/run/systemd/users/"); |
| 334 |
} |
| 335 |
|
| 336 |
FOREACH_DIRENT(de, d, return -errno) { |
| 337 |
User *u; |
| 338 |
uid_t uid; |
| 339 |
int k; |
| 340 |
|
| 341 |
if (!dirent_is_file(de)) |
| 342 |
continue; |
| 343 |
|
| 344 |
k = parse_uid(de->d_name, &uid); |
| 345 |
if (k < 0) { |
| 346 |
RET_GATHER(r, log_warning_errno(k, "Failed to parse filename /run/systemd/users/%s as UID, ignoring: %m", de->d_name)); |
| 347 |
continue; |
| 348 |
} |
| 349 |
|
| 350 |
k = manager_add_user_by_uid(m, uid, &u); |
| 351 |
if (k < 0) { |
| 352 |
RET_GATHER(r, log_warning_errno(k, "Failed to add user by filename %s, ignoring: %m", de->d_name)); |
| 353 |
continue; |
| 354 |
} |
| 355 |
|
| 356 |
user_add_to_gc_queue(u); |
| 357 |
|
| 358 |
RET_GATHER(r, user_load(u)); |
| 359 |
} |
| 360 |
|
| 361 |
return r; |
| 362 |
} |
| 363 |
|
| 364 |
static int parse_fdname(const char *fdname, char **ret_session_id, dev_t *ret_devno) { |
| 365 |
_cleanup_strv_free_ char **parts = NULL; |
| 366 |
_cleanup_free_ char *id = NULL; |
| 367 |
int r; |
| 368 |
|
| 369 |
assert(ret_session_id); |
| 370 |
assert(ret_devno); |
| 371 |
|
| 372 |
parts = strv_split(fdname, "-"); |
| 373 |
if (!parts) |
| 374 |
return -ENOMEM; |
| 375 |
|
| 376 |
if (_unlikely_(!streq(parts[0], "session"))) |
| 377 |
return -EINVAL; |
| 378 |
|
| 379 |
id = strdup(parts[1]); |
| 380 |
if (!id) |
| 381 |
return -ENOMEM; |
| 382 |
|
| 383 |
if (streq(parts[2], "leader")) { |
| 384 |
*ret_session_id = TAKE_PTR(id); |
| 385 |
*ret_devno = 0; |
| 386 |
|
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
if (_unlikely_(!streq(parts[2], "device"))) |
| 391 |
return -EINVAL; |
| 392 |
|
| 393 |
unsigned major, minor; |
| 394 |
|
| 395 |
r = safe_atou(parts[3], &major); |
| 396 |
if (r < 0) |
| 397 |
return r; |
| 398 |
|
| 399 |
r = safe_atou(parts[4], &minor); |
| 400 |
if (r < 0) |
| 401 |
return r; |
| 402 |
|
| 403 |
*ret_session_id = TAKE_PTR(id); |
| 404 |
*ret_devno = makedev(major, minor); |
| 405 |
|
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
static int deliver_session_device_fd(Session *s, const char *fdname, int fd, dev_t devno) { |
| 410 |
SessionDevice *sd; |
| 411 |
struct stat st; |
| 412 |
|
| 413 |
assert(s); |
| 414 |
assert(fdname); |
| 415 |
assert(fd >= 0); |
| 416 |
assert(devno > 0); |
| 417 |
|
| 418 |
if (fstat(fd, &st) < 0) |
| 419 |
/* The device is allowed to go away at a random point, in which case fstat() failing is |
| 420 |
* expected. */ |
| 421 |
return log_debug_errno(errno, "Failed to stat device fd '%s' for session '%s': %m", |
| 422 |
fdname, s->id); |
| 423 |
|
| 424 |
if (!S_ISCHR(st.st_mode) || st.st_rdev != devno) |
| 425 |
return log_debug_errno(SYNTHETIC_ERRNO(ENODEV), |
| 426 |
"Device fd '%s' doesn't point to the expected character device node.", |
| 427 |
fdname); |
| 428 |
|
| 429 |
sd = hashmap_get(s->devices, &devno); |
| 430 |
if (!sd) |
| 431 |
/* Weird, we got an fd for a session device which wasn't recorded in the session state |
| 432 |
* file... */ |
| 433 |
return log_warning_errno(SYNTHETIC_ERRNO(ENODEV), |
| 434 |
"Got session device fd '%s' [" DEVNUM_FORMAT_STR "], but not present in session state.", |
| 435 |
fdname, DEVNUM_FORMAT_VAL(devno)); |
| 436 |
|
| 437 |
log_debug("Attaching session device fd '%s' [" DEVNUM_FORMAT_STR "] to session '%s'.", |
| 438 |
fdname, DEVNUM_FORMAT_VAL(devno), s->id); |
| 439 |
|
| 440 |
session_device_attach_fd(sd, fd, s->was_active); |
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
static int deliver_session_leader_fd_consume(Session *s, const char *fdname, int fd_consume) { |
| 445 |
_cleanup_close_ int fd = ASSERT_FD(fd_consume); |
| 446 |
_cleanup_(pidref_done) PidRef leader_fdstore = PIDREF_NULL; |
| 447 |
int r; |
| 448 |
|
| 449 |
assert(s); |
| 450 |
assert(fdname); |
| 451 |
|
| 452 |
/* Already deserialized via pidfd id? */ |
| 453 |
if (pidref_is_set(&s->leader)) { |
| 454 |
assert(s->leader.pid == s->deserialized_pid); |
| 455 |
assert(s->leader.fd >= 0); |
| 456 |
|
| 457 |
r = fd_inode_same(fd, s->leader.fd); |
| 458 |
if (r < 0) |
| 459 |
return log_warning_errno(r, "Failed to compare pidfd with deserialized leader for session '%s': %m", |
| 460 |
s->id); |
| 461 |
if (r > 0) |
| 462 |
return 0; |
| 463 |
|
| 464 |
log_warning("Got leader pidfd for session '%s' which mismatches with the deserialized process, resetting with pidfd.", |
| 465 |
s->id); |
| 466 |
|
| 467 |
} else if (!pid_is_valid(s->deserialized_pid)) { |
| 468 |
r = log_warning_errno(SYNTHETIC_ERRNO(EOWNERDEAD), |
| 469 |
"Got leader pidfd for session '%s', but LEADER= is not set, refusing.", |
| 470 |
s->id); |
| 471 |
goto fail_close; |
| 472 |
} |
| 473 |
|
| 474 |
r = pidref_set_pidfd_take(&leader_fdstore, fd); |
| 475 |
if (r < 0) { |
| 476 |
log_warning_errno(r, |
| 477 |
r == -ESRCH ? "Leader of session '%s' is gone while deserializing." |
| 478 |
: "Failed to create reference to leader of session '%s': %m", |
| 479 |
s->id); |
| 480 |
goto fail_close; |
| 481 |
} |
| 482 |
TAKE_FD(fd); |
| 483 |
|
| 484 |
if (leader_fdstore.pid != s->deserialized_pid) |
| 485 |
log_warning("Leader from pidfd (" PID_FMT ") doesn't match with LEADER=" PID_FMT " for session '%s', proceeding anyway.", |
| 486 |
leader_fdstore.pid, s->deserialized_pid, s->id); |
| 487 |
|
| 488 |
r = session_set_leader_consume(s, TAKE_PIDREF(leader_fdstore)); |
| 489 |
if (r < 0) |
| 490 |
return log_warning_errno(r, "Failed to attach leader pidfd for session '%s': %m", s->id); |
| 491 |
|
| 492 |
return 0; |
| 493 |
|
| 494 |
fail_close: |
| 495 |
close_and_notify_warn(TAKE_FD(fd), fdname); |
| 496 |
return r; |
| 497 |
} |
| 498 |
|
| 499 |
static int manager_attach_session_fd_one_consume(Manager *m, const char *fdname, int fd) { |
| 500 |
_cleanup_free_ char *id = NULL; |
| 501 |
dev_t devno = 0; /* Explicit initialization to appease gcc */ |
| 502 |
Session *s; |
| 503 |
int r; |
| 504 |
|
| 505 |
assert(m); |
| 506 |
assert(fdname); |
| 507 |
assert(fd >= 0); |
| 508 |
|
| 509 |
r = parse_fdname(fdname, &id, &devno); |
| 510 |
if (r < 0) { |
| 511 |
log_warning_errno(r, "Failed to parse stored fd name '%s': %m", fdname); |
| 512 |
goto fail_close; |
| 513 |
} |
| 514 |
|
| 515 |
s = hashmap_get(m->sessions, id); |
| 516 |
if (!s) { |
| 517 |
/* If the session doesn't exist anymore, let's simply close this fd. */ |
| 518 |
r = log_debug_errno(SYNTHETIC_ERRNO(ENXIO), |
| 519 |
"Cannot attach fd '%s' to unknown session '%s', ignoring.", fdname, id); |
| 520 |
goto fail_close; |
| 521 |
} |
| 522 |
|
| 523 |
if (devno > 0) { |
| 524 |
r = deliver_session_device_fd(s, fdname, fd, devno); |
| 525 |
if (r < 0) |
| 526 |
goto fail_close; |
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
/* Takes ownership of fd on both success and failure */ |
| 531 |
return deliver_session_leader_fd_consume(s, fdname, fd); |
| 532 |
|
| 533 |
fail_close: |
| 534 |
close_and_notify_warn(fd, fdname); |
| 535 |
return r; |
| 536 |
} |
| 537 |
|
| 538 |
static int manager_enumerate_sessions(Manager *m) { |
| 539 |
_cleanup_closedir_ DIR *d = NULL; |
| 540 |
int r = 0; |
| 541 |
|
| 542 |
assert(m); |
| 543 |
|
| 544 |
/* Read in session data stored on disk */ |
| 545 |
d = opendir("/run/systemd/sessions"); |
| 546 |
if (!d) { |
| 547 |
if (errno == ENOENT) |
| 548 |
return 0; |
| 549 |
|
| 550 |
return log_error_errno(errno, "Failed to open %s: %m", "/run/systemd/sessions/"); |
| 551 |
} |
| 552 |
|
| 553 |
FOREACH_DIRENT(de, d, return -errno) { |
| 554 |
Session *s; |
| 555 |
int k; |
| 556 |
|
| 557 |
if (!dirent_is_file(de)) |
| 558 |
continue; |
| 559 |
|
| 560 |
k = manager_add_session(m, de->d_name, &s); |
| 561 |
if (k < 0) { |
| 562 |
RET_GATHER(r, log_warning_errno(k, "Failed to add session by filename %s, ignoring: %m", de->d_name)); |
| 563 |
continue; |
| 564 |
} |
| 565 |
|
| 566 |
session_add_to_gc_queue(s); |
| 567 |
|
| 568 |
k = session_load(s); |
| 569 |
if (k < 0 && k != -ESRCH) |
| 570 |
RET_GATHER(r, log_warning_errno(k, "Failed to deserialize session '%s', ignoring: %m", s->id)); |
| 571 |
} |
| 572 |
|
| 573 |
return r; |
| 574 |
} |
| 575 |
|
| 576 |
static int manager_enumerate_fds(Manager *m, int *ret_varlink_fd) { |
| 577 |
_cleanup_strv_free_ char **fdnames = NULL; |
| 578 |
int varlink_fd = -EBADF, n, r = 0; |
| 579 |
|
| 580 |
assert(m); |
| 581 |
assert(ret_varlink_fd); |
| 582 |
|
| 583 |
n = sd_listen_fds_with_names(/* unset_environment= */ true, &fdnames); |
| 584 |
if (n < 0) |
| 585 |
return log_error_errno(n, "Failed to acquire passed fd list: %m"); |
| 586 |
|
| 587 |
for (int i = 0; i < n; i++) { |
| 588 |
int fd = SD_LISTEN_FDS_START + i; |
| 589 |
|
| 590 |
if (streq(fdnames[i], "varlink")) { |
| 591 |
assert(varlink_fd < 0); |
| 592 |
varlink_fd = fd; |
| 593 |
continue; |
| 594 |
} |
| 595 |
|
| 596 |
RET_GATHER(r, manager_attach_session_fd_one_consume(m, fdnames[i], fd)); |
| 597 |
} |
| 598 |
|
| 599 |
if (r >= 0) |
| 600 |
*ret_varlink_fd = varlink_fd; |
| 601 |
|
| 602 |
return r; |
| 603 |
} |
| 604 |
|
| 605 |
static int manager_enumerate_inhibitors(Manager *m) { |
| 606 |
_cleanup_closedir_ DIR *d = NULL; |
| 607 |
int r = 0; |
| 608 |
|
| 609 |
assert(m); |
| 610 |
|
| 611 |
d = opendir("/run/systemd/inhibit"); |
| 612 |
if (!d) { |
| 613 |
if (errno == ENOENT) |
| 614 |
return 0; |
| 615 |
|
| 616 |
return log_error_errno(errno, "Failed to open %s: %m", "/run/systemd/inhibit/"); |
| 617 |
} |
| 618 |
|
| 619 |
FOREACH_DIRENT(de, d, return -errno) { |
| 620 |
Inhibitor *i; |
| 621 |
int k; |
| 622 |
|
| 623 |
if (!dirent_is_file(de)) |
| 624 |
continue; |
| 625 |
|
| 626 |
k = manager_add_inhibitor(m, de->d_name, &i); |
| 627 |
if (k < 0) { |
| 628 |
RET_GATHER(r, log_warning_errno(k, "Couldn't add inhibitor %s, ignoring: %m", de->d_name)); |
| 629 |
continue; |
| 630 |
} |
| 631 |
|
| 632 |
RET_GATHER(r, inhibitor_load(i)); |
| 633 |
} |
| 634 |
|
| 635 |
return r; |
| 636 |
} |
| 637 |
|
| 638 |
static int manager_dispatch_seat_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
| 639 |
Manager *m = ASSERT_PTR(userdata); |
| 640 |
int r; |
| 641 |
|
| 642 |
assert(device); |
| 643 |
|
| 644 |
/* If the event is triggered by us, do not try to start the relevant seat again. Otherwise, starting |
| 645 |
* the seat may trigger uevents again again again... */ |
| 646 |
r = manager_process_device_triggered_by_seat(m, device); |
| 647 |
if (r < 0) |
| 648 |
log_device_warning_errno(device, r, "Failed to process seat device event triggered by us, ignoring: %m"); |
| 649 |
if (r != 0) |
| 650 |
return 0; |
| 651 |
|
| 652 |
r = manager_process_seat_device(m, device); |
| 653 |
if (r < 0) |
| 654 |
log_device_warning_errno(device, r, "Failed to process seat device, ignoring: %m"); |
| 655 |
|
| 656 |
return 0; |
| 657 |
} |
| 658 |
|
| 659 |
static int manager_dispatch_device_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
| 660 |
Manager *m = ASSERT_PTR(userdata); |
| 661 |
int r; |
| 662 |
|
| 663 |
assert(device); |
| 664 |
|
| 665 |
/* If the device currently has "master-of-seat" tag, then it has been or will be processed by |
| 666 |
* manager_dispatch_seat_udev(). */ |
| 667 |
r = sd_device_has_current_tag(device, "master-of-seat"); |
| 668 |
if (r < 0) |
| 669 |
log_device_warning_errno(device, r, "Failed to check if the device currently has master-of-seat tag, ignoring: %m"); |
| 670 |
if (r != 0) |
| 671 |
return 0; |
| 672 |
|
| 673 |
/* If the event is triggered by us, do not try to start the relevant seat again. Otherwise, starting |
| 674 |
* the seat may trigger uevents again again again... */ |
| 675 |
r = manager_process_device_triggered_by_seat(m, device); |
| 676 |
if (r < 0) |
| 677 |
log_device_warning_errno(device, r, "Failed to process seat device event triggered by us, ignoring: %m"); |
| 678 |
if (r != 0) |
| 679 |
return 0; |
| 680 |
|
| 681 |
r = manager_process_seat_device(m, device); |
| 682 |
if (r < 0) |
| 683 |
log_device_warning_errno(device, r, "Failed to process seat device, ignoring: %m"); |
| 684 |
|
| 685 |
return 0; |
| 686 |
} |
| 687 |
|
| 688 |
static int manager_dispatch_uaccess_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
| 689 |
Manager *m = ASSERT_PTR(userdata); |
| 690 |
int r; |
| 691 |
|
| 692 |
assert(device); |
| 693 |
|
| 694 |
/* If the device currently has "master-of-seat" tag, then it has been or will be processed by |
| 695 |
* manager_dispatch_seat_udev(). */ |
| 696 |
r = sd_device_has_current_tag(device, "master-of-seat"); |
| 697 |
if (r < 0) |
| 698 |
log_device_warning_errno(device, r, "Failed to check if the device currently has master-of-seat tag, ignoring: %m"); |
| 699 |
if (r != 0) |
| 700 |
return 0; |
| 701 |
|
| 702 |
/* If the device is in input, graphics, or drm, then the event has been or will be processed by |
| 703 |
* manager_dispatch_device_udev(). */ |
| 704 |
r = device_in_subsystem(device, "input", "graphics", "drm"); |
| 705 |
if (r < 0) |
| 706 |
log_device_warning_errno(device, r, "Failed to check device subsystem, ignoring: %m"); |
| 707 |
if (r != 0) |
| 708 |
return 0; |
| 709 |
|
| 710 |
r = manager_process_device_triggered_by_seat(m, device); |
| 711 |
if (r < 0) |
| 712 |
log_device_warning_errno(device, r, "Failed to process seat device event triggered by us, ignoring: %m"); |
| 713 |
|
| 714 |
return 0; |
| 715 |
} |
| 716 |
|
| 717 |
static int manager_dispatch_vcsa_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
| 718 |
Manager *m = ASSERT_PTR(userdata); |
| 719 |
int r; |
| 720 |
|
| 721 |
assert(device); |
| 722 |
|
| 723 |
/* Whenever a VCSA device is removed try to reallocate our |
| 724 |
* VTs, to make sure our auto VTs never go away. */ |
| 725 |
|
| 726 |
r = device_sysname_startswith(device, "vcsa"); |
| 727 |
if (r < 0) { |
| 728 |
log_device_debug_errno(device, r, "Failed to check device sysname, ignoring: %m"); |
| 729 |
return 0; |
| 730 |
} |
| 731 |
if (r == 0) |
| 732 |
return 0; |
| 733 |
|
| 734 |
if (device_for_action(device, SD_DEVICE_REMOVE)) |
| 735 |
seat_preallocate_vts(m->seat0); |
| 736 |
|
| 737 |
return 0; |
| 738 |
} |
| 739 |
|
| 740 |
static int manager_dispatch_button_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
| 741 |
Manager *m = ASSERT_PTR(userdata); |
| 742 |
Button *b; |
| 743 |
|
| 744 |
assert(device); |
| 745 |
|
| 746 |
if (manager_process_button_device(m, device, &b) > 0) |
| 747 |
(void) button_check_switches(b); |
| 748 |
|
| 749 |
return 0; |
| 750 |
} |
| 751 |
|
| 752 |
static int manager_dispatch_console(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 753 |
Manager *m = ASSERT_PTR(userdata); |
| 754 |
|
| 755 |
assert(m->seat0); |
| 756 |
assert(m->console_active_fd == fd); |
| 757 |
|
| 758 |
seat_read_active_vt(m->seat0); |
| 759 |
return 0; |
| 760 |
} |
| 761 |
|
| 762 |
static int manager_reserve_vt(Manager *m) { |
| 763 |
_cleanup_free_ char *p = NULL; |
| 764 |
|
| 765 |
assert(m); |
| 766 |
|
| 767 |
if (m->reserve_vt <= 0) |
| 768 |
return 0; |
| 769 |
|
| 770 |
if (asprintf(&p, "/dev/tty%u", m->reserve_vt) < 0) |
| 771 |
return log_oom(); |
| 772 |
|
| 773 |
m->reserve_vt_fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); |
| 774 |
if (m->reserve_vt_fd < 0) { |
| 775 |
|
| 776 |
/* Don't complain on VT-less systems */ |
| 777 |
if (errno != ENOENT) |
| 778 |
log_warning_errno(errno, "Failed to pin reserved VT: %m"); |
| 779 |
return -errno; |
| 780 |
} |
| 781 |
|
| 782 |
return 0; |
| 783 |
} |
| 784 |
|
| 785 |
static int manager_connect_bus(Manager *m) { |
| 786 |
int r; |
| 787 |
|
| 788 |
assert(m); |
| 789 |
assert(!m->bus); |
| 790 |
|
| 791 |
r = sd_bus_default_system(&m->bus); |
| 792 |
if (r < 0) |
| 793 |
return log_error_errno(r, "Failed to connect to system bus: %m"); |
| 794 |
|
| 795 |
r = bus_add_implementation(m->bus, &manager_object, m); |
| 796 |
if (r < 0) |
| 797 |
return r; |
| 798 |
|
| 799 |
r = bus_log_control_api_register(m->bus); |
| 800 |
if (r < 0) |
| 801 |
return r; |
| 802 |
|
| 803 |
r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "JobRemoved", match_job_removed, NULL, m); |
| 804 |
if (r < 0) |
| 805 |
return log_error_errno(r, "Failed to request match for JobRemoved: %m"); |
| 806 |
|
| 807 |
r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "UnitRemoved", match_unit_removed, NULL, m); |
| 808 |
if (r < 0) |
| 809 |
return log_error_errno(r, "Failed to request match for UnitRemoved: %m"); |
| 810 |
|
| 811 |
r = sd_bus_match_signal_async( |
| 812 |
m->bus, |
| 813 |
NULL, |
| 814 |
"org.freedesktop.systemd1", |
| 815 |
NULL, |
| 816 |
"org.freedesktop.DBus.Properties", |
| 817 |
"PropertiesChanged", |
| 818 |
match_properties_changed, NULL, m); |
| 819 |
if (r < 0) |
| 820 |
return log_error_errno(r, "Failed to request match for PropertiesChanged: %m"); |
| 821 |
|
| 822 |
r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "Reloading", match_reloading, NULL, m); |
| 823 |
if (r < 0) |
| 824 |
return log_error_errno(r, "Failed to request match for Reloading: %m"); |
| 825 |
|
| 826 |
r = bus_call_method_async(m->bus, NULL, bus_systemd_mgr, "Subscribe", NULL, NULL, NULL); |
| 827 |
if (r < 0) |
| 828 |
return log_error_errno(r, "Failed to enable subscription: %m"); |
| 829 |
|
| 830 |
r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.login1", 0, NULL, NULL); |
| 831 |
if (r < 0) |
| 832 |
return log_error_errno(r, "Failed to request name: %m"); |
| 833 |
|
| 834 |
r = sd_bus_attach_event(m->bus, m->event, SD_EVENT_PRIORITY_NORMAL); |
| 835 |
if (r < 0) |
| 836 |
return log_error_errno(r, "Failed to attach bus to event loop: %m"); |
| 837 |
|
| 838 |
return 0; |
| 839 |
} |
| 840 |
|
| 841 |
static int manager_vt_switch(sd_event_source *src, const struct signalfd_siginfo *si, void *data) { |
| 842 |
Manager *m = ASSERT_PTR(data); |
| 843 |
Session *active; |
| 844 |
|
| 845 |
/* |
| 846 |
* We got a VT-switch signal and we have to acknowledge it immediately. |
| 847 |
* Preferably, we'd just use m->seat0->active->vtfd, but unfortunately, |
| 848 |
* old user-space might run multiple sessions on a single VT, *sigh*. |
| 849 |
* Therefore, we have to iterate all sessions and find one with a vtfd |
| 850 |
* on the requested VT. |
| 851 |
* As only VTs with active controllers have VT_PROCESS set, our current |
| 852 |
* notion of the active VT might be wrong (for instance if the switch |
| 853 |
* happens while we setup VT_PROCESS). Therefore, read the current VT |
| 854 |
* first and then use s->active->vtnr as reference. Note that this is |
| 855 |
* not racy, as no further VT-switch can happen as long as we're in |
| 856 |
* synchronous VT_PROCESS mode. |
| 857 |
*/ |
| 858 |
|
| 859 |
assert(m->seat0); |
| 860 |
|
| 861 |
seat_read_active_vt(m->seat0); |
| 862 |
|
| 863 |
active = m->seat0->active; |
| 864 |
if (!active || active->vtnr < 1) { |
| 865 |
_cleanup_close_ int fd = -EBADF; |
| 866 |
int r; |
| 867 |
|
| 868 |
/* We are requested to acknowledge the VT-switch signal by the kernel but |
| 869 |
* there's no registered sessions for the current VT. Normally this |
| 870 |
* shouldn't happen but something wrong might have happened when we tried |
| 871 |
* to release the VT. Better be safe than sorry, and try to release the VT |
| 872 |
* one more time otherwise the user will be locked with the current VT. */ |
| 873 |
|
| 874 |
log_warning("Received VT_PROCESS signal without a registered session, restoring VT."); |
| 875 |
|
| 876 |
/* At this point we only have the kernel mapping for referring to the current VT. */ |
| 877 |
fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); |
| 878 |
if (fd < 0) { |
| 879 |
log_warning_errno(fd, "Failed to open current VT, ignoring: %m"); |
| 880 |
return 0; |
| 881 |
} |
| 882 |
|
| 883 |
r = vt_release(fd, /* restore= */ true); |
| 884 |
if (r < 0) |
| 885 |
log_warning_errno(r, "Failed to release current VT, ignoring: %m"); |
| 886 |
|
| 887 |
return 0; |
| 888 |
} |
| 889 |
|
| 890 |
if (active->vtfd >= 0) |
| 891 |
session_leave_vt(active); |
| 892 |
else |
| 893 |
LIST_FOREACH(sessions_by_seat, iter, m->seat0->sessions) |
| 894 |
if (iter->vtnr == active->vtnr && iter->vtfd >= 0) { |
| 895 |
session_leave_vt(iter); |
| 896 |
break; |
| 897 |
} |
| 898 |
|
| 899 |
return 0; |
| 900 |
} |
| 901 |
|
| 902 |
static int manager_connect_console(Manager *m) { |
| 903 |
int r; |
| 904 |
|
| 905 |
assert(m); |
| 906 |
assert(m->console_active_fd < 0); |
| 907 |
|
| 908 |
/* On certain systems (such as S390, Xen, and containers) /dev/tty0 does not exist (as there is no VC), so |
| 909 |
* don't fail if we can't open it. */ |
| 910 |
|
| 911 |
if (access("/dev/tty0", F_OK) < 0) |
| 912 |
return 0; |
| 913 |
|
| 914 |
m->console_active_fd = open("/sys/class/tty/tty0/active", O_RDONLY|O_NOCTTY|O_CLOEXEC); |
| 915 |
if (m->console_active_fd < 0) { |
| 916 |
|
| 917 |
/* On some systems /dev/tty0 may exist even though /sys/class/tty/tty0 does not. These are broken, but |
| 918 |
* common. Let's complain but continue anyway. */ |
| 919 |
if (errno == ENOENT) { |
| 920 |
log_warning_errno(errno, "System has /dev/tty0 but not /sys/class/tty/tty0/active which is broken, ignoring: %m"); |
| 921 |
return 0; |
| 922 |
} |
| 923 |
|
| 924 |
return log_error_errno(errno, "Failed to open %s: %m", "/sys/class/tty/tty0/active"); |
| 925 |
} |
| 926 |
|
| 927 |
r = sd_event_add_io(m->event, &m->console_active_event_source, m->console_active_fd, 0, manager_dispatch_console, m); |
| 928 |
if (r < 0) |
| 929 |
return log_error_errno(r, "Failed to watch foreground console: %m"); |
| 930 |
|
| 931 |
/* |
| 932 |
* SIGRTMIN + 0 is used as global VT-release signal, SIGRTMIN + 1 is used |
| 933 |
* as VT-acquire signal. We ignore any acquire-events (yes, we still |
| 934 |
* have to provide a valid signal-number for it!) and acknowledge all |
| 935 |
* release events immediately. |
| 936 |
*/ |
| 937 |
|
| 938 |
if (SIGRTMIN + 1 > SIGRTMAX) |
| 939 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 940 |
"Not enough real-time signals available: %i-%i", |
| 941 |
SIGRTMIN, SIGRTMAX); |
| 942 |
|
| 943 |
assert_se(ignore_signals(SIGRTMIN + 1) >= 0); |
| 944 |
|
| 945 |
r = sd_event_add_signal(m->event, /* ret= */ NULL, (SIGRTMIN + 0) | SD_EVENT_SIGNAL_PROCMASK, manager_vt_switch, m); |
| 946 |
if (r < 0) |
| 947 |
return log_error_errno(r, "Failed to subscribe to SIGRTMIN+0 signal: %m"); |
| 948 |
|
| 949 |
return 0; |
| 950 |
} |
| 951 |
|
| 952 |
static int manager_connect_udev(Manager *m) { |
| 953 |
int r; |
| 954 |
|
| 955 |
assert(m); |
| 956 |
assert(!m->device_seat_monitor); |
| 957 |
assert(!m->device_monitor); |
| 958 |
assert(!m->device_uaccess_monitor); |
| 959 |
assert(!m->device_vcsa_monitor); |
| 960 |
assert(!m->device_button_monitor); |
| 961 |
|
| 962 |
r = sd_device_monitor_new(&m->device_seat_monitor); |
| 963 |
if (r < 0) |
| 964 |
return r; |
| 965 |
|
| 966 |
r = sd_device_monitor_filter_add_match_tag(m->device_seat_monitor, "master-of-seat"); |
| 967 |
if (r < 0) |
| 968 |
return r; |
| 969 |
|
| 970 |
r = sd_device_monitor_attach_event(m->device_seat_monitor, m->event); |
| 971 |
if (r < 0) |
| 972 |
return r; |
| 973 |
|
| 974 |
r = sd_device_monitor_start(m->device_seat_monitor, manager_dispatch_seat_udev, m); |
| 975 |
if (r < 0) |
| 976 |
return r; |
| 977 |
|
| 978 |
(void) sd_device_monitor_set_description(m->device_seat_monitor, "seat"); |
| 979 |
|
| 980 |
r = sd_device_monitor_new(&m->device_monitor); |
| 981 |
if (r < 0) |
| 982 |
return r; |
| 983 |
|
| 984 |
FOREACH_STRING(s, "input", "graphics", "drm") { |
| 985 |
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, s, /* devtype= */ NULL); |
| 986 |
if (r < 0) |
| 987 |
return r; |
| 988 |
} |
| 989 |
|
| 990 |
r = sd_device_monitor_attach_event(m->device_monitor, m->event); |
| 991 |
if (r < 0) |
| 992 |
return r; |
| 993 |
|
| 994 |
r = sd_device_monitor_start(m->device_monitor, manager_dispatch_device_udev, m); |
| 995 |
if (r < 0) |
| 996 |
return r; |
| 997 |
|
| 998 |
(void) sd_device_monitor_set_description(m->device_monitor, "input,graphics,drm"); |
| 999 |
|
| 1000 |
r = sd_device_monitor_new(&m->device_uaccess_monitor); |
| 1001 |
if (r < 0) |
| 1002 |
return r; |
| 1003 |
|
| 1004 |
r = sd_device_monitor_filter_add_match_tag(m->device_uaccess_monitor, "uaccess"); |
| 1005 |
if (r < 0) |
| 1006 |
return r; |
| 1007 |
|
| 1008 |
r = sd_device_monitor_attach_event(m->device_uaccess_monitor, m->event); |
| 1009 |
if (r < 0) |
| 1010 |
return r; |
| 1011 |
|
| 1012 |
r = sd_device_monitor_start(m->device_uaccess_monitor, manager_dispatch_uaccess_udev, m); |
| 1013 |
if (r < 0) |
| 1014 |
return r; |
| 1015 |
|
| 1016 |
(void) sd_device_monitor_set_description(m->device_uaccess_monitor, "uaccess"); |
| 1017 |
|
| 1018 |
/* Don't watch keys if nobody cares */ |
| 1019 |
if (!manager_all_buttons_ignored(m)) { |
| 1020 |
r = sd_device_monitor_new(&m->device_button_monitor); |
| 1021 |
if (r < 0) |
| 1022 |
return r; |
| 1023 |
|
| 1024 |
r = sd_device_monitor_filter_add_match_tag(m->device_button_monitor, "power-switch"); |
| 1025 |
if (r < 0) |
| 1026 |
return r; |
| 1027 |
|
| 1028 |
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_button_monitor, "input", NULL); |
| 1029 |
if (r < 0) |
| 1030 |
return r; |
| 1031 |
|
| 1032 |
r = sd_device_monitor_attach_event(m->device_button_monitor, m->event); |
| 1033 |
if (r < 0) |
| 1034 |
return r; |
| 1035 |
|
| 1036 |
r = sd_device_monitor_start(m->device_button_monitor, manager_dispatch_button_udev, m); |
| 1037 |
if (r < 0) |
| 1038 |
return r; |
| 1039 |
|
| 1040 |
(void) sd_device_monitor_set_description(m->device_button_monitor, "button"); |
| 1041 |
} |
| 1042 |
|
| 1043 |
/* Don't bother watching VCSA devices, if nobody cares */ |
| 1044 |
if (m->n_autovts > 0 && m->console_active_fd >= 0) { |
| 1045 |
|
| 1046 |
r = sd_device_monitor_new(&m->device_vcsa_monitor); |
| 1047 |
if (r < 0) |
| 1048 |
return r; |
| 1049 |
|
| 1050 |
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_vcsa_monitor, "vc", NULL); |
| 1051 |
if (r < 0) |
| 1052 |
return r; |
| 1053 |
|
| 1054 |
r = sd_device_monitor_attach_event(m->device_vcsa_monitor, m->event); |
| 1055 |
if (r < 0) |
| 1056 |
return r; |
| 1057 |
|
| 1058 |
r = sd_device_monitor_start(m->device_vcsa_monitor, manager_dispatch_vcsa_udev, m); |
| 1059 |
if (r < 0) |
| 1060 |
return r; |
| 1061 |
|
| 1062 |
(void) sd_device_monitor_set_description(m->device_vcsa_monitor, "vcsa"); |
| 1063 |
} |
| 1064 |
|
| 1065 |
return 0; |
| 1066 |
} |
| 1067 |
|
| 1068 |
static void manager_gc(Manager *m, bool drop_not_started) { |
| 1069 |
Seat *seat; |
| 1070 |
Session *session; |
| 1071 |
User *user; |
| 1072 |
|
| 1073 |
assert(m); |
| 1074 |
|
| 1075 |
while ((seat = LIST_POP(gc_queue, m->seat_gc_queue))) { |
| 1076 |
seat->in_gc_queue = false; |
| 1077 |
|
| 1078 |
if (seat_may_gc(seat, drop_not_started)) { |
| 1079 |
seat_stop(seat, /* force= */ false); |
| 1080 |
seat_free(seat); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
while ((session = LIST_POP(gc_queue, m->session_gc_queue))) { |
| 1085 |
session->in_gc_queue = false; |
| 1086 |
|
| 1087 |
/* First, if we are not closing yet, initiate stopping. */ |
| 1088 |
if (session_may_gc(session, drop_not_started) && |
| 1089 |
session_get_state(session) != SESSION_CLOSING) |
| 1090 |
(void) session_stop(session, /* force= */ false); |
| 1091 |
|
| 1092 |
/* Normally, this should make the session referenced again, if it doesn't then let's get rid |
| 1093 |
* of it immediately. */ |
| 1094 |
if (session_may_gc(session, drop_not_started)) { |
| 1095 |
(void) session_finalize(session); |
| 1096 |
session_free(session); |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
while ((user = LIST_POP(gc_queue, m->user_gc_queue))) { |
| 1101 |
user->in_gc_queue = false; |
| 1102 |
|
| 1103 |
/* First step: queue stop jobs */ |
| 1104 |
if (user_may_gc(user, drop_not_started)) |
| 1105 |
(void) user_stop(user, false); |
| 1106 |
|
| 1107 |
/* Second step: finalize user */ |
| 1108 |
if (user_may_gc(user, drop_not_started)) { |
| 1109 |
(void) user_finalize(user); |
| 1110 |
user_free(user); |
| 1111 |
} |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
static int manager_dispatch_idle_action(sd_event_source *s, uint64_t t, void *userdata) { |
| 1116 |
Manager *m = ASSERT_PTR(userdata); |
| 1117 |
struct dual_timestamp since; |
| 1118 |
usec_t n, elapse; |
| 1119 |
bool idle; |
| 1120 |
int r; |
| 1121 |
|
| 1122 |
if (m->idle_action == HANDLE_IGNORE || |
| 1123 |
m->idle_action_usec <= 0) |
| 1124 |
return 0; |
| 1125 |
|
| 1126 |
n = now(CLOCK_MONOTONIC); |
| 1127 |
|
| 1128 |
idle = manager_get_idle_hint(m, &since); |
| 1129 |
if (!idle) { |
| 1130 |
/* Not idle. Let's check if after a timeout it might be idle then. */ |
| 1131 |
elapse = n + m->idle_action_usec; |
| 1132 |
m->was_idle = false; |
| 1133 |
} else { |
| 1134 |
|
| 1135 |
/* Idle! Let's see if it's time to do something, or if |
| 1136 |
* we shall sleep for longer. */ |
| 1137 |
|
| 1138 |
if (n >= since.monotonic + m->idle_action_usec && |
| 1139 |
(m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) { |
| 1140 |
bool is_edge = false; |
| 1141 |
|
| 1142 |
/* We weren't idle previously or some activity happened while we were sleeping, and now we are |
| 1143 |
* idle. Let's remember that for the next time and make this an edge transition. */ |
| 1144 |
if (!m->was_idle || since.monotonic >= m->idle_action_not_before_usec) { |
| 1145 |
is_edge = true; |
| 1146 |
m->was_idle = true; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (m->idle_action == HANDLE_LOCK && !is_edge) |
| 1150 |
/* We are idle and we were before so we are actually not taking any action. */ |
| 1151 |
log_debug("System idle."); |
| 1152 |
else |
| 1153 |
log_info("System idle. Will %s now.", handle_action_verb_to_string(m->idle_action)); |
| 1154 |
|
| 1155 |
manager_handle_action(m, /* inhibit_key= */ 0, m->idle_action, /* ignore_inhibited= */ false, is_edge, /* action_seat= */ NULL); |
| 1156 |
m->idle_action_not_before_usec = n; |
| 1157 |
} |
| 1158 |
|
| 1159 |
elapse = MAX(since.monotonic, m->idle_action_not_before_usec) + m->idle_action_usec; |
| 1160 |
} |
| 1161 |
|
| 1162 |
if (!m->idle_action_event_source) { |
| 1163 |
|
| 1164 |
r = sd_event_add_time( |
| 1165 |
m->event, |
| 1166 |
&m->idle_action_event_source, |
| 1167 |
CLOCK_MONOTONIC, |
| 1168 |
elapse, MIN(USEC_PER_SEC*30, m->idle_action_usec), /* accuracy of 30s, but don't have an accuracy lower than the idle action timeout */ |
| 1169 |
manager_dispatch_idle_action, m); |
| 1170 |
if (r < 0) |
| 1171 |
return log_error_errno(r, "Failed to add idle event source: %m"); |
| 1172 |
|
| 1173 |
r = sd_event_source_set_priority(m->idle_action_event_source, SD_EVENT_PRIORITY_IDLE+10); |
| 1174 |
if (r < 0) |
| 1175 |
return log_error_errno(r, "Failed to set idle event source priority: %m"); |
| 1176 |
} else { |
| 1177 |
r = sd_event_source_set_time(m->idle_action_event_source, elapse); |
| 1178 |
if (r < 0) |
| 1179 |
return log_error_errno(r, "Failed to set idle event timer: %m"); |
| 1180 |
|
| 1181 |
r = sd_event_source_set_enabled(m->idle_action_event_source, SD_EVENT_ONESHOT); |
| 1182 |
if (r < 0) |
| 1183 |
return log_error_errno(r, "Failed to enable idle event timer: %m"); |
| 1184 |
} |
| 1185 |
|
| 1186 |
return 0; |
| 1187 |
} |
| 1188 |
|
| 1189 |
static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
| 1190 |
Manager *m = userdata; |
| 1191 |
int r; |
| 1192 |
|
| 1193 |
(void) notify_reloading(); |
| 1194 |
|
| 1195 |
manager_reset_config(m); |
| 1196 |
r = manager_parse_config_file(m); |
| 1197 |
if (r < 0) |
| 1198 |
log_warning_errno(r, "Failed to parse config file, using defaults: %m"); |
| 1199 |
else |
| 1200 |
log_info("Config file reloaded."); |
| 1201 |
|
| 1202 |
(void) sd_notify(/* unset_environment= */ false, NOTIFY_READY_MESSAGE); |
| 1203 |
return 0; |
| 1204 |
} |
| 1205 |
|
| 1206 |
static int manager_startup(Manager *m) { |
| 1207 |
_cleanup_close_ int varlink_fd = -EBADF; |
| 1208 |
int r; |
| 1209 |
Seat *seat; |
| 1210 |
Session *session; |
| 1211 |
User *user; |
| 1212 |
Button *button; |
| 1213 |
Inhibitor *inhibitor; |
| 1214 |
|
| 1215 |
assert(m); |
| 1216 |
|
| 1217 |
r = sd_event_add_signal(m->event, /* ret= */ NULL, SIGHUP|SD_EVENT_SIGNAL_PROCMASK, manager_dispatch_reload_signal, m); |
| 1218 |
if (r < 0) |
| 1219 |
return log_error_errno(r, "Failed to register SIGHUP handler: %m"); |
| 1220 |
|
| 1221 |
/* Connect to utmp */ |
| 1222 |
manager_connect_utmp(m); |
| 1223 |
|
| 1224 |
/* Connect to console */ |
| 1225 |
r = manager_connect_console(m); |
| 1226 |
if (r < 0) |
| 1227 |
return r; |
| 1228 |
|
| 1229 |
/* Connect to udev */ |
| 1230 |
r = manager_connect_udev(m); |
| 1231 |
if (r < 0) |
| 1232 |
return log_error_errno(r, "Failed to create udev watchers: %m"); |
| 1233 |
|
| 1234 |
/* Connect to the bus */ |
| 1235 |
r = manager_connect_bus(m); |
| 1236 |
if (r < 0) |
| 1237 |
return r; |
| 1238 |
|
| 1239 |
/* Instantiate magic seat 0 */ |
| 1240 |
r = manager_add_seat(m, "seat0", &m->seat0); |
| 1241 |
if (r < 0) |
| 1242 |
return log_error_errno(r, "Failed to add seat0: %m"); |
| 1243 |
|
| 1244 |
r = manager_set_lid_switch_ignore(m, 0 + m->holdoff_timeout_usec); |
| 1245 |
if (r < 0) |
| 1246 |
log_warning_errno(r, "Failed to set up lid switch ignore event source: %m"); |
| 1247 |
|
| 1248 |
/* Deserialize state */ |
| 1249 |
r = manager_enumerate_devices(m); |
| 1250 |
if (r < 0) |
| 1251 |
log_warning_errno(r, "Device enumeration failed: %m"); |
| 1252 |
|
| 1253 |
r = manager_enumerate_seats(m); |
| 1254 |
if (r < 0) |
| 1255 |
log_warning_errno(r, "Seat enumeration failed: %m"); |
| 1256 |
|
| 1257 |
r = manager_enumerate_users(m); |
| 1258 |
if (r < 0) |
| 1259 |
log_warning_errno(r, "User enumeration failed: %m"); |
| 1260 |
|
| 1261 |
r = manager_enumerate_sessions(m); |
| 1262 |
if (r < 0) |
| 1263 |
log_warning_errno(r, "Session enumeration failed: %m"); |
| 1264 |
|
| 1265 |
r = manager_enumerate_fds(m, &varlink_fd); |
| 1266 |
if (r < 0) |
| 1267 |
log_warning_errno(r, "File descriptor enumeration failed: %m"); |
| 1268 |
|
| 1269 |
r = manager_enumerate_inhibitors(m); |
| 1270 |
if (r < 0) |
| 1271 |
log_warning_errno(r, "Inhibitor enumeration failed: %m"); |
| 1272 |
|
| 1273 |
r = manager_enumerate_buttons(m); |
| 1274 |
if (r < 0) |
| 1275 |
log_warning_errno(r, "Button enumeration failed: %m"); |
| 1276 |
|
| 1277 |
r = manager_varlink_init(m, TAKE_FD(varlink_fd)); |
| 1278 |
if (r < 0) |
| 1279 |
return r; |
| 1280 |
|
| 1281 |
manager_load_scheduled_shutdown(m); |
| 1282 |
|
| 1283 |
/* Remove stale objects before we start them */ |
| 1284 |
manager_gc(m, false); |
| 1285 |
|
| 1286 |
/* Reserve the special reserved VT */ |
| 1287 |
manager_reserve_vt(m); |
| 1288 |
|
| 1289 |
/* Read in utmp if it exists */ |
| 1290 |
manager_read_utmp(m); |
| 1291 |
|
| 1292 |
/* And start everything */ |
| 1293 |
HASHMAP_FOREACH(seat, m->seats) |
| 1294 |
(void) seat_start(seat); |
| 1295 |
|
| 1296 |
HASHMAP_FOREACH(user, m->users) |
| 1297 |
(void) user_start(user); |
| 1298 |
|
| 1299 |
HASHMAP_FOREACH(session, m->sessions) |
| 1300 |
(void) session_start(session, NULL, NULL); |
| 1301 |
|
| 1302 |
HASHMAP_FOREACH(inhibitor, m->inhibitors) { |
| 1303 |
(void) inhibitor_start(inhibitor); |
| 1304 |
|
| 1305 |
/* Let's see if the inhibitor is dead now, then remove it */ |
| 1306 |
if (inhibitor_is_orphan(inhibitor)) { |
| 1307 |
inhibitor_stop(inhibitor); |
| 1308 |
inhibitor_free(inhibitor); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
HASHMAP_FOREACH(button, m->buttons) |
| 1313 |
button_check_switches(button); |
| 1314 |
|
| 1315 |
manager_dispatch_idle_action(NULL, 0, m); |
| 1316 |
|
| 1317 |
return 0; |
| 1318 |
} |
| 1319 |
|
| 1320 |
static int manager_run(Manager *m) { |
| 1321 |
int r; |
| 1322 |
|
| 1323 |
assert(m); |
| 1324 |
|
| 1325 |
for (;;) { |
| 1326 |
r = sd_event_get_state(m->event); |
| 1327 |
if (r < 0) |
| 1328 |
return r; |
| 1329 |
if (r == SD_EVENT_FINISHED) |
| 1330 |
return 0; |
| 1331 |
|
| 1332 |
manager_gc(m, true); |
| 1333 |
|
| 1334 |
r = manager_dispatch_delayed(m, false); |
| 1335 |
if (r < 0) |
| 1336 |
return r; |
| 1337 |
if (r > 0) |
| 1338 |
continue; |
| 1339 |
|
| 1340 |
r = sd_event_run(m->event, UINT64_MAX); |
| 1341 |
if (r < 0) |
| 1342 |
return r; |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
static int run(int argc, char *argv[]) { |
| 1347 |
_cleanup_(manager_freep) Manager *m = NULL; |
| 1348 |
_unused_ _cleanup_(notify_on_cleanup) const char *notify_message = NULL; |
| 1349 |
int r; |
| 1350 |
|
| 1351 |
log_set_facility(LOG_AUTH); |
| 1352 |
log_setup(); |
| 1353 |
|
| 1354 |
r = service_parse_argv("systemd-logind.service", |
| 1355 |
"Manager for user logins and devices and privileged operations.", |
| 1356 |
BUS_IMPLEMENTATIONS(&manager_object, |
| 1357 |
&log_control_object), |
| 1358 |
/* runtime_scope= */ NULL, |
| 1359 |
argc, argv); |
| 1360 |
if (r <= 0) |
| 1361 |
return r; |
| 1362 |
|
| 1363 |
umask(0022); |
| 1364 |
|
| 1365 |
r = mac_init(); |
| 1366 |
if (r < 0) |
| 1367 |
return r; |
| 1368 |
|
| 1369 |
/* Always create the directories people can create inotify watches in. Note that some applications |
| 1370 |
* might check for the existence of /run/systemd/seats/ to determine whether logind is available, so |
| 1371 |
* please always make sure these directories are created early on and unconditionally. */ |
| 1372 |
(void) mkdir_label("/run/systemd/seats", 0755); |
| 1373 |
(void) mkdir_label("/run/systemd/users", 0755); |
| 1374 |
(void) mkdir_label("/run/systemd/sessions", 0755); |
| 1375 |
|
| 1376 |
r = manager_new(&m); |
| 1377 |
if (r < 0) |
| 1378 |
return log_error_errno(r, "Failed to allocate manager object: %m"); |
| 1379 |
|
| 1380 |
(void) manager_parse_config_file(m); |
| 1381 |
|
| 1382 |
r = manager_startup(m); |
| 1383 |
if (r < 0) |
| 1384 |
return log_error_errno(r, "Failed to fully start up daemon: %m"); |
| 1385 |
|
| 1386 |
notify_message = notify_start(NOTIFY_READY_MESSAGE, NOTIFY_STOPPING_MESSAGE); |
| 1387 |
return manager_run(m); |
| 1388 |
} |
| 1389 |
|
| 1390 |
DEFINE_MAIN_FUNCTION(run); |