ctrl_iface_dbus_new_helpers.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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 "includes.h"
  16. #include "common.h"
  17. #include "eloop.h"
  18. #include "ctrl_iface_dbus_new_helpers.h"
  19. /**
  20. * struct wpa_dbus_method_desc - DBus method description
  21. */
  22. struct wpa_dbus_method_desc {
  23. /* pointer to next description in list */
  24. struct wpa_dbus_method_desc *next;
  25. /* method interface */
  26. char *dbus_interface;
  27. /* method name */
  28. char *dbus_method;
  29. /* method handling function */
  30. WPADBusMethodHandler method_handler;
  31. /* handler function argument */
  32. void *handler_argument;
  33. /* function used to free handler argument */
  34. WPADBusArgumentFreeFunction argument_free_func;
  35. /* number of method arguments */
  36. int args_num;
  37. /* array of arguments */
  38. struct wpa_dbus_argument args[];
  39. };
  40. /**
  41. * struct wpa_dbus_signal_desc - DBus signal description
  42. */
  43. struct wpa_dbus_signal_desc {
  44. /* pointer to next description in list */
  45. struct wpa_dbus_signal_desc *next;
  46. /* signal interface */
  47. char *dbus_interface;
  48. /* signal name */
  49. char *dbus_signal;
  50. /* number of signal arguments */
  51. int args_num;
  52. /* array of arguments */
  53. struct wpa_dbus_argument args[0];
  54. };
  55. /**
  56. * struct wpa_dbus_property_desc - DBus property description
  57. */
  58. struct wpa_dbus_property_desc {
  59. /* pointer to next description in list */
  60. struct wpa_dbus_property_desc *next;
  61. /* property interface */
  62. char *dbus_interface;
  63. /* property name */
  64. char *dbus_property;
  65. /* property type signature in DBus type notation */
  66. char *type;
  67. /* property access permissions */
  68. enum dbus_prop_access access;
  69. /* property getter function */
  70. WPADBusPropertyAccessor getter;
  71. /* property setter function */
  72. WPADBusPropertyAccessor setter;
  73. /* argument for getter and setter functions */
  74. void *user_data;
  75. /* function used to free accessors argument */
  76. WPADBusArgumentFreeFunction user_data_free_func;
  77. };
  78. #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
  79. #include <libxml/tree.h>
  80. struct interfaces {
  81. struct interfaces *next;
  82. char *dbus_interface;
  83. xmlNodePtr interface_node;
  84. };
  85. #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  86. static void process_watch(struct ctrl_iface_dbus_new_priv *iface,
  87. DBusWatch *watch, eloop_event_type type)
  88. {
  89. dbus_connection_ref(iface->con);
  90. iface->should_dispatch = 0;
  91. if (type == EVENT_TYPE_READ)
  92. dbus_watch_handle(watch, DBUS_WATCH_READABLE);
  93. else if (type == EVENT_TYPE_WRITE)
  94. dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
  95. else if (type == EVENT_TYPE_EXCEPTION)
  96. dbus_watch_handle(watch, DBUS_WATCH_ERROR);
  97. if (iface->should_dispatch) {
  98. while (dbus_connection_get_dispatch_status(iface->con) ==
  99. DBUS_DISPATCH_DATA_REMAINS)
  100. dbus_connection_dispatch(iface->con);
  101. iface->should_dispatch = 0;
  102. }
  103. dbus_connection_unref(iface->con);
  104. }
  105. static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
  106. {
  107. process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
  108. }
  109. static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
  110. {
  111. process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
  112. }
  113. static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
  114. {
  115. process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
  116. }
  117. static void connection_setup_add_watch(struct ctrl_iface_dbus_new_priv *iface,
  118. DBusWatch *watch)
  119. {
  120. unsigned int flags;
  121. int fd;
  122. if (!dbus_watch_get_enabled(watch))
  123. return;
  124. flags = dbus_watch_get_flags(watch);
  125. fd = dbus_watch_get_unix_fd(watch);
  126. eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
  127. iface, watch);
  128. if (flags & DBUS_WATCH_READABLE) {
  129. eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
  130. iface, watch);
  131. }
  132. if (flags & DBUS_WATCH_WRITABLE) {
  133. eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
  134. iface, watch);
  135. }
  136. dbus_watch_set_data(watch, iface, NULL);
  137. }
  138. static void connection_setup_remove_watch(
  139. struct ctrl_iface_dbus_new_priv *iface, DBusWatch *watch)
  140. {
  141. unsigned int flags;
  142. int fd;
  143. flags = dbus_watch_get_flags(watch);
  144. fd = dbus_watch_get_unix_fd(watch);
  145. eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
  146. if (flags & DBUS_WATCH_READABLE)
  147. eloop_unregister_sock(fd, EVENT_TYPE_READ);
  148. if (flags & DBUS_WATCH_WRITABLE)
  149. eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
  150. dbus_watch_set_data(watch, NULL, NULL);
  151. }
  152. static dbus_bool_t add_watch(DBusWatch *watch, void *data)
  153. {
  154. connection_setup_add_watch(data, watch);
  155. return TRUE;
  156. }
  157. static void remove_watch(DBusWatch *watch, void *data)
  158. {
  159. connection_setup_remove_watch(data, watch);
  160. }
  161. static void watch_toggled(DBusWatch *watch, void *data)
  162. {
  163. if (dbus_watch_get_enabled(watch))
  164. add_watch(watch, data);
  165. else
  166. remove_watch(watch, data);
  167. }
  168. static void process_timeout(void *eloop_ctx, void *sock_ctx)
  169. {
  170. DBusTimeout *timeout = sock_ctx;
  171. dbus_timeout_handle(timeout);
  172. }
  173. static void connection_setup_add_timeout(
  174. struct ctrl_iface_dbus_new_priv *iface, DBusTimeout *timeout)
  175. {
  176. if (!dbus_timeout_get_enabled(timeout))
  177. return;
  178. eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
  179. process_timeout, iface, timeout);
  180. dbus_timeout_set_data(timeout, iface, NULL);
  181. }
  182. static void connection_setup_remove_timeout(
  183. struct ctrl_iface_dbus_new_priv *iface, DBusTimeout *timeout)
  184. {
  185. eloop_cancel_timeout(process_timeout, iface, timeout);
  186. dbus_timeout_set_data(timeout, NULL, NULL);
  187. }
  188. static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
  189. {
  190. if (!dbus_timeout_get_enabled(timeout))
  191. return TRUE;
  192. connection_setup_add_timeout(data, timeout);
  193. return TRUE;
  194. }
  195. static void remove_timeout(DBusTimeout *timeout, void *data)
  196. {
  197. connection_setup_remove_timeout(data, timeout);
  198. }
  199. static void timeout_toggled(DBusTimeout *timeout, void *data)
  200. {
  201. if (dbus_timeout_get_enabled(timeout))
  202. add_timeout(timeout, data);
  203. else
  204. remove_timeout(timeout, data);
  205. }
  206. static void process_wakeup_main(int sig, void *eloop_ctx, void *signal_ctx)
  207. {
  208. struct ctrl_iface_dbus_new_priv *iface = signal_ctx;
  209. if (sig != SIGPOLL || !iface->con)
  210. return;
  211. if (dbus_connection_get_dispatch_status(iface->con) !=
  212. DBUS_DISPATCH_DATA_REMAINS)
  213. return;
  214. /* Only dispatch once - we do not want to starve other events */
  215. dbus_connection_ref(iface->con);
  216. dbus_connection_dispatch(iface->con);
  217. dbus_connection_unref(iface->con);
  218. }
  219. /**
  220. * wakeup_main - Attempt to wake our mainloop up
  221. * @data: dbus control interface private data
  222. *
  223. * Try to wake up the main eloop so it will process
  224. * dbus events that may have happened.
  225. */
  226. static void wakeup_main(void *data)
  227. {
  228. struct ctrl_iface_dbus_new_priv *iface = data;
  229. /* Use SIGPOLL to break out of the eloop select() */
  230. raise(SIGPOLL);
  231. iface->should_dispatch = 1;
  232. }
  233. /**
  234. * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
  235. * @iface: dbus control interface private data
  236. * Returns: 0 on success, -1 on failure
  237. *
  238. * Register our wakeup_main handler with dbus
  239. */
  240. static int connection_setup_wakeup_main(struct ctrl_iface_dbus_new_priv *iface)
  241. {
  242. if (eloop_register_signal(SIGPOLL, process_wakeup_main, iface))
  243. return -1;
  244. dbus_connection_set_wakeup_main_function(iface->con, wakeup_main,
  245. iface, NULL);
  246. return 0;
  247. }
  248. /**
  249. * wpa_dbus_next_objid - Return next available object id
  250. * @iface: dbus control interface private data
  251. * Returns: Object id
  252. */
  253. u32 wpa_dbus_next_objid(struct ctrl_iface_dbus_new_priv *iface)
  254. {
  255. return iface->next_objid++;
  256. }
  257. /**
  258. * integrate_with_eloop - Register our mainloop integration with dbus
  259. * @connection: connection to the system message bus
  260. * @iface: a dbus control interface data structure
  261. * Returns: 0 on success, -1 on failure
  262. *
  263. * We register our mainloop integration functions with dbus here.
  264. */
  265. static int integrate_with_eloop(DBusConnection *connection,
  266. struct ctrl_iface_dbus_new_priv *iface)
  267. {
  268. if (!dbus_connection_set_watch_functions(connection, add_watch,
  269. remove_watch, watch_toggled,
  270. iface, NULL)) {
  271. perror("dbus_connection_set_watch_functions[dbus]");
  272. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  273. return -1;
  274. }
  275. if (!dbus_connection_set_timeout_functions(connection, add_timeout,
  276. remove_timeout,
  277. timeout_toggled, iface,
  278. NULL)) {
  279. perror("dbus_connection_set_timeout_functions[dbus]");
  280. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  281. return -1;
  282. }
  283. if (connection_setup_wakeup_main(iface) < 0) {
  284. perror("connection_setup_wakeup_main[dbus]");
  285. wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
  286. return -1;
  287. }
  288. return 0;
  289. }
  290. /**
  291. * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
  292. * claiming bus name
  293. * @eloop_ctx: the DBusConnection to dispatch on
  294. * @timeout_ctx: unused
  295. *
  296. * If clients are quick to notice that service claimed its bus name,
  297. * there may have been messages that came in before initialization was
  298. * all finished. Dispatch those here.
  299. */
  300. static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
  301. {
  302. DBusConnection *con = eloop_ctx;
  303. while (dbus_connection_get_dispatch_status(con) ==
  304. DBUS_DISPATCH_DATA_REMAINS)
  305. dbus_connection_dispatch(con);
  306. }
  307. #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
  308. /**
  309. * extract_interfaces - Extract interfaces from methods, signals and props
  310. * @obj_dsc: Description of object from which interfaces will be extracted
  311. * @root_node: root node of XML introspection document
  312. * Returns: List of interfaces found in object description
  313. *
  314. * Iterates over all methods, signals and properties registered with
  315. * object and collects all declared DBus interfaces and create interface's
  316. * node in XML root node for each. Returned list elements contains interface
  317. * name and XML node of corresponding interface.
  318. */
  319. static struct interfaces * extract_interfaces(
  320. struct wpa_dbus_object_desc *obj_dsc, xmlNodePtr root_node)
  321. {
  322. struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  323. struct wpa_dbus_signal_desc *signal_dsc = obj_dsc->signals;
  324. struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties;
  325. struct interfaces *head = NULL;
  326. struct interfaces *iface, *last;
  327. int len;
  328. /* extract interfaces from methods */
  329. while (method_dsc) {
  330. iface = head;
  331. last = NULL;
  332. /* go to next method if its interface is already extracted */
  333. while (iface) {
  334. if (!os_strcmp(iface->dbus_interface,
  335. method_dsc->dbus_interface))
  336. break;
  337. last = iface;
  338. iface = iface->next;
  339. }
  340. if (iface) {
  341. method_dsc = method_dsc->next;
  342. continue;
  343. }
  344. iface = os_zalloc(sizeof(struct interfaces));
  345. if (!iface) {
  346. wpa_printf(MSG_ERROR, "Not enough memory to create "
  347. "interface introspection data");
  348. method_dsc = method_dsc->next;
  349. continue;
  350. }
  351. if (last)
  352. last->next = iface;
  353. else
  354. head = iface;
  355. len = os_strlen(method_dsc->dbus_interface) + 1;
  356. iface->dbus_interface = os_malloc(len);
  357. if (!iface->dbus_interface) {
  358. wpa_printf(MSG_ERROR, "Not enough memory to create "
  359. "interface introspection data (interface "
  360. "name)");
  361. method_dsc = method_dsc->next;
  362. continue;
  363. }
  364. os_strncpy(iface->dbus_interface, method_dsc->dbus_interface,
  365. len);
  366. iface->interface_node = xmlNewChild(root_node, NULL,
  367. BAD_CAST "interface",
  368. NULL);
  369. xmlNewProp(iface->interface_node, BAD_CAST "name",
  370. BAD_CAST method_dsc->dbus_interface);
  371. method_dsc = method_dsc->next;
  372. }
  373. /* extract interfaces from signals */
  374. while (signal_dsc) {
  375. iface = head;
  376. last = NULL;
  377. /* go to next signal if its interface is already extracted */
  378. while (iface) {
  379. if (!os_strcmp(iface->dbus_interface,
  380. signal_dsc->dbus_interface))
  381. break;
  382. last = iface;
  383. iface = iface->next;
  384. }
  385. if (iface) {
  386. signal_dsc = signal_dsc->next;
  387. continue;
  388. }
  389. iface = os_zalloc(sizeof(struct interfaces));
  390. if (!iface) {
  391. wpa_printf(MSG_ERROR, "Not enough memory to create "
  392. "interface introspection data");
  393. signal_dsc = signal_dsc->next;
  394. continue;
  395. }
  396. if (last)
  397. last->next = iface;
  398. else
  399. head = iface;
  400. len = os_strlen(signal_dsc->dbus_interface) + 1;
  401. iface->dbus_interface = os_malloc(len);
  402. if (!iface->dbus_interface) {
  403. wpa_printf(MSG_ERROR, "Not enough memory to create "
  404. "interface introspection data (interface "
  405. "name)");
  406. signal_dsc = signal_dsc->next;
  407. continue;
  408. }
  409. os_strncpy(iface->dbus_interface, signal_dsc->dbus_interface,
  410. len);
  411. iface->interface_node = xmlNewChild(root_node, NULL,
  412. BAD_CAST "interface",
  413. NULL);
  414. xmlNewProp(iface->interface_node, BAD_CAST "name",
  415. BAD_CAST signal_dsc->dbus_interface);
  416. signal_dsc = signal_dsc->next;
  417. }
  418. /* extract interfaces from properties */
  419. while (property_dsc) {
  420. iface = head;
  421. last = NULL;
  422. /* go to next property if its interface is already extracted */
  423. while (iface) {
  424. if (!os_strcmp(iface->dbus_interface,
  425. property_dsc->dbus_interface))
  426. break;
  427. last = iface;
  428. iface = iface->next;
  429. }
  430. if (iface) {
  431. property_dsc = property_dsc->next;
  432. continue;
  433. }
  434. iface = os_zalloc(sizeof(struct interfaces));
  435. if (!iface) {
  436. wpa_printf(MSG_ERROR, "Not enough memory to create "
  437. "interface introspection data");
  438. property_dsc = property_dsc->next;
  439. continue;
  440. }
  441. if (last)
  442. last->next = iface;
  443. else
  444. head = iface;
  445. len = os_strlen(property_dsc->dbus_interface) + 1;
  446. iface->dbus_interface = os_malloc(len);
  447. if (!iface->dbus_interface) {
  448. wpa_printf(MSG_ERROR, "Not enough memory to create "
  449. "interface introspection data (interface "
  450. "name)");
  451. property_dsc = property_dsc->next;
  452. continue;
  453. }
  454. os_strncpy(iface->dbus_interface, property_dsc->dbus_interface,
  455. len);
  456. iface->interface_node = xmlNewChild(root_node, NULL,
  457. BAD_CAST "interface",
  458. NULL);
  459. xmlNewProp(iface->interface_node, BAD_CAST "name",
  460. BAD_CAST property_dsc->dbus_interface);
  461. property_dsc = property_dsc->next;
  462. }
  463. return head;
  464. }
  465. /**
  466. * introspect - Responds for Introspect calls on object
  467. * @message: Message with Introspect call
  468. * @obj_dsc: Object description on which Introspect was called
  469. * Returns: Message with introspection result XML string as only argument
  470. *
  471. * Iterates over all methods, signals and properties registered with
  472. * object and generates introspection data for the object as XML string.
  473. */
  474. static DBusMessage * introspect(DBusMessage *message,
  475. struct wpa_dbus_object_desc *obj_dsc)
  476. {
  477. DBusMessage *reply = NULL;
  478. struct interfaces *ifaces, *tmp;
  479. struct wpa_dbus_signal_desc *signal_dsc;
  480. struct wpa_dbus_method_desc *method_dsc;
  481. struct wpa_dbus_property_desc *property_dsc;
  482. char *intro_str;
  483. char **children;
  484. int i, s;
  485. xmlDocPtr doc = NULL;
  486. xmlNodePtr root_node = NULL, node = NULL, iface_node = NULL;
  487. xmlNodePtr method_node = NULL, signal_node = NULL;
  488. xmlNodePtr property_node = NULL, arg_node = NULL;
  489. /* Create and initialize the return message */
  490. reply = dbus_message_new_method_return(message);
  491. /* root node and dtd */
  492. doc = xmlNewDoc(BAD_CAST "1.0");
  493. root_node = xmlNewNode(NULL, BAD_CAST "node");
  494. xmlDocSetRootElement(doc, root_node);
  495. xmlCreateIntSubset(doc, BAD_CAST "node",
  496. BAD_CAST DBUS_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER,
  497. BAD_CAST DBUS_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER);
  498. /* Add Introspectable interface */
  499. iface_node = xmlNewChild(root_node, NULL, BAD_CAST "interface", NULL);
  500. xmlNewProp(iface_node, BAD_CAST "name",
  501. BAD_CAST WPA_DBUS_INTROSPECTION_INTERFACE);
  502. /* Add Introspect method */
  503. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  504. xmlNewProp(method_node, BAD_CAST "name",
  505. BAD_CAST WPA_DBUS_INTROSPECTION_METHOD);
  506. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  507. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "data");
  508. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  509. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
  510. /* Add Properties interface */
  511. iface_node = xmlNewChild(root_node, NULL,
  512. BAD_CAST "interface", NULL);
  513. xmlNewProp(iface_node, BAD_CAST "name",
  514. BAD_CAST WPA_DBUS_PROPERTIES_INTERFACE);
  515. /* Add Get method */
  516. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  517. xmlNewProp(method_node, BAD_CAST "name",
  518. BAD_CAST WPA_DBUS_PROPERTIES_GET);
  519. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  520. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
  521. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  522. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  523. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  524. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
  525. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  526. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  527. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  528. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
  529. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
  530. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
  531. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  532. /* Add GetAll method */
  533. xmlNewProp(method_node, BAD_CAST "name",
  534. BAD_CAST WPA_DBUS_PROPERTIES_GETALL);
  535. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  536. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
  537. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  538. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  539. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  540. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "props");
  541. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "a{sv}");
  542. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
  543. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  544. /* Add Set method */
  545. xmlNewProp(method_node, BAD_CAST "name",
  546. BAD_CAST WPA_DBUS_PROPERTIES_SET);
  547. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  548. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
  549. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  550. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  551. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  552. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
  553. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  554. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  555. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  556. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
  557. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
  558. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  559. /* get all interfaces registered with object */
  560. ifaces = extract_interfaces(obj_dsc, root_node);
  561. /* create methods' nodes */
  562. method_dsc = obj_dsc->methods;
  563. while (method_dsc) {
  564. struct interfaces *iface = ifaces;
  565. while (iface) {
  566. if (!os_strcmp(iface->dbus_interface,
  567. method_dsc->dbus_interface))
  568. break;
  569. iface = iface->next;
  570. }
  571. if (!iface)
  572. continue;
  573. iface_node = iface->interface_node;
  574. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method",
  575. NULL);
  576. xmlNewProp(method_node, BAD_CAST "name",
  577. BAD_CAST method_dsc->dbus_method);
  578. /* create args' nodes */
  579. for (i = 0; i < method_dsc->args_num; i++) {
  580. struct wpa_dbus_argument arg = method_dsc->args[i];
  581. arg_node = xmlNewChild(method_node, NULL,
  582. BAD_CAST "arg", NULL);
  583. if (arg.name && strlen(arg.name)) {
  584. xmlNewProp(arg_node, BAD_CAST "name",
  585. BAD_CAST arg.name);
  586. }
  587. xmlNewProp(arg_node, BAD_CAST "type",
  588. BAD_CAST arg.type);
  589. xmlNewProp(arg_node, BAD_CAST "direction",
  590. BAD_CAST (arg.dir == ARG_IN ?
  591. "in" : "out"));
  592. }
  593. method_dsc = method_dsc->next;
  594. }
  595. /* create signals' nodes */
  596. signal_dsc = obj_dsc->signals;
  597. while (signal_dsc) {
  598. struct interfaces *iface = ifaces;
  599. while (iface) {
  600. if (!os_strcmp(iface->dbus_interface,
  601. signal_dsc->dbus_interface))
  602. break;
  603. iface = iface->next;
  604. }
  605. if (!iface)
  606. continue;
  607. iface_node = iface->interface_node;
  608. signal_node = xmlNewChild(iface_node, NULL, BAD_CAST "signal",
  609. NULL);
  610. xmlNewProp(signal_node, BAD_CAST "name",
  611. BAD_CAST signal_dsc->dbus_signal);
  612. /* create args' nodes */
  613. for (i = 0; i < signal_dsc->args_num; i++) {
  614. struct wpa_dbus_argument arg = signal_dsc->args[i];
  615. arg_node = xmlNewChild(signal_node, NULL,
  616. BAD_CAST "arg", NULL);
  617. if (arg.name && strlen(arg.name)) {
  618. xmlNewProp(arg_node, BAD_CAST "name",
  619. BAD_CAST arg.name);
  620. }
  621. xmlNewProp(arg_node, BAD_CAST "type",
  622. BAD_CAST arg.type);
  623. }
  624. signal_dsc = signal_dsc->next;
  625. }
  626. /* create properties' nodes */
  627. property_dsc = obj_dsc->properties;
  628. while (property_dsc) {
  629. struct interfaces *iface = ifaces;
  630. while (iface) {
  631. if (!os_strcmp(iface->dbus_interface,
  632. property_dsc->dbus_interface))
  633. break;
  634. iface = iface->next;
  635. }
  636. if (!iface)
  637. continue;
  638. iface_node = iface->interface_node;
  639. property_node = xmlNewChild(iface_node, NULL,
  640. BAD_CAST "property", NULL);
  641. xmlNewProp(property_node, BAD_CAST "name",
  642. BAD_CAST property_dsc->dbus_property);
  643. xmlNewProp(property_node, BAD_CAST "type",
  644. BAD_CAST property_dsc->type);
  645. xmlNewProp(property_node, BAD_CAST "access", BAD_CAST
  646. (property_dsc->access == R ? "read" :
  647. (property_dsc->access == W ?
  648. "write" : "readwrite")));
  649. property_dsc = property_dsc->next;
  650. }
  651. /* add child nodes to introspection tree; */
  652. dbus_connection_list_registered(obj_dsc->connection,
  653. dbus_message_get_path(message),
  654. &children);
  655. for (i = 0; children[i]; i++) {
  656. node = xmlNewChild(root_node, NULL, BAD_CAST "node", NULL);
  657. xmlNewProp(node, BAD_CAST "name", BAD_CAST children[i]);
  658. }
  659. dbus_free_string_array(children);
  660. xmlDocDumpFormatMemory(doc, (xmlChar **) &intro_str, &s, 1);
  661. xmlFreeDoc(doc);
  662. while (ifaces) {
  663. tmp = ifaces;
  664. ifaces = ifaces->next;
  665. os_free(tmp->dbus_interface);
  666. os_free(tmp);
  667. }
  668. dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
  669. DBUS_TYPE_INVALID);
  670. xmlFree(intro_str);
  671. return reply;
  672. }
  673. #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  674. /**
  675. * introspect - Responds for Introspect calls on object
  676. * @message: Message with Introspect call
  677. * @obj_dsc: Object description on which Introspect was called
  678. * Returns: Message with introspection result XML string as only argument
  679. *
  680. * Returns error informing that introspection support was not compiled.
  681. */
  682. static DBusMessage * introspect(DBusMessage *message,
  683. struct wpa_dbus_object_desc *obj_dsc)
  684. {
  685. return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
  686. "wpa_supplicant was compiled without "
  687. "introspection support.");
  688. }
  689. #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  690. /**
  691. * recursive_iter_copy - Reads arguments from one iterator and
  692. * writes to another recursively
  693. * @from: iterator to read from
  694. * @to: iterator to write to
  695. *
  696. * Copies one iterator's elements to another. If any element in
  697. * iterator is of container type, its content is copied recursively
  698. */
  699. static void recursive_iter_copy(DBusMessageIter *from, DBusMessageIter *to)
  700. {
  701. char *subtype = NULL;
  702. int type;
  703. /* iterate over iterator to copy */
  704. while ((type = dbus_message_iter_get_arg_type (from)) !=
  705. DBUS_TYPE_INVALID) {
  706. /* simply copy basic type entries */
  707. if (dbus_type_is_basic(type)) {
  708. if (dbus_type_is_fixed(type)) {
  709. /*
  710. * According to DBus documentation all
  711. * fixed-length types are guaranteed to fit
  712. * 8 bytes
  713. */
  714. dbus_uint64_t v;
  715. dbus_message_iter_get_basic (from, &v);
  716. dbus_message_iter_append_basic (to, type, &v);
  717. } else {
  718. char *v;
  719. dbus_message_iter_get_basic (from, &v);
  720. dbus_message_iter_append_basic (to, type, &v);
  721. }
  722. } else {
  723. /* recursively copy container type entries */
  724. DBusMessageIter write_subiter, read_subiter;
  725. dbus_message_iter_recurse(from, &read_subiter);
  726. if (type == DBUS_TYPE_VARIANT ||
  727. type == DBUS_TYPE_ARRAY) {
  728. subtype = dbus_message_iter_get_signature(
  729. &read_subiter);
  730. }
  731. dbus_message_iter_open_container(to, type, subtype,
  732. &write_subiter);
  733. recursive_iter_copy(&read_subiter, &write_subiter);
  734. dbus_message_iter_close_container(to, &write_subiter);
  735. if (subtype)
  736. dbus_free(subtype);
  737. }
  738. dbus_message_iter_next(from);
  739. }
  740. }
  741. /**
  742. * get_all_properties - Responds for GetAll properties calls on object
  743. * @message: Message with GetAll call
  744. * @interface: interface name which properties will be returned
  745. * @property_dsc: list of object's properties
  746. * Returns: Message with dict of variants as argument with properties values
  747. *
  748. * Iterates over all properties registered with object and execute getters
  749. * of those, which are readable and which interface matches interface
  750. * specified as argument. Returned message contains one dict argument
  751. * with properties names as keys and theirs values as values.
  752. */
  753. static DBusMessage * get_all_properties(
  754. DBusMessage *message, char *interface,
  755. struct wpa_dbus_property_desc *property_dsc)
  756. {
  757. /* Create and initialize the return message */
  758. DBusMessage *reply = dbus_message_new_method_return(message);
  759. DBusMessage *getterReply = NULL;
  760. DBusMessageIter iter, dict_iter, entry_iter, ret_iter;
  761. int counter = 0;
  762. dbus_message_iter_init_append(reply, &iter);
  763. dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  764. DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
  765. DBUS_TYPE_STRING_AS_STRING
  766. DBUS_TYPE_VARIANT_AS_STRING
  767. DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
  768. &dict_iter);
  769. while (property_dsc) {
  770. if (!os_strncmp(property_dsc->dbus_interface, interface,
  771. WPAS_DBUS_INTERFACE_MAX) &&
  772. property_dsc->access != W && property_dsc->getter) {
  773. getterReply = property_dsc->getter(
  774. message, property_dsc->user_data);
  775. dbus_message_iter_init(getterReply, &ret_iter);
  776. dbus_message_iter_open_container(&dict_iter,
  777. DBUS_TYPE_DICT_ENTRY,
  778. NULL, &entry_iter);
  779. dbus_message_iter_append_basic(
  780. &entry_iter, DBUS_TYPE_STRING,
  781. &(property_dsc->dbus_property));
  782. recursive_iter_copy(&ret_iter, &entry_iter);
  783. dbus_message_iter_close_container(&dict_iter,
  784. &entry_iter);
  785. dbus_message_unref(getterReply);
  786. counter++;
  787. }
  788. property_dsc = property_dsc->next;
  789. }
  790. dbus_message_iter_close_container(&iter, &dict_iter);
  791. if (counter == 0) {
  792. dbus_message_unref(reply);
  793. reply = dbus_message_new_error(message,
  794. DBUS_ERROR_INVALID_ARGS,
  795. "No readable properties in "
  796. "this interface");
  797. }
  798. return reply;
  799. }
  800. static int is_signature_correct(DBusMessage * message,
  801. struct wpa_dbus_method_desc *method_dsc)
  802. {
  803. /* According to DBus documentation max length of signature is 255 */
  804. #define MAX_SIG_LEN 256
  805. char registered_sig[MAX_SIG_LEN];
  806. const char *sig = dbus_message_get_signature(message);
  807. int i;
  808. registered_sig[0] = 0;
  809. for (i = 0; i < method_dsc->args_num; i++) {
  810. struct wpa_dbus_argument arg = method_dsc->args[i];
  811. if (arg.dir == ARG_IN)
  812. strcat(registered_sig, arg.type);
  813. }
  814. return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
  815. }
  816. /**
  817. * message_handler - Handles incoming DBus messages
  818. * @connection: DBus connection on which message was received
  819. * @message: Received message
  820. * @user_data: pointer to description of object to which message was sent
  821. * Returns: Returns information whether message was handled or not
  822. *
  823. * Reads message interface and method name, then checks if they matches one
  824. * of the special cases i.e. introspection call or properties get/getall/set
  825. * methods and handles it. Else it iterates over registered methods list
  826. * and tries to match method's name and interface to those read from message
  827. * If appropriate method was found it's handler function is called and
  828. * response is sent. Otherwise the DBUS_ERROR_UNKNOWN_METHOD error message
  829. * will be sent.
  830. */
  831. static DBusHandlerResult message_handler(DBusConnection *connection,
  832. DBusMessage *message, void *user_data)
  833. {
  834. struct wpa_dbus_object_desc *obj_dsc = user_data;
  835. const char *method;
  836. const char *path;
  837. const char *msg_interface;
  838. #define MESSAGE_UNHANDLED (DBusMessage *) 1
  839. DBusMessage *reply = MESSAGE_UNHANDLED;
  840. /* get method, interface and path the message is addressed to */
  841. method = dbus_message_get_member(message);
  842. path = dbus_message_get_path(message);
  843. msg_interface = dbus_message_get_interface(message);
  844. if (!method || !path || !msg_interface)
  845. goto out;
  846. /* if message is introspection method call */
  847. if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
  848. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  849. !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
  850. WPAS_DBUS_INTERFACE_MAX))
  851. reply = introspect(message, obj_dsc);
  852. else if (!strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
  853. WPAS_DBUS_INTERFACE_MAX)) {
  854. /* if message is properties method call */
  855. DBusMessageIter iter;
  856. char *interface;
  857. char *property;
  858. dbus_message_iter_init(message, &iter);
  859. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  860. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  861. !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
  862. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  863. !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  864. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  865. /* First argument: interface name (DBUS_TYPE_STRING) */
  866. if (dbus_message_iter_get_arg_type(&iter) !=
  867. DBUS_TYPE_STRING) {
  868. reply = dbus_message_new_error(
  869. message, DBUS_ERROR_INVALID_ARGS,
  870. NULL);
  871. goto out;
  872. }
  873. dbus_message_iter_get_basic(&iter, &interface);
  874. /* GetAll */
  875. if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  876. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  877. if (os_strcmp(dbus_message_get_signature(
  878. message), "s"))
  879. reply = dbus_message_new_error(
  880. message,
  881. DBUS_ERROR_INVALID_ARGS, NULL);
  882. else
  883. reply = get_all_properties(
  884. message, interface,
  885. obj_dsc->properties);
  886. } else {
  887. /* Get or Set */
  888. struct wpa_dbus_property_desc *property_dsc;
  889. property_dsc = obj_dsc->properties;
  890. /* Second argument: property name
  891. * (DBUS_TYPE_STRING) */
  892. if (!dbus_message_iter_next(&iter) ||
  893. dbus_message_iter_get_arg_type(&iter) !=
  894. DBUS_TYPE_STRING) {
  895. reply = dbus_message_new_error(
  896. message,
  897. DBUS_ERROR_INVALID_ARGS, NULL);
  898. goto out;
  899. }
  900. dbus_message_iter_get_basic(&iter, &property);
  901. while (property_dsc) {
  902. /* compare property names and
  903. * interfaces */
  904. if (!os_strncmp(property_dsc->dbus_property,
  905. property,
  906. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  907. !os_strncmp(property_dsc->dbus_interface, interface,
  908. WPAS_DBUS_INTERFACE_MAX))
  909. break;
  910. property_dsc = property_dsc->next;
  911. }
  912. if (property_dsc) {
  913. /* Get */
  914. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  915. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  916. if (os_strcmp(dbus_message_get_signature(message), "ss"))
  917. reply = dbus_message_new_error(message,
  918. DBUS_ERROR_INVALID_ARGS, NULL);
  919. else if (property_dsc->access != W &&
  920. property_dsc->getter)
  921. reply = property_dsc->getter(message,
  922. property_dsc->user_data);
  923. else
  924. reply = dbus_message_new_error(message,
  925. DBUS_ERROR_INVALID_ARGS,
  926. "Property is write-only");
  927. } else {
  928. /* Set */
  929. if (os_strcmp(dbus_message_get_signature(message), "ssv"))
  930. reply = dbus_message_new_error(message,
  931. DBUS_ERROR_INVALID_ARGS, NULL);
  932. else if (property_dsc->access != R &&
  933. property_dsc->setter) {
  934. reply = property_dsc->setter
  935. (message, property_dsc->user_data);
  936. } else {
  937. reply = dbus_message_new_error(message,
  938. DBUS_ERROR_INVALID_ARGS,
  939. "Property is read-only");
  940. }
  941. }
  942. } else {
  943. wpa_printf(MSG_DEBUG, "no property handler for %s.%s\n"
  944. "on %s", interface, property, path);
  945. reply = dbus_message_new_error(message,
  946. DBUS_ERROR_INVALID_ARGS,
  947. "No such property");
  948. }
  949. }
  950. } else {
  951. reply = dbus_message_new_error(
  952. message, DBUS_ERROR_UNKNOWN_METHOD, NULL);
  953. goto out;
  954. }
  955. } else {
  956. struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  957. /* try match call to any registered method */
  958. while (method_dsc) {
  959. /* compare method names and interfaces */
  960. if (!os_strncmp(method_dsc->dbus_method, method,
  961. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  962. !os_strncmp(method_dsc->dbus_interface,
  963. msg_interface,
  964. WPAS_DBUS_INTERFACE_MAX))
  965. break;
  966. method_dsc = method_dsc->next;
  967. }
  968. if (method_dsc) {
  969. if (is_signature_correct(message, method_dsc)) {
  970. reply = method_dsc->method_handler(
  971. message, method_dsc->handler_argument);
  972. } else {
  973. reply = dbus_message_new_error(
  974. message, DBUS_ERROR_INVALID_ARGS,
  975. NULL);
  976. }
  977. } else {
  978. wpa_printf(MSG_DEBUG, "no method handler for %s.%s "
  979. "on %s", msg_interface, method, path);
  980. reply = dbus_message_new_error(
  981. message, DBUS_ERROR_UNKNOWN_METHOD, NULL);
  982. }
  983. }
  984. out:
  985. /* If the message was handled, send back the reply */
  986. if (reply != MESSAGE_UNHANDLED) {
  987. /* If handler succeed returning NULL, reply empty message */
  988. if (!reply)
  989. reply = dbus_message_new_method_return(message);
  990. if (reply) {
  991. if (!dbus_message_get_no_reply(message))
  992. dbus_connection_send(connection, reply, NULL);
  993. dbus_message_unref(reply);
  994. }
  995. return DBUS_HANDLER_RESULT_HANDLED;
  996. } else
  997. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  998. }
  999. /**
  1000. * free_dbus_object_desc - Frees object description data structure
  1001. * @connection: DBus connection
  1002. * @obj_dsc: Object description to free
  1003. *
  1004. * Frees each of properties, methods and signals description lists and
  1005. * the object description structure itself.
  1006. */
  1007. void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
  1008. {
  1009. struct wpa_dbus_method_desc *method_dsc, *tmp_met_dsc;
  1010. struct wpa_dbus_signal_desc *signal_dsc, *tmp_sig_dsc;
  1011. struct wpa_dbus_property_desc *property_dsc, *tmp_prop_dsc;
  1012. int i;
  1013. if (!obj_dsc)
  1014. return;
  1015. /* free methods */
  1016. method_dsc = obj_dsc->methods;
  1017. while (method_dsc) {
  1018. tmp_met_dsc = method_dsc;
  1019. method_dsc = method_dsc->next;
  1020. os_free(tmp_met_dsc->dbus_interface);
  1021. os_free(tmp_met_dsc->dbus_method);
  1022. for (i = 0; i < tmp_met_dsc->args_num; i++) {
  1023. os_free(tmp_met_dsc->args[i].name);
  1024. os_free(tmp_met_dsc->args[i].type);
  1025. }
  1026. if (tmp_met_dsc->argument_free_func)
  1027. tmp_met_dsc->argument_free_func(
  1028. tmp_met_dsc->handler_argument);
  1029. os_free(tmp_met_dsc);
  1030. }
  1031. /* free signals */
  1032. signal_dsc = obj_dsc->signals;
  1033. while (signal_dsc) {
  1034. tmp_sig_dsc = signal_dsc;
  1035. signal_dsc = signal_dsc->next;
  1036. os_free(tmp_sig_dsc->dbus_interface);
  1037. os_free(tmp_sig_dsc->dbus_signal);
  1038. for (i = 0; i < tmp_sig_dsc->args_num; i++) {
  1039. os_free(tmp_sig_dsc->args[i].name);
  1040. os_free(tmp_sig_dsc->args[i].type);
  1041. }
  1042. os_free(tmp_sig_dsc);
  1043. }
  1044. /* free properties */
  1045. property_dsc = obj_dsc->properties;
  1046. while (property_dsc) {
  1047. tmp_prop_dsc = property_dsc;
  1048. property_dsc = property_dsc->next;
  1049. os_free(tmp_prop_dsc->dbus_interface);
  1050. os_free(tmp_prop_dsc->dbus_property);
  1051. os_free(tmp_prop_dsc->type);
  1052. if (tmp_prop_dsc->user_data_free_func)
  1053. tmp_prop_dsc->user_data_free_func(
  1054. tmp_prop_dsc->user_data);
  1055. os_free(tmp_prop_dsc);
  1056. }
  1057. os_free(obj_dsc);
  1058. }
  1059. static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
  1060. {
  1061. free_dbus_object_desc(obj_dsc);
  1062. }
  1063. /**
  1064. * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
  1065. * @application_data: Pointer to application specific data structure
  1066. * @dbus_path: DBus path to interface object
  1067. * @dbus_service: DBus service name to register with
  1068. * @messageHandler: a pointer to function which will handle dbus messages
  1069. * coming on interface
  1070. * Returns: Pointer to dbus_new_ctrl_iface date or %NULL on failure
  1071. *
  1072. * Initialize the dbus control interface and start receiving commands from
  1073. * external programs over the bus.
  1074. */
  1075. struct ctrl_iface_dbus_new_priv *
  1076. wpa_dbus_ctrl_iface_init(void *application_data,
  1077. char *dbus_path, char *dbus_service,
  1078. struct wpa_dbus_object_desc *obj_desc)
  1079. {
  1080. struct ctrl_iface_dbus_new_priv *iface;
  1081. DBusError error;
  1082. int ret = -1;
  1083. DBusObjectPathVTable wpa_vtable = {
  1084. &free_dbus_object_desc_cb, &message_handler,
  1085. NULL, NULL, NULL, NULL
  1086. };
  1087. iface = os_zalloc(sizeof(struct ctrl_iface_dbus_new_priv));
  1088. if (iface == NULL)
  1089. return NULL;
  1090. iface->application_data = application_data;
  1091. /* Get a reference to the system bus */
  1092. dbus_error_init(&error);
  1093. iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
  1094. dbus_error_free(&error);
  1095. if (!iface->con) {
  1096. perror("dbus_bus_get[ctrl_iface_dbus]");
  1097. wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
  1098. goto fail;
  1099. }
  1100. obj_desc->connection = iface->con;
  1101. /* Tell dbus about our mainloop integration functions */
  1102. if (integrate_with_eloop(iface->con, iface))
  1103. goto fail;
  1104. /* Register the message handler for the global dbus interface */
  1105. if (!dbus_connection_register_object_path(iface->con,
  1106. dbus_path, &wpa_vtable,
  1107. obj_desc)) {
  1108. perror("dbus_connection_register_object_path[dbus]");
  1109. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  1110. "handler.");
  1111. goto fail;
  1112. }
  1113. /* Register our service with the message bus */
  1114. dbus_error_init(&error);
  1115. switch (dbus_bus_request_name(iface->con, dbus_service,
  1116. 0, &error)) {
  1117. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  1118. ret = 0;
  1119. break;
  1120. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  1121. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  1122. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  1123. perror("dbus_bus_request_name[dbus]");
  1124. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  1125. "already registered.");
  1126. break;
  1127. default:
  1128. perror("dbus_bus_request_name[dbus]");
  1129. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  1130. "%s %s.", error.name, error.message);
  1131. break;
  1132. }
  1133. dbus_error_free(&error);
  1134. if (ret != 0)
  1135. goto fail;
  1136. wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
  1137. /*
  1138. * Dispatch initial DBus messages that may have come in since the bus
  1139. * name was claimed above. Happens when clients are quick to notice the
  1140. * service.
  1141. *
  1142. * FIXME: is there a better solution to this problem?
  1143. */
  1144. eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
  1145. iface->con, NULL);
  1146. return iface;
  1147. fail:
  1148. wpa_dbus_ctrl_iface_deinit(iface);
  1149. return NULL;
  1150. }
  1151. /**
  1152. * wpa_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
  1153. * @iface: Pointer to dbus private data from wpa_dbus_ctrl_iface_init()
  1154. *
  1155. * Deinitialize the dbus control interface that was initialized with
  1156. * wpa_dbus_ctrl_iface_init().
  1157. */
  1158. void wpa_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_new_priv *iface)
  1159. {
  1160. if (iface == NULL)
  1161. return;
  1162. if (iface->con) {
  1163. eloop_cancel_timeout(dispatch_initial_dbus_messages,
  1164. iface->con, NULL);
  1165. dbus_connection_set_watch_functions(iface->con, NULL, NULL,
  1166. NULL, NULL, NULL);
  1167. dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
  1168. NULL, NULL, NULL);
  1169. dbus_connection_unref(iface->con);
  1170. }
  1171. os_memset(iface, 0, sizeof(struct ctrl_iface_dbus_new_priv));
  1172. os_free(iface);
  1173. }
  1174. /**
  1175. * wpa_dbus_register_object_per_iface - Register a new object with dbus
  1176. * @ctrl_iface: pointer to dbus private data
  1177. * @path: DBus path to object
  1178. * @ifname: interface name
  1179. * @obj_desc: description of object's methods, signals and properties
  1180. * Returns: 0 on success, -1 on error
  1181. *
  1182. * Registers a new interface with dbus and assigns it a dbus object path.
  1183. */
  1184. int wpa_dbus_register_object_per_iface(
  1185. struct ctrl_iface_dbus_new_priv *ctrl_iface,
  1186. const char *path, const char *ifname,
  1187. struct wpa_dbus_object_desc *obj_desc)
  1188. {
  1189. DBusConnection *con;
  1190. DBusObjectPathVTable vtable = {
  1191. &free_dbus_object_desc_cb, &message_handler,
  1192. NULL, NULL, NULL, NULL
  1193. };
  1194. /* Do nothing if the control interface is not turned on */
  1195. if (ctrl_iface == NULL)
  1196. return 0;
  1197. con = ctrl_iface->con;
  1198. obj_desc->connection = con;
  1199. /* Register the message handler for the interface functions */
  1200. if (!dbus_connection_register_object_path(con, path, &vtable,
  1201. obj_desc)) {
  1202. perror("wpa_dbus_register_iface [dbus]");
  1203. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  1204. "handler for interface %s\n"
  1205. "and object %s.", ifname, path);
  1206. return -1;
  1207. }
  1208. return 0;
  1209. }
  1210. /**
  1211. * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
  1212. * @ctrl_iface: Pointer to dbus private data
  1213. * @path: DBus path to object which will be unregistered
  1214. * Returns: Zero on success and -1 on failure
  1215. *
  1216. * Unregisters DBus object given by its path
  1217. */
  1218. int wpa_dbus_unregister_object_per_iface(
  1219. struct ctrl_iface_dbus_new_priv *ctrl_iface, const char *path)
  1220. {
  1221. DBusConnection *con = ctrl_iface->con;
  1222. if (!dbus_connection_unregister_object_path(con, path))
  1223. return -1;
  1224. return 0;
  1225. }
  1226. /**
  1227. * wpa_dbus_method_register - Registers DBus method for given object
  1228. * @obj_dsc: Object description for which a method will be registered
  1229. * @dbus_interface: DBus interface under which method will be registered
  1230. * @dbus_method: a name the method will be registered with
  1231. * @method_handler: a function which will be called to handle this method call
  1232. * @handler_argument: an additional argument passed to handler function
  1233. * @argument_free_func: function used to free handler argument
  1234. * @args: method arguments list
  1235. * Returns: Zero on success and -1 on failure
  1236. *
  1237. * Registers DBus method under given name and interface for the object.
  1238. * Method calls will be handled with given handling function and optional
  1239. * argument passed to this function. Handler function is required to return
  1240. * a DBusMessage pointer which will be response to method call. Any method
  1241. * call before being handled must have registered appropriate handler by
  1242. * using this function.
  1243. */
  1244. int wpa_dbus_method_register(struct wpa_dbus_object_desc *obj_dsc,
  1245. char *dbus_interface, char *dbus_method,
  1246. WPADBusMethodHandler method_handler,
  1247. void *handler_argument,
  1248. WPADBusArgumentFreeFunction argument_free_func,
  1249. struct wpa_dbus_argument args[])
  1250. {
  1251. struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  1252. struct wpa_dbus_method_desc *prev_desc;
  1253. int args_num = 0;
  1254. int interface_len, method_len, i, len, error;
  1255. prev_desc = NULL;
  1256. while (method_dsc) {
  1257. prev_desc = method_dsc;
  1258. method_dsc = method_dsc->next;
  1259. }
  1260. /* count args */
  1261. if (args) {
  1262. while (args[args_num].name && args[args_num].type)
  1263. args_num++;
  1264. }
  1265. method_dsc = os_zalloc(sizeof(struct wpa_dbus_method_desc) +
  1266. args_num * sizeof(struct wpa_dbus_argument));
  1267. if (!method_dsc)
  1268. return -1;
  1269. if (prev_desc == NULL)
  1270. obj_dsc->methods = method_dsc;
  1271. else
  1272. prev_desc->next = method_dsc;
  1273. /* copy interface name */
  1274. interface_len = os_strlen(dbus_interface) + 1;
  1275. method_dsc->dbus_interface = os_malloc(interface_len);
  1276. if (!method_dsc->dbus_interface)
  1277. goto err;
  1278. os_strncpy(method_dsc->dbus_interface, dbus_interface, interface_len);
  1279. /* copy method name */
  1280. method_len = os_strlen(dbus_method) + 1;
  1281. method_dsc->dbus_method = os_malloc(method_len);
  1282. if (!method_dsc->dbus_method)
  1283. goto err;
  1284. os_strncpy(method_dsc->dbus_method, dbus_method, method_len);
  1285. /* copy arguments */
  1286. error = 0;
  1287. method_dsc->args_num = args_num;
  1288. for (i = 0; i < args_num; i++) {
  1289. len = os_strlen(args[i].name) + 1;
  1290. method_dsc->args[i].name = os_malloc(len);
  1291. if (!method_dsc->args[i].name) {
  1292. error = 1;
  1293. continue;
  1294. }
  1295. os_strncpy(method_dsc->args[i].name, args[i].name, len);
  1296. len = os_strlen(args[i].type) + 1;
  1297. method_dsc->args[i].type = os_malloc(len);
  1298. if (!method_dsc->args[i].type) {
  1299. error = 1;
  1300. continue;
  1301. }
  1302. os_strncpy(method_dsc->args[i].type, args[i].type, len);
  1303. method_dsc->args[i].dir = args[i].dir;
  1304. }
  1305. if (error)
  1306. goto err;
  1307. method_dsc->method_handler = method_handler;
  1308. method_dsc->handler_argument = handler_argument;
  1309. method_dsc->argument_free_func = argument_free_func;
  1310. method_dsc->next = NULL;
  1311. return 0;
  1312. err:
  1313. if (method_dsc) {
  1314. os_free(method_dsc->dbus_interface);
  1315. os_free(method_dsc->dbus_method);
  1316. for (i = 0; i < method_dsc->args_num; i++) {
  1317. os_free(method_dsc->args[i].name);
  1318. os_free(method_dsc->args[i].type);
  1319. }
  1320. if (prev_desc == NULL)
  1321. obj_dsc->methods = NULL;
  1322. else
  1323. prev_desc->next = NULL;
  1324. os_free(method_dsc);
  1325. }
  1326. return -1;
  1327. }
  1328. /**
  1329. * wpa_dbus_signal_register - Registers DBus signal for given object
  1330. * @obj_dsc: Object description for which a signal will be registered
  1331. * @dbus_interface: DBus interface under which signal will be registered
  1332. * @dbus_signal: a name the signal will be registered with
  1333. * @args: signal arguments list
  1334. * Returns: Zero on success and -1 on failure
  1335. *
  1336. * Registers DBus signal under given name and interface for the object.
  1337. * Signal registration is NOT required in order to send signals, but not
  1338. * registered signals will not be respected in introspection data
  1339. * therefore it is highly recommended to register every signal before
  1340. * using it.
  1341. */
  1342. int wpa_dbus_signal_register(struct wpa_dbus_object_desc *obj_dsc,
  1343. char *dbus_interface, char *dbus_signal,
  1344. struct wpa_dbus_argument args[])
  1345. {
  1346. struct wpa_dbus_signal_desc *signal_dsc = obj_dsc->signals;
  1347. struct wpa_dbus_signal_desc *prev_desc;
  1348. int args_num = 0;
  1349. int interface_len, signal_len, i, len, error = 0;
  1350. prev_desc = NULL;
  1351. while (signal_dsc) {
  1352. prev_desc = signal_dsc;
  1353. signal_dsc = signal_dsc->next;
  1354. }
  1355. /* count args */
  1356. if (args) {
  1357. while (args[args_num].name && args[args_num].type)
  1358. args_num++;
  1359. }
  1360. signal_dsc = os_zalloc(sizeof(struct wpa_dbus_signal_desc) +
  1361. args_num*sizeof(struct wpa_dbus_argument));
  1362. if (!signal_dsc)
  1363. return -1;
  1364. if (prev_desc == NULL)
  1365. obj_dsc->signals = signal_dsc;
  1366. else
  1367. prev_desc->next = signal_dsc;
  1368. /* copy interface name */
  1369. interface_len = strlen(dbus_interface) + 1;
  1370. signal_dsc->dbus_interface = os_malloc(interface_len);
  1371. if (!signal_dsc->dbus_interface)
  1372. goto err;
  1373. os_strncpy(signal_dsc->dbus_interface, dbus_interface, interface_len);
  1374. /* copy signal name */
  1375. signal_len = strlen(dbus_signal) + 1;
  1376. signal_dsc->dbus_signal = os_malloc(signal_len);
  1377. if (!signal_dsc->dbus_signal)
  1378. goto err;
  1379. os_strncpy(signal_dsc->dbus_signal, dbus_signal, signal_len);
  1380. /* copy arguments */
  1381. signal_dsc->args_num = args_num;
  1382. for (i = 0; i < args_num; i++) {
  1383. len = os_strlen(args[i].name) + 1;
  1384. signal_dsc->args[i].name = os_malloc(len);
  1385. if (!signal_dsc->args[i].name) {
  1386. error = 1;
  1387. continue;
  1388. }
  1389. os_strncpy(signal_dsc->args[i].name, args[i].name, len);
  1390. len = strlen(args[i].type) + 1;
  1391. signal_dsc->args[i].type = os_malloc(len);
  1392. if (!signal_dsc->args[i].type) {
  1393. error = 1;
  1394. continue;
  1395. }
  1396. os_strncpy(signal_dsc->args[i].type, args[i].type, len);
  1397. }
  1398. if (error)
  1399. goto err;
  1400. signal_dsc->next = NULL;
  1401. return 0;
  1402. err:
  1403. if (signal_dsc) {
  1404. os_free(signal_dsc->dbus_interface);
  1405. os_free(signal_dsc->dbus_signal);
  1406. for (i = 0; i < signal_dsc->args_num; i++) {
  1407. os_free(signal_dsc->args[i].name);
  1408. os_free(signal_dsc->args[i].type);
  1409. }
  1410. if (prev_desc == NULL)
  1411. obj_dsc->signals = NULL;
  1412. else
  1413. prev_desc->next = NULL;
  1414. os_free(signal_dsc);
  1415. }
  1416. return -1;
  1417. }
  1418. /**
  1419. * wpa_dbus_property_register - Registers DBus property for given object
  1420. * @obj_dsc: Object description for which a property will be registered
  1421. * @dbus_interface: DBus interface under which method will be registered
  1422. * @dbus_property: a name the property will be registered with
  1423. * @type: a property type signature in form of DBus type description
  1424. * @getter: a function called in order to get property value
  1425. * @setter: a function called in order to set property value
  1426. * @user_data: additional argument passed to setter or getter
  1427. * @user_data_free_func: function used to free additional argument
  1428. * @access: property access permissions specifier (R, W or RW)
  1429. * Returns: Zero on success and -1 on failure
  1430. *
  1431. * Registers DBus property under given name and interface for the object.
  1432. * Property are set with giver setter function and get with getter.
  1433. * Additional argument is passed to getter or setter. Getter or setter
  1434. * are required to return DBusMessage which is response to Set/Get method
  1435. * calls. Every property must be registered by this function before being
  1436. * used.
  1437. */
  1438. int wpa_dbus_property_register(struct wpa_dbus_object_desc *obj_dsc,
  1439. char *dbus_interface, char *dbus_property,
  1440. char *type,
  1441. WPADBusPropertyAccessor getter,
  1442. WPADBusPropertyAccessor setter,
  1443. void *user_data,
  1444. WPADBusArgumentFreeFunction user_data_free_func,
  1445. enum dbus_prop_access _access)
  1446. {
  1447. struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties;
  1448. struct wpa_dbus_property_desc *prev_desc;
  1449. int interface_len, property_len, type_len;
  1450. prev_desc = NULL;
  1451. while (property_dsc) {
  1452. prev_desc = property_dsc;
  1453. property_dsc = property_dsc->next;
  1454. }
  1455. property_dsc = os_zalloc(sizeof(struct wpa_dbus_property_desc));
  1456. if (!property_dsc)
  1457. return -1;
  1458. if (prev_desc == NULL)
  1459. obj_dsc->properties = property_dsc;
  1460. else
  1461. prev_desc->next = property_dsc;
  1462. /* copy interface name */
  1463. interface_len = os_strlen(dbus_interface) + 1;
  1464. property_dsc->dbus_interface = os_malloc(interface_len);
  1465. if (!property_dsc->dbus_interface)
  1466. goto err;
  1467. os_strncpy(property_dsc->dbus_interface, dbus_interface,
  1468. interface_len);
  1469. /* copy property name */
  1470. property_len = os_strlen(dbus_property) + 1;
  1471. property_dsc->dbus_property = os_malloc(property_len);
  1472. if (!property_dsc->dbus_property)
  1473. goto err;
  1474. os_strncpy(property_dsc->dbus_property, dbus_property, property_len);
  1475. /* copy property type */
  1476. type_len = os_strlen(type) + 1;
  1477. property_dsc->type = os_malloc(type_len);
  1478. if (!property_dsc->type)
  1479. goto err;
  1480. os_strncpy(property_dsc->type, type, type_len);
  1481. property_dsc->getter = getter;
  1482. property_dsc->setter = setter;
  1483. property_dsc->user_data = user_data;
  1484. property_dsc->user_data_free_func = user_data_free_func;
  1485. property_dsc->access = _access;
  1486. property_dsc->next = NULL;
  1487. return 0;
  1488. err:
  1489. if (property_dsc) {
  1490. os_free(property_dsc->dbus_interface);
  1491. os_free(property_dsc->dbus_property);
  1492. os_free(property_dsc->type);
  1493. if (prev_desc == NULL)
  1494. obj_dsc->properties = NULL;
  1495. else
  1496. prev_desc->next = NULL;
  1497. os_free(property_dsc);
  1498. }
  1499. return -1;
  1500. }
  1501. /**
  1502. * wpas_dbus_signal_network_added - Send a property changed signal
  1503. * @iface: dbus priv struct
  1504. * @property_getter: propperty getter used to fetch new property value
  1505. * @getter_arg: argument passed to property getter
  1506. * @path: path to object which property has changed
  1507. * @interface_name: signal and property interface
  1508. * @property_name: name of property which has changed
  1509. *
  1510. * Notify listeners about changing value of some property. Signal
  1511. * contains property name and its value fetched using given property
  1512. * getter.
  1513. */
  1514. void wpa_dbus_signal_property_changed(struct ctrl_iface_dbus_new_priv *iface,
  1515. WPADBusPropertyAccessor property_getter,
  1516. void *getter_arg,
  1517. const char *path,
  1518. const char *interface_name,
  1519. const char *property_name)
  1520. {
  1521. DBusConnection *connection;
  1522. DBusMessage *_signal, *getter_reply;
  1523. DBusMessageIter prop_iter, signal_iter, dict_iter, entry_iter;
  1524. if (!iface)
  1525. return;
  1526. connection = iface->con;
  1527. if (!property_getter) {
  1528. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1529. "[dbus]: property getter not specified");
  1530. return;
  1531. }
  1532. if (!path || !interface_name || !property_name) {
  1533. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1534. "[dbus]: path interface of property not specified");
  1535. return;
  1536. }
  1537. getter_reply = property_getter(NULL, getter_arg);
  1538. if (!getter_reply ||
  1539. dbus_message_get_type(getter_reply) == DBUS_MESSAGE_TYPE_ERROR) {
  1540. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1541. "[dbus]: cannot get new value of property %s",
  1542. property_name);
  1543. return;
  1544. }
  1545. _signal = dbus_message_new_signal(path, interface_name,
  1546. "PropertiesChanged");
  1547. if (!_signal) {
  1548. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1549. "[dbus]: cannot allocate signal");
  1550. dbus_message_unref(getter_reply);
  1551. return;
  1552. }
  1553. dbus_message_iter_init(getter_reply, &prop_iter);
  1554. dbus_message_iter_init_append(_signal, &signal_iter);
  1555. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  1556. "{sv}", &dict_iter)) {
  1557. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1558. "[dbus]: out of memory. cannot open dictionary");
  1559. goto err;
  1560. }
  1561. if (!dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY,
  1562. NULL, &entry_iter)) {
  1563. wpa_printf(MSG_ERROR, "iwpa_dbus_signal_property_changed"
  1564. "[dbus]: out of memory. cannot open dictionary "
  1565. "element");
  1566. goto err;
  1567. }
  1568. if (!dbus_message_iter_append_basic(&entry_iter, DBUS_TYPE_STRING,
  1569. &property_name)) {
  1570. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1571. "[dbus]: out of memory. cannot open add property "
  1572. "name");
  1573. goto err;
  1574. }
  1575. recursive_iter_copy(&prop_iter, &entry_iter);
  1576. if (!dbus_message_iter_close_container(&dict_iter, &entry_iter)) {
  1577. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1578. "[dbus]: out of memory. cannot close dictionary "
  1579. "element");
  1580. goto err;
  1581. }
  1582. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
  1583. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1584. "[dbus]: out of memory. cannot close dictionary");
  1585. goto err;
  1586. }
  1587. dbus_connection_send(connection, _signal, NULL);
  1588. err:
  1589. dbus_message_unref(getter_reply);
  1590. dbus_message_unref(_signal);
  1591. }