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