cmap.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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) {
  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. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  437. if (error != CS_OK) {
  438. return (error);
  439. }
  440. memset(&req_lib_cmap_get, 0, sizeof(req_lib_cmap_get));
  441. req_lib_cmap_get.header.size = sizeof(req_lib_cmap_get);
  442. req_lib_cmap_get.header.id = MESSAGE_REQ_CMAP_GET;
  443. memcpy(req_lib_cmap_get.key_name.value, key_name, strlen(key_name));
  444. req_lib_cmap_get.key_name.length = strlen(key_name);
  445. if (value != NULL && value_len != NULL) {
  446. req_lib_cmap_get.value_len = *value_len;
  447. } else {
  448. req_lib_cmap_get.value_len = 0;
  449. }
  450. iov.iov_base = (char *)&req_lib_cmap_get;
  451. iov.iov_len = sizeof(req_lib_cmap_get);
  452. res_size = sizeof(struct res_lib_cmap_get) + req_lib_cmap_get.value_len;
  453. res_lib_cmap_get = malloc(res_size);
  454. if (res_lib_cmap_get == NULL) {
  455. return (CS_ERR_NO_MEMORY);
  456. }
  457. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  458. cmap_inst->c,
  459. &iov,
  460. 1,
  461. res_lib_cmap_get,
  462. res_size, CS_IPC_TIMEOUT_MS));
  463. if (error == CS_OK) {
  464. error = res_lib_cmap_get->header.error;
  465. }
  466. if (error == CS_OK) {
  467. if (type != NULL) {
  468. *type = res_lib_cmap_get->type;
  469. }
  470. if (value_len != NULL) {
  471. *value_len = res_lib_cmap_get->value_len;
  472. }
  473. if (value != NULL) {
  474. memcpy(value, res_lib_cmap_get->value, res_lib_cmap_get->value_len);
  475. }
  476. }
  477. free(res_lib_cmap_get);
  478. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  479. return (error);
  480. }
  481. static cs_error_t cmap_get_int(
  482. cmap_handle_t handle,
  483. const char *key_name,
  484. void *value,
  485. size_t value_size,
  486. cmap_value_types_t type)
  487. {
  488. char key_value[16];
  489. size_t key_size;
  490. cs_error_t err;
  491. cmap_value_types_t key_type;
  492. key_size = sizeof(key_value);
  493. memset(key_value, 0, key_size);
  494. err = cmap_get(handle, key_name, key_value, &key_size, &key_type);
  495. if (err != CS_OK)
  496. return (err);
  497. if (key_type != type) {
  498. return (CS_ERR_INVALID_PARAM);
  499. }
  500. memcpy(value, key_value, value_size);
  501. return (CS_OK);
  502. }
  503. cs_error_t cmap_get_int8(cmap_handle_t handle, const char *key_name, int8_t *i8)
  504. {
  505. return (cmap_get_int(handle, key_name, i8, sizeof(*i8), CMAP_VALUETYPE_INT8));
  506. }
  507. cs_error_t cmap_get_uint8(cmap_handle_t handle, const char *key_name, uint8_t *u8)
  508. {
  509. return (cmap_get_int(handle, key_name, u8, sizeof(*u8), CMAP_VALUETYPE_UINT8));
  510. }
  511. cs_error_t cmap_get_int16(cmap_handle_t handle, const char *key_name, int16_t *i16)
  512. {
  513. return (cmap_get_int(handle, key_name, i16, sizeof(*i16), CMAP_VALUETYPE_INT16));
  514. }
  515. cs_error_t cmap_get_uint16(cmap_handle_t handle, const char *key_name, uint16_t *u16)
  516. {
  517. return (cmap_get_int(handle, key_name, u16, sizeof(*u16), CMAP_VALUETYPE_UINT16));
  518. }
  519. cs_error_t cmap_get_int32(cmap_handle_t handle, const char *key_name, int32_t *i32)
  520. {
  521. return (cmap_get_int(handle, key_name, i32, sizeof(*i32), CMAP_VALUETYPE_INT32));
  522. }
  523. cs_error_t cmap_get_uint32(cmap_handle_t handle, const char *key_name, uint32_t *u32)
  524. {
  525. return (cmap_get_int(handle, key_name, u32, sizeof(*u32), CMAP_VALUETYPE_UINT32));
  526. }
  527. cs_error_t cmap_get_int64(cmap_handle_t handle, const char *key_name, int64_t *i64)
  528. {
  529. return (cmap_get_int(handle, key_name, i64, sizeof(*i64), CMAP_VALUETYPE_INT64));
  530. }
  531. cs_error_t cmap_get_uint64(cmap_handle_t handle, const char *key_name, uint64_t *u64)
  532. {
  533. return (cmap_get_int(handle, key_name, u64, sizeof(*u64), CMAP_VALUETYPE_UINT64));
  534. }
  535. cs_error_t cmap_get_float(cmap_handle_t handle, const char *key_name, float *flt)
  536. {
  537. return (cmap_get_int(handle, key_name, flt, sizeof(*flt), CMAP_VALUETYPE_FLOAT));
  538. }
  539. cs_error_t cmap_get_double(cmap_handle_t handle, const char *key_name, double *dbl)
  540. {
  541. return (cmap_get_int(handle, key_name, dbl, sizeof(*dbl), CMAP_VALUETYPE_DOUBLE));
  542. }
  543. cs_error_t cmap_get_string(cmap_handle_t handle, const char *key_name, char **str)
  544. {
  545. cs_error_t res;
  546. size_t str_len;
  547. cmap_value_types_t type;
  548. res = cmap_get(handle, key_name, NULL, &str_len, &type);
  549. if (res != CS_OK || type != CMAP_VALUETYPE_STRING) {
  550. if (res == CS_OK) {
  551. res = CS_ERR_INVALID_PARAM;
  552. }
  553. goto return_error;
  554. }
  555. *str = malloc(str_len);
  556. if (*str == NULL) {
  557. res = CS_ERR_NO_MEMORY;
  558. goto return_error;
  559. }
  560. res = cmap_get(handle, key_name, *str, &str_len, &type);
  561. if (res != CS_OK) {
  562. free(*str);
  563. goto return_error;
  564. }
  565. return (CS_OK);
  566. return_error:
  567. return (res);
  568. }
  569. static cs_error_t cmap_adjust_int(cmap_handle_t handle, const char *key_name, int32_t step)
  570. {
  571. cs_error_t error;
  572. struct iovec iov;
  573. struct cmap_inst *cmap_inst;
  574. struct req_lib_cmap_adjust_int req_lib_cmap_adjust_int;
  575. struct res_lib_cmap_adjust_int res_lib_cmap_adjust_int;
  576. if (key_name == NULL) {
  577. return (CS_ERR_INVALID_PARAM);
  578. }
  579. if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
  580. return (CS_ERR_NAME_TOO_LONG);
  581. }
  582. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  583. if (error != CS_OK) {
  584. return (error);
  585. }
  586. memset(&req_lib_cmap_adjust_int, 0, sizeof(req_lib_cmap_adjust_int));
  587. req_lib_cmap_adjust_int.header.size = sizeof(req_lib_cmap_adjust_int);
  588. req_lib_cmap_adjust_int.header.id = MESSAGE_REQ_CMAP_ADJUST_INT;
  589. memcpy(req_lib_cmap_adjust_int.key_name.value, key_name, strlen(key_name));
  590. req_lib_cmap_adjust_int.key_name.length = strlen(key_name);
  591. req_lib_cmap_adjust_int.step = step;
  592. iov.iov_base = (char *)&req_lib_cmap_adjust_int;
  593. iov.iov_len = sizeof(req_lib_cmap_adjust_int);
  594. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  595. cmap_inst->c,
  596. &iov,
  597. 1,
  598. &res_lib_cmap_adjust_int,
  599. sizeof (struct res_lib_cmap_adjust_int), CS_IPC_TIMEOUT_MS));
  600. if (error == CS_OK) {
  601. error = res_lib_cmap_adjust_int.header.error;
  602. }
  603. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  604. return (error);
  605. }
  606. cs_error_t cmap_inc(cmap_handle_t handle, const char *key_name)
  607. {
  608. return (cmap_adjust_int(handle, key_name, 1));
  609. }
  610. cs_error_t cmap_dec(cmap_handle_t handle, const char *key_name)
  611. {
  612. return (cmap_adjust_int(handle, key_name, -1));
  613. }
  614. cs_error_t cmap_iter_init(
  615. cmap_handle_t handle,
  616. const char *prefix,
  617. cmap_iter_handle_t *cmap_iter_handle)
  618. {
  619. cs_error_t error;
  620. struct iovec iov;
  621. struct cmap_inst *cmap_inst;
  622. struct req_lib_cmap_iter_init req_lib_cmap_iter_init;
  623. struct res_lib_cmap_iter_init res_lib_cmap_iter_init;
  624. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  625. if (error != CS_OK) {
  626. return (error);
  627. }
  628. memset(&req_lib_cmap_iter_init, 0, sizeof(req_lib_cmap_iter_init));
  629. req_lib_cmap_iter_init.header.size = sizeof(req_lib_cmap_iter_init);
  630. req_lib_cmap_iter_init.header.id = MESSAGE_REQ_CMAP_ITER_INIT;
  631. if (prefix) {
  632. if (strlen(prefix) >= CS_MAX_NAME_LENGTH) {
  633. return (CS_ERR_NAME_TOO_LONG);
  634. }
  635. memcpy(req_lib_cmap_iter_init.prefix.value, prefix, strlen(prefix));
  636. req_lib_cmap_iter_init.prefix.length = strlen(prefix);
  637. }
  638. iov.iov_base = (char *)&req_lib_cmap_iter_init;
  639. iov.iov_len = sizeof(req_lib_cmap_iter_init);
  640. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  641. cmap_inst->c,
  642. &iov,
  643. 1,
  644. &res_lib_cmap_iter_init,
  645. sizeof (struct res_lib_cmap_iter_init), CS_IPC_TIMEOUT_MS));
  646. if (error == CS_OK) {
  647. error = res_lib_cmap_iter_init.header.error;
  648. }
  649. if (error == CS_OK) {
  650. *cmap_iter_handle = res_lib_cmap_iter_init.iter_handle;
  651. }
  652. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  653. return (error);
  654. }
  655. cs_error_t cmap_iter_next(
  656. cmap_handle_t handle,
  657. cmap_iter_handle_t iter_handle,
  658. char key_name[],
  659. size_t *value_len,
  660. cmap_value_types_t *type)
  661. {
  662. cs_error_t error;
  663. struct iovec iov;
  664. struct cmap_inst *cmap_inst;
  665. struct req_lib_cmap_iter_next req_lib_cmap_iter_next;
  666. struct res_lib_cmap_iter_next res_lib_cmap_iter_next;
  667. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  668. if (error != CS_OK) {
  669. return (error);
  670. }
  671. memset(&req_lib_cmap_iter_next, 0, sizeof(req_lib_cmap_iter_next));
  672. req_lib_cmap_iter_next.header.size = sizeof(req_lib_cmap_iter_next);
  673. req_lib_cmap_iter_next.header.id = MESSAGE_REQ_CMAP_ITER_NEXT;
  674. req_lib_cmap_iter_next.iter_handle = iter_handle;
  675. iov.iov_base = (char *)&req_lib_cmap_iter_next;
  676. iov.iov_len = sizeof(req_lib_cmap_iter_next);
  677. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  678. cmap_inst->c,
  679. &iov,
  680. 1,
  681. &res_lib_cmap_iter_next,
  682. sizeof (struct res_lib_cmap_iter_next), CS_IPC_TIMEOUT_MS));
  683. if (error == CS_OK) {
  684. error = res_lib_cmap_iter_next.header.error;
  685. }
  686. if (error == CS_OK) {
  687. strncpy(key_name, (const char *)res_lib_cmap_iter_next.key_name.value, CMAP_KEYNAME_MAXLEN);
  688. if (value_len != NULL) {
  689. *value_len = res_lib_cmap_iter_next.value_len;
  690. }
  691. if (type != NULL) {
  692. *type = res_lib_cmap_iter_next.type;
  693. }
  694. }
  695. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  696. return (error);
  697. }
  698. cs_error_t cmap_iter_finalize(
  699. cmap_handle_t handle,
  700. cmap_iter_handle_t iter_handle)
  701. {
  702. cs_error_t error;
  703. struct iovec iov;
  704. struct cmap_inst *cmap_inst;
  705. struct req_lib_cmap_iter_finalize req_lib_cmap_iter_finalize;
  706. struct res_lib_cmap_iter_finalize res_lib_cmap_iter_finalize;
  707. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  708. if (error != CS_OK) {
  709. return (error);
  710. }
  711. memset(&req_lib_cmap_iter_finalize, 0, sizeof(req_lib_cmap_iter_finalize));
  712. req_lib_cmap_iter_finalize.header.size = sizeof(req_lib_cmap_iter_finalize);
  713. req_lib_cmap_iter_finalize.header.id = MESSAGE_REQ_CMAP_ITER_FINALIZE;
  714. req_lib_cmap_iter_finalize.iter_handle = iter_handle;
  715. iov.iov_base = (char *)&req_lib_cmap_iter_finalize;
  716. iov.iov_len = sizeof(req_lib_cmap_iter_finalize);
  717. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  718. cmap_inst->c,
  719. &iov,
  720. 1,
  721. &res_lib_cmap_iter_finalize,
  722. sizeof (struct res_lib_cmap_iter_finalize), CS_IPC_TIMEOUT_MS));
  723. if (error == CS_OK) {
  724. error = res_lib_cmap_iter_finalize.header.error;
  725. }
  726. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  727. return (error);
  728. }
  729. cs_error_t cmap_track_add(
  730. cmap_handle_t handle,
  731. const char *key_name,
  732. int32_t track_type,
  733. cmap_notify_fn_t notify_fn,
  734. void *user_data,
  735. cmap_track_handle_t *cmap_track_handle)
  736. {
  737. cs_error_t error;
  738. struct iovec iov;
  739. struct cmap_inst *cmap_inst;
  740. struct req_lib_cmap_track_add req_lib_cmap_track_add;
  741. struct res_lib_cmap_track_add res_lib_cmap_track_add;
  742. struct cmap_track_inst *cmap_track_inst;
  743. cmap_track_handle_t cmap_track_inst_handle;
  744. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  745. if (error != CS_OK) {
  746. return (error);
  747. }
  748. error = hdb_error_to_cs(hdb_handle_create(&cmap_track_handle_t_db,
  749. sizeof(*cmap_track_inst), &cmap_track_inst_handle));
  750. if (error != CS_OK) {
  751. goto error_put;
  752. }
  753. error = hdb_error_to_cs(hdb_handle_get(&cmap_track_handle_t_db,
  754. cmap_track_inst_handle, (void *)&cmap_track_inst));
  755. if (error != CS_OK) {
  756. goto error_put_destroy;
  757. }
  758. cmap_track_inst->user_data = user_data;
  759. cmap_track_inst->notify_fn = notify_fn;
  760. cmap_track_inst->c = cmap_inst->c;
  761. memset(&req_lib_cmap_track_add, 0, sizeof(req_lib_cmap_track_add));
  762. req_lib_cmap_track_add.header.size = sizeof(req_lib_cmap_track_add);
  763. req_lib_cmap_track_add.header.id = MESSAGE_REQ_CMAP_TRACK_ADD;
  764. if (key_name) {
  765. if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
  766. return (CS_ERR_NAME_TOO_LONG);
  767. }
  768. memcpy(req_lib_cmap_track_add.key_name.value, key_name, strlen(key_name));
  769. req_lib_cmap_track_add.key_name.length = strlen(key_name);
  770. }
  771. req_lib_cmap_track_add.track_type = track_type;
  772. req_lib_cmap_track_add.track_inst_handle = cmap_track_inst_handle;
  773. iov.iov_base = (char *)&req_lib_cmap_track_add;
  774. iov.iov_len = sizeof(req_lib_cmap_track_add);
  775. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  776. cmap_inst->c,
  777. &iov,
  778. 1,
  779. &res_lib_cmap_track_add,
  780. sizeof (struct res_lib_cmap_track_add), CS_IPC_TIMEOUT_MS));
  781. if (error == CS_OK) {
  782. error = res_lib_cmap_track_add.header.error;
  783. }
  784. if (error == CS_OK) {
  785. *cmap_track_handle = res_lib_cmap_track_add.track_handle;
  786. cmap_track_inst->track_handle = *cmap_track_handle;
  787. }
  788. (void)hdb_handle_put (&cmap_track_handle_t_db, cmap_track_inst_handle);
  789. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  790. return (error);
  791. error_put_destroy:
  792. (void)hdb_handle_put (&cmap_track_handle_t_db, cmap_track_inst_handle);
  793. (void)hdb_handle_destroy (&cmap_track_handle_t_db, cmap_track_inst_handle);
  794. error_put:
  795. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  796. return (error);
  797. }
  798. cs_error_t cmap_track_delete(
  799. cmap_handle_t handle,
  800. cmap_track_handle_t track_handle)
  801. {
  802. cs_error_t error;
  803. struct iovec iov;
  804. struct cmap_inst *cmap_inst;
  805. struct cmap_track_inst *cmap_track_inst;
  806. struct req_lib_cmap_track_delete req_lib_cmap_track_delete;
  807. struct res_lib_cmap_track_delete res_lib_cmap_track_delete;
  808. error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
  809. if (error != CS_OK) {
  810. return (error);
  811. }
  812. memset(&req_lib_cmap_track_delete, 0, sizeof(req_lib_cmap_track_delete));
  813. req_lib_cmap_track_delete.header.size = sizeof(req_lib_cmap_track_delete);
  814. req_lib_cmap_track_delete.header.id = MESSAGE_REQ_CMAP_TRACK_DELETE;
  815. req_lib_cmap_track_delete.track_handle = track_handle;
  816. iov.iov_base = (char *)&req_lib_cmap_track_delete;
  817. iov.iov_len = sizeof(req_lib_cmap_track_delete);
  818. error = qb_to_cs_error(qb_ipcc_sendv_recv(
  819. cmap_inst->c,
  820. &iov,
  821. 1,
  822. &res_lib_cmap_track_delete,
  823. sizeof (struct res_lib_cmap_track_delete), CS_IPC_TIMEOUT_MS));
  824. if (error == CS_OK) {
  825. error = res_lib_cmap_track_delete.header.error;
  826. }
  827. if (error == CS_OK) {
  828. error = hdb_error_to_cs(hdb_handle_get(&cmap_track_handle_t_db,
  829. res_lib_cmap_track_delete.track_inst_handle,
  830. (void *)&cmap_track_inst));
  831. if (error != CS_OK) {
  832. goto error_put;
  833. }
  834. (void)hdb_handle_put(&cmap_track_handle_t_db, res_lib_cmap_track_delete.track_inst_handle);
  835. (void)hdb_handle_destroy(&cmap_track_handle_t_db, res_lib_cmap_track_delete.track_inst_handle);
  836. }
  837. error_put:
  838. (void)hdb_handle_put (&cmap_handle_t_db, handle);
  839. return (error);
  840. }