2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
25 #include "formats-util.h"
26 #include "logind-user.h"
28 #include "signal-util.h"
30 #include "user-util.h"
32 static int property_get_display(
35 const char *interface,
37 sd_bus_message *reply,
39 sd_bus_error *error) {
41 _cleanup_free_ char *p = NULL;
48 p = u->display ? session_bus_path(u->display) : strdup("/");
52 return sd_bus_message_append(reply, "(so)", u->display ? u->display->id : "", p);
55 static int property_get_state(
58 const char *interface,
60 sd_bus_message *reply,
62 sd_bus_error *error) {
70 return sd_bus_message_append(reply, "s", user_state_to_string(user_get_state(u)));
73 static int property_get_sessions(
76 const char *interface,
78 sd_bus_message *reply,
80 sd_bus_error *error) {
90 r = sd_bus_message_open_container(reply, 'a', "(so)");
94 LIST_FOREACH(sessions_by_user, session, u->sessions) {
95 _cleanup_free_ char *p = NULL;
97 p = session_bus_path(session);
101 r = sd_bus_message_append(reply, "(so)", session->id, p);
107 return sd_bus_message_close_container(reply);
110 static int property_get_idle_hint(
113 const char *interface,
114 const char *property,
115 sd_bus_message *reply,
117 sd_bus_error *error) {
125 return sd_bus_message_append(reply, "b", user_get_idle_hint(u, NULL) > 0);
128 static int property_get_idle_since_hint(
131 const char *interface,
132 const char *property,
133 sd_bus_message *reply,
135 sd_bus_error *error) {
138 dual_timestamp t = DUAL_TIMESTAMP_NULL;
145 user_get_idle_hint(u, &t);
146 k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
148 return sd_bus_message_append(reply, "t", k);
151 static int property_get_linger(
154 const char *interface,
155 const char *property,
156 sd_bus_message *reply,
158 sd_bus_error *error) {
167 r = user_check_linger_file(u);
169 return sd_bus_message_append(reply, "b", r > 0);
172 int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
179 r = bus_verify_polkit_async(
182 "org.freedesktop.login1.manage",
186 &u->manager->polkit_registry,
191 return 1; /* Will call us back */
193 r = user_stop(u, true);
197 return sd_bus_reply_method_return(message, NULL);
200 int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
208 r = bus_verify_polkit_async(
211 "org.freedesktop.login1.manage",
215 &u->manager->polkit_registry,
220 return 1; /* Will call us back */
222 r = sd_bus_message_read(message, "i", &signo);
226 if (!SIGNAL_VALID(signo))
227 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
229 r = user_kill(u, signo);
233 return sd_bus_reply_method_return(message, NULL);
236 const sd_bus_vtable user_vtable[] = {
237 SD_BUS_VTABLE_START(0),
239 SD_BUS_PROPERTY("UID", "u", bus_property_get_uid, offsetof(User, uid), SD_BUS_VTABLE_PROPERTY_CONST),
240 SD_BUS_PROPERTY("GID", "u", bus_property_get_gid, offsetof(User, gid), SD_BUS_VTABLE_PROPERTY_CONST),
241 SD_BUS_PROPERTY("Name", "s", NULL, offsetof(User, name), SD_BUS_VTABLE_PROPERTY_CONST),
242 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
243 SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), SD_BUS_VTABLE_PROPERTY_CONST),
244 SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), SD_BUS_VTABLE_PROPERTY_CONST),
245 SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), SD_BUS_VTABLE_PROPERTY_CONST),
246 SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
247 SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
248 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, 0),
249 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
250 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
251 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
252 SD_BUS_PROPERTY("Linger", "b", property_get_linger, 0, 0),
254 SD_BUS_METHOD("Terminate", NULL, NULL, bus_user_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED),
255 SD_BUS_METHOD("Kill", "i", NULL, bus_user_method_kill, SD_BUS_VTABLE_UNPRIVILEGED),
260 int user_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
261 Manager *m = userdata;
272 if (streq(path, "/org/freedesktop/login1/user/self")) {
273 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
274 sd_bus_message *message;
276 message = sd_bus_get_current_message(bus);
280 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
284 r = sd_bus_creds_get_owner_uid(creds, &uid);
288 p = startswith(path, "/org/freedesktop/login1/user/_");
292 r = parse_uid(p, &uid);
297 user = hashmap_get(m->users, UID_TO_PTR(uid));
305 char *user_bus_path(User *u) {
310 if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->uid) < 0)
316 int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
317 _cleanup_strv_free_ char **l = NULL;
318 sd_bus_message *message;
319 Manager *m = userdata;
328 HASHMAP_FOREACH(user, m->users, i) {
331 p = user_bus_path(user);
335 r = strv_consume(&l, p);
340 message = sd_bus_get_current_message(bus);
342 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
345 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
347 r = sd_bus_creds_get_owner_uid(creds, &uid);
349 user = hashmap_get(m->users, UID_TO_PTR(uid));
351 r = strv_extend(&l, "/org/freedesktop/login1/user/self");
365 int user_send_signal(User *u, bool new_user) {
366 _cleanup_free_ char *p = NULL;
370 p = user_bus_path(u);
374 return sd_bus_emit_signal(
376 "/org/freedesktop/login1",
377 "org.freedesktop.login1.Manager",
378 new_user ? "UserNew" : "UserRemoved",
379 "uo", (uint32_t) u->uid, p);
382 int user_send_changed(User *u, const char *properties, ...) {
383 _cleanup_free_ char *p = NULL;
391 p = user_bus_path(u);
395 l = strv_from_stdarg_alloca(properties);
397 return sd_bus_emit_properties_changed_strv(u->manager->bus, p, "org.freedesktop.login1.User", l);