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