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