chiark / gitweb /
logind: Stopped inhibitions should be considered inactive (#5698)
[elogind.git] / src / login / logind-session-dbus.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2011 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <string.h>
22
23 #include "alloc-util.h"
24 #include "bus-common-errors.h"
25 #include "bus-label.h"
26 #include "bus-util.h"
27 #include "fd-util.h"
28 #include "logind-session-device.h"
29 #include "logind-session.h"
30 #include "logind.h"
31 #include "signal-util.h"
32 #include "strv.h"
33 #include "util.h"
34
35 static int property_get_user(
36                 sd_bus *bus,
37                 const char *path,
38                 const char *interface,
39                 const char *property,
40                 sd_bus_message *reply,
41                 void *userdata,
42                 sd_bus_error *error) {
43
44         _cleanup_free_ char *p = NULL;
45         Session *s = userdata;
46
47         assert(bus);
48         assert(reply);
49         assert(s);
50
51         p = user_bus_path(s->user);
52         if (!p)
53                 return -ENOMEM;
54
55         return sd_bus_message_append(reply, "(uo)", (uint32_t) s->user->uid, p);
56 }
57
58 static int property_get_name(
59                 sd_bus *bus,
60                 const char *path,
61                 const char *interface,
62                 const char *property,
63                 sd_bus_message *reply,
64                 void *userdata,
65                 sd_bus_error *error) {
66
67         Session *s = userdata;
68
69         assert(bus);
70         assert(reply);
71         assert(s);
72
73         return sd_bus_message_append(reply, "s", s->user->name);
74 }
75
76 static int property_get_seat(
77                 sd_bus *bus,
78                 const char *path,
79                 const char *interface,
80                 const char *property,
81                 sd_bus_message *reply,
82                 void *userdata,
83                 sd_bus_error *error) {
84
85         _cleanup_free_ char *p = NULL;
86         Session *s = userdata;
87
88         assert(bus);
89         assert(reply);
90         assert(s);
91
92         p = s->seat ? seat_bus_path(s->seat) : strdup("/");
93         if (!p)
94                 return -ENOMEM;
95
96         return sd_bus_message_append(reply, "(so)", s->seat ? s->seat->id : "", p);
97 }
98
99 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, session_type, SessionType);
100 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, session_class, SessionClass);
101
102 static int property_get_active(
103                 sd_bus *bus,
104                 const char *path,
105                 const char *interface,
106                 const char *property,
107                 sd_bus_message *reply,
108                 void *userdata,
109                 sd_bus_error *error) {
110
111         Session *s = userdata;
112
113         assert(bus);
114         assert(reply);
115         assert(s);
116
117         return sd_bus_message_append(reply, "b", session_is_active(s));
118 }
119
120 static int property_get_state(
121                 sd_bus *bus,
122                 const char *path,
123                 const char *interface,
124                 const char *property,
125                 sd_bus_message *reply,
126                 void *userdata,
127                 sd_bus_error *error) {
128
129         Session *s = userdata;
130
131         assert(bus);
132         assert(reply);
133         assert(s);
134
135         return sd_bus_message_append(reply, "s", session_state_to_string(session_get_state(s)));
136 }
137
138 static int property_get_idle_hint(
139                 sd_bus *bus,
140                 const char *path,
141                 const char *interface,
142                 const char *property,
143                 sd_bus_message *reply,
144                 void *userdata,
145                 sd_bus_error *error) {
146
147         Session *s = userdata;
148
149         assert(bus);
150         assert(reply);
151         assert(s);
152
153         return sd_bus_message_append(reply, "b", session_get_idle_hint(s, NULL) > 0);
154 }
155
156 static int property_get_idle_since_hint(
157                 sd_bus *bus,
158                 const char *path,
159                 const char *interface,
160                 const char *property,
161                 sd_bus_message *reply,
162                 void *userdata,
163                 sd_bus_error *error) {
164
165         Session *s = userdata;
166         dual_timestamp t = DUAL_TIMESTAMP_NULL;
167         uint64_t u;
168         int r;
169
170         assert(bus);
171         assert(reply);
172         assert(s);
173
174         r = session_get_idle_hint(s, &t);
175         if (r < 0)
176                 return r;
177
178         u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
179
180         return sd_bus_message_append(reply, "t", u);
181 }
182
183 static int property_get_locked_hint(
184                 sd_bus *bus,
185                 const char *path,
186                 const char *interface,
187                 const char *property,
188                 sd_bus_message *reply,
189                 void *userdata,
190                 sd_bus_error *error) {
191
192         Session *s = userdata;
193
194         assert(bus);
195         assert(reply);
196         assert(s);
197
198         return sd_bus_message_append(reply, "b", session_get_locked_hint(s) > 0);
199 }
200
201 int bus_session_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
202         Session *s = userdata;
203         int r;
204
205         assert(message);
206         assert(s);
207
208         r = bus_verify_polkit_async(
209                         message,
210                         CAP_KILL,
211                         "org.freedesktop.login1.manage",
212                         NULL,
213                         false,
214                         s->user->uid,
215                         &s->manager->polkit_registry,
216                         error);
217         if (r < 0)
218                 return r;
219         if (r == 0)
220                 return 1; /* Will call us back */
221
222         r = session_stop(s, true);
223         if (r < 0)
224                 return r;
225
226         return sd_bus_reply_method_return(message, NULL);
227 }
228
229 int bus_session_method_activate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
230         Session *s = userdata;
231         int r;
232
233         assert(message);
234         assert(s);
235
236         r = session_activate(s);
237         if (r < 0)
238                 return r;
239
240         return sd_bus_reply_method_return(message, NULL);
241 }
242
243 int bus_session_method_lock(sd_bus_message *message, void *userdata, sd_bus_error *error) {
244         Session *s = userdata;
245         int r;
246
247         assert(message);
248         assert(s);
249
250         r = bus_verify_polkit_async(
251                         message,
252                         CAP_SYS_ADMIN,
253                         "org.freedesktop.login1.lock-sessions",
254                         NULL,
255                         false,
256                         s->user->uid,
257                         &s->manager->polkit_registry,
258                         error);
259         if (r < 0)
260                 return r;
261         if (r == 0)
262                 return 1; /* Will call us back */
263
264         r = session_send_lock(s, strstr(sd_bus_message_get_member(message), "Lock"));
265         if (r < 0)
266                 return r;
267
268         return sd_bus_reply_method_return(message, NULL);
269 }
270
271 static int method_set_idle_hint(sd_bus_message *message, void *userdata, sd_bus_error *error) {
272         _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
273         Session *s = userdata;
274         uid_t uid;
275         int r, b;
276
277         assert(message);
278         assert(s);
279
280         r = sd_bus_message_read(message, "b", &b);
281         if (r < 0)
282                 return r;
283
284         r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
285         if (r < 0)
286                 return r;
287
288         r = sd_bus_creds_get_euid(creds, &uid);
289         if (r < 0)
290                 return r;
291
292         if (uid != 0 && uid != s->user->uid)
293                 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set idle hint");
294
295         session_set_idle_hint(s, b);
296
297         return sd_bus_reply_method_return(message, NULL);
298 }
299
300 static int method_set_locked_hint(sd_bus_message *message, void *userdata, sd_bus_error *error) {
301         _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
302         Session *s = userdata;
303         uid_t uid;
304         int r, b;
305
306         assert(message);
307         assert(s);
308
309         r = sd_bus_message_read(message, "b", &b);
310         if (r < 0)
311                 return r;
312
313         r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
314         if (r < 0)
315                 return r;
316
317         r = sd_bus_creds_get_euid(creds, &uid);
318         if (r < 0)
319                 return r;
320
321         if (uid != 0 && uid != s->user->uid)
322                 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set locked hint");
323
324         session_set_locked_hint(s, b);
325
326         return sd_bus_reply_method_return(message, NULL);
327 }
328
329 int bus_session_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
330         Session *s = userdata;
331         const char *swho;
332         int32_t signo;
333         KillWho who;
334         int r;
335
336         assert(message);
337         assert(s);
338
339         r = sd_bus_message_read(message, "si", &swho, &signo);
340         if (r < 0)
341                 return r;
342
343         if (isempty(swho))
344                 who = KILL_ALL;
345         else {
346                 who = kill_who_from_string(swho);
347                 if (who < 0)
348                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
349         }
350
351         if (!SIGNAL_VALID(signo))
352                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
353
354         r = bus_verify_polkit_async(
355                         message,
356                         CAP_KILL,
357                         "org.freedesktop.login1.manage",
358                         NULL,
359                         false,
360                         s->user->uid,
361                         &s->manager->polkit_registry,
362                         error);
363         if (r < 0)
364                 return r;
365         if (r == 0)
366                 return 1; /* Will call us back */
367
368         r = session_kill(s, who, signo);
369         if (r < 0)
370                 return r;
371
372         return sd_bus_reply_method_return(message, NULL);
373 }
374
375 static int method_take_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
376         _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
377         Session *s = userdata;
378         int r, force;
379         uid_t uid;
380
381         assert(message);
382         assert(s);
383
384         r = sd_bus_message_read(message, "b", &force);
385         if (r < 0)
386                 return r;
387
388         r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
389         if (r < 0)
390                 return r;
391
392         r = sd_bus_creds_get_euid(creds, &uid);
393         if (r < 0)
394                 return r;
395
396         if (uid != 0 && (force || uid != s->user->uid))
397                 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may take control");
398
399         r = session_set_controller(s, sd_bus_message_get_sender(message), force, true);
400         if (r < 0)
401                 return r;
402
403         return sd_bus_reply_method_return(message, NULL);
404 }
405
406 static int method_release_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
407         Session *s = userdata;
408
409         assert(message);
410         assert(s);
411
412         if (!session_is_controller(s, sd_bus_message_get_sender(message)))
413                 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
414
415         session_drop_controller(s);
416
417         return sd_bus_reply_method_return(message, NULL);
418 }
419
420 static int method_take_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
421         Session *s = userdata;
422         uint32_t major, minor;
423         SessionDevice *sd;
424         dev_t dev;
425         int r;
426
427         assert(message);
428         assert(s);
429
430         r = sd_bus_message_read(message, "uu", &major, &minor);
431         if (r < 0)
432                 return r;
433
434         if (!session_is_controller(s, sd_bus_message_get_sender(message)))
435                 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
436
437         dev = makedev(major, minor);
438         sd = hashmap_get(s->devices, &dev);
439         if (sd)
440                 /* We don't allow retrieving a device multiple times.
441                  * The related ReleaseDevice call is not ref-counted.
442                  * The caller should use dup() if it requires more
443                  * than one fd (it would be functionally
444                  * equivalent). */
445                 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_IS_TAKEN, "Device already taken");
446
447         r = session_device_new(s, dev, true, &sd);
448         if (r < 0)
449                 return r;
450
451         r = session_device_save(sd);
452         if (r < 0)
453                 goto error;
454
455         r = sd_bus_reply_method_return(message, "hb", sd->fd, !sd->active);
456         if (r < 0)
457                 goto error;
458
459         session_save(s);
460         return 0;
461
462 error:
463         session_device_free(sd);
464         return r;
465 }
466
467 static int method_release_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
468         Session *s = userdata;
469         uint32_t major, minor;
470         SessionDevice *sd;
471         dev_t dev;
472         int r;
473
474         assert(message);
475         assert(s);
476
477         r = sd_bus_message_read(message, "uu", &major, &minor);
478         if (r < 0)
479                 return r;
480
481         if (!session_is_controller(s, sd_bus_message_get_sender(message)))
482                 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
483
484         dev = makedev(major, minor);
485         sd = hashmap_get(s->devices, &dev);
486         if (!sd)
487                 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
488
489         session_device_free(sd);
490         session_save(s);
491
492         return sd_bus_reply_method_return(message, NULL);
493 }
494
495 static int method_pause_device_complete(sd_bus_message *message, void *userdata, sd_bus_error *error) {
496         Session *s = userdata;
497         uint32_t major, minor;
498         SessionDevice *sd;
499         dev_t dev;
500         int r;
501
502         assert(message);
503         assert(s);
504
505         r = sd_bus_message_read(message, "uu", &major, &minor);
506         if (r < 0)
507                 return r;
508
509         if (!session_is_controller(s, sd_bus_message_get_sender(message)))
510                 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
511
512         dev = makedev(major, minor);
513         sd = hashmap_get(s->devices, &dev);
514         if (!sd)
515                 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
516
517         session_device_complete_pause(sd);
518
519         return sd_bus_reply_method_return(message, NULL);
520 }
521
522 const sd_bus_vtable session_vtable[] = {
523         SD_BUS_VTABLE_START(0),
524
525         SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Session, id), SD_BUS_VTABLE_PROPERTY_CONST),
526         SD_BUS_PROPERTY("User", "(uo)", property_get_user, 0, SD_BUS_VTABLE_PROPERTY_CONST),
527         SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST),
528         BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Session, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
529         SD_BUS_PROPERTY("VTNr", "u", NULL, offsetof(Session, vtnr), SD_BUS_VTABLE_PROPERTY_CONST),
530         SD_BUS_PROPERTY("Seat", "(so)", property_get_seat, 0, SD_BUS_VTABLE_PROPERTY_CONST),
531         SD_BUS_PROPERTY("TTY", "s", NULL, offsetof(Session, tty), SD_BUS_VTABLE_PROPERTY_CONST),
532         SD_BUS_PROPERTY("Display", "s", NULL, offsetof(Session, display), SD_BUS_VTABLE_PROPERTY_CONST),
533         SD_BUS_PROPERTY("Remote", "b", bus_property_get_bool, offsetof(Session, remote), SD_BUS_VTABLE_PROPERTY_CONST),
534         SD_BUS_PROPERTY("RemoteHost", "s", NULL, offsetof(Session, remote_host), SD_BUS_VTABLE_PROPERTY_CONST),
535         SD_BUS_PROPERTY("RemoteUser", "s", NULL, offsetof(Session, remote_user), SD_BUS_VTABLE_PROPERTY_CONST),
536         SD_BUS_PROPERTY("Service", "s", NULL, offsetof(Session, service), SD_BUS_VTABLE_PROPERTY_CONST),
537         SD_BUS_PROPERTY("Desktop", "s", NULL, offsetof(Session, desktop), SD_BUS_VTABLE_PROPERTY_CONST),
538         SD_BUS_PROPERTY("Scope", "s", NULL, offsetof(Session, scope), SD_BUS_VTABLE_PROPERTY_CONST),
539         SD_BUS_PROPERTY("Leader", "u", bus_property_get_pid, offsetof(Session, leader), SD_BUS_VTABLE_PROPERTY_CONST),
540         SD_BUS_PROPERTY("Audit", "u", NULL, offsetof(Session, audit_id), SD_BUS_VTABLE_PROPERTY_CONST),
541         SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Session, type), SD_BUS_VTABLE_PROPERTY_CONST),
542         SD_BUS_PROPERTY("Class", "s", property_get_class, offsetof(Session, class), SD_BUS_VTABLE_PROPERTY_CONST),
543         SD_BUS_PROPERTY("Active", "b", property_get_active, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
544         SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
545         SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
546         SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
547         SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
548         SD_BUS_PROPERTY("LockedHint", "b", property_get_locked_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
549
550         SD_BUS_METHOD("Terminate", NULL, NULL, bus_session_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED),
551         SD_BUS_METHOD("Activate", NULL, NULL, bus_session_method_activate, SD_BUS_VTABLE_UNPRIVILEGED),
552         SD_BUS_METHOD("Lock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED),
553         SD_BUS_METHOD("Unlock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED),
554         SD_BUS_METHOD("SetIdleHint", "b", NULL, method_set_idle_hint, SD_BUS_VTABLE_UNPRIVILEGED),
555         SD_BUS_METHOD("SetLockedHint", "b", NULL, method_set_locked_hint, SD_BUS_VTABLE_UNPRIVILEGED),
556         SD_BUS_METHOD("Kill", "si", NULL, bus_session_method_kill, SD_BUS_VTABLE_UNPRIVILEGED),
557         SD_BUS_METHOD("TakeControl", "b", NULL, method_take_control, SD_BUS_VTABLE_UNPRIVILEGED),
558         SD_BUS_METHOD("ReleaseControl", NULL, NULL, method_release_control, SD_BUS_VTABLE_UNPRIVILEGED),
559         SD_BUS_METHOD("TakeDevice", "uu", "hb", method_take_device, SD_BUS_VTABLE_UNPRIVILEGED),
560         SD_BUS_METHOD("ReleaseDevice", "uu", NULL, method_release_device, SD_BUS_VTABLE_UNPRIVILEGED),
561         SD_BUS_METHOD("PauseDeviceComplete", "uu", NULL, method_pause_device_complete, SD_BUS_VTABLE_UNPRIVILEGED),
562
563         SD_BUS_SIGNAL("PauseDevice", "uus", 0),
564         SD_BUS_SIGNAL("ResumeDevice", "uuh", 0),
565         SD_BUS_SIGNAL("Lock", NULL, 0),
566         SD_BUS_SIGNAL("Unlock", NULL, 0),
567
568         SD_BUS_VTABLE_END
569 };
570
571 int session_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
572         Manager *m = userdata;
573         Session *session;
574         int r;
575
576         assert(bus);
577         assert(path);
578         assert(interface);
579         assert(found);
580         assert(m);
581
582         if (streq(path, "/org/freedesktop/login1/session/self")) {
583                 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
584                 sd_bus_message *message;
585                 const char *name;
586
587                 message = sd_bus_get_current_message(bus);
588                 if (!message)
589                         return 0;
590
591                 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
592                 if (r < 0)
593                         return r;
594
595                 r = sd_bus_creds_get_session(creds, &name);
596                 if (r < 0)
597                         return r;
598
599                 session = hashmap_get(m->sessions, name);
600         } else {
601                 _cleanup_free_ char *e = NULL;
602                 const char *p;
603
604                 p = startswith(path, "/org/freedesktop/login1/session/");
605                 if (!p)
606                         return 0;
607
608                 e = bus_label_unescape(p);
609                 if (!e)
610                         return -ENOMEM;
611
612                 session = hashmap_get(m->sessions, e);
613         }
614
615         if (!session)
616                 return 0;
617
618         *found = session;
619         return 1;
620 }
621
622 char *session_bus_path(Session *s) {
623         _cleanup_free_ char *t = NULL;
624
625         assert(s);
626
627         t = bus_label_escape(s->id);
628         if (!t)
629                 return NULL;
630
631         return strappend("/org/freedesktop/login1/session/", t);
632 }
633
634 int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
635         _cleanup_strv_free_ char **l = NULL;
636         sd_bus_message *message;
637         Manager *m = userdata;
638         Session *session;
639         Iterator i;
640         int r;
641
642         assert(bus);
643         assert(path);
644         assert(nodes);
645
646         HASHMAP_FOREACH(session, m->sessions, i) {
647                 char *p;
648
649                 p = session_bus_path(session);
650                 if (!p)
651                         return -ENOMEM;
652
653                 r = strv_consume(&l, p);
654                 if (r < 0)
655                         return r;
656         }
657
658         message = sd_bus_get_current_message(bus);
659         if (message) {
660                 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
661                 const char *name;
662
663                 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
664                 if (r >= 0) {
665                         r = sd_bus_creds_get_session(creds, &name);
666                         if (r >= 0) {
667                                 session = hashmap_get(m->sessions, name);
668                                 if (session) {
669                                         r = strv_extend(&l, "/org/freedesktop/login1/session/self");
670                                         if (r < 0)
671                                                 return r;
672                                 }
673                         }
674                 }
675         }
676
677         *nodes = l;
678         l = NULL;
679
680         return 1;
681 }
682
683 int session_send_signal(Session *s, bool new_session) {
684         _cleanup_free_ char *p = NULL;
685
686         assert(s);
687
688         p = session_bus_path(s);
689         if (!p)
690                 return -ENOMEM;
691
692         return sd_bus_emit_signal(
693                         s->manager->bus,
694                         "/org/freedesktop/login1",
695                         "org.freedesktop.login1.Manager",
696                         new_session ? "SessionNew" : "SessionRemoved",
697                         "so", s->id, p);
698 }
699
700 int session_send_changed(Session *s, const char *properties, ...) {
701         _cleanup_free_ char *p = NULL;
702         char **l;
703
704         assert(s);
705
706         if (!s->started)
707                 return 0;
708
709         p = session_bus_path(s);
710         if (!p)
711                 return -ENOMEM;
712
713         l = strv_from_stdarg_alloca(properties);
714
715         return sd_bus_emit_properties_changed_strv(s->manager->bus, p, "org.freedesktop.login1.Session", l);
716 }
717
718 int session_send_lock(Session *s, bool lock) {
719         _cleanup_free_ char *p = NULL;
720
721         assert(s);
722
723         p = session_bus_path(s);
724         if (!p)
725                 return -ENOMEM;
726
727         return sd_bus_emit_signal(
728                         s->manager->bus,
729                         p,
730                         "org.freedesktop.login1.Session",
731                         lock ? "Lock" : "Unlock",
732                         NULL);
733 }
734
735 int session_send_lock_all(Manager *m, bool lock) {
736         Session *session;
737         Iterator i;
738         int r = 0;
739
740         assert(m);
741
742         HASHMAP_FOREACH(session, m->sessions, i) {
743                 int k;
744
745                 k = session_send_lock(session, lock);
746                 if (k < 0)
747                         r = k;
748         }
749
750         return r;
751 }
752
753 int session_send_create_reply(Session *s, sd_bus_error *error) {
754         _cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL;
755         _cleanup_close_ int fifo_fd = -1;
756         _cleanup_free_ char *p = NULL;
757
758         assert(s);
759
760         /* This is called after the session scope and the user service
761          * were successfully created, and finishes where
762          * bus_manager_create_session() left off. */
763
764         if (!s->create_message)
765                 return 0;
766
767 #if 0 /// elogind does not support scope and service jobs
768         if (!sd_bus_error_is_set(error) && (s->scope_job || s->user->service_job))
769                 return 0;
770 #endif // 0
771
772         c = s->create_message;
773         s->create_message = NULL;
774
775         if (error)
776                 return sd_bus_reply_method_error(c, error);
777
778         fifo_fd = session_create_fifo(s);
779         if (fifo_fd < 0)
780                 return fifo_fd;
781
782         /* Update the session state file before we notify the client
783          * about the result. */
784         session_save(s);
785
786 #if 1 /// Additionally elogind saves the user state file
787         user_save(s->user);
788 #endif // 1
789         p = session_bus_path(s);
790         if (!p)
791                 return -ENOMEM;
792
793         log_debug("Sending reply about created session: "
794                   "id=%s object_path=%s uid=%u runtime_path=%s "
795                   "session_fd=%d seat=%s vtnr=%u",
796                   s->id,
797                   p,
798                   (uint32_t) s->user->uid,
799                   s->user->runtime_path,
800                   fifo_fd,
801                   s->seat ? s->seat->id : "",
802                   (uint32_t) s->vtnr);
803
804         return sd_bus_reply_method_return(
805                         c, "soshusub",
806                         s->id,
807                         p,
808                         s->user->runtime_path,
809                         fifo_fd,
810                         (uint32_t) s->user->uid,
811                         s->seat ? s->seat->id : "",
812                         (uint32_t) s->vtnr,
813                         false);
814 }