dbus_new_helpers.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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 "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 *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. xmlChar *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, &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], *pos;
  806. const char *sig = dbus_message_get_signature(message);
  807. int i, ret;
  808. pos = registered_sig;
  809. *pos = '\0';
  810. for (i = 0; i < method_dsc->args_num; i++) {
  811. struct wpa_dbus_argument arg = method_dsc->args[i];
  812. if (arg.dir == ARG_IN) {
  813. size_t blen = registered_sig + MAX_SIG_LEN - pos;
  814. ret = os_snprintf(pos, blen, "%s", arg.type);
  815. if (ret < 0 || (size_t) ret >= blen)
  816. return 0;
  817. pos += ret;
  818. }
  819. }
  820. return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
  821. }
  822. /**
  823. * message_handler - Handles incoming DBus messages
  824. * @connection: DBus connection on which message was received
  825. * @message: Received message
  826. * @user_data: pointer to description of object to which message was sent
  827. * Returns: Returns information whether message was handled or not
  828. *
  829. * Reads message interface and method name, then checks if they matches one
  830. * of the special cases i.e. introspection call or properties get/getall/set
  831. * methods and handles it. Else it iterates over registered methods list
  832. * and tries to match method's name and interface to those read from message
  833. * If appropriate method was found it's handler function is called and
  834. * response is sent. Otherwise the DBUS_ERROR_UNKNOWN_METHOD error message
  835. * will be sent.
  836. */
  837. static DBusHandlerResult message_handler(DBusConnection *connection,
  838. DBusMessage *message, void *user_data)
  839. {
  840. struct wpa_dbus_object_desc *obj_dsc = user_data;
  841. const char *method;
  842. const char *path;
  843. const char *msg_interface;
  844. #define MESSAGE_UNHANDLED (DBusMessage *) 1
  845. DBusMessage *reply = MESSAGE_UNHANDLED;
  846. /* get method, interface and path the message is addressed to */
  847. method = dbus_message_get_member(message);
  848. path = dbus_message_get_path(message);
  849. msg_interface = dbus_message_get_interface(message);
  850. if (!method || !path || !msg_interface)
  851. goto out;
  852. wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s)",
  853. msg_interface, method, path);
  854. /* if message is introspection method call */
  855. if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
  856. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  857. !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
  858. WPAS_DBUS_INTERFACE_MAX))
  859. reply = introspect(message, obj_dsc);
  860. else if (!strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
  861. WPAS_DBUS_INTERFACE_MAX)) {
  862. /* if message is properties method call */
  863. DBusMessageIter iter;
  864. char *interface;
  865. char *property;
  866. dbus_message_iter_init(message, &iter);
  867. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  868. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  869. !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
  870. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  871. !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  872. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  873. /* First argument: interface name (DBUS_TYPE_STRING) */
  874. if (dbus_message_iter_get_arg_type(&iter) !=
  875. DBUS_TYPE_STRING) {
  876. reply = dbus_message_new_error(
  877. message, DBUS_ERROR_INVALID_ARGS,
  878. NULL);
  879. goto out;
  880. }
  881. dbus_message_iter_get_basic(&iter, &interface);
  882. /* GetAll */
  883. if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  884. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  885. if (os_strcmp(dbus_message_get_signature(
  886. message), "s"))
  887. reply = dbus_message_new_error(
  888. message,
  889. DBUS_ERROR_INVALID_ARGS, NULL);
  890. else
  891. reply = get_all_properties(
  892. message, interface,
  893. obj_dsc->properties);
  894. } else {
  895. /* Get or Set */
  896. struct wpa_dbus_property_desc *property_dsc;
  897. property_dsc = obj_dsc->properties;
  898. /* Second argument: property name
  899. * (DBUS_TYPE_STRING) */
  900. if (!dbus_message_iter_next(&iter) ||
  901. dbus_message_iter_get_arg_type(&iter) !=
  902. DBUS_TYPE_STRING) {
  903. reply = dbus_message_new_error(
  904. message,
  905. DBUS_ERROR_INVALID_ARGS, NULL);
  906. goto out;
  907. }
  908. dbus_message_iter_get_basic(&iter, &property);
  909. while (property_dsc) {
  910. /* compare property names and
  911. * interfaces */
  912. if (!os_strncmp(property_dsc->dbus_property,
  913. property,
  914. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  915. !os_strncmp(property_dsc->dbus_interface, interface,
  916. WPAS_DBUS_INTERFACE_MAX))
  917. break;
  918. property_dsc = property_dsc->next;
  919. }
  920. if (property_dsc) {
  921. /* Get */
  922. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  923. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  924. if (os_strcmp(dbus_message_get_signature(message), "ss"))
  925. reply = dbus_message_new_error(message,
  926. DBUS_ERROR_INVALID_ARGS, NULL);
  927. else if (property_dsc->access != W &&
  928. property_dsc->getter)
  929. reply = property_dsc->getter(message,
  930. property_dsc->user_data);
  931. else
  932. reply = dbus_message_new_error(message,
  933. DBUS_ERROR_INVALID_ARGS,
  934. "Property is write-only");
  935. } else {
  936. /* Set */
  937. if (os_strcmp(dbus_message_get_signature(message), "ssv"))
  938. reply = dbus_message_new_error(message,
  939. DBUS_ERROR_INVALID_ARGS, NULL);
  940. else if (property_dsc->access != R &&
  941. property_dsc->setter) {
  942. reply = property_dsc->setter
  943. (message, property_dsc->user_data);
  944. } else {
  945. reply = dbus_message_new_error(message,
  946. DBUS_ERROR_INVALID_ARGS,
  947. "Property is read-only");
  948. }
  949. }
  950. } else {
  951. wpa_printf(MSG_DEBUG, "no property handler for %s.%s\n"
  952. "on %s", interface, property, path);
  953. reply = dbus_message_new_error(message,
  954. DBUS_ERROR_INVALID_ARGS,
  955. "No such property");
  956. }
  957. }
  958. } else {
  959. reply = dbus_message_new_error(
  960. message, DBUS_ERROR_UNKNOWN_METHOD, NULL);
  961. goto out;
  962. }
  963. } else {
  964. struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  965. /* try match call to any registered method */
  966. while (method_dsc) {
  967. /* compare method names and interfaces */
  968. if (!os_strncmp(method_dsc->dbus_method, method,
  969. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  970. !os_strncmp(method_dsc->dbus_interface,
  971. msg_interface,
  972. WPAS_DBUS_INTERFACE_MAX))
  973. break;
  974. method_dsc = method_dsc->next;
  975. }
  976. if (method_dsc) {
  977. if (is_signature_correct(message, method_dsc)) {
  978. reply = method_dsc->method_handler(
  979. message, method_dsc->handler_argument);
  980. } else {
  981. reply = dbus_message_new_error(
  982. message, DBUS_ERROR_INVALID_ARGS,
  983. NULL);
  984. }
  985. } else {
  986. wpa_printf(MSG_DEBUG, "no method handler for %s.%s "
  987. "on %s", msg_interface, method, path);
  988. reply = dbus_message_new_error(
  989. message, DBUS_ERROR_UNKNOWN_METHOD, NULL);
  990. }
  991. }
  992. out:
  993. /* If the message was handled, send back the reply */
  994. if (reply != MESSAGE_UNHANDLED) {
  995. /* If handler succeed returning NULL, reply empty message */
  996. if (!reply)
  997. reply = dbus_message_new_method_return(message);
  998. if (reply) {
  999. if (!dbus_message_get_no_reply(message))
  1000. dbus_connection_send(connection, reply, NULL);
  1001. dbus_message_unref(reply);
  1002. }
  1003. return DBUS_HANDLER_RESULT_HANDLED;
  1004. } else
  1005. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  1006. }
  1007. /**
  1008. * free_dbus_object_desc - Frees object description data structure
  1009. * @connection: DBus connection
  1010. * @obj_dsc: Object description to free
  1011. *
  1012. * Frees each of properties, methods and signals description lists and
  1013. * the object description structure itself.
  1014. */
  1015. void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
  1016. {
  1017. struct wpa_dbus_method_desc *method_dsc, *tmp_met_dsc;
  1018. struct wpa_dbus_signal_desc *signal_dsc, *tmp_sig_dsc;
  1019. struct wpa_dbus_property_desc *property_dsc, *tmp_prop_dsc;
  1020. int i;
  1021. if (!obj_dsc)
  1022. return;
  1023. /* free methods */
  1024. method_dsc = obj_dsc->methods;
  1025. while (method_dsc) {
  1026. tmp_met_dsc = method_dsc;
  1027. method_dsc = method_dsc->next;
  1028. os_free(tmp_met_dsc->dbus_interface);
  1029. os_free(tmp_met_dsc->dbus_method);
  1030. for (i = 0; i < tmp_met_dsc->args_num; i++) {
  1031. os_free(tmp_met_dsc->args[i].name);
  1032. os_free(tmp_met_dsc->args[i].type);
  1033. }
  1034. if (tmp_met_dsc->argument_free_func)
  1035. tmp_met_dsc->argument_free_func(
  1036. tmp_met_dsc->handler_argument);
  1037. os_free(tmp_met_dsc);
  1038. }
  1039. /* free signals */
  1040. signal_dsc = obj_dsc->signals;
  1041. while (signal_dsc) {
  1042. tmp_sig_dsc = signal_dsc;
  1043. signal_dsc = signal_dsc->next;
  1044. os_free(tmp_sig_dsc->dbus_interface);
  1045. os_free(tmp_sig_dsc->dbus_signal);
  1046. for (i = 0; i < tmp_sig_dsc->args_num; i++) {
  1047. os_free(tmp_sig_dsc->args[i].name);
  1048. os_free(tmp_sig_dsc->args[i].type);
  1049. }
  1050. os_free(tmp_sig_dsc);
  1051. }
  1052. /* free properties */
  1053. property_dsc = obj_dsc->properties;
  1054. while (property_dsc) {
  1055. tmp_prop_dsc = property_dsc;
  1056. property_dsc = property_dsc->next;
  1057. os_free(tmp_prop_dsc->dbus_interface);
  1058. os_free(tmp_prop_dsc->dbus_property);
  1059. os_free(tmp_prop_dsc->type);
  1060. if (tmp_prop_dsc->user_data_free_func)
  1061. tmp_prop_dsc->user_data_free_func(
  1062. tmp_prop_dsc->user_data);
  1063. os_free(tmp_prop_dsc);
  1064. }
  1065. os_free(obj_dsc);
  1066. }
  1067. static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
  1068. {
  1069. free_dbus_object_desc(obj_dsc);
  1070. }
  1071. /**
  1072. * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
  1073. * @application_data: Pointer to application specific data structure
  1074. * @dbus_path: DBus path to interface object
  1075. * @dbus_service: DBus service name to register with
  1076. * @messageHandler: a pointer to function which will handle dbus messages
  1077. * coming on interface
  1078. * Returns: Pointer to dbus_new_ctrl_iface date or %NULL on failure
  1079. *
  1080. * Initialize the dbus control interface and start receiving commands from
  1081. * external programs over the bus.
  1082. */
  1083. struct ctrl_iface_dbus_new_priv *
  1084. wpa_dbus_ctrl_iface_init(void *application_data,
  1085. char *dbus_path, char *dbus_service,
  1086. struct wpa_dbus_object_desc *obj_desc)
  1087. {
  1088. struct ctrl_iface_dbus_new_priv *iface;
  1089. DBusError error;
  1090. int ret = -1;
  1091. DBusObjectPathVTable wpa_vtable = {
  1092. &free_dbus_object_desc_cb, &message_handler,
  1093. NULL, NULL, NULL, NULL
  1094. };
  1095. iface = os_zalloc(sizeof(struct ctrl_iface_dbus_new_priv));
  1096. if (iface == NULL)
  1097. return NULL;
  1098. iface->application_data = application_data;
  1099. /* Get a reference to the system bus */
  1100. dbus_error_init(&error);
  1101. iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
  1102. dbus_error_free(&error);
  1103. if (!iface->con) {
  1104. perror("dbus_bus_get[ctrl_iface_dbus]");
  1105. wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
  1106. goto fail;
  1107. }
  1108. obj_desc->connection = iface->con;
  1109. /* Tell dbus about our mainloop integration functions */
  1110. if (integrate_with_eloop(iface->con, iface))
  1111. goto fail;
  1112. /* Register the message handler for the global dbus interface */
  1113. if (!dbus_connection_register_object_path(iface->con,
  1114. dbus_path, &wpa_vtable,
  1115. obj_desc)) {
  1116. perror("dbus_connection_register_object_path[dbus]");
  1117. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  1118. "handler.");
  1119. goto fail;
  1120. }
  1121. /* Register our service with the message bus */
  1122. dbus_error_init(&error);
  1123. switch (dbus_bus_request_name(iface->con, dbus_service,
  1124. 0, &error)) {
  1125. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  1126. ret = 0;
  1127. break;
  1128. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  1129. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  1130. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  1131. perror("dbus_bus_request_name[dbus]");
  1132. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  1133. "already registered.");
  1134. break;
  1135. default:
  1136. perror("dbus_bus_request_name[dbus]");
  1137. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  1138. "%s %s.", error.name, error.message);
  1139. break;
  1140. }
  1141. dbus_error_free(&error);
  1142. if (ret != 0)
  1143. goto fail;
  1144. wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
  1145. /*
  1146. * Dispatch initial DBus messages that may have come in since the bus
  1147. * name was claimed above. Happens when clients are quick to notice the
  1148. * service.
  1149. *
  1150. * FIXME: is there a better solution to this problem?
  1151. */
  1152. eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
  1153. iface->con, NULL);
  1154. return iface;
  1155. fail:
  1156. wpa_dbus_ctrl_iface_deinit(iface);
  1157. return NULL;
  1158. }
  1159. /**
  1160. * wpa_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
  1161. * @iface: Pointer to dbus private data from wpa_dbus_ctrl_iface_init()
  1162. *
  1163. * Deinitialize the dbus control interface that was initialized with
  1164. * wpa_dbus_ctrl_iface_init().
  1165. */
  1166. void wpa_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_new_priv *iface)
  1167. {
  1168. if (iface == NULL)
  1169. return;
  1170. if (iface->con) {
  1171. eloop_cancel_timeout(dispatch_initial_dbus_messages,
  1172. iface->con, NULL);
  1173. dbus_connection_set_watch_functions(iface->con, NULL, NULL,
  1174. NULL, NULL, NULL);
  1175. dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
  1176. NULL, NULL, NULL);
  1177. dbus_connection_unref(iface->con);
  1178. }
  1179. os_memset(iface, 0, sizeof(struct ctrl_iface_dbus_new_priv));
  1180. os_free(iface);
  1181. }
  1182. /**
  1183. * wpa_dbus_register_object_per_iface - Register a new object with dbus
  1184. * @ctrl_iface: pointer to dbus private data
  1185. * @path: DBus path to object
  1186. * @ifname: interface name
  1187. * @obj_desc: description of object's methods, signals and properties
  1188. * Returns: 0 on success, -1 on error
  1189. *
  1190. * Registers a new interface with dbus and assigns it a dbus object path.
  1191. */
  1192. int wpa_dbus_register_object_per_iface(
  1193. struct ctrl_iface_dbus_new_priv *ctrl_iface,
  1194. const char *path, const char *ifname,
  1195. struct wpa_dbus_object_desc *obj_desc)
  1196. {
  1197. DBusConnection *con;
  1198. DBusObjectPathVTable vtable = {
  1199. &free_dbus_object_desc_cb, &message_handler,
  1200. NULL, NULL, NULL, NULL
  1201. };
  1202. /* Do nothing if the control interface is not turned on */
  1203. if (ctrl_iface == NULL)
  1204. return 0;
  1205. con = ctrl_iface->con;
  1206. obj_desc->connection = con;
  1207. /* Register the message handler for the interface functions */
  1208. if (!dbus_connection_register_object_path(con, path, &vtable,
  1209. obj_desc)) {
  1210. perror("wpa_dbus_register_iface [dbus]");
  1211. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  1212. "handler for interface %s\n"
  1213. "and object %s.", ifname, path);
  1214. return -1;
  1215. }
  1216. return 0;
  1217. }
  1218. /**
  1219. * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
  1220. * @ctrl_iface: Pointer to dbus private data
  1221. * @path: DBus path to object which will be unregistered
  1222. * Returns: Zero on success and -1 on failure
  1223. *
  1224. * Unregisters DBus object given by its path
  1225. */
  1226. int wpa_dbus_unregister_object_per_iface(
  1227. struct ctrl_iface_dbus_new_priv *ctrl_iface, const char *path)
  1228. {
  1229. DBusConnection *con = ctrl_iface->con;
  1230. if (!dbus_connection_unregister_object_path(con, path))
  1231. return -1;
  1232. return 0;
  1233. }
  1234. /**
  1235. * wpa_dbus_method_register - Registers DBus method for given object
  1236. * @obj_dsc: Object description for which a method will be registered
  1237. * @dbus_interface: DBus interface under which method will be registered
  1238. * @dbus_method: a name the method will be registered with
  1239. * @method_handler: a function which will be called to handle this method call
  1240. * @handler_argument: an additional argument passed to handler function
  1241. * @argument_free_func: function used to free handler argument
  1242. * @args: method arguments list
  1243. * Returns: Zero on success and -1 on failure
  1244. *
  1245. * Registers DBus method under given name and interface for the object.
  1246. * Method calls will be handled with given handling function and optional
  1247. * argument passed to this function. Handler function is required to return
  1248. * a DBusMessage pointer which will be response to method call. Any method
  1249. * call before being handled must have registered appropriate handler by
  1250. * using this function.
  1251. */
  1252. int wpa_dbus_method_register(struct wpa_dbus_object_desc *obj_dsc,
  1253. const char *dbus_interface,
  1254. const char *dbus_method,
  1255. WPADBusMethodHandler method_handler,
  1256. void *handler_argument,
  1257. WPADBusArgumentFreeFunction argument_free_func,
  1258. const struct wpa_dbus_argument args[])
  1259. {
  1260. struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  1261. struct wpa_dbus_method_desc *prev_desc;
  1262. int args_num = 0;
  1263. int interface_len, method_len, i, len, error;
  1264. prev_desc = NULL;
  1265. while (method_dsc) {
  1266. prev_desc = method_dsc;
  1267. method_dsc = method_dsc->next;
  1268. }
  1269. /* count args */
  1270. if (args) {
  1271. while (args[args_num].name && args[args_num].type)
  1272. args_num++;
  1273. }
  1274. method_dsc = os_zalloc(sizeof(struct wpa_dbus_method_desc) +
  1275. args_num * sizeof(struct wpa_dbus_argument));
  1276. if (!method_dsc)
  1277. goto err;
  1278. if (prev_desc == NULL)
  1279. obj_dsc->methods = method_dsc;
  1280. else
  1281. prev_desc->next = method_dsc;
  1282. /* copy interface name */
  1283. interface_len = os_strlen(dbus_interface) + 1;
  1284. method_dsc->dbus_interface = os_malloc(interface_len);
  1285. if (!method_dsc->dbus_interface)
  1286. goto err;
  1287. os_strncpy(method_dsc->dbus_interface, dbus_interface, interface_len);
  1288. /* copy method name */
  1289. method_len = os_strlen(dbus_method) + 1;
  1290. method_dsc->dbus_method = os_malloc(method_len);
  1291. if (!method_dsc->dbus_method)
  1292. goto err;
  1293. os_strncpy(method_dsc->dbus_method, dbus_method, method_len);
  1294. /* copy arguments */
  1295. error = 0;
  1296. method_dsc->args_num = args_num;
  1297. for (i = 0; i < args_num; i++) {
  1298. len = os_strlen(args[i].name) + 1;
  1299. method_dsc->args[i].name = os_malloc(len);
  1300. if (!method_dsc->args[i].name) {
  1301. error = 1;
  1302. continue;
  1303. }
  1304. os_strncpy(method_dsc->args[i].name, args[i].name, len);
  1305. len = os_strlen(args[i].type) + 1;
  1306. method_dsc->args[i].type = os_malloc(len);
  1307. if (!method_dsc->args[i].type) {
  1308. error = 1;
  1309. continue;
  1310. }
  1311. os_strncpy(method_dsc->args[i].type, args[i].type, len);
  1312. method_dsc->args[i].dir = args[i].dir;
  1313. }
  1314. if (error)
  1315. goto err;
  1316. method_dsc->method_handler = method_handler;
  1317. method_dsc->handler_argument = handler_argument;
  1318. method_dsc->argument_free_func = argument_free_func;
  1319. method_dsc->next = NULL;
  1320. return 0;
  1321. err:
  1322. wpa_printf(MSG_WARNING, "Failed to register dbus method %s in "
  1323. "interface %s", dbus_method, dbus_interface);
  1324. if (method_dsc) {
  1325. os_free(method_dsc->dbus_interface);
  1326. os_free(method_dsc->dbus_method);
  1327. for (i = 0; i < method_dsc->args_num; i++) {
  1328. os_free(method_dsc->args[i].name);
  1329. os_free(method_dsc->args[i].type);
  1330. }
  1331. if (prev_desc == NULL)
  1332. obj_dsc->methods = NULL;
  1333. else
  1334. prev_desc->next = NULL;
  1335. os_free(method_dsc);
  1336. }
  1337. return -1;
  1338. }
  1339. /**
  1340. * wpa_dbus_signal_register - Registers DBus signal for given object
  1341. * @obj_dsc: Object description for which a signal will be registered
  1342. * @dbus_interface: DBus interface under which signal will be registered
  1343. * @dbus_signal: a name the signal will be registered with
  1344. * @args: signal arguments list
  1345. * Returns: Zero on success and -1 on failure
  1346. *
  1347. * Registers DBus signal under given name and interface for the object.
  1348. * Signal registration is NOT required in order to send signals, but not
  1349. * registered signals will not be respected in introspection data
  1350. * therefore it is highly recommended to register every signal before
  1351. * using it.
  1352. */
  1353. int wpa_dbus_signal_register(struct wpa_dbus_object_desc *obj_dsc,
  1354. const char *dbus_interface,
  1355. const char *dbus_signal,
  1356. const struct wpa_dbus_argument args[])
  1357. {
  1358. struct wpa_dbus_signal_desc *signal_dsc = obj_dsc->signals;
  1359. struct wpa_dbus_signal_desc *prev_desc;
  1360. int args_num = 0;
  1361. int interface_len, signal_len, i, len, error = 0;
  1362. prev_desc = NULL;
  1363. while (signal_dsc) {
  1364. prev_desc = signal_dsc;
  1365. signal_dsc = signal_dsc->next;
  1366. }
  1367. /* count args */
  1368. if (args) {
  1369. while (args[args_num].name && args[args_num].type)
  1370. args_num++;
  1371. }
  1372. signal_dsc = os_zalloc(sizeof(struct wpa_dbus_signal_desc) +
  1373. args_num * sizeof(struct wpa_dbus_argument));
  1374. if (!signal_dsc)
  1375. goto err;
  1376. if (prev_desc == NULL)
  1377. obj_dsc->signals = signal_dsc;
  1378. else
  1379. prev_desc->next = signal_dsc;
  1380. /* copy interface name */
  1381. interface_len = strlen(dbus_interface) + 1;
  1382. signal_dsc->dbus_interface = os_malloc(interface_len);
  1383. if (!signal_dsc->dbus_interface)
  1384. goto err;
  1385. os_strncpy(signal_dsc->dbus_interface, dbus_interface, interface_len);
  1386. /* copy signal name */
  1387. signal_len = strlen(dbus_signal) + 1;
  1388. signal_dsc->dbus_signal = os_malloc(signal_len);
  1389. if (!signal_dsc->dbus_signal)
  1390. goto err;
  1391. os_strncpy(signal_dsc->dbus_signal, dbus_signal, signal_len);
  1392. /* copy arguments */
  1393. signal_dsc->args_num = args_num;
  1394. for (i = 0; i < args_num; i++) {
  1395. len = os_strlen(args[i].name) + 1;
  1396. signal_dsc->args[i].name = os_malloc(len);
  1397. if (!signal_dsc->args[i].name) {
  1398. error = 1;
  1399. continue;
  1400. }
  1401. os_strncpy(signal_dsc->args[i].name, args[i].name, len);
  1402. len = strlen(args[i].type) + 1;
  1403. signal_dsc->args[i].type = os_malloc(len);
  1404. if (!signal_dsc->args[i].type) {
  1405. error = 1;
  1406. continue;
  1407. }
  1408. os_strncpy(signal_dsc->args[i].type, args[i].type, len);
  1409. }
  1410. if (error)
  1411. goto err;
  1412. signal_dsc->next = NULL;
  1413. return 0;
  1414. err:
  1415. wpa_printf(MSG_WARNING, "Failed to register dbus signal %s in "
  1416. "interface %s", dbus_signal, dbus_interface);
  1417. if (signal_dsc) {
  1418. os_free(signal_dsc->dbus_interface);
  1419. os_free(signal_dsc->dbus_signal);
  1420. for (i = 0; i < signal_dsc->args_num; i++) {
  1421. os_free(signal_dsc->args[i].name);
  1422. os_free(signal_dsc->args[i].type);
  1423. }
  1424. if (prev_desc == NULL)
  1425. obj_dsc->signals = NULL;
  1426. else
  1427. prev_desc->next = NULL;
  1428. os_free(signal_dsc);
  1429. }
  1430. return -1;
  1431. }
  1432. /**
  1433. * wpa_dbus_property_register - Registers DBus property for given object
  1434. * @obj_dsc: Object description for which a property will be registered
  1435. * @dbus_interface: DBus interface under which method will be registered
  1436. * @dbus_property: a name the property will be registered with
  1437. * @type: a property type signature in form of DBus type description
  1438. * @getter: a function called in order to get property value
  1439. * @setter: a function called in order to set property value
  1440. * @user_data: additional argument passed to setter or getter
  1441. * @user_data_free_func: function used to free additional argument
  1442. * @access: property access permissions specifier (R, W or RW)
  1443. * Returns: Zero on success and -1 on failure
  1444. *
  1445. * Registers DBus property under given name and interface for the object.
  1446. * Property are set with giver setter function and get with getter.
  1447. * Additional argument is passed to getter or setter. Getter or setter
  1448. * are required to return DBusMessage which is response to Set/Get method
  1449. * calls. Every property must be registered by this function before being
  1450. * used.
  1451. */
  1452. int wpa_dbus_property_register(struct wpa_dbus_object_desc *obj_dsc,
  1453. const char *dbus_interface,
  1454. const char *dbus_property,
  1455. const char *type,
  1456. WPADBusPropertyAccessor getter,
  1457. WPADBusPropertyAccessor setter,
  1458. void *user_data,
  1459. WPADBusArgumentFreeFunction user_data_free_func,
  1460. enum dbus_prop_access _access)
  1461. {
  1462. struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties;
  1463. struct wpa_dbus_property_desc *prev_desc;
  1464. int interface_len, property_len, type_len;
  1465. prev_desc = NULL;
  1466. while (property_dsc) {
  1467. prev_desc = property_dsc;
  1468. property_dsc = property_dsc->next;
  1469. }
  1470. property_dsc = os_zalloc(sizeof(struct wpa_dbus_property_desc));
  1471. if (!property_dsc)
  1472. goto err;
  1473. if (prev_desc == NULL)
  1474. obj_dsc->properties = property_dsc;
  1475. else
  1476. prev_desc->next = property_dsc;
  1477. /* copy interface name */
  1478. interface_len = os_strlen(dbus_interface) + 1;
  1479. property_dsc->dbus_interface = os_malloc(interface_len);
  1480. if (!property_dsc->dbus_interface)
  1481. goto err;
  1482. os_strncpy(property_dsc->dbus_interface, dbus_interface,
  1483. interface_len);
  1484. /* copy property name */
  1485. property_len = os_strlen(dbus_property) + 1;
  1486. property_dsc->dbus_property = os_malloc(property_len);
  1487. if (!property_dsc->dbus_property)
  1488. goto err;
  1489. os_strncpy(property_dsc->dbus_property, dbus_property, property_len);
  1490. /* copy property type */
  1491. type_len = os_strlen(type) + 1;
  1492. property_dsc->type = os_malloc(type_len);
  1493. if (!property_dsc->type)
  1494. goto err;
  1495. os_strncpy(property_dsc->type, type, type_len);
  1496. property_dsc->getter = getter;
  1497. property_dsc->setter = setter;
  1498. property_dsc->user_data = user_data;
  1499. property_dsc->user_data_free_func = user_data_free_func;
  1500. property_dsc->access = _access;
  1501. property_dsc->next = NULL;
  1502. return 0;
  1503. err:
  1504. wpa_printf(MSG_WARNING, "Failed to register dbus property %s in "
  1505. "interface %s", dbus_property, dbus_interface);
  1506. if (property_dsc) {
  1507. os_free(property_dsc->dbus_interface);
  1508. os_free(property_dsc->dbus_property);
  1509. os_free(property_dsc->type);
  1510. if (prev_desc == NULL)
  1511. obj_dsc->properties = NULL;
  1512. else
  1513. prev_desc->next = NULL;
  1514. os_free(property_dsc);
  1515. }
  1516. return -1;
  1517. }
  1518. /**
  1519. * wpas_dbus_signal_network_added - Send a property changed signal
  1520. * @iface: dbus priv struct
  1521. * @property_getter: propperty getter used to fetch new property value
  1522. * @getter_arg: argument passed to property getter
  1523. * @path: path to object which property has changed
  1524. * @interface_name: signal and property interface
  1525. * @property_name: name of property which has changed
  1526. *
  1527. * Notify listeners about changing value of some property. Signal
  1528. * contains property name and its value fetched using given property
  1529. * getter.
  1530. */
  1531. void wpa_dbus_signal_property_changed(struct ctrl_iface_dbus_new_priv *iface,
  1532. WPADBusPropertyAccessor property_getter,
  1533. void *getter_arg,
  1534. const char *path,
  1535. const char *interface_name,
  1536. const char *property_name)
  1537. {
  1538. DBusConnection *connection;
  1539. DBusMessage *_signal, *getter_reply;
  1540. DBusMessageIter prop_iter, signal_iter, dict_iter, entry_iter;
  1541. if (!iface)
  1542. return;
  1543. connection = iface->con;
  1544. if (!property_getter) {
  1545. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1546. "[dbus]: property getter not specified");
  1547. return;
  1548. }
  1549. if (!path || !interface_name || !property_name) {
  1550. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1551. "[dbus]: path interface of property not specified");
  1552. return;
  1553. }
  1554. getter_reply = property_getter(NULL, getter_arg);
  1555. if (!getter_reply ||
  1556. dbus_message_get_type(getter_reply) == DBUS_MESSAGE_TYPE_ERROR) {
  1557. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1558. "[dbus]: cannot get new value of property %s",
  1559. property_name);
  1560. return;
  1561. }
  1562. _signal = dbus_message_new_signal(path, interface_name,
  1563. "PropertiesChanged");
  1564. if (!_signal) {
  1565. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1566. "[dbus]: cannot allocate signal");
  1567. dbus_message_unref(getter_reply);
  1568. return;
  1569. }
  1570. dbus_message_iter_init(getter_reply, &prop_iter);
  1571. dbus_message_iter_init_append(_signal, &signal_iter);
  1572. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  1573. "{sv}", &dict_iter)) {
  1574. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1575. "[dbus]: out of memory. cannot open dictionary");
  1576. goto err;
  1577. }
  1578. if (!dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY,
  1579. NULL, &entry_iter)) {
  1580. wpa_printf(MSG_ERROR, "iwpa_dbus_signal_property_changed"
  1581. "[dbus]: out of memory. cannot open dictionary "
  1582. "element");
  1583. goto err;
  1584. }
  1585. if (!dbus_message_iter_append_basic(&entry_iter, DBUS_TYPE_STRING,
  1586. &property_name)) {
  1587. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1588. "[dbus]: out of memory. cannot open add property "
  1589. "name");
  1590. goto err;
  1591. }
  1592. recursive_iter_copy(&prop_iter, &entry_iter);
  1593. if (!dbus_message_iter_close_container(&dict_iter, &entry_iter)) {
  1594. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1595. "[dbus]: out of memory. cannot close dictionary "
  1596. "element");
  1597. goto err;
  1598. }
  1599. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
  1600. wpa_printf(MSG_ERROR, "wpa_dbus_signal_property_changed"
  1601. "[dbus]: out of memory. cannot close dictionary");
  1602. goto err;
  1603. }
  1604. dbus_connection_send(connection, _signal, NULL);
  1605. err:
  1606. dbus_message_unref(getter_reply);
  1607. dbus_message_unref(_signal);
  1608. }