]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-session-dbus.c
core: remove the redundancy of 'n_fds' and 'n_storage_fds' in ExecParameters struct
[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"
6eb7c172 31#include "signal-util.h"
3ffd4af2
LP
32#include "strv.h"
33#include "util.h"
3f49d45a 34
cc377381
LP
35static 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,
ebcf1f97
LP
41 void *userdata,
42 sd_bus_error *error) {
cc377381
LP
43
44 _cleanup_free_ char *p = NULL;
45 Session *s = userdata;
46
47 assert(bus);
48 assert(reply);
3f49d45a
LP
49 assert(s);
50
cc377381
LP
51 p = user_bus_path(s->user);
52 if (!p)
3f49d45a
LP
53 return -ENOMEM;
54
cc377381
LP
55 return sd_bus_message_append(reply, "(uo)", (uint32_t) s->user->uid, p);
56}
3f49d45a 57
cc377381
LP
58static 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,
ebcf1f97
LP
64 void *userdata,
65 sd_bus_error *error) {
3f49d45a 66
cc377381 67 Session *s = userdata;
3f49d45a 68
cc377381
LP
69 assert(bus);
70 assert(reply);
71 assert(s);
3f49d45a 72
cc377381 73 return sd_bus_message_append(reply, "s", s->user->name);
3f49d45a
LP
74}
75
cc377381
LP
76static 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,
ebcf1f97
LP
82 void *userdata,
83 sd_bus_error *error) {
3f49d45a 84
cc377381
LP
85 _cleanup_free_ char *p = NULL;
86 Session *s = userdata;
3f49d45a 87
cc377381
LP
88 assert(bus);
89 assert(reply);
90 assert(s);
3f49d45a 91
cc377381 92 p = s->seat ? seat_bus_path(s->seat) : strdup("/");
3f49d45a
LP
93 if (!p)
94 return -ENOMEM;
95
cc377381
LP
96 return sd_bus_message_append(reply, "(so)", s->seat ? s->seat->id : "", p);
97}
3f49d45a 98
cc377381
LP
99static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, session_type, SessionType);
100static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, session_class, SessionClass);
3f49d45a 101
cc377381
LP
102static 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,
ebcf1f97
LP
108 void *userdata,
109 sd_bus_error *error) {
3f49d45a 110
cc377381 111 Session *s = userdata;
3f49d45a 112
cc377381
LP
113 assert(bus);
114 assert(reply);
3f49d45a
LP
115 assert(s);
116
cc377381 117 return sd_bus_message_append(reply, "b", session_is_active(s));
3f49d45a
LP
118}
119
cc377381
LP
120static 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,
ebcf1f97
LP
126 void *userdata,
127 sd_bus_error *error) {
a185c5aa 128
cc377381
LP
129 Session *s = userdata;
130
131 assert(bus);
132 assert(reply);
a185c5aa
LP
133 assert(s);
134
cc377381
LP
135 return sd_bus_message_append(reply, "s", session_state_to_string(session_get_state(s)));
136}
137
138static 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,
ebcf1f97
LP
144 void *userdata,
145 sd_bus_error *error) {
a185c5aa 146
cc377381
LP
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);
a185c5aa
LP
154}
155
cc377381
LP
156static 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,
ebcf1f97
LP
162 void *userdata,
163 sd_bus_error *error) {
cc377381
LP
164
165 Session *s = userdata;
5cb14b37 166 dual_timestamp t = DUAL_TIMESTAMP_NULL;
a185c5aa 167 uint64_t u;
ca4f2b6d 168 int r;
a185c5aa 169
cc377381
LP
170 assert(bus);
171 assert(reply);
a185c5aa
LP
172 assert(s);
173
ca4f2b6d
VP
174 r = session_get_idle_hint(s, &t);
175 if (r < 0)
176 return r;
177
a185c5aa
LP
178 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
179
cc377381 180 return sd_bus_message_append(reply, "t", u);
a185c5aa
LP
181}
182
42d35e13
VT
183static 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
19070062 201int bus_session_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
202 Session *s = userdata;
203 int r;
0604381b 204
cc377381 205 assert(message);
0604381b
LP
206 assert(s);
207
c529695e
LP
208 r = bus_verify_polkit_async(
209 message,
210 CAP_KILL,
211 "org.freedesktop.login1.manage",
403ed0e5 212 NULL,
c529695e
LP
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
9bb69af4 222 r = session_stop(s, true);
cc377381 223 if (r < 0)
ebcf1f97 224 return r;
0604381b 225
df2d202e 226 return sd_bus_reply_method_return(message, NULL);
0604381b
LP
227}
228
19070062 229int bus_session_method_activate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
230 Session *s = userdata;
231 int r;
3f49d45a 232
cc377381
LP
233 assert(message);
234 assert(s);
3f49d45a 235
cc377381
LP
236 r = session_activate(s);
237 if (r < 0)
ebcf1f97 238 return r;
3f49d45a 239
df2d202e 240 return sd_bus_reply_method_return(message, NULL);
cc377381
LP
241}
242
19070062 243int bus_session_method_lock(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
244 Session *s = userdata;
245 int r;
246
cc377381
LP
247 assert(message);
248 assert(s);
3f49d45a 249
c529695e
LP
250 r = bus_verify_polkit_async(
251 message,
252 CAP_SYS_ADMIN,
253 "org.freedesktop.login1.lock-sessions",
403ed0e5 254 NULL,
c529695e
LP
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"));
cc377381 265 if (r < 0)
ebcf1f97 266 return r;
3f49d45a 267
df2d202e 268 return sd_bus_reply_method_return(message, NULL);
3f49d45a
LP
269}
270
19070062 271static int method_set_idle_hint(sd_bus_message *message, void *userdata, sd_bus_error *error) {
4afd3348 272 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
cc377381
LP
273 Session *s = userdata;
274 uid_t uid;
275 int r, b;
d200735e 276
cc377381
LP
277 assert(message);
278 assert(s);
279
280 r = sd_bus_message_read(message, "b", &b);
281 if (r < 0)
ebcf1f97 282 return r;
d200735e 283
05bae4a6 284 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
5b12334d
LP
285 if (r < 0)
286 return r;
287
05bae4a6 288 r = sd_bus_creds_get_euid(creds, &uid);
cc377381 289 if (r < 0)
ebcf1f97 290 return r;
cc377381
LP
291
292 if (uid != 0 && uid != s->user->uid)
2b233285 293 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set idle hint");
cc377381
LP
294
295 session_set_idle_hint(s, b);
3f49d45a 296
df2d202e 297 return sd_bus_reply_method_return(message, NULL);
cc377381
LP
298}
299
42d35e13
VT
300static 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
19070062 329int bus_session_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
330 Session *s = userdata;
331 const char *swho;
332 int32_t signo;
333 KillWho who;
bef422ae
LP
334 int r;
335
3f49d45a 336 assert(message);
cc377381 337 assert(s);
3f49d45a 338
cc377381
LP
339 r = sd_bus_message_read(message, "si", &swho, &signo);
340 if (r < 0)
ebcf1f97 341 return r;
cc377381
LP
342
343 if (isempty(swho))
344 who = KILL_ALL;
345 else {
346 who = kill_who_from_string(swho);
347 if (who < 0)
ebcf1f97 348 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
cc377381 349 }
bef422ae 350
6eb7c172 351 if (!SIGNAL_VALID(signo))
ebcf1f97 352 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
bef422ae 353
c529695e
LP
354 r = bus_verify_polkit_async(
355 message,
356 CAP_KILL,
357 "org.freedesktop.login1.manage",
403ed0e5 358 NULL,
c529695e
LP
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
cc377381
LP
368 r = session_kill(s, who, signo);
369 if (r < 0)
ebcf1f97 370 return r;
bef422ae 371
df2d202e 372 return sd_bus_reply_method_return(message, NULL);
cc377381 373}
bef422ae 374
19070062 375static int method_take_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
4afd3348 376 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
cc377381
LP
377 Session *s = userdata;
378 int r, force;
379 uid_t uid;
bef422ae 380
cc377381
LP
381 assert(message);
382 assert(s);
bef422ae 383
cc377381
LP
384 r = sd_bus_message_read(message, "b", &force);
385 if (r < 0)
ebcf1f97 386 return r;
bef422ae 387
05bae4a6 388 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
5b12334d
LP
389 if (r < 0)
390 return r;
391
05bae4a6 392 r = sd_bus_creds_get_euid(creds, &uid);
cc377381 393 if (r < 0)
ebcf1f97 394 return r;
bef422ae 395
cc377381 396 if (uid != 0 && (force || uid != s->user->uid))
ebcf1f97 397 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may take control");
bef422ae 398
cc377381
LP
399 r = session_set_controller(s, sd_bus_message_get_sender(message), force);
400 if (r < 0)
ebcf1f97 401 return r;
bef422ae 402
df2d202e 403 return sd_bus_reply_method_return(message, NULL);
cc377381 404}
bef422ae 405
19070062 406static int method_release_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381 407 Session *s = userdata;
bef422ae 408
cc377381
LP
409 assert(message);
410 assert(s);
5bc849fd 411
cc377381 412 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
ebcf1f97 413 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
5bc849fd 414
cc377381 415 session_drop_controller(s);
bef422ae 416
df2d202e 417 return sd_bus_reply_method_return(message, NULL);
cc377381 418}
bef422ae 419
19070062 420static int method_take_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
421 Session *s = userdata;
422 uint32_t major, minor;
423 SessionDevice *sd;
424 dev_t dev;
425 int r;
de07ab16 426
cc377381
LP
427 assert(message);
428 assert(s);
de07ab16 429
cc377381
LP
430 r = sd_bus_message_read(message, "uu", &major, &minor);
431 if (r < 0)
ebcf1f97 432 return r;
cc377381
LP
433
434 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
ebcf1f97 435 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
cc377381
LP
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). */
ebcf1f97 445 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_IS_TAKEN, "Device already taken");
cc377381
LP
446
447 r = session_device_new(s, dev, &sd);
448 if (r < 0)
ebcf1f97 449 return r;
de07ab16 450
df2d202e 451 r = sd_bus_reply_method_return(message, "hb", sd->fd, !sd->active);
cc377381
LP
452 if (r < 0)
453 session_device_free(sd);
118ecf32 454
cc377381
LP
455 return r;
456}
118ecf32 457
19070062 458static int method_release_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
459 Session *s = userdata;
460 uint32_t major, minor;
461 SessionDevice *sd;
462 dev_t dev;
463 int r;
118ecf32 464
cc377381
LP
465 assert(message);
466 assert(s);
118ecf32 467
cc377381
LP
468 r = sd_bus_message_read(message, "uu", &major, &minor);
469 if (r < 0)
ebcf1f97 470 return r;
118ecf32 471
cc377381 472 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
ebcf1f97 473 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
118ecf32 474
cc377381
LP
475 dev = makedev(major, minor);
476 sd = hashmap_get(s->devices, &dev);
477 if (!sd)
ebcf1f97 478 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
118ecf32 479
cc377381 480 session_device_free(sd);
df2d202e 481 return sd_bus_reply_method_return(message, NULL);
cc377381 482}
118ecf32 483
19070062 484static int method_pause_device_complete(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
485 Session *s = userdata;
486 uint32_t major, minor;
487 SessionDevice *sd;
488 dev_t dev;
489 int r;
118ecf32 490
cc377381
LP
491 assert(message);
492 assert(s);
bef422ae 493
cc377381
LP
494 r = sd_bus_message_read(message, "uu", &major, &minor);
495 if (r < 0)
ebcf1f97 496 return r;
cc377381
LP
497
498 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
ebcf1f97 499 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
bef422ae 500
cc377381
LP
501 dev = makedev(major, minor);
502 sd = hashmap_get(s->devices, &dev);
503 if (!sd)
ebcf1f97 504 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
bef422ae 505
cc377381 506 session_device_complete_pause(sd);
bef422ae 507
df2d202e 508 return sd_bus_reply_method_return(message, NULL);
3f49d45a
LP
509}
510
cc377381
LP
511const sd_bus_vtable session_vtable[] = {
512 SD_BUS_VTABLE_START(0),
513
556089dc
LP
514 SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Session, id), SD_BUS_VTABLE_PROPERTY_CONST),
515 SD_BUS_PROPERTY("User", "(uo)", property_get_user, 0, SD_BUS_VTABLE_PROPERTY_CONST),
516 SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST),
517 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Session, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
518 SD_BUS_PROPERTY("VTNr", "u", NULL, offsetof(Session, vtnr), SD_BUS_VTABLE_PROPERTY_CONST),
519 SD_BUS_PROPERTY("Seat", "(so)", property_get_seat, 0, SD_BUS_VTABLE_PROPERTY_CONST),
520 SD_BUS_PROPERTY("TTY", "s", NULL, offsetof(Session, tty), SD_BUS_VTABLE_PROPERTY_CONST),
521 SD_BUS_PROPERTY("Display", "s", NULL, offsetof(Session, display), SD_BUS_VTABLE_PROPERTY_CONST),
522 SD_BUS_PROPERTY("Remote", "b", bus_property_get_bool, offsetof(Session, remote), SD_BUS_VTABLE_PROPERTY_CONST),
523 SD_BUS_PROPERTY("RemoteHost", "s", NULL, offsetof(Session, remote_host), SD_BUS_VTABLE_PROPERTY_CONST),
524 SD_BUS_PROPERTY("RemoteUser", "s", NULL, offsetof(Session, remote_user), SD_BUS_VTABLE_PROPERTY_CONST),
525 SD_BUS_PROPERTY("Service", "s", NULL, offsetof(Session, service), SD_BUS_VTABLE_PROPERTY_CONST),
a4cd87e9 526 SD_BUS_PROPERTY("Desktop", "s", NULL, offsetof(Session, desktop), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
527 SD_BUS_PROPERTY("Scope", "s", NULL, offsetof(Session, scope), SD_BUS_VTABLE_PROPERTY_CONST),
528 SD_BUS_PROPERTY("Leader", "u", bus_property_get_pid, offsetof(Session, leader), SD_BUS_VTABLE_PROPERTY_CONST),
529 SD_BUS_PROPERTY("Audit", "u", NULL, offsetof(Session, audit_id), SD_BUS_VTABLE_PROPERTY_CONST),
530 SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Session, type), SD_BUS_VTABLE_PROPERTY_CONST),
531 SD_BUS_PROPERTY("Class", "s", property_get_class, offsetof(Session, class), SD_BUS_VTABLE_PROPERTY_CONST),
cc377381
LP
532 SD_BUS_PROPERTY("Active", "b", property_get_active, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
533 SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
534 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
535 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
536 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
42d35e13 537 SD_BUS_PROPERTY("LockedHint", "b", property_get_locked_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
cc377381 538
c529695e
LP
539 SD_BUS_METHOD("Terminate", NULL, NULL, bus_session_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED),
540 SD_BUS_METHOD("Activate", NULL, NULL, bus_session_method_activate, SD_BUS_VTABLE_UNPRIVILEGED),
541 SD_BUS_METHOD("Lock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED),
542 SD_BUS_METHOD("Unlock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED),
adacb957 543 SD_BUS_METHOD("SetIdleHint", "b", NULL, method_set_idle_hint, SD_BUS_VTABLE_UNPRIVILEGED),
42d35e13 544 SD_BUS_METHOD("SetLockedHint", "b", NULL, method_set_locked_hint, SD_BUS_VTABLE_UNPRIVILEGED),
c529695e 545 SD_BUS_METHOD("Kill", "si", NULL, bus_session_method_kill, SD_BUS_VTABLE_UNPRIVILEGED),
adacb957
LP
546 SD_BUS_METHOD("TakeControl", "b", NULL, method_take_control, SD_BUS_VTABLE_UNPRIVILEGED),
547 SD_BUS_METHOD("ReleaseControl", NULL, NULL, method_release_control, SD_BUS_VTABLE_UNPRIVILEGED),
548 SD_BUS_METHOD("TakeDevice", "uu", "hb", method_take_device, SD_BUS_VTABLE_UNPRIVILEGED),
549 SD_BUS_METHOD("ReleaseDevice", "uu", NULL, method_release_device, SD_BUS_VTABLE_UNPRIVILEGED),
550 SD_BUS_METHOD("PauseDeviceComplete", "uu", NULL, method_pause_device_complete, SD_BUS_VTABLE_UNPRIVILEGED),
cc377381
LP
551
552 SD_BUS_SIGNAL("PauseDevice", "uus", 0),
553 SD_BUS_SIGNAL("ResumeDevice", "uuh", 0),
554 SD_BUS_SIGNAL("Lock", NULL, 0),
555 SD_BUS_SIGNAL("Unlock", NULL, 0),
556
557 SD_BUS_VTABLE_END
558};
3f49d45a 559
f00c3121 560int session_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
3f49d45a 561 Manager *m = userdata;
cc377381 562 Session *session;
927b1649 563 int r;
3f49d45a 564
cc377381
LP
565 assert(bus);
566 assert(path);
567 assert(interface);
568 assert(found);
569 assert(m);
3f49d45a 570
927b1649 571 if (streq(path, "/org/freedesktop/login1/session/self")) {
4afd3348 572 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
927b1649 573 sd_bus_message *message;
309a29df 574 const char *name;
3f49d45a 575
19befb2d 576 message = sd_bus_get_current_message(bus);
927b1649
LP
577 if (!message)
578 return 0;
579
309a29df 580 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
927b1649 581 if (r < 0)
5b12334d
LP
582 return r;
583
309a29df 584 r = sd_bus_creds_get_session(creds, &name);
5b12334d
LP
585 if (r < 0)
586 return r;
927b1649 587
309a29df 588 session = hashmap_get(m->sessions, name);
927b1649
LP
589 } else {
590 _cleanup_free_ char *e = NULL;
591 const char *p;
3f49d45a 592
927b1649
LP
593 p = startswith(path, "/org/freedesktop/login1/session/");
594 if (!p)
595 return 0;
596
a6278b88 597 e = bus_label_unescape(p);
927b1649
LP
598 if (!e)
599 return -ENOMEM;
600
601 session = hashmap_get(m->sessions, e);
927b1649 602 }
3f49d45a 603
309a29df
LP
604 if (!session)
605 return 0;
606
cc377381
LP
607 *found = session;
608 return 1;
3f49d45a
LP
609}
610
3f49d45a 611char *session_bus_path(Session *s) {
9444b1f2 612 _cleanup_free_ char *t = NULL;
3f49d45a
LP
613
614 assert(s);
615
a6278b88 616 t = bus_label_escape(s->id);
3f49d45a
LP
617 if (!t)
618 return NULL;
619
4654e558 620 return strappend("/org/freedesktop/login1/session/", t);
3f49d45a 621}
da119395 622
f00c3121 623int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
cc377381 624 _cleanup_strv_free_ char **l = NULL;
ca56b0a6 625 sd_bus_message *message;
cc377381
LP
626 Manager *m = userdata;
627 Session *session;
628 Iterator i;
629 int r;
630
631 assert(bus);
632 assert(path);
633 assert(nodes);
634
635 HASHMAP_FOREACH(session, m->sessions, i) {
636 char *p;
637
638 p = session_bus_path(session);
639 if (!p)
640 return -ENOMEM;
641
6e18964d
ZJS
642 r = strv_consume(&l, p);
643 if (r < 0)
cc377381 644 return r;
cc377381
LP
645 }
646
ca56b0a6
DH
647 message = sd_bus_get_current_message(bus);
648 if (message) {
4afd3348 649 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
ca56b0a6
DH
650 const char *name;
651
652 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
653 if (r >= 0) {
654 r = sd_bus_creds_get_session(creds, &name);
655 if (r >= 0) {
656 session = hashmap_get(m->sessions, name);
657 if (session) {
658 r = strv_extend(&l, "/org/freedesktop/login1/session/self");
659 if (r < 0)
660 return r;
661 }
662 }
663 }
664 }
b298e984 665
cc377381
LP
666 *nodes = l;
667 l = NULL;
668
669 return 1;
670}
671
da119395 672int session_send_signal(Session *s, bool new_session) {
ce0fc5f5 673 _cleanup_free_ char *p = NULL;
da119395
LP
674
675 assert(s);
676
da119395
LP
677 p = session_bus_path(s);
678 if (!p)
4654e558 679 return -ENOMEM;
da119395 680
cc377381
LP
681 return sd_bus_emit_signal(
682 s->manager->bus,
683 "/org/freedesktop/login1",
684 "org.freedesktop.login1.Manager",
685 new_session ? "SessionNew" : "SessionRemoved",
686 "so", s->id, p);
da119395 687}
9418f147 688
cc377381 689int session_send_changed(Session *s, const char *properties, ...) {
ce0fc5f5 690 _cleanup_free_ char *p = NULL;
cc377381 691 char **l;
9418f147
LP
692
693 assert(s);
694
ed18b08b
LP
695 if (!s->started)
696 return 0;
697
9418f147
LP
698 p = session_bus_path(s);
699 if (!p)
700 return -ENOMEM;
701
cc377381 702 l = strv_from_stdarg_alloca(properties);
9418f147 703
cc377381 704 return sd_bus_emit_properties_changed_strv(s->manager->bus, p, "org.freedesktop.login1.Session", l);
9418f147 705}
88e3dc90
LP
706
707int session_send_lock(Session *s, bool lock) {
ce0fc5f5 708 _cleanup_free_ char *p = NULL;
88e3dc90
LP
709
710 assert(s);
711
712 p = session_bus_path(s);
713 if (!p)
714 return -ENOMEM;
715
cc377381
LP
716 return sd_bus_emit_signal(
717 s->manager->bus,
718 p,
719 "org.freedesktop.login1.Session",
720 lock ? "Lock" : "Unlock",
721 NULL);
88e3dc90 722}
7ba64386
LP
723
724int session_send_lock_all(Manager *m, bool lock) {
725 Session *session;
726 Iterator i;
727 int r = 0;
728
729 assert(m);
730
731 HASHMAP_FOREACH(session, m->sessions, i) {
732 int k;
733
734 k = session_send_lock(session, lock);
735 if (k < 0)
736 r = k;
737 }
738
739 return r;
740}
fb6becb4 741
cc377381 742int session_send_create_reply(Session *s, sd_bus_error *error) {
4afd3348 743 _cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL;
cc377381
LP
744 _cleanup_close_ int fifo_fd = -1;
745 _cleanup_free_ char *p = NULL;
fb6becb4
LP
746
747 assert(s);
748
dd9b67aa
LP
749 /* This is called after the session scope and the user service
750 * were successfully created, and finishes where
751 * bus_manager_create_session() left off. */
cba38758 752
cc377381
LP
753 if (!s->create_message)
754 return 0;
fb6becb4 755
dd9b67aa
LP
756 if (!sd_bus_error_is_set(error) && (s->scope_job || s->user->service_job))
757 return 0;
758
cc377381
LP
759 c = s->create_message;
760 s->create_message = NULL;
fb6becb4 761
cc377381 762 if (error)
df2d202e 763 return sd_bus_reply_method_error(c, error);
fb6becb4 764
cc377381
LP
765 fifo_fd = session_create_fifo(s);
766 if (fifo_fd < 0)
767 return fifo_fd;
fb6becb4 768
38fdcbed
TA
769 /* Update the session state file before we notify the client
770 * about the result. */
771 session_save(s);
772
cc377381
LP
773 p = session_bus_path(s);
774 if (!p)
775 return -ENOMEM;
fb6becb4 776
5a330cda 777 log_debug("Sending reply about created session: "
236af516
DH
778 "id=%s object_path=%s uid=%u runtime_path=%s "
779 "session_fd=%d seat=%s vtnr=%u",
5a330cda
ZJS
780 s->id,
781 p,
236af516 782 (uint32_t) s->user->uid,
5a330cda
ZJS
783 s->user->runtime_path,
784 fifo_fd,
785 s->seat ? s->seat->id : "",
786 (uint32_t) s->vtnr);
787
cc377381 788 return sd_bus_reply_method_return(
baae0358 789 c, "soshusub",
cc377381
LP
790 s->id,
791 p,
792 s->user->runtime_path,
793 fifo_fd,
baae0358 794 (uint32_t) s->user->uid,
cc377381
LP
795 s->seat ? s->seat->id : "",
796 (uint32_t) s->vtnr,
797 false);
fb6becb4 798}