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