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