1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "bus-common-errors.h"
8 #include "bus-polkit.h"
10 #include "logind-dbus.h"
11 #include "logind-seat-dbus.h"
12 #include "logind-seat.h"
13 #include "logind-session-dbus.h"
15 #include "missing_capability.h"
17 #include "user-util.h"
20 static BUS_DEFINE_PROPERTY_GET(property_get_can_multi_session
, "b", Seat
, seat_can_multi_session
);
21 static BUS_DEFINE_PROPERTY_GET(property_get_can_tty
, "b", Seat
, seat_can_tty
);
22 static BUS_DEFINE_PROPERTY_GET(property_get_can_graphical
, "b", Seat
, seat_can_graphical
);
24 static int property_get_active_session(
27 const char *interface
,
29 sd_bus_message
*reply
,
31 sd_bus_error
*error
) {
33 _cleanup_free_
char *p
= NULL
;
40 p
= s
->active
? session_bus_path(s
->active
) : strdup("/");
44 return sd_bus_message_append(reply
, "(so)", s
->active
? s
->active
->id
: "", p
);
47 static int property_get_sessions(
50 const char *interface
,
52 sd_bus_message
*reply
,
54 sd_bus_error
*error
) {
64 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
68 LIST_FOREACH(sessions_by_seat
, session
, s
->sessions
) {
69 _cleanup_free_
char *p
= NULL
;
71 p
= session_bus_path(session
);
75 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
81 r
= sd_bus_message_close_container(reply
);
88 static int property_get_idle_hint(
91 const char *interface
,
93 sd_bus_message
*reply
,
95 sd_bus_error
*error
) {
103 return sd_bus_message_append(reply
, "b", seat_get_idle_hint(s
, NULL
) > 0);
106 static int property_get_idle_since_hint(
109 const char *interface
,
110 const char *property
,
111 sd_bus_message
*reply
,
113 sd_bus_error
*error
) {
124 r
= seat_get_idle_hint(s
, &t
);
128 u
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
130 return sd_bus_message_append(reply
, "t", u
);
133 int bus_seat_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
140 r
= bus_verify_polkit_async(
143 "org.freedesktop.login1.manage",
147 &s
->manager
->polkit_registry
,
152 return 1; /* Will call us back */
154 r
= seat_stop_sessions(s
, true);
158 return sd_bus_reply_method_return(message
, NULL
);
161 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
170 r
= sd_bus_message_read(message
, "s", &name
);
174 session
= hashmap_get(s
->manager
->sessions
, name
);
176 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
178 if (session
->seat
!= s
)
179 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
, "Session %s not on seat %s", name
, s
->id
);
181 r
= session_activate(session
);
185 return sd_bus_reply_method_return(message
, NULL
);
188 static int method_switch_to(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
196 r
= sd_bus_message_read(message
, "u", &to
);
203 r
= seat_switch_to(s
, to
);
207 return sd_bus_reply_method_return(message
, NULL
);
210 static int method_switch_to_next(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
217 r
= seat_switch_to_next(s
);
221 return sd_bus_reply_method_return(message
, NULL
);
224 static int method_switch_to_previous(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
231 r
= seat_switch_to_previous(s
);
235 return sd_bus_reply_method_return(message
, NULL
);
238 const sd_bus_vtable seat_vtable
[] = {
239 SD_BUS_VTABLE_START(0),
241 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Seat
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
242 SD_BUS_PROPERTY("ActiveSession", "(so)", property_get_active_session
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
243 SD_BUS_PROPERTY("CanMultiSession", "b", property_get_can_multi_session
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
244 SD_BUS_PROPERTY("CanTTY", "b", property_get_can_tty
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
245 SD_BUS_PROPERTY("CanGraphical", "b", property_get_can_graphical
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
246 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
247 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
248 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
249 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
251 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_seat_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
252 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
253 SD_BUS_METHOD("SwitchTo", "u", NULL
, method_switch_to
, SD_BUS_VTABLE_UNPRIVILEGED
),
254 SD_BUS_METHOD("SwitchToNext", NULL
, NULL
, method_switch_to_next
, SD_BUS_VTABLE_UNPRIVILEGED
),
255 SD_BUS_METHOD("SwitchToPrevious", NULL
, NULL
, method_switch_to_previous
, SD_BUS_VTABLE_UNPRIVILEGED
),
260 int seat_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
261 _cleanup_free_
char *e
= NULL
;
262 sd_bus_message
*message
;
263 Manager
*m
= userdata
;
274 p
= startswith(path
, "/org/freedesktop/login1/seat/");
278 e
= bus_label_unescape(p
);
282 message
= sd_bus_get_current_message(bus
);
284 r
= manager_get_seat_from_creds(m
, message
, e
, error
, &seat
);
286 sd_bus_error_free(error
);
296 char *seat_bus_path(Seat
*s
) {
297 _cleanup_free_
char *t
= NULL
;
301 t
= bus_label_escape(s
->id
);
305 return strjoin("/org/freedesktop/login1/seat/", t
);
308 int seat_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
309 _cleanup_strv_free_
char **l
= NULL
;
310 sd_bus_message
*message
;
311 Manager
*m
= userdata
;
320 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
323 p
= seat_bus_path(seat
);
327 r
= strv_consume(&l
, p
);
332 message
= sd_bus_get_current_message(bus
);
334 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
336 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
338 bool may_auto
= false;
341 r
= sd_bus_creds_get_session(creds
, &name
);
345 session
= hashmap_get(m
->sessions
, name
);
346 if (session
&& session
->seat
) {
347 r
= strv_extend(&l
, "/org/freedesktop/login1/seat/self");
358 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
362 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
363 may_auto
= user
&& user
->display
&& user
->display
->seat
;
368 r
= strv_extend(&l
, "/org/freedesktop/login1/seat/auto");
375 *nodes
= TAKE_PTR(l
);
379 int seat_send_signal(Seat
*s
, bool new_seat
) {
380 _cleanup_free_
char *p
= NULL
;
384 p
= seat_bus_path(s
);
388 return sd_bus_emit_signal(
390 "/org/freedesktop/login1",
391 "org.freedesktop.login1.Manager",
392 new_seat
? "SeatNew" : "SeatRemoved",
396 int seat_send_changed(Seat
*s
, const char *properties
, ...) {
397 _cleanup_free_
char *p
= NULL
;
405 p
= seat_bus_path(s
);
409 l
= strv_from_stdarg_alloca(properties
);
411 return sd_bus_emit_properties_changed_strv(s
->manager
->bus
, p
, "org.freedesktop.login1.Seat", l
);