dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / udapl / libdat / common / dat_sr.c
blobc2619cafef9d65e0aaa49f3fb7d85212cf579a4d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "%Z%%M% %I% %E% SMI"
35 * MODULE: dat_sr.c
37 * PURPOSE: static registry implementation
39 * $Id: dat_sr.c,v 1.12 2003/08/20 14:28:40 hobie16 Exp $
43 #include "dat_sr.h"
45 #include "dat_dictionary.h"
46 #include "udat_sr_parser.h"
51 * Global Variables
55 static DAT_OS_LOCK g_sr_lock;
56 static DAT_DICTIONARY *g_sr_dictionary = NULL;
61 * External Functions
67 * Function: dat_sr_init
70 DAT_RETURN
71 dat_sr_init(void)
73 DAT_RETURN status;
75 status = dat_os_lock_init(&g_sr_lock);
76 if (DAT_SUCCESS != status) {
77 return (status);
80 status = dat_dictionary_create(&g_sr_dictionary);
81 if (DAT_SUCCESS != status) {
82 return (status);
86 * Since DAT allows providers to be loaded by either the static
87 * registry or explicitly through OS dependent methods, do not
88 * return an error if no providers are loaded via the static registry.
91 (void) dat_sr_load();
93 return (DAT_SUCCESS);
98 * Function: dat_sr_fini
101 extern DAT_RETURN
102 dat_sr_fini(void)
104 DAT_RETURN status;
106 status = dat_os_lock_destroy(&g_sr_lock);
107 if (DAT_SUCCESS != status) {
108 return (status);
111 status = dat_dictionary_destroy(g_sr_dictionary);
112 if (DAT_SUCCESS != status) {
113 return (status);
116 return (DAT_SUCCESS);
121 * Function: dat_sr_insert
124 extern DAT_RETURN
125 dat_sr_insert(
126 IN const DAT_PROVIDER_INFO *info,
127 IN DAT_SR_ENTRY *entry)
129 DAT_RETURN status;
130 DAT_SR_ENTRY *data;
131 DAT_OS_SIZE lib_path_size;
132 DAT_OS_SIZE lib_path_len;
133 DAT_OS_SIZE ia_params_size;
134 DAT_OS_SIZE ia_params_len;
135 DAT_DICTIONARY_ENTRY dict_entry;
137 if (NULL == (data = dat_os_alloc(sizeof (DAT_SR_ENTRY)))) {
138 status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
139 DAT_RESOURCE_MEMORY);
140 goto bail;
143 lib_path_len = strlen(entry->lib_path);
144 lib_path_size = (lib_path_len + 1) * sizeof (char);
146 if (NULL == (data->lib_path = dat_os_alloc(lib_path_size))) {
147 status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
148 DAT_RESOURCE_MEMORY);
149 goto bail;
152 (void) dat_os_strncpy(data->lib_path, entry->lib_path, lib_path_len);
153 data->lib_path[lib_path_len] = '\0';
155 ia_params_len = strlen(entry->ia_params);
156 ia_params_size = (ia_params_len + 1) * sizeof (char);
158 if (NULL == (data->ia_params = dat_os_alloc(ia_params_size))) {
159 status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
160 DAT_RESOURCE_MEMORY);
161 goto bail;
164 (void) dat_os_strncpy(data->ia_params, entry->ia_params, ia_params_len);
165 data->ia_params[ia_params_len] = '\0';
167 data->info = entry->info;
168 data->lib_handle = entry->lib_handle;
169 data->ref_count = entry->ref_count;
171 dict_entry = NULL;
172 status = dat_dictionary_entry_create(&dict_entry);
173 if (DAT_SUCCESS != status) {
174 goto bail;
177 dat_os_lock(&g_sr_lock);
179 status = dat_dictionary_insert(g_sr_dictionary,
180 dict_entry,
181 info,
182 (DAT_DICTIONARY_DATA *)data);
183 dat_os_unlock(&g_sr_lock);
185 bail:
186 if (DAT_SUCCESS != status) {
187 if (NULL != data) {
188 if (NULL != data->lib_path) {
189 dat_os_free(data->lib_path, lib_path_size);
192 if (NULL != data->ia_params) {
193 dat_os_free(data->ia_params, ia_params_size);
196 dat_os_free(data, sizeof (DAT_SR_ENTRY));
199 if (NULL != dict_entry) {
200 (void) dat_dictionary_entry_destroy(dict_entry);
204 return (status);
209 * Function: dat_sr_size
212 extern DAT_RETURN
213 dat_sr_size(
214 OUT DAT_COUNT *size)
216 return (dat_dictionary_size(g_sr_dictionary, size));
221 * Function: dat_sr_list
224 extern DAT_RETURN
225 dat_sr_list(
226 IN DAT_COUNT max_to_return,
227 OUT DAT_COUNT *entries_returned,
228 OUT DAT_PROVIDER_INFO * (dat_provider_list[]))
230 DAT_SR_ENTRY **array;
231 DAT_COUNT array_size;
232 DAT_COUNT i;
233 DAT_RETURN status;
235 array = NULL;
236 status = DAT_SUCCESS;
239 * The dictionary size may increase between the call to
240 * dat_dictionary_size() and dat_dictionary_enumerate().
241 * Therefore we loop until a successful enumeration is made.
243 *entries_returned = 0;
244 for (;;) {
245 status = dat_dictionary_size(g_sr_dictionary, &array_size);
246 if (DAT_SUCCESS != status) {
247 goto bail;
250 if (array_size == 0) {
251 status = DAT_SUCCESS;
252 goto bail;
255 array = dat_os_alloc(array_size * sizeof (DAT_SR_ENTRY *));
256 if (array == NULL) {
257 status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
258 DAT_RESOURCE_MEMORY);
259 goto bail;
262 dat_os_lock(&g_sr_lock);
264 status = dat_dictionary_enumerate(g_sr_dictionary,
265 (DAT_DICTIONARY_DATA *) array,
266 array_size);
268 dat_os_unlock(&g_sr_lock);
270 if (DAT_SUCCESS == status) {
271 break;
272 } else {
273 dat_os_free(array,
274 array_size * sizeof (DAT_SR_ENTRY *));
275 array = NULL;
276 continue;
280 for (i = 0; (i < max_to_return) && (i < array_size); i++) {
281 if (NULL == dat_provider_list[i]) {
282 status = DAT_ERROR(DAT_INVALID_PARAMETER,
283 DAT_INVALID_ARG3);
284 goto bail;
287 *dat_provider_list[i] = array[i]->info;
290 *entries_returned = i;
292 bail:
293 if (NULL != array) {
294 dat_os_free(array, array_size * sizeof (DAT_SR_ENTRY *));
297 return (status);
303 * Function: dat_sr_provider_open
306 extern DAT_RETURN
307 dat_sr_provider_open(
308 IN const DAT_PROVIDER_INFO *info)
310 DAT_RETURN status;
311 DAT_SR_ENTRY *data;
313 dat_os_lock(&g_sr_lock);
315 status = dat_dictionary_search(g_sr_dictionary,
316 info,
317 (DAT_DICTIONARY_DATA *) &data);
319 if (DAT_SUCCESS == status) {
320 if (0 == data->ref_count) {
321 status = dat_os_library_load(data->lib_path,
322 &data->lib_handle);
323 if (status == DAT_SUCCESS) {
324 data->ref_count++;
325 } else {
326 dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
327 "DAT Registry: static registry unable to "
328 "load library %s\n", data->lib_path);
329 goto bail;
331 data->init_func = (DAT_PROVIDER_INIT_FUNC)
332 dat_os_library_sym(data->lib_handle,
333 DAT_PROVIDER_INIT_FUNC_STR);
334 data->fini_func = (DAT_PROVIDER_FINI_FUNC)
335 dat_os_library_sym(data->lib_handle,
336 DAT_PROVIDER_FINI_FUNC_STR);
338 if (NULL != data->init_func) {
339 (*data->init_func)(&data->info,
340 data->ia_params);
342 } else {
343 data->ref_count++;
347 bail:
348 dat_os_unlock(&g_sr_lock);
350 return (status);
355 * Function: dat_sr_provider_close
358 extern DAT_RETURN
359 dat_sr_provider_close(
360 IN const DAT_PROVIDER_INFO *info)
362 DAT_RETURN status;
363 DAT_SR_ENTRY *data;
365 dat_os_lock(&g_sr_lock);
367 status = dat_dictionary_search(g_sr_dictionary,
368 info,
369 (DAT_DICTIONARY_DATA *)&data);
371 if (DAT_SUCCESS == status) {
372 if (1 == data->ref_count) {
373 if (NULL != data->fini_func) {
374 (*data->fini_func)(&data->info);
377 status = dat_os_library_unload(data->lib_handle);
378 if (status == DAT_SUCCESS) {
379 data->ref_count--;
381 } else {
382 data->ref_count--;
386 dat_os_unlock(&g_sr_lock);
388 return (status);