/security/nss/lib/util/secload.c

http://github.com/zpao/v8monkey · C · 217 lines · 112 code · 10 blank · 95 comment · 17 complexity · 26aea1d8141283e947a42e24ed65eb33 MD5 · raw file

  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the Netscape security libraries.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 2000
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Dr Vipul Gupta <vipul.gupta@sun.com>, Sun Microsystems Laboratories
  24. * Kai Engert <kengert@redhat.com>
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either the GNU General Public License Version 2 or later (the "GPL"), or
  28. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  39. #include "secport.h"
  40. #include "nspr.h"
  41. #ifdef XP_UNIX
  42. #include <unistd.h>
  43. #define BL_MAXSYMLINKS 20
  44. /*
  45. * If 'link' is a symbolic link, this function follows the symbolic links
  46. * and returns the pathname of the ultimate source of the symbolic links.
  47. * If 'link' is not a symbolic link, this function returns NULL.
  48. * The caller should call PR_Free to free the string returned by this
  49. * function.
  50. */
  51. static char* loader_GetOriginalPathname(const char* link)
  52. {
  53. char* resolved = NULL;
  54. char* input = NULL;
  55. PRUint32 iterations = 0;
  56. PRInt32 len = 0, retlen = 0;
  57. if (!link) {
  58. PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
  59. return NULL;
  60. }
  61. len = PR_MAX(1024, strlen(link) + 1);
  62. resolved = PR_Malloc(len);
  63. input = PR_Malloc(len);
  64. if (!resolved || !input) {
  65. if (resolved) {
  66. PR_Free(resolved);
  67. }
  68. if (input) {
  69. PR_Free(input);
  70. }
  71. return NULL;
  72. }
  73. strcpy(input, link);
  74. while ( (iterations++ < BL_MAXSYMLINKS) &&
  75. ( (retlen = readlink(input, resolved, len - 1)) > 0) ) {
  76. char* tmp = input;
  77. resolved[retlen] = '\0'; /* NULL termination */
  78. input = resolved;
  79. resolved = tmp;
  80. }
  81. PR_Free(resolved);
  82. if (iterations == 1 && retlen < 0) {
  83. PR_Free(input);
  84. input = NULL;
  85. }
  86. return input;
  87. }
  88. #endif /* XP_UNIX */
  89. /*
  90. * Load the library with the file name 'name' residing in the same
  91. * directory as the reference library, whose pathname is 'referencePath'.
  92. */
  93. static PRLibrary *
  94. loader_LoadLibInReferenceDir(const char *referencePath, const char *name)
  95. {
  96. PRLibrary *dlh = NULL;
  97. char *fullName = NULL;
  98. char* c;
  99. PRLibSpec libSpec;
  100. /* Remove the trailing filename from referencePath and add the new one */
  101. c = strrchr(referencePath, PR_GetDirectorySeparator());
  102. if (c) {
  103. size_t referencePathSize = 1 + c - referencePath;
  104. fullName = (char*) PORT_Alloc(strlen(name) + referencePathSize + 1);
  105. if (fullName) {
  106. memcpy(fullName, referencePath, referencePathSize);
  107. strcpy(fullName + referencePathSize, name);
  108. #ifdef DEBUG_LOADER
  109. PR_fprintf(PR_STDOUT, "\nAttempting to load fully-qualified %s\n",
  110. fullName);
  111. #endif
  112. libSpec.type = PR_LibSpec_Pathname;
  113. libSpec.value.pathname = fullName;
  114. dlh = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL
  115. #ifdef PR_LD_ALT_SEARCH_PATH
  116. /* allow library's dependencies to be found in the same directory
  117. * on Windows even if PATH is not set. Requires NSPR 4.8.1 . */
  118. | PR_LD_ALT_SEARCH_PATH
  119. #endif
  120. );
  121. PORT_Free(fullName);
  122. }
  123. }
  124. return dlh;
  125. }
  126. /*
  127. * Load a shared library called "newShLibName" in the same directory as
  128. * a shared library that is already loaded, called existingShLibName.
  129. * A pointer to a static function in that shared library,
  130. * staticShLibFunc, is required.
  131. *
  132. * existingShLibName:
  133. * The file name of the shared library that shall be used as the
  134. * "reference library". The loader will attempt to load the requested
  135. * library from the same directory as the reference library.
  136. *
  137. * staticShLibFunc:
  138. * Pointer to a static function in the "reference library".
  139. *
  140. * newShLibName:
  141. * The simple file name of the new shared library to be loaded.
  142. *
  143. * We use PR_GetLibraryFilePathname to get the pathname of the loaded
  144. * shared lib that contains this function, and then do a
  145. * PR_LoadLibraryWithFlags with an absolute pathname for the shared
  146. * library to be loaded.
  147. *
  148. * On Windows, the "alternate search path" strategy is employed, if available.
  149. * On Unix, if existingShLibName is a symbolic link, and no link exists for the
  150. * new library, the original link will be resolved, and the new library loaded
  151. * from the resolved location.
  152. *
  153. * If the new shared library is not found in the same location as the reference
  154. * library, it will then be loaded from the normal system library path.
  155. *
  156. */
  157. PRLibrary *
  158. PORT_LoadLibraryFromOrigin(const char* existingShLibName,
  159. PRFuncPtr staticShLibFunc,
  160. const char *newShLibName)
  161. {
  162. PRLibrary *lib = NULL;
  163. char* fullPath = NULL;
  164. PRLibSpec libSpec;
  165. /* Get the pathname for existingShLibName, e.g. /usr/lib/libnss3.so
  166. * PR_GetLibraryFilePathname works with either the base library name or a
  167. * function pointer, depending on the platform.
  168. * We require the address of a function in the "reference library",
  169. * provided by the caller. To avoid getting the address of the stub/thunk
  170. * of an exported function by accident, use the address of a static
  171. * function rather than an exported function.
  172. */
  173. fullPath = PR_GetLibraryFilePathname(existingShLibName,
  174. staticShLibFunc);
  175. if (fullPath) {
  176. lib = loader_LoadLibInReferenceDir(fullPath, newShLibName);
  177. #ifdef XP_UNIX
  178. if (!lib) {
  179. /*
  180. * If fullPath is a symbolic link, resolve the symbolic
  181. * link and try again.
  182. */
  183. char* originalfullPath = loader_GetOriginalPathname(fullPath);
  184. if (originalfullPath) {
  185. PR_Free(fullPath);
  186. fullPath = originalfullPath;
  187. lib = loader_LoadLibInReferenceDir(fullPath, newShLibName);
  188. }
  189. }
  190. #endif
  191. PR_Free(fullPath);
  192. }
  193. if (!lib) {
  194. #ifdef DEBUG_LOADER
  195. PR_fprintf(PR_STDOUT, "\nAttempting to load %s\n", newShLibName);
  196. #endif
  197. libSpec.type = PR_LibSpec_Pathname;
  198. libSpec.value.pathname = newShLibName;
  199. lib = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL);
  200. }
  201. if (NULL == lib) {
  202. #ifdef DEBUG_LOADER
  203. PR_fprintf(PR_STDOUT, "\nLoading failed : %s.\n", newShLibName);
  204. #endif
  205. }
  206. return lib;
  207. }