1 /*-------------------------------------------------------------------------
4 * Dynamic function manager code.
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.54 2001/10/25 05:49:48 momjian Exp $
13 *-------------------------------------------------------------------------
18 #include <sys/types.h>
21 #include "dynloader.h"
22 #include "miscadmin.h"
23 #include "utils/dynamic_loader.h"
27 * List of dynamically loaded files (kept in malloc'd memory).
30 typedef struct df_files
32 struct df_files *next; /* List link */
33 dev_t device; /* Device file is on */
34 ino_t inode; /* Inode number of file */
35 void *handle; /* a handle for pg_dl* functions */
36 char filename[1]; /* Full pathname of file */
39 * we allocate the block big enough for actual length of pathname.
40 * filename[] must be last item in struct!
44 static DynamicFileList *file_list = (DynamicFileList *) NULL;
45 static DynamicFileList *file_tail = (DynamicFileList *) NULL;
47 #define SAME_INODE(A,B) ((A).st_ino == (B).inode && (A).st_dev == (B).device)
49 char *Dynamic_library_path;
51 static bool file_exists(const char *name);
52 static char *find_in_dynamic_libpath(const char *basename);
53 static char *expand_dynamic_library_name(const char *name);
54 static char *substitute_libpath_macro(const char *name);
57 * Load the specified dynamic-link library file, and look for a function
58 * named funcname in it. (funcname can be NULL to just load the file.)
60 * If the function is not found, we raise an error if signalNotFound is true,
61 * else return (PGFunction) NULL. Note that errors in loading the library
62 * will provoke elog regardless of signalNotFound.
64 * If filehandle is not NULL, then *filehandle will be set to a handle
65 * identifying the library file. The filehandle can be used with
66 * lookup_external_function to lookup additional functions in the same file
67 * at less cost than repeating load_external_function.
70 load_external_function(char *filename, char *funcname,
71 bool signalNotFound, void **filehandle)
73 DynamicFileList *file_scanner;
79 fullname = expand_dynamic_library_name(filename);
81 fullname = pstrdup(filename);
82 /* at this point fullname is always freshly palloc'd */
85 * Scan the list of loaded FILES to see if the file has been loaded.
87 for (file_scanner = file_list;
88 file_scanner != (DynamicFileList *) NULL &&
89 strcmp(fullname, file_scanner->filename) != 0;
90 file_scanner = file_scanner->next)
92 if (file_scanner == (DynamicFileList *) NULL)
95 * Check for same files - different paths (ie, symlink or link)
97 if (stat(fullname, &stat_buf) == -1)
98 elog(ERROR, "stat failed on file '%s': %m", fullname);
100 for (file_scanner = file_list;
101 file_scanner != (DynamicFileList *) NULL &&
102 !SAME_INODE(stat_buf, *file_scanner);
103 file_scanner = file_scanner->next)
107 if (file_scanner == (DynamicFileList *) NULL)
110 * File not loaded yet.
112 file_scanner = (DynamicFileList *)
113 malloc(sizeof(DynamicFileList) + strlen(fullname));
114 if (file_scanner == NULL)
115 elog(ERROR, "Out of memory in load_external_function");
117 MemSet((char *) file_scanner, 0, sizeof(DynamicFileList));
118 strcpy(file_scanner->filename, fullname);
119 file_scanner->device = stat_buf.st_dev;
120 file_scanner->inode = stat_buf.st_ino;
121 file_scanner->next = (DynamicFileList *) NULL;
123 file_scanner->handle = pg_dlopen(fullname);
124 if (file_scanner->handle == (void *) NULL)
126 load_error = (char *) pg_dlerror();
127 free((char *) file_scanner);
128 elog(ERROR, "Load of file %s failed: %s", fullname, load_error);
131 /* OK to link it into list */
132 if (file_list == (DynamicFileList *) NULL)
133 file_list = file_scanner;
135 file_tail->next = file_scanner;
136 file_tail = file_scanner;
139 /* Return handle if caller wants it. */
141 *filehandle = file_scanner->handle;
144 * If funcname is NULL, we only wanted to load the file.
146 if (funcname == (char *) NULL)
149 return (PGFunction) NULL;
152 retval = pg_dlsym(file_scanner->handle, funcname);
154 if (retval == (PGFunction) NULL && signalNotFound)
155 elog(ERROR, "Can't find function %s in file %s", funcname, fullname);
162 * This function loads a shlib file without looking up any particular
163 * function in it. If the same shlib has previously been loaded,
164 * unload and reload it.
167 load_file(char *filename)
169 DynamicFileList *file_scanner,
171 struct stat stat_buf;
174 fullname = expand_dynamic_library_name(filename);
176 fullname = pstrdup(filename);
177 /* at this point fullname is always freshly palloc'd */
180 * We need to do stat() in order to determine whether this is the same
181 * file as a previously loaded file; it's also handy so as to give a
182 * good error message if bogus file name given.
184 if (stat(fullname, &stat_buf) == -1)
185 elog(ERROR, "LOAD: could not open file '%s': %m", fullname);
187 if (file_list != (DynamicFileList *) NULL)
189 if (SAME_INODE(stat_buf, *file_list))
193 pg_dlclose(p->handle);
198 for (file_scanner = file_list;
199 file_scanner->next != (DynamicFileList *) NULL;
200 file_scanner = file_scanner->next)
202 if (SAME_INODE(stat_buf, *(file_scanner->next)))
204 p = file_scanner->next;
205 file_scanner->next = p->next;
206 pg_dlclose(p->handle);
214 load_external_function(fullname, (char *) NULL, false, (void *) NULL);
220 * Lookup a function whose library file is already loaded.
221 * Return (PGFunction) NULL if not found.
224 lookup_external_function(void *filehandle, char *funcname)
226 return pg_dlsym(filehandle, funcname);
231 file_exists(const char *name)
235 AssertArg(name != NULL);
237 if (stat(name, &st) == 0)
238 return S_ISDIR(st.st_mode) ? false : true;
239 else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
240 elog(ERROR, "stat failed on %s: %s", name, strerror(errno));
246 /* Example format: ".so" */
248 #error "DLSUFFIX must be defined to compile this file."
251 /* Example format: "/usr/local/pgsql/lib" */
253 #error "PKGLIBDIR needs to be defined to compile this file."
258 * If name contains a slash, check if the file exists, if so return
259 * the name. Else (no slash) try to expand using search path (see
260 * find_in_dynamic_libpath below); if that works, return the fully
261 * expanded file name. If the previous failed, append DLSUFFIX and
262 * try again. If all fails, return NULL.
264 * A non-NULL result will always be freshly palloc'd.
267 expand_dynamic_library_name(const char *name)
275 have_slash = (strchr(name, '/') != NULL);
279 full = find_in_dynamic_libpath(name);
285 full = substitute_libpath_macro(name);
286 if (file_exists(full))
291 new = palloc(strlen(name) + strlen(DLSUFFIX) + 1);
293 strcat(new, DLSUFFIX);
297 full = find_in_dynamic_libpath(new);
304 full = substitute_libpath_macro(new);
306 if (file_exists(full))
316 * Substitute for any macros appearing in the given string.
317 * Result is always freshly palloc'd.
320 substitute_libpath_macro(const char *name)
322 size_t macroname_len;
323 char *replacement = NULL;
325 AssertArg(name != NULL);
328 return pstrdup(name);
330 macroname_len = strcspn(name + 1, "/") + 1;
332 if (strncmp(name, "$libdir", macroname_len) == 0)
333 replacement = PKGLIBDIR;
335 elog(ERROR, "invalid macro name in dynamic library path");
337 if (name[macroname_len] == '\0')
338 return pstrdup(replacement);
343 new = palloc(strlen(replacement) + (strlen(name) - macroname_len) + 1);
345 strcpy(new, replacement);
346 strcat(new, name + macroname_len);
354 * Search for a file called 'basename' in the colon-separated search
355 * path Dynamic_library_path. If the file is found, the full file name
356 * is returned in freshly palloc'd memory. If the file is not found,
360 find_in_dynamic_libpath(const char *basename)
365 AssertArg(basename != NULL);
366 AssertArg(strchr(basename, '/') == NULL);
367 AssertState(Dynamic_library_path != NULL);
369 p = Dynamic_library_path;
373 baselen = strlen(basename);
382 len = strcspn(p, ":");
385 elog(ERROR, "zero length dynamic_library_path component");
387 piece = palloc(len + 1);
388 strncpy(piece, p, len);
391 mangled = substitute_libpath_macro(piece);
394 /* only absolute paths */
395 if (mangled[0] != '/')
396 elog(ERROR, "dynamic_library_path component is not absolute");
398 full = palloc(strlen(mangled) + 1 + baselen + 1);
399 sprintf(full, "%s/%s", mangled, basename);
403 elog(DEBUG, "find_in_dynamic_libpath: trying %s", full);
405 if (file_exists(full))