eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

dbus-proxy.c (20647B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2017-2020  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <stdio.h>
     28 #include <ell/ell.h>
     29 
     30 #include "client/agent-manager.h"
     31 #include "client/dbus-proxy.h"
     32 #include "client/display.h"
     33 #include "client/command.h"
     34 #include "client/properties.h"
     35 
     36 #define IWD_SERVICE		"net.connman.iwd"
     37 #define IWD_ROOT_PATH		"/"
     38 
     39 struct proxy_interface {
     40 	void *data;
     41 	char *path;
     42 	const struct proxy_interface_type *type;
     43 };
     44 
     45 static struct l_dbus *dbus;
     46 
     47 static struct l_queue *proxy_interfaces;
     48 static struct l_queue *proxy_interface_types;
     49 
     50 void proxy_properties_display_inline(const struct proxy_interface *proxy,
     51 					const char *margin,
     52 					unsigned int name_column_width,
     53 					unsigned int value_column_width)
     54 {
     55 	const void *data;
     56 	const struct proxy_interface_property *properties;
     57 	size_t i;
     58 
     59 	if (!proxy->type->properties)
     60 		return;
     61 
     62 	data = proxy_interface_get_data(proxy);
     63 	properties = proxy->type->properties;
     64 
     65 	for (i = 0; properties[i].name; i++) {
     66 		const char *str;
     67 
     68 		if (!properties[i].tostr)
     69 			continue;
     70 
     71 		str = properties[i].tostr(data);
     72 
     73 		display_table_row(MARGIN, 3, 8, properties[i].is_read_write ?
     74 				COLOR_BOLDGRAY("       *") : "",
     75 				name_column_width, properties[i].name,
     76 				value_column_width, str ? : "");
     77 	}
     78 }
     79 
     80 void proxy_properties_display_header(const char *caption, const char *margin,
     81 					unsigned int name_column_width,
     82 					unsigned int value_column_width)
     83 {
     84 	display_table_header(caption, "%s%-*s  %-*s  %-*s", margin,
     85 				8, "Settable",
     86 				name_column_width, "Property",
     87 				value_column_width, "Value");
     88 }
     89 
     90 void proxy_properties_display(const struct proxy_interface *proxy,
     91 				const char *caption, const char *margin,
     92 				unsigned int name_column_width,
     93 				unsigned int value_column_width)
     94 {
     95 	if (!proxy->type->properties)
     96 		return;
     97 
     98 	proxy_properties_display_header(caption, margin, name_column_width,
     99 					value_column_width);
    100 
    101 	proxy_properties_display_inline(proxy, margin, name_column_width,
    102 					value_column_width);
    103 }
    104 
    105 static const void *proxy_interface_property_tostr(
    106 					const struct proxy_interface *proxy,
    107 					const char *name)
    108 {
    109 	size_t i;
    110 	const struct proxy_interface_property *property_table =
    111 							proxy->type->properties;
    112 
    113 	for (i = 0; property_table[i].name; i++) {
    114 		if (strcmp(property_table[i].name, name))
    115 			continue;
    116 
    117 		if (!property_table[i].tostr)
    118 			break;
    119 
    120 		return property_table[i].tostr(proxy->data);
    121 	}
    122 
    123 	return NULL;
    124 }
    125 
    126 static void proxy_interface_property_update(struct proxy_interface *proxy,
    127 					const char *name,
    128 					struct l_dbus_message_iter *variant)
    129 {
    130 	size_t i;
    131 	const struct proxy_interface_property *property_table =
    132 							proxy->type->properties;
    133 
    134 	if (!property_table)
    135 		return;
    136 
    137 	for (i = 0; property_table[i].name; i++) {
    138 		if (strcmp(property_table[i].name, name))
    139 			continue;
    140 
    141 		if (!property_table[i].update)
    142 			return;
    143 
    144 		property_table[i].update(proxy->data, variant);
    145 
    146 		return;
    147 	}
    148 
    149 	l_debug("Unknown property name: %s for interface %s", name,
    150 							proxy->type->interface);
    151 }
    152 
    153 static void interface_update_properties(struct proxy_interface *proxy,
    154 					struct l_dbus_message_iter *changed,
    155 					struct l_dbus_message_iter *invalidated)
    156 {
    157 	const char *name;
    158 	struct l_dbus_message_iter variant;
    159 
    160 	while (l_dbus_message_iter_next_entry(changed, &name, &variant))
    161 		proxy_interface_property_update(proxy, name, &variant);
    162 
    163 	if (!invalidated)
    164 		return;
    165 
    166 	while (l_dbus_message_iter_next_entry(invalidated, &name))
    167 		proxy_interface_property_update(proxy, name, NULL);
    168 }
    169 
    170 static char *strdup_quoted_for_spaces(const char *str)
    171 {
    172 	const char *p;
    173 
    174 	for (p = str; *p; p++) {
    175 		if (*p != ' ')
    176 			continue;
    177 
    178 		return l_strdup_printf("\"%s\"", str);
    179 	}
    180 
    181 	return l_strdup(str);
    182 }
    183 
    184 char *proxy_property_str_completion(const struct proxy_interface_type *type,
    185 					proxy_property_match_func_t function,
    186 					const char *property_name,
    187 					const void *value, int state,
    188 					const char *extra_interface)
    189 {
    190 	static struct l_queue *match;
    191 	static const struct l_queue_entry *entry;
    192 
    193 	if (!state) {
    194 		match = proxy_interface_find_all(type->interface, function,
    195 									value);
    196 		if (!match)
    197 			return NULL;
    198 
    199 		entry = l_queue_get_entries(match);
    200 	}
    201 
    202 	while (entry) {
    203 		const struct proxy_interface *proxy = entry->data;
    204 		const char *str;
    205 
    206 		entry = entry->next;
    207 
    208 		if (extra_interface) {
    209 			const char *path = proxy_interface_get_path(proxy);
    210 			const struct proxy_interface *extra =
    211 				proxy_interface_find(extra_interface, path);
    212 
    213 			if (!extra)
    214 				continue;
    215 		}
    216 
    217 		str = proxy_interface_property_tostr(proxy, property_name);
    218 		if (!str)
    219 			goto done;
    220 
    221 		return strdup_quoted_for_spaces(str);
    222 	}
    223 done:
    224 	l_queue_destroy(match, NULL);
    225 	match = NULL;
    226 	entry = NULL;
    227 
    228 	return NULL;
    229 }
    230 
    231 static char *proxy_property_completion_value_options(const char **options,
    232 							const char *text,
    233 							int state)
    234 {
    235 	static int index;
    236 	static int len;
    237 	const char *opt;
    238 
    239 	if (!state) {
    240 		index = 0;
    241 		len = strlen(text);
    242 	}
    243 
    244 	while ((opt = options[index++])) {
    245 		if (strncmp(opt, text, len))
    246 			continue;
    247 
    248 		return l_strdup(opt);
    249 	}
    250 
    251 	return NULL;
    252 }
    253 
    254 char *proxy_property_completion(
    255 			const struct proxy_interface_property *properties,
    256 			const char *text, int state)
    257 {
    258 	static size_t i;
    259 	static size_t j;
    260 	static size_t len;
    261 	static bool first_pass;
    262 	const char *name;
    263 
    264 	if (!state) {
    265 		j = 0;
    266 		first_pass = true;
    267 	}
    268 
    269 	while (first_pass && (name = properties[j].name)) {
    270 		if (!properties[j].is_read_write)
    271 			goto next;
    272 
    273 		if (!command_line_find_token(name, 2))
    274 			goto next;
    275 
    276 		if (!properties[j].options)
    277 			goto next;
    278 
    279 		return proxy_property_completion_value_options(
    280 					properties[j].options, text,
    281 					state);
    282 next:
    283 		j++;
    284 	}
    285 
    286 	if (first_pass) {
    287 		i = 0;
    288 		first_pass = false;
    289 		len = strlen(text);
    290 	}
    291 
    292 	while ((name = properties[i].name)) {
    293 		if (!properties[i++].is_read_write)
    294 			continue;
    295 
    296 		if (strncmp(name, text, len))
    297 			continue;
    298 
    299 		return l_strdup(name);
    300 	}
    301 
    302 	return NULL;
    303 }
    304 
    305 static const struct proxy_interface_property *proxy_property_find(
    306 				const struct proxy_interface_property *types,
    307 				const char *name)
    308 {
    309 	size_t i;
    310 
    311 	for (i = 0; types[i].name; i++) {
    312 		if (strcmp(types[i].name, name))
    313 			continue;
    314 
    315 		return &types[i];
    316 	}
    317 
    318 	return NULL;
    319 }
    320 
    321 struct proxy_callback_data {
    322 	l_dbus_message_func_t callback;
    323 	void *user_data;
    324 };
    325 
    326 static void proxy_callback(struct l_dbus_message *message, void *user_data)
    327 {
    328 	struct proxy_callback_data *callback_data = user_data;
    329 	const struct proxy_interface *proxy;
    330 	const char *name;
    331 	const char *text;
    332 
    333 	if (callback_data->callback)
    334 		callback_data->callback(message, callback_data->user_data);
    335 
    336 	if (command_is_interactive_mode())
    337 		return;
    338 
    339 	if (l_dbus_message_get_error(message, &name, &text)) {
    340 		command_set_exit_status(EXIT_FAILURE);
    341 		goto quit;
    342 	}
    343 
    344 	proxy = callback_data->user_data;
    345 	if (!strcmp(proxy->type->interface, IWD_AGENT_MANAGER_INTERFACE) ||
    346 			!strcmp(proxy->type->interface, IWD_DAEMON_INTERFACE))
    347 		return;
    348 
    349 quit:
    350 	l_main_quit();
    351 }
    352 
    353 bool proxy_property_set(const struct proxy_interface *proxy, const char *name,
    354 			const char *value_str, l_dbus_message_func_t callback)
    355 {
    356 	struct l_dbus_message_builder *builder;
    357 	struct l_dbus_message *msg;
    358 	const struct proxy_interface_property *property;
    359 	struct proxy_callback_data *callback_data;
    360 
    361 	if (!proxy || !name)
    362 		return false;
    363 
    364 	property = proxy_property_find(proxy->type->properties, name);
    365 	if (!property)
    366 		return false;
    367 
    368 	if (!property->is_read_write)
    369 		return false;
    370 
    371 	if (!property->append)
    372 		return false;
    373 
    374 	msg = l_dbus_message_new_method_call(dbus, IWD_SERVICE, proxy->path,
    375 						L_DBUS_INTERFACE_PROPERTIES,
    376 						"Set");
    377 	if (!msg)
    378 		return false;
    379 
    380 
    381 	builder = l_dbus_message_builder_new(msg);
    382 	if (!builder) {
    383 		l_dbus_message_unref(msg);
    384 		return false;
    385 	}
    386 
    387 	l_dbus_message_builder_append_basic(builder, 's',
    388 							proxy->type->interface);
    389 	l_dbus_message_builder_append_basic(builder, 's', property->name);
    390 	l_dbus_message_builder_enter_variant(builder, property->type);
    391 
    392 	if (!property->append(builder, value_str)) {
    393 		l_dbus_message_builder_destroy(builder);
    394 		l_dbus_message_unref(msg);
    395 		return false;
    396 	}
    397 
    398 	l_dbus_message_builder_leave_variant(builder);
    399 	l_dbus_message_builder_finalize(builder);
    400 	l_dbus_message_builder_destroy(builder);
    401 
    402 	callback_data = l_new(struct proxy_callback_data, 1);
    403 	callback_data->callback = callback;
    404 	callback_data->user_data = (void *) proxy;
    405 
    406 	l_dbus_send_with_reply(dbus, msg, proxy_callback, callback_data,
    407 									l_free);
    408 
    409 	return true;
    410 }
    411 
    412 bool dbus_message_has_error(struct l_dbus_message *message)
    413 {
    414 	const char *name;
    415 	const char *text;
    416 
    417 	if (l_dbus_message_get_error(message, &name, &text)) {
    418 		display_error(text);
    419 		return true;
    420 	}
    421 
    422 	return false;
    423 }
    424 
    425 static bool interface_match_by_type_name(const void *a, const void *b)
    426 {
    427 	const struct proxy_interface_type *type = a;
    428 	const char *interface = b;
    429 
    430 	return !strcmp(type->interface, interface);
    431 }
    432 
    433 struct proxy_interface *proxy_interface_find(const char *interface,
    434 							const char *path)
    435 {
    436 	const struct l_queue_entry *entry;
    437 
    438 	if (!interface || !path)
    439 		return NULL;
    440 
    441 	for (entry = l_queue_get_entries(proxy_interfaces); entry;
    442 							entry = entry->next) {
    443 		struct proxy_interface *proxy = entry->data;
    444 
    445 		if (strcmp(proxy->path, path))
    446 			continue;
    447 
    448 		if (strcmp(proxy->type->interface, interface))
    449 			continue;
    450 
    451 		return proxy;
    452 	}
    453 
    454 	return NULL;
    455 }
    456 
    457 struct l_queue *proxy_interface_find_all(const char *interface,
    458 					proxy_property_match_func_t function,
    459 					const void *value)
    460 {
    461 	const struct l_queue_entry *entry;
    462 	struct l_queue *match = NULL;
    463 
    464 	if (!interface)
    465 		return NULL;
    466 
    467 	for (entry = l_queue_get_entries(proxy_interfaces); entry;
    468 							entry = entry->next) {
    469 		struct proxy_interface *proxy = entry->data;
    470 
    471 		if (!interface_match_by_type_name(proxy->type, interface))
    472 			continue;
    473 
    474 		if (function && !function(proxy->data, value))
    475 			continue;
    476 
    477 		if (!match)
    478 			match = l_queue_new();
    479 
    480 		l_queue_push_tail(match, proxy);
    481 	}
    482 
    483 	return match;
    484 }
    485 
    486 bool proxy_interface_is_same(const struct proxy_interface *a,
    487 					const struct proxy_interface *b)
    488 {
    489 	return !strcmp(a->path, b->path);
    490 }
    491 
    492 static void properties_changed_callback(struct l_dbus_message *message,
    493 								void *data)
    494 {
    495 	struct proxy_interface *proxy;
    496 	const char *path;
    497 	const char *interface;
    498 	struct l_dbus_message_iter changed;
    499 	struct l_dbus_message_iter invalidated;
    500 
    501 	if (dbus_message_has_error(message))
    502 		return;
    503 
    504 	if (!l_dbus_message_get_arguments(message, "sa{sv}as", &interface,
    505 						&changed, &invalidated)) {
    506 		l_debug("Failed to parse properties changed callback message");
    507 
    508 		return;
    509 	}
    510 
    511 	path = l_dbus_message_get_path(message);
    512 	if (!path)
    513 		return;
    514 
    515 	proxy = proxy_interface_find(interface, path);
    516 	if (!proxy)
    517 		return;
    518 
    519 	interface_update_properties(proxy, &changed, &invalidated);
    520 }
    521 
    522 static bool is_ignorable(const char *interface)
    523 {
    524 	size_t i;
    525 
    526 	static const struct {
    527 		const char *interface;
    528 	} interfaces_to_ignore[] = {
    529 		{ L_DBUS_INTERFACE_OBJECT_MANAGER },
    530 		{ L_DBUS_INTERFACE_INTROSPECTABLE },
    531 		{ L_DBUS_INTERFACE_PROPERTIES },
    532 		{ }
    533 	};
    534 
    535 	for (i = 0; interfaces_to_ignore[i].interface; i++)
    536 		if (!strcmp(interfaces_to_ignore[i].interface, interface))
    537 			return true;
    538 
    539 	return false;
    540 }
    541 
    542 static void proxy_interfaces_update_properties(const char *path,
    543 					struct l_dbus_message_iter *interfaces)
    544 {
    545 	const char *interface;
    546 	struct l_dbus_message_iter properties;
    547 	struct proxy_interface *proxy;
    548 	struct proxy_interface_type *interface_type;
    549 
    550 	if (!path)
    551 		return;
    552 
    553 	while (l_dbus_message_iter_next_entry(interfaces, &interface,
    554 								&properties)) {
    555 		interface_type = l_queue_find(proxy_interface_types,
    556 						interface_match_by_type_name,
    557 						interface);
    558 		if (!interface_type)
    559 			continue;
    560 
    561 		proxy = proxy_interface_find(interface_type->interface, path);
    562 		if (!proxy)
    563 			continue;
    564 
    565 		interface_update_properties(proxy, &properties, NULL);
    566 	}
    567 }
    568 
    569 static void proxy_interface_create(const char *path,
    570 					struct l_dbus_message_iter *interfaces)
    571 {
    572 	const char *interface;
    573 	struct l_dbus_message_iter properties;
    574 	struct proxy_interface *proxy;
    575 	struct proxy_interface_type *interface_type;
    576 
    577 	if (!path)
    578 		return;
    579 
    580 	while (l_dbus_message_iter_next_entry(interfaces, &interface,
    581 								&properties)) {
    582 		interface_type = l_queue_find(proxy_interface_types,
    583 						interface_match_by_type_name,
    584 						interface);
    585 
    586 		if (!interface_type) {
    587 			if (!is_ignorable(interface))
    588 				l_debug("Unknown DBus interface type %s",
    589 								interface);
    590 
    591 			continue;
    592 		}
    593 
    594 		proxy = proxy_interface_find(interface_type->interface, path);
    595 
    596 		if (proxy)
    597 			continue;
    598 
    599 		proxy = l_new(struct proxy_interface, 1);
    600 		proxy->path = l_strdup(path);
    601 		proxy->type = interface_type;
    602 
    603 		l_queue_push_tail(proxy_interfaces, proxy);
    604 
    605 		if (interface_type->ops && interface_type->ops->create)
    606 			proxy->data = interface_type->ops->create();
    607 	}
    608 }
    609 
    610 static void proxy_interface_destroy(void *data)
    611 {
    612 	struct proxy_interface *proxy = data;
    613 
    614 	l_free(proxy->path);
    615 
    616 	if (proxy->type->ops && proxy->type->ops->destroy)
    617 		proxy->type->ops->destroy(proxy->data);
    618 
    619 	proxy->type = NULL;
    620 
    621 	l_free(proxy);
    622 }
    623 
    624 bool proxy_interface_method_call(const struct proxy_interface *proxy,
    625 					const char *name, const char *signature,
    626 					l_dbus_message_func_t callback, ...)
    627 {
    628 	struct proxy_callback_data *callback_data;
    629 	struct l_dbus_message *call;
    630 	va_list args;
    631 
    632 	if (!proxy || !name)
    633 		return false;
    634 
    635 	call = l_dbus_message_new_method_call(dbus, IWD_SERVICE, proxy->path,
    636 						 proxy->type->interface, name);
    637 
    638 	va_start(args, callback);
    639 	l_dbus_message_set_arguments_valist(call, signature, args);
    640 	va_end(args);
    641 
    642 	callback_data = l_new(struct proxy_callback_data, 1);
    643 	callback_data->callback = callback;
    644 	callback_data->user_data = (void *) proxy;
    645 
    646 	l_dbus_send_with_reply(dbus, call, proxy_callback, callback_data,
    647 									l_free);
    648 
    649 	return true;
    650 }
    651 
    652 void *proxy_interface_get_data(const struct proxy_interface *proxy)
    653 {
    654 	return proxy->data;
    655 }
    656 
    657 const char *proxy_interface_get_interface(const struct proxy_interface *proxy)
    658 {
    659 	return proxy->type->interface;
    660 }
    661 
    662 const char *proxy_interface_get_path(const struct proxy_interface *proxy)
    663 {
    664 	return proxy->path;
    665 }
    666 
    667 const char *proxy_interface_get_identity_str(
    668 					const struct proxy_interface *proxy)
    669 {
    670 	if (proxy->type->ops && proxy->type->ops->identity)
    671 		return proxy->type->ops->identity(proxy->data);
    672 
    673 	return NULL;
    674 }
    675 
    676 void proxy_interface_display_list(const char *interface)
    677 {
    678 	const struct l_queue_entry *entry;
    679 
    680 	for (entry = l_queue_get_entries(proxy_interfaces); entry;
    681 							entry = entry->next) {
    682 		const struct proxy_interface *proxy = entry->data;
    683 
    684 		if (!interface_match_by_type_name(proxy->type, interface))
    685 			continue;
    686 
    687 		if (!proxy->type->ops || !proxy->type->ops->display)
    688 			break;
    689 
    690 		proxy->type->ops->display(MARGIN, proxy->data);
    691 	}
    692 }
    693 
    694 static void interfaces_added_callback(struct l_dbus_message *message,
    695 								void *user_data)
    696 {
    697 	const char *path;
    698 	struct l_dbus_message_iter object;
    699 
    700 	if (dbus_message_has_error(message))
    701 		return;
    702 
    703 	if (!l_dbus_message_get_arguments(message, "oa{sa{sv}}", &path,
    704 								&object))
    705 		return;
    706 
    707 	proxy_interface_create(path, &object);
    708 
    709 	if (!l_dbus_message_get_arguments(message, "oa{sa{sv}}", &path,
    710 								&object))
    711 		return;
    712 
    713 	proxy_interfaces_update_properties(path, &object);
    714 }
    715 
    716 static void interfaces_removed_callback(struct l_dbus_message *message,
    717 								void *user_data)
    718 {
    719 	const char *interface;
    720 	const char *path;
    721 	struct l_dbus_message_iter interfaces;
    722 	struct proxy_interface *proxy;
    723 
    724 	if (dbus_message_has_error(message))
    725 		return;
    726 
    727 	if (!l_dbus_message_get_arguments(message, "oas", &path, &interfaces))
    728 		return;
    729 
    730 	while (l_dbus_message_iter_next_entry(&interfaces, &interface)) {
    731 		proxy = proxy_interface_find(interface, path);
    732 
    733 		if (!proxy)
    734 			continue;
    735 
    736 		l_queue_remove(proxy_interfaces, proxy);
    737 
    738 		proxy_interface_destroy(proxy);
    739 	}
    740 }
    741 
    742 static void get_managed_objects_callback(struct l_dbus_message *message,
    743 								void *user_data)
    744 {
    745 	struct l_dbus_message_iter objects;
    746 	struct l_dbus_message_iter object;
    747 	const char *path;
    748 
    749 	if (dbus_message_has_error(message)) {
    750 		display_error("Failed to retrieve IWD dbus objects, "
    751 							"quitting...\n");
    752 
    753 		goto error;
    754 	}
    755 
    756 	if (!l_dbus_message_get_arguments(message, "a{oa{sa{sv}}}", &objects)) {
    757 		display_error("Failed to parse IWD dbus objects, "
    758 							"quitting...\n");
    759 
    760 		goto error;
    761 	}
    762 
    763 	while (l_dbus_message_iter_next_entry(&objects, &path, &object))
    764 		proxy_interface_create(path, &object);
    765 
    766 	if (!l_dbus_message_get_arguments(message, "a{oa{sa{sv}}}", &objects))
    767 		/*
    768 		 * Shouldn't happen since we parsed it above, but check return
    769 		 * anyway.
    770 		 */
    771 		goto error;
    772 
    773 	while (l_dbus_message_iter_next_entry(&objects, &path, &object))
    774 		proxy_interfaces_update_properties(path, &object);
    775 
    776 	if (command_is_interactive_mode())
    777 		display_enable_cmd_prompt();
    778 	else
    779 		command_noninteractive_trigger();
    780 
    781 	return;
    782 
    783 error:
    784 	if (!command_is_interactive_mode())
    785 		command_set_exit_status(EXIT_FAILURE);
    786 
    787 	l_main_quit();
    788 }
    789 
    790 static void get_managed_objects(void)
    791 {
    792 	l_dbus_method_call(dbus, IWD_SERVICE, IWD_ROOT_PATH,
    793 					L_DBUS_INTERFACE_OBJECT_MANAGER,
    794 					"GetManagedObjects", NULL,
    795 					get_managed_objects_callback,
    796 					NULL, NULL);
    797 }
    798 
    799 static void service_appeared_callback(struct l_dbus *dbus, void *user_data)
    800 {
    801 	l_dbus_add_signal_watch(dbus, IWD_SERVICE, IWD_ROOT_PATH,
    802 					L_DBUS_INTERFACE_OBJECT_MANAGER,
    803 					"InterfacesAdded", L_DBUS_MATCH_NONE,
    804 					interfaces_added_callback, NULL);
    805 
    806 	l_dbus_add_signal_watch(dbus, IWD_SERVICE, IWD_ROOT_PATH,
    807 					L_DBUS_INTERFACE_OBJECT_MANAGER,
    808 					"InterfacesRemoved", L_DBUS_MATCH_NONE,
    809 					interfaces_removed_callback, NULL);
    810 
    811 	l_dbus_add_signal_watch(dbus, IWD_SERVICE, NULL,
    812 					L_DBUS_INTERFACE_PROPERTIES,
    813 					"PropertiesChanged", L_DBUS_MATCH_NONE,
    814 					properties_changed_callback, NULL);
    815 
    816 	get_managed_objects();
    817 }
    818 
    819 static void service_disappeared_callback(struct l_dbus *dbus,
    820 							void *user_data)
    821 {
    822 	if (!command_is_interactive_mode()) {
    823 		command_set_exit_status(EXIT_FAILURE);
    824 		l_main_quit();
    825 	}
    826 
    827 	l_queue_clear(proxy_interfaces, proxy_interface_destroy);
    828 
    829 	display_disable_cmd_prompt();
    830 }
    831 
    832 static void dbus_disconnect_callback(void *user_data)
    833 {
    834 	if (!command_is_interactive_mode())
    835 		return;
    836 
    837 	l_main_quit();
    838 }
    839 
    840 void proxy_interface_type_register(
    841 			const struct proxy_interface_type *interface_type)
    842 {
    843 	l_queue_push_tail(proxy_interface_types, (void *) interface_type);
    844 }
    845 
    846 void proxy_interface_type_unregister(
    847 			const struct proxy_interface_type *interface_type)
    848 {
    849 	l_queue_remove(proxy_interface_types, (void *) interface_type);
    850 }
    851 
    852 struct l_dbus *dbus_get_bus(void)
    853 {
    854 	return dbus;
    855 }
    856 
    857 extern struct interface_type_desc __start___interface[];
    858 extern struct interface_type_desc __stop___interface[];
    859 
    860 bool dbus_proxy_init(void)
    861 {
    862 	struct interface_type_desc *desc;
    863 
    864 	if (dbus)
    865 		return true;
    866 
    867 	dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
    868 	if (!dbus)
    869 		return false;
    870 
    871 	proxy_interface_types = l_queue_new();
    872 	proxy_interfaces = l_queue_new();
    873 
    874 	for (desc = __start___interface; desc < __stop___interface; desc++) {
    875 		if (!desc->init)
    876 			continue;
    877 
    878 		desc->init();
    879 	}
    880 
    881 	l_dbus_set_disconnect_handler(dbus, dbus_disconnect_callback, NULL,
    882 									NULL);
    883 
    884 	if (command_is_interactive_mode())
    885 		l_dbus_add_service_watch(dbus, IWD_SERVICE,
    886 						service_appeared_callback,
    887 						service_disappeared_callback,
    888 						NULL, NULL);
    889 	else
    890 		get_managed_objects();
    891 
    892 	return true;
    893 }
    894 
    895 bool dbus_proxy_exit(void)
    896 {
    897 	struct interface_type_desc *desc;
    898 
    899 	agent_manager_unregister_agent();
    900 
    901 	for (desc = __start___interface; desc < __stop___interface; desc++) {
    902 		if (!desc->exit)
    903 			continue;
    904 
    905 		desc->exit();
    906 	}
    907 
    908 	l_queue_destroy(proxy_interface_types, NULL);
    909 	proxy_interface_types = NULL;
    910 
    911 	l_queue_destroy(proxy_interfaces, proxy_interface_destroy);
    912 	proxy_interfaces = NULL;
    913 
    914 	l_dbus_destroy(dbus);
    915 	dbus = NULL;
    916 
    917 	return true;
    918 }