pcsc-lite 1.7.2

dyn_unix.c

Go to the documentation of this file.
00001 /*
00002  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
00003  *
00004  * Copyright (C) 1999-2002
00005  *  David Corcoran <corcoran@linuxnet.com>
00006  * Copyright (C) 2002-2010
00007  *  Ludovic Rousseau <ludovic.rousseau@free.fr>
00008  *
00009  * $Id: dyn_unix.c 5047 2010-06-29 14:39:24Z rousseau $
00010  */
00011 
00017 #include "config.h"
00018 #include <stdio.h>
00019 #include <string.h>
00020 #if defined(HAVE_DLFCN_H) && !defined(HAVE_DL_H) && !defined(__APPLE__)
00021 #include <dlfcn.h>
00022 #include <stdlib.h>
00023 
00024 #include "misc.h"
00025 #include "pcsclite.h"
00026 #include "debuglog.h"
00027 #include "dyn_generic.h"
00028 
00029 INTERNAL int DYN_LoadLibrary(void **pvLHandle, char *pcLibrary)
00030 {
00031     *pvLHandle = NULL;
00032 #ifndef PCSCLITE_STATIC_DRIVER
00033     *pvLHandle = dlopen(pcLibrary, RTLD_LAZY);
00034 
00035     if (*pvLHandle == NULL)
00036     {
00037         Log3(PCSC_LOG_CRITICAL, "%s: %s", pcLibrary, dlerror());
00038         return SCARD_F_UNKNOWN_ERROR;
00039     }
00040 #endif
00041 
00042     return SCARD_S_SUCCESS;
00043 }
00044 
00045 INTERNAL int DYN_CloseLibrary(void **pvLHandle)
00046 {
00047 #ifndef PCSCLITE_STATIC_DRIVER
00048     int ret;
00049 
00050     ret = dlclose(*pvLHandle);
00051     *pvLHandle = NULL;
00052 
00053     if (ret)
00054     {
00055         Log2(PCSC_LOG_CRITICAL, "%s", dlerror());
00056         return SCARD_F_UNKNOWN_ERROR;
00057     }
00058 #endif
00059 
00060     return SCARD_S_SUCCESS;
00061 }
00062 
00063 INTERNAL int DYN_GetAddress(void *pvLHandle, void **pvFHandle, const char *pcFunction)
00064 {
00065     char pcFunctionName[256];
00066     int rv = SCARD_S_SUCCESS;
00067 
00068     /* Some platforms might need a leading underscore for the symbol */
00069     (void)snprintf(pcFunctionName, sizeof(pcFunctionName), "_%s", pcFunction);
00070 
00071     *pvFHandle = NULL;
00072 #ifndef PCSCLITE_STATIC_DRIVER
00073     *pvFHandle = dlsym(pvLHandle, pcFunctionName);
00074 
00075     /* Failed? Try again without the leading underscore */
00076     if (*pvFHandle == NULL)
00077         *pvFHandle = dlsym(pvLHandle, pcFunction);
00078 
00079     if (*pvFHandle == NULL)
00080     {
00081         Log3(PCSC_LOG_CRITICAL, "%s: %s", pcFunction, dlerror());
00082         rv = SCARD_F_UNKNOWN_ERROR;
00083     }
00084 #endif
00085 
00086     return rv;
00087 }
00088 
00089 #endif  /* HAVE_DLFCN_H && !HAVE_DL_H && !__APPLE__ */