cmap.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. strncpy(key_name, (const char *)res_lib_cmap_iter_next.key_name.value, CMAP_KEYNAME_MAXLEN);
  731. if (value_len != NULL) {
  732. *value_len = res_lib_cmap_iter_next.value_len;
  733. }
  734. if (type != NULL) {
  735. *type = res_lib_cmap_iter_next.type;
  736. }
  737. }
  738. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  739. return (error);
  740. }
  741. cs_error_t cmap_iter_finalize(
  742. cmap_handle_t handle,
  743. cmap_iter_handle_t iter_handle)
  744. {
  745. cs_error_t error;
  746. struct iovec iov;
  747. struct cmap_inst *cmap_inst;
  748. struct req_lib_cmap_iter_finalize req_lib_cmap_iter_finalize;
  749. struct res_lib_cmap_iter_finalize res_lib_cmap_iter_finalize;
  750. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  751. if (error != CS_OK) {
  752. return (error);
  753. }
  754. memset(&req_lib_cmap_iter_finalize, 0, sizeof(req_lib_cmap_iter_finalize));
  755. req_lib_cmap_iter_finalize.header.size = sizeof(req_lib_cmap_iter_finalize);
  756. req_lib_cmap_iter_finalize.header.id = MESSAGE_REQ_CMAP_ITER_FINALIZE;
  757. req_lib_cmap_iter_finalize.iter_handle = iter_handle;
  758. iov.iov_base = (char *)&req_lib_cmap_iter_finalize;
  759. iov.iov_len = sizeof(req_lib_cmap_iter_finalize);
  760. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  761. cmap_inst->c,
  762. &iov,
  763. 1,
  764. &res_lib_cmap_iter_finalize,
  765. sizeof (struct res_lib_cmap_iter_finalize), CS_IPC_TIMEOUT_MS));
  766. if (error == CS_OK) {
  767. error = res_lib_cmap_iter_finalize.header.error;
  768. }
  769. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  770. return (error);
  771. }
  772. cs_error_t cmap_track_add(
  773. cmap_handle_t handle,
  774. const char *key_name,
  775. int32_t track_type,
  776. cmap_notify_fn_t notify_fn,
  777. void *user_data,
  778. cmap_track_handle_t *cmap_track_handle)
  779. {
  780. cs_error_t error;
  781. struct iovec iov;
  782. struct cmap_inst *cmap_inst;
  783. struct req_lib_cmap_track_add req_lib_cmap_track_add;
  784. struct res_lib_cmap_track_add res_lib_cmap_track_add;
  785. struct cmap_track_inst *cmap_track_inst;
  786. cmap_track_handle_t cmap_track_inst_handle;
  787. if (cmap_track_handle == NULL || notify_fn == NULL) {
  788. return (CS_ERR_INVALID_PARAM);
  789. }
  790. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  791. if (error != CS_OK) {
  792. return (error);
  793. }
  794. error = hdb_error_to_cs(hdb_handle_create(&cmap_track_handle_t_db,
  795. sizeof(*cmap_track_inst), &cmap_track_inst_handle));
  796. if (error != CS_OK) {
  797. goto error_put;
  798. }
  799. error = hdb_error_to_cs(hdb_handle_get(&cmap_track_handle_t_db,
  800. cmap_track_inst_handle, (void *)&cmap_track_inst));
  801. if (error != CS_OK) {
  802. goto error_put_destroy;
  803. }
  804. cmap_track_inst->user_data = user_data;
  805. cmap_track_inst->notify_fn = notify_fn;
  806. cmap_track_inst->c = cmap_inst->c;
  807. memset(&req_lib_cmap_track_add, 0, sizeof(req_lib_cmap_track_add));
  808. req_lib_cmap_track_add.header.size = sizeof(req_lib_cmap_track_add);
  809. req_lib_cmap_track_add.header.id = MESSAGE_REQ_CMAP_TRACK_ADD;
  810. if (key_name) {
  811. if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
  812. return (CS_ERR_NAME_TOO_LONG);
  813. }
  814. memcpy(req_lib_cmap_track_add.key_name.value, key_name, strlen(key_name));
  815. req_lib_cmap_track_add.key_name.length = strlen(key_name);
  816. }
  817. req_lib_cmap_track_add.track_type = track_type;
  818. req_lib_cmap_track_add.track_inst_handle = cmap_track_inst_handle;
  819. iov.iov_base = (char *)&req_lib_cmap_track_add;
  820. iov.iov_len = sizeof(req_lib_cmap_track_add);
  821. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  822. cmap_inst->c,
  823. &iov,
  824. 1,
  825. &res_lib_cmap_track_add,
  826. sizeof (struct res_lib_cmap_track_add), CS_IPC_TIMEOUT_MS));
  827. if (error == CS_OK) {
  828. error = res_lib_cmap_track_add.header.error;
  829. }
  830. if (error == CS_OK) {
  831. *cmap_track_handle = res_lib_cmap_track_add.track_handle;
  832. cmap_track_inst->track_handle = *cmap_track_handle;
  833. }
  834. (void)hdb_handle_put (&cmap_track_handle_t_db, cmap_track_inst_handle);
  835. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  836. return (error);
  837. error_put_destroy:
  838. (void)hdb_handle_put (&cmap_track_handle_t_db, cmap_track_inst_handle);
  839. (void)hdb_handle_destroy (&cmap_track_handle_t_db, cmap_track_inst_handle);
  840. error_put:
  841. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  842. return (error);
  843. }
  844. cs_error_t cmap_track_delete(
  845. cmap_handle_t handle,
  846. cmap_track_handle_t track_handle)
  847. {
  848. cs_error_t error;
  849. struct iovec iov;
  850. struct cmap_inst *cmap_inst;
  851. struct cmap_track_inst *cmap_track_inst;
  852. struct req_lib_cmap_track_delete req_lib_cmap_track_delete;
  853. struct res_lib_cmap_track_delete res_lib_cmap_track_delete;
  854. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  855. if (error != CS_OK) {
  856. return (error);
  857. }
  858. memset(&req_lib_cmap_track_delete, 0, sizeof(req_lib_cmap_track_delete));
  859. req_lib_cmap_track_delete.header.size = sizeof(req_lib_cmap_track_delete);
  860. req_lib_cmap_track_delete.header.id = MESSAGE_REQ_CMAP_TRACK_DELETE;
  861. req_lib_cmap_track_delete.track_handle = track_handle;
  862. iov.iov_base = (char *)&req_lib_cmap_track_delete;
  863. iov.iov_len = sizeof(req_lib_cmap_track_delete);
  864. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  865. cmap_inst->c,
  866. &iov,
  867. 1,
  868. &res_lib_cmap_track_delete,
  869. sizeof (struct res_lib_cmap_track_delete), CS_IPC_TIMEOUT_MS));
  870. if (error == CS_OK) {
  871. error = res_lib_cmap_track_delete.header.error;
  872. }
  873. if (error == CS_OK) {
  874. error = hdb_error_to_cs(hdb_handle_get(&cmap_track_handle_t_db,
  875. res_lib_cmap_track_delete.track_inst_handle,
  876. (void *)&cmap_track_inst));
  877. if (error != CS_OK) {
  878. goto error_put;
  879. }
  880. (void)hdb_handle_put(&cmap_track_handle_t_db, res_lib_cmap_track_delete.track_inst_handle);
  881. (void)hdb_handle_destroy(&cmap_track_handle_t_db, res_lib_cmap_track_delete.track_inst_handle);
  882. }
  883. error_put:
  884. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  885. return (error);
  886. }