cmap.c 27 KB

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