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
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]
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"
37 * PURPOSE: static registry implementation
39 * $Id: dat_sr.c,v 1.12 2003/08/20 14:28:40 hobie16 Exp $
45 #include "dat_dictionary.h"
46 #include "udat_sr_parser.h"
55 static DAT_OS_LOCK g_sr_lock
;
56 static DAT_DICTIONARY
*g_sr_dictionary
= NULL
;
67 * Function: dat_sr_init
75 status
= dat_os_lock_init(&g_sr_lock
);
76 if (DAT_SUCCESS
!= status
) {
80 status
= dat_dictionary_create(&g_sr_dictionary
);
81 if (DAT_SUCCESS
!= 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.
98 * Function: dat_sr_fini
106 status
= dat_os_lock_destroy(&g_sr_lock
);
107 if (DAT_SUCCESS
!= status
) {
111 status
= dat_dictionary_destroy(g_sr_dictionary
);
112 if (DAT_SUCCESS
!= status
) {
116 return (DAT_SUCCESS
);
121 * Function: dat_sr_insert
126 IN
const DAT_PROVIDER_INFO
*info
,
127 IN DAT_SR_ENTRY
*entry
)
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
);
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
);
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
);
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
;
172 status
= dat_dictionary_entry_create(&dict_entry
);
173 if (DAT_SUCCESS
!= status
) {
177 dat_os_lock(&g_sr_lock
);
179 status
= dat_dictionary_insert(g_sr_dictionary
,
182 (DAT_DICTIONARY_DATA
*)data
);
183 dat_os_unlock(&g_sr_lock
);
186 if (DAT_SUCCESS
!= status
) {
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
);
209 * Function: dat_sr_size
216 return (dat_dictionary_size(g_sr_dictionary
, size
));
221 * Function: 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
;
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;
245 status
= dat_dictionary_size(g_sr_dictionary
, &array_size
);
246 if (DAT_SUCCESS
!= status
) {
250 if (array_size
== 0) {
251 status
= DAT_SUCCESS
;
255 array
= dat_os_alloc(array_size
* sizeof (DAT_SR_ENTRY
*));
257 status
= DAT_ERROR(DAT_INSUFFICIENT_RESOURCES
,
258 DAT_RESOURCE_MEMORY
);
262 dat_os_lock(&g_sr_lock
);
264 status
= dat_dictionary_enumerate(g_sr_dictionary
,
265 (DAT_DICTIONARY_DATA
*) array
,
268 dat_os_unlock(&g_sr_lock
);
270 if (DAT_SUCCESS
== status
) {
274 array_size
* sizeof (DAT_SR_ENTRY
*));
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
,
287 *dat_provider_list
[i
] = array
[i
]->info
;
290 *entries_returned
= i
;
294 dat_os_free(array
, array_size
* sizeof (DAT_SR_ENTRY
*));
303 * Function: dat_sr_provider_open
307 dat_sr_provider_open(
308 IN
const DAT_PROVIDER_INFO
*info
)
313 dat_os_lock(&g_sr_lock
);
315 status
= dat_dictionary_search(g_sr_dictionary
,
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
,
323 if (status
== DAT_SUCCESS
) {
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
);
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
,
348 dat_os_unlock(&g_sr_lock
);
355 * Function: dat_sr_provider_close
359 dat_sr_provider_close(
360 IN
const DAT_PROVIDER_INFO
*info
)
365 dat_os_lock(&g_sr_lock
);
367 status
= dat_dictionary_search(g_sr_dictionary
,
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
) {
386 dat_os_unlock(&g_sr_lock
);