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