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