dynalib.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* dynalib.c - dynamic library routines */
  13. #include <stdio.h>
  14. #include "prlink.h"
  15. #include "slap.h"
  16. #if defined(SOLARIS)
  17. #include <dlfcn.h> /* dlerror */
  18. #endif
  19. static struct dynalib {
  20. char *dl_name;
  21. PRLibrary *dl_handle;
  22. } **libs = NULL;
  23. static void symload_report_error( const char *libpath, char *symbol, char *plugin,
  24. int libopen );
  25. static void free_plugin_name(char *name)
  26. {
  27. PR_smprintf_free(name);
  28. }
  29. void *
  30. sym_load( char *libpath, char *symbol, char *plugin, int report_errors )
  31. {
  32. return sym_load_with_flags(libpath, symbol, plugin, report_errors, PR_FALSE, PR_FALSE);
  33. }
  34. /* libpath is the pathname from the plugin config entry - it may be an absolute path
  35. or a relative path. It does not have to have the shared lib/dll suffix. The
  36. PR_GetLibraryName function will create the correct library name and path, including
  37. the correct shared library suffix for the platform. So, for example, if you just
  38. pass in "libacl-plugin" as the libpath, and you are running on linux, the code
  39. will first test for the existence of "libacl-plugin", then will construct the full
  40. pathname to load as "PLUGINDIR/libacl-plugin.so" where PLUGINDIR is set during
  41. build time to something like /usr/lib/brand/plugins.
  42. */
  43. void *
  44. sym_load_with_flags( char *libpath, char *symbol, char *plugin, int report_errors, PRBool load_now, PRBool load_global )
  45. {
  46. int i;
  47. void *handle;
  48. PRLibSpec libSpec;
  49. unsigned int flags = PR_LD_LAZY; /* default PR_LoadLibrary flag */
  50. libSpec.type = PR_LibSpec_Pathname;
  51. libSpec.value.pathname = libpath;
  52. for ( i = 0; libs != NULL && libs[i] != NULL; i++ ) {
  53. if ( strcasecmp( libs[i]->dl_name, libpath ) == 0 ) {
  54. handle = PR_FindSymbol( libs[i]->dl_handle, symbol );
  55. if ( NULL == handle && report_errors ) {
  56. symload_report_error( libpath, symbol, plugin, 1 /* lib open */ );
  57. }
  58. return handle;
  59. }
  60. }
  61. if (load_now) {
  62. flags = PR_LD_NOW;
  63. }
  64. if (load_global) {
  65. flags |= PR_LD_GLOBAL;
  66. }
  67. if (PR_SUCCESS != PR_Access(libpath, PR_ACCESS_READ_OK)) {
  68. if (strncmp(libpath, PLUGINDIR, strlen(PLUGINDIR)) && libpath[0] != '/') {
  69. libSpec.value.pathname = slapi_get_plugin_name(PLUGINDIR, libpath);
  70. } else {
  71. libSpec.value.pathname = slapi_get_plugin_name(NULL, libpath);
  72. }
  73. /* then just handle that failure case with symload_report_error below */
  74. }
  75. if ( (handle = PR_LoadLibraryWithFlags( libSpec, flags )) == NULL ) {
  76. if ( report_errors ) {
  77. symload_report_error( libSpec.value.pathname, symbol, plugin, 0 /* lib not open */ );
  78. }
  79. if (libSpec.value.pathname != libpath) {
  80. free_plugin_name((char *)libSpec.value.pathname); /* cast ok - allocated by slapi_get_plugin_name */
  81. }
  82. return( NULL );
  83. }
  84. libs = (struct dynalib **) slapi_ch_realloc( (char *) libs, (i + 2) *
  85. sizeof(struct dynalib *) );
  86. libs[i] = (struct dynalib *) slapi_ch_malloc( sizeof(struct dynalib) );
  87. libs[i]->dl_name = slapi_ch_strdup( libpath );
  88. libs[i]->dl_handle = handle;
  89. libs[ i + 1 ] = NULL;
  90. handle = PR_FindSymbol( libs[i]->dl_handle, symbol );
  91. if ( NULL == handle && report_errors ) {
  92. symload_report_error( libSpec.value.pathname, symbol, plugin, 1 /* lib open */ );
  93. }
  94. if (libSpec.value.pathname != libpath) {
  95. free_plugin_name((char *)libSpec.value.pathname); /* cast ok - allocated by PR_GetLibraryName */
  96. }
  97. return handle;
  98. }
  99. static void
  100. symload_report_error( const char *libpath, char *symbol, char *plugin, int libopen )
  101. {
  102. char *errtext = NULL;
  103. PRInt32 errlen;
  104. errlen = PR_GetErrorTextLength();
  105. if ( errlen > 0 ) {
  106. errtext = slapi_ch_malloc( errlen+1 );
  107. if ( PR_GetErrorText( errtext ) > 0 ) {
  108. LDAPDebug( LDAP_DEBUG_ANY, SLAPI_COMPONENT_NAME_NSPR " error %d: %s\n",
  109. PR_GetError(), errtext, 0 );
  110. }
  111. slapi_ch_free( (void **)&errtext );
  112. }
  113. if ( libopen ) {
  114. LDAPDebug( LDAP_DEBUG_ANY,
  115. "Could not load symbol \"%s\" from \"%s\" for plugin %s\n",
  116. symbol, libpath, plugin );
  117. } else {
  118. LDAPDebug( LDAP_DEBUG_ANY,
  119. "Could not open library \"%s\" for plugin %s\n",
  120. libpath, plugin, 0 );
  121. }
  122. }