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