confdb.c 34 KB

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