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