1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 typedef struct Session Session
;
5 typedef enum KillWho KillWho
;
8 #include "login-util.h"
9 #include "logind-user.h"
11 #include "string-util.h"
13 typedef enum SessionState
{
14 SESSION_OPENING
, /* Session scope is being created */
15 SESSION_ONLINE
, /* Logged in */
16 SESSION_ACTIVE
, /* Logged in and in the fg */
17 SESSION_CLOSING
, /* Logged out, but scope is still there */
19 _SESSION_STATE_INVALID
= -EINVAL
,
22 typedef enum SessionClass
{
23 SESSION_USER
, /* A regular user session */
24 SESSION_USER_EARLY
, /* A user session, that is not ordered after systemd-user-sessions.service (i.e. for root) */
25 SESSION_USER_INCOMPLETE
, /* A user session that is only half-way set up and doesn't pull in the service manager, and can be upgraded to a full user session later */
26 SESSION_GREETER
, /* A login greeter pseudo-session */
27 SESSION_LOCK_SCREEN
, /* A lock screen */
28 SESSION_BACKGROUND
, /* Things like cron jobs, which are non-interactive */
29 SESSION_BACKGROUND_LIGHT
, /* Like SESSION_BACKGROUND, but without the service manager */
30 SESSION_MANAGER
, /* The service manager */
31 SESSION_MANAGER_EARLY
, /* The service manager for root (which is allowed to run before systemd-user-sessions.service) */
33 _SESSION_CLASS_INVALID
= -EINVAL
,
36 /* Whether we shall allow sessions of this class to run before 'systemd-user-sessions.service'. It's
37 * generally set for root sessions, but no one else. */
38 #define SESSION_CLASS_IS_EARLY(class) IN_SET((class), SESSION_USER_EARLY, SESSION_MANAGER_EARLY)
40 /* Which session classes want their own scope units? (all of them, except the manager, which comes in its own service unit already */
41 #define SESSION_CLASS_WANTS_SCOPE(class) IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_USER_INCOMPLETE, SESSION_GREETER, SESSION_LOCK_SCREEN, SESSION_BACKGROUND, SESSION_BACKGROUND_LIGHT)
43 /* Which session classes want their own per-user service manager? */
44 #define SESSION_CLASS_WANTS_SERVICE_MANAGER(class) IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER, SESSION_LOCK_SCREEN, SESSION_BACKGROUND)
46 /* Which session classes can pin our user tracking? */
47 #define SESSION_CLASS_PIN_USER(class) (!IN_SET((class), SESSION_MANAGER, SESSION_MANAGER_EARLY))
49 /* Which session classes decide whether system is idle? (should only cover sessions that have input, and are not idle screens themselves)*/
50 #define SESSION_CLASS_CAN_IDLE(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER))
52 /* Which session classes have a lock screen concept? */
53 #define SESSION_CLASS_CAN_LOCK(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY))
55 /* Which sessions are candidates to become "display" sessions */
56 #define SESSION_CLASS_CAN_DISPLAY(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER))
58 /* Which sessions classes should be subject to stop-in-idle */
59 #define SESSION_CLASS_CAN_STOP_ON_IDLE(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY))
61 /* Which session classes can take control of devices */
62 #define SESSION_CLASS_CAN_TAKE_DEVICE(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER, SESSION_LOCK_SCREEN))
64 /* Which session classes allow changing session types */
65 #define SESSION_CLASS_CAN_CHANGE_TYPE(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER, SESSION_LOCK_SCREEN))
67 typedef enum SessionType
{
75 _SESSION_TYPE_INVALID
= -EINVAL
,
78 #define SESSION_TYPE_IS_GRAPHICAL(type) IN_SET(type, SESSION_X11, SESSION_WAYLAND, SESSION_MIR)
84 _KILL_WHO_INVALID
= -EINVAL
,
87 typedef enum TTYValidity
{
90 TTY_UTMP_INCONSISTENT
, /* may happen on ssh sessions with multiplexed TTYs */
92 _TTY_VALIDITY_INVALID
= -EINVAL
,
102 SessionType original_type
;
109 dual_timestamp timestamp
;
113 TTYValidity tty_validity
;
129 bool leader_fd_saved
; /* pidfd of leader uploaded to fdstore */
130 pid_t deserialized_pid
; /* PID deserialized from state file (for verification when pidfd is used) */
136 sd_event_source
*fifo_event_source
;
137 sd_event_source
*leader_pidfd_event_source
;
148 dual_timestamp idle_hint_timestamp
;
150 sd_bus_message
*create_message
; /* The D-Bus message used to create the session, which we haven't responded to yet */
151 sd_bus_message
*upgrade_message
; /* The D-Bus message used to upgrade the session class user-incomplete → user, which we haven't responded to yet */
153 /* Set up when a client requested to release the session via the bus */
154 sd_event_source
*timer_event_source
;
160 sd_event_source
*stop_on_idle_event_source
;
162 LIST_FIELDS(Session
, sessions_by_user
);
163 LIST_FIELDS(Session
, sessions_by_seat
);
165 LIST_FIELDS(Session
, gc_queue
);
168 int session_new(Manager
*m
, const char *id
, Session
**ret
);
169 Session
* session_free(Session
*s
);
171 DEFINE_TRIVIAL_CLEANUP_FUNC(Session
*, session_free
);
173 void session_set_user(Session
*s
, User
*u
);
174 int session_set_leader_consume(Session
*s
, PidRef _leader
);
175 bool session_may_gc(Session
*s
, bool drop_not_started
);
176 void session_add_to_gc_queue(Session
*s
);
177 int session_activate(Session
*s
);
178 bool session_is_active(Session
*s
);
179 int session_get_idle_hint(Session
*s
, dual_timestamp
*t
);
180 int session_set_idle_hint(Session
*s
, bool b
);
181 int session_get_locked_hint(Session
*s
);
182 int session_set_locked_hint(Session
*s
, bool b
);
183 void session_set_type(Session
*s
, SessionType t
);
184 void session_set_class(Session
*s
, SessionClass c
);
185 int session_set_display(Session
*s
, const char *display
);
186 int session_set_tty(Session
*s
, const char *tty
);
187 int session_create_fifo(Session
*s
);
188 int session_start(Session
*s
, sd_bus_message
*properties
, sd_bus_error
*error
);
189 int session_stop(Session
*s
, bool force
);
190 int session_finalize(Session
*s
);
191 int session_release(Session
*s
);
192 int session_save(Session
*s
);
193 int session_load(Session
*s
);
194 int session_kill(Session
*s
, KillWho who
, int signo
);
196 SessionState
session_get_state(Session
*u
);
198 const char* session_state_to_string(SessionState t
) _const_
;
199 SessionState
session_state_from_string(const char *s
) _pure_
;
201 const char* session_type_to_string(SessionType t
) _const_
;
202 SessionType
session_type_from_string(const char *s
) _pure_
;
204 const char* session_class_to_string(SessionClass t
) _const_
;
205 SessionClass
session_class_from_string(const char *s
) _pure_
;
207 const char *kill_who_to_string(KillWho k
) _const_
;
208 KillWho
kill_who_from_string(const char *s
) _pure_
;
210 const char* tty_validity_to_string(TTYValidity t
) _const_
;
211 TTYValidity
tty_validity_from_string(const char *s
) _pure_
;
213 void session_leave_vt(Session
*s
);
215 bool session_is_controller(Session
*s
, const char *sender
);
216 int session_set_controller(Session
*s
, const char *sender
, bool force
, bool prepare
);
217 void session_drop_controller(Session
*s
);
219 static inline bool SESSION_IS_SELF(const char *name
) {
220 return isempty(name
) || streq(name
, "self");
223 static inline bool SESSION_IS_AUTO(const char *name
) {
224 return streq_ptr(name
, "auto");