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