1 /*-------------------------------------------------------------------------
4 * Dynamic function manager code.
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.91 2006/10/04 00:30:01 momjian Exp $
13 *-------------------------------------------------------------------------
19 #ifndef WIN32_ONLY_COMPILER
20 #include "dynloader.h"
22 #include "port/dynloader/win32.h"
24 #include "miscadmin.h"
25 #include "utils/dynamic_loader.h"
26 #include "utils/hsearch.h"
29 /* signatures for PostgreSQL-specific library init/fini functions */
30 typedef void (*PG_init_t) (void);
31 typedef void (*PG_fini_t) (void);
33 /* hashtable entry for rendezvous variables */
36 char varName[NAMEDATALEN]; /* hash key (must be first) */
38 } rendezvousHashEntry;
41 * List of dynamically loaded files (kept in malloc'd memory).
44 typedef struct df_files
46 struct df_files *next; /* List link */
47 dev_t device; /* Device file is on */
48 #ifndef WIN32 /* ensures we never again depend on this under
50 ino_t inode; /* Inode number of file */
52 void *handle; /* a handle for pg_dl* functions */
53 char filename[1]; /* Full pathname of file */
56 * we allocate the block big enough for actual length of pathname.
57 * filename[] must be last item in struct!
61 static DynamicFileList *file_list = NULL;
62 static DynamicFileList *file_tail = NULL;
64 /* stat() call under Win32 returns an st_ino field, but it has no meaning */
66 #define SAME_INODE(A,B) ((A).st_ino == (B).inode && (A).st_dev == (B).device)
68 #define SAME_INODE(A,B) false
71 char *Dynamic_library_path;
73 static void *internal_load_library(const char *libname);
74 static void internal_unload_library(const char *libname);
75 static bool file_exists(const char *name);
76 static char *expand_dynamic_library_name(const char *name);
77 static void check_restricted_library_name(const char *name);
78 static char *substitute_libpath_macro(const char *name);
79 static char *find_in_dynamic_libpath(const char *basename);
81 /* Magic structure that module needs to match to be accepted */
82 static const Pg_magic_struct magic_data = PG_MODULE_MAGIC_DATA;
86 * Load the specified dynamic-link library file, and look for a function
87 * named funcname in it.
89 * If the function is not found, we raise an error if signalNotFound is true,
90 * else return (PGFunction) NULL. Note that errors in loading the library
91 * will provoke ereport() regardless of signalNotFound.
93 * If filehandle is not NULL, then *filehandle will be set to a handle
94 * identifying the library file. The filehandle can be used with
95 * lookup_external_function to lookup additional functions in the same file
96 * at less cost than repeating load_external_function.
99 load_external_function(char *filename, char *funcname,
100 bool signalNotFound, void **filehandle)
106 /* Expand the possibly-abbreviated filename to an exact path name */
107 fullname = expand_dynamic_library_name(filename);
109 /* Load the shared library, unless we already did */
110 lib_handle = internal_load_library(fullname);
112 /* Return handle if caller wants it */
114 *filehandle = lib_handle;
116 /* Look up the function within the library */
117 retval = pg_dlsym(lib_handle, funcname);
119 if (retval == NULL && signalNotFound)
121 (errcode(ERRCODE_UNDEFINED_FUNCTION),
122 errmsg("could not find function \"%s\" in file \"%s\"",
123 funcname, fullname)));
130 * This function loads a shlib file without looking up any particular
131 * function in it. If the same shlib has previously been loaded,
132 * unload and reload it.
134 * When 'restricted' is true, only libraries in the presumed-secure
135 * directory $libdir/plugins may be referenced.
138 load_file(const char *filename, bool restricted)
142 /* Apply security restriction if requested */
144 check_restricted_library_name(filename);
146 /* Expand the possibly-abbreviated filename to an exact path name */
147 fullname = expand_dynamic_library_name(filename);
149 /* Unload the library if currently loaded */
150 internal_unload_library(fullname);
152 /* Load the shared library */
153 (void) internal_load_library(fullname);
159 * Lookup a function whose library file is already loaded.
160 * Return (PGFunction) NULL if not found.
163 lookup_external_function(void *filehandle, char *funcname)
165 return pg_dlsym(filehandle, funcname);
170 * Load the specified dynamic-link library file, unless it already is
171 * loaded. Return the pg_dl* handle for the file.
173 * Note: libname is expected to be an exact name for the library file.
176 internal_load_library(const char *libname)
178 DynamicFileList *file_scanner;
179 PGModuleMagicFunction magic_func;
181 struct stat stat_buf;
185 * Scan the list of loaded FILES to see if the file has been loaded.
187 for (file_scanner = file_list;
188 file_scanner != NULL &&
189 strcmp(libname, file_scanner->filename) != 0;
190 file_scanner = file_scanner->next)
193 if (file_scanner == NULL)
196 * Check for same files - different paths (ie, symlink or link)
198 if (stat(libname, &stat_buf) == -1)
200 (errcode_for_file_access(),
201 errmsg("could not access file \"%s\": %m",
204 for (file_scanner = file_list;
205 file_scanner != NULL &&
206 !SAME_INODE(stat_buf, *file_scanner);
207 file_scanner = file_scanner->next)
211 if (file_scanner == NULL)
214 * File not loaded yet.
216 file_scanner = (DynamicFileList *)
217 malloc(sizeof(DynamicFileList) + strlen(libname));
218 if (file_scanner == NULL)
220 (errcode(ERRCODE_OUT_OF_MEMORY),
221 errmsg("out of memory")));
223 MemSet(file_scanner, 0, sizeof(DynamicFileList));
224 strcpy(file_scanner->filename, libname);
225 file_scanner->device = stat_buf.st_dev;
227 file_scanner->inode = stat_buf.st_ino;
229 file_scanner->next = NULL;
231 file_scanner->handle = pg_dlopen(file_scanner->filename);
232 if (file_scanner->handle == NULL)
234 load_error = (char *) pg_dlerror();
235 free((char *) file_scanner);
236 /* errcode_for_file_access might not be appropriate here? */
238 (errcode_for_file_access(),
239 errmsg("could not load library \"%s\": %s",
240 libname, load_error)));
243 /* Check the magic function to determine compatibility */
244 magic_func = (PGModuleMagicFunction)
245 pg_dlsym(file_scanner->handle, PG_MAGIC_FUNCTION_NAME_STRING);
248 const Pg_magic_struct *magic_data_ptr = (*magic_func) ();
250 if (magic_data_ptr->len != magic_data.len ||
251 memcmp(magic_data_ptr, &magic_data, magic_data.len) != 0)
253 /* copy data block before unlinking library */
254 Pg_magic_struct module_magic_data = *magic_data_ptr;
256 /* try to unlink library */
257 pg_dlclose(file_scanner->handle);
258 free((char *) file_scanner);
261 * Report suitable error. It's probably not worth writing a
262 * separate error message for each field; only the most common
263 * case of wrong major version gets its own message.
265 if (module_magic_data.version != magic_data.version)
267 (errmsg("incompatible library \"%s\": version mismatch",
269 errdetail("Server is version %d.%d, library is version %d.%d.",
270 magic_data.version / 100,
271 magic_data.version % 100,
272 module_magic_data.version / 100,
273 module_magic_data.version % 100)));
275 (errmsg("incompatible library \"%s\": magic block mismatch",
281 /* try to unlink library */
282 pg_dlclose(file_scanner->handle);
283 free((char *) file_scanner);
286 (errmsg("incompatible library \"%s\": missing magic block",
288 errhint("Extension libraries are now required to use the PG_MODULE_MAGIC macro.")));
292 * If the library has a _PG_init() function, call it.
294 PG_init = (PG_init_t) pg_dlsym(file_scanner->handle, "_PG_init");
298 /* OK to link it into list */
299 if (file_list == NULL)
300 file_list = file_scanner;
302 file_tail->next = file_scanner;
303 file_tail = file_scanner;
306 return file_scanner->handle;
310 * Unload the specified dynamic-link library file, if it is loaded.
312 * Note: libname is expected to be an exact name for the library file.
315 internal_unload_library(const char *libname)
317 DynamicFileList *file_scanner,
320 struct stat stat_buf;
324 * We need to do stat() in order to determine whether this is the same
325 * file as a previously loaded file; it's also handy so as to give a good
326 * error message if bogus file name given.
328 if (stat(libname, &stat_buf) == -1)
330 (errcode_for_file_access(),
331 errmsg("could not access file \"%s\": %m", libname)));
334 * We have to zap all entries in the list that match on either filename or
335 * inode, else internal_load_library() will still think it's present.
338 for (file_scanner = file_list; file_scanner != NULL; file_scanner = nxt)
340 nxt = file_scanner->next;
341 if (strcmp(libname, file_scanner->filename) == 0 ||
342 SAME_INODE(stat_buf, *file_scanner))
350 * If the library has a _PG_fini() function, call it.
352 PG_fini = (PG_fini_t) pg_dlsym(file_scanner->handle, "_PG_fini");
356 clear_external_function_hash(file_scanner->handle);
357 pg_dlclose(file_scanner->handle);
358 free((char *) file_scanner);
359 /* prv does not change */
367 file_exists(const char *name)
371 AssertArg(name != NULL);
373 if (stat(name, &st) == 0)
374 return S_ISDIR(st.st_mode) ? false : true;
375 else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
377 (errcode_for_file_access(),
378 errmsg("could not access file \"%s\": %m", name)));
384 /* Example format: ".so" */
386 #error "DLSUFFIX must be defined to compile this file."
390 * If name contains a slash, check if the file exists, if so return
391 * the name. Else (no slash) try to expand using search path (see
392 * find_in_dynamic_libpath below); if that works, return the fully
393 * expanded file name. If the previous failed, append DLSUFFIX and
394 * try again. If all fails, just return the original name.
396 * The result will always be freshly palloc'd.
399 expand_dynamic_library_name(const char *name)
407 have_slash = (first_dir_separator(name) != NULL);
411 full = find_in_dynamic_libpath(name);
417 full = substitute_libpath_macro(name);
418 if (file_exists(full))
423 new = palloc(strlen(name) + strlen(DLSUFFIX) + 1);
425 strcat(new, DLSUFFIX);
429 full = find_in_dynamic_libpath(new);
436 full = substitute_libpath_macro(new);
438 if (file_exists(full))
444 * If we can't find the file, just return the string as-is. The ensuing
445 * load attempt will fail and report a suitable message.
447 return pstrdup(name);
451 * Check a restricted library name. It must begin with "$libdir/plugins/"
452 * and there must not be any directory separators after that (this is
453 * sufficient to prevent ".." style attacks).
456 check_restricted_library_name(const char *name)
458 if (strncmp(name, "$libdir/plugins/", 16) != 0 ||
459 first_dir_separator(name + 16) != NULL)
461 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
462 errmsg("access to library \"%s\" is not allowed",
467 * Substitute for any macros appearing in the given string.
468 * Result is always freshly palloc'd.
471 substitute_libpath_macro(const char *name)
476 AssertArg(name != NULL);
478 /* Currently, we only recognize $libdir at the start of the string */
480 return pstrdup(name);
482 if ((sep_ptr = first_dir_separator(name)) == NULL)
483 sep_ptr = name + strlen(name);
485 if (strlen("$libdir") != sep_ptr - name ||
486 strncmp(name, "$libdir", strlen("$libdir")) != 0)
488 (errcode(ERRCODE_INVALID_NAME),
489 errmsg("invalid macro name in dynamic library path: %s",
492 ret = palloc(strlen(pkglib_path) + strlen(sep_ptr) + 1);
494 strcpy(ret, pkglib_path);
495 strcat(ret, sep_ptr);
502 * Search for a file called 'basename' in the colon-separated search
503 * path Dynamic_library_path. If the file is found, the full file name
504 * is returned in freshly palloc'd memory. If the file is not found,
508 find_in_dynamic_libpath(const char *basename)
513 AssertArg(basename != NULL);
514 AssertArg(first_dir_separator(basename) == NULL);
515 AssertState(Dynamic_library_path != NULL);
517 p = Dynamic_library_path;
521 baselen = strlen(basename);
530 piece = first_path_separator(p);
533 (errcode(ERRCODE_INVALID_NAME),
534 errmsg("zero-length component in parameter \"dynamic_library_path\"")));
541 piece = palloc(len + 1);
542 strncpy(piece, p, len);
545 mangled = substitute_libpath_macro(piece);
548 canonicalize_path(mangled);
550 /* only absolute paths */
551 if (!is_absolute_path(mangled))
553 (errcode(ERRCODE_INVALID_NAME),
554 errmsg("component in parameter \"dynamic_library_path\" is not an absolute path")));
556 full = palloc(strlen(mangled) + 1 + baselen + 1);
557 sprintf(full, "%s/%s", mangled, basename);
560 elog(DEBUG3, "find_in_dynamic_libpath: trying \"%s\"", full);
562 if (file_exists(full))
578 * Find (or create) a rendezvous variable that one dynamically
579 * loaded library can use to meet up with another.
581 * On the first call of this function for a particular varName,
582 * a "rendezvous variable" is created with the given name.
583 * The value of the variable is a void pointer (initially set to NULL).
584 * Subsequent calls with the same varName just return the address of
585 * the existing variable. Once created, a rendezvous variable lasts
586 * for the life of the process.
588 * Dynamically loaded libraries can use rendezvous variables
589 * to find each other and share information: they just need to agree
590 * on the variable name and the data it will point to.
593 find_rendezvous_variable(const char *varName)
595 static HTAB *rendezvousHash = NULL;
597 rendezvousHashEntry *hentry;
600 /* Create a hashtable if we haven't already done so in this process */
601 if (rendezvousHash == NULL)
605 MemSet(&ctl, 0, sizeof(ctl));
606 ctl.keysize = NAMEDATALEN;
607 ctl.entrysize = sizeof(rendezvousHashEntry);
608 rendezvousHash = hash_create("Rendezvous variable hash",
614 /* Find or create the hashtable entry for this varName */
615 hentry = (rendezvousHashEntry *) hash_search(rendezvousHash,
620 /* Initialize to NULL if first time */
622 hentry->varValue = NULL;
624 return &hentry->varValue;