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