cmap.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /*
  2. * Copyright (c) 2011-2017 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@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 Red Hat, 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 CONTRIBUTORS "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 <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include <pthread.h>
  39. #include <sys/types.h>
  40. #include <sys/uio.h>
  41. #include <errno.h>
  42. #include <corosync/corotypes.h>
  43. #include <corosync/corodefs.h>
  44. #include <corosync/hdb.h>
  45. #include <qb/qbipcc.h>
  46. #include <corosync/cmap.h>
  47. #include <corosync/ipc_cmap.h>
  48. #include "util.h"
  49. #include <stdio.h>
  50. struct cmap_inst {
  51. int finalize;
  52. qb_ipcc_connection_t *c;
  53. const void *context;
  54. };
  55. struct cmap_track_inst {
  56. void *user_data;
  57. cmap_notify_fn_t notify_fn;
  58. qb_ipcc_connection_t *c;
  59. cmap_track_handle_t track_handle;
  60. };
  61. static void cmap_inst_free (void *inst);
  62. DECLARE_HDB_DATABASE(cmap_handle_t_db, cmap_inst_free);
  63. DECLARE_HDB_DATABASE(cmap_track_handle_t_db,NULL);
  64. /*
  65. * Function prototypes
  66. */
  67. static cs_error_t cmap_get_int(
  68. cmap_handle_t handle,
  69. const char *key_name,
  70. void *value,
  71. size_t value_size,
  72. cmap_value_types_t type);
  73. static cs_error_t cmap_adjust_int(cmap_handle_t handle, const char *key_name, int32_t step);
  74. /*
  75. * Function implementations
  76. */
  77. cs_error_t cmap_initialize (cmap_handle_t *handle)
  78. {
  79. cs_error_t error;
  80. struct cmap_inst *cmap_inst;
  81. error = hdb_error_to_cs(hdb_handle_create(&cmap_handle_t_db, sizeof(*cmap_inst), handle));
  82. if (error != CS_OK) {
  83. goto error_no_destroy;
  84. }
  85. error = hdb_error_to_cs(hdb_handle_get(&cmap_handle_t_db, *handle, (void *)&cmap_inst));
  86. if (error != CS_OK) {
  87. goto error_destroy;
  88. }
  89. error = CS_OK;
  90. cmap_inst->finalize = 0;
  91. cmap_inst->c = qb_ipcc_connect("cmap", IPC_REQUEST_SIZE);
  92. if (cmap_inst->c == NULL) {
  93. error = qb_to_cs_error(-errno);
  94. goto error_put_destroy;
  95. }
  96. (void)hdb_handle_put(&cmap_handle_t_db, *handle);
  97. return (CS_OK);
  98. error_put_destroy:
  99. (void)hdb_handle_put(&cmap_handle_t_db, *handle);
  100. error_destroy:
  101. (void)hdb_handle_destroy(&cmap_handle_t_db, *handle);
  102. error_no_destroy:
  103. return (error);
  104. }
  105. cs_error_t cmap_initialize_map (cmap_handle_t *handle,
  106. cmap_map_t map)
  107. {
  108. cs_error_t error;
  109. struct iovec iov[1];
  110. struct cmap_inst *cmap_inst;
  111. struct req_lib_cmap_set_current_map req_lib_cmap_set_current_map;
  112. struct qb_ipc_response_header res_lib_cmap_set_current_map;
  113. error = cmap_initialize(handle);
  114. if (error == CS_OK) {
  115. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, *handle, (void *)&cmap_inst));
  116. if (error != CS_OK) {
  117. return (error);
  118. }
  119. memset(&req_lib_cmap_set_current_map, 0, sizeof(req_lib_cmap_set_current_map));
  120. req_lib_cmap_set_current_map.header.size = sizeof(req_lib_cmap_set_current_map);
  121. req_lib_cmap_set_current_map.header.id = MESSAGE_REQ_CMAP_SET_CURRENT_MAP;
  122. req_lib_cmap_set_current_map.map = map;
  123. iov[0].iov_base = (char *)&req_lib_cmap_set_current_map;
  124. iov[0].iov_len = sizeof(req_lib_cmap_set_current_map);
  125. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  126. cmap_inst->c,
  127. iov,
  128. 1,
  129. &res_lib_cmap_set_current_map,
  130. sizeof (res_lib_cmap_set_current_map), CS_IPC_TIMEOUT_MS));
  131. if (error == CS_OK) {
  132. error = res_lib_cmap_set_current_map.error;
  133. }
  134. (void)hdb_handle_put (&cmap_handle_t_db, *handle);
  135. return (error);
  136. }
  137. return (error);
  138. }
  139. static void cmap_inst_free (void *inst)
  140. {
  141. struct cmap_inst *cmap_inst = (struct cmap_inst *)inst;
  142. qb_ipcc_disconnect(cmap_inst->c);
  143. }
  144. cs_error_t cmap_finalize(cmap_handle_t handle)
  145. {
  146. struct cmap_inst *cmap_inst;
  147. cs_error_t error;
  148. hdb_handle_t track_inst_handle = 0;
  149. struct cmap_track_inst *cmap_track_inst;
  150. error = hdb_error_to_cs(hdb_handle_get(&cmap_handle_t_db, handle, (void *)&cmap_inst));
  151. if (error != CS_OK) {
  152. return (error);
  153. }
  154. if (cmap_inst->finalize) {
  155. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  156. return (CS_ERR_BAD_HANDLE);
  157. }
  158. cmap_inst->finalize = 1;
  159. /*
  160. * Destroy all track instances for given connection
  161. */
  162. hdb_iterator_reset(&cmap_track_handle_t_db);
  163. while (hdb_iterator_next(&cmap_track_handle_t_db,
  164. (void*)&cmap_track_inst, &track_inst_handle) == 0) {
  165. if (cmap_track_inst->c == cmap_inst->c) {
  166. (void)hdb_handle_destroy(&cmap_track_handle_t_db, track_inst_handle);
  167. }
  168. (void)hdb_handle_put (&cmap_track_handle_t_db, track_inst_handle);
  169. }
  170. (void)hdb_handle_destroy(&cmap_handle_t_db, handle);
  171. (void)hdb_handle_put(&cmap_handle_t_db, handle);
  172. return (CS_OK);
  173. }
  174. cs_error_t cmap_fd_get(cmap_handle_t handle, int *fd)
  175. {
  176. cs_error_t error;
  177. struct cmap_inst *cmap_inst;
  178. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  179. if (error != CS_OK) {
  180. return (error);
  181. }
  182. error = qb_to_cs_error (qb_ipcc_fd_get (cmap_inst->c, fd));
  183. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  184. return (error);
  185. }
  186. cs_error_t cmap_dispatch (
  187. cmap_handle_t handle,
  188. cs_dispatch_flags_t dispatch_types)
  189. {
  190. int timeout = -1;
  191. cs_error_t error;
  192. int cont = 1; /* always continue do loop except when set to 0 */
  193. struct cmap_inst *cmap_inst;
  194. struct qb_ipc_response_header *dispatch_data;
  195. char dispatch_buf[IPC_DISPATCH_SIZE];
  196. struct res_lib_cmap_notify_callback *res_lib_cmap_notify_callback;
  197. struct cmap_track_inst *cmap_track_inst;
  198. struct cmap_notify_value old_val;
  199. struct cmap_notify_value new_val;
  200. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  201. if (error != CS_OK) {
  202. return (error);
  203. }
  204. /*
  205. * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
  206. * wait indefinately for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
  207. */
  208. if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  209. timeout = 0;
  210. }
  211. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  212. do {
  213. error = qb_to_cs_error(qb_ipcc_event_recv (
  214. cmap_inst->c,
  215. dispatch_buf,
  216. IPC_DISPATCH_SIZE,
  217. timeout));
  218. if (error == CS_ERR_BAD_HANDLE) {
  219. error = CS_OK;
  220. goto error_put;
  221. }
  222. if (error == CS_ERR_TRY_AGAIN) {
  223. if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  224. /*
  225. * Don't mask error
  226. */
  227. goto error_put;
  228. }
  229. error = CS_OK;
  230. if (dispatch_types == CS_DISPATCH_ALL) {
  231. break; /* exit do while cont is 1 loop */
  232. } else {
  233. continue; /* next poll */
  234. }
  235. }
  236. if (error != CS_OK) {
  237. goto error_put;
  238. }
  239. /*
  240. * Dispatch incoming message
  241. */
  242. switch (dispatch_data->id) {
  243. case MESSAGE_RES_CMAP_NOTIFY_CALLBACK:
  244. res_lib_cmap_notify_callback = (struct res_lib_cmap_notify_callback *)dispatch_data;
  245. error = hdb_error_to_cs(hdb_handle_get(&cmap_track_handle_t_db,
  246. res_lib_cmap_notify_callback->track_inst_handle,
  247. (void *)&cmap_track_inst));
  248. if (error == CS_ERR_BAD_HANDLE) {
  249. /*
  250. * User deleted tracker -> ignore error
  251. */
  252. break;
  253. }
  254. if (error != CS_OK) {
  255. goto error_put;
  256. }
  257. new_val.type = res_lib_cmap_notify_callback->new_value_type;
  258. old_val.type = res_lib_cmap_notify_callback->old_value_type;
  259. new_val.len = res_lib_cmap_notify_callback->new_value_len;
  260. old_val.len = res_lib_cmap_notify_callback->old_value_len;
  261. new_val.data = res_lib_cmap_notify_callback->new_value;
  262. old_val.data = (((const char *)res_lib_cmap_notify_callback->new_value) + new_val.len);
  263. cmap_track_inst->notify_fn(handle,
  264. cmap_track_inst->track_handle,
  265. res_lib_cmap_notify_callback->event,
  266. (char *)res_lib_cmap_notify_callback->key_name.value,
  267. new_val,
  268. old_val,
  269. cmap_track_inst->user_data);
  270. (void)hdb_handle_put(&cmap_track_handle_t_db, res_lib_cmap_notify_callback->track_inst_handle);
  271. break;
  272. default:
  273. error = CS_ERR_LIBRARY;
  274. goto error_put;
  275. break;
  276. }
  277. if (cmap_inst->finalize) {
  278. /*
  279. * If the finalize has been called then get out of the dispatch.
  280. */
  281. error = CS_ERR_BAD_HANDLE;
  282. goto error_put;
  283. }
  284. /*
  285. * Determine if more messages should be processed
  286. */
  287. if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  288. cont = 0;
  289. }
  290. } while (cont);
  291. error_put:
  292. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  293. return (error);
  294. }
  295. cs_error_t cmap_context_get (
  296. cmap_handle_t handle,
  297. const void **context)
  298. {
  299. cs_error_t error;
  300. struct cmap_inst *cmap_inst;
  301. error = hdb_error_to_cs(hdb_handle_get(&cmap_handle_t_db, handle, (void *)&cmap_inst));
  302. if (error != CS_OK) {
  303. return (error);
  304. }
  305. *context = cmap_inst->context;
  306. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  307. return (CS_OK);
  308. }
  309. cs_error_t cmap_context_set (
  310. cmap_handle_t handle,
  311. const void *context)
  312. {
  313. cs_error_t error;
  314. struct cmap_inst *cmap_inst;
  315. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  316. if (error != CS_OK) {
  317. return (error);
  318. }
  319. cmap_inst->context = context;
  320. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  321. return (CS_OK);
  322. }
  323. cs_error_t cmap_set (
  324. cmap_handle_t handle,
  325. const char *key_name,
  326. const void *value,
  327. size_t value_len,
  328. cmap_value_types_t type)
  329. {
  330. cs_error_t error;
  331. struct iovec iov[2];
  332. struct cmap_inst *cmap_inst;
  333. struct req_lib_cmap_set req_lib_cmap_set;
  334. struct res_lib_cmap_set res_lib_cmap_set;
  335. if (key_name == NULL || value == NULL) {
  336. return (CS_ERR_INVALID_PARAM);
  337. }
  338. if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
  339. return (CS_ERR_NAME_TOO_LONG);
  340. }
  341. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  342. if (error != CS_OK) {
  343. return (error);
  344. }
  345. memset(&req_lib_cmap_set, 0, sizeof(req_lib_cmap_set));
  346. req_lib_cmap_set.header.size = sizeof(req_lib_cmap_set) + value_len;
  347. req_lib_cmap_set.header.id = MESSAGE_REQ_CMAP_SET;
  348. memcpy(req_lib_cmap_set.key_name.value, key_name, strlen(key_name));
  349. req_lib_cmap_set.key_name.length = strlen(key_name);
  350. req_lib_cmap_set.value_len = value_len;
  351. req_lib_cmap_set.type = type;
  352. iov[0].iov_base = (char *)&req_lib_cmap_set;
  353. iov[0].iov_len = sizeof(req_lib_cmap_set);
  354. iov[1].iov_base = (void *)value;
  355. iov[1].iov_len = value_len;
  356. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  357. cmap_inst->c,
  358. iov,
  359. 2,
  360. &res_lib_cmap_set,
  361. sizeof (struct res_lib_cmap_set), CS_IPC_TIMEOUT_MS));
  362. if (error == CS_OK) {
  363. error = res_lib_cmap_set.header.error;
  364. }
  365. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  366. return (error);
  367. }
  368. cs_error_t cmap_set_int8(cmap_handle_t handle, const char *key_name, int8_t value)
  369. {
  370. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_INT8));
  371. }
  372. cs_error_t cmap_set_uint8(cmap_handle_t handle, const char *key_name, uint8_t value)
  373. {
  374. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_UINT8));
  375. }
  376. cs_error_t cmap_set_int16(cmap_handle_t handle, const char *key_name, int16_t value)
  377. {
  378. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_INT16));
  379. }
  380. cs_error_t cmap_set_uint16(cmap_handle_t handle, const char *key_name, uint16_t value)
  381. {
  382. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_UINT16));
  383. }
  384. cs_error_t cmap_set_int32(cmap_handle_t handle, const char *key_name, int32_t value)
  385. {
  386. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_INT32));
  387. }
  388. cs_error_t cmap_set_uint32(cmap_handle_t handle, const char *key_name, uint32_t value)
  389. {
  390. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_UINT32));
  391. }
  392. cs_error_t cmap_set_int64(cmap_handle_t handle, const char *key_name, int64_t value)
  393. {
  394. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_INT64));
  395. }
  396. cs_error_t cmap_set_uint64(cmap_handle_t handle, const char *key_name, uint64_t value)
  397. {
  398. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_UINT64));
  399. }
  400. cs_error_t cmap_set_float(cmap_handle_t handle, const char *key_name, float value)
  401. {
  402. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_FLOAT));
  403. }
  404. cs_error_t cmap_set_double(cmap_handle_t handle, const char *key_name, double value)
  405. {
  406. return (cmap_set(handle, key_name, &value, sizeof(value), CMAP_VALUETYPE_DOUBLE));
  407. }
  408. cs_error_t cmap_set_string(cmap_handle_t handle, const char *key_name, const char *value)
  409. {
  410. if (value == NULL) {
  411. return (CS_ERR_INVALID_PARAM);
  412. }
  413. return (cmap_set(handle, key_name, value, strlen(value), CMAP_VALUETYPE_STRING));
  414. }
  415. cs_error_t cmap_delete(cmap_handle_t handle, const char *key_name)
  416. {
  417. cs_error_t error;
  418. struct iovec iov;
  419. struct cmap_inst *cmap_inst;
  420. struct req_lib_cmap_delete req_lib_cmap_delete;
  421. struct res_lib_cmap_delete res_lib_cmap_delete;
  422. if (key_name == NULL) {
  423. return (CS_ERR_INVALID_PARAM);
  424. }
  425. if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
  426. return (CS_ERR_NAME_TOO_LONG);
  427. }
  428. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  429. if (error != CS_OK) {
  430. return (error);
  431. }
  432. memset(&req_lib_cmap_delete, 0, sizeof(req_lib_cmap_delete));
  433. req_lib_cmap_delete.header.size = sizeof(req_lib_cmap_delete);
  434. req_lib_cmap_delete.header.id = MESSAGE_REQ_CMAP_DELETE;
  435. memcpy(req_lib_cmap_delete.key_name.value, key_name, strlen(key_name));
  436. req_lib_cmap_delete.key_name.length = strlen(key_name);
  437. iov.iov_base = (char *)&req_lib_cmap_delete;
  438. iov.iov_len = sizeof(req_lib_cmap_delete);
  439. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  440. cmap_inst->c,
  441. &iov,
  442. 1,
  443. &res_lib_cmap_delete,
  444. sizeof (struct res_lib_cmap_delete), CS_IPC_TIMEOUT_MS));
  445. if (error == CS_OK) {
  446. error = res_lib_cmap_delete.header.error;
  447. }
  448. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  449. return (error);
  450. }
  451. cs_error_t cmap_get(
  452. cmap_handle_t handle,
  453. const char *key_name,
  454. void *value,
  455. size_t *value_len,
  456. cmap_value_types_t *type)
  457. {
  458. cs_error_t error;
  459. struct cmap_inst *cmap_inst;
  460. struct iovec iov;
  461. struct req_lib_cmap_get req_lib_cmap_get;
  462. struct res_lib_cmap_get *res_lib_cmap_get;
  463. size_t res_size;
  464. if (key_name == NULL) {
  465. return (CS_ERR_INVALID_PARAM);
  466. }
  467. if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
  468. return (CS_ERR_NAME_TOO_LONG);
  469. }
  470. if (value != NULL && value_len == NULL) {
  471. return (CS_ERR_INVALID_PARAM);
  472. }
  473. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  474. if (error != CS_OK) {
  475. return (error);
  476. }
  477. memset(&req_lib_cmap_get, 0, sizeof(req_lib_cmap_get));
  478. req_lib_cmap_get.header.size = sizeof(req_lib_cmap_get);
  479. req_lib_cmap_get.header.id = MESSAGE_REQ_CMAP_GET;
  480. memcpy(req_lib_cmap_get.key_name.value, key_name, strlen(key_name));
  481. req_lib_cmap_get.key_name.length = strlen(key_name);
  482. if (value != NULL && value_len != NULL) {
  483. req_lib_cmap_get.value_len = *value_len;
  484. } else {
  485. req_lib_cmap_get.value_len = 0;
  486. }
  487. iov.iov_base = (char *)&req_lib_cmap_get;
  488. iov.iov_len = sizeof(req_lib_cmap_get);
  489. res_size = sizeof(struct res_lib_cmap_get) + req_lib_cmap_get.value_len;
  490. res_lib_cmap_get = malloc(res_size);
  491. if (res_lib_cmap_get == NULL) {
  492. return (CS_ERR_NO_MEMORY);
  493. }
  494. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  495. cmap_inst->c,
  496. &iov,
  497. 1,
  498. res_lib_cmap_get,
  499. res_size, CS_IPC_TIMEOUT_MS));
  500. if (error == CS_OK) {
  501. error = res_lib_cmap_get->header.error;
  502. }
  503. if (error == CS_OK) {
  504. if (type != NULL) {
  505. *type = res_lib_cmap_get->type;
  506. }
  507. if (value_len != NULL) {
  508. *value_len = res_lib_cmap_get->value_len;
  509. }
  510. if (value != NULL && value_len != NULL) {
  511. memcpy(value, res_lib_cmap_get->value, res_lib_cmap_get->value_len);
  512. }
  513. }
  514. free(res_lib_cmap_get);
  515. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  516. return (error);
  517. }
  518. static cs_error_t cmap_get_int(
  519. cmap_handle_t handle,
  520. const char *key_name,
  521. void *value,
  522. size_t value_size,
  523. cmap_value_types_t type)
  524. {
  525. char key_value[16];
  526. size_t key_size;
  527. cs_error_t err;
  528. cmap_value_types_t key_type;
  529. key_size = sizeof(key_value);
  530. memset(key_value, 0, key_size);
  531. err = cmap_get(handle, key_name, key_value, &key_size, &key_type);
  532. if (err != CS_OK)
  533. return (err);
  534. if (key_type != type) {
  535. return (CS_ERR_INVALID_PARAM);
  536. }
  537. memcpy(value, key_value, value_size);
  538. return (CS_OK);
  539. }
  540. cs_error_t cmap_get_int8(cmap_handle_t handle, const char *key_name, int8_t *i8)
  541. {
  542. return (cmap_get_int(handle, key_name, i8, sizeof(*i8), CMAP_VALUETYPE_INT8));
  543. }
  544. cs_error_t cmap_get_uint8(cmap_handle_t handle, const char *key_name, uint8_t *u8)
  545. {
  546. return (cmap_get_int(handle, key_name, u8, sizeof(*u8), CMAP_VALUETYPE_UINT8));
  547. }
  548. cs_error_t cmap_get_int16(cmap_handle_t handle, const char *key_name, int16_t *i16)
  549. {
  550. return (cmap_get_int(handle, key_name, i16, sizeof(*i16), CMAP_VALUETYPE_INT16));
  551. }
  552. cs_error_t cmap_get_uint16(cmap_handle_t handle, const char *key_name, uint16_t *u16)
  553. {
  554. return (cmap_get_int(handle, key_name, u16, sizeof(*u16), CMAP_VALUETYPE_UINT16));
  555. }
  556. cs_error_t cmap_get_int32(cmap_handle_t handle, const char *key_name, int32_t *i32)
  557. {
  558. return (cmap_get_int(handle, key_name, i32, sizeof(*i32), CMAP_VALUETYPE_INT32));
  559. }
  560. cs_error_t cmap_get_uint32(cmap_handle_t handle, const char *key_name, uint32_t *u32)
  561. {
  562. return (cmap_get_int(handle, key_name, u32, sizeof(*u32), CMAP_VALUETYPE_UINT32));
  563. }
  564. cs_error_t cmap_get_int64(cmap_handle_t handle, const char *key_name, int64_t *i64)
  565. {
  566. return (cmap_get_int(handle, key_name, i64, sizeof(*i64), CMAP_VALUETYPE_INT64));
  567. }
  568. cs_error_t cmap_get_uint64(cmap_handle_t handle, const char *key_name, uint64_t *u64)
  569. {
  570. return (cmap_get_int(handle, key_name, u64, sizeof(*u64), CMAP_VALUETYPE_UINT64));
  571. }
  572. cs_error_t cmap_get_float(cmap_handle_t handle, const char *key_name, float *flt)
  573. {
  574. return (cmap_get_int(handle, key_name, flt, sizeof(*flt), CMAP_VALUETYPE_FLOAT));
  575. }
  576. cs_error_t cmap_get_double(cmap_handle_t handle, const char *key_name, double *dbl)
  577. {
  578. return (cmap_get_int(handle, key_name, dbl, sizeof(*dbl), CMAP_VALUETYPE_DOUBLE));
  579. }
  580. cs_error_t cmap_get_string(cmap_handle_t handle, const char *key_name, char **str)
  581. {
  582. cs_error_t res;
  583. size_t str_len;
  584. cmap_value_types_t type;
  585. res = cmap_get(handle, key_name, NULL, &str_len, &type);
  586. if (res != CS_OK || type != CMAP_VALUETYPE_STRING) {
  587. if (res == CS_OK) {
  588. res = CS_ERR_INVALID_PARAM;
  589. }
  590. goto return_error;
  591. }
  592. *str = malloc(str_len);
  593. if (*str == NULL) {
  594. res = CS_ERR_NO_MEMORY;
  595. goto return_error;
  596. }
  597. res = cmap_get(handle, key_name, *str, &str_len, &type);
  598. if (res != CS_OK) {
  599. free(*str);
  600. goto return_error;
  601. }
  602. return (CS_OK);
  603. return_error:
  604. return (res);
  605. }
  606. static cs_error_t cmap_adjust_int(cmap_handle_t handle, const char *key_name, int32_t step)
  607. {
  608. cs_error_t error;
  609. struct iovec iov;
  610. struct cmap_inst *cmap_inst;
  611. struct req_lib_cmap_adjust_int req_lib_cmap_adjust_int;
  612. struct res_lib_cmap_adjust_int res_lib_cmap_adjust_int;
  613. if (key_name == NULL) {
  614. return (CS_ERR_INVALID_PARAM);
  615. }
  616. if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
  617. return (CS_ERR_NAME_TOO_LONG);
  618. }
  619. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  620. if (error != CS_OK) {
  621. return (error);
  622. }
  623. memset(&req_lib_cmap_adjust_int, 0, sizeof(req_lib_cmap_adjust_int));
  624. req_lib_cmap_adjust_int.header.size = sizeof(req_lib_cmap_adjust_int);
  625. req_lib_cmap_adjust_int.header.id = MESSAGE_REQ_CMAP_ADJUST_INT;
  626. memcpy(req_lib_cmap_adjust_int.key_name.value, key_name, strlen(key_name));
  627. req_lib_cmap_adjust_int.key_name.length = strlen(key_name);
  628. req_lib_cmap_adjust_int.step = step;
  629. iov.iov_base = (char *)&req_lib_cmap_adjust_int;
  630. iov.iov_len = sizeof(req_lib_cmap_adjust_int);
  631. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  632. cmap_inst->c,
  633. &iov,
  634. 1,
  635. &res_lib_cmap_adjust_int,
  636. sizeof (struct res_lib_cmap_adjust_int), CS_IPC_TIMEOUT_MS));
  637. if (error == CS_OK) {
  638. error = res_lib_cmap_adjust_int.header.error;
  639. }
  640. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  641. return (error);
  642. }
  643. cs_error_t cmap_inc(cmap_handle_t handle, const char *key_name)
  644. {
  645. return (cmap_adjust_int(handle, key_name, 1));
  646. }
  647. cs_error_t cmap_dec(cmap_handle_t handle, const char *key_name)
  648. {
  649. return (cmap_adjust_int(handle, key_name, -1));
  650. }
  651. cs_error_t cmap_iter_init(
  652. cmap_handle_t handle,
  653. const char *prefix,
  654. cmap_iter_handle_t *cmap_iter_handle)
  655. {
  656. cs_error_t error;
  657. struct iovec iov;
  658. struct cmap_inst *cmap_inst;
  659. struct req_lib_cmap_iter_init req_lib_cmap_iter_init;
  660. struct res_lib_cmap_iter_init res_lib_cmap_iter_init;
  661. if (cmap_iter_handle == NULL) {
  662. return (CS_ERR_INVALID_PARAM);
  663. }
  664. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  665. if (error != CS_OK) {
  666. return (error);
  667. }
  668. memset(&req_lib_cmap_iter_init, 0, sizeof(req_lib_cmap_iter_init));
  669. req_lib_cmap_iter_init.header.size = sizeof(req_lib_cmap_iter_init);
  670. req_lib_cmap_iter_init.header.id = MESSAGE_REQ_CMAP_ITER_INIT;
  671. if (prefix) {
  672. if (strlen(prefix) >= CS_MAX_NAME_LENGTH) {
  673. return (CS_ERR_NAME_TOO_LONG);
  674. }
  675. memcpy(req_lib_cmap_iter_init.prefix.value, prefix, strlen(prefix));
  676. req_lib_cmap_iter_init.prefix.length = strlen(prefix);
  677. }
  678. iov.iov_base = (char *)&req_lib_cmap_iter_init;
  679. iov.iov_len = sizeof(req_lib_cmap_iter_init);
  680. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  681. cmap_inst->c,
  682. &iov,
  683. 1,
  684. &res_lib_cmap_iter_init,
  685. sizeof (struct res_lib_cmap_iter_init), CS_IPC_TIMEOUT_MS));
  686. if (error == CS_OK) {
  687. error = res_lib_cmap_iter_init.header.error;
  688. }
  689. if (error == CS_OK) {
  690. *cmap_iter_handle = res_lib_cmap_iter_init.iter_handle;
  691. }
  692. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  693. return (error);
  694. }
  695. cs_error_t cmap_iter_next(
  696. cmap_handle_t handle,
  697. cmap_iter_handle_t iter_handle,
  698. char key_name[],
  699. size_t *value_len,
  700. cmap_value_types_t *type)
  701. {
  702. cs_error_t error;
  703. struct iovec iov;
  704. struct cmap_inst *cmap_inst;
  705. struct req_lib_cmap_iter_next req_lib_cmap_iter_next;
  706. struct res_lib_cmap_iter_next res_lib_cmap_iter_next;
  707. if (key_name == NULL) {
  708. return (CS_ERR_INVALID_PARAM);
  709. }
  710. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  711. if (error != CS_OK) {
  712. return (error);
  713. }
  714. memset(&req_lib_cmap_iter_next, 0, sizeof(req_lib_cmap_iter_next));
  715. req_lib_cmap_iter_next.header.size = sizeof(req_lib_cmap_iter_next);
  716. req_lib_cmap_iter_next.header.id = MESSAGE_REQ_CMAP_ITER_NEXT;
  717. req_lib_cmap_iter_next.iter_handle = iter_handle;
  718. iov.iov_base = (char *)&req_lib_cmap_iter_next;
  719. iov.iov_len = sizeof(req_lib_cmap_iter_next);
  720. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  721. cmap_inst->c,
  722. &iov,
  723. 1,
  724. &res_lib_cmap_iter_next,
  725. sizeof (struct res_lib_cmap_iter_next), CS_IPC_TIMEOUT_MS));
  726. if (error == CS_OK) {
  727. error = res_lib_cmap_iter_next.header.error;
  728. }
  729. if (error == CS_OK) {
  730. memcpy(key_name, (const char *)res_lib_cmap_iter_next.key_name.value,
  731. res_lib_cmap_iter_next.key_name.length);
  732. key_name[res_lib_cmap_iter_next.key_name.length] = '\0';
  733. if (value_len != NULL) {
  734. *value_len = res_lib_cmap_iter_next.value_len;
  735. }
  736. if (type != NULL) {
  737. *type = res_lib_cmap_iter_next.type;
  738. }
  739. }
  740. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  741. return (error);
  742. }
  743. cs_error_t cmap_iter_finalize(
  744. cmap_handle_t handle,
  745. cmap_iter_handle_t iter_handle)
  746. {
  747. cs_error_t error;
  748. struct iovec iov;
  749. struct cmap_inst *cmap_inst;
  750. struct req_lib_cmap_iter_finalize req_lib_cmap_iter_finalize;
  751. struct res_lib_cmap_iter_finalize res_lib_cmap_iter_finalize;
  752. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  753. if (error != CS_OK) {
  754. return (error);
  755. }
  756. memset(&req_lib_cmap_iter_finalize, 0, sizeof(req_lib_cmap_iter_finalize));
  757. req_lib_cmap_iter_finalize.header.size = sizeof(req_lib_cmap_iter_finalize);
  758. req_lib_cmap_iter_finalize.header.id = MESSAGE_REQ_CMAP_ITER_FINALIZE;
  759. req_lib_cmap_iter_finalize.iter_handle = iter_handle;
  760. iov.iov_base = (char *)&req_lib_cmap_iter_finalize;
  761. iov.iov_len = sizeof(req_lib_cmap_iter_finalize);
  762. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  763. cmap_inst->c,
  764. &iov,
  765. 1,
  766. &res_lib_cmap_iter_finalize,
  767. sizeof (struct res_lib_cmap_iter_finalize), CS_IPC_TIMEOUT_MS));
  768. if (error == CS_OK) {
  769. error = res_lib_cmap_iter_finalize.header.error;
  770. }
  771. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  772. return (error);
  773. }
  774. cs_error_t cmap_track_add(
  775. cmap_handle_t handle,
  776. const char *key_name,
  777. int32_t track_type,
  778. cmap_notify_fn_t notify_fn,
  779. void *user_data,
  780. cmap_track_handle_t *cmap_track_handle)
  781. {
  782. cs_error_t error;
  783. struct iovec iov;
  784. struct cmap_inst *cmap_inst;
  785. struct req_lib_cmap_track_add req_lib_cmap_track_add;
  786. struct res_lib_cmap_track_add res_lib_cmap_track_add;
  787. struct cmap_track_inst *cmap_track_inst;
  788. cmap_track_handle_t cmap_track_inst_handle;
  789. if (cmap_track_handle == NULL || notify_fn == NULL) {
  790. return (CS_ERR_INVALID_PARAM);
  791. }
  792. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  793. if (error != CS_OK) {
  794. return (error);
  795. }
  796. error = hdb_error_to_cs(hdb_handle_create(&cmap_track_handle_t_db,
  797. sizeof(*cmap_track_inst), &cmap_track_inst_handle));
  798. if (error != CS_OK) {
  799. goto error_put;
  800. }
  801. error = hdb_error_to_cs(hdb_handle_get(&cmap_track_handle_t_db,
  802. cmap_track_inst_handle, (void *)&cmap_track_inst));
  803. if (error != CS_OK) {
  804. goto error_put_destroy;
  805. }
  806. cmap_track_inst->user_data = user_data;
  807. cmap_track_inst->notify_fn = notify_fn;
  808. cmap_track_inst->c = cmap_inst->c;
  809. memset(&req_lib_cmap_track_add, 0, sizeof(req_lib_cmap_track_add));
  810. req_lib_cmap_track_add.header.size = sizeof(req_lib_cmap_track_add);
  811. req_lib_cmap_track_add.header.id = MESSAGE_REQ_CMAP_TRACK_ADD;
  812. if (key_name) {
  813. if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
  814. return (CS_ERR_NAME_TOO_LONG);
  815. }
  816. memcpy(req_lib_cmap_track_add.key_name.value, key_name, strlen(key_name));
  817. req_lib_cmap_track_add.key_name.length = strlen(key_name);
  818. }
  819. req_lib_cmap_track_add.track_type = track_type;
  820. req_lib_cmap_track_add.track_inst_handle = cmap_track_inst_handle;
  821. iov.iov_base = (char *)&req_lib_cmap_track_add;
  822. iov.iov_len = sizeof(req_lib_cmap_track_add);
  823. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  824. cmap_inst->c,
  825. &iov,
  826. 1,
  827. &res_lib_cmap_track_add,
  828. sizeof (struct res_lib_cmap_track_add), CS_IPC_TIMEOUT_MS));
  829. if (error == CS_OK) {
  830. error = res_lib_cmap_track_add.header.error;
  831. }
  832. if (error == CS_OK) {
  833. *cmap_track_handle = res_lib_cmap_track_add.track_handle;
  834. cmap_track_inst->track_handle = *cmap_track_handle;
  835. }
  836. (void)hdb_handle_put (&cmap_track_handle_t_db, cmap_track_inst_handle);
  837. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  838. return (error);
  839. error_put_destroy:
  840. (void)hdb_handle_put (&cmap_track_handle_t_db, cmap_track_inst_handle);
  841. (void)hdb_handle_destroy (&cmap_track_handle_t_db, cmap_track_inst_handle);
  842. error_put:
  843. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  844. return (error);
  845. }
  846. cs_error_t cmap_track_delete(
  847. cmap_handle_t handle,
  848. cmap_track_handle_t track_handle)
  849. {
  850. cs_error_t error;
  851. struct iovec iov;
  852. struct cmap_inst *cmap_inst;
  853. struct cmap_track_inst *cmap_track_inst;
  854. struct req_lib_cmap_track_delete req_lib_cmap_track_delete;
  855. struct res_lib_cmap_track_delete res_lib_cmap_track_delete;
  856. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  857. if (error != CS_OK) {
  858. return (error);
  859. }
  860. memset(&req_lib_cmap_track_delete, 0, sizeof(req_lib_cmap_track_delete));
  861. req_lib_cmap_track_delete.header.size = sizeof(req_lib_cmap_track_delete);
  862. req_lib_cmap_track_delete.header.id = MESSAGE_REQ_CMAP_TRACK_DELETE;
  863. req_lib_cmap_track_delete.track_handle = track_handle;
  864. iov.iov_base = (char *)&req_lib_cmap_track_delete;
  865. iov.iov_len = sizeof(req_lib_cmap_track_delete);
  866. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  867. cmap_inst->c,
  868. &iov,
  869. 1,
  870. &res_lib_cmap_track_delete,
  871. sizeof (struct res_lib_cmap_track_delete), CS_IPC_TIMEOUT_MS));
  872. if (error == CS_OK) {
  873. error = res_lib_cmap_track_delete.header.error;
  874. }
  875. if (error == CS_OK) {
  876. error = hdb_error_to_cs(hdb_handle_get(&cmap_track_handle_t_db,
  877. res_lib_cmap_track_delete.track_inst_handle,
  878. (void *)&cmap_track_inst));
  879. if (error != CS_OK) {
  880. goto error_put;
  881. }
  882. (void)hdb_handle_put(&cmap_track_handle_t_db, res_lib_cmap_track_delete.track_inst_handle);
  883. (void)hdb_handle_destroy(&cmap_track_handle_t_db, res_lib_cmap_track_delete.track_inst_handle);
  884. }
  885. error_put:
  886. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  887. return (error);
  888. }