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