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