Header And Logo

PostgreSQL
| The world's most advanced open source database.

linux.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * linux.c
00004  *    Dynamic Loader for Postgres for Linux, generated from those for
00005  *    Ultrix.
00006  *
00007  *    You need to install the dld library on your Linux system!
00008  *
00009  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00010  * Portions Copyright (c) 1994, Regents of the University of California
00011  *
00012  *
00013  * IDENTIFICATION
00014  *    src/backend/port/dynloader/linux.c
00015  *
00016  *-------------------------------------------------------------------------
00017  */
00018 
00019 #include "postgres.h"
00020 
00021 #ifdef HAVE_DLD_H
00022 #include <dld.h>
00023 #endif
00024 
00025 #include "dynloader.h"
00026 #include "miscadmin.h"
00027 
00028 
00029 #ifndef HAVE_DLOPEN
00030 
00031 void *
00032 pg_dlopen(char *filename)
00033 {
00034 #ifndef HAVE_DLD_H
00035     elog(ERROR, "dynamic load not supported");
00036     return NULL;
00037 #else
00038     static int  dl_initialized = 0;
00039 
00040     /*
00041      * initializes the dynamic loader with the executable's pathname. (only
00042      * needs to do this the first time pg_dlopen is called.)
00043      */
00044     if (!dl_initialized)
00045     {
00046         if (dld_init(dld_find_executable(my_exec_path)))
00047             return NULL;
00048 
00049         /*
00050          * if there are undefined symbols, we want dl to search from the
00051          * following libraries also.
00052          */
00053         dl_initialized = 1;
00054     }
00055 
00056     /*
00057      * link the file, then check for undefined symbols!
00058      */
00059     if (dld_link(filename))
00060         return NULL;
00061 
00062     /*
00063      * If undefined symbols: try to link with the C and math libraries! This
00064      * could be smarter, if the dynamic linker was able to handle shared libs!
00065      */
00066     if (dld_undefined_sym_count > 0)
00067     {
00068         if (dld_link("/usr/lib/libc.a"))
00069         {
00070             elog(WARNING, "could not link C library");
00071             return NULL;
00072         }
00073         if (dld_undefined_sym_count > 0)
00074         {
00075             if (dld_link("/usr/lib/libm.a"))
00076             {
00077                 elog(WARNING, "could not link math library");
00078                 return NULL;
00079             }
00080             if (dld_undefined_sym_count > 0)
00081             {
00082                 int         count = dld_undefined_sym_count;
00083                 char      **list = dld_list_undefined_sym();
00084 
00085                 /* list the undefined symbols, if any */
00086                 do
00087                 {
00088                     elog(WARNING, "\"%s\" is undefined", *list);
00089                     list++;
00090                     count--;
00091                 } while (count > 0);
00092 
00093                 dld_unlink_by_file(filename, 1);
00094                 return NULL;
00095             }
00096         }
00097     }
00098 
00099     return (void *) strdup(filename);
00100 #endif
00101 }
00102 
00103 PGFunction
00104 pg_dlsym(void *handle, char *funcname)
00105 {
00106 #ifndef HAVE_DLD_H
00107     return NULL;
00108 #else
00109     return (PGFunction) dld_get_func((funcname));
00110 #endif
00111 }
00112 
00113 void
00114 pg_dlclose(void *handle)
00115 {
00116 #ifndef HAVE_DLD_H
00117 #else
00118     dld_unlink_by_file(handle, 1);
00119     free(handle);
00120 #endif
00121 }
00122 
00123 char *
00124 pg_dlerror(void)
00125 {
00126 #ifndef HAVE_DLD_H
00127     return "dynaloader unspported";
00128 #else
00129     return dld_strerror(dld_errno);
00130 #endif
00131 }
00132 
00133 #endif   /* !HAVE_DLOPEN */