]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-session.h
logind: rework logic to decide whether lock + idle + display applies to a session
[thirdparty/systemd.git] / src / login / logind-session.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 typedef struct Session Session;
5 typedef enum KillWho KillWho;
6
7 #include "list.h"
8 #include "login-util.h"
9 #include "logind-user.h"
10 #include "pidref.h"
11 #include "string-util.h"
12
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 */
18 _SESSION_STATE_MAX,
19 _SESSION_STATE_INVALID = -EINVAL,
20 } SessionState;
21
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_GREETER, /* A login greeter pseudo-session */
26 SESSION_LOCK_SCREEN, /* A lock screen */
27 SESSION_BACKGROUND, /* Things like cron jobs, which are non-interactive */
28 SESSION_BACKGROUND_LIGHT, /* Like SESSION_BACKGROUND, but without the service manager */
29 SESSION_MANAGER, /* The service manager */
30 SESSION_MANAGER_EARLY, /* The service manager for root (which is allowed to run before systemd-user-sessions.service) */
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_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), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER, SESSION_LOCK_SCREEN, SESSION_BACKGROUND, SESSION_BACKGROUND_LIGHT)
41
42 /* Which session classes want their own per-user service manager? */
43 #define SESSION_CLASS_WANTS_SERVICE_MANAGER(class) IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER, SESSION_LOCK_SCREEN, SESSION_BACKGROUND)
44
45 /* Which session classes can pin our user tracking? */
46 #define SESSION_CLASS_PIN_USER(class) (!IN_SET((class), SESSION_MANAGER, SESSION_MANAGER_EARLY))
47
48 /* Which session classes decide whether system is idle? (should only cover sessions that have input, and are not idle screens themselves)*/
49 #define SESSION_CLASS_CAN_IDLE(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER))
50
51 /* Which session classes have a lock screen concept? */
52 #define SESSION_CLASS_CAN_LOCK(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY))
53
54 /* Which sessions are candidates to become "display" sessions */
55 #define SESSION_CLASS_CAN_DISPLAY(class) (IN_SET((class), SESSION_USER, SESSION_USER_EARLY, SESSION_GREETER))
56
57 typedef enum SessionType {
58 SESSION_UNSPECIFIED,
59 SESSION_TTY,
60 SESSION_X11,
61 SESSION_WAYLAND,
62 SESSION_MIR,
63 SESSION_WEB,
64 _SESSION_TYPE_MAX,
65 _SESSION_TYPE_INVALID = -EINVAL,
66 } SessionType;
67
68 #define SESSION_TYPE_IS_GRAPHICAL(type) IN_SET(type, SESSION_X11, SESSION_WAYLAND, SESSION_MIR)
69
70 enum KillWho {
71 KILL_LEADER,
72 KILL_ALL,
73 _KILL_WHO_MAX,
74 _KILL_WHO_INVALID = -EINVAL,
75 };
76
77 typedef enum TTYValidity {
78 TTY_FROM_PAM,
79 TTY_FROM_UTMP,
80 TTY_UTMP_INCONSISTENT, /* may happen on ssh sessions with multiplexed TTYs */
81 _TTY_VALIDITY_MAX,
82 _TTY_VALIDITY_INVALID = -EINVAL,
83 } TTYValidity;
84
85 struct Session {
86 Manager *manager;
87
88 const char *id;
89 unsigned position;
90 SessionType type;
91 SessionType original_type;
92 SessionClass class;
93
94 char *state_file;
95
96 User *user;
97
98 dual_timestamp timestamp;
99
100 char *display;
101 char *tty;
102 TTYValidity tty_validity;
103
104 bool remote;
105 char *remote_user;
106 char *remote_host;
107 char *service;
108 char *desktop;
109
110 char *scope;
111 char *scope_job;
112
113 Seat *seat;
114 unsigned vtnr;
115 int vtfd;
116
117 PidRef leader;
118 bool leader_fd_saved; /* pidfd of leader uploaded to fdstore */
119 pid_t deserialized_pid; /* PID deserialized from state file (for verification when pidfd is used) */
120 uint32_t audit_id;
121
122 int fifo_fd;
123 char *fifo_path;
124
125 sd_event_source *fifo_event_source;
126 sd_event_source *leader_pidfd_event_source;
127
128 bool idle_hint;
129 dual_timestamp idle_hint_timestamp;
130
131 bool locked_hint;
132
133 bool in_gc_queue:1;
134 bool started:1;
135 bool stopping:1;
136
137 bool was_active:1;
138
139 sd_bus_message *create_message;
140
141 /* Set up when a client requested to release the session via the bus */
142 sd_event_source *timer_event_source;
143
144 char *controller;
145 Hashmap *devices;
146 sd_bus_track *track;
147
148 sd_event_source *stop_on_idle_event_source;
149
150 LIST_FIELDS(Session, sessions_by_user);
151 LIST_FIELDS(Session, sessions_by_seat);
152
153 LIST_FIELDS(Session, gc_queue);
154 };
155
156 int session_new(Session **ret, Manager *m, const char *id);
157 Session* session_free(Session *s);
158
159 DEFINE_TRIVIAL_CLEANUP_FUNC(Session *, session_free);
160
161 void session_set_user(Session *s, User *u);
162 int session_set_leader_consume(Session *s, PidRef _leader);
163 bool session_may_gc(Session *s, bool drop_not_started);
164 void session_add_to_gc_queue(Session *s);
165 int session_activate(Session *s);
166 bool session_is_active(Session *s);
167 int session_get_idle_hint(Session *s, dual_timestamp *t);
168 int session_set_idle_hint(Session *s, bool b);
169 int session_get_locked_hint(Session *s);
170 int session_set_locked_hint(Session *s, bool b);
171 void session_set_type(Session *s, SessionType t);
172 int session_set_display(Session *s, const char *display);
173 int session_set_tty(Session *s, const char *tty);
174 int session_create_fifo(Session *s);
175 int session_start(Session *s, sd_bus_message *properties, sd_bus_error *error);
176 int session_stop(Session *s, bool force);
177 int session_finalize(Session *s);
178 int session_release(Session *s);
179 int session_save(Session *s);
180 int session_load(Session *s);
181 int session_kill(Session *s, KillWho who, int signo);
182
183 SessionState session_get_state(Session *u);
184
185 const char* session_state_to_string(SessionState t) _const_;
186 SessionState session_state_from_string(const char *s) _pure_;
187
188 const char* session_type_to_string(SessionType t) _const_;
189 SessionType session_type_from_string(const char *s) _pure_;
190
191 const char* session_class_to_string(SessionClass t) _const_;
192 SessionClass session_class_from_string(const char *s) _pure_;
193
194 const char *kill_who_to_string(KillWho k) _const_;
195 KillWho kill_who_from_string(const char *s) _pure_;
196
197 const char* tty_validity_to_string(TTYValidity t) _const_;
198 TTYValidity tty_validity_from_string(const char *s) _pure_;
199
200 void session_leave_vt(Session *s);
201
202 bool session_is_controller(Session *s, const char *sender);
203 int session_set_controller(Session *s, const char *sender, bool force, bool prepare);
204 void session_drop_controller(Session *s);
205
206 static inline bool SESSION_IS_SELF(const char *name) {
207 return isempty(name) || streq(name, "self");
208 }
209
210 static inline bool SESSION_IS_AUTO(const char *name) {
211 return streq_ptr(name, "auto");
212 }