]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-session-dbus.c
Merge pull request #7301 from poettering/loginctl-ellipsize
[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
dc6284e9 399 r = session_set_controller(s, sd_bus_message_get_sender(message), force, true);
cc377381 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 446
aed24c4c 447 r = session_device_new(s, dev, true, &sd);
cc377381 448 if (r < 0)
ebcf1f97 449 return r;
de07ab16 450
aed24c4c
FB
451 r = session_device_save(sd);
452 if (r < 0)
453 goto error;
454
df2d202e 455 r = sd_bus_reply_method_return(message, "hb", sd->fd, !sd->active);
cc377381 456 if (r < 0)
aed24c4c
FB
457 goto error;
458
459 session_save(s);
2e681921 460 return 1;
118ecf32 461
aed24c4c
FB
462error:
463 session_device_free(sd);
cc377381
LP
464 return r;
465}
118ecf32 466
19070062 467static int method_release_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
468 Session *s = userdata;
469 uint32_t major, minor;
470 SessionDevice *sd;
471 dev_t dev;
472 int r;
118ecf32 473
cc377381
LP
474 assert(message);
475 assert(s);
118ecf32 476
cc377381
LP
477 r = sd_bus_message_read(message, "uu", &major, &minor);
478 if (r < 0)
ebcf1f97 479 return r;
118ecf32 480
cc377381 481 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
ebcf1f97 482 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
118ecf32 483
cc377381
LP
484 dev = makedev(major, minor);
485 sd = hashmap_get(s->devices, &dev);
486 if (!sd)
ebcf1f97 487 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
118ecf32 488
cc377381 489 session_device_free(sd);
aed24c4c
FB
490 session_save(s);
491
df2d202e 492 return sd_bus_reply_method_return(message, NULL);
cc377381 493}
118ecf32 494
19070062 495static int method_pause_device_complete(sd_bus_message *message, void *userdata, sd_bus_error *error) {
cc377381
LP
496 Session *s = userdata;
497 uint32_t major, minor;
498 SessionDevice *sd;
499 dev_t dev;
500 int r;
118ecf32 501
cc377381
LP
502 assert(message);
503 assert(s);
bef422ae 504
cc377381
LP
505 r = sd_bus_message_read(message, "uu", &major, &minor);
506 if (r < 0)
ebcf1f97 507 return r;
cc377381
LP
508
509 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
ebcf1f97 510 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
bef422ae 511
cc377381
LP
512 dev = makedev(major, minor);
513 sd = hashmap_get(s->devices, &dev);
514 if (!sd)
ebcf1f97 515 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
bef422ae 516
cc377381 517 session_device_complete_pause(sd);
bef422ae 518
df2d202e 519 return sd_bus_reply_method_return(message, NULL);
3f49d45a
LP
520}
521
cc377381
LP
522const sd_bus_vtable session_vtable[] = {
523 SD_BUS_VTABLE_START(0),
524
556089dc
LP
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),
a4cd87e9 537 SD_BUS_PROPERTY("Desktop", "s", NULL, offsetof(Session, desktop), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
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),
cc377381
LP
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),
42d35e13 548 SD_BUS_PROPERTY("LockedHint", "b", property_get_locked_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
cc377381 549
c529695e
LP
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),
adacb957 554 SD_BUS_METHOD("SetIdleHint", "b", NULL, method_set_idle_hint, SD_BUS_VTABLE_UNPRIVILEGED),
42d35e13 555 SD_BUS_METHOD("SetLockedHint", "b", NULL, method_set_locked_hint, SD_BUS_VTABLE_UNPRIVILEGED),
c529695e 556 SD_BUS_METHOD("Kill", "si", NULL, bus_session_method_kill, SD_BUS_VTABLE_UNPRIVILEGED),
adacb957
LP
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),
cc377381
LP
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};
3f49d45a 570
f00c3121 571int session_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
3f49d45a 572 Manager *m = userdata;
cc377381 573 Session *session;
927b1649 574 int r;
3f49d45a 575
cc377381
LP
576 assert(bus);
577 assert(path);
578 assert(interface);
579 assert(found);
580 assert(m);
3f49d45a 581
927b1649 582 if (streq(path, "/org/freedesktop/login1/session/self")) {
4afd3348 583 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
927b1649 584 sd_bus_message *message;
309a29df 585 const char *name;
3f49d45a 586
19befb2d 587 message = sd_bus_get_current_message(bus);
927b1649
LP
588 if (!message)
589 return 0;
590
309a29df 591 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
927b1649 592 if (r < 0)
5b12334d
LP
593 return r;
594
309a29df 595 r = sd_bus_creds_get_session(creds, &name);
5b12334d
LP
596 if (r < 0)
597 return r;
927b1649 598
309a29df 599 session = hashmap_get(m->sessions, name);
927b1649
LP
600 } else {
601 _cleanup_free_ char *e = NULL;
602 const char *p;
3f49d45a 603
927b1649
LP
604 p = startswith(path, "/org/freedesktop/login1/session/");
605 if (!p)
606 return 0;
607
a6278b88 608 e = bus_label_unescape(p);
927b1649
LP
609 if (!e)
610 return -ENOMEM;
611
612 session = hashmap_get(m->sessions, e);
927b1649 613 }
3f49d45a 614
309a29df
LP
615 if (!session)
616 return 0;
617
cc377381
LP
618 *found = session;
619 return 1;
3f49d45a
LP
620}
621
3f49d45a 622char *session_bus_path(Session *s) {
9444b1f2 623 _cleanup_free_ char *t = NULL;
3f49d45a
LP
624
625 assert(s);
626
a6278b88 627 t = bus_label_escape(s->id);
3f49d45a
LP
628 if (!t)
629 return NULL;
630
4654e558 631 return strappend("/org/freedesktop/login1/session/", t);
3f49d45a 632}
da119395 633
f00c3121 634int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
cc377381 635 _cleanup_strv_free_ char **l = NULL;
ca56b0a6 636 sd_bus_message *message;
cc377381
LP
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
6e18964d
ZJS
653 r = strv_consume(&l, p);
654 if (r < 0)
cc377381 655 return r;
cc377381
LP
656 }
657
ca56b0a6
DH
658 message = sd_bus_get_current_message(bus);
659 if (message) {
4afd3348 660 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
ca56b0a6
DH
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 }
b298e984 676
cc377381
LP
677 *nodes = l;
678 l = NULL;
679
680 return 1;
681}
682
da119395 683int session_send_signal(Session *s, bool new_session) {
ce0fc5f5 684 _cleanup_free_ char *p = NULL;
da119395
LP
685
686 assert(s);
687
da119395
LP
688 p = session_bus_path(s);
689 if (!p)
4654e558 690 return -ENOMEM;
da119395 691
cc377381
LP
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);
da119395 698}
9418f147 699
cc377381 700int session_send_changed(Session *s, const char *properties, ...) {
ce0fc5f5 701 _cleanup_free_ char *p = NULL;
cc377381 702 char **l;
9418f147
LP
703
704 assert(s);
705
ed18b08b
LP
706 if (!s->started)
707 return 0;
708
9418f147
LP
709 p = session_bus_path(s);
710 if (!p)
711 return -ENOMEM;
712
cc377381 713 l = strv_from_stdarg_alloca(properties);
9418f147 714
cc377381 715 return sd_bus_emit_properties_changed_strv(s->manager->bus, p, "org.freedesktop.login1.Session", l);
9418f147 716}
88e3dc90
LP
717
718int session_send_lock(Session *s, bool lock) {
ce0fc5f5 719 _cleanup_free_ char *p = NULL;
88e3dc90
LP
720
721 assert(s);
722
723 p = session_bus_path(s);
724 if (!p)
725 return -ENOMEM;
726
cc377381
LP
727 return sd_bus_emit_signal(
728 s->manager->bus,
729 p,
730 "org.freedesktop.login1.Session",
731 lock ? "Lock" : "Unlock",
732 NULL);
88e3dc90 733}
7ba64386
LP
734
735int 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}
fb6becb4 752
cc377381 753int session_send_create_reply(Session *s, sd_bus_error *error) {
4afd3348 754 _cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL;
cc377381
LP
755 _cleanup_close_ int fifo_fd = -1;
756 _cleanup_free_ char *p = NULL;
fb6becb4
LP
757
758 assert(s);
759
dd9b67aa
LP
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. */
cba38758 763
cc377381
LP
764 if (!s->create_message)
765 return 0;
fb6becb4 766
dd9b67aa
LP
767 if (!sd_bus_error_is_set(error) && (s->scope_job || s->user->service_job))
768 return 0;
769
cc377381
LP
770 c = s->create_message;
771 s->create_message = NULL;
fb6becb4 772
cc377381 773 if (error)
df2d202e 774 return sd_bus_reply_method_error(c, error);
fb6becb4 775
cc377381
LP
776 fifo_fd = session_create_fifo(s);
777 if (fifo_fd < 0)
778 return fifo_fd;
fb6becb4 779
38fdcbed
TA
780 /* Update the session state file before we notify the client
781 * about the result. */
782 session_save(s);
783
cc377381
LP
784 p = session_bus_path(s);
785 if (!p)
786 return -ENOMEM;
fb6becb4 787
5a330cda 788 log_debug("Sending reply about created session: "
236af516
DH
789 "id=%s object_path=%s uid=%u runtime_path=%s "
790 "session_fd=%d seat=%s vtnr=%u",
5a330cda
ZJS
791 s->id,
792 p,
236af516 793 (uint32_t) s->user->uid,
5a330cda
ZJS
794 s->user->runtime_path,
795 fifo_fd,
796 s->seat ? s->seat->id : "",
797 (uint32_t) s->vtnr);
798
cc377381 799 return sd_bus_reply_method_return(
baae0358 800 c, "soshusub",
cc377381
LP
801 s->id,
802 p,
803 s->user->runtime_path,
804 fifo_fd,
baae0358 805 (uint32_t) s->user->uid,
cc377381
LP
806 s->seat ? s->seat->id : "",
807 (uint32_t) s->vtnr,
808 false);
fb6becb4 809}