chiark / gitweb /
logind: implement D-Bus properties
[elogind.git] / src / logind-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 "logind.h"
23 #include "dbus-common.h"
24
25 #define BUS_MANAGER_INTERFACE                                           \
26         " <interface name=\"org.freedesktop.login1.Manager\">\n"        \
27         "  <method name=\"GetSeat\">\n"                                 \
28         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
29         "   <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n"        \
30         "  </method>\n"                                                 \
31         "  <method name=\"GetUser\">\n"                                 \
32         "   <arg name=\"uid\" type=\"t\" direction=\"in\"/>\n"          \
33         "   <arg name=\"user\" type=\"o\" direction=\"out\"/>\n"        \
34         "  </method>\n"                                                 \
35         "  <method name=\"GetSession\">\n"                              \
36         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
37         "   <arg name=\"session\" type=\"o\" direction=\"out\"/>\n"     \
38         "  </method>\n"                                                 \
39         "  <method name=\"ListSeats\">\n"                               \
40         "   <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n"   \
41         "  </method>\n"                                                 \
42         "  <method name=\"ListUsers\">\n"                               \
43         "   <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n"  \
44         "  </method>\n"                                                 \
45         "  <method name=\"ListSessions\">\n"                            \
46         "   <arg name=\"users\" type=\"a(sussso)\" direction=\"out\"/>\n" \
47         "  </method>\n"                                                 \
48         "  <method name=\"CreateSession\">\n"                           \
49         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
50         "   <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n"       \
51         "   <arg name=\"type\" type=\"s\" direction=\"in\"/>\n"         \
52         "   <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n"         \
53         "   <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n"          \
54         "   <arg name=\"display\" type=\"s\" direction=\"in\"/>\n"      \
55         "   <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n"       \
56         "   <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n"  \
57         "   <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n"  \
58         "   <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
59         "   <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
60         "   <arg name=\"kill_processes\" type=\"as\" direction=\"in\"/>\n" \
61         "   <arg name=\"id\" type=\"s\" direction=\"out\"/>\n"          \
62         "   <arg name=\"path\" type=\"o\" direction=\"out\"/>\n"        \
63         "   <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n"          \
64         "  </method>\n"                                                 \
65         "  <method name=\"TerminateSession\">\n"                        \
66         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
67         "  </method>\n"                                                 \
68         "  <method name=\"TerminateUser\">\n"                           \
69         "   <arg name=\"uid\" type=\"t\" direction=\"in\"/>\n"          \
70         "  </method>\n"                                                 \
71         "  <method name=\"TerminateSeat\">\n"                           \
72         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
73         "  </method>\n"                                                 \
74         "  <signal name=\"SessionNew\">\n"                              \
75         "   <arg name=\"id\" type=\"s\"/>\n"                            \
76         "   <arg name=\"path\" type=\"o\"/>\n"                          \
77         "  </signal>\n"                                                 \
78         "  <signal name=\"SessionRemoved\">\n"                          \
79         "   <arg name=\"id\" type=\"s\"/>\n"                            \
80         "   <arg name=\"path\" type=\"o\"/>\n"                          \
81         "  </signal>\n"                                                 \
82         "  <signal name=\"UserNew\">\n"                                 \
83         "   <arg name=\"uid\" type=\"u\"/>\n"                           \
84         "   <arg name=\"path\" type=\"o\"/>\n"                          \
85         "  </signal>\n"                                                 \
86         "  <signal name=\"UserRemoved\">\n"                             \
87         "   <arg name=\"uid\" type=\"u\"/>\n"                           \
88         "   <arg name=\"path\" type=\"o\"/>\n"                          \
89         "  </signal>\n"                                                 \
90         "  <signal name=\"SeatNew\">\n"                                 \
91         "   <arg name=\"id\" type=\"s\"/>\n"                            \
92         "   <arg name=\"path\" type=\"o\"/>\n"                          \
93         "  </signal>\n"                                                 \
94         "  <signal name=\"SeatRemoved\">\n"                             \
95         "   <arg name=\"id\" type=\"s\"/>\n"                            \
96         "   <arg name=\"path\" type=\"o\"/>\n"                          \
97         "  </signal>\n"                                                 \
98         "  <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
99         "  <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
100         "  <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
101         "  <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
102         "  <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
103         "  <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
104         "  <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
105         " </interface>\n"
106
107 #define INTROSPECTION_BEGIN                                             \
108         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
109         "<node>\n"                                                      \
110         BUS_MANAGER_INTERFACE                                           \
111         BUS_PROPERTIES_INTERFACE                                        \
112         BUS_PEER_INTERFACE                                              \
113         BUS_INTROSPECTABLE_INTERFACE
114
115 #define INTROSPECTION_END                                               \
116         "</node>\n"
117
118 #define INTERFACES_LIST                              \
119         BUS_GENERIC_INTERFACES_LIST                  \
120         "org.freedesktop.login1.Manager\0"
121
122 static DBusHandlerResult manager_message_handler(
123                 DBusConnection *connection,
124                 DBusMessage *message,
125                 void *userdata) {
126
127         Manager *m = userdata;
128
129         const BusProperty properties[] = {
130                 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string,   "s",  m->cgroup_path          },
131                 { "org.freedesktop.login1.Manager", "Controllers",           bus_property_append_strv,     "as", m->controllers          },
132                 { "org.freedesktop.login1.Manager", "ResetControllers",      bus_property_append_strv,     "as", m->reset_controllers    },
133                 { "org.freedesktop.login1.Manager", "NAutoVTs",              bus_property_append_unsigned, "u",  &m->n_autovts           },
134                 { "org.freedesktop.login1.Manager", "KillOnlyUsers",         bus_property_append_strv,     "as", m->kill_only_users      },
135                 { "org.freedesktop.login1.Manager", "KillExcludeUsers",      bus_property_append_strv,     "as", m->kill_exclude_users   },
136                 { "org.freedesktop.login1.Manager", "KillUserProcesses",     bus_property_append_bool,     "b",  &m->kill_user_processes },
137                 { NULL, NULL, NULL, NULL, NULL }
138         };
139
140         DBusError error;
141         DBusMessage *reply = NULL;
142
143         assert(connection);
144         assert(message);
145         assert(m);
146
147         dbus_error_init(&error);
148
149         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
150                 char *introspection = NULL;
151                 FILE *f;
152                 Iterator i;
153                 Session *session;
154                 Seat *seat;
155                 User *user;
156                 size_t size;
157                 char *p;
158
159                 if (!(reply = dbus_message_new_method_return(message)))
160                         goto oom;
161
162                 /* We roll our own introspection code here, instead of
163                  * relying on bus_default_message_handler() because we
164                  * need to generate our introspection string
165                  * dynamically. */
166
167                 if (!(f = open_memstream(&introspection, &size)))
168                         goto oom;
169
170                 fputs(INTROSPECTION_BEGIN, f);
171
172                 HASHMAP_FOREACH(seat, m->seats, i) {
173                         p = bus_path_escape(seat->id);
174
175                         if (p) {
176                                 fprintf(f, "<node name=\"seat/%s\"/>", p);
177                                 free(p);
178                         }
179                 }
180
181                 HASHMAP_FOREACH(user, m->users, i)
182                         fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
183
184                 HASHMAP_FOREACH(session, m->sessions, i) {
185                         p = bus_path_escape(session->id);
186
187                         if (p) {
188                                 fprintf(f, "<node name=\"session/%s\"/>", p);
189                                 free(p);
190                         }
191                 }
192
193                 fputs(INTROSPECTION_END, f);
194
195                 if (ferror(f)) {
196                         fclose(f);
197                         free(introspection);
198                         goto oom;
199                 }
200
201                 fclose(f);
202
203                 if (!introspection)
204                         goto oom;
205
206                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
207                         free(introspection);
208                         goto oom;
209                 }
210
211                 free(introspection);
212         } else
213                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
214
215         if (reply) {
216                 if (!dbus_connection_send(connection, reply, NULL))
217                         goto oom;
218
219                 dbus_message_unref(reply);
220         }
221
222         return DBUS_HANDLER_RESULT_HANDLED;
223
224 oom:
225         if (reply)
226                 dbus_message_unref(reply);
227
228         dbus_error_free(&error);
229
230         return DBUS_HANDLER_RESULT_NEED_MEMORY;
231 }
232
233 const DBusObjectPathVTable bus_manager_vtable = {
234         .message_function = manager_message_handler
235 };