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