1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
26 #include "format-util.h"
27 #include "logind-user.h"
29 #include "signal-util.h"
31 #include "user-util.h"
33 static int property_get_display(
36 const char *interface
,
38 sd_bus_message
*reply
,
40 sd_bus_error
*error
) {
42 _cleanup_free_
char *p
= NULL
;
49 p
= u
->display
? session_bus_path(u
->display
) : strdup("/");
53 return sd_bus_message_append(reply
, "(so)", u
->display
? u
->display
->id
: "", p
);
56 static int property_get_state(
59 const char *interface
,
61 sd_bus_message
*reply
,
63 sd_bus_error
*error
) {
71 return sd_bus_message_append(reply
, "s", user_state_to_string(user_get_state(u
)));
74 static int property_get_sessions(
77 const char *interface
,
79 sd_bus_message
*reply
,
81 sd_bus_error
*error
) {
91 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
95 LIST_FOREACH(sessions_by_user
, session
, u
->sessions
) {
96 _cleanup_free_
char *p
= NULL
;
98 p
= session_bus_path(session
);
102 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
108 return sd_bus_message_close_container(reply
);
111 static int property_get_idle_hint(
114 const char *interface
,
115 const char *property
,
116 sd_bus_message
*reply
,
118 sd_bus_error
*error
) {
126 return sd_bus_message_append(reply
, "b", user_get_idle_hint(u
, NULL
) > 0);
129 static int property_get_idle_since_hint(
132 const char *interface
,
133 const char *property
,
134 sd_bus_message
*reply
,
136 sd_bus_error
*error
) {
139 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
146 user_get_idle_hint(u
, &t
);
147 k
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
149 return sd_bus_message_append(reply
, "t", k
);
152 static int property_get_linger(
155 const char *interface
,
156 const char *property
,
157 sd_bus_message
*reply
,
159 sd_bus_error
*error
) {
168 r
= user_check_linger_file(u
);
170 return sd_bus_message_append(reply
, "b", r
> 0);
173 int bus_user_method_terminate(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
= user_stop(u
, true);
198 return sd_bus_reply_method_return(message
, NULL
);
201 int bus_user_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
209 r
= bus_verify_polkit_async(
212 "org.freedesktop.login1.manage",
216 &u
->manager
->polkit_registry
,
221 return 1; /* Will call us back */
223 r
= sd_bus_message_read(message
, "i", &signo
);
227 if (!SIGNAL_VALID(signo
))
228 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
230 r
= user_kill(u
, signo
);
234 return sd_bus_reply_method_return(message
, NULL
);
237 const sd_bus_vtable user_vtable
[] = {
238 SD_BUS_VTABLE_START(0),
240 SD_BUS_PROPERTY("UID", "u", bus_property_get_uid
, offsetof(User
, uid
), SD_BUS_VTABLE_PROPERTY_CONST
),
241 SD_BUS_PROPERTY("GID", "u", bus_property_get_gid
, offsetof(User
, gid
), SD_BUS_VTABLE_PROPERTY_CONST
),
242 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(User
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
243 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
244 SD_BUS_PROPERTY("RuntimePath", "s", NULL
, offsetof(User
, runtime_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
245 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(User
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
246 SD_BUS_PROPERTY("Slice", "s", NULL
, offsetof(User
, slice
), SD_BUS_VTABLE_PROPERTY_CONST
),
247 SD_BUS_PROPERTY("Display", "(so)", property_get_display
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
248 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
249 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
250 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
251 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
252 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
253 SD_BUS_PROPERTY("Linger", "b", property_get_linger
, 0, 0),
255 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_user_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
256 SD_BUS_METHOD("Kill", "i", NULL
, bus_user_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
261 int user_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
262 Manager
*m
= userdata
;
273 if (streq(path
, "/org/freedesktop/login1/user/self")) {
274 sd_bus_message
*message
;
276 message
= sd_bus_get_current_message(bus
);
280 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
286 p
= startswith(path
, "/org/freedesktop/login1/user/_");
290 r
= parse_uid(p
, &uid
);
295 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
303 char *user_bus_path(User
*u
) {
308 if (asprintf(&s
, "/org/freedesktop/login1/user/_"UID_FMT
, u
->uid
) < 0)
314 int user_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
315 _cleanup_strv_free_
char **l
= NULL
;
316 sd_bus_message
*message
;
317 Manager
*m
= userdata
;
326 HASHMAP_FOREACH(user
, m
->users
, i
) {
329 p
= user_bus_path(user
);
333 r
= strv_consume(&l
, p
);
338 message
= sd_bus_get_current_message(bus
);
340 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
343 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
345 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
347 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
349 r
= strv_extend(&l
, "/org/freedesktop/login1/user/self");
363 int user_send_signal(User
*u
, bool new_user
) {
364 _cleanup_free_
char *p
= NULL
;
368 p
= user_bus_path(u
);
372 return sd_bus_emit_signal(
374 "/org/freedesktop/login1",
375 "org.freedesktop.login1.Manager",
376 new_user
? "UserNew" : "UserRemoved",
377 "uo", (uint32_t) u
->uid
, p
);
380 int user_send_changed(User
*u
, const char *properties
, ...) {
381 _cleanup_free_
char *p
= NULL
;
389 p
= user_bus_path(u
);
393 l
= strv_from_stdarg_alloca(properties
);
395 return sd_bus_emit_properties_changed_strv(u
->manager
->bus
, p
, "org.freedesktop.login1.User", l
);