pcsc-lite  1.8.20
dyn_unix.c
Go to the documentation of this file.
1 /*
2  * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html )
3  *
4  * Copyright (C) 1999-2002
5  * David Corcoran <corcoran@musclecard.com>
6  * Copyright (C) 2002-2010
7  * Ludovic Rousseau <ludovic.rousseau@free.fr>
8  *
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12 
13 1. Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright
16  notice, this list of conditions and the following disclaimer in the
17  documentation and/or other materials provided with the distribution.
18 3. The name of the author may not be used to endorse or promote products
19  derived from this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
38 #include "config.h"
39 #include <stdio.h>
40 #include <string.h>
41 #if defined(HAVE_DLFCN_H) && !defined(HAVE_DL_H) && !defined(__APPLE__)
42 #include <dlfcn.h>
43 #include <stdlib.h>
44 
45 #include "misc.h"
46 #include "pcsclite.h"
47 #include "debuglog.h"
48 #include "dyn_generic.h"
49 
50 INTERNAL LONG DYN_LoadLibrary(void **pvLHandle, char *pcLibrary)
51 {
52  *pvLHandle = NULL;
53 #ifndef PCSCLITE_STATIC_DRIVER
54  *pvLHandle = dlopen(pcLibrary, RTLD_LAZY);
55 
56  if (*pvLHandle == NULL)
57  {
58  Log3(PCSC_LOG_CRITICAL, "%s: %s", pcLibrary, dlerror());
59  return SCARD_F_UNKNOWN_ERROR;
60  }
61 #endif
62 
63  return SCARD_S_SUCCESS;
64 }
65 
66 INTERNAL LONG DYN_CloseLibrary(void **pvLHandle)
67 {
68 #ifndef PCSCLITE_STATIC_DRIVER
69  int ret;
70 
71  ret = dlclose(*pvLHandle);
72  *pvLHandle = NULL;
73 
74  if (ret)
75  {
76  Log2(PCSC_LOG_CRITICAL, "%s", dlerror());
77  return SCARD_F_UNKNOWN_ERROR;
78  }
79 #endif
80 
81  return SCARD_S_SUCCESS;
82 }
83 
84 INTERNAL LONG DYN_GetAddress(void *pvLHandle, void **pvFHandle,
85  const char *pcFunction, int mayfail)
86 {
87  char pcFunctionName[256];
88  LONG rv = SCARD_S_SUCCESS;
89 
90  /* Some platforms might need a leading underscore for the symbol */
91  (void)snprintf(pcFunctionName, sizeof(pcFunctionName), "_%s", pcFunction);
92 
93  *pvFHandle = NULL;
94 #ifndef PCSCLITE_STATIC_DRIVER
95  *pvFHandle = dlsym(pvLHandle, pcFunctionName);
96 
97  /* Failed? Try again without the leading underscore */
98  if (*pvFHandle == NULL)
99  *pvFHandle = dlsym(pvLHandle, pcFunction);
100 
101  if (*pvFHandle == NULL)
102  {
103 #ifdef NO_LOG
104  (void)mayfail;
105 #endif
106  Log3(mayfail ? PCSC_LOG_INFO : PCSC_LOG_CRITICAL, "%s: %s",
107  pcFunction, dlerror());
109  }
110 #endif
111 
112  return rv;
113 }
114 
115 #endif /* HAVE_DLFCN_H && !HAVE_DL_H && !__APPLE__ */
This abstracts dynamic library loading functions.
#define SCARD_S_SUCCESS
No error was encountered.
Definition: pcsclite.h:107
#define SCARD_F_UNKNOWN_ERROR
An internal error has been detected, but the source is unknown.
Definition: pcsclite.h:147
This keeps a list of defines for pcsc-lite.
This handles debugging.