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