chiark / gitweb /
mechanisms: add mechanisms to change system locale and clock
[elogind.git] / src / logind-seat-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-seat.h"
26 #include "dbus-common.h"
27 #include "util.h"
28
29 #define BUS_SEAT_INTERFACE \
30         " <interface name=\"org.freedesktop.login1.Seat\">\n"           \
31         "  <method name=\"Terminate\"/>\n"                              \
32         "  <method name=\"ActivateSession\">\n"                         \
33         "   <arg name=\"id\" type=\"s\"/>\n"                            \
34         "  </method>\n"                                                 \
35         "  <property name=\"Id\" type=\"s\" access=\"read\"/>\n"        \
36         "  <property name=\"ActiveSession\" type=\"so\" access=\"read\"/>\n" \
37         "  <property name=\"CanActivateSessions\" type=\"b\" access=\"read\"/>\n" \
38         "  <property name=\"Sessions\" type=\"a(so)\" access=\"read\"/>\n" \
39         " </interface>\n"                                               \
40
41 #define INTROSPECTION                                                   \
42         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
43         "<node>\n"                                                      \
44         BUS_SEAT_INTERFACE                                              \
45         BUS_PROPERTIES_INTERFACE                                        \
46         BUS_PEER_INTERFACE                                              \
47         BUS_INTROSPECTABLE_INTERFACE                                    \
48         "</node>\n"
49
50 #define INTERFACES_LIST                              \
51         BUS_GENERIC_INTERFACES_LIST                  \
52         "org.freedesktop.login1.Seat\0"
53
54 static int bus_seat_append_active(DBusMessageIter *i, const char *property, void *data) {
55         DBusMessageIter sub;
56         Seat *s = data;
57         const char *id, *path;
58         char *p = NULL;
59
60         assert(i);
61         assert(property);
62         assert(s);
63
64         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
65                 return -ENOMEM;
66
67         if (s->active) {
68                 id = s->active->id;
69                 path = p = session_bus_path(s->active);
70
71                 if (!p)
72                         return -ENOMEM;
73         } else {
74                 id = "";
75                 path = "/";
76         }
77
78         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
79             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
80                 free(p);
81                 return -ENOMEM;
82         }
83
84         free(p);
85
86         if (!dbus_message_iter_close_container(i, &sub))
87                 return -ENOMEM;
88
89         return 0;
90 }
91
92 static int bus_seat_append_sessions(DBusMessageIter *i, const char *property, void *data) {
93         DBusMessageIter sub, sub2;
94         Seat *s = data;
95         Session *session;
96
97         assert(i);
98         assert(property);
99         assert(s);
100
101         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "so", &sub))
102                 return -ENOMEM;
103
104         LIST_FOREACH(sessions_by_seat, session, s->sessions) {
105                 char *p;
106
107                 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
108                         return -ENOMEM;
109
110                 p = session_bus_path(session);
111                 if (!p)
112                         return -ENOMEM;
113
114                 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
115                     !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
116                         free(p);
117                         return -ENOMEM;
118                 }
119
120                 free(p);
121
122                 if (!dbus_message_iter_close_container(&sub, &sub2))
123                         return -ENOMEM;
124         }
125
126         if (!dbus_message_iter_close_container(i, &sub))
127                 return -ENOMEM;
128
129         return 0;
130 }
131
132
133 static int bus_seat_append_can_activate(DBusMessageIter *i, const char *property, void *data) {
134         Seat *s = data;
135         dbus_bool_t b;
136
137         assert(i);
138         assert(property);
139         assert(s);
140
141         b = s->manager->vtconsole == s;
142
143         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
144                 return -ENOMEM;
145
146         return 0;
147 }
148
149 static int get_seat_for_path(Manager *m, const char *path, Seat **_s) {
150         Seat *s;
151         char *id;
152
153         assert(m);
154         assert(path);
155         assert(_s);
156
157         if (!startswith(path, "/org/freedesktop/login1/seat/"))
158                 return -EINVAL;
159
160         id = bus_path_unescape(path + 29);
161         if (!id)
162                 return -ENOMEM;
163
164         s = hashmap_get(m->seats, id);
165         free(id);
166
167         if (!s)
168                 return -ENOENT;
169
170         *_s = s;
171         return 0;
172 }
173
174 static DBusHandlerResult seat_message_dispatch(
175                 Seat *s,
176                 DBusConnection *connection,
177                 DBusMessage *message) {
178
179         const BusProperty properties[] = {
180                 { "org.freedesktop.login1.Seat", "Id",                  bus_property_append_string,   "s",     s->id },
181                 { "org.freedesktop.login1.Seat", "ActiveSession",       bus_seat_append_active,       "(so)",  s     },
182                 { "org.freedesktop.login1.Seat", "CanActivateSessions", bus_seat_append_can_activate, "b",     s     },
183                 { "org.freedesktop.login1.Seat", "Sessions",            bus_seat_append_sessions,     "a(so)", s     },
184                 { NULL, NULL, NULL, NULL, NULL }
185         };
186
187         assert(s);
188         assert(connection);
189         assert(message);
190
191         return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
192 }
193
194 static DBusHandlerResult seat_message_handler(
195                 DBusConnection *connection,
196                 DBusMessage *message,
197                 void *userdata) {
198
199         Manager *m = userdata;
200         Seat *s;
201         int r;
202
203         r = get_seat_for_path(m, dbus_message_get_path(message), &s);
204         if (r < 0) {
205
206                 if (r == -ENOMEM)
207                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
208
209                 if (r == -ENOENT) {
210                         DBusError e;
211
212                         dbus_error_init(&e);
213                         dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown seat");
214                         return bus_send_error_reply(connection, message, &e, r);
215                 }
216
217                 return bus_send_error_reply(connection, message, NULL, r);
218         }
219
220         return seat_message_dispatch(s, connection, message);
221 }
222
223 const DBusObjectPathVTable bus_seat_vtable = {
224         .message_function = seat_message_handler
225 };
226
227 char *seat_bus_path(Seat *s) {
228         char *t, *r;
229
230         assert(s);
231
232         t = bus_path_escape(s->id);
233         if (!t)
234                 return NULL;
235
236         r = strappend("/org/freedesktop/login1/seat/", t);
237         free(t);
238
239         return r;
240 }