| 1 |
/* |
| 2 |
* Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* This Software is licensed under one of the following licenses: |
| 5 |
* |
| 6 |
* 1) under the terms of the "Common Public License 1.0" a copy of which is |
| 7 |
* in the file LICENSE.txt in the root directory. The license is also |
| 8 |
* available from the Open Source Initiative, see |
| 9 |
* http://www.opensource.org/licenses/cpl.php. |
| 10 |
* |
| 11 |
* 2) under the terms of the "The BSD License" a copy of which is in the file |
| 12 |
* LICENSE2.txt in the root directory. The license is also available from |
| 13 |
* the Open Source Initiative, see |
| 14 |
* http://www.opensource.org/licenses/bsd-license.php. |
| 15 |
* |
| 16 |
* 3) under the terms of the "GNU General Public License (GPL) Version 2" a |
| 17 |
* copy of which is in the file LICENSE3.txt in the root directory. The |
| 18 |
* license is also available from the Open Source Initiative, see |
| 19 |
* http://www.opensource.org/licenses/gpl-license.php. |
| 20 |
* |
| 21 |
* Licensee has the right to choose one of the above licenses. |
| 22 |
* |
| 23 |
* Redistributions of source code must retain the above copyright |
| 24 |
* notice and one of the license notices. |
| 25 |
* |
| 26 |
* Redistributions in binary form must reproduce both the above copyright |
| 27 |
* notice, one of the license notices in the documentation |
| 28 |
* and/or other materials provided with the distribution. |
| 29 |
*/ |
| 30 |
|
| 31 |
/********************************************************************** |
| 32 |
* |
| 33 |
* MODULE: dat_sr.c |
| 34 |
* |
| 35 |
* PURPOSE: static registry implementation |
| 36 |
* |
| 37 |
* $Id: dat_sr.c,v 1.17 2005/03/24 05:58:27 jlentini Exp $ |
| 38 |
**********************************************************************/ |
| 39 |
|
| 40 |
#include "dat_sr.h" |
| 41 |
|
| 42 |
#include "dat_dictionary.h" |
| 43 |
#include "udat_sr_parser.h" |
| 44 |
|
| 45 |
/********************************************************************* |
| 46 |
* * |
| 47 |
* Global Variables * |
| 48 |
* * |
| 49 |
*********************************************************************/ |
| 50 |
|
| 51 |
static DAT_OS_LOCK g_sr_lock; |
| 52 |
static DAT_DICTIONARY *g_sr_dictionary = NULL; |
| 53 |
|
| 54 |
/********************************************************************* |
| 55 |
* * |
| 56 |
* External Functions * |
| 57 |
* * |
| 58 |
*********************************************************************/ |
| 59 |
|
| 60 |
//*********************************************************************** |
| 61 |
// Function: dat_sr_init |
| 62 |
//*********************************************************************** |
| 63 |
|
| 64 |
DAT_RETURN dat_sr_init(void) |
| 65 |
{ |
| 66 |
DAT_RETURN status; |
| 67 |
|
| 68 |
status = dat_os_lock_init(&g_sr_lock); |
| 69 |
if (DAT_SUCCESS != status) { |
| 70 |
return status; |
| 71 |
} |
| 72 |
|
| 73 |
status = dat_dictionary_create(&g_sr_dictionary); |
| 74 |
if (DAT_SUCCESS != status) { |
| 75 |
return status; |
| 76 |
} |
| 77 |
|
| 78 |
/* |
| 79 |
* Since DAT allows providers to be loaded by either the static |
| 80 |
* registry or explicitly through OS dependent methods, do not |
| 81 |
* return an error if no providers are loaded via the static registry. |
| 82 |
*/ |
| 83 |
|
| 84 |
(void)dat_sr_load(); |
| 85 |
|
| 86 |
return DAT_SUCCESS; |
| 87 |
} |
| 88 |
|
| 89 |
//*********************************************************************** |
| 90 |
// Function: dat_sr_fini |
| 91 |
//*********************************************************************** |
| 92 |
|
| 93 |
extern DAT_RETURN dat_sr_fini(void) |
| 94 |
{ |
| 95 |
DAT_RETURN status; |
| 96 |
|
| 97 |
status = dat_sr_remove_all(g_sr_dictionary); |
| 98 |
if (DAT_SUCCESS != status) { |
| 99 |
return status; |
| 100 |
} |
| 101 |
|
| 102 |
status = dat_os_lock_destroy(&g_sr_lock); |
| 103 |
if (DAT_SUCCESS != status) { |
| 104 |
return status; |
| 105 |
} |
| 106 |
|
| 107 |
status = dat_dictionary_destroy(g_sr_dictionary); |
| 108 |
if (DAT_SUCCESS != status) { |
| 109 |
return status; |
| 110 |
} |
| 111 |
|
| 112 |
return DAT_SUCCESS; |
| 113 |
} |
| 114 |
|
| 115 |
//*********************************************************************** |
| 116 |
// Function: dat_sr_insert |
| 117 |
//*********************************************************************** |
| 118 |
|
| 119 |
extern DAT_RETURN |
| 120 |
dat_sr_insert(IN const DAT_PROVIDER_INFO * info, IN DAT_SR_ENTRY * entry) |
| 121 |
{ |
| 122 |
DAT_RETURN status; |
| 123 |
DAT_SR_ENTRY *data; |
| 124 |
DAT_OS_SIZE lib_path_len; |
| 125 |
DAT_OS_SIZE ia_params_len; |
| 126 |
DAT_DICTIONARY_ENTRY dict_entry; |
| 127 |
DAT_DICTIONARY_DATA prev_data; |
| 128 |
|
| 129 |
if (NULL == (data = dat_os_alloc(sizeof(DAT_SR_ENTRY)))) { |
| 130 |
status = |
| 131 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 132 |
goto bail; |
| 133 |
} |
| 134 |
|
| 135 |
dat_os_memset(data, '\0', sizeof(DAT_SR_ENTRY)); |
| 136 |
|
| 137 |
lib_path_len = strlen(entry->lib_path); |
| 138 |
data->lib_path_size = (lib_path_len + 1) * sizeof(char); |
| 139 |
|
| 140 |
if (NULL == (data->lib_path = dat_os_alloc(data->lib_path_size))) { |
| 141 |
status = |
| 142 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 143 |
goto bail; |
| 144 |
} |
| 145 |
|
| 146 |
dat_os_strncpy(data->lib_path, entry->lib_path, lib_path_len); |
| 147 |
data->lib_path[lib_path_len] = '\0'; |
| 148 |
|
| 149 |
ia_params_len = strlen(entry->ia_params); |
| 150 |
data->ia_params_size = (ia_params_len + 1) * sizeof(char); |
| 151 |
|
| 152 |
if (NULL == (data->ia_params = dat_os_alloc(data->ia_params_size))) { |
| 153 |
status = |
| 154 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 155 |
goto bail; |
| 156 |
} |
| 157 |
|
| 158 |
dat_os_strncpy(data->ia_params, entry->ia_params, ia_params_len); |
| 159 |
data->ia_params[ia_params_len] = '\0'; |
| 160 |
|
| 161 |
data->info = entry->info; |
| 162 |
data->lib_handle = entry->lib_handle; |
| 163 |
data->ref_count = entry->ref_count; |
| 164 |
data->next = NULL; |
| 165 |
|
| 166 |
dict_entry = NULL; |
| 167 |
status = dat_dictionary_entry_create(&dict_entry); |
| 168 |
if (DAT_SUCCESS != status) { |
| 169 |
goto bail; |
| 170 |
} |
| 171 |
|
| 172 |
dat_os_lock(&g_sr_lock); |
| 173 |
|
| 174 |
status = dat_dictionary_search(g_sr_dictionary, info, &prev_data); |
| 175 |
if (DAT_SUCCESS == status) { |
| 176 |
/* We already have a dictionary entry, so we don't need a new one. |
| 177 |
* This means there are multiple duplicate names in dat.conf, |
| 178 |
* but presumably they have different paths. Simply link the |
| 179 |
* new entry at the end of the chain of like-named entries. |
| 180 |
*/ |
| 181 |
(void)dat_dictionary_entry_destroy(dict_entry); |
| 182 |
dict_entry = NULL; |
| 183 |
|
| 184 |
/* Find the next available slot in this chain */ |
| 185 |
while (NULL != ((DAT_SR_ENTRY *) prev_data)->next) { |
| 186 |
prev_data = ((DAT_SR_ENTRY *) prev_data)->next; |
| 187 |
} |
| 188 |
dat_os_assert(NULL != prev_data); |
| 189 |
((DAT_SR_ENTRY *) prev_data)->next = data; |
| 190 |
} else { |
| 191 |
status = dat_dictionary_insert(g_sr_dictionary, |
| 192 |
dict_entry, |
| 193 |
info, |
| 194 |
(DAT_DICTIONARY_DATA *) data); |
| 195 |
} |
| 196 |
|
| 197 |
dat_os_unlock(&g_sr_lock); |
| 198 |
|
| 199 |
bail: |
| 200 |
if (DAT_SUCCESS != status) { |
| 201 |
if (NULL != data) { |
| 202 |
if (NULL != data->lib_path) { |
| 203 |
dat_os_free(data->lib_path, data->lib_path_size); |
| 204 |
} |
| 205 |
|
| 206 |
if (NULL != data->ia_params) { |
| 207 |
dat_os_free(data->ia_params, data->ia_params_size); |
| 208 |
} |
| 209 |
|
| 210 |
dat_os_free(data, sizeof(DAT_SR_ENTRY)); |
| 211 |
} |
| 212 |
|
| 213 |
if (NULL != dict_entry) { |
| 214 |
(void)dat_dictionary_entry_destroy(dict_entry); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return status; |
| 219 |
} |
| 220 |
|
| 221 |
//*********************************************************************** |
| 222 |
// Function: dat_sr_size |
| 223 |
//*********************************************************************** |
| 224 |
extern DAT_RETURN dat_sr_remove(IN const DAT_PROVIDER_INFO *info) |
| 225 |
{ |
| 226 |
DAT_DICTIONARY_ENTRY dict_entry = NULL; |
| 227 |
DAT_RETURN status = DAT_ERROR(DAT_PROVIDER_IN_USE, 0); |
| 228 |
DAT_SR_ENTRY *data; |
| 229 |
|
| 230 |
dat_os_lock(&g_sr_lock); |
| 231 |
status = dat_dictionary_search(g_sr_dictionary, info, |
| 232 |
(DAT_DICTIONARY_DATA)&data); |
| 233 |
if (DAT_SUCCESS != status) |
| 234 |
goto bail; |
| 235 |
|
| 236 |
if (0 != ((DAT_SR_ENTRY *) data)->ref_count) |
| 237 |
goto bail; |
| 238 |
|
| 239 |
status = dat_dictionary_remove(g_sr_dictionary, |
| 240 |
&dict_entry, info, |
| 241 |
(DAT_DICTIONARY_DATA)&data); |
| 242 |
if (DAT_SUCCESS != status) |
| 243 |
goto bail; |
| 244 |
|
| 245 |
dat_os_free(data->lib_path, data->lib_path_size); |
| 246 |
dat_os_free(data->ia_params, data->ia_params_size); |
| 247 |
dat_os_free(data, sizeof(DAT_SR_ENTRY)); |
| 248 |
bail: |
| 249 |
dat_os_unlock(&g_sr_lock); |
| 250 |
|
| 251 |
if (NULL != dict_entry) |
| 252 |
(void)dat_dictionary_entry_destroy(dict_entry); |
| 253 |
|
| 254 |
return status; |
| 255 |
} |
| 256 |
|
| 257 |
//*********************************************************************** |
| 258 |
// Function: dat_sr_size |
| 259 |
//*********************************************************************** |
| 260 |
|
| 261 |
extern DAT_RETURN dat_sr_size(OUT DAT_COUNT * size) |
| 262 |
{ |
| 263 |
return dat_dictionary_size(g_sr_dictionary, size); |
| 264 |
} |
| 265 |
|
| 266 |
//*********************************************************************** |
| 267 |
// Function: dat_sr_list |
| 268 |
//*********************************************************************** |
| 269 |
|
| 270 |
extern DAT_RETURN |
| 271 |
dat_sr_list(IN DAT_COUNT max_to_return, |
| 272 |
OUT DAT_COUNT * entries_returned, |
| 273 |
OUT DAT_PROVIDER_INFO * (dat_provider_list[])) |
| 274 |
{ |
| 275 |
DAT_SR_ENTRY **array; |
| 276 |
DAT_COUNT array_size; |
| 277 |
DAT_COUNT i; |
| 278 |
DAT_RETURN status; |
| 279 |
|
| 280 |
array = NULL; |
| 281 |
status = DAT_SUCCESS; |
| 282 |
|
| 283 |
/* The dictionary size may increase between the call to */ |
| 284 |
/* dat_dictionary_size() and dat_dictionary_enumerate(). */ |
| 285 |
/* Therefore we loop until a successful enumeration is made. */ |
| 286 |
*entries_returned = 0; |
| 287 |
for (;;) { |
| 288 |
status = dat_dictionary_size(g_sr_dictionary, &array_size); |
| 289 |
if (DAT_SUCCESS != status) { |
| 290 |
goto bail; |
| 291 |
} |
| 292 |
|
| 293 |
if (array_size == 0) { |
| 294 |
status = DAT_SUCCESS; |
| 295 |
goto bail; |
| 296 |
} |
| 297 |
|
| 298 |
array = dat_os_alloc(array_size * sizeof(DAT_SR_ENTRY *)); |
| 299 |
if (array == NULL) { |
| 300 |
status = |
| 301 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, |
| 302 |
DAT_RESOURCE_MEMORY); |
| 303 |
goto bail; |
| 304 |
} |
| 305 |
|
| 306 |
dat_os_lock(&g_sr_lock); |
| 307 |
|
| 308 |
status = dat_dictionary_enumerate(g_sr_dictionary, |
| 309 |
(DAT_DICTIONARY_DATA *) array, |
| 310 |
array_size); |
| 311 |
|
| 312 |
dat_os_unlock(&g_sr_lock); |
| 313 |
|
| 314 |
if (DAT_SUCCESS == status) { |
| 315 |
break; |
| 316 |
} else { |
| 317 |
dat_os_free(array, array_size * sizeof(DAT_SR_ENTRY *)); |
| 318 |
array = NULL; |
| 319 |
continue; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
for (i = 0; (i < max_to_return) && (i < array_size); i++) { |
| 324 |
if (NULL == dat_provider_list[i]) { |
| 325 |
status = |
| 326 |
DAT_ERROR(DAT_INVALID_PARAMETER, DAT_INVALID_ARG3); |
| 327 |
goto bail; |
| 328 |
} |
| 329 |
|
| 330 |
*dat_provider_list[i] = array[i]->info; |
| 331 |
} |
| 332 |
|
| 333 |
*entries_returned = i; |
| 334 |
|
| 335 |
bail: |
| 336 |
if (NULL != array) { |
| 337 |
dat_os_free(array, array_size * sizeof(DAT_SR_ENTRY *)); |
| 338 |
} |
| 339 |
|
| 340 |
return status; |
| 341 |
} |
| 342 |
|
| 343 |
//*********************************************************************** |
| 344 |
// Function: dat_sr_remove_all() |
| 345 |
//*********************************************************************** |
| 346 |
extern DAT_RETURN dat_sr_remove_all(IN DAT_DICTIONARY *p_dictionary) |
| 347 |
{ |
| 348 |
int i; |
| 349 |
DAT_RETURN status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 350 |
DAT_COUNT entries = 0; |
| 351 |
DAT_PROVIDER_INFO **plist; |
| 352 |
|
| 353 |
/* get provider count */ |
| 354 |
dat_sr_size(&entries); |
| 355 |
|
| 356 |
/* need array of pointers to info */ |
| 357 |
plist = dat_os_alloc(entries * sizeof(DAT_PROVIDER_INFO *)); |
| 358 |
if (plist == NULL) |
| 359 |
goto bail; |
| 360 |
|
| 361 |
dat_os_memset(plist, 0, entries * sizeof(DAT_PROVIDER_INFO *)); |
| 362 |
for (i = 0; i < entries; i++ ) { |
| 363 |
plist[i] = dat_os_alloc(sizeof(DAT_PROVIDER_INFO)); |
| 364 |
if (plist[i] == NULL) |
| 365 |
goto bail; |
| 366 |
} |
| 367 |
|
| 368 |
/* remove and destroy each SR entry */ |
| 369 |
status = dat_sr_list(entries, &entries, plist); |
| 370 |
if (DAT_SUCCESS != status) |
| 371 |
goto bail; |
| 372 |
|
| 373 |
for (i=0;i<entries;i++) { |
| 374 |
status = dat_sr_remove(plist[i]); |
| 375 |
if (DAT_SUCCESS != status) { |
| 376 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 377 |
" WARNING: libdat2 remove SR" |
| 378 |
" provider: IA %s busy\n", |
| 379 |
plist[i]->ia_name); |
| 380 |
goto bail; |
| 381 |
} |
| 382 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 383 |
" libdat2 removed SR" |
| 384 |
" provider: IA %s\n", |
| 385 |
plist[i]->ia_name); |
| 386 |
} |
| 387 |
bail: |
| 388 |
for (i = 0; i < entries; i++ ) { |
| 389 |
if (plist[i] != NULL) |
| 390 |
dat_os_free(plist[i], sizeof(DAT_PROVIDER_INFO)); |
| 391 |
} |
| 392 |
dat_os_free(plist, entries * sizeof(DAT_PROVIDER_INFO *)); |
| 393 |
|
| 394 |
return status; |
| 395 |
|
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
//*********************************************************************** |
| 400 |
// Function: dat_sr_provider_open |
| 401 |
//*********************************************************************** |
| 402 |
|
| 403 |
extern DAT_RETURN dat_sr_provider_open(IN const DAT_PROVIDER_INFO * info) |
| 404 |
{ |
| 405 |
DAT_RETURN status; |
| 406 |
DAT_SR_ENTRY *data; |
| 407 |
DAT_DICTIONARY_DATA dict_data; |
| 408 |
|
| 409 |
dat_os_lock(&g_sr_lock); |
| 410 |
|
| 411 |
status = dat_dictionary_search(g_sr_dictionary, info, &dict_data); |
| 412 |
|
| 413 |
if (DAT_SUCCESS == status) { |
| 414 |
data = (DAT_SR_ENTRY *) dict_data; |
| 415 |
while (data != NULL) { |
| 416 |
if (0 == data->ref_count) { |
| 417 |
/* |
| 418 |
* Try to open the path. If it fails, try the next |
| 419 |
* path in the chain. Only the first successful library |
| 420 |
* open matters, the others will be unused. |
| 421 |
*/ |
| 422 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 423 |
"DAT Registry: IA %s, trying to load library %s\n", |
| 424 |
data->info.ia_name, |
| 425 |
data->lib_path); |
| 426 |
|
| 427 |
status = dat_os_library_load(data->lib_path, |
| 428 |
&data->lib_handle); |
| 429 |
|
| 430 |
if (status == DAT_SUCCESS) { |
| 431 |
#ifdef DAT_DBG |
| 432 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 433 |
"DAT2 Registry: IA %s, loaded library %s\n", |
| 434 |
data->info.ia_name, |
| 435 |
data->lib_path); |
| 436 |
#endif |
| 437 |
data->ref_count++; |
| 438 |
data->init_func = |
| 439 |
dat_os_library_sym(data->lib_handle, |
| 440 |
DAT_PROVIDER_INIT_FUNC_STR); |
| 441 |
data->fini_func = |
| 442 |
dat_os_library_sym(data->lib_handle, |
| 443 |
DAT_PROVIDER_FINI_FUNC_STR); |
| 444 |
/* Warning: DAT and DAPL libraries not ext compatible */ |
| 445 |
#ifdef DAT_EXTENSIONS |
| 446 |
{ |
| 447 |
void *fncptr; |
| 448 |
|
| 449 |
fncptr = |
| 450 |
dat_os_library_sym(data-> |
| 451 |
lib_handle, |
| 452 |
"dapl_extensions"); |
| 453 |
|
| 454 |
if ((dat_os_library_error() != |
| 455 |
NULL) |
| 456 |
|| (fncptr == NULL)) { |
| 457 |
dat_os_dbg_print |
| 458 |
(DAT_OS_DBG_TYPE_SR, |
| 459 |
"DAT Registry: WARNING: library %s, " |
| 460 |
"extended DAT expected extended uDAPL: %s\n", |
| 461 |
data->lib_path, |
| 462 |
strerror(errno)); |
| 463 |
} |
| 464 |
} |
| 465 |
#endif |
| 466 |
if (NULL != data->init_func) { |
| 467 |
(*data->init_func) (&data->info, |
| 468 |
data-> |
| 469 |
ia_params); |
| 470 |
} else { |
| 471 |
dat_os_dbg_print |
| 472 |
(DAT_OS_DBG_TYPE_SR, |
| 473 |
"DAT Registry: Cannot find library init func (%s)\n", |
| 474 |
DAT_PROVIDER_INIT_FUNC_STR); |
| 475 |
} |
| 476 |
|
| 477 |
/* exit after we find the first valid entry */ |
| 478 |
break; |
| 479 |
} else { |
| 480 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 481 |
"DAT Registry: static registry unable to " |
| 482 |
"load library %s\n", |
| 483 |
data->lib_path); |
| 484 |
} |
| 485 |
} else { |
| 486 |
data->ref_count++; |
| 487 |
break; |
| 488 |
} |
| 489 |
data = data->next; |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
dat_os_unlock(&g_sr_lock); |
| 494 |
|
| 495 |
return status; |
| 496 |
} |
| 497 |
|
| 498 |
//*********************************************************************** |
| 499 |
// Function: dat_sr_provider_close |
| 500 |
//*********************************************************************** |
| 501 |
|
| 502 |
extern DAT_RETURN dat_sr_provider_close(IN const DAT_PROVIDER_INFO * info) |
| 503 |
{ |
| 504 |
DAT_RETURN status; |
| 505 |
DAT_SR_ENTRY *data; |
| 506 |
DAT_DICTIONARY_DATA dict_data; |
| 507 |
|
| 508 |
dat_os_lock(&g_sr_lock); |
| 509 |
|
| 510 |
status = dat_dictionary_search(g_sr_dictionary, info, &dict_data); |
| 511 |
|
| 512 |
if (DAT_SUCCESS == status) { |
| 513 |
data = (DAT_SR_ENTRY *) dict_data; |
| 514 |
while (data != NULL) { |
| 515 |
if (1 == data->ref_count) { |
| 516 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 517 |
"DAT Registry: IA %s, unloading library %s\n", |
| 518 |
data->info.ia_name, |
| 519 |
data->lib_path); |
| 520 |
|
| 521 |
if (NULL != data->fini_func) { |
| 522 |
(*data->fini_func) (&data->info); |
| 523 |
} |
| 524 |
|
| 525 |
status = |
| 526 |
dat_os_library_unload(data->lib_handle); |
| 527 |
if (status == DAT_SUCCESS) { |
| 528 |
data->ref_count--; |
| 529 |
} |
| 530 |
break; |
| 531 |
} else if (data->ref_count > 0) { |
| 532 |
data->ref_count--; |
| 533 |
break; |
| 534 |
} |
| 535 |
data = data->next; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
dat_os_unlock(&g_sr_lock); |
| 540 |
|
| 541 |
return status; |
| 542 |
} |