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(
137 "org.freedesktop.login1.manage",
139 &s
->manager
->polkit_registry
,
144 return 1; /* Will call us back */
146 r
= seat_stop_sessions(s
, /* force = */ true);
150 return sd_bus_reply_method_return(message
, NULL
);
153 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
154 Seat
*s
= ASSERT_PTR(userdata
);
161 r
= sd_bus_message_read(message
, "s", &name
);
165 session
= hashmap_get(s
->manager
->sessions
, name
);
167 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
169 if (session
->seat
!= s
)
170 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
, "Session %s not on seat %s", name
, s
->id
);
172 r
= check_polkit_chvt(message
, s
->manager
, error
);
176 return 1; /* Will call us back */
178 r
= session_activate(session
);
182 return sd_bus_reply_method_return(message
, NULL
);
185 static int method_switch_to(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
186 Seat
*s
= ASSERT_PTR(userdata
);
192 r
= sd_bus_message_read(message
, "u", &to
);
197 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid virtual terminal");
199 r
= check_polkit_chvt(message
, s
->manager
, error
);
203 return 1; /* Will call us back */
205 r
= seat_switch_to(s
, to
);
209 return sd_bus_reply_method_return(message
, NULL
);
212 static int method_switch_to_next(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
213 Seat
*s
= ASSERT_PTR(userdata
);
218 r
= check_polkit_chvt(message
, s
->manager
, error
);
222 return 1; /* Will call us back */
224 r
= seat_switch_to_next(s
);
228 return sd_bus_reply_method_return(message
, NULL
);
231 static int method_switch_to_previous(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
232 Seat
*s
= ASSERT_PTR(userdata
);
237 r
= check_polkit_chvt(message
, s
->manager
, error
);
241 return 1; /* Will call us back */
243 r
= seat_switch_to_previous(s
);
247 return sd_bus_reply_method_return(message
, NULL
);
250 static int seat_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
251 _cleanup_free_
char *e
= NULL
;
252 sd_bus_message
*message
;
253 Manager
*m
= ASSERT_PTR(userdata
);
263 p
= startswith(path
, "/org/freedesktop/login1/seat/");
267 e
= bus_label_unescape(p
);
271 message
= sd_bus_get_current_message(bus
);
273 r
= manager_get_seat_from_creds(m
, message
, e
, error
, &seat
);
275 sd_bus_error_free(error
);
285 char *seat_bus_path(Seat
*s
) {
286 _cleanup_free_
char *t
= NULL
;
290 t
= bus_label_escape(s
->id
);
294 return strjoin("/org/freedesktop/login1/seat/", t
);
297 static int seat_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
298 _cleanup_strv_free_
char **l
= NULL
;
299 sd_bus_message
*message
;
300 Manager
*m
= userdata
;
308 HASHMAP_FOREACH(seat
, m
->seats
) {
311 p
= seat_bus_path(seat
);
315 r
= strv_consume(&l
, p
);
320 message
= sd_bus_get_current_message(bus
);
322 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
324 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
326 bool may_auto
= false;
329 r
= sd_bus_creds_get_session(creds
, &name
);
333 session
= hashmap_get(m
->sessions
, name
);
334 if (session
&& session
->seat
) {
335 r
= strv_extend(&l
, "/org/freedesktop/login1/seat/self");
346 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
350 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
351 may_auto
= user
&& user
->display
&& user
->display
->seat
;
356 r
= strv_extend(&l
, "/org/freedesktop/login1/seat/auto");
363 *nodes
= TAKE_PTR(l
);
367 int seat_send_signal(Seat
*s
, bool new_seat
) {
368 _cleanup_free_
char *p
= NULL
;
372 p
= seat_bus_path(s
);
376 return sd_bus_emit_signal(
378 "/org/freedesktop/login1",
379 "org.freedesktop.login1.Manager",
380 new_seat
? "SeatNew" : "SeatRemoved",
384 int seat_send_changed(Seat
*s
, const char *properties
, ...) {
385 _cleanup_free_
char *p
= NULL
;
393 p
= seat_bus_path(s
);
397 l
= strv_from_stdarg_alloca(properties
);
399 return sd_bus_emit_properties_changed_strv(s
->manager
->bus
, p
, "org.freedesktop.login1.Seat", l
);
402 static const sd_bus_vtable seat_vtable
[] = {
403 SD_BUS_VTABLE_START(0),
405 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Seat
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
406 SD_BUS_PROPERTY("ActiveSession", "(so)", property_get_active_session
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
407 SD_BUS_PROPERTY("CanMultiSession", "b", property_get_const_true
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
408 SD_BUS_PROPERTY("CanTTY", "b", property_get_can_tty
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
409 SD_BUS_PROPERTY("CanGraphical", "b", property_get_can_graphical
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
410 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
411 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
412 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
413 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
415 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_seat_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
417 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
418 SD_BUS_ARGS("s", session_id
),
420 method_activate_session
,
421 SD_BUS_VTABLE_UNPRIVILEGED
),
422 SD_BUS_METHOD_WITH_ARGS("SwitchTo",
423 SD_BUS_ARGS("u", vtnr
),
426 SD_BUS_VTABLE_UNPRIVILEGED
),
428 SD_BUS_METHOD("SwitchToNext", NULL
, NULL
, method_switch_to_next
, SD_BUS_VTABLE_UNPRIVILEGED
),
429 SD_BUS_METHOD("SwitchToPrevious", NULL
, NULL
, method_switch_to_previous
, SD_BUS_VTABLE_UNPRIVILEGED
),
434 const BusObjectImplementation seat_object
= {
435 "/org/freedesktop/login1/seat",
436 "org.freedesktop.login1.Seat",
437 .fallback_vtables
= BUS_FALLBACK_VTABLES({seat_vtable
, seat_object_find
}),
438 .node_enumerator
= seat_node_enumerator
,