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