chiark / gitweb /
a83e89411440e7ddbc51a3603a612e7782f4540a
[elogind.git] / src / login / logind.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2011 Lennart Poettering
8 ***/
9
10 #include <stdbool.h>
11
12 #if 0 /// elogind needs the systems udev header
13 #include "libudev.h"
14 #else
15 #include <libudev.h>
16 #endif // 0
17 #include "sd-bus.h"
18 #include "sd-event.h"
19
20 #include "hashmap.h"
21 #include "list.h"
22 #include "set.h"
23
24 typedef struct Manager Manager;
25
26 #include "logind-action.h"
27 #include "logind-button.h"
28 #include "logind-device.h"
29 #include "logind-inhibit.h"
30
31 /// Additional includes needed by elogind
32 #include "cgroup-util.h"
33
34 #if 1 /// elogind has to ident itself
35 #define MANAGER_IS_SYSTEM(m) ((m)->is_system)
36 #define MANAGER_IS_USER(m) (!((m)->is_system))
37 #endif // 1
38 struct Manager {
39         sd_event *event;
40         sd_bus *bus;
41
42         Hashmap *devices;
43         Hashmap *seats;
44         Hashmap *sessions;
45         Hashmap *users;
46         Hashmap *inhibitors;
47         Hashmap *buttons;
48
49         LIST_HEAD(Seat, seat_gc_queue);
50         LIST_HEAD(Session, session_gc_queue);
51         LIST_HEAD(User, user_gc_queue);
52
53         struct udev *udev;
54         struct udev_monitor *udev_seat_monitor, *udev_device_monitor, *udev_vcsa_monitor, *udev_button_monitor;
55
56         sd_event_source *console_active_event_source;
57         sd_event_source *udev_seat_event_source;
58         sd_event_source *udev_device_event_source;
59         sd_event_source *udev_vcsa_event_source;
60         sd_event_source *udev_button_event_source;
61
62 #if 0 /// elogind does not support autospawning of vts
63         int console_active_fd;
64
65         unsigned n_autovts;
66
67         unsigned reserve_vt;
68         int reserve_vt_fd;
69 #else
70         /* Make sure the user cannot accidentally unmount our cgroup
71          * file system */
72         int pin_cgroupfs_fd;
73
74         /* fd for handling cgroup socket if elogind is its own cgroups manager */
75         int cgroups_agent_fd;
76         sd_event_source *cgroups_agent_event_source;
77
78         /* Flags */
79         unsigned test_run_flags;
80         bool is_system:1; /* true if elogind is its own cgroups manager */
81
82         /* Data specific to the cgroup subsystem */
83         CGroupMask cgroup_supported;
84         char *cgroup_root;
85
86         int console_active_fd;
87 #endif // 0
88
89         Seat *seat0;
90
91         char **kill_only_users, **kill_exclude_users;
92         bool kill_user_processes;
93
94         unsigned long session_counter;
95         unsigned long inhibit_counter;
96
97         Hashmap *session_units;
98         Hashmap *user_units;
99
100         usec_t inhibit_delay_max;
101
102         /* If an action is currently being executed or is delayed,
103          * this is != 0 and encodes what is being done */
104         InhibitWhat action_what;
105
106 #if 0 /// elogind does all relevant actions on its own. No systemd jobs and units.
107         /* If a shutdown/suspend was delayed due to a inhibitor this
108            contains the unit name we are supposed to start after the
109            delay is over */
110         const char *action_unit;
111
112         /* If a shutdown/suspend is currently executed, then this is
113          * the job of it */
114         char *action_job;
115 #else
116         /* If a shutdown/suspend was delayed due to a inhibitor this
117            contains the action we are supposed to perform after the
118            delay is over */
119         HandleAction pending_action;
120
121         char **suspend_state,      **suspend_mode;
122         char **hibernate_state,    **hibernate_mode;
123         char **hybrid_sleep_state, **hybrid_sleep_mode;
124
125 #endif // 0
126         sd_event_source *inhibit_timeout_source;
127
128         char *scheduled_shutdown_type;
129         usec_t scheduled_shutdown_timeout;
130         sd_event_source *scheduled_shutdown_timeout_source;
131         uid_t scheduled_shutdown_uid;
132         char *scheduled_shutdown_tty;
133         sd_event_source *nologin_timeout_source;
134         bool unlink_nologin;
135
136         char *wall_message;
137         unsigned enable_wall_messages;
138         sd_event_source *wall_message_timeout_source;
139
140         bool shutdown_dry_run;
141
142         sd_event_source *idle_action_event_source;
143         usec_t idle_action_usec;
144         usec_t idle_action_not_before_usec;
145         HandleAction idle_action;
146
147         HandleAction handle_power_key;
148         HandleAction handle_suspend_key;
149         HandleAction handle_hibernate_key;
150         HandleAction handle_lid_switch;
151         HandleAction handle_lid_switch_ep;
152         HandleAction handle_lid_switch_docked;
153
154         bool power_key_ignore_inhibited;
155         bool suspend_key_ignore_inhibited;
156         bool hibernate_key_ignore_inhibited;
157         bool lid_switch_ignore_inhibited;
158
159         bool remove_ipc;
160
161         Hashmap *polkit_registry;
162
163         usec_t holdoff_timeout_usec;
164         sd_event_source *lid_switch_ignore_event_source;
165
166         size_t runtime_dir_size;
167         uint64_t user_tasks_max;
168         uint64_t sessions_max;
169         uint64_t inhibitors_max;
170 };
171
172 void manager_reset_config(Manager *m);
173 int manager_parse_config_file(Manager *m);
174
175 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device);
176 int manager_add_button(Manager *m, const char *name, Button **_button);
177 int manager_add_seat(Manager *m, const char *id, Seat **_seat);
178 int manager_add_session(Manager *m, const char *id, Session **_session);
179 int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user);
180 int manager_add_user_by_name(Manager *m, const char *name, User **_user);
181 int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user);
182 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor);
183
184 int manager_process_seat_device(Manager *m, struct udev_device *d);
185 int manager_process_button_device(Manager *m, struct udev_device *d);
186
187 #if 0 /// UNNEEDED by elogind
188 int manager_spawn_autovt(Manager *m, unsigned int vtnr);
189 #endif // 0
190
191 bool manager_shall_kill(Manager *m, const char *user);
192
193 int manager_get_idle_hint(Manager *m, dual_timestamp *t);
194
195 int manager_get_user_by_pid(Manager *m, pid_t pid, User **user);
196 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session);
197
198 bool manager_is_docked_or_external_displays(Manager *m);
199 bool manager_is_on_external_power(void);
200 bool manager_all_buttons_ignored(Manager *m);
201
202 extern const sd_bus_vtable manager_vtable[];
203
204 #if 0 /// UNNEEDED by elogind
205 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error);
206 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error);
207 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error);
208 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error);
209 int match_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error);
210
211 int bus_manager_shutdown_or_sleep_now_or_later(Manager *m, const char *unit_name, InhibitWhat w, sd_bus_error *error);
212 #else
213 /// eloginds own version does the action itself
214 int bus_manager_shutdown_or_sleep_now_or_later(Manager *m, HandleAction action, InhibitWhat w, sd_bus_error *error);
215 #endif // 0
216
217 int manager_send_changed(Manager *manager, const char *property, ...) _sentinel_;
218
219 #if 0 /// UNNEEDED by elogind
220 int manager_start_slice(Manager *manager, const char *slice, const char *description, const char *after, const char *after2, uint64_t tasks_max, sd_bus_error *error, char **job);
221 int manager_start_scope(Manager *manager, const char *scope, pid_t pid, const char *slice, const char *description, const char *after, const char *after2, sd_bus_message *more_properties, sd_bus_error *error, char **job);
222 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job);
223 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job);
224 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error);
225 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error);
226 int manager_unit_is_active(Manager *manager, const char *unit);
227 int manager_job_is_active(Manager *manager, const char *path);
228 #endif // 0
229
230 /* gperf lookup function */
231 const struct ConfigPerfItem* logind_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
232
233 int manager_set_lid_switch_ignore(Manager *m, usec_t until);
234
235 int config_parse_n_autovts(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
236 int config_parse_tmpfs_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
237
238 int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret);
239 int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret);
240 int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret);
241
242 int manager_setup_wall_message_timer(Manager *m);
243 bool logind_wall_tty_filter(const char *tty, void *userdata);
244
245 int manager_dispatch_delayed(Manager *manager, bool timeout);