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