summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/Makefile.am15
-rw-r--r--common/lexer.c2
-rw-r--r--common/library.c85
-rw-r--r--common/library.h14
-rw-r--r--common/message.c140
-rw-r--r--common/message.h62
-rw-r--r--common/mock.c2
-rw-r--r--common/tests/Makefile.am3
-rw-r--r--common/tests/test-base64.c5
-rw-r--r--common/tests/test-lexer.c3
-rw-r--r--common/tests/test-url.c5
11 files changed, 234 insertions, 102 deletions
diff --git a/common/Makefile.am b/common/Makefile.am
index bfed1d7..cb6e95e 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -10,29 +10,30 @@ inc_HEADERS = \
$(NULL)
noinst_LTLIBRARIES = \
- libp11-compat.la \
+ libp11-common.la \
libp11-library.la \
libp11-mock.la \
$(NULL)
-libp11_compat_la_SOURCES = \
- compat.c compat.h \
- $(NULL)
-
-libp11_library_la_SOURCES = \
+libp11_common_la_SOURCES = \
attrs.c attrs.h \
array.c array.h \
buffer.c buffer.h \
+ compat.c compat.h \
constants.c constants.h \
debug.c debug.h \
dict.c dict.h \
hash.c hash.h \
lexer.c lexer.h \
- library.c library.h \
+ message.c message.h \
pkcs11.h pkcs11x.h \
url.c url.h \
$(NULL)
+libp11_library_la_SOURCES = \
+ library.c library.h \
+ $(NULL)
+
libp11_mock_la_SOURCES = \
mock.c mock.h \
$(NULL)
diff --git a/common/lexer.c b/common/lexer.c
index 9898e2c..329881f 100644
--- a/common/lexer.c
+++ b/common/lexer.c
@@ -41,7 +41,7 @@
#define P11_DEBUG_FLAG P11_DEBUG_CONF
#include "debug.h"
#include "lexer.h"
-#include "library.h"
+#include "message.h"
#include <assert.h>
#include <ctype.h>
diff --git a/common/library.c b/common/library.c
index 71dd3b9..1f9dc7a 100644
--- a/common/library.c
+++ b/common/library.c
@@ -41,6 +41,7 @@
#define P11_DEBUG_FLAG P11_DEBUG_LIB
#include "debug.h"
#include "library.h"
+#include "message.h"
#include <assert.h>
#include <stdarg.h>
@@ -48,13 +49,10 @@
#include <stdio.h>
#include <string.h>
-#define P11_MAX_MESSAGE 512
+#define P11_MESSAGE_MAX 512
typedef struct {
- char message[P11_MAX_MESSAGE];
-#ifdef OS_WIN32
- void *last_error;
-#endif
+ char message[P11_MESSAGE_MAX];
} p11_local;
static p11_local * _p11_library_get_thread_local (void);
@@ -65,76 +63,18 @@ p11_mutex_t p11_library_mutex;
pthread_once_t p11_library_once;
#endif
-static bool print_messages = true;
-
-void
-p11_message_store (const char* msg,
- size_t length)
+static char *
+thread_local_message (void)
{
p11_local *local;
-
- if (length > P11_MAX_MESSAGE - 1)
- length = P11_MAX_MESSAGE - 1;
-
local = _p11_library_get_thread_local ();
- if (local != NULL) {
- memcpy (local->message, msg, length);
- local->message[length] = 0;
- }
+ return local ? local->message : NULL;
}
-void
-p11_message (const char* msg,
- ...)
+static char *
+dont_store_message (void)
{
- char buffer[P11_MAX_MESSAGE];
- va_list va;
- size_t length;
-
- va_start (va, msg);
- length = vsnprintf (buffer, P11_MAX_MESSAGE - 1, msg, va);
- va_end (va);
-
- /* Was it truncated? */
- if (length > P11_MAX_MESSAGE - 1)
- length = P11_MAX_MESSAGE - 1;
- buffer[length] = 0;
-
- /* If printing is not disabled, just print out */
- if (print_messages)
- fprintf (stderr, "p11-kit: %s\n", buffer);
- else
- p11_debug_message (P11_DEBUG_LIB, "message: %s", buffer);
- p11_message_store (buffer, length);
-}
-
-void
-p11_message_quiet (void)
-{
- print_messages = false;
-}
-
-void
-p11_message_loud (void)
-{
- print_messages = true;
-}
-
-const char*
-p11_message_last (void)
-{
- p11_local *local;
- local = _p11_library_get_thread_local ();
- return local && local->message[0] ? local->message : NULL;
-}
-
-void
-p11_message_clear (void)
-{
- p11_local *local;
- local = _p11_library_get_thread_local ();
- if (local != NULL)
- local->message[0] = 0;
+ return NULL;
}
static void
@@ -170,6 +110,7 @@ p11_library_init_impl (void)
p11_debug ("initializing library");
p11_mutex_init (&p11_library_mutex);
pthread_key_create (&thread_local, free);
+ p11_message_storage = thread_local_message;
}
void
@@ -187,6 +128,7 @@ p11_library_uninit (void)
free (pthread_getspecific (thread_local));
pthread_setspecific (thread_local, NULL);
+ p11_message_storage = dont_store_message;
pthread_key_delete (thread_local);
p11_mutex_uninit (&p11_library_mutex);
}
@@ -225,6 +167,8 @@ p11_library_init (void)
thread_local = TlsAlloc ();
if (thread_local == TLS_OUT_OF_INDEXES)
p11_debug ("couldn't setup tls");
+ else
+ p11_message_storage = thread_local_message;
}
void
@@ -234,8 +178,6 @@ p11_library_thread_cleanup (void)
if (thread_local != TLS_OUT_OF_INDEXES) {
p11_debug ("thread stopped, freeing tls");
local = TlsGetValue (thread_local);
- if (local->last_error)
- LocalFree (local->last_error);
LocalFree (local);
}
}
@@ -248,6 +190,7 @@ p11_library_uninit (void)
uninit_common ();
if (thread_local != TLS_OUT_OF_INDEXES) {
+ p11_message_storage = dont_store_message;
data = TlsGetValue (thread_local);
free (data);
TlsFree (thread_local);
diff --git a/common/library.h b/common/library.h
index b310cb9..33a33fb 100644
--- a/common/library.h
+++ b/common/library.h
@@ -48,20 +48,6 @@ extern p11_mutex_t p11_library_mutex;
#define p11_unlock() p11_mutex_unlock (&p11_library_mutex);
-void p11_message (const char* msg,
- ...) GNUC_PRINTF (1, 2);
-
-void p11_message_store (const char* msg,
- size_t length);
-
-const char * p11_message_last (void);
-
-void p11_message_clear (void);
-
-void p11_message_quiet (void);
-
-void p11_message_loud (void);
-
#ifdef OS_WIN32
/* No implementation, because done by DllMain */
diff --git a/common/message.c b/common/message.c
new file mode 100644
index 0000000..8b54ad1
--- /dev/null
+++ b/common/message.c
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2011 Collabora Ltd
+ * Copyright (c) 2012 Stef Walter
+ *
+ * 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.
+ *
+ *
+ * CONTRIBUTORS
+ * Stef Walter <stef@thewalter.net>
+ */
+
+#include "config.h"
+
+#include "compat.h"
+#define P11_DEBUG_FLAG P11_DEBUG_LIB
+#include "debug.h"
+#include "message.h"
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+static bool print_messages = true;
+
+static char *
+default_message_storage (void)
+{
+ static char message[P11_MESSAGE_MAX] = { 0, };
+ return message;
+}
+
+/* Function pointer declared in message.h as extern */
+char * (* p11_message_storage) (void) = default_message_storage;
+
+void
+p11_message_store (const char* msg,
+ size_t length)
+{
+ char *buffer;
+
+ /*
+ * p11_message_storage() is called to get a storage location for
+ * the last message. It defaults to a globally allocated buffer
+ * but is overridden in library.c with a function that returns
+ * per thread buffers.
+ *
+ * The returned value is P11_MESSAGE_MAX bytes long
+ */
+ buffer = p11_message_storage ();
+
+ if (length > P11_MESSAGE_MAX - 1)
+ length = P11_MESSAGE_MAX - 1;
+
+ if (buffer != NULL) {
+ memcpy (buffer, msg, length);
+ buffer[length] = 0;
+ }
+}
+
+void
+p11_message (const char* msg,
+ ...)
+{
+ char buffer[P11_MESSAGE_MAX];
+ va_list va;
+ size_t length;
+
+ va_start (va, msg);
+ length = vsnprintf (buffer, P11_MESSAGE_MAX - 1, msg, va);
+ va_end (va);
+
+ /* Was it truncated? */
+ if (length > P11_MESSAGE_MAX - 1)
+ length = P11_MESSAGE_MAX - 1;
+ buffer[length] = 0;
+
+ /* If printing is not disabled, just print out */
+ if (print_messages)
+ fprintf (stderr, "p11-kit: %s\n", buffer);
+ else
+ p11_debug_message (P11_DEBUG_LIB, "message: %s", buffer);
+ p11_message_store (buffer, length);
+}
+
+void
+p11_message_quiet (void)
+{
+ print_messages = false;
+}
+
+void
+p11_message_loud (void)
+{
+ print_messages = true;
+}
+
+const char *
+p11_message_last (void)
+{
+ char *buffer;
+ buffer = p11_message_storage ();
+ return buffer && buffer[0] ? buffer : NULL;
+}
+
+void
+p11_message_clear (void)
+{
+ char *buffer;
+ buffer = p11_message_storage ();
+ if (buffer != NULL)
+ buffer[0] = 0;
+}
diff --git a/common/message.h b/common/message.h
new file mode 100644
index 0000000..60a7f81
--- /dev/null
+++ b/common/message.h
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ *
+ *
+ * CONTRIBUTORS
+ * Stef Walter <stef@memberwebs.com>
+ */
+
+#ifndef P11_MESSAGE_H_
+#define P11_MESSAGE_H_
+
+#include "compat.h"
+
+#include <sys/types.h>
+
+#define P11_MESSAGE_MAX 512
+
+extern char * (* p11_message_storage) (void);
+
+void p11_message (const char* msg,
+ ...) GNUC_PRINTF (1, 2);
+
+void p11_message_store (const char* msg,
+ size_t length);
+
+const char * p11_message_last (void);
+
+void p11_message_clear (void);
+
+void p11_message_quiet (void);
+
+void p11_message_loud (void);
+
+#endif /* P11_MESSAGE_H_ */
diff --git a/common/mock.c b/common/mock.c
index 9d6c960..1a283b9 100644
--- a/common/mock.c
+++ b/common/mock.c
@@ -35,9 +35,9 @@
#include "config.h"
#include "debug.h"
-#include "library.h"
#define CRYPTOKI_EXPORTS
#include "pkcs11.h"
+#include "message.h"
#include "mock.h"
diff --git a/common/tests/Makefile.am b/common/tests/Makefile.am
index d024b80..ba9a72f 100644
--- a/common/tests/Makefile.am
+++ b/common/tests/Makefile.am
@@ -60,6 +60,5 @@ endif # WITH_ASN1
TESTS = $(CHECK_PROGS)
LDADD += \
- $(top_builddir)/common/libp11-library.la \
- $(top_builddir)/common/libp11-compat.la \
+ $(top_builddir)/common/libp11-common.la \
$(CUTEST_LIBS)
diff --git a/common/tests/test-base64.c b/common/tests/test-base64.c
index c053305..90c1f49 100644
--- a/common/tests/test-base64.c
+++ b/common/tests/test-base64.c
@@ -36,7 +36,8 @@
#include "CuTest.h"
#include "base64.h"
-#include "library.h"
+#include "debug.h"
+#include "message.h"
#include <assert.h>
#include <string.h>
@@ -196,7 +197,7 @@ main (void)
int ret;
putenv ("P11_KIT_STRICT=1");
- p11_library_init ();
+ p11_debug_init ();
SUITE_ADD_TEST (suite, test_decode_simple);
SUITE_ADD_TEST (suite, test_decode_thawte);
diff --git a/common/tests/test-lexer.c b/common/tests/test-lexer.c
index 02ea5c5..58d5d65 100644
--- a/common/tests/test-lexer.c
+++ b/common/tests/test-lexer.c
@@ -42,7 +42,7 @@
#include "compat.h"
#include "debug.h"
#include "lexer.h"
-#include "library.h"
+#include "message.h"
#include "pem.h"
typedef struct {
@@ -260,7 +260,6 @@ main (void)
putenv ("P11_KIT_STRICT=1");
p11_debug_init ();
- p11_library_init ();
SUITE_ADD_TEST (suite, test_basic);
SUITE_ADD_TEST (suite, test_corners);
diff --git a/common/tests/test-url.c b/common/tests/test-url.c
index 096563b..ed84f0c 100644
--- a/common/tests/test-url.c
+++ b/common/tests/test-url.c
@@ -35,7 +35,8 @@
#include "config.h"
#include "CuTest.h"
-#include "library.h"
+#include "debug.h"
+#include "message.h"
#include <assert.h>
#include <string.h>
@@ -146,7 +147,7 @@ main (void)
int ret;
putenv ("P11_KIT_STRICT=1");
- p11_library_init ();
+ p11_debug_init ();
SUITE_ADD_TEST (suite, test_decode_success);
SUITE_ADD_TEST (suite, test_decode_skip);