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