PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
dfmgr.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include "dynloader.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "storage/shmem.h"
#include "utils/dynamic_loader.h"
#include "utils/hsearch.h"
Include dependency graph for dfmgr.c:

Go to the source code of this file.

Data Structures

struct  rendezvousHashEntry
 
struct  df_files
 

Macros

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

Typedefs

typedef void(* PG_init_t )(void)
 
typedef void(* PG_fini_t )(void)
 
typedef struct df_files DynamicFileList
 

Functions

static void * internal_load_library (const char *libname)
 
static void incompatible_module_error (const char *libname, const Pg_magic_struct *module_magic_data) pg_attribute_noreturn()
 
static void internal_unload_library (const char *libname)
 
static bool file_exists (const char *name)
 
static char * expand_dynamic_library_name (const char *name)
 
static void check_restricted_library_name (const char *name)
 
static char * substitute_libpath_macro (const char *name)
 
static char * find_in_dynamic_libpath (const char *basename)
 
PGFunction load_external_function (char *filename, char *funcname, bool signalNotFound, void **filehandle)
 
void load_file (const char *filename, bool restricted)
 
PGFunction lookup_external_function (void *filehandle, char *funcname)
 
void ** find_rendezvous_variable (const char *varName)
 
Size EstimateLibraryStateSpace (void)
 
void SerializeLibraryState (Size maxsize, char *start_address)
 
void RestoreLibraryState (char *start_address)
 

Variables

static DynamicFileListfile_list = NULL
 
static DynamicFileListfile_tail = NULL
 
char * Dynamic_library_path
 
static const Pg_magic_struct magic_data = PG_MODULE_MAGIC_DATA
 

Macro Definition Documentation

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

Definition at line 59 of file dfmgr.c.

Referenced by internal_load_library(), and internal_unload_library().

Typedef Documentation

typedef void(* PG_fini_t)(void)

Definition at line 29 of file dfmgr.c.

typedef void(* PG_init_t)(void)

Definition at line 28 of file dfmgr.c.

Function Documentation

static void check_restricted_library_name ( const char *  name)
static

Definition at line 537 of file dfmgr.c.

References ereport, errcode(), errmsg(), ERROR, first_dir_separator(), and NULL.

Referenced by load_file().

538 {
539  if (strncmp(name, "$libdir/plugins/", 16) != 0 ||
540  first_dir_separator(name + 16) != NULL)
541  ereport(ERROR,
542  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
543  errmsg("access to library \"%s\" is not allowed",
544  name)));
545 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
char * first_dir_separator(const char *filename)
Definition: path.c:103
#define NULL
Definition: c.h:229
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
Size EstimateLibraryStateSpace ( void  )

Definition at line 706 of file dfmgr.c.

References add_size(), df_files::filename, df_files::next, and NULL.

Referenced by InitializeParallelDSM().

707 {
708  DynamicFileList *file_scanner;
709  Size size = 1;
710 
711  for (file_scanner = file_list;
712  file_scanner != NULL;
713  file_scanner = file_scanner->next)
714  size = add_size(size, strlen(file_scanner->filename) + 1);
715 
716  return size;
717 }
char filename[FLEXIBLE_ARRAY_MEMBER]
Definition: dfmgr.c:51
static DynamicFileList * file_list
Definition: dfmgr.c:54
Definition: dfmgr.c:42
struct df_files * next
Definition: dfmgr.c:44
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define NULL
Definition: c.h:229
size_t Size
Definition: c.h:356
static char * expand_dynamic_library_name ( const char *  name)
static

Definition at line 482 of file dfmgr.c.

References AssertArg, file_exists(), find_in_dynamic_libpath(), first_dir_separator(), NULL, pfree(), psprintf(), pstrdup(), and substitute_libpath_macro().

Referenced by load_external_function(), and load_file().

483 {
484  bool have_slash;
485  char *new;
486  char *full;
487 
488  AssertArg(name);
489 
490  have_slash = (first_dir_separator(name) != NULL);
491 
492  if (!have_slash)
493  {
495  if (full)
496  return full;
497  }
498  else
499  {
501  if (file_exists(full))
502  return full;
503  pfree(full);
504  }
505 
506  new = psprintf("%s%s", name, DLSUFFIX);
507 
508  if (!have_slash)
509  {
510  full = find_in_dynamic_libpath(new);
511  pfree(new);
512  if (full)
513  return full;
514  }
515  else
516  {
517  full = substitute_libpath_macro(new);
518  pfree(new);
519  if (file_exists(full))
520  return full;
521  pfree(full);
522  }
523 
524  /*
525  * If we can't find the file, just return the string as-is. The ensuing
526  * load attempt will fail and report a suitable message.
527  */
528  return pstrdup(name);
529 }
static char * substitute_libpath_macro(const char *name)
Definition: dfmgr.c:552
char * pstrdup(const char *in)
Definition: mcxt.c:1077
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void pfree(void *pointer)
Definition: mcxt.c:950
static bool file_exists(const char *name)
Definition: dfmgr.c:450
#define AssertArg(condition)
Definition: c.h:677
char * first_dir_separator(const char *filename)
Definition: path.c:103
#define NULL
Definition: c.h:229
const char * name
Definition: encode.c:521
static char * find_in_dynamic_libpath(const char *basename)
Definition: dfmgr.c:583
static bool file_exists ( const char *  name)
static

Definition at line 450 of file dfmgr.c.

References AssertArg, ereport, errcode_for_file_access(), errmsg(), ERROR, and NULL.

Referenced by expand_dynamic_library_name(), find_in_dynamic_libpath(), isolation_start_test(), and psql_start_test().

451 {
452  struct stat st;
453 
454  AssertArg(name != NULL);
455 
456  if (stat(name, &st) == 0)
457  return S_ISDIR(st.st_mode) ? false : true;
458  else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
459  ereport(ERROR,
461  errmsg("could not access file \"%s\": %m", name)));
462 
463  return false;
464 }
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:677
#define NULL
Definition: c.h:229
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
static char * find_in_dynamic_libpath ( const char *  basename)
static

Definition at line 583 of file dfmgr.c.

References AssertArg, AssertState, canonicalize_path(), DEBUG3, Dynamic_library_path, elog, ereport, errcode(), errmsg(), ERROR, file_exists(), first_dir_separator(), first_path_var_separator(), is_absolute_path, NULL, palloc(), pfree(), strlcpy(), and substitute_libpath_macro().

Referenced by expand_dynamic_library_name().

584 {
585  const char *p;
586  size_t baselen;
587 
588  AssertArg(basename != NULL);
589  AssertArg(first_dir_separator(basename) == NULL);
591 
593  if (strlen(p) == 0)
594  return NULL;
595 
596  baselen = strlen(basename);
597 
598  for (;;)
599  {
600  size_t len;
601  char *piece;
602  char *mangled;
603  char *full;
604 
605  piece = first_path_var_separator(p);
606  if (piece == p)
607  ereport(ERROR,
608  (errcode(ERRCODE_INVALID_NAME),
609  errmsg("zero-length component in parameter \"dynamic_library_path\"")));
610 
611  if (piece == NULL)
612  len = strlen(p);
613  else
614  len = piece - p;
615 
616  piece = palloc(len + 1);
617  strlcpy(piece, p, len + 1);
618 
619  mangled = substitute_libpath_macro(piece);
620  pfree(piece);
621 
622  canonicalize_path(mangled);
623 
624  /* only absolute paths */
625  if (!is_absolute_path(mangled))
626  ereport(ERROR,
627  (errcode(ERRCODE_INVALID_NAME),
628  errmsg("component in parameter \"dynamic_library_path\" is not an absolute path")));
629 
630  full = palloc(strlen(mangled) + 1 + baselen + 1);
631  sprintf(full, "%s/%s", mangled, basename);
632  pfree(mangled);
633 
634  elog(DEBUG3, "find_in_dynamic_libpath: trying \"%s\"", full);
635 
636  if (file_exists(full))
637  return full;
638 
639  pfree(full);
640 
641  if (p[len] == '\0')
642  break;
643  else
644  p += len + 1;
645  }
646 
647  return NULL;
648 }
#define AssertState(condition)
Definition: c.h:678
#define DEBUG3
Definition: elog.h:23
static char * substitute_libpath_macro(const char *name)
Definition: dfmgr.c:552
void canonicalize_path(char *path)
Definition: path.c:254
int errcode(int sqlerrcode)
Definition: elog.c:575
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
static bool file_exists(const char *name)
Definition: dfmgr.c:450
char * Dynamic_library_path
Definition: dfmgr.c:64
#define is_absolute_path(filename)
Definition: port.h:77
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:677
char * first_dir_separator(const char *filename)
Definition: path.c:103
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define NULL
Definition: c.h:229
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
char * first_path_var_separator(const char *pathlist)
Definition: path.c:120
void** find_rendezvous_variable ( const char *  varName)

Definition at line 667 of file dfmgr.c.

References HASHCTL::entrysize, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), HASHCTL::keysize, MemSet, NAMEDATALEN, NULL, and rendezvousHashEntry::varValue.

Referenced by _PG_init().

668 {
669  static HTAB *rendezvousHash = NULL;
670 
671  rendezvousHashEntry *hentry;
672  bool found;
673 
674  /* Create a hashtable if we haven't already done so in this process */
675  if (rendezvousHash == NULL)
676  {
677  HASHCTL ctl;
678 
679  MemSet(&ctl, 0, sizeof(ctl));
680  ctl.keysize = NAMEDATALEN;
681  ctl.entrysize = sizeof(rendezvousHashEntry);
682  rendezvousHash = hash_create("Rendezvous variable hash",
683  16,
684  &ctl,
685  HASH_ELEM);
686  }
687 
688  /* Find or create the hashtable entry for this varName */
689  hentry = (rendezvousHashEntry *) hash_search(rendezvousHash,
690  varName,
691  HASH_ENTER,
692  &found);
693 
694  /* Initialize to NULL if first time */
695  if (!found)
696  hentry->varValue = NULL;
697 
698  return &hentry->varValue;
699 }
void * varValue
Definition: dfmgr.c:35
#define HASH_ELEM
Definition: hsearch.h:87
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:857
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
#define NAMEDATALEN
Definition: dynahash.c:193
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
Size keysize
Definition: hsearch.h:72
#define NULL
Definition: c.h:229
static void incompatible_module_error ( const char *  libname,
const Pg_magic_struct module_magic_data 
)
static

Definition at line 293 of file dfmgr.c.

References _, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), StringInfoData::data, ereport, errdetail(), errdetail_internal(), errmsg(), ERROR, Pg_magic_struct::float4byval, Pg_magic_struct::float8byval, Pg_magic_struct::funcmaxargs, Pg_magic_struct::indexmaxkeys, initStringInfo(), StringInfoData::len, Pg_magic_struct::namedatalen, snprintf(), and Pg_magic_struct::version.

Referenced by internal_load_library().

295 {
296  StringInfoData details;
297 
298  /*
299  * If the version doesn't match, just report that, because the rest of the
300  * block might not even have the fields we expect.
301  */
302  if (magic_data.version != module_magic_data->version)
303  {
304  char library_version[32];
305 
306  if (module_magic_data->version >= 1000)
307  snprintf(library_version, sizeof(library_version), "%d",
308  module_magic_data->version / 100);
309  else
310  snprintf(library_version, sizeof(library_version), "%d.%d",
311  module_magic_data->version / 100,
312  module_magic_data->version % 100);
313  ereport(ERROR,
314  (errmsg("incompatible library \"%s\": version mismatch",
315  libname),
316  errdetail("Server is version %d, library is version %s.",
317  magic_data.version / 100, library_version)));
318  }
319 
320  /*
321  * Otherwise, spell out which fields don't agree.
322  *
323  * XXX this code has to be adjusted any time the set of fields in a magic
324  * block change!
325  */
326  initStringInfo(&details);
327 
328  if (module_magic_data->funcmaxargs != magic_data.funcmaxargs)
329  {
330  if (details.len)
331  appendStringInfoChar(&details, '\n');
332  appendStringInfo(&details,
333  _("Server has FUNC_MAX_ARGS = %d, library has %d."),
335  module_magic_data->funcmaxargs);
336  }
337  if (module_magic_data->indexmaxkeys != magic_data.indexmaxkeys)
338  {
339  if (details.len)
340  appendStringInfoChar(&details, '\n');
341  appendStringInfo(&details,
342  _("Server has INDEX_MAX_KEYS = %d, library has %d."),
344  module_magic_data->indexmaxkeys);
345  }
346  if (module_magic_data->namedatalen != magic_data.namedatalen)
347  {
348  if (details.len)
349  appendStringInfoChar(&details, '\n');
350  appendStringInfo(&details,
351  _("Server has NAMEDATALEN = %d, library has %d."),
353  module_magic_data->namedatalen);
354  }
355  if (module_magic_data->float4byval != magic_data.float4byval)
356  {
357  if (details.len)
358  appendStringInfoChar(&details, '\n');
359  appendStringInfo(&details,
360  _("Server has FLOAT4PASSBYVAL = %s, library has %s."),
361  magic_data.float4byval ? "true" : "false",
362  module_magic_data->float4byval ? "true" : "false");
363  }
364  if (module_magic_data->float8byval != magic_data.float8byval)
365  {
366  if (details.len)
367  appendStringInfoChar(&details, '\n');
368  appendStringInfo(&details,
369  _("Server has FLOAT8PASSBYVAL = %s, library has %s."),
370  magic_data.float8byval ? "true" : "false",
371  module_magic_data->float8byval ? "true" : "false");
372  }
373 
374  if (details.len == 0)
375  appendStringInfoString(&details,
376  _("Magic block has unexpected length or padding difference."));
377 
378  ereport(ERROR,
379  (errmsg("incompatible library \"%s\": magic block mismatch",
380  libname),
381  errdetail_internal("%s", details.data)));
382 }
int indexmaxkeys
Definition: fmgr.h:416
int namedatalen
Definition: fmgr.h:417
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static const Pg_magic_struct magic_data
Definition: dfmgr.c:77
int errdetail_internal(const char *fmt,...)
Definition: elog.c:900
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
int funcmaxargs
Definition: fmgr.h:415
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
int float8byval
Definition: fmgr.h:419
int float4byval
Definition: fmgr.h:418
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define _(x)
Definition: elog.c:84
int version
Definition: fmgr.h:414
static void * internal_load_library ( const char *  libname)
static

Definition at line 171 of file dfmgr.c.

References df_files::device, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, df_files::filename, filename, free, df_files::handle, incompatible_module_error(), df_files::inode, Pg_magic_struct::len, malloc, MemSet, df_files::next, NULL, offsetof, pg_dlclose, pg_dlerror, pg_dlopen, pg_dlsym, PG_MAGIC_FUNCTION_NAME_STRING, and SAME_INODE.

Referenced by load_external_function(), load_file(), and RestoreLibraryState().

172 {
173  DynamicFileList *file_scanner;
174  PGModuleMagicFunction magic_func;
175  char *load_error;
176  struct stat stat_buf;
177  PG_init_t PG_init;
178 
179  /*
180  * Scan the list of loaded FILES to see if the file has been loaded.
181  */
182  for (file_scanner = file_list;
183  file_scanner != NULL &&
184  strcmp(libname, file_scanner->filename) != 0;
185  file_scanner = file_scanner->next)
186  ;
187 
188  if (file_scanner == NULL)
189  {
190  /*
191  * Check for same files - different paths (ie, symlink or link)
192  */
193  if (stat(libname, &stat_buf) == -1)
194  ereport(ERROR,
196  errmsg("could not access file \"%s\": %m",
197  libname)));
198 
199  for (file_scanner = file_list;
200  file_scanner != NULL &&
201  !SAME_INODE(stat_buf, *file_scanner);
202  file_scanner = file_scanner->next)
203  ;
204  }
205 
206  if (file_scanner == NULL)
207  {
208  /*
209  * File not loaded yet.
210  */
211  file_scanner = (DynamicFileList *)
212  malloc(offsetof(DynamicFileList, filename) +strlen(libname) + 1);
213  if (file_scanner == NULL)
214  ereport(ERROR,
215  (errcode(ERRCODE_OUT_OF_MEMORY),
216  errmsg("out of memory")));
217 
218  MemSet(file_scanner, 0, offsetof(DynamicFileList, filename));
219  strcpy(file_scanner->filename, libname);
220  file_scanner->device = stat_buf.st_dev;
221 #ifndef WIN32
222  file_scanner->inode = stat_buf.st_ino;
223 #endif
224  file_scanner->next = NULL;
225 
226  file_scanner->handle = pg_dlopen(file_scanner->filename);
227  if (file_scanner->handle == NULL)
228  {
229  load_error = (char *) pg_dlerror();
230  free((char *) file_scanner);
231  /* errcode_for_file_access might not be appropriate here? */
232  ereport(ERROR,
234  errmsg("could not load library \"%s\": %s",
235  libname, load_error)));
236  }
237 
238  /* Check the magic function to determine compatibility */
239  magic_func = (PGModuleMagicFunction)
241  if (magic_func)
242  {
243  const Pg_magic_struct *magic_data_ptr = (*magic_func) ();
244 
245  if (magic_data_ptr->len != magic_data.len ||
246  memcmp(magic_data_ptr, &magic_data, magic_data.len) != 0)
247  {
248  /* copy data block before unlinking library */
249  Pg_magic_struct module_magic_data = *magic_data_ptr;
250 
251  /* try to unlink library */
252  pg_dlclose(file_scanner->handle);
253  free((char *) file_scanner);
254 
255  /* issue suitable complaint */
256  incompatible_module_error(libname, &module_magic_data);
257  }
258  }
259  else
260  {
261  /* try to unlink library */
262  pg_dlclose(file_scanner->handle);
263  free((char *) file_scanner);
264  /* complain */
265  ereport(ERROR,
266  (errmsg("incompatible library \"%s\": missing magic block",
267  libname),
268  errhint("Extension libraries are required to use the PG_MODULE_MAGIC macro.")));
269  }
270 
271  /*
272  * If the library has a _PG_init() function, call it.
273  */
274  PG_init = (PG_init_t) pg_dlsym(file_scanner->handle, "_PG_init");
275  if (PG_init)
276  (*PG_init) ();
277 
278  /* OK to link it into list */
279  if (file_list == NULL)
280  file_list = file_scanner;
281  else
282  file_tail->next = file_scanner;
283  file_tail = file_scanner;
284  }
285 
286  return file_scanner->handle;
287 }
char filename[FLEXIBLE_ARRAY_MEMBER]
Definition: dfmgr.c:51
#define SAME_INODE(A, B)
Definition: dfmgr.c:59
int errhint(const char *fmt,...)
Definition: elog.c:987
static void incompatible_module_error(const char *libname, const Pg_magic_struct *module_magic_data) pg_attribute_noreturn()
Definition: dfmgr.c:293
static DynamicFileList * file_list
Definition: dfmgr.c:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:857
#define pg_dlclose(h)
Definition: aix.h:36
static const Pg_magic_struct magic_data
Definition: dfmgr.c:77
#define pg_dlopen(f)
Definition: aix.h:34
#define malloc(a)
Definition: header.h:50
#define ERROR
Definition: elog.h:43
struct stat stat_buf
Definition: pg_standby.c:101
void * handle
Definition: dfmgr.c:50
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
#define pg_dlerror()
Definition: aix.h:37
Definition: dfmgr.c:42
#define PG_MAGIC_FUNCTION_NAME_STRING
Definition: fmgr.h:441
struct df_files * next
Definition: dfmgr.c:44
dev_t device
Definition: dfmgr.c:45
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
#define pg_dlsym(h, f)
Definition: aix.h:35
void(* PG_init_t)(void)
Definition: dfmgr.c:28
static DynamicFileList * file_tail
Definition: dfmgr.c:55
ino_t inode
Definition: dfmgr.c:48
static char * filename
Definition: pg_dumpall.c:87
int errmsg(const char *fmt,...)
Definition: elog.c:797
const Pg_magic_struct *(* PGModuleMagicFunction)(void)
Definition: fmgr.h:438
#define offsetof(type, field)
Definition: c.h:555
static void internal_unload_library ( const char *  libname)
static

Definition at line 396 of file dfmgr.c.

References clear_external_function_hash(), ereport, errcode_for_file_access(), errmsg(), ERROR, df_files::filename, free, df_files::handle, df_files::next, NULL, pg_dlclose, pg_dlsym, and SAME_INODE.

Referenced by load_file().

397 {
398 #ifdef NOT_USED
399  DynamicFileList *file_scanner,
400  *prv,
401  *nxt;
402  struct stat stat_buf;
403  PG_fini_t PG_fini;
404 
405  /*
406  * We need to do stat() in order to determine whether this is the same
407  * file as a previously loaded file; it's also handy so as to give a good
408  * error message if bogus file name given.
409  */
410  if (stat(libname, &stat_buf) == -1)
411  ereport(ERROR,
413  errmsg("could not access file \"%s\": %m", libname)));
414 
415  /*
416  * We have to zap all entries in the list that match on either filename or
417  * inode, else internal_load_library() will still think it's present.
418  */
419  prv = NULL;
420  for (file_scanner = file_list; file_scanner != NULL; file_scanner = nxt)
421  {
422  nxt = file_scanner->next;
423  if (strcmp(libname, file_scanner->filename) == 0 ||
424  SAME_INODE(stat_buf, *file_scanner))
425  {
426  if (prv)
427  prv->next = nxt;
428  else
429  file_list = nxt;
430 
431  /*
432  * If the library has a _PG_fini() function, call it.
433  */
434  PG_fini = (PG_fini_t) pg_dlsym(file_scanner->handle, "_PG_fini");
435  if (PG_fini)
436  (*PG_fini) ();
437 
438  clear_external_function_hash(file_scanner->handle);
439  pg_dlclose(file_scanner->handle);
440  free((char *) file_scanner);
441  /* prv does not change */
442  }
443  else
444  prv = file_scanner;
445  }
446 #endif /* NOT_USED */
447 }
char filename[FLEXIBLE_ARRAY_MEMBER]
Definition: dfmgr.c:51
#define SAME_INODE(A, B)
Definition: dfmgr.c:59
static DynamicFileList * file_list
Definition: dfmgr.c:54
#define pg_dlclose(h)
Definition: aix.h:36
#define ERROR
Definition: elog.h:43
struct stat stat_buf
Definition: pg_standby.c:101
void * handle
Definition: dfmgr.c:50
int errcode_for_file_access(void)
Definition: elog.c:598
void clear_external_function_hash(void *filehandle)
Definition: fmgr.c:567
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: dfmgr.c:42
struct df_files * next
Definition: dfmgr.c:44
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
#define pg_dlsym(h, f)
Definition: aix.h:35
int errmsg(const char *fmt,...)
Definition: elog.c:797
void(* PG_fini_t)(void)
Definition: dfmgr.c:29
PGFunction load_external_function ( char *  filename,
char *  funcname,
bool  signalNotFound,
void **  filehandle 
)

Definition at line 94 of file dfmgr.c.

References ereport, errcode(), errmsg(), ERROR, expand_dynamic_library_name(), internal_load_library(), NULL, pfree(), and pg_dlsym.

Referenced by _PG_init(), fmgr_c_validator(), fmgr_info_C_lang(), LoadOutputPlugin(), ParallelExtensionTrampoline(), and StartBackgroundWorker().

96 {
97  char *fullname;
98  void *lib_handle;
99  PGFunction retval;
100 
101  /* Expand the possibly-abbreviated filename to an exact path name */
103 
104  /* Load the shared library, unless we already did */
105  lib_handle = internal_load_library(fullname);
106 
107  /* Return handle if caller wants it */
108  if (filehandle)
109  *filehandle = lib_handle;
110 
111  /* Look up the function within the library */
112  retval = (PGFunction) pg_dlsym(lib_handle, funcname);
113 
114  if (retval == NULL && signalNotFound)
115  ereport(ERROR,
116  (errcode(ERRCODE_UNDEFINED_FUNCTION),
117  errmsg("could not find function \"%s\" in file \"%s\"",
118  funcname, fullname)));
119 
120  pfree(fullname);
121  return retval;
122 }
Datum(* PGFunction)(FunctionCallInfo fcinfo)
Definition: fmgr.h:40
int errcode(int sqlerrcode)
Definition: elog.c:575
static char * expand_dynamic_library_name(const char *name)
Definition: dfmgr.c:482
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
static void * internal_load_library(const char *libname)
Definition: dfmgr.c:171
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
#define pg_dlsym(h, f)
Definition: aix.h:35
static char * filename
Definition: pg_dumpall.c:87
int errmsg(const char *fmt,...)
Definition: elog.c:797
void load_file ( const char *  filename,
bool  restricted 
)

Definition at line 133 of file dfmgr.c.

References check_restricted_library_name(), expand_dynamic_library_name(), internal_load_library(), internal_unload_library(), and pfree().

Referenced by AlterSubscription_refresh(), ApplyWorkerMain(), CreateSubscription(), DropSubscription(), load_libraries(), standard_ProcessUtility(), and WalReceiverMain().

134 {
135  char *fullname;
136 
137  /* Apply security restriction if requested */
138  if (restricted)
140 
141  /* Expand the possibly-abbreviated filename to an exact path name */
143 
144  /* Unload the library if currently loaded */
145  internal_unload_library(fullname);
146 
147  /* Load the shared library */
148  (void) internal_load_library(fullname);
149 
150  pfree(fullname);
151 }
static char * expand_dynamic_library_name(const char *name)
Definition: dfmgr.c:482
static void internal_unload_library(const char *libname)
Definition: dfmgr.c:396
void pfree(void *pointer)
Definition: mcxt.c:950
static void * internal_load_library(const char *libname)
Definition: dfmgr.c:171
static void check_restricted_library_name(const char *name)
Definition: dfmgr.c:537
static char * filename
Definition: pg_dumpall.c:87
PGFunction lookup_external_function ( void *  filehandle,
char *  funcname 
)

Definition at line 158 of file dfmgr.c.

References pg_dlsym.

Referenced by fetch_finfo_record().

159 {
160  return (PGFunction) pg_dlsym(filehandle, funcname);
161 }
Datum(* PGFunction)(FunctionCallInfo fcinfo)
Definition: fmgr.h:40
#define pg_dlsym(h, f)
Definition: aix.h:35
void RestoreLibraryState ( char *  start_address)

Definition at line 745 of file dfmgr.c.

References internal_load_library().

Referenced by ParallelWorkerMain().

746 {
747  while (*start_address != '\0')
748  {
749  internal_load_library(start_address);
750  start_address += strlen(start_address) + 1;
751  }
752 }
static void * internal_load_library(const char *libname)
Definition: dfmgr.c:171
void SerializeLibraryState ( Size  maxsize,
char *  start_address 
)

Definition at line 723 of file dfmgr.c.

References Assert, df_files::filename, df_files::next, NULL, and strlcpy().

Referenced by InitializeParallelDSM().

724 {
725  DynamicFileList *file_scanner;
726 
727  for (file_scanner = file_list;
728  file_scanner != NULL;
729  file_scanner = file_scanner->next)
730  {
731  Size len;
732 
733  len = strlcpy(start_address, file_scanner->filename, maxsize) + 1;
734  Assert(len < maxsize);
735  maxsize -= len;
736  start_address += len;
737  }
738  start_address[0] = '\0';
739 }
char filename[FLEXIBLE_ARRAY_MEMBER]
Definition: dfmgr.c:51
static DynamicFileList * file_list
Definition: dfmgr.c:54
Definition: dfmgr.c:42
struct df_files * next
Definition: dfmgr.c:44
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
size_t Size
Definition: c.h:356
static char * substitute_libpath_macro ( const char *  name)
static

Definition at line 552 of file dfmgr.c.

References AssertArg, ereport, errcode(), errmsg(), ERROR, first_dir_separator(), NULL, pkglib_path, psprintf(), and pstrdup().

Referenced by expand_dynamic_library_name(), and find_in_dynamic_libpath().

553 {
554  const char *sep_ptr;
555 
556  AssertArg(name != NULL);
557 
558  /* Currently, we only recognize $libdir at the start of the string */
559  if (name[0] != '$')
560  return pstrdup(name);
561 
562  if ((sep_ptr = first_dir_separator(name)) == NULL)
563  sep_ptr = name + strlen(name);
564 
565  if (strlen("$libdir") != sep_ptr - name ||
566  strncmp(name, "$libdir", strlen("$libdir")) != 0)
567  ereport(ERROR,
568  (errcode(ERRCODE_INVALID_NAME),
569  errmsg("invalid macro name in dynamic library path: %s",
570  name)));
571 
572  return psprintf("%s%s", pkglib_path, sep_ptr);
573 }
char * pstrdup(const char *in)
Definition: mcxt.c:1077
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:677
char * first_dir_separator(const char *filename)
Definition: path.c:103
#define NULL
Definition: c.h:229
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
char pkglib_path[MAXPGPATH]
Definition: globals.c:64

Variable Documentation

char* Dynamic_library_path

Definition at line 64 of file dfmgr.c.

Referenced by find_in_dynamic_libpath().

DynamicFileList* file_list = NULL
static

Definition at line 54 of file dfmgr.c.

DynamicFileList* file_tail = NULL
static

Definition at line 55 of file dfmgr.c.

const Pg_magic_struct magic_data = PG_MODULE_MAGIC_DATA
static

Definition at line 77 of file dfmgr.c.