dfmgr.c 4.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * dfmgr.c
4
 *	  Dynamic function manager code.
5
 *
6
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.47 2001/01/24 19:43:15 momjian Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15 16
#include "postgres.h"

17 18 19
#include <sys/types.h>
#include <sys/stat.h>

B
Bruce Momjian 已提交
20
#include "dynloader.h"
21
#include "utils/dynamic_loader.h"
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

/*
 * List of dynamically loaded files.
 */

typedef struct df_files
{
	struct df_files *next;		/* List link */
	dev_t		device;			/* Device file is on */
	ino_t		inode;			/* Inode number of file */
	void	   *handle;			/* a handle for pg_dl* functions */
	char		filename[1];	/* Full pathname of file */
	/* we allocate the block big enough for actual length of pathname.
	 * filename[] must be last item in struct!
	 */
} DynamicFileList;

40 41 42
static DynamicFileList *file_list = (DynamicFileList *) NULL;
static DynamicFileList *file_tail = (DynamicFileList *) NULL;

43
#define SAME_INODE(A,B) ((A).st_ino == (B).inode && (A).st_dev == (B).device)
44 45


46 47 48 49 50 51 52
/*
 * Load the specified dynamic-link library file, and look for a function
 * named funcname in it.  If the function is not found, we raise an error
 * if signalNotFound is true, else return (PGFunction) NULL.  Note that
 * errors in loading the library will provoke elog regardless of
 * signalNotFound. 
 */
53
PGFunction
54 55
load_external_function(char *filename, char *funcname,
					   bool signalNotFound)
56
{
57 58
	DynamicFileList *file_scanner;
	PGFunction	retval;
59 60
	char	   *load_error;
	struct stat stat_buf;
61 62

	/*
63
	 * Scan the list of loaded FILES to see if the file has been loaded.
64
	 */
65 66 67 68 69 70
	for (file_scanner = file_list;
		 file_scanner != (DynamicFileList *) NULL &&
			 strcmp(filename, file_scanner->filename) != 0;
		 file_scanner = file_scanner->next)
		;
	if (file_scanner == (DynamicFileList *) NULL)
71
	{
72 73 74 75 76 77
		/*
		 * Check for same files - different paths (ie, symlink or link)
		 */
		if (stat(filename, &stat_buf) == -1)
			elog(ERROR, "stat failed on file '%s': %m", filename);

78
		for (file_scanner = file_list;
79 80
			 file_scanner != (DynamicFileList *) NULL &&
				 !SAME_INODE(stat_buf, *file_scanner);
81 82 83 84 85 86
			 file_scanner = file_scanner->next)
			;
	}

	if (file_scanner == (DynamicFileList *) NULL)
	{
87 88 89 90 91 92
		/*
		 * File not loaded yet.
		 */
		file_scanner = (DynamicFileList *)
			malloc(sizeof(DynamicFileList) + strlen(filename));
		if (file_scanner == NULL)
93
			elog(ERROR, "Out of memory in load_external_function");
94

95
		MemSet((char *) file_scanner, 0, sizeof(DynamicFileList));
96 97 98 99 100 101 102 103 104 105
		strcpy(file_scanner->filename, filename);
		file_scanner->device = stat_buf.st_dev;
		file_scanner->inode = stat_buf.st_ino;
		file_scanner->next = (DynamicFileList *) NULL;

		file_scanner->handle = pg_dlopen(filename);
		if (file_scanner->handle == (void *) NULL)
		{
			load_error = (char *) pg_dlerror();
			free((char *) file_scanner);
106
			elog(ERROR, "Load of file %s failed: %s", filename, load_error);
107 108
		}

109 110 111 112 113
		/* OK to link it into list */
		if (file_list == (DynamicFileList *) NULL)
			file_list = file_scanner;
		else
			file_tail->next = file_scanner;
114 115 116
		file_tail = file_scanner;
	}

117 118 119 120 121 122 123
	/*
	 * If funcname is NULL, we only wanted to load the file.
	 */
	if (funcname == (char *) NULL)
		return (PGFunction) NULL;

	retval = pg_dlsym(file_scanner->handle, funcname);
124

125
	if (retval == (PGFunction) NULL && signalNotFound)
126
		elog(ERROR, "Can't find function %s in file %s", funcname, filename);
127

128
	return retval;
129 130 131
}

/*
132 133 134
 * This function loads a shlib file without looking up any particular
 * function in it.  If the same shlib has previously been loaded,
 * unload and reload it.
135 136 137 138
 */
void
load_file(char *filename)
{
139
	DynamicFileList *file_scanner,
B
Bruce Momjian 已提交
140
			   *p;
141
	struct stat stat_buf;
142

143
	/*
B
Bruce Momjian 已提交
144 145 146
	 * We need to do stat() in order to determine whether this is the same
	 * file as a previously loaded file; it's also handy so as to give a
	 * good error message if bogus file name given.
147
	 */
148
	if (stat(filename, &stat_buf) == -1)
149
		elog(ERROR, "LOAD: could not open file '%s': %m", filename);
150

151
	if (file_list != (DynamicFileList *) NULL)
152
	{
153
		if (SAME_INODE(stat_buf, *file_list))
154
		{
155 156 157 158
			p = file_list;
			file_list = p->next;
			pg_dlclose(p->handle);
			free((char *) p);
159
		}
160
		else
161
		{
162 163 164 165 166 167 168 169 170 171 172 173 174
			for (file_scanner = file_list;
				 file_scanner->next != (DynamicFileList *) NULL;
				 file_scanner = file_scanner->next)
			{
				if (SAME_INODE(stat_buf, *(file_scanner->next)))
				{
					p = file_scanner->next;
					file_scanner->next = p->next;
					pg_dlclose(p->handle);
					free((char *) p);
					break;
				}
			}
175 176 177
		}
	}

178
	load_external_function(filename, (char *) NULL, false);
179
}