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