Debian Codegraph @

src/login/logind-session.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "list.h"
5 #include "logind-forward.h"
6 #include "pidref.h"
7 #include "time-util.h"
8
9 typedef enum SessionState {
10 SESSION_OPENING, /* Session scope is being created */
11 SESSION_ONLINE, /* Logged in */
12 SESSION_ACTIVE, /* Logged in and in the fg */
13 SESSION_CLOSING, /* Logged out, but scope is still there */
14 _SESSION_STATE_MAX,
15 _SESSION_STATE_INVALID = -EINVAL,
16 } SessionState;
17
18 typedef enum SessionClass {
19 SESSION_USER, /* A regular user session */
20 SESSION_USER_EARLY, /* A user session, that is not ordered after systemd-user-sessions.service (i.e. for root) */
21 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 */
22 SESSION_USER_LIGHT, /* Just like SESSION_USER, but doesn't pull in service manager */
23 SESSION_USER_EARLY_LIGHT, /* Just like SESSION_USER_EARLY, but doesn't pull in service manager */
24 SESSION_GREETER, /* A login greeter pseudo-session */
25 SESSION_LOCK_SCREEN, /* A lock screen */
26 SESSION_BACKGROUND, /* Things like cron jobs, which are non-interactive */
27 SESSION_BACKGROUND_LIGHT, /* Like SESSION_BACKGROUND, but without the service manager */
28 SESSION_MANAGER, /* The service manager */
29 SESSION_MANAGER_EARLY, /* The service manager for root (which is allowed to run before systemd-user-sessions.service) */
30 SESSION_NONE, /* A session not registered with logind */
31 _SESSION_CLASS_MAX,
32 _SESSION_CLASS_INVALID = -EINVAL,
33 } SessionClass;
34
35 /* Whether we shall allow sessions of this class to run before 'systemd-user-sessions.service'. It's
36 * generally set for root sessions, but no one else. */
37 #define SESSION_CLASS_IS_EARLY(class) IN_SET((class), SESSION_USER_EARLY, SESSION_USER_EARLY_LIGHT, SESSION_MANAGER_EARLY)
38
39 /* Which session classes want their own scope units? (all of them, except the manager, which comes in its own service unit already */
40 #define SESSION_CLASS_WANTS_SCOPE(class) IN_SET((class), \
41 SESSION_USER, SESSION_USER_EARLY, SESSION_USER_INCOMPLETE, SESSION_USER_LIGHT, SESSION_USER_EARLY_LIGHT, \
42 SESSION_GREETER, SESSION_LOCK_SCREEN, SESSION_BACKGROUND, SESSION_BACKGROUND_LIGHT)
43
44 /* Which session classes want their own per-user service manager? */
45 #define SESSION_CLASS_WANTS_SERVICE_MANAGER(class) IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER, SESSION_LOCK_SCREEN, SESSION_BACKGROUND)
46
47 /* Which session classes can pin our user tracking? */
48 #define SESSION_CLASS_PIN_USER(class) (!IN_SET((class), SESSION_MANAGER, SESSION_MANAGER_EARLY, SESSION_NONE))
49
50 /* Which session classes decide whether system is idle? (should only cover sessions that have input, and are not idle screens themselves) */
51 #define SESSION_CLASS_CAN_IDLE(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_USER_EARLY_LIGHT, SESSION_USER_LIGHT, SESSION_GREETER))
52
53 /* Which session classes have a lock screen concept? */
54 #define SESSION_CLASS_CAN_LOCK(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_USER_EARLY_LIGHT, SESSION_USER_LIGHT))
55
56 /* Which sessions are candidates to become "display" sessions */
57 #define SESSION_CLASS_CAN_DISPLAY(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_USER_EARLY_LIGHT, SESSION_USER_LIGHT, SESSION_GREETER))
58
59 /* Which sessions classes should be subject to stop-in-idle */
60 #define SESSION_CLASS_CAN_STOP_ON_IDLE(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_USER_LIGHT, SESSION_USER_EARLY_LIGHT))
61
62 /* Which session classes can take control of devices */
63 #define SESSION_CLASS_CAN_TAKE_DEVICE(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_USER_LIGHT, SESSION_USER_EARLY_LIGHT, SESSION_GREETER, SESSION_LOCK_SCREEN))
64
65 /* Which session classes allow changing session types */
66 #define SESSION_CLASS_CAN_CHANGE_TYPE(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_USER_LIGHT, SESSION_USER_EARLY_LIGHT, SESSION_GREETER, SESSION_LOCK_SCREEN))
67
68 /* Which session classes are taken into account when deciding whether shutdown shall be allowed if other users are logged in */
69 #define SESSION_CLASS_IS_INHIBITOR_LIKE(class) IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_USER_LIGHT, SESSION_USER_EARLY_LIGHT)
70
71 typedef enum SessionType {
72 SESSION_UNSPECIFIED,
73 SESSION_TTY,
74 SESSION_X11,
75 SESSION_WAYLAND,
76 SESSION_MIR,
77 SESSION_WEB,
78 _SESSION_TYPE_MAX,
79 _SESSION_TYPE_INVALID = -EINVAL,
80 } SessionType;
81
82 #define SESSION_TYPE_IS_GRAPHICAL(type) IN_SET(type, SESSION_X11, SESSION_WAYLAND, SESSION_MIR)
83
84 typedef enum KillWhom {
85 KILL_LEADER,
86 KILL_ALL,
87 _KILL_WHOM_MAX,
88 _KILL_WHOM_INVALID = -EINVAL,
89 } KillWhom;
90
91 typedef enum TTYValidity {
92 TTY_FROM_PAM,
93 TTY_FROM_UTMP,
94 TTY_UTMP_INCONSISTENT, /* may happen on ssh sessions with multiplexed TTYs */
95 _TTY_VALIDITY_MAX,
96 _TTY_VALIDITY_INVALID = -EINVAL,
97 } TTYValidity;
98
99 typedef struct Session {
100 Manager *manager;
101
102 char *id;
103
104 unsigned position;
105 SessionType type;
106 SessionType original_type;
107 SessionClass class;
108
109 char *state_file;
110
111 User *user;
112
113 dual_timestamp timestamp;
114
115 char *display;
116 char *tty;
117 TTYValidity tty_validity;
118
119 bool remote;
120 char *remote_user;
121 char *remote_host;
122 char *service;
123 char *desktop;
124 char **extra_device_access;
125
126 char *scope;
127 char *scope_job;
128
129 Seat *seat;
130 unsigned vtnr;
131 int vtfd;
132
133 PidRef leader;
134 bool leader_fd_saved; /* pidfd of leader uploaded to fdstore */
135 pid_t deserialized_pid; /* PID deserialized from state file (for verification when pidfd is used) */
136 uint32_t audit_id;
137
138 sd_event_source *leader_pidfd_event_source;
139
140 bool in_gc_queue;
141 bool started;
142 bool stopping;
143
144 bool was_active;
145
146 bool locked_hint;
147
148 bool idle_hint;
149 dual_timestamp idle_hint_timestamp;
150
151 sd_bus_message *create_message; /* The D-Bus message used to create the session, which we haven't responded to yet */
152 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
154 sd_varlink *create_link; /* The Varlink connection used to create session, which we haven't responded to yet */
155
156 /* Set up when a client requested to release the session via the bus */
157 sd_event_source *timer_event_source;
158
159 char *controller;
160 Hashmap *devices;
161 sd_bus_track *track;
162
163 sd_event_source *stop_on_idle_event_source;
164
165 LIST_FIELDS(Session, sessions_by_user);
166 LIST_FIELDS(Session, sessions_by_seat);
167
168 LIST_FIELDS(Session, gc_queue);
169 } Session;
170
171 int session_new(Manager *m, const char *id, Session **ret);
172 Session* session_free(Session *s);
173
174 DEFINE_TRIVIAL_CLEANUP_FUNC(Session*, session_free);
175
176 void session_set_user(Session *s, User *u);
177 int session_set_leader_consume(Session *s, PidRef _leader);
178 bool session_may_gc(Session *s, bool drop_not_started);
179 void session_add_to_gc_queue(Session *s);
180 int session_activate(Session *s);
181 bool session_is_active(Session *s);
182 bool session_get_idle_hint(Session *s, dual_timestamp *ret_timestamp);
183 int session_set_idle_hint(Session *s, bool b);
184 int session_get_locked_hint(Session *s);
185 int session_set_locked_hint(Session *s, bool b);
186 void session_set_type(Session *s, SessionType t);
187 void session_set_class(Session *s, SessionClass c);
188 int session_set_display(Session *s, const char *display);
189 int session_set_tty(Session *s, const char *tty);
190 int session_start(Session *s, sd_bus_message *properties, sd_bus_error *error);
191 int session_stop(Session *s, bool force);
192 int session_finalize(Session *s);
193 int session_release(Session *s);
194 int session_save(Session *s);
195 int session_load(Session *s);
196 int session_kill(Session *s, KillWhom whom, int signo, sd_bus_error *error);
197
198 SessionState session_get_state(Session *s);
199
200 DECLARE_STRING_TABLE_LOOKUP(session_state, SessionState);
201
202 DECLARE_STRING_TABLE_LOOKUP(session_type, SessionType);
203
204 DECLARE_STRING_TABLE_LOOKUP(session_class, SessionClass);
205
206 DECLARE_STRING_TABLE_LOOKUP(kill_whom, KillWhom);
207
208 DECLARE_STRING_TABLE_LOOKUP(tty_validity, TTYValidity);
209
210 void session_leave_vt(Session *s);
211
212 bool session_is_controller(Session *s, const char *sender);
213 int session_set_controller(Session *s, const char *sender, bool force, bool prepare);
214 void session_drop_controller(Session *s);
215
216 bool session_job_pending(Session *s);
217
218 int session_send_create_reply(Session *s, const sd_bus_error *error);
219
220 bool session_is_self(const char *name);
221 bool session_is_auto(const char *name);
222
223 int session_build_json(Session *s, sd_json_variant **ret);