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