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