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