Header And Logo

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

freebsd.c

Go to the documentation of this file.
00001 /*
00002  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00003  * Portions Copyright (c) 1990 The Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * src/backend/port/dynloader/freebsd.c
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. Neither the name of the University nor the names of its contributors
00017  *    may be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00024  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00025  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00026  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00027  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00029  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  */
00032 
00033 #if defined(LIBC_SCCS) && !defined(lint)
00034 static char sccsid[] = "@(#)dl.c    5.4 (Berkeley) 2/23/91";
00035 #endif   /* LIBC_SCCS and not lint */
00036 
00037 #include "postgres.h"
00038 
00039 #include <nlist.h>
00040 #include <link.h>
00041 #include <dlfcn.h>
00042 
00043 #include "dynloader.h"
00044 
00045 static char error_message[BUFSIZ];
00046 
00047 char *
00048 BSD44_derived_dlerror(void)
00049 {
00050     static char ret[BUFSIZ];
00051 
00052     strcpy(ret, error_message);
00053     error_message[0] = 0;
00054     return (ret[0] == 0) ? NULL : ret;
00055 }
00056 
00057 void *
00058 BSD44_derived_dlopen(const char *file, int num)
00059 {
00060 #if !defined(HAVE_DLOPEN)
00061     snprintf(error_message, sizeof(error_message),
00062              "dlopen (%s) not supported", file);
00063     return NULL;
00064 #else
00065     void       *vp;
00066 
00067     if ((vp = dlopen((char *) file, num)) == NULL)
00068         snprintf(error_message, sizeof(error_message),
00069                  "dlopen (%s) failed: %s", file, dlerror());
00070     return vp;
00071 #endif
00072 }
00073 
00074 void *
00075 BSD44_derived_dlsym(void *handle, const char *name)
00076 {
00077 #if !defined(HAVE_DLOPEN)
00078     snprintf(error_message, sizeof(error_message),
00079              "dlsym (%s) failed", name);
00080     return NULL;
00081 #else
00082     void       *vp;
00083 
00084 #ifndef __ELF__
00085     char        buf[BUFSIZ];
00086 
00087     if (*name != '_')
00088     {
00089         snprintf(buf, sizeof(buf), "_%s", name);
00090         name = buf;
00091     }
00092 #endif   /* !__ELF__ */
00093     if ((vp = dlsym(handle, (char *) name)) == NULL)
00094         snprintf(error_message, sizeof(error_message),
00095                  "dlsym (%s) failed", name);
00096     return vp;
00097 #endif
00098 }
00099 
00100 void
00101 BSD44_derived_dlclose(void *handle)
00102 {
00103 #if defined(HAVE_DLOPEN)
00104     dlclose(handle);
00105 #endif
00106 }