2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
24 #include "bus-common-errors.h"
25 #include "bus-label.h"
27 #include "logind-seat.h"
30 #include "user-util.h"
33 static int property_get_active_session(
36 const char *interface
,
38 sd_bus_message
*reply
,
40 sd_bus_error
*error
) {
42 _cleanup_free_
char *p
= NULL
;
49 p
= s
->active
? session_bus_path(s
->active
) : strdup("/");
53 return sd_bus_message_append(reply
, "(so)", s
->active
? s
->active
->id
: "", p
);
56 static int property_get_can_multi_session(
59 const char *interface
,
61 sd_bus_message
*reply
,
63 sd_bus_error
*error
) {
71 return sd_bus_message_append(reply
, "b", seat_can_multi_session(s
));
74 static int property_get_can_tty(
77 const char *interface
,
79 sd_bus_message
*reply
,
81 sd_bus_error
*error
) {
89 return sd_bus_message_append(reply
, "b", seat_can_tty(s
));
92 static int property_get_can_graphical(
95 const char *interface
,
97 sd_bus_message
*reply
,
99 sd_bus_error
*error
) {
107 return sd_bus_message_append(reply
, "b", seat_can_graphical(s
));
110 static int property_get_sessions(
113 const char *interface
,
114 const char *property
,
115 sd_bus_message
*reply
,
117 sd_bus_error
*error
) {
127 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
131 LIST_FOREACH(sessions_by_seat
, session
, s
->sessions
) {
132 _cleanup_free_
char *p
= NULL
;
134 p
= session_bus_path(session
);
138 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
144 r
= sd_bus_message_close_container(reply
);
151 static int property_get_idle_hint(
154 const char *interface
,
155 const char *property
,
156 sd_bus_message
*reply
,
158 sd_bus_error
*error
) {
166 return sd_bus_message_append(reply
, "b", seat_get_idle_hint(s
, NULL
) > 0);
169 static int property_get_idle_since_hint(
172 const char *interface
,
173 const char *property
,
174 sd_bus_message
*reply
,
176 sd_bus_error
*error
) {
187 r
= seat_get_idle_hint(s
, &t
);
191 u
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
193 return sd_bus_message_append(reply
, "t", u
);
196 int bus_seat_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
203 r
= bus_verify_polkit_async(
206 "org.freedesktop.login1.manage",
210 &s
->manager
->polkit_registry
,
215 return 1; /* Will call us back */
217 r
= seat_stop_sessions(s
, true);
221 return sd_bus_reply_method_return(message
, NULL
);
224 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
233 r
= sd_bus_message_read(message
, "s", &name
);
237 session
= hashmap_get(s
->manager
->sessions
, name
);
239 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
241 if (session
->seat
!= s
)
242 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
, "Session %s not on seat %s", name
, s
->id
);
244 r
= session_activate(session
);
248 return sd_bus_reply_method_return(message
, NULL
);
251 static int method_switch_to(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
259 r
= sd_bus_message_read(message
, "u", &to
);
266 r
= seat_switch_to(s
, to
);
270 return sd_bus_reply_method_return(message
, NULL
);
273 static int method_switch_to_next(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
280 r
= seat_switch_to_next(s
);
284 return sd_bus_reply_method_return(message
, NULL
);
287 static int method_switch_to_previous(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
294 r
= seat_switch_to_previous(s
);
298 return sd_bus_reply_method_return(message
, NULL
);
301 const sd_bus_vtable seat_vtable
[] = {
302 SD_BUS_VTABLE_START(0),
304 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Seat
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
305 SD_BUS_PROPERTY("ActiveSession", "(so)", property_get_active_session
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
306 SD_BUS_PROPERTY("CanMultiSession", "b", property_get_can_multi_session
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
307 SD_BUS_PROPERTY("CanTTY", "b", property_get_can_tty
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
308 SD_BUS_PROPERTY("CanGraphical", "b", property_get_can_graphical
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
309 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
310 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
311 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
312 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
314 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_seat_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
315 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
316 SD_BUS_METHOD("SwitchTo", "u", NULL
, method_switch_to
, SD_BUS_VTABLE_UNPRIVILEGED
),
317 SD_BUS_METHOD("SwitchToNext", NULL
, NULL
, method_switch_to_next
, SD_BUS_VTABLE_UNPRIVILEGED
),
318 SD_BUS_METHOD("SwitchToPrevious", NULL
, NULL
, method_switch_to_previous
, SD_BUS_VTABLE_UNPRIVILEGED
),
323 int seat_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
324 Manager
*m
= userdata
;
334 if (streq(path
, "/org/freedesktop/login1/seat/self")) {
335 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
336 sd_bus_message
*message
;
340 message
= sd_bus_get_current_message(bus
);
344 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
, &creds
);
348 r
= sd_bus_creds_get_session(creds
, &name
);
352 session
= hashmap_get(m
->sessions
, name
);
356 seat
= session
->seat
;
358 _cleanup_free_
char *e
= NULL
;
361 p
= startswith(path
, "/org/freedesktop/login1/seat/");
365 e
= bus_label_unescape(p
);
369 seat
= hashmap_get(m
->seats
, e
);
379 char *seat_bus_path(Seat
*s
) {
380 _cleanup_free_
char *t
= NULL
;
384 t
= bus_label_escape(s
->id
);
388 return strappend("/org/freedesktop/login1/seat/", t
);
391 int seat_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
392 _cleanup_strv_free_
char **l
= NULL
;
393 sd_bus_message
*message
;
394 Manager
*m
= userdata
;
403 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
406 p
= seat_bus_path(seat
);
410 r
= strv_consume(&l
, p
);
415 message
= sd_bus_get_current_message(bus
);
417 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
421 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
, &creds
);
423 r
= sd_bus_creds_get_session(creds
, &name
);
425 session
= hashmap_get(m
->sessions
, name
);
426 if (session
&& session
->seat
) {
427 r
= strv_extend(&l
, "/org/freedesktop/login1/seat/self");
441 int seat_send_signal(Seat
*s
, bool new_seat
) {
442 _cleanup_free_
char *p
= NULL
;
446 p
= seat_bus_path(s
);
450 return sd_bus_emit_signal(
452 "/org/freedesktop/login1",
453 "org.freedesktop.login1.Manager",
454 new_seat
? "SeatNew" : "SeatRemoved",
458 int seat_send_changed(Seat
*s
, const char *properties
, ...) {
459 _cleanup_free_
char *p
= NULL
;
467 p
= seat_bus_path(s
);
471 l
= strv_from_stdarg_alloca(properties
);
473 return sd_bus_emit_properties_changed_strv(s
->manager
->bus
, p
, "org.freedesktop.login1.Seat", l
);