]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-dbus.c
Update 60-sensor.hwdb for Acer Acer One 10 S1002
[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
cc377381
LP
171static int property_get_idle_hint(
172 sd_bus *bus,
173 const char *path,
174 const char *interface,
175 const char *property,
176 sd_bus_message *reply,
ebcf1f97
LP
177 void *userdata,
178 sd_bus_error *error) {
a185c5aa 179
cc377381 180 Manager *m = userdata;
a185c5aa 181
cc377381
LP
182 assert(bus);
183 assert(reply);
184 assert(m);
185
186 return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
a185c5aa
LP
187}
188
cc377381
LP
189static int property_get_idle_since_hint(
190 sd_bus *bus,
191 const char *path,
192 const char *interface,
193 const char *property,
194 sd_bus_message *reply,
ebcf1f97
LP
195 void *userdata,
196 sd_bus_error *error) {
cc377381
LP
197
198 Manager *m = userdata;
5cb14b37 199 dual_timestamp t = DUAL_TIMESTAMP_NULL;
a185c5aa 200
cc377381
LP
201 assert(bus);
202 assert(reply);
a185c5aa
LP
203 assert(m);
204
205 manager_get_idle_hint(m, &t);
a185c5aa 206
cc377381 207 return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
a185c5aa
LP
208}
209
cc377381
LP
210static int property_get_inhibited(
211 sd_bus *bus,
212 const char *path,
213 const char *interface,
214 const char *property,
215 sd_bus_message *reply,
ebcf1f97
LP
216 void *userdata,
217 sd_bus_error *error) {
cc377381
LP
218
219 Manager *m = userdata;
f8e2fb7b 220 InhibitWhat w;
f8e2fb7b 221
cc377381
LP
222 assert(bus);
223 assert(reply);
224 assert(m);
f8e2fb7b 225
cc377381 226 w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
f8e2fb7b 227
cc377381 228 return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
f8e2fb7b
LP
229}
230
cc377381
LP
231static int property_get_preparing(
232 sd_bus *bus,
233 const char *path,
234 const char *interface,
235 const char *property,
236 sd_bus_message *reply,
ebcf1f97
LP
237 void *userdata,
238 sd_bus_error *error) {
cc377381
LP
239
240 Manager *m = userdata;
241 bool b;
5e4a79da 242
cc377381
LP
243 assert(bus);
244 assert(reply);
245 assert(m);
5e4a79da
LP
246
247 if (streq(property, "PreparingForShutdown"))
5d904a6a 248 b = m->action_what & INHIBIT_SHUTDOWN;
5e4a79da 249 else
5d904a6a 250 b = m->action_what & INHIBIT_SLEEP;
5e4a79da 251
cc377381 252 return sd_bus_message_append(reply, "b", b);
5e4a79da
LP
253}
254
8aaa023a
DM
255static int property_get_scheduled_shutdown(
256 sd_bus *bus,
257 const char *path,
258 const char *interface,
259 const char *property,
260 sd_bus_message *reply,
261 void *userdata,
262 sd_bus_error *error) {
263
264 Manager *m = userdata;
265 int r;
266
267 assert(bus);
268 assert(reply);
269 assert(m);
270
271 r = sd_bus_message_open_container(reply, 'r', "st");
272 if (r < 0)
273 return r;
274
275 r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
276 if (r < 0)
277 return r;
278
279 return sd_bus_message_close_container(reply);
280}
281
cc377381 282static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
01adcd69 283static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays);
9b9c23da 284static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed, "b", Manager, manager_is_lid_closed);
4e96eb68 285static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power, "b", manager_is_on_external_power);
01adcd69
YW
286static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX);
287static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size);
28414939 288
19070062 289static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
290 _cleanup_free_ char *p = NULL;
291 Manager *m = userdata;
292 const char *name;
293 Session *session;
294 int r;
295
cc377381
LP
296 assert(message);
297 assert(m);
298
299 r = sd_bus_message_read(message, "s", &name);
300 if (r < 0)
ebcf1f97 301 return r;
cc377381 302
309a29df
LP
303 r = manager_get_session_from_creds(m, message, name, error, &session);
304 if (r < 0)
305 return r;
cc377381
LP
306
307 p = session_bus_path(session);
308 if (!p)
ebcf1f97 309 return -ENOMEM;
cc377381 310
df2d202e 311 return sd_bus_reply_method_return(message, "o", p);
cc377381
LP
312}
313
7b33c622
AJ
314/* Get login session of a process. This is not what you are looking for these days,
315 * as apps may instead belong to a user service unit. This includes terminal
316 * emulators and hence command-line apps. */
19070062 317static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381 318 _cleanup_free_ char *p = NULL;
954449b8 319 Session *session = NULL;
cc377381 320 Manager *m = userdata;
4e724d9c 321 pid_t pid;
cc377381
LP
322 int r;
323
cc377381
LP
324 assert(message);
325 assert(m);
326
4e724d9c
LP
327 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
328
cc377381
LP
329 r = sd_bus_message_read(message, "u", &pid);
330 if (r < 0)
ebcf1f97 331 return r;
07b38ba5 332 if (pid < 0)
06820eaf 333 return -EINVAL;
cc377381 334
06820eaf 335 if (pid == 0) {
309a29df 336 r = manager_get_session_from_creds(m, message, NULL, error, &session);
5b12334d
LP
337 if (r < 0)
338 return r;
309a29df
LP
339 } else {
340 r = manager_get_session_by_pid(m, pid, &session);
4e724d9c 341 if (r < 0)
ebcf1f97 342 return r;
4e724d9c 343
309a29df
LP
344 if (!session)
345 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
346 }
cc377381
LP
347
348 p = session_bus_path(session);
349 if (!p)
ebcf1f97 350 return -ENOMEM;
cc377381 351
df2d202e 352 return sd_bus_reply_method_return(message, "o", p);
cc377381
LP
353}
354
19070062 355static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
356 _cleanup_free_ char *p = NULL;
357 Manager *m = userdata;
358 uint32_t uid;
359 User *user;
360 int r;
361
cc377381
LP
362 assert(message);
363 assert(m);
364
365 r = sd_bus_message_read(message, "u", &uid);
366 if (r < 0)
ebcf1f97 367 return r;
cc377381 368
309a29df
LP
369 r = manager_get_user_from_creds(m, message, uid, error, &user);
370 if (r < 0)
371 return r;
cc377381
LP
372
373 p = user_bus_path(user);
374 if (!p)
ebcf1f97 375 return -ENOMEM;
cc377381 376
df2d202e 377 return sd_bus_reply_method_return(message, "o", p);
cc377381
LP
378}
379
19070062 380static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
381 _cleanup_free_ char *p = NULL;
382 Manager *m = userdata;
954449b8 383 User *user = NULL;
4e724d9c 384 pid_t pid;
fb6becb4 385 int r;
98a28fef 386
cc377381 387 assert(message);
98a28fef 388 assert(m);
cc377381 389
4e724d9c
LP
390 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
391
cc377381
LP
392 r = sd_bus_message_read(message, "u", &pid);
393 if (r < 0)
ebcf1f97 394 return r;
07b38ba5 395 if (pid < 0)
06820eaf 396 return -EINVAL;
cc377381 397
06820eaf 398 if (pid == 0) {
309a29df 399 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
5b12334d
LP
400 if (r < 0)
401 return r;
309a29df
LP
402 } else {
403 r = manager_get_user_by_pid(m, pid, &user);
4e724d9c 404 if (r < 0)
ebcf1f97 405 return r;
309a29df 406 if (!user)
095b8833
AJ
407 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID,
408 "PID "PID_FMT" does not belong to any logged in user or lingering user",
409 pid);
4e724d9c
LP
410 }
411
cc377381
LP
412 p = user_bus_path(user);
413 if (!p)
ebcf1f97 414 return -ENOMEM;
cc377381 415
df2d202e 416 return sd_bus_reply_method_return(message, "o", p);
cc377381
LP
417}
418
19070062 419static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
420 _cleanup_free_ char *p = NULL;
421 Manager *m = userdata;
422 const char *name;
423 Seat *seat;
424 int r;
425
98a28fef 426 assert(message);
cc377381 427 assert(m);
98a28fef 428
cc377381
LP
429 r = sd_bus_message_read(message, "s", &name);
430 if (r < 0)
ebcf1f97 431 return r;
98a28fef 432
309a29df
LP
433 r = manager_get_seat_from_creds(m, message, name, error, &seat);
434 if (r < 0)
435 return r;
98a28fef 436
cc377381
LP
437 p = seat_bus_path(seat);
438 if (!p)
ebcf1f97 439 return -ENOMEM;
98a28fef 440
df2d202e 441 return sd_bus_reply_method_return(message, "o", p);
cc377381 442}
98a28fef 443
19070062 444static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
4afd3348 445 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
cc377381
LP
446 Manager *m = userdata;
447 Session *session;
448 Iterator i;
449 int r;
450
cc377381
LP
451 assert(message);
452 assert(m);
98a28fef 453
df2d202e 454 r = sd_bus_message_new_method_return(message, &reply);
cc377381 455 if (r < 0)
ebcf1f97 456 return r;
98a28fef 457
cc377381
LP
458 r = sd_bus_message_open_container(reply, 'a', "(susso)");
459 if (r < 0)
ebcf1f97 460 return r;
cc377381
LP
461
462 HASHMAP_FOREACH(session, m->sessions, i) {
463 _cleanup_free_ char *p = NULL;
464
465 p = session_bus_path(session);
466 if (!p)
ebcf1f97 467 return -ENOMEM;
cc377381
LP
468
469 r = sd_bus_message_append(reply, "(susso)",
470 session->id,
471 (uint32_t) session->user->uid,
472 session->user->name,
473 session->seat ? session->seat->id : "",
474 p);
475 if (r < 0)
ebcf1f97 476 return r;
cc377381
LP
477 }
478
479 r = sd_bus_message_close_container(reply);
480 if (r < 0)
ebcf1f97 481 return r;
cc377381 482
9030ca46 483 return sd_bus_send(NULL, reply, NULL);
cc377381
LP
484}
485
19070062 486static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
4afd3348 487 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
cc377381
LP
488 Manager *m = userdata;
489 User *user;
490 Iterator i;
491 int r;
492
cc377381
LP
493 assert(message);
494 assert(m);
495
df2d202e 496 r = sd_bus_message_new_method_return(message, &reply);
cc377381 497 if (r < 0)
ebcf1f97 498 return r;
cc377381
LP
499
500 r = sd_bus_message_open_container(reply, 'a', "(uso)");
501 if (r < 0)
ebcf1f97 502 return r;
cc377381
LP
503
504 HASHMAP_FOREACH(user, m->users, i) {
505 _cleanup_free_ char *p = NULL;
506
507 p = user_bus_path(user);
508 if (!p)
ebcf1f97 509 return -ENOMEM;
cc377381
LP
510
511 r = sd_bus_message_append(reply, "(uso)",
512 (uint32_t) user->uid,
513 user->name,
514 p);
515 if (r < 0)
ebcf1f97 516 return r;
cc377381
LP
517 }
518
519 r = sd_bus_message_close_container(reply);
520 if (r < 0)
ebcf1f97 521 return r;
cc377381 522
9030ca46 523 return sd_bus_send(NULL, reply, NULL);
cc377381
LP
524}
525
19070062 526static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
4afd3348 527 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
cc377381
LP
528 Manager *m = userdata;
529 Seat *seat;
530 Iterator i;
531 int r;
532
cc377381
LP
533 assert(message);
534 assert(m);
535
df2d202e 536 r = sd_bus_message_new_method_return(message, &reply);
cc377381 537 if (r < 0)
ebcf1f97 538 return r;
cc377381
LP
539
540 r = sd_bus_message_open_container(reply, 'a', "(so)");
541 if (r < 0)
ebcf1f97 542 return r;
cc377381
LP
543
544 HASHMAP_FOREACH(seat, m->seats, i) {
545 _cleanup_free_ char *p = NULL;
546
547 p = seat_bus_path(seat);
548 if (!p)
ebcf1f97 549 return -ENOMEM;
cc377381 550
b8358bce 551 r = sd_bus_message_append(reply, "(so)", seat->id, p);
cc377381 552 if (r < 0)
ebcf1f97 553 return r;
cc377381
LP
554 }
555
556 r = sd_bus_message_close_container(reply);
557 if (r < 0)
ebcf1f97 558 return r;
cc377381 559
9030ca46 560 return sd_bus_send(NULL, reply, NULL);
cc377381
LP
561}
562
19070062 563static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
4afd3348 564 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
cc377381
LP
565 Manager *m = userdata;
566 Inhibitor *inhibitor;
567 Iterator i;
568 int r;
569
19070062
LP
570 assert(message);
571 assert(m);
572
df2d202e 573 r = sd_bus_message_new_method_return(message, &reply);
cc377381 574 if (r < 0)
ebcf1f97 575 return r;
cc377381
LP
576
577 r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
578 if (r < 0)
ebcf1f97 579 return r;
cc377381
LP
580
581 HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
582
dbfa3fbb 583 r = sd_bus_message_append(reply, "(ssssuu)",
cc377381
LP
584 strempty(inhibit_what_to_string(inhibitor->what)),
585 strempty(inhibitor->who),
586 strempty(inhibitor->why),
587 strempty(inhibit_mode_to_string(inhibitor->mode)),
588 (uint32_t) inhibitor->uid,
589 (uint32_t) inhibitor->pid);
590 if (r < 0)
ebcf1f97 591 return r;
cc377381
LP
592 }
593
594 r = sd_bus_message_close_container(reply);
595 if (r < 0)
ebcf1f97 596 return r;
cc377381 597
9030ca46 598 return sd_bus_send(NULL, reply, NULL);
cc377381
LP
599}
600
19070062 601static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
a4cd87e9 602 const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
c7543606 603 _cleanup_free_ char *id = NULL;
cc377381 604 Session *session = NULL;
bc2b6332 605 uint32_t audit_id = 0;
cc377381
LP
606 Manager *m = userdata;
607 User *user = NULL;
608 Seat *seat = NULL;
06820eaf
LP
609 pid_t leader;
610 uid_t uid;
cc377381
LP
611 int remote;
612 uint32_t vtnr = 0;
613 SessionType t;
614 SessionClass c;
615 int r;
616
cc377381
LP
617 assert(message);
618 assert(m);
619
06820eaf
LP
620 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
621 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
622
a4cd87e9 623 r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
cc377381 624 if (r < 0)
ebcf1f97 625 return r;
cc377381 626
06820eaf
LP
627 if (!uid_is_valid(uid))
628 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID");
bc2b6332 629 if (leader < 0 || leader == 1 || leader == getpid_cached())
ebcf1f97 630 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
98a28fef 631
e2acb67b
LP
632 if (isempty(type))
633 t = _SESSION_TYPE_INVALID;
634 else {
635 t = session_type_from_string(type);
636 if (t < 0)
ebcf1f97 637 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
e2acb67b 638 }
98a28fef 639
55efac6c 640 if (isempty(class))
e2acb67b
LP
641 c = _SESSION_CLASS_INVALID;
642 else {
55efac6c 643 c = session_class_from_string(class);
e2acb67b 644 if (c < 0)
ebcf1f97 645 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
e2acb67b 646 }
55efac6c 647
a4cd87e9
LP
648 if (isempty(desktop))
649 desktop = NULL;
650 else {
651 if (!string_is_safe(desktop))
652 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
653 }
654
954449b8
LP
655 if (isempty(cseat))
656 seat = NULL;
98a28fef 657 else {
954449b8
LP
658 seat = hashmap_get(m->seats, cseat);
659 if (!seat)
d14ab08b 660 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
98a28fef
LP
661 }
662
98a28fef 663 if (tty_is_vc(tty)) {
4d6d6518 664 int v;
98a28fef 665
954449b8 666 if (!seat)
92432fcc
DH
667 seat = m->seat0;
668 else if (seat != m->seat0)
d14ab08b 669 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 670
4d6d6518 671 v = vtnr_from_tty(tty);
4d6d6518 672 if (v <= 0)
ebcf1f97 673 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
98a28fef 674
bc2b6332 675 if (vtnr == 0)
4d6d6518
LP
676 vtnr = (uint32_t) v;
677 else if (vtnr != (uint32_t) v)
ebcf1f97 678 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
cc377381 679
d1122ad5
LP
680 } else if (tty_is_console(tty)) {
681
954449b8 682 if (!seat)
92432fcc
DH
683 seat = m->seat0;
684 else if (seat != m->seat0)
ebcf1f97 685 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
d1122ad5
LP
686
687 if (vtnr != 0)
ebcf1f97 688 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
978cf3c7 689 }
98a28fef 690
954449b8 691 if (seat) {
bf7825ae 692 if (seat_has_vts(seat)) {
bc2b6332 693 if (vtnr <= 0 || vtnr > 63)
ebcf1f97 694 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
4d6d6518 695 } else {
d1122ad5 696 if (vtnr != 0)
ebcf1f97 697 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
4d6d6518
LP
698 }
699 }
700
e2acb67b
LP
701 if (t == _SESSION_TYPE_INVALID) {
702 if (!isempty(display))
703 t = SESSION_X11;
704 else if (!isempty(tty))
705 t = SESSION_TTY;
706 else
707 t = SESSION_UNSPECIFIED;
708 }
709
710 if (c == _SESSION_CLASS_INVALID) {
a4cd87e9 711 if (t == SESSION_UNSPECIFIED)
e2acb67b 712 c = SESSION_BACKGROUND;
a4cd87e9
LP
713 else
714 c = SESSION_USER;
e2acb67b
LP
715 }
716
06820eaf 717 if (leader == 0) {
4afd3348 718 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
5b12334d
LP
719
720 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
721 if (r < 0)
722 return r;
723
5b12334d 724 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
cc377381 725 if (r < 0)
ebcf1f97 726 return r;
9444b1f2
LP
727 }
728
bc2b6332
LP
729 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
730 * that avoids creating a logind session. */
731 r = manager_get_user_by_pid(m, leader, NULL);
e8a3144e
AJ
732 if (r < 0)
733 return r;
bc2b6332
LP
734 if (r > 0)
735 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session or user slice");
b80120c4
DH
736
737 /*
738 * Old gdm and lightdm start the user-session on the same VT as
739 * the greeter session. But they destroy the greeter session
740 * after the user-session and want the user-session to take
741 * over the VT. We need to support this for
742 * backwards-compatibility, so make sure we allow new sessions
cc85d562
DH
743 * on a VT that a greeter is running on. Furthermore, to allow
744 * re-logins, we have to allow a greeter to take over a used VT for
745 * the exact same reasons.
b80120c4 746 */
cc85d562
DH
747 if (c != SESSION_GREETER &&
748 vtnr > 0 &&
b80120c4
DH
749 vtnr < m->seat0->position_count &&
750 m->seat0->positions[vtnr] &&
751 m->seat0->positions[vtnr]->class != SESSION_GREETER)
752 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
21c390cc 753
183e0738
LP
754 if (hashmap_size(m->sessions) >= m->sessions_max)
755 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
756
3a87a86e
LP
757 (void) audit_session_from_pid(leader, &audit_id);
758 if (audit_session_is_valid(audit_id)) {
954449b8 759 /* Keep our session IDs and the audit session IDs in sync */
21c390cc 760
de0671ee 761 if (asprintf(&id, "%"PRIu32, audit_id) < 0)
ebcf1f97 762 return -ENOMEM;
21c390cc 763
954449b8
LP
764 /* Wut? There's already a session by this name and we
765 * didn't find it above? Weird, then let's not trust
766 * the audit data and let's better register a new
767 * ID */
768 if (hashmap_get(m->sessions, id)) {
bc2b6332 769 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
3a87a86e 770 audit_id = AUDIT_SESSION_INVALID;
97b11eed 771 id = mfree(id);
07714753 772 }
954449b8 773 }
07714753 774
954449b8 775 if (!id) {
07714753 776 do {
97b11eed 777 id = mfree(id);
07714753 778
cc377381 779 if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
ebcf1f97 780 return -ENOMEM;
07714753
LP
781
782 } while (hashmap_get(m->sessions, id));
98a28fef
LP
783 }
784
3d0ef5c7
LP
785 /* If we are not watching utmp aleady, try again */
786 manager_reconnect_utmp(m);
787
954449b8 788 r = manager_add_user_by_uid(m, uid, &user);
ebcf1f97 789 if (r < 0)
954449b8
LP
790 goto fail;
791
9444b1f2 792 r = manager_add_session(m, id, &session);
ebcf1f97 793 if (r < 0)
98a28fef
LP
794 goto fail;
795
9444b1f2 796 session_set_user(session, user);
fe3ab845
YW
797 r = session_set_leader(session, leader);
798 if (r < 0)
799 goto fail;
9444b1f2 800
98a28fef 801 session->type = t;
55efac6c 802 session->class = c;
98a28fef 803 session->remote = remote;
98a28fef
LP
804 session->vtnr = vtnr;
805
98a28fef
LP
806 if (!isempty(tty)) {
807 session->tty = strdup(tty);
808 if (!session->tty) {
ebcf1f97 809 r = -ENOMEM;
98a28fef
LP
810 goto fail;
811 }
3d0ef5c7
LP
812
813 session->tty_validity = TTY_FROM_PAM;
98a28fef
LP
814 }
815
816 if (!isempty(display)) {
817 session->display = strdup(display);
818 if (!session->display) {
ebcf1f97 819 r = -ENOMEM;
98a28fef
LP
820 goto fail;
821 }
822 }
823
824 if (!isempty(remote_user)) {
825 session->remote_user = strdup(remote_user);
826 if (!session->remote_user) {
ebcf1f97 827 r = -ENOMEM;
98a28fef
LP
828 goto fail;
829 }
830 }
831
832 if (!isempty(remote_host)) {
833 session->remote_host = strdup(remote_host);
834 if (!session->remote_host) {
ebcf1f97 835 r = -ENOMEM;
98a28fef
LP
836 goto fail;
837 }
838 }
839
840 if (!isempty(service)) {
841 session->service = strdup(service);
842 if (!session->service) {
ebcf1f97 843 r = -ENOMEM;
98a28fef
LP
844 goto fail;
845 }
846 }
847
a4cd87e9
LP
848 if (!isempty(desktop)) {
849 session->desktop = strdup(desktop);
850 if (!session->desktop) {
851 r = -ENOMEM;
852 goto fail;
853 }
854 }
855
954449b8
LP
856 if (seat) {
857 r = seat_attach_session(seat, session);
ebcf1f97 858 if (r < 0)
98a28fef
LP
859 goto fail;
860 }
861
22f93314
JS
862 r = sd_bus_message_enter_container(message, 'a', "(sv)");
863 if (r < 0)
d88ffeee 864 goto fail;
22f93314 865
25a1ab4e 866 r = session_start(session, message, error);
22f93314
JS
867 if (r < 0)
868 goto fail;
869
870 r = sd_bus_message_exit_container(message);
ebcf1f97 871 if (r < 0)
98a28fef
LP
872 goto fail;
873
cc377381 874 session->create_message = sd_bus_message_ref(message);
98a28fef 875
bc2b6332 876 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
f7340ab2 877 * session_send_create_reply(). */
cba38758 878
cc377381 879 return 1;
98a28fef
LP
880
881fail:
98a28fef
LP
882 if (session)
883 session_add_to_gc_queue(session);
884
885 if (user)
886 user_add_to_gc_queue(user);
887
98a28fef
LP
888 return r;
889}
890
19070062 891static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
892 Manager *m = userdata;
893 Session *session;
894 const char *name;
895 int r;
314b4b0a 896
cc377381
LP
897 assert(message);
898 assert(m);
899
900 r = sd_bus_message_read(message, "s", &name);
901 if (r < 0)
ebcf1f97 902 return r;
cc377381 903
309a29df
LP
904 r = manager_get_session_from_creds(m, message, name, error, &session);
905 if (r < 0)
906 return r;
cc377381 907
ad8780c9
ZJS
908 r = session_release(session);
909 if (r < 0)
910 return r;
cc377381 911
df2d202e 912 return sd_bus_reply_method_return(message, NULL);
cc377381
LP
913}
914
19070062 915static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
916 Manager *m = userdata;
917 Session *session;
918 const char *name;
919 int r;
f8e2fb7b 920
cc377381 921 assert(message);
f8e2fb7b 922 assert(m);
cc377381
LP
923
924 r = sd_bus_message_read(message, "s", &name);
925 if (r < 0)
ebcf1f97 926 return r;
cc377381 927
309a29df
LP
928 r = manager_get_session_from_creds(m, message, name, error, &session);
929 if (r < 0)
930 return r;
cc377381 931
19070062 932 return bus_session_method_activate(message, session, error);
cc377381
LP
933}
934
19070062 935static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
936 const char *session_name, *seat_name;
937 Manager *m = userdata;
938 Session *session;
939 Seat *seat;
940 int r;
941
f8e2fb7b 942 assert(message);
cc377381 943 assert(m);
f8e2fb7b 944
cc377381
LP
945 /* Same as ActivateSession() but refuses to work if
946 * the seat doesn't match */
f8e2fb7b 947
cc377381
LP
948 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
949 if (r < 0)
ebcf1f97 950 return r;
eecd1362 951
309a29df
LP
952 r = manager_get_session_from_creds(m, message, session_name, error, &session);
953 if (r < 0)
954 return r;
beaafb2e 955
309a29df
LP
956 r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
957 if (r < 0)
958 return r;
314b4b0a 959
cc377381 960 if (session->seat != seat)
ebcf1f97 961 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
cc377381
LP
962
963 r = session_activate(session);
f8e2fb7b 964 if (r < 0)
ebcf1f97 965 return r;
f8e2fb7b 966
df2d202e 967 return sd_bus_reply_method_return(message, NULL);
cc377381 968}
f8e2fb7b 969
19070062 970static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
971 Manager *m = userdata;
972 Session *session;
973 const char *name;
974 int r;
f8e2fb7b 975
cc377381
LP
976 assert(message);
977 assert(m);
f8e2fb7b 978
cc377381
LP
979 r = sd_bus_message_read(message, "s", &name);
980 if (r < 0)
ebcf1f97 981 return r;
f8e2fb7b 982
309a29df
LP
983 r = manager_get_session_from_creds(m, message, name, error, &session);
984 if (r < 0)
985 return r;
f8e2fb7b 986
19070062 987 return bus_session_method_lock(message, session, error);
cc377381 988}
f8e2fb7b 989
19070062 990static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
991 Manager *m = userdata;
992 int r;
f8e2fb7b 993
cc377381
LP
994 assert(message);
995 assert(m);
f8e2fb7b 996
c529695e
LP
997 r = bus_verify_polkit_async(
998 message,
999 CAP_SYS_ADMIN,
1000 "org.freedesktop.login1.lock-sessions",
403ed0e5 1001 NULL,
c529695e
LP
1002 false,
1003 UID_INVALID,
1004 &m->polkit_registry,
1005 error);
1006 if (r < 0)
1007 return r;
1008 if (r == 0)
1009 return 1; /* Will call us back */
1010
cc377381
LP
1011 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
1012 if (r < 0)
ebcf1f97 1013 return r;
f8e2fb7b 1014
df2d202e 1015 return sd_bus_reply_method_return(message, NULL);
cc377381
LP
1016}
1017
19070062 1018static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c529695e 1019 const char *name;
cc377381
LP
1020 Manager *m = userdata;
1021 Session *session;
cc377381
LP
1022 int r;
1023
cc377381
LP
1024 assert(message);
1025 assert(m);
1026
c529695e 1027 r = sd_bus_message_read(message, "s", &name);
cc377381 1028 if (r < 0)
ebcf1f97 1029 return r;
cc377381 1030
309a29df
LP
1031 r = manager_get_session_from_creds(m, message, name, error, &session);
1032 if (r < 0)
1033 return r;
f8e2fb7b 1034
19070062 1035 return bus_session_method_kill(message, session, error);
cc377381 1036}
f8e2fb7b 1037
19070062 1038static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
1039 Manager *m = userdata;
1040 uint32_t uid;
cc377381
LP
1041 User *user;
1042 int r;
f8e2fb7b 1043
cc377381
LP
1044 assert(message);
1045 assert(m);
1046
c529695e 1047 r = sd_bus_message_read(message, "u", &uid);
cc377381 1048 if (r < 0)
ebcf1f97 1049 return r;
cc377381 1050
309a29df
LP
1051 r = manager_get_user_from_creds(m, message, uid, error, &user);
1052 if (r < 0)
1053 return r;
cc377381 1054
19070062 1055 return bus_user_method_kill(message, user, error);
cc377381
LP
1056}
1057
19070062 1058static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
1059 Manager *m = userdata;
1060 const char *name;
1061 Session *session;
1062 int r;
1063
cc377381
LP
1064 assert(message);
1065 assert(m);
1066
1067 r = sd_bus_message_read(message, "s", &name);
1068 if (r < 0)
ebcf1f97 1069 return r;
cc377381 1070
309a29df
LP
1071 r = manager_get_session_from_creds(m, message, name, error, &session);
1072 if (r < 0)
1073 return r;
cc377381 1074
19070062 1075 return bus_session_method_terminate(message, session, error);
cc377381
LP
1076}
1077
19070062 1078static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
1079 Manager *m = userdata;
1080 uint32_t uid;
1081 User *user;
1082 int r;
1083
cc377381
LP
1084 assert(message);
1085 assert(m);
1086
1087 r = sd_bus_message_read(message, "u", &uid);
1088 if (r < 0)
ebcf1f97 1089 return r;
cc377381 1090
309a29df
LP
1091 r = manager_get_user_from_creds(m, message, uid, error, &user);
1092 if (r < 0)
1093 return r;
cc377381 1094
19070062 1095 return bus_user_method_terminate(message, user, error);
cc377381
LP
1096}
1097
19070062 1098static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
1099 Manager *m = userdata;
1100 const char *name;
1101 Seat *seat;
1102 int r;
1103
cc377381
LP
1104 assert(message);
1105 assert(m);
1106
1107 r = sd_bus_message_read(message, "s", &name);
1108 if (r < 0)
ebcf1f97 1109 return r;
cc377381 1110
309a29df
LP
1111 r = manager_get_seat_from_creds(m, message, name, error, &seat);
1112 if (r < 0)
1113 return r;
cc377381 1114
19070062 1115 return bus_seat_method_terminate(message, seat, error);
cc377381
LP
1116}
1117
19070062 1118static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
34160d91 1119 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
cc377381
LP
1120 _cleanup_free_ char *cc = NULL;
1121 Manager *m = userdata;
152199f2 1122 int r, b, interactive;
cc377381
LP
1123 struct passwd *pw;
1124 const char *path;
34160d91 1125 uint32_t uid, auth_uid;
cc377381 1126
cc377381
LP
1127 assert(message);
1128 assert(m);
1129
1130 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1131 if (r < 0)
ebcf1f97 1132 return r;
cc377381 1133
34160d91
AJ
1134 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID |
1135 SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1136 if (r < 0)
1137 return r;
309a29df 1138
34160d91
AJ
1139 if (!uid_is_valid(uid)) {
1140 /* Note that we get the owner UID of the session or user unit,
1141 * not the actual client UID here! */
309a29df
LP
1142 r = sd_bus_creds_get_owner_uid(creds, &uid);
1143 if (r < 0)
1144 return r;
34160d91 1145 }
06820eaf 1146
34160d91
AJ
1147 /* owner_uid is racy, so for authorization we must use euid */
1148 r = sd_bus_creds_get_euid(creds, &auth_uid);
1149 if (r < 0)
1150 return r;
309a29df 1151
cc377381
LP
1152 errno = 0;
1153 pw = getpwuid(uid);
1154 if (!pw)
f5e5c28f 1155 return errno > 0 ? -errno : -ENOENT;
cc377381 1156
f3885791
LP
1157 r = bus_verify_polkit_async(
1158 message,
1159 CAP_SYS_ADMIN,
34160d91
AJ
1160 uid == auth_uid ? "org.freedesktop.login1.set-self-linger" :
1161 "org.freedesktop.login1.set-user-linger",
403ed0e5 1162 NULL,
f3885791 1163 interactive,
c529695e 1164 UID_INVALID,
f3885791
LP
1165 &m->polkit_registry,
1166 error);
cc377381 1167 if (r < 0)
ebcf1f97 1168 return r;
cc377381
LP
1169 if (r == 0)
1170 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1171
1172 mkdir_p_label("/var/lib/systemd", 0755);
1173
37c1d5e9 1174 r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE);
cc377381 1175 if (r < 0)
ebcf1f97 1176 return r;
cc377381
LP
1177
1178 cc = cescape(pw->pw_name);
1179 if (!cc)
ebcf1f97 1180 return -ENOMEM;
cc377381 1181
63c372cb 1182 path = strjoina("/var/lib/systemd/linger/", cc);
cc377381
LP
1183 if (b) {
1184 User *u;
1185
1186 r = touch(path);
1187 if (r < 0)
ebcf1f97 1188 return r;
cc377381
LP
1189
1190 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1191 user_start(u);
1192
1193 } else {
1194 User *u;
1195
1196 r = unlink(path);
1197 if (r < 0 && errno != ENOENT)
ebcf1f97 1198 return -errno;
cc377381 1199
8cb4ab00 1200 u = hashmap_get(m->users, UID_TO_PTR(uid));
cc377381
LP
1201 if (u)
1202 user_add_to_gc_queue(u);
1203 }
1204
df2d202e 1205 return sd_bus_reply_method_return(message, NULL);
f8e2fb7b
LP
1206}
1207
4f209af7
YW
1208static int trigger_device(Manager *m, sd_device *d) {
1209 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
b668e064
LP
1210 int r;
1211
1212 assert(m);
1213
4f209af7
YW
1214 r = sd_device_enumerator_new(&e);
1215 if (r < 0)
1216 return r;
1217
1218 r = sd_device_enumerator_allow_uninitialized(e);
1219 if (r < 0)
1220 return r;
b668e064 1221
2eb916cd 1222 if (d) {
4f209af7 1223 r = sd_device_enumerator_add_match_parent(e, d);
06acf2d4
LP
1224 if (r < 0)
1225 return r;
2eb916cd
LP
1226 }
1227
8437c059 1228 FOREACH_DEVICE(e, d) {
cc377381 1229 _cleanup_free_ char *t = NULL;
b668e064
LP
1230 const char *p;
1231
4f209af7
YW
1232 r = sd_device_get_syspath(d, &p);
1233 if (r < 0)
1234 return r;
b668e064 1235
b668e064 1236 t = strappend(p, "/uevent");
06acf2d4
LP
1237 if (!t)
1238 return -ENOMEM;
b668e064 1239
57512c89 1240 (void) write_string_file(t, "change", WRITE_STRING_FILE_DISABLE_BUFFER);
b668e064
LP
1241 }
1242
06acf2d4 1243 return 0;
b668e064
LP
1244}
1245
47a26690 1246static int attach_device(Manager *m, const char *seat, const char *sysfs) {
4f209af7 1247 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
7fd1b19b 1248 _cleanup_free_ char *rule = NULL, *file = NULL;
c28fa3d3 1249 const char *id_for_seat;
47a26690
LP
1250 int r;
1251
1252 assert(m);
1253 assert(seat);
1254 assert(sysfs);
1255
4f209af7
YW
1256 r = sd_device_new_from_syspath(&d, sysfs);
1257 if (r < 0)
1258 return r;
47a26690 1259
4f209af7 1260 if (sd_device_has_tag(d, "seat") <= 0)
06acf2d4 1261 return -ENODEV;
47a26690 1262
4f209af7 1263 if (sd_device_get_property_value(d, "ID_FOR_SEAT", &id_for_seat) < 0)
06acf2d4 1264 return -ENODEV;
47a26690 1265
06acf2d4
LP
1266 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1267 return -ENOMEM;
47a26690 1268
06acf2d4
LP
1269 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1270 return -ENOMEM;
47a26690 1271
4f209af7 1272 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
574d5f2d 1273 r = write_string_file_atomic_label(file, rule);
a0a0c7f1 1274 if (r < 0)
06acf2d4 1275 return r;
47a26690 1276
06acf2d4 1277 return trigger_device(m, d);
47a26690
LP
1278}
1279
b668e064 1280static int flush_devices(Manager *m) {
7fd1b19b 1281 _cleanup_closedir_ DIR *d;
b668e064
LP
1282
1283 assert(m);
1284
1285 d = opendir("/etc/udev/rules.d");
1286 if (!d) {
1287 if (errno != ENOENT)
56f64d95 1288 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
b668e064
LP
1289 } else {
1290 struct dirent *de;
1291
8fb3f009 1292 FOREACH_DIRENT_ALL(de, d, break) {
b668e064
LP
1293 if (!dirent_is_file(de))
1294 continue;
1295
1296 if (!startswith(de->d_name, "72-seat-"))
1297 continue;
1298
1299 if (!endswith(de->d_name, ".rules"))
1300 continue;
1301
1302 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
56f64d95 1303 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
b668e064 1304 }
b668e064
LP
1305 }
1306
1307 return trigger_device(m, NULL);
1308}
1309
19070062 1310static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
1311 const char *sysfs, *seat;
1312 Manager *m = userdata;
1313 int interactive, r;
1314
cc377381
LP
1315 assert(message);
1316 assert(m);
1317
1318 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1319 if (r < 0)
ebcf1f97 1320 return r;
cc377381
LP
1321
1322 if (!path_startswith(sysfs, "/sys"))
ebcf1f97 1323 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
cc377381
LP
1324
1325 if (!seat_name_is_valid(seat))
ebcf1f97 1326 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
cc377381 1327
f3885791
LP
1328 r = bus_verify_polkit_async(
1329 message,
1330 CAP_SYS_ADMIN,
1331 "org.freedesktop.login1.attach-device",
403ed0e5 1332 NULL,
f3885791 1333 interactive,
c529695e 1334 UID_INVALID,
f3885791
LP
1335 &m->polkit_registry,
1336 error);
cc377381 1337 if (r < 0)
ebcf1f97 1338 return r;
cc377381
LP
1339 if (r == 0)
1340 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1341
1342 r = attach_device(m, seat, sysfs);
1343 if (r < 0)
ebcf1f97 1344 return r;
cc377381 1345
df2d202e 1346 return sd_bus_reply_method_return(message, NULL);
cc377381
LP
1347}
1348
19070062 1349static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
1350 Manager *m = userdata;
1351 int interactive, r;
1352
cc377381
LP
1353 assert(message);
1354 assert(m);
1355
1356 r = sd_bus_message_read(message, "b", &interactive);
1357 if (r < 0)
ebcf1f97 1358 return r;
cc377381 1359
f3885791
LP
1360 r = bus_verify_polkit_async(
1361 message,
1362 CAP_SYS_ADMIN,
1363 "org.freedesktop.login1.flush-devices",
403ed0e5 1364 NULL,
f3885791 1365 interactive,
c529695e 1366 UID_INVALID,
f3885791
LP
1367 &m->polkit_registry,
1368 error);
cc377381 1369 if (r < 0)
ebcf1f97 1370 return r;
cc377381
LP
1371 if (r == 0)
1372 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1373
1374 r = flush_devices(m);
1375 if (r < 0)
ebcf1f97 1376 return r;
cc377381 1377
df2d202e 1378 return sd_bus_reply_method_return(message, NULL);
cc377381
LP
1379}
1380
89f13440 1381static int have_multiple_sessions(
89f13440 1382 Manager *m,
409133be 1383 uid_t uid) {
89f13440 1384
2154761f
MS
1385 Session *session;
1386 Iterator i;
89f13440
LP
1387
1388 assert(m);
1389
1ca04b87
LP
1390 /* Check for other users' sessions. Greeter sessions do not
1391 * count, and non-login sessions do not count either. */
2154761f 1392 HASHMAP_FOREACH(session, m->sessions, i)
1ca04b87 1393 if (session->class == SESSION_USER &&
1ca04b87 1394 session->user->uid != uid)
2154761f 1395 return true;
89f13440
LP
1396
1397 return false;
1398}
1399
314b4b0a
LP
1400static int bus_manager_log_shutdown(
1401 Manager *m,
314b4b0a
LP
1402 const char *unit_name) {
1403
5744f59a 1404 const char *p, *q;
314b4b0a
LP
1405
1406 assert(m);
1407 assert(unit_name);
1408
314b4b0a 1409 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
f868cb58 1410 p = "MESSAGE=System is powering down";
314b4b0a 1411 q = "SHUTDOWN=power-off";
314b4b0a 1412 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
f868cb58 1413 p = "MESSAGE=System is rebooting";
314b4b0a 1414 q = "SHUTDOWN=reboot";
36b69c31
LP
1415 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1416 p = "MESSAGE=System is halting";
1417 q = "SHUTDOWN=halt";
314b4b0a 1418 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
f868cb58 1419 p = "MESSAGE=System is rebooting with kexec";
314b4b0a
LP
1420 q = "SHUTDOWN=kexec";
1421 } else {
f868cb58 1422 p = "MESSAGE=System is shutting down";
314b4b0a
LP
1423 q = NULL;
1424 }
1425
f868cb58
ZJS
1426 if (isempty(m->wall_message))
1427 p = strjoina(p, ".");
1428 else
1429 p = strjoina(p, " (", m->wall_message, ").");
9ef15026 1430
e2cc6eca 1431 return log_struct(LOG_NOTICE,
2b044526 1432 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
314b4b0a 1433 p,
a1230ff9 1434 q);
314b4b0a
LP
1435}
1436
b5d3e168
KS
1437static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1438 Manager *m = userdata;
1439
1440 assert(e);
1441 assert(m);
1442
1443 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1444 return 0;
1445}
1446
1447int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1448 int r;
1449
1450 assert(m);
1451
1452 if (until <= now(CLOCK_MONOTONIC))
1453 return 0;
1454
1455 /* We want to ignore the lid switch for a while after each
1456 * suspend, and after boot-up. Hence let's install a timer for
1457 * this. As long as the event source exists we ignore the lid
1458 * switch. */
1459
1460 if (m->lid_switch_ignore_event_source) {
1461 usec_t u;
1462
1463 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1464 if (r < 0)
1465 return r;
1466
1467 if (until <= u)
1468 return 0;
1469
1470 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1471 } else
6a0f1f6d
LP
1472 r = sd_event_add_time(
1473 m->event,
1474 &m->lid_switch_ignore_event_source,
1475 CLOCK_MONOTONIC,
1476 until, 0,
1477 lid_switch_ignore_handler, m);
b5d3e168
KS
1478
1479 return r;
1480}
1481
6d7f7fd4 1482static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
6d7f7fd4
AJ
1483 int active = _active;
1484
1485 assert(m);
06c2f0a8 1486 assert(IN_SET(w, INHIBIT_SHUTDOWN, INHIBIT_SLEEP));
6d7f7fd4
AJ
1487
1488 return sd_bus_emit_signal(m->bus,
1489 "/org/freedesktop/login1",
1490 "org.freedesktop.login1.Manager",
06c2f0a8 1491 w == INHIBIT_SHUTDOWN ? "PrepareForShutdown" : "PrepareForSleep",
6d7f7fd4
AJ
1492 "b",
1493 active);
1494}
1495
314b4b0a
LP
1496static int execute_shutdown_or_sleep(
1497 Manager *m,
1498 InhibitWhat w,
1499 const char *unit_name,
cc377381 1500 sd_bus_error *error) {
314b4b0a 1501
4afd3348 1502 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
cc377381 1503 const char *p;
cc377381 1504 int r;
eecd1362 1505
af9792ac 1506 assert(m);
b61fa4e0 1507 assert(w > 0);
314b4b0a 1508 assert(w < _INHIBIT_WHAT_MAX);
d889a206 1509 assert(unit_name);
eecd1362 1510
df75a1a8
AJ
1511 if (w == INHIBIT_SHUTDOWN)
1512 bus_manager_log_shutdown(m, unit_name);
314b4b0a 1513
df75a1a8
AJ
1514 r = sd_bus_call_method(
1515 m->bus,
1516 "org.freedesktop.systemd1",
1517 "/org/freedesktop/systemd1",
1518 "org.freedesktop.systemd1.Manager",
1519 "StartUnit",
1520 error,
1521 &reply,
1522 "ss", unit_name, "replace-irreversibly");
1523 if (r < 0)
6d7f7fd4 1524 goto error;
af9792ac 1525
df75a1a8
AJ
1526 r = sd_bus_message_read(reply, "o", &p);
1527 if (r < 0)
6d7f7fd4 1528 goto error;
af9792ac 1529
6b9f8b71
YW
1530 r = free_and_strdup(&m->action_job, p);
1531 if (r < 0)
6d7f7fd4 1532 goto error;
af9792ac 1533
314b4b0a 1534 m->action_unit = unit_name;
314b4b0a 1535 m->action_what = w;
af9792ac 1536
f9cd6be1 1537 /* Make sure the lid switch is ignored for a while */
9d10cbee 1538 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
f9cd6be1 1539
af9792ac 1540 return 0;
6d7f7fd4
AJ
1541
1542error:
1543 /* Tell people that they now may take a lock again */
401e33ed 1544 (void) send_prepare_for(m, w, false);
6d7f7fd4
AJ
1545
1546 return r;
eecd1362
LP
1547}
1548
418b22b8 1549int manager_dispatch_delayed(Manager *manager, bool timeout) {
c0f32805 1550
4afd3348 1551 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
c0f32805 1552 Inhibitor *offending = NULL;
c0f32805
DM
1553 int r;
1554
1555 assert(manager);
c0f32805
DM
1556
1557 if (manager->action_what == 0 || manager->action_job)
1558 return 0;
1559
1560 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1561 _cleanup_free_ char *comm = NULL, *u = NULL;
1562
418b22b8
DM
1563 if (!timeout)
1564 return 0;
1565
c0f32805
DM
1566 (void) get_process_comm(offending->pid, &comm);
1567 u = uid_to_name(offending->uid);
1568
1569 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1570 offending->uid, strna(u),
1571 offending->pid, strna(comm));
1572 }
1573
1574 /* Actually do the operation */
1575 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1576 if (r < 0) {
6d7f7fd4
AJ
1577 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1578 bus_error_message(&error, r));
c0f32805
DM
1579
1580 manager->action_unit = NULL;
1581 manager->action_what = 0;
418b22b8 1582 return r;
c0f32805
DM
1583 }
1584
418b22b8
DM
1585 return 1;
1586}
1587
1588static int manager_inhibit_timeout_handler(
1589 sd_event_source *s,
1590 uint64_t usec,
1591 void *userdata) {
1592
1593 Manager *manager = userdata;
1594 int r;
1595
1596 assert(manager);
1597 assert(manager->inhibit_timeout_source == s);
1598
1599 r = manager_dispatch_delayed(manager, true);
1600 return (r < 0) ? r : 0;
c0f32805
DM
1601}
1602
314b4b0a
LP
1603static int delay_shutdown_or_sleep(
1604 Manager *m,
1605 InhibitWhat w,
1606 const char *unit_name) {
eecd1362 1607
c0f32805
DM
1608 int r;
1609 usec_t timeout_val;
1610
eecd1362 1611 assert(m);
d889a206
LP
1612 assert(w >= 0);
1613 assert(w < _INHIBIT_WHAT_MAX);
314b4b0a 1614 assert(unit_name);
eecd1362 1615
c0f32805
DM
1616 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1617
1618 if (m->inhibit_timeout_source) {
1619 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1620 if (r < 0)
c2a23db0 1621 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
c0f32805
DM
1622
1623 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1624 if (r < 0)
c2a23db0 1625 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
c0f32805
DM
1626 } else {
1627 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1628 timeout_val, 0, manager_inhibit_timeout_handler, m);
1629 if (r < 0)
1630 return r;
1631 }
1632
314b4b0a
LP
1633 m->action_unit = unit_name;
1634 m->action_what = w;
d889a206
LP
1635
1636 return 0;
1637}
1638
069cfc85
LP
1639int bus_manager_shutdown_or_sleep_now_or_later(
1640 Manager *m,
1641 const char *unit_name,
1642 InhibitWhat w,
cc377381 1643 sd_bus_error *error) {
069cfc85 1644
c8c8ee85 1645 _cleanup_free_ char *load_state = NULL;
069cfc85
LP
1646 bool delayed;
1647 int r;
1648
1649 assert(m);
1650 assert(unit_name);
b61fa4e0 1651 assert(w > 0);
543680f4 1652 assert(w < _INHIBIT_WHAT_MAX);
af9792ac 1653 assert(!m->action_job);
069cfc85 1654
c8c8ee85
ZJS
1655 r = unit_load_state(m->bus, unit_name, &load_state);
1656 if (r < 0)
1657 return r;
1658
baaa35ad
ZJS
1659 if (!streq(load_state, "loaded"))
1660 return log_notice_errno(SYNTHETIC_ERRNO(EACCES),
1661 "Unit %s is %s, refusing operation.",
1662 unit_name, load_state);
c8c8ee85 1663
314b4b0a 1664 /* Tell everybody to prepare for shutdown/sleep */
401e33ed 1665 (void) send_prepare_for(m, w, true);
314b4b0a 1666
069cfc85
LP
1667 delayed =
1668 m->inhibit_delay_max > 0 &&
85a428c6 1669 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
069cfc85
LP
1670
1671 if (delayed)
1672 /* Shutdown is delayed, keep in mind what we
1673 * want to do, and start a timeout */
1674 r = delay_shutdown_or_sleep(m, w, unit_name);
314b4b0a 1675 else
069cfc85
LP
1676 /* Shutdown is not delayed, execute it
1677 * immediately */
314b4b0a 1678 r = execute_shutdown_or_sleep(m, w, unit_name, error);
069cfc85
LP
1679
1680 return r;
1681}
1682
b7aa9589 1683static int verify_shutdown_creds(
d889a206 1684 Manager *m,
cc377381 1685 sd_bus_message *message,
d889a206 1686 InhibitWhat w,
b7aa9589 1687 bool interactive,
d889a206
LP
1688 const char *action,
1689 const char *action_multiple_sessions,
1690 const char *action_ignore_inhibit,
ebcf1f97 1691 sd_bus_error *error) {
d889a206 1692
4afd3348 1693 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
069cfc85 1694 bool multiple_sessions, blocked;
cc377381 1695 uid_t uid;
b7aa9589 1696 int r;
d889a206
LP
1697
1698 assert(m);
d889a206 1699 assert(message);
d889a206
LP
1700 assert(w >= 0);
1701 assert(w <= _INHIBIT_WHAT_MAX);
6524990f 1702
05bae4a6 1703 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
5b12334d
LP
1704 if (r < 0)
1705 return r;
1706
05bae4a6 1707 r = sd_bus_creds_get_euid(creds, &uid);
cc377381 1708 if (r < 0)
ebcf1f97 1709 return r;
409133be 1710
cc377381 1711 r = have_multiple_sessions(m, uid);
d889a206 1712 if (r < 0)
ebcf1f97 1713 return r;
d889a206
LP
1714
1715 multiple_sessions = r > 0;
85a428c6 1716 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
d889a206 1717
b7aa9589 1718 if (multiple_sessions && action_multiple_sessions) {
403ed0e5 1719 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
d889a206 1720 if (r < 0)
ebcf1f97 1721 return r;
055d4066
ZJS
1722 if (r == 0)
1723 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
d889a206
LP
1724 }
1725
b7aa9589 1726 if (blocked && action_ignore_inhibit) {
403ed0e5 1727 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
d889a206 1728 if (r < 0)
ebcf1f97 1729 return r;
055d4066
ZJS
1730 if (r == 0)
1731 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
d889a206
LP
1732 }
1733
b7aa9589 1734 if (!multiple_sessions && !blocked && action) {
403ed0e5 1735 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
d889a206 1736 if (r < 0)
ebcf1f97 1737 return r;
055d4066
ZJS
1738 if (r == 0)
1739 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
d889a206
LP
1740 }
1741
b7aa9589
DM
1742 return 0;
1743}
1744
1745static int method_do_shutdown_or_sleep(
1746 Manager *m,
1747 sd_bus_message *message,
1748 const char *unit_name,
1749 InhibitWhat w,
1750 const char *action,
1751 const char *action_multiple_sessions,
1752 const char *action_ignore_inhibit,
1753 const char *sleep_verb,
1754 sd_bus_error *error) {
1755
1756 int interactive, r;
1757
1758 assert(m);
1759 assert(message);
1760 assert(unit_name);
1761 assert(w >= 0);
1762 assert(w <= _INHIBIT_WHAT_MAX);
1763
1764 r = sd_bus_message_read(message, "b", &interactive);
1765 if (r < 0)
1766 return r;
1767
1768 /* Don't allow multiple jobs being executed at the same time */
f8bfa318 1769 if (m->action_what > 0)
b7aa9589
DM
1770 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1771
1772 if (sleep_verb) {
1773 r = can_sleep(sleep_verb);
b71c9758 1774 if (r == -ENOSPC)
8340b762
ZJS
1775 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1776 "Not enough swap space for hibernation");
b71c9758 1777 if (r == 0)
8340b762
ZJS
1778 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1779 "Sleep verb \"%s\" not supported", sleep_verb);
b7aa9589
DM
1780 if (r < 0)
1781 return r;
b7aa9589
DM
1782 }
1783
1784 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1785 action_ignore_inhibit, error);
1786 if (r != 0)
1787 return r;
1788
ebcf1f97 1789 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
d889a206 1790 if (r < 0)
ebcf1f97 1791 return r;
d889a206 1792
df2d202e 1793 return sd_bus_reply_method_return(message, NULL);
eecd1362
LP
1794}
1795
19070062 1796static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3f49d45a
LP
1797 Manager *m = userdata;
1798
cc377381
LP
1799 return method_do_shutdown_or_sleep(
1800 m, message,
1801 SPECIAL_POWEROFF_TARGET,
1802 INHIBIT_SHUTDOWN,
1803 "org.freedesktop.login1.power-off",
1804 "org.freedesktop.login1.power-off-multiple-sessions",
1805 "org.freedesktop.login1.power-off-ignore-inhibit",
1806 NULL,
ebcf1f97 1807 error);
cc377381 1808}
88e3dc90 1809
19070062 1810static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381 1811 Manager *m = userdata;
88e3dc90 1812
cc377381
LP
1813 return method_do_shutdown_or_sleep(
1814 m, message,
1815 SPECIAL_REBOOT_TARGET,
1816 INHIBIT_SHUTDOWN,
1817 "org.freedesktop.login1.reboot",
1818 "org.freedesktop.login1.reboot-multiple-sessions",
1819 "org.freedesktop.login1.reboot-ignore-inhibit",
1820 NULL,
ebcf1f97 1821 error);
cc377381 1822}
88e3dc90 1823
36b69c31
LP
1824static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1825 Manager *m = userdata;
1826
1827 return method_do_shutdown_or_sleep(
1828 m, message,
1829 SPECIAL_HALT_TARGET,
1830 INHIBIT_SHUTDOWN,
1831 "org.freedesktop.login1.halt",
1832 "org.freedesktop.login1.halt-multiple-sessions",
1833 "org.freedesktop.login1.halt-ignore-inhibit",
1834 NULL,
1835 error);
1836}
1837
19070062 1838static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381 1839 Manager *m = userdata;
88e3dc90 1840
cc377381
LP
1841 return method_do_shutdown_or_sleep(
1842 m, message,
1843 SPECIAL_SUSPEND_TARGET,
1844 INHIBIT_SLEEP,
1845 "org.freedesktop.login1.suspend",
1846 "org.freedesktop.login1.suspend-multiple-sessions",
1847 "org.freedesktop.login1.suspend-ignore-inhibit",
1848 "suspend",
ebcf1f97 1849 error);
cc377381 1850}
88e3dc90 1851
15e99a43
LP
1852static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1853 Manager *m = userdata;
1854
1855 return method_do_shutdown_or_sleep(
1856 m, message,
1857 SPECIAL_HIBERNATE_TARGET,
1858 INHIBIT_SLEEP,
1859 "org.freedesktop.login1.hibernate",
1860 "org.freedesktop.login1.hibernate-multiple-sessions",
1861 "org.freedesktop.login1.hibernate-ignore-inhibit",
1862 "hibernate",
1863 error);
1864}
1865
1866static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1867 Manager *m = userdata;
1868
1869 return method_do_shutdown_or_sleep(
1870 m, message,
1871 SPECIAL_HYBRID_SLEEP_TARGET,
1872 INHIBIT_SLEEP,
1873 "org.freedesktop.login1.hibernate",
1874 "org.freedesktop.login1.hibernate-multiple-sessions",
1875 "org.freedesktop.login1.hibernate-ignore-inhibit",
1876 "hybrid-sleep",
1877 error);
1878}
1879
e68c79db 1880static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c58493c0
ML
1881 Manager *m = userdata;
1882
1883 return method_do_shutdown_or_sleep(
1884 m, message,
e68c79db 1885 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
c58493c0
ML
1886 INHIBIT_SLEEP,
1887 "org.freedesktop.login1.hibernate",
1888 "org.freedesktop.login1.hibernate-multiple-sessions",
1889 "org.freedesktop.login1.hibernate-ignore-inhibit",
1890 "hybrid-sleep",
1891 error);
1892}
1893
867c37f6
DM
1894static int nologin_timeout_handler(
1895 sd_event_source *s,
1896 uint64_t usec,
1897 void *userdata) {
1898
1899 Manager *m = userdata;
867c37f6
DM
1900
1901 log_info("Creating /run/nologin, blocking further logins...");
1902
6e11e7e6
LP
1903 m->unlink_nologin =
1904 create_shutdown_run_nologin_or_warn() >= 0;
867c37f6
DM
1905
1906 return 0;
1907}
1908
1909static int update_schedule_file(Manager *m) {
91b3e7fb 1910 _cleanup_free_ char *temp_path = NULL;
867c37f6 1911 _cleanup_fclose_ FILE *f = NULL;
91b3e7fb 1912 int r;
867c37f6
DM
1913
1914 assert(m);
1915
37c1d5e9 1916 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
867c37f6
DM
1917 if (r < 0)
1918 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
1919
867c37f6
DM
1920 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
1921 if (r < 0)
1922 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
1923
1924 (void) fchmod(fileno(f), 0644);
1925
1926 fprintf(f,
1927 "USEC="USEC_FMT"\n"
1928 "WARN_WALL=%i\n"
1929 "MODE=%s\n",
1930 m->scheduled_shutdown_timeout,
1931 m->enable_wall_messages,
1932 m->scheduled_shutdown_type);
1933
91b3e7fb
LP
1934 if (!isempty(m->wall_message)) {
1935 _cleanup_free_ char *t;
1936
1937 t = cescape(m->wall_message);
1938 if (!t) {
1939 r = -ENOMEM;
1940 goto fail;
1941 }
1942
867c37f6 1943 fprintf(f, "WALL_MESSAGE=%s\n", t);
91b3e7fb 1944 }
867c37f6 1945
dacd6cee
LP
1946 r = fflush_and_check(f);
1947 if (r < 0)
1948 goto fail;
867c37f6 1949
dacd6cee 1950 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
867c37f6 1951 r = -errno;
dacd6cee 1952 goto fail;
867c37f6
DM
1953 }
1954
dacd6cee
LP
1955 return 0;
1956
1957fail:
1958 (void) unlink(temp_path);
1959 (void) unlink("/run/systemd/shutdown/scheduled");
1960
1961 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
867c37f6
DM
1962}
1963
df75a1a8 1964static void reset_scheduled_shutdown(Manager *m) {
36b69c31
LP
1965 assert(m);
1966
df75a1a8
AJ
1967 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
1968 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
1969 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
36b69c31 1970
df75a1a8
AJ
1971 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
1972 m->scheduled_shutdown_timeout = 0;
1973 m->shutdown_dry_run = false;
1974
1975 if (m->unlink_nologin) {
af229d7a 1976 (void) unlink_or_warn("/run/nologin");
df75a1a8
AJ
1977 m->unlink_nologin = false;
1978 }
36b69c31 1979
df75a1a8
AJ
1980 (void) unlink("/run/systemd/shutdown/scheduled");
1981}
1982
8aaa023a
DM
1983static int manager_scheduled_shutdown_handler(
1984 sd_event_source *s,
1985 uint64_t usec,
1986 void *userdata) {
1987
4afd3348 1988 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
8aaa023a
DM
1989 Manager *m = userdata;
1990 const char *target;
1991 int r;
1992
1993 assert(m);
1994
1995 if (isempty(m->scheduled_shutdown_type))
1996 return 0;
1997
36b69c31 1998 if (streq(m->scheduled_shutdown_type, "poweroff"))
8aaa023a 1999 target = SPECIAL_POWEROFF_TARGET;
36b69c31 2000 else if (streq(m->scheduled_shutdown_type, "reboot"))
8aaa023a 2001 target = SPECIAL_REBOOT_TARGET;
36b69c31
LP
2002 else if (streq(m->scheduled_shutdown_type, "halt"))
2003 target = SPECIAL_HALT_TARGET;
2004 else
2005 assert_not_reached("unexpected shutdown type");
8aaa023a 2006
b498d6ea 2007 /* Don't allow multiple jobs being executed at the same time */
f8bfa318 2008 if (m->action_what > 0) {
6d7f7fd4 2009 r = -EALREADY;
b498d6ea 2010 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target);
6d7f7fd4 2011 goto error;
b498d6ea
AJ
2012 }
2013
df75a1a8
AJ
2014 if (m->shutdown_dry_run) {
2015 /* We do not process delay inhibitors here. Otherwise, we
2016 * would have to be considered "in progress" (like the check
2017 * above) for some seconds after our admin has seen the final
2018 * wall message. */
2019
2020 bus_manager_log_shutdown(m, target);
2021 log_info("Running in dry run, suppressing action.");
2022 reset_scheduled_shutdown(m);
2023
2024 return 0;
2025 }
2026
2027 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error);
6d7f7fd4
AJ
2028 if (r < 0) {
2029 log_error_errno(r, "Scheduled shutdown to %s failed: %m", target);
2030 goto error;
2031 }
8aaa023a
DM
2032
2033 return 0;
6d7f7fd4
AJ
2034
2035error:
2036 reset_scheduled_shutdown(m);
2037 return r;
8aaa023a
DM
2038}
2039
19070062 2040static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
8aaa023a 2041 Manager *m = userdata;
4afd3348 2042 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
8aaa023a
DM
2043 const char *action_multiple_sessions = NULL;
2044 const char *action_ignore_inhibit = NULL;
2045 const char *action = NULL;
2046 uint64_t elapse;
2047 char *type;
2048 int r;
d13f5e16 2049 bool dry_run = false;
8aaa023a
DM
2050
2051 assert(m);
2052 assert(message);
2053
2054 r = sd_bus_message_read(message, "st", &type, &elapse);
2055 if (r < 0)
2056 return r;
2057
1389f4b9
DM
2058 if (startswith(type, "dry-")) {
2059 type += 4;
d13f5e16 2060 dry_run = true;
1389f4b9
DM
2061 }
2062
36b69c31
LP
2063 if (streq(type, "poweroff")) {
2064 action = "org.freedesktop.login1.power-off";
2065 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2066 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2067 } else if (streq(type, "reboot")) {
8aaa023a
DM
2068 action = "org.freedesktop.login1.reboot";
2069 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2070 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2071 } else if (streq(type, "halt")) {
2072 action = "org.freedesktop.login1.halt";
2073 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2074 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
8aaa023a
DM
2075 } else
2076 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2077
2078 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2079 action, action_multiple_sessions, action_ignore_inhibit, error);
2080 if (r != 0)
2081 return r;
2082
2083 if (m->scheduled_shutdown_timeout_source) {
2084 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2085 if (r < 0)
c2a23db0 2086 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
8aaa023a
DM
2087
2088 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2089 if (r < 0)
c2a23db0 2090 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
8aaa023a
DM
2091 } else {
2092 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2093 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2094 if (r < 0)
c2a23db0 2095 return log_error_errno(r, "sd_event_add_time() failed: %m");
8aaa023a
DM
2096 }
2097
2098 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2099 if (r < 0) {
2100 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2101 return log_oom();
2102 }
2103
d13f5e16
LP
2104 m->shutdown_dry_run = dry_run;
2105
867c37f6
DM
2106 if (m->nologin_timeout_source) {
2107 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2108 if (r < 0)
c2a23db0 2109 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
867c37f6
DM
2110
2111 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2112 if (r < 0)
c2a23db0 2113 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
867c37f6
DM
2114 } else {
2115 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2116 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2117 if (r < 0)
c2a23db0 2118 return log_error_errno(r, "sd_event_add_time() failed: %m");
867c37f6
DM
2119 }
2120
8aaa023a
DM
2121 m->scheduled_shutdown_timeout = elapse;
2122
e2fa5721
DM
2123 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2124 if (r >= 0) {
011062f3 2125 const char *tty = NULL;
e2fa5721
DM
2126
2127 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2128 (void) sd_bus_creds_get_tty(creds, &tty);
2129
2130 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2131 if (r < 0) {
2132 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2133 return log_oom();
2134 }
2135 }
2136
2137 r = manager_setup_wall_message_timer(m);
2138 if (r < 0)
2139 return r;
2140
f8169e62
AJ
2141 r = update_schedule_file(m);
2142 if (r < 0)
2143 return r;
867c37f6 2144
8aaa023a
DM
2145 return sd_bus_reply_method_return(message, NULL);
2146}
2147
19070062 2148static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
8aaa023a
DM
2149 Manager *m = userdata;
2150 bool cancelled;
2151
2152 assert(m);
2153 assert(message);
2154
2155 cancelled = m->scheduled_shutdown_type != NULL;
1389f4b9 2156 reset_scheduled_shutdown(m);
fb91034c 2157
6e78fa4a 2158 if (cancelled && m->enable_wall_messages) {
4afd3348 2159 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
e99742ef 2160 _cleanup_free_ char *username = NULL;
e2fa5721
DM
2161 const char *tty = NULL;
2162 uid_t uid = 0;
2163 int r;
2164
2165 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2166 if (r >= 0) {
2167 (void) sd_bus_creds_get_uid(creds, &uid);
2168 (void) sd_bus_creds_get_tty(creds, &tty);
2169 }
2170
e99742ef 2171 username = uid_to_name(uid);
e2fa5721 2172 utmp_wall("The system shutdown has been cancelled",
e99742ef 2173 username, tty, logind_wall_tty_filter, m);
e2fa5721
DM
2174 }
2175
8aaa023a
DM
2176 return sd_bus_reply_method_return(message, "b", cancelled);
2177}
2178
cc377381
LP
2179static int method_can_shutdown_or_sleep(
2180 Manager *m,
2181 sd_bus_message *message,
2182 InhibitWhat w,
2183 const char *action,
2184 const char *action_multiple_sessions,
2185 const char *action_ignore_inhibit,
ebcf1f97
LP
2186 const char *sleep_verb,
2187 sd_bus_error *error) {
de07ab16 2188
4afd3348 2189 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
c8c8ee85 2190 HandleAction handle;
cc377381
LP
2191 bool multiple_sessions, challenge, blocked;
2192 const char *result = NULL;
2193 uid_t uid;
2194 int r;
de07ab16 2195
cc377381
LP
2196 assert(m);
2197 assert(message);
2198 assert(w >= 0);
2199 assert(w <= _INHIBIT_WHAT_MAX);
2200 assert(action);
2201 assert(action_multiple_sessions);
2202 assert(action_ignore_inhibit);
de07ab16 2203
cc377381
LP
2204 if (sleep_verb) {
2205 r = can_sleep(sleep_verb);
8340b762 2206 if (IN_SET(r, 0, -ENOSPC))
b71c9758 2207 return sd_bus_reply_method_return(message, "s", "na");
de07ab16 2208 if (r < 0)
ebcf1f97 2209 return r;
cc377381 2210 }
de07ab16 2211
05bae4a6 2212 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
5b12334d
LP
2213 if (r < 0)
2214 return r;
2215
05bae4a6 2216 r = sd_bus_creds_get_euid(creds, &uid);
cc377381 2217 if (r < 0)
ebcf1f97 2218 return r;
de07ab16 2219
cc377381
LP
2220 r = have_multiple_sessions(m, uid);
2221 if (r < 0)
ebcf1f97 2222 return r;
de07ab16 2223
cc377381 2224 multiple_sessions = r > 0;
85a428c6 2225 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
de07ab16 2226
c8c8ee85
ZJS
2227 handle = handle_action_from_string(sleep_verb);
2228 if (handle >= 0) {
2229 const char *target;
2230
2231 target = manager_target_for_action(handle);
2232 if (target) {
2233 _cleanup_free_ char *load_state = NULL;
2234
2235 r = unit_load_state(m->bus, target, &load_state);
2236 if (r < 0)
2237 return r;
2238
2239 if (!streq(load_state, "loaded")) {
2240 result = "no";
2241 goto finish;
2242 }
2243 }
2244 }
2245
cc377381 2246 if (multiple_sessions) {
403ed0e5 2247 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
de07ab16 2248 if (r < 0)
ebcf1f97 2249 return r;
bef422ae 2250
cc377381
LP
2251 if (r > 0)
2252 result = "yes";
2253 else if (challenge)
2254 result = "challenge";
2255 else
2256 result = "no";
2257 }
bef422ae 2258
cc377381 2259 if (blocked) {
403ed0e5 2260 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
bef422ae 2261 if (r < 0)
ebcf1f97 2262 return r;
bef422ae 2263
0c093a62
HT
2264 if (r > 0) {
2265 if (!result)
2266 result = "yes";
2267 } else if (challenge) {
2268 if (!result || streq(result, "yes"))
2269 result = "challenge";
2270 } else
cc377381
LP
2271 result = "no";
2272 }
bef422ae 2273
cc377381
LP
2274 if (!multiple_sessions && !blocked) {
2275 /* If neither inhibit nor multiple sessions
2276 * apply then just check the normal policy */
bef422ae 2277
403ed0e5 2278 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
bef422ae 2279 if (r < 0)
ebcf1f97 2280 return r;
bef422ae 2281
cc377381
LP
2282 if (r > 0)
2283 result = "yes";
2284 else if (challenge)
2285 result = "challenge";
2286 else
2287 result = "no";
2288 }
bef422ae 2289
c8c8ee85 2290 finish:
df2d202e 2291 return sd_bus_reply_method_return(message, "s", result);
cc377381 2292}
bef422ae 2293
19070062 2294static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381 2295 Manager *m = userdata;
bef422ae 2296
cc377381
LP
2297 return method_can_shutdown_or_sleep(
2298 m, message,
2299 INHIBIT_SHUTDOWN,
2300 "org.freedesktop.login1.power-off",
2301 "org.freedesktop.login1.power-off-multiple-sessions",
2302 "org.freedesktop.login1.power-off-ignore-inhibit",
ebcf1f97
LP
2303 NULL,
2304 error);
cc377381 2305}
bef422ae 2306
19070062 2307static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381 2308 Manager *m = userdata;
bef422ae 2309
cc377381
LP
2310 return method_can_shutdown_or_sleep(
2311 m, message,
2312 INHIBIT_SHUTDOWN,
2313 "org.freedesktop.login1.reboot",
2314 "org.freedesktop.login1.reboot-multiple-sessions",
2315 "org.freedesktop.login1.reboot-ignore-inhibit",
ebcf1f97
LP
2316 NULL,
2317 error);
cc377381 2318}
bef422ae 2319
36b69c31
LP
2320static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2321 Manager *m = userdata;
2322
2323 return method_can_shutdown_or_sleep(
2324 m, message,
2325 INHIBIT_SHUTDOWN,
2326 "org.freedesktop.login1.halt",
2327 "org.freedesktop.login1.halt-multiple-sessions",
2328 "org.freedesktop.login1.halt-ignore-inhibit",
2329 NULL,
2330 error);
2331}
2332
19070062 2333static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381 2334 Manager *m = userdata;
7f7bb946 2335
cc377381
LP
2336 return method_can_shutdown_or_sleep(
2337 m, message,
2338 INHIBIT_SLEEP,
2339 "org.freedesktop.login1.suspend",
2340 "org.freedesktop.login1.suspend-multiple-sessions",
2341 "org.freedesktop.login1.suspend-ignore-inhibit",
ebcf1f97
LP
2342 "suspend",
2343 error);
cc377381 2344}
7f7bb946 2345
19070062 2346static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381 2347 Manager *m = userdata;
02b16a19 2348
cc377381
LP
2349 return method_can_shutdown_or_sleep(
2350 m, message,
2351 INHIBIT_SLEEP,
2352 "org.freedesktop.login1.hibernate",
2353 "org.freedesktop.login1.hibernate-multiple-sessions",
2354 "org.freedesktop.login1.hibernate-ignore-inhibit",
ebcf1f97
LP
2355 "hibernate",
2356 error);
cc377381 2357}
7f7bb946 2358
19070062 2359static int method_can_hybrid_sleep(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.hibernate",
2366 "org.freedesktop.login1.hibernate-multiple-sessions",
2367 "org.freedesktop.login1.hibernate-ignore-inhibit",
ebcf1f97
LP
2368 "hybrid-sleep",
2369 error);
cc377381 2370}
38f3fc7d 2371
e68c79db 2372static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c58493c0
ML
2373 Manager *m = userdata;
2374
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",
e68c79db 2381 "suspend-then-hibernate",
c58493c0
ML
2382 error);
2383}
2384
5bdf2243
JJ
2385static int property_get_reboot_to_firmware_setup(
2386 sd_bus *bus,
2387 const char *path,
2388 const char *interface,
2389 const char *property,
2390 sd_bus_message *reply,
2391 void *userdata,
2392 sd_bus_error *error) {
2393 int r;
2394
2395 assert(bus);
2396 assert(reply);
2397 assert(userdata);
2398
e667266a
LP
2399 r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2400 if (r == -ENXIO) {
2401 /* EFI case: let's see what is currently configured in the EFI variables */
2402 r = efi_get_reboot_to_firmware();
2403 if (r < 0 && r != -EOPNOTSUPP)
2404 log_warning_errno(r, "Failed to determine reboot-to-firmware-setup state: %m");
2405 } else if (r < 0)
2406 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2407 else if (r > 0) {
2408 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2409 if (access("/run/systemd/reboot-to-firmware-setup", F_OK) < 0) {
2410 if (errno != ENOENT)
2411 log_warning_errno(errno, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2412
2413 r = false;
2414 } else
2415 r = true;
2416 }
5bdf2243
JJ
2417
2418 return sd_bus_message_append(reply, "b", r > 0);
2419}
2420
2421static int method_set_reboot_to_firmware_setup(
5bdf2243
JJ
2422 sd_bus_message *message,
2423 void *userdata,
2424 sd_bus_error *error) {
2425
5bdf2243 2426 Manager *m = userdata;
e667266a
LP
2427 bool use_efi;
2428 int b, r;
5bdf2243 2429
5bdf2243
JJ
2430 assert(message);
2431 assert(m);
2432
889f25b2 2433 r = sd_bus_message_read(message, "b", &b);
5bdf2243
JJ
2434 if (r < 0)
2435 return r;
2436
e667266a
LP
2437 r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2438 if (r == -ENXIO) {
2439 /* EFI case: let's see what the firmware supports */
2440
2441 r = efi_reboot_to_firmware_supported();
2442 if (r == -EOPNOTSUPP)
2443 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware.");
2444 if (r < 0)
2445 return r;
2446
2447 use_efi = true;
2448
2449 } else if (r <= 0) {
2450 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2451
2452 if (r < 0)
2453 log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2454
2455 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware.");
2456 } else
2457 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2458 use_efi = false;
2459
5bdf2243
JJ
2460 r = bus_verify_polkit_async(message,
2461 CAP_SYS_ADMIN,
2462 "org.freedesktop.login1.set-reboot-to-firmware-setup",
403ed0e5 2463 NULL,
889f25b2 2464 false,
5bdf2243
JJ
2465 UID_INVALID,
2466 &m->polkit_registry,
2467 error);
2468 if (r < 0)
2469 return r;
2470 if (r == 0)
2471 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2472
e667266a
LP
2473 if (use_efi) {
2474 r = efi_set_reboot_to_firmware(b);
2475 if (r < 0)
2476 return r;
2477 } else {
2478 if (b) {
2479 r = touch("/run/systemd/reboot-to-firmware-setup");
2480 if (r < 0)
2481 return r;
2482 } else {
2483 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno != ENOENT)
2484 return -errno;
2485 }
2486 }
5bdf2243
JJ
2487
2488 return sd_bus_reply_method_return(message, NULL);
2489}
2490
350f9518
LP
2491static int return_test_polkit(
2492 sd_bus_message *message,
2493 int capability,
2494 const char *action,
2495 const char **details,
2496 uid_t good_user,
2497 sd_bus_error *e) {
2498
2499 const char *result;
2500 bool challenge;
2501 int r;
2502
2503 r = bus_test_polkit(message, capability, action, details, good_user, &challenge, e);
2504 if (r < 0)
2505 return r;
2506
2507 if (r > 0)
2508 result = "yes";
2509 else if (challenge)
2510 result = "challenge";
2511 else
2512 result = "no";
2513
2514 return sd_bus_reply_method_return(message, "s", result);
2515}
2516
5bdf2243 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) {
cc377381
LP
3666 /* systemd might have droppped off momentarily, let's
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}