1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "sd-messages.h"
11 #include "alloc-util.h"
12 #include "errno-util.h"
15 #include "format-util.h"
16 #include "logind-acl.h"
17 #include "logind-seat-dbus.h"
18 #include "logind-seat.h"
19 #include "logind-session-dbus.h"
21 #include "parse-util.h"
22 #include "stdio-util.h"
23 #include "string-util.h"
24 #include "terminal-util.h"
25 #include "tmpfile-util.h"
28 int seat_new(Seat
** ret
, Manager
*m
, const char *id
) {
29 _cleanup_(seat_freep
) Seat
*s
= NULL
;
36 if (!seat_name_is_valid(id
))
47 s
->state_file
= strappend("/run/systemd/seats/", id
);
51 s
->id
= basename(s
->state_file
);
53 r
= hashmap_put(m
->seats
, s
->id
, s
);
61 Seat
* seat_free(Seat
*s
) {
66 LIST_REMOVE(gc_queue
, s
->manager
->seat_gc_queue
, s
);
69 session_free(s
->sessions
);
74 device_free(s
->devices
);
76 hashmap_remove(s
->manager
->seats
, s
->id
);
84 int seat_save(Seat
*s
) {
85 _cleanup_free_
char *temp_path
= NULL
;
86 _cleanup_fclose_
FILE *f
= NULL
;
94 r
= mkdir_safe_label("/run/systemd/seats", 0755, 0, 0, MKDIR_WARN_MODE
);
98 r
= fopen_temporary(s
->state_file
, &f
, &temp_path
);
102 (void) fchmod(fileno(f
), 0644);
105 "# This is private data. Do not parse.\n"
107 "CAN_MULTI_SESSION=%i\n"
109 "CAN_GRAPHICAL=%i\n",
111 seat_can_multi_session(s
),
113 seat_can_graphical(s
));
116 assert(s
->active
->user
);
120 "ACTIVE_UID="UID_FMT
"\n",
122 s
->active
->user
->uid
);
128 fputs("SESSIONS=", f
);
129 LIST_FOREACH(sessions_by_seat
, i
, s
->sessions
) {
133 i
->sessions_by_seat_next
? ' ' : '\n');
137 LIST_FOREACH(sessions_by_seat
, i
, s
->sessions
)
141 i
->sessions_by_seat_next
? ' ' : '\n');
144 r
= fflush_and_check(f
);
148 if (rename(temp_path
, s
->state_file
) < 0) {
156 (void) unlink(s
->state_file
);
159 (void) unlink(temp_path
);
161 return log_error_errno(r
, "Failed to save seat data %s: %m", s
->state_file
);
164 int seat_load(Seat
*s
) {
167 /* There isn't actually anything to read here ... */
172 static int vt_allocate(unsigned vtnr
) {
173 char p
[sizeof("/dev/tty") + DECIMAL_STR_MAX(unsigned)];
174 _cleanup_close_
int fd
= -1;
178 xsprintf(p
, "/dev/tty%u", vtnr
);
179 fd
= open_terminal(p
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
186 int seat_preallocate_vts(Seat
*s
) {
193 log_debug("Preallocating VTs...");
195 if (s
->manager
->n_autovts
<= 0)
198 if (!seat_has_vts(s
))
201 for (i
= 1; i
<= s
->manager
->n_autovts
; i
++) {
206 r
= log_error_errno(q
, "Failed to preallocate VT %u: %m", i
);
212 int seat_apply_acls(Seat
*s
, Session
*old_active
) {
217 r
= devnode_acl_all(s
->id
,
219 !!old_active
, old_active
? old_active
->user
->uid
: 0,
220 !!s
->active
, s
->active
? s
->active
->user
->uid
: 0);
223 return log_error_errno(r
, "Failed to apply ACLs: %m");
228 int seat_set_active(Seat
*s
, Session
*session
) {
232 assert(!session
|| session
->seat
== s
);
234 if (session
== s
->active
)
237 old_active
= s
->active
;
241 session_device_pause_all(old_active
);
242 session_send_changed(old_active
, "Active", NULL
);
245 (void) seat_apply_acls(s
, old_active
);
247 if (session
&& session
->started
) {
248 session_send_changed(session
, "Active", NULL
);
249 session_device_resume_all(session
);
252 if (!session
|| session
->started
)
253 seat_send_changed(s
, "ActiveSession", NULL
);
258 session_save(session
);
259 user_save(session
->user
);
263 session_save(old_active
);
264 if (!session
|| session
->user
!= old_active
->user
)
265 user_save(old_active
->user
);
271 int seat_switch_to(Seat
*s
, unsigned num
) {
272 /* Public session positions skip 0 (there is only F1-F12). Maybe it
273 * will get reassigned in the future, so return error for now. */
277 if (num
>= s
->position_count
|| !s
->positions
[num
]) {
278 /* allow switching to unused VTs to trigger auto-activate */
279 if (seat_has_vts(s
) && num
< 64)
285 return session_activate(s
->positions
[num
]);
288 int seat_switch_to_next(Seat
*s
) {
291 if (s
->position_count
== 0)
295 if (s
->active
&& s
->active
->position
> 0)
296 start
= s
->active
->position
;
298 for (i
= start
+ 1; i
< s
->position_count
; ++i
)
300 return session_activate(s
->positions
[i
]);
302 for (i
= 1; i
< start
; ++i
)
304 return session_activate(s
->positions
[i
]);
309 int seat_switch_to_previous(Seat
*s
) {
312 if (s
->position_count
== 0)
316 if (s
->active
&& s
->active
->position
> 0)
317 start
= s
->active
->position
;
319 for (i
= start
- 1; i
> 0; --i
)
321 return session_activate(s
->positions
[i
]);
323 for (i
= s
->position_count
- 1; i
> start
; --i
)
325 return session_activate(s
->positions
[i
]);
330 int seat_active_vt_changed(Seat
*s
, unsigned vtnr
) {
331 Session
*i
, *new_active
= NULL
;
337 if (!seat_has_vts(s
))
340 log_debug("VT changed to %u", vtnr
);
342 /* we might have earlier closing sessions on the same VT, so try to
343 * find a running one first */
344 LIST_FOREACH(sessions_by_seat
, i
, s
->sessions
)
345 if (i
->vtnr
== vtnr
&& !i
->stopping
) {
351 /* no running one? then we can't decide which one is the
352 * active one, let the first one win */
353 LIST_FOREACH(sessions_by_seat
, i
, s
->sessions
)
354 if (i
->vtnr
== vtnr
) {
360 r
= seat_set_active(s
, new_active
);
361 manager_spawn_autovt(s
->manager
, vtnr
);
366 int seat_read_active_vt(Seat
*s
) {
373 if (!seat_has_vts(s
))
376 if (lseek(s
->manager
->console_active_fd
, SEEK_SET
, 0) < 0)
377 return log_error_errno(errno
, "lseek on console_active_fd failed: %m");
379 k
= read(s
->manager
->console_active_fd
, t
, sizeof(t
)-1);
381 log_error("Failed to read current console: %s", k
< 0 ? strerror_safe(errno
) : "EOF");
382 return k
< 0 ? -errno
: -EIO
;
388 vtnr
= vtnr_from_tty(t
);
390 log_error_errno(vtnr
, "Hm, /sys/class/tty/tty0/active is badly formatted: %m");
394 return seat_active_vt_changed(s
, vtnr
);
397 int seat_start(Seat
*s
) {
404 "MESSAGE_ID=" SD_MESSAGE_SEAT_START_STR
,
406 LOG_MESSAGE("New seat %s.", s
->id
));
408 /* Initialize VT magic stuff */
409 seat_preallocate_vts(s
);
411 /* Read current VT */
412 seat_read_active_vt(s
);
419 seat_send_signal(s
, true);
424 int seat_stop(Seat
*s
, bool force
) {
431 "MESSAGE_ID=" SD_MESSAGE_SEAT_STOP_STR
,
433 LOG_MESSAGE("Removed seat %s.", s
->id
));
435 r
= seat_stop_sessions(s
, force
);
437 (void) unlink(s
->state_file
);
438 seat_add_to_gc_queue(s
);
441 seat_send_signal(s
, false);
448 int seat_stop_sessions(Seat
*s
, bool force
) {
454 LIST_FOREACH(sessions_by_seat
, session
, s
->sessions
) {
455 k
= session_stop(session
, force
);
463 void seat_evict_position(Seat
*s
, Session
*session
) {
465 unsigned pos
= session
->position
;
467 session
->position
= 0;
472 if (pos
< s
->position_count
&& s
->positions
[pos
] == session
) {
473 s
->positions
[pos
] = NULL
;
475 /* There might be another session claiming the same
476 * position (eg., during gdm->session transition), so let's look
477 * for it and set it on the free slot. */
478 LIST_FOREACH(sessions_by_seat
, iter
, s
->sessions
) {
479 if (iter
->position
== pos
&& session_get_state(iter
) != SESSION_CLOSING
) {
480 s
->positions
[pos
] = iter
;
487 void seat_claim_position(Seat
*s
, Session
*session
, unsigned pos
) {
488 /* with VTs, the position is always the same as the VTnr */
492 if (!GREEDY_REALLOC0(s
->positions
, s
->position_count
, pos
+ 1))
495 seat_evict_position(s
, session
);
497 session
->position
= pos
;
499 s
->positions
[pos
] = session
;
502 static void seat_assign_position(Seat
*s
, Session
*session
) {
505 if (session
->position
> 0)
508 for (pos
= 1; pos
< s
->position_count
; ++pos
)
509 if (!s
->positions
[pos
])
512 seat_claim_position(s
, session
, pos
);
515 int seat_attach_session(Seat
*s
, Session
*session
) {
518 assert(!session
->seat
);
520 if (!seat_has_vts(s
) != !session
->vtnr
)
524 LIST_PREPEND(sessions_by_seat
, s
->sessions
, session
);
525 seat_assign_position(s
, session
);
527 /* On seats with VTs, the VT logic defines which session is active. On
528 * seats without VTs, we automatically activate new sessions. */
529 if (!seat_has_vts(s
))
530 seat_set_active(s
, session
);
535 void seat_complete_switch(Seat
*s
) {
540 /* if no session-switch is pending or if it got canceled, do nothing */
541 if (!s
->pending_switch
)
544 session
= TAKE_PTR(s
->pending_switch
);
546 seat_set_active(s
, session
);
549 bool seat_has_vts(Seat
*s
) {
552 return seat_is_seat0(s
) && s
->manager
->console_active_fd
>= 0;
555 bool seat_is_seat0(Seat
*s
) {
558 return s
->manager
->seat0
== s
;
561 bool seat_can_multi_session(Seat
*s
) {
564 return seat_has_vts(s
);
567 bool seat_can_tty(Seat
*s
) {
570 return seat_has_vts(s
);
573 bool seat_has_master_device(Seat
*s
) {
576 /* device list is ordered by "master" flag */
577 return !!s
->devices
&& s
->devices
->master
;
580 bool seat_can_graphical(Seat
*s
) {
583 return seat_has_master_device(s
);
586 int seat_get_idle_hint(Seat
*s
, dual_timestamp
*t
) {
588 bool idle_hint
= true;
589 dual_timestamp ts
= DUAL_TIMESTAMP_NULL
;
593 LIST_FOREACH(sessions_by_seat
, session
, s
->sessions
) {
597 ih
= session_get_idle_hint(session
, &k
);
603 if (k
.monotonic
> ts
.monotonic
)
609 } else if (idle_hint
) {
611 if (k
.monotonic
> ts
.monotonic
)
622 bool seat_may_gc(Seat
*s
, bool drop_not_started
) {
625 if (drop_not_started
&& !s
->started
)
628 if (seat_is_seat0(s
))
631 return !seat_has_master_device(s
);
634 void seat_add_to_gc_queue(Seat
*s
) {
640 LIST_PREPEND(gc_queue
, s
->manager
->seat_gc_queue
, s
);
641 s
->in_gc_queue
= true;
644 static bool seat_name_valid_char(char c
) {
646 (c
>= 'a' && c
<= 'z') ||
647 (c
>= 'A' && c
<= 'Z') ||
648 (c
>= '0' && c
<= '9') ||
652 bool seat_name_is_valid(const char *name
) {
657 if (!startswith(name
, "seat"))
663 for (p
= name
; *p
; p
++)
664 if (!seat_name_valid_char(*p
))
667 if (strlen(name
) > 255)