David Herrmann | 59ab900 | 2013-10-22 00:28:06 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2013 David Herrmann <dh.herrmann@gmail.com> |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * DBus Helpers |
| 25 | * This file contains the dbus mainloop integration and several helpers to |
| 26 | * make lowlevel libdbus access easier. |
| 27 | */ |
| 28 | |
| 29 | #include "config.h" |
| 30 | |
| 31 | #include <dbus/dbus.h> |
| 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <stdbool.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <sys/epoll.h> |
| 38 | #include <sys/eventfd.h> |
| 39 | #include <sys/timerfd.h> |
| 40 | #include <unistd.h> |
| 41 | #include <wayland-server.h> |
| 42 | |
| 43 | #include "compositor.h" |
| 44 | #include "dbus.h" |
| 45 | |
| 46 | /* |
| 47 | * DBus Mainloop Integration |
| 48 | * weston_dbus_bind() and weston_dbus_unbind() allow to bind an existing |
| 49 | * DBusConnection to an existing wl_event_loop object. All dbus dispatching |
| 50 | * is then nicely integrated into the wayland event loop. |
| 51 | * Note that this only provides basic watch and timeout dispatching. No |
| 52 | * remote thread wakeup, signal handling or other dbus insanity is supported. |
| 53 | * This is fine as long as you don't use any of the deprecated libdbus |
| 54 | * interfaces (like waking up remote threads..). There is really no rational |
| 55 | * reason to support these. |
| 56 | */ |
| 57 | |
| 58 | static int weston_dbus_dispatch_watch(int fd, uint32_t mask, void *data) |
| 59 | { |
| 60 | DBusWatch *watch = data; |
| 61 | uint32_t flags = 0; |
| 62 | |
| 63 | if (dbus_watch_get_enabled(watch)) { |
| 64 | if (mask & WL_EVENT_READABLE) |
| 65 | flags |= DBUS_WATCH_READABLE; |
| 66 | if (mask & WL_EVENT_WRITABLE) |
| 67 | flags |= DBUS_WATCH_WRITABLE; |
| 68 | if (mask & WL_EVENT_HANGUP) |
| 69 | flags |= DBUS_WATCH_HANGUP; |
| 70 | if (mask & WL_EVENT_ERROR) |
| 71 | flags |= DBUS_WATCH_ERROR; |
| 72 | |
| 73 | dbus_watch_handle(watch, flags); |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static dbus_bool_t weston_dbus_add_watch(DBusWatch *watch, void *data) |
| 80 | { |
| 81 | struct wl_event_loop *loop = data; |
| 82 | struct wl_event_source *s; |
| 83 | int fd; |
| 84 | uint32_t mask = 0, flags; |
| 85 | |
| 86 | if (dbus_watch_get_enabled(watch)) { |
| 87 | flags = dbus_watch_get_flags(watch); |
| 88 | if (flags & DBUS_WATCH_READABLE) |
| 89 | mask |= WL_EVENT_READABLE; |
| 90 | if (flags & DBUS_WATCH_WRITABLE) |
| 91 | mask |= WL_EVENT_WRITABLE; |
| 92 | } |
| 93 | |
| 94 | fd = dbus_watch_get_unix_fd(watch); |
| 95 | s = wl_event_loop_add_fd(loop, fd, mask, weston_dbus_dispatch_watch, |
| 96 | watch); |
| 97 | if (!s) |
| 98 | return FALSE; |
| 99 | |
| 100 | dbus_watch_set_data(watch, s, NULL); |
| 101 | return TRUE; |
| 102 | } |
| 103 | |
| 104 | static void weston_dbus_remove_watch(DBusWatch *watch, void *data) |
| 105 | { |
| 106 | struct wl_event_source *s; |
| 107 | |
| 108 | s = dbus_watch_get_data(watch); |
| 109 | if (!s) |
| 110 | return; |
| 111 | |
| 112 | wl_event_source_remove(s); |
| 113 | } |
| 114 | |
| 115 | static void weston_dbus_toggle_watch(DBusWatch *watch, void *data) |
| 116 | { |
| 117 | struct wl_event_source *s; |
| 118 | uint32_t mask = 0, flags; |
| 119 | |
| 120 | s = dbus_watch_get_data(watch); |
| 121 | if (!s) |
| 122 | return; |
| 123 | |
| 124 | if (dbus_watch_get_enabled(watch)) { |
| 125 | flags = dbus_watch_get_flags(watch); |
| 126 | if (flags & DBUS_WATCH_READABLE) |
| 127 | mask |= WL_EVENT_READABLE; |
| 128 | if (flags & DBUS_WATCH_WRITABLE) |
| 129 | mask |= WL_EVENT_WRITABLE; |
| 130 | } |
| 131 | |
| 132 | wl_event_source_fd_update(s, mask); |
| 133 | } |
| 134 | |
| 135 | static int weston_dbus_dispatch_timeout(void *data) |
| 136 | { |
| 137 | DBusTimeout *timeout = data; |
| 138 | |
| 139 | if (dbus_timeout_get_enabled(timeout)) |
| 140 | dbus_timeout_handle(timeout); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int weston_dbus_adjust_timeout(DBusTimeout *timeout, |
| 146 | struct wl_event_source *s) |
| 147 | { |
| 148 | int64_t t = 0; |
| 149 | |
| 150 | if (dbus_timeout_get_enabled(timeout)) |
| 151 | t = dbus_timeout_get_interval(timeout); |
| 152 | |
| 153 | return wl_event_source_timer_update(s, t); |
| 154 | } |
| 155 | |
| 156 | static dbus_bool_t weston_dbus_add_timeout(DBusTimeout *timeout, void *data) |
| 157 | { |
| 158 | struct wl_event_loop *loop = data; |
| 159 | struct wl_event_source *s; |
| 160 | int r; |
| 161 | |
| 162 | s = wl_event_loop_add_timer(loop, weston_dbus_dispatch_timeout, |
| 163 | timeout); |
| 164 | if (!s) |
| 165 | return FALSE; |
| 166 | |
| 167 | r = weston_dbus_adjust_timeout(timeout, s); |
| 168 | if (r < 0) { |
| 169 | wl_event_source_remove(s); |
| 170 | return FALSE; |
| 171 | } |
| 172 | |
| 173 | dbus_timeout_set_data(timeout, s, NULL); |
| 174 | return TRUE; |
| 175 | } |
| 176 | |
| 177 | static void weston_dbus_remove_timeout(DBusTimeout *timeout, void *data) |
| 178 | { |
| 179 | struct wl_event_source *s; |
| 180 | |
| 181 | s = dbus_timeout_get_data(timeout); |
| 182 | if (!s) |
| 183 | return; |
| 184 | |
| 185 | wl_event_source_remove(s); |
| 186 | } |
| 187 | |
| 188 | static void weston_dbus_toggle_timeout(DBusTimeout *timeout, void *data) |
| 189 | { |
| 190 | struct wl_event_source *s; |
| 191 | |
| 192 | s = dbus_timeout_get_data(timeout); |
| 193 | if (!s) |
| 194 | return; |
| 195 | |
| 196 | weston_dbus_adjust_timeout(timeout, s); |
| 197 | } |
| 198 | |
| 199 | static int weston_dbus_dispatch(int fd, uint32_t mask, void *data) |
| 200 | { |
| 201 | DBusConnection *c = data; |
| 202 | int r; |
| 203 | |
| 204 | do { |
| 205 | r = dbus_connection_dispatch(c); |
| 206 | if (r == DBUS_DISPATCH_COMPLETE) |
| 207 | r = 0; |
| 208 | else if (r == DBUS_DISPATCH_DATA_REMAINS) |
| 209 | r = -EAGAIN; |
| 210 | else if (r == DBUS_DISPATCH_NEED_MEMORY) |
| 211 | r = -ENOMEM; |
| 212 | else |
| 213 | r = -EIO; |
| 214 | } while (r == -EAGAIN); |
| 215 | |
| 216 | if (r) |
| 217 | weston_log("cannot dispatch dbus events: %d\n", r); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int weston_dbus_bind(struct wl_event_loop *loop, DBusConnection *c, |
| 223 | struct wl_event_source **ctx_out) |
| 224 | { |
| 225 | bool b; |
| 226 | int r, fd; |
| 227 | |
| 228 | /* Idle events cannot reschedule themselves, therefore we use a dummy |
| 229 | * event-fd and mark it for post-dispatch. Hence, the dbus |
| 230 | * dispatcher is called after every dispatch-round. |
| 231 | * This is required as dbus doesn't allow dispatching events from |
| 232 | * within its own event sources. */ |
| 233 | fd = eventfd(0, EFD_CLOEXEC); |
| 234 | if (fd < 0) |
| 235 | return -errno; |
| 236 | |
| 237 | *ctx_out = wl_event_loop_add_fd(loop, fd, 0, weston_dbus_dispatch, c); |
| 238 | close(fd); |
| 239 | |
| 240 | if (!*ctx_out) |
| 241 | return -ENOMEM; |
| 242 | |
| 243 | wl_event_source_check(*ctx_out); |
| 244 | |
| 245 | b = dbus_connection_set_watch_functions(c, |
| 246 | weston_dbus_add_watch, |
| 247 | weston_dbus_remove_watch, |
| 248 | weston_dbus_toggle_watch, |
| 249 | loop, |
| 250 | NULL); |
| 251 | if (!b) { |
| 252 | r = -ENOMEM; |
| 253 | goto error; |
| 254 | } |
| 255 | |
| 256 | b = dbus_connection_set_timeout_functions(c, |
| 257 | weston_dbus_add_timeout, |
| 258 | weston_dbus_remove_timeout, |
| 259 | weston_dbus_toggle_timeout, |
| 260 | loop, |
| 261 | NULL); |
| 262 | if (!b) { |
| 263 | r = -ENOMEM; |
| 264 | goto error; |
| 265 | } |
| 266 | |
| 267 | dbus_connection_ref(c); |
| 268 | return 0; |
| 269 | |
| 270 | error: |
| 271 | dbus_connection_set_timeout_functions(c, NULL, NULL, NULL, |
| 272 | NULL, NULL); |
| 273 | dbus_connection_set_watch_functions(c, NULL, NULL, NULL, |
| 274 | NULL, NULL); |
| 275 | wl_event_source_remove(*ctx_out); |
| 276 | *ctx_out = NULL; |
| 277 | return r; |
| 278 | } |
| 279 | |
| 280 | static void weston_dbus_unbind(DBusConnection *c, struct wl_event_source *ctx) |
| 281 | { |
| 282 | dbus_connection_set_timeout_functions(c, NULL, NULL, NULL, |
| 283 | NULL, NULL); |
| 284 | dbus_connection_set_watch_functions(c, NULL, NULL, NULL, |
| 285 | NULL, NULL); |
| 286 | dbus_connection_unref(c); |
| 287 | wl_event_source_remove(ctx); |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Convenience Helpers |
| 292 | * Several convenience helpers are provided to make using dbus in weston |
| 293 | * easier. We don't use any of the gdbus or qdbus helpers as they pull in |
| 294 | * huge dependencies and actually are quite awful to use. Instead, we only |
| 295 | * use the basic low-level libdbus library. |
| 296 | */ |
| 297 | |
| 298 | int weston_dbus_open(struct wl_event_loop *loop, DBusBusType bus, |
| 299 | DBusConnection **out, struct wl_event_source **ctx_out) |
| 300 | { |
| 301 | DBusConnection *c; |
| 302 | int r; |
| 303 | |
| 304 | /* Ihhh, global state.. stupid dbus. */ |
| 305 | dbus_connection_set_change_sigpipe(FALSE); |
| 306 | |
| 307 | /* This is actually synchronous. It blocks for some authentication and |
| 308 | * setup. We just trust the dbus-server here and accept this blocking |
| 309 | * call. There is no real reason to complicate things further and make |
| 310 | * this asynchronous/non-blocking. A context should be created during |
| 311 | * thead/process/app setup, so blocking calls should be fine. */ |
| 312 | c = dbus_bus_get_private(bus, NULL); |
| 313 | if (!c) |
| 314 | return -EIO; |
| 315 | |
| 316 | dbus_connection_set_exit_on_disconnect(c, FALSE); |
| 317 | |
| 318 | r = weston_dbus_bind(loop, c, ctx_out); |
| 319 | if (r < 0) |
| 320 | goto error; |
| 321 | |
| 322 | *out = c; |
| 323 | return r; |
| 324 | |
| 325 | error: |
| 326 | dbus_connection_close(c); |
| 327 | dbus_connection_unref(c); |
| 328 | return r; |
| 329 | } |
| 330 | |
| 331 | void weston_dbus_close(DBusConnection *c, struct wl_event_source *ctx) |
| 332 | { |
| 333 | weston_dbus_unbind(c, ctx); |
| 334 | dbus_connection_close(c); |
| 335 | dbus_connection_unref(c); |
| 336 | } |