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