]>
Commit | Line | Data |
---|---|---|
20263082 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2011 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
20263082 LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
20263082 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
20263082 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
20 | #include <errno.h> | |
20263082 LP |
21 | #include <fcntl.h> |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
20263082 | 24 | |
b4bbcaa9 | 25 | #include "libudev.h" |
cc377381 | 26 | #include "sd-daemon.h" |
07630cea | 27 | |
b5efdb8a | 28 | #include "alloc-util.h" |
cc377381 | 29 | #include "bus-error.h" |
07630cea LP |
30 | #include "bus-util.h" |
31 | #include "conf-parser.h" | |
a0f29c76 | 32 | #include "def.h" |
a0956174 | 33 | #include "dirent-util.h" |
3ffd4af2 | 34 | #include "fd-util.h" |
6482f626 | 35 | #include "formats-util.h" |
3ffd4af2 | 36 | #include "logind.h" |
4b51966c | 37 | #include "selinux-util.h" |
24882e06 | 38 | #include "signal-util.h" |
07630cea LP |
39 | #include "strv.h" |
40 | #include "udev-util.h" | |
f5058264 | 41 | #include "cgroup-util.h" |
20263082 | 42 | |
905f0a39 DM |
43 | static void manager_free(Manager *m); |
44 | ||
a2ed7077 | 45 | static void manager_reset_config(Manager *m) { |
20263082 | 46 | m->n_autovts = 6; |
98a77df5 | 47 | m->reserve_vt = 6; |
66cdd0f2 | 48 | m->remove_ipc = true; |
eecd1362 | 49 | m->inhibit_delay_max = 5 * USEC_PER_SEC; |
beaafb2e | 50 | m->handle_power_key = HANDLE_POWEROFF; |
8e7fd6ad LP |
51 | m->handle_suspend_key = HANDLE_SUSPEND; |
52 | m->handle_hibernate_key = HANDLE_HIBERNATE; | |
beaafb2e | 53 | m->handle_lid_switch = HANDLE_SUSPEND; |
3c56cab4 | 54 | m->handle_lid_switch_docked = HANDLE_IGNORE; |
a2ed7077 ZJS |
55 | m->power_key_ignore_inhibited = false; |
56 | m->suspend_key_ignore_inhibited = false; | |
57 | m->hibernate_key_ignore_inhibited = false; | |
beaafb2e | 58 | m->lid_switch_ignore_inhibited = true; |
a2ed7077 | 59 | |
9d10cbee | 60 | m->holdoff_timeout_usec = 30 * USEC_PER_SEC; |
20263082 | 61 | |
23406ce5 LP |
62 | m->idle_action_usec = 30 * USEC_PER_MINUTE; |
63 | m->idle_action = HANDLE_IGNORE; | |
23406ce5 | 64 | |
d8cf2ac7 | 65 | m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */ |
f5058264 | 66 | m->user_tasks_max = system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE, 100U); /* 33% */ |
64b56896 LP |
67 | m->sessions_max = 8192; |
68 | m->inhibitors_max = 8192; | |
1c231f56 | 69 | |
95365a57 | 70 | m->kill_user_processes = KILL_USER_PROCESSES; |
a2ed7077 ZJS |
71 | |
72 | m->kill_only_users = strv_free(m->kill_only_users); | |
73 | m->kill_exclude_users = strv_free(m->kill_exclude_users); | |
74 | } | |
75 | ||
76 | static Manager *manager_new(void) { | |
77 | Manager *m; | |
78 | int r; | |
79 | ||
80 | m = new0(Manager, 1); | |
81 | if (!m) | |
82 | return NULL; | |
83 | ||
84 | m->console_active_fd = -1; | |
85 | m->reserve_vt_fd = -1; | |
86 | ||
87 | m->idle_action_not_before_usec = now(CLOCK_MONOTONIC); | |
88 | ||
d5099efc MS |
89 | m->devices = hashmap_new(&string_hash_ops); |
90 | m->seats = hashmap_new(&string_hash_ops); | |
91 | m->sessions = hashmap_new(&string_hash_ops); | |
92 | m->users = hashmap_new(NULL); | |
93 | m->inhibitors = hashmap_new(&string_hash_ops); | |
94 | m->buttons = hashmap_new(&string_hash_ops); | |
f8e2fb7b | 95 | |
d5099efc MS |
96 | m->user_units = hashmap_new(&string_hash_ops); |
97 | m->session_units = hashmap_new(&string_hash_ops); | |
8c8c4351 | 98 | |
3cde9e8f | 99 | if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units) |
ed4ba7e4 | 100 | goto fail; |
25d93491 | 101 | |
20263082 | 102 | m->udev = udev_new(); |
ed4ba7e4 LP |
103 | if (!m->udev) |
104 | goto fail; | |
20263082 | 105 | |
afc6adb5 | 106 | r = sd_event_default(&m->event); |
ed4ba7e4 LP |
107 | if (r < 0) |
108 | goto fail; | |
cc377381 | 109 | |
cde93897 LP |
110 | sd_event_set_watchdog(m->event, true); |
111 | ||
a2ed7077 ZJS |
112 | manager_reset_config(m); |
113 | ||
20263082 | 114 | return m; |
ed4ba7e4 LP |
115 | |
116 | fail: | |
117 | manager_free(m); | |
118 | return NULL; | |
20263082 LP |
119 | } |
120 | ||
905f0a39 | 121 | static void manager_free(Manager *m) { |
20263082 LP |
122 | Session *session; |
123 | User *u; | |
124 | Device *d; | |
125 | Seat *s; | |
f8e2fb7b | 126 | Inhibitor *i; |
069cfc85 | 127 | Button *b; |
20263082 LP |
128 | |
129 | assert(m); | |
130 | ||
131 | while ((session = hashmap_first(m->sessions))) | |
132 | session_free(session); | |
133 | ||
134 | while ((u = hashmap_first(m->users))) | |
135 | user_free(u); | |
136 | ||
137 | while ((d = hashmap_first(m->devices))) | |
138 | device_free(d); | |
139 | ||
140 | while ((s = hashmap_first(m->seats))) | |
141 | seat_free(s); | |
142 | ||
f8e2fb7b LP |
143 | while ((i = hashmap_first(m->inhibitors))) |
144 | inhibitor_free(i); | |
145 | ||
069cfc85 LP |
146 | while ((b = hashmap_first(m->buttons))) |
147 | button_free(b); | |
148 | ||
20263082 LP |
149 | hashmap_free(m->devices); |
150 | hashmap_free(m->seats); | |
f8e2fb7b LP |
151 | hashmap_free(m->sessions); |
152 | hashmap_free(m->users); | |
153 | hashmap_free(m->inhibitors); | |
069cfc85 | 154 | hashmap_free(m->buttons); |
f8e2fb7b | 155 | |
fb6becb4 LP |
156 | hashmap_free(m->user_units); |
157 | hashmap_free(m->session_units); | |
8c8c4351 | 158 | |
cc377381 | 159 | sd_event_source_unref(m->idle_action_event_source); |
c0f32805 | 160 | sd_event_source_unref(m->inhibit_timeout_source); |
8aaa023a | 161 | sd_event_source_unref(m->scheduled_shutdown_timeout_source); |
867c37f6 | 162 | sd_event_source_unref(m->nologin_timeout_source); |
e2fa5721 | 163 | sd_event_source_unref(m->wall_message_timeout_source); |
cc377381 LP |
164 | |
165 | sd_event_source_unref(m->console_active_event_source); | |
166 | sd_event_source_unref(m->udev_seat_event_source); | |
167 | sd_event_source_unref(m->udev_device_event_source); | |
168 | sd_event_source_unref(m->udev_vcsa_event_source); | |
169 | sd_event_source_unref(m->udev_button_event_source); | |
f9cd6be1 | 170 | sd_event_source_unref(m->lid_switch_ignore_event_source); |
20263082 | 171 | |
03e334a1 | 172 | safe_close(m->console_active_fd); |
20263082 | 173 | |
3e044c49 ME |
174 | udev_monitor_unref(m->udev_seat_monitor); |
175 | udev_monitor_unref(m->udev_device_monitor); | |
176 | udev_monitor_unref(m->udev_vcsa_monitor); | |
177 | udev_monitor_unref(m->udev_button_monitor); | |
178 | ||
179 | udev_unref(m->udev); | |
20263082 | 180 | |
867c37f6 | 181 | if (m->unlink_nologin) |
a790812c | 182 | (void) unlink("/run/nologin"); |
867c37f6 | 183 | |
36e34057 | 184 | bus_verify_polkit_async_registry_free(m->polkit_registry); |
20263082 | 185 | |
cc377381 LP |
186 | sd_bus_unref(m->bus); |
187 | sd_event_unref(m->event); | |
20263082 | 188 | |
03e334a1 | 189 | safe_close(m->reserve_vt_fd); |
98a77df5 | 190 | |
193197e8 LP |
191 | strv_free(m->kill_only_users); |
192 | strv_free(m->kill_exclude_users); | |
193 | ||
8aaa023a | 194 | free(m->scheduled_shutdown_type); |
e2fa5721 DM |
195 | free(m->scheduled_shutdown_tty); |
196 | free(m->wall_message); | |
af9792ac | 197 | free(m->action_job); |
20263082 LP |
198 | free(m); |
199 | } | |
200 | ||
9588bc32 | 201 | static int manager_enumerate_devices(Manager *m) { |
20263082 | 202 | struct udev_list_entry *item = NULL, *first = NULL; |
bf5332d2 | 203 | _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL; |
14c3baca | 204 | int r; |
20263082 LP |
205 | |
206 | assert(m); | |
207 | ||
208 | /* Loads devices from udev and creates seats for them as | |
209 | * necessary */ | |
210 | ||
211 | e = udev_enumerate_new(m->udev); | |
bf5332d2 LP |
212 | if (!e) |
213 | return -ENOMEM; | |
20263082 | 214 | |
2d96536d | 215 | r = udev_enumerate_add_match_tag(e, "master-of-seat"); |
14c3baca | 216 | if (r < 0) |
bf5332d2 | 217 | return r; |
20263082 | 218 | |
e1202047 LP |
219 | r = udev_enumerate_add_match_is_initialized(e); |
220 | if (r < 0) | |
221 | return r; | |
222 | ||
14c3baca LP |
223 | r = udev_enumerate_scan_devices(e); |
224 | if (r < 0) | |
bf5332d2 | 225 | return r; |
20263082 LP |
226 | |
227 | first = udev_enumerate_get_list_entry(e); | |
228 | udev_list_entry_foreach(item, first) { | |
bf5332d2 | 229 | _cleanup_udev_device_unref_ struct udev_device *d = NULL; |
20263082 LP |
230 | int k; |
231 | ||
232 | d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item)); | |
bf5332d2 LP |
233 | if (!d) |
234 | return -ENOMEM; | |
20263082 | 235 | |
bf5332d2 | 236 | k = manager_process_seat_device(m, d); |
20263082 LP |
237 | if (k < 0) |
238 | r = k; | |
239 | } | |
240 | ||
20263082 LP |
241 | return r; |
242 | } | |
243 | ||
9588bc32 | 244 | static int manager_enumerate_buttons(Manager *m) { |
bf5332d2 | 245 | _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL; |
069cfc85 | 246 | struct udev_list_entry *item = NULL, *first = NULL; |
069cfc85 LP |
247 | int r; |
248 | ||
249 | assert(m); | |
250 | ||
251 | /* Loads buttons from udev */ | |
252 | ||
beaafb2e | 253 | if (m->handle_power_key == HANDLE_IGNORE && |
8e7fd6ad LP |
254 | m->handle_suspend_key == HANDLE_IGNORE && |
255 | m->handle_hibernate_key == HANDLE_IGNORE && | |
3c56cab4 BW |
256 | m->handle_lid_switch == HANDLE_IGNORE && |
257 | m->handle_lid_switch_docked == HANDLE_IGNORE) | |
6de0e0e5 LP |
258 | return 0; |
259 | ||
069cfc85 | 260 | e = udev_enumerate_new(m->udev); |
bf5332d2 LP |
261 | if (!e) |
262 | return -ENOMEM; | |
069cfc85 LP |
263 | |
264 | r = udev_enumerate_add_match_subsystem(e, "input"); | |
265 | if (r < 0) | |
bf5332d2 | 266 | return r; |
069cfc85 LP |
267 | |
268 | r = udev_enumerate_add_match_tag(e, "power-switch"); | |
269 | if (r < 0) | |
bf5332d2 | 270 | return r; |
069cfc85 | 271 | |
e1202047 LP |
272 | r = udev_enumerate_add_match_is_initialized(e); |
273 | if (r < 0) | |
274 | return r; | |
275 | ||
069cfc85 LP |
276 | r = udev_enumerate_scan_devices(e); |
277 | if (r < 0) | |
bf5332d2 | 278 | return r; |
069cfc85 LP |
279 | |
280 | first = udev_enumerate_get_list_entry(e); | |
281 | udev_list_entry_foreach(item, first) { | |
bf5332d2 | 282 | _cleanup_udev_device_unref_ struct udev_device *d = NULL; |
069cfc85 LP |
283 | int k; |
284 | ||
285 | d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item)); | |
bf5332d2 LP |
286 | if (!d) |
287 | return -ENOMEM; | |
069cfc85 | 288 | |
bf5332d2 | 289 | k = manager_process_button_device(m, d); |
069cfc85 LP |
290 | if (k < 0) |
291 | r = k; | |
292 | } | |
293 | ||
069cfc85 LP |
294 | return r; |
295 | } | |
296 | ||
9588bc32 | 297 | static int manager_enumerate_seats(Manager *m) { |
9444b1f2 | 298 | _cleanup_closedir_ DIR *d = NULL; |
20263082 LP |
299 | struct dirent *de; |
300 | int r = 0; | |
301 | ||
302 | assert(m); | |
303 | ||
304 | /* This loads data about seats stored on disk, but does not | |
305 | * actually create any seats. Removes data of seats that no | |
306 | * longer exist. */ | |
307 | ||
308 | d = opendir("/run/systemd/seats"); | |
309 | if (!d) { | |
310 | if (errno == ENOENT) | |
311 | return 0; | |
312 | ||
e1427b13 | 313 | return log_error_errno(errno, "Failed to open /run/systemd/seats: %m"); |
20263082 LP |
314 | } |
315 | ||
9444b1f2 | 316 | FOREACH_DIRENT(de, d, return -errno) { |
20263082 LP |
317 | Seat *s; |
318 | int k; | |
319 | ||
320 | if (!dirent_is_file(de)) | |
321 | continue; | |
322 | ||
323 | s = hashmap_get(m->seats, de->d_name); | |
324 | if (!s) { | |
325 | unlinkat(dirfd(d), de->d_name, 0); | |
326 | continue; | |
327 | } | |
328 | ||
329 | k = seat_load(s); | |
330 | if (k < 0) | |
331 | r = k; | |
332 | } | |
333 | ||
14c3baca LP |
334 | return r; |
335 | } | |
20263082 | 336 | |
14c3baca | 337 | static int manager_enumerate_linger_users(Manager *m) { |
9444b1f2 | 338 | _cleanup_closedir_ DIR *d = NULL; |
14c3baca LP |
339 | struct dirent *de; |
340 | int r = 0; | |
341 | ||
9444b1f2 LP |
342 | assert(m); |
343 | ||
14c3baca LP |
344 | d = opendir("/var/lib/systemd/linger"); |
345 | if (!d) { | |
346 | if (errno == ENOENT) | |
347 | return 0; | |
348 | ||
e1427b13 | 349 | return log_error_errno(errno, "Failed to open /var/lib/systemd/linger/: %m"); |
14c3baca LP |
350 | } |
351 | ||
9444b1f2 | 352 | FOREACH_DIRENT(de, d, return -errno) { |
14c3baca LP |
353 | int k; |
354 | ||
355 | if (!dirent_is_file(de)) | |
20263082 LP |
356 | continue; |
357 | ||
14c3baca LP |
358 | k = manager_add_user_by_name(m, de->d_name, NULL); |
359 | if (k < 0) { | |
da927ba9 | 360 | log_notice_errno(k, "Couldn't add lingering user %s: %m", de->d_name); |
14c3baca | 361 | r = k; |
20263082 LP |
362 | } |
363 | } | |
364 | ||
20263082 LP |
365 | return r; |
366 | } | |
367 | ||
9588bc32 | 368 | static int manager_enumerate_users(Manager *m) { |
9444b1f2 | 369 | _cleanup_closedir_ DIR *d = NULL; |
20263082 | 370 | struct dirent *de; |
14c3baca | 371 | int r, k; |
20263082 LP |
372 | |
373 | assert(m); | |
374 | ||
9444b1f2 | 375 | /* Add lingering users */ |
b3629c7d | 376 | r = manager_enumerate_linger_users(m); |
14c3baca | 377 | |
9444b1f2 | 378 | /* Read in user data stored on disk */ |
20263082 LP |
379 | d = opendir("/run/systemd/users"); |
380 | if (!d) { | |
381 | if (errno == ENOENT) | |
382 | return 0; | |
383 | ||
e1427b13 | 384 | return log_error_errno(errno, "Failed to open /run/systemd/users: %m"); |
20263082 LP |
385 | } |
386 | ||
9444b1f2 | 387 | FOREACH_DIRENT(de, d, return -errno) { |
20263082 | 388 | User *u; |
20263082 LP |
389 | |
390 | if (!dirent_is_file(de)) | |
391 | continue; | |
392 | ||
9444b1f2 | 393 | k = manager_add_user_by_name(m, de->d_name, &u); |
20263082 | 394 | if (k < 0) { |
da927ba9 | 395 | log_error_errno(k, "Failed to add user by file name %s: %m", de->d_name); |
20263082 | 396 | |
9444b1f2 | 397 | r = k; |
20263082 LP |
398 | continue; |
399 | } | |
400 | ||
9444b1f2 LP |
401 | user_add_to_gc_queue(u); |
402 | ||
20263082 LP |
403 | k = user_load(u); |
404 | if (k < 0) | |
405 | r = k; | |
406 | } | |
407 | ||
20263082 LP |
408 | return r; |
409 | } | |
410 | ||
9588bc32 | 411 | static int manager_enumerate_sessions(Manager *m) { |
9444b1f2 LP |
412 | _cleanup_closedir_ DIR *d = NULL; |
413 | struct dirent *de; | |
20263082 LP |
414 | int r = 0; |
415 | ||
9444b1f2 LP |
416 | assert(m); |
417 | ||
418 | /* Read in session data stored on disk */ | |
419 | d = opendir("/run/systemd/sessions"); | |
420 | if (!d) { | |
421 | if (errno == ENOENT) | |
422 | return 0; | |
423 | ||
e1427b13 | 424 | return log_error_errno(errno, "Failed to open /run/systemd/sessions: %m"); |
9444b1f2 LP |
425 | } |
426 | ||
427 | FOREACH_DIRENT(de, d, return -errno) { | |
428 | struct Session *s; | |
20263082 LP |
429 | int k; |
430 | ||
9444b1f2 | 431 | if (!dirent_is_file(de)) |
14c3baca LP |
432 | continue; |
433 | ||
4b549144 ZJS |
434 | if (!session_id_valid(de->d_name)) { |
435 | log_warning("Invalid session file name '%s', ignoring.", de->d_name); | |
436 | r = -EINVAL; | |
437 | continue; | |
438 | } | |
439 | ||
9444b1f2 | 440 | k = manager_add_session(m, de->d_name, &s); |
20263082 | 441 | if (k < 0) { |
da927ba9 | 442 | log_error_errno(k, "Failed to add session by file name %s: %m", de->d_name); |
20263082 | 443 | |
20263082 LP |
444 | r = k; |
445 | continue; | |
446 | } | |
447 | ||
9444b1f2 | 448 | session_add_to_gc_queue(s); |
20263082 | 449 | |
9444b1f2 | 450 | k = session_load(s); |
20263082 LP |
451 | if (k < 0) |
452 | r = k; | |
453 | } | |
454 | ||
455 | return r; | |
456 | } | |
457 | ||
9588bc32 | 458 | static int manager_enumerate_inhibitors(Manager *m) { |
9444b1f2 | 459 | _cleanup_closedir_ DIR *d = NULL; |
20263082 LP |
460 | struct dirent *de; |
461 | int r = 0; | |
462 | ||
463 | assert(m); | |
464 | ||
9444b1f2 | 465 | d = opendir("/run/systemd/inhibit"); |
20263082 LP |
466 | if (!d) { |
467 | if (errno == ENOENT) | |
468 | return 0; | |
469 | ||
e1427b13 | 470 | return log_error_errno(errno, "Failed to open /run/systemd/inhibit: %m"); |
20263082 LP |
471 | } |
472 | ||
9444b1f2 | 473 | FOREACH_DIRENT(de, d, return -errno) { |
20263082 | 474 | int k; |
9444b1f2 | 475 | Inhibitor *i; |
20263082 LP |
476 | |
477 | if (!dirent_is_file(de)) | |
478 | continue; | |
479 | ||
9444b1f2 LP |
480 | k = manager_add_inhibitor(m, de->d_name, &i); |
481 | if (k < 0) { | |
da927ba9 | 482 | log_notice_errno(k, "Couldn't add inhibitor %s: %m", de->d_name); |
9444b1f2 | 483 | r = k; |
20263082 LP |
484 | continue; |
485 | } | |
486 | ||
9444b1f2 | 487 | k = inhibitor_load(i); |
20263082 LP |
488 | if (k < 0) |
489 | r = k; | |
490 | } | |
491 | ||
20263082 LP |
492 | return r; |
493 | } | |
494 | ||
cc377381 | 495 | static int manager_dispatch_seat_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
7b77ed8c | 496 | _cleanup_udev_device_unref_ struct udev_device *d = NULL; |
cc377381 | 497 | Manager *m = userdata; |
20263082 LP |
498 | |
499 | assert(m); | |
500 | ||
30ed21ce | 501 | d = udev_monitor_receive_device(m->udev_seat_monitor); |
20263082 LP |
502 | if (!d) |
503 | return -ENOMEM; | |
504 | ||
7b77ed8c LP |
505 | manager_process_seat_device(m, d); |
506 | return 0; | |
30ed21ce LP |
507 | } |
508 | ||
cc377381 | 509 | static int manager_dispatch_device_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
7b77ed8c | 510 | _cleanup_udev_device_unref_ struct udev_device *d = NULL; |
cc377381 | 511 | Manager *m = userdata; |
718d006a DH |
512 | |
513 | assert(m); | |
514 | ||
515 | d = udev_monitor_receive_device(m->udev_device_monitor); | |
516 | if (!d) | |
517 | return -ENOMEM; | |
518 | ||
7b77ed8c LP |
519 | manager_process_seat_device(m, d); |
520 | return 0; | |
718d006a DH |
521 | } |
522 | ||
cc377381 | 523 | static int manager_dispatch_vcsa_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
7b77ed8c | 524 | _cleanup_udev_device_unref_ struct udev_device *d = NULL; |
cc377381 | 525 | Manager *m = userdata; |
30ed21ce LP |
526 | const char *name; |
527 | ||
528 | assert(m); | |
529 | ||
530 | d = udev_monitor_receive_device(m->udev_vcsa_monitor); | |
531 | if (!d) | |
532 | return -ENOMEM; | |
533 | ||
534 | name = udev_device_get_sysname(d); | |
535 | ||
536 | /* Whenever a VCSA device is removed try to reallocate our | |
537 | * VTs, to make sure our auto VTs never go away. */ | |
538 | ||
539 | if (name && startswith(name, "vcsa") && streq_ptr(udev_device_get_action(d), "remove")) | |
7b77ed8c | 540 | seat_preallocate_vts(m->seat0); |
30ed21ce | 541 | |
7b77ed8c | 542 | return 0; |
20263082 LP |
543 | } |
544 | ||
cc377381 | 545 | static int manager_dispatch_button_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
7b77ed8c | 546 | _cleanup_udev_device_unref_ struct udev_device *d = NULL; |
cc377381 | 547 | Manager *m = userdata; |
069cfc85 LP |
548 | |
549 | assert(m); | |
550 | ||
551 | d = udev_monitor_receive_device(m->udev_button_monitor); | |
552 | if (!d) | |
553 | return -ENOMEM; | |
554 | ||
7b77ed8c LP |
555 | manager_process_button_device(m, d); |
556 | return 0; | |
069cfc85 LP |
557 | } |
558 | ||
cc377381 LP |
559 | static int manager_dispatch_console(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
560 | Manager *m = userdata; | |
561 | ||
20263082 | 562 | assert(m); |
92432fcc | 563 | assert(m->seat0); |
cc377381 | 564 | assert(m->console_active_fd == fd); |
20263082 | 565 | |
92432fcc | 566 | seat_read_active_vt(m->seat0); |
20263082 LP |
567 | return 0; |
568 | } | |
569 | ||
98a77df5 LP |
570 | static int manager_reserve_vt(Manager *m) { |
571 | _cleanup_free_ char *p = NULL; | |
572 | ||
573 | assert(m); | |
574 | ||
575 | if (m->reserve_vt <= 0) | |
576 | return 0; | |
577 | ||
578 | if (asprintf(&p, "/dev/tty%u", m->reserve_vt) < 0) | |
579 | return log_oom(); | |
580 | ||
581 | m->reserve_vt_fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); | |
582 | if (m->reserve_vt_fd < 0) { | |
d4d882e5 LP |
583 | |
584 | /* Don't complain on VT-less systems */ | |
585 | if (errno != ENOENT) | |
56f64d95 | 586 | log_warning_errno(errno, "Failed to pin reserved VT: %m"); |
98a77df5 LP |
587 | return -errno; |
588 | } | |
589 | ||
590 | return 0; | |
591 | } | |
592 | ||
cc377381 | 593 | static int manager_connect_bus(Manager *m) { |
4afd3348 | 594 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
cc377381 | 595 | int r; |
31b79c2b | 596 | |
cc377381 LP |
597 | assert(m); |
598 | assert(!m->bus); | |
31b79c2b | 599 | |
76b54375 | 600 | r = sd_bus_default_system(&m->bus); |
f647962d MS |
601 | if (r < 0) |
602 | return log_error_errno(r, "Failed to connect to system bus: %m"); | |
31b79c2b | 603 | |
19befb2d | 604 | r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/login1", "org.freedesktop.login1.Manager", manager_vtable, m); |
f647962d MS |
605 | if (r < 0) |
606 | return log_error_errno(r, "Failed to add manager object vtable: %m"); | |
f8e2fb7b | 607 | |
19befb2d | 608 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/seat", "org.freedesktop.login1.Seat", seat_vtable, seat_object_find, m); |
f647962d MS |
609 | if (r < 0) |
610 | return log_error_errno(r, "Failed to add seat object vtable: %m"); | |
069cfc85 | 611 | |
19befb2d | 612 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/seat", seat_node_enumerator, m); |
f647962d MS |
613 | if (r < 0) |
614 | return log_error_errno(r, "Failed to add seat enumerator: %m"); | |
20263082 | 615 | |
19befb2d | 616 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/session", "org.freedesktop.login1.Session", session_vtable, session_object_find, m); |
f647962d MS |
617 | if (r < 0) |
618 | return log_error_errno(r, "Failed to add session object vtable: %m"); | |
20263082 | 619 | |
19befb2d | 620 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/session", session_node_enumerator, m); |
f647962d MS |
621 | if (r < 0) |
622 | return log_error_errno(r, "Failed to add session enumerator: %m"); | |
20263082 | 623 | |
19befb2d | 624 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/user", "org.freedesktop.login1.User", user_vtable, user_object_find, m); |
f647962d MS |
625 | if (r < 0) |
626 | return log_error_errno(r, "Failed to add user object vtable: %m"); | |
20263082 | 627 | |
19befb2d | 628 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/user", user_node_enumerator, m); |
f647962d MS |
629 | if (r < 0) |
630 | return log_error_errno(r, "Failed to add user enumerator: %m"); | |
20263082 | 631 | |
cc377381 | 632 | r = sd_bus_add_match(m->bus, |
19befb2d | 633 | NULL, |
cc377381 LP |
634 | "type='signal'," |
635 | "sender='org.freedesktop.systemd1'," | |
636 | "interface='org.freedesktop.systemd1.Manager'," | |
637 | "member='JobRemoved'," | |
638 | "path='/org/freedesktop/systemd1'", | |
639 | match_job_removed, m); | |
f647962d MS |
640 | if (r < 0) |
641 | return log_error_errno(r, "Failed to add match for JobRemoved: %m"); | |
943aca8e | 642 | |
cc377381 | 643 | r = sd_bus_add_match(m->bus, |
19befb2d | 644 | NULL, |
cc377381 LP |
645 | "type='signal'," |
646 | "sender='org.freedesktop.systemd1'," | |
647 | "interface='org.freedesktop.systemd1.Manager'," | |
648 | "member='UnitRemoved'," | |
649 | "path='/org/freedesktop/systemd1'", | |
650 | match_unit_removed, m); | |
f647962d MS |
651 | if (r < 0) |
652 | return log_error_errno(r, "Failed to add match for UnitRemoved: %m"); | |
fb6becb4 | 653 | |
cc377381 | 654 | r = sd_bus_add_match(m->bus, |
19befb2d | 655 | NULL, |
cc377381 LP |
656 | "type='signal'," |
657 | "sender='org.freedesktop.systemd1'," | |
658 | "interface='org.freedesktop.DBus.Properties'," | |
659 | "member='PropertiesChanged'", | |
660 | match_properties_changed, m); | |
f647962d MS |
661 | if (r < 0) |
662 | return log_error_errno(r, "Failed to add match for PropertiesChanged: %m"); | |
fb6becb4 | 663 | |
cc377381 | 664 | r = sd_bus_add_match(m->bus, |
19befb2d | 665 | NULL, |
cc377381 LP |
666 | "type='signal'," |
667 | "sender='org.freedesktop.systemd1'," | |
668 | "interface='org.freedesktop.systemd1.Manager'," | |
669 | "member='Reloading'," | |
670 | "path='/org/freedesktop/systemd1'", | |
671 | match_reloading, m); | |
f647962d MS |
672 | if (r < 0) |
673 | return log_error_errno(r, "Failed to add match for Reloading: %m"); | |
6797c324 | 674 | |
cc377381 | 675 | r = sd_bus_call_method( |
fb6becb4 LP |
676 | m->bus, |
677 | "org.freedesktop.systemd1", | |
678 | "/org/freedesktop/systemd1", | |
679 | "org.freedesktop.systemd1.Manager", | |
680 | "Subscribe", | |
fb6becb4 | 681 | &error, |
cc377381 | 682 | NULL, NULL); |
fb6becb4 | 683 | if (r < 0) { |
cc377381 LP |
684 | log_error("Failed to enable subscription: %s", bus_error_message(&error, r)); |
685 | return r; | |
20263082 LP |
686 | } |
687 | ||
5bb658a1 | 688 | r = sd_bus_request_name(m->bus, "org.freedesktop.login1", 0); |
f647962d MS |
689 | if (r < 0) |
690 | return log_error_errno(r, "Failed to register name: %m"); | |
bafd4449 | 691 | |
e11544a8 | 692 | r = sd_bus_attach_event(m->bus, m->event, SD_EVENT_PRIORITY_NORMAL); |
f647962d MS |
693 | if (r < 0) |
694 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
20263082 | 695 | |
20263082 | 696 | return 0; |
20263082 LP |
697 | } |
698 | ||
92683ad2 DH |
699 | static int manager_vt_switch(sd_event_source *src, const struct signalfd_siginfo *si, void *data) { |
700 | Manager *m = data; | |
701 | Session *active, *iter; | |
702 | ||
703 | /* | |
704 | * We got a VT-switch signal and we have to acknowledge it immediately. | |
705 | * Preferably, we'd just use m->seat0->active->vtfd, but unfortunately, | |
706 | * old user-space might run multiple sessions on a single VT, *sigh*. | |
707 | * Therefore, we have to iterate all sessions and find one with a vtfd | |
708 | * on the requested VT. | |
709 | * As only VTs with active controllers have VT_PROCESS set, our current | |
710 | * notion of the active VT might be wrong (for instance if the switch | |
711 | * happens while we setup VT_PROCESS). Therefore, read the current VT | |
712 | * first and then use s->active->vtnr as reference. Note that this is | |
713 | * not racy, as no further VT-switch can happen as long as we're in | |
714 | * synchronous VT_PROCESS mode. | |
715 | */ | |
716 | ||
717 | assert(m->seat0); | |
718 | seat_read_active_vt(m->seat0); | |
719 | ||
720 | active = m->seat0->active; | |
721 | if (!active || active->vtnr < 1) { | |
722 | log_warning("Received VT_PROCESS signal without a registered session on that VT."); | |
723 | return 0; | |
724 | } | |
725 | ||
726 | if (active->vtfd >= 0) { | |
2ec3ff66 | 727 | session_leave_vt(active); |
92683ad2 DH |
728 | } else { |
729 | LIST_FOREACH(sessions_by_seat, iter, m->seat0->sessions) { | |
730 | if (iter->vtnr == active->vtnr && iter->vtfd >= 0) { | |
2ec3ff66 | 731 | session_leave_vt(iter); |
92683ad2 DH |
732 | break; |
733 | } | |
734 | } | |
735 | } | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
20263082 | 740 | static int manager_connect_console(Manager *m) { |
cc377381 | 741 | int r; |
20263082 LP |
742 | |
743 | assert(m); | |
744 | assert(m->console_active_fd < 0); | |
745 | ||
74afee9c LP |
746 | /* On certain architectures (S390 and Xen, and containers), |
747 | /dev/tty0 does not exist, so don't fail if we can't open | |
748 | it. */ | |
cc377381 | 749 | if (access("/dev/tty0", F_OK) < 0) |
74afee9c | 750 | return 0; |
74afee9c | 751 | |
20263082 LP |
752 | m->console_active_fd = open("/sys/class/tty/tty0/active", O_RDONLY|O_NOCTTY|O_CLOEXEC); |
753 | if (m->console_active_fd < 0) { | |
cdc564d2 MS |
754 | |
755 | /* On some systems the device node /dev/tty0 may exist | |
756 | * even though /sys/class/tty/tty0 does not. */ | |
757 | if (errno == ENOENT) | |
758 | return 0; | |
759 | ||
e1427b13 | 760 | return log_error_errno(errno, "Failed to open /sys/class/tty/tty0/active: %m"); |
20263082 LP |
761 | } |
762 | ||
151b9b96 | 763 | r = sd_event_add_io(m->event, &m->console_active_event_source, m->console_active_fd, 0, manager_dispatch_console, m); |
cc377381 LP |
764 | if (r < 0) { |
765 | log_error("Failed to watch foreground console"); | |
766 | return r; | |
767 | } | |
20263082 | 768 | |
92683ad2 DH |
769 | /* |
770 | * SIGRTMIN is used as global VT-release signal, SIGRTMIN + 1 is used | |
771 | * as VT-acquire signal. We ignore any acquire-events (yes, we still | |
772 | * have to provide a valid signal-number for it!) and acknowledge all | |
773 | * release events immediately. | |
774 | */ | |
775 | ||
776 | if (SIGRTMIN + 1 > SIGRTMAX) { | |
777 | log_error("Not enough real-time signals available: %u-%u", SIGRTMIN, SIGRTMAX); | |
778 | return -EINVAL; | |
779 | } | |
780 | ||
72c0a2c2 LP |
781 | assert_se(ignore_signals(SIGRTMIN + 1, -1) >= 0); |
782 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN, -1) >= 0); | |
92683ad2 DH |
783 | |
784 | r = sd_event_add_signal(m->event, NULL, SIGRTMIN, manager_vt_switch, m); | |
785 | if (r < 0) | |
786 | return r; | |
787 | ||
20263082 LP |
788 | return 0; |
789 | } | |
790 | ||
791 | static int manager_connect_udev(Manager *m) { | |
14c3baca | 792 | int r; |
20263082 LP |
793 | |
794 | assert(m); | |
30ed21ce | 795 | assert(!m->udev_seat_monitor); |
718d006a | 796 | assert(!m->udev_device_monitor); |
30ed21ce | 797 | assert(!m->udev_vcsa_monitor); |
069cfc85 | 798 | assert(!m->udev_button_monitor); |
20263082 | 799 | |
30ed21ce LP |
800 | m->udev_seat_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
801 | if (!m->udev_seat_monitor) | |
20263082 LP |
802 | return -ENOMEM; |
803 | ||
2d96536d | 804 | r = udev_monitor_filter_add_match_tag(m->udev_seat_monitor, "master-of-seat"); |
14c3baca LP |
805 | if (r < 0) |
806 | return r; | |
20263082 | 807 | |
30ed21ce | 808 | r = udev_monitor_enable_receiving(m->udev_seat_monitor); |
14c3baca LP |
809 | if (r < 0) |
810 | return r; | |
20263082 | 811 | |
151b9b96 | 812 | r = sd_event_add_io(m->event, &m->udev_seat_event_source, udev_monitor_get_fd(m->udev_seat_monitor), EPOLLIN, manager_dispatch_seat_udev, m); |
cc377381 LP |
813 | if (r < 0) |
814 | return r; | |
069cfc85 | 815 | |
718d006a DH |
816 | m->udev_device_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
817 | if (!m->udev_device_monitor) | |
818 | return -ENOMEM; | |
819 | ||
820 | r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_device_monitor, "input", NULL); | |
821 | if (r < 0) | |
822 | return r; | |
823 | ||
824 | r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_device_monitor, "graphics", NULL); | |
825 | if (r < 0) | |
826 | return r; | |
827 | ||
828 | r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_device_monitor, "drm", NULL); | |
829 | if (r < 0) | |
830 | return r; | |
831 | ||
832 | r = udev_monitor_enable_receiving(m->udev_device_monitor); | |
833 | if (r < 0) | |
834 | return r; | |
835 | ||
151b9b96 | 836 | r = sd_event_add_io(m->event, &m->udev_device_event_source, udev_monitor_get_fd(m->udev_device_monitor), EPOLLIN, manager_dispatch_device_udev, m); |
cc377381 LP |
837 | if (r < 0) |
838 | return r; | |
718d006a | 839 | |
6de0e0e5 | 840 | /* Don't watch keys if nobody cares */ |
beaafb2e | 841 | if (m->handle_power_key != HANDLE_IGNORE || |
8e7fd6ad LP |
842 | m->handle_suspend_key != HANDLE_IGNORE || |
843 | m->handle_hibernate_key != HANDLE_IGNORE || | |
3c56cab4 BW |
844 | m->handle_lid_switch != HANDLE_IGNORE || |
845 | m->handle_lid_switch_docked != HANDLE_IGNORE) { | |
069cfc85 | 846 | |
6de0e0e5 LP |
847 | m->udev_button_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
848 | if (!m->udev_button_monitor) | |
849 | return -ENOMEM; | |
069cfc85 | 850 | |
6de0e0e5 LP |
851 | r = udev_monitor_filter_add_match_tag(m->udev_button_monitor, "power-switch"); |
852 | if (r < 0) | |
853 | return r; | |
069cfc85 | 854 | |
6de0e0e5 LP |
855 | r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_button_monitor, "input", NULL); |
856 | if (r < 0) | |
857 | return r; | |
069cfc85 | 858 | |
6de0e0e5 LP |
859 | r = udev_monitor_enable_receiving(m->udev_button_monitor); |
860 | if (r < 0) | |
861 | return r; | |
069cfc85 | 862 | |
151b9b96 | 863 | r = sd_event_add_io(m->event, &m->udev_button_event_source, udev_monitor_get_fd(m->udev_button_monitor), EPOLLIN, manager_dispatch_button_udev, m); |
cc377381 LP |
864 | if (r < 0) |
865 | return r; | |
6de0e0e5 | 866 | } |
069cfc85 | 867 | |
976c088a | 868 | /* Don't bother watching VCSA devices, if nobody cares */ |
6de0e0e5 | 869 | if (m->n_autovts > 0 && m->console_active_fd >= 0) { |
30ed21ce | 870 | |
6de0e0e5 LP |
871 | m->udev_vcsa_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
872 | if (!m->udev_vcsa_monitor) | |
873 | return -ENOMEM; | |
30ed21ce | 874 | |
6de0e0e5 LP |
875 | r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_vcsa_monitor, "vc", NULL); |
876 | if (r < 0) | |
877 | return r; | |
30ed21ce | 878 | |
6de0e0e5 LP |
879 | r = udev_monitor_enable_receiving(m->udev_vcsa_monitor); |
880 | if (r < 0) | |
881 | return r; | |
30ed21ce | 882 | |
151b9b96 | 883 | r = sd_event_add_io(m->event, &m->udev_vcsa_event_source, udev_monitor_get_fd(m->udev_vcsa_monitor), EPOLLIN, manager_dispatch_vcsa_udev, m); |
cc377381 LP |
884 | if (r < 0) |
885 | return r; | |
6de0e0e5 | 886 | } |
20263082 LP |
887 | |
888 | return 0; | |
889 | } | |
890 | ||
905f0a39 | 891 | static void manager_gc(Manager *m, bool drop_not_started) { |
14c3baca LP |
892 | Seat *seat; |
893 | Session *session; | |
894 | User *user; | |
895 | ||
896 | assert(m); | |
897 | ||
898 | while ((seat = m->seat_gc_queue)) { | |
71fda00f | 899 | LIST_REMOVE(gc_queue, m->seat_gc_queue, seat); |
14c3baca LP |
900 | seat->in_gc_queue = false; |
901 | ||
cc377381 | 902 | if (!seat_check_gc(seat, drop_not_started)) { |
9bb69af4 | 903 | seat_stop(seat, false); |
14c3baca LP |
904 | seat_free(seat); |
905 | } | |
906 | } | |
907 | ||
908 | while ((session = m->session_gc_queue)) { | |
71fda00f | 909 | LIST_REMOVE(gc_queue, m->session_gc_queue, session); |
14c3baca LP |
910 | session->in_gc_queue = false; |
911 | ||
5f41d1f1 LP |
912 | /* First, if we are not closing yet, initiate stopping */ |
913 | if (!session_check_gc(session, drop_not_started) && | |
914 | session_get_state(session) != SESSION_CLOSING) | |
9bb69af4 | 915 | session_stop(session, false); |
5f41d1f1 | 916 | |
491ac9f2 LP |
917 | /* Normally, this should make the session referenced |
918 | * again, if it doesn't then let's get rid of it | |
5f41d1f1 LP |
919 | * immediately */ |
920 | if (!session_check_gc(session, drop_not_started)) { | |
405e0255 | 921 | session_finalize(session); |
14c3baca LP |
922 | session_free(session); |
923 | } | |
924 | } | |
925 | ||
926 | while ((user = m->user_gc_queue)) { | |
71fda00f | 927 | LIST_REMOVE(gc_queue, m->user_gc_queue, user); |
14c3baca LP |
928 | user->in_gc_queue = false; |
929 | ||
b58b227a DH |
930 | /* First step: queue stop jobs */ |
931 | if (!user_check_gc(user, drop_not_started)) | |
9bb69af4 | 932 | user_stop(user, false); |
5f41d1f1 | 933 | |
b58b227a | 934 | /* Second step: finalize user */ |
5f41d1f1 | 935 | if (!user_check_gc(user, drop_not_started)) { |
405e0255 | 936 | user_finalize(user); |
14c3baca LP |
937 | user_free(user); |
938 | } | |
939 | } | |
940 | } | |
941 | ||
cc377381 LP |
942 | static int manager_dispatch_idle_action(sd_event_source *s, uint64_t t, void *userdata) { |
943 | Manager *m = userdata; | |
23406ce5 | 944 | struct dual_timestamp since; |
cc377381 | 945 | usec_t n, elapse; |
23406ce5 | 946 | int r; |
23406ce5 LP |
947 | |
948 | assert(m); | |
949 | ||
950 | if (m->idle_action == HANDLE_IGNORE || | |
cc377381 LP |
951 | m->idle_action_usec <= 0) |
952 | return 0; | |
23406ce5 | 953 | |
23406ce5 LP |
954 | n = now(CLOCK_MONOTONIC); |
955 | ||
956 | r = manager_get_idle_hint(m, &since); | |
957 | if (r <= 0) | |
39ccc87c | 958 | /* Not idle. Let's check if after a timeout it might be idle then. */ |
cc377381 | 959 | elapse = n + m->idle_action_usec; |
23406ce5 LP |
960 | else { |
961 | /* Idle! Let's see if it's time to do something, or if | |
962 | * we shall sleep for longer. */ | |
963 | ||
964 | if (n >= since.monotonic + m->idle_action_usec && | |
965 | (m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) { | |
966 | log_info("System idle. Taking action."); | |
967 | ||
968 | manager_handle_action(m, 0, m->idle_action, false, false); | |
969 | m->idle_action_not_before_usec = n; | |
970 | } | |
971 | ||
cc377381 | 972 | elapse = MAX(since.monotonic, m->idle_action_not_before_usec) + m->idle_action_usec; |
23406ce5 LP |
973 | } |
974 | ||
cc377381 | 975 | if (!m->idle_action_event_source) { |
23406ce5 | 976 | |
6a0f1f6d LP |
977 | r = sd_event_add_time( |
978 | m->event, | |
979 | &m->idle_action_event_source, | |
980 | CLOCK_MONOTONIC, | |
981 | elapse, USEC_PER_SEC*30, | |
982 | manager_dispatch_idle_action, m); | |
f647962d MS |
983 | if (r < 0) |
984 | return log_error_errno(r, "Failed to add idle event source: %m"); | |
23406ce5 | 985 | |
718db961 | 986 | r = sd_event_source_set_priority(m->idle_action_event_source, SD_EVENT_PRIORITY_IDLE+10); |
f647962d MS |
987 | if (r < 0) |
988 | return log_error_errno(r, "Failed to set idle event source priority: %m"); | |
cc377381 LP |
989 | } else { |
990 | r = sd_event_source_set_time(m->idle_action_event_source, elapse); | |
f647962d MS |
991 | if (r < 0) |
992 | return log_error_errno(r, "Failed to set idle event timer: %m"); | |
23406ce5 | 993 | |
cc377381 | 994 | r = sd_event_source_set_enabled(m->idle_action_event_source, SD_EVENT_ONESHOT); |
f647962d MS |
995 | if (r < 0) |
996 | return log_error_errno(r, "Failed to enable idle event timer: %m"); | |
23406ce5 LP |
997 | } |
998 | ||
999 | return 0; | |
23406ce5 | 1000 | } |
cc377381 | 1001 | |
a2ed7077 ZJS |
1002 | static int manager_parse_config_file(Manager *m) { |
1003 | assert(m); | |
1004 | ||
1005 | return config_parse_many(PKGSYSCONFDIR "/logind.conf", | |
1006 | CONF_PATHS_NULSTR("systemd/logind.conf.d"), | |
1007 | "Login\0", | |
1008 | config_item_perf_lookup, logind_gperf_lookup, | |
1009 | false, m); | |
1010 | } | |
1011 | ||
1012 | static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
1013 | Manager *m = userdata; | |
1014 | int r; | |
1015 | ||
1016 | manager_reset_config(m); | |
1017 | r = manager_parse_config_file(m); | |
1018 | if (r < 0) | |
1019 | log_warning_errno(r, "Failed to parse config file, using defaults: %m"); | |
1020 | else | |
1021 | log_info("Config file reloaded."); | |
1022 | ||
1023 | return 0; | |
1024 | } | |
1025 | ||
905f0a39 | 1026 | static int manager_startup(Manager *m) { |
20263082 | 1027 | int r; |
14c3baca LP |
1028 | Seat *seat; |
1029 | Session *session; | |
1030 | User *user; | |
ed4ba7e4 | 1031 | Button *button; |
f8e2fb7b | 1032 | Inhibitor *inhibitor; |
14c3baca | 1033 | Iterator i; |
20263082 LP |
1034 | |
1035 | assert(m); | |
20263082 | 1036 | |
a2ed7077 ZJS |
1037 | assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGHUP, -1) >= 0); |
1038 | ||
1039 | r = sd_event_add_signal(m->event, NULL, SIGHUP, manager_dispatch_reload_signal, m); | |
1040 | if (r < 0) | |
1041 | return log_error_errno(r, "Failed to register SIGHUP handler: %m"); | |
1042 | ||
976c088a LP |
1043 | /* Connect to console */ |
1044 | r = manager_connect_console(m); | |
20263082 LP |
1045 | if (r < 0) |
1046 | return r; | |
1047 | ||
976c088a LP |
1048 | /* Connect to udev */ |
1049 | r = manager_connect_udev(m); | |
f647962d MS |
1050 | if (r < 0) |
1051 | return log_error_errno(r, "Failed to create udev watchers: %m"); | |
20263082 LP |
1052 | |
1053 | /* Connect to the bus */ | |
1054 | r = manager_connect_bus(m); | |
1055 | if (r < 0) | |
1056 | return r; | |
1057 | ||
14c3baca | 1058 | /* Instantiate magic seat 0 */ |
92432fcc | 1059 | r = manager_add_seat(m, "seat0", &m->seat0); |
f647962d MS |
1060 | if (r < 0) |
1061 | return log_error_errno(r, "Failed to add seat0: %m"); | |
14c3baca | 1062 | |
9d10cbee | 1063 | r = manager_set_lid_switch_ignore(m, 0 + m->holdoff_timeout_usec); |
f9cd6be1 | 1064 | if (r < 0) |
da927ba9 | 1065 | log_warning_errno(r, "Failed to set up lid switch ignore event source: %m"); |
f9cd6be1 | 1066 | |
20263082 | 1067 | /* Deserialize state */ |
042f5988 ZJS |
1068 | r = manager_enumerate_devices(m); |
1069 | if (r < 0) | |
da927ba9 | 1070 | log_warning_errno(r, "Device enumeration failed: %m"); |
042f5988 ZJS |
1071 | |
1072 | r = manager_enumerate_seats(m); | |
1073 | if (r < 0) | |
da927ba9 | 1074 | log_warning_errno(r, "Seat enumeration failed: %m"); |
042f5988 ZJS |
1075 | |
1076 | r = manager_enumerate_users(m); | |
1077 | if (r < 0) | |
da927ba9 | 1078 | log_warning_errno(r, "User enumeration failed: %m"); |
042f5988 ZJS |
1079 | |
1080 | r = manager_enumerate_sessions(m); | |
1081 | if (r < 0) | |
da927ba9 | 1082 | log_warning_errno(r, "Session enumeration failed: %m"); |
042f5988 ZJS |
1083 | |
1084 | r = manager_enumerate_inhibitors(m); | |
1085 | if (r < 0) | |
da927ba9 | 1086 | log_warning_errno(r, "Inhibitor enumeration failed: %m"); |
042f5988 ZJS |
1087 | |
1088 | r = manager_enumerate_buttons(m); | |
1089 | if (r < 0) | |
da927ba9 | 1090 | log_warning_errno(r, "Button enumeration failed: %m"); |
20263082 | 1091 | |
4a4b033f LP |
1092 | /* Remove stale objects before we start them */ |
1093 | manager_gc(m, false); | |
1094 | ||
98a77df5 LP |
1095 | /* Reserve the special reserved VT */ |
1096 | manager_reserve_vt(m); | |
1097 | ||
14c3baca LP |
1098 | /* And start everything */ |
1099 | HASHMAP_FOREACH(seat, m->seats, i) | |
1100 | seat_start(seat); | |
1101 | ||
1102 | HASHMAP_FOREACH(user, m->users, i) | |
1103 | user_start(user); | |
1104 | ||
1105 | HASHMAP_FOREACH(session, m->sessions, i) | |
1106 | session_start(session); | |
20263082 | 1107 | |
f8e2fb7b LP |
1108 | HASHMAP_FOREACH(inhibitor, m->inhibitors, i) |
1109 | inhibitor_start(inhibitor); | |
1110 | ||
ed4ba7e4 | 1111 | HASHMAP_FOREACH(button, m->buttons, i) |
2d62c530 | 1112 | button_check_switches(button); |
ed4ba7e4 | 1113 | |
cc377381 | 1114 | manager_dispatch_idle_action(NULL, 0, m); |
23406ce5 | 1115 | |
20263082 LP |
1116 | return 0; |
1117 | } | |
1118 | ||
905f0a39 | 1119 | static int manager_run(Manager *m) { |
cc377381 LP |
1120 | int r; |
1121 | ||
20263082 LP |
1122 | assert(m); |
1123 | ||
1124 | for (;;) { | |
cc377381 LP |
1125 | r = sd_event_get_state(m->event); |
1126 | if (r < 0) | |
1127 | return r; | |
1128 | if (r == SD_EVENT_FINISHED) | |
1129 | return 0; | |
20263082 | 1130 | |
4a4b033f | 1131 | manager_gc(m, true); |
14c3baca | 1132 | |
418b22b8 DM |
1133 | r = manager_dispatch_delayed(m, false); |
1134 | if (r < 0) | |
1135 | return r; | |
1136 | if (r > 0) | |
1137 | continue; | |
1138 | ||
c0f32805 | 1139 | r = sd_event_run(m->event, (uint64_t) -1); |
cc377381 LP |
1140 | if (r < 0) |
1141 | return r; | |
20263082 | 1142 | } |
20263082 LP |
1143 | } |
1144 | ||
1145 | int main(int argc, char *argv[]) { | |
1146 | Manager *m = NULL; | |
5eda94dd | 1147 | int r; |
20263082 LP |
1148 | |
1149 | log_set_target(LOG_TARGET_AUTO); | |
3eff4208 | 1150 | log_set_facility(LOG_AUTH); |
20263082 LP |
1151 | log_parse_environment(); |
1152 | log_open(); | |
1153 | ||
4c12626c LP |
1154 | umask(0022); |
1155 | ||
20263082 LP |
1156 | if (argc != 1) { |
1157 | log_error("This program takes no arguments."); | |
1158 | r = -EINVAL; | |
1159 | goto finish; | |
1160 | } | |
1161 | ||
c3dacc8b | 1162 | r = mac_selinux_init(); |
4b51966c NI |
1163 | if (r < 0) { |
1164 | log_error_errno(r, "Could not initialize labelling: %m"); | |
1165 | goto finish; | |
1166 | } | |
1167 | ||
bb27ff66 LP |
1168 | /* Always create the directories people can create inotify |
1169 | * watches in. Note that some applications might check for the | |
ab06eef8 | 1170 | * existence of /run/systemd/seats/ to determine whether |
bb27ff66 LP |
1171 | * logind is available, so please always make sure this check |
1172 | * stays in. */ | |
1173 | mkdir_label("/run/systemd/seats", 0755); | |
1174 | mkdir_label("/run/systemd/users", 0755); | |
1175 | mkdir_label("/run/systemd/sessions", 0755); | |
1176 | ||
20263082 LP |
1177 | m = manager_new(); |
1178 | if (!m) { | |
0d0f0c50 | 1179 | r = log_oom(); |
20263082 LP |
1180 | goto finish; |
1181 | } | |
1182 | ||
193197e8 LP |
1183 | manager_parse_config_file(m); |
1184 | ||
20263082 LP |
1185 | r = manager_startup(m); |
1186 | if (r < 0) { | |
da927ba9 | 1187 | log_error_errno(r, "Failed to fully start up daemon: %m"); |
20263082 LP |
1188 | goto finish; |
1189 | } | |
1190 | ||
de0671ee | 1191 | log_debug("systemd-logind running as pid "PID_FMT, getpid()); |
e6960940 LP |
1192 | |
1193 | sd_notify(false, | |
1194 | "READY=1\n" | |
1195 | "STATUS=Processing requests..."); | |
1196 | ||
20263082 LP |
1197 | r = manager_run(m); |
1198 | ||
de0671ee | 1199 | log_debug("systemd-logind stopped as pid "PID_FMT, getpid()); |
e6960940 | 1200 | |
20263082 | 1201 | finish: |
e6960940 | 1202 | sd_notify(false, |
af4ec430 | 1203 | "STOPPING=1\n" |
e6960940 LP |
1204 | "STATUS=Shutting down..."); |
1205 | ||
3e044c49 | 1206 | manager_free(m); |
20263082 LP |
1207 | |
1208 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
1209 | } |