pcsc-lite  1.8.14
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  * $Id$
33  */
34 
40 #include "config.h"
41 #include <stdio.h>
42 #include <string.h>
43 #if defined(HAVE_DLFCN_H) && !defined(HAVE_DL_H) && !defined(__APPLE__)
44 #include <dlfcn.h>
45 #include <stdlib.h>
46 
47 #include "misc.h"
48 #include "pcsclite.h"
49 #include "debuglog.h"
50 #include "dyn_generic.h"
51 
52 INTERNAL int DYN_LoadLibrary(void **pvLHandle, char *pcLibrary)
53 {
54  *pvLHandle = NULL;
55 #ifndef PCSCLITE_STATIC_DRIVER
56  *pvLHandle = dlopen(pcLibrary, RTLD_LAZY);
57 
58  if (*pvLHandle == NULL)
59  {
60  Log3(PCSC_LOG_CRITICAL, "%s: %s", pcLibrary, dlerror());
61  return SCARD_F_UNKNOWN_ERROR;
62  }
63 #endif
64 
65  return SCARD_S_SUCCESS;
66 }
67 
68 INTERNAL int DYN_CloseLibrary(void **pvLHandle)
69 {
70 #ifndef PCSCLITE_STATIC_DRIVER
71  int ret;
72 
73  ret = dlclose(*pvLHandle);
74  *pvLHandle = NULL;
75 
76  if (ret)
77  {
78  Log2(PCSC_LOG_CRITICAL, "%s", dlerror());
79  return SCARD_F_UNKNOWN_ERROR;
80  }
81 #endif
82 
83  return SCARD_S_SUCCESS;
84 }
85 
86 INTERNAL int DYN_GetAddress(void *pvLHandle, void **pvFHandle,
87  const char *pcFunction, int mayfail)
88 {
89  char pcFunctionName[256];
90  int rv = SCARD_S_SUCCESS;
91 
92  /* Some platforms might need a leading underscore for the symbol */
93  (void)snprintf(pcFunctionName, sizeof(pcFunctionName), "_%s", pcFunction);
94 
95  *pvFHandle = NULL;
96 #ifndef PCSCLITE_STATIC_DRIVER
97  *pvFHandle = dlsym(pvLHandle, pcFunctionName);
98 
99  /* Failed? Try again without the leading underscore */
100  if (*pvFHandle == NULL)
101  *pvFHandle = dlsym(pvLHandle, pcFunction);
102 
103  if (*pvFHandle == NULL)
104  {
105  Log3(mayfail ? PCSC_LOG_INFO : PCSC_LOG_CRITICAL, "%s: %s",
106  pcFunction, dlerror());
108  }
109 #endif
110 
111  return rv;
112 }
113 
114 #endif /* HAVE_DLFCN_H && !HAVE_DL_H && !__APPLE__ */
This abstracts dynamic library loading functions.
#define SCARD_F_UNKNOWN_ERROR
An internal error has been detected, but the source is unknown.
Definition: pcsclite.h:123
This keeps a list of defines for pcsc-lite.
#define SCARD_S_SUCCESS
error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx
Definition: pcsclite.h:103
This handles debugging.