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