1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
11 #include "alloc-util.h"
12 #include "bus-common-errors.h"
13 #include "bus-label.h"
15 #include "logind-seat.h"
18 #include "user-util.h"
21 static int property_get_active_session(
24 const char *interface
,
26 sd_bus_message
*reply
,
28 sd_bus_error
*error
) {
30 _cleanup_free_
char *p
= NULL
;
37 p
= s
->active
? session_bus_path(s
->active
) : strdup("/");
41 return sd_bus_message_append(reply
, "(so)", s
->active
? s
->active
->id
: "", p
);
44 static int property_get_can_multi_session(
47 const char *interface
,
49 sd_bus_message
*reply
,
51 sd_bus_error
*error
) {
59 return sd_bus_message_append(reply
, "b", seat_can_multi_session(s
));
62 static int property_get_can_tty(
65 const char *interface
,
67 sd_bus_message
*reply
,
69 sd_bus_error
*error
) {
77 return sd_bus_message_append(reply
, "b", seat_can_tty(s
));
80 static int property_get_can_graphical(
83 const char *interface
,
85 sd_bus_message
*reply
,
87 sd_bus_error
*error
) {
95 return sd_bus_message_append(reply
, "b", seat_can_graphical(s
));
98 static int property_get_sessions(
101 const char *interface
,
102 const char *property
,
103 sd_bus_message
*reply
,
105 sd_bus_error
*error
) {
115 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
119 LIST_FOREACH(sessions_by_seat
, session
, s
->sessions
) {
120 _cleanup_free_
char *p
= NULL
;
122 p
= session_bus_path(session
);
126 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
132 r
= sd_bus_message_close_container(reply
);
139 static int property_get_idle_hint(
142 const char *interface
,
143 const char *property
,
144 sd_bus_message
*reply
,
146 sd_bus_error
*error
) {
154 return sd_bus_message_append(reply
, "b", seat_get_idle_hint(s
, NULL
) > 0);
157 static int property_get_idle_since_hint(
160 const char *interface
,
161 const char *property
,
162 sd_bus_message
*reply
,
164 sd_bus_error
*error
) {
175 r
= seat_get_idle_hint(s
, &t
);
179 u
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
181 return sd_bus_message_append(reply
, "t", u
);
184 int bus_seat_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
191 r
= bus_verify_polkit_async(
194 "org.freedesktop.login1.manage",
198 &s
->manager
->polkit_registry
,
203 return 1; /* Will call us back */
205 r
= seat_stop_sessions(s
, true);
209 return sd_bus_reply_method_return(message
, NULL
);
212 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
221 r
= sd_bus_message_read(message
, "s", &name
);
225 session
= hashmap_get(s
->manager
->sessions
, name
);
227 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
229 if (session
->seat
!= s
)
230 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
, "Session %s not on seat %s", name
, s
->id
);
232 r
= session_activate(session
);
236 return sd_bus_reply_method_return(message
, NULL
);
239 static int method_switch_to(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
247 r
= sd_bus_message_read(message
, "u", &to
);
254 r
= seat_switch_to(s
, to
);
258 return sd_bus_reply_method_return(message
, NULL
);
261 static int method_switch_to_next(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
268 r
= seat_switch_to_next(s
);
272 return sd_bus_reply_method_return(message
, NULL
);
275 static int method_switch_to_previous(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
282 r
= seat_switch_to_previous(s
);
286 return sd_bus_reply_method_return(message
, NULL
);
289 const sd_bus_vtable seat_vtable
[] = {
290 SD_BUS_VTABLE_START(0),
292 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Seat
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
293 SD_BUS_PROPERTY("ActiveSession", "(so)", property_get_active_session
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
294 SD_BUS_PROPERTY("CanMultiSession", "b", property_get_can_multi_session
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
295 SD_BUS_PROPERTY("CanTTY", "b", property_get_can_tty
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
296 SD_BUS_PROPERTY("CanGraphical", "b", property_get_can_graphical
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
297 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
298 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
299 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
300 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
302 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_seat_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
303 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
304 SD_BUS_METHOD("SwitchTo", "u", NULL
, method_switch_to
, SD_BUS_VTABLE_UNPRIVILEGED
),
305 SD_BUS_METHOD("SwitchToNext", NULL
, NULL
, method_switch_to_next
, SD_BUS_VTABLE_UNPRIVILEGED
),
306 SD_BUS_METHOD("SwitchToPrevious", NULL
, NULL
, method_switch_to_previous
, SD_BUS_VTABLE_UNPRIVILEGED
),
311 int seat_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
312 Manager
*m
= userdata
;
322 if (streq(path
, "/org/freedesktop/login1/seat/self")) {
323 sd_bus_message
*message
;
325 message
= sd_bus_get_current_message(bus
);
329 r
= manager_get_seat_from_creds(m
, message
, NULL
, error
, &seat
);
333 _cleanup_free_
char *e
= NULL
;
336 p
= startswith(path
, "/org/freedesktop/login1/seat/");
340 e
= bus_label_unescape(p
);
344 seat
= hashmap_get(m
->seats
, e
);
353 char *seat_bus_path(Seat
*s
) {
354 _cleanup_free_
char *t
= NULL
;
358 t
= bus_label_escape(s
->id
);
362 return strappend("/org/freedesktop/login1/seat/", t
);
365 int seat_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
366 _cleanup_strv_free_
char **l
= NULL
;
367 sd_bus_message
*message
;
368 Manager
*m
= userdata
;
377 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
380 p
= seat_bus_path(seat
);
384 r
= strv_consume(&l
, p
);
389 message
= sd_bus_get_current_message(bus
);
391 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
395 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
, &creds
);
397 r
= sd_bus_creds_get_session(creds
, &name
);
399 session
= hashmap_get(m
->sessions
, name
);
400 if (session
&& session
->seat
) {
401 r
= strv_extend(&l
, "/org/freedesktop/login1/seat/self");
409 *nodes
= TAKE_PTR(l
);
414 int seat_send_signal(Seat
*s
, bool new_seat
) {
415 _cleanup_free_
char *p
= NULL
;
419 p
= seat_bus_path(s
);
423 return sd_bus_emit_signal(
425 "/org/freedesktop/login1",
426 "org.freedesktop.login1.Manager",
427 new_seat
? "SeatNew" : "SeatRemoved",
431 int seat_send_changed(Seat
*s
, const char *properties
, ...) {
432 _cleanup_free_
char *p
= NULL
;
440 p
= seat_bus_path(s
);
444 l
= strv_from_stdarg_alloca(properties
);
446 return sd_bus_emit_properties_changed_strv(s
->manager
->bus
, p
, "org.freedesktop.login1.Seat", l
);