PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
linux.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * linux.c
4  * Dynamic Loader for Postgres for Linux, generated from those for
5  * Ultrix.
6  *
7  * You need to install the dld library on your Linux system!
8  *
9  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  *
13  * IDENTIFICATION
14  * src/backend/port/dynloader/linux.c
15  *
16  *-------------------------------------------------------------------------
17  */
18 
19 #include "postgres.h"
20 
21 #ifdef HAVE_DLD_H
22 #include <dld.h>
23 #endif
24 
25 #include "dynloader.h"
26 #include "miscadmin.h"
27 
28 
29 #ifndef HAVE_DLOPEN
30 
31 void *
33 {
34 #ifndef HAVE_DLD_H
35  elog(ERROR, "dynamic load not supported");
36  return NULL;
37 #else
38  static int dl_initialized = 0;
39 
40  /*
41  * initializes the dynamic loader with the executable's pathname. (only
42  * needs to do this the first time pg_dlopen is called.)
43  */
44  if (!dl_initialized)
45  {
46  if (dld_init(dld_find_executable(my_exec_path)))
47  return NULL;
48 
49  /*
50  * if there are undefined symbols, we want dl to search from the
51  * following libraries also.
52  */
53  dl_initialized = 1;
54  }
55 
56  /*
57  * link the file, then check for undefined symbols!
58  */
59  if (dld_link(filename))
60  return NULL;
61 
62  /*
63  * If undefined symbols: try to link with the C and math libraries! This
64  * could be smarter, if the dynamic linker was able to handle shared libs!
65  */
66  if (dld_undefined_sym_count > 0)
67  {
68  if (dld_link("/usr/lib/libc.a"))
69  {
70  elog(WARNING, "could not link C library");
71  return NULL;
72  }
73  if (dld_undefined_sym_count > 0)
74  {
75  if (dld_link("/usr/lib/libm.a"))
76  {
77  elog(WARNING, "could not link math library");
78  return NULL;
79  }
80  if (dld_undefined_sym_count > 0)
81  {
82  int count = dld_undefined_sym_count;
83  char **list = dld_list_undefined_sym();
84 
85  /* list the undefined symbols, if any */
86  do
87  {
88  elog(WARNING, "\"%s\" is undefined", *list);
89  list++;
90  count--;
91  } while (count > 0);
92 
93  dld_unlink_by_file(filename, 1);
94  return NULL;
95  }
96  }
97  }
98 
99  return (void *) strdup(filename);
100 #endif
101 }
102 
104 pg_dlsym(void *handle, char *funcname)
105 {
106 #ifndef HAVE_DLD_H
107  return NULL;
108 #else
109  return (PGFunction) dld_get_func((funcname));
110 #endif
111 }
112 
113 void
114 pg_dlclose(void *handle)
115 {
116 #ifndef HAVE_DLD_H
117 #else
118  dld_unlink_by_file(handle, 1);
119  free(handle);
120 #endif
121 }
122 
123 char *
125 {
126 #ifndef HAVE_DLD_H
127  return "dynaloader unsupported";
128 #else
129  return dld_strerror(dld_errno);
130 #endif
131 }
132 
133 #endif /* !HAVE_DLOPEN */
void * pg_dlopen(char *filename)
Definition: linux.c:32
Datum(* PGFunction)(FunctionCallInfo fcinfo)
Definition: fmgr.h:40
#define ERROR
Definition: elog.h:43
PGFunction pg_dlsym(void *handle, char *funcname)
Definition: linux.c:104
char * pg_dlerror(void)
Definition: linux.c:124
#define WARNING
Definition: elog.h:40
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
void pg_dlclose(void *handle)
Definition: linux.c:114
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
static char * filename
Definition: pg_dumpall.c:89
tuple list
Definition: sort-test.py:11
#define elog
Definition: elog.h:219