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