| 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_dr.c |
| 34 |
* |
| 35 |
* PURPOSE: dynamic registry implementation |
| 36 |
* |
| 37 |
* $Id: dat_dr.c,v 1.17 2005/03/24 05:58:27 jlentini Exp $ |
| 38 |
**********************************************************************/ |
| 39 |
|
| 40 |
#include <dat2/dat_platform_specific.h> |
| 41 |
#include "dat_dr.h" |
| 42 |
#include "dat_dictionary.h" |
| 43 |
|
| 44 |
/********************************************************************* |
| 45 |
* * |
| 46 |
* Global Variables * |
| 47 |
* * |
| 48 |
*********************************************************************/ |
| 49 |
|
| 50 |
static DAT_OS_LOCK g_dr_lock; |
| 51 |
static DAT_DICTIONARY *g_dr_dictionary = NULL; |
| 52 |
|
| 53 |
/********************************************************************* |
| 54 |
* * |
| 55 |
* External Functions * |
| 56 |
* * |
| 57 |
*********************************************************************/ |
| 58 |
|
| 59 |
//*********************************************************************** |
| 60 |
// Function: dat_dr_init |
| 61 |
//*********************************************************************** |
| 62 |
|
| 63 |
DAT_RETURN dat_dr_init(void) |
| 64 |
{ |
| 65 |
DAT_RETURN status; |
| 66 |
|
| 67 |
status = dat_os_lock_init(&g_dr_lock); |
| 68 |
if (DAT_SUCCESS != status) { |
| 69 |
return status; |
| 70 |
} |
| 71 |
|
| 72 |
status = dat_dictionary_create(&g_dr_dictionary); |
| 73 |
if (DAT_SUCCESS != status) { |
| 74 |
return status; |
| 75 |
} |
| 76 |
|
| 77 |
return DAT_SUCCESS; |
| 78 |
} |
| 79 |
|
| 80 |
//*********************************************************************** |
| 81 |
// Function: dat_dr_fini |
| 82 |
//*********************************************************************** |
| 83 |
|
| 84 |
DAT_RETURN dat_dr_fini(void) |
| 85 |
{ |
| 86 |
DAT_RETURN status; |
| 87 |
|
| 88 |
status = dat_os_lock_destroy(&g_dr_lock); |
| 89 |
if (DAT_SUCCESS != status) { |
| 90 |
return status; |
| 91 |
} |
| 92 |
|
| 93 |
status = dat_dictionary_destroy(g_dr_dictionary); |
| 94 |
if (DAT_SUCCESS != status) { |
| 95 |
return status; |
| 96 |
} |
| 97 |
|
| 98 |
return DAT_SUCCESS; |
| 99 |
} |
| 100 |
|
| 101 |
//*********************************************************************** |
| 102 |
// Function: dat_dr_insert |
| 103 |
//*********************************************************************** |
| 104 |
|
| 105 |
DAT_RETURN |
| 106 |
dat_dr_insert(IN const DAT_PROVIDER_INFO * info, IN DAT_DR_ENTRY * entry) |
| 107 |
{ |
| 108 |
DAT_RETURN status; |
| 109 |
DAT_DICTIONARY_ENTRY dict_entry = NULL; |
| 110 |
DAT_DR_ENTRY *data; |
| 111 |
|
| 112 |
data = dat_os_alloc(sizeof(DAT_DR_ENTRY)); |
| 113 |
if (NULL == data) { |
| 114 |
status = |
| 115 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 116 |
goto bail; |
| 117 |
} |
| 118 |
|
| 119 |
*data = *entry; |
| 120 |
|
| 121 |
status = dat_dictionary_entry_create(&dict_entry); |
| 122 |
if (DAT_SUCCESS != status) { |
| 123 |
goto bail; |
| 124 |
} |
| 125 |
|
| 126 |
dat_os_lock(&g_dr_lock); |
| 127 |
|
| 128 |
status = dat_dictionary_insert(g_dr_dictionary, |
| 129 |
dict_entry, |
| 130 |
info, (DAT_DICTIONARY_DATA *) data); |
| 131 |
|
| 132 |
dat_os_unlock(&g_dr_lock); |
| 133 |
|
| 134 |
bail: |
| 135 |
if (DAT_SUCCESS != status) { |
| 136 |
if (NULL != data) { |
| 137 |
dat_os_free(data, sizeof(DAT_DR_ENTRY)); |
| 138 |
} |
| 139 |
|
| 140 |
if (NULL != dict_entry) { |
| 141 |
(void)dat_dictionary_entry_destroy(dict_entry); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return status; |
| 146 |
} |
| 147 |
|
| 148 |
//*********************************************************************** |
| 149 |
// Function: dat_dr_remove |
| 150 |
//*********************************************************************** |
| 151 |
|
| 152 |
DAT_RETURN dat_dr_remove(IN const DAT_PROVIDER_INFO * info) |
| 153 |
{ |
| 154 |
DAT_DICTIONARY_ENTRY dict_entry; |
| 155 |
DAT_RETURN status; |
| 156 |
DAT_DICTIONARY_DATA data; |
| 157 |
|
| 158 |
dict_entry = NULL; |
| 159 |
dat_os_lock(&g_dr_lock); |
| 160 |
|
| 161 |
status = dat_dictionary_search(g_dr_dictionary, info, &data); |
| 162 |
|
| 163 |
if (DAT_SUCCESS != status) { |
| 164 |
/* return status from dat_dictionary_search() */ |
| 165 |
goto bail; |
| 166 |
} |
| 167 |
|
| 168 |
if (0 != ((DAT_DR_ENTRY *) data)->ref_count) { |
| 169 |
status = DAT_ERROR(DAT_PROVIDER_IN_USE, 0); |
| 170 |
goto bail; |
| 171 |
} |
| 172 |
|
| 173 |
status = dat_dictionary_remove(g_dr_dictionary, |
| 174 |
&dict_entry, info, &data); |
| 175 |
if (DAT_SUCCESS != status) { |
| 176 |
/* return status from dat_dictionary_remove() */ |
| 177 |
goto bail; |
| 178 |
} |
| 179 |
|
| 180 |
dat_os_free(data, sizeof(DAT_DR_ENTRY)); |
| 181 |
|
| 182 |
bail: |
| 183 |
dat_os_unlock(&g_dr_lock); |
| 184 |
|
| 185 |
if (NULL != dict_entry) { |
| 186 |
(void)dat_dictionary_entry_destroy(dict_entry); |
| 187 |
} |
| 188 |
|
| 189 |
return status; |
| 190 |
} |
| 191 |
|
| 192 |
//*********************************************************************** |
| 193 |
// Function: dat_dr_provider_open |
| 194 |
//*********************************************************************** |
| 195 |
|
| 196 |
DAT_RETURN |
| 197 |
dat_dr_provider_open(IN const DAT_PROVIDER_INFO * info, |
| 198 |
OUT DAT_IA_OPEN_FUNC * p_ia_open_func) |
| 199 |
{ |
| 200 |
DAT_RETURN status; |
| 201 |
DAT_DICTIONARY_DATA data; |
| 202 |
|
| 203 |
dat_os_lock(&g_dr_lock); |
| 204 |
status = dat_dictionary_search(g_dr_dictionary, info, &data); |
| 205 |
dat_os_unlock(&g_dr_lock); |
| 206 |
|
| 207 |
if (DAT_SUCCESS == status) { |
| 208 |
((DAT_DR_ENTRY *) data)->ref_count++; |
| 209 |
*p_ia_open_func = ((DAT_DR_ENTRY *) data)->ia_open_func; |
| 210 |
} |
| 211 |
|
| 212 |
return status; |
| 213 |
} |
| 214 |
|
| 215 |
//*********************************************************************** |
| 216 |
// Function: dat_dr_provider_open_ext |
| 217 |
//*********************************************************************** |
| 218 |
#ifdef DAT_EXTENSIONS |
| 219 |
DAT_RETURN |
| 220 |
dat_dr_provider_open_ext(IN const DAT_PROVIDER_INFO *info, |
| 221 |
OUT DAT_HANDLE_EXTENDEDOP_FUNC *p_ext_func) |
| 222 |
{ |
| 223 |
DAT_RETURN status; |
| 224 |
DAT_DICTIONARY_DATA data; |
| 225 |
|
| 226 |
dat_os_lock(&g_dr_lock); |
| 227 |
status = dat_dictionary_search(g_dr_dictionary, info, &data); |
| 228 |
dat_os_unlock(&g_dr_lock); |
| 229 |
|
| 230 |
if (DAT_SUCCESS == status) { |
| 231 |
((DAT_DR_ENTRY *) data)->ref_count++; |
| 232 |
*p_ext_func = ((DAT_DR_ENTRY *)data)->ia_ext_func; |
| 233 |
} |
| 234 |
|
| 235 |
return status; |
| 236 |
} |
| 237 |
#endif |
| 238 |
|
| 239 |
//*********************************************************************** |
| 240 |
// Function: dat_dr_provider_close |
| 241 |
//*********************************************************************** |
| 242 |
|
| 243 |
DAT_RETURN dat_dr_provider_close(IN const DAT_PROVIDER_INFO * info) |
| 244 |
{ |
| 245 |
DAT_RETURN status; |
| 246 |
DAT_DICTIONARY_DATA data; |
| 247 |
|
| 248 |
dat_os_lock(&g_dr_lock); |
| 249 |
status = dat_dictionary_search(g_dr_dictionary, info, &data); |
| 250 |
dat_os_unlock(&g_dr_lock); |
| 251 |
|
| 252 |
if (DAT_SUCCESS == status) { |
| 253 |
((DAT_DR_ENTRY *) data)->ref_count--; |
| 254 |
} |
| 255 |
|
| 256 |
return status; |
| 257 |
} |
| 258 |
|
| 259 |
//*********************************************************************** |
| 260 |
// Function: dat_dr_size |
| 261 |
//*********************************************************************** |
| 262 |
|
| 263 |
DAT_RETURN dat_dr_size(OUT DAT_COUNT * size) |
| 264 |
{ |
| 265 |
return dat_dictionary_size(g_dr_dictionary, size); |
| 266 |
} |
| 267 |
|
| 268 |
//*********************************************************************** |
| 269 |
// Function: dat_dr_list |
| 270 |
//*********************************************************************** |
| 271 |
|
| 272 |
DAT_RETURN |
| 273 |
dat_dr_list(IN DAT_COUNT max_to_return, |
| 274 |
OUT DAT_COUNT * entries_returned, |
| 275 |
OUT DAT_PROVIDER_INFO * (dat_provider_list[])) |
| 276 |
{ |
| 277 |
DAT_DR_ENTRY **array; |
| 278 |
DAT_COUNT array_size; |
| 279 |
DAT_COUNT i; |
| 280 |
DAT_RETURN status; |
| 281 |
|
| 282 |
array = NULL; |
| 283 |
status = DAT_SUCCESS; |
| 284 |
|
| 285 |
/* The dictionary size may increase between the call to */ |
| 286 |
/* dat_dictionary_size() and dat_dictionary_enumerate(). */ |
| 287 |
/* Therefore we loop until a successful enumeration is made. */ |
| 288 |
*entries_returned = 0; |
| 289 |
for (;;) { |
| 290 |
status = dat_dictionary_size(g_dr_dictionary, &array_size); |
| 291 |
if (status != DAT_SUCCESS) { |
| 292 |
goto bail; |
| 293 |
} |
| 294 |
|
| 295 |
if (array_size == 0) { |
| 296 |
status = DAT_SUCCESS; |
| 297 |
goto bail; |
| 298 |
} |
| 299 |
|
| 300 |
array = dat_os_alloc(array_size * sizeof(DAT_DR_ENTRY *)); |
| 301 |
if (array == NULL) { |
| 302 |
status = |
| 303 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, |
| 304 |
DAT_RESOURCE_MEMORY); |
| 305 |
goto bail; |
| 306 |
} |
| 307 |
|
| 308 |
dat_os_lock(&g_dr_lock); |
| 309 |
|
| 310 |
status = dat_dictionary_enumerate(g_dr_dictionary, |
| 311 |
(DAT_DICTIONARY_DATA *) array, |
| 312 |
array_size); |
| 313 |
|
| 314 |
dat_os_unlock(&g_dr_lock); |
| 315 |
|
| 316 |
if (DAT_SUCCESS == status) { |
| 317 |
break; |
| 318 |
} else { |
| 319 |
dat_os_free(array, array_size * sizeof(DAT_DR_ENTRY *)); |
| 320 |
array = NULL; |
| 321 |
continue; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
for (i = 0; (i < max_to_return) && (i < array_size); i++) { |
| 326 |
if (NULL == dat_provider_list[i]) { |
| 327 |
status = |
| 328 |
DAT_ERROR(DAT_INVALID_PARAMETER, DAT_INVALID_ARG3); |
| 329 |
goto bail; |
| 330 |
} |
| 331 |
|
| 332 |
*dat_provider_list[i] = array[i]->info; |
| 333 |
} |
| 334 |
|
| 335 |
*entries_returned = i; |
| 336 |
|
| 337 |
bail: |
| 338 |
if (NULL != array) { |
| 339 |
dat_os_free(array, array_size * sizeof(DAT_DR_ENTRY *)); |
| 340 |
} |
| 341 |
|
| 342 |
return status; |
| 343 |
} |
| 344 |
|
| 345 |
/* |
| 346 |
* Local variables: |
| 347 |
* c-indent-level: 4 |
| 348 |
* c-basic-offset: 4 |
| 349 |
* tab-width: 8 |
| 350 |
* End: |
| 351 |
*/ |