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 
63 struct 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 
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,
83 	cmap_value_types_t type);
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  */
cmap_initialize(cmap_handle_t * handle)90 cs_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;
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 
cmap_inst_free(void * inst)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 
cmap_finalize(cmap_handle_t handle)131 cs_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 
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 
cmap_fd_get(cmap_handle_t handle,int * fd)170 cs_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 
cmap_dispatch(cmap_handle_t handle,cs_dispatch_flags_t dispatch_types)187 cs_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 	/*
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,
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) {
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) {
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 		}
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 
cmap_context_get(cmap_handle_t handle,const void ** context)310 cs_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 
cmap_context_set(cmap_handle_t handle,const void * context)329 cs_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 
cmap_set(cmap_handle_t handle,const char * key_name,const void * value,size_t value_len,cmap_value_types_t type)348 cs_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 
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 
cmap_set_int8(cmap_handle_t handle,const char * key_name,int8_t value)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 
cmap_set_uint8(cmap_handle_t handle,const char * key_name,uint8_t value)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 
cmap_set_int16(cmap_handle_t handle,const char * key_name,int16_t value)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 
cmap_set_uint16(cmap_handle_t handle,const char * key_name,uint16_t value)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 
cmap_set_int32(cmap_handle_t handle,const char * key_name,int32_t value)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 
cmap_set_uint32(cmap_handle_t handle,const char * key_name,uint32_t value)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 
cmap_set_int64(cmap_handle_t handle,const char * key_name,int64_t value)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 
cmap_set_uint64(cmap_handle_t handle,const char * key_name,uint64_t value)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 
cmap_set_float(cmap_handle_t handle,const char * key_name,float value)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 
cmap_set_double(cmap_handle_t handle,const char * key_name,double value)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 
cmap_set_string(cmap_handle_t handle,const char * key_name,const char * value)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 
cmap_delete(cmap_handle_t handle,const char * key_name)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 
cmap_get(cmap_handle_t handle,const char * key_name,void * value,size_t * value_len,cmap_value_types_t * type)511 cs_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 	}
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 
cmap_get_int(cmap_handle_t handle,const char * key_name,void * value,size_t value_size,cmap_value_types_t type)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,
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 
cmap_get_int8(cmap_handle_t handle,const char * key_name,int8_t * i8)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 
cmap_get_uint8(cmap_handle_t handle,const char * key_name,uint8_t * u8)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 
cmap_get_int16(cmap_handle_t handle,const char * key_name,int16_t * i16)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 
cmap_get_uint16(cmap_handle_t handle,const char * key_name,uint16_t * u16)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 
cmap_get_int32(cmap_handle_t handle,const char * key_name,int32_t * i32)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 
cmap_get_uint32(cmap_handle_t handle,const char * key_name,uint32_t * u32)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 
cmap_get_int64(cmap_handle_t handle,const char * key_name,int64_t * i64)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 
cmap_get_uint64(cmap_handle_t handle,const char * key_name,uint64_t * u64)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 
cmap_get_float(cmap_handle_t handle,const char * key_name,float * flt)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 
cmap_get_double(cmap_handle_t handle,const char * key_name,double * dbl)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 
cmap_get_string(cmap_handle_t handle,const char * key_name,char ** str)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;
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 
717 return_error:
718 	return (res);
719 }
720 
cmap_adjust_int(cmap_handle_t handle,const char * key_name,int32_t step)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;
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 	}
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 
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,
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 
cmap_inc(cmap_handle_t handle,const char * key_name)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 
cmap_dec(cmap_handle_t handle,const char * key_name)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 
cmap_iter_init(cmap_handle_t handle,const char * prefix,cmap_iter_handle_t * cmap_iter_handle)781 cs_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 
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 
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)836 cs_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 
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 		memcpy(key_name, (const char *)res_lib_cmap_iter_next.key_name.value,
879 		    res_lib_cmap_iter_next.key_name.length);
880 		key_name[res_lib_cmap_iter_next.key_name.length] = '\0';
881 
882 		if (value_len != NULL) {
883 			*value_len = res_lib_cmap_iter_next.value_len;
884 		}
885 
886 		if (type != NULL) {
887 			*type = res_lib_cmap_iter_next.type;
888 		}
889 	}
890 
891 	(void)hdb_handle_put (&cmap_handle_t_db, handle);
892 
893 	return (error);
894 }
895 
cmap_iter_finalize(cmap_handle_t handle,cmap_iter_handle_t iter_handle)896 cs_error_t cmap_iter_finalize(
897 		cmap_handle_t handle,
898 		cmap_iter_handle_t iter_handle)
899 {
900 	cs_error_t error;
901 	struct iovec iov;
902 	struct cmap_inst *cmap_inst;
903 	struct req_lib_cmap_iter_finalize req_lib_cmap_iter_finalize;
904 	struct res_lib_cmap_iter_finalize res_lib_cmap_iter_finalize;
905 
906 	error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
907 	if (error != CS_OK) {
908 		return (error);
909 	}
910 
911 	memset(&req_lib_cmap_iter_finalize, 0, sizeof(req_lib_cmap_iter_finalize));
912 	req_lib_cmap_iter_finalize.header.size = sizeof(req_lib_cmap_iter_finalize);
913 	req_lib_cmap_iter_finalize.header.id = MESSAGE_REQ_CMAP_ITER_FINALIZE;
914 	req_lib_cmap_iter_finalize.iter_handle = iter_handle;
915 
916 	iov.iov_base = (char *)&req_lib_cmap_iter_finalize;
917 	iov.iov_len = sizeof(req_lib_cmap_iter_finalize);
918 
919 	error = qb_to_cs_error(qb_ipcc_sendv_recv(
920 		cmap_inst->c,
921 		&iov,
922 		1,
923 		&res_lib_cmap_iter_finalize,
924 		sizeof (struct res_lib_cmap_iter_finalize), CS_IPC_TIMEOUT_MS));
925 
926 	if (error == CS_OK) {
927 		error = res_lib_cmap_iter_finalize.header.error;
928 	}
929 
930 	(void)hdb_handle_put (&cmap_handle_t_db, handle);
931 
932 	return (error);
933 }
934 
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)935 cs_error_t cmap_track_add(
936 	cmap_handle_t handle,
937 	const char *key_name,
938 	int32_t track_type,
939 	cmap_notify_fn_t notify_fn,
940 	void *user_data,
941 	cmap_track_handle_t *cmap_track_handle)
942 {
943 	cs_error_t error;
944 	struct iovec iov;
945 	struct cmap_inst *cmap_inst;
946 	struct req_lib_cmap_track_add req_lib_cmap_track_add;
947 	struct res_lib_cmap_track_add res_lib_cmap_track_add;
948 	struct cmap_track_inst *cmap_track_inst;
949 	cmap_track_handle_t cmap_track_inst_handle;
950 
951 	if (cmap_track_handle == NULL || notify_fn == NULL) {
952 		return (CS_ERR_INVALID_PARAM);
953 	}
954 
955 	error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
956 	if (error != CS_OK) {
957 		return (error);
958 	}
959 
960 	error = hdb_error_to_cs(hdb_handle_create(&cmap_track_handle_t_db,
961 				sizeof(*cmap_track_inst), &cmap_track_inst_handle));
962 	if (error != CS_OK) {
963 		goto error_put;
964 	}
965 
966 	error = hdb_error_to_cs(hdb_handle_get(&cmap_track_handle_t_db,
967 				cmap_track_inst_handle, (void *)&cmap_track_inst));
968 	if (error != CS_OK) {
969 		goto error_put_destroy;
970 	}
971 
972 	cmap_track_inst->user_data = user_data;
973 	cmap_track_inst->notify_fn = notify_fn;
974 	cmap_track_inst->c = cmap_inst->c;
975 
976 	memset(&req_lib_cmap_track_add, 0, sizeof(req_lib_cmap_track_add));
977 	req_lib_cmap_track_add.header.size = sizeof(req_lib_cmap_track_add);
978 	req_lib_cmap_track_add.header.id = MESSAGE_REQ_CMAP_TRACK_ADD;
979 
980 	if (key_name) {
981 		if (strlen(key_name) >= CS_MAX_NAME_LENGTH) {
982 			return (CS_ERR_NAME_TOO_LONG);
983 		}
984 		memcpy(req_lib_cmap_track_add.key_name.value, key_name, strlen(key_name));
985 		req_lib_cmap_track_add.key_name.length = strlen(key_name);
986 	}
987 
988 	req_lib_cmap_track_add.track_type = track_type;
989 	req_lib_cmap_track_add.track_inst_handle = cmap_track_inst_handle;
990 
991 	iov.iov_base = (char *)&req_lib_cmap_track_add;
992 	iov.iov_len = sizeof(req_lib_cmap_track_add);
993 
994 	error = qb_to_cs_error(qb_ipcc_sendv_recv(
995 		cmap_inst->c,
996 		&iov,
997 		1,
998 		&res_lib_cmap_track_add,
999 		sizeof (struct res_lib_cmap_track_add), CS_IPC_TIMEOUT_MS));
1000 
1001 	if (error == CS_OK) {
1002 		error = res_lib_cmap_track_add.header.error;
1003 	}
1004 
1005 	if (error == CS_OK) {
1006 		*cmap_track_handle = res_lib_cmap_track_add.track_handle;
1007 		cmap_track_inst->track_handle = *cmap_track_handle;
1008 	}
1009 
1010 	(void)hdb_handle_put (&cmap_track_handle_t_db, cmap_track_inst_handle);
1011 
1012 	(void)hdb_handle_put (&cmap_handle_t_db, handle);
1013 
1014 	return (error);
1015 
1016 error_put_destroy:
1017 	(void)hdb_handle_put (&cmap_track_handle_t_db, cmap_track_inst_handle);
1018 	(void)hdb_handle_destroy (&cmap_track_handle_t_db, cmap_track_inst_handle);
1019 
1020 error_put:
1021 	(void)hdb_handle_put (&cmap_handle_t_db, handle);
1022 
1023 	return (error);
1024 }
1025 
cmap_track_delete(cmap_handle_t handle,cmap_track_handle_t track_handle)1026 cs_error_t cmap_track_delete(
1027 		cmap_handle_t handle,
1028 		cmap_track_handle_t track_handle)
1029 {
1030 	cs_error_t error;
1031 	struct iovec iov;
1032 	struct cmap_inst *cmap_inst;
1033 	struct cmap_track_inst *cmap_track_inst;
1034 	struct req_lib_cmap_track_delete req_lib_cmap_track_delete;
1035 	struct res_lib_cmap_track_delete res_lib_cmap_track_delete;
1036 
1037 	error = hdb_error_to_cs(hdb_handle_get (&cmap_handle_t_db, handle, (void *)&cmap_inst));
1038 	if (error != CS_OK) {
1039 		return (error);
1040 	}
1041 
1042 	memset(&req_lib_cmap_track_delete, 0, sizeof(req_lib_cmap_track_delete));
1043 	req_lib_cmap_track_delete.header.size = sizeof(req_lib_cmap_track_delete);
1044 	req_lib_cmap_track_delete.header.id = MESSAGE_REQ_CMAP_TRACK_DELETE;
1045 	req_lib_cmap_track_delete.track_handle = track_handle;
1046 
1047 	iov.iov_base = (char *)&req_lib_cmap_track_delete;
1048 	iov.iov_len = sizeof(req_lib_cmap_track_delete);
1049 
1050 	error = qb_to_cs_error(qb_ipcc_sendv_recv(
1051 		cmap_inst->c,
1052 		&iov,
1053 		1,
1054 		&res_lib_cmap_track_delete,
1055 		sizeof (struct res_lib_cmap_track_delete), CS_IPC_TIMEOUT_MS));
1056 
1057 	if (error == CS_OK) {
1058 		error = res_lib_cmap_track_delete.header.error;
1059 	}
1060 
1061 	if (error == CS_OK) {
1062 		error = hdb_error_to_cs(hdb_handle_get(&cmap_track_handle_t_db,
1063 					res_lib_cmap_track_delete.track_inst_handle,
1064 					(void *)&cmap_track_inst));
1065 		if (error != CS_OK) {
1066 			goto error_put;
1067 		}
1068 
1069 		(void)hdb_handle_put(&cmap_track_handle_t_db, res_lib_cmap_track_delete.track_inst_handle);
1070 		(void)hdb_handle_destroy(&cmap_track_handle_t_db, res_lib_cmap_track_delete.track_inst_handle);
1071 	}
1072 
1073 error_put:
1074 	(void)hdb_handle_put (&cmap_handle_t_db, handle);
1075 
1076 	return (error);
1077 }
1078