1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/ioctl.h>
13 #include "sd-messages.h"
15 #include "alloc-util.h"
16 #include "audit-util.h"
17 #include "bus-error.h"
23 #include "format-util.h"
25 #include "logind-session.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "serialize.h"
31 #include "string-table.h"
33 #include "terminal-util.h"
34 #include "tmpfile-util.h"
35 #include "user-util.h"
38 #define RELEASE_USEC (20*USEC_PER_SEC)
40 static void session_remove_fifo(Session
*s
);
41 static void session_restore_vt(Session
*s
);
43 int session_new(Session
**ret
, Manager
*m
, const char *id
) {
44 _cleanup_(session_freep
) Session
*s
= NULL
;
51 if (!session_id_valid(id
))
62 .audit_id
= AUDIT_SESSION_INVALID
,
63 .tty_validity
= _TTY_VALIDITY_INVALID
,
66 s
->state_file
= strappend("/run/systemd/sessions/", id
);
70 s
->id
= basename(s
->state_file
);
72 s
->devices
= hashmap_new(&devt_hash_ops
);
76 r
= hashmap_put(m
->sessions
, s
->id
, s
);
84 Session
* session_free(Session
*s
) {
91 LIST_REMOVE(gc_queue
, s
->manager
->session_gc_queue
, s
);
93 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
95 session_remove_fifo(s
);
97 session_drop_controller(s
);
99 while ((sd
= hashmap_first(s
->devices
)))
100 session_device_free(sd
);
102 hashmap_free(s
->devices
);
105 LIST_REMOVE(sessions_by_user
, s
->user
->sessions
, s
);
107 if (s
->user
->display
== s
)
108 s
->user
->display
= NULL
;
110 user_update_last_session_timer(s
->user
);
114 if (s
->seat
->active
== s
)
115 s
->seat
->active
= NULL
;
116 if (s
->seat
->pending_switch
== s
)
117 s
->seat
->pending_switch
= NULL
;
119 seat_evict_position(s
->seat
, s
);
120 LIST_REMOVE(sessions_by_seat
, s
->seat
->sessions
, s
);
124 hashmap_remove(s
->manager
->session_units
, s
->scope
);
128 if (pid_is_valid(s
->leader
))
129 (void) hashmap_remove_value(s
->manager
->sessions_by_leader
, PID_TO_PTR(s
->leader
), s
);
133 sd_bus_message_unref(s
->create_message
);
137 free(s
->remote_host
);
138 free(s
->remote_user
);
142 hashmap_remove(s
->manager
->sessions
, s
->id
);
149 void session_set_user(Session
*s
, User
*u
) {
154 LIST_PREPEND(sessions_by_user
, u
->sessions
, s
);
156 user_update_last_session_timer(u
);
159 int session_set_leader(Session
*s
, pid_t pid
) {
164 if (!pid_is_valid(pid
))
167 if (s
->leader
== pid
)
170 r
= hashmap_put(s
->manager
->sessions_by_leader
, PID_TO_PTR(pid
), s
);
174 if (pid_is_valid(s
->leader
))
175 (void) hashmap_remove_value(s
->manager
->sessions_by_leader
, PID_TO_PTR(s
->leader
), s
);
178 (void) audit_session_from_pid(pid
, &s
->audit_id
);
183 static void session_save_devices(Session
*s
, FILE *f
) {
187 if (!hashmap_isempty(s
->devices
)) {
188 fprintf(f
, "DEVICES=");
189 HASHMAP_FOREACH(sd
, s
->devices
, i
)
190 fprintf(f
, "%u:%u ", major(sd
->dev
), minor(sd
->dev
));
195 int session_save(Session
*s
) {
196 _cleanup_free_
char *temp_path
= NULL
;
197 _cleanup_fclose_
FILE *f
= NULL
;
208 r
= mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0, MKDIR_WARN_MODE
);
212 r
= fopen_temporary(s
->state_file
, &f
, &temp_path
);
216 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
217 (void) fchmod(fileno(f
), 0644);
220 "# This is private data. Do not parse.\n"
229 session_is_active(s
),
230 s
->user
->display
== s
,
231 session_state_to_string(session_get_state(s
)),
235 fprintf(f
, "TYPE=%s\n", session_type_to_string(s
->type
));
238 fprintf(f
, "CLASS=%s\n", session_class_to_string(s
->class));
241 fprintf(f
, "SCOPE=%s\n", s
->scope
);
243 fprintf(f
, "SCOPE_JOB=%s\n", s
->scope_job
);
246 fprintf(f
, "FIFO=%s\n", s
->fifo_path
);
249 fprintf(f
, "SEAT=%s\n", s
->seat
->id
);
252 fprintf(f
, "TTY=%s\n", s
->tty
);
254 if (s
->tty_validity
>= 0)
255 fprintf(f
, "TTY_VALIDITY=%s\n", tty_validity_to_string(s
->tty_validity
));
258 fprintf(f
, "DISPLAY=%s\n", s
->display
);
260 if (s
->remote_host
) {
261 _cleanup_free_
char *escaped
;
263 escaped
= cescape(s
->remote_host
);
269 fprintf(f
, "REMOTE_HOST=%s\n", escaped
);
272 if (s
->remote_user
) {
273 _cleanup_free_
char *escaped
;
275 escaped
= cescape(s
->remote_user
);
281 fprintf(f
, "REMOTE_USER=%s\n", escaped
);
285 _cleanup_free_
char *escaped
;
287 escaped
= cescape(s
->service
);
293 fprintf(f
, "SERVICE=%s\n", escaped
);
297 _cleanup_free_
char *escaped
;
299 escaped
= cescape(s
->desktop
);
305 fprintf(f
, "DESKTOP=%s\n", escaped
);
308 if (s
->seat
&& seat_has_vts(s
->seat
))
309 fprintf(f
, "VTNR=%u\n", s
->vtnr
);
312 fprintf(f
, "POSITION=%u\n", s
->position
);
314 if (pid_is_valid(s
->leader
))
315 fprintf(f
, "LEADER="PID_FMT
"\n", s
->leader
);
317 if (audit_session_is_valid(s
->audit_id
))
318 fprintf(f
, "AUDIT=%"PRIu32
"\n", s
->audit_id
);
320 if (dual_timestamp_is_set(&s
->timestamp
))
322 "REALTIME="USEC_FMT
"\n"
323 "MONOTONIC="USEC_FMT
"\n",
324 s
->timestamp
.realtime
,
325 s
->timestamp
.monotonic
);
328 fprintf(f
, "CONTROLLER=%s\n", s
->controller
);
329 session_save_devices(s
, f
);
332 r
= fflush_and_check(f
);
336 if (rename(temp_path
, s
->state_file
) < 0) {
344 (void) unlink(s
->state_file
);
347 (void) unlink(temp_path
);
349 return log_error_errno(r
, "Failed to save session data %s: %m", s
->state_file
);
352 static int session_load_devices(Session
*s
, const char *devices
) {
358 for (p
= devices
;;) {
359 _cleanup_free_
char *word
= NULL
;
364 k
= extract_first_word(&p
, &word
, NULL
, 0);
372 k
= parse_dev(word
, &dev
);
378 /* The file descriptors for loaded devices will be reattached later. */
379 k
= session_device_new(s
, dev
, false, &sd
);
385 log_error_errno(r
, "Loading session devices for session %s failed: %m", s
->id
);
390 int session_load(Session
*s
) {
391 _cleanup_free_
char *remote
= NULL
,
393 *tty_validity
= NULL
,
412 r
= parse_env_file(NULL
, s
->state_file
,
415 "SCOPE_JOB", &s
->scope_job
,
416 "FIFO", &s
->fifo_path
,
419 "TTY_VALIDITY", &tty_validity
,
420 "DISPLAY", &s
->display
,
421 "REMOTE_HOST", &s
->remote_host
,
422 "REMOTE_USER", &s
->remote_user
,
423 "SERVICE", &s
->service
,
424 "DESKTOP", &s
->desktop
,
427 "POSITION", &position
,
432 "REALTIME", &realtime
,
433 "MONOTONIC", &monotonic
,
434 "CONTROLLER", &controller
,
437 "IS_DISPLAY", &is_display
);
440 return log_error_errno(r
, "Failed to read %s: %m", s
->state_file
);
447 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
448 "UID not specified for session %s",
451 r
= parse_uid(uid
, &u
);
453 log_error("Failed to parse UID value %s for session %s.", uid
, s
->id
);
457 user
= hashmap_get(s
->manager
->users
, UID_TO_PTR(u
));
459 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
460 "User of session %s not known.",
463 session_set_user(s
, user
);
467 k
= parse_boolean(remote
);
473 safe_atou(vtnr
, &s
->vtnr
);
475 if (seat
&& !s
->seat
) {
478 o
= hashmap_get(s
->manager
->seats
, seat
);
480 r
= seat_attach_session(o
, s
);
482 log_error("Cannot attach session %s to seat %s", s
->id
, seat
);
485 if (!s
->seat
|| !seat_has_vts(s
->seat
))
488 if (position
&& s
->seat
) {
491 safe_atou(position
, &npos
);
492 seat_claim_position(s
->seat
, s
, npos
);
498 v
= tty_validity_from_string(tty_validity
);
500 log_debug("Failed to parse TTY validity: %s", tty_validity
);
508 r
= parse_pid(leader
, &pid
);
510 log_debug_errno(r
, "Failed to parse leader PID of session: %s", leader
);
512 r
= session_set_leader(s
, pid
);
514 log_warning_errno(r
, "Failed to set session leader PID, ignoring: %m");
521 t
= session_type_from_string(type
);
529 c
= session_class_from_string(class);
534 if (state
&& streq(state
, "closing"))
540 /* If we open an unopened pipe for reading we will not
541 get an EOF. to trigger an EOF we hence open it for
542 writing, but close it right away which then will
543 trigger the EOF. This will happen immediately if no
544 other process has the FIFO open for writing, i. e.
545 when the session died before logind (re)started. */
547 fd
= session_create_fifo(s
);
552 (void) deserialize_usec(realtime
, &s
->timestamp
.realtime
);
554 (void) deserialize_usec(monotonic
, &s
->timestamp
.monotonic
);
557 k
= parse_boolean(active
);
563 /* Note that when enumerating users are loaded before sessions, hence the display session to use is
564 * something we have to store along with the session and not the user, as in that case we couldn't
565 * apply it at the time we load the user. */
567 k
= parse_boolean(is_display
);
569 log_warning_errno(k
, "Failed to parse IS_DISPLAY session property: %m");
571 s
->user
->display
= s
;
575 if (bus_name_has_owner(s
->manager
->bus
, controller
, NULL
) > 0) {
576 session_set_controller(s
, controller
, false, false);
577 session_load_devices(s
, devices
);
579 session_restore_vt(s
);
585 int session_activate(Session
*s
) {
586 unsigned num_pending
;
594 if (s
->seat
->active
== s
)
597 /* on seats with VTs, we let VTs manage session-switching */
598 if (seat_has_vts(s
->seat
)) {
602 return chvt(s
->vtnr
);
605 /* On seats without VTs, we implement session-switching in logind. We
606 * try to pause all session-devices and wait until the session
607 * controller acknowledged them. Once all devices are asleep, we simply
608 * switch the active session and be done.
609 * We save the session we want to switch to in seat->pending_switch and
610 * seat_complete_switch() will perform the final switch. */
612 s
->seat
->pending_switch
= s
;
614 /* if no devices are running, immediately perform the session switch */
615 num_pending
= session_device_try_pause_all(s
);
617 seat_complete_switch(s
->seat
);
622 static int session_start_scope(Session
*s
, sd_bus_message
*properties
, sd_bus_error
*error
) {
629 _cleanup_free_
char *scope
= NULL
;
630 const char *description
;
632 s
->scope_job
= mfree(s
->scope_job
);
634 scope
= strjoin("session-", s
->id
, ".scope");
638 description
= strjoina("Session ", s
->id
, " of user ", s
->user
->name
);
640 r
= manager_start_scope(
646 STRV_MAKE(s
->user
->runtime_dir_service
, s
->user
->service
), /* These two have StopWhenUnneeded= set, hence add a dep towards them */
647 STRV_MAKE("systemd-logind.service", "systemd-user-sessions.service", s
->user
->runtime_dir_service
, s
->user
->service
), /* And order us after some more */
653 return log_error_errno(r
, "Failed to start session scope %s: %s", scope
, bus_error_message(error
, r
));
655 s
->scope
= TAKE_PTR(scope
);
659 (void) hashmap_put(s
->manager
->session_units
, s
->scope
, s
);
664 int session_start(Session
*s
, sd_bus_message
*properties
, sd_bus_error
*error
) {
678 r
= user_start(s
->user
);
682 r
= session_start_scope(s
, properties
, error
);
686 log_struct(s
->class == SESSION_BACKGROUND
? LOG_DEBUG
: LOG_INFO
,
687 "MESSAGE_ID=" SD_MESSAGE_SESSION_START_STR
,
688 "SESSION_ID=%s", s
->id
,
689 "USER_ID=%s", s
->user
->name
,
690 "LEADER="PID_FMT
, s
->leader
,
691 LOG_MESSAGE("New session %s of user %s.", s
->id
, s
->user
->name
));
693 if (!dual_timestamp_is_set(&s
->timestamp
))
694 dual_timestamp_get(&s
->timestamp
);
697 seat_read_active_vt(s
->seat
);
701 user_elect_display(s
->user
);
710 session_send_signal(s
, true);
711 user_send_changed(s
->user
, "Display", NULL
);
713 if (s
->seat
->active
== s
)
714 seat_send_changed(s
->seat
, "ActiveSession", NULL
);
720 static int session_stop_scope(Session
*s
, bool force
) {
721 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
729 /* Let's always abandon the scope first. This tells systemd that we are not interested anymore, and everything
730 * that is left in the scope is "left-over". Informing systemd about this has the benefit that it will log
731 * when killing any processes left after this point. */
732 r
= manager_abandon_scope(s
->manager
, s
->scope
, &error
);
734 log_warning_errno(r
, "Failed to abandon session scope, ignoring: %s", bus_error_message(&error
, r
));
735 sd_bus_error_free(&error
);
738 s
->scope_job
= mfree(s
->scope_job
);
740 /* Optionally, let's kill everything that's left now. */
741 if (force
|| manager_shall_kill(s
->manager
, s
->user
->name
)) {
743 r
= manager_stop_unit(s
->manager
, s
->scope
, &error
, &s
->scope_job
);
746 return log_error_errno(r
, "Failed to stop session scope: %s", bus_error_message(&error
, r
));
748 log_warning_errno(r
, "Failed to stop session scope, ignoring: %s", bus_error_message(&error
, r
));
752 /* With no killing, this session is allowed to persist in "closing" state indefinitely.
753 * Therefore session stop and session removal may be two distinct events.
754 * Session stop is quite significant on its own, let's log it. */
755 log_struct(s
->class == SESSION_BACKGROUND
? LOG_DEBUG
: LOG_INFO
,
756 "SESSION_ID=%s", s
->id
,
757 "USER_ID=%s", s
->user
->name
,
758 "LEADER="PID_FMT
, s
->leader
,
759 LOG_MESSAGE("Session %s logged out. Waiting for processes to exit.", s
->id
));
765 int session_stop(Session
*s
, bool force
) {
770 /* This is called whenever we begin with tearing down a session record. It's called in four cases: explicit API
771 * request via the bus (either directly for the session object or for the seat or user object this session
772 * belongs to; 'force' is true), or due to automatic GC (i.e. scope vanished; 'force' is false), or because the
773 * session FIFO saw an EOF ('force' is false), or because the release timer hit ('force' is false). */
782 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
785 seat_evict_position(s
->seat
, s
);
787 /* We are going down, don't care about FIFOs anymore */
788 session_remove_fifo(s
);
791 r
= session_stop_scope(s
, force
);
795 user_elect_display(s
->user
);
803 int session_finalize(Session
*s
) {
812 log_struct(s
->class == SESSION_BACKGROUND
? LOG_DEBUG
: LOG_INFO
,
813 "MESSAGE_ID=" SD_MESSAGE_SESSION_STOP_STR
,
814 "SESSION_ID=%s", s
->id
,
815 "USER_ID=%s", s
->user
->name
,
816 "LEADER="PID_FMT
, s
->leader
,
817 LOG_MESSAGE("Removed session %s.", s
->id
));
819 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
822 seat_evict_position(s
->seat
, s
);
824 /* Kill session devices */
825 while ((sd
= hashmap_first(s
->devices
)))
826 session_device_free(sd
);
828 (void) unlink(s
->state_file
);
829 session_add_to_gc_queue(s
);
830 user_add_to_gc_queue(s
->user
);
833 session_send_signal(s
, false);
838 if (s
->seat
->active
== s
)
839 seat_set_active(s
->seat
, NULL
);
845 user_send_changed(s
->user
, "Display", NULL
);
850 static int release_timeout_callback(sd_event_source
*es
, uint64_t usec
, void *userdata
) {
851 Session
*s
= userdata
;
856 session_stop(s
, false);
860 int session_release(Session
*s
) {
863 if (!s
->started
|| s
->stopping
)
866 if (s
->timer_event_source
)
869 return sd_event_add_time(s
->manager
->event
,
870 &s
->timer_event_source
,
872 usec_add(now(CLOCK_MONOTONIC
), RELEASE_USEC
), 0,
873 release_timeout_callback
, s
);
876 bool session_is_active(Session
*s
) {
882 return s
->seat
->active
== s
;
885 static int get_tty_atime(const char *tty
, usec_t
*atime
) {
886 _cleanup_free_
char *p
= NULL
;
892 if (!path_is_absolute(tty
)) {
893 p
= strappend("/dev/", tty
);
898 } else if (!path_startswith(tty
, "/dev/"))
901 if (lstat(tty
, &st
) < 0)
904 *atime
= timespec_load(&st
.st_atim
);
908 static int get_process_ctty_atime(pid_t pid
, usec_t
*atime
) {
909 _cleanup_free_
char *p
= NULL
;
915 r
= get_ctty(pid
, NULL
, &p
);
919 return get_tty_atime(p
, atime
);
922 int session_get_idle_hint(Session
*s
, dual_timestamp
*t
) {
928 /* Explicit idle hint is set */
931 *t
= s
->idle_hint_timestamp
;
936 /* Graphical sessions should really implement a real
938 if (SESSION_TYPE_IS_GRAPHICAL(s
->type
))
941 /* For sessions with an explicitly configured tty, let's check
944 r
= get_tty_atime(s
->tty
, &atime
);
949 /* For sessions with a leader but no explicitly configured
950 * tty, let's check the controlling tty of the leader */
951 if (pid_is_valid(s
->leader
)) {
952 r
= get_process_ctty_atime(s
->leader
, &atime
);
959 *t
= s
->idle_hint_timestamp
;
965 dual_timestamp_from_realtime(t
, atime
);
967 n
= now(CLOCK_REALTIME
);
969 if (s
->manager
->idle_action_usec
<= 0)
972 return atime
+ s
->manager
->idle_action_usec
<= n
;
975 void session_set_idle_hint(Session
*s
, bool b
) {
978 if (s
->idle_hint
== b
)
982 dual_timestamp_get(&s
->idle_hint_timestamp
);
984 session_send_changed(s
, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic", NULL
);
987 seat_send_changed(s
->seat
, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic", NULL
);
989 user_send_changed(s
->user
, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic", NULL
);
990 manager_send_changed(s
->manager
, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic", NULL
);
993 int session_get_locked_hint(Session
*s
) {
996 return s
->locked_hint
;
999 void session_set_locked_hint(Session
*s
, bool b
) {
1002 if (s
->locked_hint
== b
)
1007 session_send_changed(s
, "LockedHint", NULL
);
1010 static int session_dispatch_fifo(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
1011 Session
*s
= userdata
;
1014 assert(s
->fifo_fd
== fd
);
1016 /* EOF on the FIFO means the session died abnormally. */
1018 session_remove_fifo(s
);
1019 session_stop(s
, false);
1024 int session_create_fifo(Session
*s
) {
1030 if (!s
->fifo_path
) {
1031 r
= mkdir_safe_label("/run/systemd/sessions", 0755, 0, 0, MKDIR_WARN_MODE
);
1035 s
->fifo_path
= strjoin("/run/systemd/sessions/", s
->id
, ".ref");
1039 if (mkfifo(s
->fifo_path
, 0600) < 0 && errno
!= EEXIST
)
1043 /* Open reading side */
1044 if (s
->fifo_fd
< 0) {
1045 s
->fifo_fd
= open(s
->fifo_path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1050 if (!s
->fifo_event_source
) {
1051 r
= sd_event_add_io(s
->manager
->event
, &s
->fifo_event_source
, s
->fifo_fd
, 0, session_dispatch_fifo
, s
);
1055 /* Let's make sure we noticed dead sessions before we process new bus requests (which might create new
1057 r
= sd_event_source_set_priority(s
->fifo_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1062 /* Open writing side */
1063 r
= open(s
->fifo_path
, O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
);
1070 static void session_remove_fifo(Session
*s
) {
1073 s
->fifo_event_source
= sd_event_source_unref(s
->fifo_event_source
);
1074 s
->fifo_fd
= safe_close(s
->fifo_fd
);
1077 (void) unlink(s
->fifo_path
);
1078 s
->fifo_path
= mfree(s
->fifo_path
);
1082 bool session_may_gc(Session
*s
, bool drop_not_started
) {
1087 if (drop_not_started
&& !s
->started
)
1093 if (s
->fifo_fd
>= 0) {
1094 if (pipe_eof(s
->fifo_fd
) <= 0)
1099 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1101 r
= manager_job_is_active(s
->manager
, s
->scope_job
, &error
);
1103 log_debug_errno(r
, "Failed to determine whether job '%s' is pending, ignoring: %s", s
->scope_job
, bus_error_message(&error
, r
));
1109 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1111 r
= manager_unit_is_active(s
->manager
, s
->scope
, &error
);
1113 log_debug_errno(r
, "Failed to determine whether unit '%s' is active, ignoring: %s", s
->scope
, bus_error_message(&error
, r
));
1121 void session_add_to_gc_queue(Session
*s
) {
1127 LIST_PREPEND(gc_queue
, s
->manager
->session_gc_queue
, s
);
1128 s
->in_gc_queue
= true;
1131 SessionState
session_get_state(Session
*s
) {
1134 /* always check closing first */
1135 if (s
->stopping
|| s
->timer_event_source
)
1136 return SESSION_CLOSING
;
1138 if (s
->scope_job
|| s
->fifo_fd
< 0)
1139 return SESSION_OPENING
;
1141 if (session_is_active(s
))
1142 return SESSION_ACTIVE
;
1144 return SESSION_ONLINE
;
1147 int session_kill(Session
*s
, KillWho who
, int signo
) {
1153 return manager_kill_unit(s
->manager
, s
->scope
, who
, signo
, NULL
);
1156 static int session_open_vt(Session
*s
) {
1157 char path
[sizeof("/dev/tty") + DECIMAL_STR_MAX(s
->vtnr
)];
1165 sprintf(path
, "/dev/tty%u", s
->vtnr
);
1166 s
->vtfd
= open_terminal(path
, O_RDWR
| O_CLOEXEC
| O_NONBLOCK
| O_NOCTTY
);
1168 return log_error_errno(s
->vtfd
, "cannot open VT %s of session %s: %m", path
, s
->id
);
1173 int session_prepare_vt(Session
*s
) {
1175 struct vt_mode mode
= { 0 };
1180 vt
= session_open_vt(s
);
1184 r
= fchown(vt
, s
->user
->uid
, -1);
1186 r
= log_error_errno(errno
,
1187 "Cannot change owner of /dev/tty%u: %m",
1192 r
= ioctl(vt
, KDSKBMODE
, K_OFF
);
1194 r
= log_error_errno(errno
,
1195 "Cannot set K_OFF on /dev/tty%u: %m",
1200 r
= ioctl(vt
, KDSETMODE
, KD_GRAPHICS
);
1202 r
= log_error_errno(errno
,
1203 "Cannot set KD_GRAPHICS on /dev/tty%u: %m",
1208 /* Oh, thanks to the VT layer, VT_AUTO does not work with KD_GRAPHICS.
1209 * So we need a dummy handler here which just acknowledges *all* VT
1210 * switch requests. */
1211 mode
.mode
= VT_PROCESS
;
1212 mode
.relsig
= SIGRTMIN
;
1213 mode
.acqsig
= SIGRTMIN
+ 1;
1214 r
= ioctl(vt
, VT_SETMODE
, &mode
);
1216 r
= log_error_errno(errno
,
1217 "Cannot set VT_PROCESS on /dev/tty%u: %m",
1225 session_restore_vt(s
);
1229 static void session_restore_vt(Session
*s
) {
1232 /* We need to get a fresh handle to the virtual terminal,
1233 * since the old file-descriptor is potentially in a hung-up
1234 * state after the controlling process exited; we do a
1235 * little dance to avoid having the terminal be available
1236 * for reuse before we've cleaned it up.
1238 old_fd
= TAKE_FD(s
->vtfd
);
1240 vt
= session_open_vt(s
);
1248 log_warning_errno(r
, "Failed to restore VT, ignoring: %m");
1250 s
->vtfd
= safe_close(s
->vtfd
);
1253 void session_leave_vt(Session
*s
) {
1258 /* This is called whenever we get a VT-switch signal from the kernel.
1259 * We acknowledge all of them unconditionally. Note that session are
1260 * free to overwrite those handlers and we only register them for
1261 * sessions with controllers. Legacy sessions are not affected.
1262 * However, if we switch from a non-legacy to a legacy session, we must
1263 * make sure to pause all device before acknowledging the switch. We
1264 * process the real switch only after we are notified via sysfs, so the
1265 * legacy session might have already started using the devices. If we
1266 * don't pause the devices before the switch, we might confuse the
1267 * session we switch to. */
1272 session_device_pause_all(s
);
1273 r
= vt_release(s
->vtfd
, false);
1275 log_debug_errno(r
, "Cannot release VT of session %s: %m", s
->id
);
1278 bool session_is_controller(Session
*s
, const char *sender
) {
1281 return streq_ptr(s
->controller
, sender
);
1284 static void session_release_controller(Session
*s
, bool notify
) {
1285 _cleanup_free_
char *name
= NULL
;
1291 name
= s
->controller
;
1293 /* By resetting the controller before releasing the devices, we won't
1294 * send notification signals. This avoids sending useless notifications
1295 * if the controller is released on disconnects. */
1297 s
->controller
= NULL
;
1299 while ((sd
= hashmap_first(s
->devices
)))
1300 session_device_free(sd
);
1302 s
->controller
= NULL
;
1303 s
->track
= sd_bus_track_unref(s
->track
);
1306 static int on_bus_track(sd_bus_track
*track
, void *userdata
) {
1307 Session
*s
= userdata
;
1312 session_drop_controller(s
);
1317 int session_set_controller(Session
*s
, const char *sender
, bool force
, bool prepare
) {
1318 _cleanup_free_
char *name
= NULL
;
1324 if (session_is_controller(s
, sender
))
1326 if (s
->controller
&& !force
)
1329 name
= strdup(sender
);
1333 s
->track
= sd_bus_track_unref(s
->track
);
1334 r
= sd_bus_track_new(s
->manager
->bus
, &s
->track
, on_bus_track
, s
);
1338 r
= sd_bus_track_add_name(s
->track
, name
);
1342 /* When setting a session controller, we forcibly mute the VT and set
1343 * it into graphics-mode. Applications can override that by changing
1344 * VT state after calling TakeControl(). However, this serves as a good
1345 * default and well-behaving controllers can now ignore VTs entirely.
1346 * Note that we reset the VT on ReleaseControl() and if the controller
1348 * If logind crashes/restarts, we restore the controller during restart
1349 * (without preparing the VT since the controller has probably overridden
1350 * VT state by now) or reset the VT in case it crashed/exited, too. */
1352 r
= session_prepare_vt(s
);
1354 s
->track
= sd_bus_track_unref(s
->track
);
1359 session_release_controller(s
, true);
1360 s
->controller
= TAKE_PTR(name
);
1366 void session_drop_controller(Session
*s
) {
1372 s
->track
= sd_bus_track_unref(s
->track
);
1373 session_release_controller(s
, false);
1375 session_restore_vt(s
);
1378 static const char* const session_state_table
[_SESSION_STATE_MAX
] = {
1379 [SESSION_OPENING
] = "opening",
1380 [SESSION_ONLINE
] = "online",
1381 [SESSION_ACTIVE
] = "active",
1382 [SESSION_CLOSING
] = "closing"
1385 DEFINE_STRING_TABLE_LOOKUP(session_state
, SessionState
);
1387 static const char* const session_type_table
[_SESSION_TYPE_MAX
] = {
1388 [SESSION_UNSPECIFIED
] = "unspecified",
1389 [SESSION_TTY
] = "tty",
1390 [SESSION_X11
] = "x11",
1391 [SESSION_WAYLAND
] = "wayland",
1392 [SESSION_MIR
] = "mir",
1393 [SESSION_WEB
] = "web",
1396 DEFINE_STRING_TABLE_LOOKUP(session_type
, SessionType
);
1398 static const char* const session_class_table
[_SESSION_CLASS_MAX
] = {
1399 [SESSION_USER
] = "user",
1400 [SESSION_GREETER
] = "greeter",
1401 [SESSION_LOCK_SCREEN
] = "lock-screen",
1402 [SESSION_BACKGROUND
] = "background"
1405 DEFINE_STRING_TABLE_LOOKUP(session_class
, SessionClass
);
1407 static const char* const kill_who_table
[_KILL_WHO_MAX
] = {
1408 [KILL_LEADER
] = "leader",
1412 DEFINE_STRING_TABLE_LOOKUP(kill_who
, KillWho
);
1414 static const char* const tty_validity_table
[_TTY_VALIDITY_MAX
] = {
1415 [TTY_FROM_PAM
] = "from-pam",
1416 [TTY_FROM_UTMP
] = "from-utmp",
1417 [TTY_UTMP_INCONSISTENT
] = "utmp-inconsistent",
1420 DEFINE_STRING_TABLE_LOOKUP(tty_validity
, TTYValidity
);