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