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