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