]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-dbus.c
tree-wide: use UINT64_MAX or friends
[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_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID");
699 if (leader < 0 || leader == 1 || leader == getpid_cached())
700 return sd_bus_error_setf(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_setf(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 _cleanup_free_ char *t = NULL;
1323 const char *p;
1324
1325 r = sd_device_get_syspath(d, &p);
1326 if (r < 0)
1327 return r;
1328
1329 t = path_join(p, "uevent");
1330 if (!t)
1331 return -ENOMEM;
1332
1333 (void) write_string_file(t, "change", WRITE_STRING_FILE_DISABLE_BUFFER);
1334 }
1335
1336 return 0;
1337 }
1338
1339 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1340 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
1341 _cleanup_free_ char *rule = NULL, *file = NULL;
1342 const char *id_for_seat;
1343 int r;
1344
1345 assert(m);
1346 assert(seat);
1347 assert(sysfs);
1348
1349 r = sd_device_new_from_syspath(&d, sysfs);
1350 if (r < 0)
1351 return r;
1352
1353 if (sd_device_has_current_tag(d, "seat") <= 0)
1354 return -ENODEV;
1355
1356 if (sd_device_get_property_value(d, "ID_FOR_SEAT", &id_for_seat) < 0)
1357 return -ENODEV;
1358
1359 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1360 return -ENOMEM;
1361
1362 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1363 return -ENOMEM;
1364
1365 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1366 r = write_string_file_atomic_label(file, rule);
1367 if (r < 0)
1368 return r;
1369
1370 return trigger_device(m, d);
1371 }
1372
1373 static int flush_devices(Manager *m) {
1374 _cleanup_closedir_ DIR *d;
1375
1376 assert(m);
1377
1378 d = opendir("/etc/udev/rules.d");
1379 if (!d) {
1380 if (errno != ENOENT)
1381 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1382 } else {
1383 struct dirent *de;
1384
1385 FOREACH_DIRENT_ALL(de, d, break) {
1386 dirent_ensure_type(d, de);
1387 if (!dirent_is_file(de))
1388 continue;
1389
1390 if (!startswith(de->d_name, "72-seat-"))
1391 continue;
1392
1393 if (!endswith(de->d_name, ".rules"))
1394 continue;
1395
1396 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1397 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1398 }
1399 }
1400
1401 return trigger_device(m, NULL);
1402 }
1403
1404 static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1405 const char *sysfs, *seat;
1406 Manager *m = userdata;
1407 int interactive, r;
1408
1409 assert(message);
1410 assert(m);
1411
1412 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1413 if (r < 0)
1414 return r;
1415
1416 if (!path_is_normalized(sysfs))
1417 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", sysfs);
1418 if (!path_startswith(sysfs, "/sys"))
1419 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1420
1421 if (SEAT_IS_SELF(seat) || SEAT_IS_AUTO(seat)) {
1422 Seat *found;
1423
1424 r = manager_get_seat_from_creds(m, message, seat, error, &found);
1425 if (r < 0)
1426 return r;
1427
1428 seat = found->id;
1429
1430 } else if (!seat_name_is_valid(seat)) /* Note that a seat does not have to exist yet for this operation to succeed */
1431 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat name %s is not valid", seat);
1432
1433 r = bus_verify_polkit_async(
1434 message,
1435 CAP_SYS_ADMIN,
1436 "org.freedesktop.login1.attach-device",
1437 NULL,
1438 interactive,
1439 UID_INVALID,
1440 &m->polkit_registry,
1441 error);
1442 if (r < 0)
1443 return r;
1444 if (r == 0)
1445 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1446
1447 r = attach_device(m, seat, sysfs);
1448 if (r < 0)
1449 return r;
1450
1451 return sd_bus_reply_method_return(message, NULL);
1452 }
1453
1454 static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1455 Manager *m = userdata;
1456 int interactive, r;
1457
1458 assert(message);
1459 assert(m);
1460
1461 r = sd_bus_message_read(message, "b", &interactive);
1462 if (r < 0)
1463 return r;
1464
1465 r = bus_verify_polkit_async(
1466 message,
1467 CAP_SYS_ADMIN,
1468 "org.freedesktop.login1.flush-devices",
1469 NULL,
1470 interactive,
1471 UID_INVALID,
1472 &m->polkit_registry,
1473 error);
1474 if (r < 0)
1475 return r;
1476 if (r == 0)
1477 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1478
1479 r = flush_devices(m);
1480 if (r < 0)
1481 return r;
1482
1483 return sd_bus_reply_method_return(message, NULL);
1484 }
1485
1486 static int have_multiple_sessions(
1487 Manager *m,
1488 uid_t uid) {
1489
1490 Session *session;
1491
1492 assert(m);
1493
1494 /* Check for other users' sessions. Greeter sessions do not
1495 * count, and non-login sessions do not count either. */
1496 HASHMAP_FOREACH(session, m->sessions)
1497 if (session->class == SESSION_USER &&
1498 session->user->user_record->uid != uid)
1499 return true;
1500
1501 return false;
1502 }
1503
1504 static int bus_manager_log_shutdown(
1505 Manager *m,
1506 const char *unit_name) {
1507
1508 const char *p, *q;
1509
1510 assert(m);
1511 assert(unit_name);
1512
1513 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1514 p = "MESSAGE=System is powering down";
1515 q = "SHUTDOWN=power-off";
1516 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1517 p = "MESSAGE=System is rebooting";
1518 q = "SHUTDOWN=reboot";
1519 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1520 p = "MESSAGE=System is halting";
1521 q = "SHUTDOWN=halt";
1522 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1523 p = "MESSAGE=System is rebooting with kexec";
1524 q = "SHUTDOWN=kexec";
1525 } else {
1526 p = "MESSAGE=System is shutting down";
1527 q = NULL;
1528 }
1529
1530 if (isempty(m->wall_message))
1531 p = strjoina(p, ".");
1532 else
1533 p = strjoina(p, " (", m->wall_message, ").");
1534
1535 return log_struct(LOG_NOTICE,
1536 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
1537 p,
1538 q);
1539 }
1540
1541 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1542 Manager *m = userdata;
1543
1544 assert(e);
1545 assert(m);
1546
1547 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1548 return 0;
1549 }
1550
1551 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1552 int r;
1553
1554 assert(m);
1555
1556 if (until <= now(CLOCK_MONOTONIC))
1557 return 0;
1558
1559 /* We want to ignore the lid switch for a while after each
1560 * suspend, and after boot-up. Hence let's install a timer for
1561 * this. As long as the event source exists we ignore the lid
1562 * switch. */
1563
1564 if (m->lid_switch_ignore_event_source) {
1565 usec_t u;
1566
1567 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1568 if (r < 0)
1569 return r;
1570
1571 if (until <= u)
1572 return 0;
1573
1574 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1575 } else
1576 r = sd_event_add_time(
1577 m->event,
1578 &m->lid_switch_ignore_event_source,
1579 CLOCK_MONOTONIC,
1580 until, 0,
1581 lid_switch_ignore_handler, m);
1582
1583 return r;
1584 }
1585
1586 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1587 int active = _active;
1588
1589 assert(m);
1590 assert(IN_SET(w, INHIBIT_SHUTDOWN, INHIBIT_SLEEP));
1591
1592 return sd_bus_emit_signal(m->bus,
1593 "/org/freedesktop/login1",
1594 "org.freedesktop.login1.Manager",
1595 w == INHIBIT_SHUTDOWN ? "PrepareForShutdown" : "PrepareForSleep",
1596 "b",
1597 active);
1598 }
1599
1600 static int execute_shutdown_or_sleep(
1601 Manager *m,
1602 InhibitWhat w,
1603 const char *unit_name,
1604 sd_bus_error *error) {
1605
1606 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1607 const char *p;
1608 int r;
1609
1610 assert(m);
1611 assert(w > 0);
1612 assert(w < _INHIBIT_WHAT_MAX);
1613 assert(unit_name);
1614
1615 if (w == INHIBIT_SHUTDOWN)
1616 bus_manager_log_shutdown(m, unit_name);
1617
1618 r = bus_call_method(
1619 m->bus,
1620 bus_systemd_mgr,
1621 "StartUnit",
1622 error,
1623 &reply,
1624 "ss", unit_name, "replace-irreversibly");
1625 if (r < 0)
1626 goto error;
1627
1628 r = sd_bus_message_read(reply, "o", &p);
1629 if (r < 0)
1630 goto error;
1631
1632 r = free_and_strdup(&m->action_job, p);
1633 if (r < 0)
1634 goto error;
1635
1636 m->action_unit = unit_name;
1637 m->action_what = w;
1638
1639 /* Make sure the lid switch is ignored for a while */
1640 manager_set_lid_switch_ignore(m, usec_add(now(CLOCK_MONOTONIC), m->holdoff_timeout_usec));
1641
1642 return 0;
1643
1644 error:
1645 /* Tell people that they now may take a lock again */
1646 (void) send_prepare_for(m, w, false);
1647
1648 return r;
1649 }
1650
1651 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1652
1653 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1654 Inhibitor *offending = NULL;
1655 int r;
1656
1657 assert(manager);
1658
1659 if (manager->action_what == 0 || manager->action_job)
1660 return 0;
1661
1662 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1663 _cleanup_free_ char *comm = NULL, *u = NULL;
1664
1665 if (!timeout)
1666 return 0;
1667
1668 (void) get_process_comm(offending->pid, &comm);
1669 u = uid_to_name(offending->uid);
1670
1671 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1672 offending->uid, strna(u),
1673 offending->pid, strna(comm));
1674 }
1675
1676 /* Actually do the operation */
1677 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1678 if (r < 0) {
1679 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1680 bus_error_message(&error, r));
1681
1682 manager->action_unit = NULL;
1683 manager->action_what = 0;
1684 return r;
1685 }
1686
1687 return 1;
1688 }
1689
1690 static int manager_inhibit_timeout_handler(
1691 sd_event_source *s,
1692 uint64_t usec,
1693 void *userdata) {
1694
1695 Manager *manager = userdata;
1696 int r;
1697
1698 assert(manager);
1699 assert(manager->inhibit_timeout_source == s);
1700
1701 r = manager_dispatch_delayed(manager, true);
1702 return (r < 0) ? r : 0;
1703 }
1704
1705 static int delay_shutdown_or_sleep(
1706 Manager *m,
1707 InhibitWhat w,
1708 const char *unit_name) {
1709
1710 int r;
1711
1712 assert(m);
1713 assert(w >= 0);
1714 assert(w < _INHIBIT_WHAT_MAX);
1715 assert(unit_name);
1716
1717 if (m->inhibit_timeout_source) {
1718 r = sd_event_source_set_time_relative(m->inhibit_timeout_source, m->inhibit_delay_max);
1719 if (r < 0)
1720 return log_error_errno(r, "sd_event_source_set_time_relative() failed: %m");
1721
1722 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1723 if (r < 0)
1724 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1725 } else {
1726 r = sd_event_add_time_relative(
1727 m->event,
1728 &m->inhibit_timeout_source,
1729 CLOCK_MONOTONIC, m->inhibit_delay_max, 0,
1730 manager_inhibit_timeout_handler, m);
1731 if (r < 0)
1732 return r;
1733 }
1734
1735 m->action_unit = unit_name;
1736 m->action_what = w;
1737
1738 return 0;
1739 }
1740
1741 int bus_manager_shutdown_or_sleep_now_or_later(
1742 Manager *m,
1743 const char *unit_name,
1744 InhibitWhat w,
1745 sd_bus_error *error) {
1746
1747 _cleanup_free_ char *load_state = NULL;
1748 bool delayed;
1749 int r;
1750
1751 assert(m);
1752 assert(unit_name);
1753 assert(w > 0);
1754 assert(w < _INHIBIT_WHAT_MAX);
1755 assert(!m->action_job);
1756
1757 r = unit_load_state(m->bus, unit_name, &load_state);
1758 if (r < 0)
1759 return r;
1760
1761 if (!streq(load_state, "loaded"))
1762 return log_notice_errno(SYNTHETIC_ERRNO(EACCES),
1763 "Unit %s is %s, refusing operation.",
1764 unit_name, load_state);
1765
1766 /* Tell everybody to prepare for shutdown/sleep */
1767 (void) send_prepare_for(m, w, true);
1768
1769 delayed =
1770 m->inhibit_delay_max > 0 &&
1771 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1772
1773 if (delayed)
1774 /* Shutdown is delayed, keep in mind what we
1775 * want to do, and start a timeout */
1776 r = delay_shutdown_or_sleep(m, w, unit_name);
1777 else
1778 /* Shutdown is not delayed, execute it
1779 * immediately */
1780 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1781
1782 return r;
1783 }
1784
1785 static int verify_shutdown_creds(
1786 Manager *m,
1787 sd_bus_message *message,
1788 InhibitWhat w,
1789 const char *action,
1790 const char *action_multiple_sessions,
1791 const char *action_ignore_inhibit,
1792 uint64_t flags,
1793 sd_bus_error *error) {
1794
1795 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1796 bool multiple_sessions, blocked, interactive;
1797 uid_t uid;
1798 int r;
1799
1800 assert(m);
1801 assert(message);
1802 assert(w >= 0);
1803 assert(w <= _INHIBIT_WHAT_MAX);
1804
1805 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1806 if (r < 0)
1807 return r;
1808
1809 r = sd_bus_creds_get_euid(creds, &uid);
1810 if (r < 0)
1811 return r;
1812
1813 r = have_multiple_sessions(m, uid);
1814 if (r < 0)
1815 return r;
1816
1817 multiple_sessions = r > 0;
1818 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1819 interactive = flags & SD_LOGIND_INTERACTIVE;
1820
1821 if (multiple_sessions && action_multiple_sessions) {
1822 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1823 if (r < 0)
1824 return r;
1825 if (r == 0)
1826 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1827 }
1828
1829 if (blocked) {
1830 /* We don't check polkit for root here, because you can't be more privileged than root */
1831 if (uid == 0 && (flags & SD_LOGIND_ROOT_CHECK_INHIBITORS))
1832 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED,
1833 "Access denied to root due to active block inhibitor");
1834
1835 if (action_ignore_inhibit) {
1836 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, 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
1844 if (!multiple_sessions && !blocked && action) {
1845 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1846 if (r < 0)
1847 return r;
1848 if (r == 0)
1849 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1850 }
1851
1852 return 0;
1853 }
1854
1855 static int method_do_shutdown_or_sleep(
1856 Manager *m,
1857 sd_bus_message *message,
1858 const char *unit_name,
1859 InhibitWhat w,
1860 const char *action,
1861 const char *action_multiple_sessions,
1862 const char *action_ignore_inhibit,
1863 const char *sleep_verb,
1864 bool with_flags,
1865 sd_bus_error *error) {
1866
1867 uint64_t flags;
1868 int r;
1869
1870 assert(m);
1871 assert(message);
1872 assert(unit_name);
1873 assert(w >= 0);
1874 assert(w <= _INHIBIT_WHAT_MAX);
1875
1876 if (with_flags) {
1877 /* New style method: with flags parameter (and interactive bool in the bus message header) */
1878 r = sd_bus_message_read(message, "t", &flags);
1879 if (r < 0)
1880 return r;
1881 if ((flags & ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC) != 0)
1882 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
1883 } else {
1884 /* Old style method: no flags parameter, but interactive bool passed as boolean in
1885 * payload. Let's convert this argument to the new-style flags parameter for our internal
1886 * use. */
1887 int interactive;
1888
1889 r = sd_bus_message_read(message, "b", &interactive);
1890 if (r < 0)
1891 return r;
1892
1893 flags = interactive ? SD_LOGIND_INTERACTIVE : 0;
1894 }
1895
1896 /* Don't allow multiple jobs being executed at the same time */
1897 if (m->action_what > 0)
1898 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS,
1899 "There's already a shutdown or sleep operation in progress");
1900
1901 if (sleep_verb) {
1902 r = can_sleep(sleep_verb);
1903 if (r == -ENOSPC)
1904 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1905 "Not enough swap space for hibernation");
1906 if (r == 0)
1907 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1908 "Sleep verb \"%s\" not supported", sleep_verb);
1909 if (r < 0)
1910 return r;
1911 }
1912
1913 r = verify_shutdown_creds(m, message, w, action, action_multiple_sessions,
1914 action_ignore_inhibit, flags, error);
1915 if (r != 0)
1916 return r;
1917
1918 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
1919 if (r < 0)
1920 return r;
1921
1922 return sd_bus_reply_method_return(message, NULL);
1923 }
1924
1925 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1926 Manager *m = userdata;
1927
1928 return method_do_shutdown_or_sleep(
1929 m, message,
1930 SPECIAL_POWEROFF_TARGET,
1931 INHIBIT_SHUTDOWN,
1932 "org.freedesktop.login1.power-off",
1933 "org.freedesktop.login1.power-off-multiple-sessions",
1934 "org.freedesktop.login1.power-off-ignore-inhibit",
1935 NULL,
1936 sd_bus_message_is_method_call(message, NULL, "PowerOffWithFlags"),
1937 error);
1938 }
1939
1940 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1941 Manager *m = userdata;
1942
1943 return method_do_shutdown_or_sleep(
1944 m, message,
1945 SPECIAL_REBOOT_TARGET,
1946 INHIBIT_SHUTDOWN,
1947 "org.freedesktop.login1.reboot",
1948 "org.freedesktop.login1.reboot-multiple-sessions",
1949 "org.freedesktop.login1.reboot-ignore-inhibit",
1950 NULL,
1951 sd_bus_message_is_method_call(message, NULL, "RebootWithFlags"),
1952 error);
1953 }
1954
1955 static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1956 Manager *m = userdata;
1957
1958 return method_do_shutdown_or_sleep(
1959 m, message,
1960 SPECIAL_HALT_TARGET,
1961 INHIBIT_SHUTDOWN,
1962 "org.freedesktop.login1.halt",
1963 "org.freedesktop.login1.halt-multiple-sessions",
1964 "org.freedesktop.login1.halt-ignore-inhibit",
1965 NULL,
1966 sd_bus_message_is_method_call(message, NULL, "HaltWithFlags"),
1967 error);
1968 }
1969
1970 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1971 Manager *m = userdata;
1972
1973 return method_do_shutdown_or_sleep(
1974 m, message,
1975 SPECIAL_SUSPEND_TARGET,
1976 INHIBIT_SLEEP,
1977 "org.freedesktop.login1.suspend",
1978 "org.freedesktop.login1.suspend-multiple-sessions",
1979 "org.freedesktop.login1.suspend-ignore-inhibit",
1980 "suspend",
1981 sd_bus_message_is_method_call(message, NULL, "SuspendWithFlags"),
1982 error);
1983 }
1984
1985 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1986 Manager *m = userdata;
1987
1988 return method_do_shutdown_or_sleep(
1989 m, message,
1990 SPECIAL_HIBERNATE_TARGET,
1991 INHIBIT_SLEEP,
1992 "org.freedesktop.login1.hibernate",
1993 "org.freedesktop.login1.hibernate-multiple-sessions",
1994 "org.freedesktop.login1.hibernate-ignore-inhibit",
1995 "hibernate",
1996 sd_bus_message_is_method_call(message, NULL, "HibernateWithFlags"),
1997 error);
1998 }
1999
2000 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2001 Manager *m = userdata;
2002
2003 return method_do_shutdown_or_sleep(
2004 m, message,
2005 SPECIAL_HYBRID_SLEEP_TARGET,
2006 INHIBIT_SLEEP,
2007 "org.freedesktop.login1.hibernate",
2008 "org.freedesktop.login1.hibernate-multiple-sessions",
2009 "org.freedesktop.login1.hibernate-ignore-inhibit",
2010 "hybrid-sleep",
2011 sd_bus_message_is_method_call(message, NULL, "HybridSleepWithFlags"),
2012 error);
2013 }
2014
2015 static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2016 Manager *m = userdata;
2017
2018 return method_do_shutdown_or_sleep(
2019 m, message,
2020 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
2021 INHIBIT_SLEEP,
2022 "org.freedesktop.login1.hibernate",
2023 "org.freedesktop.login1.hibernate-multiple-sessions",
2024 "org.freedesktop.login1.hibernate-ignore-inhibit",
2025 "hybrid-sleep",
2026 sd_bus_message_is_method_call(message, NULL, "SuspendThenHibernateWithFlags"),
2027 error);
2028 }
2029
2030 static int nologin_timeout_handler(
2031 sd_event_source *s,
2032 uint64_t usec,
2033 void *userdata) {
2034
2035 Manager *m = userdata;
2036
2037 log_info("Creating /run/nologin, blocking further logins...");
2038
2039 m->unlink_nologin =
2040 create_shutdown_run_nologin_or_warn() >= 0;
2041
2042 return 0;
2043 }
2044
2045 static int update_schedule_file(Manager *m) {
2046 _cleanup_free_ char *temp_path = NULL;
2047 _cleanup_fclose_ FILE *f = NULL;
2048 int r;
2049
2050 assert(m);
2051
2052 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
2053 if (r < 0)
2054 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
2055
2056 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
2057 if (r < 0)
2058 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
2059
2060 (void) fchmod(fileno(f), 0644);
2061
2062 fprintf(f,
2063 "USEC="USEC_FMT"\n"
2064 "WARN_WALL=%i\n"
2065 "MODE=%s\n",
2066 m->scheduled_shutdown_timeout,
2067 m->enable_wall_messages,
2068 m->scheduled_shutdown_type);
2069
2070 if (!isempty(m->wall_message)) {
2071 _cleanup_free_ char *t;
2072
2073 t = cescape(m->wall_message);
2074 if (!t) {
2075 r = -ENOMEM;
2076 goto fail;
2077 }
2078
2079 fprintf(f, "WALL_MESSAGE=%s\n", t);
2080 }
2081
2082 r = fflush_and_check(f);
2083 if (r < 0)
2084 goto fail;
2085
2086 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
2087 r = -errno;
2088 goto fail;
2089 }
2090
2091 return 0;
2092
2093 fail:
2094 (void) unlink(temp_path);
2095 (void) unlink("/run/systemd/shutdown/scheduled");
2096
2097 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
2098 }
2099
2100 static void reset_scheduled_shutdown(Manager *m) {
2101 assert(m);
2102
2103 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2104 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
2105 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
2106
2107 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
2108 m->scheduled_shutdown_timeout = 0;
2109 m->shutdown_dry_run = false;
2110
2111 if (m->unlink_nologin) {
2112 (void) unlink_or_warn("/run/nologin");
2113 m->unlink_nologin = false;
2114 }
2115
2116 (void) unlink("/run/systemd/shutdown/scheduled");
2117 }
2118
2119 static int manager_scheduled_shutdown_handler(
2120 sd_event_source *s,
2121 uint64_t usec,
2122 void *userdata) {
2123
2124 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2125 Manager *m = userdata;
2126 const char *target;
2127 int r;
2128
2129 assert(m);
2130
2131 if (isempty(m->scheduled_shutdown_type))
2132 return 0;
2133
2134 if (streq(m->scheduled_shutdown_type, "poweroff"))
2135 target = SPECIAL_POWEROFF_TARGET;
2136 else if (streq(m->scheduled_shutdown_type, "reboot"))
2137 target = SPECIAL_REBOOT_TARGET;
2138 else if (streq(m->scheduled_shutdown_type, "halt"))
2139 target = SPECIAL_HALT_TARGET;
2140 else
2141 assert_not_reached("unexpected shutdown type");
2142
2143 /* Don't allow multiple jobs being executed at the same time */
2144 if (m->action_what > 0) {
2145 r = -EALREADY;
2146 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target);
2147 goto error;
2148 }
2149
2150 if (m->shutdown_dry_run) {
2151 /* We do not process delay inhibitors here. Otherwise, we
2152 * would have to be considered "in progress" (like the check
2153 * above) for some seconds after our admin has seen the final
2154 * wall message. */
2155
2156 bus_manager_log_shutdown(m, target);
2157 log_info("Running in dry run, suppressing action.");
2158 reset_scheduled_shutdown(m);
2159
2160 return 0;
2161 }
2162
2163 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error);
2164 if (r < 0) {
2165 log_error_errno(r, "Scheduled shutdown to %s failed: %m", target);
2166 goto error;
2167 }
2168
2169 return 0;
2170
2171 error:
2172 reset_scheduled_shutdown(m);
2173 return r;
2174 }
2175
2176 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2177 Manager *m = userdata;
2178 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2179 const char *action_multiple_sessions = NULL;
2180 const char *action_ignore_inhibit = NULL;
2181 const char *action = NULL;
2182 uint64_t elapse;
2183 char *type;
2184 int r;
2185 bool dry_run = false;
2186
2187 assert(m);
2188 assert(message);
2189
2190 r = sd_bus_message_read(message, "st", &type, &elapse);
2191 if (r < 0)
2192 return r;
2193
2194 if (startswith(type, "dry-")) {
2195 type += 4;
2196 dry_run = true;
2197 }
2198
2199 if (streq(type, "poweroff")) {
2200 action = "org.freedesktop.login1.power-off";
2201 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2202 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2203 } else if (streq(type, "reboot")) {
2204 action = "org.freedesktop.login1.reboot";
2205 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2206 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2207 } else if (streq(type, "halt")) {
2208 action = "org.freedesktop.login1.halt";
2209 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2210 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2211 } else
2212 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2213
2214 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, action, action_multiple_sessions,
2215 action_ignore_inhibit, 0, error);
2216 if (r != 0)
2217 return r;
2218
2219 if (m->scheduled_shutdown_timeout_source) {
2220 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2221 if (r < 0)
2222 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2223
2224 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2225 if (r < 0)
2226 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2227 } else {
2228 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2229 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2230 if (r < 0)
2231 return log_error_errno(r, "sd_event_add_time() failed: %m");
2232 }
2233
2234 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2235 if (r < 0) {
2236 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2237 return log_oom();
2238 }
2239
2240 m->shutdown_dry_run = dry_run;
2241
2242 if (m->nologin_timeout_source) {
2243 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2244 if (r < 0)
2245 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2246
2247 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2248 if (r < 0)
2249 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2250 } else {
2251 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2252 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2253 if (r < 0)
2254 return log_error_errno(r, "sd_event_add_time() failed: %m");
2255 }
2256
2257 m->scheduled_shutdown_timeout = elapse;
2258
2259 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2260 if (r >= 0) {
2261 const char *tty = NULL;
2262
2263 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2264 (void) sd_bus_creds_get_tty(creds, &tty);
2265
2266 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2267 if (r < 0) {
2268 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2269 return log_oom();
2270 }
2271 }
2272
2273 r = manager_setup_wall_message_timer(m);
2274 if (r < 0)
2275 return r;
2276
2277 r = update_schedule_file(m);
2278 if (r < 0)
2279 return r;
2280
2281 return sd_bus_reply_method_return(message, NULL);
2282 }
2283
2284 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2285 Manager *m = userdata;
2286 bool cancelled;
2287
2288 assert(m);
2289 assert(message);
2290
2291 cancelled = m->scheduled_shutdown_type != NULL;
2292 reset_scheduled_shutdown(m);
2293
2294 if (cancelled && m->enable_wall_messages) {
2295 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2296 _cleanup_free_ char *username = NULL;
2297 const char *tty = NULL;
2298 uid_t uid = 0;
2299 int r;
2300
2301 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2302 if (r >= 0) {
2303 (void) sd_bus_creds_get_uid(creds, &uid);
2304 (void) sd_bus_creds_get_tty(creds, &tty);
2305 }
2306
2307 username = uid_to_name(uid);
2308 utmp_wall("The system shutdown has been cancelled",
2309 username, tty, logind_wall_tty_filter, m);
2310 }
2311
2312 return sd_bus_reply_method_return(message, "b", cancelled);
2313 }
2314
2315 static int method_can_shutdown_or_sleep(
2316 Manager *m,
2317 sd_bus_message *message,
2318 InhibitWhat w,
2319 const char *action,
2320 const char *action_multiple_sessions,
2321 const char *action_ignore_inhibit,
2322 const char *sleep_verb,
2323 sd_bus_error *error) {
2324
2325 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2326 HandleAction handle;
2327 bool multiple_sessions, challenge, blocked;
2328 const char *result = NULL;
2329 uid_t uid;
2330 int r;
2331
2332 assert(m);
2333 assert(message);
2334 assert(w >= 0);
2335 assert(w <= _INHIBIT_WHAT_MAX);
2336 assert(action);
2337 assert(action_multiple_sessions);
2338 assert(action_ignore_inhibit);
2339
2340 if (sleep_verb) {
2341 r = can_sleep(sleep_verb);
2342 if (IN_SET(r, 0, -ENOSPC))
2343 return sd_bus_reply_method_return(message, "s", "na");
2344 if (r < 0)
2345 return r;
2346 }
2347
2348 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2349 if (r < 0)
2350 return r;
2351
2352 r = sd_bus_creds_get_euid(creds, &uid);
2353 if (r < 0)
2354 return r;
2355
2356 r = have_multiple_sessions(m, uid);
2357 if (r < 0)
2358 return r;
2359
2360 multiple_sessions = r > 0;
2361 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2362
2363 handle = handle_action_from_string(sleep_verb);
2364 if (handle >= 0) {
2365 const char *target;
2366
2367 target = manager_target_for_action(handle);
2368 if (target) {
2369 _cleanup_free_ char *load_state = NULL;
2370
2371 r = unit_load_state(m->bus, target, &load_state);
2372 if (r < 0)
2373 return r;
2374
2375 if (!streq(load_state, "loaded")) {
2376 result = "no";
2377 goto finish;
2378 }
2379 }
2380 }
2381
2382 if (multiple_sessions) {
2383 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2384 if (r < 0)
2385 return r;
2386
2387 if (r > 0)
2388 result = "yes";
2389 else if (challenge)
2390 result = "challenge";
2391 else
2392 result = "no";
2393 }
2394
2395 if (blocked) {
2396 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2397 if (r < 0)
2398 return r;
2399
2400 if (r > 0) {
2401 if (!result)
2402 result = "yes";
2403 } else if (challenge) {
2404 if (!result || streq(result, "yes"))
2405 result = "challenge";
2406 } else
2407 result = "no";
2408 }
2409
2410 if (!multiple_sessions && !blocked) {
2411 /* If neither inhibit nor multiple sessions
2412 * apply then just check the normal policy */
2413
2414 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2415 if (r < 0)
2416 return r;
2417
2418 if (r > 0)
2419 result = "yes";
2420 else if (challenge)
2421 result = "challenge";
2422 else
2423 result = "no";
2424 }
2425
2426 finish:
2427 return sd_bus_reply_method_return(message, "s", result);
2428 }
2429
2430 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2431 Manager *m = userdata;
2432
2433 return method_can_shutdown_or_sleep(
2434 m, message,
2435 INHIBIT_SHUTDOWN,
2436 "org.freedesktop.login1.power-off",
2437 "org.freedesktop.login1.power-off-multiple-sessions",
2438 "org.freedesktop.login1.power-off-ignore-inhibit",
2439 NULL,
2440 error);
2441 }
2442
2443 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2444 Manager *m = userdata;
2445
2446 return method_can_shutdown_or_sleep(
2447 m, message,
2448 INHIBIT_SHUTDOWN,
2449 "org.freedesktop.login1.reboot",
2450 "org.freedesktop.login1.reboot-multiple-sessions",
2451 "org.freedesktop.login1.reboot-ignore-inhibit",
2452 NULL,
2453 error);
2454 }
2455
2456 static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2457 Manager *m = userdata;
2458
2459 return method_can_shutdown_or_sleep(
2460 m, message,
2461 INHIBIT_SHUTDOWN,
2462 "org.freedesktop.login1.halt",
2463 "org.freedesktop.login1.halt-multiple-sessions",
2464 "org.freedesktop.login1.halt-ignore-inhibit",
2465 NULL,
2466 error);
2467 }
2468
2469 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2470 Manager *m = userdata;
2471
2472 return method_can_shutdown_or_sleep(
2473 m, message,
2474 INHIBIT_SLEEP,
2475 "org.freedesktop.login1.suspend",
2476 "org.freedesktop.login1.suspend-multiple-sessions",
2477 "org.freedesktop.login1.suspend-ignore-inhibit",
2478 "suspend",
2479 error);
2480 }
2481
2482 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2483 Manager *m = userdata;
2484
2485 return method_can_shutdown_or_sleep(
2486 m, message,
2487 INHIBIT_SLEEP,
2488 "org.freedesktop.login1.hibernate",
2489 "org.freedesktop.login1.hibernate-multiple-sessions",
2490 "org.freedesktop.login1.hibernate-ignore-inhibit",
2491 "hibernate",
2492 error);
2493 }
2494
2495 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2496 Manager *m = userdata;
2497
2498 return method_can_shutdown_or_sleep(
2499 m, message,
2500 INHIBIT_SLEEP,
2501 "org.freedesktop.login1.hibernate",
2502 "org.freedesktop.login1.hibernate-multiple-sessions",
2503 "org.freedesktop.login1.hibernate-ignore-inhibit",
2504 "hybrid-sleep",
2505 error);
2506 }
2507
2508 static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2509 Manager *m = userdata;
2510
2511 return method_can_shutdown_or_sleep(
2512 m, message,
2513 INHIBIT_SLEEP,
2514 "org.freedesktop.login1.hibernate",
2515 "org.freedesktop.login1.hibernate-multiple-sessions",
2516 "org.freedesktop.login1.hibernate-ignore-inhibit",
2517 "suspend-then-hibernate",
2518 error);
2519 }
2520
2521 static int property_get_reboot_parameter(
2522 sd_bus *bus,
2523 const char *path,
2524 const char *interface,
2525 const char *property,
2526 sd_bus_message *reply,
2527 void *userdata,
2528 sd_bus_error *error) {
2529 _cleanup_free_ char *parameter = NULL;
2530 int r;
2531
2532 assert(bus);
2533 assert(reply);
2534 assert(userdata);
2535
2536 r = read_reboot_parameter(&parameter);
2537 if (r < 0)
2538 return r;
2539
2540 return sd_bus_message_append(reply, "s", parameter);
2541 }
2542
2543 static int method_set_reboot_parameter(
2544 sd_bus_message *message,
2545 void *userdata,
2546 sd_bus_error *error) {
2547
2548 Manager *m = userdata;
2549 const char *arg;
2550 int r;
2551
2552 assert(message);
2553 assert(m);
2554
2555 r = sd_bus_message_read(message, "s", &arg);
2556 if (r < 0)
2557 return r;
2558
2559 r = detect_container();
2560 if (r < 0)
2561 return r;
2562 if (r > 0)
2563 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED,
2564 "Reboot parameter not supported in containers, refusing.");
2565
2566 r = bus_verify_polkit_async(message,
2567 CAP_SYS_ADMIN,
2568 "org.freedesktop.login1.set-reboot-parameter",
2569 NULL,
2570 false,
2571 UID_INVALID,
2572 &m->polkit_registry,
2573 error);
2574 if (r < 0)
2575 return r;
2576 if (r == 0)
2577 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2578
2579 r = update_reboot_parameter_and_warn(arg, false);
2580 if (r < 0)
2581 return r;
2582
2583 return sd_bus_reply_method_return(message, NULL);
2584 }
2585
2586 static int method_can_reboot_parameter(
2587 sd_bus_message *message,
2588 void *userdata,
2589 sd_bus_error *error) {
2590
2591 _unused_ Manager *m = userdata;
2592 int r;
2593
2594 assert(message);
2595 assert(m);
2596
2597 r = detect_container();
2598 if (r < 0)
2599 return r;
2600 if (r > 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2601 return sd_bus_reply_method_return(message, "s", "na");
2602
2603 return return_test_polkit(
2604 message,
2605 CAP_SYS_ADMIN,
2606 "org.freedesktop.login1.set-reboot-parameter",
2607 NULL,
2608 UID_INVALID,
2609 error);
2610 }
2611
2612 static int property_get_reboot_to_firmware_setup(
2613 sd_bus *bus,
2614 const char *path,
2615 const char *interface,
2616 const char *property,
2617 sd_bus_message *reply,
2618 void *userdata,
2619 sd_bus_error *error) {
2620 int r;
2621
2622 assert(bus);
2623 assert(reply);
2624 assert(userdata);
2625
2626 r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2627 if (r == -ENXIO) {
2628 /* EFI case: let's see what is currently configured in the EFI variables */
2629 r = efi_get_reboot_to_firmware();
2630 if (r < 0 && r != -EOPNOTSUPP)
2631 log_warning_errno(r, "Failed to determine reboot-to-firmware-setup state: %m");
2632 } else if (r < 0)
2633 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2634 else if (r > 0) {
2635 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2636 if (access("/run/systemd/reboot-to-firmware-setup", F_OK) < 0) {
2637 if (errno != ENOENT)
2638 log_warning_errno(errno, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2639
2640 r = false;
2641 } else
2642 r = true;
2643 }
2644
2645 return sd_bus_message_append(reply, "b", r > 0);
2646 }
2647
2648 static int method_set_reboot_to_firmware_setup(
2649 sd_bus_message *message,
2650 void *userdata,
2651 sd_bus_error *error) {
2652
2653 Manager *m = userdata;
2654 bool use_efi;
2655 int b, r;
2656
2657 assert(message);
2658 assert(m);
2659
2660 r = sd_bus_message_read(message, "b", &b);
2661 if (r < 0)
2662 return r;
2663
2664 r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2665 if (r == -ENXIO) {
2666 /* EFI case: let's see what the firmware supports */
2667
2668 r = efi_reboot_to_firmware_supported();
2669 if (r == -EOPNOTSUPP)
2670 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware.");
2671 if (r < 0)
2672 return r;
2673
2674 use_efi = true;
2675
2676 } else if (r <= 0) {
2677 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2678
2679 if (r < 0)
2680 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2681
2682 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware.");
2683 } else
2684 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2685 use_efi = false;
2686
2687 r = bus_verify_polkit_async(message,
2688 CAP_SYS_ADMIN,
2689 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2690 NULL,
2691 false,
2692 UID_INVALID,
2693 &m->polkit_registry,
2694 error);
2695 if (r < 0)
2696 return r;
2697 if (r == 0)
2698 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2699
2700 if (use_efi) {
2701 r = efi_set_reboot_to_firmware(b);
2702 if (r < 0)
2703 return r;
2704 } else {
2705 if (b) {
2706 r = touch("/run/systemd/reboot-to-firmware-setup");
2707 if (r < 0)
2708 return r;
2709 } else {
2710 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno != ENOENT)
2711 return -errno;
2712 }
2713 }
2714
2715 return sd_bus_reply_method_return(message, NULL);
2716 }
2717
2718 static int method_can_reboot_to_firmware_setup(
2719 sd_bus_message *message,
2720 void *userdata,
2721 sd_bus_error *error) {
2722
2723 _unused_ Manager *m = userdata;
2724 int r;
2725
2726 assert(message);
2727 assert(m);
2728
2729 r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2730 if (r == -ENXIO) {
2731 /* EFI case: let's see what the firmware supports */
2732
2733 r = efi_reboot_to_firmware_supported();
2734 if (r < 0) {
2735 if (r != -EOPNOTSUPP)
2736 log_warning_errno(r, "Failed to determine whether reboot to firmware is supported: %m");
2737
2738 return sd_bus_reply_method_return(message, "s", "na");
2739 }
2740
2741 } else if (r <= 0) {
2742 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2743
2744 if (r < 0)
2745 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2746
2747 return sd_bus_reply_method_return(message, "s", "na");
2748 }
2749
2750 return return_test_polkit(
2751 message,
2752 CAP_SYS_ADMIN,
2753 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2754 NULL,
2755 UID_INVALID,
2756 error);
2757 }
2758
2759 static int property_get_reboot_to_boot_loader_menu(
2760 sd_bus *bus,
2761 const char *path,
2762 const char *interface,
2763 const char *property,
2764 sd_bus_message *reply,
2765 void *userdata,
2766 sd_bus_error *error) {
2767
2768 uint64_t x = UINT64_MAX;
2769 int r;
2770
2771 assert(bus);
2772 assert(reply);
2773 assert(userdata);
2774
2775 r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2776 if (r == -ENXIO) {
2777 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2778 *
2779 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2780 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2781 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2782 */
2783
2784 r = efi_loader_get_config_timeout_one_shot(&x);
2785 if (r < 0) {
2786 if (r != -ENOENT)
2787 log_warning_errno(r, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
2788 }
2789
2790 } else if (r < 0)
2791 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2792 else if (r > 0) {
2793 _cleanup_free_ char *v = NULL;
2794
2795 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2796
2797 r = read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v);
2798 if (r < 0) {
2799 if (r != -ENOENT)
2800 log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2801 } else {
2802 r = safe_atou64(v, &x);
2803 if (r < 0)
2804 log_warning_errno(r, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2805 }
2806 }
2807
2808 return sd_bus_message_append(reply, "t", x);
2809 }
2810
2811 static int method_set_reboot_to_boot_loader_menu(
2812 sd_bus_message *message,
2813 void *userdata,
2814 sd_bus_error *error) {
2815
2816 Manager *m = userdata;
2817 bool use_efi;
2818 uint64_t x;
2819 int r;
2820
2821 assert(message);
2822 assert(m);
2823
2824 r = sd_bus_message_read(message, "t", &x);
2825 if (r < 0)
2826 return r;
2827
2828 r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2829 if (r == -ENXIO) {
2830 uint64_t features;
2831
2832 /* EFI case: let's see if booting into boot loader menu is supported. */
2833
2834 r = efi_loader_get_features(&features);
2835 if (r < 0)
2836 log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m");
2837 if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT))
2838 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu.");
2839
2840 use_efi = true;
2841
2842 } else if (r <= 0) {
2843 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2844
2845 if (r < 0)
2846 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2847
2848 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu.");
2849 } else
2850 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2851 use_efi = false;
2852
2853 r = bus_verify_polkit_async(message,
2854 CAP_SYS_ADMIN,
2855 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2856 NULL,
2857 false,
2858 UID_INVALID,
2859 &m->polkit_registry,
2860 error);
2861 if (r < 0)
2862 return r;
2863 if (r == 0)
2864 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2865
2866 if (use_efi) {
2867 if (x == UINT64_MAX)
2868 r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderConfigTimeoutOneShot", NULL, 0);
2869 else {
2870 char buf[DECIMAL_STR_MAX(uint64_t) + 1];
2871 xsprintf(buf, "%" PRIu64, DIV_ROUND_UP(x, USEC_PER_SEC)); /* second granularity */
2872
2873 r = efi_set_variable_string(EFI_VENDOR_LOADER, "LoaderConfigTimeoutOneShot", buf);
2874 }
2875 if (r < 0)
2876 return r;
2877 } else {
2878 if (x == UINT64_MAX) {
2879 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno != ENOENT)
2880 return -errno;
2881 } else {
2882 char buf[DECIMAL_STR_MAX(uint64_t) + 1];
2883
2884 xsprintf(buf, "%" PRIu64, x); /* µs granularity */
2885
2886 r = write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf);
2887 if (r < 0)
2888 return r;
2889 }
2890 }
2891
2892 return sd_bus_reply_method_return(message, NULL);
2893 }
2894
2895 static int method_can_reboot_to_boot_loader_menu(
2896 sd_bus_message *message,
2897 void *userdata,
2898 sd_bus_error *error) {
2899
2900 _unused_ Manager *m = userdata;
2901 int r;
2902
2903 assert(message);
2904 assert(m);
2905
2906 r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2907 if (r == -ENXIO) {
2908 uint64_t features = 0;
2909
2910 /* EFI case, let's see if booting into boot loader menu is supported. */
2911
2912 r = efi_loader_get_features(&features);
2913 if (r < 0)
2914 log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m");
2915 if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT))
2916 return sd_bus_reply_method_return(message, "s", "na");
2917
2918 } else if (r <= 0) {
2919 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2920
2921 if (r < 0)
2922 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2923
2924 return sd_bus_reply_method_return(message, "s", "na");
2925 }
2926
2927 return return_test_polkit(
2928 message,
2929 CAP_SYS_ADMIN,
2930 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2931 NULL,
2932 UID_INVALID,
2933 error);
2934 }
2935
2936 static int property_get_reboot_to_boot_loader_entry(
2937 sd_bus *bus,
2938 const char *path,
2939 const char *interface,
2940 const char *property,
2941 sd_bus_message *reply,
2942 void *userdata,
2943 sd_bus_error *error) {
2944
2945 _cleanup_free_ char *v = NULL;
2946 Manager *m = userdata;
2947 const char *x = NULL;
2948 int r;
2949
2950 assert(bus);
2951 assert(reply);
2952 assert(m);
2953
2954 r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2955 if (r == -ENXIO) {
2956 /* EFI case: let's read the LoaderEntryOneShot variable */
2957
2958 r = efi_loader_update_entry_one_shot_cache(&m->efi_loader_entry_one_shot, &m->efi_loader_entry_one_shot_stat);
2959 if (r < 0) {
2960 if (r != -ENOENT)
2961 log_warning_errno(r, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
2962 } else
2963 x = m->efi_loader_entry_one_shot;
2964
2965 } else if (r < 0)
2966 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2967 else if (r > 0) {
2968
2969 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2970
2971 r = read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v);
2972 if (r < 0) {
2973 if (r != -ENOENT)
2974 log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
2975 } else if (!efi_loader_entry_name_valid(v))
2976 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2977 else
2978 x = v;
2979 }
2980
2981 return sd_bus_message_append(reply, "s", x);
2982 }
2983
2984 static int boot_loader_entry_exists(Manager *m, const char *id) {
2985 _cleanup_(boot_config_free) BootConfig config = {};
2986 int r;
2987
2988 assert(m);
2989 assert(id);
2990
2991 r = boot_entries_load_config_auto(NULL, NULL, &config);
2992 if (r < 0 && r != -ENOKEY) /* don't complain if no GPT is found, hence skip ENOKEY */
2993 return r;
2994
2995 r = manager_read_efi_boot_loader_entries(m);
2996 if (r >= 0)
2997 (void) boot_entries_augment_from_loader(&config, m->efi_boot_loader_entries, true);
2998
2999 return boot_config_has_entry(&config, id);
3000 }
3001
3002 static int method_set_reboot_to_boot_loader_entry(
3003 sd_bus_message *message,
3004 void *userdata,
3005 sd_bus_error *error) {
3006
3007 Manager *m = userdata;
3008 bool use_efi;
3009 const char *v;
3010 int r;
3011
3012 assert(message);
3013 assert(m);
3014
3015 r = sd_bus_message_read(message, "s", &v);
3016 if (r < 0)
3017 return r;
3018
3019 if (isempty(v))
3020 v = NULL;
3021 else if (efi_loader_entry_name_valid(v)) {
3022 r = boot_loader_entry_exists(m, v);
3023 if (r < 0)
3024 return r;
3025 if (r == 0)
3026 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader entry '%s' is not known.", v);
3027 } else
3028 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Boot loader entry name '%s' is not valid, refusing.", v);
3029
3030 r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3031 if (r == -ENXIO) {
3032 uint64_t features;
3033
3034 /* EFI case: let's see if booting into boot loader entry is supported. */
3035
3036 r = efi_loader_get_features(&features);
3037 if (r < 0)
3038 log_warning_errno(r, "Failed to determine whether reboot into boot loader entry is supported: %m");
3039 if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT))
3040 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry.");
3041
3042 use_efi = true;
3043
3044 } else if (r <= 0) {
3045 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3046
3047 if (r < 0)
3048 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3049
3050 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry.");
3051 } else
3052 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3053 use_efi = false;
3054
3055 r = bus_verify_polkit_async(message,
3056 CAP_SYS_ADMIN,
3057 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3058 NULL,
3059 false,
3060 UID_INVALID,
3061 &m->polkit_registry,
3062 error);
3063 if (r < 0)
3064 return r;
3065 if (r == 0)
3066 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3067
3068 if (use_efi) {
3069 if (isempty(v))
3070 /* Delete item */
3071 r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderEntryOneShot", NULL, 0);
3072 else
3073 r = efi_set_variable_string(EFI_VENDOR_LOADER, "LoaderEntryOneShot", v);
3074 if (r < 0)
3075 return r;
3076 } else {
3077 if (isempty(v)) {
3078 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno != ENOENT)
3079 return -errno;
3080 } else {
3081 r = write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v);
3082 if (r < 0)
3083 return r;
3084 }
3085 }
3086
3087 return sd_bus_reply_method_return(message, NULL);
3088 }
3089
3090 static int method_can_reboot_to_boot_loader_entry(
3091 sd_bus_message *message,
3092 void *userdata,
3093 sd_bus_error *error) {
3094
3095 _unused_ Manager *m = userdata;
3096 int r;
3097
3098 assert(message);
3099 assert(m);
3100
3101 r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3102 if (r == -ENXIO) {
3103 uint64_t features = 0;
3104
3105 /* EFI case, let's see if booting into boot loader entry is supported. */
3106
3107 r = efi_loader_get_features(&features);
3108 if (r < 0)
3109 log_warning_errno(r, "Failed to determine whether reboot to boot loader entry is supported: %m");
3110 if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT))
3111 return sd_bus_reply_method_return(message, "s", "na");
3112
3113 } else if (r <= 0) {
3114 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3115
3116 if (r < 0)
3117 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3118
3119 return sd_bus_reply_method_return(message, "s", "na");
3120 }
3121
3122 return return_test_polkit(
3123 message,
3124 CAP_SYS_ADMIN,
3125 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3126 NULL,
3127 UID_INVALID,
3128 error);
3129 }
3130
3131 static int property_get_boot_loader_entries(
3132 sd_bus *bus,
3133 const char *path,
3134 const char *interface,
3135 const char *property,
3136 sd_bus_message *reply,
3137 void *userdata,
3138 sd_bus_error *error) {
3139
3140 _cleanup_(boot_config_free) BootConfig config = {};
3141 Manager *m = userdata;
3142 size_t i;
3143 int r;
3144
3145 assert(bus);
3146 assert(reply);
3147 assert(m);
3148
3149 r = boot_entries_load_config_auto(NULL, NULL, &config);
3150 if (r < 0 && r != -ENOKEY) /* don't complain if there's no GPT found */
3151 return r;
3152
3153 r = manager_read_efi_boot_loader_entries(m);
3154 if (r >= 0)
3155 (void) boot_entries_augment_from_loader(&config, m->efi_boot_loader_entries, true);
3156
3157 r = sd_bus_message_open_container(reply, 'a', "s");
3158 if (r < 0)
3159 return r;
3160
3161 for (i = 0; i < config.n_entries; i++) {
3162 BootEntry *e = config.entries + i;
3163
3164 r = sd_bus_message_append(reply, "s", e->id);
3165 if (r < 0)
3166 return r;
3167 }
3168
3169 return sd_bus_message_close_container(reply);
3170 }
3171
3172 static int method_set_wall_message(
3173 sd_bus_message *message,
3174 void *userdata,
3175 sd_bus_error *error) {
3176
3177 int r;
3178 Manager *m = userdata;
3179 char *wall_message;
3180 unsigned enable_wall_messages;
3181
3182 assert(message);
3183 assert(m);
3184
3185 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
3186 if (r < 0)
3187 return r;
3188
3189 /* Short-circuit the operation if the desired state is already in place, to
3190 * avoid an unnecessary polkit permission check. */
3191 if (streq_ptr(m->wall_message, empty_to_null(wall_message)) &&
3192 m->enable_wall_messages == enable_wall_messages)
3193 goto done;
3194
3195 r = bus_verify_polkit_async(message,
3196 CAP_SYS_ADMIN,
3197 "org.freedesktop.login1.set-wall-message",
3198 NULL,
3199 false,
3200 UID_INVALID,
3201 &m->polkit_registry,
3202 error);
3203 if (r < 0)
3204 return r;
3205 if (r == 0)
3206 return 1; /* Will call us back */
3207
3208 r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
3209 if (r < 0)
3210 return log_oom();
3211
3212 m->enable_wall_messages = enable_wall_messages;
3213
3214 done:
3215 return sd_bus_reply_method_return(message, NULL);
3216 }
3217
3218 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3219 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
3220 const char *who, *why, *what, *mode;
3221 _cleanup_free_ char *id = NULL;
3222 _cleanup_close_ int fifo_fd = -1;
3223 Manager *m = userdata;
3224 InhibitMode mm;
3225 InhibitWhat w;
3226 pid_t pid;
3227 uid_t uid;
3228 int r;
3229
3230 assert(message);
3231 assert(m);
3232
3233 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
3234 if (r < 0)
3235 return r;
3236
3237 w = inhibit_what_from_string(what);
3238 if (w <= 0)
3239 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
3240 "Invalid what specification %s", what);
3241
3242 mm = inhibit_mode_from_string(mode);
3243 if (mm < 0)
3244 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
3245 "Invalid mode specification %s", mode);
3246
3247 /* Delay is only supported for shutdown/sleep */
3248 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
3249 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
3250 "Delay inhibitors only supported for shutdown and sleep");
3251
3252 /* Don't allow taking delay locks while we are already
3253 * executing the operation. We shouldn't create the impression
3254 * that the lock was successful if the machine is about to go
3255 * down/suspend any moment. */
3256 if (m->action_what & w)
3257 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS,
3258 "The operation inhibition has been requested for is already running");
3259
3260 r = bus_verify_polkit_async(
3261 message,
3262 CAP_SYS_BOOT,
3263 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3264 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3265 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
3266 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
3267 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3268 w == INHIBIT_HANDLE_REBOOT_KEY ? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3269 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3270 "org.freedesktop.login1.inhibit-handle-lid-switch",
3271 NULL,
3272 false,
3273 UID_INVALID,
3274 &m->polkit_registry,
3275 error);
3276 if (r < 0)
3277 return r;
3278 if (r == 0)
3279 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3280
3281 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
3282 if (r < 0)
3283 return r;
3284
3285 r = sd_bus_creds_get_euid(creds, &uid);
3286 if (r < 0)
3287 return r;
3288
3289 r = sd_bus_creds_get_pid(creds, &pid);
3290 if (r < 0)
3291 return r;
3292
3293 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
3294 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED,
3295 "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.",
3296 m->inhibitors_max);
3297
3298 do {
3299 id = mfree(id);
3300
3301 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
3302 return -ENOMEM;
3303
3304 } while (hashmap_get(m->inhibitors, id));
3305
3306 _cleanup_(inhibitor_freep) Inhibitor *i = NULL;
3307 r = manager_add_inhibitor(m, id, &i);
3308 if (r < 0)
3309 return r;
3310
3311 i->what = w;
3312 i->mode = mm;
3313 i->pid = pid;
3314 i->uid = uid;
3315 i->why = strdup(why);
3316 i->who = strdup(who);
3317
3318 if (!i->why || !i->who)
3319 return -ENOMEM;
3320
3321 fifo_fd = inhibitor_create_fifo(i);
3322 if (fifo_fd < 0)
3323 return fifo_fd;
3324
3325 r = inhibitor_start(i);
3326 if (r < 0)
3327 return r;
3328 TAKE_PTR(i);
3329
3330 return sd_bus_reply_method_return(message, "h", fifo_fd);
3331 }
3332
3333 static const sd_bus_vtable manager_vtable[] = {
3334 SD_BUS_VTABLE_START(0),
3335
3336 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
3337 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
3338
3339 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
3340 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
3341 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
3342 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
3343 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter, 0, 0),
3344 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, 0),
3345 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu, 0, 0),
3346 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry, 0, 0),
3347 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries, 0, SD_BUS_VTABLE_PROPERTY_CONST),
3348 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
3349 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
3350 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
3351 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
3352 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
3353 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
3354 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL, offsetof(Manager, user_stop_delay), SD_BUS_VTABLE_PROPERTY_CONST),
3355 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
3356 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
3357 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
3358 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
3359 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
3360 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
3361 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
3362 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
3363 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
3364 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
3365 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
3366 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
3367 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
3368 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed, 0, 0),
3369 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power, 0, 0),
3370 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
3371 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
3372 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL, offsetof(Manager, runtime_dir_inodes), SD_BUS_VTABLE_PROPERTY_CONST),
3373 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
3374 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0),
3375 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
3376 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0),
3377 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
3378
3379 SD_BUS_METHOD_WITH_NAMES("GetSession",
3380 "s",
3381 SD_BUS_PARAM(session_id),
3382 "o",
3383 SD_BUS_PARAM(object_path),
3384 method_get_session,
3385 SD_BUS_VTABLE_UNPRIVILEGED),
3386 SD_BUS_METHOD_WITH_NAMES("GetSessionByPID",
3387 "u",
3388 SD_BUS_PARAM(pid),
3389 "o",
3390 SD_BUS_PARAM(object_path),
3391 method_get_session_by_pid,
3392 SD_BUS_VTABLE_UNPRIVILEGED),
3393 SD_BUS_METHOD_WITH_NAMES("GetUser",
3394 "u",
3395 SD_BUS_PARAM(uid),
3396 "o",
3397 SD_BUS_PARAM(object_path),
3398 method_get_user,
3399 SD_BUS_VTABLE_UNPRIVILEGED),
3400 SD_BUS_METHOD_WITH_NAMES("GetUserByPID",
3401 "u",
3402 SD_BUS_PARAM(pid),
3403 "o",
3404 SD_BUS_PARAM(object_path),
3405 method_get_user_by_pid,
3406 SD_BUS_VTABLE_UNPRIVILEGED),
3407 SD_BUS_METHOD_WITH_NAMES("GetSeat",
3408 "s",
3409 SD_BUS_PARAM(seat_id),
3410 "o",
3411 SD_BUS_PARAM(object_path),
3412 method_get_seat,
3413 SD_BUS_VTABLE_UNPRIVILEGED),
3414 SD_BUS_METHOD_WITH_NAMES("ListSessions",
3415 NULL,,
3416 "a(susso)",
3417 SD_BUS_PARAM(sessions),
3418 method_list_sessions,
3419 SD_BUS_VTABLE_UNPRIVILEGED),
3420 SD_BUS_METHOD_WITH_NAMES("ListUsers",
3421 NULL,,
3422 "a(uso)",
3423 SD_BUS_PARAM(users),
3424 method_list_users,
3425 SD_BUS_VTABLE_UNPRIVILEGED),
3426 SD_BUS_METHOD_WITH_NAMES("ListSeats",
3427 NULL,,
3428 "a(so)",
3429 SD_BUS_PARAM(seats),
3430 method_list_seats,
3431 SD_BUS_VTABLE_UNPRIVILEGED),
3432 SD_BUS_METHOD_WITH_NAMES("ListInhibitors",
3433 NULL,,
3434 "a(ssssuu)",
3435 SD_BUS_PARAM(inhibitors),
3436 method_list_inhibitors,
3437 SD_BUS_VTABLE_UNPRIVILEGED),
3438 SD_BUS_METHOD_WITH_NAMES("CreateSession",
3439 "uusssssussbssa(sv)",
3440 SD_BUS_PARAM(uid)
3441 SD_BUS_PARAM(pid)
3442 SD_BUS_PARAM(service)
3443 SD_BUS_PARAM(type)
3444 SD_BUS_PARAM(class)
3445 SD_BUS_PARAM(desktop)
3446 SD_BUS_PARAM(seat_id)
3447 SD_BUS_PARAM(vtnr)
3448 SD_BUS_PARAM(tty)
3449 SD_BUS_PARAM(display)
3450 SD_BUS_PARAM(remote)
3451 SD_BUS_PARAM(remote_user)
3452 SD_BUS_PARAM(remote_host)
3453 SD_BUS_PARAM(properties),
3454 "soshusub",
3455 SD_BUS_PARAM(session_id)
3456 SD_BUS_PARAM(object_path)
3457 SD_BUS_PARAM(runtime_path)
3458 SD_BUS_PARAM(fifo_fd)
3459 SD_BUS_PARAM(uid)
3460 SD_BUS_PARAM(seat_id)
3461 SD_BUS_PARAM(vtnr)
3462 SD_BUS_PARAM(existing),
3463 method_create_session,
3464 0),
3465 SD_BUS_METHOD_WITH_NAMES("ReleaseSession",
3466 "s",
3467 SD_BUS_PARAM(session_id),
3468 NULL,,
3469 method_release_session,
3470 0),
3471 SD_BUS_METHOD_WITH_NAMES("ActivateSession",
3472 "s",
3473 SD_BUS_PARAM(session_id),
3474 NULL,,
3475 method_activate_session,
3476 SD_BUS_VTABLE_UNPRIVILEGED),
3477 SD_BUS_METHOD_WITH_NAMES("ActivateSessionOnSeat",
3478 "ss",
3479 SD_BUS_PARAM(session_id)
3480 SD_BUS_PARAM(seat_id),
3481 NULL,,
3482 method_activate_session_on_seat,
3483 SD_BUS_VTABLE_UNPRIVILEGED),
3484 SD_BUS_METHOD_WITH_NAMES("LockSession",
3485 "s",
3486 SD_BUS_PARAM(session_id),
3487 NULL,,
3488 method_lock_session,
3489 SD_BUS_VTABLE_UNPRIVILEGED),
3490 SD_BUS_METHOD_WITH_NAMES("UnlockSession",
3491 "s",
3492 SD_BUS_PARAM(session_id),
3493 NULL,,
3494 method_lock_session,
3495 SD_BUS_VTABLE_UNPRIVILEGED),
3496 SD_BUS_METHOD("LockSessions",
3497 NULL,
3498 NULL,
3499 method_lock_sessions,
3500 SD_BUS_VTABLE_UNPRIVILEGED),
3501 SD_BUS_METHOD("UnlockSessions",
3502 NULL,
3503 NULL,
3504 method_lock_sessions,
3505 SD_BUS_VTABLE_UNPRIVILEGED),
3506 SD_BUS_METHOD_WITH_NAMES("KillSession",
3507 "ssi",
3508 SD_BUS_PARAM(session_id)
3509 SD_BUS_PARAM(who)
3510 SD_BUS_PARAM(signal_number),
3511 NULL,,
3512 method_kill_session,
3513 SD_BUS_VTABLE_UNPRIVILEGED),
3514 SD_BUS_METHOD_WITH_NAMES("KillUser",
3515 "ui",
3516 SD_BUS_PARAM(uid)
3517 SD_BUS_PARAM(signal_number),
3518 NULL,,
3519 method_kill_user,
3520 SD_BUS_VTABLE_UNPRIVILEGED),
3521 SD_BUS_METHOD_WITH_NAMES("TerminateSession",
3522 "s",
3523 SD_BUS_PARAM(session_id),
3524 NULL,,
3525 method_terminate_session,
3526 SD_BUS_VTABLE_UNPRIVILEGED),
3527 SD_BUS_METHOD_WITH_NAMES("TerminateUser",
3528 "u",
3529 SD_BUS_PARAM(uid),
3530 NULL,,
3531 method_terminate_user,
3532 SD_BUS_VTABLE_UNPRIVILEGED),
3533 SD_BUS_METHOD_WITH_NAMES("TerminateSeat",
3534 "s",
3535 SD_BUS_PARAM(seat_id),
3536 NULL,,
3537 method_terminate_seat,
3538 SD_BUS_VTABLE_UNPRIVILEGED),
3539 SD_BUS_METHOD_WITH_NAMES("SetUserLinger",
3540 "ubb",
3541 SD_BUS_PARAM(uid)
3542 SD_BUS_PARAM(enable)
3543 SD_BUS_PARAM(interactive),
3544 NULL,,
3545 method_set_user_linger,
3546 SD_BUS_VTABLE_UNPRIVILEGED),
3547 SD_BUS_METHOD_WITH_NAMES("AttachDevice",
3548 "ssb",
3549 SD_BUS_PARAM(seat_id)
3550 SD_BUS_PARAM(sysfs_path)
3551 SD_BUS_PARAM(interactive),
3552 NULL,,
3553 method_attach_device,
3554 SD_BUS_VTABLE_UNPRIVILEGED),
3555 SD_BUS_METHOD_WITH_NAMES("FlushDevices",
3556 "b",
3557 SD_BUS_PARAM(interactive),
3558 NULL,,
3559 method_flush_devices,
3560 SD_BUS_VTABLE_UNPRIVILEGED),
3561 SD_BUS_METHOD_WITH_NAMES("PowerOff",
3562 "b",
3563 SD_BUS_PARAM(interactive),
3564 NULL,,
3565 method_poweroff,
3566 SD_BUS_VTABLE_UNPRIVILEGED),
3567 SD_BUS_METHOD_WITH_NAMES("PowerOffWithFlags",
3568 "t",
3569 SD_BUS_PARAM(flags),
3570 NULL,,
3571 method_poweroff,
3572 SD_BUS_VTABLE_UNPRIVILEGED),
3573 SD_BUS_METHOD_WITH_NAMES("Reboot",
3574 "b",
3575 SD_BUS_PARAM(interactive),
3576 NULL,,
3577 method_reboot,
3578 SD_BUS_VTABLE_UNPRIVILEGED),
3579 SD_BUS_METHOD_WITH_NAMES("RebootWithFlags",
3580 "t",
3581 SD_BUS_PARAM(flags),
3582 NULL,,
3583 method_reboot,
3584 SD_BUS_VTABLE_UNPRIVILEGED),
3585 SD_BUS_METHOD_WITH_NAMES("Halt",
3586 "b",
3587 SD_BUS_PARAM(interactive),
3588 NULL,,
3589 method_halt,
3590 SD_BUS_VTABLE_UNPRIVILEGED),
3591 SD_BUS_METHOD_WITH_NAMES("HaltWithFlags",
3592 "t",
3593 SD_BUS_PARAM(flags),
3594 NULL,,
3595 method_halt,
3596 SD_BUS_VTABLE_UNPRIVILEGED),
3597 SD_BUS_METHOD_WITH_NAMES("Suspend",
3598 "b",
3599 SD_BUS_PARAM(interactive),
3600 NULL,,
3601 method_suspend,
3602 SD_BUS_VTABLE_UNPRIVILEGED),
3603 SD_BUS_METHOD_WITH_NAMES("SuspendWithFlags",
3604 "t",
3605 SD_BUS_PARAM(flags),
3606 NULL,,
3607 method_suspend,
3608 SD_BUS_VTABLE_UNPRIVILEGED),
3609 SD_BUS_METHOD_WITH_NAMES("Hibernate",
3610 "b",
3611 SD_BUS_PARAM(interactive),
3612 NULL,,
3613 method_hibernate,
3614 SD_BUS_VTABLE_UNPRIVILEGED),
3615 SD_BUS_METHOD_WITH_NAMES("HibernateWithFlags",
3616 "t",
3617 SD_BUS_PARAM(flags),
3618 NULL,,
3619 method_hibernate,
3620 SD_BUS_VTABLE_UNPRIVILEGED),
3621 SD_BUS_METHOD_WITH_NAMES("HybridSleep",
3622 "b",
3623 SD_BUS_PARAM(interactive),
3624 NULL,,
3625 method_hybrid_sleep,
3626 SD_BUS_VTABLE_UNPRIVILEGED),
3627 SD_BUS_METHOD_WITH_NAMES("HybridSleepWithFlags",
3628 "t",
3629 SD_BUS_PARAM(flags),
3630 NULL,,
3631 method_hybrid_sleep,
3632 SD_BUS_VTABLE_UNPRIVILEGED),
3633 SD_BUS_METHOD_WITH_NAMES("SuspendThenHibernate",
3634 "b",
3635 SD_BUS_PARAM(interactive),
3636 NULL,,
3637 method_suspend_then_hibernate,
3638 SD_BUS_VTABLE_UNPRIVILEGED),
3639 SD_BUS_METHOD_WITH_NAMES("SuspendThenHibernateWithFlags",
3640 "t",
3641 SD_BUS_PARAM(flags),
3642 NULL,,
3643 method_suspend_then_hibernate,
3644 SD_BUS_VTABLE_UNPRIVILEGED),
3645 SD_BUS_METHOD_WITH_NAMES("CanPowerOff",
3646 NULL,,
3647 "s",
3648 SD_BUS_PARAM(result),
3649 method_can_poweroff,
3650 SD_BUS_VTABLE_UNPRIVILEGED),
3651 SD_BUS_METHOD_WITH_NAMES("CanReboot",
3652 NULL,,
3653 "s",
3654 SD_BUS_PARAM(result),
3655 method_can_reboot,
3656 SD_BUS_VTABLE_UNPRIVILEGED),
3657 SD_BUS_METHOD_WITH_NAMES("CanHalt",
3658 NULL,,
3659 "s",
3660 SD_BUS_PARAM(result),
3661 method_can_halt,
3662 SD_BUS_VTABLE_UNPRIVILEGED),
3663 SD_BUS_METHOD_WITH_NAMES("CanSuspend",
3664 NULL,,
3665 "s",
3666 SD_BUS_PARAM(result),
3667 method_can_suspend,
3668 SD_BUS_VTABLE_UNPRIVILEGED),
3669 SD_BUS_METHOD_WITH_NAMES("CanHibernate",
3670 NULL,,
3671 "s",
3672 SD_BUS_PARAM(result),
3673 method_can_hibernate,
3674 SD_BUS_VTABLE_UNPRIVILEGED),
3675 SD_BUS_METHOD_WITH_NAMES("CanHybridSleep",
3676 NULL,,
3677 "s",
3678 SD_BUS_PARAM(result),
3679 method_can_hybrid_sleep,
3680 SD_BUS_VTABLE_UNPRIVILEGED),
3681 SD_BUS_METHOD_WITH_NAMES("CanSuspendThenHibernate",
3682 NULL,,
3683 "s",
3684 SD_BUS_PARAM(result),
3685 method_can_suspend_then_hibernate,
3686 SD_BUS_VTABLE_UNPRIVILEGED),
3687 SD_BUS_METHOD_WITH_NAMES("ScheduleShutdown",
3688 "st",
3689 SD_BUS_PARAM(type)
3690 SD_BUS_PARAM(usec),
3691 NULL,,
3692 method_schedule_shutdown,
3693 SD_BUS_VTABLE_UNPRIVILEGED),
3694 SD_BUS_METHOD_WITH_NAMES("CancelScheduledShutdown",
3695 NULL,,
3696 "b",
3697 SD_BUS_PARAM(cancelled),
3698 method_cancel_scheduled_shutdown,
3699 SD_BUS_VTABLE_UNPRIVILEGED),
3700 SD_BUS_METHOD_WITH_NAMES("Inhibit",
3701 "ssss",
3702 SD_BUS_PARAM(what)
3703 SD_BUS_PARAM(who)
3704 SD_BUS_PARAM(why)
3705 SD_BUS_PARAM(mode),
3706 "h",
3707 SD_BUS_PARAM(pipe_fd),
3708 method_inhibit,
3709 SD_BUS_VTABLE_UNPRIVILEGED),
3710 SD_BUS_METHOD_WITH_NAMES("CanRebootParameter",
3711 NULL,,
3712 "s",
3713 SD_BUS_PARAM(result),
3714 method_can_reboot_parameter,
3715 SD_BUS_VTABLE_UNPRIVILEGED),
3716 SD_BUS_METHOD_WITH_NAMES("SetRebootParameter",
3717 "s",
3718 SD_BUS_PARAM(parameter),
3719 NULL,,
3720 method_set_reboot_parameter,
3721 SD_BUS_VTABLE_UNPRIVILEGED),
3722 SD_BUS_METHOD_WITH_NAMES("CanRebootToFirmwareSetup",
3723 NULL,,
3724 "s",
3725 SD_BUS_PARAM(result),
3726 method_can_reboot_to_firmware_setup,
3727 SD_BUS_VTABLE_UNPRIVILEGED),
3728 SD_BUS_METHOD_WITH_NAMES("SetRebootToFirmwareSetup",
3729 "b",
3730 SD_BUS_PARAM(enable),
3731 NULL,,
3732 method_set_reboot_to_firmware_setup,
3733 SD_BUS_VTABLE_UNPRIVILEGED),
3734 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderMenu",
3735 NULL,,
3736 "s",
3737 SD_BUS_PARAM(result),
3738 method_can_reboot_to_boot_loader_menu,
3739 SD_BUS_VTABLE_UNPRIVILEGED),
3740 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderMenu",
3741 "t",
3742 SD_BUS_PARAM(timeout),
3743 NULL,,
3744 method_set_reboot_to_boot_loader_menu,
3745 SD_BUS_VTABLE_UNPRIVILEGED),
3746 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderEntry",
3747 NULL,,
3748 "s",
3749 SD_BUS_PARAM(result),
3750 method_can_reboot_to_boot_loader_entry,
3751 SD_BUS_VTABLE_UNPRIVILEGED),
3752 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderEntry",
3753 "s",
3754 SD_BUS_PARAM(boot_loader_entry),
3755 NULL,,
3756 method_set_reboot_to_boot_loader_entry,
3757 SD_BUS_VTABLE_UNPRIVILEGED),
3758 SD_BUS_METHOD_WITH_NAMES("SetWallMessage",
3759 "sb",
3760 SD_BUS_PARAM(wall_message)
3761 SD_BUS_PARAM(enable),
3762 NULL,,
3763 method_set_wall_message,
3764 SD_BUS_VTABLE_UNPRIVILEGED),
3765
3766 SD_BUS_SIGNAL_WITH_NAMES("SessionNew",
3767 "so",
3768 SD_BUS_PARAM(session_id)
3769 SD_BUS_PARAM(object_path),
3770 0),
3771 SD_BUS_SIGNAL_WITH_NAMES("SessionRemoved",
3772 "so",
3773 SD_BUS_PARAM(session_id)
3774 SD_BUS_PARAM(object_path),
3775 0),
3776 SD_BUS_SIGNAL_WITH_NAMES("UserNew",
3777 "uo",
3778 SD_BUS_PARAM(uid)
3779 SD_BUS_PARAM(object_path),
3780 0),
3781 SD_BUS_SIGNAL_WITH_NAMES("UserRemoved",
3782 "uo",
3783 SD_BUS_PARAM(uid)
3784 SD_BUS_PARAM(object_path),
3785 0),
3786 SD_BUS_SIGNAL_WITH_NAMES("SeatNew",
3787 "so",
3788 SD_BUS_PARAM(seat_id)
3789 SD_BUS_PARAM(object_path),
3790 0),
3791 SD_BUS_SIGNAL_WITH_NAMES("SeatRemoved",
3792 "so",
3793 SD_BUS_PARAM(seat_id)
3794 SD_BUS_PARAM(object_path),
3795 0),
3796 SD_BUS_SIGNAL_WITH_NAMES("PrepareForShutdown",
3797 "b",
3798 SD_BUS_PARAM(start),
3799 0),
3800 SD_BUS_SIGNAL_WITH_NAMES("PrepareForSleep",
3801 "b",
3802 SD_BUS_PARAM(start),
3803 0),
3804
3805 SD_BUS_VTABLE_END
3806 };
3807
3808 const BusObjectImplementation manager_object = {
3809 "/org/freedesktop/login1",
3810 "org.freedesktop.login1.Manager",
3811 .vtables = BUS_VTABLES(manager_vtable),
3812 .children = BUS_IMPLEMENTATIONS(&seat_object,
3813 &session_object,
3814 &user_object),
3815 };
3816
3817 static int session_jobs_reply(Session *s, uint32_t jid, const char *unit, const char *result) {
3818 assert(s);
3819 assert(unit);
3820
3821 if (!s->started)
3822 return 0;
3823
3824 if (result && !streq(result, "done")) {
3825 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
3826
3827 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED,
3828 "Job %u for unit '%s' failed with '%s'", jid, unit, result);
3829 return session_send_create_reply(s, &e);
3830 }
3831
3832 return session_send_create_reply(s, NULL);
3833 }
3834
3835 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3836 const char *path, *result, *unit;
3837 Manager *m = userdata;
3838 Session *session;
3839 uint32_t id;
3840 User *user;
3841 int r;
3842
3843 assert(message);
3844 assert(m);
3845
3846 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
3847 if (r < 0) {
3848 bus_log_parse_error(r);
3849 return 0;
3850 }
3851
3852 if (m->action_job && streq(m->action_job, path)) {
3853 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
3854
3855 /* Tell people that they now may take a lock again */
3856 (void) send_prepare_for(m, m->action_what, false);
3857
3858 m->action_job = mfree(m->action_job);
3859 m->action_unit = NULL;
3860 m->action_what = 0;
3861 return 0;
3862 }
3863
3864 session = hashmap_get(m->session_units, unit);
3865 if (session) {
3866 if (streq_ptr(path, session->scope_job)) {
3867 session->scope_job = mfree(session->scope_job);
3868 (void) session_jobs_reply(session, id, unit, result);
3869
3870 session_save(session);
3871 user_save(session->user);
3872 }
3873
3874 session_add_to_gc_queue(session);
3875 }
3876
3877 user = hashmap_get(m->user_units, unit);
3878 if (user) {
3879 if (streq_ptr(path, user->service_job)) {
3880 user->service_job = mfree(user->service_job);
3881
3882 LIST_FOREACH(sessions_by_user, session, user->sessions)
3883 (void) session_jobs_reply(session, id, unit, NULL /* don't propagate user service failures to the client */);
3884
3885 user_save(user);
3886 }
3887
3888 user_add_to_gc_queue(user);
3889 }
3890
3891 return 0;
3892 }
3893
3894 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3895 const char *path, *unit;
3896 Manager *m = userdata;
3897 Session *session;
3898 User *user;
3899 int r;
3900
3901 assert(message);
3902 assert(m);
3903
3904 r = sd_bus_message_read(message, "so", &unit, &path);
3905 if (r < 0) {
3906 bus_log_parse_error(r);
3907 return 0;
3908 }
3909
3910 session = hashmap_get(m->session_units, unit);
3911 if (session)
3912 session_add_to_gc_queue(session);
3913
3914 user = hashmap_get(m->user_units, unit);
3915 if (user)
3916 user_add_to_gc_queue(user);
3917
3918 return 0;
3919 }
3920
3921 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3922 _cleanup_free_ char *unit = NULL;
3923 Manager *m = userdata;
3924 const char *path;
3925 Session *session;
3926 User *user;
3927 int r;
3928
3929 assert(message);
3930 assert(m);
3931
3932 path = sd_bus_message_get_path(message);
3933 if (!path)
3934 return 0;
3935
3936 r = unit_name_from_dbus_path(path, &unit);
3937 if (r == -EINVAL) /* not a unit */
3938 return 0;
3939 if (r < 0) {
3940 log_oom();
3941 return 0;
3942 }
3943
3944 session = hashmap_get(m->session_units, unit);
3945 if (session)
3946 session_add_to_gc_queue(session);
3947
3948 user = hashmap_get(m->user_units, unit);
3949 if (user)
3950 user_add_to_gc_queue(user);
3951
3952 return 0;
3953 }
3954
3955 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3956 Manager *m = userdata;
3957 Session *session;
3958 int b, r;
3959
3960 assert(message);
3961 assert(m);
3962
3963 r = sd_bus_message_read(message, "b", &b);
3964 if (r < 0) {
3965 bus_log_parse_error(r);
3966 return 0;
3967 }
3968
3969 if (b)
3970 return 0;
3971
3972 /* systemd finished reloading, let's recheck all our sessions */
3973 log_debug("System manager has been reloaded, rechecking sessions...");
3974
3975 HASHMAP_FOREACH(session, m->sessions)
3976 session_add_to_gc_queue(session);
3977
3978 return 0;
3979 }
3980
3981 int manager_send_changed(Manager *manager, const char *property, ...) {
3982 char **l;
3983
3984 assert(manager);
3985
3986 l = strv_from_stdarg_alloca(property);
3987
3988 return sd_bus_emit_properties_changed_strv(
3989 manager->bus,
3990 "/org/freedesktop/login1",
3991 "org.freedesktop.login1.Manager",
3992 l);
3993 }
3994
3995 static int strdup_job(sd_bus_message *reply, char **job) {
3996 const char *j;
3997 char *copy;
3998 int r;
3999
4000 r = sd_bus_message_read(reply, "o", &j);
4001 if (r < 0)
4002 return r;
4003
4004 copy = strdup(j);
4005 if (!copy)
4006 return -ENOMEM;
4007
4008 *job = copy;
4009 return 1;
4010 }
4011
4012 int manager_start_scope(
4013 Manager *manager,
4014 const char *scope,
4015 pid_t pid,
4016 const char *slice,
4017 const char *description,
4018 char **wants,
4019 char **after,
4020 const char *requires_mounts_for,
4021 sd_bus_message *more_properties,
4022 sd_bus_error *error,
4023 char **job) {
4024
4025 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
4026 char **i;
4027 int r;
4028
4029 assert(manager);
4030 assert(scope);
4031 assert(pid > 1);
4032 assert(job);
4033
4034 r = bus_message_new_method_call(manager->bus, &m, bus_systemd_mgr, "StartTransientUnit");
4035 if (r < 0)
4036 return r;
4037
4038 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
4039 if (r < 0)
4040 return r;
4041
4042 r = sd_bus_message_open_container(m, 'a', "(sv)");
4043 if (r < 0)
4044 return r;
4045
4046 if (!isempty(slice)) {
4047 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
4048 if (r < 0)
4049 return r;
4050 }
4051
4052 if (!isempty(description)) {
4053 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
4054 if (r < 0)
4055 return r;
4056 }
4057
4058 STRV_FOREACH(i, wants) {
4059 r = sd_bus_message_append(m, "(sv)", "Wants", "as", 1, *i);
4060 if (r < 0)
4061 return r;
4062 }
4063
4064 STRV_FOREACH(i, after) {
4065 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, *i);
4066 if (r < 0)
4067 return r;
4068 }
4069
4070 if (!empty_or_root(requires_mounts_for)) {
4071 r = sd_bus_message_append(m, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for);
4072 if (r < 0)
4073 return r;
4074 }
4075
4076 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4077 * SIGTERM */
4078 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
4079 if (r < 0)
4080 return r;
4081
4082 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
4083 if (r < 0)
4084 return r;
4085
4086 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4087 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", UINT64_MAX);
4088 if (r < 0)
4089 return bus_log_create_error(r);
4090
4091 if (more_properties) {
4092 /* If TasksMax also appears here, it will overwrite the default value set above */
4093 r = sd_bus_message_copy(m, more_properties, true);
4094 if (r < 0)
4095 return r;
4096 }
4097
4098 r = sd_bus_message_close_container(m);
4099 if (r < 0)
4100 return r;
4101
4102 r = sd_bus_message_append(m, "a(sa(sv))", 0);
4103 if (r < 0)
4104 return r;
4105
4106 r = sd_bus_call(manager->bus, m, 0, error, &reply);
4107 if (r < 0)
4108 return r;
4109
4110 return strdup_job(reply, job);
4111 }
4112
4113 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
4114 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
4115 int r;
4116
4117 assert(manager);
4118 assert(unit);
4119 assert(job);
4120
4121 r = bus_call_method(
4122 manager->bus,
4123 bus_systemd_mgr,
4124 "StartUnit",
4125 error,
4126 &reply,
4127 "ss", unit, "replace");
4128 if (r < 0)
4129 return r;
4130
4131 return strdup_job(reply, job);
4132 }
4133
4134 int manager_stop_unit(Manager *manager, const char *unit, const char *job_mode, sd_bus_error *error, char **ret_job) {
4135 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
4136 int r;
4137
4138 assert(manager);
4139 assert(unit);
4140 assert(ret_job);
4141
4142 r = bus_call_method(
4143 manager->bus,
4144 bus_systemd_mgr,
4145 "StopUnit",
4146 error,
4147 &reply,
4148 "ss", unit, job_mode ?: "fail");
4149 if (r < 0) {
4150 if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT,
4151 BUS_ERROR_LOAD_FAILED)) {
4152
4153 *ret_job = NULL;
4154 sd_bus_error_free(error);
4155 return 0;
4156 }
4157
4158 return r;
4159 }
4160
4161 return strdup_job(reply, ret_job);
4162 }
4163
4164 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *ret_error) {
4165 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4166 _cleanup_free_ char *path = NULL;
4167 int r;
4168
4169 assert(manager);
4170 assert(scope);
4171
4172 path = unit_dbus_path_from_name(scope);
4173 if (!path)
4174 return -ENOMEM;
4175
4176 r = sd_bus_call_method(
4177 manager->bus,
4178 "org.freedesktop.systemd1",
4179 path,
4180 "org.freedesktop.systemd1.Scope",
4181 "Abandon",
4182 &error,
4183 NULL,
4184 NULL);
4185 if (r < 0) {
4186 if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT,
4187 BUS_ERROR_LOAD_FAILED,
4188 BUS_ERROR_SCOPE_NOT_RUNNING))
4189 return 0;
4190
4191 sd_bus_error_move(ret_error, &error);
4192 return r;
4193 }
4194
4195 return 1;
4196 }
4197
4198 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
4199 assert(manager);
4200 assert(unit);
4201
4202 return bus_call_method(
4203 manager->bus,
4204 bus_systemd_mgr,
4205 "KillUnit",
4206 error,
4207 NULL,
4208 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
4209 }
4210
4211 int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret_error) {
4212 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4213 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
4214 _cleanup_free_ char *path = NULL;
4215 const char *state;
4216 int r;
4217
4218 assert(manager);
4219 assert(unit);
4220
4221 path = unit_dbus_path_from_name(unit);
4222 if (!path)
4223 return -ENOMEM;
4224
4225 r = sd_bus_get_property(
4226 manager->bus,
4227 "org.freedesktop.systemd1",
4228 path,
4229 "org.freedesktop.systemd1.Unit",
4230 "ActiveState",
4231 &error,
4232 &reply,
4233 "s");
4234 if (r < 0) {
4235 /* systemd might have dropped off momentarily, let's
4236 * not make this an error */
4237 if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
4238 SD_BUS_ERROR_DISCONNECTED))
4239 return true;
4240
4241 /* If the unit is already unloaded then it's not
4242 * active */
4243 if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT,
4244 BUS_ERROR_LOAD_FAILED))
4245 return false;
4246
4247 sd_bus_error_move(ret_error, &error);
4248 return r;
4249 }
4250
4251 r = sd_bus_message_read(reply, "s", &state);
4252 if (r < 0)
4253 return r;
4254
4255 return !STR_IN_SET(state, "inactive", "failed");
4256 }
4257
4258 int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *ret_error) {
4259 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4260 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
4261 int r;
4262
4263 assert(manager);
4264 assert(path);
4265
4266 r = sd_bus_get_property(
4267 manager->bus,
4268 "org.freedesktop.systemd1",
4269 path,
4270 "org.freedesktop.systemd1.Job",
4271 "State",
4272 &error,
4273 &reply,
4274 "s");
4275 if (r < 0) {
4276 if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
4277 SD_BUS_ERROR_DISCONNECTED))
4278 return true;
4279
4280 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
4281 return false;
4282
4283 sd_bus_error_move(ret_error, &error);
4284 return r;
4285 }
4286
4287 /* We don't actually care about the state really. The fact
4288 * that we could read the job state is enough for us */
4289
4290 return true;
4291 }