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