]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-seat.c
hibernate-resume: add resumeflags= kernel option
[thirdparty/systemd.git] / src / login / logind-seat.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <string.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8
9 #include "sd-messages.h"
10
11 #include "alloc-util.h"
12 #include "fd-util.h"
13 #include "fileio.h"
14 #include "format-util.h"
15 #include "logind-acl.h"
16 #include "logind-seat.h"
17 #include "mkdir.h"
18 #include "parse-util.h"
19 #include "stdio-util.h"
20 #include "string-util.h"
21 #include "terminal-util.h"
22 #include "tmpfile-util.h"
23 #include "util.h"
24
25 int seat_new(Seat** ret, Manager *m, const char *id) {
26 _cleanup_(seat_freep) Seat *s = NULL;
27 int r;
28
29 assert(ret);
30 assert(m);
31 assert(id);
32
33 if (!seat_name_is_valid(id))
34 return -EINVAL;
35
36 s = new(Seat, 1);
37 if (!s)
38 return -ENOMEM;
39
40 *s = (Seat) {
41 .manager = m,
42 };
43
44 s->state_file = strappend("/run/systemd/seats/", id);
45 if (!s->state_file)
46 return -ENOMEM;
47
48 s->id = basename(s->state_file);
49
50 r = hashmap_put(m->seats, s->id, s);
51 if (r < 0)
52 return r;
53
54 *ret = TAKE_PTR(s);
55 return 0;
56 }
57
58 Seat* seat_free(Seat *s) {
59 if (!s)
60 return NULL;
61
62 if (s->in_gc_queue)
63 LIST_REMOVE(gc_queue, s->manager->seat_gc_queue, s);
64
65 while (s->sessions)
66 session_free(s->sessions);
67
68 assert(!s->active);
69
70 while (s->devices)
71 device_free(s->devices);
72
73 hashmap_remove(s->manager->seats, s->id);
74
75 free(s->positions);
76 free(s->state_file);
77
78 return mfree(s);
79 }
80
81 int seat_save(Seat *s) {
82 _cleanup_free_ char *temp_path = NULL;
83 _cleanup_fclose_ FILE *f = NULL;
84 int r;
85
86 assert(s);
87
88 if (!s->started)
89 return 0;
90
91 r = mkdir_safe_label("/run/systemd/seats", 0755, 0, 0, MKDIR_WARN_MODE);
92 if (r < 0)
93 goto fail;
94
95 r = fopen_temporary(s->state_file, &f, &temp_path);
96 if (r < 0)
97 goto fail;
98
99 (void) fchmod(fileno(f), 0644);
100
101 fprintf(f,
102 "# This is private data. Do not parse.\n"
103 "IS_SEAT0=%i\n"
104 "CAN_MULTI_SESSION=%i\n"
105 "CAN_TTY=%i\n"
106 "CAN_GRAPHICAL=%i\n",
107 seat_is_seat0(s),
108 seat_can_multi_session(s),
109 seat_can_tty(s),
110 seat_can_graphical(s));
111
112 if (s->active) {
113 assert(s->active->user);
114
115 fprintf(f,
116 "ACTIVE=%s\n"
117 "ACTIVE_UID="UID_FMT"\n",
118 s->active->id,
119 s->active->user->uid);
120 }
121
122 if (s->sessions) {
123 Session *i;
124
125 fputs("SESSIONS=", f);
126 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
127 fprintf(f,
128 "%s%c",
129 i->id,
130 i->sessions_by_seat_next ? ' ' : '\n');
131 }
132
133 fputs("UIDS=", f);
134 LIST_FOREACH(sessions_by_seat, i, s->sessions)
135 fprintf(f,
136 UID_FMT"%c",
137 i->user->uid,
138 i->sessions_by_seat_next ? ' ' : '\n');
139 }
140
141 r = fflush_and_check(f);
142 if (r < 0)
143 goto fail;
144
145 if (rename(temp_path, s->state_file) < 0) {
146 r = -errno;
147 goto fail;
148 }
149
150 return 0;
151
152 fail:
153 (void) unlink(s->state_file);
154
155 if (temp_path)
156 (void) unlink(temp_path);
157
158 return log_error_errno(r, "Failed to save seat data %s: %m", s->state_file);
159 }
160
161 int seat_load(Seat *s) {
162 assert(s);
163
164 /* There isn't actually anything to read here ... */
165
166 return 0;
167 }
168
169 static int vt_allocate(unsigned vtnr) {
170 char p[sizeof("/dev/tty") + DECIMAL_STR_MAX(unsigned)];
171 _cleanup_close_ int fd = -1;
172
173 assert(vtnr >= 1);
174
175 xsprintf(p, "/dev/tty%u", vtnr);
176 fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
177 if (fd < 0)
178 return fd;
179
180 return 0;
181 }
182
183 int seat_preallocate_vts(Seat *s) {
184 int r = 0;
185 unsigned i;
186
187 assert(s);
188 assert(s->manager);
189
190 log_debug("Preallocating VTs...");
191
192 if (s->manager->n_autovts <= 0)
193 return 0;
194
195 if (!seat_has_vts(s))
196 return 0;
197
198 for (i = 1; i <= s->manager->n_autovts; i++) {
199 int q;
200
201 q = vt_allocate(i);
202 if (q < 0)
203 r = log_error_errno(q, "Failed to preallocate VT %u: %m", i);
204 }
205
206 return r;
207 }
208
209 int seat_apply_acls(Seat *s, Session *old_active) {
210 int r;
211
212 assert(s);
213
214 r = devnode_acl_all(s->id,
215 false,
216 !!old_active, old_active ? old_active->user->uid : 0,
217 !!s->active, s->active ? s->active->user->uid : 0);
218
219 if (r < 0)
220 return log_error_errno(r, "Failed to apply ACLs: %m");
221
222 return 0;
223 }
224
225 int seat_set_active(Seat *s, Session *session) {
226 Session *old_active;
227
228 assert(s);
229 assert(!session || session->seat == s);
230
231 if (session == s->active)
232 return 0;
233
234 old_active = s->active;
235 s->active = session;
236
237 if (old_active) {
238 session_device_pause_all(old_active);
239 session_send_changed(old_active, "Active", NULL);
240 }
241
242 (void) seat_apply_acls(s, old_active);
243
244 if (session && session->started) {
245 session_send_changed(session, "Active", NULL);
246 session_device_resume_all(session);
247 }
248
249 if (!session || session->started)
250 seat_send_changed(s, "ActiveSession", NULL);
251
252 seat_save(s);
253
254 if (session) {
255 session_save(session);
256 user_save(session->user);
257 }
258
259 if (old_active) {
260 session_save(old_active);
261 if (!session || session->user != old_active->user)
262 user_save(old_active->user);
263 }
264
265 return 0;
266 }
267
268 int seat_switch_to(Seat *s, unsigned num) {
269 /* Public session positions skip 0 (there is only F1-F12). Maybe it
270 * will get reassigned in the future, so return error for now. */
271 if (num == 0)
272 return -EINVAL;
273
274 if (num >= s->position_count || !s->positions[num]) {
275 /* allow switching to unused VTs to trigger auto-activate */
276 if (seat_has_vts(s) && num < 64)
277 return chvt(num);
278
279 return -EINVAL;
280 }
281
282 return session_activate(s->positions[num]);
283 }
284
285 int seat_switch_to_next(Seat *s) {
286 unsigned start, i;
287
288 if (s->position_count == 0)
289 return -EINVAL;
290
291 start = 1;
292 if (s->active && s->active->position > 0)
293 start = s->active->position;
294
295 for (i = start + 1; i < s->position_count; ++i)
296 if (s->positions[i])
297 return session_activate(s->positions[i]);
298
299 for (i = 1; i < start; ++i)
300 if (s->positions[i])
301 return session_activate(s->positions[i]);
302
303 return -EINVAL;
304 }
305
306 int seat_switch_to_previous(Seat *s) {
307 unsigned start, i;
308
309 if (s->position_count == 0)
310 return -EINVAL;
311
312 start = 1;
313 if (s->active && s->active->position > 0)
314 start = s->active->position;
315
316 for (i = start - 1; i > 0; --i)
317 if (s->positions[i])
318 return session_activate(s->positions[i]);
319
320 for (i = s->position_count - 1; i > start; --i)
321 if (s->positions[i])
322 return session_activate(s->positions[i]);
323
324 return -EINVAL;
325 }
326
327 int seat_active_vt_changed(Seat *s, unsigned vtnr) {
328 Session *i, *new_active = NULL;
329 int r;
330
331 assert(s);
332 assert(vtnr >= 1);
333
334 if (!seat_has_vts(s))
335 return -EINVAL;
336
337 log_debug("VT changed to %u", vtnr);
338
339 /* we might have earlier closing sessions on the same VT, so try to
340 * find a running one first */
341 LIST_FOREACH(sessions_by_seat, i, s->sessions)
342 if (i->vtnr == vtnr && !i->stopping) {
343 new_active = i;
344 break;
345 }
346
347 if (!new_active) {
348 /* no running one? then we can't decide which one is the
349 * active one, let the first one win */
350 LIST_FOREACH(sessions_by_seat, i, s->sessions)
351 if (i->vtnr == vtnr) {
352 new_active = i;
353 break;
354 }
355 }
356
357 r = seat_set_active(s, new_active);
358 manager_spawn_autovt(s->manager, vtnr);
359
360 return r;
361 }
362
363 int seat_read_active_vt(Seat *s) {
364 char t[64];
365 ssize_t k;
366 int vtnr;
367
368 assert(s);
369
370 if (!seat_has_vts(s))
371 return 0;
372
373 if (lseek(s->manager->console_active_fd, SEEK_SET, 0) < 0)
374 return log_error_errno(errno, "lseek on console_active_fd failed: %m");
375
376 k = read(s->manager->console_active_fd, t, sizeof(t)-1);
377 if (k <= 0) {
378 log_error("Failed to read current console: %s", k < 0 ? strerror(errno) : "EOF");
379 return k < 0 ? -errno : -EIO;
380 }
381
382 t[k] = 0;
383 truncate_nl(t);
384
385 vtnr = vtnr_from_tty(t);
386 if (vtnr < 0) {
387 log_error_errno(vtnr, "Hm, /sys/class/tty/tty0/active is badly formatted: %m");
388 return -EIO;
389 }
390
391 return seat_active_vt_changed(s, vtnr);
392 }
393
394 int seat_start(Seat *s) {
395 assert(s);
396
397 if (s->started)
398 return 0;
399
400 log_struct(LOG_INFO,
401 "MESSAGE_ID=" SD_MESSAGE_SEAT_START_STR,
402 "SEAT_ID=%s", s->id,
403 LOG_MESSAGE("New seat %s.", s->id));
404
405 /* Initialize VT magic stuff */
406 seat_preallocate_vts(s);
407
408 /* Read current VT */
409 seat_read_active_vt(s);
410
411 s->started = true;
412
413 /* Save seat data */
414 seat_save(s);
415
416 seat_send_signal(s, true);
417
418 return 0;
419 }
420
421 int seat_stop(Seat *s, bool force) {
422 int r;
423
424 assert(s);
425
426 if (s->started)
427 log_struct(LOG_INFO,
428 "MESSAGE_ID=" SD_MESSAGE_SEAT_STOP_STR,
429 "SEAT_ID=%s", s->id,
430 LOG_MESSAGE("Removed seat %s.", s->id));
431
432 r = seat_stop_sessions(s, force);
433
434 (void) unlink(s->state_file);
435 seat_add_to_gc_queue(s);
436
437 if (s->started)
438 seat_send_signal(s, false);
439
440 s->started = false;
441
442 return r;
443 }
444
445 int seat_stop_sessions(Seat *s, bool force) {
446 Session *session;
447 int r = 0, k;
448
449 assert(s);
450
451 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
452 k = session_stop(session, force);
453 if (k < 0)
454 r = k;
455 }
456
457 return r;
458 }
459
460 void seat_evict_position(Seat *s, Session *session) {
461 Session *iter;
462 unsigned pos = session->position;
463
464 session->position = 0;
465
466 if (pos == 0)
467 return;
468
469 if (pos < s->position_count && s->positions[pos] == session) {
470 s->positions[pos] = NULL;
471
472 /* There might be another session claiming the same
473 * position (eg., during gdm->session transition), so let's look
474 * for it and set it on the free slot. */
475 LIST_FOREACH(sessions_by_seat, iter, s->sessions) {
476 if (iter->position == pos && session_get_state(iter) != SESSION_CLOSING) {
477 s->positions[pos] = iter;
478 break;
479 }
480 }
481 }
482 }
483
484 void seat_claim_position(Seat *s, Session *session, unsigned pos) {
485 /* with VTs, the position is always the same as the VTnr */
486 if (seat_has_vts(s))
487 pos = session->vtnr;
488
489 if (!GREEDY_REALLOC0(s->positions, s->position_count, pos + 1))
490 return;
491
492 seat_evict_position(s, session);
493
494 session->position = pos;
495 if (pos > 0)
496 s->positions[pos] = session;
497 }
498
499 static void seat_assign_position(Seat *s, Session *session) {
500 unsigned pos;
501
502 if (session->position > 0)
503 return;
504
505 for (pos = 1; pos < s->position_count; ++pos)
506 if (!s->positions[pos])
507 break;
508
509 seat_claim_position(s, session, pos);
510 }
511
512 int seat_attach_session(Seat *s, Session *session) {
513 assert(s);
514 assert(session);
515 assert(!session->seat);
516
517 if (!seat_has_vts(s) != !session->vtnr)
518 return -EINVAL;
519
520 session->seat = s;
521 LIST_PREPEND(sessions_by_seat, s->sessions, session);
522 seat_assign_position(s, session);
523
524 /* On seats with VTs, the VT logic defines which session is active. On
525 * seats without VTs, we automatically activate new sessions. */
526 if (!seat_has_vts(s))
527 seat_set_active(s, session);
528
529 return 0;
530 }
531
532 void seat_complete_switch(Seat *s) {
533 Session *session;
534
535 assert(s);
536
537 /* if no session-switch is pending or if it got canceled, do nothing */
538 if (!s->pending_switch)
539 return;
540
541 session = TAKE_PTR(s->pending_switch);
542
543 seat_set_active(s, session);
544 }
545
546 bool seat_has_vts(Seat *s) {
547 assert(s);
548
549 return seat_is_seat0(s) && s->manager->console_active_fd >= 0;
550 }
551
552 bool seat_is_seat0(Seat *s) {
553 assert(s);
554
555 return s->manager->seat0 == s;
556 }
557
558 bool seat_can_multi_session(Seat *s) {
559 assert(s);
560
561 return seat_has_vts(s);
562 }
563
564 bool seat_can_tty(Seat *s) {
565 assert(s);
566
567 return seat_has_vts(s);
568 }
569
570 bool seat_has_master_device(Seat *s) {
571 assert(s);
572
573 /* device list is ordered by "master" flag */
574 return !!s->devices && s->devices->master;
575 }
576
577 bool seat_can_graphical(Seat *s) {
578 assert(s);
579
580 return seat_has_master_device(s);
581 }
582
583 int seat_get_idle_hint(Seat *s, dual_timestamp *t) {
584 Session *session;
585 bool idle_hint = true;
586 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
587
588 assert(s);
589
590 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
591 dual_timestamp k;
592 int ih;
593
594 ih = session_get_idle_hint(session, &k);
595 if (ih < 0)
596 return ih;
597
598 if (!ih) {
599 if (!idle_hint) {
600 if (k.monotonic > ts.monotonic)
601 ts = k;
602 } else {
603 idle_hint = false;
604 ts = k;
605 }
606 } else if (idle_hint) {
607
608 if (k.monotonic > ts.monotonic)
609 ts = k;
610 }
611 }
612
613 if (t)
614 *t = ts;
615
616 return idle_hint;
617 }
618
619 bool seat_may_gc(Seat *s, bool drop_not_started) {
620 assert(s);
621
622 if (drop_not_started && !s->started)
623 return true;
624
625 if (seat_is_seat0(s))
626 return false;
627
628 return !seat_has_master_device(s);
629 }
630
631 void seat_add_to_gc_queue(Seat *s) {
632 assert(s);
633
634 if (s->in_gc_queue)
635 return;
636
637 LIST_PREPEND(gc_queue, s->manager->seat_gc_queue, s);
638 s->in_gc_queue = true;
639 }
640
641 static bool seat_name_valid_char(char c) {
642 return
643 (c >= 'a' && c <= 'z') ||
644 (c >= 'A' && c <= 'Z') ||
645 (c >= '0' && c <= '9') ||
646 IN_SET(c, '-', '_');
647 }
648
649 bool seat_name_is_valid(const char *name) {
650 const char *p;
651
652 assert(name);
653
654 if (!startswith(name, "seat"))
655 return false;
656
657 if (!name[4])
658 return false;
659
660 for (p = name; *p; p++)
661 if (!seat_name_valid_char(*p))
662 return false;
663
664 if (strlen(name) > 255)
665 return false;
666
667 return true;
668 }