Blame nss/lib/freebl/genload.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * This file is meant to be included by other .c files.
Packit 40b132
 * This file takes a "parameter", the scope which includes this
Packit 40b132
 * code shall declare this variable:
Packit 40b132
 *   const char *NameOfThisSharedLib;
Packit 40b132
 *
Packit 40b132
 * NameOfThisSharedLib:
Packit 40b132
 *   The file name of the shared library that shall be used as the 
Packit 40b132
 *   "reference library". The loader will attempt to load the requested
Packit 40b132
 *   library from the same directory as the reference library.
Packit 40b132
 */
Packit 40b132
Packit 40b132
#ifdef XP_UNIX
Packit 40b132
#include <unistd.h>
Packit 40b132
#define BL_MAXSYMLINKS 20
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * If 'link' is a symbolic link, this function follows the symbolic links
Packit 40b132
 * and returns the pathname of the ultimate source of the symbolic links.
Packit 40b132
 * If 'link' is not a symbolic link, this function returns NULL.
Packit 40b132
 * The caller should call PR_Free to free the string returned by this
Packit 40b132
 * function.
Packit 40b132
 */
Packit 40b132
static char* loader_GetOriginalPathname(const char* link)
Packit 40b132
{
Packit 40b132
#ifdef __GLIBC__
Packit 40b132
    char* tmp = realpath(link, NULL);
Packit 40b132
    char* resolved;
Packit 40b132
    if (! tmp)
Packit 40b132
    	return NULL;
Packit 40b132
    resolved = PR_Malloc(strlen(tmp) + 1);
Packit 40b132
    strcpy(resolved, tmp); /* This is necessary because PR_Free might not be using free() */
Packit 40b132
    free(tmp);
Packit 40b132
    return resolved;
Packit 40b132
#else
Packit 40b132
    char* resolved = NULL;
Packit 40b132
    char* input = NULL;
Packit 40b132
    PRUint32 iterations = 0;
Packit 40b132
    PRInt32 len = 0, retlen = 0;
Packit 40b132
    if (!link) {
Packit 40b132
        PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
Packit 40b132
        return NULL;
Packit 40b132
    }
Packit 40b132
    len = PR_MAX(1024, strlen(link) + 1);
Packit 40b132
    resolved = PR_Malloc(len);
Packit 40b132
    input = PR_Malloc(len);
Packit 40b132
    if (!resolved || !input) {
Packit 40b132
        if (resolved) {
Packit 40b132
            PR_Free(resolved);
Packit 40b132
        }
Packit 40b132
        if (input) {
Packit 40b132
            PR_Free(input);
Packit 40b132
        }
Packit 40b132
        return NULL;
Packit 40b132
    }
Packit 40b132
    strcpy(input, link);
Packit 40b132
    while ( (iterations++ < BL_MAXSYMLINKS) &&
Packit 40b132
            ( (retlen = readlink(input, resolved, len - 1)) > 0) ) {
Packit 40b132
        char* tmp = input;
Packit 40b132
        resolved[retlen] = '\0'; /* NULL termination */
Packit 40b132
        input = resolved;
Packit 40b132
        resolved = tmp;
Packit 40b132
    }
Packit 40b132
    PR_Free(resolved);
Packit 40b132
    if (iterations == 1 && retlen < 0) {
Packit 40b132
        PR_Free(input);
Packit 40b132
        input = NULL;
Packit 40b132
    }
Packit 40b132
    return input;
Packit 40b132
#endif
Packit 40b132
}
Packit 40b132
#endif /* XP_UNIX */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Load the library with the file name 'name' residing in the same
Packit 40b132
 * directory as the reference library, whose pathname is 'referencePath'.
Packit 40b132
 */
Packit 40b132
static PRLibrary *
Packit 40b132
loader_LoadLibInReferenceDir(const char *referencePath, const char *name)
Packit 40b132
{
Packit 40b132
    PRLibrary *dlh = NULL;
Packit 40b132
    char *fullName = NULL;
Packit 40b132
    char* c;
Packit 40b132
    PRLibSpec libSpec;
Packit 40b132
Packit 40b132
    /* Remove the trailing filename from referencePath and add the new one */
Packit 40b132
    c = strrchr(referencePath, PR_GetDirectorySeparator());
Packit 40b132
    if (c) {
Packit 40b132
        size_t referencePathSize = 1 + c - referencePath;
Packit 40b132
        fullName = (char*) PORT_Alloc(strlen(name) + referencePathSize + 1);
Packit 40b132
        if (fullName) {
Packit 40b132
            memcpy(fullName, referencePath, referencePathSize);
Packit 40b132
            strcpy(fullName + referencePathSize, name); 
Packit 40b132
#ifdef DEBUG_LOADER
Packit 40b132
            PR_fprintf(PR_STDOUT, "\nAttempting to load fully-qualified %s\n", 
Packit 40b132
                       fullName);
Packit 40b132
#endif
Packit 40b132
            libSpec.type = PR_LibSpec_Pathname;
Packit 40b132
            libSpec.value.pathname = fullName;
Packit 40b132
            dlh = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL);
Packit 40b132
            PORT_Free(fullName);
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
    return dlh;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * We use PR_GetLibraryFilePathname to get the pathname of the loaded 
Packit 40b132
 * shared lib that contains this function, and then do a PR_LoadLibrary
Packit 40b132
 * with an absolute pathname for the softoken shared library.
Packit 40b132
 */
Packit 40b132
Packit 40b132
static PRLibrary *
Packit 40b132
loader_LoadLibrary(const char *nameToLoad)
Packit 40b132
{
Packit 40b132
    PRLibrary *lib = NULL;
Packit 40b132
    char* fullPath = NULL;
Packit 40b132
    PRLibSpec libSpec;
Packit 40b132
Packit 40b132
    /* Get the pathname for nameOfAlreadyLoadedLib, i.e. /usr/lib/libnss3.so
Packit 40b132
     * PR_GetLibraryFilePathname works with either the base library name or a
Packit 40b132
     * function pointer, depending on the platform. We can't query an exported
Packit 40b132
     * symbol such as NSC_GetFunctionList, because on some platforms we can't
Packit 40b132
     * find symbols in loaded implicit dependencies.
Packit 40b132
     * But we can just get the address of this function !
Packit 40b132
     */
Packit 40b132
    fullPath = PR_GetLibraryFilePathname(NameOfThisSharedLib,
Packit 40b132
                                         (PRFuncPtr)&loader_LoadLibrary);
Packit 40b132
Packit 40b132
    if (fullPath) {
Packit 40b132
        lib = loader_LoadLibInReferenceDir(fullPath, nameToLoad);
Packit 40b132
#ifdef XP_UNIX
Packit 40b132
        if (!lib) {
Packit 40b132
            /*
Packit 40b132
             * If fullPath is a symbolic link, resolve the symbolic
Packit 40b132
             * link and try again.
Packit 40b132
             */
Packit 40b132
            char* originalfullPath = loader_GetOriginalPathname(fullPath);
Packit 40b132
            if (originalfullPath) {
Packit 40b132
                PR_Free(fullPath);
Packit 40b132
                fullPath = originalfullPath;
Packit 40b132
                lib = loader_LoadLibInReferenceDir(fullPath, nameToLoad);
Packit 40b132
            }
Packit 40b132
        }
Packit 40b132
#endif
Packit 40b132
        PR_Free(fullPath);
Packit 40b132
    }
Packit 40b132
    if (!lib) {
Packit 40b132
#ifdef DEBUG_LOADER
Packit 40b132
        PR_fprintf(PR_STDOUT, "\nAttempting to load %s\n", nameToLoad);
Packit 40b132
#endif
Packit 40b132
        libSpec.type = PR_LibSpec_Pathname;
Packit 40b132
        libSpec.value.pathname = nameToLoad;
Packit 40b132
        lib = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL);
Packit 40b132
    }
Packit 40b132
    if (NULL == lib) {
Packit 40b132
#ifdef DEBUG_LOADER
Packit 40b132
        PR_fprintf(PR_STDOUT, "\nLoading failed : %s.\n", nameToLoad);
Packit 40b132
#endif
Packit 40b132
    }
Packit 40b132
    return lib;
Packit 40b132
}
Packit 40b132