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