confdb.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /*
  2. * Copyright (c) 2008-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <sys/types.h>
  36. #include <unistd.h>
  37. #include <fcntl.h>
  38. #include <stdlib.h>
  39. #include <errno.h>
  40. #include <unistd.h>
  41. #include <poll.h>
  42. #include <corosync/corotypes.h>
  43. #include <corosync/coroipc_types.h>
  44. #include <corosync/corodefs.h>
  45. #include <corosync/cfg.h>
  46. #include <corosync/list.h>
  47. #include <corosync/mar_gen.h>
  48. #include <corosync/ipc_confdb.h>
  49. #include <corosync/lcr/lcr_comp.h>
  50. #include <corosync/engine/logsys.h>
  51. #include <corosync/engine/coroapi.h>
  52. #include <corosync/totem/coropoll.h>
  53. LOGSYS_DECLARE_SUBSYS ("CONFDB");
  54. static hdb_handle_t *
  55. m2h (mar_uint64_t *m)
  56. {
  57. /* FIXME enable the following when/if we use gnulib:
  58. (it's a compile-time assertion; i.e., zero run-time cost)
  59. verify (sizeof (*m) == sizeof (hdb_handle_t)); */
  60. return (void *) m;
  61. }
  62. static struct corosync_api_v1 *api;
  63. static int notify_pipe[2];
  64. struct confdb_ipc_message_holder {
  65. void *conn;
  66. size_t mlen;
  67. struct list_head list;
  68. char msg[];
  69. };
  70. DECLARE_LIST_INIT(confdb_ipc_message_holder_list_head);
  71. pthread_mutex_t confdb_ipc_message_holder_list_mutex =
  72. PTHREAD_MUTEX_INITIALIZER;
  73. static int confdb_exec_init_fn (
  74. struct corosync_api_v1 *corosync_api);
  75. static int confdb_exec_exit_fn(void);
  76. static int fd_set_nonblocking(int fd);
  77. static int objdb_notify_dispatch(hdb_handle_t handle,
  78. int fd, int revents, void *data);
  79. static int confdb_lib_init_fn (void *conn);
  80. static int confdb_lib_exit_fn (void *conn);
  81. static void message_handler_req_lib_confdb_object_create (void *conn,
  82. const void *message);
  83. static void message_handler_req_lib_confdb_object_destroy (void *conn,
  84. const void *message);
  85. static void message_handler_req_lib_confdb_object_find_destroy (void *conn,
  86. const void *message);
  87. static void message_handler_req_lib_confdb_key_create (void *conn,
  88. const void *message);
  89. static void message_handler_req_lib_confdb_key_create_typed (void *conn,
  90. const void *message);
  91. static void message_handler_req_lib_confdb_key_create_typed2 (void *conn,
  92. const void *message);
  93. static void message_handler_req_lib_confdb_key_get (void *conn,
  94. const void *message);
  95. static void message_handler_req_lib_confdb_key_get_typed (void *conn,
  96. const void *message);
  97. static void message_handler_req_lib_confdb_key_get_typed2 (void *conn,
  98. const void *message);
  99. static void message_handler_req_lib_confdb_key_replace (void *conn,
  100. const void *message);
  101. static void message_handler_req_lib_confdb_key_replace2 (void *conn,
  102. const void *message);
  103. static void message_handler_req_lib_confdb_key_delete (void *conn,
  104. const void *message);
  105. static void message_handler_req_lib_confdb_key_iter (void *conn,
  106. const void *message);
  107. static void message_handler_req_lib_confdb_key_iter_typed (void *conn,
  108. const void *message);
  109. static void message_handler_req_lib_confdb_key_iter_typed2 (void *conn,
  110. const void *message);
  111. static void message_handler_req_lib_confdb_key_increment (void *conn,
  112. const void *message);
  113. static void message_handler_req_lib_confdb_key_decrement (void *conn,
  114. const void *message);
  115. static void message_handler_req_lib_confdb_object_iter (void *conn,
  116. const void *message);
  117. static void message_handler_req_lib_confdb_object_find (void *conn,
  118. const void *message);
  119. static void message_handler_req_lib_confdb_object_parent_get (void *conn,
  120. const void *message);
  121. static void message_handler_req_lib_confdb_object_name_get (void *conn,
  122. const void *message);
  123. static void message_handler_req_lib_confdb_write (void *conn,
  124. const void *message);
  125. static void message_handler_req_lib_confdb_reload (void *conn,
  126. const void *message);
  127. static void message_handler_req_lib_confdb_track_start (void *conn,
  128. const void *message);
  129. static void message_handler_req_lib_confdb_track_stop (void *conn,
  130. const void *message);
  131. static void confdb_notify_lib_of_key_change(
  132. object_change_type_t change_type,
  133. hdb_handle_t parent_object_handle,
  134. hdb_handle_t object_handle,
  135. const void *object_name_pt, size_t object_name_len,
  136. const void *key_name_pt, size_t key_name_len,
  137. const void *key_value_pt, size_t key_value_len,
  138. void *priv_data_pt);
  139. static void confdb_notify_lib_of_new_object(
  140. hdb_handle_t parent_object_handle,
  141. hdb_handle_t object_handle,
  142. const void *name_pt, size_t name_len,
  143. void *priv_data_pt);
  144. static void confdb_notify_lib_of_destroyed_object(
  145. hdb_handle_t parent_object_handle,
  146. const void *name_pt, size_t name_len,
  147. void *priv_data_pt);
  148. static void confdb_notify_lib_of_reload(
  149. objdb_reload_notify_type_t notify_type,
  150. int flush,
  151. void *priv_data_pt);
  152. /*
  153. * Library Handler Definition
  154. */
  155. static struct corosync_lib_handler confdb_lib_engine[] =
  156. {
  157. { /* 0 */
  158. .lib_handler_fn = message_handler_req_lib_confdb_object_create,
  159. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  160. },
  161. { /* 1 */
  162. .lib_handler_fn = message_handler_req_lib_confdb_object_destroy,
  163. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  164. },
  165. { /* 2 */
  166. .lib_handler_fn = message_handler_req_lib_confdb_object_find,
  167. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  168. },
  169. { /* 3 */
  170. .lib_handler_fn = message_handler_req_lib_confdb_key_create,
  171. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  172. },
  173. { /* 4 */
  174. .lib_handler_fn = message_handler_req_lib_confdb_key_get,
  175. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  176. },
  177. { /* 5 */
  178. .lib_handler_fn = message_handler_req_lib_confdb_key_replace,
  179. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  180. },
  181. { /* 6 */
  182. .lib_handler_fn = message_handler_req_lib_confdb_key_delete,
  183. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  184. },
  185. { /* 7 */
  186. .lib_handler_fn = message_handler_req_lib_confdb_object_iter,
  187. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  188. },
  189. { /* 8 */
  190. .lib_handler_fn = message_handler_req_lib_confdb_object_parent_get,
  191. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  192. },
  193. { /* 9 */
  194. .lib_handler_fn = message_handler_req_lib_confdb_key_iter,
  195. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  196. },
  197. { /* 10 */
  198. .lib_handler_fn = message_handler_req_lib_confdb_track_start,
  199. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  200. },
  201. { /* 11 */
  202. .lib_handler_fn = message_handler_req_lib_confdb_track_stop,
  203. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  204. },
  205. { /* 12 */
  206. .lib_handler_fn = message_handler_req_lib_confdb_write,
  207. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  208. },
  209. { /* 13 */
  210. .lib_handler_fn = message_handler_req_lib_confdb_reload,
  211. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  212. },
  213. { /* 14 */
  214. .lib_handler_fn = message_handler_req_lib_confdb_object_find_destroy,
  215. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  216. },
  217. { /* 15 */
  218. .lib_handler_fn = message_handler_req_lib_confdb_key_increment,
  219. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  220. },
  221. { /* 16 */
  222. .lib_handler_fn = message_handler_req_lib_confdb_key_decrement,
  223. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  224. },
  225. { /* 17 */
  226. .lib_handler_fn = message_handler_req_lib_confdb_key_create_typed,
  227. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  228. },
  229. { /* 18 */
  230. .lib_handler_fn = message_handler_req_lib_confdb_key_get_typed,
  231. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  232. },
  233. { /* 19 */
  234. .lib_handler_fn = message_handler_req_lib_confdb_key_iter_typed,
  235. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  236. },
  237. { /* 20 */
  238. .lib_handler_fn = message_handler_req_lib_confdb_object_name_get,
  239. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  240. },
  241. { /* 21 */
  242. .lib_handler_fn = message_handler_req_lib_confdb_key_iter_typed2,
  243. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  244. },
  245. { /* 22 */
  246. .lib_handler_fn = message_handler_req_lib_confdb_key_replace2,
  247. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  248. },
  249. { /* 23 */
  250. .lib_handler_fn = message_handler_req_lib_confdb_key_get_typed2,
  251. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  252. },
  253. { /* 24 */
  254. .lib_handler_fn = message_handler_req_lib_confdb_key_create_typed2,
  255. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  256. },
  257. };
  258. struct corosync_service_engine confdb_service_engine = {
  259. .name = "corosync cluster config database access v1.01",
  260. .id = CONFDB_SERVICE,
  261. .priority = 1,
  262. .private_data_size = 0,
  263. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  264. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  265. .lib_init_fn = confdb_lib_init_fn,
  266. .lib_exit_fn = confdb_lib_exit_fn,
  267. .lib_engine = confdb_lib_engine,
  268. .lib_engine_count = sizeof (confdb_lib_engine) / sizeof (struct corosync_lib_handler),
  269. .exec_init_fn = confdb_exec_init_fn,
  270. .exec_exit_fn = confdb_exec_exit_fn,
  271. };
  272. /*
  273. * Dynamic loader definition
  274. */
  275. static struct corosync_service_engine *confdb_get_service_engine_ver0 (void);
  276. static struct corosync_service_engine_iface_ver0 confdb_service_engine_iface = {
  277. .corosync_get_service_engine_ver0 = confdb_get_service_engine_ver0
  278. };
  279. static struct lcr_iface corosync_confdb_ver0[1] = {
  280. {
  281. .name = "corosync_confdb",
  282. .version = 0,
  283. .versions_replace = 0,
  284. .versions_replace_count = 0,
  285. .dependencies = 0,
  286. .dependency_count = 0,
  287. .constructor = NULL,
  288. .destructor = NULL,
  289. .interfaces = NULL
  290. }
  291. };
  292. static struct lcr_comp confdb_comp_ver0 = {
  293. .iface_count = 1,
  294. .ifaces = corosync_confdb_ver0
  295. };
  296. static struct corosync_service_engine *confdb_get_service_engine_ver0 (void)
  297. {
  298. return (&confdb_service_engine);
  299. }
  300. #ifdef COROSYNC_SOLARIS
  301. void corosync_lcr_component_register (void);
  302. void corosync_lcr_component_register (void) {
  303. #else
  304. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  305. #endif
  306. lcr_interfaces_set (&corosync_confdb_ver0[0], &confdb_service_engine_iface);
  307. lcr_component_register (&confdb_comp_ver0);
  308. }
  309. static void free_confdb_ipc_message_holder_list(void)
  310. {
  311. struct confdb_ipc_message_holder *holder;
  312. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  313. while (!list_empty (&confdb_ipc_message_holder_list_head)) {
  314. holder = list_entry (confdb_ipc_message_holder_list_head.next,
  315. struct confdb_ipc_message_holder, list);
  316. list_del (&holder->list);
  317. api->ipc_refcnt_dec(holder->conn);
  318. free(holder);
  319. }
  320. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  321. }
  322. static int confdb_exec_exit_fn(void)
  323. {
  324. api->poll_dispatch_delete(api->poll_handle_get(), notify_pipe[0]);
  325. close(notify_pipe[0]);
  326. close(notify_pipe[1]);
  327. free_confdb_ipc_message_holder_list();
  328. return 0;
  329. }
  330. static int confdb_exec_init_fn (
  331. struct corosync_api_v1 *corosync_api)
  332. {
  333. int i;
  334. #ifdef COROSYNC_SOLARIS
  335. logsys_subsys_init();
  336. #endif
  337. api = corosync_api;
  338. if (pipe(notify_pipe) != 0) {
  339. return -1;
  340. }
  341. for (i = 0; i < 2; i++) {
  342. if (fd_set_nonblocking (notify_pipe[i]) == -1) {
  343. return -1;
  344. }
  345. }
  346. return api->poll_dispatch_add(api->poll_handle_get(), notify_pipe[0],
  347. POLLIN, NULL, objdb_notify_dispatch);
  348. }
  349. static int confdb_lib_init_fn (void *conn)
  350. {
  351. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p\n", conn);
  352. return (0);
  353. }
  354. static int confdb_lib_exit_fn (void *conn)
  355. {
  356. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p\n", conn);
  357. /* cleanup the object trackers for this client. */
  358. api->object_track_stop(confdb_notify_lib_of_key_change,
  359. confdb_notify_lib_of_new_object,
  360. confdb_notify_lib_of_destroyed_object,
  361. confdb_notify_lib_of_reload,
  362. conn);
  363. return (0);
  364. }
  365. static int fd_set_nonblocking(int fd)
  366. {
  367. int flags;
  368. int res;
  369. flags = fcntl (fd, F_GETFL);
  370. if (flags == -1) {
  371. return -1;
  372. }
  373. flags |= O_NONBLOCK;
  374. res = fcntl (fd, F_SETFL, flags);
  375. return res;
  376. }
  377. static void message_handler_req_lib_confdb_object_create (void *conn,
  378. const void *message)
  379. {
  380. const struct req_lib_confdb_object_create *req_lib_confdb_object_create
  381. = message;
  382. struct res_lib_confdb_object_create res_lib_confdb_object_create;
  383. hdb_handle_t object_handle;
  384. int ret = CS_OK;
  385. if (api->object_create(req_lib_confdb_object_create->parent_object_handle,
  386. &object_handle,
  387. req_lib_confdb_object_create->object_name.value,
  388. req_lib_confdb_object_create->object_name.length))
  389. ret = CS_ERR_ACCESS;
  390. res_lib_confdb_object_create.object_handle = object_handle;
  391. res_lib_confdb_object_create.header.size = sizeof(res_lib_confdb_object_create);
  392. res_lib_confdb_object_create.header.id = MESSAGE_RES_CONFDB_OBJECT_CREATE;
  393. res_lib_confdb_object_create.header.error = ret;
  394. api->ipc_response_send(conn, &res_lib_confdb_object_create, sizeof(res_lib_confdb_object_create));
  395. }
  396. static void message_handler_req_lib_confdb_object_destroy (void *conn,
  397. const void *message)
  398. {
  399. const struct req_lib_confdb_object_destroy *req_lib_confdb_object_destroy
  400. = message;
  401. coroipc_response_header_t res;
  402. int ret = CS_OK;
  403. if (api->object_destroy(req_lib_confdb_object_destroy->object_handle))
  404. ret = CS_ERR_ACCESS;
  405. res.size = sizeof(res);
  406. res.id = MESSAGE_RES_CONFDB_OBJECT_DESTROY;
  407. res.error = ret;
  408. api->ipc_response_send(conn, &res, sizeof(res));
  409. }
  410. static void message_handler_req_lib_confdb_object_find_destroy (void *conn,
  411. const void *message)
  412. {
  413. const struct req_lib_confdb_object_find_destroy
  414. *req_lib_confdb_object_find_destroy = message;
  415. coroipc_response_header_t res;
  416. int ret = CS_OK;
  417. if (api->object_find_destroy(req_lib_confdb_object_find_destroy->find_handle))
  418. ret = CS_ERR_ACCESS;
  419. res.size = sizeof(res);
  420. res.id = MESSAGE_RES_CONFDB_OBJECT_FIND_DESTROY;
  421. res.error = ret;
  422. api->ipc_response_send(conn, &res, sizeof(res));
  423. }
  424. static void message_handler_req_lib_confdb_key_create (void *conn,
  425. const void *message)
  426. {
  427. const struct req_lib_confdb_key_create *req_lib_confdb_key_create
  428. = message;
  429. coroipc_response_header_t res;
  430. int ret = CS_OK;
  431. if (api->object_key_create(req_lib_confdb_key_create->object_handle,
  432. req_lib_confdb_key_create->key_name.value,
  433. req_lib_confdb_key_create->key_name.length,
  434. req_lib_confdb_key_create->value.value,
  435. req_lib_confdb_key_create->value.length))
  436. ret = CS_ERR_ACCESS;
  437. res.size = sizeof(res);
  438. res.id = MESSAGE_RES_CONFDB_KEY_CREATE;
  439. res.error = ret;
  440. api->ipc_response_send(conn, &res, sizeof(res));
  441. }
  442. static void message_handler_req_lib_confdb_key_create_typed (void *conn,
  443. const void *message)
  444. {
  445. const struct req_lib_confdb_key_create_typed *req_lib_confdb_key_create
  446. = message;
  447. coroipc_response_header_t res;
  448. int ret = CS_OK;
  449. if (api->object_key_create_typed(req_lib_confdb_key_create->object_handle,
  450. (char*)req_lib_confdb_key_create->key_name.value,
  451. req_lib_confdb_key_create->value.value,
  452. req_lib_confdb_key_create->value.length,
  453. req_lib_confdb_key_create->type))
  454. ret = CS_ERR_ACCESS;
  455. res.size = sizeof(res);
  456. res.id = MESSAGE_RES_CONFDB_KEY_CREATE;
  457. res.error = ret;
  458. api->ipc_response_send(conn, &res, sizeof(res));
  459. }
  460. static void message_handler_req_lib_confdb_key_get (void *conn,
  461. const void *message)
  462. {
  463. const struct req_lib_confdb_key_get *req_lib_confdb_key_get = message;
  464. struct res_lib_confdb_key_get res_lib_confdb_key_get;
  465. size_t value_len;
  466. void *value;
  467. int ret = CS_OK;
  468. if (api->object_key_get(req_lib_confdb_key_get->parent_object_handle,
  469. req_lib_confdb_key_get->key_name.value,
  470. req_lib_confdb_key_get->key_name.length,
  471. &value,
  472. &value_len))
  473. ret = CS_ERR_ACCESS;
  474. else {
  475. if (value_len > CS_MAX_NAME_LENGTH) {
  476. ret = CS_ERR_TOO_BIG;
  477. } else {
  478. memcpy(res_lib_confdb_key_get.value.value, value, value_len);
  479. }
  480. res_lib_confdb_key_get.value.length = value_len;
  481. }
  482. res_lib_confdb_key_get.header.size = sizeof(res_lib_confdb_key_get);
  483. res_lib_confdb_key_get.header.id = MESSAGE_RES_CONFDB_KEY_GET;
  484. res_lib_confdb_key_get.header.error = ret;
  485. api->ipc_response_send(conn, &res_lib_confdb_key_get, sizeof(res_lib_confdb_key_get));
  486. }
  487. static void message_handler_req_lib_confdb_key_get_typed (void *conn,
  488. const void *message)
  489. {
  490. const struct req_lib_confdb_key_get *req_lib_confdb_key_get = message;
  491. struct res_lib_confdb_key_get_typed res_lib_confdb_key_get;
  492. size_t value_len;
  493. void *value;
  494. int ret = CS_OK;
  495. objdb_value_types_t type;
  496. char * key_name = (char*)req_lib_confdb_key_get->key_name.value;
  497. key_name[req_lib_confdb_key_get->key_name.length] = '\0';
  498. if (api->object_key_get_typed(req_lib_confdb_key_get->parent_object_handle,
  499. key_name,
  500. &value,
  501. &value_len, &type))
  502. ret = CS_ERR_ACCESS;
  503. else {
  504. if (value_len > CS_MAX_NAME_LENGTH) {
  505. ret = CS_ERR_TOO_BIG;
  506. } else {
  507. memcpy(res_lib_confdb_key_get.value.value, value, value_len);
  508. }
  509. res_lib_confdb_key_get.value.length = value_len;
  510. res_lib_confdb_key_get.type = type;
  511. }
  512. res_lib_confdb_key_get.header.size = sizeof(res_lib_confdb_key_get);
  513. res_lib_confdb_key_get.header.id = MESSAGE_RES_CONFDB_KEY_GET_TYPED;
  514. res_lib_confdb_key_get.header.error = ret;
  515. api->ipc_response_send(conn, &res_lib_confdb_key_get, sizeof(res_lib_confdb_key_get));
  516. }
  517. static void message_handler_req_lib_confdb_key_get_typed2 (void *conn,
  518. const void *message)
  519. {
  520. const struct req_lib_confdb_key_get *req_lib_confdb_key_get = message;
  521. struct res_lib_confdb_key_get_typed2 res_lib_confdb_key_get;
  522. struct res_lib_confdb_key_get_typed2 *res = &res_lib_confdb_key_get;
  523. size_t value_len;
  524. void *value;
  525. int ret = CS_OK;
  526. objdb_value_types_t type;
  527. char * key_name = (char*)req_lib_confdb_key_get->key_name.value;
  528. key_name[req_lib_confdb_key_get->key_name.length] = '\0';
  529. if (api->object_key_get_typed(req_lib_confdb_key_get->parent_object_handle,
  530. key_name,
  531. &value,
  532. &value_len, &type)) {
  533. ret = CS_ERR_ACCESS;
  534. res->header.size = sizeof(res_lib_confdb_key_get);
  535. }
  536. else {
  537. res = alloca(sizeof(struct res_lib_confdb_key_get_typed2) + value_len);
  538. memcpy(&res->value, value, value_len);
  539. res->value_length = value_len;
  540. res->type = type;
  541. res->header.size = sizeof(struct res_lib_confdb_key_get_typed2)+value_len;
  542. res->header.error = ret;
  543. }
  544. res->header.id = MESSAGE_RES_CONFDB_KEY_GET_TYPED2;
  545. res->header.error = ret;
  546. api->ipc_response_send(conn, res, res->header.size);
  547. }
  548. static void message_handler_req_lib_confdb_key_increment (void *conn,
  549. const void *message)
  550. {
  551. const struct req_lib_confdb_key_get *req_lib_confdb_key_get = message;
  552. struct res_lib_confdb_key_incdec res_lib_confdb_key_incdec;
  553. int ret = CS_OK;
  554. if (api->object_key_increment(req_lib_confdb_key_get->parent_object_handle,
  555. req_lib_confdb_key_get->key_name.value,
  556. req_lib_confdb_key_get->key_name.length,
  557. &res_lib_confdb_key_incdec.value))
  558. ret = CS_ERR_ACCESS;
  559. res_lib_confdb_key_incdec.header.size = sizeof(res_lib_confdb_key_incdec);
  560. res_lib_confdb_key_incdec.header.id = MESSAGE_RES_CONFDB_KEY_INCREMENT;
  561. res_lib_confdb_key_incdec.header.error = ret;
  562. api->ipc_response_send(conn, &res_lib_confdb_key_incdec, sizeof(res_lib_confdb_key_incdec));
  563. }
  564. static void message_handler_req_lib_confdb_key_decrement (void *conn,
  565. const void *message)
  566. {
  567. const struct req_lib_confdb_key_get *req_lib_confdb_key_get = message;
  568. struct res_lib_confdb_key_incdec res_lib_confdb_key_incdec;
  569. int ret = CS_OK;
  570. if (api->object_key_decrement(req_lib_confdb_key_get->parent_object_handle,
  571. req_lib_confdb_key_get->key_name.value,
  572. req_lib_confdb_key_get->key_name.length,
  573. &res_lib_confdb_key_incdec.value))
  574. ret = CS_ERR_ACCESS;
  575. res_lib_confdb_key_incdec.header.size = sizeof(res_lib_confdb_key_incdec);
  576. res_lib_confdb_key_incdec.header.id = MESSAGE_RES_CONFDB_KEY_DECREMENT;
  577. res_lib_confdb_key_incdec.header.error = ret;
  578. api->ipc_response_send(conn, &res_lib_confdb_key_incdec, sizeof(res_lib_confdb_key_incdec));
  579. }
  580. static void message_handler_req_lib_confdb_key_replace (void *conn,
  581. const void *message)
  582. {
  583. const struct req_lib_confdb_key_replace *req_lib_confdb_key_replace
  584. = message;
  585. coroipc_response_header_t res;
  586. int ret = CS_OK;
  587. if (api->object_key_replace(req_lib_confdb_key_replace->object_handle,
  588. req_lib_confdb_key_replace->key_name.value,
  589. req_lib_confdb_key_replace->key_name.length,
  590. req_lib_confdb_key_replace->new_value.value,
  591. req_lib_confdb_key_replace->new_value.length))
  592. ret = CS_ERR_ACCESS;
  593. res.size = sizeof(res);
  594. res.id = MESSAGE_RES_CONFDB_KEY_REPLACE;
  595. res.error = ret;
  596. api->ipc_response_send(conn, &res, sizeof(res));
  597. }
  598. static void message_handler_req_lib_confdb_key_delete (void *conn,
  599. const void *message)
  600. {
  601. const struct req_lib_confdb_key_delete *req_lib_confdb_key_delete
  602. = message;
  603. coroipc_response_header_t res;
  604. int ret = CS_OK;
  605. if (api->object_key_delete(req_lib_confdb_key_delete->object_handle,
  606. req_lib_confdb_key_delete->key_name.value,
  607. req_lib_confdb_key_delete->key_name.length))
  608. ret = CS_ERR_ACCESS;
  609. res.size = sizeof(res);
  610. res.id = MESSAGE_RES_CONFDB_KEY_DELETE;
  611. res.error = ret;
  612. api->ipc_response_send(conn, &res, sizeof(res));
  613. }
  614. static void message_handler_req_lib_confdb_object_parent_get (void *conn,
  615. const void *message)
  616. {
  617. const struct req_lib_confdb_object_parent_get
  618. *req_lib_confdb_object_parent_get = message;
  619. struct res_lib_confdb_object_parent_get res_lib_confdb_object_parent_get;
  620. hdb_handle_t object_handle;
  621. int ret = CS_OK;
  622. if (api->object_parent_get(req_lib_confdb_object_parent_get->object_handle,
  623. &object_handle))
  624. ret = CS_ERR_ACCESS;
  625. res_lib_confdb_object_parent_get.parent_object_handle = object_handle;
  626. res_lib_confdb_object_parent_get.header.size = sizeof(res_lib_confdb_object_parent_get);
  627. res_lib_confdb_object_parent_get.header.id = MESSAGE_RES_CONFDB_OBJECT_CREATE;
  628. res_lib_confdb_object_parent_get.header.error = ret;
  629. api->ipc_response_send(conn, &res_lib_confdb_object_parent_get, sizeof(res_lib_confdb_object_parent_get));
  630. }
  631. static void message_handler_req_lib_confdb_object_name_get (void *conn,
  632. const void *message)
  633. {
  634. const struct req_lib_confdb_object_name_get *request = message;
  635. struct res_lib_confdb_object_name_get response;
  636. int ret = CS_OK;
  637. char object_name[CS_MAX_NAME_LENGTH];
  638. size_t object_name_len;
  639. if (api->object_name_get(request->object_handle,
  640. object_name, &object_name_len)) {
  641. ret = CS_ERR_ACCESS;
  642. }
  643. response.object_name.length = object_name_len;
  644. strncpy((char*)response.object_name.value, object_name, CS_MAX_NAME_LENGTH);
  645. response.object_name.value[CS_MAX_NAME_LENGTH-1] = '\0';
  646. response.header.size = sizeof(response);
  647. response.header.id = MESSAGE_RES_CONFDB_OBJECT_NAME_GET;
  648. response.header.error = ret;
  649. api->ipc_response_send(conn, &response, sizeof(response));
  650. }
  651. static void message_handler_req_lib_confdb_key_iter (void *conn,
  652. const void *message)
  653. {
  654. const struct req_lib_confdb_key_iter *req_lib_confdb_key_iter = message;
  655. struct res_lib_confdb_key_iter res_lib_confdb_key_iter;
  656. void *key_name;
  657. size_t key_name_len;
  658. void *value;
  659. size_t value_len;
  660. int ret = CS_OK;
  661. if (api->object_key_iter_from(req_lib_confdb_key_iter->parent_object_handle,
  662. req_lib_confdb_key_iter->next_entry,
  663. &key_name,
  664. &key_name_len,
  665. &value,
  666. &value_len))
  667. ret = CS_ERR_ACCESS;
  668. else {
  669. memcpy(res_lib_confdb_key_iter.key_name.value, key_name, key_name_len);
  670. if (value_len > CS_MAX_NAME_LENGTH) {
  671. ret = CS_ERR_TOO_BIG;
  672. } else {
  673. memcpy(res_lib_confdb_key_iter.value.value, value, value_len);
  674. }
  675. res_lib_confdb_key_iter.key_name.length = key_name_len;
  676. res_lib_confdb_key_iter.value.length = value_len;
  677. }
  678. res_lib_confdb_key_iter.header.size = sizeof(res_lib_confdb_key_iter);
  679. res_lib_confdb_key_iter.header.id = MESSAGE_RES_CONFDB_KEY_ITER;
  680. res_lib_confdb_key_iter.header.error = ret;
  681. api->ipc_response_send(conn, &res_lib_confdb_key_iter, sizeof(res_lib_confdb_key_iter));
  682. }
  683. static void message_handler_req_lib_confdb_key_iter_typed (void *conn,
  684. const void *message)
  685. {
  686. const struct req_lib_confdb_key_iter *req_lib_confdb_key_iter = message;
  687. struct res_lib_confdb_key_iter_typed res_lib_confdb_key_iter;
  688. void *key_name;
  689. size_t key_name_len;
  690. void *value;
  691. size_t value_len;
  692. int ret = CS_OK;
  693. objdb_value_types_t my_type;
  694. if (api->object_key_iter_from(req_lib_confdb_key_iter->parent_object_handle,
  695. req_lib_confdb_key_iter->next_entry,
  696. &key_name,
  697. &key_name_len,
  698. &value,
  699. &value_len))
  700. ret = CS_ERR_ACCESS;
  701. else {
  702. memcpy(res_lib_confdb_key_iter.key_name.value, key_name, key_name_len);
  703. if (value_len > CS_MAX_NAME_LENGTH) {
  704. ret = CS_ERR_TOO_BIG;
  705. } else {
  706. memcpy(res_lib_confdb_key_iter.value.value, value, value_len);
  707. }
  708. res_lib_confdb_key_iter.key_name.length = key_name_len;
  709. res_lib_confdb_key_iter.key_name.value[key_name_len] = '\0';
  710. res_lib_confdb_key_iter.value.length = value_len;
  711. api->object_key_get_typed(req_lib_confdb_key_iter->parent_object_handle,
  712. (const char*)res_lib_confdb_key_iter.key_name.value,
  713. &value,
  714. &value_len,
  715. &my_type);
  716. res_lib_confdb_key_iter.type = my_type;
  717. }
  718. res_lib_confdb_key_iter.header.size = sizeof(res_lib_confdb_key_iter);
  719. res_lib_confdb_key_iter.header.id = MESSAGE_RES_CONFDB_KEY_ITER_TYPED;
  720. res_lib_confdb_key_iter.header.error = ret;
  721. api->ipc_response_send(conn, &res_lib_confdb_key_iter, sizeof(res_lib_confdb_key_iter));
  722. }
  723. static void message_handler_req_lib_confdb_key_iter_typed2 (void *conn,
  724. const void *message)
  725. {
  726. const struct req_lib_confdb_key_iter *req_lib_confdb_key_iter = message;
  727. struct res_lib_confdb_key_iter_typed2 res_lib_confdb_key_iter;
  728. struct res_lib_confdb_key_iter_typed2 *res = &res_lib_confdb_key_iter;
  729. void *key_name;
  730. size_t key_name_len;
  731. void *value;
  732. size_t value_len;
  733. int ret = CS_OK;
  734. objdb_value_types_t my_type;
  735. if (api->object_key_iter_from(req_lib_confdb_key_iter->parent_object_handle,
  736. req_lib_confdb_key_iter->next_entry,
  737. &key_name,
  738. &key_name_len,
  739. &value,
  740. &value_len)) {
  741. ret = CS_ERR_ACCESS;
  742. res->header.size = sizeof(res_lib_confdb_key_iter);
  743. }
  744. else {
  745. res = alloca(sizeof(struct res_lib_confdb_key_iter_typed2) + value_len);
  746. memcpy(res->key_name.value, key_name, key_name_len);
  747. res->key_name.length = key_name_len;
  748. res->key_name.value[key_name_len] = '\0';
  749. memcpy(&res->value, value, value_len);
  750. res->value_length = value_len;
  751. api->object_key_get_typed(req_lib_confdb_key_iter->parent_object_handle,
  752. (const char*)res->key_name.value,
  753. &value,
  754. &value_len,
  755. &my_type);
  756. res->type = my_type;
  757. res->header.size = sizeof(res_lib_confdb_key_iter)+value_len;
  758. }
  759. res->header.id = MESSAGE_RES_CONFDB_KEY_ITER_TYPED2;
  760. res->header.error = ret;
  761. api->ipc_response_send(conn, res, res->header.size);
  762. }
  763. static void message_handler_req_lib_confdb_object_iter (void *conn,
  764. const void *message)
  765. {
  766. const struct req_lib_confdb_object_iter *req_lib_confdb_object_iter
  767. = message;
  768. struct res_lib_confdb_object_iter res_lib_confdb_object_iter;
  769. size_t object_name_len;
  770. int ret = CS_OK;
  771. if (!req_lib_confdb_object_iter->find_handle) {
  772. if (api->object_find_create(req_lib_confdb_object_iter->parent_object_handle,
  773. NULL, 0,
  774. m2h(&res_lib_confdb_object_iter.find_handle)) == -1) {
  775. ret = CS_ERR_ACCESS;
  776. goto response_send;
  777. }
  778. }
  779. else
  780. res_lib_confdb_object_iter.find_handle = req_lib_confdb_object_iter->find_handle;
  781. if (api->object_find_next(res_lib_confdb_object_iter.find_handle,
  782. m2h(&res_lib_confdb_object_iter.object_handle))) {
  783. ret = CS_ERR_ACCESS;
  784. api->object_find_destroy(res_lib_confdb_object_iter.find_handle);
  785. }
  786. else {
  787. if (api->object_name_get(res_lib_confdb_object_iter.object_handle,
  788. (char *)res_lib_confdb_object_iter.object_name.value,
  789. &object_name_len) == -1) {
  790. ret = CS_ERR_ACCESS;
  791. goto response_send;
  792. } else {
  793. res_lib_confdb_object_iter.object_name.length = object_name_len;
  794. }
  795. }
  796. response_send:
  797. res_lib_confdb_object_iter.header.size = sizeof(res_lib_confdb_object_iter);
  798. res_lib_confdb_object_iter.header.id = MESSAGE_RES_CONFDB_OBJECT_ITER;
  799. res_lib_confdb_object_iter.header.error = ret;
  800. api->ipc_response_send(conn, &res_lib_confdb_object_iter, sizeof(res_lib_confdb_object_iter));
  801. }
  802. static void message_handler_req_lib_confdb_object_find (void *conn,
  803. const void *message)
  804. {
  805. const struct req_lib_confdb_object_find *req_lib_confdb_object_find
  806. = message;
  807. struct res_lib_confdb_object_find res_lib_confdb_object_find;
  808. int ret = CS_OK;
  809. if (!req_lib_confdb_object_find->find_handle) {
  810. if (api->object_find_create(req_lib_confdb_object_find->parent_object_handle,
  811. req_lib_confdb_object_find->object_name.value,
  812. req_lib_confdb_object_find->object_name.length,
  813. m2h(&res_lib_confdb_object_find.find_handle)) == -1) {
  814. ret = CS_ERR_ACCESS;
  815. goto response_send;
  816. }
  817. }
  818. else
  819. res_lib_confdb_object_find.find_handle = req_lib_confdb_object_find->find_handle;
  820. if (api->object_find_next(res_lib_confdb_object_find.find_handle,
  821. m2h(&res_lib_confdb_object_find.object_handle))) {
  822. ret = CS_ERR_ACCESS;
  823. api->object_find_destroy(res_lib_confdb_object_find.find_handle);
  824. }
  825. response_send:
  826. res_lib_confdb_object_find.header.size = sizeof(res_lib_confdb_object_find);
  827. res_lib_confdb_object_find.header.id = MESSAGE_RES_CONFDB_OBJECT_FIND;
  828. res_lib_confdb_object_find.header.error = ret;
  829. api->ipc_response_send(conn, &res_lib_confdb_object_find, sizeof(res_lib_confdb_object_find));
  830. }
  831. static void message_handler_req_lib_confdb_write (void *conn,
  832. const void *message)
  833. {
  834. struct res_lib_confdb_write res_lib_confdb_write;
  835. int ret = CS_OK;
  836. const char *error_string = NULL;
  837. if (api->object_write_config(&error_string))
  838. ret = CS_ERR_ACCESS;
  839. res_lib_confdb_write.header.size = sizeof(res_lib_confdb_write);
  840. res_lib_confdb_write.header.id = MESSAGE_RES_CONFDB_WRITE;
  841. res_lib_confdb_write.header.error = ret;
  842. if (error_string) {
  843. strcpy((char *)res_lib_confdb_write.error.value, error_string);
  844. res_lib_confdb_write.error.length = strlen(error_string) + 1;
  845. } else
  846. res_lib_confdb_write.error.length = 0;
  847. api->ipc_response_send(conn, &res_lib_confdb_write, sizeof(res_lib_confdb_write));
  848. }
  849. static void message_handler_req_lib_confdb_reload (void *conn,
  850. const void *message)
  851. {
  852. const struct req_lib_confdb_reload *req_lib_confdb_reload = message;
  853. struct res_lib_confdb_reload res_lib_confdb_reload;
  854. int ret = CS_OK;
  855. const char *error_string = NULL;
  856. if (api->object_reload_config(req_lib_confdb_reload->flush, &error_string))
  857. ret = CS_ERR_ACCESS;
  858. res_lib_confdb_reload.header.size = sizeof(res_lib_confdb_reload);
  859. res_lib_confdb_reload.header.id = MESSAGE_RES_CONFDB_RELOAD;
  860. res_lib_confdb_reload.header.error = ret;
  861. if(error_string) {
  862. strcpy((char *)res_lib_confdb_reload.error.value, error_string);
  863. res_lib_confdb_reload.error.length = strlen(error_string) + 1;
  864. } else
  865. res_lib_confdb_reload.error.length = 0;
  866. api->ipc_response_send(conn, &res_lib_confdb_reload, sizeof(res_lib_confdb_reload));
  867. }
  868. /*
  869. * Write byte to notify_pipe, what makes objdb_notify_dispatch trigger.
  870. * Return -1 on failure otherwise 0.
  871. */
  872. static int write_to_notify_pipe(void)
  873. {
  874. char pipe_cmd;
  875. ssize_t written;
  876. pipe_cmd = 'M'; /* Message */
  877. retry_write:
  878. written = write(notify_pipe[1], &pipe_cmd, sizeof(pipe_cmd));
  879. if (written == -1) {
  880. if (errno == EINTR) {
  881. goto retry_write;
  882. }
  883. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  884. /*
  885. * Different error then EINTR or BLOCK -> exit with error
  886. */
  887. return (-1);
  888. }
  889. } else if (written != sizeof (pipe_cmd)) {
  890. return (-1);
  891. }
  892. return (0);
  893. }
  894. static int objdb_notify_dispatch(hdb_handle_t handle,
  895. int fd, int revents, void *data)
  896. {
  897. struct confdb_ipc_message_holder *holder;
  898. ssize_t rc;
  899. char pipe_cmd;
  900. int counter;
  901. if (revents & POLLHUP) {
  902. return -1;
  903. }
  904. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  905. retry_read:
  906. rc = read(fd, &pipe_cmd, sizeof(pipe_cmd));
  907. if (rc == sizeof(pipe_cmd)) {
  908. goto retry_read; /* Flush whole buffer */
  909. }
  910. if (rc == -1) {
  911. if (errno == EINTR) {
  912. goto retry_read;
  913. }
  914. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  915. goto unlock_exit;
  916. }
  917. } else {
  918. goto unlock_exit; /* rc != -1 && rc != 1 -> end of file */
  919. }
  920. /*
  921. * To ensure we will not spent too much time in this function, counter is added
  922. * and terminate condition for while cycle is not only empty_list but also number
  923. * of processed items.
  924. */
  925. counter = 0;
  926. while (!list_empty (&confdb_ipc_message_holder_list_head) && counter++ < 256) {
  927. holder = list_entry (confdb_ipc_message_holder_list_head.next,
  928. struct confdb_ipc_message_holder, list);
  929. list_del (&holder->list);
  930. /*
  931. * All list operations are done now, so unlock list mutex to
  932. * prevent deadlock in IPC.
  933. */
  934. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  935. api->ipc_dispatch_send(holder->conn, holder->msg, holder->mlen);
  936. api->ipc_refcnt_dec(holder->conn);
  937. free(holder);
  938. /*
  939. * Next operation is again list one, so lock list again.
  940. */
  941. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  942. }
  943. if (!list_empty (&confdb_ipc_message_holder_list_head)) {
  944. /*
  945. * Ensure to call this function again. We have no way how
  946. * to handle error so it's ignored.
  947. */
  948. (void)write_to_notify_pipe();
  949. }
  950. unlock_exit:
  951. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  952. return 0;
  953. }
  954. static int32_t ipc_dispatch_send_from_poll_thread(void *conn, const void *msg, size_t mlen)
  955. {
  956. struct confdb_ipc_message_holder *holder;
  957. size_t holder_size;
  958. api->ipc_refcnt_inc(conn);
  959. holder_size = sizeof (*holder) + mlen;
  960. holder = malloc (holder_size);
  961. if (holder == NULL) {
  962. api->ipc_refcnt_dec(conn);
  963. return -1;
  964. }
  965. memset(holder, 0, holder_size);
  966. holder->conn = conn;
  967. holder->mlen = mlen;
  968. memcpy(holder->msg, msg, mlen);
  969. list_init(&holder->list);
  970. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  971. list_add_tail (&holder->list, &confdb_ipc_message_holder_list_head);
  972. if (write_to_notify_pipe() == -1) {
  973. goto refcnt_del_unlock_exit;
  974. }
  975. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  976. return 0;
  977. refcnt_del_unlock_exit:
  978. list_del (&holder->list);
  979. free(holder);
  980. api->ipc_refcnt_dec(conn);
  981. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  982. return -1;
  983. }
  984. static void confdb_notify_lib_of_key_change(object_change_type_t change_type,
  985. hdb_handle_t parent_object_handle,
  986. hdb_handle_t object_handle,
  987. const void *object_name_pt, size_t object_name_len,
  988. const void *key_name_pt, size_t key_name_len,
  989. const void *key_value_pt, size_t key_value_len,
  990. void *priv_data_pt)
  991. {
  992. struct res_lib_confdb_key_change_callback2 *res;
  993. res = alloca(sizeof(struct res_lib_confdb_key_change_callback2) + key_value_len);
  994. res->header.size = sizeof(struct res_lib_confdb_key_change_callback2) + key_value_len;
  995. res->header.id = MESSAGE_RES_CONFDB_KEY_CHANGE_CALLBACK2;
  996. res->header.error = CS_OK;
  997. // handle & type
  998. res->change_type = change_type;
  999. res->parent_object_handle = parent_object_handle;
  1000. res->object_handle = object_handle;
  1001. //object
  1002. memcpy(res->object_name.value, object_name_pt, object_name_len);
  1003. res->object_name.length = object_name_len;
  1004. //key name
  1005. memcpy(res->key_name.value, key_name_pt, key_name_len);
  1006. res->key_name.length = key_name_len;
  1007. //key value
  1008. memcpy(&res->key_value, key_value_pt, key_value_len);
  1009. res->key_value_length = key_value_len;
  1010. ipc_dispatch_send_from_poll_thread(priv_data_pt, res, res->header.size);
  1011. }
  1012. static void confdb_notify_lib_of_new_object(hdb_handle_t parent_object_handle,
  1013. hdb_handle_t object_handle,
  1014. const void *name_pt, size_t name_len,
  1015. void *priv_data_pt)
  1016. {
  1017. struct res_lib_confdb_object_create_callback res;
  1018. res.header.size = sizeof(res);
  1019. res.header.id = MESSAGE_RES_CONFDB_OBJECT_CREATE_CALLBACK;
  1020. res.header.error = CS_OK;
  1021. res.parent_object_handle = parent_object_handle;
  1022. res.object_handle = object_handle;
  1023. memcpy(res.name.value, name_pt, name_len);
  1024. res.name.length = name_len;
  1025. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  1026. }
  1027. static void confdb_notify_lib_of_destroyed_object(
  1028. hdb_handle_t parent_object_handle,
  1029. const void *name_pt, size_t name_len,
  1030. void *priv_data_pt)
  1031. {
  1032. struct res_lib_confdb_object_destroy_callback res;
  1033. res.header.size = sizeof(res);
  1034. res.header.id = MESSAGE_RES_CONFDB_OBJECT_DESTROY_CALLBACK;
  1035. res.header.error = CS_OK;
  1036. res.parent_object_handle = parent_object_handle;
  1037. memcpy(res.name.value, name_pt, name_len);
  1038. res.name.length = name_len;
  1039. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  1040. }
  1041. static void confdb_notify_lib_of_reload(objdb_reload_notify_type_t notify_type,
  1042. int flush,
  1043. void *priv_data_pt)
  1044. {
  1045. struct res_lib_confdb_reload_callback res;
  1046. res.header.size = sizeof(res);
  1047. res.header.id = MESSAGE_RES_CONFDB_RELOAD_CALLBACK;
  1048. res.header.error = CS_OK;
  1049. res.type = notify_type;
  1050. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  1051. }
  1052. static void message_handler_req_lib_confdb_track_start (void *conn,
  1053. const void *message)
  1054. {
  1055. const struct req_lib_confdb_object_track_start *req = message;
  1056. coroipc_response_header_t res;
  1057. api->object_track_start(req->object_handle,
  1058. req->flags,
  1059. confdb_notify_lib_of_key_change,
  1060. confdb_notify_lib_of_new_object,
  1061. confdb_notify_lib_of_destroyed_object,
  1062. confdb_notify_lib_of_reload,
  1063. conn);
  1064. res.size = sizeof(res);
  1065. res.id = MESSAGE_RES_CONFDB_TRACK_START;
  1066. res.error = CS_OK;
  1067. api->ipc_response_send(conn, &res, sizeof(res));
  1068. }
  1069. static void message_handler_req_lib_confdb_track_stop (void *conn,
  1070. const void *message)
  1071. {
  1072. coroipc_response_header_t res;
  1073. api->object_track_stop(confdb_notify_lib_of_key_change,
  1074. confdb_notify_lib_of_new_object,
  1075. confdb_notify_lib_of_destroyed_object,
  1076. confdb_notify_lib_of_reload,
  1077. conn);
  1078. res.size = sizeof(res);
  1079. res.id = MESSAGE_RES_CONFDB_TRACK_STOP;
  1080. res.error = CS_OK;
  1081. api->ipc_response_send(conn, &res, sizeof(res));
  1082. }
  1083. static void message_handler_req_lib_confdb_key_create_typed2 (void *conn,
  1084. const void *message)
  1085. {
  1086. const struct req_lib_confdb_key_create_typed2 *req_lib_confdb_key_create
  1087. = message;
  1088. coroipc_response_header_t res;
  1089. int ret = CS_OK;
  1090. if (api->object_key_create_typed(req_lib_confdb_key_create->object_handle,
  1091. (char*)req_lib_confdb_key_create->key_name.value,
  1092. &req_lib_confdb_key_create->value,
  1093. req_lib_confdb_key_create->value_length,
  1094. req_lib_confdb_key_create->type))
  1095. ret = CS_ERR_ACCESS;
  1096. res.size = sizeof(res);
  1097. res.id = MESSAGE_RES_CONFDB_KEY_CREATE;
  1098. res.error = ret;
  1099. api->ipc_response_send(conn, &res, sizeof(res));
  1100. }
  1101. static void message_handler_req_lib_confdb_key_replace2 (void *conn,
  1102. const void *message)
  1103. {
  1104. const struct req_lib_confdb_key_replace2 *req_lib_confdb_key_replace
  1105. = message;
  1106. coroipc_response_header_t res;
  1107. int ret = CS_OK;
  1108. if (api->object_key_replace(req_lib_confdb_key_replace->object_handle,
  1109. req_lib_confdb_key_replace->key_name.value,
  1110. req_lib_confdb_key_replace->key_name.length,
  1111. &req_lib_confdb_key_replace->new_value,
  1112. req_lib_confdb_key_replace->new_value_length))
  1113. ret = CS_ERR_ACCESS;
  1114. res.size = sizeof(res);
  1115. res.id = MESSAGE_RES_CONFDB_KEY_REPLACE;
  1116. res.error = ret;
  1117. api->ipc_response_send(conn, &res, sizeof(res));
  1118. }