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 "string-util.h"
11 
12 typedef enum SessionState {
13         SESSION_OPENING,  /* Session scope is being created */
14         SESSION_ONLINE,   /* Logged in */
15         SESSION_ACTIVE,   /* Logged in and in the fg */
16         SESSION_CLOSING,  /* Logged out, but scope is still there */
17         _SESSION_STATE_MAX,
18         _SESSION_STATE_INVALID = -EINVAL,
19 } SessionState;
20 
21 typedef enum SessionClass {
22         SESSION_USER,
23         SESSION_GREETER,
24         SESSION_LOCK_SCREEN,
25         SESSION_BACKGROUND,
26         _SESSION_CLASS_MAX,
27         _SESSION_CLASS_INVALID = -EINVAL,
28 } SessionClass;
29 
30 typedef enum SessionType {
31         SESSION_UNSPECIFIED,
32         SESSION_TTY,
33         SESSION_X11,
34         SESSION_WAYLAND,
35         SESSION_MIR,
36         SESSION_WEB,
37         _SESSION_TYPE_MAX,
38         _SESSION_TYPE_INVALID = -EINVAL,
39 } SessionType;
40 
41 #define SESSION_TYPE_IS_GRAPHICAL(type) IN_SET(type, SESSION_X11, SESSION_WAYLAND, SESSION_MIR)
42 
43 enum KillWho {
44         KILL_LEADER,
45         KILL_ALL,
46         _KILL_WHO_MAX,
47         _KILL_WHO_INVALID = -EINVAL,
48 };
49 
50 typedef enum TTYValidity {
51         TTY_FROM_PAM,
52         TTY_FROM_UTMP,
53         TTY_UTMP_INCONSISTENT, /* may happen on ssh sessions with multiplexed TTYs */
54         _TTY_VALIDITY_MAX,
55         _TTY_VALIDITY_INVALID = -EINVAL,
56 } TTYValidity;
57 
58 struct Session {
59         Manager *manager;
60 
61         const char *id;
62         unsigned position;
63         SessionType type;
64         SessionType original_type;
65         SessionClass class;
66 
67         char *state_file;
68 
69         User *user;
70 
71         dual_timestamp timestamp;
72 
73         char *display;
74         char *tty;
75         TTYValidity tty_validity;
76 
77         bool remote;
78         char *remote_user;
79         char *remote_host;
80         char *service;
81         char *desktop;
82 
83         char *scope;
84         char *scope_job;
85 
86         Seat *seat;
87         unsigned vtnr;
88         int vtfd;
89 
90         pid_t leader;
91         uint32_t audit_id;
92 
93         int fifo_fd;
94         char *fifo_path;
95 
96         sd_event_source *fifo_event_source;
97 
98         bool idle_hint;
99         dual_timestamp idle_hint_timestamp;
100 
101         bool locked_hint;
102 
103         bool in_gc_queue:1;
104         bool started:1;
105         bool stopping:1;
106 
107         bool was_active:1;
108 
109         sd_bus_message *create_message;
110 
111         /* Set up when a client requested to release the session via the bus */
112         sd_event_source *timer_event_source;
113 
114         char *controller;
115         Hashmap *devices;
116         sd_bus_track *track;
117 
118         LIST_FIELDS(Session, sessions_by_user);
119         LIST_FIELDS(Session, sessions_by_seat);
120 
121         LIST_FIELDS(Session, gc_queue);
122 };
123 
124 int session_new(Session **ret, Manager *m, const char *id);
125 Session* session_free(Session *s);
126 
127 DEFINE_TRIVIAL_CLEANUP_FUNC(Session *, session_free);
128 
129 void session_set_user(Session *s, User *u);
130 int session_set_leader(Session *s, pid_t pid);
131 bool session_may_gc(Session *s, bool drop_not_started);
132 void session_add_to_gc_queue(Session *s);
133 int session_activate(Session *s);
134 bool session_is_active(Session *s);
135 int session_get_idle_hint(Session *s, dual_timestamp *t);
136 int session_set_idle_hint(Session *s, bool b);
137 int session_get_locked_hint(Session *s);
138 void session_set_locked_hint(Session *s, bool b);
139 void session_set_type(Session *s, SessionType t);
140 int session_create_fifo(Session *s);
141 int session_start(Session *s, sd_bus_message *properties, sd_bus_error *error);
142 int session_stop(Session *s, bool force);
143 int session_finalize(Session *s);
144 int session_release(Session *s);
145 int session_save(Session *s);
146 int session_load(Session *s);
147 int session_kill(Session *s, KillWho who, int signo);
148 
149 SessionState session_get_state(Session *u);
150 
151 const char* session_state_to_string(SessionState t) _const_;
152 SessionState session_state_from_string(const char *s) _pure_;
153 
154 const char* session_type_to_string(SessionType t) _const_;
155 SessionType session_type_from_string(const char *s) _pure_;
156 
157 const char* session_class_to_string(SessionClass t) _const_;
158 SessionClass session_class_from_string(const char *s) _pure_;
159 
160 const char *kill_who_to_string(KillWho k) _const_;
161 KillWho kill_who_from_string(const char *s) _pure_;
162 
163 const char* tty_validity_to_string(TTYValidity t) _const_;
164 TTYValidity tty_validity_from_string(const char *s) _pure_;
165 
166 void session_leave_vt(Session *s);
167 
168 bool session_is_controller(Session *s, const char *sender);
169 int session_set_controller(Session *s, const char *sender, bool force, bool prepare);
170 void session_drop_controller(Session *s);
171 
SESSION_IS_SELF(const char * name)172 static inline bool SESSION_IS_SELF(const char *name) {
173         return isempty(name) || streq(name, "self");
174 }
175 
SESSION_IS_AUTO(const char * name)176 static inline bool SESSION_IS_AUTO(const char *name) {
177         return streq_ptr(name, "auto");
178 }
179