chiark / gitweb /
logind: implement D-Bus properties
[elogind.git] / src / logind-session-dbus.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
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   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23
24 #include "logind.h"
25 #include "logind-session.h"
26 #include "dbus-common.h"
27 #include "util.h"
28
29 #define BUS_SESSION_INTERFACE \
30         " <interface name=\"org.freedesktop.login1.Session\">\n"        \
31         "  <method name=\"Terminate\"/>\n"                              \
32         "  <method name=\"Activate\"/>\n"                               \
33         "  <property name=\"Id\" type=\"u\" access=\"read\"/>\n"        \
34         "  <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n"   \
35         "  <property name=\"Name\" type=\"s\" access=\"read\"/>\n"      \
36         "  <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
37         "  <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n"      \
38         "  <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n"   \
39         "  <property name=\"TTY\" type=\"s\" access=\"read\"/>\n"       \
40         "  <property name=\"Display\" type=\"s\" access=\"read\"/>\n"   \
41         "  <property name=\"Remote\" type=\"b\" access=\"read\"/>\n"    \
42         "  <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \
43         "  <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \
44         "  <property name=\"Leader\" type=\"u\" access=\"read\"/>\n"    \
45         "  <property name=\"Audit\" type=\"u\" access=\"read\"/>\n"     \
46         "  <property name=\"Type\" type=\"s\" access=\"read\"/>\n"      \
47         "  <property name=\"Active\" type=\"b\" access=\"read\"/>\n"    \
48         "  <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
49         "  <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
50         "  <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
51         " </interface>\n"
52
53 #define INTROSPECTION                                                   \
54         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
55         "<node>\n"                                                      \
56         BUS_SESSION_INTERFACE                                           \
57         BUS_PROPERTIES_INTERFACE                                        \
58         BUS_PEER_INTERFACE                                              \
59         BUS_INTROSPECTABLE_INTERFACE                                    \
60         "</node>\n"
61
62 #define INTERFACES_LIST                              \
63         BUS_GENERIC_INTERFACES_LIST                  \
64         "org.freedesktop.login1.Session\0"
65
66 static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) {
67         DBusMessageIter sub;
68         Session *s = data;
69         const char *id, *path;
70         char *p = NULL;
71
72         assert(i);
73         assert(property);
74         assert(s);
75
76         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
77                 return -ENOMEM;
78
79         if (s->seat) {
80                 id = s->seat->id;
81                 path = p = seat_bus_path(s->seat);
82
83                 if (!p)
84                         return -ENOMEM;
85         } else {
86                 id = "";
87                 path = "/";
88         }
89
90         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
91             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
92                 free(p);
93                 return -ENOMEM;
94         }
95
96         free(p);
97
98         if (!dbus_message_iter_close_container(i, &sub))
99                 return -ENOMEM;
100
101         return 0;
102 }
103
104 static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
105         DBusMessageIter sub;
106         Session *s = data;
107         char *p = NULL;
108
109         assert(i);
110         assert(property);
111         assert(s);
112
113         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
114                 return -ENOMEM;
115
116         p = user_bus_path(s->user);
117         if (!p)
118                 return -ENOMEM;
119
120         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &s->user->uid) ||
121             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
122                 free(p);
123                 return -ENOMEM;
124         }
125
126         free(p);
127
128         if (!dbus_message_iter_close_container(i, &sub))
129                 return -ENOMEM;
130
131         return 0;
132 }
133
134 static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) {
135         Session *s = data;
136         bool b;
137
138         assert(i);
139         assert(property);
140         assert(s);
141
142         b = session_is_active(s);
143         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
144                 return -ENOMEM;
145
146         return 0;
147 }
148
149 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType);
150
151 static int get_session_for_path(Manager *m, const char *path, Session **_s) {
152         Session *s;
153         char *id;
154
155         assert(m);
156         assert(path);
157         assert(_s);
158
159         if (!startswith(path, "/org/freedesktop/login1/session/"))
160                 return -EINVAL;
161
162         id = bus_path_unescape(path + 32);
163         if (!id)
164                 return -ENOMEM;
165
166         s = hashmap_get(m->sessions, id);
167         free(id);
168
169         if (!s)
170                 return -ENOENT;
171
172         *_s = s;
173         return 0;
174 }
175
176 static DBusHandlerResult session_message_dispatch(
177                 Session *s,
178                 DBusConnection *connection,
179                 DBusMessage *message) {
180
181         const BusProperty properties[] = {
182                 { "org.freedesktop.login1.Session", "Id",               bus_property_append_string, "s",    s->id                },
183                 { "org.freedesktop.login1.Session", "User",             bus_session_append_user,    "(uo)", s                    },
184                 { "org.freedesktop.login1.Session", "Name",             bus_property_append_string, "s",    s->user->name        },
185                 { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string, "s",    s->cgroup_path       },
186                 { "org.freedesktop.login1.Session", "VTNr",             bus_property_append_uint32, "u",    &s->vtnr             },
187                 { "org.freedesktop.login1.Session", "Seat",             bus_session_append_seat,    "(so)", s                    },
188                 { "org.freedesktop.login1.Session", "TTY",              bus_property_append_string, "s",    s->tty               },
189                 { "org.freedesktop.login1.Session", "Display",          bus_property_append_string, "s",    s->display           },
190                 { "org.freedesktop.login1.Session", "Remote",           bus_property_append_bool,   "b",    &s->remote           },
191                 { "org.freedesktop.login1.Session", "RemoteUser",       bus_property_append_string, "s",    s->remote_user       },
192                 { "org.freedesktop.login1.Session", "RemoteHost",       bus_property_append_string, "s",    s->remote_host       },
193                 { "org.freedesktop.login1.Session", "Leader",           bus_property_append_pid,    "u",    &s->leader           },
194                 { "org.freedesktop.login1.Session", "Audit",            bus_property_append_uint32, "u",    &s->audit_id         },
195                 { "org.freedesktop.login1.Session", "Type",             bus_session_append_type,    "s",    &s->type             },
196                 { "org.freedesktop.login1.Session", "Active",           bus_session_append_active,  "b",    s                    },
197                 { "org.freedesktop.login1.Session", "Controllers",      bus_property_append_strv,   "as",   s->controllers       },
198                 { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv,   "as",   s->reset_controllers },
199                 { "org.freedesktop.login1.Session", "KillProcesses",    bus_property_append_bool,   "b",    &s->kill_processes   },
200                 { NULL, NULL, NULL, NULL, NULL }
201         };
202
203         assert(s);
204         assert(connection);
205         assert(message);
206
207         return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
208 }
209
210 static DBusHandlerResult session_message_handler(
211                 DBusConnection *connection,
212                 DBusMessage *message,
213                 void *userdata) {
214
215         Manager *m = userdata;
216         Session *s;
217         int r;
218
219         r = get_session_for_path(m, dbus_message_get_path(message), &s);
220         if (r < 0) {
221
222                 if (r == -ENOMEM)
223                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
224
225                 if (r == -ENOENT) {
226                         DBusError e;
227
228                         dbus_error_init(&e);
229                         dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session");
230                         return bus_send_error_reply(connection, message, &e, r);
231                 }
232
233                 return bus_send_error_reply(connection, message, NULL, r);
234         }
235
236         return session_message_dispatch(s, connection, message);
237 }
238
239 const DBusObjectPathVTable bus_session_vtable = {
240         .message_function = session_message_handler
241 };
242
243 char *session_bus_path(Session *s) {
244         char *t, *r;
245
246         assert(s);
247
248         t = bus_path_escape(s->id);
249         if (!t)
250                 return NULL;
251
252         r = strappend("/org/freedesktop/login1/session/", t);
253         free(t);
254
255         return r;
256 }