]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-session.h
7dfbabf4813a2a6540d631a2587565d45a511c9b
[thirdparty/systemd.git] / src / login / logind-session.h
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
125 char *scope;
126 char *scope_job;
127
128 Seat *seat;
129 unsigned vtnr;
130 int vtfd;
131
132 PidRef leader;
133 bool leader_fd_saved; /* pidfd of leader uploaded to fdstore */
134 pid_t deserialized_pid; /* PID deserialized from state file (for verification when pidfd is used) */
135 uint32_t audit_id;
136
137 sd_event_source *leader_pidfd_event_source;
138
139 bool in_gc_queue;
140 bool started;
141 bool stopping;
142
143 bool was_active;
144
145 bool locked_hint;
146
147 bool idle_hint;
148 dual_timestamp idle_hint_timestamp;
149
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 */
152
153 sd_varlink *create_link; /* The Varlink connection used to create session, which we haven't responded to yet */
154
155 /* Set up when a client requested to release the session via the bus */
156 sd_event_source *timer_event_source;
157
158 char *controller;
159 Hashmap *devices;
160 sd_bus_track *track;
161
162 sd_event_source *stop_on_idle_event_source;
163
164 LIST_FIELDS(Session, sessions_by_user);
165 LIST_FIELDS(Session, sessions_by_seat);
166
167 LIST_FIELDS(Session, gc_queue);
168 } Session;
169
170 int session_new(Manager *m, const char *id, Session **ret);
171 Session* session_free(Session *s);
172
173 DEFINE_TRIVIAL_CLEANUP_FUNC(Session*, session_free);
174
175 void session_set_user(Session *s, User *u);
176 int session_set_leader_consume(Session *s, PidRef _leader);
177 bool session_may_gc(Session *s, bool drop_not_started);
178 void session_add_to_gc_queue(Session *s);
179 int session_activate(Session *s);
180 bool session_is_active(Session *s);
181 int session_get_idle_hint(Session *s, dual_timestamp *t);
182 int session_set_idle_hint(Session *s, bool b);
183 int session_get_locked_hint(Session *s);
184 int session_set_locked_hint(Session *s, bool b);
185 void session_set_type(Session *s, SessionType t);
186 void session_set_class(Session *s, SessionClass c);
187 int session_set_display(Session *s, const char *display);
188 int session_set_tty(Session *s, const char *tty);
189 int session_start(Session *s, sd_bus_message *properties, sd_bus_error *error);
190 int session_stop(Session *s, bool force);
191 int session_finalize(Session *s);
192 int session_release(Session *s);
193 int session_save(Session *s);
194 int session_load(Session *s);
195 int session_kill(Session *s, KillWhom whom, int signo, sd_bus_error *error);
196
197 SessionState session_get_state(Session *u);
198
199 const char* session_state_to_string(SessionState t) _const_;
200 SessionState session_state_from_string(const char *s) _pure_;
201
202 const char* session_type_to_string(SessionType t) _const_;
203 SessionType session_type_from_string(const char *s) _pure_;
204
205 const char* session_class_to_string(SessionClass t) _const_;
206 SessionClass session_class_from_string(const char *s) _pure_;
207
208 const char* kill_whom_to_string(KillWhom k) _const_;
209 KillWhom kill_whom_from_string(const char *s) _pure_;
210
211 const char* tty_validity_to_string(TTYValidity t) _const_;
212 TTYValidity tty_validity_from_string(const char *s) _pure_;
213
214 void session_leave_vt(Session *s);
215
216 bool session_is_controller(Session *s, const char *sender);
217 int session_set_controller(Session *s, const char *sender, bool force, bool prepare);
218 void session_drop_controller(Session *s);
219
220 bool session_job_pending(Session *s);
221
222 int session_send_create_reply(Session *s, const sd_bus_error *error);
223
224 bool session_is_self(const char *name);
225 bool session_is_auto(const char *name);