Example #1
0
File: compat.c Project: aiolos/sasc
bool DllsLoad(const char *libdir)
{
  char pat[32];
  snprintf(pat,sizeof(pat),"%s*-%d%s%s",LIBSC_PREFIX,SCAPIVERS,SO_INDICATOR,APIVERSION);
  bool res=true;
  cReadDir dir(libdir);
  struct dirent *e;
  while((e=dir.Next())) {
    if(!fnmatch(pat,e->d_name,FNM_PATHNAME|FNM_NOESCAPE)) {
      if(!DllLoad(AddDirectory(libdir,e->d_name))) res=false;
      }
    }
  return res;
}
Example #2
0
void *wrapper_load( const tsd_t *TSD, const streng *module )
{
   handle_type handle=(handle_type)NULL ;
#if defined(DYNAMIC_OS2)
   CHAR LoadError[256];
   APIRET rc=0L;
#endif
#if defined(DYNAMIC_WIN32)
   char LoadError[256];
#endif
#if defined(DYNAMIC_HPSHLOAD) || defined(DYNAMIC_BEOS)
   char buf[1024];
#endif
#ifdef DYNAMIC_SKYOS
   sDllHandle tmp_handle;
#endif
   char *file_name, *module_name, *udpart, *postfix, *orig_module;

   orig_module = str_ofTSD( module );
#ifdef DYNLIBLEN
   module_name = (char *)MallocTSD( Str_len( module ) + strlen(DYNLIBPRE) +
                                    strlen(DYNLIBPST) + 1 ) ;
   strcpy(module_name, DYNLIBPRE );
   udpart = module_name + strlen(DYNLIBPRE);
   memcpy(udpart, module->value, Str_len(module) );
   strcpy(udpart + Str_len(module), DYNLIBPST );
   file_name = module_name;
   postfix = udpart + Str_len(module);
# if defined(DYNAMIC_HPSHLOAD)
   file_name = buf;
# endif
#else
   file_name = module_name = str_ofTSD(module);
#endif

#if defined(DYNAMIC_STATIC)
   handle = static_dlopen( file_name );
   if (handle == NULL)
   {
      set_err_message(TSD, "static_dlopen() failed loading:", file_name );
      handle = (handle_type)NULL;
   }
#elif defined(DYNAMIC_DLOPEN)
   /*
    * Try and load the module name exactly as specified (before wrapping it
    * in lib*.so)
    */
   handle = dlopen( orig_module, RTLD_LAZY ) ;
   if (handle == NULL)
   {
      handle = dlopen( file_name, RTLD_LAZY ) ;
      /* deal with incorrect case in call */
      if (handle == NULL)
      {
         mem_lower( udpart, Str_len( module ) );
         handle = dlopen(module_name, RTLD_LAZY);

         if (handle == NULL)
         {
            mem_upper( udpart, Str_len( module ) );
            handle = dlopen(module_name, RTLD_LAZY);
            /*
             * Reset the original module portion of the filename to be
             * searched again so that any error message returned uses the
             * original module name
             */
            if ( handle == NULL )
            {
               memcpy(udpart, module->value, Str_len(module) );
               handle = dlopen(module_name, RTLD_LAZY);
            }
         }
      }
   }

   /* or maybe it's just not there */
   if (handle==NULL)
   {
      char *msg=NULL;
      msg = dlerror();
      if (msg)
         set_err_message(TSD, "dlopen() failed: ", msg ) ;
      else
         set_err_message(TSD, "", "");
   }
#elif defined(DYNAMIC_HPSHLOAD)
   /*
    * Try and load the module name exactly as specified (before wrapping it
    * in lib*.sl)
    */
   find_shared_library( TSD, orig_module, "SHLIB_PATH", buf );
   handle = shl_load( file_name, BIND_IMMEDIATE | DYNAMIC_PATH, 0L ) ;
   if (handle == NULL)
   {
      find_shared_library(TSD,module_name,"SHLIB_PATH",buf);
      handle = shl_load( file_name, BIND_IMMEDIATE | DYNAMIC_PATH, 0L ) ;
      if (handle == NULL)
      {
         mem_lower( udpart, Str_len( module ) );
         find_shared_library( TSD, module_name, "SHLIB_PATH", buf );
         handle = shl_load( file_name, BIND_IMMEDIATE | DYNAMIC_PATH ,0L ) ;

         if (handle == NULL)
         {
            mem_upper( udpart, Str_len( module ) );
            find_shared_library( TSD, module_name, "SHLIB_PATH", buf );
            handle = shl_load( file_name, BIND_IMMEDIATE | DYNAMIC_PATH ,0L ) ;
         }
      }
   }

   if (handle==NULL)
      set_err_message(TSD,  "shl_load() failed: ", strerror(errno)) ;
#elif defined(DYNAMIC_AIXLOAD)
   /*
    * Try and load the module name exactly as specified (before wrapping it
    * in lib*.a)
    */
   handle = load( orig_module, 1, NULL ) ;
   if ( handle == NULL )
   {
      handle = load( file_name, 1, NULL ) ;
      if ( handle == NULL )
      {
         set_err_message(TSD,  "load() failed: ", strerror( errno )) ;
      }
      else
      {
         int rc=loadbind( 0, (void *)wrapper_dummy_for_aix, (void *)handle ) ;
         if (rc)
         {
            handle = NULL ;
            set_err_message(TSD,  "loadbind() failed: ", strerror( errno )) ;
         }
      }
#elif defined(DYNAMIC_OS2)
   rc = DosLoadModule( LoadError, sizeof(LoadError),
                       file_name, &handle ) ;
   if (rc)
   {
      set_err_message(TSD, "DosLoadModule() unable to load DLL: ", LoadError);
      handle = (handle_type)NULL;
   }
#elif defined(DYNAMIC_WIN32)
   handle = LoadLibrary( file_name ) ;
   if (handle==NULL)
   {
      char buf[150];
      FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
      sprintf( buf, "Failed to load \"%s\" library: LoadLibrary() failed: ", file_name );
      set_err_message(TSD, buf, LoadError);
   }
#elif defined(DYNAMIC_BEOS)
   handle = load_add_on( orig_module );
   if (handle < B_NO_ERROR)
   {
      handle = load_add_on( file_name );
      if (handle < B_NO_ERROR)
      {
         sprintf( buf, "load_add_on() failed loading \"%s\" with error:", file_name );
         set_err_message(TSD, buf, strerror( handle ) );
         handle = (handle_type)NULL;
      }
   }
#elif defined(DYNAMIC_SKYOS)
   handle = (handle_type)MallocTSD( sizeof( sDllHandle ) );
   /*
    * Don't try and load the module without the trimmings.
    * You get a binary popup window otherwise!
    */
fprintf(stderr,"%s %d %x %s:\n",__FILE__,__LINE__,handle,file_name);
   if ( DllLoad( file_name, &tmp_handle ) != 0 )
   {
      char buf[150];
      sprintf( buf, "Failed to load \"%s\" library: DllLoad() failed: ", file_name );
      set_err_message(TSD, buf, strerror( errno ) );
      FreeTSD( handle );
      handle = (handle_type)NULL;
   }
fprintf(stderr,"%s %d:\n",__FILE__,__LINE__);
   memcpy( handle, &tmp_handle, sizeof( sDllHandle ) );
fprintf(stderr,"%s %d:\n",__FILE__,__LINE__);
#endif

   FreeTSD( module_name );
   FreeTSD( orig_module );

   return (void *)handle ;
}

void wrapper_unload( const tsd_t *TSD, void *libhandle )
{
#ifdef DYNAMIC_STATIC

   libhandle = libhandle;

#elif defined(DYNAMIC_DLOPEN)

   dlclose((handle_type) libhandle);

#elif defined(DYNAMIC_HPSHLOAD)

   shl_unload((handle_type) libhandle);

#elif defined(DYNAMIC_AIXLOAD)

   unload((handle_type) libhandle);

#elif defined(DYNAMIC_OS2)

   DosFreeModule((handle_type) libhandle);

#elif defined(DYNAMIC_WIN32)

   FreeLibrary((handle_type) libhandle);

#elif defined(DYNAMIC_BEOS)

   unload_add_on((handle_type) libhandle);

#elif defined(DYNAMIC_SKYOS)

fprintf(stderr,"%s %d:\n",__FILE__,__LINE__);
   FreeTSD( libhandle );

#else
   (libhandle = libhandle);
#endif
}