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