• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013 David Herrmann <dh.herrmann@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 /*
27  * DBus Helpers
28  * This file contains the dbus mainloop integration and several helpers to
29  * make lowlevel libdbus access easier.
30  */
31 
32 #include "config.h"
33 
34 #include <dbus/dbus.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/epoll.h>
42 #include <sys/eventfd.h>
43 #include <sys/timerfd.h>
44 #include <unistd.h>
45 #include <wayland-server.h>
46 
47 #include <libweston/libweston.h>
48 #include "dbus.h"
49 
50 /*
51  * DBus Mainloop Integration
52  * weston_dbus_bind() and weston_dbus_unbind() allow to bind an existing
53  * DBusConnection to an existing wl_event_loop object. All dbus dispatching
54  * is then nicely integrated into the wayland event loop.
55  * Note that this only provides basic watch and timeout dispatching. No
56  * remote thread wakeup, signal handling or other dbus insanity is supported.
57  * This is fine as long as you don't use any of the deprecated libdbus
58  * interfaces (like waking up remote threads..). There is really no rational
59  * reason to support these.
60  */
61 
weston_dbus_dispatch_watch(int fd,uint32_t mask,void * data)62 static int weston_dbus_dispatch_watch(int fd, uint32_t mask, void *data)
63 {
64 	DBusWatch *watch = data;
65 	uint32_t flags = 0;
66 
67 	if (dbus_watch_get_enabled(watch)) {
68 		if (mask & WL_EVENT_READABLE)
69 			flags |= DBUS_WATCH_READABLE;
70 		if (mask & WL_EVENT_WRITABLE)
71 			flags |= DBUS_WATCH_WRITABLE;
72 		if (mask & WL_EVENT_HANGUP)
73 			flags |= DBUS_WATCH_HANGUP;
74 		if (mask & WL_EVENT_ERROR)
75 			flags |= DBUS_WATCH_ERROR;
76 
77 		dbus_watch_handle(watch, flags);
78 	}
79 
80 	return 0;
81 }
82 
weston_dbus_add_watch(DBusWatch * watch,void * data)83 static dbus_bool_t weston_dbus_add_watch(DBusWatch *watch, void *data)
84 {
85 	struct wl_event_loop *loop = data;
86 	struct wl_event_source *s;
87 	int fd;
88 	uint32_t mask = 0, flags;
89 
90 	if (dbus_watch_get_enabled(watch)) {
91 		flags = dbus_watch_get_flags(watch);
92 		if (flags & DBUS_WATCH_READABLE)
93 			mask |= WL_EVENT_READABLE;
94 		if (flags & DBUS_WATCH_WRITABLE)
95 			mask |= WL_EVENT_WRITABLE;
96 	}
97 
98 	fd = dbus_watch_get_unix_fd(watch);
99 	s = wl_event_loop_add_fd(loop, fd, mask, weston_dbus_dispatch_watch,
100 				 watch);
101 	if (!s)
102 		return FALSE;
103 
104 	dbus_watch_set_data(watch, s, NULL);
105 	return TRUE;
106 }
107 
weston_dbus_remove_watch(DBusWatch * watch,void * data)108 static void weston_dbus_remove_watch(DBusWatch *watch, void *data)
109 {
110 	struct wl_event_source *s;
111 
112 	s = dbus_watch_get_data(watch);
113 	if (!s)
114 		return;
115 
116 	wl_event_source_remove(s);
117 }
118 
weston_dbus_toggle_watch(DBusWatch * watch,void * data)119 static void weston_dbus_toggle_watch(DBusWatch *watch, void *data)
120 {
121 	struct wl_event_source *s;
122 	uint32_t mask = 0, flags;
123 
124 	s = dbus_watch_get_data(watch);
125 	if (!s)
126 		return;
127 
128 	if (dbus_watch_get_enabled(watch)) {
129 		flags = dbus_watch_get_flags(watch);
130 		if (flags & DBUS_WATCH_READABLE)
131 			mask |= WL_EVENT_READABLE;
132 		if (flags & DBUS_WATCH_WRITABLE)
133 			mask |= WL_EVENT_WRITABLE;
134 	}
135 
136 	wl_event_source_fd_update(s, mask);
137 }
138 
weston_dbus_dispatch_timeout(void * data)139 static int weston_dbus_dispatch_timeout(void *data)
140 {
141 	DBusTimeout *timeout = data;
142 
143 	if (dbus_timeout_get_enabled(timeout))
144 		dbus_timeout_handle(timeout);
145 
146 	return 0;
147 }
148 
weston_dbus_adjust_timeout(DBusTimeout * timeout,struct wl_event_source * s)149 static int weston_dbus_adjust_timeout(DBusTimeout *timeout,
150 				      struct wl_event_source *s)
151 {
152 	int64_t t = 0;
153 
154 	if (dbus_timeout_get_enabled(timeout))
155 		t = dbus_timeout_get_interval(timeout);
156 
157 	return wl_event_source_timer_update(s, t);
158 }
159 
weston_dbus_add_timeout(DBusTimeout * timeout,void * data)160 static dbus_bool_t weston_dbus_add_timeout(DBusTimeout *timeout, void *data)
161 {
162 	struct wl_event_loop *loop = data;
163 	struct wl_event_source *s;
164 	int r;
165 
166 	s = wl_event_loop_add_timer(loop, weston_dbus_dispatch_timeout,
167 				    timeout);
168 	if (!s)
169 		return FALSE;
170 
171 	r = weston_dbus_adjust_timeout(timeout, s);
172 	if (r < 0) {
173 		wl_event_source_remove(s);
174 		return FALSE;
175 	}
176 
177 	dbus_timeout_set_data(timeout, s, NULL);
178 	return TRUE;
179 }
180 
weston_dbus_remove_timeout(DBusTimeout * timeout,void * data)181 static void weston_dbus_remove_timeout(DBusTimeout *timeout, void *data)
182 {
183 	struct wl_event_source *s;
184 
185 	s = dbus_timeout_get_data(timeout);
186 	if (!s)
187 		return;
188 
189 	wl_event_source_remove(s);
190 }
191 
weston_dbus_toggle_timeout(DBusTimeout * timeout,void * data)192 static void weston_dbus_toggle_timeout(DBusTimeout *timeout, void *data)
193 {
194 	struct wl_event_source *s;
195 
196 	s = dbus_timeout_get_data(timeout);
197 	if (!s)
198 		return;
199 
200 	weston_dbus_adjust_timeout(timeout, s);
201 }
202 
weston_dbus_dispatch(int fd,uint32_t mask,void * data)203 static int weston_dbus_dispatch(int fd, uint32_t mask, void *data)
204 {
205 	DBusConnection *c = data;
206 	int r;
207 
208 	do {
209 		r = dbus_connection_dispatch(c);
210 		if (r == DBUS_DISPATCH_COMPLETE)
211 			r = 0;
212 		else if (r == DBUS_DISPATCH_DATA_REMAINS)
213 			r = -EAGAIN;
214 		else if (r == DBUS_DISPATCH_NEED_MEMORY)
215 			r = -ENOMEM;
216 		else
217 			r = -EIO;
218 	} while (r == -EAGAIN);
219 
220 	if (r)
221 		weston_log("cannot dispatch dbus events: %d\n", r);
222 
223 	return 0;
224 }
225 
weston_dbus_bind(struct wl_event_loop * loop,DBusConnection * c,struct wl_event_source ** ctx_out)226 static int weston_dbus_bind(struct wl_event_loop *loop, DBusConnection *c,
227 			    struct wl_event_source **ctx_out)
228 {
229 	bool b;
230 	int r, fd;
231 
232 	/* Idle events cannot reschedule themselves, therefore we use a dummy
233 	 * event-fd and mark it for post-dispatch. Hence, the dbus
234 	 * dispatcher is called after every dispatch-round.
235 	 * This is required as dbus doesn't allow dispatching events from
236 	 * within its own event sources. */
237 	fd = eventfd(0, EFD_CLOEXEC);
238 	if (fd < 0)
239 		return -errno;
240 
241 	*ctx_out = wl_event_loop_add_fd(loop, fd, 0, weston_dbus_dispatch, c);
242 	close(fd);
243 
244 	if (!*ctx_out)
245 		return -ENOMEM;
246 
247 	wl_event_source_check(*ctx_out);
248 
249 	b = dbus_connection_set_watch_functions(c,
250 						weston_dbus_add_watch,
251 						weston_dbus_remove_watch,
252 						weston_dbus_toggle_watch,
253 						loop,
254 						NULL);
255 	if (!b) {
256 		r = -ENOMEM;
257 		goto error;
258 	}
259 
260 	b = dbus_connection_set_timeout_functions(c,
261 						  weston_dbus_add_timeout,
262 						  weston_dbus_remove_timeout,
263 						  weston_dbus_toggle_timeout,
264 						  loop,
265 						  NULL);
266 	if (!b) {
267 		r = -ENOMEM;
268 		goto error;
269 	}
270 
271 	dbus_connection_ref(c);
272 	return 0;
273 
274 error:
275 	dbus_connection_set_timeout_functions(c, NULL, NULL, NULL,
276 					      NULL, NULL);
277 	dbus_connection_set_watch_functions(c, NULL, NULL, NULL,
278 					    NULL, NULL);
279 	wl_event_source_remove(*ctx_out);
280 	*ctx_out = NULL;
281 	return r;
282 }
283 
weston_dbus_unbind(DBusConnection * c,struct wl_event_source * ctx)284 static void weston_dbus_unbind(DBusConnection *c, struct wl_event_source *ctx)
285 {
286 	dbus_connection_set_timeout_functions(c, NULL, NULL, NULL,
287 					      NULL, NULL);
288 	dbus_connection_set_watch_functions(c, NULL, NULL, NULL,
289 					    NULL, NULL);
290 	dbus_connection_unref(c);
291 	wl_event_source_remove(ctx);
292 }
293 
294 /*
295  * Convenience Helpers
296  * Several convenience helpers are provided to make using dbus in weston
297  * easier. We don't use any of the gdbus or qdbus helpers as they pull in
298  * huge dependencies and actually are quite awful to use. Instead, we only
299  * use the basic low-level libdbus library.
300  */
301 
weston_dbus_open(struct wl_event_loop * loop,DBusBusType bus,DBusConnection ** out,struct wl_event_source ** ctx_out)302 int weston_dbus_open(struct wl_event_loop *loop, DBusBusType bus,
303 		     DBusConnection **out, struct wl_event_source **ctx_out)
304 {
305 	DBusConnection *c;
306 	int r;
307 
308 	/* Ihhh, global state.. stupid dbus. */
309 	dbus_connection_set_change_sigpipe(FALSE);
310 
311 	/* This is actually synchronous. It blocks for some authentication and
312 	 * setup. We just trust the dbus-server here and accept this blocking
313 	 * call. There is no real reason to complicate things further and make
314 	 * this asynchronous/non-blocking. A context should be created during
315 	 * thead/process/app setup, so blocking calls should be fine. */
316 	c = dbus_bus_get_private(bus, NULL);
317 	if (!c)
318 		return -EIO;
319 
320 	dbus_connection_set_exit_on_disconnect(c, FALSE);
321 
322 	r = weston_dbus_bind(loop, c, ctx_out);
323 	if (r < 0)
324 		goto error;
325 
326 	*out = c;
327 	return r;
328 
329 error:
330 	dbus_connection_close(c);
331 	dbus_connection_unref(c);
332 	return r;
333 }
334 
weston_dbus_close(DBusConnection * c,struct wl_event_source * ctx)335 void weston_dbus_close(DBusConnection *c, struct wl_event_source *ctx)
336 {
337 	weston_dbus_unbind(c, ctx);
338 	dbus_connection_close(c);
339 	dbus_connection_unref(c);
340 }
341 
weston_dbus_add_match(DBusConnection * c,const char * format,...)342 int weston_dbus_add_match(DBusConnection *c, const char *format, ...)
343 {
344 	DBusError err;
345 	int r;
346 	va_list list;
347 	char *str;
348 
349 	va_start(list, format);
350 	r = vasprintf(&str, format, list);
351 	va_end(list);
352 
353 	if (r < 0)
354 		return -ENOMEM;
355 
356 	dbus_error_init(&err);
357 	dbus_bus_add_match(c, str, &err);
358 	free(str);
359 	if (dbus_error_is_set(&err)) {
360 		dbus_error_free(&err);
361 		return -EIO;
362 	}
363 
364 	return 0;
365 }
366 
weston_dbus_add_match_signal(DBusConnection * c,const char * sender,const char * iface,const char * member,const char * path)367 int weston_dbus_add_match_signal(DBusConnection *c, const char *sender,
368 				 const char *iface, const char *member,
369 				 const char *path)
370 {
371 	return weston_dbus_add_match(c,
372 				     "type='signal',"
373 				     "sender='%s',"
374 				     "interface='%s',"
375 				     "member='%s',"
376 				     "path='%s'",
377 				     sender, iface, member, path);
378 }
379 
weston_dbus_remove_match(DBusConnection * c,const char * format,...)380 void weston_dbus_remove_match(DBusConnection *c, const char *format, ...)
381 {
382 	int r;
383 	va_list list;
384 	char *str;
385 
386 	va_start(list, format);
387 	r = vasprintf(&str, format, list);
388 	va_end(list);
389 
390 	if (r < 0)
391 		return;
392 
393 	dbus_bus_remove_match(c, str, NULL);
394 	free(str);
395 }
396 
weston_dbus_remove_match_signal(DBusConnection * c,const char * sender,const char * iface,const char * member,const char * path)397 void weston_dbus_remove_match_signal(DBusConnection *c, const char *sender,
398 				     const char *iface, const char *member,
399 				     const char *path)
400 {
401 	weston_dbus_remove_match(c,
402 				 "type='signal',"
403 				 "sender='%s',"
404 				 "interface='%s',"
405 				 "member='%s',"
406 				 "path='%s'",
407 				 sender, iface, member, path);
408 }
409