1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "bus-common-errors.h"
7 #include "bus-get-properties.h"
9 #include "bus-polkit.h"
11 #include "logind-dbus.h"
12 #include "logind-polkit.h"
13 #include "logind-seat-dbus.h"
14 #include "logind-seat.h"
15 #include "logind-session-dbus.h"
17 #include "missing_capability.h"
19 #include "user-util.h"
21 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_const_true
, "b", true);
22 static BUS_DEFINE_PROPERTY_GET(property_get_can_tty
, "b", Seat
, seat_can_tty
);
23 static BUS_DEFINE_PROPERTY_GET(property_get_can_graphical
, "b", Seat
, seat_can_graphical
);
25 static int property_get_active_session(
28 const char *interface
,
30 sd_bus_message
*reply
,
32 sd_bus_error
*error
) {
34 _cleanup_free_
char *p
= NULL
;
35 Seat
*s
= ASSERT_PTR(userdata
);
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
) {
56 Seat
*s
= ASSERT_PTR(userdata
);
62 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
66 LIST_FOREACH(sessions_by_seat
, session
, s
->sessions
) {
67 _cleanup_free_
char *p
= NULL
;
69 p
= session_bus_path(session
);
73 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
79 r
= sd_bus_message_close_container(reply
);
86 static int property_get_idle_hint(
89 const char *interface
,
91 sd_bus_message
*reply
,
93 sd_bus_error
*error
) {
95 Seat
*s
= ASSERT_PTR(userdata
);
100 return sd_bus_message_append(reply
, "b", seat_get_idle_hint(s
, NULL
) > 0);
103 static int property_get_idle_since_hint(
106 const char *interface
,
107 const char *property
,
108 sd_bus_message
*reply
,
110 sd_bus_error
*error
) {
112 Seat
*s
= ASSERT_PTR(userdata
);
120 r
= seat_get_idle_hint(s
, &t
);
124 u
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
126 return sd_bus_message_append(reply
, "t", u
);
129 int bus_seat_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
130 Seat
*s
= ASSERT_PTR(userdata
);
135 r
= bus_verify_polkit_async(
138 "org.freedesktop.login1.manage",
142 &s
->manager
->polkit_registry
,
147 return 1; /* Will call us back */
149 r
= seat_stop_sessions(s
, /* force = */ true);
153 return sd_bus_reply_method_return(message
, NULL
);
156 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
157 Seat
*s
= ASSERT_PTR(userdata
);
164 r
= sd_bus_message_read(message
, "s", &name
);
168 session
= hashmap_get(s
->manager
->sessions
, name
);
170 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
172 if (session
->seat
!= s
)
173 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
, "Session %s not on seat %s", name
, s
->id
);
175 r
= check_polkit_chvt(message
, s
->manager
, error
);
179 return 1; /* Will call us back */
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
) {
189 Seat
*s
= ASSERT_PTR(userdata
);
195 r
= sd_bus_message_read(message
, "u", &to
);
200 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid virtual terminal");
202 r
= check_polkit_chvt(message
, s
->manager
, error
);
206 return 1; /* Will call us back */
208 r
= seat_switch_to(s
, to
);
212 return sd_bus_reply_method_return(message
, NULL
);
215 static int method_switch_to_next(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
216 Seat
*s
= ASSERT_PTR(userdata
);
221 r
= check_polkit_chvt(message
, s
->manager
, error
);
225 return 1; /* Will call us back */
227 r
= seat_switch_to_next(s
);
231 return sd_bus_reply_method_return(message
, NULL
);
234 static int method_switch_to_previous(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
235 Seat
*s
= ASSERT_PTR(userdata
);
240 r
= check_polkit_chvt(message
, s
->manager
, error
);
244 return 1; /* Will call us back */
246 r
= seat_switch_to_previous(s
);
250 return sd_bus_reply_method_return(message
, NULL
);
253 static int seat_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
254 _cleanup_free_
char *e
= NULL
;
255 sd_bus_message
*message
;
256 Manager
*m
= ASSERT_PTR(userdata
);
266 p
= startswith(path
, "/org/freedesktop/login1/seat/");
270 e
= bus_label_unescape(p
);
274 message
= sd_bus_get_current_message(bus
);
276 r
= manager_get_seat_from_creds(m
, message
, e
, error
, &seat
);
278 sd_bus_error_free(error
);
288 char *seat_bus_path(Seat
*s
) {
289 _cleanup_free_
char *t
= NULL
;
293 t
= bus_label_escape(s
->id
);
297 return strjoin("/org/freedesktop/login1/seat/", t
);
300 static int seat_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
301 _cleanup_strv_free_
char **l
= NULL
;
302 sd_bus_message
*message
;
303 Manager
*m
= userdata
;
311 HASHMAP_FOREACH(seat
, m
->seats
) {
314 p
= seat_bus_path(seat
);
318 r
= strv_consume(&l
, p
);
323 message
= sd_bus_get_current_message(bus
);
325 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
327 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
329 bool may_auto
= false;
332 r
= sd_bus_creds_get_session(creds
, &name
);
336 session
= hashmap_get(m
->sessions
, name
);
337 if (session
&& session
->seat
) {
338 r
= strv_extend(&l
, "/org/freedesktop/login1/seat/self");
349 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
353 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
354 may_auto
= user
&& user
->display
&& user
->display
->seat
;
359 r
= strv_extend(&l
, "/org/freedesktop/login1/seat/auto");
366 *nodes
= TAKE_PTR(l
);
370 int seat_send_signal(Seat
*s
, bool new_seat
) {
371 _cleanup_free_
char *p
= NULL
;
375 p
= seat_bus_path(s
);
379 return sd_bus_emit_signal(
381 "/org/freedesktop/login1",
382 "org.freedesktop.login1.Manager",
383 new_seat
? "SeatNew" : "SeatRemoved",
387 int seat_send_changed(Seat
*s
, const char *properties
, ...) {
388 _cleanup_free_
char *p
= NULL
;
396 p
= seat_bus_path(s
);
400 l
= strv_from_stdarg_alloca(properties
);
402 return sd_bus_emit_properties_changed_strv(s
->manager
->bus
, p
, "org.freedesktop.login1.Seat", l
);
405 static const sd_bus_vtable seat_vtable
[] = {
406 SD_BUS_VTABLE_START(0),
408 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Seat
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
409 SD_BUS_PROPERTY("ActiveSession", "(so)", property_get_active_session
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
410 SD_BUS_PROPERTY("CanMultiSession", "b", property_get_const_true
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
411 SD_BUS_PROPERTY("CanTTY", "b", property_get_can_tty
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
412 SD_BUS_PROPERTY("CanGraphical", "b", property_get_can_graphical
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
413 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
414 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
415 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
416 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
418 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_seat_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
420 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
421 SD_BUS_ARGS("s", session_id
),
423 method_activate_session
,
424 SD_BUS_VTABLE_UNPRIVILEGED
),
425 SD_BUS_METHOD_WITH_ARGS("SwitchTo",
426 SD_BUS_ARGS("u", vtnr
),
429 SD_BUS_VTABLE_UNPRIVILEGED
),
431 SD_BUS_METHOD("SwitchToNext", NULL
, NULL
, method_switch_to_next
, SD_BUS_VTABLE_UNPRIVILEGED
),
432 SD_BUS_METHOD("SwitchToPrevious", NULL
, NULL
, method_switch_to_previous
, SD_BUS_VTABLE_UNPRIVILEGED
),
437 const BusObjectImplementation seat_object
= {
438 "/org/freedesktop/login1/seat",
439 "org.freedesktop.login1.Seat",
440 .fallback_vtables
= BUS_FALLBACK_VTABLES({seat_vtable
, seat_object_find
}),
441 .node_enumerator
= seat_node_enumerator
,