]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-dbus.c
nss: prevent PROTECT_ERRNO from squashing changes to *errnop
[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 int active = _active;
1478
1479 assert(m);
1480 assert(IN_SET(w, INHIBIT_SHUTDOWN, INHIBIT_SLEEP));
1481
1482 return sd_bus_emit_signal(m->bus,
1483 "/org/freedesktop/login1",
1484 "org.freedesktop.login1.Manager",
1485 w == INHIBIT_SHUTDOWN ? "PrepareForShutdown" : "PrepareForSleep",
1486 "b",
1487 active);
1488 }
1489
1490 static int execute_shutdown_or_sleep(
1491 Manager *m,
1492 InhibitWhat w,
1493 const char *unit_name,
1494 sd_bus_error *error) {
1495
1496 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1497 const char *p;
1498 int r;
1499
1500 assert(m);
1501 assert(w > 0);
1502 assert(w < _INHIBIT_WHAT_MAX);
1503 assert(unit_name);
1504
1505 if (w == INHIBIT_SHUTDOWN)
1506 bus_manager_log_shutdown(m, unit_name);
1507
1508 r = sd_bus_call_method(
1509 m->bus,
1510 "org.freedesktop.systemd1",
1511 "/org/freedesktop/systemd1",
1512 "org.freedesktop.systemd1.Manager",
1513 "StartUnit",
1514 error,
1515 &reply,
1516 "ss", unit_name, "replace-irreversibly");
1517 if (r < 0)
1518 goto error;
1519
1520 r = sd_bus_message_read(reply, "o", &p);
1521 if (r < 0)
1522 goto error;
1523
1524 r = free_and_strdup(&m->action_job, p);
1525 if (r < 0)
1526 goto error;
1527
1528 m->action_unit = unit_name;
1529 m->action_what = w;
1530
1531 /* Make sure the lid switch is ignored for a while */
1532 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1533
1534 return 0;
1535
1536 error:
1537 /* Tell people that they now may take a lock again */
1538 (void) send_prepare_for(m, w, false);
1539
1540 return r;
1541 }
1542
1543 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1544
1545 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1546 Inhibitor *offending = NULL;
1547 int r;
1548
1549 assert(manager);
1550
1551 if (manager->action_what == 0 || manager->action_job)
1552 return 0;
1553
1554 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1555 _cleanup_free_ char *comm = NULL, *u = NULL;
1556
1557 if (!timeout)
1558 return 0;
1559
1560 (void) get_process_comm(offending->pid, &comm);
1561 u = uid_to_name(offending->uid);
1562
1563 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1564 offending->uid, strna(u),
1565 offending->pid, strna(comm));
1566 }
1567
1568 /* Actually do the operation */
1569 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1570 if (r < 0) {
1571 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1572 bus_error_message(&error, r));
1573
1574 manager->action_unit = NULL;
1575 manager->action_what = 0;
1576 return r;
1577 }
1578
1579 return 1;
1580 }
1581
1582 static int manager_inhibit_timeout_handler(
1583 sd_event_source *s,
1584 uint64_t usec,
1585 void *userdata) {
1586
1587 Manager *manager = userdata;
1588 int r;
1589
1590 assert(manager);
1591 assert(manager->inhibit_timeout_source == s);
1592
1593 r = manager_dispatch_delayed(manager, true);
1594 return (r < 0) ? r : 0;
1595 }
1596
1597 static int delay_shutdown_or_sleep(
1598 Manager *m,
1599 InhibitWhat w,
1600 const char *unit_name) {
1601
1602 int r;
1603 usec_t timeout_val;
1604
1605 assert(m);
1606 assert(w >= 0);
1607 assert(w < _INHIBIT_WHAT_MAX);
1608 assert(unit_name);
1609
1610 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1611
1612 if (m->inhibit_timeout_source) {
1613 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1614 if (r < 0)
1615 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1616
1617 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1618 if (r < 0)
1619 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1620 } else {
1621 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1622 timeout_val, 0, manager_inhibit_timeout_handler, m);
1623 if (r < 0)
1624 return r;
1625 }
1626
1627 m->action_unit = unit_name;
1628 m->action_what = w;
1629
1630 return 0;
1631 }
1632
1633 int bus_manager_shutdown_or_sleep_now_or_later(
1634 Manager *m,
1635 const char *unit_name,
1636 InhibitWhat w,
1637 sd_bus_error *error) {
1638
1639 _cleanup_free_ char *load_state = NULL;
1640 bool delayed;
1641 int r;
1642
1643 assert(m);
1644 assert(unit_name);
1645 assert(w > 0);
1646 assert(w < _INHIBIT_WHAT_MAX);
1647 assert(!m->action_job);
1648
1649 r = unit_load_state(m->bus, unit_name, &load_state);
1650 if (r < 0)
1651 return r;
1652
1653 if (!streq(load_state, "loaded"))
1654 return log_notice_errno(SYNTHETIC_ERRNO(EACCES),
1655 "Unit %s is %s, refusing operation.",
1656 unit_name, load_state);
1657
1658 /* Tell everybody to prepare for shutdown/sleep */
1659 (void) send_prepare_for(m, w, true);
1660
1661 delayed =
1662 m->inhibit_delay_max > 0 &&
1663 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1664
1665 if (delayed)
1666 /* Shutdown is delayed, keep in mind what we
1667 * want to do, and start a timeout */
1668 r = delay_shutdown_or_sleep(m, w, unit_name);
1669 else
1670 /* Shutdown is not delayed, execute it
1671 * immediately */
1672 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1673
1674 return r;
1675 }
1676
1677 static int verify_shutdown_creds(
1678 Manager *m,
1679 sd_bus_message *message,
1680 InhibitWhat w,
1681 bool interactive,
1682 const char *action,
1683 const char *action_multiple_sessions,
1684 const char *action_ignore_inhibit,
1685 sd_bus_error *error) {
1686
1687 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1688 bool multiple_sessions, blocked;
1689 uid_t uid;
1690 int r;
1691
1692 assert(m);
1693 assert(message);
1694 assert(w >= 0);
1695 assert(w <= _INHIBIT_WHAT_MAX);
1696
1697 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1698 if (r < 0)
1699 return r;
1700
1701 r = sd_bus_creds_get_euid(creds, &uid);
1702 if (r < 0)
1703 return r;
1704
1705 r = have_multiple_sessions(m, uid);
1706 if (r < 0)
1707 return r;
1708
1709 multiple_sessions = r > 0;
1710 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1711
1712 if (multiple_sessions && action_multiple_sessions) {
1713 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1714 if (r < 0)
1715 return r;
1716 if (r == 0)
1717 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1718 }
1719
1720 if (blocked && action_ignore_inhibit) {
1721 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1722 if (r < 0)
1723 return r;
1724 if (r == 0)
1725 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1726 }
1727
1728 if (!multiple_sessions && !blocked && action) {
1729 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1730 if (r < 0)
1731 return r;
1732 if (r == 0)
1733 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1734 }
1735
1736 return 0;
1737 }
1738
1739 static int method_do_shutdown_or_sleep(
1740 Manager *m,
1741 sd_bus_message *message,
1742 const char *unit_name,
1743 InhibitWhat w,
1744 const char *action,
1745 const char *action_multiple_sessions,
1746 const char *action_ignore_inhibit,
1747 const char *sleep_verb,
1748 sd_bus_error *error) {
1749
1750 int interactive, r;
1751
1752 assert(m);
1753 assert(message);
1754 assert(unit_name);
1755 assert(w >= 0);
1756 assert(w <= _INHIBIT_WHAT_MAX);
1757
1758 r = sd_bus_message_read(message, "b", &interactive);
1759 if (r < 0)
1760 return r;
1761
1762 /* Don't allow multiple jobs being executed at the same time */
1763 if (m->action_what > 0)
1764 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1765
1766 if (sleep_verb) {
1767 r = can_sleep(sleep_verb);
1768 if (r == -ENOSPC)
1769 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1770 "Not enough swap space for hibernation");
1771 if (r == 0)
1772 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1773 "Sleep verb \"%s\" not supported", sleep_verb);
1774 if (r < 0)
1775 return r;
1776 }
1777
1778 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1779 action_ignore_inhibit, error);
1780 if (r != 0)
1781 return r;
1782
1783 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
1784 if (r < 0)
1785 return r;
1786
1787 return sd_bus_reply_method_return(message, NULL);
1788 }
1789
1790 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1791 Manager *m = userdata;
1792
1793 return method_do_shutdown_or_sleep(
1794 m, message,
1795 SPECIAL_POWEROFF_TARGET,
1796 INHIBIT_SHUTDOWN,
1797 "org.freedesktop.login1.power-off",
1798 "org.freedesktop.login1.power-off-multiple-sessions",
1799 "org.freedesktop.login1.power-off-ignore-inhibit",
1800 NULL,
1801 error);
1802 }
1803
1804 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1805 Manager *m = userdata;
1806
1807 return method_do_shutdown_or_sleep(
1808 m, message,
1809 SPECIAL_REBOOT_TARGET,
1810 INHIBIT_SHUTDOWN,
1811 "org.freedesktop.login1.reboot",
1812 "org.freedesktop.login1.reboot-multiple-sessions",
1813 "org.freedesktop.login1.reboot-ignore-inhibit",
1814 NULL,
1815 error);
1816 }
1817
1818 static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1819 Manager *m = userdata;
1820
1821 return method_do_shutdown_or_sleep(
1822 m, message,
1823 SPECIAL_HALT_TARGET,
1824 INHIBIT_SHUTDOWN,
1825 "org.freedesktop.login1.halt",
1826 "org.freedesktop.login1.halt-multiple-sessions",
1827 "org.freedesktop.login1.halt-ignore-inhibit",
1828 NULL,
1829 error);
1830 }
1831
1832 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1833 Manager *m = userdata;
1834
1835 return method_do_shutdown_or_sleep(
1836 m, message,
1837 SPECIAL_SUSPEND_TARGET,
1838 INHIBIT_SLEEP,
1839 "org.freedesktop.login1.suspend",
1840 "org.freedesktop.login1.suspend-multiple-sessions",
1841 "org.freedesktop.login1.suspend-ignore-inhibit",
1842 "suspend",
1843 error);
1844 }
1845
1846 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1847 Manager *m = userdata;
1848
1849 return method_do_shutdown_or_sleep(
1850 m, message,
1851 SPECIAL_HIBERNATE_TARGET,
1852 INHIBIT_SLEEP,
1853 "org.freedesktop.login1.hibernate",
1854 "org.freedesktop.login1.hibernate-multiple-sessions",
1855 "org.freedesktop.login1.hibernate-ignore-inhibit",
1856 "hibernate",
1857 error);
1858 }
1859
1860 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1861 Manager *m = userdata;
1862
1863 return method_do_shutdown_or_sleep(
1864 m, message,
1865 SPECIAL_HYBRID_SLEEP_TARGET,
1866 INHIBIT_SLEEP,
1867 "org.freedesktop.login1.hibernate",
1868 "org.freedesktop.login1.hibernate-multiple-sessions",
1869 "org.freedesktop.login1.hibernate-ignore-inhibit",
1870 "hybrid-sleep",
1871 error);
1872 }
1873
1874 static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1875 Manager *m = userdata;
1876
1877 return method_do_shutdown_or_sleep(
1878 m, message,
1879 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
1880 INHIBIT_SLEEP,
1881 "org.freedesktop.login1.hibernate",
1882 "org.freedesktop.login1.hibernate-multiple-sessions",
1883 "org.freedesktop.login1.hibernate-ignore-inhibit",
1884 "hybrid-sleep",
1885 error);
1886 }
1887
1888 static int nologin_timeout_handler(
1889 sd_event_source *s,
1890 uint64_t usec,
1891 void *userdata) {
1892
1893 Manager *m = userdata;
1894
1895 log_info("Creating /run/nologin, blocking further logins...");
1896
1897 m->unlink_nologin =
1898 create_shutdown_run_nologin_or_warn() >= 0;
1899
1900 return 0;
1901 }
1902
1903 static int update_schedule_file(Manager *m) {
1904 _cleanup_free_ char *temp_path = NULL;
1905 _cleanup_fclose_ FILE *f = NULL;
1906 int r;
1907
1908 assert(m);
1909
1910 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
1911 if (r < 0)
1912 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
1913
1914 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
1915 if (r < 0)
1916 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
1917
1918 (void) fchmod(fileno(f), 0644);
1919
1920 fprintf(f,
1921 "USEC="USEC_FMT"\n"
1922 "WARN_WALL=%i\n"
1923 "MODE=%s\n",
1924 m->scheduled_shutdown_timeout,
1925 m->enable_wall_messages,
1926 m->scheduled_shutdown_type);
1927
1928 if (!isempty(m->wall_message)) {
1929 _cleanup_free_ char *t;
1930
1931 t = cescape(m->wall_message);
1932 if (!t) {
1933 r = -ENOMEM;
1934 goto fail;
1935 }
1936
1937 fprintf(f, "WALL_MESSAGE=%s\n", t);
1938 }
1939
1940 r = fflush_and_check(f);
1941 if (r < 0)
1942 goto fail;
1943
1944 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
1945 r = -errno;
1946 goto fail;
1947 }
1948
1949 return 0;
1950
1951 fail:
1952 (void) unlink(temp_path);
1953 (void) unlink("/run/systemd/shutdown/scheduled");
1954
1955 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
1956 }
1957
1958 static void reset_scheduled_shutdown(Manager *m) {
1959 assert(m);
1960
1961 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
1962 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
1963 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
1964
1965 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
1966 m->scheduled_shutdown_timeout = 0;
1967 m->shutdown_dry_run = false;
1968
1969 if (m->unlink_nologin) {
1970 (void) unlink_or_warn("/run/nologin");
1971 m->unlink_nologin = false;
1972 }
1973
1974 (void) unlink("/run/systemd/shutdown/scheduled");
1975 }
1976
1977 static int manager_scheduled_shutdown_handler(
1978 sd_event_source *s,
1979 uint64_t usec,
1980 void *userdata) {
1981
1982 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1983 Manager *m = userdata;
1984 const char *target;
1985 int r;
1986
1987 assert(m);
1988
1989 if (isempty(m->scheduled_shutdown_type))
1990 return 0;
1991
1992 if (streq(m->scheduled_shutdown_type, "poweroff"))
1993 target = SPECIAL_POWEROFF_TARGET;
1994 else if (streq(m->scheduled_shutdown_type, "reboot"))
1995 target = SPECIAL_REBOOT_TARGET;
1996 else if (streq(m->scheduled_shutdown_type, "halt"))
1997 target = SPECIAL_HALT_TARGET;
1998 else
1999 assert_not_reached("unexpected shutdown type");
2000
2001 /* Don't allow multiple jobs being executed at the same time */
2002 if (m->action_what > 0) {
2003 r = -EALREADY;
2004 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target);
2005 goto error;
2006 }
2007
2008 if (m->shutdown_dry_run) {
2009 /* We do not process delay inhibitors here. Otherwise, we
2010 * would have to be considered "in progress" (like the check
2011 * above) for some seconds after our admin has seen the final
2012 * wall message. */
2013
2014 bus_manager_log_shutdown(m, target);
2015 log_info("Running in dry run, suppressing action.");
2016 reset_scheduled_shutdown(m);
2017
2018 return 0;
2019 }
2020
2021 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error);
2022 if (r < 0) {
2023 log_error_errno(r, "Scheduled shutdown to %s failed: %m", target);
2024 goto error;
2025 }
2026
2027 return 0;
2028
2029 error:
2030 reset_scheduled_shutdown(m);
2031 return r;
2032 }
2033
2034 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2035 Manager *m = userdata;
2036 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2037 const char *action_multiple_sessions = NULL;
2038 const char *action_ignore_inhibit = NULL;
2039 const char *action = NULL;
2040 uint64_t elapse;
2041 char *type;
2042 int r;
2043 bool dry_run = false;
2044
2045 assert(m);
2046 assert(message);
2047
2048 r = sd_bus_message_read(message, "st", &type, &elapse);
2049 if (r < 0)
2050 return r;
2051
2052 if (startswith(type, "dry-")) {
2053 type += 4;
2054 dry_run = true;
2055 }
2056
2057 if (streq(type, "poweroff")) {
2058 action = "org.freedesktop.login1.power-off";
2059 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2060 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2061 } else if (streq(type, "reboot")) {
2062 action = "org.freedesktop.login1.reboot";
2063 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2064 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2065 } else if (streq(type, "halt")) {
2066 action = "org.freedesktop.login1.halt";
2067 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2068 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2069 } else
2070 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2071
2072 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2073 action, action_multiple_sessions, action_ignore_inhibit, error);
2074 if (r != 0)
2075 return r;
2076
2077 if (m->scheduled_shutdown_timeout_source) {
2078 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2079 if (r < 0)
2080 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2081
2082 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2083 if (r < 0)
2084 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2085 } else {
2086 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2087 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2088 if (r < 0)
2089 return log_error_errno(r, "sd_event_add_time() failed: %m");
2090 }
2091
2092 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2093 if (r < 0) {
2094 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2095 return log_oom();
2096 }
2097
2098 m->shutdown_dry_run = dry_run;
2099
2100 if (m->nologin_timeout_source) {
2101 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2102 if (r < 0)
2103 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2104
2105 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2106 if (r < 0)
2107 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2108 } else {
2109 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2110 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2111 if (r < 0)
2112 return log_error_errno(r, "sd_event_add_time() failed: %m");
2113 }
2114
2115 m->scheduled_shutdown_timeout = elapse;
2116
2117 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2118 if (r >= 0) {
2119 const char *tty = NULL;
2120
2121 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2122 (void) sd_bus_creds_get_tty(creds, &tty);
2123
2124 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2125 if (r < 0) {
2126 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2127 return log_oom();
2128 }
2129 }
2130
2131 r = manager_setup_wall_message_timer(m);
2132 if (r < 0)
2133 return r;
2134
2135 r = update_schedule_file(m);
2136 if (r < 0)
2137 return r;
2138
2139 return sd_bus_reply_method_return(message, NULL);
2140 }
2141
2142 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2143 Manager *m = userdata;
2144 bool cancelled;
2145
2146 assert(m);
2147 assert(message);
2148
2149 cancelled = m->scheduled_shutdown_type != NULL;
2150 reset_scheduled_shutdown(m);
2151
2152 if (cancelled && m->enable_wall_messages) {
2153 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2154 _cleanup_free_ char *username = NULL;
2155 const char *tty = NULL;
2156 uid_t uid = 0;
2157 int r;
2158
2159 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2160 if (r >= 0) {
2161 (void) sd_bus_creds_get_uid(creds, &uid);
2162 (void) sd_bus_creds_get_tty(creds, &tty);
2163 }
2164
2165 username = uid_to_name(uid);
2166 utmp_wall("The system shutdown has been cancelled",
2167 username, tty, logind_wall_tty_filter, m);
2168 }
2169
2170 return sd_bus_reply_method_return(message, "b", cancelled);
2171 }
2172
2173 static int method_can_shutdown_or_sleep(
2174 Manager *m,
2175 sd_bus_message *message,
2176 InhibitWhat w,
2177 const char *action,
2178 const char *action_multiple_sessions,
2179 const char *action_ignore_inhibit,
2180 const char *sleep_verb,
2181 sd_bus_error *error) {
2182
2183 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2184 HandleAction handle;
2185 bool multiple_sessions, challenge, blocked;
2186 const char *result = NULL;
2187 uid_t uid;
2188 int r;
2189
2190 assert(m);
2191 assert(message);
2192 assert(w >= 0);
2193 assert(w <= _INHIBIT_WHAT_MAX);
2194 assert(action);
2195 assert(action_multiple_sessions);
2196 assert(action_ignore_inhibit);
2197
2198 if (sleep_verb) {
2199 r = can_sleep(sleep_verb);
2200 if (IN_SET(r, 0, -ENOSPC))
2201 return sd_bus_reply_method_return(message, "s", "na");
2202 if (r < 0)
2203 return r;
2204 }
2205
2206 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2207 if (r < 0)
2208 return r;
2209
2210 r = sd_bus_creds_get_euid(creds, &uid);
2211 if (r < 0)
2212 return r;
2213
2214 r = have_multiple_sessions(m, uid);
2215 if (r < 0)
2216 return r;
2217
2218 multiple_sessions = r > 0;
2219 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2220
2221 handle = handle_action_from_string(sleep_verb);
2222 if (handle >= 0) {
2223 const char *target;
2224
2225 target = manager_target_for_action(handle);
2226 if (target) {
2227 _cleanup_free_ char *load_state = NULL;
2228
2229 r = unit_load_state(m->bus, target, &load_state);
2230 if (r < 0)
2231 return r;
2232
2233 if (!streq(load_state, "loaded")) {
2234 result = "no";
2235 goto finish;
2236 }
2237 }
2238 }
2239
2240 if (multiple_sessions) {
2241 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2242 if (r < 0)
2243 return r;
2244
2245 if (r > 0)
2246 result = "yes";
2247 else if (challenge)
2248 result = "challenge";
2249 else
2250 result = "no";
2251 }
2252
2253 if (blocked) {
2254 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2255 if (r < 0)
2256 return r;
2257
2258 if (r > 0) {
2259 if (!result)
2260 result = "yes";
2261 } else if (challenge) {
2262 if (!result || streq(result, "yes"))
2263 result = "challenge";
2264 } else
2265 result = "no";
2266 }
2267
2268 if (!multiple_sessions && !blocked) {
2269 /* If neither inhibit nor multiple sessions
2270 * apply then just check the normal policy */
2271
2272 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2273 if (r < 0)
2274 return r;
2275
2276 if (r > 0)
2277 result = "yes";
2278 else if (challenge)
2279 result = "challenge";
2280 else
2281 result = "no";
2282 }
2283
2284 finish:
2285 return sd_bus_reply_method_return(message, "s", result);
2286 }
2287
2288 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2289 Manager *m = userdata;
2290
2291 return method_can_shutdown_or_sleep(
2292 m, message,
2293 INHIBIT_SHUTDOWN,
2294 "org.freedesktop.login1.power-off",
2295 "org.freedesktop.login1.power-off-multiple-sessions",
2296 "org.freedesktop.login1.power-off-ignore-inhibit",
2297 NULL,
2298 error);
2299 }
2300
2301 static int method_can_reboot(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.reboot",
2308 "org.freedesktop.login1.reboot-multiple-sessions",
2309 "org.freedesktop.login1.reboot-ignore-inhibit",
2310 NULL,
2311 error);
2312 }
2313
2314 static int method_can_halt(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.halt",
2321 "org.freedesktop.login1.halt-multiple-sessions",
2322 "org.freedesktop.login1.halt-ignore-inhibit",
2323 NULL,
2324 error);
2325 }
2326
2327 static int method_can_suspend(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_SLEEP,
2333 "org.freedesktop.login1.suspend",
2334 "org.freedesktop.login1.suspend-multiple-sessions",
2335 "org.freedesktop.login1.suspend-ignore-inhibit",
2336 "suspend",
2337 error);
2338 }
2339
2340 static int method_can_hibernate(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.hibernate",
2347 "org.freedesktop.login1.hibernate-multiple-sessions",
2348 "org.freedesktop.login1.hibernate-ignore-inhibit",
2349 "hibernate",
2350 error);
2351 }
2352
2353 static int method_can_hybrid_sleep(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 "hybrid-sleep",
2363 error);
2364 }
2365
2366 static int method_can_suspend_then_hibernate(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 "suspend-then-hibernate",
2376 error);
2377 }
2378
2379 static int property_get_reboot_to_firmware_setup(
2380 sd_bus *bus,
2381 const char *path,
2382 const char *interface,
2383 const char *property,
2384 sd_bus_message *reply,
2385 void *userdata,
2386 sd_bus_error *error) {
2387 int r;
2388
2389 assert(bus);
2390 assert(reply);
2391 assert(userdata);
2392
2393 r = efi_get_reboot_to_firmware();
2394 if (r < 0 && r != -EOPNOTSUPP)
2395 log_warning_errno(r, "Failed to determine reboot-to-firmware state: %m");
2396
2397 return sd_bus_message_append(reply, "b", r > 0);
2398 }
2399
2400 static int method_set_reboot_to_firmware_setup(
2401 sd_bus_message *message,
2402 void *userdata,
2403 sd_bus_error *error) {
2404
2405 int b, r;
2406 Manager *m = userdata;
2407
2408 assert(message);
2409 assert(m);
2410
2411 r = sd_bus_message_read(message, "b", &b);
2412 if (r < 0)
2413 return r;
2414
2415 r = bus_verify_polkit_async(message,
2416 CAP_SYS_ADMIN,
2417 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2418 NULL,
2419 false,
2420 UID_INVALID,
2421 &m->polkit_registry,
2422 error);
2423 if (r < 0)
2424 return r;
2425 if (r == 0)
2426 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2427
2428 r = efi_set_reboot_to_firmware(b);
2429 if (r < 0)
2430 return r;
2431
2432 return sd_bus_reply_method_return(message, NULL);
2433 }
2434
2435 static int method_can_reboot_to_firmware_setup(
2436 sd_bus_message *message,
2437 void *userdata,
2438 sd_bus_error *error) {
2439
2440 int r;
2441 bool challenge;
2442 const char *result;
2443 Manager *m = userdata;
2444
2445 assert(message);
2446 assert(m);
2447
2448 r = efi_reboot_to_firmware_supported();
2449 if (r < 0) {
2450 if (r != -EOPNOTSUPP)
2451 log_warning_errno(r, "Failed to determine whether reboot to firmware is supported: %m");
2452
2453 return sd_bus_reply_method_return(message, "s", "na");
2454 }
2455
2456 r = bus_test_polkit(message,
2457 CAP_SYS_ADMIN,
2458 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2459 NULL,
2460 UID_INVALID,
2461 &challenge,
2462 error);
2463 if (r < 0)
2464 return r;
2465
2466 if (r > 0)
2467 result = "yes";
2468 else if (challenge)
2469 result = "challenge";
2470 else
2471 result = "no";
2472
2473 return sd_bus_reply_method_return(message, "s", result);
2474 }
2475
2476 static int method_set_wall_message(
2477 sd_bus_message *message,
2478 void *userdata,
2479 sd_bus_error *error) {
2480
2481 int r;
2482 Manager *m = userdata;
2483 char *wall_message;
2484 unsigned enable_wall_messages;
2485
2486 assert(message);
2487 assert(m);
2488
2489 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2490 if (r < 0)
2491 return r;
2492
2493 r = bus_verify_polkit_async(message,
2494 CAP_SYS_ADMIN,
2495 "org.freedesktop.login1.set-wall-message",
2496 NULL,
2497 false,
2498 UID_INVALID,
2499 &m->polkit_registry,
2500 error);
2501 if (r < 0)
2502 return r;
2503 if (r == 0)
2504 return 1; /* Will call us back */
2505
2506 r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
2507 if (r < 0)
2508 return log_oom();
2509
2510 m->enable_wall_messages = enable_wall_messages;
2511
2512 return sd_bus_reply_method_return(message, NULL);
2513 }
2514
2515 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2516 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2517 const char *who, *why, *what, *mode;
2518 _cleanup_free_ char *id = NULL;
2519 _cleanup_close_ int fifo_fd = -1;
2520 Manager *m = userdata;
2521 Inhibitor *i = NULL;
2522 InhibitMode mm;
2523 InhibitWhat w;
2524 pid_t pid;
2525 uid_t uid;
2526 int r;
2527
2528 assert(message);
2529 assert(m);
2530
2531 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2532 if (r < 0)
2533 return r;
2534
2535 w = inhibit_what_from_string(what);
2536 if (w <= 0)
2537 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2538
2539 mm = inhibit_mode_from_string(mode);
2540 if (mm < 0)
2541 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2542
2543 /* Delay is only supported for shutdown/sleep */
2544 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2545 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2546
2547 /* Don't allow taking delay locks while we are already
2548 * executing the operation. We shouldn't create the impression
2549 * that the lock was successful if the machine is about to go
2550 * down/suspend any moment. */
2551 if (m->action_what & w)
2552 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2553
2554 r = bus_verify_polkit_async(
2555 message,
2556 CAP_SYS_BOOT,
2557 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2558 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2559 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2560 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2561 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2562 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2563 "org.freedesktop.login1.inhibit-handle-lid-switch",
2564 NULL,
2565 false,
2566 UID_INVALID,
2567 &m->polkit_registry,
2568 error);
2569 if (r < 0)
2570 return r;
2571 if (r == 0)
2572 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2573
2574 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2575 if (r < 0)
2576 return r;
2577
2578 r = sd_bus_creds_get_euid(creds, &uid);
2579 if (r < 0)
2580 return r;
2581
2582 r = sd_bus_creds_get_pid(creds, &pid);
2583 if (r < 0)
2584 return r;
2585
2586 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2587 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2588
2589 do {
2590 id = mfree(id);
2591
2592 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2593 return -ENOMEM;
2594
2595 } while (hashmap_get(m->inhibitors, id));
2596
2597 r = manager_add_inhibitor(m, id, &i);
2598 if (r < 0)
2599 return r;
2600
2601 i->what = w;
2602 i->mode = mm;
2603 i->pid = pid;
2604 i->uid = uid;
2605 i->why = strdup(why);
2606 i->who = strdup(who);
2607
2608 if (!i->why || !i->who) {
2609 r = -ENOMEM;
2610 goto fail;
2611 }
2612
2613 fifo_fd = inhibitor_create_fifo(i);
2614 if (fifo_fd < 0) {
2615 r = fifo_fd;
2616 goto fail;
2617 }
2618
2619 inhibitor_start(i);
2620
2621 return sd_bus_reply_method_return(message, "h", fifo_fd);
2622
2623 fail:
2624 if (i)
2625 inhibitor_free(i);
2626
2627 return r;
2628 }
2629
2630 const sd_bus_vtable manager_vtable[] = {
2631 SD_BUS_VTABLE_START(0),
2632
2633 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2634 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2635
2636 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2637 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2638 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2639 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2640 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, 0),
2641 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2642 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2643 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2644 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2645 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2646 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2647 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL, offsetof(Manager, user_stop_delay), SD_BUS_VTABLE_PROPERTY_CONST),
2648 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2649 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2650 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2651 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2652 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
2653 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2654 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2655 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2656 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2657 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2658 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2659 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2660 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2661 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed, 0, 0),
2662 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power, 0, 0),
2663 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2664 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2665 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2666 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0),
2667 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2668 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0),
2669 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
2670
2671 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2672 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2673 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2674 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2675 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2676 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2677 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2678 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2679 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2680 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2681 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2682 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2683 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2684 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2685 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2686 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2687 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2688 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2689 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2690 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2691 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2692 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2693 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2694 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2695 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2696 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2697 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2698 SD_BUS_METHOD("Halt", "b", NULL, method_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2699 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2700 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2701 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2702 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL, method_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2703 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2704 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2705 SD_BUS_METHOD("CanHalt", NULL, "s", method_can_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2706 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2707 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2708 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2709 SD_BUS_METHOD("CanSuspendThenHibernate", NULL, "s", method_can_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2710 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2711 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2712 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2713 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2714 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2715 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2716
2717 SD_BUS_SIGNAL("SessionNew", "so", 0),
2718 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2719 SD_BUS_SIGNAL("UserNew", "uo", 0),
2720 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2721 SD_BUS_SIGNAL("SeatNew", "so", 0),
2722 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2723 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2724 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2725
2726 SD_BUS_VTABLE_END
2727 };
2728
2729 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2730 assert(s);
2731 assert(unit);
2732
2733 if (!s->started)
2734 return 0;
2735
2736 if (result && !streq(result, "done")) {
2737 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2738
2739 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit '%s' failed with '%s'", unit, result);
2740 return session_send_create_reply(s, &e);
2741 }
2742
2743 return session_send_create_reply(s, NULL);
2744 }
2745
2746 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2747 const char *path, *result, *unit;
2748 Manager *m = userdata;
2749 Session *session;
2750 uint32_t id;
2751 User *user;
2752 int r;
2753
2754 assert(message);
2755 assert(m);
2756
2757 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2758 if (r < 0) {
2759 bus_log_parse_error(r);
2760 return 0;
2761 }
2762
2763 if (m->action_job && streq(m->action_job, path)) {
2764 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2765
2766 /* Tell people that they now may take a lock again */
2767 (void) send_prepare_for(m, m->action_what, false);
2768
2769 m->action_job = mfree(m->action_job);
2770 m->action_unit = NULL;
2771 m->action_what = 0;
2772 return 0;
2773 }
2774
2775 session = hashmap_get(m->session_units, unit);
2776 if (session) {
2777 if (streq_ptr(path, session->scope_job)) {
2778 session->scope_job = mfree(session->scope_job);
2779 (void) session_jobs_reply(session, unit, result);
2780
2781 session_save(session);
2782 user_save(session->user);
2783 }
2784
2785 session_add_to_gc_queue(session);
2786 }
2787
2788 user = hashmap_get(m->user_units, unit);
2789 if (user) {
2790 if (streq_ptr(path, user->service_job)) {
2791 user->service_job = mfree(user->service_job);
2792
2793 LIST_FOREACH(sessions_by_user, session, user->sessions)
2794 (void) session_jobs_reply(session, unit, NULL /* don't propagate user service failures to the client */);
2795
2796 user_save(user);
2797 }
2798
2799 user_add_to_gc_queue(user);
2800 }
2801
2802 return 0;
2803 }
2804
2805 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2806 const char *path, *unit;
2807 Manager *m = userdata;
2808 Session *session;
2809 User *user;
2810 int r;
2811
2812 assert(message);
2813 assert(m);
2814
2815 r = sd_bus_message_read(message, "so", &unit, &path);
2816 if (r < 0) {
2817 bus_log_parse_error(r);
2818 return 0;
2819 }
2820
2821 session = hashmap_get(m->session_units, unit);
2822 if (session)
2823 session_add_to_gc_queue(session);
2824
2825 user = hashmap_get(m->user_units, unit);
2826 if (user)
2827 user_add_to_gc_queue(user);
2828
2829 return 0;
2830 }
2831
2832 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2833 _cleanup_free_ char *unit = NULL;
2834 Manager *m = userdata;
2835 const char *path;
2836 Session *session;
2837 User *user;
2838 int r;
2839
2840 assert(message);
2841 assert(m);
2842
2843 path = sd_bus_message_get_path(message);
2844 if (!path)
2845 return 0;
2846
2847 r = unit_name_from_dbus_path(path, &unit);
2848 if (r == -EINVAL) /* not a unit */
2849 return 0;
2850 if (r < 0) {
2851 log_oom();
2852 return 0;
2853 }
2854
2855 session = hashmap_get(m->session_units, unit);
2856 if (session)
2857 session_add_to_gc_queue(session);
2858
2859 user = hashmap_get(m->user_units, unit);
2860 if (user)
2861 user_add_to_gc_queue(user);
2862
2863 return 0;
2864 }
2865
2866 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2867 Manager *m = userdata;
2868 Session *session;
2869 Iterator i;
2870 int b, r;
2871
2872 assert(message);
2873 assert(m);
2874
2875 r = sd_bus_message_read(message, "b", &b);
2876 if (r < 0) {
2877 bus_log_parse_error(r);
2878 return 0;
2879 }
2880
2881 if (b)
2882 return 0;
2883
2884 /* systemd finished reloading, let's recheck all our sessions */
2885 log_debug("System manager has been reloaded, rechecking sessions...");
2886
2887 HASHMAP_FOREACH(session, m->sessions, i)
2888 session_add_to_gc_queue(session);
2889
2890 return 0;
2891 }
2892
2893 int manager_send_changed(Manager *manager, const char *property, ...) {
2894 char **l;
2895
2896 assert(manager);
2897
2898 l = strv_from_stdarg_alloca(property);
2899
2900 return sd_bus_emit_properties_changed_strv(
2901 manager->bus,
2902 "/org/freedesktop/login1",
2903 "org.freedesktop.login1.Manager",
2904 l);
2905 }
2906
2907 static int strdup_job(sd_bus_message *reply, char **job) {
2908 const char *j;
2909 char *copy;
2910 int r;
2911
2912 r = sd_bus_message_read(reply, "o", &j);
2913 if (r < 0)
2914 return r;
2915
2916 copy = strdup(j);
2917 if (!copy)
2918 return -ENOMEM;
2919
2920 *job = copy;
2921 return 1;
2922 }
2923
2924 int manager_start_scope(
2925 Manager *manager,
2926 const char *scope,
2927 pid_t pid,
2928 const char *slice,
2929 const char *description,
2930 char **wants,
2931 char **after,
2932 const char *requires_mounts_for,
2933 sd_bus_message *more_properties,
2934 sd_bus_error *error,
2935 char **job) {
2936
2937 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
2938 char **i;
2939 int r;
2940
2941 assert(manager);
2942 assert(scope);
2943 assert(pid > 1);
2944 assert(job);
2945
2946 r = sd_bus_message_new_method_call(
2947 manager->bus,
2948 &m,
2949 "org.freedesktop.systemd1",
2950 "/org/freedesktop/systemd1",
2951 "org.freedesktop.systemd1.Manager",
2952 "StartTransientUnit");
2953 if (r < 0)
2954 return r;
2955
2956 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
2957 if (r < 0)
2958 return r;
2959
2960 r = sd_bus_message_open_container(m, 'a', "(sv)");
2961 if (r < 0)
2962 return r;
2963
2964 if (!isempty(slice)) {
2965 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
2966 if (r < 0)
2967 return r;
2968 }
2969
2970 if (!isempty(description)) {
2971 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
2972 if (r < 0)
2973 return r;
2974 }
2975
2976 STRV_FOREACH(i, wants) {
2977 r = sd_bus_message_append(m, "(sv)", "Wants", "as", 1, *i);
2978 if (r < 0)
2979 return r;
2980 }
2981
2982 STRV_FOREACH(i, after) {
2983 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, *i);
2984 if (r < 0)
2985 return r;
2986 }
2987
2988 if (!empty_or_root(requires_mounts_for)) {
2989 r = sd_bus_message_append(m, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for);
2990 if (r < 0)
2991 return r;
2992 }
2993
2994 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
2995 * SIGTERM */
2996 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
2997 if (r < 0)
2998 return r;
2999
3000 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
3001 if (r < 0)
3002 return r;
3003
3004 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3005 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", (uint64_t)-1);
3006 if (r < 0)
3007 return bus_log_create_error(r);
3008
3009 if (more_properties) {
3010 /* If TasksMax also appears here, it will overwrite the default value set above */
3011 r = sd_bus_message_copy(m, more_properties, true);
3012 if (r < 0)
3013 return r;
3014 }
3015
3016 r = sd_bus_message_close_container(m);
3017 if (r < 0)
3018 return r;
3019
3020 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3021 if (r < 0)
3022 return r;
3023
3024 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3025 if (r < 0)
3026 return r;
3027
3028 return strdup_job(reply, job);
3029 }
3030
3031 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3032 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3033 int r;
3034
3035 assert(manager);
3036 assert(unit);
3037 assert(job);
3038
3039 r = sd_bus_call_method(
3040 manager->bus,
3041 "org.freedesktop.systemd1",
3042 "/org/freedesktop/systemd1",
3043 "org.freedesktop.systemd1.Manager",
3044 "StartUnit",
3045 error,
3046 &reply,
3047 "ss", unit, "replace");
3048 if (r < 0)
3049 return r;
3050
3051 return strdup_job(reply, job);
3052 }
3053
3054 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3055 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3056 int r;
3057
3058 assert(manager);
3059 assert(unit);
3060 assert(job);
3061
3062 r = sd_bus_call_method(
3063 manager->bus,
3064 "org.freedesktop.systemd1",
3065 "/org/freedesktop/systemd1",
3066 "org.freedesktop.systemd1.Manager",
3067 "StopUnit",
3068 error,
3069 &reply,
3070 "ss", unit, "fail");
3071 if (r < 0) {
3072 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3073 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3074
3075 *job = NULL;
3076 sd_bus_error_free(error);
3077 return 0;
3078 }
3079
3080 return r;
3081 }
3082
3083 return strdup_job(reply, job);
3084 }
3085
3086 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *ret_error) {
3087 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3088 _cleanup_free_ char *path = NULL;
3089 int r;
3090
3091 assert(manager);
3092 assert(scope);
3093
3094 path = unit_dbus_path_from_name(scope);
3095 if (!path)
3096 return -ENOMEM;
3097
3098 r = sd_bus_call_method(
3099 manager->bus,
3100 "org.freedesktop.systemd1",
3101 path,
3102 "org.freedesktop.systemd1.Scope",
3103 "Abandon",
3104 &error,
3105 NULL,
3106 NULL);
3107 if (r < 0) {
3108 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3109 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED) ||
3110 sd_bus_error_has_name(&error, BUS_ERROR_SCOPE_NOT_RUNNING))
3111 return 0;
3112
3113 sd_bus_error_move(ret_error, &error);
3114 return r;
3115 }
3116
3117 return 1;
3118 }
3119
3120 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3121 assert(manager);
3122 assert(unit);
3123
3124 return sd_bus_call_method(
3125 manager->bus,
3126 "org.freedesktop.systemd1",
3127 "/org/freedesktop/systemd1",
3128 "org.freedesktop.systemd1.Manager",
3129 "KillUnit",
3130 error,
3131 NULL,
3132 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3133 }
3134
3135 int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret_error) {
3136 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3137 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3138 _cleanup_free_ char *path = NULL;
3139 const char *state;
3140 int r;
3141
3142 assert(manager);
3143 assert(unit);
3144
3145 path = unit_dbus_path_from_name(unit);
3146 if (!path)
3147 return -ENOMEM;
3148
3149 r = sd_bus_get_property(
3150 manager->bus,
3151 "org.freedesktop.systemd1",
3152 path,
3153 "org.freedesktop.systemd1.Unit",
3154 "ActiveState",
3155 &error,
3156 &reply,
3157 "s");
3158 if (r < 0) {
3159 /* systemd might have droppped off momentarily, let's
3160 * not make this an error */
3161 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3162 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3163 return true;
3164
3165 /* If the unit is already unloaded then it's not
3166 * active */
3167 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3168 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3169 return false;
3170
3171 sd_bus_error_move(ret_error, &error);
3172 return r;
3173 }
3174
3175 r = sd_bus_message_read(reply, "s", &state);
3176 if (r < 0)
3177 return r;
3178
3179 return !STR_IN_SET(state, "inactive", "failed");
3180 }
3181
3182 int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *ret_error) {
3183 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3184 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3185 int r;
3186
3187 assert(manager);
3188 assert(path);
3189
3190 r = sd_bus_get_property(
3191 manager->bus,
3192 "org.freedesktop.systemd1",
3193 path,
3194 "org.freedesktop.systemd1.Job",
3195 "State",
3196 &error,
3197 &reply,
3198 "s");
3199 if (r < 0) {
3200 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3201 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3202 return true;
3203
3204 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3205 return false;
3206
3207 sd_bus_error_move(ret_error, &error);
3208 return r;
3209 }
3210
3211 /* We don't actually care about the state really. The fact
3212 * that we could read the job state is enough for us */
3213
3214 return true;
3215 }