ctrl_iface_dbus_new_helpers.c 53 KB

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