File: dat_sr.c

package info (click to toggle)
dapl 2.0.19-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,300 kB
  • sloc: ansic: 39,217; sh: 10,475; makefile: 529
file content (447 lines) | stat: -rwxr-xr-x 12,255 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
 *
 * This Software is licensed under one of the following licenses:
 *
 * 1) under the terms of the "Common Public License 1.0" a copy of which is
 *    in the file LICENSE.txt in the root directory. The license is also
 *    available from the Open Source Initiative, see
 *    http://www.opensource.org/licenses/cpl.php.
 *
 * 2) under the terms of the "The BSD License" a copy of which is in the file
 *    LICENSE2.txt in the root directory. The license is also available from
 *    the Open Source Initiative, see
 *    http://www.opensource.org/licenses/bsd-license.php.
 *
 * 3) under the terms of the "GNU General Public License (GPL) Version 2" a 
 *    copy of which is in the file LICENSE3.txt in the root directory. The 
 *    license is also available from the Open Source Initiative, see
 *    http://www.opensource.org/licenses/gpl-license.php.
 *
 * Licensee has the right to choose one of the above licenses.
 *
 * Redistributions of source code must retain the above copyright
 * notice and one of the license notices.
 *
 * Redistributions in binary form must reproduce both the above copyright
 * notice, one of the license notices in the documentation
 * and/or other materials provided with the distribution.
 */

/**********************************************************************
 *
 * MODULE: dat_sr.c
 *
 * PURPOSE: static registry implementation
 *
 * $Id: dat_sr.c,v 1.17 2005/03/24 05:58:27 jlentini Exp $
 **********************************************************************/

#include "dat_sr.h"

#include "dat_dictionary.h"
#include "udat_sr_parser.h"

/*********************************************************************
 *                                                                   *
 * Global Variables                                                  *
 *                                                                   *
 *********************************************************************/

static DAT_OS_LOCK g_sr_lock;
static DAT_DICTIONARY *g_sr_dictionary = NULL;

/*********************************************************************
 *                                                                   *
 * External Functions                                                *
 *                                                                   *
 *********************************************************************/

//***********************************************************************
// Function: dat_sr_init
//***********************************************************************

DAT_RETURN dat_sr_init(void)
{
	DAT_RETURN status;

	status = dat_os_lock_init(&g_sr_lock);
	if (DAT_SUCCESS != status) {
		return status;
	}

	status = dat_dictionary_create(&g_sr_dictionary);
	if (DAT_SUCCESS != status) {
		return status;
	}

	/*
	 * Since DAT allows providers to be loaded by either the static
	 * registry or explicitly through OS dependent methods, do not
	 * return an error if no providers are loaded via the static registry.
	 */

	(void)dat_sr_load();

	return DAT_SUCCESS;
}

//***********************************************************************
// Function: dat_sr_fini
//***********************************************************************

extern DAT_RETURN dat_sr_fini(void)
{
	DAT_RETURN status;

	status = dat_os_lock_destroy(&g_sr_lock);
	if (DAT_SUCCESS != status) {
		return status;
	}

	status = dat_dictionary_destroy(g_sr_dictionary);
	if (DAT_SUCCESS != status) {
		return status;
	}

	return DAT_SUCCESS;
}

//***********************************************************************
// Function: dat_sr_insert
//***********************************************************************

extern DAT_RETURN
dat_sr_insert(IN const DAT_PROVIDER_INFO * info, IN DAT_SR_ENTRY * entry)
{
	DAT_RETURN status;
	DAT_SR_ENTRY *data;
	DAT_OS_SIZE lib_path_size;
	DAT_OS_SIZE lib_path_len;
	DAT_OS_SIZE ia_params_size;
	DAT_OS_SIZE ia_params_len;
	DAT_DICTIONARY_ENTRY dict_entry;
	DAT_DICTIONARY_DATA prev_data;

	if (NULL == (data = dat_os_alloc(sizeof(DAT_SR_ENTRY)))) {
		status =
		    DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY);
		goto bail;
	}

	dat_os_memset(data, '\0', sizeof(DAT_SR_ENTRY));

	lib_path_len = strlen(entry->lib_path);
	lib_path_size = (lib_path_len + 1) * sizeof(char);

	if (NULL == (data->lib_path = dat_os_alloc(lib_path_size))) {
		status =
		    DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY);
		goto bail;
	}

	dat_os_strncpy(data->lib_path, entry->lib_path, lib_path_len);
	data->lib_path[lib_path_len] = '\0';

	ia_params_len = strlen(entry->ia_params);
	ia_params_size = (ia_params_len + 1) * sizeof(char);

	if (NULL == (data->ia_params = dat_os_alloc(ia_params_size))) {
		status =
		    DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY);
		goto bail;
	}

	dat_os_strncpy(data->ia_params, entry->ia_params, ia_params_len);
	data->ia_params[ia_params_len] = '\0';

	data->info = entry->info;
	data->lib_handle = entry->lib_handle;
	data->ref_count = entry->ref_count;
	data->next = NULL;

	dict_entry = NULL;
	status = dat_dictionary_entry_create(&dict_entry);
	if (DAT_SUCCESS != status) {
		goto bail;
	}

	dat_os_lock(&g_sr_lock);

	status = dat_dictionary_search(g_sr_dictionary, info, &prev_data);
	if (DAT_SUCCESS == status) {
		/* We already have a dictionary entry, so we don't need a new one.
		 * This means there are multiple duplicate names in dat.conf,
		 * but presumably they have different paths. Simply link the
		 * new entry at the end of the chain of like-named entries.
		 */
		(void)dat_dictionary_entry_destroy(dict_entry);
		dict_entry = NULL;

		/* Find the next available slot in this chain */
		while (NULL != ((DAT_SR_ENTRY *) prev_data)->next) {
			prev_data = ((DAT_SR_ENTRY *) prev_data)->next;
		}
		dat_os_assert(NULL != prev_data);
		((DAT_SR_ENTRY *) prev_data)->next = data;
	} else {
		status = dat_dictionary_insert(g_sr_dictionary,
					       dict_entry,
					       info,
					       (DAT_DICTIONARY_DATA *) data);
	}

	dat_os_unlock(&g_sr_lock);

      bail:
	if (DAT_SUCCESS != status) {
		if (NULL != data) {
			if (NULL != data->lib_path) {
				dat_os_free(data->lib_path, lib_path_size);
			}

			if (NULL != data->ia_params) {
				dat_os_free(data->ia_params, ia_params_size);
			}

			dat_os_free(data, sizeof(DAT_SR_ENTRY));
		}

		if (NULL != dict_entry) {
			(void)dat_dictionary_entry_destroy(dict_entry);
		}
	}

	return status;
}

//***********************************************************************
// Function: dat_sr_size
//***********************************************************************

extern DAT_RETURN dat_sr_size(OUT DAT_COUNT * size)
{
	return dat_dictionary_size(g_sr_dictionary, size);
}

//***********************************************************************
// Function: dat_sr_list
//***********************************************************************

extern DAT_RETURN
dat_sr_list(IN DAT_COUNT max_to_return,
	    OUT DAT_COUNT * entries_returned,
	    OUT DAT_PROVIDER_INFO * (dat_provider_list[]))
{
	DAT_SR_ENTRY **array;
	DAT_COUNT array_size;
	DAT_COUNT i;
	DAT_RETURN status;

	array = NULL;
	status = DAT_SUCCESS;

	/* The dictionary size may increase between the call to      */
	/* dat_dictionary_size() and dat_dictionary_enumerate().     */
	/* Therefore we loop until a successful enumeration is made. */
	*entries_returned = 0;
	for (;;) {
		status = dat_dictionary_size(g_sr_dictionary, &array_size);
		if (DAT_SUCCESS != status) {
			goto bail;
		}

		if (array_size == 0) {
			status = DAT_SUCCESS;
			goto bail;
		}

		array = dat_os_alloc(array_size * sizeof(DAT_SR_ENTRY *));
		if (array == NULL) {
			status =
			    DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
				      DAT_RESOURCE_MEMORY);
			goto bail;
		}

		dat_os_lock(&g_sr_lock);

		status = dat_dictionary_enumerate(g_sr_dictionary,
						  (DAT_DICTIONARY_DATA *) array,
						  array_size);

		dat_os_unlock(&g_sr_lock);

		if (DAT_SUCCESS == status) {
			break;
		} else {
			dat_os_free(array, array_size * sizeof(DAT_SR_ENTRY *));
			array = NULL;
			continue;
		}
	}

	for (i = 0; (i < max_to_return) && (i < array_size); i++) {
		if (NULL == dat_provider_list[i]) {
			status =
			    DAT_ERROR(DAT_INVALID_PARAMETER, DAT_INVALID_ARG3);
			goto bail;
		}

		*dat_provider_list[i] = array[i]->info;
	}

	*entries_returned = i;

      bail:
	if (NULL != array) {
		dat_os_free(array, array_size * sizeof(DAT_SR_ENTRY *));
	}

	return status;
}

//***********************************************************************
// Function: dat_sr_provider_open
//***********************************************************************

extern DAT_RETURN dat_sr_provider_open(IN const DAT_PROVIDER_INFO * info)
{
	DAT_RETURN status;
	DAT_SR_ENTRY *data;
	DAT_DICTIONARY_DATA dict_data;

	dat_os_lock(&g_sr_lock);

	status = dat_dictionary_search(g_sr_dictionary, info, &dict_data);

	if (DAT_SUCCESS == status) {
		data = (DAT_SR_ENTRY *) dict_data;
		while (data != NULL) {
			if (0 == data->ref_count) {
				/*
				 * Try to open the path. If it fails, try the next
				 * path in the chain. Only the first successful library
				 * open matters, the others will be unused.
				 */
				dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
						 "DAT Registry: IA %s, trying to load library %s\n",
						 data->info.ia_name,
						 data->lib_path);

				status = dat_os_library_load(data->lib_path,
							     &data->lib_handle);

				if (status == DAT_SUCCESS) {
#ifdef DAT_DBG
					dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
							 "DAT2 Registry: IA %s, loaded library %s\n",
							 data->info.ia_name,
							 data->lib_path);
#endif
					data->ref_count++;
					data->init_func =
					    dat_os_library_sym(data->lib_handle,
							       DAT_PROVIDER_INIT_FUNC_STR);
					data->fini_func =
					    dat_os_library_sym(data->lib_handle,
							       DAT_PROVIDER_FINI_FUNC_STR);
					/* Warning: DAT and DAPL libraries not ext compatible */
#ifdef DAT_EXTENSIONS
					{
						void *fncptr;

						fncptr =
						    dat_os_library_sym(data->
								       lib_handle,
								       "dapl_extensions");

						if ((dat_os_library_error() !=
						     NULL)
						    || (fncptr == NULL)) {
							dat_os_dbg_print
							    (DAT_OS_DBG_TYPE_SR,
							     "DAT Registry: WARNING: library %s, "
							     "extended DAT expected extended uDAPL: %s\n",
							     data->lib_path,
							     strerror(errno));
						}
					}
#endif
					if (NULL != data->init_func) {
						(*data->init_func) (&data->info,
								    data->
								    ia_params);
					} else {
						dat_os_dbg_print
						    (DAT_OS_DBG_TYPE_SR,
						     "DAT Registry: Cannot find library init func (%s)\n",
						     DAT_PROVIDER_INIT_FUNC_STR);
					}

					/* exit after we find the first valid entry */
					break;
				} else {
					dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
							 "DAT Registry: static registry unable to "
							 "load library %s\n",
							 data->lib_path);
				}
			} else {
				data->ref_count++;
				break;
			}
			data = data->next;
		}
	}

	dat_os_unlock(&g_sr_lock);

	return status;
}

//***********************************************************************
// Function: dat_sr_provider_close
//***********************************************************************

extern DAT_RETURN dat_sr_provider_close(IN const DAT_PROVIDER_INFO * info)
{
	DAT_RETURN status;
	DAT_SR_ENTRY *data;
	DAT_DICTIONARY_DATA dict_data;

	dat_os_lock(&g_sr_lock);

	status = dat_dictionary_search(g_sr_dictionary, info, &dict_data);

	if (DAT_SUCCESS == status) {
		data = (DAT_SR_ENTRY *) dict_data;
		while (data != NULL) {
			if (1 == data->ref_count) {
				dat_os_dbg_print(DAT_OS_DBG_TYPE_SR,
						 "DAT Registry: IA %s, unloading library %s\n",
						 data->info.ia_name,
						 data->lib_path);

				if (NULL != data->fini_func) {
					(*data->fini_func) (&data->info);
				}

				status =
				    dat_os_library_unload(data->lib_handle);
				if (status == DAT_SUCCESS) {
					data->ref_count--;
				}
				break;
			} else if (data->ref_count > 0) {
				data->ref_count--;
				break;
			}
			data = data->next;
		}
	}

	dat_os_unlock(&g_sr_lock);

	return status;
}