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