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