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