confdb.c 31 KB

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