confdb.c 34 KB

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