Debian Codegraph @

src/login/logind-seat.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7 #include "sd-messages.h"
8
9 #include "acl-util.h"
10 #include "alloc-util.h"
11 #include "device-util.h"
12 #include "dirent-util.h"
13 #include "errno-util.h"
14 #include "fd-util.h"
15 #include "format-util.h"
16 #include "fs-util.h"
17 #include "hashmap.h"
18 #include "id128-util.h"
19 #include "json-util.h"
20 #include "log.h"
21 #include "logind.h"
22 #include "logind-device.h"
23 #include "logind-seat.h"
24 #include "logind-seat-dbus.h"
25 #include "logind-session.h"
26 #include "logind-session-dbus.h"
27 #include "logind-session-device.h"
28 #include "logind-user.h"
29 #include "mkdir.h"
30 #include "path-util.h"
31 #include "set.h"
32 #include "stat-util.h"
33 #include "stdio-util.h"
34 #include "string-util.h"
35 #include "terminal-util.h"
36 #include "tmpfile-util.h"
37 #include "udev-util.h"
38 #include "user-record.h"
39 #include "user-util.h"
40
41 int seat_new(Manager *m, const char *id, Seat **ret) {
42 _cleanup_(seat_freep) Seat *s = NULL;
43 int r;
44
45 assert(m);
46 assert(id);
47 assert(ret);
48
49 if (!seat_name_is_valid(id))
50 return -EINVAL;
51
52 s = new(Seat, 1);
53 if (!s)
54 return -ENOMEM;
55
56 *s = (Seat) {
57 .manager = m,
58 .id = strdup(id),
59 .state_file = path_join("/run/systemd/seats/", id),
60 };
61 if (!s->id || !s->state_file)
62 return -ENOMEM;
63
64 r = hashmap_put(m->seats, s->id, s);
65 if (r < 0)
66 return r;
67
68 *ret = TAKE_PTR(s);
69 return 0;
70 }
71
72 Seat* seat_free(Seat *s) {
73 if (!s)
74 return NULL;
75
76 if (s->in_gc_queue)
77 LIST_REMOVE(gc_queue, s->manager->seat_gc_queue, s);
78
79 while (s->sessions)
80 session_free(s->sessions);
81
82 assert(!s->active);
83
84 while (s->devices)
85 device_free(s->devices);
86
87 hashmap_remove(s->manager->seats, s->id);
88
89 set_free(s->uevents);
90 free(s->positions);
91 free(s->state_file);
92 free(s->id);
93
94 return mfree(s);
95 }
96
97 int seat_save(Seat *s) {
98 int r;
99
100 assert(s);
101
102 if (!s->started)
103 return 0;
104
105 r = mkdir_safe_label("/run/systemd/seats", 0755, 0, 0, MKDIR_WARN_MODE);
106 if (r < 0)
107 return log_error_errno(r, "Failed to create /run/systemd/seats/: %m");
108
109 _cleanup_(unlink_and_freep) char *temp_path = NULL;
110 _cleanup_fclose_ FILE *f = NULL;
111 r = fopen_tmpfile_linkable(s->state_file, O_WRONLY|O_CLOEXEC, &temp_path, &f);
112 if (r < 0)
113 return log_error_errno(r, "Failed to create state file '%s': %m", s->state_file);
114
115 if (fchmod(fileno(f), 0644) < 0)
116 return log_error_errno(errno, "Failed to set access mode for state file '%s' to 0644: %m", s->state_file);
117
118 fprintf(f,
119 "# This is private data. Do not parse.\n"
120 "IS_SEAT0=%i\n"
121 "CAN_MULTI_SESSION=1\n"
122 "CAN_TTY=%i\n"
123 "CAN_GRAPHICAL=%i\n",
124 seat_is_seat0(s),
125 seat_can_tty(s),
126 seat_can_graphical(s));
127
128 if (s->active) {
129 assert(s->active->user);
130
131 fprintf(f,
132 "ACTIVE=%s\n"
133 "ACTIVE_UID="UID_FMT"\n",
134 s->active->id,
135 s->active->user->user_record->uid);
136 }
137
138 if (s->sessions) {
139 fputs("SESSIONS=", f);
140 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
141 fprintf(f,
142 "%s%c",
143 i->id,
144 i->sessions_by_seat_next ? ' ' : '\n');
145 }
146
147 fputs("UIDS=", f);
148 LIST_FOREACH(sessions_by_seat, i, s->sessions)
149 fprintf(f,
150 UID_FMT"%c",
151 i->user->user_record->uid,
152 i->sessions_by_seat_next ? ' ' : '\n');
153 }
154
155 r = flink_tmpfile(f, temp_path, s->state_file, LINK_TMPFILE_REPLACE);
156 if (r < 0)
157 return log_error_errno(r, "Failed to move '%s' into place: %m", s->state_file);
158
159 temp_path = mfree(temp_path); /* disarm auto-destroy: temporary file does not exist anymore */
160 return 0;
161 }
162
163 int seat_load(Seat *s) {
164 assert(s);
165
166 /* There isn't actually anything to read here ... */
167
168 return 0;
169 }
170
171 static int vt_allocate(unsigned vtnr) {
172 char p[sizeof("/dev/tty") + DECIMAL_STR_MAX(unsigned)];
173 _cleanup_close_ int fd = -EBADF;
174
175 assert(vtnr >= 1);
176
177 xsprintf(p, "/dev/tty%u", vtnr);
178 fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
179 if (fd < 0)
180 return fd;
181
182 return 0;
183 }
184
185 int seat_preallocate_vts(Seat *s) {
186 int r = 0;
187 unsigned i;
188
189 assert(s);
190 assert(s->manager);
191
192 if (s->manager->n_autovts <= 0)
193 return 0;
194
195 if (!seat_has_vts(s))
196 return 0;
197
198 log_debug("Preallocating VTs...");
199
200 for (i = 1; i <= s->manager->n_autovts; i++) {
201 int q;
202
203 q = vt_allocate(i);
204 if (q < 0)
205 r = log_error_errno(q, "Failed to preallocate VT %u: %m", i);
206 }
207
208 return r;
209 }
210
211 static void seat_triggered_uevents_done(Seat *s) {
212 assert(s);
213
214 if (!set_isempty(s->uevents)) {
215 log_debug("%s: waiting for %u events being processed by udevd.", s->id, set_size(s->uevents));
216 return;
217 }
218
219 Session *session = s->active;
220
221 if (session) {
222 session_save(session);
223 user_save(session->user);
224 }
225
226 if (session && session->started) {
227 session_send_changed(session, "Active");
228 session_device_resume_all(session);
229 }
230
231 if (!session || session->started)
232 seat_send_changed(s, "ActiveSession");
233 }
234
235 int manager_process_device_triggered_by_seat(Manager *m, sd_device *dev) {
236 int r;
237
238 assert(m);
239 assert(dev);
240
241 sd_id128_t uuid;
242 r = sd_device_get_trigger_uuid(dev, &uuid);
243 if (IN_SET(r, -ENOENT, -ENODATA))
244 return 0;
245 if (r < 0)
246 return r;
247
248 Seat *s;
249 HASHMAP_FOREACH(s, m->seats)
250 if (set_contains(s->uevents, &uuid))
251 break;
252 if (!s)
253 return 0;
254
255 log_device_uevent(dev, "Received event processed by udevd");
256 free(ASSERT_PTR(set_remove(s->uevents, &uuid)));
257 seat_triggered_uevents_done(s);
258
259 const char *id;
260 r = device_get_seat(dev, &id);
261 if (r < 0)
262 return r;
263
264 if (!streq(id, s->id)) {
265 log_device_debug(dev, "ID_SEAT is changed in the triggered uevent: \"%s\" -> \"%s\"", s->id, id);
266 return 0;
267 }
268
269 return 1; /* The uevent is triggered by the relevant seat. */
270 }
271
272 static int seat_trigger_devices(Seat *s) {
273 int r;
274
275 assert(s);
276
277 set_clear(s->uevents);
278
279 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
280 r = sd_device_enumerator_new(&e);
281 if (r < 0)
282 return r;
283
284 r = sd_device_enumerator_add_match_tag(e, "uaccess");
285 if (r < 0)
286 return r;
287
288 FOREACH_DEVICE(e, d) {
289 /* Verify that the tag is still in place. */
290 r = sd_device_has_current_tag(d, "uaccess");
291 if (r < 0)
292 return r;
293 if (r == 0)
294 continue;
295
296 /* In case people mistag devices without nodes, we need to ignore this. */
297 r = sd_device_get_devname(d, NULL);
298 if (r == -ENOENT)
299 continue;
300 if (r < 0)
301 return r;
302
303 const char *id;
304 r = device_get_seat(d, &id);
305 if (r < 0)
306 return r;
307
308 if (!streq(id, s->id))
309 continue;
310
311 sd_id128_t uuid;
312 r = sd_device_trigger_with_uuid(d, SD_DEVICE_CHANGE, &uuid);
313 if (r < 0) {
314 log_device_debug_errno(d, r, "Failed to trigger 'change' event, ignoring: %m");
315 continue;
316 }
317
318 log_device_debug(d, "Triggered synthetic event (ACTION=change, UUID=%s).", SD_ID128_TO_UUID_STRING(uuid));
319
320 _cleanup_free_ sd_id128_t *copy = newdup(sd_id128_t, &uuid, 1);
321 if (!copy)
322 return -ENOMEM;
323
324 r = set_ensure_consume(&s->uevents, &id128_hash_ops_free, TAKE_PTR(copy));
325 if (r < 0)
326 return r;
327 }
328
329 return 0;
330 }
331
332 static int static_node_acl(Seat *s) {
333 #if HAVE_ACL
334 int r, ret = 0;
335 _cleanup_set_free_ Set *uids = NULL;
336
337 assert(s);
338
339 if (s->active) {
340 r = set_ensure_put(&uids, NULL, UID_TO_PTR(s->active->user->user_record->uid));
341 if (r < 0)
342 return log_oom();
343 }
344
345 _cleanup_closedir_ DIR *dir = opendir("/run/udev/static_node-tags/uaccess/");
346 if (!dir) {
347 if (errno == ENOENT)
348 return 0;
349
350 return log_debug_errno(errno, "Failed to open /run/udev/static_node-tags/uaccess/: %m");
351 }
352
353 FOREACH_DIRENT(de, dir, return -errno) {
354 _cleanup_close_ int fd = RET_NERRNO(openat(dirfd(dir), de->d_name, O_CLOEXEC|O_PATH));
355 if (ERRNO_IS_NEG_DEVICE_ABSENT_OR_EMPTY(fd))
356 continue;
357 if (fd < 0) {
358 RET_GATHER(ret, log_debug_errno(fd, "Failed to open '/run/udev/static_node-tags/uaccess/%s': %m", de->d_name));
359 continue;
360 }
361
362 struct stat st;
363 if (fstat(fd, &st) < 0) {
364 RET_GATHER(ret, log_debug_errno(errno, "Failed to stat '/run/udev/static_node-tags/uaccess/%s': %m", de->d_name));
365 continue;
366 }
367
368 r = stat_verify_device_node(&st);
369 if (r < 0) {
370 RET_GATHER(ret, log_debug_errno(fd, "'/run/udev/static_node-tags/uaccess/%s' points to a non-device node: %m", de->d_name));
371 continue;
372 }
373
374 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
375 r = sd_device_new_from_stat_rdev(&dev, &st);
376 if (r >= 0) {
377 log_device_debug(dev, "'/run/udev/static_node-tags/uaccess/%s' points to a non-static device node, ignoring.", de->d_name);
378 continue;
379 }
380 if (!ERRNO_IS_NEG_DEVICE_ABSENT_OR_EMPTY(r))
381 log_debug_errno(r, "Failed to check if '/run/udev/static_node-tags/uaccess/%s' points to a static device node, ignoring: %m", de->d_name);
382
383 r = devnode_acl(fd, uids);
384 if (r >= 0 || r == -ENOENT)
385 continue;
386
387 /* de->d_name is escaped, like "snd\x2ftimer", hence let's use the path to node, if possible. */
388 _cleanup_free_ char *node = NULL;
389 (void) fd_get_path(fd, &node);
390
391 if (!set_isempty(uids)) {
392 RET_GATHER(ret, log_debug_errno(r, "Failed to apply ACL on '%s': %m", node ?: de->d_name));
393
394 /* Better be safe than sorry and reset ACL */
395 r = devnode_acl(fd, /* uids= */ NULL);
396 if (r >= 0 || r == -ENOENT)
397 continue;
398 }
399 if (r < 0)
400 RET_GATHER(ret, log_debug_errno(r, "Failed to flush ACL on '%s': %m", node ?: de->d_name));
401 }
402
403 return ret;
404 #else
405 return 0;
406 #endif
407 }
408
409 int seat_set_active(Seat *s, Session *session) {
410 Session *old_active;
411 int r;
412
413 assert(s);
414 assert(!session || session->seat == s);
415
416 /* When logind receives the SIGRTMIN signal from the kernel, it will
417 * execute session_leave_vt and stop all devices of the session; at
418 * this time, if the session is active and there is no change in the
419 * session, then the session does not have the permissions of the device,
420 * and the machine will have a black screen and suspended animation.
421 * Therefore, if the active session has executed session_leave_vt ,
422 * A resume is required here. */
423 if (session == s->active) {
424 if (session && set_isempty(s->uevents)) {
425 log_debug("Active session remains unchanged, resuming session devices.");
426 session_device_resume_all(session);
427 }
428 return 0;
429 }
430
431 old_active = s->active;
432 s->active = session;
433
434 seat_save(s);
435
436 if (old_active) {
437 user_save(old_active->user);
438 session_save(old_active);
439 session_device_pause_all(old_active);
440 session_send_changed(old_active, "Active");
441 }
442
443 r = seat_trigger_devices(s);
444 if (r < 0)
445 return r;
446
447 r = static_node_acl(s);
448 if (r < 0)
449 return r;
450
451 seat_triggered_uevents_done(s);
452 return 0;
453 }
454
455 static Session* seat_get_position(Seat *s, unsigned pos) {
456 assert(s);
457
458 if (pos >= MALLOC_ELEMENTSOF(s->positions))
459 return NULL;
460
461 return s->positions[pos];
462 }
463
464 int seat_switch_to(Seat *s, unsigned num) {
465 Session *session;
466
467 /* Public session positions skip 0 (there is only F1-F12). Maybe it
468 * will get reassigned in the future, so return error for now. */
469 if (num == 0)
470 return -EINVAL;
471
472 session = seat_get_position(s, num);
473 if (!session) {
474 /* allow switching to unused VTs to trigger auto-activate */
475 if (seat_has_vts(s) && num < 64)
476 return chvt(num);
477
478 return -EINVAL;
479 }
480
481 return session_activate(session);
482 }
483
484 int seat_switch_to_next(Seat *s) {
485 unsigned start, i;
486 Session *session;
487
488 if (MALLOC_ELEMENTSOF(s->positions) == 0)
489 return -EINVAL;
490
491 start = 1;
492 if (s->active && s->active->position > 0)
493 start = s->active->position;
494
495 for (i = start + 1; i < MALLOC_ELEMENTSOF(s->positions); ++i) {
496 session = seat_get_position(s, i);
497 if (session)
498 return session_activate(session);
499 }
500
501 for (i = 1; i < start; ++i) {
502 session = seat_get_position(s, i);
503 if (session)
504 return session_activate(session);
505 }
506
507 return -EINVAL;
508 }
509
510 int seat_switch_to_previous(Seat *s) {
511 if (MALLOC_ELEMENTSOF(s->positions) == 0)
512 return -EINVAL;
513
514 size_t start = s->active && s->active->position > 0 ? s->active->position : 1;
515
516 for (size_t i = start - 1; i > 0; i--) {
517 Session *session = seat_get_position(s, i);
518 if (session)
519 return session_activate(session);
520 }
521
522 for (size_t i = MALLOC_ELEMENTSOF(s->positions) - 1; i > start; i--) {
523 Session *session = seat_get_position(s, i);
524 if (session)
525 return session_activate(session);
526 }
527
528 return -EINVAL;
529 }
530
531 int seat_active_vt_changed(Seat *s, unsigned vtnr) {
532 Session *new_active = NULL;
533 int r;
534
535 assert(s);
536 assert(vtnr >= 1);
537
538 if (!seat_has_vts(s))
539 return -EINVAL;
540
541 log_debug("VT changed to %u", vtnr);
542
543 /* we might have earlier closing sessions on the same VT, so try to
544 * find a running one first */
545 LIST_FOREACH(sessions_by_seat, i, s->sessions)
546 if (i->vtnr == vtnr && !i->stopping) {
547 new_active = i;
548 break;
549 }
550
551 if (!new_active)
552 /* no running one? then we can't decide which one is the
553 * active one, let the first one win */
554 LIST_FOREACH(sessions_by_seat, i, s->sessions)
555 if (i->vtnr == vtnr) {
556 new_active = i;
557 break;
558 }
559
560 r = seat_set_active(s, new_active);
561 manager_spawn_autovt(s->manager, vtnr);
562
563 return r;
564 }
565
566 int seat_read_active_vt(Seat *s) {
567 char t[64];
568 ssize_t k;
569 int vtnr;
570
571 assert(s);
572
573 if (!seat_has_vts(s))
574 return 0;
575
576 if (lseek(s->manager->console_active_fd, 0, SEEK_SET) < 0)
577 return log_error_errno(errno, "lseek() on console_active_fd failed: %m");
578
579 errno = 0;
580 k = read(s->manager->console_active_fd, t, sizeof(t)-1);
581 if (k <= 0)
582 return log_error_errno(errno ?: EIO,
583 "Failed to read current console: %s", STRERROR_OR_EOF(errno));
584
585 t[k] = 0;
586 truncate_nl(t);
587
588 vtnr = vtnr_from_tty(t);
589 if (vtnr < 0) {
590 log_error_errno(vtnr, "Hm, /sys/class/tty/tty0/active is badly formatted: %m");
591 return -EIO;
592 }
593
594 return seat_active_vt_changed(s, vtnr);
595 }
596
597 int seat_start(Seat *s) {
598 assert(s);
599
600 if (s->started)
601 return 0;
602
603 log_struct(LOG_INFO,
604 "MESSAGE_ID=" SD_MESSAGE_SEAT_START_STR,
605 "SEAT_ID=%s", s->id,
606 LOG_MESSAGE("New seat %s.", s->id));
607
608 /* Initialize VT magic stuff */
609 seat_preallocate_vts(s);
610
611 /* Read current VT */
612 seat_read_active_vt(s);
613
614 s->started = true;
615
616 /* Save seat data */
617 seat_save(s);
618
619 seat_send_signal(s, true);
620
621 return 0;
622 }
623
624 int seat_stop(Seat *s, bool force) {
625 int r;
626
627 assert(s);
628
629 if (s->started)
630 log_struct(LOG_INFO,
631 "MESSAGE_ID=" SD_MESSAGE_SEAT_STOP_STR,
632 "SEAT_ID=%s", s->id,
633 LOG_MESSAGE("Removed seat %s.", s->id));
634
635 r = seat_stop_sessions(s, force);
636
637 (void) unlink(s->state_file);
638 seat_add_to_gc_queue(s);
639
640 if (s->started)
641 seat_send_signal(s, false);
642
643 s->started = false;
644
645 return r;
646 }
647
648 int seat_stop_sessions(Seat *s, bool force) {
649 int r = 0, k;
650
651 assert(s);
652
653 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
654 k = session_stop(session, force);
655 if (k < 0)
656 r = k;
657 }
658
659 return r;
660 }
661
662 void seat_evict_position(Seat *s, Session *session) {
663 unsigned pos = session->position;
664
665 session->position = 0;
666
667 if (pos == 0)
668 return;
669
670 if (pos < MALLOC_ELEMENTSOF(s->positions) && s->positions[pos] == session) {
671 s->positions[pos] = NULL;
672
673 /* There might be another session claiming the same
674 * position (eg., during gdm->session transition), so let's look
675 * for it and set it on the free slot. */
676 LIST_FOREACH(sessions_by_seat, iter, s->sessions)
677 if (iter->position == pos && session_get_state(iter) != SESSION_CLOSING) {
678 s->positions[pos] = iter;
679 break;
680 }
681 }
682 }
683
684 void seat_claim_position(Seat *s, Session *session, unsigned pos) {
685 /* with VTs, the position is always the same as the VTnr */
686 if (seat_has_vts(s))
687 pos = session->vtnr;
688
689 if (!GREEDY_REALLOC0(s->positions, pos + 1))
690 return;
691
692 seat_evict_position(s, session);
693
694 session->position = pos;
695 if (pos > 0)
696 s->positions[pos] = session;
697 }
698
699 static void seat_assign_position(Seat *s, Session *session) {
700 unsigned pos;
701
702 if (session->position > 0)
703 return;
704
705 for (pos = 1; pos < MALLOC_ELEMENTSOF(s->positions); ++pos)
706 if (!s->positions[pos])
707 break;
708
709 seat_claim_position(s, session, pos);
710 }
711
712 int seat_attach_session(Seat *s, Session *session) {
713 assert(s);
714 assert(session);
715 assert(!session->seat);
716
717 if (!seat_has_vts(s) != !session->vtnr)
718 return -EINVAL;
719
720 session->seat = s;
721 LIST_PREPEND(sessions_by_seat, s->sessions, session);
722 seat_assign_position(s, session);
723
724 /* On seats with VTs, the VT logic defines which session is active. On
725 * seats without VTs, we automatically activate new sessions. */
726 if (!seat_has_vts(s))
727 seat_set_active(s, session);
728
729 return 0;
730 }
731
732 void seat_complete_switch(Seat *s) {
733 Session *session;
734
735 assert(s);
736
737 /* if no session-switch is pending or if it got canceled, do nothing */
738 if (!s->pending_switch)
739 return;
740
741 session = TAKE_PTR(s->pending_switch);
742
743 seat_set_active(s, session);
744 }
745
746 bool seat_has_vts(Seat *s) {
747 assert(s);
748
749 return seat_is_seat0(s) && s->manager->console_active_fd >= 0;
750 }
751
752 bool seat_is_seat0(Seat *s) {
753 assert(s);
754
755 return s->manager->seat0 == s;
756 }
757
758 bool seat_can_tty(Seat *s) {
759 assert(s);
760
761 return seat_has_vts(s);
762 }
763
764 bool seat_has_master_device(Seat *s) {
765 assert(s);
766
767 /* device list is ordered by "master" flag */
768 return !!s->devices && s->devices->master;
769 }
770
771 bool seat_can_graphical(Seat *s) {
772 assert(s);
773
774 return seat_has_master_device(s);
775 }
776
777 bool seat_get_idle_hint(Seat *s, dual_timestamp *ret_timestamp) {
778 bool idle_hint = true;
779 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
780
781 assert(s);
782
783 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
784 dual_timestamp k;
785 bool ih;
786
787 ih = session_get_idle_hint(session, &k);
788 if (!ih) {
789 if (!idle_hint) {
790 if (k.monotonic > ts.monotonic)
791 ts = k;
792 } else {
793 idle_hint = false;
794 ts = k;
795 }
796 } else if (idle_hint) {
797 if (k.monotonic > ts.monotonic)
798 ts = k;
799 }
800 }
801
802 if (ret_timestamp)
803 *ret_timestamp = ts;
804
805 return idle_hint;
806 }
807
808 bool seat_may_gc(Seat *s, bool drop_not_started) {
809 assert(s);
810
811 if (drop_not_started && !s->started)
812 return true;
813
814 if (seat_is_seat0(s))
815 return false;
816
817 return !seat_has_master_device(s);
818 }
819
820 void seat_add_to_gc_queue(Seat *s) {
821 assert(s);
822
823 if (s->in_gc_queue)
824 return;
825
826 LIST_PREPEND(gc_queue, s->manager->seat_gc_queue, s);
827 s->in_gc_queue = true;
828 }
829
830 static int seat_sessions_build_json(sd_json_variant **ret, const char *name, void *userdata) {
831 Seat *s = ASSERT_PTR(userdata);
832 int r;
833
834 assert(ret);
835
836 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
837 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
838 r = sd_json_variant_append_arrayb(&v, SD_JSON_BUILD_STRING(session->id));
839 if (r < 0)
840 return r;
841 }
842
843 *ret = TAKE_PTR(v);
844 return 0;
845 }
846
847 static int seat_context_build_json(sd_json_variant **ret, const char *name, void *userdata) {
848 Seat *s = ASSERT_PTR(userdata);
849
850 assert(ret);
851
852 return sd_json_buildo(
853 ret,
854 SD_JSON_BUILD_PAIR_STRING("Id", s->id));
855 }
856
857 static int seat_runtime_build_json(sd_json_variant **ret, const char *name, void *userdata) {
858 Seat *s = ASSERT_PTR(userdata);
859
860 assert(ret);
861
862 dual_timestamp idle_ts;
863 bool idle = seat_get_idle_hint(s, &idle_ts);
864
865 return sd_json_buildo(
866 ret,
867 JSON_BUILD_PAIR_STRING_NON_EMPTY("ActiveSession", s->active ? s->active->id : NULL),
868 JSON_BUILD_PAIR_CALLBACK_NON_NULL("Sessions", seat_sessions_build_json, s),
869 SD_JSON_BUILD_PAIR_BOOLEAN("CanTTY", seat_can_tty(s)),
870 SD_JSON_BUILD_PAIR_BOOLEAN("CanGraphical", seat_can_graphical(s)),
871 SD_JSON_BUILD_PAIR_BOOLEAN("IdleHint", idle),
872 SD_JSON_BUILD_PAIR_CONDITION(idle, "IdleSinceHint", JSON_BUILD_DUAL_TIMESTAMP(&idle_ts)));
873 }
874
875 int seat_build_json(Seat *s, sd_json_variant **ret) {
876 assert(s);
877 assert(ret);
878
879 return sd_json_buildo(
880 ret,
881 SD_JSON_BUILD_PAIR_CALLBACK("context", seat_context_build_json, s),
882 SD_JSON_BUILD_PAIR_CALLBACK("runtime", seat_runtime_build_json, s));
883 }
884
885 static bool seat_name_valid_char(char c) {
886 return
887 ascii_isalpha(c) ||
888 ascii_isdigit(c) ||
889 IN_SET(c, '-', '_');
890 }
891
892 bool seat_name_is_valid(const char *name) {
893 const char *p;
894
895 assert(name);
896
897 if (!startswith(name, "seat"))
898 return false;
899
900 if (!name[4])
901 return false;
902
903 for (p = name; *p; p++)
904 if (!seat_name_valid_char(*p))
905 return false;
906
907 if (strlen(name) > 255)
908 return false;
909
910 return true;
911 }
912
913 bool seat_is_self(const char *name) {
914 return isempty(name) || streq(name, "self");
915 }
916
917 bool seat_is_auto(const char *name) {
918 return streq_ptr(name, "auto");
919 }