confdb.c 34 KB

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