xref: /freebsd/contrib/wpa/wpa_supplicant/dbus/dbus_common.c (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
1 /*
2  * wpa_supplicant D-Bus control interface - common functionality
3  * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4  * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
5  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6  *
7  * This software may be distributed under the terms of the BSD license.
8  * See README for more details.
9  */
10 
11 #include "utils/includes.h"
12 #include <dbus/dbus.h>
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "dbus_common.h"
17 #include "dbus_common_i.h"
18 #include "dbus_new.h"
19 #include "../wpa_supplicant_i.h"
20 
21 
22 #ifndef SIGPOLL
23 #ifdef SIGIO
24 /*
25  * If we do not have SIGPOLL, try to use SIGIO instead. This is needed for
26  * FreeBSD.
27  */
28 #define SIGPOLL SIGIO
29 #endif
30 #endif
31 
32 
dispatch_data(DBusConnection * con)33 static void dispatch_data(DBusConnection *con)
34 {
35 	while (dbus_connection_get_dispatch_status(con) ==
36 	       DBUS_DISPATCH_DATA_REMAINS)
37 		dbus_connection_dispatch(con);
38 }
39 
40 
41 /**
42  * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
43  *     claiming bus name
44  * @eloop_ctx: the DBusConnection to dispatch on
45  * @timeout_ctx: unused
46  *
47  * If clients are quick to notice that service claimed its bus name,
48  * there may have been messages that came in before initialization was
49  * all finished.  Dispatch those here.
50  */
dispatch_initial_dbus_messages(void * eloop_ctx,void * timeout_ctx)51 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
52 {
53 	DBusConnection *con = eloop_ctx;
54 	dispatch_data(con);
55 }
56 
57 
process_watch(struct wpas_dbus_priv * priv,DBusWatch * watch,eloop_event_type type)58 static void process_watch(struct wpas_dbus_priv *priv,
59 			  DBusWatch *watch, eloop_event_type type)
60 {
61 	dbus_connection_ref(priv->con);
62 
63 	priv->should_dispatch = 0;
64 
65 	if (type == EVENT_TYPE_READ)
66 		dbus_watch_handle(watch, DBUS_WATCH_READABLE);
67 	else if (type == EVENT_TYPE_WRITE)
68 		dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
69 	else if (type == EVENT_TYPE_EXCEPTION)
70 		dbus_watch_handle(watch, DBUS_WATCH_ERROR);
71 
72 	if (priv->should_dispatch) {
73 		dispatch_data(priv->con);
74 		priv->should_dispatch = 0;
75 	}
76 
77 	dbus_connection_unref(priv->con);
78 }
79 
80 
process_watch_exception(int sock,void * eloop_ctx,void * sock_ctx)81 static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
82 {
83 	process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
84 }
85 
86 
process_watch_read(int sock,void * eloop_ctx,void * sock_ctx)87 static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
88 {
89 	process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
90 }
91 
92 
process_watch_write(int sock,void * eloop_ctx,void * sock_ctx)93 static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
94 {
95 	process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
96 }
97 
98 
add_watch(DBusWatch * watch,void * data)99 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
100 {
101 	struct wpas_dbus_priv *priv = data;
102 	unsigned int flags;
103 	int fd;
104 
105 	if (!dbus_watch_get_enabled(watch))
106 		return TRUE;
107 
108 	flags = dbus_watch_get_flags(watch);
109 	fd = dbus_watch_get_unix_fd(watch);
110 
111 	if (eloop_register_sock(fd, EVENT_TYPE_EXCEPTION,
112 				process_watch_exception, priv, watch) < 0)
113 		return FALSE;
114 
115 	if ((flags & DBUS_WATCH_READABLE) &&
116 	    eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
117 				priv, watch) < 0)
118 		return FALSE;
119 	if ((flags & DBUS_WATCH_WRITABLE) &&
120 	    eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
121 				priv, watch) < 0)
122 		return FALSE;
123 
124 	dbus_watch_set_data(watch, priv, NULL);
125 
126 	return TRUE;
127 }
128 
129 
remove_watch(DBusWatch * watch,void * data)130 static void remove_watch(DBusWatch *watch, void *data)
131 {
132 	unsigned int flags;
133 	int fd;
134 
135 	flags = dbus_watch_get_flags(watch);
136 	fd = dbus_watch_get_unix_fd(watch);
137 
138 	eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
139 
140 	if (flags & DBUS_WATCH_READABLE)
141 		eloop_unregister_sock(fd, EVENT_TYPE_READ);
142 	if (flags & DBUS_WATCH_WRITABLE)
143 		eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
144 
145 	dbus_watch_set_data(watch, NULL, NULL);
146 }
147 
148 
watch_toggled(DBusWatch * watch,void * data)149 static void watch_toggled(DBusWatch *watch, void *data)
150 {
151 	if (dbus_watch_get_enabled(watch))
152 		add_watch(watch, data);
153 	else
154 		remove_watch(watch, data);
155 }
156 
157 
process_timeout(void * eloop_ctx,void * sock_ctx)158 static void process_timeout(void *eloop_ctx, void *sock_ctx)
159 {
160 	DBusTimeout *timeout = sock_ctx;
161 	dbus_timeout_handle(timeout);
162 }
163 
164 
add_timeout(DBusTimeout * timeout,void * data)165 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
166 {
167 	struct wpas_dbus_priv *priv = data;
168 
169 	if (!dbus_timeout_get_enabled(timeout))
170 		return TRUE;
171 
172 	eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
173 			       process_timeout, priv, timeout);
174 
175 	dbus_timeout_set_data(timeout, priv, NULL);
176 
177 	return TRUE;
178 }
179 
180 
remove_timeout(DBusTimeout * timeout,void * data)181 static void remove_timeout(DBusTimeout *timeout, void *data)
182 {
183 	struct wpas_dbus_priv *priv = data;
184 
185 	eloop_cancel_timeout(process_timeout, priv, timeout);
186 	dbus_timeout_set_data(timeout, NULL, NULL);
187 }
188 
189 
timeout_toggled(DBusTimeout * timeout,void * data)190 static void timeout_toggled(DBusTimeout *timeout, void *data)
191 {
192 	if (dbus_timeout_get_enabled(timeout))
193 		add_timeout(timeout, data);
194 	else
195 		remove_timeout(timeout, data);
196 }
197 
198 
process_wakeup_main(int sig,void * signal_ctx)199 static void process_wakeup_main(int sig, void *signal_ctx)
200 {
201 	struct wpas_dbus_priv *priv = signal_ctx;
202 
203 	if (sig != SIGPOLL || !priv->con)
204 		return;
205 
206 	if (dbus_connection_get_dispatch_status(priv->con) !=
207 	    DBUS_DISPATCH_DATA_REMAINS)
208 		return;
209 
210 	/* Only dispatch once - we do not want to starve other events */
211 	dbus_connection_ref(priv->con);
212 	dbus_connection_dispatch(priv->con);
213 	dbus_connection_unref(priv->con);
214 }
215 
216 
217 /**
218  * wakeup_main - Attempt to wake our mainloop up
219  * @data: dbus control interface private data
220  *
221  * Try to wake up the main eloop so it will process
222  * dbus events that may have happened.
223  */
wakeup_main(void * data)224 static void wakeup_main(void *data)
225 {
226 	struct wpas_dbus_priv *priv = data;
227 
228 	/* Use SIGPOLL to break out of the eloop select() */
229 	raise(SIGPOLL);
230 	priv->should_dispatch = 1;
231 }
232 
233 
234 /**
235  * integrate_with_eloop - Register our mainloop integration with dbus
236  * @connection: connection to the system message bus
237  * @priv: a dbus control interface data structure
238  * Returns: 0 on success, -1 on failure
239  */
integrate_with_eloop(struct wpas_dbus_priv * priv)240 static int integrate_with_eloop(struct wpas_dbus_priv *priv)
241 {
242 	if (!dbus_connection_set_watch_functions(priv->con, add_watch,
243 						 remove_watch, watch_toggled,
244 						 priv, NULL) ||
245 	    !dbus_connection_set_timeout_functions(priv->con, add_timeout,
246 						   remove_timeout,
247 						   timeout_toggled, priv,
248 						   NULL)) {
249 		wpa_printf(MSG_ERROR, "dbus: Failed to set callback functions");
250 		return -1;
251 	}
252 
253 	if (eloop_register_signal(SIGPOLL, process_wakeup_main, priv))
254 		return -1;
255 	dbus_connection_set_wakeup_main_function(priv->con, wakeup_main,
256 						 priv, NULL);
257 
258 	return 0;
259 }
260 
261 
disconnect_filter(DBusConnection * conn,DBusMessage * message,void * data)262 static DBusHandlerResult disconnect_filter(DBusConnection *conn,
263 					   DBusMessage *message, void *data)
264 {
265 	struct wpas_dbus_priv *priv = data;
266 
267 	if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
268 				   "Disconnected")) {
269 		wpa_printf(MSG_DEBUG, "dbus: bus disconnected, terminating");
270 		dbus_connection_set_exit_on_disconnect(conn, FALSE);
271 		wpa_supplicant_terminate_proc(priv->global);
272 		return DBUS_HANDLER_RESULT_HANDLED;
273 	} else
274 		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
275 }
276 
277 
wpas_dbus_init_common(struct wpas_dbus_priv * priv)278 static int wpas_dbus_init_common(struct wpas_dbus_priv *priv)
279 {
280 	DBusError error;
281 	int ret = 0;
282 
283 	/* Get a reference to the system bus */
284 	dbus_error_init(&error);
285 	priv->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
286 	if (priv->con) {
287 		dbus_connection_add_filter(priv->con, disconnect_filter, priv,
288 					   NULL);
289 	} else {
290 		wpa_printf(MSG_ERROR,
291 			   "dbus: Could not acquire the system bus: %s - %s",
292 			   error.name, error.message);
293 		ret = -1;
294 	}
295 	dbus_error_free(&error);
296 
297 	return ret;
298 }
299 
300 
wpas_dbus_init_common_finish(struct wpas_dbus_priv * priv)301 static int wpas_dbus_init_common_finish(struct wpas_dbus_priv *priv)
302 {
303 	/* Tell dbus about our mainloop integration functions */
304 	integrate_with_eloop(priv);
305 
306 	/*
307 	 * Dispatch initial DBus messages that may have come in since the bus
308 	 * name was claimed above. Happens when clients are quick to notice the
309 	 * service.
310 	 *
311 	 * FIXME: is there a better solution to this problem?
312 	 */
313 	eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
314 			       priv->con, NULL);
315 
316 	return 0;
317 }
318 
319 
wpas_dbus_deinit_common(struct wpas_dbus_priv * priv)320 static void wpas_dbus_deinit_common(struct wpas_dbus_priv *priv)
321 {
322 	if (priv->con) {
323 		eloop_cancel_timeout(dispatch_initial_dbus_messages,
324 				     priv->con, NULL);
325 		eloop_cancel_timeout(process_timeout, priv, ELOOP_ALL_CTX);
326 
327 		dbus_connection_set_watch_functions(priv->con, NULL, NULL,
328 						    NULL, NULL, NULL);
329 		dbus_connection_set_timeout_functions(priv->con, NULL, NULL,
330 						      NULL, NULL, NULL);
331 		dbus_connection_remove_filter(priv->con, disconnect_filter,
332 					      priv);
333 
334 		dbus_connection_unref(priv->con);
335 	}
336 
337 	os_free(priv);
338 }
339 
340 
wpas_dbus_init(struct wpa_global * global)341 struct wpas_dbus_priv * wpas_dbus_init(struct wpa_global *global)
342 {
343 	struct wpas_dbus_priv *priv;
344 
345 	priv = os_zalloc(sizeof(*priv));
346 	if (priv == NULL)
347 		return NULL;
348 	priv->global = global;
349 
350 	if (wpas_dbus_init_common(priv) < 0 ||
351 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
352 	    wpas_dbus_ctrl_iface_init(priv) < 0 ||
353 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
354 	    wpas_dbus_init_common_finish(priv) < 0) {
355 		wpas_dbus_deinit(priv);
356 		return NULL;
357 	}
358 
359 	return priv;
360 }
361 
362 
wpas_dbus_deinit(struct wpas_dbus_priv * priv)363 void wpas_dbus_deinit(struct wpas_dbus_priv *priv)
364 {
365 	if (priv == NULL)
366 		return;
367 
368 #ifdef CONFIG_CTRL_IFACE_DBUS_NEW
369 	wpas_dbus_ctrl_iface_deinit(priv);
370 #endif /* CONFIG_CTRL_IFACE_DBUS_NEW */
371 
372 	wpas_dbus_deinit_common(priv);
373 }
374