xref: /titanic_44/usr/src/lib/libidmap/common/utils.c (revision 30a5e8fa1253cb33980ee4514743cf683f584b4e)
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 idmap_retcode
45 _udt_extend_batch(idmap_udt_handle_t *udthandle) {
46 	idmap_update_op	*tmplist;
47 	size_t		nsize;
48 
49 	if (udthandle->next >= udthandle->batch.idmap_update_batch_len) {
50 		nsize = (udthandle->batch.idmap_update_batch_len +
51 				_UDT_SIZE_INCR) * sizeof (*tmplist);
52 		tmplist = realloc(
53 			udthandle->batch.idmap_update_batch_val, nsize);
54 		if (tmplist == NULL)
55 			return (IDMAP_ERR_MEMORY);
56 		(void) memset((uchar_t *)tmplist +
57 			(udthandle->batch.idmap_update_batch_len *
58 			sizeof (*tmplist)), 0,
59 			_UDT_SIZE_INCR * sizeof (*tmplist));
60 		udthandle->batch.idmap_update_batch_val = tmplist;
61 		udthandle->batch.idmap_update_batch_len += _UDT_SIZE_INCR;
62 	}
63 	udthandle->batch.idmap_update_batch_val[udthandle->next].opnum =
64 		OP_NONE;
65 	return (IDMAP_SUCCESS);
66 }
67 
68 idmap_retcode
69 _get_ids_extend_batch(idmap_get_handle_t *gh) {
70 	idmap_mapping	*t1;
71 	idmap_get_res_t	*t2;
72 	size_t		nsize, len;
73 
74 	len = gh->batch.idmap_mapping_batch_len;
75 	if (gh->next >= len) {
76 		/* extend the request array */
77 		nsize = (len + _GET_IDS_SIZE_INCR) * sizeof (*t1);
78 		t1 = realloc(gh->batch.idmap_mapping_batch_val, nsize);
79 		if (t1 == NULL)
80 			return (IDMAP_ERR_MEMORY);
81 		(void) memset((uchar_t *)t1 + (len * sizeof (*t1)), 0,
82 			_GET_IDS_SIZE_INCR * sizeof (*t1));
83 		gh->batch.idmap_mapping_batch_val = t1;
84 
85 		/* extend the return list */
86 		nsize = (len + _GET_IDS_SIZE_INCR) * sizeof (*t2);
87 		t2 = realloc(gh->retlist, nsize);
88 		if (t2 == NULL)
89 			return (IDMAP_ERR_MEMORY);
90 		(void) memset((uchar_t *)t2 + (len * sizeof (*t2)), 0,
91 			_GET_IDS_SIZE_INCR * sizeof (*t2));
92 		gh->retlist = t2;
93 
94 		gh->batch.idmap_mapping_batch_len += _GET_IDS_SIZE_INCR;
95 	}
96 	return (IDMAP_SUCCESS);
97 }
98 
99 idmap_stat
100 _iter_get_next_list(int type, idmap_iter_t *iter,
101 		void *arg, uchar_t **list, size_t valsize,
102 		xdrproc_t xdr_arg_proc, xdrproc_t xdr_res_proc) {
103 
104 	CLIENT		*clnt;
105 	enum clnt_stat	clntstat;
106 
107 	iter->next = 0;
108 	iter->retlist = NULL;
109 	_IDMAP_GET_CLIENT_HANDLE(iter->ih, clnt);
110 
111 	/* init the result */
112 	if (*list) {
113 		xdr_free(xdr_res_proc, (caddr_t)*list);
114 	} else {
115 		if ((*list = malloc(valsize)) == NULL) {
116 			errno = ENOMEM;
117 			return (IDMAP_ERR_MEMORY);
118 		}
119 	}
120 	(void) memset(*list, 0, valsize);
121 
122 	clntstat = clnt_call(clnt, type,
123 		xdr_arg_proc, (caddr_t)arg,
124 		xdr_res_proc, (caddr_t)*list,
125 		TIMEOUT);
126 	if (clntstat != RPC_SUCCESS) {
127 		free(*list);
128 		return (_idmap_rpc2stat(clnt));
129 	}
130 	iter->retlist = *list;
131 	return (IDMAP_SUCCESS);
132 }
133 
134 idmap_stat
135 _idmap_rpc2stat(CLIENT *clnt) {
136 	/*
137 	 * We only deal with door_call(3C) errors here. We look at
138 	 * r_err.re_errno instead of r_err.re_status because we need
139 	 * to differentiate between RPC failures caused by bad door fd
140 	 * and others.
141 	 */
142 	struct rpc_err r_err;
143 	if (clnt) {
144 		clnt_geterr(clnt, &r_err);
145 		errno = r_err.re_errno;
146 		switch (r_err.re_errno) {
147 		case ENOMEM:
148 			return (IDMAP_ERR_MEMORY);
149 		case EBADF:
150 			return (IDMAP_ERR_RPC_HANDLE);
151 		default:
152 			return (IDMAP_ERR_RPC);
153 		}
154 	}
155 
156 	/* null handle */
157 	return (IDMAP_ERR_RPC_HANDLE);
158 }
159