]> git.proxmox.com Git - mirror_corosync.git/blame - lib/cmap.c
icmap: Add function to return global icmap
[mirror_corosync.git] / lib / cmap.c
CommitLineData
b3c99977 1/*
03c33697 2 * Copyright (c) 2011-2012 Red Hat, Inc.
b3c99977
JF
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>
932829bf 42#include <sys/uio.h>
b3c99977
JF
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
57struct cmap_inst {
acad48bf 58 int finalize;
b3c99977
JF
59 qb_ipcc_connection_t *c;
60 const void *context;
61};
62
63struct cmap_track_inst {
64 void *user_data;
65 cmap_notify_fn_t notify_fn;
66 qb_ipcc_connection_t *c;
67 cmap_track_handle_t track_handle;
68};
69
acad48bf
AS
70static void cmap_inst_free (void *inst);
71
72DECLARE_HDB_DATABASE(cmap_handle_t_db, cmap_inst_free);
b3c99977
JF
73DECLARE_HDB_DATABASE(cmap_track_handle_t_db,NULL);
74
75/*
76 * Function prototypes
77 */
78static cs_error_t cmap_get_int(
79 cmap_handle_t handle,
80 const char *key_name,
81 void *value,
82 size_t value_size,
83 cmap_value_types_t type);
84
85static cs_error_t cmap_adjust_int(cmap_handle_t handle, const char *key_name, int32_t step);
86
87/*
88 * Function implementations
89 */
90cs_error_t cmap_initialize (cmap_handle_t *handle)
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;
acad48bf 106 cmap_inst->finalize = 0;
b3c99977
JF
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
117error_put_destroy:
118 (void)hdb_handle_put(&cmap_handle_t_db, *handle);
119error_destroy:
120 (void)hdb_handle_destroy(&cmap_handle_t_db, *handle);
121error_no_destroy:
122 return (error);
123}
124
acad48bf
AS
125static 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
b3c99977
JF
131cs_error_t cmap_finalize(cmap_handle_t handle)
132{
133 struct cmap_inst *cmap_inst;
134 cs_error_t error;
135 hdb_handle_t track_inst_handle = 0;
136 struct cmap_track_inst *cmap_track_inst;
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
acad48bf
AS
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;
b3c99977
JF
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
170cs_error_t cmap_fd_get(cmap_handle_t handle, int *fd)
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
187cs_error_t cmap_dispatch (
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];
197 struct res_lib_cmap_notify_callback *res_lib_cmap_notify_callback;
198 struct cmap_track_inst *cmap_track_inst;
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 /*
9260efdf
JF
208 * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
209 * wait indefinately for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
b3c99977 210 */
9260efdf 211 if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
b3c99977
JF
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,
220 IPC_DISPATCH_SIZE,
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) {
9260efdf
JF
228 if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
229 /*
230 * Don't mask error
231 */
232 goto error_put;
233 }
b3c99977
JF
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) {
250 case MESSAGE_RES_CMAP_NOTIFY_CALLBACK:
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 }
acad48bf
AS
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 }
b3c99977
JF
295
296 /*
297 * Determine if more messages should be processed
298 */
9260efdf 299 if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
b3c99977
JF
300 cont = 0;
301 }
302 } while (cont);
303
304error_put:
305 (void)hdb_handle_put (&cmap_handle_t_db, handle);
306
307 return (error);
308}
309
310cs_error_t cmap_context_get (
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
329cs_error_t cmap_context_set (
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
348cs_error_t cmap_set (
349 cmap_handle_t handle,
350 const char *key_name,
351 const void *value,
352 size_t value_len,
353 cmap_value_types_t type)
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
28af3523 361 if (key_name == NULL || value == NULL) {
b3c99977
JF
362 return (CS_ERR_INVALID_PARAM);
363 }
364
fefdc2db
JF
365 if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
366 return (CS_ERR_NAME_TOO_LONG);
367 }
368
b3c99977
JF
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,
9e36255b 394 sizeof (struct res_lib_cmap_set), CS_IPC_TIMEOUT_MS));
b3c99977
JF
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
405cs_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
410cs_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
415cs_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
420cs_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
425cs_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
430cs_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
435cs_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
440cs_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
445cs_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
450cs_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
455cs_error_t cmap_set_string(cmap_handle_t handle, const char *key_name, const char *value)
456{
8cde53aa
JF
457
458 if (value == NULL) {
459 return (CS_ERR_INVALID_PARAM);
460 }
461
b3c99977
JF
462 return (cmap_set(handle, key_name, value, strlen(value), CMAP_VALUETYPE_STRING));
463}
464
465cs_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 }
fefdc2db
JF
476 if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
477 return (CS_ERR_NAME_TOO_LONG);
478 }
b3c99977
JF
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,
9e36255b 500 sizeof (struct res_lib_cmap_delete), CS_IPC_TIMEOUT_MS));
b3c99977
JF
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
511cs_error_t cmap_get(
512 cmap_handle_t handle,
513 const char *key_name,
514 void *value,
515 size_t *value_len,
516 cmap_value_types_t *type)
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;
522 struct res_lib_cmap_get *res_lib_cmap_get;
523 size_t res_size;
524
525 if (key_name == NULL) {
526 return (CS_ERR_INVALID_PARAM);
527 }
fefdc2db
JF
528 if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
529 return (CS_ERR_NAME_TOO_LONG);
530 }
b3c99977 531
28af3523
JF
532 if (value != NULL && value_len == NULL) {
533 return (CS_ERR_INVALID_PARAM);
534 }
535
b3c99977
JF
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,
9e36255b 569 res_size, CS_IPC_TIMEOUT_MS));
b3c99977
JF
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
28af3523 584 if (value != NULL && value_len != NULL) {
b3c99977
JF
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
596static cs_error_t cmap_get_int(
597 cmap_handle_t handle,
598 const char *key_name,
599 void *value,
600 size_t value_size,
601 cmap_value_types_t type)
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
625cs_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
631cs_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
637cs_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
643cs_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
649cs_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
655cs_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
661cs_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
667cs_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
673cs_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
679cs_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
685cs_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;
689 cmap_value_types_t type;
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
717return_error:
718 return (res);
719}
720
721static 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;
726 struct req_lib_cmap_adjust_int req_lib_cmap_adjust_int;
727 struct res_lib_cmap_adjust_int res_lib_cmap_adjust_int;
728
729 if (key_name == NULL) {
730 return (CS_ERR_INVALID_PARAM);
731 }
fefdc2db
JF
732 if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
733 return (CS_ERR_NAME_TOO_LONG);
734 }
b3c99977
JF
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
741 memset(&req_lib_cmap_adjust_int, 0, sizeof(req_lib_cmap_adjust_int));
742 req_lib_cmap_adjust_int.header.size = sizeof(req_lib_cmap_adjust_int);
743 req_lib_cmap_adjust_int.header.id = MESSAGE_REQ_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,
757 &res_lib_cmap_adjust_int,
9e36255b 758 sizeof (struct res_lib_cmap_adjust_int), CS_IPC_TIMEOUT_MS));
b3c99977
JF
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
769cs_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
775cs_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
781cs_error_t cmap_iter_init(
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
11f4c114
JF
792 if (cmap_iter_handle == NULL) {
793 return (CS_ERR_INVALID_PARAM);
794 }
795
b3c99977
JF
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) {
fefdc2db
JF
806 if (strlen(prefix) >= CS_MAX_NAME_LENGTH) {
807 return (CS_ERR_NAME_TOO_LONG);
808 }
b3c99977
JF
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,
9e36255b 821 sizeof (struct res_lib_cmap_iter_init), CS_IPC_TIMEOUT_MS));
b3c99977
JF
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
836cs_error_t cmap_iter_next(
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
11f4c114
JF
849 if (key_name == NULL) {
850 return (CS_ERR_INVALID_PARAM);
851 }
852
b3c99977
JF
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,
9e36255b 871 sizeof (struct res_lib_cmap_iter_next), CS_IPC_TIMEOUT_MS));
b3c99977
JF
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
894cs_error_t cmap_iter_finalize(
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,
9e36255b 922 sizeof (struct res_lib_cmap_iter_finalize), CS_IPC_TIMEOUT_MS));
b3c99977
JF
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
933cs_error_t cmap_track_add(
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;
946 struct cmap_track_inst *cmap_track_inst;
947 cmap_track_handle_t cmap_track_inst_handle;
948
11f4c114
JF
949 if (cmap_track_handle == NULL || notify_fn == NULL) {
950 return (CS_ERR_INVALID_PARAM);
951 }
952
b3c99977
JF
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) {
fefdc2db
JF
979 if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
980 return (CS_ERR_NAME_TOO_LONG);
981 }
b3c99977
JF
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,
9e36255b 997 sizeof (struct res_lib_cmap_track_add), CS_IPC_TIMEOUT_MS));
b3c99977
JF
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
1014error_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
1018error_put:
1019 (void)hdb_handle_put (&cmap_handle_t_db, handle);
1020
1021 return (error);
1022}
1023
1024cs_error_t cmap_track_delete(
1025 cmap_handle_t handle,
1026 cmap_track_handle_t track_handle)
1027{
1028 cs_error_t error;
1029 struct iovec iov;
1030 struct cmap_inst *cmap_inst;
1031 struct cmap_track_inst *cmap_track_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,
9e36255b 1053 sizeof (struct res_lib_cmap_track_delete), CS_IPC_TIMEOUT_MS));
b3c99977
JF
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
1071error_put:
1072 (void)hdb_handle_put (&cmap_handle_t_db, handle);
1073
1074 return (error);
1075}