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