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