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