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