From a3bcb9037ddf6657f79f0aae42aa83dd2b8f6b14 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Mon, 30 Apr 2012 22:49:41 +0200 Subject: Move the compat.[ch] headers into common directory/ * And the compat stuff in the p11-kit directory merged into util.c and util.h --- common/compat.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++++++++ common/compat.h | 63 +++++++++++++++ p11-kit/Makefile.am | 3 +- p11-kit/compat.c | 114 -------------------------- p11-kit/compat.h | 149 ---------------------------------- p11-kit/conf.c | 2 +- p11-kit/debug.c | 1 - p11-kit/private.h | 2 +- p11-kit/util.c | 66 +++++++++++++++ p11-kit/util.h | 111 +++++++++++++++++++++++++ tests/mock-module.c | 2 +- tests/test-init.c | 2 +- tools/Makefile.am | 3 +- tools/compat.c | 228 ---------------------------------------------------- tools/compat.h | 63 --------------- tools/p11-kit.c | 4 +- 16 files changed, 477 insertions(+), 564 deletions(-) create mode 100644 common/compat.c create mode 100644 common/compat.h delete mode 100644 p11-kit/compat.c delete mode 100644 p11-kit/compat.h delete mode 100644 tools/compat.c delete mode 100644 tools/compat.h diff --git a/common/compat.c b/common/compat.c new file mode 100644 index 0000000..93ba77c --- /dev/null +++ b/common/compat.c @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2011 Collabora Ltd. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#include "config.h" + +#include "compat.h" + +#ifndef HAVE_ERR_H + +#include +#include +#include +#include + +static const char * +calc_prog_name (void) +{ + static char prognamebuf[256]; + static int prepared = 0; + + if(!prepared) + { + const char* beg = strrchr(__argv[0], '\\'); + const char* temp = strrchr(__argv[0], '/'); + beg = (beg > temp) ? beg : temp; + beg = (beg) ? beg + 1 : __argv[0]; + + temp = strrchr(__argv[0], '.'); + temp = (temp > beg) ? temp : __argv[0] + strlen(__argv[0]); + + if((temp - beg) > 255) + temp = beg + 255; + + strncpy(prognamebuf, beg, temp - beg); + prognamebuf[temp - beg] = 0; + prepared = 1; + } + + return prognamebuf; +} + +static FILE *err_file; /* file to use for error output */ + +/* + * This is declared to take a `void *' so that the caller is not required + * to include first. However, it is really a `FILE *', and the + * manual page documents it as such. + */ +void +err_set_file (void *fp) +{ + if (fp) + err_file = fp; + else + err_file = stderr; +} + +void +err (int eval, + const char *fmt, + ...) +{ + va_list ap; + va_start(ap, fmt); + verrc(eval, errno, fmt, ap); + va_end(ap); +} + +void +verr (int eval, + const char *fmt, + va_list ap) +{ + verrc(eval, errno, fmt, ap); +} + +void +errc (int eval, + int code, + const char *fmt, + ...) +{ + va_list ap; + va_start(ap, fmt); + verrc(eval, code, fmt, ap); + va_end(ap); +} + +void +verrc (int eval, + int code, + const char *fmt, + va_list ap) +{ + if (err_file == 0) + err_set_file((FILE *)0); + fprintf(err_file, "%s: ", calc_prog_name()); + if (fmt != NULL) { + vfprintf(err_file, fmt, ap); + fprintf(err_file, ": "); + } + fprintf(err_file, "%s\n", strerror(code)); + exit(eval); +} + +void +errx (int eval, + const char *fmt, + ...) +{ + va_list ap; + va_start(ap, fmt); + verrx(eval, fmt, ap); + va_end(ap); +} + +void +verrx (int eval, + const char *fmt, + va_list ap) +{ + if (err_file == 0) + err_set_file((FILE *)0); + fprintf(err_file, "%s: ", calc_prog_name()); + if (fmt != NULL) + vfprintf(err_file, fmt, ap); + fprintf(err_file, "\n"); + exit(eval); +} + +void +warn (const char *fmt, + ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnc(errno, fmt, ap); + va_end(ap); +} + +void +vwarn (const char *fmt, + va_list ap) +{ + vwarnc(errno, fmt, ap); +} + +void +warnc (int code, + const char *fmt, + ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnc(code, fmt, ap); + va_end(ap); +} + +void +vwarnc (int code, + const char *fmt, + va_list ap) +{ + if (err_file == 0) + err_set_file((FILE *)0); + fprintf(err_file, "%s: ", calc_prog_name()); + if (fmt != NULL) + { + vfprintf(err_file, fmt, ap); + fprintf(err_file, ": "); + } + fprintf(err_file, "%s\n", strerror(code)); +} + +void +warnx (const char *fmt, + ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); +} + +void +vwarnx (const char *fmt, + va_list ap) +{ + if(err_file == 0) + err_set_file((FILE*)0); + fprintf(err_file, "%s: ", calc_prog_name()); + if(fmt != NULL) + vfprintf(err_file, fmt, ap); + fprintf(err_file, "\n"); +} + +#endif /* HAVE_ERR_H */ diff --git a/common/compat.h b/common/compat.h new file mode 100644 index 0000000..1562964 --- /dev/null +++ b/common/compat.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2011 Collabora Ltd. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter + */ + +#ifndef __ERR_H__ +#define __ERR_H__ + +#include "config.h" + +#ifdef HAVE_ERR_H +#include + +#else /* !HAVE_ERR_H */ + +#include +void err_set_file (void *fp); +void err_set_exit (void (*ef)(int)); +void err (int eval, const char *fmt, ...); +void verr (int eval, const char *fmt, va_list ap); +void errc (int eval, int code, const char *fmt, ...); +void verrc (int eval, int code, const char *fmt, va_list ap); +void errx (int eval, const char *fmt, ...); +void verrx (int eval, const char *fmt, va_list ap); +void warn (const char *fmt, ...); +void vwarn (const char *fmt, va_list ap); +void warnc (int code, const char *fmt, ...); +void vwarnc (int code, const char *fmt, va_list ap); +void warnx (const char *fmt, ...); +void vwarnx (const char *fmt, va_list ap); + +#endif /* !HAVE_ERR_H */ + +#endif /* __ERR_H__ */ diff --git a/p11-kit/Makefile.am b/p11-kit/Makefile.am index 8605c15..b855fb7 100644 --- a/p11-kit/Makefile.am +++ b/p11-kit/Makefile.am @@ -15,7 +15,6 @@ inc_HEADERS = \ MODULE_SRCS = \ util.c util.h \ - compat.c compat.h \ conf.c conf.h \ debug.c debug.h \ hashmap.c hashmap.h \ @@ -66,7 +65,7 @@ libp11_kit_testable_la_CFLAGS = \ $(NULL) libp11_kit_compat_la_SOURCES = \ - compat.c compat.h + util.c util.h pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = p11-kit-1.pc diff --git a/p11-kit/compat.c b/p11-kit/compat.c deleted file mode 100644 index db0b0ce..0000000 --- a/p11-kit/compat.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2011 Collabora Ltd. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" - -#include "compat.h" -#include "private.h" - -#include - -#ifdef OS_UNIX - -void -_p11_mutex_init (mutex_t *mutex) -{ - pthread_mutexattr_t attr; - int ret; - - pthread_mutexattr_init (&attr); - pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); - ret = pthread_mutex_init (mutex, &attr); - assert (ret == 0); - pthread_mutexattr_destroy (&attr); -} - -#endif /* OS_UNIX */ - -#ifdef OS_WIN32 - -const char * -_p11_module_error (void) -{ - DWORD code = GetLastError(); - p11_local *local; - LPVOID msg_buf; - - local = _p11_library_get_thread_local (); - - FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, code, - MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&msg_buf, 0, NULL); - - if (local->last_error) - LocalFree (local->last_error); - local->last_error = msg_buf; - - return msg_buf; -} - -int -_p11_thread_create (thread_t *thread, - thread_routine routine, - void *arg) -{ - assert (thread); - - *thread = CreateThread (NULL, 0, - (LPTHREAD_START_ROUTINE)routine, - arg, 0, NULL); - - if (*thread == NULL) - return GetLastError (); - - return 0; -} - -int -_p11_thread_join (thread_t thread) -{ - DWORD res; - - res = WaitForSingleObject (thread, INFINITE); - if (res == WAIT_FAILED) - return GetLastError (); - - CloseHandle (thread); - return 0; -} - -#endif /* OS_WIN32 */ diff --git a/p11-kit/compat.h b/p11-kit/compat.h deleted file mode 100644 index 4b839b2..0000000 --- a/p11-kit/compat.h +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (c) 2011 Collabora Ltd. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#ifndef __COMPAT_H__ -#define __COMPAT_H__ - -#include "config.h" - -/* ----------------------------------------------------------------------------- - * WIN32 - */ - -#ifdef OS_WIN32 - -#ifndef _WIN32_WINNT -#define _WIN32_WINNT 0x500 -#endif - -#ifndef _WIN32_IE -#define _WIN32_IE 0x500 -#endif - -#define WIN32_LEAN_AND_MEAN 1 -#include - -/* Oh ... my ... god */ -#undef CreateMutex - -typedef CRITICAL_SECTION mutex_t; - -typedef HANDLE thread_t; - -#define _p11_mutex_init(m) \ - (InitializeCriticalSection (m)) -#define _p11_mutex_lock(m) \ - (EnterCriticalSection (m)) -#define _p11_mutex_unlock(m) \ - (LeaveCriticalSection (m)) -#define _p11_mutex_uninit(m) \ - (DeleteCriticalSection (m)) - -typedef void * (*thread_routine) (void *arg); - -int _p11_thread_create (thread_t *thread, thread_routine, void *arg); - -int _p11_thread_join (thread_t thread); - -#define _p11_thread_self() \ - (GetCurrentThread ()) - -typedef HMODULE dl_module_t; - -#define _p11_module_open(f) \ - (LoadLibrary (f)) -#define _p11_module_close(d) \ - (FreeLibrary (d)) -#define _p11_module_symbol(d, s) \ - ((void *)GetProcAddress ((d), (s))) - -const char * _p11_module_error (void); - -#define _p11_sleep_ms(ms) \ - (Sleep (ms)) - -#endif /* OS_WIN32 */ - -/* ---------------------------------------------------------------------------- - * UNIX - */ - -#ifdef OS_UNIX - -#include -#include -#include - -typedef pthread_mutex_t mutex_t; - -void _p11_mutex_init (mutex_t *mutex); - -#define _p11_mutex_lock(m) \ - (pthread_mutex_lock (m)) -#define _p11_mutex_unlock(m) \ - (pthread_mutex_unlock (m)) -#define _p11_mutex_uninit(m) \ - (pthread_mutex_destroy(m)) - -typedef pthread_t thread_t; - -typedef void * (*thread_routine) (void *arg); - -#define _p11_thread_create(t, r, a) \ - (pthread_create ((t), NULL, (r), (a))) -#define _p11_thread_join(t) \ - (pthread_join ((t), NULL)) -#define _p11_thread_self(m) \ - (pthread_self ()) - -typedef void * dl_module_t; - -#define _p11_module_open(f) \ - (dlopen ((f), RTLD_LOCAL | RTLD_NOW)) -#define _p11_module_close(d) \ - (dlclose(d)) -#define _p11_module_error() \ - (dlerror ()) -#define _p11_module_symbol(d, s) \ - (dlsym ((d), (s))) - -#define _p11_sleep_ms(ms) \ - do { int _ms = (ms); \ - struct timespec _ts = { _ms / 1000, (_ms % 1000) * 1000 * 1000 }; \ - nanosleep (&_ts, NULL); \ - } while(0) - -#endif /* OS_UNIX */ - -#endif /* __COMPAT_H__ */ diff --git a/p11-kit/conf.c b/p11-kit/conf.c index 6b82d53..5c09ff5 100644 --- a/p11-kit/conf.c +++ b/p11-kit/conf.c @@ -37,11 +37,11 @@ #include "config.h" -#include "compat.h" #include "conf.h" #define DEBUG_FLAG DEBUG_CONF #include "debug.h" #include "private.h" +#include "util.h" #include #include diff --git a/p11-kit/debug.c b/p11-kit/debug.c index 6616459..37095be 100644 --- a/p11-kit/debug.c +++ b/p11-kit/debug.c @@ -36,7 +36,6 @@ #include "config.h" -#include "compat.h" #include "debug.h" #include diff --git a/p11-kit/private.h b/p11-kit/private.h index 36acb7e..da9fbae 100644 --- a/p11-kit/private.h +++ b/p11-kit/private.h @@ -36,7 +36,7 @@ #define __P11_KIT_PRIVATE_H__ #include "pkcs11.h" -#include "compat.h" +#include "util.h" extern mutex_t _p11_mutex; diff --git a/p11-kit/util.c b/p11-kit/util.c index 97a113b..63183b1 100644 --- a/p11-kit/util.c +++ b/p11-kit/util.c @@ -303,6 +303,19 @@ _p11_library_uninit (void) _p11_mutex_uninit (&_p11_mutex); } +void +_p11_mutex_init (mutex_t *mutex) +{ + pthread_mutexattr_t attr; + int ret; + + pthread_mutexattr_init (&attr); + pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); + ret = pthread_mutex_init (mutex, &attr); + assert (ret == 0); + pthread_mutexattr_destroy (&attr); +} + #if defined (HAVE_PROGRAM_INVOCATION_SHORT_NAME) && !HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME extern char *program_invocation_short_name; #endif @@ -398,6 +411,59 @@ _p11_library_uninit (void) _p11_mutex_uninit (&_p11_mutex); } +const char * +_p11_module_error (void) +{ + DWORD code = GetLastError(); + p11_local *local; + LPVOID msg_buf; + + local = _p11_library_get_thread_local (); + + FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, code, + MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&msg_buf, 0, NULL); + + if (local->last_error) + LocalFree (local->last_error); + local->last_error = msg_buf; + + return msg_buf; +} + +int +_p11_thread_create (thread_t *thread, + thread_routine routine, + void *arg) +{ + assert (thread); + + *thread = CreateThread (NULL, 0, + (LPTHREAD_START_ROUTINE)routine, + arg, 0, NULL); + + if (*thread == NULL) + return GetLastError (); + + return 0; +} + +int +_p11_thread_join (thread_t thread) +{ + DWORD res; + + res = WaitForSingleObject (thread, INFINITE); + if (res == WAIT_FAILED) + return GetLastError (); + + CloseHandle (thread); + return 0; +} + BOOL WINAPI DllMain (HINSTANCE instance, DWORD reason, diff --git a/p11-kit/util.h b/p11-kit/util.h index 36c8c24..aa7ed19 100644 --- a/p11-kit/util.h +++ b/p11-kit/util.h @@ -37,8 +37,119 @@ #ifndef __UTIL_H__ #define __UTIL_H__ +#include "config.h" + #include void* _p11_realloc (void *memory, size_t length); +/* ----------------------------------------------------------------------------- + * WIN32 + */ + +#ifdef OS_WIN32 + +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x500 +#endif + +#ifndef _WIN32_IE +#define _WIN32_IE 0x500 +#endif + +#define WIN32_LEAN_AND_MEAN 1 +#include + +/* Oh ... my ... god */ +#undef CreateMutex + +typedef CRITICAL_SECTION mutex_t; + +typedef HANDLE thread_t; + +#define _p11_mutex_init(m) \ + (InitializeCriticalSection (m)) +#define _p11_mutex_lock(m) \ + (EnterCriticalSection (m)) +#define _p11_mutex_unlock(m) \ + (LeaveCriticalSection (m)) +#define _p11_mutex_uninit(m) \ + (DeleteCriticalSection (m)) + +typedef void * (*thread_routine) (void *arg); + +int _p11_thread_create (thread_t *thread, thread_routine, void *arg); + +int _p11_thread_join (thread_t thread); + +#define _p11_thread_self() \ + (GetCurrentThread ()) + +typedef HMODULE dl_module_t; + +#define _p11_module_open(f) \ + (LoadLibrary (f)) +#define _p11_module_close(d) \ + (FreeLibrary (d)) +#define _p11_module_symbol(d, s) \ + ((void *)GetProcAddress ((d), (s))) + +const char * _p11_module_error (void); + +#define _p11_sleep_ms(ms) \ + (Sleep (ms)) + +#endif /* OS_WIN32 */ + +/* ---------------------------------------------------------------------------- + * UNIX + */ + +#ifdef OS_UNIX + +#include +#include +#include + +typedef pthread_mutex_t mutex_t; + +void _p11_mutex_init (mutex_t *mutex); + +#define _p11_mutex_lock(m) \ + (pthread_mutex_lock (m)) +#define _p11_mutex_unlock(m) \ + (pthread_mutex_unlock (m)) +#define _p11_mutex_uninit(m) \ + (pthread_mutex_destroy(m)) + +typedef pthread_t thread_t; + +typedef void * (*thread_routine) (void *arg); + +#define _p11_thread_create(t, r, a) \ + (pthread_create ((t), NULL, (r), (a))) +#define _p11_thread_join(t) \ + (pthread_join ((t), NULL)) +#define _p11_thread_self(m) \ + (pthread_self ()) + +typedef void * dl_module_t; + +#define _p11_module_open(f) \ + (dlopen ((f), RTLD_LOCAL | RTLD_NOW)) +#define _p11_module_close(d) \ + (dlclose(d)) +#define _p11_module_error() \ + (dlerror ()) +#define _p11_module_symbol(d, s) \ + (dlsym ((d), (s))) + +#define _p11_sleep_ms(ms) \ + do { int _ms = (ms); \ + struct timespec _ts = { _ms / 1000, (_ms % 1000) * 1000 * 1000 }; \ + nanosleep (&_ts, NULL); \ + } while(0) + +#endif /* OS_UNIX */ + #endif /* __UTIL_H__ */ diff --git a/tests/mock-module.c b/tests/mock-module.c index 9cb1831..36515d0 100644 --- a/tests/mock-module.c +++ b/tests/mock-module.c @@ -38,7 +38,7 @@ #include "pkcs11.h" #include "mock-module.h" -#include "p11-kit/compat.h" +#include "p11-kit/util.h" #include #include diff --git a/tests/test-init.c b/tests/test-init.c index 10790a8..be970aa 100644 --- a/tests/test-init.c +++ b/tests/test-init.c @@ -44,8 +44,8 @@ #include #include -#include "p11-kit/compat.h" #include "p11-kit/p11-kit.h" +#include "p11-kit/util.h" #include "mock-module.h" diff --git a/tools/Makefile.am b/tools/Makefile.am index e38d1a4..cec6bfc 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -8,7 +8,8 @@ bin_PROGRAMS = \ p11-kit p11_kit_SOURCES = \ - compat.c compat.h \ + $(top_srcdir)/common/compat.c \ + $(top_srcdir)/common/compat.h \ p11-kit.c p11_kit_LDADD = \ diff --git a/tools/compat.c b/tools/compat.c deleted file mode 100644 index 93ba77c..0000000 --- a/tools/compat.c +++ /dev/null @@ -1,228 +0,0 @@ -/* - * Copyright (c) 2011 Collabora Ltd. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#include "config.h" - -#include "compat.h" - -#ifndef HAVE_ERR_H - -#include -#include -#include -#include - -static const char * -calc_prog_name (void) -{ - static char prognamebuf[256]; - static int prepared = 0; - - if(!prepared) - { - const char* beg = strrchr(__argv[0], '\\'); - const char* temp = strrchr(__argv[0], '/'); - beg = (beg > temp) ? beg : temp; - beg = (beg) ? beg + 1 : __argv[0]; - - temp = strrchr(__argv[0], '.'); - temp = (temp > beg) ? temp : __argv[0] + strlen(__argv[0]); - - if((temp - beg) > 255) - temp = beg + 255; - - strncpy(prognamebuf, beg, temp - beg); - prognamebuf[temp - beg] = 0; - prepared = 1; - } - - return prognamebuf; -} - -static FILE *err_file; /* file to use for error output */ - -/* - * This is declared to take a `void *' so that the caller is not required - * to include first. However, it is really a `FILE *', and the - * manual page documents it as such. - */ -void -err_set_file (void *fp) -{ - if (fp) - err_file = fp; - else - err_file = stderr; -} - -void -err (int eval, - const char *fmt, - ...) -{ - va_list ap; - va_start(ap, fmt); - verrc(eval, errno, fmt, ap); - va_end(ap); -} - -void -verr (int eval, - const char *fmt, - va_list ap) -{ - verrc(eval, errno, fmt, ap); -} - -void -errc (int eval, - int code, - const char *fmt, - ...) -{ - va_list ap; - va_start(ap, fmt); - verrc(eval, code, fmt, ap); - va_end(ap); -} - -void -verrc (int eval, - int code, - const char *fmt, - va_list ap) -{ - if (err_file == 0) - err_set_file((FILE *)0); - fprintf(err_file, "%s: ", calc_prog_name()); - if (fmt != NULL) { - vfprintf(err_file, fmt, ap); - fprintf(err_file, ": "); - } - fprintf(err_file, "%s\n", strerror(code)); - exit(eval); -} - -void -errx (int eval, - const char *fmt, - ...) -{ - va_list ap; - va_start(ap, fmt); - verrx(eval, fmt, ap); - va_end(ap); -} - -void -verrx (int eval, - const char *fmt, - va_list ap) -{ - if (err_file == 0) - err_set_file((FILE *)0); - fprintf(err_file, "%s: ", calc_prog_name()); - if (fmt != NULL) - vfprintf(err_file, fmt, ap); - fprintf(err_file, "\n"); - exit(eval); -} - -void -warn (const char *fmt, - ...) -{ - va_list ap; - va_start(ap, fmt); - vwarnc(errno, fmt, ap); - va_end(ap); -} - -void -vwarn (const char *fmt, - va_list ap) -{ - vwarnc(errno, fmt, ap); -} - -void -warnc (int code, - const char *fmt, - ...) -{ - va_list ap; - va_start(ap, fmt); - vwarnc(code, fmt, ap); - va_end(ap); -} - -void -vwarnc (int code, - const char *fmt, - va_list ap) -{ - if (err_file == 0) - err_set_file((FILE *)0); - fprintf(err_file, "%s: ", calc_prog_name()); - if (fmt != NULL) - { - vfprintf(err_file, fmt, ap); - fprintf(err_file, ": "); - } - fprintf(err_file, "%s\n", strerror(code)); -} - -void -warnx (const char *fmt, - ...) -{ - va_list ap; - va_start(ap, fmt); - vwarnx(fmt, ap); - va_end(ap); -} - -void -vwarnx (const char *fmt, - va_list ap) -{ - if(err_file == 0) - err_set_file((FILE*)0); - fprintf(err_file, "%s: ", calc_prog_name()); - if(fmt != NULL) - vfprintf(err_file, fmt, ap); - fprintf(err_file, "\n"); -} - -#endif /* HAVE_ERR_H */ diff --git a/tools/compat.h b/tools/compat.h deleted file mode 100644 index 1562964..0000000 --- a/tools/compat.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2011 Collabora Ltd. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * Author: Stef Walter - */ - -#ifndef __ERR_H__ -#define __ERR_H__ - -#include "config.h" - -#ifdef HAVE_ERR_H -#include - -#else /* !HAVE_ERR_H */ - -#include -void err_set_file (void *fp); -void err_set_exit (void (*ef)(int)); -void err (int eval, const char *fmt, ...); -void verr (int eval, const char *fmt, va_list ap); -void errc (int eval, int code, const char *fmt, ...); -void verrc (int eval, int code, const char *fmt, va_list ap); -void errx (int eval, const char *fmt, ...); -void verrx (int eval, const char *fmt, va_list ap); -void warn (const char *fmt, ...); -void vwarn (const char *fmt, va_list ap); -void warnc (int code, const char *fmt, ...); -void vwarnc (int code, const char *fmt, va_list ap); -void warnx (const char *fmt, ...); -void vwarnx (const char *fmt, va_list ap); - -#endif /* !HAVE_ERR_H */ - -#endif /* __ERR_H__ */ diff --git a/tools/p11-kit.c b/tools/p11-kit.c index f63779e..d4b0759 100644 --- a/tools/p11-kit.c +++ b/tools/p11-kit.c @@ -34,7 +34,7 @@ #include "config.h" -#include "compat.h" +#include "common/compat.h" #include #include @@ -146,7 +146,7 @@ print_token_info (CK_FUNCTION_LIST_PTR module, CK_SLOT_ID slot_id) X(CKF_LOGIN_REQUIRED, "login-required"); X(CKF_USER_PIN_INITIALIZED, "user-pin-initialized"); X(CKF_RESTORE_KEY_NOT_NEEDED, "restore-key-not-needed"); - X(CKF_CLOCK_ON_TOKEN, "clock-on-tokne"); + X(CKF_CLOCK_ON_TOKEN, "clock-on-token"); X(CKF_PROTECTED_AUTHENTICATION_PATH, "protected-authentication-path"); X(CKF_DUAL_CRYPTO_OPERATIONS, "dual-crypto-operations"); X(CKF_TOKEN_INITIALIZED, "token-initialized"); -- cgit v1.1