confdb.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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 <qb/qbloop.h>
  44. #include <qb/qbipc_common.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/corodefs.h>
  47. #include <corosync/cfg.h>
  48. #include <corosync/list.h>
  49. #include <corosync/mar_gen.h>
  50. #include <corosync/ipc_confdb.h>
  51. #include <corosync/lcr/lcr_comp.h>
  52. #include <corosync/engine/logsys.h>
  53. #include <corosync/engine/coroapi.h>
  54. LOGSYS_DECLARE_SUBSYS ("CONFDB");
  55. static hdb_handle_t *
  56. m2h (mar_uint64_t *m)
  57. {
  58. /* FIXME enable the following when/if we use gnulib:
  59. (it's a compile-time assertion; i.e., zero run-time cost)
  60. verify (sizeof (*m) == sizeof (hdb_handle_t)); */
  61. return (void *) m;
  62. }
  63. static struct corosync_api_v1 *api;
  64. static int notify_pipe[2];
  65. struct confdb_ipc_message_holder {
  66. void *conn;
  67. size_t mlen;
  68. struct list_head list;
  69. char msg[];
  70. };
  71. DECLARE_LIST_INIT(confdb_ipc_message_holder_list_head);
  72. pthread_mutex_t confdb_ipc_message_holder_list_mutex =
  73. PTHREAD_MUTEX_INITIALIZER;
  74. static int confdb_exec_init_fn (
  75. struct corosync_api_v1 *corosync_api);
  76. static int confdb_exec_exit_fn(void);
  77. static int fd_set_nonblocking(int fd);
  78. static int objdb_notify_dispatch(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. struct qb_ipc_response_header 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. struct qb_ipc_response_header 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. struct qb_ipc_response_header 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. struct qb_ipc_response_header 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. struct qb_ipc_response_header 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. struct qb_ipc_response_header 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(int fd, int revents, void *data)
  744. {
  745. struct confdb_ipc_message_holder *holder;
  746. ssize_t rc;
  747. char pipe_cmd;
  748. if (revents & POLLHUP) {
  749. return -1;
  750. }
  751. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  752. retry_read:
  753. rc = read(fd, &pipe_cmd, sizeof(pipe_cmd));
  754. if (rc == sizeof(pipe_cmd)) {
  755. goto retry_read; /* Flush whole buffer */
  756. }
  757. if (rc == -1) {
  758. if (errno == EINTR) {
  759. goto retry_read;
  760. }
  761. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  762. goto unlock_exit;
  763. }
  764. } else {
  765. goto unlock_exit; /* rc != -1 && rc != 1 -> end of file */
  766. }
  767. while (!list_empty (&confdb_ipc_message_holder_list_head)) {
  768. holder = list_entry (confdb_ipc_message_holder_list_head.next,
  769. struct confdb_ipc_message_holder, list);
  770. list_del (&holder->list);
  771. /*
  772. * All list operations are done now, so unlock list mutex to
  773. * prevent deadlock in IPC.
  774. */
  775. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  776. api->ipc_dispatch_send(holder->conn, holder->msg, holder->mlen);
  777. api->ipc_refcnt_dec(holder->conn);
  778. free(holder);
  779. /*
  780. * Next operation is again list one, so lock list again.
  781. */
  782. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  783. }
  784. unlock_exit:
  785. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  786. return 0;
  787. }
  788. static int32_t ipc_dispatch_send_from_poll_thread(void *conn, const void *msg, size_t mlen)
  789. {
  790. struct confdb_ipc_message_holder *holder;
  791. ssize_t written;
  792. size_t holder_size;
  793. char pipe_cmd;
  794. api->ipc_refcnt_inc(conn);
  795. holder_size = sizeof (*holder) + mlen;
  796. holder = malloc (holder_size);
  797. if (holder == NULL) {
  798. api->ipc_refcnt_dec(conn);
  799. return -1;
  800. }
  801. memset(holder, 0, holder_size);
  802. holder->conn = conn;
  803. holder->mlen = mlen;
  804. memcpy(holder->msg, msg, mlen);
  805. list_init(&holder->list);
  806. pthread_mutex_lock (&confdb_ipc_message_holder_list_mutex);
  807. list_add_tail (&holder->list, &confdb_ipc_message_holder_list_head);
  808. pipe_cmd = 'M'; /* Message */
  809. retry_write:
  810. written = write(notify_pipe[1], &pipe_cmd, sizeof(pipe_cmd));
  811. if (written == -1) {
  812. if (errno == EINTR) {
  813. goto retry_write;
  814. }
  815. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  816. /*
  817. * Different error then EINTR or BLOCK -> exit with error
  818. */
  819. goto refcnt_del_unlock_exit;
  820. }
  821. } else if (written != sizeof (pipe_cmd)) {
  822. goto refcnt_del_unlock_exit;
  823. }
  824. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  825. return 0;
  826. refcnt_del_unlock_exit:
  827. list_del (&holder->list);
  828. free(holder);
  829. api->ipc_refcnt_dec(conn);
  830. pthread_mutex_unlock (&confdb_ipc_message_holder_list_mutex);
  831. return -1;
  832. }
  833. static void confdb_notify_lib_of_key_change(object_change_type_t change_type,
  834. hdb_handle_t parent_object_handle,
  835. hdb_handle_t object_handle,
  836. const void *object_name_pt, size_t object_name_len,
  837. const void *key_name_pt, size_t key_name_len,
  838. const void *key_value_pt, size_t key_value_len,
  839. void *priv_data_pt)
  840. {
  841. struct res_lib_confdb_key_change_callback res;
  842. res.header.size = sizeof(res);
  843. res.header.id = MESSAGE_RES_CONFDB_KEY_CHANGE_CALLBACK;
  844. res.header.error = CS_OK;
  845. // handle & type
  846. res.change_type = change_type;
  847. res.parent_object_handle = parent_object_handle;
  848. res.object_handle = object_handle;
  849. //object
  850. memcpy(res.object_name.value, object_name_pt, object_name_len);
  851. res.object_name.length = object_name_len;
  852. //key name
  853. memcpy(res.key_name.value, key_name_pt, key_name_len);
  854. res.key_name.length = key_name_len;
  855. //key value
  856. memcpy(res.key_value.value, key_value_pt, key_value_len);
  857. res.key_value.length = key_value_len;
  858. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  859. }
  860. static void confdb_notify_lib_of_new_object(hdb_handle_t parent_object_handle,
  861. hdb_handle_t object_handle,
  862. const void *name_pt, size_t name_len,
  863. void *priv_data_pt)
  864. {
  865. struct res_lib_confdb_object_create_callback res;
  866. res.header.size = sizeof(res);
  867. res.header.id = MESSAGE_RES_CONFDB_OBJECT_CREATE_CALLBACK;
  868. res.header.error = CS_OK;
  869. res.parent_object_handle = parent_object_handle;
  870. res.object_handle = object_handle;
  871. memcpy(res.name.value, name_pt, name_len);
  872. res.name.length = name_len;
  873. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  874. }
  875. static void confdb_notify_lib_of_destroyed_object(
  876. hdb_handle_t parent_object_handle,
  877. const void *name_pt, size_t name_len,
  878. void *priv_data_pt)
  879. {
  880. struct res_lib_confdb_object_destroy_callback res;
  881. res.header.size = sizeof(res);
  882. res.header.id = MESSAGE_RES_CONFDB_OBJECT_DESTROY_CALLBACK;
  883. res.header.error = CS_OK;
  884. res.parent_object_handle = parent_object_handle;
  885. memcpy(res.name.value, name_pt, name_len);
  886. res.name.length = name_len;
  887. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  888. }
  889. static void confdb_notify_lib_of_reload(objdb_reload_notify_type_t notify_type,
  890. int flush,
  891. void *priv_data_pt)
  892. {
  893. struct res_lib_confdb_reload_callback res;
  894. res.header.size = sizeof(res);
  895. res.header.id = MESSAGE_RES_CONFDB_RELOAD_CALLBACK;
  896. res.header.error = CS_OK;
  897. res.type = notify_type;
  898. ipc_dispatch_send_from_poll_thread(priv_data_pt, &res, sizeof(res));
  899. }
  900. static void message_handler_req_lib_confdb_track_start (void *conn,
  901. const void *message)
  902. {
  903. const struct req_lib_confdb_object_track_start *req = message;
  904. struct qb_ipc_response_header res;
  905. api->object_track_start(req->object_handle,
  906. req->flags,
  907. confdb_notify_lib_of_key_change,
  908. confdb_notify_lib_of_new_object,
  909. confdb_notify_lib_of_destroyed_object,
  910. confdb_notify_lib_of_reload,
  911. conn);
  912. res.size = sizeof(res);
  913. res.id = MESSAGE_RES_CONFDB_TRACK_START;
  914. res.error = CS_OK;
  915. api->ipc_response_send(conn, &res, sizeof(res));
  916. }
  917. static void message_handler_req_lib_confdb_track_stop (void *conn,
  918. const void *message)
  919. {
  920. struct qb_ipc_response_header res;
  921. api->object_track_stop(confdb_notify_lib_of_key_change,
  922. confdb_notify_lib_of_new_object,
  923. confdb_notify_lib_of_destroyed_object,
  924. confdb_notify_lib_of_reload,
  925. conn);
  926. res.size = sizeof(res);
  927. res.id = MESSAGE_RES_CONFDB_TRACK_STOP;
  928. res.error = CS_OK;
  929. api->ipc_response_send(conn, &res, sizeof(res));
  930. }