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