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