summaryrefslogtreecommitdiff
path: root/p11-kit/util.c
diff options
context:
space:
mode:
authorStef Walter <stefw@collabora.co.uk>2011-06-09 09:42:55 +0200
committerStef Walter <stefw@collabora.co.uk>2011-06-09 09:42:55 +0200
commit48a08272bfcc0153887b850b4ea82e8fb7d8f1ae (patch)
treed17ab88ff14e5e515edb6a7126e0778dd95f34cf /p11-kit/util.c
parent21333019a5afceb5f07637fb50b784a4ecd9f9ff (diff)
downloadp11-kit-48a08272bfcc0153887b850b4ea82e8fb7d8f1ae.tar.gz
Store last failure message per thread.
* Add p11_kit_message() function to get last message.
Diffstat (limited to 'p11-kit/util.c')
-rw-r--r--p11-kit/util.c87
1 files changed, 82 insertions, 5 deletions
diff --git a/p11-kit/util.c b/p11-kit/util.c
index 7ea125f..dda4703 100644
--- a/p11-kit/util.c
+++ b/p11-kit/util.c
@@ -46,6 +46,11 @@
#include <stdio.h>
#include <string.h>
+#define MAX_MESSAGE 512
+static pthread_once_t key_once = PTHREAD_ONCE_INIT;
+static pthread_key_t message_buffer_key = 0;
+static int print_messages = 1;
+
void*
xrealloc (void *memory, size_t length)
{
@@ -127,16 +132,88 @@ p11_kit_space_strdup (const unsigned char *string, size_t max_length)
return result;
}
+static void
+create_message_buffer_key (void)
+{
+ pthread_key_create (&message_buffer_key, free);
+}
+
+static void
+store_message_buffer (const char* msg, size_t length)
+{
+ char *thread_buf;
+
+ if (length > MAX_MESSAGE - 1)
+ length = MAX_MESSAGE - 1;
+
+ pthread_once (&key_once, create_message_buffer_key);
+ thread_buf = pthread_getspecific (message_buffer_key);
+ if (!thread_buf) {
+ thread_buf = malloc (MAX_MESSAGE);
+ pthread_setspecific (message_buffer_key, thread_buf);
+ }
+
+ memcpy (thread_buf, msg, length);
+ thread_buf[length] = 0;
+}
+
void
-_p11_warning (const char* msg, ...)
+_p11_message (const char* msg, ...)
{
- char buffer[512];
+ char buffer[MAX_MESSAGE];
va_list va;
+ size_t length;
va_start (va, msg);
- vsnprintf (buffer, sizeof (buffer) - 1, msg, va);
+ length = vsnprintf (buffer, MAX_MESSAGE - 1, msg, va);
va_end (va);
- buffer[sizeof (buffer) - 1] = 0;
- fprintf (stderr, "p11-kit: %s\n", buffer);
+ /* Was it truncated? */
+ if (length > MAX_MESSAGE - 1)
+ length = MAX_MESSAGE - 1;
+ buffer[length] = 0;
+
+ /* If printing is not disabled, just print out */
+ if (print_messages)
+ fprintf (stderr, "p11-kit: %s\n", buffer);
+
+ store_message_buffer (buffer, length);
+}
+
+void
+p11_kit_be_quiet (void)
+{
+ _p11_lock ();
+ print_messages = 0;
+ _p11_unlock ();
+}
+
+const char*
+p11_kit_message (void)
+{
+ char *thread_buf;
+ pthread_once (&key_once, create_message_buffer_key);
+ thread_buf = pthread_getspecific (message_buffer_key);
+ return thread_buf && thread_buf[0] ? thread_buf : NULL;
+}
+
+void
+_p11_kit_clear_message (void)
+{
+ char *thread_buf;
+ pthread_once (&key_once, create_message_buffer_key);
+ thread_buf = pthread_getspecific (message_buffer_key);
+ if (thread_buf != NULL)
+ thread_buf[0] = 0;
+}
+
+void
+_p11_kit_default_message (CK_RV rv)
+{
+ const char *msg;
+
+ if (rv != CKR_OK) {
+ msg = p11_kit_strerror (rv);
+ store_message_buffer (msg, strlen (msg));
+ }
}