xref: /illumos-gate/usr/src/lib/libidmap/common/utils.c (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Utility routines
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <libintl.h>
36 #include "idmap_impl.h"
37 
38 #define	_UDT_SIZE_INCR	1
39 
40 #define	_GET_IDS_SIZE_INCR	1
41 
42 static struct timeval TIMEOUT = { 25, 0 };
43 
44 extern int __idmap_verbose;
45 
46 idmap_retcode
47 _udt_extend_batch(idmap_udt_handle_t *udthandle, int opnum) {
48 	idmap_update_op	*tmplist;
49 	size_t		nsize;
50 
51 	if (udthandle->next >= udthandle->batch.idmap_update_batch_len) {
52 		nsize = (udthandle->batch.idmap_update_batch_len +
53 				_UDT_SIZE_INCR) * sizeof (*tmplist);
54 		tmplist = realloc(
55 			udthandle->batch.idmap_update_batch_val, nsize);
56 		if (tmplist == NULL)
57 			return (IDMAP_ERR_MEMORY);
58 		(void) memset((uchar_t *)tmplist +
59 			(udthandle->batch.idmap_update_batch_len *
60 			sizeof (*tmplist)), 0,
61 			_UDT_SIZE_INCR * sizeof (*tmplist));
62 		udthandle->batch.idmap_update_batch_val = tmplist;
63 		udthandle->batch.idmap_update_batch_len += _UDT_SIZE_INCR;
64 	}
65 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
66 		opnum;
67 	return (IDMAP_SUCCESS);
68 }
69 
70 idmap_retcode
71 _get_ids_extend_batch(idmap_get_handle_t *gh) {
72 	idmap_mapping	*t1;
73 	idmap_get_res_t	*t2;
74 	size_t		nsize, len;
75 
76 	len = gh->batch.idmap_mapping_batch_len;
77 	if (gh->next >= len) {
78 		/* extend the request array */
79 		nsize = (len + _GET_IDS_SIZE_INCR) * sizeof (*t1);
80 		t1 = realloc(gh->batch.idmap_mapping_batch_val, nsize);
81 		if (t1 == NULL)
82 			return (IDMAP_ERR_MEMORY);
83 		(void) memset((uchar_t *)t1 + (len * sizeof (*t1)), 0,
84 			_GET_IDS_SIZE_INCR * sizeof (*t1));
85 		gh->batch.idmap_mapping_batch_val = t1;
86 
87 		/* extend the return list */
88 		nsize = (len + _GET_IDS_SIZE_INCR) * sizeof (*t2);
89 		t2 = realloc(gh->retlist, nsize);
90 		if (t2 == NULL)
91 			return (IDMAP_ERR_MEMORY);
92 		(void) memset((uchar_t *)t2 + (len * sizeof (*t2)), 0,
93 			_GET_IDS_SIZE_INCR * sizeof (*t2));
94 		gh->retlist = t2;
95 
96 		gh->batch.idmap_mapping_batch_len += _GET_IDS_SIZE_INCR;
97 	}
98 	return (IDMAP_SUCCESS);
99 }
100 
101 idmap_stat
102 _iter_get_next_list(int type, idmap_iter_t *iter,
103 		void *arg, uchar_t **list, size_t valsize,
104 		xdrproc_t xdr_arg_proc, xdrproc_t xdr_res_proc) {
105 
106 	CLIENT		*clnt;
107 	enum clnt_stat	clntstat;
108 	const char	*me = "_iter_get_next_list";
109 
110 	if (__idmap_verbose)
111 		(void) fprintf(stdout, "%s\n", me);
112 
113 	iter->next = 0;
114 	iter->retlist = NULL;
115 	_IDMAP_GET_CLIENT_HANDLE(iter->ih, clnt);
116 
117 	/* init the result */
118 	if (*list) {
119 		xdr_free(xdr_res_proc, (caddr_t)*list);
120 	} else {
121 		if ((*list = malloc(valsize)) == NULL) {
122 			(void) fprintf(stderr, gettext("Out of memory\n"));
123 			errno = ENOMEM;
124 			return (IDMAP_ERR_MEMORY);
125 		}
126 	}
127 	(void) memset(*list, 0, valsize);
128 
129 	clntstat = clnt_call(clnt, type,
130 		xdr_arg_proc, (caddr_t)arg,
131 		xdr_res_proc, (caddr_t)*list,
132 		TIMEOUT);
133 	if (clntstat != RPC_SUCCESS) {
134 		free(*list);
135 		if (__idmap_verbose)
136 			clnt_perror(clnt, me);
137 		return (IDMAP_ERR_RPC);
138 	}
139 	iter->retlist = *list;
140 	return (IDMAP_SUCCESS);
141 }
142