]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <sys/stat.h> | |
5 | #include <unistd.h> | |
6 | ||
7 | #include "sd-device.h" | |
8 | #include "sd-messages.h" | |
9 | ||
10 | #include "alloc-util.h" | |
11 | #include "audit-util.h" | |
12 | #include "bootspec.h" | |
13 | #include "bus-common-errors.h" | |
14 | #include "bus-error.h" | |
15 | #include "bus-get-properties.h" | |
16 | #include "bus-locator.h" | |
17 | #include "bus-polkit.h" | |
18 | #include "bus-unit-util.h" | |
19 | #include "bus-util.h" | |
20 | #include "cgroup-util.h" | |
21 | #include "device-util.h" | |
22 | #include "dirent-util.h" | |
23 | #include "efi-api.h" | |
24 | #include "efi-loader.h" | |
25 | #include "efivars.h" | |
26 | #include "env-file.h" | |
27 | #include "env-util.h" | |
28 | #include "escape.h" | |
29 | #include "event-util.h" | |
30 | #include "fd-util.h" | |
31 | #include "fileio-label.h" | |
32 | #include "fileio.h" | |
33 | #include "format-util.h" | |
34 | #include "fs-util.h" | |
35 | #include "logind-action.h" | |
36 | #include "logind-dbus.h" | |
37 | #include "logind-polkit.h" | |
38 | #include "logind-seat-dbus.h" | |
39 | #include "logind-session-dbus.h" | |
40 | #include "logind-user-dbus.h" | |
41 | #include "logind.h" | |
42 | #include "missing_capability.h" | |
43 | #include "mkdir-label.h" | |
44 | #include "parse-util.h" | |
45 | #include "path-util.h" | |
46 | #include "process-util.h" | |
47 | #include "reboot-util.h" | |
48 | #include "selinux-util.h" | |
49 | #include "sleep-config.h" | |
50 | #include "special.h" | |
51 | #include "serialize.h" | |
52 | #include "stdio-util.h" | |
53 | #include "strv.h" | |
54 | #include "terminal-util.h" | |
55 | #include "tmpfile-util.h" | |
56 | #include "unit-name.h" | |
57 | #include "user-util.h" | |
58 | #include "utmp-wtmp.h" | |
59 | #include "virt.h" | |
60 | ||
61 | /* As a random fun fact sysvinit had a 252 (256-(strlen(" \r\n")+1)) | |
62 | * character limit for the wall message. | |
63 | * https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72 | |
64 | * There is no real technical need for that but doesn't make sense | |
65 | * to store arbitrary amounts either. As we are not stingy here, we | |
66 | * allow 4k. | |
67 | */ | |
68 | #define WALL_MESSAGE_MAX 4096U | |
69 | ||
70 | #define SHUTDOWN_SCHEDULE_FILE "/run/systemd/shutdown/scheduled" | |
71 | ||
72 | static int update_schedule_file(Manager *m); | |
73 | static void reset_scheduled_shutdown(Manager *m); | |
74 | static int manager_setup_shutdown_timers(Manager* m); | |
75 | ||
76 | static int get_sender_session( | |
77 | Manager *m, | |
78 | sd_bus_message *message, | |
79 | bool consult_display, | |
80 | sd_bus_error *error, | |
81 | Session **ret) { | |
82 | ||
83 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
84 | Session *session = NULL; | |
85 | const char *name; | |
86 | int r; | |
87 | ||
88 | /* Acquire the sender's session. This first checks if the sending process is inside a session itself, | |
89 | * and returns that. If not and 'consult_display' is true, this returns the display session of the | |
90 | * owning user of the caller. */ | |
91 | ||
92 | r = sd_bus_query_sender_creds(message, | |
93 | SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT| | |
94 | (consult_display ? SD_BUS_CREDS_OWNER_UID : 0), &creds); | |
95 | if (r < 0) | |
96 | return r; | |
97 | ||
98 | r = sd_bus_creds_get_session(creds, &name); | |
99 | if (r < 0) { | |
100 | if (r != -ENXIO) | |
101 | return r; | |
102 | ||
103 | if (consult_display) { | |
104 | uid_t uid; | |
105 | ||
106 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
107 | if (r < 0) { | |
108 | if (r != -ENXIO) | |
109 | return r; | |
110 | } else { | |
111 | User *user; | |
112 | ||
113 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
114 | if (user) | |
115 | session = user->display; | |
116 | } | |
117 | } | |
118 | } else | |
119 | session = hashmap_get(m->sessions, name); | |
120 | ||
121 | if (!session) | |
122 | return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, | |
123 | consult_display ? | |
124 | "Caller does not belong to any known session and doesn't own any suitable session." : | |
125 | "Caller does not belong to any known session."); | |
126 | ||
127 | *ret = session; | |
128 | return 0; | |
129 | } | |
130 | ||
131 | int manager_get_session_from_creds( | |
132 | Manager *m, | |
133 | sd_bus_message *message, | |
134 | const char *name, | |
135 | sd_bus_error *error, | |
136 | Session **ret) { | |
137 | ||
138 | Session *session; | |
139 | ||
140 | assert(m); | |
141 | assert(ret); | |
142 | ||
143 | if (SEAT_IS_SELF(name)) /* the caller's own session */ | |
144 | return get_sender_session(m, message, false, error, ret); | |
145 | if (SEAT_IS_AUTO(name)) /* The caller's own session if they have one, otherwise their user's display session */ | |
146 | return get_sender_session(m, message, true, error, ret); | |
147 | ||
148 | session = hashmap_get(m->sessions, name); | |
149 | if (!session) | |
150 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); | |
151 | ||
152 | *ret = session; | |
153 | return 0; | |
154 | } | |
155 | ||
156 | static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) { | |
157 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
158 | uid_t uid; | |
159 | User *user; | |
160 | int r; | |
161 | ||
162 | /* Note that we get the owner UID of the session, not the actual client UID here! */ | |
163 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
164 | if (r < 0) | |
165 | return r; | |
166 | ||
167 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
168 | if (r < 0) { | |
169 | if (r != -ENXIO) | |
170 | return r; | |
171 | ||
172 | user = NULL; | |
173 | } else | |
174 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
175 | ||
176 | if (!user) | |
177 | return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, | |
178 | "Caller does not belong to any logged in or lingering user"); | |
179 | ||
180 | *ret = user; | |
181 | return 0; | |
182 | } | |
183 | ||
184 | int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) { | |
185 | User *user; | |
186 | ||
187 | assert(m); | |
188 | assert(ret); | |
189 | ||
190 | if (!uid_is_valid(uid)) | |
191 | return get_sender_user(m, message, error, ret); | |
192 | ||
193 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
194 | if (!user) | |
195 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, | |
196 | "User ID "UID_FMT" is not logged in or lingering", uid); | |
197 | ||
198 | *ret = user; | |
199 | return 0; | |
200 | } | |
201 | ||
202 | int manager_get_seat_from_creds( | |
203 | Manager *m, | |
204 | sd_bus_message *message, | |
205 | const char *name, | |
206 | sd_bus_error *error, | |
207 | Seat **ret) { | |
208 | ||
209 | Seat *seat; | |
210 | int r; | |
211 | ||
212 | assert(m); | |
213 | assert(ret); | |
214 | ||
215 | if (SEAT_IS_SELF(name) || SEAT_IS_AUTO(name)) { | |
216 | Session *session; | |
217 | ||
218 | /* Use these special seat names as session names */ | |
219 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
220 | if (r < 0) | |
221 | return r; | |
222 | ||
223 | seat = session->seat; | |
224 | if (!seat) | |
225 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session '%s' has no seat.", session->id); | |
226 | } else { | |
227 | seat = hashmap_get(m->seats, name); | |
228 | if (!seat) | |
229 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name); | |
230 | } | |
231 | ||
232 | *ret = seat; | |
233 | return 0; | |
234 | } | |
235 | ||
236 | static int return_test_polkit( | |
237 | sd_bus_message *message, | |
238 | int capability, | |
239 | const char *action, | |
240 | const char **details, | |
241 | uid_t good_user, | |
242 | sd_bus_error *e) { | |
243 | ||
244 | const char *result; | |
245 | bool challenge; | |
246 | int r; | |
247 | ||
248 | r = bus_test_polkit(message, capability, action, details, good_user, &challenge, e); | |
249 | if (r < 0) | |
250 | return r; | |
251 | ||
252 | if (r > 0) | |
253 | result = "yes"; | |
254 | else if (challenge) | |
255 | result = "challenge"; | |
256 | else | |
257 | result = "no"; | |
258 | ||
259 | return sd_bus_reply_method_return(message, "s", result); | |
260 | } | |
261 | ||
262 | static int property_get_idle_hint( | |
263 | sd_bus *bus, | |
264 | const char *path, | |
265 | const char *interface, | |
266 | const char *property, | |
267 | sd_bus_message *reply, | |
268 | void *userdata, | |
269 | sd_bus_error *error) { | |
270 | ||
271 | Manager *m = ASSERT_PTR(userdata); | |
272 | ||
273 | assert(bus); | |
274 | assert(reply); | |
275 | ||
276 | return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0); | |
277 | } | |
278 | ||
279 | static int property_get_idle_since_hint( | |
280 | sd_bus *bus, | |
281 | const char *path, | |
282 | const char *interface, | |
283 | const char *property, | |
284 | sd_bus_message *reply, | |
285 | void *userdata, | |
286 | sd_bus_error *error) { | |
287 | ||
288 | Manager *m = ASSERT_PTR(userdata); | |
289 | dual_timestamp t = DUAL_TIMESTAMP_NULL; | |
290 | ||
291 | assert(bus); | |
292 | assert(reply); | |
293 | ||
294 | manager_get_idle_hint(m, &t); | |
295 | ||
296 | return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic); | |
297 | } | |
298 | ||
299 | static int property_get_inhibited( | |
300 | sd_bus *bus, | |
301 | const char *path, | |
302 | const char *interface, | |
303 | const char *property, | |
304 | sd_bus_message *reply, | |
305 | void *userdata, | |
306 | sd_bus_error *error) { | |
307 | ||
308 | Manager *m = ASSERT_PTR(userdata); | |
309 | InhibitWhat w; | |
310 | ||
311 | assert(bus); | |
312 | assert(reply); | |
313 | ||
314 | w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY); | |
315 | ||
316 | return sd_bus_message_append(reply, "s", inhibit_what_to_string(w)); | |
317 | } | |
318 | ||
319 | static int property_get_preparing( | |
320 | sd_bus *bus, | |
321 | const char *path, | |
322 | const char *interface, | |
323 | const char *property, | |
324 | sd_bus_message *reply, | |
325 | void *userdata, | |
326 | sd_bus_error *error) { | |
327 | ||
328 | Manager *m = ASSERT_PTR(userdata); | |
329 | bool b = false; | |
330 | ||
331 | assert(bus); | |
332 | assert(reply); | |
333 | ||
334 | if (m->delayed_action) { | |
335 | if (streq(property, "PreparingForShutdown")) | |
336 | b = m->delayed_action->inhibit_what & INHIBIT_SHUTDOWN; | |
337 | else | |
338 | b = m->delayed_action->inhibit_what & INHIBIT_SLEEP; | |
339 | } | |
340 | ||
341 | return sd_bus_message_append(reply, "b", b); | |
342 | } | |
343 | ||
344 | static int property_get_scheduled_shutdown( | |
345 | sd_bus *bus, | |
346 | const char *path, | |
347 | const char *interface, | |
348 | const char *property, | |
349 | sd_bus_message *reply, | |
350 | void *userdata, | |
351 | sd_bus_error *error) { | |
352 | ||
353 | Manager *m = ASSERT_PTR(userdata); | |
354 | int r; | |
355 | ||
356 | assert(bus); | |
357 | assert(reply); | |
358 | ||
359 | r = sd_bus_message_open_container(reply, 'r', "st"); | |
360 | if (r < 0) | |
361 | return r; | |
362 | ||
363 | r = sd_bus_message_append(reply, "st", | |
364 | m->scheduled_shutdown_action ? handle_action_to_string(m->scheduled_shutdown_action->handle) : NULL, | |
365 | m->scheduled_shutdown_timeout); | |
366 | if (r < 0) | |
367 | return r; | |
368 | ||
369 | return sd_bus_message_close_container(reply); | |
370 | } | |
371 | ||
372 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction); | |
373 | static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays); | |
374 | static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed, "b", Manager, manager_is_lid_closed); | |
375 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power, "b", manager_is_on_external_power()); | |
376 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX); | |
377 | static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size); | |
378 | ||
379 | static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
380 | _cleanup_free_ char *p = NULL; | |
381 | Manager *m = ASSERT_PTR(userdata); | |
382 | const char *name; | |
383 | Session *session; | |
384 | int r; | |
385 | ||
386 | assert(message); | |
387 | ||
388 | r = sd_bus_message_read(message, "s", &name); | |
389 | if (r < 0) | |
390 | return r; | |
391 | ||
392 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
393 | if (r < 0) | |
394 | return r; | |
395 | ||
396 | p = session_bus_path(session); | |
397 | if (!p) | |
398 | return -ENOMEM; | |
399 | ||
400 | return sd_bus_reply_method_return(message, "o", p); | |
401 | } | |
402 | ||
403 | /* Get login session of a process. This is not what you are looking for these days, | |
404 | * as apps may instead belong to a user service unit. This includes terminal | |
405 | * emulators and hence command-line apps. */ | |
406 | static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
407 | _cleanup_free_ char *p = NULL; | |
408 | Session *session = NULL; | |
409 | Manager *m = ASSERT_PTR(userdata); | |
410 | pid_t pid; | |
411 | int r; | |
412 | ||
413 | assert(message); | |
414 | ||
415 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); | |
416 | ||
417 | r = sd_bus_message_read(message, "u", &pid); | |
418 | if (r < 0) | |
419 | return r; | |
420 | if (pid < 0) | |
421 | return -EINVAL; | |
422 | ||
423 | if (pid == 0) { | |
424 | r = manager_get_session_from_creds(m, message, NULL, error, &session); | |
425 | if (r < 0) | |
426 | return r; | |
427 | } else { | |
428 | r = manager_get_session_by_pid(m, pid, &session); | |
429 | if (r < 0) | |
430 | return r; | |
431 | ||
432 | if (!session) | |
433 | return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, | |
434 | "PID "PID_FMT" does not belong to any known session", pid); | |
435 | } | |
436 | ||
437 | p = session_bus_path(session); | |
438 | if (!p) | |
439 | return -ENOMEM; | |
440 | ||
441 | return sd_bus_reply_method_return(message, "o", p); | |
442 | } | |
443 | ||
444 | static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
445 | _cleanup_free_ char *p = NULL; | |
446 | Manager *m = ASSERT_PTR(userdata); | |
447 | uint32_t uid; | |
448 | User *user; | |
449 | int r; | |
450 | ||
451 | assert(message); | |
452 | ||
453 | r = sd_bus_message_read(message, "u", &uid); | |
454 | if (r < 0) | |
455 | return r; | |
456 | ||
457 | r = manager_get_user_from_creds(m, message, uid, error, &user); | |
458 | if (r < 0) | |
459 | return r; | |
460 | ||
461 | p = user_bus_path(user); | |
462 | if (!p) | |
463 | return -ENOMEM; | |
464 | ||
465 | return sd_bus_reply_method_return(message, "o", p); | |
466 | } | |
467 | ||
468 | static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
469 | _cleanup_free_ char *p = NULL; | |
470 | Manager *m = ASSERT_PTR(userdata); | |
471 | User *user = NULL; | |
472 | pid_t pid; | |
473 | int r; | |
474 | ||
475 | assert(message); | |
476 | ||
477 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); | |
478 | ||
479 | r = sd_bus_message_read(message, "u", &pid); | |
480 | if (r < 0) | |
481 | return r; | |
482 | if (pid < 0) | |
483 | return -EINVAL; | |
484 | ||
485 | if (pid == 0) { | |
486 | r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user); | |
487 | if (r < 0) | |
488 | return r; | |
489 | } else { | |
490 | r = manager_get_user_by_pid(m, pid, &user); | |
491 | if (r < 0) | |
492 | return r; | |
493 | if (!user) | |
494 | return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, | |
495 | "PID "PID_FMT" does not belong to any logged in user or lingering user", | |
496 | pid); | |
497 | } | |
498 | ||
499 | p = user_bus_path(user); | |
500 | if (!p) | |
501 | return -ENOMEM; | |
502 | ||
503 | return sd_bus_reply_method_return(message, "o", p); | |
504 | } | |
505 | ||
506 | static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
507 | _cleanup_free_ char *p = NULL; | |
508 | Manager *m = ASSERT_PTR(userdata); | |
509 | const char *name; | |
510 | Seat *seat; | |
511 | int r; | |
512 | ||
513 | assert(message); | |
514 | ||
515 | r = sd_bus_message_read(message, "s", &name); | |
516 | if (r < 0) | |
517 | return r; | |
518 | ||
519 | r = manager_get_seat_from_creds(m, message, name, error, &seat); | |
520 | if (r < 0) | |
521 | return r; | |
522 | ||
523 | p = seat_bus_path(seat); | |
524 | if (!p) | |
525 | return -ENOMEM; | |
526 | ||
527 | return sd_bus_reply_method_return(message, "o", p); | |
528 | } | |
529 | ||
530 | static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
531 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
532 | Manager *m = ASSERT_PTR(userdata); | |
533 | Session *session; | |
534 | int r; | |
535 | ||
536 | assert(message); | |
537 | ||
538 | r = sd_bus_message_new_method_return(message, &reply); | |
539 | if (r < 0) | |
540 | return r; | |
541 | ||
542 | r = sd_bus_message_open_container(reply, 'a', "(susso)"); | |
543 | if (r < 0) | |
544 | return r; | |
545 | ||
546 | HASHMAP_FOREACH(session, m->sessions) { | |
547 | _cleanup_free_ char *p = NULL; | |
548 | ||
549 | p = session_bus_path(session); | |
550 | if (!p) | |
551 | return -ENOMEM; | |
552 | ||
553 | r = sd_bus_message_append(reply, "(susso)", | |
554 | session->id, | |
555 | (uint32_t) session->user->user_record->uid, | |
556 | session->user->user_record->user_name, | |
557 | session->seat ? session->seat->id : "", | |
558 | p); | |
559 | if (r < 0) | |
560 | return r; | |
561 | } | |
562 | ||
563 | r = sd_bus_message_close_container(reply); | |
564 | if (r < 0) | |
565 | return r; | |
566 | ||
567 | return sd_bus_send(NULL, reply, NULL); | |
568 | } | |
569 | ||
570 | static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
571 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
572 | Manager *m = ASSERT_PTR(userdata); | |
573 | User *user; | |
574 | int r; | |
575 | ||
576 | assert(message); | |
577 | ||
578 | r = sd_bus_message_new_method_return(message, &reply); | |
579 | if (r < 0) | |
580 | return r; | |
581 | ||
582 | r = sd_bus_message_open_container(reply, 'a', "(uso)"); | |
583 | if (r < 0) | |
584 | return r; | |
585 | ||
586 | HASHMAP_FOREACH(user, m->users) { | |
587 | _cleanup_free_ char *p = NULL; | |
588 | ||
589 | p = user_bus_path(user); | |
590 | if (!p) | |
591 | return -ENOMEM; | |
592 | ||
593 | r = sd_bus_message_append(reply, "(uso)", | |
594 | (uint32_t) user->user_record->uid, | |
595 | user->user_record->user_name, | |
596 | p); | |
597 | if (r < 0) | |
598 | return r; | |
599 | } | |
600 | ||
601 | r = sd_bus_message_close_container(reply); | |
602 | if (r < 0) | |
603 | return r; | |
604 | ||
605 | return sd_bus_send(NULL, reply, NULL); | |
606 | } | |
607 | ||
608 | static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
609 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
610 | Manager *m = ASSERT_PTR(userdata); | |
611 | Seat *seat; | |
612 | int r; | |
613 | ||
614 | assert(message); | |
615 | ||
616 | r = sd_bus_message_new_method_return(message, &reply); | |
617 | if (r < 0) | |
618 | return r; | |
619 | ||
620 | r = sd_bus_message_open_container(reply, 'a', "(so)"); | |
621 | if (r < 0) | |
622 | return r; | |
623 | ||
624 | HASHMAP_FOREACH(seat, m->seats) { | |
625 | _cleanup_free_ char *p = NULL; | |
626 | ||
627 | p = seat_bus_path(seat); | |
628 | if (!p) | |
629 | return -ENOMEM; | |
630 | ||
631 | r = sd_bus_message_append(reply, "(so)", seat->id, p); | |
632 | if (r < 0) | |
633 | return r; | |
634 | } | |
635 | ||
636 | r = sd_bus_message_close_container(reply); | |
637 | if (r < 0) | |
638 | return r; | |
639 | ||
640 | return sd_bus_send(NULL, reply, NULL); | |
641 | } | |
642 | ||
643 | static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
644 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
645 | Manager *m = ASSERT_PTR(userdata); | |
646 | Inhibitor *inhibitor; | |
647 | int r; | |
648 | ||
649 | assert(message); | |
650 | ||
651 | r = sd_bus_message_new_method_return(message, &reply); | |
652 | if (r < 0) | |
653 | return r; | |
654 | ||
655 | r = sd_bus_message_open_container(reply, 'a', "(ssssuu)"); | |
656 | if (r < 0) | |
657 | return r; | |
658 | ||
659 | HASHMAP_FOREACH(inhibitor, m->inhibitors) { | |
660 | ||
661 | r = sd_bus_message_append(reply, "(ssssuu)", | |
662 | strempty(inhibit_what_to_string(inhibitor->what)), | |
663 | strempty(inhibitor->who), | |
664 | strempty(inhibitor->why), | |
665 | strempty(inhibit_mode_to_string(inhibitor->mode)), | |
666 | (uint32_t) inhibitor->uid, | |
667 | (uint32_t) inhibitor->pid); | |
668 | if (r < 0) | |
669 | return r; | |
670 | } | |
671 | ||
672 | r = sd_bus_message_close_container(reply); | |
673 | if (r < 0) | |
674 | return r; | |
675 | ||
676 | return sd_bus_send(NULL, reply, NULL); | |
677 | } | |
678 | ||
679 | static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
680 | const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop; | |
681 | _cleanup_free_ char *id = NULL; | |
682 | Session *session = NULL; | |
683 | uint32_t audit_id = 0; | |
684 | Manager *m = ASSERT_PTR(userdata); | |
685 | User *user = NULL; | |
686 | Seat *seat = NULL; | |
687 | pid_t leader; | |
688 | uid_t uid; | |
689 | int remote; | |
690 | uint32_t vtnr = 0; | |
691 | SessionType t; | |
692 | SessionClass c; | |
693 | int r; | |
694 | ||
695 | assert(message); | |
696 | ||
697 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); | |
698 | assert_cc(sizeof(uid_t) == sizeof(uint32_t)); | |
699 | ||
700 | r = sd_bus_message_read(message, "uusssssussbss", | |
701 | &uid, &leader, &service, &type, &class, &desktop, &cseat, | |
702 | &vtnr, &tty, &display, &remote, &remote_user, &remote_host); | |
703 | if (r < 0) | |
704 | return r; | |
705 | ||
706 | if (!uid_is_valid(uid)) | |
707 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID"); | |
708 | if (leader < 0 || leader == 1 || leader == getpid_cached()) | |
709 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); | |
710 | ||
711 | if (isempty(type)) | |
712 | t = _SESSION_TYPE_INVALID; | |
713 | else { | |
714 | t = session_type_from_string(type); | |
715 | if (t < 0) | |
716 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
717 | "Invalid session type %s", type); | |
718 | } | |
719 | ||
720 | if (isempty(class)) | |
721 | c = _SESSION_CLASS_INVALID; | |
722 | else { | |
723 | c = session_class_from_string(class); | |
724 | if (c < 0) | |
725 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
726 | "Invalid session class %s", class); | |
727 | } | |
728 | ||
729 | if (isempty(desktop)) | |
730 | desktop = NULL; | |
731 | else { | |
732 | if (!string_is_safe(desktop)) | |
733 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
734 | "Invalid desktop string %s", desktop); | |
735 | } | |
736 | ||
737 | if (isempty(cseat)) | |
738 | seat = NULL; | |
739 | else { | |
740 | seat = hashmap_get(m->seats, cseat); | |
741 | if (!seat) | |
742 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, | |
743 | "No seat '%s' known", cseat); | |
744 | } | |
745 | ||
746 | if (tty_is_vc(tty)) { | |
747 | int v; | |
748 | ||
749 | if (!seat) | |
750 | seat = m->seat0; | |
751 | else if (seat != m->seat0) | |
752 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
753 | "TTY %s is virtual console but seat %s is not seat0", tty, seat->id); | |
754 | ||
755 | v = vtnr_from_tty(tty); | |
756 | if (v <= 0) | |
757 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
758 | "Cannot determine VT number from virtual console TTY %s", tty); | |
759 | ||
760 | if (vtnr == 0) | |
761 | vtnr = (uint32_t) v; | |
762 | else if (vtnr != (uint32_t) v) | |
763 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
764 | "Specified TTY and VT number do not match"); | |
765 | ||
766 | } else if (tty_is_console(tty)) { | |
767 | ||
768 | if (!seat) | |
769 | seat = m->seat0; | |
770 | else if (seat != m->seat0) | |
771 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
772 | "Console TTY specified but seat is not seat0"); | |
773 | ||
774 | if (vtnr != 0) | |
775 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
776 | "Console TTY specified but VT number is not 0"); | |
777 | } | |
778 | ||
779 | if (seat) { | |
780 | if (seat_has_vts(seat)) { | |
781 | if (vtnr <= 0 || vtnr > 63) | |
782 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
783 | "VT number out of range"); | |
784 | } else { | |
785 | if (vtnr != 0) | |
786 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
787 | "Seat has no VTs but VT number not 0"); | |
788 | } | |
789 | } | |
790 | ||
791 | if (t == _SESSION_TYPE_INVALID) { | |
792 | if (!isempty(display)) | |
793 | t = SESSION_X11; | |
794 | else if (!isempty(tty)) | |
795 | t = SESSION_TTY; | |
796 | else | |
797 | t = SESSION_UNSPECIFIED; | |
798 | } | |
799 | ||
800 | if (c == _SESSION_CLASS_INVALID) { | |
801 | if (t == SESSION_UNSPECIFIED) | |
802 | c = SESSION_BACKGROUND; | |
803 | else | |
804 | c = SESSION_USER; | |
805 | } | |
806 | ||
807 | if (leader == 0) { | |
808 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
809 | ||
810 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
811 | if (r < 0) | |
812 | return r; | |
813 | ||
814 | r = sd_bus_creds_get_pid(creds, (pid_t*) &leader); | |
815 | if (r < 0) | |
816 | return r; | |
817 | } | |
818 | ||
819 | /* Check if we are already in a logind session. Or if we are in user@.service | |
820 | * which is a special PAM session that avoids creating a logind session. */ | |
821 | r = manager_get_user_by_pid(m, leader, NULL); | |
822 | if (r < 0) | |
823 | return r; | |
824 | if (r > 0) | |
825 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, | |
826 | "Already running in a session or user slice"); | |
827 | ||
828 | /* | |
829 | * Old gdm and lightdm start the user-session on the same VT as | |
830 | * the greeter session. But they destroy the greeter session | |
831 | * after the user-session and want the user-session to take | |
832 | * over the VT. We need to support this for | |
833 | * backwards-compatibility, so make sure we allow new sessions | |
834 | * on a VT that a greeter is running on. Furthermore, to allow | |
835 | * re-logins, we have to allow a greeter to take over a used VT for | |
836 | * the exact same reasons. | |
837 | */ | |
838 | if (c != SESSION_GREETER && | |
839 | vtnr > 0 && | |
840 | vtnr < MALLOC_ELEMENTSOF(m->seat0->positions) && | |
841 | m->seat0->positions[vtnr] && | |
842 | m->seat0->positions[vtnr]->class != SESSION_GREETER) | |
843 | return sd_bus_error_set(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session"); | |
844 | ||
845 | if (hashmap_size(m->sessions) >= m->sessions_max) | |
846 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, | |
847 | "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", | |
848 | m->sessions_max); | |
849 | ||
850 | (void) audit_session_from_pid(leader, &audit_id); | |
851 | if (audit_session_is_valid(audit_id)) { | |
852 | /* Keep our session IDs and the audit session IDs in sync */ | |
853 | ||
854 | if (asprintf(&id, "%"PRIu32, audit_id) < 0) | |
855 | return -ENOMEM; | |
856 | ||
857 | /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's | |
858 | * not trust the audit data and let's better register a new ID */ | |
859 | if (hashmap_contains(m->sessions, id)) { | |
860 | log_warning("Existing logind session ID %s used by new audit session, ignoring.", id); | |
861 | audit_id = AUDIT_SESSION_INVALID; | |
862 | id = mfree(id); | |
863 | } | |
864 | } | |
865 | ||
866 | if (!id) { | |
867 | do { | |
868 | id = mfree(id); | |
869 | ||
870 | if (asprintf(&id, "c%lu", ++m->session_counter) < 0) | |
871 | return -ENOMEM; | |
872 | ||
873 | } while (hashmap_contains(m->sessions, id)); | |
874 | } | |
875 | ||
876 | /* The generated names should not clash with 'auto' or 'self' */ | |
877 | assert(!SESSION_IS_SELF(id)); | |
878 | assert(!SESSION_IS_AUTO(id)); | |
879 | ||
880 | /* If we are not watching utmp already, try again */ | |
881 | manager_reconnect_utmp(m); | |
882 | ||
883 | r = manager_add_user_by_uid(m, uid, &user); | |
884 | if (r < 0) | |
885 | goto fail; | |
886 | ||
887 | r = manager_add_session(m, id, &session); | |
888 | if (r < 0) | |
889 | goto fail; | |
890 | ||
891 | session_set_user(session, user); | |
892 | r = session_set_leader(session, leader); | |
893 | if (r < 0) | |
894 | goto fail; | |
895 | ||
896 | session->original_type = session->type = t; | |
897 | session->class = c; | |
898 | session->remote = remote; | |
899 | session->vtnr = vtnr; | |
900 | ||
901 | if (!isempty(tty)) { | |
902 | session->tty = strdup(tty); | |
903 | if (!session->tty) { | |
904 | r = -ENOMEM; | |
905 | goto fail; | |
906 | } | |
907 | ||
908 | session->tty_validity = TTY_FROM_PAM; | |
909 | } | |
910 | ||
911 | if (!isempty(display)) { | |
912 | session->display = strdup(display); | |
913 | if (!session->display) { | |
914 | r = -ENOMEM; | |
915 | goto fail; | |
916 | } | |
917 | } | |
918 | ||
919 | if (!isempty(remote_user)) { | |
920 | session->remote_user = strdup(remote_user); | |
921 | if (!session->remote_user) { | |
922 | r = -ENOMEM; | |
923 | goto fail; | |
924 | } | |
925 | } | |
926 | ||
927 | if (!isempty(remote_host)) { | |
928 | session->remote_host = strdup(remote_host); | |
929 | if (!session->remote_host) { | |
930 | r = -ENOMEM; | |
931 | goto fail; | |
932 | } | |
933 | } | |
934 | ||
935 | if (!isempty(service)) { | |
936 | session->service = strdup(service); | |
937 | if (!session->service) { | |
938 | r = -ENOMEM; | |
939 | goto fail; | |
940 | } | |
941 | } | |
942 | ||
943 | if (!isempty(desktop)) { | |
944 | session->desktop = strdup(desktop); | |
945 | if (!session->desktop) { | |
946 | r = -ENOMEM; | |
947 | goto fail; | |
948 | } | |
949 | } | |
950 | ||
951 | if (seat) { | |
952 | r = seat_attach_session(seat, session); | |
953 | if (r < 0) | |
954 | goto fail; | |
955 | } | |
956 | ||
957 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); | |
958 | if (r < 0) | |
959 | goto fail; | |
960 | ||
961 | r = session_start(session, message, error); | |
962 | if (r < 0) | |
963 | goto fail; | |
964 | ||
965 | r = sd_bus_message_exit_container(message); | |
966 | if (r < 0) | |
967 | goto fail; | |
968 | ||
969 | session->create_message = sd_bus_message_ref(message); | |
970 | ||
971 | /* Now, let's wait until the slice unit and stuff got created. We send the reply back from | |
972 | * session_send_create_reply(). */ | |
973 | ||
974 | return 1; | |
975 | ||
976 | fail: | |
977 | if (session) | |
978 | session_add_to_gc_queue(session); | |
979 | ||
980 | if (user) | |
981 | user_add_to_gc_queue(user); | |
982 | ||
983 | return r; | |
984 | } | |
985 | ||
986 | static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
987 | Manager *m = ASSERT_PTR(userdata); | |
988 | Session *session; | |
989 | const char *name; | |
990 | int r; | |
991 | ||
992 | assert(message); | |
993 | ||
994 | r = sd_bus_message_read(message, "s", &name); | |
995 | if (r < 0) | |
996 | return r; | |
997 | ||
998 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
999 | if (r < 0) | |
1000 | return r; | |
1001 | ||
1002 | r = session_release(session); | |
1003 | if (r < 0) | |
1004 | return r; | |
1005 | ||
1006 | return sd_bus_reply_method_return(message, NULL); | |
1007 | } | |
1008 | ||
1009 | static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1010 | Manager *m = ASSERT_PTR(userdata); | |
1011 | Session *session; | |
1012 | const char *name; | |
1013 | int r; | |
1014 | ||
1015 | assert(message); | |
1016 | ||
1017 | r = sd_bus_message_read(message, "s", &name); | |
1018 | if (r < 0) | |
1019 | return r; | |
1020 | ||
1021 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
1022 | if (r < 0) | |
1023 | return r; | |
1024 | ||
1025 | /* PolicyKit is done by bus_session_method_activate() */ | |
1026 | ||
1027 | return bus_session_method_activate(message, session, error); | |
1028 | } | |
1029 | ||
1030 | static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1031 | const char *session_name, *seat_name; | |
1032 | Manager *m = ASSERT_PTR(userdata); | |
1033 | Session *session; | |
1034 | Seat *seat; | |
1035 | int r; | |
1036 | ||
1037 | assert(message); | |
1038 | ||
1039 | /* Same as ActivateSession() but refuses to work if the seat doesn't match */ | |
1040 | ||
1041 | r = sd_bus_message_read(message, "ss", &session_name, &seat_name); | |
1042 | if (r < 0) | |
1043 | return r; | |
1044 | ||
1045 | r = manager_get_session_from_creds(m, message, session_name, error, &session); | |
1046 | if (r < 0) | |
1047 | return r; | |
1048 | ||
1049 | r = manager_get_seat_from_creds(m, message, seat_name, error, &seat); | |
1050 | if (r < 0) | |
1051 | return r; | |
1052 | ||
1053 | if (session->seat != seat) | |
1054 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, | |
1055 | "Session %s not on seat %s", session_name, seat_name); | |
1056 | ||
1057 | r = check_polkit_chvt(message, m, error); | |
1058 | if (r < 0) | |
1059 | return r; | |
1060 | if (r == 0) | |
1061 | return 1; /* Will call us back */ | |
1062 | ||
1063 | r = session_activate(session); | |
1064 | if (r < 0) | |
1065 | return r; | |
1066 | ||
1067 | return sd_bus_reply_method_return(message, NULL); | |
1068 | } | |
1069 | ||
1070 | static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1071 | Manager *m = ASSERT_PTR(userdata); | |
1072 | Session *session; | |
1073 | const char *name; | |
1074 | int r; | |
1075 | ||
1076 | assert(message); | |
1077 | ||
1078 | r = sd_bus_message_read(message, "s", &name); | |
1079 | if (r < 0) | |
1080 | return r; | |
1081 | ||
1082 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
1083 | if (r < 0) | |
1084 | return r; | |
1085 | ||
1086 | return bus_session_method_lock(message, session, error); | |
1087 | } | |
1088 | ||
1089 | static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1090 | Manager *m = ASSERT_PTR(userdata); | |
1091 | int r; | |
1092 | ||
1093 | assert(message); | |
1094 | ||
1095 | r = bus_verify_polkit_async( | |
1096 | message, | |
1097 | CAP_SYS_ADMIN, | |
1098 | "org.freedesktop.login1.lock-sessions", | |
1099 | NULL, | |
1100 | false, | |
1101 | UID_INVALID, | |
1102 | &m->polkit_registry, | |
1103 | error); | |
1104 | if (r < 0) | |
1105 | return r; | |
1106 | if (r == 0) | |
1107 | return 1; /* Will call us back */ | |
1108 | ||
1109 | r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions")); | |
1110 | if (r < 0) | |
1111 | return r; | |
1112 | ||
1113 | return sd_bus_reply_method_return(message, NULL); | |
1114 | } | |
1115 | ||
1116 | static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1117 | const char *name; | |
1118 | Manager *m = ASSERT_PTR(userdata); | |
1119 | Session *session; | |
1120 | int r; | |
1121 | ||
1122 | assert(message); | |
1123 | ||
1124 | r = sd_bus_message_read(message, "s", &name); | |
1125 | if (r < 0) | |
1126 | return r; | |
1127 | ||
1128 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
1129 | if (r < 0) | |
1130 | return r; | |
1131 | ||
1132 | return bus_session_method_kill(message, session, error); | |
1133 | } | |
1134 | ||
1135 | static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1136 | Manager *m = ASSERT_PTR(userdata); | |
1137 | uint32_t uid; | |
1138 | User *user; | |
1139 | int r; | |
1140 | ||
1141 | assert(message); | |
1142 | ||
1143 | r = sd_bus_message_read(message, "u", &uid); | |
1144 | if (r < 0) | |
1145 | return r; | |
1146 | ||
1147 | r = manager_get_user_from_creds(m, message, uid, error, &user); | |
1148 | if (r < 0) | |
1149 | return r; | |
1150 | ||
1151 | return bus_user_method_kill(message, user, error); | |
1152 | } | |
1153 | ||
1154 | static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1155 | Manager *m = ASSERT_PTR(userdata); | |
1156 | const char *name; | |
1157 | Session *session; | |
1158 | int r; | |
1159 | ||
1160 | assert(message); | |
1161 | ||
1162 | r = sd_bus_message_read(message, "s", &name); | |
1163 | if (r < 0) | |
1164 | return r; | |
1165 | ||
1166 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
1167 | if (r < 0) | |
1168 | return r; | |
1169 | ||
1170 | return bus_session_method_terminate(message, session, error); | |
1171 | } | |
1172 | ||
1173 | static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1174 | Manager *m = ASSERT_PTR(userdata); | |
1175 | uint32_t uid; | |
1176 | User *user; | |
1177 | int r; | |
1178 | ||
1179 | assert(message); | |
1180 | ||
1181 | r = sd_bus_message_read(message, "u", &uid); | |
1182 | if (r < 0) | |
1183 | return r; | |
1184 | ||
1185 | r = manager_get_user_from_creds(m, message, uid, error, &user); | |
1186 | if (r < 0) | |
1187 | return r; | |
1188 | ||
1189 | return bus_user_method_terminate(message, user, error); | |
1190 | } | |
1191 | ||
1192 | static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1193 | Manager *m = ASSERT_PTR(userdata); | |
1194 | const char *name; | |
1195 | Seat *seat; | |
1196 | int r; | |
1197 | ||
1198 | assert(message); | |
1199 | ||
1200 | r = sd_bus_message_read(message, "s", &name); | |
1201 | if (r < 0) | |
1202 | return r; | |
1203 | ||
1204 | r = manager_get_seat_from_creds(m, message, name, error, &seat); | |
1205 | if (r < 0) | |
1206 | return r; | |
1207 | ||
1208 | return bus_seat_method_terminate(message, seat, error); | |
1209 | } | |
1210 | ||
1211 | static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1212 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
1213 | _cleanup_free_ char *cc = NULL; | |
1214 | Manager *m = ASSERT_PTR(userdata); | |
1215 | int r, b, interactive; | |
1216 | struct passwd *pw; | |
1217 | const char *path; | |
1218 | uint32_t uid, auth_uid; | |
1219 | ||
1220 | assert(message); | |
1221 | ||
1222 | r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive); | |
1223 | if (r < 0) | |
1224 | return r; | |
1225 | ||
1226 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID | | |
1227 | SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
1228 | if (r < 0) | |
1229 | return r; | |
1230 | ||
1231 | if (!uid_is_valid(uid)) { | |
1232 | /* Note that we get the owner UID of the session or user unit, | |
1233 | * not the actual client UID here! */ | |
1234 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
1235 | if (r < 0) | |
1236 | return r; | |
1237 | } | |
1238 | ||
1239 | /* owner_uid is racy, so for authorization we must use euid */ | |
1240 | r = sd_bus_creds_get_euid(creds, &auth_uid); | |
1241 | if (r < 0) | |
1242 | return r; | |
1243 | ||
1244 | errno = 0; | |
1245 | pw = getpwuid(uid); | |
1246 | if (!pw) | |
1247 | return errno_or_else(ENOENT); | |
1248 | ||
1249 | r = bus_verify_polkit_async( | |
1250 | message, | |
1251 | CAP_SYS_ADMIN, | |
1252 | uid == auth_uid ? "org.freedesktop.login1.set-self-linger" : | |
1253 | "org.freedesktop.login1.set-user-linger", | |
1254 | NULL, | |
1255 | interactive, | |
1256 | UID_INVALID, | |
1257 | &m->polkit_registry, | |
1258 | error); | |
1259 | if (r < 0) | |
1260 | return r; | |
1261 | if (r == 0) | |
1262 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1263 | ||
1264 | (void) mkdir_p_label("/var/lib/systemd", 0755); | |
1265 | r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE); | |
1266 | if (r < 0) | |
1267 | return r; | |
1268 | ||
1269 | cc = cescape(pw->pw_name); | |
1270 | if (!cc) | |
1271 | return -ENOMEM; | |
1272 | ||
1273 | path = strjoina("/var/lib/systemd/linger/", cc); | |
1274 | if (b) { | |
1275 | User *u; | |
1276 | ||
1277 | r = touch(path); | |
1278 | if (r < 0) | |
1279 | return r; | |
1280 | ||
1281 | if (manager_add_user_by_uid(m, uid, &u) >= 0) | |
1282 | user_start(u); | |
1283 | ||
1284 | } else { | |
1285 | User *u; | |
1286 | ||
1287 | r = unlink(path); | |
1288 | if (r < 0 && errno != ENOENT) | |
1289 | return -errno; | |
1290 | ||
1291 | u = hashmap_get(m->users, UID_TO_PTR(uid)); | |
1292 | if (u) | |
1293 | user_add_to_gc_queue(u); | |
1294 | } | |
1295 | ||
1296 | return sd_bus_reply_method_return(message, NULL); | |
1297 | } | |
1298 | ||
1299 | static int trigger_device(Manager *m, sd_device *d) { | |
1300 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
1301 | int r; | |
1302 | ||
1303 | assert(m); | |
1304 | ||
1305 | r = sd_device_enumerator_new(&e); | |
1306 | if (r < 0) | |
1307 | return r; | |
1308 | ||
1309 | r = sd_device_enumerator_allow_uninitialized(e); | |
1310 | if (r < 0) | |
1311 | return r; | |
1312 | ||
1313 | if (d) { | |
1314 | r = sd_device_enumerator_add_match_parent(e, d); | |
1315 | if (r < 0) | |
1316 | return r; | |
1317 | } | |
1318 | ||
1319 | FOREACH_DEVICE(e, d) { | |
1320 | r = sd_device_trigger(d, SD_DEVICE_CHANGE); | |
1321 | if (r < 0) | |
1322 | log_device_debug_errno(d, r, "Failed to trigger device, ignoring: %m"); | |
1323 | } | |
1324 | ||
1325 | return 0; | |
1326 | } | |
1327 | ||
1328 | static int attach_device(Manager *m, const char *seat, const char *sysfs) { | |
1329 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; | |
1330 | _cleanup_free_ char *rule = NULL, *file = NULL; | |
1331 | const char *id_for_seat; | |
1332 | int r; | |
1333 | ||
1334 | assert(m); | |
1335 | assert(seat); | |
1336 | assert(sysfs); | |
1337 | ||
1338 | r = sd_device_new_from_syspath(&d, sysfs); | |
1339 | if (r < 0) | |
1340 | return r; | |
1341 | ||
1342 | if (sd_device_has_current_tag(d, "seat") <= 0) | |
1343 | return -ENODEV; | |
1344 | ||
1345 | if (sd_device_get_property_value(d, "ID_FOR_SEAT", &id_for_seat) < 0) | |
1346 | return -ENODEV; | |
1347 | ||
1348 | if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) | |
1349 | return -ENOMEM; | |
1350 | ||
1351 | if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) | |
1352 | return -ENOMEM; | |
1353 | ||
1354 | (void) mkdir_p_label("/etc/udev/rules.d", 0755); | |
1355 | r = write_string_file_atomic_label(file, rule); | |
1356 | if (r < 0) | |
1357 | return r; | |
1358 | ||
1359 | return trigger_device(m, d); | |
1360 | } | |
1361 | ||
1362 | static int flush_devices(Manager *m) { | |
1363 | _cleanup_closedir_ DIR *d = NULL; | |
1364 | ||
1365 | assert(m); | |
1366 | ||
1367 | d = opendir("/etc/udev/rules.d"); | |
1368 | if (!d) { | |
1369 | if (errno != ENOENT) | |
1370 | log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m"); | |
1371 | } else | |
1372 | FOREACH_DIRENT_ALL(de, d, break) { | |
1373 | if (!dirent_is_file(de)) | |
1374 | continue; | |
1375 | ||
1376 | if (!startswith(de->d_name, "72-seat-")) | |
1377 | continue; | |
1378 | ||
1379 | if (!endswith(de->d_name, ".rules")) | |
1380 | continue; | |
1381 | ||
1382 | if (unlinkat(dirfd(d), de->d_name, 0) < 0) | |
1383 | log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name); | |
1384 | } | |
1385 | ||
1386 | return trigger_device(m, NULL); | |
1387 | } | |
1388 | ||
1389 | static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1390 | const char *sysfs, *seat; | |
1391 | Manager *m = ASSERT_PTR(userdata); | |
1392 | int interactive, r; | |
1393 | ||
1394 | assert(message); | |
1395 | ||
1396 | r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive); | |
1397 | if (r < 0) | |
1398 | return r; | |
1399 | ||
1400 | if (!path_is_normalized(sysfs)) | |
1401 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", sysfs); | |
1402 | if (!path_startswith(sysfs, "/sys")) | |
1403 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs); | |
1404 | ||
1405 | if (SEAT_IS_SELF(seat) || SEAT_IS_AUTO(seat)) { | |
1406 | Seat *found; | |
1407 | ||
1408 | r = manager_get_seat_from_creds(m, message, seat, error, &found); | |
1409 | if (r < 0) | |
1410 | return r; | |
1411 | ||
1412 | seat = found->id; | |
1413 | ||
1414 | } else if (!seat_name_is_valid(seat)) /* Note that a seat does not have to exist yet for this operation to succeed */ | |
1415 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat name %s is not valid", seat); | |
1416 | ||
1417 | r = bus_verify_polkit_async( | |
1418 | message, | |
1419 | CAP_SYS_ADMIN, | |
1420 | "org.freedesktop.login1.attach-device", | |
1421 | NULL, | |
1422 | interactive, | |
1423 | UID_INVALID, | |
1424 | &m->polkit_registry, | |
1425 | error); | |
1426 | if (r < 0) | |
1427 | return r; | |
1428 | if (r == 0) | |
1429 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1430 | ||
1431 | r = attach_device(m, seat, sysfs); | |
1432 | if (r < 0) | |
1433 | return r; | |
1434 | ||
1435 | return sd_bus_reply_method_return(message, NULL); | |
1436 | } | |
1437 | ||
1438 | static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1439 | Manager *m = ASSERT_PTR(userdata); | |
1440 | int interactive, r; | |
1441 | ||
1442 | assert(message); | |
1443 | ||
1444 | r = sd_bus_message_read(message, "b", &interactive); | |
1445 | if (r < 0) | |
1446 | return r; | |
1447 | ||
1448 | r = bus_verify_polkit_async( | |
1449 | message, | |
1450 | CAP_SYS_ADMIN, | |
1451 | "org.freedesktop.login1.flush-devices", | |
1452 | NULL, | |
1453 | interactive, | |
1454 | UID_INVALID, | |
1455 | &m->polkit_registry, | |
1456 | error); | |
1457 | if (r < 0) | |
1458 | return r; | |
1459 | if (r == 0) | |
1460 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1461 | ||
1462 | r = flush_devices(m); | |
1463 | if (r < 0) | |
1464 | return r; | |
1465 | ||
1466 | return sd_bus_reply_method_return(message, NULL); | |
1467 | } | |
1468 | ||
1469 | static int have_multiple_sessions( | |
1470 | Manager *m, | |
1471 | uid_t uid) { | |
1472 | ||
1473 | Session *session; | |
1474 | ||
1475 | assert(m); | |
1476 | ||
1477 | /* Check for other users' sessions. Greeter sessions do not | |
1478 | * count, and non-login sessions do not count either. */ | |
1479 | HASHMAP_FOREACH(session, m->sessions) | |
1480 | if (session->class == SESSION_USER && | |
1481 | session->user->user_record->uid != uid) | |
1482 | return true; | |
1483 | ||
1484 | return false; | |
1485 | } | |
1486 | ||
1487 | static int bus_manager_log_shutdown( | |
1488 | Manager *m, | |
1489 | const HandleActionData *a) { | |
1490 | assert(m); | |
1491 | assert(a); | |
1492 | ||
1493 | const char *message = a->message ?: "System is shutting down"; | |
1494 | const char *log_verb = a->log_verb ? strjoina("SHUTDOWN=", a->log_verb) : NULL; | |
1495 | ||
1496 | return log_struct(LOG_NOTICE, | |
1497 | "MESSAGE_ID=%s", a->message_id ?: SD_MESSAGE_SHUTDOWN_STR, | |
1498 | LOG_MESSAGE("%s%s%s%s.", | |
1499 | message, | |
1500 | m->wall_message ? " (" : "", | |
1501 | strempty(m->wall_message), | |
1502 | m->wall_message ? ")" : ""), | |
1503 | log_verb); | |
1504 | } | |
1505 | ||
1506 | static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) { | |
1507 | Manager *m = ASSERT_PTR(userdata); | |
1508 | ||
1509 | assert(e); | |
1510 | ||
1511 | m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source); | |
1512 | return 0; | |
1513 | } | |
1514 | ||
1515 | int manager_set_lid_switch_ignore(Manager *m, usec_t until) { | |
1516 | int r; | |
1517 | ||
1518 | assert(m); | |
1519 | ||
1520 | if (until <= now(CLOCK_MONOTONIC)) | |
1521 | return 0; | |
1522 | ||
1523 | /* We want to ignore the lid switch for a while after each | |
1524 | * suspend, and after boot-up. Hence let's install a timer for | |
1525 | * this. As long as the event source exists we ignore the lid | |
1526 | * switch. */ | |
1527 | ||
1528 | if (m->lid_switch_ignore_event_source) { | |
1529 | usec_t u; | |
1530 | ||
1531 | r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u); | |
1532 | if (r < 0) | |
1533 | return r; | |
1534 | ||
1535 | if (until <= u) | |
1536 | return 0; | |
1537 | ||
1538 | r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until); | |
1539 | } else | |
1540 | r = sd_event_add_time( | |
1541 | m->event, | |
1542 | &m->lid_switch_ignore_event_source, | |
1543 | CLOCK_MONOTONIC, | |
1544 | until, 0, | |
1545 | lid_switch_ignore_handler, m); | |
1546 | ||
1547 | return r; | |
1548 | } | |
1549 | ||
1550 | static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) { | |
1551 | int active = _active; | |
1552 | ||
1553 | assert(m); | |
1554 | assert(IN_SET(w, INHIBIT_SHUTDOWN, INHIBIT_SLEEP)); | |
1555 | ||
1556 | return sd_bus_emit_signal(m->bus, | |
1557 | "/org/freedesktop/login1", | |
1558 | "org.freedesktop.login1.Manager", | |
1559 | w == INHIBIT_SHUTDOWN ? "PrepareForShutdown" : "PrepareForSleep", | |
1560 | "b", | |
1561 | active); | |
1562 | } | |
1563 | ||
1564 | static int execute_shutdown_or_sleep( | |
1565 | Manager *m, | |
1566 | const HandleActionData *a, | |
1567 | sd_bus_error *error) { | |
1568 | ||
1569 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1570 | const char *p; | |
1571 | int r; | |
1572 | ||
1573 | assert(m); | |
1574 | assert(a); | |
1575 | ||
1576 | if (a->inhibit_what == INHIBIT_SHUTDOWN) | |
1577 | bus_manager_log_shutdown(m, a); | |
1578 | ||
1579 | r = bus_call_method( | |
1580 | m->bus, | |
1581 | bus_systemd_mgr, | |
1582 | "StartUnit", | |
1583 | error, | |
1584 | &reply, | |
1585 | "ss", a->target, "replace-irreversibly"); | |
1586 | if (r < 0) | |
1587 | goto error; | |
1588 | ||
1589 | r = sd_bus_message_read(reply, "o", &p); | |
1590 | if (r < 0) | |
1591 | goto error; | |
1592 | ||
1593 | r = free_and_strdup(&m->action_job, p); | |
1594 | if (r < 0) | |
1595 | goto error; | |
1596 | ||
1597 | m->delayed_action = a; | |
1598 | ||
1599 | /* Make sure the lid switch is ignored for a while */ | |
1600 | manager_set_lid_switch_ignore(m, usec_add(now(CLOCK_MONOTONIC), m->holdoff_timeout_usec)); | |
1601 | ||
1602 | return 0; | |
1603 | ||
1604 | error: | |
1605 | /* Tell people that they now may take a lock again */ | |
1606 | (void) send_prepare_for(m, a->inhibit_what, false); | |
1607 | ||
1608 | return r; | |
1609 | } | |
1610 | ||
1611 | int manager_dispatch_delayed(Manager *manager, bool timeout) { | |
1612 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1613 | Inhibitor *offending = NULL; | |
1614 | int r; | |
1615 | ||
1616 | assert(manager); | |
1617 | ||
1618 | if (!manager->delayed_action || manager->action_job) | |
1619 | return 0; | |
1620 | ||
1621 | if (manager_is_inhibited(manager, manager->delayed_action->inhibit_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) { | |
1622 | _cleanup_free_ char *comm = NULL, *u = NULL; | |
1623 | ||
1624 | if (!timeout) | |
1625 | return 0; | |
1626 | ||
1627 | (void) get_process_comm(offending->pid, &comm); | |
1628 | u = uid_to_name(offending->uid); | |
1629 | ||
1630 | log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.", | |
1631 | offending->uid, strna(u), | |
1632 | offending->pid, strna(comm)); | |
1633 | } | |
1634 | ||
1635 | /* Actually do the operation */ | |
1636 | r = execute_shutdown_or_sleep(manager, manager->delayed_action, &error); | |
1637 | if (r < 0) { | |
1638 | log_warning("Error during inhibitor-delayed operation (already returned success to client): %s", | |
1639 | bus_error_message(&error, r)); | |
1640 | ||
1641 | manager->delayed_action = NULL; | |
1642 | } | |
1643 | ||
1644 | return 1; /* We did some work. */ | |
1645 | } | |
1646 | ||
1647 | static int manager_inhibit_timeout_handler( | |
1648 | sd_event_source *s, | |
1649 | uint64_t usec, | |
1650 | void *userdata) { | |
1651 | ||
1652 | Manager *manager = ASSERT_PTR(userdata); | |
1653 | ||
1654 | assert(manager->inhibit_timeout_source == s); | |
1655 | ||
1656 | return manager_dispatch_delayed(manager, true); | |
1657 | } | |
1658 | ||
1659 | static int delay_shutdown_or_sleep( | |
1660 | Manager *m, | |
1661 | const HandleActionData *a) { | |
1662 | ||
1663 | int r; | |
1664 | ||
1665 | assert(m); | |
1666 | assert(a); | |
1667 | ||
1668 | if (m->inhibit_timeout_source) { | |
1669 | r = sd_event_source_set_time_relative(m->inhibit_timeout_source, m->inhibit_delay_max); | |
1670 | if (r < 0) | |
1671 | return log_error_errno(r, "sd_event_source_set_time_relative() failed: %m"); | |
1672 | ||
1673 | r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT); | |
1674 | if (r < 0) | |
1675 | return log_error_errno(r, "sd_event_source_set_enabled() failed: %m"); | |
1676 | } else { | |
1677 | r = sd_event_add_time_relative( | |
1678 | m->event, | |
1679 | &m->inhibit_timeout_source, | |
1680 | CLOCK_MONOTONIC, m->inhibit_delay_max, 0, | |
1681 | manager_inhibit_timeout_handler, m); | |
1682 | if (r < 0) | |
1683 | return r; | |
1684 | } | |
1685 | ||
1686 | m->delayed_action = a; | |
1687 | ||
1688 | return 0; | |
1689 | } | |
1690 | ||
1691 | int bus_manager_shutdown_or_sleep_now_or_later( | |
1692 | Manager *m, | |
1693 | const HandleActionData *a, | |
1694 | sd_bus_error *error) { | |
1695 | ||
1696 | _cleanup_free_ char *load_state = NULL; | |
1697 | bool delayed; | |
1698 | int r; | |
1699 | ||
1700 | assert(m); | |
1701 | assert(a); | |
1702 | assert(!m->action_job); | |
1703 | ||
1704 | r = unit_load_state(m->bus, a->target, &load_state); | |
1705 | if (r < 0) | |
1706 | return r; | |
1707 | ||
1708 | if (!streq(load_state, "loaded")) | |
1709 | return log_notice_errno(SYNTHETIC_ERRNO(EACCES), | |
1710 | "Unit %s is %s, refusing operation.", | |
1711 | a->target, load_state); | |
1712 | ||
1713 | /* Tell everybody to prepare for shutdown/sleep */ | |
1714 | (void) send_prepare_for(m, a->inhibit_what, true); | |
1715 | ||
1716 | delayed = | |
1717 | m->inhibit_delay_max > 0 && | |
1718 | manager_is_inhibited(m, a->inhibit_what, INHIBIT_DELAY, NULL, false, false, 0, NULL); | |
1719 | ||
1720 | if (delayed) | |
1721 | /* Shutdown is delayed, keep in mind what we | |
1722 | * want to do, and start a timeout */ | |
1723 | r = delay_shutdown_or_sleep(m, a); | |
1724 | else | |
1725 | /* Shutdown is not delayed, execute it | |
1726 | * immediately */ | |
1727 | r = execute_shutdown_or_sleep(m, a, error); | |
1728 | ||
1729 | return r; | |
1730 | } | |
1731 | ||
1732 | static int verify_shutdown_creds( | |
1733 | Manager *m, | |
1734 | sd_bus_message *message, | |
1735 | const HandleActionData *a, | |
1736 | uint64_t flags, | |
1737 | sd_bus_error *error) { | |
1738 | ||
1739 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
1740 | bool multiple_sessions, blocked, interactive; | |
1741 | uid_t uid; | |
1742 | int r; | |
1743 | ||
1744 | assert(m); | |
1745 | assert(a); | |
1746 | assert(message); | |
1747 | ||
1748 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); | |
1749 | if (r < 0) | |
1750 | return r; | |
1751 | ||
1752 | r = sd_bus_creds_get_euid(creds, &uid); | |
1753 | if (r < 0) | |
1754 | return r; | |
1755 | ||
1756 | r = have_multiple_sessions(m, uid); | |
1757 | if (r < 0) | |
1758 | return r; | |
1759 | ||
1760 | multiple_sessions = r > 0; | |
1761 | blocked = manager_is_inhibited(m, a->inhibit_what, INHIBIT_BLOCK, NULL, false, true, uid, NULL); | |
1762 | interactive = flags & SD_LOGIND_INTERACTIVE; | |
1763 | ||
1764 | if (multiple_sessions) { | |
1765 | r = bus_verify_polkit_async( | |
1766 | message, | |
1767 | CAP_SYS_BOOT, | |
1768 | a->polkit_action_multiple_sessions, | |
1769 | NULL, | |
1770 | interactive, | |
1771 | UID_INVALID, | |
1772 | &m->polkit_registry, | |
1773 | error); | |
1774 | if (r < 0) | |
1775 | return r; | |
1776 | if (r == 0) | |
1777 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1778 | } | |
1779 | ||
1780 | if (blocked) { | |
1781 | /* We don't check polkit for root here, because you can't be more privileged than root */ | |
1782 | if (uid == 0 && (flags & SD_LOGIND_ROOT_CHECK_INHIBITORS)) | |
1783 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, | |
1784 | "Access denied to root due to active block inhibitor"); | |
1785 | ||
1786 | r = bus_verify_polkit_async(message, | |
1787 | CAP_SYS_BOOT, | |
1788 | a->polkit_action_ignore_inhibit, | |
1789 | NULL, | |
1790 | interactive, | |
1791 | UID_INVALID, | |
1792 | &m->polkit_registry, | |
1793 | error); | |
1794 | if (r < 0) | |
1795 | return r; | |
1796 | if (r == 0) | |
1797 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1798 | } | |
1799 | ||
1800 | if (!multiple_sessions && !blocked) { | |
1801 | r = bus_verify_polkit_async(message, | |
1802 | CAP_SYS_BOOT, | |
1803 | a->polkit_action, | |
1804 | NULL, | |
1805 | interactive, | |
1806 | UID_INVALID, | |
1807 | &m->polkit_registry, | |
1808 | error); | |
1809 | if (r < 0) | |
1810 | return r; | |
1811 | if (r == 0) | |
1812 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1813 | } | |
1814 | ||
1815 | return 0; | |
1816 | } | |
1817 | ||
1818 | static int setup_wall_message_timer(Manager *m, sd_bus_message* message) { | |
1819 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
1820 | int r; | |
1821 | ||
1822 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); | |
1823 | if (r >= 0) { | |
1824 | const char *tty = NULL; | |
1825 | ||
1826 | (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid); | |
1827 | (void) sd_bus_creds_get_tty(creds, &tty); | |
1828 | ||
1829 | r = free_and_strdup(&m->scheduled_shutdown_tty, tty); | |
1830 | if (r < 0) | |
1831 | return log_oom(); | |
1832 | } | |
1833 | ||
1834 | r = manager_setup_wall_message_timer(m); | |
1835 | if (r < 0) | |
1836 | return r; | |
1837 | ||
1838 | return 0; | |
1839 | } | |
1840 | ||
1841 | static int method_do_shutdown_or_sleep( | |
1842 | Manager *m, | |
1843 | sd_bus_message *message, | |
1844 | const HandleActionData *a, | |
1845 | bool with_flags, | |
1846 | sd_bus_error *error) { | |
1847 | ||
1848 | uint64_t flags; | |
1849 | int r; | |
1850 | ||
1851 | assert(m); | |
1852 | assert(message); | |
1853 | assert(a); | |
1854 | ||
1855 | if (with_flags) { | |
1856 | /* New style method: with flags parameter (and interactive bool in the bus message header) */ | |
1857 | r = sd_bus_message_read(message, "t", &flags); | |
1858 | if (r < 0) | |
1859 | return r; | |
1860 | if ((flags & ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC) != 0) | |
1861 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, | |
1862 | "Invalid flags parameter"); | |
1863 | if (a->handle != HANDLE_REBOOT && (flags & SD_LOGIND_REBOOT_VIA_KEXEC)) | |
1864 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, | |
1865 | "Reboot via kexec is only applicable with reboot operations"); | |
1866 | } else { | |
1867 | /* Old style method: no flags parameter, but interactive bool passed as boolean in | |
1868 | * payload. Let's convert this argument to the new-style flags parameter for our internal | |
1869 | * use. */ | |
1870 | int interactive; | |
1871 | ||
1872 | r = sd_bus_message_read(message, "b", &interactive); | |
1873 | if (r < 0) | |
1874 | return r; | |
1875 | ||
1876 | flags = interactive ? SD_LOGIND_INTERACTIVE : 0; | |
1877 | } | |
1878 | ||
1879 | if ((flags & SD_LOGIND_REBOOT_VIA_KEXEC) && kexec_loaded()) | |
1880 | a = handle_action_lookup(HANDLE_KEXEC); | |
1881 | ||
1882 | /* Don't allow multiple jobs being executed at the same time */ | |
1883 | if (m->delayed_action) | |
1884 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, | |
1885 | "There's already a shutdown or sleep operation in progress"); | |
1886 | ||
1887 | if (a->sleep_operation >= 0) { | |
1888 | r = can_sleep(a->sleep_operation); | |
1889 | if (r == -ENOSPC) | |
1890 | return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, | |
1891 | "Not enough swap space for hibernation"); | |
1892 | if (r == 0) | |
1893 | return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, | |
1894 | "Sleep verb \"%s\" not supported", | |
1895 | sleep_operation_to_string(a->sleep_operation)); | |
1896 | if (r < 0) | |
1897 | return r; | |
1898 | } | |
1899 | ||
1900 | r = verify_shutdown_creds(m, message, a, flags, error); | |
1901 | if (r != 0) | |
1902 | return r; | |
1903 | ||
1904 | /* reset case we're shorting a scheduled shutdown */ | |
1905 | m->unlink_nologin = false; | |
1906 | reset_scheduled_shutdown(m); | |
1907 | ||
1908 | m->scheduled_shutdown_timeout = 0; | |
1909 | m->scheduled_shutdown_action = a; | |
1910 | ||
1911 | (void) setup_wall_message_timer(m, message); | |
1912 | ||
1913 | r = bus_manager_shutdown_or_sleep_now_or_later(m, a, error); | |
1914 | if (r < 0) | |
1915 | return r; | |
1916 | ||
1917 | return sd_bus_reply_method_return(message, NULL); | |
1918 | } | |
1919 | ||
1920 | static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1921 | Manager *m = userdata; | |
1922 | ||
1923 | return method_do_shutdown_or_sleep( | |
1924 | m, message, | |
1925 | handle_action_lookup(HANDLE_POWEROFF), | |
1926 | sd_bus_message_is_method_call(message, NULL, "PowerOffWithFlags"), | |
1927 | error); | |
1928 | } | |
1929 | ||
1930 | static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1931 | Manager *m = userdata; | |
1932 | ||
1933 | return method_do_shutdown_or_sleep( | |
1934 | m, message, | |
1935 | handle_action_lookup(HANDLE_REBOOT), | |
1936 | sd_bus_message_is_method_call(message, NULL, "RebootWithFlags"), | |
1937 | error); | |
1938 | } | |
1939 | ||
1940 | static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1941 | Manager *m = userdata; | |
1942 | ||
1943 | return method_do_shutdown_or_sleep( | |
1944 | m, message, | |
1945 | handle_action_lookup(HANDLE_HALT), | |
1946 | sd_bus_message_is_method_call(message, NULL, "HaltWithFlags"), | |
1947 | error); | |
1948 | } | |
1949 | ||
1950 | static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1951 | Manager *m = userdata; | |
1952 | ||
1953 | return method_do_shutdown_or_sleep( | |
1954 | m, message, | |
1955 | handle_action_lookup(HANDLE_SUSPEND), | |
1956 | sd_bus_message_is_method_call(message, NULL, "SuspendWithFlags"), | |
1957 | error); | |
1958 | } | |
1959 | ||
1960 | static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1961 | Manager *m = userdata; | |
1962 | ||
1963 | return method_do_shutdown_or_sleep( | |
1964 | m, message, | |
1965 | handle_action_lookup(HANDLE_HIBERNATE), | |
1966 | sd_bus_message_is_method_call(message, NULL, "HibernateWithFlags"), | |
1967 | error); | |
1968 | } | |
1969 | ||
1970 | static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1971 | Manager *m = userdata; | |
1972 | ||
1973 | return method_do_shutdown_or_sleep( | |
1974 | m, message, | |
1975 | handle_action_lookup(HANDLE_HYBRID_SLEEP), | |
1976 | sd_bus_message_is_method_call(message, NULL, "HybridSleepWithFlags"), | |
1977 | error); | |
1978 | } | |
1979 | ||
1980 | static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1981 | Manager *m = userdata; | |
1982 | ||
1983 | return method_do_shutdown_or_sleep( | |
1984 | m, message, | |
1985 | handle_action_lookup(HANDLE_SUSPEND_THEN_HIBERNATE), | |
1986 | sd_bus_message_is_method_call(message, NULL, "SuspendThenHibernateWithFlags"), | |
1987 | error); | |
1988 | } | |
1989 | ||
1990 | static int nologin_timeout_handler( | |
1991 | sd_event_source *s, | |
1992 | uint64_t usec, | |
1993 | void *userdata) { | |
1994 | ||
1995 | Manager *m = userdata; | |
1996 | ||
1997 | log_info("Creating /run/nologin, blocking further logins..."); | |
1998 | ||
1999 | m->unlink_nologin = | |
2000 | create_shutdown_run_nologin_or_warn() >= 0; | |
2001 | ||
2002 | return 0; | |
2003 | } | |
2004 | ||
2005 | static usec_t nologin_timeout_usec(usec_t elapse) { | |
2006 | /* Issue /run/nologin five minutes before shutdown */ | |
2007 | return LESS_BY(elapse, 5 * USEC_PER_MINUTE); | |
2008 | } | |
2009 | ||
2010 | void manager_load_scheduled_shutdown(Manager *m) { | |
2011 | _cleanup_fclose_ FILE *f = NULL; | |
2012 | _cleanup_free_ char *usec = NULL, | |
2013 | *warn_wall = NULL, | |
2014 | *mode = NULL, | |
2015 | *wall_message = NULL, | |
2016 | *uid = NULL, | |
2017 | *tty = NULL; | |
2018 | int r; | |
2019 | ||
2020 | assert(m); | |
2021 | ||
2022 | r = parse_env_file(f, SHUTDOWN_SCHEDULE_FILE, | |
2023 | "USEC", &usec, | |
2024 | "WARN_WALL", &warn_wall, | |
2025 | "MODE", &mode, | |
2026 | "WALL_MESSAGE", &wall_message, | |
2027 | "UID", &uid, | |
2028 | "TTY", &tty); | |
2029 | ||
2030 | /* reset will delete the file */ | |
2031 | reset_scheduled_shutdown(m); | |
2032 | ||
2033 | if (r == -ENOENT) | |
2034 | return; | |
2035 | if (r < 0) | |
2036 | return (void) log_debug_errno(r, "Failed to parse " SHUTDOWN_SCHEDULE_FILE ": %m"); | |
2037 | ||
2038 | HandleAction handle = handle_action_from_string(mode); | |
2039 | if (handle < 0) | |
2040 | return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse scheduled shutdown type: %s", mode); | |
2041 | ||
2042 | if (!usec) | |
2043 | return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "USEC is required"); | |
2044 | if (deserialize_usec(usec, &m->scheduled_shutdown_timeout) < 0) | |
2045 | return; | |
2046 | ||
2047 | /* assign parsed type only after we know usec is also valid */ | |
2048 | m->scheduled_shutdown_action = handle_action_lookup(handle); | |
2049 | ||
2050 | if (warn_wall) { | |
2051 | r = parse_boolean(warn_wall); | |
2052 | if (r < 0) | |
2053 | log_debug_errno(r, "Failed to parse enabling wall messages"); | |
2054 | else | |
2055 | m->enable_wall_messages = r; | |
2056 | } | |
2057 | ||
2058 | if (wall_message) { | |
2059 | _cleanup_free_ char *unescaped = NULL; | |
2060 | r = cunescape(wall_message, 0, &unescaped); | |
2061 | if (r < 0) | |
2062 | log_debug_errno(r, "Failed to parse wall message: %s", wall_message); | |
2063 | else | |
2064 | free_and_replace(m->wall_message, unescaped); | |
2065 | } | |
2066 | ||
2067 | if (uid) { | |
2068 | r = parse_uid(uid, &m->scheduled_shutdown_uid); | |
2069 | if (r < 0) | |
2070 | log_debug_errno(r, "Failed to parse wall uid: %s", uid); | |
2071 | } | |
2072 | ||
2073 | free_and_replace(m->scheduled_shutdown_tty, tty); | |
2074 | ||
2075 | r = manager_setup_shutdown_timers(m); | |
2076 | if (r < 0) | |
2077 | return reset_scheduled_shutdown(m); | |
2078 | ||
2079 | (void) manager_setup_wall_message_timer(m); | |
2080 | (void) update_schedule_file(m); | |
2081 | ||
2082 | return; | |
2083 | } | |
2084 | ||
2085 | static int update_schedule_file(Manager *m) { | |
2086 | _cleanup_free_ char *temp_path = NULL; | |
2087 | _cleanup_fclose_ FILE *f = NULL; | |
2088 | int r; | |
2089 | ||
2090 | assert(m); | |
2091 | assert(m->scheduled_shutdown_action); | |
2092 | ||
2093 | r = mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE, 0755); | |
2094 | if (r < 0) | |
2095 | return log_error_errno(r, "Failed to create shutdown subdirectory: %m"); | |
2096 | ||
2097 | r = fopen_temporary(SHUTDOWN_SCHEDULE_FILE, &f, &temp_path); | |
2098 | if (r < 0) | |
2099 | return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m"); | |
2100 | ||
2101 | (void) fchmod(fileno(f), 0644); | |
2102 | ||
2103 | serialize_usec(f, "USEC", m->scheduled_shutdown_timeout); | |
2104 | serialize_item_format(f, "WARN_WALL", "%s", one_zero(m->enable_wall_messages)); | |
2105 | serialize_item_format(f, "MODE", "%s", handle_action_to_string(m->scheduled_shutdown_action->handle)); | |
2106 | serialize_item_format(f, "UID", UID_FMT, m->scheduled_shutdown_uid); | |
2107 | ||
2108 | if (m->scheduled_shutdown_tty) | |
2109 | serialize_item_format(f, "TTY", "%s", m->scheduled_shutdown_tty); | |
2110 | ||
2111 | if (!isempty(m->wall_message)) { | |
2112 | r = serialize_item_escaped(f, "WALL_MESSAGE", m->wall_message); | |
2113 | if (r < 0) | |
2114 | goto fail; | |
2115 | } | |
2116 | ||
2117 | r = fflush_and_check(f); | |
2118 | if (r < 0) | |
2119 | goto fail; | |
2120 | ||
2121 | if (rename(temp_path, SHUTDOWN_SCHEDULE_FILE) < 0) { | |
2122 | r = -errno; | |
2123 | goto fail; | |
2124 | } | |
2125 | ||
2126 | return 0; | |
2127 | ||
2128 | fail: | |
2129 | (void) unlink(temp_path); | |
2130 | (void) unlink(SHUTDOWN_SCHEDULE_FILE); | |
2131 | ||
2132 | return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m"); | |
2133 | } | |
2134 | ||
2135 | static void reset_scheduled_shutdown(Manager *m) { | |
2136 | assert(m); | |
2137 | ||
2138 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); | |
2139 | m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source); | |
2140 | m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source); | |
2141 | ||
2142 | m->scheduled_shutdown_action = NULL; | |
2143 | m->scheduled_shutdown_timeout = USEC_INFINITY; | |
2144 | m->scheduled_shutdown_uid = UID_INVALID; | |
2145 | m->scheduled_shutdown_tty = mfree(m->scheduled_shutdown_tty); | |
2146 | m->wall_message = mfree(m->wall_message); | |
2147 | m->shutdown_dry_run = false; | |
2148 | ||
2149 | if (m->unlink_nologin) { | |
2150 | (void) unlink_or_warn("/run/nologin"); | |
2151 | m->unlink_nologin = false; | |
2152 | } | |
2153 | ||
2154 | (void) unlink(SHUTDOWN_SCHEDULE_FILE); | |
2155 | } | |
2156 | ||
2157 | static int manager_scheduled_shutdown_handler( | |
2158 | sd_event_source *s, | |
2159 | uint64_t usec, | |
2160 | void *userdata) { | |
2161 | ||
2162 | const HandleActionData *a = NULL; | |
2163 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2164 | Manager *m = ASSERT_PTR(userdata); | |
2165 | int r; | |
2166 | ||
2167 | a = m->scheduled_shutdown_action; | |
2168 | assert(a); | |
2169 | ||
2170 | /* Don't allow multiple jobs being executed at the same time */ | |
2171 | if (m->delayed_action) { | |
2172 | r = -EALREADY; | |
2173 | log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", a->target); | |
2174 | goto error; | |
2175 | } | |
2176 | ||
2177 | if (m->shutdown_dry_run) { | |
2178 | /* We do not process delay inhibitors here. Otherwise, we | |
2179 | * would have to be considered "in progress" (like the check | |
2180 | * above) for some seconds after our admin has seen the final | |
2181 | * wall message. */ | |
2182 | ||
2183 | bus_manager_log_shutdown(m, a); | |
2184 | log_info("Running in dry run, suppressing action."); | |
2185 | reset_scheduled_shutdown(m); | |
2186 | ||
2187 | return 0; | |
2188 | } | |
2189 | ||
2190 | r = bus_manager_shutdown_or_sleep_now_or_later(m, m->scheduled_shutdown_action, &error); | |
2191 | if (r < 0) { | |
2192 | log_error_errno(r, "Scheduled shutdown to %s failed: %m", a->target); | |
2193 | goto error; | |
2194 | } | |
2195 | ||
2196 | return 0; | |
2197 | ||
2198 | error: | |
2199 | reset_scheduled_shutdown(m); | |
2200 | return r; | |
2201 | } | |
2202 | ||
2203 | static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2204 | Manager *m = ASSERT_PTR(userdata); | |
2205 | HandleAction handle; | |
2206 | const HandleActionData *a; | |
2207 | uint64_t elapse; | |
2208 | char *type; | |
2209 | int r; | |
2210 | bool dry_run = false; | |
2211 | ||
2212 | assert(message); | |
2213 | ||
2214 | r = sd_bus_message_read(message, "st", &type, &elapse); | |
2215 | if (r < 0) | |
2216 | return r; | |
2217 | ||
2218 | if (startswith(type, "dry-")) { | |
2219 | type += 4; | |
2220 | dry_run = true; | |
2221 | } | |
2222 | ||
2223 | handle = handle_action_from_string(type); | |
2224 | if (!IN_SET(handle, HANDLE_POWEROFF, HANDLE_REBOOT, HANDLE_HALT, HANDLE_KEXEC)) | |
2225 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type"); | |
2226 | ||
2227 | a = handle_action_lookup(handle); | |
2228 | assert(a); | |
2229 | assert(a->polkit_action); | |
2230 | ||
2231 | r = verify_shutdown_creds(m, message, a, 0, error); | |
2232 | if (r != 0) | |
2233 | return r; | |
2234 | ||
2235 | m->scheduled_shutdown_action = a; | |
2236 | m->shutdown_dry_run = dry_run; | |
2237 | m->scheduled_shutdown_timeout = elapse; | |
2238 | ||
2239 | r = manager_setup_shutdown_timers(m); | |
2240 | if (r < 0) | |
2241 | return r; | |
2242 | ||
2243 | r = setup_wall_message_timer(m, message); | |
2244 | if (r >= 0) | |
2245 | r = update_schedule_file(m); | |
2246 | ||
2247 | if (r < 0) { | |
2248 | reset_scheduled_shutdown(m); | |
2249 | return r; | |
2250 | } | |
2251 | ||
2252 | return sd_bus_reply_method_return(message, NULL); | |
2253 | } | |
2254 | ||
2255 | static int manager_setup_shutdown_timers(Manager* m) { | |
2256 | int r; | |
2257 | ||
2258 | r = event_reset_time(m->event, &m->scheduled_shutdown_timeout_source, | |
2259 | CLOCK_REALTIME, | |
2260 | m->scheduled_shutdown_timeout, 0, | |
2261 | manager_scheduled_shutdown_handler, m, | |
2262 | 0, "scheduled-shutdown-timeout", true); | |
2263 | if (r < 0) | |
2264 | goto fail; | |
2265 | ||
2266 | r = event_reset_time(m->event, &m->nologin_timeout_source, | |
2267 | CLOCK_REALTIME, | |
2268 | nologin_timeout_usec(m->scheduled_shutdown_timeout), 0, | |
2269 | nologin_timeout_handler, m, | |
2270 | 0, "nologin-timeout", true); | |
2271 | if (r < 0) | |
2272 | goto fail; | |
2273 | ||
2274 | return 0; | |
2275 | ||
2276 | fail: | |
2277 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); | |
2278 | m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source); | |
2279 | ||
2280 | return r; | |
2281 | } | |
2282 | ||
2283 | static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2284 | Manager *m = ASSERT_PTR(userdata); | |
2285 | const HandleActionData *a; | |
2286 | bool cancelled; | |
2287 | int r; | |
2288 | ||
2289 | assert(message); | |
2290 | ||
2291 | cancelled = m->scheduled_shutdown_action | |
2292 | && !IN_SET(m->scheduled_shutdown_action->handle, HANDLE_IGNORE, _HANDLE_ACTION_INVALID); | |
2293 | if (!cancelled) | |
2294 | return sd_bus_reply_method_return(message, "b", false); | |
2295 | ||
2296 | a = m->scheduled_shutdown_action; | |
2297 | if (!a->polkit_action) | |
2298 | return sd_bus_error_set(error, SD_BUS_ERROR_AUTH_FAILED, "Unsupported shutdown type"); | |
2299 | ||
2300 | r = bus_verify_polkit_async( | |
2301 | message, | |
2302 | CAP_SYS_BOOT, | |
2303 | a->polkit_action, | |
2304 | NULL, | |
2305 | false, | |
2306 | UID_INVALID, | |
2307 | &m->polkit_registry, | |
2308 | error); | |
2309 | if (r < 0) | |
2310 | return r; | |
2311 | if (r == 0) | |
2312 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2313 | ||
2314 | if (m->enable_wall_messages) { | |
2315 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
2316 | const char *tty = NULL; | |
2317 | uid_t uid = 0; | |
2318 | ||
2319 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); | |
2320 | if (r >= 0) { | |
2321 | (void) sd_bus_creds_get_uid(creds, &uid); | |
2322 | (void) sd_bus_creds_get_tty(creds, &tty); | |
2323 | } | |
2324 | ||
2325 | _cleanup_free_ char *username = uid_to_name(uid); | |
2326 | ||
2327 | log_struct(LOG_INFO, | |
2328 | LOG_MESSAGE("System shutdown has been cancelled"), | |
2329 | "ACTION=%s", handle_action_to_string(a->handle), | |
2330 | "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_CANCELED_STR, | |
2331 | username ? "OPERATOR=%s" : NULL, username); | |
2332 | ||
2333 | utmp_wall("System shutdown has been cancelled", | |
2334 | username, tty, logind_wall_tty_filter, m); | |
2335 | } | |
2336 | ||
2337 | reset_scheduled_shutdown(m); | |
2338 | ||
2339 | return sd_bus_reply_method_return(message, "b", true); | |
2340 | } | |
2341 | ||
2342 | static int method_can_shutdown_or_sleep( | |
2343 | Manager *m, | |
2344 | sd_bus_message *message, | |
2345 | const HandleActionData *a, | |
2346 | sd_bus_error *error) { | |
2347 | ||
2348 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
2349 | bool multiple_sessions, challenge, blocked; | |
2350 | const char *result = NULL; | |
2351 | uid_t uid; | |
2352 | int r; | |
2353 | ||
2354 | assert(m); | |
2355 | assert(message); | |
2356 | assert(a); | |
2357 | ||
2358 | if (a->sleep_operation >= 0) { | |
2359 | r = can_sleep(a->sleep_operation); | |
2360 | if (IN_SET(r, 0, -ENOSPC)) | |
2361 | return sd_bus_reply_method_return(message, "s", "na"); | |
2362 | if (r < 0) | |
2363 | return r; | |
2364 | } | |
2365 | ||
2366 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); | |
2367 | if (r < 0) | |
2368 | return r; | |
2369 | ||
2370 | r = sd_bus_creds_get_euid(creds, &uid); | |
2371 | if (r < 0) | |
2372 | return r; | |
2373 | ||
2374 | r = have_multiple_sessions(m, uid); | |
2375 | if (r < 0) | |
2376 | return r; | |
2377 | ||
2378 | multiple_sessions = r > 0; | |
2379 | blocked = manager_is_inhibited(m, a->inhibit_what, INHIBIT_BLOCK, NULL, false, true, uid, NULL); | |
2380 | ||
2381 | HandleAction handle = handle_action_from_string(sleep_operation_to_string(a->sleep_operation)); | |
2382 | if (handle >= 0) { | |
2383 | const char *target; | |
2384 | ||
2385 | target = handle_action_lookup(handle)->target; | |
2386 | if (target) { | |
2387 | _cleanup_free_ char *load_state = NULL; | |
2388 | ||
2389 | r = unit_load_state(m->bus, target, &load_state); | |
2390 | if (r < 0) | |
2391 | return r; | |
2392 | ||
2393 | if (!streq(load_state, "loaded")) { | |
2394 | result = "no"; | |
2395 | goto finish; | |
2396 | } | |
2397 | } | |
2398 | } | |
2399 | ||
2400 | if (multiple_sessions) { | |
2401 | r = bus_test_polkit(message, CAP_SYS_BOOT, a->polkit_action_multiple_sessions, NULL, UID_INVALID, &challenge, error); | |
2402 | if (r < 0) | |
2403 | return r; | |
2404 | ||
2405 | if (r > 0) | |
2406 | result = "yes"; | |
2407 | else if (challenge) | |
2408 | result = "challenge"; | |
2409 | else | |
2410 | result = "no"; | |
2411 | } | |
2412 | ||
2413 | if (blocked) { | |
2414 | r = bus_test_polkit(message, CAP_SYS_BOOT, a->polkit_action_ignore_inhibit, NULL, UID_INVALID, &challenge, error); | |
2415 | if (r < 0) | |
2416 | return r; | |
2417 | ||
2418 | if (r > 0) { | |
2419 | if (!result) | |
2420 | result = "yes"; | |
2421 | } else if (challenge) { | |
2422 | if (!result || streq(result, "yes")) | |
2423 | result = "challenge"; | |
2424 | } else | |
2425 | result = "no"; | |
2426 | } | |
2427 | ||
2428 | if (!multiple_sessions && !blocked) { | |
2429 | /* If neither inhibit nor multiple sessions | |
2430 | * apply then just check the normal policy */ | |
2431 | ||
2432 | r = bus_test_polkit(message, CAP_SYS_BOOT, a->polkit_action, NULL, UID_INVALID, &challenge, error); | |
2433 | if (r < 0) | |
2434 | return r; | |
2435 | ||
2436 | if (r > 0) | |
2437 | result = "yes"; | |
2438 | else if (challenge) | |
2439 | result = "challenge"; | |
2440 | else | |
2441 | result = "no"; | |
2442 | } | |
2443 | ||
2444 | finish: | |
2445 | return sd_bus_reply_method_return(message, "s", result); | |
2446 | } | |
2447 | ||
2448 | static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2449 | Manager *m = userdata; | |
2450 | ||
2451 | return method_can_shutdown_or_sleep( | |
2452 | m, message, handle_action_lookup(HANDLE_POWEROFF), | |
2453 | error); | |
2454 | } | |
2455 | ||
2456 | static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2457 | Manager *m = userdata; | |
2458 | ||
2459 | return method_can_shutdown_or_sleep( | |
2460 | m, message, handle_action_lookup(HANDLE_REBOOT), | |
2461 | error); | |
2462 | } | |
2463 | ||
2464 | static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2465 | Manager *m = userdata; | |
2466 | ||
2467 | return method_can_shutdown_or_sleep( | |
2468 | m, message, handle_action_lookup(HANDLE_HALT), | |
2469 | error); | |
2470 | } | |
2471 | ||
2472 | static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2473 | Manager *m = userdata; | |
2474 | ||
2475 | return method_can_shutdown_or_sleep( | |
2476 | m, message, handle_action_lookup(HANDLE_SUSPEND), | |
2477 | error); | |
2478 | } | |
2479 | ||
2480 | static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2481 | Manager *m = userdata; | |
2482 | ||
2483 | return method_can_shutdown_or_sleep( | |
2484 | m, message, handle_action_lookup(HANDLE_HIBERNATE), | |
2485 | error); | |
2486 | } | |
2487 | ||
2488 | static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2489 | Manager *m = userdata; | |
2490 | ||
2491 | return method_can_shutdown_or_sleep( | |
2492 | m, message, handle_action_lookup(HANDLE_HYBRID_SLEEP), | |
2493 | error); | |
2494 | } | |
2495 | ||
2496 | static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2497 | Manager *m = userdata; | |
2498 | ||
2499 | return method_can_shutdown_or_sleep( | |
2500 | m, message, handle_action_lookup(HANDLE_SUSPEND_THEN_HIBERNATE), | |
2501 | error); | |
2502 | } | |
2503 | ||
2504 | static int property_get_reboot_parameter( | |
2505 | sd_bus *bus, | |
2506 | const char *path, | |
2507 | const char *interface, | |
2508 | const char *property, | |
2509 | sd_bus_message *reply, | |
2510 | void *userdata, | |
2511 | sd_bus_error *error) { | |
2512 | _cleanup_free_ char *parameter = NULL; | |
2513 | int r; | |
2514 | ||
2515 | assert(bus); | |
2516 | assert(reply); | |
2517 | assert(userdata); | |
2518 | ||
2519 | r = read_reboot_parameter(¶meter); | |
2520 | if (r < 0) | |
2521 | return r; | |
2522 | ||
2523 | return sd_bus_message_append(reply, "s", parameter); | |
2524 | } | |
2525 | ||
2526 | static int method_set_reboot_parameter( | |
2527 | sd_bus_message *message, | |
2528 | void *userdata, | |
2529 | sd_bus_error *error) { | |
2530 | ||
2531 | Manager *m = ASSERT_PTR(userdata); | |
2532 | const char *arg; | |
2533 | int r; | |
2534 | ||
2535 | assert(message); | |
2536 | ||
2537 | r = sd_bus_message_read(message, "s", &arg); | |
2538 | if (r < 0) | |
2539 | return r; | |
2540 | ||
2541 | r = detect_container(); | |
2542 | if (r < 0) | |
2543 | return r; | |
2544 | if (r > 0) | |
2545 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, | |
2546 | "Reboot parameter not supported in containers, refusing."); | |
2547 | ||
2548 | r = bus_verify_polkit_async(message, | |
2549 | CAP_SYS_ADMIN, | |
2550 | "org.freedesktop.login1.set-reboot-parameter", | |
2551 | NULL, | |
2552 | false, | |
2553 | UID_INVALID, | |
2554 | &m->polkit_registry, | |
2555 | error); | |
2556 | if (r < 0) | |
2557 | return r; | |
2558 | if (r == 0) | |
2559 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2560 | ||
2561 | r = update_reboot_parameter_and_warn(arg, false); | |
2562 | if (r < 0) | |
2563 | return r; | |
2564 | ||
2565 | return sd_bus_reply_method_return(message, NULL); | |
2566 | } | |
2567 | ||
2568 | static int method_can_reboot_parameter( | |
2569 | sd_bus_message *message, | |
2570 | void *userdata, | |
2571 | sd_bus_error *error) { | |
2572 | ||
2573 | _unused_ Manager *m = ASSERT_PTR(userdata); | |
2574 | int r; | |
2575 | ||
2576 | assert(message); | |
2577 | ||
2578 | r = detect_container(); | |
2579 | if (r < 0) | |
2580 | return r; | |
2581 | if (r > 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */ | |
2582 | return sd_bus_reply_method_return(message, "s", "na"); | |
2583 | ||
2584 | return return_test_polkit( | |
2585 | message, | |
2586 | CAP_SYS_ADMIN, | |
2587 | "org.freedesktop.login1.set-reboot-parameter", | |
2588 | NULL, | |
2589 | UID_INVALID, | |
2590 | error); | |
2591 | } | |
2592 | ||
2593 | static int property_get_reboot_to_firmware_setup( | |
2594 | sd_bus *bus, | |
2595 | const char *path, | |
2596 | const char *interface, | |
2597 | const char *property, | |
2598 | sd_bus_message *reply, | |
2599 | void *userdata, | |
2600 | sd_bus_error *error) { | |
2601 | int r; | |
2602 | ||
2603 | assert(bus); | |
2604 | assert(reply); | |
2605 | assert(userdata); | |
2606 | ||
2607 | r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); | |
2608 | if (r == -ENXIO) { | |
2609 | /* EFI case: let's see what is currently configured in the EFI variables */ | |
2610 | r = efi_get_reboot_to_firmware(); | |
2611 | if (r < 0 && r != -EOPNOTSUPP) | |
2612 | log_warning_errno(r, "Failed to determine reboot-to-firmware-setup state: %m"); | |
2613 | } else if (r < 0) | |
2614 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); | |
2615 | else if (r > 0) { | |
2616 | /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */ | |
2617 | if (access("/run/systemd/reboot-to-firmware-setup", F_OK) < 0) { | |
2618 | if (errno != ENOENT) | |
2619 | log_warning_errno(errno, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m"); | |
2620 | ||
2621 | r = false; | |
2622 | } else | |
2623 | r = true; | |
2624 | } | |
2625 | ||
2626 | return sd_bus_message_append(reply, "b", r > 0); | |
2627 | } | |
2628 | ||
2629 | static int method_set_reboot_to_firmware_setup( | |
2630 | sd_bus_message *message, | |
2631 | void *userdata, | |
2632 | sd_bus_error *error) { | |
2633 | ||
2634 | Manager *m = ASSERT_PTR(userdata); | |
2635 | bool use_efi; | |
2636 | int b, r; | |
2637 | ||
2638 | assert(message); | |
2639 | ||
2640 | r = sd_bus_message_read(message, "b", &b); | |
2641 | if (r < 0) | |
2642 | return r; | |
2643 | ||
2644 | r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); | |
2645 | if (r == -ENXIO) { | |
2646 | /* EFI case: let's see what the firmware supports */ | |
2647 | ||
2648 | r = efi_reboot_to_firmware_supported(); | |
2649 | if (r == -EOPNOTSUPP) | |
2650 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware."); | |
2651 | if (r < 0) | |
2652 | return r; | |
2653 | ||
2654 | use_efi = true; | |
2655 | ||
2656 | } else if (r <= 0) { | |
2657 | /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */ | |
2658 | ||
2659 | if (r < 0) | |
2660 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); | |
2661 | ||
2662 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware."); | |
2663 | } else | |
2664 | /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */ | |
2665 | use_efi = false; | |
2666 | ||
2667 | r = bus_verify_polkit_async(message, | |
2668 | CAP_SYS_ADMIN, | |
2669 | "org.freedesktop.login1.set-reboot-to-firmware-setup", | |
2670 | NULL, | |
2671 | false, | |
2672 | UID_INVALID, | |
2673 | &m->polkit_registry, | |
2674 | error); | |
2675 | if (r < 0) | |
2676 | return r; | |
2677 | if (r == 0) | |
2678 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2679 | ||
2680 | if (use_efi) { | |
2681 | r = efi_set_reboot_to_firmware(b); | |
2682 | if (r < 0) | |
2683 | return r; | |
2684 | } else { | |
2685 | if (b) { | |
2686 | r = touch("/run/systemd/reboot-to-firmware-setup"); | |
2687 | if (r < 0) | |
2688 | return r; | |
2689 | } else { | |
2690 | if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno != ENOENT) | |
2691 | return -errno; | |
2692 | } | |
2693 | } | |
2694 | ||
2695 | return sd_bus_reply_method_return(message, NULL); | |
2696 | } | |
2697 | ||
2698 | static int method_can_reboot_to_firmware_setup( | |
2699 | sd_bus_message *message, | |
2700 | void *userdata, | |
2701 | sd_bus_error *error) { | |
2702 | ||
2703 | _unused_ Manager *m = ASSERT_PTR(userdata); | |
2704 | int r; | |
2705 | ||
2706 | assert(message); | |
2707 | ||
2708 | r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); | |
2709 | if (r == -ENXIO) { | |
2710 | /* EFI case: let's see what the firmware supports */ | |
2711 | ||
2712 | r = efi_reboot_to_firmware_supported(); | |
2713 | if (r < 0) { | |
2714 | if (r != -EOPNOTSUPP) | |
2715 | log_warning_errno(r, "Failed to determine whether reboot to firmware is supported: %m"); | |
2716 | ||
2717 | return sd_bus_reply_method_return(message, "s", "na"); | |
2718 | } | |
2719 | ||
2720 | } else if (r <= 0) { | |
2721 | /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */ | |
2722 | ||
2723 | if (r < 0) | |
2724 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); | |
2725 | ||
2726 | return sd_bus_reply_method_return(message, "s", "na"); | |
2727 | } | |
2728 | ||
2729 | return return_test_polkit( | |
2730 | message, | |
2731 | CAP_SYS_ADMIN, | |
2732 | "org.freedesktop.login1.set-reboot-to-firmware-setup", | |
2733 | NULL, | |
2734 | UID_INVALID, | |
2735 | error); | |
2736 | } | |
2737 | ||
2738 | static int property_get_reboot_to_boot_loader_menu( | |
2739 | sd_bus *bus, | |
2740 | const char *path, | |
2741 | const char *interface, | |
2742 | const char *property, | |
2743 | sd_bus_message *reply, | |
2744 | void *userdata, | |
2745 | sd_bus_error *error) { | |
2746 | ||
2747 | uint64_t x = UINT64_MAX; | |
2748 | int r; | |
2749 | ||
2750 | assert(bus); | |
2751 | assert(reply); | |
2752 | assert(userdata); | |
2753 | ||
2754 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); | |
2755 | if (r == -ENXIO) { | |
2756 | /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished: | |
2757 | * | |
2758 | * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user) | |
2759 | * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user) | |
2760 | * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds | |
2761 | */ | |
2762 | ||
2763 | r = efi_loader_get_config_timeout_one_shot(&x); | |
2764 | if (r < 0) { | |
2765 | if (r != -ENOENT) | |
2766 | log_warning_errno(r, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m"); | |
2767 | } | |
2768 | ||
2769 | } else if (r < 0) | |
2770 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); | |
2771 | else if (r > 0) { | |
2772 | _cleanup_free_ char *v = NULL; | |
2773 | ||
2774 | /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */ | |
2775 | ||
2776 | r = read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v); | |
2777 | if (r < 0) { | |
2778 | if (r != -ENOENT) | |
2779 | log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m"); | |
2780 | } else { | |
2781 | r = safe_atou64(v, &x); | |
2782 | if (r < 0) | |
2783 | log_warning_errno(r, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m"); | |
2784 | } | |
2785 | } | |
2786 | ||
2787 | return sd_bus_message_append(reply, "t", x); | |
2788 | } | |
2789 | ||
2790 | static int method_set_reboot_to_boot_loader_menu( | |
2791 | sd_bus_message *message, | |
2792 | void *userdata, | |
2793 | sd_bus_error *error) { | |
2794 | ||
2795 | Manager *m = ASSERT_PTR(userdata); | |
2796 | bool use_efi; | |
2797 | uint64_t x; | |
2798 | int r; | |
2799 | ||
2800 | assert(message); | |
2801 | ||
2802 | r = sd_bus_message_read(message, "t", &x); | |
2803 | if (r < 0) | |
2804 | return r; | |
2805 | ||
2806 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); | |
2807 | if (r == -ENXIO) { | |
2808 | uint64_t features; | |
2809 | ||
2810 | /* EFI case: let's see if booting into boot loader menu is supported. */ | |
2811 | ||
2812 | r = efi_loader_get_features(&features); | |
2813 | if (r < 0) | |
2814 | log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m"); | |
2815 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT)) | |
2816 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu."); | |
2817 | ||
2818 | use_efi = true; | |
2819 | ||
2820 | } else if (r <= 0) { | |
2821 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */ | |
2822 | ||
2823 | if (r < 0) | |
2824 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); | |
2825 | ||
2826 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu."); | |
2827 | } else | |
2828 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */ | |
2829 | use_efi = false; | |
2830 | ||
2831 | r = bus_verify_polkit_async(message, | |
2832 | CAP_SYS_ADMIN, | |
2833 | "org.freedesktop.login1.set-reboot-to-boot-loader-menu", | |
2834 | NULL, | |
2835 | false, | |
2836 | UID_INVALID, | |
2837 | &m->polkit_registry, | |
2838 | error); | |
2839 | if (r < 0) | |
2840 | return r; | |
2841 | if (r == 0) | |
2842 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2843 | ||
2844 | if (use_efi) { | |
2845 | if (x == UINT64_MAX) | |
2846 | r = efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot), NULL, 0); | |
2847 | else { | |
2848 | char buf[DECIMAL_STR_MAX(uint64_t) + 1]; | |
2849 | xsprintf(buf, "%" PRIu64, DIV_ROUND_UP(x, USEC_PER_SEC)); /* second granularity */ | |
2850 | ||
2851 | r = efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot), buf); | |
2852 | } | |
2853 | if (r < 0) | |
2854 | return r; | |
2855 | } else { | |
2856 | if (x == UINT64_MAX) { | |
2857 | if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno != ENOENT) | |
2858 | return -errno; | |
2859 | } else { | |
2860 | char buf[DECIMAL_STR_MAX(uint64_t) + 1]; | |
2861 | ||
2862 | xsprintf(buf, "%" PRIu64, x); /* µs granularity */ | |
2863 | ||
2864 | r = write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf); | |
2865 | if (r < 0) | |
2866 | return r; | |
2867 | } | |
2868 | } | |
2869 | ||
2870 | return sd_bus_reply_method_return(message, NULL); | |
2871 | } | |
2872 | ||
2873 | static int method_can_reboot_to_boot_loader_menu( | |
2874 | sd_bus_message *message, | |
2875 | void *userdata, | |
2876 | sd_bus_error *error) { | |
2877 | ||
2878 | _unused_ Manager *m = ASSERT_PTR(userdata); | |
2879 | int r; | |
2880 | ||
2881 | assert(message); | |
2882 | ||
2883 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); | |
2884 | if (r == -ENXIO) { | |
2885 | uint64_t features = 0; | |
2886 | ||
2887 | /* EFI case, let's see if booting into boot loader menu is supported. */ | |
2888 | ||
2889 | r = efi_loader_get_features(&features); | |
2890 | if (r < 0) | |
2891 | log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m"); | |
2892 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT)) | |
2893 | return sd_bus_reply_method_return(message, "s", "na"); | |
2894 | ||
2895 | } else if (r <= 0) { | |
2896 | /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */ | |
2897 | ||
2898 | if (r < 0) | |
2899 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); | |
2900 | ||
2901 | return sd_bus_reply_method_return(message, "s", "na"); | |
2902 | } | |
2903 | ||
2904 | return return_test_polkit( | |
2905 | message, | |
2906 | CAP_SYS_ADMIN, | |
2907 | "org.freedesktop.login1.set-reboot-to-boot-loader-menu", | |
2908 | NULL, | |
2909 | UID_INVALID, | |
2910 | error); | |
2911 | } | |
2912 | ||
2913 | static int property_get_reboot_to_boot_loader_entry( | |
2914 | sd_bus *bus, | |
2915 | const char *path, | |
2916 | const char *interface, | |
2917 | const char *property, | |
2918 | sd_bus_message *reply, | |
2919 | void *userdata, | |
2920 | sd_bus_error *error) { | |
2921 | ||
2922 | _cleanup_free_ char *v = NULL; | |
2923 | Manager *m = ASSERT_PTR(userdata); | |
2924 | const char *x = NULL; | |
2925 | int r; | |
2926 | ||
2927 | assert(bus); | |
2928 | assert(reply); | |
2929 | ||
2930 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); | |
2931 | if (r == -ENXIO) { | |
2932 | /* EFI case: let's read the LoaderEntryOneShot variable */ | |
2933 | ||
2934 | r = efi_loader_update_entry_one_shot_cache(&m->efi_loader_entry_one_shot, &m->efi_loader_entry_one_shot_stat); | |
2935 | if (r < 0) { | |
2936 | if (r != -ENOENT) | |
2937 | log_warning_errno(r, "Failed to read LoaderEntryOneShot variable, ignoring: %m"); | |
2938 | } else | |
2939 | x = m->efi_loader_entry_one_shot; | |
2940 | ||
2941 | } else if (r < 0) | |
2942 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); | |
2943 | else if (r > 0) { | |
2944 | ||
2945 | /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */ | |
2946 | ||
2947 | r = read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v); | |
2948 | if (r < 0) { | |
2949 | if (r != -ENOENT) | |
2950 | log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m"); | |
2951 | } else if (!efi_loader_entry_name_valid(v)) | |
2952 | log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring."); | |
2953 | else | |
2954 | x = v; | |
2955 | } | |
2956 | ||
2957 | return sd_bus_message_append(reply, "s", x); | |
2958 | } | |
2959 | ||
2960 | static int boot_loader_entry_exists(Manager *m, const char *id) { | |
2961 | _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL; | |
2962 | int r; | |
2963 | ||
2964 | assert(m); | |
2965 | assert(id); | |
2966 | ||
2967 | r = boot_config_load_auto(&config, NULL, NULL); | |
2968 | if (r < 0 && r != -ENOKEY) /* don't complain if no GPT is found, hence skip ENOKEY */ | |
2969 | return r; | |
2970 | ||
2971 | r = manager_read_efi_boot_loader_entries(m); | |
2972 | if (r >= 0) | |
2973 | (void) boot_config_augment_from_loader(&config, m->efi_boot_loader_entries, /* auto_only= */ true); | |
2974 | ||
2975 | return !!boot_config_find_entry(&config, id); | |
2976 | } | |
2977 | ||
2978 | static int method_set_reboot_to_boot_loader_entry( | |
2979 | sd_bus_message *message, | |
2980 | void *userdata, | |
2981 | sd_bus_error *error) { | |
2982 | ||
2983 | Manager *m = ASSERT_PTR(userdata); | |
2984 | bool use_efi; | |
2985 | const char *v; | |
2986 | int r; | |
2987 | ||
2988 | assert(message); | |
2989 | ||
2990 | r = sd_bus_message_read(message, "s", &v); | |
2991 | if (r < 0) | |
2992 | return r; | |
2993 | ||
2994 | if (isempty(v)) | |
2995 | v = NULL; | |
2996 | else if (efi_loader_entry_name_valid(v)) { | |
2997 | r = boot_loader_entry_exists(m, v); | |
2998 | if (r < 0) | |
2999 | return r; | |
3000 | if (r == 0) | |
3001 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader entry '%s' is not known.", v); | |
3002 | } else | |
3003 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Boot loader entry name '%s' is not valid, refusing.", v); | |
3004 | ||
3005 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); | |
3006 | if (r == -ENXIO) { | |
3007 | uint64_t features; | |
3008 | ||
3009 | /* EFI case: let's see if booting into boot loader entry is supported. */ | |
3010 | ||
3011 | r = efi_loader_get_features(&features); | |
3012 | if (r < 0) | |
3013 | log_warning_errno(r, "Failed to determine whether reboot into boot loader entry is supported: %m"); | |
3014 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT)) | |
3015 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry."); | |
3016 | ||
3017 | use_efi = true; | |
3018 | ||
3019 | } else if (r <= 0) { | |
3020 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */ | |
3021 | ||
3022 | if (r < 0) | |
3023 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); | |
3024 | ||
3025 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry."); | |
3026 | } else | |
3027 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */ | |
3028 | use_efi = false; | |
3029 | ||
3030 | r = bus_verify_polkit_async(message, | |
3031 | CAP_SYS_ADMIN, | |
3032 | "org.freedesktop.login1.set-reboot-to-boot-loader-entry", | |
3033 | NULL, | |
3034 | false, | |
3035 | UID_INVALID, | |
3036 | &m->polkit_registry, | |
3037 | error); | |
3038 | if (r < 0) | |
3039 | return r; | |
3040 | if (r == 0) | |
3041 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
3042 | ||
3043 | if (use_efi) { | |
3044 | if (isempty(v)) | |
3045 | /* Delete item */ | |
3046 | r = efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot), NULL, 0); | |
3047 | else | |
3048 | r = efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot), v); | |
3049 | if (r < 0) | |
3050 | return r; | |
3051 | } else { | |
3052 | if (isempty(v)) { | |
3053 | if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno != ENOENT) | |
3054 | return -errno; | |
3055 | } else { | |
3056 | r = write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v); | |
3057 | if (r < 0) | |
3058 | return r; | |
3059 | } | |
3060 | } | |
3061 | ||
3062 | return sd_bus_reply_method_return(message, NULL); | |
3063 | } | |
3064 | ||
3065 | static int method_can_reboot_to_boot_loader_entry( | |
3066 | sd_bus_message *message, | |
3067 | void *userdata, | |
3068 | sd_bus_error *error) { | |
3069 | ||
3070 | _unused_ Manager *m = ASSERT_PTR(userdata); | |
3071 | int r; | |
3072 | ||
3073 | assert(message); | |
3074 | ||
3075 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); | |
3076 | if (r == -ENXIO) { | |
3077 | uint64_t features = 0; | |
3078 | ||
3079 | /* EFI case, let's see if booting into boot loader entry is supported. */ | |
3080 | ||
3081 | r = efi_loader_get_features(&features); | |
3082 | if (r < 0) | |
3083 | log_warning_errno(r, "Failed to determine whether reboot to boot loader entry is supported: %m"); | |
3084 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT)) | |
3085 | return sd_bus_reply_method_return(message, "s", "na"); | |
3086 | ||
3087 | } else if (r <= 0) { | |
3088 | /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */ | |
3089 | ||
3090 | if (r < 0) | |
3091 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); | |
3092 | ||
3093 | return sd_bus_reply_method_return(message, "s", "na"); | |
3094 | } | |
3095 | ||
3096 | return return_test_polkit( | |
3097 | message, | |
3098 | CAP_SYS_ADMIN, | |
3099 | "org.freedesktop.login1.set-reboot-to-boot-loader-entry", | |
3100 | NULL, | |
3101 | UID_INVALID, | |
3102 | error); | |
3103 | } | |
3104 | ||
3105 | static int property_get_boot_loader_entries( | |
3106 | sd_bus *bus, | |
3107 | const char *path, | |
3108 | const char *interface, | |
3109 | const char *property, | |
3110 | sd_bus_message *reply, | |
3111 | void *userdata, | |
3112 | sd_bus_error *error) { | |
3113 | ||
3114 | _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL; | |
3115 | Manager *m = ASSERT_PTR(userdata); | |
3116 | size_t i; | |
3117 | int r; | |
3118 | ||
3119 | assert(bus); | |
3120 | assert(reply); | |
3121 | ||
3122 | r = boot_config_load_auto(&config, NULL, NULL); | |
3123 | if (r < 0 && r != -ENOKEY) /* don't complain if there's no GPT found */ | |
3124 | return r; | |
3125 | ||
3126 | r = manager_read_efi_boot_loader_entries(m); | |
3127 | if (r >= 0) | |
3128 | (void) boot_config_augment_from_loader(&config, m->efi_boot_loader_entries, /* auto_only= */ true); | |
3129 | ||
3130 | r = sd_bus_message_open_container(reply, 'a', "s"); | |
3131 | if (r < 0) | |
3132 | return r; | |
3133 | ||
3134 | for (i = 0; i < config.n_entries; i++) { | |
3135 | BootEntry *e = config.entries + i; | |
3136 | ||
3137 | r = sd_bus_message_append(reply, "s", e->id); | |
3138 | if (r < 0) | |
3139 | return r; | |
3140 | } | |
3141 | ||
3142 | return sd_bus_message_close_container(reply); | |
3143 | } | |
3144 | ||
3145 | static int method_set_wall_message( | |
3146 | sd_bus_message *message, | |
3147 | void *userdata, | |
3148 | sd_bus_error *error) { | |
3149 | ||
3150 | int r; | |
3151 | Manager *m = ASSERT_PTR(userdata); | |
3152 | char *wall_message; | |
3153 | int enable_wall_messages; | |
3154 | ||
3155 | assert(message); | |
3156 | ||
3157 | r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages); | |
3158 | if (r < 0) | |
3159 | return r; | |
3160 | ||
3161 | if (strlen(wall_message) > WALL_MESSAGE_MAX) | |
3162 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
3163 | "Wall message too long, maximum permitted length is %u characters.", | |
3164 | WALL_MESSAGE_MAX); | |
3165 | ||
3166 | /* Short-circuit the operation if the desired state is already in place, to | |
3167 | * avoid an unnecessary polkit permission check. */ | |
3168 | if (streq_ptr(m->wall_message, empty_to_null(wall_message)) && | |
3169 | m->enable_wall_messages == enable_wall_messages) | |
3170 | goto done; | |
3171 | ||
3172 | r = bus_verify_polkit_async(message, | |
3173 | CAP_SYS_ADMIN, | |
3174 | "org.freedesktop.login1.set-wall-message", | |
3175 | NULL, | |
3176 | false, | |
3177 | UID_INVALID, | |
3178 | &m->polkit_registry, | |
3179 | error); | |
3180 | if (r < 0) | |
3181 | return r; | |
3182 | if (r == 0) | |
3183 | return 1; /* Will call us back */ | |
3184 | ||
3185 | r = free_and_strdup(&m->wall_message, empty_to_null(wall_message)); | |
3186 | if (r < 0) | |
3187 | return log_oom(); | |
3188 | ||
3189 | m->enable_wall_messages = enable_wall_messages; | |
3190 | ||
3191 | done: | |
3192 | return sd_bus_reply_method_return(message, NULL); | |
3193 | } | |
3194 | ||
3195 | static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
3196 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
3197 | const char *who, *why, *what, *mode; | |
3198 | _cleanup_free_ char *id = NULL; | |
3199 | _cleanup_close_ int fifo_fd = -1; | |
3200 | Manager *m = ASSERT_PTR(userdata); | |
3201 | InhibitMode mm; | |
3202 | InhibitWhat w; | |
3203 | pid_t pid; | |
3204 | uid_t uid; | |
3205 | int r; | |
3206 | ||
3207 | assert(message); | |
3208 | ||
3209 | r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode); | |
3210 | if (r < 0) | |
3211 | return r; | |
3212 | ||
3213 | w = inhibit_what_from_string(what); | |
3214 | if (w <= 0) | |
3215 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
3216 | "Invalid what specification %s", what); | |
3217 | ||
3218 | mm = inhibit_mode_from_string(mode); | |
3219 | if (mm < 0) | |
3220 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
3221 | "Invalid mode specification %s", mode); | |
3222 | ||
3223 | /* Delay is only supported for shutdown/sleep */ | |
3224 | if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP))) | |
3225 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
3226 | "Delay inhibitors only supported for shutdown and sleep"); | |
3227 | ||
3228 | /* Don't allow taking delay locks while we are already | |
3229 | * executing the operation. We shouldn't create the impression | |
3230 | * that the lock was successful if the machine is about to go | |
3231 | * down/suspend any moment. */ | |
3232 | if (m->delayed_action && m->delayed_action->inhibit_what & w) | |
3233 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, | |
3234 | "The operation inhibition has been requested for is already running"); | |
3235 | ||
3236 | r = bus_verify_polkit_async( | |
3237 | message, | |
3238 | CAP_SYS_BOOT, | |
3239 | w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") : | |
3240 | w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") : | |
3241 | w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" : | |
3242 | w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" : | |
3243 | w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" : | |
3244 | w == INHIBIT_HANDLE_REBOOT_KEY ? "org.freedesktop.login1.inhibit-handle-reboot-key" : | |
3245 | w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" : | |
3246 | "org.freedesktop.login1.inhibit-handle-lid-switch", | |
3247 | NULL, | |
3248 | false, | |
3249 | UID_INVALID, | |
3250 | &m->polkit_registry, | |
3251 | error); | |
3252 | if (r < 0) | |
3253 | return r; | |
3254 | if (r == 0) | |
3255 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
3256 | ||
3257 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds); | |
3258 | if (r < 0) | |
3259 | return r; | |
3260 | ||
3261 | r = sd_bus_creds_get_euid(creds, &uid); | |
3262 | if (r < 0) | |
3263 | return r; | |
3264 | ||
3265 | r = sd_bus_creds_get_pid(creds, &pid); | |
3266 | if (r < 0) | |
3267 | return r; | |
3268 | ||
3269 | if (hashmap_size(m->inhibitors) >= m->inhibitors_max) | |
3270 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, | |
3271 | "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", | |
3272 | m->inhibitors_max); | |
3273 | ||
3274 | do { | |
3275 | id = mfree(id); | |
3276 | ||
3277 | if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0) | |
3278 | return -ENOMEM; | |
3279 | ||
3280 | } while (hashmap_get(m->inhibitors, id)); | |
3281 | ||
3282 | _cleanup_(inhibitor_freep) Inhibitor *i = NULL; | |
3283 | r = manager_add_inhibitor(m, id, &i); | |
3284 | if (r < 0) | |
3285 | return r; | |
3286 | ||
3287 | i->what = w; | |
3288 | i->mode = mm; | |
3289 | i->pid = pid; | |
3290 | i->uid = uid; | |
3291 | i->why = strdup(why); | |
3292 | i->who = strdup(who); | |
3293 | ||
3294 | if (!i->why || !i->who) | |
3295 | return -ENOMEM; | |
3296 | ||
3297 | fifo_fd = inhibitor_create_fifo(i); | |
3298 | if (fifo_fd < 0) | |
3299 | return fifo_fd; | |
3300 | ||
3301 | r = inhibitor_start(i); | |
3302 | if (r < 0) | |
3303 | return r; | |
3304 | TAKE_PTR(i); | |
3305 | ||
3306 | return sd_bus_reply_method_return(message, "h", fifo_fd); | |
3307 | } | |
3308 | ||
3309 | static const sd_bus_vtable manager_vtable[] = { | |
3310 | SD_BUS_VTABLE_START(0), | |
3311 | ||
3312 | SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool, bus_property_set_bool, offsetof(Manager, enable_wall_messages), 0), | |
3313 | SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0), | |
3314 | ||
3315 | SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST), | |
3316 | SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
3317 | SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
3318 | SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST), | |
3319 | SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter, 0, 0), | |
3320 | SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, 0), | |
3321 | SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu, 0, 0), | |
3322 | SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry, 0, 0), | |
3323 | SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
3324 | SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3325 | SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3326 | SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3327 | SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3328 | SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3329 | SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST), | |
3330 | SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL, offsetof(Manager, user_stop_delay), SD_BUS_VTABLE_PROPERTY_CONST), | |
3331 | SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
3332 | SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_power_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), | |
3333 | SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action, offsetof(Manager, handle_reboot_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
3334 | SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_reboot_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), | |
3335 | SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
3336 | SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), | |
3337 | SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
3338 | SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), | |
3339 | SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST), | |
3340 | SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST), | |
3341 | SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST), | |
3342 | SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
3343 | SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST), | |
3344 | SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
3345 | SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0), | |
3346 | SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0), | |
3347 | SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0), | |
3348 | SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0), | |
3349 | SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed, 0, 0), | |
3350 | SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power, 0, 0), | |
3351 | SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST), | |
3352 | SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST), | |
3353 | SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL, offsetof(Manager, runtime_dir_inodes), SD_BUS_VTABLE_PROPERTY_CONST), | |
3354 | SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST), | |
3355 | SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0), | |
3356 | SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST), | |
3357 | SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0), | |
3358 | SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), | |
3359 | SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL, offsetof(Manager, stop_idle_session_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
3360 | ||
3361 | SD_BUS_METHOD_WITH_ARGS("GetSession", | |
3362 | SD_BUS_ARGS("s", session_id), | |
3363 | SD_BUS_RESULT("o", object_path), | |
3364 | method_get_session, | |
3365 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3366 | SD_BUS_METHOD_WITH_ARGS("GetSessionByPID", | |
3367 | SD_BUS_ARGS("u", pid), | |
3368 | SD_BUS_RESULT("o", object_path), | |
3369 | method_get_session_by_pid, | |
3370 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3371 | SD_BUS_METHOD_WITH_ARGS("GetUser", | |
3372 | SD_BUS_ARGS("u", uid), | |
3373 | SD_BUS_RESULT("o", object_path), | |
3374 | method_get_user, | |
3375 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3376 | SD_BUS_METHOD_WITH_ARGS("GetUserByPID", | |
3377 | SD_BUS_ARGS("u", pid), | |
3378 | SD_BUS_RESULT("o", object_path), | |
3379 | method_get_user_by_pid, | |
3380 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3381 | SD_BUS_METHOD_WITH_ARGS("GetSeat", | |
3382 | SD_BUS_ARGS("s", seat_id), | |
3383 | SD_BUS_RESULT("o", object_path), | |
3384 | method_get_seat, | |
3385 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3386 | SD_BUS_METHOD_WITH_ARGS("ListSessions", | |
3387 | SD_BUS_NO_ARGS, | |
3388 | SD_BUS_RESULT("a(susso)", sessions), | |
3389 | method_list_sessions, | |
3390 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3391 | SD_BUS_METHOD_WITH_ARGS("ListUsers", | |
3392 | SD_BUS_NO_ARGS, | |
3393 | SD_BUS_RESULT("a(uso)", users), | |
3394 | method_list_users, | |
3395 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3396 | SD_BUS_METHOD_WITH_ARGS("ListSeats", | |
3397 | SD_BUS_NO_ARGS, | |
3398 | SD_BUS_RESULT("a(so)", seats), | |
3399 | method_list_seats, | |
3400 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3401 | SD_BUS_METHOD_WITH_ARGS("ListInhibitors", | |
3402 | SD_BUS_NO_ARGS, | |
3403 | SD_BUS_RESULT("a(ssssuu)", inhibitors), | |
3404 | method_list_inhibitors, | |
3405 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3406 | SD_BUS_METHOD_WITH_ARGS("CreateSession", | |
3407 | SD_BUS_ARGS("u", uid, | |
3408 | "u", pid, | |
3409 | "s", service, | |
3410 | "s", type, | |
3411 | "s", class, | |
3412 | "s", desktop, | |
3413 | "s", seat_id, | |
3414 | "u", vtnr, | |
3415 | "s", tty, | |
3416 | "s", display, | |
3417 | "b", remote, | |
3418 | "s", remote_user, | |
3419 | "s", remote_host, | |
3420 | "a(sv)", properties), | |
3421 | SD_BUS_RESULT("s", session_id, | |
3422 | "o", object_path, | |
3423 | "s", runtime_path, | |
3424 | "h", fifo_fd, | |
3425 | "u", uid, | |
3426 | "s", seat_id, | |
3427 | "u", vtnr, | |
3428 | "b", existing), | |
3429 | method_create_session, | |
3430 | 0), | |
3431 | SD_BUS_METHOD_WITH_ARGS("ReleaseSession", | |
3432 | SD_BUS_ARGS("s", session_id), | |
3433 | SD_BUS_NO_RESULT, | |
3434 | method_release_session, | |
3435 | 0), | |
3436 | SD_BUS_METHOD_WITH_ARGS("ActivateSession", | |
3437 | SD_BUS_ARGS("s", session_id), | |
3438 | SD_BUS_NO_RESULT, | |
3439 | method_activate_session, | |
3440 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3441 | SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat", | |
3442 | SD_BUS_ARGS("s", session_id, "s", seat_id), | |
3443 | SD_BUS_NO_RESULT, | |
3444 | method_activate_session_on_seat, | |
3445 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3446 | SD_BUS_METHOD_WITH_ARGS("LockSession", | |
3447 | SD_BUS_ARGS("s", session_id), | |
3448 | SD_BUS_NO_RESULT, | |
3449 | method_lock_session, | |
3450 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3451 | SD_BUS_METHOD_WITH_ARGS("UnlockSession", | |
3452 | SD_BUS_ARGS("s", session_id), | |
3453 | SD_BUS_NO_RESULT, | |
3454 | method_lock_session, | |
3455 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3456 | SD_BUS_METHOD("LockSessions", | |
3457 | NULL, | |
3458 | NULL, | |
3459 | method_lock_sessions, | |
3460 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3461 | SD_BUS_METHOD("UnlockSessions", | |
3462 | NULL, | |
3463 | NULL, | |
3464 | method_lock_sessions, | |
3465 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3466 | SD_BUS_METHOD_WITH_ARGS("KillSession", | |
3467 | SD_BUS_ARGS("s", session_id, "s", who, "i", signal_number), | |
3468 | SD_BUS_NO_RESULT, | |
3469 | method_kill_session, | |
3470 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3471 | SD_BUS_METHOD_WITH_ARGS("KillUser", | |
3472 | SD_BUS_ARGS("u", uid, "i", signal_number), | |
3473 | SD_BUS_NO_RESULT, | |
3474 | method_kill_user, | |
3475 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3476 | SD_BUS_METHOD_WITH_ARGS("TerminateSession", | |
3477 | SD_BUS_ARGS("s", session_id), | |
3478 | SD_BUS_NO_RESULT, | |
3479 | method_terminate_session, | |
3480 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3481 | SD_BUS_METHOD_WITH_ARGS("TerminateUser", | |
3482 | SD_BUS_ARGS("u", uid), | |
3483 | SD_BUS_NO_RESULT, | |
3484 | method_terminate_user, | |
3485 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3486 | SD_BUS_METHOD_WITH_ARGS("TerminateSeat", | |
3487 | SD_BUS_ARGS("s", seat_id), | |
3488 | SD_BUS_NO_RESULT, | |
3489 | method_terminate_seat, | |
3490 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3491 | SD_BUS_METHOD_WITH_ARGS("SetUserLinger", | |
3492 | SD_BUS_ARGS("u", uid, "b", enable, "b", interactive), | |
3493 | SD_BUS_NO_RESULT, | |
3494 | method_set_user_linger, | |
3495 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3496 | SD_BUS_METHOD_WITH_ARGS("AttachDevice", | |
3497 | SD_BUS_ARGS("s", seat_id, "s", sysfs_path, "b", interactive), | |
3498 | SD_BUS_NO_RESULT, | |
3499 | method_attach_device, | |
3500 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3501 | SD_BUS_METHOD_WITH_ARGS("FlushDevices", | |
3502 | SD_BUS_ARGS("b", interactive), | |
3503 | SD_BUS_NO_RESULT, | |
3504 | method_flush_devices, | |
3505 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3506 | SD_BUS_METHOD_WITH_ARGS("PowerOff", | |
3507 | SD_BUS_ARGS("b", interactive), | |
3508 | SD_BUS_NO_RESULT, | |
3509 | method_poweroff, | |
3510 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3511 | SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags", | |
3512 | SD_BUS_ARGS("t", flags), | |
3513 | SD_BUS_NO_RESULT, | |
3514 | method_poweroff, | |
3515 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3516 | SD_BUS_METHOD_WITH_ARGS("Reboot", | |
3517 | SD_BUS_ARGS("b", interactive), | |
3518 | SD_BUS_NO_RESULT, | |
3519 | method_reboot, | |
3520 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3521 | SD_BUS_METHOD_WITH_ARGS("RebootWithFlags", | |
3522 | SD_BUS_ARGS("t", flags), | |
3523 | SD_BUS_NO_RESULT, | |
3524 | method_reboot, | |
3525 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3526 | SD_BUS_METHOD_WITH_ARGS("Halt", | |
3527 | SD_BUS_ARGS("b", interactive), | |
3528 | SD_BUS_NO_RESULT, | |
3529 | method_halt, | |
3530 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3531 | SD_BUS_METHOD_WITH_ARGS("HaltWithFlags", | |
3532 | SD_BUS_ARGS("t", flags), | |
3533 | SD_BUS_NO_RESULT, | |
3534 | method_halt, | |
3535 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3536 | SD_BUS_METHOD_WITH_ARGS("Suspend", | |
3537 | SD_BUS_ARGS("b", interactive), | |
3538 | SD_BUS_NO_RESULT, | |
3539 | method_suspend, | |
3540 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3541 | SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags", | |
3542 | SD_BUS_ARGS("t", flags), | |
3543 | SD_BUS_NO_RESULT, | |
3544 | method_suspend, | |
3545 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3546 | SD_BUS_METHOD_WITH_ARGS("Hibernate", | |
3547 | SD_BUS_ARGS("b", interactive), | |
3548 | SD_BUS_NO_RESULT, | |
3549 | method_hibernate, | |
3550 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3551 | SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags", | |
3552 | SD_BUS_ARGS("t", flags), | |
3553 | SD_BUS_NO_RESULT, | |
3554 | method_hibernate, | |
3555 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3556 | SD_BUS_METHOD_WITH_ARGS("HybridSleep", | |
3557 | SD_BUS_ARGS("b", interactive), | |
3558 | SD_BUS_NO_RESULT, | |
3559 | method_hybrid_sleep, | |
3560 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3561 | SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags", | |
3562 | SD_BUS_ARGS("t", flags), | |
3563 | SD_BUS_NO_RESULT, | |
3564 | method_hybrid_sleep, | |
3565 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3566 | SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate", | |
3567 | SD_BUS_ARGS("b", interactive), | |
3568 | SD_BUS_NO_RESULT, | |
3569 | method_suspend_then_hibernate, | |
3570 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3571 | SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags", | |
3572 | SD_BUS_ARGS("t", flags), | |
3573 | SD_BUS_NO_RESULT, | |
3574 | method_suspend_then_hibernate, | |
3575 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3576 | SD_BUS_METHOD_WITH_ARGS("CanPowerOff", | |
3577 | SD_BUS_NO_ARGS, | |
3578 | SD_BUS_RESULT("s", result), | |
3579 | method_can_poweroff, | |
3580 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3581 | SD_BUS_METHOD_WITH_ARGS("CanReboot", | |
3582 | SD_BUS_NO_ARGS, | |
3583 | SD_BUS_RESULT("s", result), | |
3584 | method_can_reboot, | |
3585 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3586 | SD_BUS_METHOD_WITH_ARGS("CanHalt", | |
3587 | SD_BUS_NO_ARGS, | |
3588 | SD_BUS_RESULT("s", result), | |
3589 | method_can_halt, | |
3590 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3591 | SD_BUS_METHOD_WITH_ARGS("CanSuspend", | |
3592 | SD_BUS_NO_ARGS, | |
3593 | SD_BUS_RESULT("s", result), | |
3594 | method_can_suspend, | |
3595 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3596 | SD_BUS_METHOD_WITH_ARGS("CanHibernate", | |
3597 | SD_BUS_NO_ARGS, | |
3598 | SD_BUS_RESULT("s", result), | |
3599 | method_can_hibernate, | |
3600 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3601 | SD_BUS_METHOD_WITH_ARGS("CanHybridSleep", | |
3602 | SD_BUS_NO_ARGS, | |
3603 | SD_BUS_RESULT("s", result), | |
3604 | method_can_hybrid_sleep, | |
3605 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3606 | SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate", | |
3607 | SD_BUS_NO_ARGS, | |
3608 | SD_BUS_RESULT("s", result), | |
3609 | method_can_suspend_then_hibernate, | |
3610 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3611 | SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown", | |
3612 | SD_BUS_ARGS("s", type, "t", usec), | |
3613 | SD_BUS_NO_RESULT, | |
3614 | method_schedule_shutdown, | |
3615 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3616 | SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown", | |
3617 | SD_BUS_NO_ARGS, | |
3618 | SD_BUS_RESULT("b", cancelled), | |
3619 | method_cancel_scheduled_shutdown, | |
3620 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3621 | SD_BUS_METHOD_WITH_ARGS("Inhibit", | |
3622 | SD_BUS_ARGS("s", what, "s", who, "s", why, "s", mode), | |
3623 | SD_BUS_RESULT("h", pipe_fd), | |
3624 | method_inhibit, | |
3625 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3626 | SD_BUS_METHOD_WITH_ARGS("CanRebootParameter", | |
3627 | SD_BUS_NO_ARGS, | |
3628 | SD_BUS_RESULT("s", result), | |
3629 | method_can_reboot_parameter, | |
3630 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3631 | SD_BUS_METHOD_WITH_ARGS("SetRebootParameter", | |
3632 | SD_BUS_ARGS("s", parameter), | |
3633 | SD_BUS_NO_RESULT, | |
3634 | method_set_reboot_parameter, | |
3635 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3636 | SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup", | |
3637 | SD_BUS_NO_ARGS, | |
3638 | SD_BUS_RESULT("s", result), | |
3639 | method_can_reboot_to_firmware_setup, | |
3640 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3641 | SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup", | |
3642 | SD_BUS_ARGS("b", enable), | |
3643 | SD_BUS_NO_RESULT, | |
3644 | method_set_reboot_to_firmware_setup, | |
3645 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3646 | SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu", | |
3647 | SD_BUS_NO_ARGS, | |
3648 | SD_BUS_RESULT("s", result), | |
3649 | method_can_reboot_to_boot_loader_menu, | |
3650 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3651 | SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu", | |
3652 | SD_BUS_ARGS("t", timeout), | |
3653 | SD_BUS_NO_RESULT, | |
3654 | method_set_reboot_to_boot_loader_menu, | |
3655 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3656 | SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry", | |
3657 | SD_BUS_NO_ARGS, | |
3658 | SD_BUS_RESULT("s", result), | |
3659 | method_can_reboot_to_boot_loader_entry, | |
3660 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3661 | SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry", | |
3662 | SD_BUS_ARGS("s", boot_loader_entry), | |
3663 | SD_BUS_NO_RESULT, | |
3664 | method_set_reboot_to_boot_loader_entry, | |
3665 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3666 | SD_BUS_METHOD_WITH_ARGS("SetWallMessage", | |
3667 | SD_BUS_ARGS("s", wall_message, "b", enable), | |
3668 | SD_BUS_NO_RESULT, | |
3669 | method_set_wall_message, | |
3670 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3671 | ||
3672 | SD_BUS_SIGNAL_WITH_ARGS("SessionNew", | |
3673 | SD_BUS_ARGS("s", session_id, "o", object_path), | |
3674 | 0), | |
3675 | SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved", | |
3676 | SD_BUS_ARGS("s", session_id, "o", object_path), | |
3677 | 0), | |
3678 | SD_BUS_SIGNAL_WITH_ARGS("UserNew", | |
3679 | SD_BUS_ARGS("u", uid, "o", object_path), | |
3680 | 0), | |
3681 | SD_BUS_SIGNAL_WITH_ARGS("UserRemoved", | |
3682 | SD_BUS_ARGS("u", uid, "o", object_path), | |
3683 | 0), | |
3684 | SD_BUS_SIGNAL_WITH_ARGS("SeatNew", | |
3685 | SD_BUS_ARGS("s", seat_id, "o", object_path), | |
3686 | 0), | |
3687 | SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved", | |
3688 | SD_BUS_ARGS("s", seat_id, "o", object_path), | |
3689 | 0), | |
3690 | SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown", | |
3691 | SD_BUS_ARGS("b", start), | |
3692 | 0), | |
3693 | SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep", | |
3694 | SD_BUS_ARGS("b", start), | |
3695 | 0), | |
3696 | ||
3697 | SD_BUS_VTABLE_END | |
3698 | }; | |
3699 | ||
3700 | const BusObjectImplementation manager_object = { | |
3701 | "/org/freedesktop/login1", | |
3702 | "org.freedesktop.login1.Manager", | |
3703 | .vtables = BUS_VTABLES(manager_vtable), | |
3704 | .children = BUS_IMPLEMENTATIONS(&seat_object, | |
3705 | &session_object, | |
3706 | &user_object), | |
3707 | }; | |
3708 | ||
3709 | static int session_jobs_reply(Session *s, uint32_t jid, const char *unit, const char *result) { | |
3710 | assert(s); | |
3711 | assert(unit); | |
3712 | ||
3713 | if (!s->started) | |
3714 | return 0; | |
3715 | ||
3716 | if (result && !streq(result, "done")) { | |
3717 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; | |
3718 | ||
3719 | sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, | |
3720 | "Job %u for unit '%s' failed with '%s'", jid, unit, result); | |
3721 | return session_send_create_reply(s, &e); | |
3722 | } | |
3723 | ||
3724 | return session_send_create_reply(s, NULL); | |
3725 | } | |
3726 | ||
3727 | int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
3728 | const char *path, *result, *unit; | |
3729 | Manager *m = ASSERT_PTR(userdata); | |
3730 | Session *session; | |
3731 | uint32_t id; | |
3732 | User *user; | |
3733 | int r; | |
3734 | ||
3735 | assert(message); | |
3736 | ||
3737 | r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); | |
3738 | if (r < 0) { | |
3739 | bus_log_parse_error(r); | |
3740 | return 0; | |
3741 | } | |
3742 | ||
3743 | if (m->action_job && streq(m->action_job, path)) { | |
3744 | assert(m->delayed_action); | |
3745 | log_info("Operation '%s' finished.", inhibit_what_to_string(m->delayed_action->inhibit_what)); | |
3746 | ||
3747 | /* Tell people that they now may take a lock again */ | |
3748 | (void) send_prepare_for(m, m->delayed_action->inhibit_what, false); | |
3749 | ||
3750 | m->action_job = mfree(m->action_job); | |
3751 | m->delayed_action = NULL; | |
3752 | return 0; | |
3753 | } | |
3754 | ||
3755 | session = hashmap_get(m->session_units, unit); | |
3756 | if (session) { | |
3757 | if (streq_ptr(path, session->scope_job)) { | |
3758 | session->scope_job = mfree(session->scope_job); | |
3759 | (void) session_jobs_reply(session, id, unit, result); | |
3760 | ||
3761 | session_save(session); | |
3762 | user_save(session->user); | |
3763 | } | |
3764 | ||
3765 | session_add_to_gc_queue(session); | |
3766 | } | |
3767 | ||
3768 | user = hashmap_get(m->user_units, unit); | |
3769 | if (user) { | |
3770 | if (streq_ptr(path, user->service_job)) { | |
3771 | user->service_job = mfree(user->service_job); | |
3772 | ||
3773 | LIST_FOREACH(sessions_by_user, s, user->sessions) | |
3774 | (void) session_jobs_reply(s, id, unit, NULL /* don't propagate user service failures to the client */); | |
3775 | ||
3776 | user_save(user); | |
3777 | } | |
3778 | ||
3779 | user_add_to_gc_queue(user); | |
3780 | } | |
3781 | ||
3782 | return 0; | |
3783 | } | |
3784 | ||
3785 | int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
3786 | const char *path, *unit; | |
3787 | Manager *m = ASSERT_PTR(userdata); | |
3788 | Session *session; | |
3789 | User *user; | |
3790 | int r; | |
3791 | ||
3792 | assert(message); | |
3793 | ||
3794 | r = sd_bus_message_read(message, "so", &unit, &path); | |
3795 | if (r < 0) { | |
3796 | bus_log_parse_error(r); | |
3797 | return 0; | |
3798 | } | |
3799 | ||
3800 | session = hashmap_get(m->session_units, unit); | |
3801 | if (session) | |
3802 | session_add_to_gc_queue(session); | |
3803 | ||
3804 | user = hashmap_get(m->user_units, unit); | |
3805 | if (user) | |
3806 | user_add_to_gc_queue(user); | |
3807 | ||
3808 | return 0; | |
3809 | } | |
3810 | ||
3811 | int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
3812 | _cleanup_free_ char *unit = NULL; | |
3813 | Manager *m = ASSERT_PTR(userdata); | |
3814 | const char *path; | |
3815 | Session *session; | |
3816 | User *user; | |
3817 | int r; | |
3818 | ||
3819 | assert(message); | |
3820 | ||
3821 | path = sd_bus_message_get_path(message); | |
3822 | if (!path) | |
3823 | return 0; | |
3824 | ||
3825 | r = unit_name_from_dbus_path(path, &unit); | |
3826 | if (r == -EINVAL) /* not a unit */ | |
3827 | return 0; | |
3828 | if (r < 0) { | |
3829 | log_oom(); | |
3830 | return 0; | |
3831 | } | |
3832 | ||
3833 | session = hashmap_get(m->session_units, unit); | |
3834 | if (session) | |
3835 | session_add_to_gc_queue(session); | |
3836 | ||
3837 | user = hashmap_get(m->user_units, unit); | |
3838 | if (user) | |
3839 | user_add_to_gc_queue(user); | |
3840 | ||
3841 | return 0; | |
3842 | } | |
3843 | ||
3844 | int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
3845 | Manager *m = ASSERT_PTR(userdata); | |
3846 | Session *session; | |
3847 | int b, r; | |
3848 | ||
3849 | assert(message); | |
3850 | ||
3851 | r = sd_bus_message_read(message, "b", &b); | |
3852 | if (r < 0) { | |
3853 | bus_log_parse_error(r); | |
3854 | return 0; | |
3855 | } | |
3856 | ||
3857 | if (b) | |
3858 | return 0; | |
3859 | ||
3860 | /* systemd finished reloading, let's recheck all our sessions */ | |
3861 | log_debug("System manager has been reloaded, rechecking sessions..."); | |
3862 | ||
3863 | HASHMAP_FOREACH(session, m->sessions) | |
3864 | session_add_to_gc_queue(session); | |
3865 | ||
3866 | return 0; | |
3867 | } | |
3868 | ||
3869 | int manager_send_changed(Manager *manager, const char *property, ...) { | |
3870 | char **l; | |
3871 | ||
3872 | assert(manager); | |
3873 | ||
3874 | l = strv_from_stdarg_alloca(property); | |
3875 | ||
3876 | return sd_bus_emit_properties_changed_strv( | |
3877 | manager->bus, | |
3878 | "/org/freedesktop/login1", | |
3879 | "org.freedesktop.login1.Manager", | |
3880 | l); | |
3881 | } | |
3882 | ||
3883 | static int strdup_job(sd_bus_message *reply, char **job) { | |
3884 | const char *j; | |
3885 | char *copy; | |
3886 | int r; | |
3887 | ||
3888 | r = sd_bus_message_read(reply, "o", &j); | |
3889 | if (r < 0) | |
3890 | return r; | |
3891 | ||
3892 | copy = strdup(j); | |
3893 | if (!copy) | |
3894 | return -ENOMEM; | |
3895 | ||
3896 | *job = copy; | |
3897 | return 1; | |
3898 | } | |
3899 | ||
3900 | int manager_start_scope( | |
3901 | Manager *manager, | |
3902 | const char *scope, | |
3903 | pid_t pid, | |
3904 | const char *slice, | |
3905 | const char *description, | |
3906 | char **wants, | |
3907 | char **after, | |
3908 | const char *requires_mounts_for, | |
3909 | sd_bus_message *more_properties, | |
3910 | sd_bus_error *error, | |
3911 | char **job) { | |
3912 | ||
3913 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
3914 | int r; | |
3915 | ||
3916 | assert(manager); | |
3917 | assert(scope); | |
3918 | assert(pid > 1); | |
3919 | assert(job); | |
3920 | ||
3921 | r = bus_message_new_method_call(manager->bus, &m, bus_systemd_mgr, "StartTransientUnit"); | |
3922 | if (r < 0) | |
3923 | return r; | |
3924 | ||
3925 | r = sd_bus_message_append(m, "ss", strempty(scope), "fail"); | |
3926 | if (r < 0) | |
3927 | return r; | |
3928 | ||
3929 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
3930 | if (r < 0) | |
3931 | return r; | |
3932 | ||
3933 | if (!isempty(slice)) { | |
3934 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); | |
3935 | if (r < 0) | |
3936 | return r; | |
3937 | } | |
3938 | ||
3939 | if (!isempty(description)) { | |
3940 | r = sd_bus_message_append(m, "(sv)", "Description", "s", description); | |
3941 | if (r < 0) | |
3942 | return r; | |
3943 | } | |
3944 | ||
3945 | STRV_FOREACH(i, wants) { | |
3946 | r = sd_bus_message_append(m, "(sv)", "Wants", "as", 1, *i); | |
3947 | if (r < 0) | |
3948 | return r; | |
3949 | } | |
3950 | ||
3951 | STRV_FOREACH(i, after) { | |
3952 | r = sd_bus_message_append(m, "(sv)", "After", "as", 1, *i); | |
3953 | if (r < 0) | |
3954 | return r; | |
3955 | } | |
3956 | ||
3957 | if (!empty_or_root(requires_mounts_for)) { | |
3958 | r = sd_bus_message_append(m, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for); | |
3959 | if (r < 0) | |
3960 | return r; | |
3961 | } | |
3962 | ||
3963 | /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore | |
3964 | * SIGTERM */ | |
3965 | r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true); | |
3966 | if (r < 0) | |
3967 | return r; | |
3968 | ||
3969 | r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid); | |
3970 | if (r < 0) | |
3971 | return r; | |
3972 | ||
3973 | /* For login session scopes, if a process is OOM killed by the kernel, *don't* terminate the rest of | |
3974 | the scope */ | |
3975 | r = sd_bus_message_append(m, "(sv)", "OOMPolicy", "s", "continue"); | |
3976 | if (r < 0) | |
3977 | return r; | |
3978 | ||
3979 | /* disable TasksMax= for the session scope, rely on the slice setting for it */ | |
3980 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", UINT64_MAX); | |
3981 | if (r < 0) | |
3982 | return bus_log_create_error(r); | |
3983 | ||
3984 | if (more_properties) { | |
3985 | /* If TasksMax also appears here, it will overwrite the default value set above */ | |
3986 | r = sd_bus_message_copy(m, more_properties, true); | |
3987 | if (r < 0) | |
3988 | return r; | |
3989 | } | |
3990 | ||
3991 | r = sd_bus_message_close_container(m); | |
3992 | if (r < 0) | |
3993 | return r; | |
3994 | ||
3995 | r = sd_bus_message_append(m, "a(sa(sv))", 0); | |
3996 | if (r < 0) | |
3997 | return r; | |
3998 | ||
3999 | r = sd_bus_call(manager->bus, m, 0, error, &reply); | |
4000 | if (r < 0) | |
4001 | return r; | |
4002 | ||
4003 | return strdup_job(reply, job); | |
4004 | } | |
4005 | ||
4006 | int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { | |
4007 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4008 | int r; | |
4009 | ||
4010 | assert(manager); | |
4011 | assert(unit); | |
4012 | assert(job); | |
4013 | ||
4014 | r = bus_call_method( | |
4015 | manager->bus, | |
4016 | bus_systemd_mgr, | |
4017 | "StartUnit", | |
4018 | error, | |
4019 | &reply, | |
4020 | "ss", unit, "replace"); | |
4021 | if (r < 0) | |
4022 | return r; | |
4023 | ||
4024 | return strdup_job(reply, job); | |
4025 | } | |
4026 | ||
4027 | int manager_stop_unit(Manager *manager, const char *unit, const char *job_mode, sd_bus_error *error, char **ret_job) { | |
4028 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4029 | int r; | |
4030 | ||
4031 | assert(manager); | |
4032 | assert(unit); | |
4033 | assert(ret_job); | |
4034 | ||
4035 | r = bus_call_method( | |
4036 | manager->bus, | |
4037 | bus_systemd_mgr, | |
4038 | "StopUnit", | |
4039 | error, | |
4040 | &reply, | |
4041 | "ss", unit, job_mode ?: "fail"); | |
4042 | if (r < 0) { | |
4043 | if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT, | |
4044 | BUS_ERROR_LOAD_FAILED)) { | |
4045 | ||
4046 | *ret_job = NULL; | |
4047 | sd_bus_error_free(error); | |
4048 | return 0; | |
4049 | } | |
4050 | ||
4051 | return r; | |
4052 | } | |
4053 | ||
4054 | return strdup_job(reply, ret_job); | |
4055 | } | |
4056 | ||
4057 | int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *ret_error) { | |
4058 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4059 | _cleanup_free_ char *path = NULL; | |
4060 | int r; | |
4061 | ||
4062 | assert(manager); | |
4063 | assert(scope); | |
4064 | ||
4065 | path = unit_dbus_path_from_name(scope); | |
4066 | if (!path) | |
4067 | return -ENOMEM; | |
4068 | ||
4069 | r = sd_bus_call_method( | |
4070 | manager->bus, | |
4071 | "org.freedesktop.systemd1", | |
4072 | path, | |
4073 | "org.freedesktop.systemd1.Scope", | |
4074 | "Abandon", | |
4075 | &error, | |
4076 | NULL, | |
4077 | NULL); | |
4078 | if (r < 0) { | |
4079 | if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, | |
4080 | BUS_ERROR_LOAD_FAILED, | |
4081 | BUS_ERROR_SCOPE_NOT_RUNNING)) | |
4082 | return 0; | |
4083 | ||
4084 | sd_bus_error_move(ret_error, &error); | |
4085 | return r; | |
4086 | } | |
4087 | ||
4088 | return 1; | |
4089 | } | |
4090 | ||
4091 | int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) { | |
4092 | assert(manager); | |
4093 | assert(unit); | |
4094 | ||
4095 | return bus_call_method( | |
4096 | manager->bus, | |
4097 | bus_systemd_mgr, | |
4098 | "KillUnit", | |
4099 | error, | |
4100 | NULL, | |
4101 | "ssi", unit, who == KILL_LEADER ? "main" : "all", signo); | |
4102 | } | |
4103 | ||
4104 | int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret_error) { | |
4105 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4106 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4107 | _cleanup_free_ char *path = NULL; | |
4108 | const char *state; | |
4109 | int r; | |
4110 | ||
4111 | assert(manager); | |
4112 | assert(unit); | |
4113 | ||
4114 | path = unit_dbus_path_from_name(unit); | |
4115 | if (!path) | |
4116 | return -ENOMEM; | |
4117 | ||
4118 | r = sd_bus_get_property( | |
4119 | manager->bus, | |
4120 | "org.freedesktop.systemd1", | |
4121 | path, | |
4122 | "org.freedesktop.systemd1.Unit", | |
4123 | "ActiveState", | |
4124 | &error, | |
4125 | &reply, | |
4126 | "s"); | |
4127 | if (r < 0) { | |
4128 | /* systemd might have dropped off momentarily, let's | |
4129 | * not make this an error */ | |
4130 | if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, | |
4131 | SD_BUS_ERROR_DISCONNECTED)) | |
4132 | return true; | |
4133 | ||
4134 | /* If the unit is already unloaded then it's not | |
4135 | * active */ | |
4136 | if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, | |
4137 | BUS_ERROR_LOAD_FAILED)) | |
4138 | return false; | |
4139 | ||
4140 | sd_bus_error_move(ret_error, &error); | |
4141 | return r; | |
4142 | } | |
4143 | ||
4144 | r = sd_bus_message_read(reply, "s", &state); | |
4145 | if (r < 0) | |
4146 | return r; | |
4147 | ||
4148 | return !STR_IN_SET(state, "inactive", "failed"); | |
4149 | } | |
4150 | ||
4151 | int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *ret_error) { | |
4152 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4153 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4154 | int r; | |
4155 | ||
4156 | assert(manager); | |
4157 | assert(path); | |
4158 | ||
4159 | r = sd_bus_get_property( | |
4160 | manager->bus, | |
4161 | "org.freedesktop.systemd1", | |
4162 | path, | |
4163 | "org.freedesktop.systemd1.Job", | |
4164 | "State", | |
4165 | &error, | |
4166 | &reply, | |
4167 | "s"); | |
4168 | if (r < 0) { | |
4169 | if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, | |
4170 | SD_BUS_ERROR_DISCONNECTED)) | |
4171 | return true; | |
4172 | ||
4173 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) | |
4174 | return false; | |
4175 | ||
4176 | sd_bus_error_move(ret_error, &error); | |
4177 | return r; | |
4178 | } | |
4179 | ||
4180 | /* We don't actually care about the state really. The fact | |
4181 | * that we could read the job state is enough for us */ | |
4182 | ||
4183 | return true; | |
4184 | } |