diff --git a/src/libredirect.c b/src/libredirect.c index ac023f46..d6d19f06 100644 --- a/src/libredirect.c +++ b/src/libredirect.c @@ -125,7 +125,7 @@ static void log_line(const char *fmt, ...) } #endif -static LONG load_lib(void) +static void load_lib(void) { #define LIBPCSC "libpcsclite_real.so.1" @@ -140,15 +140,15 @@ static LONG load_lib(void) if (NULL == Lib_handle) { log_line("loading \"%s\" failed: %s", lib, dlerror()); - return SCARD_F_INTERNAL_ERROR; + return; } -#define get_symbol(s) do { redirect.s = dlsym(Lib_handle, #s); if (NULL == redirect.s) { log_line("%s", dlerror()); return SCARD_F_INTERNAL_ERROR; } } while (0) +#define get_symbol(s) do { redirect.s = dlsym(Lib_handle, #s); if (NULL == redirect.s) { log_line("%s", dlerror()); return; } } while (0) if (SCardEstablishContext == dlsym(Lib_handle, "SCardEstablishContext")) { log_line("Symbols dlsym error"); - return SCARD_F_INTERNAL_ERROR; + return; } get_symbol(SCardEstablishContext); @@ -169,29 +169,17 @@ static LONG load_lib(void) get_symbol(SCardCancel); get_symbol(SCardGetAttrib); get_symbol(SCardSetAttrib); - - return SCARD_S_SUCCESS; } - /* exported functions */ PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext) { - LONG rv; - static int init = 0; + static pthread_once_t once_control = PTHREAD_ONCE_INIT; - if (!init) - { - init = 1; - - /* load the real library */ - rv = load_lib(); - if (rv != SCARD_S_SUCCESS) - return rv; - } + pthread_once(&once_control, load_lib); return redirect.SCardEstablishContext(dwScope, pvReserved1, pvReserved2, phContext);