confdb.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*
  2. * Copyright (c) 2008-2010 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_get (void *conn,
  92. const void *message);
  93. static void message_handler_req_lib_confdb_key_get_typed (void *conn,
  94. const void *message);
  95. static void message_handler_req_lib_confdb_key_replace (void *conn,
  96. const void *message);
  97. static void message_handler_req_lib_confdb_key_delete (void *conn,
  98. const void *message);
  99. static void message_handler_req_lib_confdb_key_iter (void *conn,
  100. const void *message);
  101. static void message_handler_req_lib_confdb_key_iter_typed (void *conn,
  102. const void *message);
  103. static void message_handler_req_lib_confdb_key_increment (void *conn,
  104. const void *message);
  105. static void message_handler_req_lib_confdb_key_decrement (void *conn,
  106. const void *message);
  107. static void message_handler_req_lib_confdb_object_iter (void *conn,
  108. const void *message);
  109. static void message_handler_req_lib_confdb_object_find (void *conn,
  110. const void *message);
  111. static void message_handler_req_lib_confdb_object_parent_get (void *conn,
  112. const void *message);
  113. static void message_handler_req_lib_confdb_write (void *conn,
  114. const void *message);
  115. static void message_handler_req_lib_confdb_reload (void *conn,
  116. const void *message);
  117. static void message_handler_req_lib_confdb_track_start (void *conn,
  118. const void *message);
  119. static void message_handler_req_lib_confdb_track_stop (void *conn,
  120. const void *message);
  121. static void confdb_notify_lib_of_key_change(
  122. object_change_type_t change_type,
  123. hdb_handle_t parent_object_handle,
  124. hdb_handle_t object_handle,
  125. const void *object_name_pt, size_t object_name_len,
  126. const void *key_name_pt, size_t key_name_len,
  127. const void *key_value_pt, size_t key_value_len,
  128. void *priv_data_pt);
  129. static void confdb_notify_lib_of_new_object(
  130. hdb_handle_t parent_object_handle,
  131. hdb_handle_t object_handle,
  132. const uint8_t *name_pt, size_t name_len,
  133. void *priv_data_pt);
  134. static void confdb_notify_lib_of_destroyed_object(
  135. hdb_handle_t parent_object_handle,
  136. const uint8_t *name_pt, size_t name_len,
  137. void *priv_data_pt);
  138. static void confdb_notify_lib_of_reload(
  139. objdb_reload_notify_type_t notify_type,
  140. int flush,
  141. void *priv_data_pt);
  142. /*
  143. * Library Handler Definition
  144. */
  145. static struct corosync_lib_handler confdb_lib_engine[] =
  146. {
  147. { /* 0 */
  148. .lib_handler_fn = message_handler_req_lib_confdb_object_create,
  149. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  150. },
  151. { /* 1 */
  152. .lib_handler_fn = message_handler_req_lib_confdb_object_destroy,
  153. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  154. },
  155. { /* 2 */
  156. .lib_handler_fn = message_handler_req_lib_confdb_object_find,
  157. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  158. },
  159. { /* 3 */
  160. .lib_handler_fn = message_handler_req_lib_confdb_key_create,
  161. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  162. },
  163. { /* 4 */
  164. .lib_handler_fn = message_handler_req_lib_confdb_key_get,
  165. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  166. },
  167. { /* 5 */
  168. .lib_handler_fn = message_handler_req_lib_confdb_key_replace,
  169. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  170. },
  171. { /* 6 */
  172. .lib_handler_fn = message_handler_req_lib_confdb_key_delete,
  173. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  174. },
  175. { /* 7 */
  176. .lib_handler_fn = message_handler_req_lib_confdb_object_iter,
  177. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  178. },
  179. { /* 8 */
  180. .lib_handler_fn = message_handler_req_lib_confdb_object_parent_get,
  181. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  182. },
  183. { /* 9 */
  184. .lib_handler_fn = message_handler_req_lib_confdb_key_iter,
  185. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  186. },
  187. { /* 10 */
  188. .lib_handler_fn = message_handler_req_lib_confdb_track_start,
  189. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  190. },
  191. { /* 11 */
  192. .lib_handler_fn = message_handler_req_lib_confdb_track_stop,
  193. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  194. },
  195. { /* 12 */
  196. .lib_handler_fn = message_handler_req_lib_confdb_write,
  197. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  198. },
  199. { /* 13 */
  200. .lib_handler_fn = message_handler_req_lib_confdb_reload,
  201. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  202. },
  203. { /* 14 */
  204. .lib_handler_fn = message_handler_req_lib_confdb_object_find_destroy,
  205. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  206. },
  207. { /* 15 */
  208. .lib_handler_fn = message_handler_req_lib_confdb_key_increment,
  209. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  210. },
  211. { /* 16 */
  212. .lib_handler_fn = message_handler_req_lib_confdb_key_decrement,
  213. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  214. },
  215. { /* 17 */
  216. .lib_handler_fn = message_handler_req_lib_confdb_key_create_typed,
  217. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  218. },
  219. { /* 18 */
  220. .lib_handler_fn = message_handler_req_lib_confdb_key_get_typed,
  221. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  222. },
  223. { /* 19 */
  224. .lib_handler_fn = message_handler_req_lib_confdb_key_iter_typed,
  225. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  226. },
  227. };
  228. struct corosync_service_engine confdb_service_engine = {
  229. .name = "corosync cluster config database access v1.01",
  230. .id = CONFDB_SERVICE,
  231. .priority = 1,
  232. .private_data_size = 0,
  233. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  234. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  235. .lib_init_fn = confdb_lib_init_fn,
  236. .lib_exit_fn = confdb_lib_exit_fn,
  237. .lib_engine = confdb_lib_engine,
  238. .lib_engine_count = sizeof (confdb_lib_engine) / sizeof (struct corosync_lib_handler),
  239. .exec_init_fn = confdb_exec_init_fn,
  240. .exec_exit_fn = confdb_exec_exit_fn,
  241. };
  242. /*
  243. * Dynamic loader definition
  244. */
  245. static struct corosync_service_engine *confdb_get_service_engine_ver0 (void);
  246. static struct corosync_service_engine_iface_ver0 confdb_service_engine_iface = {
  247. .corosync_get_service_engine_ver0 = confdb_get_service_engine_ver0
  248. };
  249. static struct lcr_iface corosync_confdb_ver0[1] = {
  250. {
  251. .name = "corosync_confdb",
  252. .version = 0,
  253. .versions_replace = 0,
  254. .versions_replace_count = 0,
  255. .dependencies = 0,
  256. .dependency_count = 0,
  257. .constructor = NULL,
  258. .destructor = NULL,
  259. .interfaces = NULL
  260. }
  261. };
  262. static struct lcr_comp confdb_comp_ver0 = {
  263. .iface_count = 1,
  264. .ifaces = corosync_confdb_ver0
  265. };
  266. static struct corosync_service_engine *confdb_get_service_engine_ver0 (void)
  267. {
  268. return (&confdb_service_engine);
  269. }
  270. #ifdef COROSYNC_SOLARIS
  271. void corosync_lcr_component_register (void);
  272. void corosync_lcr_component_register (void) {
  273. #else
  274. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  275. #endif
  276. lcr_interfaces_set (&corosync_confdb_ver0[0], &confdb_service_engine_iface);
  277. lcr_component_register (&confdb_comp_ver0);
  278. }
  279. static int confdb_exec_exit_fn(void)
  280. {
  281. api->poll_dispatch_delete(api->poll_handle_get(), notify_pipe[0]);
  282. close(notify_pipe[0]);
  283. close(notify_pipe[1]);
  284. return 0;
  285. }
  286. static int confdb_exec_init_fn (
  287. struct corosync_api_v1 *corosync_api)
  288. {
  289. int i;
  290. #ifdef COROSYNC_SOLARIS
  291. logsys_subsys_init();
  292. #endif
  293. api = corosync_api;
  294. if (pipe(notify_pipe) != 0) {
  295. return -1;
  296. }
  297. for (i = 0; i < 2; i++) {
  298. if (fd_set_nonblocking (notify_pipe[i]) == -1) {
  299. return -1;
  300. }
  301. }
  302. return api->poll_dispatch_add(api->poll_handle_get(), notify_pipe[0],
  303. POLLIN, NULL, objdb_notify_dispatch);
  304. }
  305. static int confdb_lib_init_fn (void *conn)
  306. {
  307. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p\n", conn);
  308. return (0);
  309. }
  310. static int confdb_lib_exit_fn (void *conn)
  311. {
  312. log_printf(LOGSYS_LEVEL_DEBUG, "exit_fn for conn=%p\n", conn);
  313. /* cleanup the object trackers for this client. */
  314. api->object_track_stop(confdb_notify_lib_of_key_change,
  315. confdb_notify_lib_of_new_object,
  316. confdb_notify_lib_of_destroyed_object,
  317. confdb_notify_lib_of_reload,
  318. conn);
  319. return (0);
  320. }
  321. static int fd_set_nonblocking(int fd)
  322. {
  323. int flags;
  324. int res;
  325. flags = fcntl (fd, F_GETFL);
  326. if (flags == -1) {
  327. return -1;
  328. }
  329. flags |= O_NONBLOCK;
  330. res = fcntl (fd, F_SETFL, flags);
  331. return res;
  332. }
  333. static void message_handler_req_lib_confdb_object_create (void *conn,
  334. const void *message)
  335. {
  336. const struct req_lib_confdb_object_create *req_lib_confdb_object_create
  337. = message;
  338. struct res_lib_confdb_object_create res_lib_confdb_object_create;
  339. hdb_handle_t object_handle;
  340. int ret = CS_OK;
  341. if (api->object_create(req_lib_confdb_object_create->parent_object_handle,
  342. &object_handle,
  343. req_lib_confdb_object_create->object_name.value,
  344. req_lib_confdb_object_create->object_name.length))
  345. ret = CS_ERR_ACCESS;
  346. res_lib_confdb_object_create.object_handle = object_handle;
  347. res_lib_confdb_object_create.header.size = sizeof(res_lib_confdb_object_create);
  348. res_lib_confdb_object_create.header.id = MESSAGE_RES_CONFDB_OBJECT_CREATE;
  349. res_lib_confdb_object_create.header.error = ret;
  350. api->ipc_response_send(conn, &res_lib_confdb_object_create, sizeof(res_lib_confdb_object_create));
  351. }
  352. static void message_handler_req_lib_confdb_object_destroy (void *conn,
  353. const void *message)
  354. {
  355. const struct req_lib_confdb_object_destroy *req_lib_confdb_object_destroy
  356. = message;
  357. coroipc_response_header_t res;
  358. int ret = CS_OK;
  359. if (api->object_destroy(req_lib_confdb_object_destroy->object_handle))
  360. ret = CS_ERR_ACCESS;
  361. res.size = sizeof(res);
  362. res.id = MESSAGE_RES_CONFDB_OBJECT_DESTROY;
  363. res.error = ret;
  364. api->ipc_response_send(conn, &res, sizeof(res));
  365. }
  366. static void message_handler_req_lib_confdb_object_find_destroy (void *conn,
  367. const void *message)
  368. {
  369. const struct req_lib_confdb_object_find_destroy
  370. *req_lib_confdb_object_find_destroy = message;
  371. coroipc_response_header_t res;
  372. int ret = CS_OK;
  373. if (api->object_find_destroy(req_lib_confdb_object_find_destroy->find_handle))
  374. ret = CS_ERR_ACCESS;
  375. res.size = sizeof(res);
  376. res.id = MESSAGE_RES_CONFDB_OBJECT_FIND_DESTROY;
  377. res.error = ret;
  378. api->ipc_response_send(conn, &res, sizeof(res));
  379. }
  380. static void message_handler_req_lib_confdb_key_create (void *conn,
  381. const void *message)
  382. {
  383. const struct req_lib_confdb_key_create *req_lib_confdb_key_create
  384. = message;
  385. coroipc_response_header_t res;
  386. int ret = CS_OK;
  387. if (api->object_key_create(req_lib_confdb_key_create->object_handle,
  388. req_lib_confdb_key_create->key_name.value,
  389. req_lib_confdb_key_create->key_name.length,
  390. req_lib_confdb_key_create->value.value,
  391. req_lib_confdb_key_create->value.length))
  392. ret = CS_ERR_ACCESS;
  393. res.size = sizeof(res);
  394. res.id = MESSAGE_RES_CONFDB_KEY_CREATE;
  395. res.error = ret;
  396. api->ipc_response_send(conn, &res, sizeof(res));
  397. }
  398. static void message_handler_req_lib_confdb_key_create_typed (void *conn,
  399. const void *message)
  400. {
  401. const struct req_lib_confdb_key_create_typed *req_lib_confdb_key_create
  402. = message;
  403. coroipc_response_header_t res;
  404. int ret = CS_OK;
  405. if (api->object_key_create_typed(req_lib_confdb_key_create->object_handle,
  406. (char*)req_lib_confdb_key_create->key_name.value,
  407. req_lib_confdb_key_create->value.value,
  408. req_lib_confdb_key_create->value.length,
  409. req_lib_confdb_key_create->type))
  410. ret = CS_ERR_ACCESS;
  411. res.size = sizeof(res);
  412. res.id = MESSAGE_RES_CONFDB_KEY_CREATE;
  413. res.error = ret;
  414. api->ipc_response_send(conn, &res, sizeof(res));
  415. }
  416. static void message_handler_req_lib_confdb_key_get (void *conn,
  417. const void *message)
  418. {
  419. const struct req_lib_confdb_key_get *req_lib_confdb_key_get = message;
  420. struct res_lib_confdb_key_get res_lib_confdb_key_get;
  421. size_t value_len;
  422. void *value;
  423. int ret = CS_OK;
  424. if (api->object_key_get(req_lib_confdb_key_get->parent_object_handle,
  425. req_lib_confdb_key_get->key_name.value,
  426. req_lib_confdb_key_get->key_name.length,
  427. &value,
  428. &value_len))
  429. ret = CS_ERR_ACCESS;
  430. else {
  431. memcpy(res_lib_confdb_key_get.value.value, value, value_len);
  432. res_lib_confdb_key_get.value.length = value_len;
  433. }
  434. res_lib_confdb_key_get.header.size = sizeof(res_lib_confdb_key_get);
  435. res_lib_confdb_key_get.header.id = MESSAGE_RES_CONFDB_KEY_GET;
  436. res_lib_confdb_key_get.header.error = ret;
  437. api->ipc_response_send(conn, &res_lib_confdb_key_get, sizeof(res_lib_confdb_key_get));
  438. }
  439. static void message_handler_req_lib_confdb_key_get_typed (void *conn,
  440. const void *message)
  441. {
  442. const struct req_lib_confdb_key_get *req_lib_confdb_key_get = message;
  443. struct res_lib_confdb_key_get_typed res_lib_confdb_key_get;
  444. size_t value_len;
  445. void *value;
  446. int ret = CS_OK;
  447. objdb_value_types_t type;
  448. char * key_name = (char*)req_lib_confdb_key_get->key_name.value;
  449. key_name[req_lib_confdb_key_get->key_name.length] = '\0';
  450. if (api->object_key_get_typed(req_lib_confdb_key_get->parent_object_handle,
  451. key_name,
  452. &value,
  453. &value_len, &type))
  454. ret = CS_ERR_ACCESS;
  455. else {
  456. memcpy(res_lib_confdb_key_get.value.value, value, value_len);
  457. res_lib_confdb_key_get.value.length = value_len;
  458. res_lib_confdb_key_get.type = type;
  459. }
  460. res_lib_confdb_key_get.header.size = sizeof(res_lib_confdb_key_get);
  461. res_lib_confdb_key_get.header.id = MESSAGE_RES_CONFDB_KEY_GET_TYPED;
  462. res_lib_confdb_key_get.header.error = ret;
  463. api->ipc_response_send(conn, &res_lib_confdb_key_get, sizeof(res_lib_confdb_key_get));
  464. }
  465. static void message_handler_req_lib_confdb_key_increment (void *conn,
  466. const void *message)
  467. {
  468. const struct req_lib_confdb_key_get *req_lib_confdb_key_get = message;
  469. struct res_lib_confdb_key_incdec res_lib_confdb_key_incdec;
  470. int ret = CS_OK;
  471. if (api->object_key_increment(req_lib_confdb_key_get->parent_object_handle,
  472. req_lib_confdb_key_get->key_name.value,
  473. req_lib_confdb_key_get->key_name.length,
  474. &res_lib_confdb_key_incdec.value))
  475. ret = CS_ERR_ACCESS;
  476. res_lib_confdb_key_incdec.header.size = sizeof(res_lib_confdb_key_incdec);
  477. res_lib_confdb_key_incdec.header.id = MESSAGE_RES_CONFDB_KEY_INCREMENT;
  478. res_lib_confdb_key_incdec.header.error = ret;
  479. api->ipc_response_send(conn, &res_lib_confdb_key_incdec, sizeof(res_lib_confdb_key_incdec));
  480. }
  481. static void message_handler_req_lib_confdb_key_decrement (void *conn,
  482. const void *message)
  483. {
  484. const struct req_lib_confdb_key_get *req_lib_confdb_key_get = message;
  485. struct res_lib_confdb_key_incdec res_lib_confdb_key_incdec;
  486. int ret = CS_OK;
  487. if (api->object_key_decrement(req_lib_confdb_key_get->parent_object_handle,
  488. req_lib_confdb_key_get->key_name.value,
  489. req_lib_confdb_key_get->key_name.length,
  490. &res_lib_confdb_key_incdec.value))
  491. ret = CS_ERR_ACCESS;
  492. res_lib_confdb_key_incdec.header.size = sizeof(res_lib_confdb_key_incdec);
  493. res_lib_confdb_key_incdec.header.id = MESSAGE_RES_CONFDB_KEY_DECREMENT;
  494. res_lib_confdb_key_incdec.header.error = ret;
  495. api->ipc_response_send(conn, &res_lib_confdb_key_incdec, sizeof(res_lib_confdb_key_incdec));
  496. }
  497. static void message_handler_req_lib_confdb_key_replace (void *conn,
  498. const void *message)
  499. {
  500. const struct req_lib_confdb_key_replace *req_lib_confdb_key_replace
  501. = message;
  502. coroipc_response_header_t res;
  503. int ret = CS_OK;
  504. if (api->object_key_replace(req_lib_confdb_key_replace->object_handle,
  505. req_lib_confdb_key_replace->key_name.value,
  506. req_lib_confdb_key_replace->key_name.length,
  507. req_lib_confdb_key_replace->new_value.value,
  508. req_lib_confdb_key_replace->new_value.length))
  509. ret = CS_ERR_ACCESS;
  510. res.size = sizeof(res);
  511. res.id = MESSAGE_RES_CONFDB_KEY_REPLACE;
  512. res.error = ret;
  513. api->ipc_response_send(conn, &res, sizeof(res));
  514. }
  515. static void message_handler_req_lib_confdb_key_delete (void *conn,
  516. const void *message)
  517. {
  518. const struct req_lib_confdb_key_delete *req_lib_confdb_key_delete
  519. = message;
  520. coroipc_response_header_t res;
  521. int ret = CS_OK;
  522. if (api->object_key_delete(req_lib_confdb_key_delete->object_handle,
  523. req_lib_confdb_key_delete->key_name.value,
  524. req_lib_confdb_key_delete->key_name.length))
  525. ret = CS_ERR_ACCESS;
  526. res.size = sizeof(res);
  527. res.id = MESSAGE_RES_CONFDB_KEY_DELETE;
  528. res.error = ret;
  529. api->ipc_response_send(conn, &res, sizeof(res));
  530. }
  531. static void message_handler_req_lib_confdb_object_parent_get (void *conn,
  532. const void *message)
  533. {
  534. const struct req_lib_confdb_object_parent_get
  535. *req_lib_confdb_object_parent_get = message;
  536. struct res_lib_confdb_object_parent_get res_lib_confdb_object_parent_get;
  537. hdb_handle_t object_handle;
  538. int ret = CS_OK;
  539. if (api->object_parent_get(req_lib_confdb_object_parent_get->object_handle,
  540. &object_handle))
  541. ret = CS_ERR_ACCESS;
  542. res_lib_confdb_object_parent_get.parent_object_handle = object_handle;
  543. res_lib_confdb_object_parent_get.header.size = sizeof(res_lib_confdb_object_parent_get);
  544. res_lib_confdb_object_parent_get.header.id = MESSAGE_RES_CONFDB_OBJECT_PARENT_GET;
  545. res_lib_confdb_object_parent_get.header.error = ret;
  546. api->ipc_response_send(conn, &res_lib_confdb_object_parent_get, sizeof(res_lib_confdb_object_parent_get));
  547. }
  548. static void message_handler_req_lib_confdb_key_iter (void *conn,
  549. const void *message)
  550. {
  551. const struct req_lib_confdb_key_iter *req_lib_confdb_key_iter = message;
  552. struct res_lib_confdb_key_iter res_lib_confdb_key_iter;
  553. void *key_name;
  554. size_t key_name_len;
  555. void *value;
  556. size_t value_len;
  557. int ret = CS_OK;
  558. if (api->object_key_iter_from(req_lib_confdb_key_iter->parent_object_handle,
  559. req_lib_confdb_key_iter->next_entry,
  560. &key_name,
  561. &key_name_len,
  562. &value,
  563. &value_len))
  564. ret = CS_ERR_ACCESS;
  565. else {
  566. memcpy(res_lib_confdb_key_iter.key_name.value, key_name, key_name_len);
  567. memcpy(res_lib_confdb_key_iter.value.value, value, value_len);
  568. res_lib_confdb_key_iter.key_name.length = key_name_len;
  569. res_lib_confdb_key_iter.value.length = value_len;
  570. }
  571. res_lib_confdb_key_iter.header.size = sizeof(res_lib_confdb_key_iter);
  572. res_lib_confdb_key_iter.header.id = MESSAGE_RES_CONFDB_KEY_ITER;
  573. res_lib_confdb_key_iter.header.error = ret;
  574. api->ipc_response_send(conn, &res_lib_confdb_key_iter, sizeof(res_lib_confdb_key_iter));
  575. }
  576. static void message_handler_req_lib_confdb_key_iter_typed (void *conn,
  577. const void *message)
  578. {
  579. const struct req_lib_confdb_key_iter *req_lib_confdb_key_iter = message;
  580. struct res_lib_confdb_key_iter_typed res_lib_confdb_key_iter;
  581. void *key_name;
  582. size_t key_name_len;
  583. void *value;
  584. size_t value_len;
  585. int ret = CS_OK;
  586. objdb_value_types_t my_type;
  587. if (api->object_key_iter_from(req_lib_confdb_key_iter->parent_object_handle,
  588. req_lib_confdb_key_iter->next_entry,
  589. &key_name,
  590. &key_name_len,
  591. &value,
  592. &value_len))
  593. ret = CS_ERR_ACCESS;
  594. else {
  595. memcpy(res_lib_confdb_key_iter.key_name.value, key_name, key_name_len);
  596. memcpy(res_lib_confdb_key_iter.value.value, value, value_len);
  597. res_lib_confdb_key_iter.key_name.length = key_name_len;
  598. res_lib_confdb_key_iter.key_name.value[key_name_len] = '\0';
  599. res_lib_confdb_key_iter.value.length = value_len;
  600. api->object_key_get_typed(req_lib_confdb_key_iter->parent_object_handle,
  601. (const char*)res_lib_confdb_key_iter.key_name.value,
  602. &value,
  603. &value_len,
  604. &my_type);
  605. res_lib_confdb_key_iter.type = my_type;
  606. }
  607. res_lib_confdb_key_iter.header.size = sizeof(res_lib_confdb_key_iter);
  608. res_lib_confdb_key_iter.header.id = MESSAGE_RES_CONFDB_KEY_ITER_TYPED;
  609. res_lib_confdb_key_iter.header.error = ret;
  610. api->ipc_response_send(conn, &res_lib_confdb_key_iter, sizeof(res_lib_confdb_key_iter));
  611. }
  612. static void message_handler_req_lib_confdb_object_iter (void *conn,
  613. const void *message)
  614. {
  615. const struct req_lib_confdb_object_iter *req_lib_confdb_object_iter
  616. = message;
  617. struct res_lib_confdb_object_iter res_lib_confdb_object_iter;
  618. size_t object_name_len;
  619. int ret = CS_OK;
  620. if (!req_lib_confdb_object_iter->find_handle) {
  621. if (api->object_find_create(req_lib_confdb_object_iter->parent_object_handle,
  622. NULL, 0,
  623. m2h(&res_lib_confdb_object_iter.find_handle)) == -1) {
  624. ret = CS_ERR_ACCESS;
  625. goto response_send;
  626. }
  627. }
  628. else
  629. res_lib_confdb_object_iter.find_handle = req_lib_confdb_object_iter->find_handle;
  630. if (api->object_find_next(res_lib_confdb_object_iter.find_handle,
  631. m2h(&res_lib_confdb_object_iter.object_handle))) {
  632. ret = CS_ERR_ACCESS;
  633. api->object_find_destroy(res_lib_confdb_object_iter.find_handle);
  634. }
  635. else {
  636. if (api->object_name_get(res_lib_confdb_object_iter.object_handle,
  637. (char *)res_lib_confdb_object_iter.object_name.value,
  638. &object_name_len) == -1) {
  639. ret = CS_ERR_ACCESS;
  640. goto response_send;
  641. } else {
  642. res_lib_confdb_object_iter.object_name.length = object_name_len;
  643. }
  644. }
  645. response_send:
  646. res_lib_confdb_object_iter.header.size = sizeof(res_lib_confdb_object_iter);
  647. res_lib_confdb_object_iter.header.id = MESSAGE_RES_CONFDB_OBJECT_ITER;
  648. res_lib_confdb_object_iter.header.error = ret;
  649. api->ipc_response_send(conn, &res_lib_confdb_object_iter, sizeof(res_lib_confdb_object_iter));
  650. }
  651. static void message_handler_req_lib_confdb_object_find (void *conn,
  652. const void *message)
  653. {
  654. const struct req_lib_confdb_object_find *req_lib_confdb_object_find
  655. = message;
  656. struct res_lib_confdb_object_find res_lib_confdb_object_find;
  657. int ret = CS_OK;
  658. if (!req_lib_confdb_object_find->find_handle) {
  659. if (api->object_find_create(req_lib_confdb_object_find->parent_object_handle,
  660. req_lib_confdb_object_find->object_name.value,
  661. req_lib_confdb_object_find->object_name.length,
  662. m2h(&res_lib_confdb_object_find.find_handle)) == -1) {
  663. ret = CS_ERR_ACCESS;
  664. goto response_send;
  665. }
  666. }
  667. else
  668. res_lib_confdb_object_find.find_handle = req_lib_confdb_object_find->find_handle;
  669. if (api->object_find_next(res_lib_confdb_object_find.find_handle,
  670. m2h(&res_lib_confdb_object_find.object_handle))) {
  671. ret = CS_ERR_ACCESS;
  672. api->object_find_destroy(res_lib_confdb_object_find.find_handle);
  673. }
  674. response_send:
  675. res_lib_confdb_object_find.header.size = sizeof(res_lib_confdb_object_find);
  676. res_lib_confdb_object_find.header.id = MESSAGE_RES_CONFDB_OBJECT_FIND;
  677. res_lib_confdb_object_find.header.error = ret;
  678. api->ipc_response_send(conn, &res_lib_confdb_object_find, sizeof(res_lib_confdb_object_find));
  679. }
  680. static void message_handler_req_lib_confdb_write (void *conn,
  681. const void *message)
  682. {
  683. struct res_lib_confdb_write res_lib_confdb_write;
  684. int ret = CS_OK;
  685. const char *error_string = NULL;
  686. if (api->object_write_config(&error_string))
  687. ret = CS_ERR_ACCESS;
  688. res_lib_confdb_write.header.size = sizeof(res_lib_confdb_write);
  689. res_lib_confdb_write.header.id = MESSAGE_RES_CONFDB_WRITE;
  690. res_lib_confdb_write.header.error = ret;
  691. if (error_string) {
  692. strcpy((char *)res_lib_confdb_write.error.value, error_string);
  693. res_lib_confdb_write.error.length = strlen(error_string) + 1;
  694. } else
  695. res_lib_confdb_write.error.length = 0;
  696. api->ipc_response_send(conn, &res_lib_confdb_write, sizeof(res_lib_confdb_write));
  697. }
  698. static void message_handler_req_lib_confdb_reload (void *conn,
  699. const void *message)
  700. {
  701. const struct req_lib_confdb_reload *req_lib_confdb_reload = message;
  702. struct res_lib_confdb_reload res_lib_confdb_reload;
  703. int ret = CS_OK;
  704. const char *error_string = NULL;
  705. if (api->object_reload_config(req_lib_confdb_reload->flush, &error_string))
  706. ret = CS_ERR_ACCESS;
  707. res_lib_confdb_reload.header.size = sizeof(res_lib_confdb_reload);
  708. res_lib_confdb_reload.header.id = MESSAGE_RES_CONFDB_RELOAD;
  709. res_lib_confdb_reload.header.error = ret;
  710. if(error_string) {
  711. strcpy((char *)res_lib_confdb_reload.error.value, error_string);
  712. res_lib_confdb_reload.error.length = strlen(error_string) + 1;
  713. } else
  714. res_lib_confdb_reload.error.length = 0;
  715. api->ipc_response_send(conn, &res_lib_confdb_reload, sizeof(res_lib_confdb_reload));
  716. }
  717. static int objdb_notify_dispatch(hdb_handle_t handle,
  718. int fd, int revents, void *data)
  719. {
  720. struct confdb_ipc_message_holder *holder;
  721. ssize_t rc;
  722. char pipe_cmd;
  723. if (revents & POLLHUP) {
  724. return -1;
  725. }
  726. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  727. retry_read:
  728. rc = read(fd, &pipe_cmd, sizeof(pipe_cmd));
  729. if (rc == sizeof(pipe_cmd)) {
  730. goto retry_read; /* Flush whole buffer */
  731. }
  732. if (rc == -1) {
  733. if (errno == EINTR) {
  734. goto retry_read;
  735. }
  736. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  737. goto unlock_exit;
  738. }
  739. } else {
  740. goto unlock_exit; /* rc != -1 && rc != 1 -> end of file */
  741. }
  742. while (!list_empty (&confdb_ipc_message_holder_list_head)) {
  743. holder = list_entry (confdb_ipc_message_holder_list_head.next,
  744. struct confdb_ipc_message_holder, list);
  745. list_del (&holder->list);
  746. /*
  747. * All list operations are done now, so unlock list mutex to
  748. * prevent deadlock in IPC.
  749. */
  750. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  751. api->ipc_dispatch_send(holder->conn, holder->msg, holder->mlen);
  752. api->ipc_refcnt_dec(holder->conn);
  753. free(holder);
  754. /*
  755. * Next operation is again list one, so lock list again.
  756. */
  757. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  758. }
  759. unlock_exit:
  760. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  761. return 0;
  762. }
  763. static int32_t ipc_dispatch_send_from_poll_thread(void *conn, const void *msg, size_t mlen)
  764. {
  765. struct confdb_ipc_message_holder *holder;
  766. ssize_t written;
  767. size_t holder_size;
  768. char pipe_cmd;
  769. api->ipc_refcnt_inc(conn);
  770. holder_size = sizeof (*holder) + mlen;
  771. holder = malloc (holder_size);
  772. if (holder == NULL) {
  773. api->ipc_refcnt_dec(conn);
  774. return -1;
  775. }
  776. memset(holder, 0, holder_size);
  777. holder->conn = conn;
  778. holder->mlen = mlen;
  779. memcpy(holder->msg, msg, mlen);
  780. list_init(&holder->list);
  781. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  782. list_add_tail (&holder->list, &confdb_ipc_message_holder_list_head);
  783. pipe_cmd = 'M'; /* Message */
  784. retry_write:
  785. written = write(notify_pipe[1], &pipe_cmd, sizeof(pipe_cmd));
  786. if (written == -1) {
  787. if (errno == EINTR) {
  788. goto retry_write;
  789. }
  790. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  791. /*
  792. * Different error then EINTR or BLOCK -> exit with error
  793. */
  794. goto refcnt_del_unlock_exit;
  795. }
  796. } else if (written != sizeof (pipe_cmd)) {
  797. goto refcnt_del_unlock_exit;
  798. }
  799. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  800. return 0;
  801. refcnt_del_unlock_exit:
  802. list_del (&holder->list);
  803. free(holder);
  804. api->ipc_refcnt_dec(conn);
  805. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  806. return -1;
  807. }
  808. static void confdb_notify_lib_of_key_change(object_change_type_t change_type,
  809. hdb_handle_t parent_object_handle,
  810. hdb_handle_t object_handle,
  811. const void *object_name_pt, size_t object_name_len,
  812. const void *key_name_pt, size_t key_name_len,
  813. const void *key_value_pt, size_t key_value_len,
  814. void *priv_data_pt)
  815. {
  816. struct res_lib_confdb_key_change_callback res;
  817. res.header.size = sizeof(res);
  818. res.header.id = MESSAGE_RES_CONFDB_KEY_CHANGE_CALLBACK;
  819. res.header.error = CS_OK;
  820. // handle & type
  821. res.change_type = change_type;
  822. res.parent_object_handle = parent_object_handle;
  823. res.object_handle = object_handle;
  824. //object
  825. memcpy(res.object_name.value, object_name_pt, object_name_len);
  826. res.object_name.length = object_name_len;
  827. //key name
  828. memcpy(res.key_name.value, key_name_pt, key_name_len);
  829. res.key_name.length = key_name_len;
  830. //key value
  831. memcpy(res.key_value.value, key_value_pt, key_value_len);
  832. res.key_value.length = key_value_len;
  833. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  834. }
  835. static void confdb_notify_lib_of_new_object(hdb_handle_t parent_object_handle,
  836. hdb_handle_t object_handle,
  837. const uint8_t *name_pt, size_t name_len,
  838. void *priv_data_pt)
  839. {
  840. struct res_lib_confdb_object_create_callback res;
  841. res.header.size = sizeof(res);
  842. res.header.id = MESSAGE_RES_CONFDB_OBJECT_CREATE_CALLBACK;
  843. res.header.error = CS_OK;
  844. res.parent_object_handle = parent_object_handle;
  845. res.object_handle = object_handle;
  846. memcpy(res.name.value, name_pt, name_len);
  847. res.name.length = name_len;
  848. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  849. }
  850. static void confdb_notify_lib_of_destroyed_object(
  851. hdb_handle_t parent_object_handle,
  852. const uint8_t *name_pt, size_t name_len,
  853. void *priv_data_pt)
  854. {
  855. struct res_lib_confdb_object_destroy_callback res;
  856. res.header.size = sizeof(res);
  857. res.header.id = MESSAGE_RES_CONFDB_OBJECT_DESTROY_CALLBACK;
  858. res.header.error = CS_OK;
  859. res.parent_object_handle = parent_object_handle;
  860. memcpy(res.name.value, name_pt, name_len);
  861. res.name.length = name_len;
  862. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  863. }
  864. static void confdb_notify_lib_of_reload(objdb_reload_notify_type_t notify_type,
  865. int flush,
  866. void *priv_data_pt)
  867. {
  868. struct res_lib_confdb_reload_callback res;
  869. res.header.size = sizeof(res);
  870. res.header.id = MESSAGE_RES_CONFDB_RELOAD_CALLBACK;
  871. res.header.error = CS_OK;
  872. res.type = notify_type;
  873. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  874. }
  875. static void message_handler_req_lib_confdb_track_start (void *conn,
  876. const void *message)
  877. {
  878. const struct req_lib_confdb_object_track_start *req = message;
  879. coroipc_response_header_t res;
  880. api->object_track_start(req->object_handle,
  881. req->flags,
  882. confdb_notify_lib_of_key_change,
  883. confdb_notify_lib_of_new_object,
  884. confdb_notify_lib_of_destroyed_object,
  885. confdb_notify_lib_of_reload,
  886. conn);
  887. res.size = sizeof(res);
  888. res.id = MESSAGE_RES_CONFDB_TRACK_START;
  889. res.error = CS_OK;
  890. api->ipc_response_send(conn, &res, sizeof(res));
  891. }
  892. static void message_handler_req_lib_confdb_track_stop (void *conn,
  893. const void *message)
  894. {
  895. coroipc_response_header_t res;
  896. api->object_track_stop(confdb_notify_lib_of_key_change,
  897. confdb_notify_lib_of_new_object,
  898. confdb_notify_lib_of_destroyed_object,
  899. confdb_notify_lib_of_reload,
  900. conn);
  901. res.size = sizeof(res);
  902. res.id = MESSAGE_RES_CONFDB_TRACK_STOP;
  903. res.error = CS_OK;
  904. api->ipc_response_send(conn, &res, sizeof(res));
  905. }