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