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