1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
11 #include "alloc-util.h"
13 #include "format-util.h"
14 #include "logind-user.h"
16 #include "signal-util.h"
18 #include "user-util.h"
20 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", User
, user_get_state
, user_state_to_string
);
22 static int property_get_display(
25 const char *interface
,
27 sd_bus_message
*reply
,
29 sd_bus_error
*error
) {
31 _cleanup_free_
char *p
= NULL
;
38 p
= u
->display
? session_bus_path(u
->display
) : strdup("/");
42 return sd_bus_message_append(reply
, "(so)", u
->display
? u
->display
->id
: "", p
);
45 static int property_get_sessions(
48 const char *interface
,
50 sd_bus_message
*reply
,
52 sd_bus_error
*error
) {
62 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
66 LIST_FOREACH(sessions_by_user
, session
, u
->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 return sd_bus_message_close_container(reply
);
82 static int property_get_idle_hint(
85 const char *interface
,
87 sd_bus_message
*reply
,
89 sd_bus_error
*error
) {
97 return sd_bus_message_append(reply
, "b", user_get_idle_hint(u
, NULL
) > 0);
100 static int property_get_idle_since_hint(
103 const char *interface
,
104 const char *property
,
105 sd_bus_message
*reply
,
107 sd_bus_error
*error
) {
110 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
117 user_get_idle_hint(u
, &t
);
118 k
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
120 return sd_bus_message_append(reply
, "t", k
);
123 static int property_get_linger(
126 const char *interface
,
127 const char *property
,
128 sd_bus_message
*reply
,
130 sd_bus_error
*error
) {
139 r
= user_check_linger_file(u
);
141 return sd_bus_message_append(reply
, "b", r
> 0);
144 int bus_user_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
151 r
= bus_verify_polkit_async(
154 "org.freedesktop.login1.manage",
158 &u
->manager
->polkit_registry
,
163 return 1; /* Will call us back */
165 r
= user_stop(u
, true);
169 return sd_bus_reply_method_return(message
, NULL
);
172 int bus_user_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
180 r
= bus_verify_polkit_async(
183 "org.freedesktop.login1.manage",
187 &u
->manager
->polkit_registry
,
192 return 1; /* Will call us back */
194 r
= sd_bus_message_read(message
, "i", &signo
);
198 if (!SIGNAL_VALID(signo
))
199 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
201 r
= user_kill(u
, signo
);
205 return sd_bus_reply_method_return(message
, NULL
);
208 const sd_bus_vtable user_vtable
[] = {
209 SD_BUS_VTABLE_START(0),
211 SD_BUS_PROPERTY("UID", "u", bus_property_get_uid
, offsetof(User
, uid
), SD_BUS_VTABLE_PROPERTY_CONST
),
212 SD_BUS_PROPERTY("GID", "u", bus_property_get_gid
, offsetof(User
, gid
), SD_BUS_VTABLE_PROPERTY_CONST
),
213 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(User
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
214 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
215 SD_BUS_PROPERTY("RuntimePath", "s", NULL
, offsetof(User
, runtime_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
216 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(User
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
217 SD_BUS_PROPERTY("Slice", "s", NULL
, offsetof(User
, slice
), SD_BUS_VTABLE_PROPERTY_CONST
),
218 SD_BUS_PROPERTY("Display", "(so)", property_get_display
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
219 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
220 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
221 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
222 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
223 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
224 SD_BUS_PROPERTY("Linger", "b", property_get_linger
, 0, 0),
226 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_user_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
227 SD_BUS_METHOD("Kill", "i", NULL
, bus_user_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
232 int user_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
233 Manager
*m
= userdata
;
244 if (streq(path
, "/org/freedesktop/login1/user/self")) {
245 sd_bus_message
*message
;
247 message
= sd_bus_get_current_message(bus
);
251 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
257 p
= startswith(path
, "/org/freedesktop/login1/user/_");
261 r
= parse_uid(p
, &uid
);
265 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
274 char *user_bus_path(User
*u
) {
279 if (asprintf(&s
, "/org/freedesktop/login1/user/_"UID_FMT
, u
->uid
) < 0)
285 int user_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
286 _cleanup_strv_free_
char **l
= NULL
;
287 sd_bus_message
*message
;
288 Manager
*m
= userdata
;
297 HASHMAP_FOREACH(user
, m
->users
, i
) {
300 p
= user_bus_path(user
);
304 r
= strv_consume(&l
, p
);
309 message
= sd_bus_get_current_message(bus
);
311 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
314 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
316 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
318 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
320 r
= strv_extend(&l
, "/org/freedesktop/login1/user/self");
328 *nodes
= TAKE_PTR(l
);
333 int user_send_signal(User
*u
, bool new_user
) {
334 _cleanup_free_
char *p
= NULL
;
338 p
= user_bus_path(u
);
342 return sd_bus_emit_signal(
344 "/org/freedesktop/login1",
345 "org.freedesktop.login1.Manager",
346 new_user
? "UserNew" : "UserRemoved",
347 "uo", (uint32_t) u
->uid
, p
);
350 int user_send_changed(User
*u
, const char *properties
, ...) {
351 _cleanup_free_
char *p
= NULL
;
359 p
= user_bus_path(u
);
363 l
= strv_from_stdarg_alloca(properties
);
365 return sd_bus_emit_properties_changed_strv(u
->manager
->bus
, p
, "org.freedesktop.login1.User", l
);