1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "logind-user.h"
29 #include "formats-util.h"
31 static int property_get_display(
34 const char *interface,
36 sd_bus_message *reply,
38 sd_bus_error *error) {
40 _cleanup_free_ char *p = NULL;
47 p = u->display ? session_bus_path(u->display) : strdup("/");
51 return sd_bus_message_append(reply, "(so)", u->display ? u->display->id : "", p);
54 static int property_get_state(
57 const char *interface,
59 sd_bus_message *reply,
61 sd_bus_error *error) {
69 return sd_bus_message_append(reply, "s", user_state_to_string(user_get_state(u)));
72 static int property_get_sessions(
75 const char *interface,
77 sd_bus_message *reply,
79 sd_bus_error *error) {
89 r = sd_bus_message_open_container(reply, 'a', "(so)");
93 LIST_FOREACH(sessions_by_user, session, u->sessions) {
94 _cleanup_free_ char *p = NULL;
96 p = session_bus_path(session);
100 r = sd_bus_message_append(reply, "(so)", session->id, p);
106 return sd_bus_message_close_container(reply);
109 static int property_get_idle_hint(
112 const char *interface,
113 const char *property,
114 sd_bus_message *reply,
116 sd_bus_error *error) {
124 return sd_bus_message_append(reply, "b", user_get_idle_hint(u, NULL) > 0);
127 static int property_get_idle_since_hint(
130 const char *interface,
131 const char *property,
132 sd_bus_message *reply,
134 sd_bus_error *error) {
137 dual_timestamp t = DUAL_TIMESTAMP_NULL;
144 user_get_idle_hint(u, &t);
145 k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
147 return sd_bus_message_append(reply, "t", k);
150 static int property_get_linger(
153 const char *interface,
154 const char *property,
155 sd_bus_message *reply,
157 sd_bus_error *error) {
166 r = user_check_linger_file(u);
168 return sd_bus_message_append(reply, "b", r > 0);
171 int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
178 r = bus_verify_polkit_async(
181 "org.freedesktop.login1.manage",
185 &u->manager->polkit_registry,
190 return 1; /* Will call us back */
192 r = user_stop(u, true);
196 return sd_bus_reply_method_return(message, NULL);
199 int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
207 r = bus_verify_polkit_async(
210 "org.freedesktop.login1.manage",
214 &u->manager->polkit_registry,
219 return 1; /* Will call us back */
221 r = sd_bus_message_read(message, "i", &signo);
225 if (signo <= 0 || signo >= _NSIG)
226 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
228 r = user_kill(u, signo);
232 return sd_bus_reply_method_return(message, NULL);
235 const sd_bus_vtable user_vtable[] = {
236 SD_BUS_VTABLE_START(0),
238 SD_BUS_PROPERTY("UID", "u", bus_property_get_uid, offsetof(User, uid), SD_BUS_VTABLE_PROPERTY_CONST),
239 SD_BUS_PROPERTY("GID", "u", bus_property_get_gid, offsetof(User, gid), SD_BUS_VTABLE_PROPERTY_CONST),
240 SD_BUS_PROPERTY("Name", "s", NULL, offsetof(User, name), SD_BUS_VTABLE_PROPERTY_CONST),
241 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
242 SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), SD_BUS_VTABLE_PROPERTY_CONST),
243 SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), SD_BUS_VTABLE_PROPERTY_CONST),
244 SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), SD_BUS_VTABLE_PROPERTY_CONST),
245 SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
246 SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
247 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
248 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
249 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
250 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
251 SD_BUS_PROPERTY("Linger", "b", property_get_linger, 0, 0),
253 SD_BUS_METHOD("Terminate", NULL, NULL, bus_user_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED),
254 SD_BUS_METHOD("Kill", "i", NULL, bus_user_method_kill, SD_BUS_VTABLE_UNPRIVILEGED),
259 int user_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
260 Manager *m = userdata;
271 if (streq(path, "/org/freedesktop/login1/user/self")) {
272 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
273 sd_bus_message *message;
275 message = sd_bus_get_current_message(bus);
279 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
283 r = sd_bus_creds_get_owner_uid(creds, &uid);
287 p = startswith(path, "/org/freedesktop/login1/user/_");
291 r = parse_uid(p, &uid);
296 user = hashmap_get(m->users, UID_TO_PTR(uid));
304 char *user_bus_path(User *u) {
309 if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->uid) < 0)
315 int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
316 _cleanup_strv_free_ char **l = NULL;
317 sd_bus_message *message;
318 Manager *m = userdata;
327 HASHMAP_FOREACH(user, m->users, i) {
330 p = user_bus_path(user);
334 r = strv_consume(&l, p);
339 message = sd_bus_get_current_message(bus);
341 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
344 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
346 r = sd_bus_creds_get_owner_uid(creds, &uid);
348 user = hashmap_get(m->users, UID_TO_PTR(uid));
350 r = strv_extend(&l, "/org/freedesktop/login1/user/self");
364 int user_send_signal(User *u, bool new_user) {
365 _cleanup_free_ char *p = NULL;
369 p = user_bus_path(u);
373 return sd_bus_emit_signal(
375 "/org/freedesktop/login1",
376 "org.freedesktop.login1.Manager",
377 new_user ? "UserNew" : "UserRemoved",
378 "uo", (uint32_t) u->uid, p);
381 int user_send_changed(User *u, const char *properties, ...) {
382 _cleanup_free_ char *p = NULL;
390 p = user_bus_path(u);
394 l = strv_from_stdarg_alloca(properties);
396 return sd_bus_emit_properties_changed_strv(u->manager->bus, p, "org.freedesktop.login1.User", l);