root/src/ftdm_dso.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. FT_DECLARE
  2. FT_DECLARE
  3. FT_DECLARE
  4. FT_DECLARE
  5. FT_DECLARE
  6. FT_DECLARE

   1 /* 
   2  * Cross Platform dso/dll load abstraction
   3  * Copyright(C) 2008 Michael Jerris
   4  *
   5  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
   6  * copies of the Software, and permit persons to whom the Software is
   7  * furnished to do so.
   8  *
   9  * This work is provided under this license on an "as is" basis, without warranty of any kind,
  10  * either expressed or implied, including, without limitation, warranties that the covered code
  11  * is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
  12  * risk as to the quality and performance of the covered code is with you. Should any covered
  13  * code prove defective in any respect, you (not the initial developer or any other contributor)
  14  * assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
  15  * constitutes an essential part of this license. No use of any covered code is authorized hereunder
  16  * except under this disclaimer. 
  17  *
  18  */
  19 
  20 #include "private/ftdm_core.h"
  21 #include "ftdm_dso.h"
  22 #include <stdlib.h>
  23 #include <string.h>
  24 
  25 #ifdef WIN32
  26 #include <windows.h>
  27 #include <stdio.h>
  28 
  29 
  30 FT_DECLARE(ftdm_status_t) ftdm_dso_destroy(ftdm_dso_lib_t *lib) {
  31         if (lib && *lib) {
  32                 FreeLibrary(*lib);
  33                 *lib = NULL;
  34         }
  35         return FTDM_SUCCESS;
  36 }
  37 
  38 FT_DECLARE(ftdm_dso_lib_t) ftdm_dso_open(const char *path, char **err) {
  39     HINSTANCE lib;
  40         
  41         lib = LoadLibraryEx(path, NULL, 0);
  42 
  43         if (!lib) {
  44                 LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  45         }
  46 
  47         if (!lib) {
  48                 DWORD error = GetLastError();
  49                 char tmp[80];
  50                 sprintf(tmp, "dll open error [%ul]\n", error);
  51                 *err = ftdm_strdup(tmp);
  52         }
  53 
  54         return lib;
  55 }
  56 
  57 FT_DECLARE(void*) ftdm_dso_func_sym(ftdm_dso_lib_t lib, const char *sym, char **err) {
  58         FARPROC func = GetProcAddress(lib, sym);
  59         if (!func) {
  60                 DWORD error = GetLastError();
  61                 char tmp[80];
  62                 sprintf(tmp, "dll sym error [%ul]\n", error);
  63                 *err = ftdm_strdup(tmp);
  64         }
  65         return (void *)(intptr_t)func; // this should really be addr - ftdm_dso_func_data
  66 }
  67 
  68 #else
  69 
  70 /*
  71 ** {========================================================================
  72 ** This is an implementation of loadlib based on the dlfcn interface.
  73 ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
  74 ** NetBSD, AIX 4.2, HPUX 11, and  probably most other Unix flavors, at least
  75 ** as an emulation layer on top of native functions.
  76 ** =========================================================================
  77 */
  78 
  79 
  80 #include <dlfcn.h>
  81 
  82 FT_DECLARE(ftdm_status_t) ftdm_dso_destroy(ftdm_dso_lib_t *lib) {
  83         int rc;
  84         if (lib && *lib) {
  85                 rc = dlclose(*lib);
  86                 if (rc) {
  87                         ftdm_log(FTDM_LOG_ERROR, "Failed to close lib %p: %s\n", *lib, dlerror());
  88                         return FTDM_FAIL;
  89                 }
  90                 ftdm_log(FTDM_LOG_DEBUG, "lib %p was closed with success\n", *lib);
  91                 *lib = NULL;
  92                 return FTDM_SUCCESS;
  93         }
  94         ftdm_log(FTDM_LOG_ERROR, "Invalid pointer provided to ftdm_dso_destroy\n");
  95         return FTDM_FAIL;
  96 }
  97 
  98 FT_DECLARE(ftdm_dso_lib_t) ftdm_dso_open(const char *path, char **err) {
  99         void *lib = dlopen(path, RTLD_NOW | RTLD_LOCAL);
 100         if (lib == NULL) {
 101                 *err = ftdm_strdup(dlerror());
 102         }
 103         return lib;
 104 }
 105 
 106 FT_DECLARE(void *) ftdm_dso_func_sym(ftdm_dso_lib_t lib, const char *sym, char **err) {
 107         void *func = dlsym(lib, sym);
 108         if (!func) {
 109                 *err = ftdm_strdup(dlerror());
 110         }
 111         return func;
 112 }
 113 #endif
 114 
 115 /* }====================================================== */
 116 
 117 /* For Emacs:
 118  * Local Variables:
 119  * mode:c
 120  * indent-tabs-mode:t
 121  * tab-width:4
 122  * c-basic-offset:4
 123  * End:
 124  * For VIM:
 125  * vim:set softtabstop=4 shiftwidth=4 tabstop=4
 126  */

/* [<][>][^][v][top][bottom][index][help] */