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