1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
8 #include "format-util.h"
9 #include "logind-user.h"
11 #include "signal-util.h"
13 #include "user-util.h"
15 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", User
, user_get_state
, user_state_to_string
);
17 static int property_get_display(
20 const char *interface
,
22 sd_bus_message
*reply
,
24 sd_bus_error
*error
) {
26 _cleanup_free_
char *p
= NULL
;
33 p
= u
->display
? session_bus_path(u
->display
) : strdup("/");
37 return sd_bus_message_append(reply
, "(so)", u
->display
? u
->display
->id
: "", p
);
40 static int property_get_sessions(
43 const char *interface
,
45 sd_bus_message
*reply
,
47 sd_bus_error
*error
) {
57 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
61 LIST_FOREACH(sessions_by_user
, session
, u
->sessions
) {
62 _cleanup_free_
char *p
= NULL
;
64 p
= session_bus_path(session
);
68 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
74 return sd_bus_message_close_container(reply
);
77 static int property_get_idle_hint(
80 const char *interface
,
82 sd_bus_message
*reply
,
84 sd_bus_error
*error
) {
92 return sd_bus_message_append(reply
, "b", user_get_idle_hint(u
, NULL
) > 0);
95 static int property_get_idle_since_hint(
98 const char *interface
,
100 sd_bus_message
*reply
,
102 sd_bus_error
*error
) {
105 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
112 (void) user_get_idle_hint(u
, &t
);
113 k
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
115 return sd_bus_message_append(reply
, "t", k
);
118 static int property_get_linger(
121 const char *interface
,
122 const char *property
,
123 sd_bus_message
*reply
,
125 sd_bus_error
*error
) {
134 r
= user_check_linger_file(u
);
136 return sd_bus_message_append(reply
, "b", r
> 0);
139 int bus_user_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
146 r
= bus_verify_polkit_async(
149 "org.freedesktop.login1.manage",
153 &u
->manager
->polkit_registry
,
158 return 1; /* Will call us back */
160 r
= user_stop(u
, true);
164 return sd_bus_reply_method_return(message
, NULL
);
167 int bus_user_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
175 r
= bus_verify_polkit_async(
178 "org.freedesktop.login1.manage",
182 &u
->manager
->polkit_registry
,
187 return 1; /* Will call us back */
189 r
= sd_bus_message_read(message
, "i", &signo
);
193 if (!SIGNAL_VALID(signo
))
194 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
196 r
= user_kill(u
, signo
);
200 return sd_bus_reply_method_return(message
, NULL
);
203 const sd_bus_vtable user_vtable
[] = {
204 SD_BUS_VTABLE_START(0),
206 SD_BUS_PROPERTY("UID", "u", bus_property_get_uid
, offsetof(User
, uid
), SD_BUS_VTABLE_PROPERTY_CONST
),
207 SD_BUS_PROPERTY("GID", "u", bus_property_get_gid
, offsetof(User
, gid
), SD_BUS_VTABLE_PROPERTY_CONST
),
208 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(User
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
209 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
210 SD_BUS_PROPERTY("RuntimePath", "s", NULL
, offsetof(User
, runtime_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
211 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(User
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
212 SD_BUS_PROPERTY("Slice", "s", NULL
, offsetof(User
, slice
), SD_BUS_VTABLE_PROPERTY_CONST
),
213 SD_BUS_PROPERTY("Display", "(so)", property_get_display
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
214 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
215 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
216 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
217 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
218 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
219 SD_BUS_PROPERTY("Linger", "b", property_get_linger
, 0, 0),
221 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_user_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
222 SD_BUS_METHOD("Kill", "i", NULL
, bus_user_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
227 int user_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
228 Manager
*m
= userdata
;
239 if (streq(path
, "/org/freedesktop/login1/user/self")) {
240 sd_bus_message
*message
;
242 message
= sd_bus_get_current_message(bus
);
246 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
252 p
= startswith(path
, "/org/freedesktop/login1/user/_");
256 r
= parse_uid(p
, &uid
);
260 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
269 char *user_bus_path(User
*u
) {
274 if (asprintf(&s
, "/org/freedesktop/login1/user/_"UID_FMT
, u
->uid
) < 0)
280 int user_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
281 _cleanup_strv_free_
char **l
= NULL
;
282 sd_bus_message
*message
;
283 Manager
*m
= userdata
;
292 HASHMAP_FOREACH(user
, m
->users
, i
) {
295 p
= user_bus_path(user
);
299 r
= strv_consume(&l
, p
);
304 message
= sd_bus_get_current_message(bus
);
306 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
309 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
311 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
313 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
315 r
= strv_extend(&l
, "/org/freedesktop/login1/user/self");
323 *nodes
= TAKE_PTR(l
);
328 int user_send_signal(User
*u
, bool new_user
) {
329 _cleanup_free_
char *p
= NULL
;
333 p
= user_bus_path(u
);
337 return sd_bus_emit_signal(
339 "/org/freedesktop/login1",
340 "org.freedesktop.login1.Manager",
341 new_user
? "UserNew" : "UserRemoved",
342 "uo", (uint32_t) u
->uid
, p
);
345 int user_send_changed(User
*u
, const char *properties
, ...) {
346 _cleanup_free_
char *p
= NULL
;
354 p
= user_bus_path(u
);
358 l
= strv_from_stdarg_alloca(properties
);
360 return sd_bus_emit_properties_changed_strv(u
->manager
->bus
, p
, "org.freedesktop.login1.User", l
);