OLD | NEW |
(Empty) | |
| 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 |
| 40 /* |
| 41 * This file is meant to be included by other .c files. |
| 42 * This file takes a "parameter", the scope which includes this |
| 43 * code shall declare this variable: |
| 44 * const char *NameOfThisSharedLib; |
| 45 * |
| 46 * NameOfThisSharedLib: |
| 47 * The file name of the shared library that shall be used as the |
| 48 * "reference library". The loader will attempt to load the requested |
| 49 * library from the same directory as the reference library. |
| 50 */ |
| 51 |
| 52 #ifdef XP_UNIX |
| 53 #include <unistd.h> |
| 54 #define BL_MAXSYMLINKS 20 |
| 55 |
| 56 /* |
| 57 * If 'link' is a symbolic link, this function follows the symbolic links |
| 58 * and returns the pathname of the ultimate source of the symbolic links. |
| 59 * If 'link' is not a symbolic link, this function returns NULL. |
| 60 * The caller should call PR_Free to free the string returned by this |
| 61 * function. |
| 62 */ |
| 63 static char* loader_GetOriginalPathname(const char* link) |
| 64 { |
| 65 char* resolved = NULL; |
| 66 char* input = NULL; |
| 67 PRUint32 iterations = 0; |
| 68 PRInt32 len = 0, retlen = 0; |
| 69 if (!link) { |
| 70 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
| 71 return NULL; |
| 72 } |
| 73 len = PR_MAX(1024, strlen(link) + 1); |
| 74 resolved = PR_Malloc(len); |
| 75 input = PR_Malloc(len); |
| 76 if (!resolved || !input) { |
| 77 if (resolved) { |
| 78 PR_Free(resolved); |
| 79 } |
| 80 if (input) { |
| 81 PR_Free(input); |
| 82 } |
| 83 return NULL; |
| 84 } |
| 85 strcpy(input, link); |
| 86 while ( (iterations++ < BL_MAXSYMLINKS) && |
| 87 ( (retlen = readlink(input, resolved, len - 1)) > 0) ) { |
| 88 char* tmp = input; |
| 89 resolved[retlen] = '\0'; /* NULL termination */ |
| 90 input = resolved; |
| 91 resolved = tmp; |
| 92 } |
| 93 PR_Free(resolved); |
| 94 if (iterations == 1 && retlen < 0) { |
| 95 PR_Free(input); |
| 96 input = NULL; |
| 97 } |
| 98 return input; |
| 99 } |
| 100 #endif /* XP_UNIX */ |
| 101 |
| 102 /* |
| 103 * Load the library with the file name 'name' residing in the same |
| 104 * directory as the reference library, whose pathname is 'referencePath'. |
| 105 */ |
| 106 static PRLibrary * |
| 107 loader_LoadLibInReferenceDir(const char *referencePath, const char *name) |
| 108 { |
| 109 PRLibrary *dlh = NULL; |
| 110 char *fullName = NULL; |
| 111 char* c; |
| 112 PRLibSpec libSpec; |
| 113 |
| 114 /* Remove the trailing filename from referencePath and add the new one */ |
| 115 c = strrchr(referencePath, PR_GetDirectorySeparator()); |
| 116 if (c) { |
| 117 size_t referencePathSize = 1 + c - referencePath; |
| 118 fullName = (char*) PORT_Alloc(strlen(name) + referencePathSize + 1); |
| 119 if (fullName) { |
| 120 memcpy(fullName, referencePath, referencePathSize); |
| 121 strcpy(fullName + referencePathSize, name); |
| 122 #ifdef DEBUG_LOADER |
| 123 PR_fprintf(PR_STDOUT, "\nAttempting to load fully-qualified %s\n", |
| 124 fullName); |
| 125 #endif |
| 126 libSpec.type = PR_LibSpec_Pathname; |
| 127 libSpec.value.pathname = fullName; |
| 128 dlh = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL); |
| 129 PORT_Free(fullName); |
| 130 } |
| 131 } |
| 132 return dlh; |
| 133 } |
| 134 |
| 135 /* |
| 136 * We use PR_GetLibraryFilePathname to get the pathname of the loaded |
| 137 * shared lib that contains this function, and then do a PR_LoadLibrary |
| 138 * with an absolute pathname for the softoken shared library. |
| 139 */ |
| 140 |
| 141 static PRLibrary * |
| 142 loader_LoadLibrary(const char *nameToLoad) |
| 143 { |
| 144 PRLibrary *lib = NULL; |
| 145 char* fullPath = NULL; |
| 146 PRLibSpec libSpec; |
| 147 |
| 148 /* Get the pathname for nameOfAlreadyLoadedLib, i.e. /usr/lib/libnss3.so |
| 149 * PR_GetLibraryFilePathname works with either the base library name or a |
| 150 * function pointer, depending on the platform. We can't query an exported |
| 151 * symbol such as NSC_GetFunctionList, because on some platforms we can't |
| 152 * find symbols in loaded implicit dependencies. |
| 153 * But we can just get the address of this function ! |
| 154 */ |
| 155 fullPath = PR_GetLibraryFilePathname(NameOfThisSharedLib, |
| 156 (PRFuncPtr)&loader_LoadLibrary); |
| 157 |
| 158 if (fullPath) { |
| 159 lib = loader_LoadLibInReferenceDir(fullPath, nameToLoad); |
| 160 #ifdef XP_UNIX |
| 161 if (!lib) { |
| 162 /* |
| 163 * If fullPath is a symbolic link, resolve the symbolic |
| 164 * link and try again. |
| 165 */ |
| 166 char* originalfullPath = loader_GetOriginalPathname(fullPath); |
| 167 if (originalfullPath) { |
| 168 PR_Free(fullPath); |
| 169 fullPath = originalfullPath; |
| 170 lib = loader_LoadLibInReferenceDir(fullPath, nameToLoad); |
| 171 } |
| 172 } |
| 173 #endif |
| 174 PR_Free(fullPath); |
| 175 } |
| 176 if (!lib) { |
| 177 #ifdef DEBUG_LOADER |
| 178 PR_fprintf(PR_STDOUT, "\nAttempting to load %s\n", nameToLoad); |
| 179 #endif |
| 180 libSpec.type = PR_LibSpec_Pathname; |
| 181 libSpec.value.pathname = nameToLoad; |
| 182 lib = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL); |
| 183 } |
| 184 if (NULL == lib) { |
| 185 #ifdef DEBUG_LOADER |
| 186 PR_fprintf(PR_STDOUT, "\nLoading failed : %s.\n", nameToLoad); |
| 187 #endif |
| 188 } |
| 189 return lib; |
| 190 } |
| 191 |
OLD | NEW |