J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include <dlfcn.h> |
| 31 | #include <link.h> |
| 32 | |
| 33 | #include <jni_util.h> |
| 34 | |
| 35 | #include "j2secmod.h" |
| 36 | |
| 37 | void *findFunction(JNIEnv *env, jlong jHandle, const char *functionName) { |
| 38 | void *hModule = (void*)jHandle; |
| 39 | void *fAddress = dlsym(hModule, functionName); |
| 40 | if (fAddress == NULL) { |
| 41 | char errorMessage[256]; |
| 42 | snprintf(errorMessage, sizeof(errorMessage), "Symbol not found: %s", functionName); |
| 43 | JNU_ThrowNullPointerException(env, errorMessage); |
| 44 | return NULL; |
| 45 | } |
| 46 | return fAddress; |
| 47 | } |
| 48 | |
| 49 | JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_Secmod_nssGetLibraryHandle |
| 50 | (JNIEnv *env, jclass thisClass, jstring jLibName) |
| 51 | { |
| 52 | const char *libName = (*env)->GetStringUTFChars(env, jLibName, NULL); |
| 53 | // look up existing handle only, do not load |
| 54 | void *hModule = dlopen(libName, RTLD_NOLOAD); |
| 55 | dprintf2("-handle for %s: %u\n", libName, hModule); |
| 56 | (*env)->ReleaseStringUTFChars(env, jLibName, libName); |
| 57 | return (jlong)hModule; |
| 58 | } |
| 59 | |
| 60 | JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_Secmod_nssLoadLibrary |
| 61 | (JNIEnv *env, jclass thisClass, jstring jLibName) |
| 62 | { |
| 63 | void *hModule; |
| 64 | const char *libName = (*env)->GetStringUTFChars(env, jLibName, NULL); |
| 65 | |
| 66 | dprintf1("-lib %s\n", libName); |
| 67 | hModule = dlopen(libName, RTLD_LAZY); |
| 68 | (*env)->ReleaseStringUTFChars(env, jLibName, libName); |
| 69 | dprintf2("-handle: %u (0X%X)\n", hModule, hModule); |
| 70 | |
| 71 | if (hModule == NULL) { |
| 72 | JNU_ThrowIOException(env, dlerror()); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | return (jlong)hModule; |
| 77 | } |