summaryrefslogtreecommitdiffstats
path: root/src/H5PL.c
blob: 6fcf7ebcb5c32d61a18945373ae7b1c3724bc7b6 (plain)
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#define H5PL_PACKAGE		/*suppress error about including H5PLpkg	  */

/* Interface initialization */
#define H5_INTERFACE_INIT_FUNC	H5PL_init_interface

#include "H5private.h"		/* Generic Functions			*/
#include "H5Dprivate.h"		/* Dataset functions			*/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5Iprivate.h"		/* IDs			  		*/
#include "H5MMprivate.h"	/* Memory management			*/
#include "H5Oprivate.h"		/* Object headers		  	*/
#include "H5Pprivate.h"         /* Property lists                       */
#include "H5Sprivate.h"		/* Dataspace functions			*/
#include "H5Zprivate.h"		/* Filter pipeline			*/
#include "H5PLpkg.h"		/* Plugin       			*/

#define H5PL_DEFAULT_PATH       "/usr:/usr/lib:/usr/local"
#define H5PL_PATH_SEPERATOR     ":"


/*--------------------------------------------------------------------------
NAME
   H5PL_init_interface -- Initialize interface-specific information
USAGE
    herr_t H5PL_init_interface()

RETURNS
    Non-negative on success/Negative on failure
DESCRIPTION
    Initializes any interface-specific data or routines.

--------------------------------------------------------------------------*/
static herr_t
H5PL_init_interface(void)
{
    FUNC_ENTER_NOAPI_NOINIT_NOERR

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5PL_init_interface() */


/*-------------------------------------------------------------------------
 * Function:	H5PL_term_interface
 *
 * Purpose:	Terminate the H5PL interface: release all memory, reset all
 *		global variables to initial values. This only happens if all
 *		types have been destroyed from other interfaces.
 *
 * Return:	Success:	Positive if any action was taken that might
 *				affect some other interface; zero otherwise.
 *
 * 		Failure:	Negative.
 *
 * Programmer:	Raymond Lu
 *              20 February 2013
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
H5PL_term_interface(void)
{
    void    *handle = NULL;
    size_t  i = 0;
    
    FUNC_ENTER_NOAPI_NOINIT_NOERR

    if(H5_interface_initialize_g) {
	/* Close opened dynamic libraries */
        for(i=0; i<H5PL_table_used_g; i++) { 
            handle = (H5PL_table_g[i]).handle;
            H5PL_close(handle);
        }

	/* Free the table of dynamic libraries */
	H5PL_table_g = (H5PL_table_t *)H5MM_xfree(H5PL_table_g);
	H5PL_table_used_g = H5PL_table_alloc_g = 0;

        /* Free the table of search paths */
        for(i = 0; i < num_paths; i++) {
            if(path_table[i])
                path_table[i] = (char *)H5MM_xfree(path_table[i]);
        }
        num_paths = 0;
        path_found = FALSE;

	H5_interface_initialize_g = 0;
        i = 1;
    } /* end if */

    FUNC_LEAVE_NOAPI(i)
} /* end H5PL_term_interface() */



/*-------------------------------------------------------------------------
 * Function:	H5PL_load
 *
 * Purpose:	Given the plugin type and identifier, this function searches
 *              and/or loads a dynamic plugin library first among the already
 *              opened libraries then in the designated location paths.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Raymond Lu
 *              13 February 2013
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void *
H5PL_load(H5PL_type_t type, int id)
{
    char           *dl_path = NULL;
    char           *origin_dl_path = NULL;
    size_t         len = 0;
    char           *dir = NULL;
    size_t	   i;
    htri_t         found_in_table = FALSE;
    htri_t         found_in_path = FALSE;
    H5Z_class2_t   *plugin_info = NULL;
    void           *ret_value = NULL;

    FUNC_ENTER_NOAPI(NULL)

    /* Search in the table of already loaded plugin libraries */
    if((found_in_table = H5PL_search_table(type, id, (void **)&plugin_info)) < 0)
        HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, NULL, "search in table failed")

    /* Finish the function if found */
    if(found_in_table && plugin_info) {
        ret_value = (void *)plugin_info;
        HGOTO_DONE(ret_value)
    }

    /* Find the location paths for dynamic libraries */
    if(FALSE == path_found) {
        /* Retrieve paths from HDF5_PLUGIN_PATH if the user sets it
         * or from the default paths if it isn't set */
        origin_dl_path = HDgetenv("HDF5_PLUGIN_PATH");
        if(origin_dl_path == NULL) {
            len = HDstrlen(H5PL_DEFAULT_PATH) + 1;
            dl_path = (char *)H5MM_malloc(len*sizeof(char));
            HDstrncpy(dl_path, H5PL_DEFAULT_PATH, len-1);
            dl_path[len-1] = '\0';
        } else {
            len = HDstrlen(origin_dl_path) + 1;
            dl_path = (char *)H5MM_malloc(len*sizeof(char));
            HDstrncpy(dl_path, origin_dl_path, len);
        }

        /* Put paths in the path table.  They are seperated by ":" */
        dir = HDstrtok(dl_path, H5PL_PATH_SEPERATOR);
        while(dir) {
            path_table[num_paths] = (char *)HDmalloc(HDstrlen(dir) + 1);
            path_table[num_paths][0] = '\0';
            HDstrcat(path_table[num_paths], dir);
            num_paths++;
            dir = HDstrtok(NULL, H5PL_PATH_SEPERATOR);
        }
   
        path_found = TRUE;
    }

    /* Iterate through the path table to find the right dynamic libraries */
    for(i=0; i<num_paths; i++) {
        if((found_in_path = H5PL_find(type, id, path_table[i], (void **)&plugin_info)) < 0)
            HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, NULL, "search in paths failed")
 
        /* Finish the function if found */
        if(found_in_path && plugin_info) {
            ret_value = (void *)plugin_info;
            HGOTO_DONE(ret_value)
        }
    }

done:
    if(dl_path)
        dl_path = (char *)H5MM_xfree(dl_path);

    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:	H5PL_find
 *
 * Purpose:     Given a path, this function opens the directory and envokes
 *              another function to go through all files to find the right 
 *              plugin library. Two function definitions are for Unix and 
 *              Windows.
 *
 * Return:	TRUE on success, 
 *              FALSE on not found,
 *              negative on failure
 *
 * Programmer:	Raymond Lu
 *              13 February 2013
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
#ifndef H5_HAVE_WIN32_API
htri_t
H5PL_find(H5PL_type_t plugin_type, int type_id, char *dir, void **info)
{
    char           *pathname = NULL;
    DIR            *dirp = NULL;
    struct dirent  *dp = NULL;
    struct stat    my_stat;
    htri_t         found_in_dir = FALSE;
    htri_t         ret_value = FALSE;

    FUNC_ENTER_NOAPI(FAIL)

    /* Open the directory */  
    if(!(dirp = HDopendir(dir)))
        HGOTO_ERROR(H5E_PLUGIN, H5E_OPENERROR, FAIL, "can't open directory")

    /* Iterates through all entries in the directory to find the right plugin library */
    while ((dp = HDreaddir(dirp)) != NULL) {
	   /* The library we are looking for should be called libxxx.so... on Unix 
            * or libxxx.xxx.dylib on Mac */ 
	   if(!HDstrncmp(dp->d_name, "lib", (size_t)3) && 
                   (HDstrstr(dp->d_name, ".so") || HDstrstr(dp->d_name, ".dylib"))) {
	       pathname = (char *)H5MM_malloc(strlen(dir) + strlen(dp->d_name) + 2); 
	       HDstrncpy(pathname, dir, strlen(dir)+1);
	       HDstrcat(pathname, "/");
	       HDstrcat(pathname, dp->d_name);

	       /*fprintf(stderr, "dp->d_name=%s, pathname=%s. ", dp->d_name, pathname);
	       fprintf(stderr, "\n");*/

	       if(HDstat(pathname, &my_stat) == -1)
		   HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "can't stat file: %s", strerror(errno))
	      
               /* If it is a directory, skip it */ 
	       if(S_ISDIR(my_stat.st_mode))
                   continue;

               if((found_in_dir = H5PL_open(plugin_type, pathname, type_id, info)) < 0)
                   HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "search in directory failed")

               if(found_in_dir) {
            	   ret_value = TRUE; 
	           HGOTO_DONE(ret_value)
               } else
	           if(pathname) 
		       pathname = (char *)H5MM_xfree(pathname);
	   }
    }

    if(HDclosedir(dirp) < 0)
        HGOTO_ERROR(H5E_FILE, H5E_CLOSEERROR, FAIL, "can't close directory: %s", strerror(errno))
    dirp = NULL;

done:
    if(pathname) 
        pathname = (char *)H5MM_xfree(pathname);
    if(dirp) 
        HDclosedir(dirp);

    FUNC_LEAVE_NOAPI(ret_value)
}
#else
htri_t
H5PL_find(H5PL_type_t plugin_type, int type_id, char *dir, void **info)
{
    char           *pathname = NULL;
    struct dirent  *dp = NULL;

    WIN32_FIND_DATA fdFile;
    HANDLE hFind = NULL;

    htri_t         found_in_dir = FALSE;
    htri_t         ret_value = FALSE;

    FUNC_ENTER_NOAPI(FAIL)

    if((hFind = FindFirstFile(dir, &fdFile)) == INVALID_HANDLE_VALUE) {
        /*fprintf(stderr, "Path not found: [%s]\n", dir);*/
        HGOTO_ERROR(H5E_PLUGIN, H5E_OPENERROR, FAIL, "can't open directory")
    }

    do {
        /* Find first file will always return "."
         * and ".." as the first two directories. */
        if(HDstrcmp(fdFile.cFileName, ".") != 0 && HDstrcmp(fdFile.cFileName, "..") != 0) {
	    pathname = (char *)H5MM_malloc(strlen(dir) + strlen(fdFile.cFileName) + 2); 
	    HDstrncpy(pathname, dir, strlen(dir)+1);
	    HDstrcat(pathname, "\\");
	    HDstrcat(pathname, fdFile.cFileName);

            /* Is the entity a File or Folder? */
            if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
                continue;

            if((found_in_dir = H5PL_open(plugin_type, pathname, type_id, info)) < 0)
                HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "search in directory failed")

            if(found_in_dir) {
       	        ret_value = TRUE; 
	        HGOTO_DONE(ret_value)
            } else
	        if(pathname) 
		    pathname = (char *)H5MM_xfree(pathname);
        }
    } while(FindNextFile(hFind, &fdFile)); /* Find the next file. */

    /* Clean things up! */
    FindClose(hFind);
    hFind = NULL;

done:
    if(pathname) 
        pathname = (char *)H5MM_xfree(pathname);
    if(hFind) 
        FindClose(hFind);

    FUNC_LEAVE_NOAPI(ret_value)
}
#endif


/*-------------------------------------------------------------------------
 * Function:	H5PL_open
 *
 * Purpose:     Iterates through all files to find the right plugin library.
 *              It loads the dynamic plugin library and keeps it on the list 
 *              of loaded libraries. 	
 *
 * Return:	TRUE on success, 
 *              FALSE on not found,
 *              negative on failure
 *
 * Programmer:	Raymond Lu
 *              13 February 2013
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
htri_t
H5PL_open(H5PL_type_t pl_type, char *libname, int pl_id, void **pl_info)
{
    H5PL_HANDLE    handle = NULL;
    H5Z_class2_t*  (*H5PL_get_plugin_info)(void) = NULL; 
    H5Z_class2_t   *plugin_info = NULL;
    htri_t         ret_value = FALSE;

    FUNC_ENTER_NOAPI(FAIL)

    /* There are different reasons why a library can't be open, e.g. wrong architecture.
     * simply continue if we can't open it */
    if(NULL == (handle = H5PL_OPEN_DLIB(libname)))
        /*fprintf(stderr, "not open dl library: %s\n", H5PL_CLR_ERR);*/
        HGOTO_DONE(ret_value)

        H5PL_CLR_ERROR; /*clear error*/

    /* Return a handle for the function H5PL_get_plugin_info in the dynamic library.
     * The plugin library is suppose to define this function. */
    if(NULL == (H5PL_get_plugin_info = H5PL_GET_LIB_FUNC(handle, "H5PL_get_plugin_info"))) {
        if(H5PL_close(handle) < 0)
            HGOTO_ERROR(H5E_PLUGIN, H5E_CLOSEERROR, FAIL, "can't close dynamic library")
    }

    /* Envoke H5PL_get_plugin_info to verify this is the right library we are looking for.
     * Move on if it isn't. */
    if(H5PL_get_plugin_info) {
        if(NULL == (plugin_info = (*H5PL_get_plugin_info)())) {
            if(H5PL_close(handle) < 0)
	        HGOTO_ERROR(H5E_PLUGIN, H5E_CLOSEERROR, FAIL, "can't close dynamic library")
	    HGOTO_ERROR(H5E_DATATYPE, H5E_CANTGET, FAIL, "can't get plugin info")
        }

        if(plugin_info->id == pl_id) {
	    (H5PL_table_g[H5PL_table_used_g]).handle = handle;
	    (H5PL_table_g[H5PL_table_used_g]).pl_type = pl_type;
	    (H5PL_table_g[H5PL_table_used_g]).pl_id = plugin_info->id;
/*fprintf(stderr, "%s: H5PL_table_used_g=%d, id=%d, id 2=%d\n", FUNC, H5PL_table_used_g, (H5PL_table_g[H5PL_table_used_g]).pl_id, plugin_info->id);*/
	    H5PL_table_used_g++;

	    *pl_info = (void *)plugin_info;
	    ret_value = TRUE;
 
	    HGOTO_DONE(ret_value)
        } else if(H5PL_close(handle) < 0)
	    HGOTO_ERROR(H5E_PLUGIN, H5E_CLOSEERROR, FAIL, "can't close dynamic library")
    }

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:	H5PL_search_table
 *
 * Purpose:     Search in the list of already opened dynamic libraries
 *              to see if the one we are looking for is already opened.
 *
 * Return:	TRUE on success, 
 *              FALSE on not found,
 *              negative on failure
 *
 * Programmer:	Raymond Lu
 *              13 February 2013
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
htri_t
H5PL_search_table(H5PL_type_t plugin_type, int type_id, void **info)
{
    H5Z_class2_t*  (*H5PL_get_plugin_info)(void) = NULL; 
    H5Z_class2_t   *plugin_info = NULL;
    size_t         i;
    htri_t         ret_value = FALSE;

    FUNC_ENTER_NOAPI(FAIL)

/*if(0 < H5PL_table_used_g)
fprintf(stderr, "%s: H5PL_table_used_g=%d, id=%d\n", FUNC, H5PL_table_used_g, (H5PL_table_g[H5PL_table_used_g-1]).pl_id);*/

    /* Search in the table of already opened dynamic libraries */
    if(0 < H5PL_table_used_g) {
        for(i=0; i<H5PL_table_used_g; i++) {
            if((plugin_type == (H5PL_table_g[i]).pl_type) && (type_id == (H5PL_table_g[i]).pl_id)) {
  	        if(NULL == (H5PL_get_plugin_info = H5PL_GET_LIB_FUNC((H5PL_table_g[i]).handle, "H5PL_get_plugin_info")))
		    HGOTO_ERROR(H5E_DATATYPE, H5E_CANTGET, FAIL, "can't get function: H5PL_GET_LIB_FUNC")

	        if(NULL == (plugin_info = (*H5PL_get_plugin_info)()))
		    HGOTO_ERROR(H5E_DATATYPE, H5E_CANTGET, FAIL, "can't get plugin info")

	        /*fprintf(stderr, "%s: handle=%p, H5PL_get_plugin_info=%p, plugin_info=%p, id=%d\n", FUNC, (H5PL_table_g[i]).handle, H5PL_get_plugin_info, plugin_info, plugin_info->id);*/

	        *info = (void *)plugin_info;
	        ret_value = TRUE;
            }
        }
    }

    /* Expand the table if it is too small */
    if(H5PL_table_used_g >= H5PL_table_alloc_g) {
        size_t n = MAX(H5Z_MAX_NFILTERS, 2*H5PL_table_alloc_g);
        H5PL_table_t *table = (H5PL_table_t *)H5MM_realloc(H5PL_table_g, n * sizeof(H5PL_table_t));

        if(!table)
	    HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "unable to extend dynamic library table")

        H5PL_table_g = table;
        H5PL_table_alloc_g = n;
    } /* end if */

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:	H5PL_close
 *
 * Purpose:     Closes the handle for dynamic library	
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Raymond Lu
 *              13 February 2013
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5PL_close(H5PL_HANDLE handle)
{
    herr_t        ret_value = SUCCEED;

    FUNC_ENTER_NOAPI(FAIL)
/*fprintf(stderr, "%s: closing. handle=%p\n", FUNC, handle);*/
    H5PL_CLOSE_LIB(handle);
   
done:
    FUNC_LEAVE_NOAPI(ret_value)
}