]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-dbus.c
Merge pull request #10307 from poettering/portable-path
[thirdparty/systemd.git] / src / login / logind-dbus.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <pwd.h>
5 #include <string.h>
6 #include <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 "bus-common-errors.h"
14 #include "bus-error.h"
15 #include "bus-unit-util.h"
16 #include "bus-util.h"
17 #include "device-util.h"
18 #include "dirent-util.h"
19 #include "efivars.h"
20 #include "escape.h"
21 #include "fd-util.h"
22 #include "fileio-label.h"
23 #include "format-util.h"
24 #include "fs-util.h"
25 #include "logind.h"
26 #include "mkdir.h"
27 #include "path-util.h"
28 #include "process-util.h"
29 #include "cgroup-util.h"
30 #include "selinux-util.h"
31 #include "sleep-config.h"
32 #include "special.h"
33 #include "strv.h"
34 #include "terminal-util.h"
35 #include "unit-name.h"
36 #include "user-util.h"
37 #include "utmp-wtmp.h"
38
39 static int get_sender_session(Manager *m, sd_bus_message *message, sd_bus_error *error, Session **ret) {
40
41 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
42 const char *name;
43 Session *session;
44 int r;
45
46 /* Get client login session. This is not what you are looking for these days,
47 * as apps may instead belong to a user service unit. This includes terminal
48 * emulators and hence command-line apps. */
49 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
50 if (r < 0)
51 return r;
52
53 r = sd_bus_creds_get_session(creds, &name);
54 if (r == -ENXIO)
55 goto err_no_session;
56 if (r < 0)
57 return r;
58
59 session = hashmap_get(m->sessions, name);
60 if (!session)
61 goto err_no_session;
62
63 *ret = session;
64 return 0;
65
66 err_no_session:
67 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID,
68 "Caller does not belong to any known session");
69 }
70
71 int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
72 Session *session;
73
74 assert(m);
75 assert(message);
76 assert(ret);
77
78 if (isempty(name))
79 return get_sender_session(m, message, error, ret);
80
81 session = hashmap_get(m->sessions, name);
82 if (!session)
83 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
84
85 *ret = session;
86 return 0;
87 }
88
89 static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) {
90
91 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
92 uid_t uid;
93 User *user;
94 int r;
95
96 /* Note that we get the owner UID of the session, not the actual client UID here! */
97 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
98 if (r < 0)
99 return r;
100
101 r = sd_bus_creds_get_owner_uid(creds, &uid);
102 if (r == -ENXIO)
103 goto err_no_user;
104 if (r < 0)
105 return r;
106
107 user = hashmap_get(m->users, UID_TO_PTR(uid));
108 if (!user)
109 goto err_no_user;
110
111 *ret = user;
112 return 0;
113
114 err_no_user:
115 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "Caller does not belong to any logged in user or lingering user");
116 }
117
118 int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) {
119 User *user;
120
121 assert(m);
122 assert(message);
123 assert(ret);
124
125 if (!uid_is_valid(uid))
126 return get_sender_user(m, message, error, ret);
127
128 user = hashmap_get(m->users, UID_TO_PTR(uid));
129 if (!user)
130 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "User ID "UID_FMT" is not logged in or lingering", uid);
131
132 *ret = user;
133 return 0;
134 }
135
136 int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret) {
137 Seat *seat;
138 int r;
139
140 assert(m);
141 assert(message);
142 assert(ret);
143
144 if (isempty(name)) {
145 Session *session;
146
147 r = manager_get_session_from_creds(m, message, NULL, error, &session);
148 if (r < 0)
149 return r;
150
151 seat = session->seat;
152 if (!seat)
153 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session has no seat.");
154 } else {
155 seat = hashmap_get(m->seats, name);
156 if (!seat)
157 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
158 }
159
160 *ret = seat;
161 return 0;
162 }
163
164 static int property_get_idle_hint(
165 sd_bus *bus,
166 const char *path,
167 const char *interface,
168 const char *property,
169 sd_bus_message *reply,
170 void *userdata,
171 sd_bus_error *error) {
172
173 Manager *m = userdata;
174
175 assert(bus);
176 assert(reply);
177 assert(m);
178
179 return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
180 }
181
182 static int property_get_idle_since_hint(
183 sd_bus *bus,
184 const char *path,
185 const char *interface,
186 const char *property,
187 sd_bus_message *reply,
188 void *userdata,
189 sd_bus_error *error) {
190
191 Manager *m = userdata;
192 dual_timestamp t = DUAL_TIMESTAMP_NULL;
193
194 assert(bus);
195 assert(reply);
196 assert(m);
197
198 manager_get_idle_hint(m, &t);
199
200 return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
201 }
202
203 static int property_get_inhibited(
204 sd_bus *bus,
205 const char *path,
206 const char *interface,
207 const char *property,
208 sd_bus_message *reply,
209 void *userdata,
210 sd_bus_error *error) {
211
212 Manager *m = userdata;
213 InhibitWhat w;
214
215 assert(bus);
216 assert(reply);
217 assert(m);
218
219 w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
220
221 return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
222 }
223
224 static int property_get_preparing(
225 sd_bus *bus,
226 const char *path,
227 const char *interface,
228 const char *property,
229 sd_bus_message *reply,
230 void *userdata,
231 sd_bus_error *error) {
232
233 Manager *m = userdata;
234 bool b;
235
236 assert(bus);
237 assert(reply);
238 assert(m);
239
240 if (streq(property, "PreparingForShutdown"))
241 b = m->action_what & INHIBIT_SHUTDOWN;
242 else
243 b = m->action_what & INHIBIT_SLEEP;
244
245 return sd_bus_message_append(reply, "b", b);
246 }
247
248 static int property_get_scheduled_shutdown(
249 sd_bus *bus,
250 const char *path,
251 const char *interface,
252 const char *property,
253 sd_bus_message *reply,
254 void *userdata,
255 sd_bus_error *error) {
256
257 Manager *m = userdata;
258 int r;
259
260 assert(bus);
261 assert(reply);
262 assert(m);
263
264 r = sd_bus_message_open_container(reply, 'r', "st");
265 if (r < 0)
266 return r;
267
268 r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
269 if (r < 0)
270 return r;
271
272 return sd_bus_message_close_container(reply);
273 }
274
275 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
276 static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays);
277 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX);
278 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size);
279
280 static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
281 _cleanup_free_ char *p = NULL;
282 Manager *m = userdata;
283 const char *name;
284 Session *session;
285 int r;
286
287 assert(message);
288 assert(m);
289
290 r = sd_bus_message_read(message, "s", &name);
291 if (r < 0)
292 return r;
293
294 r = manager_get_session_from_creds(m, message, name, error, &session);
295 if (r < 0)
296 return r;
297
298 p = session_bus_path(session);
299 if (!p)
300 return -ENOMEM;
301
302 return sd_bus_reply_method_return(message, "o", p);
303 }
304
305 /* Get login session of a process. This is not what you are looking for these days,
306 * as apps may instead belong to a user service unit. This includes terminal
307 * emulators and hence command-line apps. */
308 static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
309 _cleanup_free_ char *p = NULL;
310 Session *session = NULL;
311 Manager *m = userdata;
312 pid_t pid;
313 int r;
314
315 assert(message);
316 assert(m);
317
318 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
319
320 r = sd_bus_message_read(message, "u", &pid);
321 if (r < 0)
322 return r;
323 if (pid < 0)
324 return -EINVAL;
325
326 if (pid == 0) {
327 r = manager_get_session_from_creds(m, message, NULL, error, &session);
328 if (r < 0)
329 return r;
330 } else {
331 r = manager_get_session_by_pid(m, pid, &session);
332 if (r < 0)
333 return r;
334
335 if (!session)
336 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
337 }
338
339 p = session_bus_path(session);
340 if (!p)
341 return -ENOMEM;
342
343 return sd_bus_reply_method_return(message, "o", p);
344 }
345
346 static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
347 _cleanup_free_ char *p = NULL;
348 Manager *m = userdata;
349 uint32_t uid;
350 User *user;
351 int r;
352
353 assert(message);
354 assert(m);
355
356 r = sd_bus_message_read(message, "u", &uid);
357 if (r < 0)
358 return r;
359
360 r = manager_get_user_from_creds(m, message, uid, error, &user);
361 if (r < 0)
362 return r;
363
364 p = user_bus_path(user);
365 if (!p)
366 return -ENOMEM;
367
368 return sd_bus_reply_method_return(message, "o", p);
369 }
370
371 static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
372 _cleanup_free_ char *p = NULL;
373 Manager *m = userdata;
374 User *user = NULL;
375 pid_t pid;
376 int r;
377
378 assert(message);
379 assert(m);
380
381 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
382
383 r = sd_bus_message_read(message, "u", &pid);
384 if (r < 0)
385 return r;
386 if (pid < 0)
387 return -EINVAL;
388
389 if (pid == 0) {
390 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
391 if (r < 0)
392 return r;
393 } else {
394 r = manager_get_user_by_pid(m, pid, &user);
395 if (r < 0)
396 return r;
397 if (!user)
398 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID,
399 "PID "PID_FMT" does not belong to any logged in user or lingering user",
400 pid);
401 }
402
403 p = user_bus_path(user);
404 if (!p)
405 return -ENOMEM;
406
407 return sd_bus_reply_method_return(message, "o", p);
408 }
409
410 static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
411 _cleanup_free_ char *p = NULL;
412 Manager *m = userdata;
413 const char *name;
414 Seat *seat;
415 int r;
416
417 assert(message);
418 assert(m);
419
420 r = sd_bus_message_read(message, "s", &name);
421 if (r < 0)
422 return r;
423
424 r = manager_get_seat_from_creds(m, message, name, error, &seat);
425 if (r < 0)
426 return r;
427
428 p = seat_bus_path(seat);
429 if (!p)
430 return -ENOMEM;
431
432 return sd_bus_reply_method_return(message, "o", p);
433 }
434
435 static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
436 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
437 Manager *m = userdata;
438 Session *session;
439 Iterator i;
440 int r;
441
442 assert(message);
443 assert(m);
444
445 r = sd_bus_message_new_method_return(message, &reply);
446 if (r < 0)
447 return r;
448
449 r = sd_bus_message_open_container(reply, 'a', "(susso)");
450 if (r < 0)
451 return r;
452
453 HASHMAP_FOREACH(session, m->sessions, i) {
454 _cleanup_free_ char *p = NULL;
455
456 p = session_bus_path(session);
457 if (!p)
458 return -ENOMEM;
459
460 r = sd_bus_message_append(reply, "(susso)",
461 session->id,
462 (uint32_t) session->user->uid,
463 session->user->name,
464 session->seat ? session->seat->id : "",
465 p);
466 if (r < 0)
467 return r;
468 }
469
470 r = sd_bus_message_close_container(reply);
471 if (r < 0)
472 return r;
473
474 return sd_bus_send(NULL, reply, NULL);
475 }
476
477 static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
478 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
479 Manager *m = userdata;
480 User *user;
481 Iterator i;
482 int r;
483
484 assert(message);
485 assert(m);
486
487 r = sd_bus_message_new_method_return(message, &reply);
488 if (r < 0)
489 return r;
490
491 r = sd_bus_message_open_container(reply, 'a', "(uso)");
492 if (r < 0)
493 return r;
494
495 HASHMAP_FOREACH(user, m->users, i) {
496 _cleanup_free_ char *p = NULL;
497
498 p = user_bus_path(user);
499 if (!p)
500 return -ENOMEM;
501
502 r = sd_bus_message_append(reply, "(uso)",
503 (uint32_t) user->uid,
504 user->name,
505 p);
506 if (r < 0)
507 return r;
508 }
509
510 r = sd_bus_message_close_container(reply);
511 if (r < 0)
512 return r;
513
514 return sd_bus_send(NULL, reply, NULL);
515 }
516
517 static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
518 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
519 Manager *m = userdata;
520 Seat *seat;
521 Iterator i;
522 int r;
523
524 assert(message);
525 assert(m);
526
527 r = sd_bus_message_new_method_return(message, &reply);
528 if (r < 0)
529 return r;
530
531 r = sd_bus_message_open_container(reply, 'a', "(so)");
532 if (r < 0)
533 return r;
534
535 HASHMAP_FOREACH(seat, m->seats, i) {
536 _cleanup_free_ char *p = NULL;
537
538 p = seat_bus_path(seat);
539 if (!p)
540 return -ENOMEM;
541
542 r = sd_bus_message_append(reply, "(so)", seat->id, p);
543 if (r < 0)
544 return r;
545 }
546
547 r = sd_bus_message_close_container(reply);
548 if (r < 0)
549 return r;
550
551 return sd_bus_send(NULL, reply, NULL);
552 }
553
554 static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
555 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
556 Manager *m = userdata;
557 Inhibitor *inhibitor;
558 Iterator i;
559 int r;
560
561 assert(message);
562 assert(m);
563
564 r = sd_bus_message_new_method_return(message, &reply);
565 if (r < 0)
566 return r;
567
568 r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
569 if (r < 0)
570 return r;
571
572 HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
573
574 r = sd_bus_message_append(reply, "(ssssuu)",
575 strempty(inhibit_what_to_string(inhibitor->what)),
576 strempty(inhibitor->who),
577 strempty(inhibitor->why),
578 strempty(inhibit_mode_to_string(inhibitor->mode)),
579 (uint32_t) inhibitor->uid,
580 (uint32_t) inhibitor->pid);
581 if (r < 0)
582 return r;
583 }
584
585 r = sd_bus_message_close_container(reply);
586 if (r < 0)
587 return r;
588
589 return sd_bus_send(NULL, reply, NULL);
590 }
591
592 static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
593 const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
594 _cleanup_free_ char *id = NULL;
595 Session *session = NULL;
596 uint32_t audit_id = 0;
597 Manager *m = userdata;
598 User *user = NULL;
599 Seat *seat = NULL;
600 pid_t leader;
601 uid_t uid;
602 int remote;
603 uint32_t vtnr = 0;
604 SessionType t;
605 SessionClass c;
606 int r;
607
608 assert(message);
609 assert(m);
610
611 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
612 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
613
614 r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
615 if (r < 0)
616 return r;
617
618 if (!uid_is_valid(uid))
619 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID");
620 if (leader < 0 || leader == 1 || leader == getpid_cached())
621 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
622
623 if (isempty(type))
624 t = _SESSION_TYPE_INVALID;
625 else {
626 t = session_type_from_string(type);
627 if (t < 0)
628 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
629 }
630
631 if (isempty(class))
632 c = _SESSION_CLASS_INVALID;
633 else {
634 c = session_class_from_string(class);
635 if (c < 0)
636 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
637 }
638
639 if (isempty(desktop))
640 desktop = NULL;
641 else {
642 if (!string_is_safe(desktop))
643 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
644 }
645
646 if (isempty(cseat))
647 seat = NULL;
648 else {
649 seat = hashmap_get(m->seats, cseat);
650 if (!seat)
651 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
652 }
653
654 if (tty_is_vc(tty)) {
655 int v;
656
657 if (!seat)
658 seat = m->seat0;
659 else if (seat != m->seat0)
660 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);
661
662 v = vtnr_from_tty(tty);
663 if (v <= 0)
664 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
665
666 if (vtnr == 0)
667 vtnr = (uint32_t) v;
668 else if (vtnr != (uint32_t) v)
669 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
670
671 } else if (tty_is_console(tty)) {
672
673 if (!seat)
674 seat = m->seat0;
675 else if (seat != m->seat0)
676 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
677
678 if (vtnr != 0)
679 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
680 }
681
682 if (seat) {
683 if (seat_has_vts(seat)) {
684 if (vtnr <= 0 || vtnr > 63)
685 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
686 } else {
687 if (vtnr != 0)
688 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
689 }
690 }
691
692 if (t == _SESSION_TYPE_INVALID) {
693 if (!isempty(display))
694 t = SESSION_X11;
695 else if (!isempty(tty))
696 t = SESSION_TTY;
697 else
698 t = SESSION_UNSPECIFIED;
699 }
700
701 if (c == _SESSION_CLASS_INVALID) {
702 if (t == SESSION_UNSPECIFIED)
703 c = SESSION_BACKGROUND;
704 else
705 c = SESSION_USER;
706 }
707
708 if (leader == 0) {
709 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
710
711 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
712 if (r < 0)
713 return r;
714
715 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
716 if (r < 0)
717 return r;
718 }
719
720 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
721 * that avoids creating a logind session. */
722 r = manager_get_user_by_pid(m, leader, NULL);
723 if (r < 0)
724 return r;
725 if (r > 0)
726 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session or user slice");
727
728 /*
729 * Old gdm and lightdm start the user-session on the same VT as
730 * the greeter session. But they destroy the greeter session
731 * after the user-session and want the user-session to take
732 * over the VT. We need to support this for
733 * backwards-compatibility, so make sure we allow new sessions
734 * on a VT that a greeter is running on. Furthermore, to allow
735 * re-logins, we have to allow a greeter to take over a used VT for
736 * the exact same reasons.
737 */
738 if (c != SESSION_GREETER &&
739 vtnr > 0 &&
740 vtnr < m->seat0->position_count &&
741 m->seat0->positions[vtnr] &&
742 m->seat0->positions[vtnr]->class != SESSION_GREETER)
743 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
744
745 if (hashmap_size(m->sessions) >= m->sessions_max)
746 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
747
748 (void) audit_session_from_pid(leader, &audit_id);
749 if (audit_session_is_valid(audit_id)) {
750 /* Keep our session IDs and the audit session IDs in sync */
751
752 if (asprintf(&id, "%"PRIu32, audit_id) < 0)
753 return -ENOMEM;
754
755 /* Wut? There's already a session by this name and we
756 * didn't find it above? Weird, then let's not trust
757 * the audit data and let's better register a new
758 * ID */
759 if (hashmap_get(m->sessions, id)) {
760 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
761 audit_id = AUDIT_SESSION_INVALID;
762 id = mfree(id);
763 }
764 }
765
766 if (!id) {
767 do {
768 id = mfree(id);
769
770 if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
771 return -ENOMEM;
772
773 } while (hashmap_get(m->sessions, id));
774 }
775
776 r = manager_add_user_by_uid(m, uid, &user);
777 if (r < 0)
778 goto fail;
779
780 r = manager_add_session(m, id, &session);
781 if (r < 0)
782 goto fail;
783
784 session_set_user(session, user);
785
786 session->leader = leader;
787 session->audit_id = audit_id;
788 session->type = t;
789 session->class = c;
790 session->remote = remote;
791 session->vtnr = vtnr;
792
793 if (!isempty(tty)) {
794 session->tty = strdup(tty);
795 if (!session->tty) {
796 r = -ENOMEM;
797 goto fail;
798 }
799 }
800
801 if (!isempty(display)) {
802 session->display = strdup(display);
803 if (!session->display) {
804 r = -ENOMEM;
805 goto fail;
806 }
807 }
808
809 if (!isempty(remote_user)) {
810 session->remote_user = strdup(remote_user);
811 if (!session->remote_user) {
812 r = -ENOMEM;
813 goto fail;
814 }
815 }
816
817 if (!isempty(remote_host)) {
818 session->remote_host = strdup(remote_host);
819 if (!session->remote_host) {
820 r = -ENOMEM;
821 goto fail;
822 }
823 }
824
825 if (!isempty(service)) {
826 session->service = strdup(service);
827 if (!session->service) {
828 r = -ENOMEM;
829 goto fail;
830 }
831 }
832
833 if (!isempty(desktop)) {
834 session->desktop = strdup(desktop);
835 if (!session->desktop) {
836 r = -ENOMEM;
837 goto fail;
838 }
839 }
840
841 if (seat) {
842 r = seat_attach_session(seat, session);
843 if (r < 0)
844 goto fail;
845 }
846
847 r = sd_bus_message_enter_container(message, 'a', "(sv)");
848 if (r < 0)
849 return r;
850
851 r = session_start(session, message);
852 if (r < 0)
853 goto fail;
854
855 r = sd_bus_message_exit_container(message);
856 if (r < 0)
857 goto fail;
858
859 session->create_message = sd_bus_message_ref(message);
860
861 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
862 * session_send_create_reply(). */
863
864 return 1;
865
866 fail:
867 if (session)
868 session_add_to_gc_queue(session);
869
870 if (user)
871 user_add_to_gc_queue(user);
872
873 return r;
874 }
875
876 static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
877 Manager *m = userdata;
878 Session *session;
879 const char *name;
880 int r;
881
882 assert(message);
883 assert(m);
884
885 r = sd_bus_message_read(message, "s", &name);
886 if (r < 0)
887 return r;
888
889 r = manager_get_session_from_creds(m, message, name, error, &session);
890 if (r < 0)
891 return r;
892
893 r = session_release(session);
894 if (r < 0)
895 return r;
896
897 return sd_bus_reply_method_return(message, NULL);
898 }
899
900 static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
901 Manager *m = userdata;
902 Session *session;
903 const char *name;
904 int r;
905
906 assert(message);
907 assert(m);
908
909 r = sd_bus_message_read(message, "s", &name);
910 if (r < 0)
911 return r;
912
913 r = manager_get_session_from_creds(m, message, name, error, &session);
914 if (r < 0)
915 return r;
916
917 return bus_session_method_activate(message, session, error);
918 }
919
920 static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
921 const char *session_name, *seat_name;
922 Manager *m = userdata;
923 Session *session;
924 Seat *seat;
925 int r;
926
927 assert(message);
928 assert(m);
929
930 /* Same as ActivateSession() but refuses to work if
931 * the seat doesn't match */
932
933 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
934 if (r < 0)
935 return r;
936
937 r = manager_get_session_from_creds(m, message, session_name, error, &session);
938 if (r < 0)
939 return r;
940
941 r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
942 if (r < 0)
943 return r;
944
945 if (session->seat != seat)
946 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
947
948 r = session_activate(session);
949 if (r < 0)
950 return r;
951
952 return sd_bus_reply_method_return(message, NULL);
953 }
954
955 static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
956 Manager *m = userdata;
957 Session *session;
958 const char *name;
959 int r;
960
961 assert(message);
962 assert(m);
963
964 r = sd_bus_message_read(message, "s", &name);
965 if (r < 0)
966 return r;
967
968 r = manager_get_session_from_creds(m, message, name, error, &session);
969 if (r < 0)
970 return r;
971
972 return bus_session_method_lock(message, session, error);
973 }
974
975 static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
976 Manager *m = userdata;
977 int r;
978
979 assert(message);
980 assert(m);
981
982 r = bus_verify_polkit_async(
983 message,
984 CAP_SYS_ADMIN,
985 "org.freedesktop.login1.lock-sessions",
986 NULL,
987 false,
988 UID_INVALID,
989 &m->polkit_registry,
990 error);
991 if (r < 0)
992 return r;
993 if (r == 0)
994 return 1; /* Will call us back */
995
996 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
997 if (r < 0)
998 return r;
999
1000 return sd_bus_reply_method_return(message, NULL);
1001 }
1002
1003 static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1004 const char *name;
1005 Manager *m = userdata;
1006 Session *session;
1007 int r;
1008
1009 assert(message);
1010 assert(m);
1011
1012 r = sd_bus_message_read(message, "s", &name);
1013 if (r < 0)
1014 return r;
1015
1016 r = manager_get_session_from_creds(m, message, name, error, &session);
1017 if (r < 0)
1018 return r;
1019
1020 return bus_session_method_kill(message, session, error);
1021 }
1022
1023 static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1024 Manager *m = userdata;
1025 uint32_t uid;
1026 User *user;
1027 int r;
1028
1029 assert(message);
1030 assert(m);
1031
1032 r = sd_bus_message_read(message, "u", &uid);
1033 if (r < 0)
1034 return r;
1035
1036 r = manager_get_user_from_creds(m, message, uid, error, &user);
1037 if (r < 0)
1038 return r;
1039
1040 return bus_user_method_kill(message, user, error);
1041 }
1042
1043 static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1044 Manager *m = userdata;
1045 const char *name;
1046 Session *session;
1047 int r;
1048
1049 assert(message);
1050 assert(m);
1051
1052 r = sd_bus_message_read(message, "s", &name);
1053 if (r < 0)
1054 return r;
1055
1056 r = manager_get_session_from_creds(m, message, name, error, &session);
1057 if (r < 0)
1058 return r;
1059
1060 return bus_session_method_terminate(message, session, error);
1061 }
1062
1063 static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1064 Manager *m = userdata;
1065 uint32_t uid;
1066 User *user;
1067 int r;
1068
1069 assert(message);
1070 assert(m);
1071
1072 r = sd_bus_message_read(message, "u", &uid);
1073 if (r < 0)
1074 return r;
1075
1076 r = manager_get_user_from_creds(m, message, uid, error, &user);
1077 if (r < 0)
1078 return r;
1079
1080 return bus_user_method_terminate(message, user, error);
1081 }
1082
1083 static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1084 Manager *m = userdata;
1085 const char *name;
1086 Seat *seat;
1087 int r;
1088
1089 assert(message);
1090 assert(m);
1091
1092 r = sd_bus_message_read(message, "s", &name);
1093 if (r < 0)
1094 return r;
1095
1096 r = manager_get_seat_from_creds(m, message, name, error, &seat);
1097 if (r < 0)
1098 return r;
1099
1100 return bus_seat_method_terminate(message, seat, error);
1101 }
1102
1103 static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1104 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1105 _cleanup_free_ char *cc = NULL;
1106 Manager *m = userdata;
1107 int r, b, interactive;
1108 struct passwd *pw;
1109 const char *path;
1110 uint32_t uid, auth_uid;
1111
1112 assert(message);
1113 assert(m);
1114
1115 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1116 if (r < 0)
1117 return r;
1118
1119 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID |
1120 SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1121 if (r < 0)
1122 return r;
1123
1124 if (!uid_is_valid(uid)) {
1125 /* Note that we get the owner UID of the session or user unit,
1126 * not the actual client UID here! */
1127 r = sd_bus_creds_get_owner_uid(creds, &uid);
1128 if (r < 0)
1129 return r;
1130 }
1131
1132 /* owner_uid is racy, so for authorization we must use euid */
1133 r = sd_bus_creds_get_euid(creds, &auth_uid);
1134 if (r < 0)
1135 return r;
1136
1137 errno = 0;
1138 pw = getpwuid(uid);
1139 if (!pw)
1140 return errno > 0 ? -errno : -ENOENT;
1141
1142 r = bus_verify_polkit_async(
1143 message,
1144 CAP_SYS_ADMIN,
1145 uid == auth_uid ? "org.freedesktop.login1.set-self-linger" :
1146 "org.freedesktop.login1.set-user-linger",
1147 NULL,
1148 interactive,
1149 UID_INVALID,
1150 &m->polkit_registry,
1151 error);
1152 if (r < 0)
1153 return r;
1154 if (r == 0)
1155 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1156
1157 mkdir_p_label("/var/lib/systemd", 0755);
1158
1159 r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE);
1160 if (r < 0)
1161 return r;
1162
1163 cc = cescape(pw->pw_name);
1164 if (!cc)
1165 return -ENOMEM;
1166
1167 path = strjoina("/var/lib/systemd/linger/", cc);
1168 if (b) {
1169 User *u;
1170
1171 r = touch(path);
1172 if (r < 0)
1173 return r;
1174
1175 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1176 user_start(u);
1177
1178 } else {
1179 User *u;
1180
1181 r = unlink(path);
1182 if (r < 0 && errno != ENOENT)
1183 return -errno;
1184
1185 u = hashmap_get(m->users, UID_TO_PTR(uid));
1186 if (u)
1187 user_add_to_gc_queue(u);
1188 }
1189
1190 return sd_bus_reply_method_return(message, NULL);
1191 }
1192
1193 static int trigger_device(Manager *m, sd_device *d) {
1194 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
1195 int r;
1196
1197 assert(m);
1198
1199 r = sd_device_enumerator_new(&e);
1200 if (r < 0)
1201 return r;
1202
1203 r = sd_device_enumerator_allow_uninitialized(e);
1204 if (r < 0)
1205 return r;
1206
1207 if (d) {
1208 r = sd_device_enumerator_add_match_parent(e, d);
1209 if (r < 0)
1210 return r;
1211 }
1212
1213 FOREACH_DEVICE(e, d) {
1214 _cleanup_free_ char *t = NULL;
1215 const char *p;
1216
1217 r = sd_device_get_syspath(d, &p);
1218 if (r < 0)
1219 return r;
1220
1221 t = strappend(p, "/uevent");
1222 if (!t)
1223 return -ENOMEM;
1224
1225 (void) write_string_file(t, "change", 0);
1226 }
1227
1228 return 0;
1229 }
1230
1231 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1232 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
1233 _cleanup_free_ char *rule = NULL, *file = NULL;
1234 const char *id_for_seat;
1235 int r;
1236
1237 assert(m);
1238 assert(seat);
1239 assert(sysfs);
1240
1241 r = sd_device_new_from_syspath(&d, sysfs);
1242 if (r < 0)
1243 return r;
1244
1245 if (sd_device_has_tag(d, "seat") <= 0)
1246 return -ENODEV;
1247
1248 if (sd_device_get_property_value(d, "ID_FOR_SEAT", &id_for_seat) < 0)
1249 return -ENODEV;
1250
1251 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1252 return -ENOMEM;
1253
1254 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1255 return -ENOMEM;
1256
1257 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1258 r = write_string_file_atomic_label(file, rule);
1259 if (r < 0)
1260 return r;
1261
1262 return trigger_device(m, d);
1263 }
1264
1265 static int flush_devices(Manager *m) {
1266 _cleanup_closedir_ DIR *d;
1267
1268 assert(m);
1269
1270 d = opendir("/etc/udev/rules.d");
1271 if (!d) {
1272 if (errno != ENOENT)
1273 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1274 } else {
1275 struct dirent *de;
1276
1277 FOREACH_DIRENT_ALL(de, d, break) {
1278 if (!dirent_is_file(de))
1279 continue;
1280
1281 if (!startswith(de->d_name, "72-seat-"))
1282 continue;
1283
1284 if (!endswith(de->d_name, ".rules"))
1285 continue;
1286
1287 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1288 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1289 }
1290 }
1291
1292 return trigger_device(m, NULL);
1293 }
1294
1295 static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1296 const char *sysfs, *seat;
1297 Manager *m = userdata;
1298 int interactive, r;
1299
1300 assert(message);
1301 assert(m);
1302
1303 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1304 if (r < 0)
1305 return r;
1306
1307 if (!path_startswith(sysfs, "/sys"))
1308 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1309
1310 if (!seat_name_is_valid(seat))
1311 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
1312
1313 r = bus_verify_polkit_async(
1314 message,
1315 CAP_SYS_ADMIN,
1316 "org.freedesktop.login1.attach-device",
1317 NULL,
1318 interactive,
1319 UID_INVALID,
1320 &m->polkit_registry,
1321 error);
1322 if (r < 0)
1323 return r;
1324 if (r == 0)
1325 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1326
1327 r = attach_device(m, seat, sysfs);
1328 if (r < 0)
1329 return r;
1330
1331 return sd_bus_reply_method_return(message, NULL);
1332 }
1333
1334 static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1335 Manager *m = userdata;
1336 int interactive, r;
1337
1338 assert(message);
1339 assert(m);
1340
1341 r = sd_bus_message_read(message, "b", &interactive);
1342 if (r < 0)
1343 return r;
1344
1345 r = bus_verify_polkit_async(
1346 message,
1347 CAP_SYS_ADMIN,
1348 "org.freedesktop.login1.flush-devices",
1349 NULL,
1350 interactive,
1351 UID_INVALID,
1352 &m->polkit_registry,
1353 error);
1354 if (r < 0)
1355 return r;
1356 if (r == 0)
1357 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1358
1359 r = flush_devices(m);
1360 if (r < 0)
1361 return r;
1362
1363 return sd_bus_reply_method_return(message, NULL);
1364 }
1365
1366 static int have_multiple_sessions(
1367 Manager *m,
1368 uid_t uid) {
1369
1370 Session *session;
1371 Iterator i;
1372
1373 assert(m);
1374
1375 /* Check for other users' sessions. Greeter sessions do not
1376 * count, and non-login sessions do not count either. */
1377 HASHMAP_FOREACH(session, m->sessions, i)
1378 if (session->class == SESSION_USER &&
1379 session->user->uid != uid)
1380 return true;
1381
1382 return false;
1383 }
1384
1385 static int bus_manager_log_shutdown(
1386 Manager *m,
1387 const char *unit_name) {
1388
1389 const char *p, *q;
1390
1391 assert(m);
1392 assert(unit_name);
1393
1394 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1395 p = "MESSAGE=System is powering down";
1396 q = "SHUTDOWN=power-off";
1397 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1398 p = "MESSAGE=System is rebooting";
1399 q = "SHUTDOWN=reboot";
1400 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1401 p = "MESSAGE=System is halting";
1402 q = "SHUTDOWN=halt";
1403 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1404 p = "MESSAGE=System is rebooting with kexec";
1405 q = "SHUTDOWN=kexec";
1406 } else {
1407 p = "MESSAGE=System is shutting down";
1408 q = NULL;
1409 }
1410
1411 if (isempty(m->wall_message))
1412 p = strjoina(p, ".");
1413 else
1414 p = strjoina(p, " (", m->wall_message, ").");
1415
1416 return log_struct(LOG_NOTICE,
1417 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
1418 p,
1419 q);
1420 }
1421
1422 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1423 Manager *m = userdata;
1424
1425 assert(e);
1426 assert(m);
1427
1428 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1429 return 0;
1430 }
1431
1432 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1433 int r;
1434
1435 assert(m);
1436
1437 if (until <= now(CLOCK_MONOTONIC))
1438 return 0;
1439
1440 /* We want to ignore the lid switch for a while after each
1441 * suspend, and after boot-up. Hence let's install a timer for
1442 * this. As long as the event source exists we ignore the lid
1443 * switch. */
1444
1445 if (m->lid_switch_ignore_event_source) {
1446 usec_t u;
1447
1448 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1449 if (r < 0)
1450 return r;
1451
1452 if (until <= u)
1453 return 0;
1454
1455 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1456 } else
1457 r = sd_event_add_time(
1458 m->event,
1459 &m->lid_switch_ignore_event_source,
1460 CLOCK_MONOTONIC,
1461 until, 0,
1462 lid_switch_ignore_handler, m);
1463
1464 return r;
1465 }
1466
1467 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1468
1469 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1470 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1471 [INHIBIT_SLEEP] = "PrepareForSleep"
1472 };
1473
1474 int active = _active;
1475
1476 assert(m);
1477 assert(w >= 0);
1478 assert(w < _INHIBIT_WHAT_MAX);
1479 assert(signal_name[w]);
1480
1481 return sd_bus_emit_signal(m->bus,
1482 "/org/freedesktop/login1",
1483 "org.freedesktop.login1.Manager",
1484 signal_name[w],
1485 "b",
1486 active);
1487 }
1488
1489 static int execute_shutdown_or_sleep(
1490 Manager *m,
1491 InhibitWhat w,
1492 const char *unit_name,
1493 sd_bus_error *error) {
1494
1495 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1496 char *c = NULL;
1497 const char *p;
1498 int r;
1499
1500 assert(m);
1501 assert(w > 0);
1502 assert(w < _INHIBIT_WHAT_MAX);
1503 assert(unit_name);
1504
1505 if (w == INHIBIT_SHUTDOWN)
1506 bus_manager_log_shutdown(m, unit_name);
1507
1508 r = sd_bus_call_method(
1509 m->bus,
1510 "org.freedesktop.systemd1",
1511 "/org/freedesktop/systemd1",
1512 "org.freedesktop.systemd1.Manager",
1513 "StartUnit",
1514 error,
1515 &reply,
1516 "ss", unit_name, "replace-irreversibly");
1517 if (r < 0)
1518 goto error;
1519
1520 r = sd_bus_message_read(reply, "o", &p);
1521 if (r < 0)
1522 goto error;
1523
1524 c = strdup(p);
1525 if (!c) {
1526 r = -ENOMEM;
1527 goto error;
1528 }
1529
1530 m->action_unit = unit_name;
1531 free(m->action_job);
1532 m->action_job = c;
1533 m->action_what = w;
1534
1535 /* Make sure the lid switch is ignored for a while */
1536 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1537
1538 return 0;
1539
1540 error:
1541 /* Tell people that they now may take a lock again */
1542 (void) send_prepare_for(m, w, false);
1543
1544 return r;
1545 }
1546
1547 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1548
1549 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1550 Inhibitor *offending = NULL;
1551 int r;
1552
1553 assert(manager);
1554
1555 if (manager->action_what == 0 || manager->action_job)
1556 return 0;
1557
1558 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1559 _cleanup_free_ char *comm = NULL, *u = NULL;
1560
1561 if (!timeout)
1562 return 0;
1563
1564 (void) get_process_comm(offending->pid, &comm);
1565 u = uid_to_name(offending->uid);
1566
1567 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1568 offending->uid, strna(u),
1569 offending->pid, strna(comm));
1570 }
1571
1572 /* Actually do the operation */
1573 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1574 if (r < 0) {
1575 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1576 bus_error_message(&error, r));
1577
1578 manager->action_unit = NULL;
1579 manager->action_what = 0;
1580 return r;
1581 }
1582
1583 return 1;
1584 }
1585
1586 static int manager_inhibit_timeout_handler(
1587 sd_event_source *s,
1588 uint64_t usec,
1589 void *userdata) {
1590
1591 Manager *manager = userdata;
1592 int r;
1593
1594 assert(manager);
1595 assert(manager->inhibit_timeout_source == s);
1596
1597 r = manager_dispatch_delayed(manager, true);
1598 return (r < 0) ? r : 0;
1599 }
1600
1601 static int delay_shutdown_or_sleep(
1602 Manager *m,
1603 InhibitWhat w,
1604 const char *unit_name) {
1605
1606 int r;
1607 usec_t timeout_val;
1608
1609 assert(m);
1610 assert(w >= 0);
1611 assert(w < _INHIBIT_WHAT_MAX);
1612 assert(unit_name);
1613
1614 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1615
1616 if (m->inhibit_timeout_source) {
1617 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1618 if (r < 0)
1619 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1620
1621 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1622 if (r < 0)
1623 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1624 } else {
1625 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1626 timeout_val, 0, manager_inhibit_timeout_handler, m);
1627 if (r < 0)
1628 return r;
1629 }
1630
1631 m->action_unit = unit_name;
1632 m->action_what = w;
1633
1634 return 0;
1635 }
1636
1637 int bus_manager_shutdown_or_sleep_now_or_later(
1638 Manager *m,
1639 const char *unit_name,
1640 InhibitWhat w,
1641 sd_bus_error *error) {
1642
1643 _cleanup_free_ char *load_state = NULL;
1644 bool delayed;
1645 int r;
1646
1647 assert(m);
1648 assert(unit_name);
1649 assert(w > 0);
1650 assert(w <= _INHIBIT_WHAT_MAX);
1651 assert(!m->action_job);
1652
1653 r = unit_load_state(m->bus, unit_name, &load_state);
1654 if (r < 0)
1655 return r;
1656
1657 if (!streq(load_state, "loaded")) {
1658 log_notice("Unit %s is %s, refusing operation.", unit_name, load_state);
1659 return -EACCES;
1660 }
1661
1662 /* Tell everybody to prepare for shutdown/sleep */
1663 (void) send_prepare_for(m, w, true);
1664
1665 delayed =
1666 m->inhibit_delay_max > 0 &&
1667 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1668
1669 if (delayed)
1670 /* Shutdown is delayed, keep in mind what we
1671 * want to do, and start a timeout */
1672 r = delay_shutdown_or_sleep(m, w, unit_name);
1673 else
1674 /* Shutdown is not delayed, execute it
1675 * immediately */
1676 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1677
1678 return r;
1679 }
1680
1681 static int verify_shutdown_creds(
1682 Manager *m,
1683 sd_bus_message *message,
1684 InhibitWhat w,
1685 bool interactive,
1686 const char *action,
1687 const char *action_multiple_sessions,
1688 const char *action_ignore_inhibit,
1689 sd_bus_error *error) {
1690
1691 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1692 bool multiple_sessions, blocked;
1693 uid_t uid;
1694 int r;
1695
1696 assert(m);
1697 assert(message);
1698 assert(w >= 0);
1699 assert(w <= _INHIBIT_WHAT_MAX);
1700
1701 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1702 if (r < 0)
1703 return r;
1704
1705 r = sd_bus_creds_get_euid(creds, &uid);
1706 if (r < 0)
1707 return r;
1708
1709 r = have_multiple_sessions(m, uid);
1710 if (r < 0)
1711 return r;
1712
1713 multiple_sessions = r > 0;
1714 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1715
1716 if (multiple_sessions && action_multiple_sessions) {
1717 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1718 if (r < 0)
1719 return r;
1720 if (r == 0)
1721 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1722 }
1723
1724 if (blocked && action_ignore_inhibit) {
1725 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1726 if (r < 0)
1727 return r;
1728 if (r == 0)
1729 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1730 }
1731
1732 if (!multiple_sessions && !blocked && action) {
1733 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1734 if (r < 0)
1735 return r;
1736 if (r == 0)
1737 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1738 }
1739
1740 return 0;
1741 }
1742
1743 static int method_do_shutdown_or_sleep(
1744 Manager *m,
1745 sd_bus_message *message,
1746 const char *unit_name,
1747 InhibitWhat w,
1748 const char *action,
1749 const char *action_multiple_sessions,
1750 const char *action_ignore_inhibit,
1751 const char *sleep_verb,
1752 sd_bus_error *error) {
1753
1754 int interactive, r;
1755
1756 assert(m);
1757 assert(message);
1758 assert(unit_name);
1759 assert(w >= 0);
1760 assert(w <= _INHIBIT_WHAT_MAX);
1761
1762 r = sd_bus_message_read(message, "b", &interactive);
1763 if (r < 0)
1764 return r;
1765
1766 /* Don't allow multiple jobs being executed at the same time */
1767 if (m->action_what)
1768 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1769
1770 if (sleep_verb) {
1771 r = can_sleep(sleep_verb);
1772 if (r == -ENOSPC)
1773 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Not enough swap space for hibernation");
1774 if (r == -ENOMEDIUM)
1775 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Kernel image has been removed, can't hibernate");
1776 if (r == -EADV)
1777 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Resume not configured, can't hibernate");
1778 if (r == 0)
1779 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "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
1796 static 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
1810 static 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
1824 static 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
1838 static 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
1852 static 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
1866 static 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
1880 static 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
1894 static 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
1909 static 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
1957 fail:
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
1964 static 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
1983 static 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) {
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
2035 error:
2036 reset_scheduled_shutdown(m);
2037 return r;
2038 }
2039
2040 static 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
2148 static 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 const char *tty = NULL;
2161 uid_t uid = 0;
2162 int r;
2163
2164 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2165 if (r >= 0) {
2166 (void) sd_bus_creds_get_uid(creds, &uid);
2167 (void) sd_bus_creds_get_tty(creds, &tty);
2168 }
2169
2170 utmp_wall("The system shutdown has been cancelled",
2171 uid_to_name(uid), tty, logind_wall_tty_filter, m);
2172 }
2173
2174 return sd_bus_reply_method_return(message, "b", cancelled);
2175 }
2176
2177 static int method_can_shutdown_or_sleep(
2178 Manager *m,
2179 sd_bus_message *message,
2180 InhibitWhat w,
2181 const char *action,
2182 const char *action_multiple_sessions,
2183 const char *action_ignore_inhibit,
2184 const char *sleep_verb,
2185 sd_bus_error *error) {
2186
2187 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2188 HandleAction handle;
2189 bool multiple_sessions, challenge, blocked;
2190 const char *result = NULL;
2191 uid_t uid;
2192 int r;
2193
2194 assert(m);
2195 assert(message);
2196 assert(w >= 0);
2197 assert(w <= _INHIBIT_WHAT_MAX);
2198 assert(action);
2199 assert(action_multiple_sessions);
2200 assert(action_ignore_inhibit);
2201
2202 if (sleep_verb) {
2203 r = can_sleep(sleep_verb);
2204 if (IN_SET(r, 0, -ENOSPC, -ENOMEDIUM, -EADV))
2205 return sd_bus_reply_method_return(message, "s", "na");
2206 if (r < 0)
2207 return r;
2208 }
2209
2210 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2211 if (r < 0)
2212 return r;
2213
2214 r = sd_bus_creds_get_euid(creds, &uid);
2215 if (r < 0)
2216 return r;
2217
2218 r = have_multiple_sessions(m, uid);
2219 if (r < 0)
2220 return r;
2221
2222 multiple_sessions = r > 0;
2223 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2224
2225 handle = handle_action_from_string(sleep_verb);
2226 if (handle >= 0) {
2227 const char *target;
2228
2229 target = manager_target_for_action(handle);
2230 if (target) {
2231 _cleanup_free_ char *load_state = NULL;
2232
2233 r = unit_load_state(m->bus, target, &load_state);
2234 if (r < 0)
2235 return r;
2236
2237 if (!streq(load_state, "loaded")) {
2238 result = "no";
2239 goto finish;
2240 }
2241 }
2242 }
2243
2244 if (multiple_sessions) {
2245 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2246 if (r < 0)
2247 return r;
2248
2249 if (r > 0)
2250 result = "yes";
2251 else if (challenge)
2252 result = "challenge";
2253 else
2254 result = "no";
2255 }
2256
2257 if (blocked) {
2258 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2259 if (r < 0)
2260 return r;
2261
2262 if (r > 0 && !result)
2263 result = "yes";
2264 else if (challenge && (!result || streq(result, "yes")))
2265 result = "challenge";
2266 else
2267 result = "no";
2268 }
2269
2270 if (!multiple_sessions && !blocked) {
2271 /* If neither inhibit nor multiple sessions
2272 * apply then just check the normal policy */
2273
2274 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2275 if (r < 0)
2276 return r;
2277
2278 if (r > 0)
2279 result = "yes";
2280 else if (challenge)
2281 result = "challenge";
2282 else
2283 result = "no";
2284 }
2285
2286 finish:
2287 return sd_bus_reply_method_return(message, "s", result);
2288 }
2289
2290 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2291 Manager *m = userdata;
2292
2293 return method_can_shutdown_or_sleep(
2294 m, message,
2295 INHIBIT_SHUTDOWN,
2296 "org.freedesktop.login1.power-off",
2297 "org.freedesktop.login1.power-off-multiple-sessions",
2298 "org.freedesktop.login1.power-off-ignore-inhibit",
2299 NULL,
2300 error);
2301 }
2302
2303 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2304 Manager *m = userdata;
2305
2306 return method_can_shutdown_or_sleep(
2307 m, message,
2308 INHIBIT_SHUTDOWN,
2309 "org.freedesktop.login1.reboot",
2310 "org.freedesktop.login1.reboot-multiple-sessions",
2311 "org.freedesktop.login1.reboot-ignore-inhibit",
2312 NULL,
2313 error);
2314 }
2315
2316 static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2317 Manager *m = userdata;
2318
2319 return method_can_shutdown_or_sleep(
2320 m, message,
2321 INHIBIT_SHUTDOWN,
2322 "org.freedesktop.login1.halt",
2323 "org.freedesktop.login1.halt-multiple-sessions",
2324 "org.freedesktop.login1.halt-ignore-inhibit",
2325 NULL,
2326 error);
2327 }
2328
2329 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2330 Manager *m = userdata;
2331
2332 return method_can_shutdown_or_sleep(
2333 m, message,
2334 INHIBIT_SLEEP,
2335 "org.freedesktop.login1.suspend",
2336 "org.freedesktop.login1.suspend-multiple-sessions",
2337 "org.freedesktop.login1.suspend-ignore-inhibit",
2338 "suspend",
2339 error);
2340 }
2341
2342 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2343 Manager *m = userdata;
2344
2345 return method_can_shutdown_or_sleep(
2346 m, message,
2347 INHIBIT_SLEEP,
2348 "org.freedesktop.login1.hibernate",
2349 "org.freedesktop.login1.hibernate-multiple-sessions",
2350 "org.freedesktop.login1.hibernate-ignore-inhibit",
2351 "hibernate",
2352 error);
2353 }
2354
2355 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2356 Manager *m = userdata;
2357
2358 return method_can_shutdown_or_sleep(
2359 m, message,
2360 INHIBIT_SLEEP,
2361 "org.freedesktop.login1.hibernate",
2362 "org.freedesktop.login1.hibernate-multiple-sessions",
2363 "org.freedesktop.login1.hibernate-ignore-inhibit",
2364 "hybrid-sleep",
2365 error);
2366 }
2367
2368 static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2369 Manager *m = userdata;
2370
2371 return method_can_shutdown_or_sleep(
2372 m, message,
2373 INHIBIT_SLEEP,
2374 "org.freedesktop.login1.hibernate",
2375 "org.freedesktop.login1.hibernate-multiple-sessions",
2376 "org.freedesktop.login1.hibernate-ignore-inhibit",
2377 "suspend-then-hibernate",
2378 error);
2379 }
2380
2381 static int property_get_reboot_to_firmware_setup(
2382 sd_bus *bus,
2383 const char *path,
2384 const char *interface,
2385 const char *property,
2386 sd_bus_message *reply,
2387 void *userdata,
2388 sd_bus_error *error) {
2389 int r;
2390
2391 assert(bus);
2392 assert(reply);
2393 assert(userdata);
2394
2395 r = efi_get_reboot_to_firmware();
2396 if (r < 0 && r != -EOPNOTSUPP)
2397 log_warning_errno(r, "Failed to determine reboot-to-firmware state: %m");
2398
2399 return sd_bus_message_append(reply, "b", r > 0);
2400 }
2401
2402 static int method_set_reboot_to_firmware_setup(
2403 sd_bus_message *message,
2404 void *userdata,
2405 sd_bus_error *error) {
2406
2407 int b, r;
2408 Manager *m = userdata;
2409
2410 assert(message);
2411 assert(m);
2412
2413 r = sd_bus_message_read(message, "b", &b);
2414 if (r < 0)
2415 return r;
2416
2417 r = bus_verify_polkit_async(message,
2418 CAP_SYS_ADMIN,
2419 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2420 NULL,
2421 false,
2422 UID_INVALID,
2423 &m->polkit_registry,
2424 error);
2425 if (r < 0)
2426 return r;
2427 if (r == 0)
2428 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2429
2430 r = efi_set_reboot_to_firmware(b);
2431 if (r < 0)
2432 return r;
2433
2434 return sd_bus_reply_method_return(message, NULL);
2435 }
2436
2437 static int method_can_reboot_to_firmware_setup(
2438 sd_bus_message *message,
2439 void *userdata,
2440 sd_bus_error *error) {
2441
2442 int r;
2443 bool challenge;
2444 const char *result;
2445 Manager *m = userdata;
2446
2447 assert(message);
2448 assert(m);
2449
2450 r = efi_reboot_to_firmware_supported();
2451 if (r < 0) {
2452 if (r != -EOPNOTSUPP)
2453 log_warning_errno(errno, "Failed to determine whether reboot to firmware is supported: %m");
2454
2455 return sd_bus_reply_method_return(message, "s", "na");
2456 }
2457
2458 r = bus_test_polkit(message,
2459 CAP_SYS_ADMIN,
2460 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2461 NULL,
2462 UID_INVALID,
2463 &challenge,
2464 error);
2465 if (r < 0)
2466 return r;
2467
2468 if (r > 0)
2469 result = "yes";
2470 else if (challenge)
2471 result = "challenge";
2472 else
2473 result = "no";
2474
2475 return sd_bus_reply_method_return(message, "s", result);
2476 }
2477
2478 static int method_set_wall_message(
2479 sd_bus_message *message,
2480 void *userdata,
2481 sd_bus_error *error) {
2482
2483 int r;
2484 Manager *m = userdata;
2485 char *wall_message;
2486 unsigned enable_wall_messages;
2487
2488 assert(message);
2489 assert(m);
2490
2491 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2492 if (r < 0)
2493 return r;
2494
2495 r = bus_verify_polkit_async(message,
2496 CAP_SYS_ADMIN,
2497 "org.freedesktop.login1.set-wall-message",
2498 NULL,
2499 false,
2500 UID_INVALID,
2501 &m->polkit_registry,
2502 error);
2503 if (r < 0)
2504 return r;
2505 if (r == 0)
2506 return 1; /* Will call us back */
2507
2508 r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
2509 if (r < 0)
2510 return log_oom();
2511
2512 m->enable_wall_messages = enable_wall_messages;
2513
2514 return sd_bus_reply_method_return(message, NULL);
2515 }
2516
2517 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2518 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2519 const char *who, *why, *what, *mode;
2520 _cleanup_free_ char *id = NULL;
2521 _cleanup_close_ int fifo_fd = -1;
2522 Manager *m = userdata;
2523 Inhibitor *i = NULL;
2524 InhibitMode mm;
2525 InhibitWhat w;
2526 pid_t pid;
2527 uid_t uid;
2528 int r;
2529
2530 assert(message);
2531 assert(m);
2532
2533 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2534 if (r < 0)
2535 return r;
2536
2537 w = inhibit_what_from_string(what);
2538 if (w <= 0)
2539 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2540
2541 mm = inhibit_mode_from_string(mode);
2542 if (mm < 0)
2543 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2544
2545 /* Delay is only supported for shutdown/sleep */
2546 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2547 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2548
2549 /* Don't allow taking delay locks while we are already
2550 * executing the operation. We shouldn't create the impression
2551 * that the lock was successful if the machine is about to go
2552 * down/suspend any moment. */
2553 if (m->action_what & w)
2554 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2555
2556 r = bus_verify_polkit_async(
2557 message,
2558 CAP_SYS_BOOT,
2559 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2560 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2561 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2562 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2563 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2564 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2565 "org.freedesktop.login1.inhibit-handle-lid-switch",
2566 NULL,
2567 false,
2568 UID_INVALID,
2569 &m->polkit_registry,
2570 error);
2571 if (r < 0)
2572 return r;
2573 if (r == 0)
2574 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2575
2576 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2577 if (r < 0)
2578 return r;
2579
2580 r = sd_bus_creds_get_euid(creds, &uid);
2581 if (r < 0)
2582 return r;
2583
2584 r = sd_bus_creds_get_pid(creds, &pid);
2585 if (r < 0)
2586 return r;
2587
2588 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2589 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2590
2591 do {
2592 id = mfree(id);
2593
2594 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2595 return -ENOMEM;
2596
2597 } while (hashmap_get(m->inhibitors, id));
2598
2599 r = manager_add_inhibitor(m, id, &i);
2600 if (r < 0)
2601 return r;
2602
2603 i->what = w;
2604 i->mode = mm;
2605 i->pid = pid;
2606 i->uid = uid;
2607 i->why = strdup(why);
2608 i->who = strdup(who);
2609
2610 if (!i->why || !i->who) {
2611 r = -ENOMEM;
2612 goto fail;
2613 }
2614
2615 fifo_fd = inhibitor_create_fifo(i);
2616 if (fifo_fd < 0) {
2617 r = fifo_fd;
2618 goto fail;
2619 }
2620
2621 inhibitor_start(i);
2622
2623 return sd_bus_reply_method_return(message, "h", fifo_fd);
2624
2625 fail:
2626 if (i)
2627 inhibitor_free(i);
2628
2629 return r;
2630 }
2631
2632 const sd_bus_vtable manager_vtable[] = {
2633 SD_BUS_VTABLE_START(0),
2634
2635 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2636 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2637
2638 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2639 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2640 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2641 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2642 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2643 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2644 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2645 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2646 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2647 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2648 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2649 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2650 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2651 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2652 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2653 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
2654 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2655 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2656 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2657 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2658 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2659 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2660 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2661 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2662 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2663 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2664 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2665 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0),
2666 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2667 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0),
2668 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
2669
2670 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2671 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2672 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2673 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2674 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2675 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2676 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2677 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2678 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2679 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2680 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2681 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2682 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2683 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2684 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2685 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2686 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2687 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2688 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2689 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2690 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2691 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2692 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2693 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2694 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2695 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2696 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2697 SD_BUS_METHOD("Halt", "b", NULL, method_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2698 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2699 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2700 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2701 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL, method_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2702 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2703 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2704 SD_BUS_METHOD("CanHalt", NULL, "s", method_can_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2705 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2706 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2707 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2708 SD_BUS_METHOD("CanSuspendThenHibernate", NULL, "s", method_can_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2709 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2710 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2711 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2712 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2713 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2714 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2715
2716 SD_BUS_SIGNAL("SessionNew", "so", 0),
2717 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2718 SD_BUS_SIGNAL("UserNew", "uo", 0),
2719 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2720 SD_BUS_SIGNAL("SeatNew", "so", 0),
2721 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2722 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2723 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2724
2725 SD_BUS_VTABLE_END
2726 };
2727
2728 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2729 int r = 0;
2730
2731 assert(s);
2732 assert(unit);
2733
2734 if (!s->started)
2735 return r;
2736
2737 if (streq(result, "done"))
2738 r = session_send_create_reply(s, NULL);
2739 else {
2740 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2741
2742 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
2743 r = session_send_create_reply(s, &e);
2744 }
2745
2746 return r;
2747 }
2748
2749 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2750 const char *path, *result, *unit;
2751 Manager *m = userdata;
2752 Session *session;
2753 uint32_t id;
2754 User *user;
2755 int r;
2756
2757 assert(message);
2758 assert(m);
2759
2760 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2761 if (r < 0) {
2762 bus_log_parse_error(r);
2763 return 0;
2764 }
2765
2766 if (m->action_job && streq(m->action_job, path)) {
2767 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2768
2769 /* Tell people that they now may take a lock again */
2770 (void) send_prepare_for(m, m->action_what, false);
2771
2772 m->action_job = mfree(m->action_job);
2773 m->action_unit = NULL;
2774 m->action_what = 0;
2775 return 0;
2776 }
2777
2778 session = hashmap_get(m->session_units, unit);
2779 if (session && streq_ptr(path, session->scope_job)) {
2780 session->scope_job = mfree(session->scope_job);
2781 session_jobs_reply(session, unit, result);
2782
2783 session_save(session);
2784 user_save(session->user);
2785 session_add_to_gc_queue(session);
2786 }
2787
2788 user = hashmap_get(m->user_units, unit);
2789 if (user &&
2790 (streq_ptr(path, user->service_job) ||
2791 streq_ptr(path, user->slice_job))) {
2792
2793 if (streq_ptr(path, user->service_job))
2794 user->service_job = mfree(user->service_job);
2795
2796 if (streq_ptr(path, user->slice_job))
2797 user->slice_job = mfree(user->slice_job);
2798
2799 LIST_FOREACH(sessions_by_user, session, user->sessions)
2800 session_jobs_reply(session, unit, result);
2801
2802 user_save(user);
2803 user_add_to_gc_queue(user);
2804 }
2805
2806 return 0;
2807 }
2808
2809 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2810 const char *path, *unit;
2811 Manager *m = userdata;
2812 Session *session;
2813 User *user;
2814 int r;
2815
2816 assert(message);
2817 assert(m);
2818
2819 r = sd_bus_message_read(message, "so", &unit, &path);
2820 if (r < 0) {
2821 bus_log_parse_error(r);
2822 return 0;
2823 }
2824
2825 session = hashmap_get(m->session_units, unit);
2826 if (session)
2827 session_add_to_gc_queue(session);
2828
2829 user = hashmap_get(m->user_units, unit);
2830 if (user)
2831 user_add_to_gc_queue(user);
2832
2833 return 0;
2834 }
2835
2836 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2837 _cleanup_free_ char *unit = NULL;
2838 Manager *m = userdata;
2839 const char *path;
2840 Session *session;
2841 User *user;
2842 int r;
2843
2844 assert(message);
2845 assert(m);
2846
2847 path = sd_bus_message_get_path(message);
2848 if (!path)
2849 return 0;
2850
2851 r = unit_name_from_dbus_path(path, &unit);
2852 if (r == -EINVAL) /* not a unit */
2853 return 0;
2854 if (r < 0) {
2855 log_oom();
2856 return 0;
2857 }
2858
2859 session = hashmap_get(m->session_units, unit);
2860 if (session)
2861 session_add_to_gc_queue(session);
2862
2863 user = hashmap_get(m->user_units, unit);
2864 if (user)
2865 user_add_to_gc_queue(user);
2866
2867 return 0;
2868 }
2869
2870 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2871 Manager *m = userdata;
2872 Session *session;
2873 Iterator i;
2874 int b, r;
2875
2876 assert(message);
2877 assert(m);
2878
2879 r = sd_bus_message_read(message, "b", &b);
2880 if (r < 0) {
2881 bus_log_parse_error(r);
2882 return 0;
2883 }
2884
2885 if (b)
2886 return 0;
2887
2888 /* systemd finished reloading, let's recheck all our sessions */
2889 log_debug("System manager has been reloaded, rechecking sessions...");
2890
2891 HASHMAP_FOREACH(session, m->sessions, i)
2892 session_add_to_gc_queue(session);
2893
2894 return 0;
2895 }
2896
2897 int manager_send_changed(Manager *manager, const char *property, ...) {
2898 char **l;
2899
2900 assert(manager);
2901
2902 l = strv_from_stdarg_alloca(property);
2903
2904 return sd_bus_emit_properties_changed_strv(
2905 manager->bus,
2906 "/org/freedesktop/login1",
2907 "org.freedesktop.login1.Manager",
2908 l);
2909 }
2910
2911 static int strdup_job(sd_bus_message *reply, char **job) {
2912 const char *j;
2913 char *copy;
2914 int r;
2915
2916 r = sd_bus_message_read(reply, "o", &j);
2917 if (r < 0)
2918 return r;
2919
2920 copy = strdup(j);
2921 if (!copy)
2922 return -ENOMEM;
2923
2924 *job = copy;
2925 return 1;
2926 }
2927
2928 int manager_start_scope(
2929 Manager *manager,
2930 const char *scope,
2931 pid_t pid,
2932 const char *slice,
2933 const char *description,
2934 const char *after,
2935 const char *after2,
2936 sd_bus_message *more_properties,
2937 sd_bus_error *error,
2938 char **job) {
2939
2940 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
2941 int r;
2942
2943 assert(manager);
2944 assert(scope);
2945 assert(pid > 1);
2946 assert(job);
2947
2948 r = sd_bus_message_new_method_call(
2949 manager->bus,
2950 &m,
2951 "org.freedesktop.systemd1",
2952 "/org/freedesktop/systemd1",
2953 "org.freedesktop.systemd1.Manager",
2954 "StartTransientUnit");
2955 if (r < 0)
2956 return r;
2957
2958 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
2959 if (r < 0)
2960 return r;
2961
2962 r = sd_bus_message_open_container(m, 'a', "(sv)");
2963 if (r < 0)
2964 return r;
2965
2966 if (!isempty(slice)) {
2967 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
2968 if (r < 0)
2969 return r;
2970 }
2971
2972 if (!isempty(description)) {
2973 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
2974 if (r < 0)
2975 return r;
2976 }
2977
2978 if (!isempty(after)) {
2979 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
2980 if (r < 0)
2981 return r;
2982 }
2983
2984 if (!isempty(after2)) {
2985 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
2986 if (r < 0)
2987 return r;
2988 }
2989
2990 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
2991 * SIGTERM */
2992 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
2993 if (r < 0)
2994 return r;
2995
2996 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
2997 if (r < 0)
2998 return r;
2999
3000 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3001 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", (uint64_t)-1);
3002 if (r < 0)
3003 return bus_log_create_error(r);
3004
3005 if (more_properties) {
3006 /* If TasksMax also appears here, it will overwrite the default value set above */
3007 r = sd_bus_message_copy(m, more_properties, true);
3008 if (r < 0)
3009 return r;
3010 }
3011
3012 r = sd_bus_message_close_container(m);
3013 if (r < 0)
3014 return r;
3015
3016 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3017 if (r < 0)
3018 return r;
3019
3020 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3021 if (r < 0)
3022 return r;
3023
3024 return strdup_job(reply, job);
3025 }
3026
3027 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3028 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3029 int r;
3030
3031 assert(manager);
3032 assert(unit);
3033 assert(job);
3034
3035 r = sd_bus_call_method(
3036 manager->bus,
3037 "org.freedesktop.systemd1",
3038 "/org/freedesktop/systemd1",
3039 "org.freedesktop.systemd1.Manager",
3040 "StartUnit",
3041 error,
3042 &reply,
3043 "ss", unit, "replace");
3044 if (r < 0)
3045 return r;
3046
3047 return strdup_job(reply, job);
3048 }
3049
3050 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3051 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3052 int r;
3053
3054 assert(manager);
3055 assert(unit);
3056 assert(job);
3057
3058 r = sd_bus_call_method(
3059 manager->bus,
3060 "org.freedesktop.systemd1",
3061 "/org/freedesktop/systemd1",
3062 "org.freedesktop.systemd1.Manager",
3063 "StopUnit",
3064 error,
3065 &reply,
3066 "ss", unit, "fail");
3067 if (r < 0) {
3068 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3069 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3070
3071 *job = NULL;
3072 sd_bus_error_free(error);
3073 return 0;
3074 }
3075
3076 return r;
3077 }
3078
3079 return strdup_job(reply, job);
3080 }
3081
3082 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
3083 _cleanup_free_ char *path = NULL;
3084 int r;
3085
3086 assert(manager);
3087 assert(scope);
3088
3089 path = unit_dbus_path_from_name(scope);
3090 if (!path)
3091 return -ENOMEM;
3092
3093 r = sd_bus_call_method(
3094 manager->bus,
3095 "org.freedesktop.systemd1",
3096 path,
3097 "org.freedesktop.systemd1.Scope",
3098 "Abandon",
3099 error,
3100 NULL,
3101 NULL);
3102 if (r < 0) {
3103 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3104 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
3105 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
3106 sd_bus_error_free(error);
3107 return 0;
3108 }
3109
3110 return r;
3111 }
3112
3113 return 1;
3114 }
3115
3116 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3117 assert(manager);
3118 assert(unit);
3119
3120 return sd_bus_call_method(
3121 manager->bus,
3122 "org.freedesktop.systemd1",
3123 "/org/freedesktop/systemd1",
3124 "org.freedesktop.systemd1.Manager",
3125 "KillUnit",
3126 error,
3127 NULL,
3128 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3129 }
3130
3131 int manager_unit_is_active(Manager *manager, const char *unit) {
3132 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3133 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3134 _cleanup_free_ char *path = NULL;
3135 const char *state;
3136 int r;
3137
3138 assert(manager);
3139 assert(unit);
3140
3141 path = unit_dbus_path_from_name(unit);
3142 if (!path)
3143 return -ENOMEM;
3144
3145 r = sd_bus_get_property(
3146 manager->bus,
3147 "org.freedesktop.systemd1",
3148 path,
3149 "org.freedesktop.systemd1.Unit",
3150 "ActiveState",
3151 &error,
3152 &reply,
3153 "s");
3154 if (r < 0) {
3155 /* systemd might have droppped off momentarily, let's
3156 * not make this an error */
3157 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3158 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3159 return true;
3160
3161 /* If the unit is already unloaded then it's not
3162 * active */
3163 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3164 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3165 return false;
3166
3167 return r;
3168 }
3169
3170 r = sd_bus_message_read(reply, "s", &state);
3171 if (r < 0)
3172 return -EINVAL;
3173
3174 return !streq(state, "inactive") && !streq(state, "failed");
3175 }
3176
3177 int manager_job_is_active(Manager *manager, const char *path) {
3178 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3179 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3180 int r;
3181
3182 assert(manager);
3183 assert(path);
3184
3185 r = sd_bus_get_property(
3186 manager->bus,
3187 "org.freedesktop.systemd1",
3188 path,
3189 "org.freedesktop.systemd1.Job",
3190 "State",
3191 &error,
3192 &reply,
3193 "s");
3194 if (r < 0) {
3195 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3196 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3197 return true;
3198
3199 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3200 return false;
3201
3202 return r;
3203 }
3204
3205 /* We don't actually care about the state really. The fact
3206 * that we could read the job state is enough for us */
3207
3208 return true;
3209 }