Header And Logo

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

darwin.c

Go to the documentation of this file.
00001 /*
00002  * Dynamic loading support for Darwin
00003  *
00004  * If dlopen() is available (Darwin 10.3 and later), we just use it.
00005  * Otherwise we emulate it with the older, now deprecated, NSLinkModule API.
00006  *
00007  * src/backend/port/dynloader/darwin.c
00008  */
00009 #include "postgres.h"
00010 
00011 #ifdef HAVE_DLOPEN
00012 #include <dlfcn.h>
00013 #else
00014 #include <mach-o/dyld.h>
00015 #endif
00016 
00017 #include "dynloader.h"
00018 
00019 
00020 #ifdef HAVE_DLOPEN
00021 
00022 void *
00023 pg_dlopen(char *filename)
00024 {
00025     return dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
00026 }
00027 
00028 void
00029 pg_dlclose(void *handle)
00030 {
00031     dlclose(handle);
00032 }
00033 
00034 PGFunction
00035 pg_dlsym(void *handle, char *funcname)
00036 {
00037     /* Do not prepend an underscore: see dlopen(3) */
00038     return dlsym(handle, funcname);
00039 }
00040 
00041 char *
00042 pg_dlerror(void)
00043 {
00044     return dlerror();
00045 }
00046 #else                           /* !HAVE_DLOPEN */
00047 
00048 /*
00049  * These routines were taken from the Apache source, but were made
00050  * available with a PostgreSQL-compatible license.  Kudos Wilfredo
00051  * Sánchez <[email protected]>.
00052  */
00053 
00054 static NSObjectFileImageReturnCode cofiff_result = NSObjectFileImageFailure;
00055 
00056 void *
00057 pg_dlopen(char *filename)
00058 {
00059     NSObjectFileImage image;
00060 
00061     cofiff_result = NSCreateObjectFileImageFromFile(filename, &image);
00062     if (cofiff_result != NSObjectFileImageSuccess)
00063         return NULL;
00064     return NSLinkModule(image, filename,
00065                         NSLINKMODULE_OPTION_BINDNOW |
00066                         NSLINKMODULE_OPTION_RETURN_ON_ERROR);
00067 }
00068 
00069 void
00070 pg_dlclose(void *handle)
00071 {
00072     NSUnLinkModule(handle, FALSE);
00073 }
00074 
00075 PGFunction
00076 pg_dlsym(void *handle, char *funcname)
00077 {
00078     NSSymbol symbol;
00079     char       *symname = (char *) malloc(strlen(funcname) + 2);
00080 
00081     sprintf(symname, "_%s", funcname);
00082     if (NSIsSymbolNameDefined(symname))
00083     {
00084         symbol = NSLookupAndBindSymbol(symname);
00085 
00086         free(symname);
00087         return (PGFunction) NSAddressOfSymbol(symbol);
00088     }
00089     else
00090     {
00091         free(symname);
00092         return NULL;
00093     }
00094 }
00095 
00096 char *
00097 pg_dlerror(void)
00098 {
00099     NSLinkEditErrors c;
00100     int         errorNumber;
00101     const char *fileName;
00102     const char *errorString = NULL;
00103 
00104     switch (cofiff_result)
00105     {
00106         case NSObjectFileImageSuccess:
00107             /* must have failed in NSLinkModule */
00108             NSLinkEditError(&c, &errorNumber, &fileName, &errorString);
00109             if (errorString == NULL || *errorString == '\0')
00110                 errorString = "unknown link-edit failure";
00111             break;
00112         case NSObjectFileImageFailure:
00113             errorString = "failed to open object file";
00114             break;
00115         case NSObjectFileImageInappropriateFile:
00116             errorString = "inappropriate object file";
00117             break;
00118         case NSObjectFileImageArch:
00119             errorString = "object file is for wrong architecture";
00120             break;
00121         case NSObjectFileImageFormat:
00122             errorString = "object file has wrong format";
00123             break;
00124         case NSObjectFileImageAccess:
00125             errorString = "insufficient permissions for object file";
00126             break;
00127         default:
00128             errorString = "unknown failure to open object file";
00129             break;
00130     }
00131 
00132     return (char *) errorString;
00133 }
00134 
00135 #endif   /* HAVE_DLOPEN */