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