1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
8 #include "format-util.h"
9 #include "logind-user.h"
11 #include "missing_capability.h"
12 #include "signal-util.h"
14 #include "user-util.h"
16 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", User
, user_get_state
, user_state_to_string
);
18 static int property_get_display(
21 const char *interface
,
23 sd_bus_message
*reply
,
25 sd_bus_error
*error
) {
27 _cleanup_free_
char *p
= NULL
;
34 p
= u
->display
? session_bus_path(u
->display
) : strdup("/");
38 return sd_bus_message_append(reply
, "(so)", u
->display
? u
->display
->id
: "", p
);
41 static int property_get_sessions(
44 const char *interface
,
46 sd_bus_message
*reply
,
48 sd_bus_error
*error
) {
58 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
62 LIST_FOREACH(sessions_by_user
, session
, u
->sessions
) {
63 _cleanup_free_
char *p
= NULL
;
65 p
= session_bus_path(session
);
69 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
75 return sd_bus_message_close_container(reply
);
78 static int property_get_idle_hint(
81 const char *interface
,
83 sd_bus_message
*reply
,
85 sd_bus_error
*error
) {
93 return sd_bus_message_append(reply
, "b", user_get_idle_hint(u
, NULL
) > 0);
96 static int property_get_idle_since_hint(
99 const char *interface
,
100 const char *property
,
101 sd_bus_message
*reply
,
103 sd_bus_error
*error
) {
106 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
113 (void) user_get_idle_hint(u
, &t
);
114 k
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
116 return sd_bus_message_append(reply
, "t", k
);
119 static int property_get_linger(
122 const char *interface
,
123 const char *property
,
124 sd_bus_message
*reply
,
126 sd_bus_error
*error
) {
135 r
= user_check_linger_file(u
);
137 return sd_bus_message_append(reply
, "b", r
> 0);
140 int bus_user_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
147 r
= bus_verify_polkit_async(
150 "org.freedesktop.login1.manage",
154 &u
->manager
->polkit_registry
,
159 return 1; /* Will call us back */
161 r
= user_stop(u
, true);
165 return sd_bus_reply_method_return(message
, NULL
);
168 int bus_user_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
176 r
= bus_verify_polkit_async(
179 "org.freedesktop.login1.manage",
183 &u
->manager
->polkit_registry
,
188 return 1; /* Will call us back */
190 r
= sd_bus_message_read(message
, "i", &signo
);
194 if (!SIGNAL_VALID(signo
))
195 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
197 r
= user_kill(u
, signo
);
201 return sd_bus_reply_method_return(message
, NULL
);
204 const sd_bus_vtable user_vtable
[] = {
205 SD_BUS_VTABLE_START(0),
207 SD_BUS_PROPERTY("UID", "u", bus_property_get_uid
, offsetof(User
, uid
), SD_BUS_VTABLE_PROPERTY_CONST
),
208 SD_BUS_PROPERTY("GID", "u", bus_property_get_gid
, offsetof(User
, gid
), SD_BUS_VTABLE_PROPERTY_CONST
),
209 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(User
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
210 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
211 SD_BUS_PROPERTY("RuntimePath", "s", NULL
, offsetof(User
, runtime_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
212 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(User
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
213 SD_BUS_PROPERTY("Slice", "s", NULL
, offsetof(User
, slice
), SD_BUS_VTABLE_PROPERTY_CONST
),
214 SD_BUS_PROPERTY("Display", "(so)", property_get_display
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
215 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
216 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
217 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
218 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
219 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
220 SD_BUS_PROPERTY("Linger", "b", property_get_linger
, 0, 0),
222 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_user_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
223 SD_BUS_METHOD("Kill", "i", NULL
, bus_user_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
228 int user_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
229 Manager
*m
= userdata
;
240 if (streq(path
, "/org/freedesktop/login1/user/self")) {
241 sd_bus_message
*message
;
243 message
= sd_bus_get_current_message(bus
);
247 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
253 p
= startswith(path
, "/org/freedesktop/login1/user/_");
257 r
= parse_uid(p
, &uid
);
261 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
270 char *user_bus_path(User
*u
) {
275 if (asprintf(&s
, "/org/freedesktop/login1/user/_"UID_FMT
, u
->uid
) < 0)
281 int user_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
282 _cleanup_strv_free_
char **l
= NULL
;
283 sd_bus_message
*message
;
284 Manager
*m
= userdata
;
293 HASHMAP_FOREACH(user
, m
->users
, i
) {
296 p
= user_bus_path(user
);
300 r
= strv_consume(&l
, p
);
305 message
= sd_bus_get_current_message(bus
);
307 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
310 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
312 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
314 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
316 r
= strv_extend(&l
, "/org/freedesktop/login1/user/self");
324 *nodes
= TAKE_PTR(l
);
329 int user_send_signal(User
*u
, bool new_user
) {
330 _cleanup_free_
char *p
= NULL
;
334 p
= user_bus_path(u
);
338 return sd_bus_emit_signal(
340 "/org/freedesktop/login1",
341 "org.freedesktop.login1.Manager",
342 new_user
? "UserNew" : "UserRemoved",
343 "uo", (uint32_t) u
->uid
, p
);
346 int user_send_changed(User
*u
, const char *properties
, ...) {
347 _cleanup_free_
char *p
= NULL
;
355 p
= user_bus_path(u
);
359 l
= strv_from_stdarg_alloca(properties
);
361 return sd_bus_emit_properties_changed_strv(u
->manager
->bus
, p
, "org.freedesktop.login1.User", l
);