confdb.c 32 KB

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