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