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