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