Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

dbus_new_helpers.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "utils/includes.h"
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "dbus_common.h"
  19. #include "dbus_common_i.h"
  20. #include "dbus_new.h"
  21. #include "dbus_new_helpers.h"
  22. /**
  23. * recursive_iter_copy - Reads arguments from one iterator and
  24. * writes to another recursively
  25. * @from: iterator to read from
  26. * @to: iterator to write to
  27. *
  28. * Copies one iterator's elements to another. If any element in
  29. * iterator is of container type, its content is copied recursively
  30. */
  31. static void recursive_iter_copy(DBusMessageIter *from, DBusMessageIter *to)
  32. {
  33. char *subtype = NULL;
  34. int type;
  35. /* iterate over iterator to copy */
  36. while ((type = dbus_message_iter_get_arg_type(from)) !=
  37. DBUS_TYPE_INVALID) {
  38. /* simply copy basic type entries */
  39. if (dbus_type_is_basic(type)) {
  40. if (dbus_type_is_fixed(type)) {
  41. /*
  42. * According to DBus documentation all
  43. * fixed-length types are guaranteed to fit
  44. * 8 bytes
  45. */
  46. dbus_uint64_t v;
  47. dbus_message_iter_get_basic(from, &v);
  48. dbus_message_iter_append_basic(to, type, &v);
  49. } else {
  50. char *v;
  51. dbus_message_iter_get_basic(from, &v);
  52. dbus_message_iter_append_basic(to, type, &v);
  53. }
  54. } else {
  55. /* recursively copy container type entries */
  56. DBusMessageIter write_subiter, read_subiter;
  57. dbus_message_iter_recurse(from, &read_subiter);
  58. if (type == DBUS_TYPE_VARIANT ||
  59. type == DBUS_TYPE_ARRAY) {
  60. subtype = dbus_message_iter_get_signature(
  61. &read_subiter);
  62. }
  63. dbus_message_iter_open_container(to, type, subtype,
  64. &write_subiter);
  65. recursive_iter_copy(&read_subiter, &write_subiter);
  66. dbus_message_iter_close_container(to, &write_subiter);
  67. if (subtype)
  68. dbus_free(subtype);
  69. }
  70. dbus_message_iter_next(from);
  71. }
  72. }
  73. static unsigned int fill_dict_with_properties(
  74. DBusMessageIter *dict_iter, const struct wpa_dbus_property_desc *props,
  75. const char *interface, const void *user_data)
  76. {
  77. DBusMessage *reply;
  78. DBusMessageIter entry_iter, ret_iter;
  79. unsigned int counter = 0;
  80. const struct wpa_dbus_property_desc *dsc;
  81. for (dsc = props; dsc && dsc->dbus_property; dsc++) {
  82. if (!os_strncmp(dsc->dbus_interface, interface,
  83. WPAS_DBUS_INTERFACE_MAX) &&
  84. dsc->access != W && dsc->getter) {
  85. reply = dsc->getter(NULL, user_data);
  86. if (!reply)
  87. continue;
  88. if (dbus_message_get_type(reply) ==
  89. DBUS_MESSAGE_TYPE_ERROR) {
  90. dbus_message_unref(reply);
  91. continue;
  92. }
  93. dbus_message_iter_init(reply, &ret_iter);
  94. dbus_message_iter_open_container(dict_iter,
  95. DBUS_TYPE_DICT_ENTRY,
  96. NULL, &entry_iter);
  97. dbus_message_iter_append_basic(
  98. &entry_iter, DBUS_TYPE_STRING,
  99. &dsc->dbus_property);
  100. recursive_iter_copy(&ret_iter, &entry_iter);
  101. dbus_message_iter_close_container(dict_iter,
  102. &entry_iter);
  103. dbus_message_unref(reply);
  104. counter++;
  105. }
  106. }
  107. return counter;
  108. }
  109. /**
  110. * get_all_properties - Responds for GetAll properties calls on object
  111. * @message: Message with GetAll call
  112. * @interface: interface name which properties will be returned
  113. * @property_dsc: list of object's properties
  114. * Returns: Message with dict of variants as argument with properties values
  115. *
  116. * Iterates over all properties registered with object and execute getters
  117. * of those, which are readable and which interface matches interface
  118. * specified as argument. Returned message contains one dict argument
  119. * with properties names as keys and theirs values as values.
  120. */
  121. static DBusMessage * get_all_properties(
  122. DBusMessage *message, char *interface,
  123. struct wpa_dbus_object_desc *obj_dsc)
  124. {
  125. /* Create and initialize the return message */
  126. DBusMessage *reply = dbus_message_new_method_return(message);
  127. DBusMessageIter iter, dict_iter;
  128. int props_num;
  129. dbus_message_iter_init_append(reply, &iter);
  130. dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  131. DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
  132. DBUS_TYPE_STRING_AS_STRING
  133. DBUS_TYPE_VARIANT_AS_STRING
  134. DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
  135. &dict_iter);
  136. props_num = fill_dict_with_properties(&dict_iter, obj_dsc->properties,
  137. interface, obj_dsc->user_data);
  138. dbus_message_iter_close_container(&iter, &dict_iter);
  139. if (props_num == 0) {
  140. dbus_message_unref(reply);
  141. reply = dbus_message_new_error(message,
  142. DBUS_ERROR_INVALID_ARGS,
  143. "No readable properties in "
  144. "this interface");
  145. }
  146. return reply;
  147. }
  148. static int is_signature_correct(DBusMessage *message,
  149. const struct wpa_dbus_method_desc *method_dsc)
  150. {
  151. /* According to DBus documentation max length of signature is 255 */
  152. #define MAX_SIG_LEN 256
  153. char registered_sig[MAX_SIG_LEN], *pos;
  154. const char *sig = dbus_message_get_signature(message);
  155. int ret;
  156. const struct wpa_dbus_argument *arg;
  157. pos = registered_sig;
  158. *pos = '\0';
  159. for (arg = method_dsc->args; arg && arg->name; arg++) {
  160. if (arg->dir == ARG_IN) {
  161. size_t blen = registered_sig + MAX_SIG_LEN - pos;
  162. ret = os_snprintf(pos, blen, "%s", arg->type);
  163. if (ret < 0 || (size_t) ret >= blen)
  164. return 0;
  165. pos += ret;
  166. }
  167. }
  168. return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
  169. }
  170. static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
  171. struct wpa_dbus_object_desc *obj_dsc)
  172. {
  173. if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
  174. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  175. NULL);
  176. return get_all_properties(message, interface, obj_dsc);
  177. }
  178. static DBusMessage * properties_get(DBusMessage *message,
  179. const struct wpa_dbus_property_desc *dsc,
  180. void *user_data)
  181. {
  182. if (os_strcmp(dbus_message_get_signature(message), "ss"))
  183. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  184. NULL);
  185. if (dsc->access != W && dsc->getter)
  186. return dsc->getter(message, user_data);
  187. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  188. "Property is write-only");
  189. }
  190. static DBusMessage * properties_set(DBusMessage *message,
  191. const struct wpa_dbus_property_desc *dsc,
  192. void *user_data)
  193. {
  194. if (os_strcmp(dbus_message_get_signature(message), "ssv"))
  195. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  196. NULL);
  197. if (dsc->access != R && dsc->setter)
  198. return dsc->setter(message, user_data);
  199. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  200. "Property is read-only");
  201. }
  202. static DBusMessage *
  203. properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
  204. char *interface,
  205. struct wpa_dbus_object_desc *obj_dsc)
  206. {
  207. const struct wpa_dbus_property_desc *property_dsc;
  208. char *property;
  209. const char *method;
  210. method = dbus_message_get_member(message);
  211. property_dsc = obj_dsc->properties;
  212. /* Second argument: property name (DBUS_TYPE_STRING) */
  213. if (!dbus_message_iter_next(iter) ||
  214. dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
  215. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  216. NULL);
  217. }
  218. dbus_message_iter_get_basic(iter, &property);
  219. while (property_dsc && property_dsc->dbus_property) {
  220. /* compare property names and
  221. * interfaces */
  222. if (!os_strncmp(property_dsc->dbus_property, property,
  223. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  224. !os_strncmp(property_dsc->dbus_interface, interface,
  225. WPAS_DBUS_INTERFACE_MAX))
  226. break;
  227. property_dsc++;
  228. }
  229. if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
  230. wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
  231. interface, property,
  232. dbus_message_get_path(message));
  233. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  234. "No such property");
  235. }
  236. if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  237. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0)
  238. return properties_get(message, property_dsc,
  239. obj_dsc->user_data);
  240. return properties_set(message, property_dsc, obj_dsc->user_data);
  241. }
  242. static DBusMessage * properties_handler(DBusMessage *message,
  243. struct wpa_dbus_object_desc *obj_dsc)
  244. {
  245. DBusMessageIter iter;
  246. char *interface;
  247. const char *method;
  248. method = dbus_message_get_member(message);
  249. dbus_message_iter_init(message, &iter);
  250. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  251. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  252. !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
  253. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  254. !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  255. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  256. /* First argument: interface name (DBUS_TYPE_STRING) */
  257. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
  258. {
  259. return dbus_message_new_error(message,
  260. DBUS_ERROR_INVALID_ARGS,
  261. NULL);
  262. }
  263. dbus_message_iter_get_basic(&iter, &interface);
  264. if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  265. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  266. /* GetAll */
  267. return properties_get_all(message, interface, obj_dsc);
  268. }
  269. /* Get or Set */
  270. return properties_get_or_set(message, &iter, interface,
  271. obj_dsc);
  272. }
  273. return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
  274. NULL);
  275. }
  276. static DBusMessage * msg_method_handler(DBusMessage *message,
  277. struct wpa_dbus_object_desc *obj_dsc)
  278. {
  279. const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  280. const char *method;
  281. const char *msg_interface;
  282. method = dbus_message_get_member(message);
  283. msg_interface = dbus_message_get_interface(message);
  284. /* try match call to any registered method */
  285. while (method_dsc && method_dsc->dbus_method) {
  286. /* compare method names and interfaces */
  287. if (!os_strncmp(method_dsc->dbus_method, method,
  288. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  289. !os_strncmp(method_dsc->dbus_interface, msg_interface,
  290. WPAS_DBUS_INTERFACE_MAX))
  291. break;
  292. method_dsc++;
  293. }
  294. if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
  295. wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
  296. msg_interface, method,
  297. dbus_message_get_path(message));
  298. return dbus_message_new_error(message,
  299. DBUS_ERROR_UNKNOWN_METHOD, NULL);
  300. }
  301. if (!is_signature_correct(message, method_dsc)) {
  302. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  303. NULL);
  304. }
  305. return method_dsc->method_handler(message,
  306. obj_dsc->user_data);
  307. }
  308. /**
  309. * message_handler - Handles incoming DBus messages
  310. * @connection: DBus connection on which message was received
  311. * @message: Received message
  312. * @user_data: pointer to description of object to which message was sent
  313. * Returns: Returns information whether message was handled or not
  314. *
  315. * Reads message interface and method name, then checks if they matches one
  316. * of the special cases i.e. introspection call or properties get/getall/set
  317. * methods and handles it. Else it iterates over registered methods list
  318. * and tries to match method's name and interface to those read from message
  319. * If appropriate method was found its handler function is called and
  320. * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
  321. * will be sent.
  322. */
  323. static DBusHandlerResult message_handler(DBusConnection *connection,
  324. DBusMessage *message, void *user_data)
  325. {
  326. struct wpa_dbus_object_desc *obj_dsc = user_data;
  327. const char *method;
  328. const char *path;
  329. const char *msg_interface;
  330. DBusMessage *reply;
  331. /* get method, interface and path the message is addressed to */
  332. method = dbus_message_get_member(message);
  333. path = dbus_message_get_path(message);
  334. msg_interface = dbus_message_get_interface(message);
  335. if (!method || !path || !msg_interface)
  336. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  337. wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s)",
  338. msg_interface, method, path);
  339. /* if message is introspection method call */
  340. if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
  341. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  342. !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
  343. WPAS_DBUS_INTERFACE_MAX)) {
  344. #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
  345. reply = wpa_dbus_introspect(message, obj_dsc);
  346. #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  347. reply = dbus_message_new_error(
  348. message, DBUS_ERROR_UNKNOWN_METHOD,
  349. "wpa_supplicant was compiled without "
  350. "introspection support.");
  351. #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  352. } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
  353. WPAS_DBUS_INTERFACE_MAX)) {
  354. /* if message is properties method call */
  355. reply = properties_handler(message, obj_dsc);
  356. } else {
  357. reply = msg_method_handler(message, obj_dsc);
  358. }
  359. /* If handler succeed returning NULL, reply empty message */
  360. if (!reply)
  361. reply = dbus_message_new_method_return(message);
  362. if (reply) {
  363. if (!dbus_message_get_no_reply(message))
  364. dbus_connection_send(connection, reply, NULL);
  365. dbus_message_unref(reply);
  366. }
  367. wpa_dbus_flush_all_changed_properties(connection);
  368. return DBUS_HANDLER_RESULT_HANDLED;
  369. }
  370. /**
  371. * free_dbus_object_desc - Frees object description data structure
  372. * @connection: DBus connection
  373. * @obj_dsc: Object description to free
  374. *
  375. * Frees each of properties, methods and signals description lists and
  376. * the object description structure itself.
  377. */
  378. void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
  379. {
  380. if (!obj_dsc)
  381. return;
  382. /* free handler's argument */
  383. if (obj_dsc->user_data_free_func)
  384. obj_dsc->user_data_free_func(obj_dsc->user_data);
  385. os_free(obj_dsc->path);
  386. os_free(obj_dsc->prop_changed_flags);
  387. os_free(obj_dsc);
  388. }
  389. static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
  390. {
  391. free_dbus_object_desc(obj_dsc);
  392. }
  393. /**
  394. * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
  395. * @application_data: Pointer to application specific data structure
  396. * @dbus_path: DBus path to interface object
  397. * @dbus_service: DBus service name to register with
  398. * @messageHandler: a pointer to function which will handle dbus messages
  399. * coming on interface
  400. * Returns: 0 on success, -1 on failure
  401. *
  402. * Initialize the dbus control interface and start receiving commands from
  403. * external programs over the bus.
  404. */
  405. int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
  406. char *dbus_path, char *dbus_service,
  407. struct wpa_dbus_object_desc *obj_desc)
  408. {
  409. DBusError error;
  410. int ret = -1;
  411. DBusObjectPathVTable wpa_vtable = {
  412. &free_dbus_object_desc_cb, &message_handler,
  413. NULL, NULL, NULL, NULL
  414. };
  415. obj_desc->connection = iface->con;
  416. obj_desc->path = os_strdup(dbus_path);
  417. /* Register the message handler for the global dbus interface */
  418. if (!dbus_connection_register_object_path(iface->con,
  419. dbus_path, &wpa_vtable,
  420. obj_desc)) {
  421. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  422. "handler");
  423. return -1;
  424. }
  425. /* Register our service with the message bus */
  426. dbus_error_init(&error);
  427. switch (dbus_bus_request_name(iface->con, dbus_service,
  428. 0, &error)) {
  429. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  430. ret = 0;
  431. break;
  432. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  433. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  434. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  435. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  436. "already registered");
  437. break;
  438. default:
  439. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  440. "%s %s", error.name, error.message);
  441. break;
  442. }
  443. dbus_error_free(&error);
  444. if (ret != 0)
  445. return -1;
  446. wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
  447. return 0;
  448. }
  449. /**
  450. * wpa_dbus_register_object_per_iface - Register a new object with dbus
  451. * @ctrl_iface: pointer to dbus private data
  452. * @path: DBus path to object
  453. * @ifname: interface name
  454. * @obj_desc: description of object's methods, signals and properties
  455. * Returns: 0 on success, -1 on error
  456. *
  457. * Registers a new interface with dbus and assigns it a dbus object path.
  458. */
  459. int wpa_dbus_register_object_per_iface(
  460. struct wpas_dbus_priv *ctrl_iface,
  461. const char *path, const char *ifname,
  462. struct wpa_dbus_object_desc *obj_desc)
  463. {
  464. DBusConnection *con;
  465. DBusError error;
  466. DBusObjectPathVTable vtable = {
  467. &free_dbus_object_desc_cb, &message_handler,
  468. NULL, NULL, NULL, NULL
  469. };
  470. /* Do nothing if the control interface is not turned on */
  471. if (ctrl_iface == NULL)
  472. return 0;
  473. con = ctrl_iface->con;
  474. obj_desc->connection = con;
  475. obj_desc->path = os_strdup(path);
  476. dbus_error_init(&error);
  477. /* Register the message handler for the interface functions */
  478. if (!dbus_connection_try_register_object_path(con, path, &vtable,
  479. obj_desc, &error)) {
  480. if (!os_strcmp(error.name, DBUS_ERROR_OBJECT_PATH_IN_USE)) {
  481. wpa_printf(MSG_DEBUG, "dbus: %s", error.message);
  482. } else {
  483. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  484. "handler for interface %s object %s",
  485. ifname, path);
  486. wpa_printf(MSG_ERROR, "dbus error: %s", error.name);
  487. wpa_printf(MSG_ERROR, "dbus: %s", error.message);
  488. }
  489. dbus_error_free(&error);
  490. return -1;
  491. }
  492. dbus_error_free(&error);
  493. return 0;
  494. }
  495. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
  496. /**
  497. * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
  498. * @ctrl_iface: Pointer to dbus private data
  499. * @path: DBus path to object which will be unregistered
  500. * Returns: Zero on success and -1 on failure
  501. *
  502. * Unregisters DBus object given by its path
  503. */
  504. int wpa_dbus_unregister_object_per_iface(
  505. struct wpas_dbus_priv *ctrl_iface, const char *path)
  506. {
  507. DBusConnection *con = ctrl_iface->con;
  508. struct wpa_dbus_object_desc *obj_desc = NULL;
  509. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  510. if (!obj_desc) {
  511. wpa_printf(MSG_ERROR, "dbus: %s: Could not obtain object's "
  512. "private data: %s", __func__, path);
  513. } else {
  514. eloop_cancel_timeout(flush_object_timeout_handler, con,
  515. obj_desc);
  516. }
  517. if (!dbus_connection_unregister_object_path(con, path))
  518. return -1;
  519. return 0;
  520. }
  521. static void put_changed_properties(const struct wpa_dbus_object_desc *obj_dsc,
  522. const char *interface,
  523. DBusMessageIter *dict_iter)
  524. {
  525. DBusMessage *getter_reply;
  526. DBusMessageIter prop_iter, entry_iter;
  527. const struct wpa_dbus_property_desc *dsc;
  528. int i;
  529. for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
  530. dsc++, i++) {
  531. if (obj_dsc->prop_changed_flags == NULL ||
  532. !obj_dsc->prop_changed_flags[i])
  533. continue;
  534. if (os_strcmp(dsc->dbus_interface, interface) != 0)
  535. continue;
  536. obj_dsc->prop_changed_flags[i] = 0;
  537. getter_reply = dsc->getter(NULL, obj_dsc->user_data);
  538. if (!getter_reply ||
  539. dbus_message_get_type(getter_reply) ==
  540. DBUS_MESSAGE_TYPE_ERROR) {
  541. wpa_printf(MSG_ERROR, "dbus: %s: Cannot get new value "
  542. "of property %s", __func__,
  543. dsc->dbus_property);
  544. continue;
  545. }
  546. if (!dbus_message_iter_init(getter_reply, &prop_iter) ||
  547. !dbus_message_iter_open_container(dict_iter,
  548. DBUS_TYPE_DICT_ENTRY,
  549. NULL, &entry_iter) ||
  550. !dbus_message_iter_append_basic(&entry_iter,
  551. DBUS_TYPE_STRING,
  552. &dsc->dbus_property))
  553. goto err;
  554. recursive_iter_copy(&prop_iter, &entry_iter);
  555. if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
  556. goto err;
  557. dbus_message_unref(getter_reply);
  558. }
  559. return;
  560. err:
  561. wpa_printf(MSG_ERROR, "dbus: %s: Cannot construct signal", __func__);
  562. }
  563. static void send_prop_changed_signal(
  564. DBusConnection *con, const char *path, const char *interface,
  565. const struct wpa_dbus_object_desc *obj_dsc)
  566. {
  567. DBusMessage *msg;
  568. DBusMessageIter signal_iter, dict_iter;
  569. msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
  570. if (msg == NULL)
  571. return;
  572. dbus_message_iter_init_append(msg, &signal_iter);
  573. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  574. "{sv}", &dict_iter))
  575. goto err;
  576. put_changed_properties(obj_dsc, interface, &dict_iter);
  577. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
  578. goto err;
  579. dbus_connection_send(con, msg, NULL);
  580. out:
  581. dbus_message_unref(msg);
  582. return;
  583. err:
  584. wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
  585. __func__);
  586. goto out;
  587. }
  588. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
  589. {
  590. DBusConnection *con = eloop_ctx;
  591. struct wpa_dbus_object_desc *obj_desc = timeout_ctx;
  592. wpa_printf(MSG_DEBUG, "dbus: %s: Timeout - sending changed properties "
  593. "of object %s", __func__, obj_desc->path);
  594. wpa_dbus_flush_object_changed_properties(con, obj_desc->path);
  595. }
  596. static void recursive_flush_changed_properties(DBusConnection *con,
  597. const char *path)
  598. {
  599. char **objects = NULL;
  600. char subobj_path[WPAS_DBUS_OBJECT_PATH_MAX];
  601. int i;
  602. wpa_dbus_flush_object_changed_properties(con, path);
  603. if (!dbus_connection_list_registered(con, path, &objects))
  604. goto out;
  605. for (i = 0; objects[i]; i++) {
  606. os_snprintf(subobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  607. "%s/%s", path, objects[i]);
  608. recursive_flush_changed_properties(con, subobj_path);
  609. }
  610. out:
  611. dbus_free_string_array(objects);
  612. }
  613. /**
  614. * wpa_dbus_flush_all_changed_properties - Send all PropertiesChanged signals
  615. * @con: DBus connection
  616. *
  617. * Traverses through all registered objects and sends PropertiesChanged for
  618. * each properties.
  619. */
  620. void wpa_dbus_flush_all_changed_properties(DBusConnection *con)
  621. {
  622. recursive_flush_changed_properties(con, WPAS_DBUS_NEW_PATH);
  623. }
  624. /**
  625. * wpa_dbus_flush_object_changed_properties - Send PropertiesChanged for object
  626. * @con: DBus connection
  627. * @path: path to a DBus object for which PropertiesChanged will be sent.
  628. *
  629. * Iterates over all properties registered with object and for each interface
  630. * containing properties marked as changed, sends a PropertiesChanged signal
  631. * containing names and new values of properties that have changed.
  632. *
  633. * You need to call this function after wpa_dbus_mark_property_changed()
  634. * if you want to send PropertiesChanged signal immediately (i.e., without
  635. * waiting timeout to expire). PropertiesChanged signal for an object is sent
  636. * automatically short time after first marking property as changed. All
  637. * PropertiesChanged signals are sent automatically after responding on DBus
  638. * message, so if you marked a property changed as a result of DBus call
  639. * (e.g., param setter), you usually do not need to call this function.
  640. */
  641. void wpa_dbus_flush_object_changed_properties(DBusConnection *con,
  642. const char *path)
  643. {
  644. struct wpa_dbus_object_desc *obj_desc = NULL;
  645. const struct wpa_dbus_property_desc *dsc;
  646. int i;
  647. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  648. if (!obj_desc)
  649. return;
  650. eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
  651. dsc = obj_desc->properties;
  652. for (dsc = obj_desc->properties, i = 0; dsc && dsc->dbus_property;
  653. dsc++, i++) {
  654. if (obj_desc->prop_changed_flags == NULL ||
  655. !obj_desc->prop_changed_flags[i])
  656. continue;
  657. send_prop_changed_signal(con, path, dsc->dbus_interface,
  658. obj_desc);
  659. }
  660. }
  661. #define WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT 5000
  662. /**
  663. * wpa_dbus_mark_property_changed - Mark a property as changed and
  664. * @iface: dbus priv struct
  665. * @path: path to DBus object which property has changed
  666. * @interface: interface containing changed property
  667. * @property: property name which has changed
  668. *
  669. * Iterates over all properties registered with an object and marks the one
  670. * given in parameters as changed. All parameters registered for an object
  671. * within a single interface will be aggregated together and sent in one
  672. * PropertiesChanged signal when function
  673. * wpa_dbus_flush_object_changed_properties() is called.
  674. */
  675. void wpa_dbus_mark_property_changed(struct wpas_dbus_priv *iface,
  676. const char *path, const char *interface,
  677. const char *property)
  678. {
  679. struct wpa_dbus_object_desc *obj_desc = NULL;
  680. const struct wpa_dbus_property_desc *dsc;
  681. int i = 0;
  682. if (iface == NULL)
  683. return;
  684. dbus_connection_get_object_path_data(iface->con, path,
  685. (void **) &obj_desc);
  686. if (!obj_desc) {
  687. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  688. "could not obtain object's private data: %s", path);
  689. return;
  690. }
  691. for (dsc = obj_desc->properties; dsc && dsc->dbus_property; dsc++, i++)
  692. if (os_strcmp(property, dsc->dbus_property) == 0 &&
  693. os_strcmp(interface, dsc->dbus_interface) == 0) {
  694. if (obj_desc->prop_changed_flags)
  695. obj_desc->prop_changed_flags[i] = 1;
  696. break;
  697. }
  698. if (!dsc || !dsc->dbus_property) {
  699. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  700. "no property %s in object %s", property, path);
  701. return;
  702. }
  703. if (!eloop_is_timeout_registered(flush_object_timeout_handler,
  704. iface->con, obj_desc->path)) {
  705. eloop_register_timeout(0, WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT,
  706. flush_object_timeout_handler,
  707. iface->con, obj_desc);
  708. }
  709. }
  710. /**
  711. * wpa_dbus_get_object_properties - Put object's properties into dictionary
  712. * @iface: dbus priv struct
  713. * @path: path to DBus object which properties will be obtained
  714. * @interface: interface name which properties will be obtained
  715. * @dict_iter: correct, open DBus dictionary iterator.
  716. *
  717. * Iterates over all properties registered with object and execute getters
  718. * of those, which are readable and which interface matches interface
  719. * specified as argument. Obtained properties values are stored in
  720. * dict_iter dictionary.
  721. */
  722. void wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
  723. const char *path, const char *interface,
  724. DBusMessageIter *dict_iter)
  725. {
  726. struct wpa_dbus_object_desc *obj_desc = NULL;
  727. dbus_connection_get_object_path_data(iface->con, path,
  728. (void **) &obj_desc);
  729. if (!obj_desc) {
  730. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_get_object_properties: "
  731. "could not obtain object's private data: %s", path);
  732. return;
  733. }
  734. fill_dict_with_properties(dict_iter, obj_desc->properties,
  735. interface, obj_desc->user_data);
  736. }