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/>.
29 #include "logind-user.h"
31 static int property_get_display(
34 const char *interface,
36 sd_bus_message *reply,
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,
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,
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 r = sd_bus_message_close_container(reply);
113 static int property_get_idle_hint(
116 const char *interface,
117 const char *property,
118 sd_bus_message *reply,
128 return sd_bus_message_append(reply, "b", user_get_idle_hint(u, NULL) > 0);
131 static int property_get_idle_since_hint(
134 const char *interface,
135 const char *property,
136 sd_bus_message *reply,
148 user_get_idle_hint(u, &t);
149 k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
151 return sd_bus_message_append(reply, "t", k);
154 static int property_get_linger(
157 const char *interface,
158 const char *property,
159 sd_bus_message *reply,
170 r = user_check_linger_file(u);
172 return sd_bus_message_append(reply, "b", r > 0);
175 static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata) {
185 return sd_bus_reply_method_errno(bus, message, r, NULL);
187 return sd_bus_reply_method_return(bus, message, NULL);
190 static int method_kill(sd_bus *bus, sd_bus_message *message, void *userdata) {
199 r = sd_bus_message_read(message, "i", &signo);
201 return sd_bus_reply_method_errno(bus, message, r, NULL);
203 if (signo <= 0 || signo >= _NSIG)
204 return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
206 r = user_kill(u, signo);
208 return sd_bus_reply_method_errno(bus, message, r, NULL);
210 return sd_bus_reply_method_return(bus, message, NULL);
213 const sd_bus_vtable user_vtable[] = {
214 SD_BUS_VTABLE_START(0),
216 SD_BUS_PROPERTY("UID", "u", bus_property_get_uid, offsetof(User, uid), 0),
217 SD_BUS_PROPERTY("GID", "u", bus_property_get_gid, offsetof(User, gid), 0),
218 SD_BUS_PROPERTY("Name", "s", NULL, offsetof(User, name), 0),
219 SD_BUS_PROPERTY("Timestamp", "t", NULL, offsetof(User, timestamp.realtime), 0),
220 SD_BUS_PROPERTY("TimestampMonotonic", "t", NULL, offsetof(User, timestamp.monotonic), 0),
221 SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), 0),
222 SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), 0),
223 SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), 0),
224 SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, 0),
225 SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
226 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
227 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
228 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
229 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
230 SD_BUS_PROPERTY("Linger", "b", property_get_linger, 0, 0),
232 SD_BUS_METHOD("Terminate", NULL, NULL, method_terminate, 0),
233 SD_BUS_METHOD("Kill", "i", NULL, method_kill, 0),
238 int user_object_find(sd_bus *bus, const char *path, const char *interface, void **found, void *userdata) {
239 Manager *m = userdata;
249 if (streq(path, "/org/freedesktop/login1/user/self")) {
250 sd_bus_message *message;
253 message = sd_bus_get_current(bus);
257 r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), &pid);
261 r = manager_get_user_by_pid(m, pid, &user);
268 p = startswith(path, "/org/freedesktop/login1/user/_");
272 r = safe_atolu(p, &lu);
276 user = hashmap_get(m->users, ULONG_TO_PTR(lu));
285 char *user_bus_path(User *u) {
290 if (asprintf(&s, "/org/freedesktop/login1/user/_%llu", (unsigned long long) u->uid) < 0)
296 int user_node_enumerator(sd_bus *bus, const char *path, char ***nodes, void *userdata) {
297 _cleanup_strv_free_ char **l = NULL;
298 Manager *m = userdata;
307 HASHMAP_FOREACH(user, m->users, i) {
310 p = user_bus_path(user);
314 r = strv_push(&l, p);
327 int user_send_signal(User *u, bool new_user) {
328 _cleanup_free_ char *p = NULL;
332 p = user_bus_path(u);
336 return sd_bus_emit_signal(
338 "/org/freedesktop/login1",
339 "org.freedesktop.login1.Manager",
340 new_user ? "UserNew" : "UserRemoved",
341 "uo", (uint32_t) u->uid, p);
344 int user_send_changed(User *u, const char *properties, ...) {
345 _cleanup_free_ char *p = NULL;
353 p = user_bus_path(u);
357 l = strv_from_stdarg_alloca(properties);
359 return sd_bus_emit_properties_changed_strv(u->manager->bus, p, "org.freedesktop.login1.User", l);