| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/kd.h> |
| 5 |
#include <linux/vt.h> |
| 6 |
#include <signal.h> |
| 7 |
#include <sys/ioctl.h> |
| 8 |
#include <sys/stat.h> |
| 9 |
#include <unistd.h> |
| 10 |
|
| 11 |
#include "sd-bus.h" |
| 12 |
#include "sd-device.h" |
| 13 |
#include "sd-event.h" |
| 14 |
#include "sd-messages.h" |
| 15 |
#include "sd-varlink.h" |
| 16 |
|
| 17 |
#include "alloc-util.h" |
| 18 |
#include "audit-util.h" |
| 19 |
#include "bus-error.h" |
| 20 |
#include "bus-util.h" |
| 21 |
#include "daemon-util.h" |
| 22 |
#include "device-util.h" |
| 23 |
#include "devnum-util.h" |
| 24 |
#include "env-file.h" |
| 25 |
#include "errno-util.h" |
| 26 |
#include "extract-word.h" |
| 27 |
#include "fd-util.h" |
| 28 |
#include "format-util.h" |
| 29 |
#include "fs-util.h" |
| 30 |
#include "hashmap.h" |
| 31 |
#include "json-util.h" |
| 32 |
#include "login-util.h" |
| 33 |
#include "logind.h" |
| 34 |
#include "logind-dbus.h" |
| 35 |
#include "logind-seat.h" |
| 36 |
#include "logind-seat-dbus.h" |
| 37 |
#include "logind-session.h" |
| 38 |
#include "logind-session-dbus.h" |
| 39 |
#include "logind-session-device.h" |
| 40 |
#include "logind-user.h" |
| 41 |
#include "logind-user-dbus.h" |
| 42 |
#include "logind-varlink.h" |
| 43 |
#include "mkdir.h" |
| 44 |
#include "parse-util.h" |
| 45 |
#include "path-util.h" |
| 46 |
#include "process-util.h" |
| 47 |
#include "serialize.h" |
| 48 |
#include "string-table.h" |
| 49 |
#include "strv.h" |
| 50 |
#include "terminal-util.h" |
| 51 |
#include "tmpfile-util.h" |
| 52 |
#include "user-record.h" |
| 53 |
#include "user-util.h" |
| 54 |
|
| 55 |
#define RELEASE_USEC (20*USEC_PER_SEC) |
| 56 |
|
| 57 |
static void session_restore_vt(Session *s); |
| 58 |
|
| 59 |
int session_new(Manager *m, const char *id, Session **ret) { |
| 60 |
_cleanup_(session_freep) Session *s = NULL; |
| 61 |
int r; |
| 62 |
|
| 63 |
assert(m); |
| 64 |
assert(id); |
| 65 |
assert(ret); |
| 66 |
|
| 67 |
if (!session_id_valid(id)) |
| 68 |
return -EINVAL; |
| 69 |
|
| 70 |
s = new(Session, 1); |
| 71 |
if (!s) |
| 72 |
return -ENOMEM; |
| 73 |
|
| 74 |
*s = (Session) { |
| 75 |
.manager = m, |
| 76 |
.id = strdup(id), |
| 77 |
.state_file = path_join("/run/systemd/sessions/", id), |
| 78 |
.vtfd = -EBADF, |
| 79 |
.audit_id = AUDIT_SESSION_INVALID, |
| 80 |
.tty_validity = _TTY_VALIDITY_INVALID, |
| 81 |
.leader = PIDREF_NULL, |
| 82 |
}; |
| 83 |
if (!s->id || !s->state_file) |
| 84 |
return -ENOMEM; |
| 85 |
|
| 86 |
s->devices = hashmap_new(&devt_hash_ops); |
| 87 |
if (!s->devices) |
| 88 |
return -ENOMEM; |
| 89 |
|
| 90 |
r = hashmap_put(m->sessions, s->id, s); |
| 91 |
if (r < 0) |
| 92 |
return r; |
| 93 |
|
| 94 |
*ret = TAKE_PTR(s); |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
static int session_dispatch_leader_pidfd(sd_event_source *es, int fd, uint32_t revents, void *userdata) { |
| 99 |
Session *s = ASSERT_PTR(userdata); |
| 100 |
|
| 101 |
assert(s->leader.fd == fd); |
| 102 |
|
| 103 |
s->leader_pidfd_event_source = sd_event_source_unref(s->leader_pidfd_event_source); |
| 104 |
|
| 105 |
session_stop(s, /* force= */ false); |
| 106 |
|
| 107 |
session_add_to_gc_queue(s); |
| 108 |
|
| 109 |
return 1; |
| 110 |
} |
| 111 |
|
| 112 |
static int session_watch_pidfd(Session *s) { |
| 113 |
int r; |
| 114 |
|
| 115 |
assert(s); |
| 116 |
assert(s->manager); |
| 117 |
assert(pidref_is_set(&s->leader)); |
| 118 |
assert(s->leader.fd >= 0); |
| 119 |
assert(!s->leader_pidfd_event_source); |
| 120 |
|
| 121 |
r = sd_event_add_io(s->manager->event, &s->leader_pidfd_event_source, s->leader.fd, EPOLLIN, session_dispatch_leader_pidfd, s); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
|
| 125 |
r = sd_event_source_set_priority(s->leader_pidfd_event_source, SD_EVENT_PRIORITY_IMPORTANT); |
| 126 |
if (r < 0) |
| 127 |
return r; |
| 128 |
|
| 129 |
(void) sd_event_source_set_description(s->leader_pidfd_event_source, "session-pidfd"); |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
static void session_reset_leader(Session *s, bool keep_fdstore) { |
| 135 |
assert(s); |
| 136 |
|
| 137 |
if (!keep_fdstore) { |
| 138 |
/* Clear fdstore if we're asked to, no matter if s->leader is set or not, so that when |
| 139 |
* initially deserializing leader fd we clear the old fd too. */ |
| 140 |
(void) notify_remove_fd_warnf("session-%s-leader-fd", s->id); |
| 141 |
s->leader_fd_saved = false; |
| 142 |
} |
| 143 |
|
| 144 |
if (!pidref_is_set(&s->leader)) |
| 145 |
return; |
| 146 |
|
| 147 |
s->leader_pidfd_event_source = sd_event_source_disable_unref(s->leader_pidfd_event_source); |
| 148 |
|
| 149 |
(void) hashmap_remove_value(s->manager->sessions_by_leader, &s->leader, s); |
| 150 |
|
| 151 |
return pidref_done(&s->leader); |
| 152 |
} |
| 153 |
|
| 154 |
Session* session_free(Session *s) { |
| 155 |
SessionDevice *sd; |
| 156 |
|
| 157 |
if (!s) |
| 158 |
return NULL; |
| 159 |
|
| 160 |
sd_event_source_unref(s->stop_on_idle_event_source); |
| 161 |
|
| 162 |
if (s->in_gc_queue) { |
| 163 |
assert(s->manager); |
| 164 |
LIST_REMOVE(gc_queue, s->manager->session_gc_queue, s); |
| 165 |
} |
| 166 |
|
| 167 |
sd_event_source_unref(s->timer_event_source); |
| 168 |
|
| 169 |
session_drop_controller(s); |
| 170 |
|
| 171 |
while ((sd = hashmap_first(s->devices))) |
| 172 |
session_device_free(sd); |
| 173 |
|
| 174 |
hashmap_free(s->devices); |
| 175 |
|
| 176 |
if (s->user) { |
| 177 |
LIST_REMOVE(sessions_by_user, s->user->sessions, s); |
| 178 |
|
| 179 |
if (s->user->display == s) |
| 180 |
s->user->display = NULL; |
| 181 |
|
| 182 |
user_update_last_session_timer(s->user); |
| 183 |
} |
| 184 |
|
| 185 |
if (s->seat) { |
| 186 |
if (s->seat->active == s) |
| 187 |
s->seat->active = NULL; |
| 188 |
if (s->seat->pending_switch == s) |
| 189 |
s->seat->pending_switch = NULL; |
| 190 |
|
| 191 |
seat_evict_position(s->seat, s); |
| 192 |
LIST_REMOVE(sessions_by_seat, s->seat->sessions, s); |
| 193 |
} |
| 194 |
|
| 195 |
if (s->scope) { |
| 196 |
hashmap_remove(s->manager->session_units, s->scope); |
| 197 |
free(s->scope); |
| 198 |
} |
| 199 |
|
| 200 |
free(s->scope_job); |
| 201 |
|
| 202 |
session_reset_leader(s, /* keep_fdstore= */ true); |
| 203 |
|
| 204 |
sd_bus_message_unref(s->create_message); |
| 205 |
sd_bus_message_unref(s->upgrade_message); |
| 206 |
|
| 207 |
sd_varlink_unref(s->create_link); |
| 208 |
|
| 209 |
free(s->tty); |
| 210 |
free(s->display); |
| 211 |
free(s->remote_host); |
| 212 |
free(s->remote_user); |
| 213 |
free(s->service); |
| 214 |
free(s->desktop); |
| 215 |
strv_free(s->extra_device_access); |
| 216 |
|
| 217 |
hashmap_remove(s->manager->sessions, s->id); |
| 218 |
|
| 219 |
/* Note that we don't remove the state file here, since it's supposed to survive daemon restarts */ |
| 220 |
free(s->state_file); |
| 221 |
free(s->id); |
| 222 |
|
| 223 |
return mfree(s); |
| 224 |
} |
| 225 |
|
| 226 |
void session_set_user(Session *s, User *u) { |
| 227 |
assert(s); |
| 228 |
assert(!s->user); |
| 229 |
|
| 230 |
s->user = u; |
| 231 |
LIST_PREPEND(sessions_by_user, u->sessions, s); |
| 232 |
|
| 233 |
user_update_last_session_timer(u); |
| 234 |
} |
| 235 |
|
| 236 |
int session_set_leader_consume(Session *s, PidRef _leader) { |
| 237 |
_cleanup_(pidref_done) PidRef pidref = _leader; |
| 238 |
int r; |
| 239 |
|
| 240 |
assert(s); |
| 241 |
assert(pidref_is_set(&pidref)); |
| 242 |
assert(pidref.fd >= 0); |
| 243 |
|
| 244 |
if (pidref_equal(&s->leader, &pidref)) |
| 245 |
return 0; |
| 246 |
|
| 247 |
session_reset_leader(s, /* keep_fdstore= */ false); |
| 248 |
|
| 249 |
s->leader = TAKE_PIDREF(pidref); |
| 250 |
|
| 251 |
r = session_watch_pidfd(s); |
| 252 |
if (r < 0) |
| 253 |
return log_error_errno(r, "Failed to watch leader pidfd for session '%s': %m", s->id); |
| 254 |
|
| 255 |
r = hashmap_ensure_put(&s->manager->sessions_by_leader, &pidref_hash_ops, &s->leader, s); |
| 256 |
if (r < 0) |
| 257 |
return r; |
| 258 |
assert(r > 0); |
| 259 |
|
| 260 |
if (s->leader.fd >= 0) { |
| 261 |
r = notify_push_fdf(s->leader.fd, "session-%s-leader-fd", s->id); |
| 262 |
if (r < 0) |
| 263 |
log_warning_errno(r, "Failed to push leader pidfd for session '%s', ignoring: %m", s->id); |
| 264 |
else |
| 265 |
s->leader_fd_saved = true; |
| 266 |
} |
| 267 |
|
| 268 |
(void) audit_session_from_pid(&s->leader, &s->audit_id); |
| 269 |
|
| 270 |
return 1; |
| 271 |
} |
| 272 |
|
| 273 |
static void session_save_devices(Session *s, FILE *f) { |
| 274 |
SessionDevice *sd; |
| 275 |
|
| 276 |
if (!hashmap_isempty(s->devices)) { |
| 277 |
fprintf(f, "DEVICES="); |
| 278 |
HASHMAP_FOREACH(sd, s->devices) |
| 279 |
fprintf(f, DEVNUM_FORMAT_STR " ", DEVNUM_FORMAT_VAL(sd->dev)); |
| 280 |
fprintf(f, "\n"); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
static int trigger_xaccess(char * const *extra_devices) { |
| 285 |
int r; |
| 286 |
|
| 287 |
if (strv_isempty(extra_devices)) |
| 288 |
return 0; |
| 289 |
|
| 290 |
_cleanup_strv_free_ char **tags = NULL; |
| 291 |
r = strv_extend_strv_biconcat(&tags, "xaccess-", (const char * const *)extra_devices, /* suffix= */ NULL); |
| 292 |
if (r < 0) |
| 293 |
return r; |
| 294 |
|
| 295 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 296 |
r = sd_device_enumerator_new(&e); |
| 297 |
if (r < 0) |
| 298 |
return r; |
| 299 |
|
| 300 |
STRV_FOREACH(tag, tags) { |
| 301 |
r = sd_device_enumerator_add_match_tag(e, *tag); |
| 302 |
if (r < 0) |
| 303 |
return r; |
| 304 |
} |
| 305 |
|
| 306 |
FOREACH_DEVICE(e, d) { |
| 307 |
/* Verify that the tag is still in place. */ |
| 308 |
bool has_xaccess = false; |
| 309 |
STRV_FOREACH(tag, tags) |
| 310 |
if (sd_device_has_current_tag(d, *tag)) { |
| 311 |
has_xaccess = true; |
| 312 |
break; |
| 313 |
} |
| 314 |
if (!has_xaccess) |
| 315 |
continue; |
| 316 |
|
| 317 |
/* In case people mistag devices without nodes, we need to ignore this. */ |
| 318 |
r = sd_device_get_devname(d, NULL); |
| 319 |
if (r == -ENOENT) |
| 320 |
continue; |
| 321 |
if (r < 0) |
| 322 |
return r; |
| 323 |
|
| 324 |
sd_id128_t uuid; |
| 325 |
r = sd_device_trigger_with_uuid(d, SD_DEVICE_CHANGE, &uuid); |
| 326 |
if (r < 0) { |
| 327 |
log_device_debug_errno(d, r, "Failed to trigger 'change' event, ignoring: %m"); |
| 328 |
continue; |
| 329 |
} |
| 330 |
|
| 331 |
log_device_debug(d, "Triggered synthetic event (ACTION=change, UUID=%s).", SD_ID128_TO_UUID_STRING(uuid)); |
| 332 |
} |
| 333 |
|
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
int session_save(Session *s) { |
| 338 |
int r; |
| 339 |
|
| 340 |
assert(s); |
| 341 |
|
| 342 |
if (!s->user) |
| 343 |
return -ESTALE; |
| 344 |
|
| 345 |
if (!s->started) |
| 346 |
return 0; |
| 347 |
|
| 348 |
r = mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0, MKDIR_WARN_MODE); |
| 349 |
if (r < 0) |
| 350 |
return log_error_errno(r, "Failed to create /run/systemd/sessions/: %m"); |
| 351 |
|
| 352 |
_cleanup_(unlink_and_freep) char *temp_path = NULL; |
| 353 |
_cleanup_fclose_ FILE *f = NULL; |
| 354 |
r = fopen_tmpfile_linkable(s->state_file, O_WRONLY|O_CLOEXEC, &temp_path, &f); |
| 355 |
if (r < 0) |
| 356 |
return log_error_errno(r, "Failed to create state file '%s': %m", s->state_file); |
| 357 |
|
| 358 |
if (fchmod(fileno(f), 0644) < 0) |
| 359 |
return log_error_errno(errno, "Failed to set access mode for state file '%s' to 0644: %m", s->state_file); |
| 360 |
|
| 361 |
fprintf(f, |
| 362 |
"# This is private data. Do not parse.\n" |
| 363 |
"UID="UID_FMT"\n" |
| 364 |
"ACTIVE=%s\n" |
| 365 |
"IS_DISPLAY=%s\n" |
| 366 |
"STATE=%s\n" |
| 367 |
"REMOTE=%s\n" |
| 368 |
"LEADER_FD_SAVED=%s\n", |
| 369 |
s->user->user_record->uid, |
| 370 |
one_zero(session_is_active(s)), |
| 371 |
one_zero(s->user->display == s), |
| 372 |
session_state_to_string(session_get_state(s)), |
| 373 |
one_zero(s->remote), |
| 374 |
one_zero(s->leader_fd_saved)); |
| 375 |
|
| 376 |
env_file_fputs_assignment(f, "USER=", s->user->user_record->user_name); |
| 377 |
|
| 378 |
if (s->type >= 0) |
| 379 |
fprintf(f, "TYPE=%s\n", session_type_to_string(s->type)); |
| 380 |
|
| 381 |
if (s->original_type >= 0) |
| 382 |
fprintf(f, "ORIGINAL_TYPE=%s\n", session_type_to_string(s->original_type)); |
| 383 |
|
| 384 |
if (s->class >= 0) |
| 385 |
fprintf(f, "CLASS=%s\n", session_class_to_string(s->class)); |
| 386 |
|
| 387 |
env_file_fputs_assignment(f, "SCOPE=", s->scope); |
| 388 |
env_file_fputs_assignment(f, "SCOPE_JOB=", s->scope_job); |
| 389 |
if (s->seat) |
| 390 |
env_file_fputs_assignment(f, "SEAT=", s->seat->id); |
| 391 |
env_file_fputs_assignment(f, "TTY=", s->tty); |
| 392 |
|
| 393 |
if (s->tty_validity >= 0) |
| 394 |
fprintf(f, "TTY_VALIDITY=%s\n", tty_validity_to_string(s->tty_validity)); |
| 395 |
|
| 396 |
env_file_fputs_assignment(f, "DISPLAY=", s->display); |
| 397 |
env_file_fputs_assignment(f, "REMOTE_HOST=", s->remote_host); |
| 398 |
env_file_fputs_assignment(f, "REMOTE_USER=", s->remote_user); |
| 399 |
env_file_fputs_assignment(f, "SERVICE=", s->service); |
| 400 |
env_file_fputs_assignment(f, "DESKTOP=", s->desktop); |
| 401 |
|
| 402 |
if (s->seat) { |
| 403 |
if (!seat_has_vts(s->seat)) |
| 404 |
fprintf(f, "POSITION=%u\n", s->position); |
| 405 |
else if (s->vtnr > 0) |
| 406 |
fprintf(f, "VTNR=%u\n", s->vtnr); |
| 407 |
} |
| 408 |
|
| 409 |
if (pidref_is_set(&s->leader)) { |
| 410 |
fprintf(f, "LEADER="PID_FMT"\n", s->leader.pid); |
| 411 |
(void) pidref_acquire_pidfd_id(&s->leader); |
| 412 |
if (s->leader.fd_id != 0) |
| 413 |
fprintf(f, "LEADER_PIDFDID=%" PRIu64 "\n", s->leader.fd_id); |
| 414 |
} |
| 415 |
|
| 416 |
if (audit_session_is_valid(s->audit_id)) |
| 417 |
fprintf(f, "AUDIT=%"PRIu32"\n", s->audit_id); |
| 418 |
|
| 419 |
if (dual_timestamp_is_set(&s->timestamp)) |
| 420 |
fprintf(f, |
| 421 |
"REALTIME="USEC_FMT"\n" |
| 422 |
"MONOTONIC="USEC_FMT"\n", |
| 423 |
s->timestamp.realtime, |
| 424 |
s->timestamp.monotonic); |
| 425 |
|
| 426 |
if (s->controller) { |
| 427 |
env_file_fputs_assignment(f, "CONTROLLER=", s->controller); |
| 428 |
session_save_devices(s, f); |
| 429 |
} |
| 430 |
|
| 431 |
if (s->extra_device_access) { |
| 432 |
_cleanup_free_ char *extra_devices = strv_join(s->extra_device_access, " "); |
| 433 |
if (!extra_devices) |
| 434 |
return log_oom(); |
| 435 |
fprintf(f, "EXTRA_DEVICE_ACCESS=%s\n", extra_devices); |
| 436 |
} |
| 437 |
|
| 438 |
r = flink_tmpfile(f, temp_path, s->state_file, LINK_TMPFILE_REPLACE); |
| 439 |
if (r < 0) |
| 440 |
return log_error_errno(r, "Failed to move '%s' into place: %m", s->state_file); |
| 441 |
|
| 442 |
temp_path = mfree(temp_path); /* disarm auto-destroy: temporary file does not exist anymore */ |
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
static int session_load_devices(Session *s, const char *devices) { |
| 447 |
int r = 0; |
| 448 |
|
| 449 |
assert(s); |
| 450 |
|
| 451 |
for (const char *p = devices;;) { |
| 452 |
_cleanup_free_ char *word = NULL; |
| 453 |
dev_t dev; |
| 454 |
int k; |
| 455 |
|
| 456 |
k = extract_first_word(&p, &word, NULL, 0); |
| 457 |
if (k <= 0) { |
| 458 |
RET_GATHER(r, k); |
| 459 |
break; |
| 460 |
} |
| 461 |
|
| 462 |
k = parse_devnum(word, &dev); |
| 463 |
if (k < 0) { |
| 464 |
RET_GATHER(r, k); |
| 465 |
continue; |
| 466 |
} |
| 467 |
|
| 468 |
/* The file descriptors for loaded devices will be reattached later. */ |
| 469 |
RET_GATHER(r, session_device_new(s, dev, /* open_device= */ false, /* ret= */ NULL)); |
| 470 |
} |
| 471 |
|
| 472 |
if (r < 0) |
| 473 |
log_error_errno(r, "Failed to load some session devices for session '%s': %m", s->id); |
| 474 |
return r; |
| 475 |
} |
| 476 |
|
| 477 |
static int session_load_leader(Session *s, uint64_t pidfdid) { |
| 478 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 479 |
int r; |
| 480 |
|
| 481 |
assert(s); |
| 482 |
assert(pid_is_valid(s->deserialized_pid)); |
| 483 |
assert(!pidref_is_set(&s->leader)); |
| 484 |
|
| 485 |
if (pidfdid == 0 && s->leader_fd_saved) |
| 486 |
/* We have no pidfd id for stable reference, but the pidfd has been submitted to fdstore. |
| 487 |
* manager_enumerate_fds() will dispatch the leader fd for us later. */ |
| 488 |
return 0; |
| 489 |
|
| 490 |
r = pidref_set_pid(&pidref, s->deserialized_pid); |
| 491 |
if (r == -ESRCH) |
| 492 |
return log_warning_errno(r, "Leader of session '%s' is gone while deserializing.", s->id); |
| 493 |
if (r < 0) |
| 494 |
return log_error_errno(r, "Failed to deserialize leader PID for session '%s': %m", s->id); |
| 495 |
if (pidref.fd < 0) |
| 496 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), |
| 497 |
"Failed to acquire pidfd for session leader '" PID_FMT "', refusing.", |
| 498 |
pidref.pid); |
| 499 |
|
| 500 |
if (pidfdid > 0) { |
| 501 |
r = pidref_acquire_pidfd_id(&pidref); |
| 502 |
if (r < 0) |
| 503 |
return log_error_errno(r, "Failed to acquire pidfd id of deserialized leader '" PID_FMT "': %m", |
| 504 |
pidref.pid); |
| 505 |
|
| 506 |
if (pidref.fd_id != pidfdid) |
| 507 |
return log_warning_errno(SYNTHETIC_ERRNO(ESRCH), |
| 508 |
"Deserialized pidfd id for process " PID_FMT " (%" PRIu64 ") doesn't match the current one (%" PRIu64 "). PID recycled while deserializing?", |
| 509 |
pidref.pid, pidfdid, pidref.fd_id); |
| 510 |
} |
| 511 |
|
| 512 |
r = session_set_leader_consume(s, TAKE_PIDREF(pidref)); |
| 513 |
if (r < 0) |
| 514 |
return log_error_errno(r, "Failed to set leader PID for session '%s': %m", s->id); |
| 515 |
|
| 516 |
return 1; |
| 517 |
} |
| 518 |
|
| 519 |
int session_load(Session *s) { |
| 520 |
_cleanup_free_ char *remote = NULL, |
| 521 |
*extra_device_access = NULL, |
| 522 |
*seat = NULL, |
| 523 |
*tty_validity = NULL, |
| 524 |
*vtnr = NULL, |
| 525 |
*state = NULL, |
| 526 |
*position = NULL, |
| 527 |
*leader_pid = NULL, |
| 528 |
*leader_fd_saved = NULL, |
| 529 |
*leader_pidfdid = NULL, |
| 530 |
*type = NULL, |
| 531 |
*original_type = NULL, |
| 532 |
*class = NULL, |
| 533 |
*uid = NULL, |
| 534 |
*realtime = NULL, |
| 535 |
*monotonic = NULL, |
| 536 |
*controller = NULL, |
| 537 |
*active = NULL, |
| 538 |
*devices = NULL, |
| 539 |
*is_display = NULL, |
| 540 |
*fifo_path = NULL; /* compat only, not used */ |
| 541 |
|
| 542 |
int k, r; |
| 543 |
|
| 544 |
assert(s); |
| 545 |
|
| 546 |
r = parse_env_file(NULL, s->state_file, |
| 547 |
"REMOTE", &remote, |
| 548 |
"EXTRA_DEVICE_ACCESS", &extra_device_access, |
| 549 |
"SCOPE", &s->scope, |
| 550 |
"SCOPE_JOB", &s->scope_job, |
| 551 |
"FIFO", &fifo_path, |
| 552 |
"SEAT", &seat, |
| 553 |
"TTY", &s->tty, |
| 554 |
"TTY_VALIDITY", &tty_validity, |
| 555 |
"DISPLAY", &s->display, |
| 556 |
"REMOTE_HOST", &s->remote_host, |
| 557 |
"REMOTE_USER", &s->remote_user, |
| 558 |
"SERVICE", &s->service, |
| 559 |
"DESKTOP", &s->desktop, |
| 560 |
"VTNR", &vtnr, |
| 561 |
"STATE", &state, |
| 562 |
"POSITION", &position, |
| 563 |
"LEADER", &leader_pid, |
| 564 |
"LEADER_FD_SAVED", &leader_fd_saved, |
| 565 |
"LEADER_PIDFDID", &leader_pidfdid, |
| 566 |
"TYPE", &type, |
| 567 |
"ORIGINAL_TYPE", &original_type, |
| 568 |
"CLASS", &class, |
| 569 |
"UID", &uid, |
| 570 |
"REALTIME", &realtime, |
| 571 |
"MONOTONIC", &monotonic, |
| 572 |
"CONTROLLER", &controller, |
| 573 |
"ACTIVE", &active, |
| 574 |
"DEVICES", &devices, |
| 575 |
"IS_DISPLAY", &is_display); |
| 576 |
if (r < 0) |
| 577 |
return log_error_errno(r, "Failed to read %s: %m", s->state_file); |
| 578 |
|
| 579 |
if (!s->user) { |
| 580 |
uid_t u; |
| 581 |
User *user; |
| 582 |
|
| 583 |
if (!uid) |
| 584 |
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), |
| 585 |
"UID not specified for session %s", |
| 586 |
s->id); |
| 587 |
|
| 588 |
r = parse_uid(uid, &u); |
| 589 |
if (r < 0) { |
| 590 |
log_error("Failed to parse UID value %s for session %s.", uid, s->id); |
| 591 |
return r; |
| 592 |
} |
| 593 |
|
| 594 |
user = hashmap_get(s->manager->users, UID_TO_PTR(u)); |
| 595 |
if (!user) |
| 596 |
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), |
| 597 |
"User of session %s not known.", |
| 598 |
s->id); |
| 599 |
|
| 600 |
session_set_user(s, user); |
| 601 |
} |
| 602 |
|
| 603 |
if (remote) { |
| 604 |
k = parse_boolean(remote); |
| 605 |
if (k >= 0) |
| 606 |
s->remote = k; |
| 607 |
} |
| 608 |
|
| 609 |
if (extra_device_access) { |
| 610 |
s->extra_device_access = strv_split(extra_device_access, /* separators= */ NULL); |
| 611 |
if (!s->extra_device_access) |
| 612 |
return log_oom(); |
| 613 |
} |
| 614 |
|
| 615 |
if (vtnr) |
| 616 |
(void) safe_atou(vtnr, &s->vtnr); |
| 617 |
|
| 618 |
if (seat && !s->seat) { |
| 619 |
Seat *o; |
| 620 |
|
| 621 |
o = hashmap_get(s->manager->seats, seat); |
| 622 |
if (o) |
| 623 |
r = seat_attach_session(o, s); |
| 624 |
if (!o || r < 0) |
| 625 |
log_error("Cannot attach session %s to seat %s", s->id, seat); |
| 626 |
} |
| 627 |
|
| 628 |
if (!s->seat || !seat_has_vts(s->seat)) |
| 629 |
s->vtnr = 0; |
| 630 |
|
| 631 |
if (position && s->seat) { |
| 632 |
unsigned npos; |
| 633 |
|
| 634 |
(void) safe_atou(position, &npos); |
| 635 |
seat_claim_position(s->seat, s, npos); |
| 636 |
} |
| 637 |
|
| 638 |
if (tty_validity) { |
| 639 |
TTYValidity v; |
| 640 |
|
| 641 |
v = tty_validity_from_string(tty_validity); |
| 642 |
if (v < 0) |
| 643 |
log_debug("Failed to parse TTY validity: %s", tty_validity); |
| 644 |
else |
| 645 |
s->tty_validity = v; |
| 646 |
} |
| 647 |
|
| 648 |
if (type) { |
| 649 |
SessionType t; |
| 650 |
|
| 651 |
t = session_type_from_string(type); |
| 652 |
if (t >= 0) |
| 653 |
s->type = t; |
| 654 |
} |
| 655 |
|
| 656 |
if (original_type) { |
| 657 |
SessionType ot; |
| 658 |
|
| 659 |
ot = session_type_from_string(original_type); |
| 660 |
if (ot >= 0) |
| 661 |
s->original_type = ot; |
| 662 |
} else |
| 663 |
/* Pre-v246 compat: initialize original_type if not set in the state file */ |
| 664 |
s->original_type = s->type; |
| 665 |
|
| 666 |
if (class) { |
| 667 |
SessionClass c; |
| 668 |
|
| 669 |
c = session_class_from_string(class); |
| 670 |
if (c >= 0) |
| 671 |
s->class = c; |
| 672 |
} |
| 673 |
|
| 674 |
if (streq_ptr(state, "closing")) |
| 675 |
s->stopping = true; |
| 676 |
|
| 677 |
/* logind before v258 used a fifo for session close notification. Since v258 we fully employ |
| 678 |
* pidfd for the job, hence just unlink the legacy fifo. */ |
| 679 |
if (fifo_path) |
| 680 |
(void) unlink(fifo_path); |
| 681 |
|
| 682 |
if (realtime) |
| 683 |
(void) deserialize_usec(realtime, &s->timestamp.realtime); |
| 684 |
if (monotonic) |
| 685 |
(void) deserialize_usec(monotonic, &s->timestamp.monotonic); |
| 686 |
|
| 687 |
if (active) { |
| 688 |
k = parse_boolean(active); |
| 689 |
if (k >= 0) |
| 690 |
s->was_active = k; |
| 691 |
} |
| 692 |
|
| 693 |
if (is_display) { |
| 694 |
/* Note that when enumerating users are loaded before sessions, hence the display session to use is |
| 695 |
* something we have to store along with the session and not the user, as in that case we couldn't |
| 696 |
* apply it at the time we load the user. */ |
| 697 |
|
| 698 |
k = parse_boolean(is_display); |
| 699 |
if (k < 0) |
| 700 |
log_warning_errno(k, "Failed to parse IS_DISPLAY session property: %m"); |
| 701 |
else if (k > 0) |
| 702 |
s->user->display = s; |
| 703 |
} |
| 704 |
|
| 705 |
if (controller) { |
| 706 |
if (bus_name_has_owner(s->manager->bus, controller, NULL) > 0) { |
| 707 |
session_set_controller(s, controller, false, false); |
| 708 |
session_load_devices(s, devices); |
| 709 |
} else |
| 710 |
session_restore_vt(s); |
| 711 |
} |
| 712 |
|
| 713 |
if (leader_pid) { |
| 714 |
r = parse_pid(leader_pid, &s->deserialized_pid); |
| 715 |
if (r < 0) |
| 716 |
return log_error_errno(r, "Failed to parse LEADER=%s: %m", leader_pid); |
| 717 |
|
| 718 |
if (leader_fd_saved) { |
| 719 |
r = parse_boolean(leader_fd_saved); |
| 720 |
if (r < 0) |
| 721 |
return log_error_errno(r, "Failed to parse LEADER_FD_SAVED=%s: %m", leader_fd_saved); |
| 722 |
s->leader_fd_saved = r > 0; |
| 723 |
} |
| 724 |
|
| 725 |
uint64_t pidfdid; |
| 726 |
if (leader_pidfdid) { |
| 727 |
r = safe_atou64(leader_pidfdid, &pidfdid); |
| 728 |
if (r < 0) |
| 729 |
return log_error_errno(r, "Failed to parse LEADER_PIDFDID=%s: %m", leader_pidfdid); |
| 730 |
} else |
| 731 |
pidfdid = 0; |
| 732 |
|
| 733 |
r = session_load_leader(s, pidfdid); |
| 734 |
if (r < 0) |
| 735 |
return r; |
| 736 |
} |
| 737 |
|
| 738 |
return 0; |
| 739 |
} |
| 740 |
|
| 741 |
int session_activate(Session *s) { |
| 742 |
unsigned num_pending; |
| 743 |
|
| 744 |
assert(s); |
| 745 |
assert(s->user); |
| 746 |
|
| 747 |
if (!s->seat) |
| 748 |
return -EOPNOTSUPP; |
| 749 |
|
| 750 |
if (s->seat->active == s) |
| 751 |
return 0; |
| 752 |
|
| 753 |
/* on seats with VTs, we let VTs manage session-switching */ |
| 754 |
if (seat_has_vts(s->seat)) { |
| 755 |
if (s->vtnr == 0) |
| 756 |
return -EOPNOTSUPP; |
| 757 |
|
| 758 |
return chvt(s->vtnr); |
| 759 |
} |
| 760 |
|
| 761 |
/* On seats without VTs, we implement session-switching in logind. We |
| 762 |
* try to pause all session-devices and wait until the session |
| 763 |
* controller acknowledged them. Once all devices are asleep, we simply |
| 764 |
* switch the active session and be done. |
| 765 |
* We save the session we want to switch to in seat->pending_switch and |
| 766 |
* seat_complete_switch() will perform the final switch. */ |
| 767 |
|
| 768 |
s->seat->pending_switch = s; |
| 769 |
|
| 770 |
/* if no devices are running, immediately perform the session switch */ |
| 771 |
num_pending = session_device_try_pause_all(s); |
| 772 |
if (!num_pending) |
| 773 |
seat_complete_switch(s->seat); |
| 774 |
|
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
static int session_start_scope(Session *s, sd_bus_message *properties, sd_bus_error *error) { |
| 779 |
_cleanup_free_ char *scope = NULL; |
| 780 |
const char *description; |
| 781 |
int r; |
| 782 |
|
| 783 |
assert(s); |
| 784 |
assert(s->user); |
| 785 |
|
| 786 |
if (!SESSION_CLASS_WANTS_SCOPE(s->class)) |
| 787 |
return 0; |
| 788 |
|
| 789 |
if (s->scope) |
| 790 |
goto finish; |
| 791 |
|
| 792 |
s->scope_job = mfree(s->scope_job); |
| 793 |
|
| 794 |
scope = strjoin("session-", s->id, ".scope"); |
| 795 |
if (!scope) |
| 796 |
return log_oom(); |
| 797 |
|
| 798 |
description = strjoina("Session ", s->id, " of User ", s->user->user_record->user_name); |
| 799 |
|
| 800 |
r = manager_start_scope( |
| 801 |
s->manager, |
| 802 |
scope, |
| 803 |
&s->leader, |
| 804 |
/* allow_pidfd= */ true, |
| 805 |
s->user->slice, |
| 806 |
description, |
| 807 |
/* These should have been pulled in explicitly in user_start(). Just to be sure. */ |
| 808 |
/* requires= */ STRV_MAKE_CONST(s->user->runtime_dir_unit), |
| 809 |
/* wants= */ STRV_MAKE_CONST(SESSION_CLASS_WANTS_SERVICE_MANAGER(s->class) ? s->user->service_manager_unit : NULL), |
| 810 |
/* We usually want to order session scopes after systemd-user-sessions.service |
| 811 |
* since the unit is used as login session barrier for unprivileged users. However |
| 812 |
* the barrier doesn't apply for root as sysadmin should always be able to log in |
| 813 |
* (and without waiting for any timeout to expire) in case something goes wrong |
| 814 |
* during the boot process. */ |
| 815 |
/* extra_after= */ STRV_MAKE_CONST("systemd-logind.service", |
| 816 |
SESSION_CLASS_IS_EARLY(s->class) ? NULL : "systemd-user-sessions.service"), |
| 817 |
user_record_home_directory(s->user->user_record), |
| 818 |
properties, |
| 819 |
error, |
| 820 |
&s->scope_job); |
| 821 |
if (r < 0) |
| 822 |
return log_error_errno(r, "Failed to start session scope %s: %s", |
| 823 |
scope, bus_error_message(error, r)); |
| 824 |
|
| 825 |
s->scope = TAKE_PTR(scope); |
| 826 |
|
| 827 |
finish: |
| 828 |
(void) hashmap_put(s->manager->session_units, s->scope, s); |
| 829 |
return 0; |
| 830 |
} |
| 831 |
|
| 832 |
static int session_dispatch_stop_on_idle(sd_event_source *source, uint64_t t, void *userdata) { |
| 833 |
Session *s = userdata; |
| 834 |
dual_timestamp ts; |
| 835 |
int r, idle; |
| 836 |
|
| 837 |
assert(s); |
| 838 |
|
| 839 |
if (s->stopping) |
| 840 |
return 0; |
| 841 |
|
| 842 |
idle = session_get_idle_hint(s, &ts); |
| 843 |
if (idle) { |
| 844 |
log_info("Session \"%s\" of user \"%s\" is idle, stopping.", s->id, s->user->user_record->user_name); |
| 845 |
|
| 846 |
return session_stop(s, /* force= */ true); |
| 847 |
} |
| 848 |
|
| 849 |
r = sd_event_source_set_time( |
| 850 |
source, |
| 851 |
usec_add(dual_timestamp_is_set(&ts) ? ts.monotonic : now(CLOCK_MONOTONIC), |
| 852 |
s->manager->stop_idle_session_usec)); |
| 853 |
if (r < 0) |
| 854 |
return log_error_errno(r, "Failed to configure stop on idle session event source: %m"); |
| 855 |
|
| 856 |
r = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); |
| 857 |
if (r < 0) |
| 858 |
return log_error_errno(r, "Failed to enable stop on idle session event source: %m"); |
| 859 |
|
| 860 |
return 1; |
| 861 |
} |
| 862 |
|
| 863 |
static int session_setup_stop_on_idle_timer(Session *s) { |
| 864 |
int r; |
| 865 |
|
| 866 |
assert(s); |
| 867 |
|
| 868 |
if (s->manager->stop_idle_session_usec == USEC_INFINITY || !SESSION_CLASS_CAN_STOP_ON_IDLE(s->class)) |
| 869 |
return 0; |
| 870 |
|
| 871 |
r = sd_event_add_time_relative( |
| 872 |
s->manager->event, |
| 873 |
&s->stop_on_idle_event_source, |
| 874 |
CLOCK_MONOTONIC, |
| 875 |
s->manager->stop_idle_session_usec, |
| 876 |
0, |
| 877 |
session_dispatch_stop_on_idle, s); |
| 878 |
if (r < 0) |
| 879 |
return log_error_errno(r, "Failed to add stop on idle session event source: %m"); |
| 880 |
|
| 881 |
return 0; |
| 882 |
} |
| 883 |
|
| 884 |
int session_start(Session *s, sd_bus_message *properties, sd_bus_error *error) { |
| 885 |
int r; |
| 886 |
|
| 887 |
assert(s); |
| 888 |
|
| 889 |
if (!s->user) |
| 890 |
return -ESTALE; |
| 891 |
|
| 892 |
if (s->stopping) |
| 893 |
return -EINVAL; |
| 894 |
|
| 895 |
if (s->started) |
| 896 |
return 0; |
| 897 |
|
| 898 |
r = user_start(s->user); |
| 899 |
if (r < 0) |
| 900 |
return r; |
| 901 |
|
| 902 |
r = session_start_scope(s, properties, error); |
| 903 |
if (r < 0) |
| 904 |
return r; |
| 905 |
|
| 906 |
r = session_setup_stop_on_idle_timer(s); |
| 907 |
if (r < 0) |
| 908 |
return r; |
| 909 |
|
| 910 |
log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO, |
| 911 |
LOG_MESSAGE_ID(SD_MESSAGE_SESSION_START_STR), |
| 912 |
LOG_ITEM("SESSION_ID=%s", s->id), |
| 913 |
LOG_ITEM("USER_ID=%s", s->user->user_record->user_name), |
| 914 |
LOG_ITEM("LEADER="PID_FMT, s->leader.pid), |
| 915 |
LOG_ITEM("CLASS=%s", session_class_to_string(s->class)), |
| 916 |
LOG_ITEM("TYPE=%s", session_type_to_string(s->type)), |
| 917 |
LOG_MESSAGE("New session '%s' of user '%s' with class '%s' and type '%s'.", |
| 918 |
s->id, |
| 919 |
s->user->user_record->user_name, |
| 920 |
session_class_to_string(s->class), |
| 921 |
session_type_to_string(s->type))); |
| 922 |
|
| 923 |
if (!dual_timestamp_is_set(&s->timestamp)) |
| 924 |
dual_timestamp_now(&s->timestamp); |
| 925 |
|
| 926 |
if (s->seat) |
| 927 |
seat_read_active_vt(s->seat); |
| 928 |
|
| 929 |
s->started = true; |
| 930 |
|
| 931 |
user_elect_display(s->user); |
| 932 |
|
| 933 |
/* Save data */ |
| 934 |
(void) session_save(s); |
| 935 |
(void) user_save(s->user); |
| 936 |
if (s->seat) |
| 937 |
(void) seat_save(s->seat); |
| 938 |
|
| 939 |
(void) trigger_xaccess(s->extra_device_access); |
| 940 |
|
| 941 |
/* Send signals */ |
| 942 |
(void) session_send_signal(s, true); |
| 943 |
(void) user_send_changed(s->user, "Display"); |
| 944 |
|
| 945 |
if (s->seat && s->seat->active == s) |
| 946 |
(void) seat_send_changed(s->seat, "ActiveSession"); |
| 947 |
|
| 948 |
return 0; |
| 949 |
} |
| 950 |
|
| 951 |
static int session_stop_scope(Session *s, bool force) { |
| 952 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 953 |
int r; |
| 954 |
|
| 955 |
assert(s); |
| 956 |
|
| 957 |
if (!s->scope) |
| 958 |
return 0; |
| 959 |
|
| 960 |
/* Let's always abandon the scope first. This tells systemd that we are not interested anymore, and everything |
| 961 |
* that is left in the scope is "left-over". Informing systemd about this has the benefit that it will log |
| 962 |
* when killing any processes left after this point. */ |
| 963 |
r = manager_abandon_scope(s->manager, s->scope, &error); |
| 964 |
if (r < 0) { |
| 965 |
log_warning_errno(r, "Failed to abandon session scope, ignoring: %s", bus_error_message(&error, r)); |
| 966 |
sd_bus_error_free(&error); |
| 967 |
} |
| 968 |
|
| 969 |
s->scope_job = mfree(s->scope_job); |
| 970 |
|
| 971 |
/* Optionally, let's kill everything that's left now. */ |
| 972 |
if (force || |
| 973 |
(s->user->user_record->kill_processes != 0 && |
| 974 |
(s->user->user_record->kill_processes > 0 || |
| 975 |
manager_shall_kill(s->manager, s->user->user_record->user_name)))) { |
| 976 |
|
| 977 |
r = manager_stop_unit(s->manager, s->scope, force ? "replace" : "fail", &error, &s->scope_job); |
| 978 |
if (r < 0) { |
| 979 |
if (force) |
| 980 |
return log_error_errno(r, "Failed to stop session scope: %s", bus_error_message(&error, r)); |
| 981 |
|
| 982 |
log_warning_errno(r, "Failed to stop session scope, ignoring: %s", bus_error_message(&error, r)); |
| 983 |
} |
| 984 |
} else { |
| 985 |
|
| 986 |
/* With no killing, this session is allowed to persist in "closing" state indefinitely. |
| 987 |
* Therefore session stop and session removal may be two distinct events. |
| 988 |
* Session stop is quite significant on its own, let's log it. */ |
| 989 |
log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO, |
| 990 |
LOG_ITEM("SESSION_ID=%s", s->id), |
| 991 |
LOG_ITEM("USER_ID=%s", s->user->user_record->user_name), |
| 992 |
LOG_ITEM("LEADER="PID_FMT, s->leader.pid), |
| 993 |
LOG_MESSAGE("Session %s logged out. Waiting for processes to exit.", s->id)); |
| 994 |
} |
| 995 |
|
| 996 |
return 0; |
| 997 |
} |
| 998 |
|
| 999 |
int session_stop(Session *s, bool force) { |
| 1000 |
int r; |
| 1001 |
|
| 1002 |
assert(s); |
| 1003 |
|
| 1004 |
/* This is called whenever we begin with tearing down a session record. It's called in four cases: explicit API |
| 1005 |
* request via the bus (either directly for the session object or for the seat or user object this session |
| 1006 |
* belongs to; 'force' is true), or due to automatic GC (i.e. scope vanished; 'force' is false), or because the |
| 1007 |
* session FIFO saw an EOF ('force' is false), or because the release timer hit ('force' is false). */ |
| 1008 |
|
| 1009 |
if (!s->user) |
| 1010 |
return -ESTALE; |
| 1011 |
if (!s->started) |
| 1012 |
return 0; |
| 1013 |
if (s->stopping) |
| 1014 |
return 0; |
| 1015 |
|
| 1016 |
s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
| 1017 |
|
| 1018 |
if (s->seat) |
| 1019 |
seat_evict_position(s->seat, s); |
| 1020 |
|
| 1021 |
/* Kill cgroup */ |
| 1022 |
r = session_stop_scope(s, force); |
| 1023 |
|
| 1024 |
s->stopping = true; |
| 1025 |
|
| 1026 |
user_elect_display(s->user); |
| 1027 |
|
| 1028 |
(void) session_save(s); |
| 1029 |
(void) user_save(s->user); |
| 1030 |
|
| 1031 |
(void) trigger_xaccess(s->extra_device_access); |
| 1032 |
|
| 1033 |
return r; |
| 1034 |
} |
| 1035 |
|
| 1036 |
int session_finalize(Session *s) { |
| 1037 |
SessionDevice *sd; |
| 1038 |
|
| 1039 |
assert(s); |
| 1040 |
|
| 1041 |
if (!s->user) |
| 1042 |
return -ESTALE; |
| 1043 |
|
| 1044 |
if (s->started) |
| 1045 |
log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO, |
| 1046 |
LOG_MESSAGE_ID(SD_MESSAGE_SESSION_STOP_STR), |
| 1047 |
LOG_ITEM("SESSION_ID=%s", s->id), |
| 1048 |
LOG_ITEM("USER_ID=%s", s->user->user_record->user_name), |
| 1049 |
LOG_ITEM("LEADER="PID_FMT, s->leader.pid), |
| 1050 |
LOG_MESSAGE("Removed session %s.", s->id)); |
| 1051 |
|
| 1052 |
s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
| 1053 |
|
| 1054 |
if (s->seat) |
| 1055 |
seat_evict_position(s->seat, s); |
| 1056 |
|
| 1057 |
/* Kill session devices */ |
| 1058 |
while ((sd = hashmap_first(s->devices))) |
| 1059 |
session_device_free(sd); |
| 1060 |
|
| 1061 |
(void) unlink(s->state_file); |
| 1062 |
session_add_to_gc_queue(s); |
| 1063 |
user_add_to_gc_queue(s->user); |
| 1064 |
|
| 1065 |
if (s->started) { |
| 1066 |
session_send_signal(s, false); |
| 1067 |
s->started = false; |
| 1068 |
} |
| 1069 |
|
| 1070 |
if (s->seat) { |
| 1071 |
if (s->seat->active == s) |
| 1072 |
seat_set_active(s->seat, NULL); |
| 1073 |
|
| 1074 |
seat_save(s->seat); |
| 1075 |
} |
| 1076 |
|
| 1077 |
session_reset_leader(s, /* keep_fdstore= */ false); |
| 1078 |
|
| 1079 |
(void) user_save(s->user); |
| 1080 |
(void) user_send_changed(s->user, "Display"); |
| 1081 |
|
| 1082 |
return 0; |
| 1083 |
} |
| 1084 |
|
| 1085 |
static int release_timeout_callback(sd_event_source *es, uint64_t usec, void *userdata) { |
| 1086 |
Session *s = ASSERT_PTR(userdata); |
| 1087 |
|
| 1088 |
assert(es); |
| 1089 |
|
| 1090 |
session_stop(s, /* force= */ false); |
| 1091 |
return 0; |
| 1092 |
} |
| 1093 |
|
| 1094 |
int session_release(Session *s) { |
| 1095 |
assert(s); |
| 1096 |
|
| 1097 |
if (!s->started || s->stopping) |
| 1098 |
return 0; |
| 1099 |
|
| 1100 |
if (s->timer_event_source) |
| 1101 |
return 0; |
| 1102 |
|
| 1103 |
return sd_event_add_time_relative( |
| 1104 |
s->manager->event, |
| 1105 |
&s->timer_event_source, |
| 1106 |
CLOCK_MONOTONIC, |
| 1107 |
RELEASE_USEC, 0, |
| 1108 |
release_timeout_callback, s); |
| 1109 |
} |
| 1110 |
|
| 1111 |
bool session_is_active(Session *s) { |
| 1112 |
assert(s); |
| 1113 |
|
| 1114 |
if (!s->seat) |
| 1115 |
return true; |
| 1116 |
|
| 1117 |
return s->seat->active == s; |
| 1118 |
} |
| 1119 |
|
| 1120 |
static int get_tty_atime(const char *tty, usec_t *atime) { |
| 1121 |
_cleanup_free_ char *p = NULL; |
| 1122 |
struct stat st; |
| 1123 |
|
| 1124 |
assert(tty); |
| 1125 |
assert(atime); |
| 1126 |
|
| 1127 |
if (!path_is_absolute(tty)) { |
| 1128 |
p = path_join("/dev", tty); |
| 1129 |
if (!p) |
| 1130 |
return -ENOMEM; |
| 1131 |
|
| 1132 |
tty = p; |
| 1133 |
} else if (!path_startswith(tty, "/dev/")) |
| 1134 |
return -ENOENT; |
| 1135 |
|
| 1136 |
if (lstat(tty, &st) < 0) |
| 1137 |
return -errno; |
| 1138 |
|
| 1139 |
*atime = timespec_load(&st.st_atim); |
| 1140 |
return 0; |
| 1141 |
} |
| 1142 |
|
| 1143 |
static int get_process_ctty_atime(pid_t pid, usec_t *atime) { |
| 1144 |
_cleanup_free_ char *p = NULL; |
| 1145 |
int r; |
| 1146 |
|
| 1147 |
assert(pid > 0); |
| 1148 |
assert(atime); |
| 1149 |
|
| 1150 |
r = get_ctty(pid, NULL, &p); |
| 1151 |
if (r < 0) |
| 1152 |
return r; |
| 1153 |
|
| 1154 |
return get_tty_atime(p, atime); |
| 1155 |
} |
| 1156 |
|
| 1157 |
bool session_get_idle_hint(Session *s, dual_timestamp *ret_timestamp) { |
| 1158 |
usec_t atime = 0, dtime = 0; |
| 1159 |
int r; |
| 1160 |
|
| 1161 |
assert(s); |
| 1162 |
|
| 1163 |
if (!SESSION_CLASS_CAN_IDLE(s->class)) { |
| 1164 |
if (ret_timestamp) |
| 1165 |
*ret_timestamp = DUAL_TIMESTAMP_NULL; |
| 1166 |
|
| 1167 |
return false; |
| 1168 |
} |
| 1169 |
|
| 1170 |
/* Graphical sessions have an explicit idle hint */ |
| 1171 |
if (SESSION_TYPE_IS_GRAPHICAL(s->type)) { |
| 1172 |
if (ret_timestamp) |
| 1173 |
*ret_timestamp = s->idle_hint_timestamp; |
| 1174 |
|
| 1175 |
return s->idle_hint; |
| 1176 |
} |
| 1177 |
|
| 1178 |
if (s->type == SESSION_TTY) { |
| 1179 |
/* For sessions with an explicitly configured tty, let's check its atime */ |
| 1180 |
if (s->tty) { |
| 1181 |
r = get_tty_atime(s->tty, &atime); |
| 1182 |
if (r >= 0) |
| 1183 |
goto found_atime; |
| 1184 |
} |
| 1185 |
|
| 1186 |
/* For sessions with a leader but no explicitly configured tty, let's check the controlling tty of |
| 1187 |
* the leader */ |
| 1188 |
if (pidref_is_set(&s->leader)) { |
| 1189 |
r = get_process_ctty_atime(s->leader.pid, &atime); |
| 1190 |
if (r >= 0) |
| 1191 |
goto found_atime; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
if (ret_timestamp) |
| 1196 |
*ret_timestamp = DUAL_TIMESTAMP_NULL; |
| 1197 |
|
| 1198 |
return false; |
| 1199 |
|
| 1200 |
found_atime: |
| 1201 |
if (ret_timestamp) |
| 1202 |
dual_timestamp_from_realtime(ret_timestamp, atime); |
| 1203 |
|
| 1204 |
if (s->manager->idle_action_usec > 0 && s->manager->stop_idle_session_usec != USEC_INFINITY) |
| 1205 |
dtime = MIN(s->manager->idle_action_usec, s->manager->stop_idle_session_usec); |
| 1206 |
else if (s->manager->idle_action_usec > 0) |
| 1207 |
dtime = s->manager->idle_action_usec; |
| 1208 |
else if (s->manager->stop_idle_session_usec != USEC_INFINITY) |
| 1209 |
dtime = s->manager->stop_idle_session_usec; |
| 1210 |
else |
| 1211 |
return false; |
| 1212 |
|
| 1213 |
return usec_add(atime, dtime) <= now(CLOCK_REALTIME); |
| 1214 |
} |
| 1215 |
|
| 1216 |
int session_set_idle_hint(Session *s, bool b) { |
| 1217 |
assert(s); |
| 1218 |
|
| 1219 |
if (!SESSION_CLASS_CAN_IDLE(s->class)) /* Only some session classes know the idle concept at all */ |
| 1220 |
return -ENOTTY; |
| 1221 |
if (!SESSION_TYPE_IS_GRAPHICAL(s->type)) /* And only graphical session types can set the field explicitly */ |
| 1222 |
return -ENOTTY; |
| 1223 |
|
| 1224 |
if (s->idle_hint == b) |
| 1225 |
return 0; |
| 1226 |
|
| 1227 |
s->idle_hint = b; |
| 1228 |
dual_timestamp_now(&s->idle_hint_timestamp); |
| 1229 |
|
| 1230 |
(void) session_send_changed(s, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic"); |
| 1231 |
|
| 1232 |
if (s->seat) |
| 1233 |
(void) seat_send_changed(s->seat, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic"); |
| 1234 |
|
| 1235 |
(void) user_send_changed(s->user, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic"); |
| 1236 |
(void) manager_send_changed(s->manager, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic"); |
| 1237 |
|
| 1238 |
return 1; |
| 1239 |
} |
| 1240 |
|
| 1241 |
int session_get_locked_hint(Session *s) { |
| 1242 |
assert(s); |
| 1243 |
|
| 1244 |
return s->locked_hint; |
| 1245 |
} |
| 1246 |
|
| 1247 |
int session_set_locked_hint(Session *s, bool b) { |
| 1248 |
assert(s); |
| 1249 |
|
| 1250 |
if (!SESSION_CLASS_CAN_LOCK(s->class)) |
| 1251 |
return -ENOTTY; |
| 1252 |
|
| 1253 |
if (s->locked_hint == b) |
| 1254 |
return 0; |
| 1255 |
|
| 1256 |
s->locked_hint = b; |
| 1257 |
(void) session_save(s); |
| 1258 |
(void) session_send_changed(s, "LockedHint"); |
| 1259 |
|
| 1260 |
return 1; |
| 1261 |
} |
| 1262 |
|
| 1263 |
void session_set_type(Session *s, SessionType t) { |
| 1264 |
assert(s); |
| 1265 |
|
| 1266 |
if (s->type == t) |
| 1267 |
return; |
| 1268 |
|
| 1269 |
s->type = t; |
| 1270 |
(void) session_save(s); |
| 1271 |
(void) session_send_changed(s, "Type"); |
| 1272 |
} |
| 1273 |
|
| 1274 |
void session_set_class(Session *s, SessionClass c) { |
| 1275 |
assert(s); |
| 1276 |
|
| 1277 |
if (s->class == c) |
| 1278 |
return; |
| 1279 |
|
| 1280 |
s->class = c; |
| 1281 |
(void) session_save(s); |
| 1282 |
(void) session_send_changed(s, "Class"); |
| 1283 |
|
| 1284 |
/* This class change might mean we need the per-user session manager now. Try to start it. */ |
| 1285 |
(void) user_start_service_manager(s->user); |
| 1286 |
} |
| 1287 |
|
| 1288 |
int session_set_display(Session *s, const char *display) { |
| 1289 |
int r; |
| 1290 |
|
| 1291 |
assert(s); |
| 1292 |
assert(display); |
| 1293 |
|
| 1294 |
r = free_and_strdup(&s->display, display); |
| 1295 |
if (r <= 0) /* 0 means the strings were equal */ |
| 1296 |
return r; |
| 1297 |
|
| 1298 |
(void) session_save(s); |
| 1299 |
(void) session_send_changed(s, "Display"); |
| 1300 |
|
| 1301 |
return 1; |
| 1302 |
} |
| 1303 |
|
| 1304 |
int session_set_tty(Session *s, const char *tty) { |
| 1305 |
int r; |
| 1306 |
|
| 1307 |
assert(s); |
| 1308 |
assert(tty); |
| 1309 |
|
| 1310 |
r = free_and_strdup(&s->tty, tty); |
| 1311 |
if (r <= 0) /* 0 means the strings were equal */ |
| 1312 |
return r; |
| 1313 |
|
| 1314 |
(void) session_save(s); |
| 1315 |
(void) session_send_changed(s, "TTY"); |
| 1316 |
|
| 1317 |
return 1; |
| 1318 |
} |
| 1319 |
|
| 1320 |
bool session_may_gc(Session *s, bool drop_not_started) { |
| 1321 |
int r; |
| 1322 |
|
| 1323 |
assert(s); |
| 1324 |
|
| 1325 |
if (drop_not_started && !s->started) |
| 1326 |
return true; |
| 1327 |
|
| 1328 |
if (!s->user) |
| 1329 |
return true; |
| 1330 |
|
| 1331 |
r = pidref_is_alive(&s->leader); |
| 1332 |
if (r == -ESRCH) |
| 1333 |
/* Session has no leader. This is probably because the leader vanished before deserializing |
| 1334 |
* pidfd from FD store. */ |
| 1335 |
return true; |
| 1336 |
if (r < 0) |
| 1337 |
log_debug_errno(r, "Unable to determine if leader PID " PID_FMT " is still alive, assuming not: %m", s->leader.pid); |
| 1338 |
if (r > 0) |
| 1339 |
return false; |
| 1340 |
|
| 1341 |
if (s->scope_job) { |
| 1342 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1343 |
|
| 1344 |
r = manager_job_is_active(s->manager, s->scope_job, &error); |
| 1345 |
if (r < 0) |
| 1346 |
log_debug_errno(r, "Failed to determine whether job '%s' is pending, ignoring: %s", s->scope_job, bus_error_message(&error, r)); |
| 1347 |
if (r != 0) |
| 1348 |
return false; |
| 1349 |
} |
| 1350 |
|
| 1351 |
if (s->scope) { |
| 1352 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1353 |
|
| 1354 |
r = manager_unit_is_active(s->manager, s->scope, &error); |
| 1355 |
if (r < 0) |
| 1356 |
log_debug_errno(r, "Failed to determine whether unit '%s' is active, ignoring: %s", s->scope, bus_error_message(&error, r)); |
| 1357 |
if (r != 0) |
| 1358 |
return false; |
| 1359 |
} |
| 1360 |
|
| 1361 |
return true; |
| 1362 |
} |
| 1363 |
|
| 1364 |
void session_add_to_gc_queue(Session *s) { |
| 1365 |
assert(s); |
| 1366 |
|
| 1367 |
if (s->in_gc_queue) |
| 1368 |
return; |
| 1369 |
|
| 1370 |
LIST_PREPEND(gc_queue, s->manager->session_gc_queue, s); |
| 1371 |
s->in_gc_queue = true; |
| 1372 |
} |
| 1373 |
|
| 1374 |
SessionState session_get_state(Session *s) { |
| 1375 |
assert(s); |
| 1376 |
|
| 1377 |
/* always check closing first */ |
| 1378 |
if (s->stopping || s->timer_event_source) |
| 1379 |
return SESSION_CLOSING; |
| 1380 |
|
| 1381 |
if (s->scope_job || !pidref_is_set(&s->leader)) |
| 1382 |
return SESSION_OPENING; |
| 1383 |
|
| 1384 |
if (session_is_active(s)) |
| 1385 |
return SESSION_ACTIVE; |
| 1386 |
|
| 1387 |
return SESSION_ONLINE; |
| 1388 |
} |
| 1389 |
|
| 1390 |
int session_kill(Session *s, KillWhom whom, int signo, sd_bus_error *error) { |
| 1391 |
assert(s); |
| 1392 |
|
| 1393 |
switch (whom) { |
| 1394 |
|
| 1395 |
case KILL_ALL: |
| 1396 |
if (!SESSION_CLASS_WANTS_SCOPE(s->class)) |
| 1397 |
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, |
| 1398 |
"Session '%s' has no associated scope", s->id); |
| 1399 |
|
| 1400 |
if (!s->scope) |
| 1401 |
return sd_bus_error_set_errnof(error, ESRCH, "Scope for session '%s' not active", s->id); |
| 1402 |
|
| 1403 |
return manager_kill_unit(s->manager, s->scope, KILL_ALL, signo, error); |
| 1404 |
|
| 1405 |
case KILL_LEADER: |
| 1406 |
return pidref_kill(&s->leader, signo); |
| 1407 |
|
| 1408 |
default: |
| 1409 |
assert_not_reached(); |
| 1410 |
} |
| 1411 |
} |
| 1412 |
|
| 1413 |
static int session_open_vt(Session *s, bool reopen) { |
| 1414 |
_cleanup_close_ int fd = -EBADF; |
| 1415 |
char path[sizeof("/dev/tty") + DECIMAL_STR_MAX(s->vtnr)]; |
| 1416 |
|
| 1417 |
assert(s); |
| 1418 |
|
| 1419 |
if (s->vtnr < 1) |
| 1420 |
return -ENODEV; |
| 1421 |
|
| 1422 |
if (!reopen && s->vtfd >= 0) |
| 1423 |
return s->vtfd; |
| 1424 |
|
| 1425 |
sprintf(path, "/dev/tty%u", s->vtnr); |
| 1426 |
|
| 1427 |
fd = open_terminal(path, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY); |
| 1428 |
if (fd < 0) |
| 1429 |
return log_error_errno(fd, "Cannot open VT %s of session %s: %m", path, s->id); |
| 1430 |
|
| 1431 |
close_and_replace(s->vtfd, fd); |
| 1432 |
return s->vtfd; |
| 1433 |
} |
| 1434 |
|
| 1435 |
static int session_prepare_vt(Session *s) { |
| 1436 |
int vt, r; |
| 1437 |
struct vt_mode mode = {}; |
| 1438 |
|
| 1439 |
assert(s); |
| 1440 |
|
| 1441 |
if (s->vtnr < 1) |
| 1442 |
return 0; |
| 1443 |
|
| 1444 |
vt = session_open_vt(s, /* reopen= */ false); |
| 1445 |
if (vt < 0) |
| 1446 |
return vt; |
| 1447 |
|
| 1448 |
r = fchown(vt, s->user->user_record->uid, -1); |
| 1449 |
if (r < 0) { |
| 1450 |
r = log_error_errno(errno, |
| 1451 |
"Cannot change owner of /dev/tty%u: %m", |
| 1452 |
s->vtnr); |
| 1453 |
goto error; |
| 1454 |
} |
| 1455 |
|
| 1456 |
r = ioctl(vt, KDSKBMODE, K_OFF); |
| 1457 |
if (r < 0) { |
| 1458 |
r = log_error_errno(errno, |
| 1459 |
"Cannot set K_OFF on /dev/tty%u: %m", |
| 1460 |
s->vtnr); |
| 1461 |
goto error; |
| 1462 |
} |
| 1463 |
|
| 1464 |
r = ioctl(vt, KDSETMODE, KD_GRAPHICS); |
| 1465 |
if (r < 0) { |
| 1466 |
r = log_error_errno(errno, |
| 1467 |
"Cannot set KD_GRAPHICS on /dev/tty%u: %m", |
| 1468 |
s->vtnr); |
| 1469 |
goto error; |
| 1470 |
} |
| 1471 |
|
| 1472 |
/* Oh, thanks to the VT layer, VT_AUTO does not work with KD_GRAPHICS. |
| 1473 |
* So we need a dummy handler here which just acknowledges *all* VT |
| 1474 |
* switch requests. */ |
| 1475 |
mode.mode = VT_PROCESS; |
| 1476 |
mode.relsig = SIGRTMIN; |
| 1477 |
mode.acqsig = SIGRTMIN + 1; |
| 1478 |
r = ioctl(vt, VT_SETMODE, &mode); |
| 1479 |
if (r < 0) { |
| 1480 |
r = log_error_errno(errno, |
| 1481 |
"Cannot set VT_PROCESS on /dev/tty%u: %m", |
| 1482 |
s->vtnr); |
| 1483 |
goto error; |
| 1484 |
} |
| 1485 |
|
| 1486 |
return 0; |
| 1487 |
|
| 1488 |
error: |
| 1489 |
session_restore_vt(s); |
| 1490 |
return r; |
| 1491 |
} |
| 1492 |
|
| 1493 |
static void session_restore_vt(Session *s) { |
| 1494 |
int r; |
| 1495 |
|
| 1496 |
assert(s); |
| 1497 |
|
| 1498 |
if (s->vtfd < 0) |
| 1499 |
return; |
| 1500 |
|
| 1501 |
r = vt_restore(s->vtfd); |
| 1502 |
if (r == -EIO) { |
| 1503 |
/* It might happen if the controlling process exited before or while we were |
| 1504 |
* restoring the VT as it would leave the old file-descriptor in a hung-up |
| 1505 |
* state. In this case let's retry with a fresh handle to the virtual terminal. */ |
| 1506 |
|
| 1507 |
/* We do a little dance to avoid having the terminal be available |
| 1508 |
* for reuse before we've cleaned it up. */ |
| 1509 |
|
| 1510 |
int fd = session_open_vt(s, /* reopen= */ true); |
| 1511 |
if (fd >= 0) |
| 1512 |
r = vt_restore(fd); |
| 1513 |
} |
| 1514 |
if (r < 0) |
| 1515 |
log_warning_errno(r, "Failed to restore VT, ignoring: %m"); |
| 1516 |
|
| 1517 |
s->vtfd = safe_close(s->vtfd); |
| 1518 |
} |
| 1519 |
|
| 1520 |
void session_leave_vt(Session *s) { |
| 1521 |
int r; |
| 1522 |
|
| 1523 |
assert(s); |
| 1524 |
|
| 1525 |
/* This is called whenever we get a VT-switch signal from the kernel. |
| 1526 |
* We acknowledge all of them unconditionally. Note that session are |
| 1527 |
* free to overwrite those handlers and we only register them for |
| 1528 |
* sessions with controllers. Legacy sessions are not affected. |
| 1529 |
* However, if we switch from a non-legacy to a legacy session, we must |
| 1530 |
* make sure to pause all device before acknowledging the switch. We |
| 1531 |
* process the real switch only after we are notified via sysfs, so the |
| 1532 |
* legacy session might have already started using the devices. If we |
| 1533 |
* don't pause the devices before the switch, we might confuse the |
| 1534 |
* session we switch to. */ |
| 1535 |
|
| 1536 |
if (s->vtfd < 0) |
| 1537 |
return; |
| 1538 |
|
| 1539 |
session_device_pause_all(s); |
| 1540 |
r = vt_release(s->vtfd, /* restore= */ false); |
| 1541 |
if (r == -EIO) { |
| 1542 |
/* Handle the same VT hung-up case as in session_restore_vt */ |
| 1543 |
|
| 1544 |
int fd = session_open_vt(s, /* reopen= */ true); |
| 1545 |
if (fd >= 0) |
| 1546 |
r = vt_release(fd, /* restore= */ false); |
| 1547 |
} |
| 1548 |
if (r < 0) |
| 1549 |
log_debug_errno(r, "Cannot release VT of session %s: %m", s->id); |
| 1550 |
} |
| 1551 |
|
| 1552 |
bool session_is_controller(Session *s, const char *sender) { |
| 1553 |
return streq_ptr(ASSERT_PTR(s)->controller, sender); |
| 1554 |
} |
| 1555 |
|
| 1556 |
static void session_release_controller(Session *s, bool notify) { |
| 1557 |
_unused_ _cleanup_free_ char *name = NULL; |
| 1558 |
SessionDevice *sd; |
| 1559 |
|
| 1560 |
assert(s); |
| 1561 |
|
| 1562 |
if (!s->controller) |
| 1563 |
return; |
| 1564 |
|
| 1565 |
name = s->controller; |
| 1566 |
|
| 1567 |
/* By resetting the controller before releasing the devices, we won't send notification signals. |
| 1568 |
* This avoids sending useless notifications if the controller is released on disconnects. */ |
| 1569 |
if (!notify) |
| 1570 |
s->controller = NULL; |
| 1571 |
|
| 1572 |
while ((sd = hashmap_first(s->devices))) |
| 1573 |
session_device_free(sd); |
| 1574 |
|
| 1575 |
s->controller = NULL; |
| 1576 |
s->track = sd_bus_track_unref(s->track); |
| 1577 |
} |
| 1578 |
|
| 1579 |
static int on_bus_track(sd_bus_track *track, void *userdata) { |
| 1580 |
Session *s = ASSERT_PTR(userdata); |
| 1581 |
|
| 1582 |
assert(track); |
| 1583 |
|
| 1584 |
session_drop_controller(s); |
| 1585 |
|
| 1586 |
return 0; |
| 1587 |
} |
| 1588 |
|
| 1589 |
int session_set_controller(Session *s, const char *sender, bool force, bool prepare) { |
| 1590 |
_cleanup_free_ char *name = NULL; |
| 1591 |
int r; |
| 1592 |
|
| 1593 |
assert(s); |
| 1594 |
assert(sender); |
| 1595 |
|
| 1596 |
if (session_is_controller(s, sender)) |
| 1597 |
return 0; |
| 1598 |
if (s->controller && !force) |
| 1599 |
return -EBUSY; |
| 1600 |
|
| 1601 |
name = strdup(sender); |
| 1602 |
if (!name) |
| 1603 |
return -ENOMEM; |
| 1604 |
|
| 1605 |
s->track = sd_bus_track_unref(s->track); |
| 1606 |
r = sd_bus_track_new(s->manager->bus, &s->track, on_bus_track, s); |
| 1607 |
if (r < 0) |
| 1608 |
return r; |
| 1609 |
|
| 1610 |
r = sd_bus_track_add_name(s->track, name); |
| 1611 |
if (r < 0) |
| 1612 |
return r; |
| 1613 |
|
| 1614 |
/* When setting a session controller, we forcibly mute the VT and set |
| 1615 |
* it into graphics-mode. Applications can override that by changing |
| 1616 |
* VT state after calling TakeControl(). However, this serves as a good |
| 1617 |
* default and well-behaving controllers can now ignore VTs entirely. |
| 1618 |
* Note that we reset the VT on ReleaseControl() and if the controller |
| 1619 |
* exits. |
| 1620 |
* If logind crashes/restarts, we restore the controller during restart |
| 1621 |
* (without preparing the VT since the controller has probably overridden |
| 1622 |
* VT state by now) or reset the VT in case it crashed/exited, too. */ |
| 1623 |
if (prepare) { |
| 1624 |
r = session_prepare_vt(s); |
| 1625 |
if (r < 0) { |
| 1626 |
s->track = sd_bus_track_unref(s->track); |
| 1627 |
return r; |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
session_release_controller(s, true); |
| 1632 |
s->controller = TAKE_PTR(name); |
| 1633 |
(void) session_save(s); |
| 1634 |
|
| 1635 |
return 0; |
| 1636 |
} |
| 1637 |
|
| 1638 |
void session_drop_controller(Session *s) { |
| 1639 |
assert(s); |
| 1640 |
|
| 1641 |
if (!s->controller) |
| 1642 |
return; |
| 1643 |
|
| 1644 |
s->track = sd_bus_track_unref(s->track); |
| 1645 |
session_set_type(s, s->original_type); |
| 1646 |
session_release_controller(s, false); |
| 1647 |
(void) session_save(s); |
| 1648 |
session_restore_vt(s); |
| 1649 |
} |
| 1650 |
|
| 1651 |
bool session_job_pending(Session *s) { |
| 1652 |
assert(s); |
| 1653 |
assert(s->user); |
| 1654 |
|
| 1655 |
/* Check if we have some jobs enqueued and not finished yet. Each time we get JobRemoved signal about |
| 1656 |
* relevant units, session_send_create_reply and hence us is called (see match_job_removed). |
| 1657 |
* Note that we don't care about job result here. */ |
| 1658 |
|
| 1659 |
return s->scope_job || |
| 1660 |
s->user->runtime_dir_job || |
| 1661 |
(SESSION_CLASS_WANTS_SERVICE_MANAGER(s->class) && s->user->service_manager_job); |
| 1662 |
} |
| 1663 |
|
| 1664 |
int session_send_create_reply(Session *s, const sd_bus_error *error) { |
| 1665 |
int r; |
| 1666 |
|
| 1667 |
assert(s); |
| 1668 |
|
| 1669 |
/* If error occurred, return it immediately. Otherwise let's wait for all jobs to finish before |
| 1670 |
* continuing. */ |
| 1671 |
if (!sd_bus_error_is_set(error) && session_job_pending(s)) |
| 1672 |
return 0; |
| 1673 |
|
| 1674 |
r = 0; |
| 1675 |
RET_GATHER(r, session_send_create_reply_bus(s, error)); |
| 1676 |
RET_GATHER(r, session_send_create_reply_varlink(s, error)); |
| 1677 |
return r; |
| 1678 |
} |
| 1679 |
|
| 1680 |
bool session_is_self(const char *name) { |
| 1681 |
return isempty(name) || streq(name, "self"); |
| 1682 |
} |
| 1683 |
|
| 1684 |
bool session_is_auto(const char *name) { |
| 1685 |
return streq_ptr(name, "auto"); |
| 1686 |
} |
| 1687 |
|
| 1688 |
static int session_context_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 1689 |
Session *s = ASSERT_PTR(userdata); |
| 1690 |
|
| 1691 |
assert(ret); |
| 1692 |
assert(s->user); |
| 1693 |
|
| 1694 |
return sd_json_buildo( |
| 1695 |
ret, |
| 1696 |
SD_JSON_BUILD_PAIR_UNSIGNED("UID", s->user->user_record->uid), |
| 1697 |
JSON_BUILD_PAIR_PIDREF_NON_NULL("PID", &s->leader), |
| 1698 |
JSON_BUILD_PAIR_ENUM("Type", session_type_to_string(s->type)), |
| 1699 |
JSON_BUILD_PAIR_ENUM("Class", session_class_to_string(s->class)), |
| 1700 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Service", s->service), |
| 1701 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Desktop", s->desktop), |
| 1702 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Seat", s->seat ? s->seat->id : NULL), |
| 1703 |
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("VTNr", s->vtnr), |
| 1704 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("TTY", s->tty), |
| 1705 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Display", s->display), |
| 1706 |
SD_JSON_BUILD_PAIR_BOOLEAN("Remote", s->remote), |
| 1707 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("RemoteHost", s->remote_host), |
| 1708 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("RemoteUser", s->remote_user), |
| 1709 |
JSON_BUILD_PAIR_STRV_NON_EMPTY("ExtraDeviceAccess", s->extra_device_access)); |
| 1710 |
} |
| 1711 |
|
| 1712 |
static int session_runtime_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 1713 |
Session *s = ASSERT_PTR(userdata); |
| 1714 |
|
| 1715 |
assert(ret); |
| 1716 |
|
| 1717 |
dual_timestamp idle_ts; |
| 1718 |
bool idle = session_get_idle_hint(s, &idle_ts); |
| 1719 |
|
| 1720 |
return sd_json_buildo( |
| 1721 |
ret, |
| 1722 |
SD_JSON_BUILD_PAIR_STRING("Id", s->id), |
| 1723 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Scope", s->scope), |
| 1724 |
SD_JSON_BUILD_PAIR_CONDITION(audit_session_is_valid(s->audit_id), |
| 1725 |
"Audit", SD_JSON_BUILD_UNSIGNED(s->audit_id)), |
| 1726 |
JSON_BUILD_PAIR_DUAL_TIMESTAMP_NON_NULL("Timestamp", &s->timestamp), |
| 1727 |
JSON_BUILD_PAIR_ENUM("State", session_state_to_string(session_get_state(s))), |
| 1728 |
SD_JSON_BUILD_PAIR_BOOLEAN("Active", session_is_active(s)), |
| 1729 |
SD_JSON_BUILD_PAIR_BOOLEAN("IdleHint", idle), |
| 1730 |
SD_JSON_BUILD_PAIR_CONDITION(idle, "IdleSinceHint", JSON_BUILD_DUAL_TIMESTAMP(&idle_ts)), |
| 1731 |
SD_JSON_BUILD_PAIR_BOOLEAN("LockedHint", s->locked_hint), |
| 1732 |
SD_JSON_BUILD_PAIR_BOOLEAN("CanIdle", SESSION_CLASS_CAN_IDLE(s->class)), |
| 1733 |
SD_JSON_BUILD_PAIR_BOOLEAN("CanLock", SESSION_CLASS_CAN_LOCK(s->class))); |
| 1734 |
} |
| 1735 |
|
| 1736 |
int session_build_json(Session *s, sd_json_variant **ret) { |
| 1737 |
assert(s); |
| 1738 |
assert(s->user); |
| 1739 |
assert(ret); |
| 1740 |
|
| 1741 |
return sd_json_buildo( |
| 1742 |
ret, |
| 1743 |
SD_JSON_BUILD_PAIR_CALLBACK("context", session_context_build_json, s), |
| 1744 |
SD_JSON_BUILD_PAIR_CALLBACK("runtime", session_runtime_build_json, s)); |
| 1745 |
} |
| 1746 |
|
| 1747 |
static const char* const session_state_table[_SESSION_STATE_MAX] = { |
| 1748 |
[SESSION_OPENING] = "opening", |
| 1749 |
[SESSION_ONLINE] = "online", |
| 1750 |
[SESSION_ACTIVE] = "active", |
| 1751 |
[SESSION_CLOSING] = "closing", |
| 1752 |
}; |
| 1753 |
|
| 1754 |
DEFINE_STRING_TABLE_LOOKUP(session_state, SessionState); |
| 1755 |
|
| 1756 |
static const char* const session_type_table[_SESSION_TYPE_MAX] = { |
| 1757 |
[SESSION_UNSPECIFIED] = "unspecified", |
| 1758 |
[SESSION_TTY] = "tty", |
| 1759 |
[SESSION_X11] = "x11", |
| 1760 |
[SESSION_WAYLAND] = "wayland", |
| 1761 |
[SESSION_MIR] = "mir", |
| 1762 |
[SESSION_WEB] = "web", |
| 1763 |
}; |
| 1764 |
|
| 1765 |
DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType); |
| 1766 |
|
| 1767 |
static const char* const session_class_table[_SESSION_CLASS_MAX] = { |
| 1768 |
[SESSION_USER] = "user", |
| 1769 |
[SESSION_USER_EARLY] = "user-early", |
| 1770 |
[SESSION_USER_INCOMPLETE] = "user-incomplete", |
| 1771 |
[SESSION_USER_LIGHT] = "user-light", |
| 1772 |
[SESSION_USER_EARLY_LIGHT] = "user-early-light", |
| 1773 |
[SESSION_GREETER] = "greeter", |
| 1774 |
[SESSION_LOCK_SCREEN] = "lock-screen", |
| 1775 |
[SESSION_BACKGROUND] = "background", |
| 1776 |
[SESSION_BACKGROUND_LIGHT] = "background-light", |
| 1777 |
[SESSION_MANAGER] = "manager", |
| 1778 |
[SESSION_MANAGER_EARLY] = "manager-early", |
| 1779 |
[SESSION_NONE] = "none", |
| 1780 |
}; |
| 1781 |
|
| 1782 |
DEFINE_STRING_TABLE_LOOKUP(session_class, SessionClass); |
| 1783 |
|
| 1784 |
static const char* const kill_whom_table[_KILL_WHOM_MAX] = { |
| 1785 |
[KILL_LEADER] = "leader", |
| 1786 |
[KILL_ALL] = "all", |
| 1787 |
}; |
| 1788 |
|
| 1789 |
DEFINE_STRING_TABLE_LOOKUP(kill_whom, KillWhom); |
| 1790 |
|
| 1791 |
static const char* const tty_validity_table[_TTY_VALIDITY_MAX] = { |
| 1792 |
[TTY_FROM_PAM] = "from-pam", |
| 1793 |
[TTY_FROM_UTMP] = "from-utmp", |
| 1794 |
[TTY_UTMP_INCONSISTENT] = "utmp-inconsistent", |
| 1795 |
}; |
| 1796 |
|
| 1797 |
DEFINE_STRING_TABLE_LOOKUP(tty_validity, TTYValidity); |