summaryrefslogtreecommitdiff
path: root/p11-kit
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
parent21333019a5afceb5f07637fb50b784a4ecd9f9ff (diff)
Store last failure message per thread.
* Add p11_kit_message() function to get last message.
Diffstat (limited to 'p11-kit')
-rw-r--r--p11-kit/Makefile.am3
-rw-r--r--p11-kit/conf.c22
-rw-r--r--p11-kit/modules.c46
-rw-r--r--p11-kit/p11-kit.h8
-rw-r--r--p11-kit/private.h6
-rw-r--r--p11-kit/util.c87
6 files changed, 142 insertions, 30 deletions
diff --git a/p11-kit/Makefile.am b/p11-kit/Makefile.am
index 6f2fc19..9afc6b6 100644
--- a/p11-kit/Makefile.am
+++ b/p11-kit/Makefile.am
@@ -1,6 +1,7 @@
INCLUDES = \
- -I$(top_srcdir)
+ -I$(top_srcdir) \
+ -DP11_KIT_FUTURE_UNSTABLE_API
incdir = $(includedir)/p11-kit-1/p11-kit
diff --git a/p11-kit/conf.c b/p11-kit/conf.c
index 8d4703e..6b66288 100644
--- a/p11-kit/conf.c
+++ b/p11-kit/conf.c
@@ -156,14 +156,15 @@ read_config_file (const char* filename, int flags)
if (f == NULL) {
error = errno;
if ((flags & CONF_IGNORE_MISSING) &&
- (errno == ENOENT || errno == ENOTDIR)) {
+ (error == ENOENT || error == ENOTDIR)) {
debug ("config file does not exist");
config = strdup ("\n");
if (!config)
errno = ENOMEM;
return config;
}
- _p11_warning ("couldn't open config file: %s", filename);
+ _p11_message ("couldn't open config file: %s: %s", filename,
+ strerror (error));
errno = error;
return NULL;
}
@@ -173,13 +174,13 @@ read_config_file (const char* filename, int flags)
(len = ftell (f)) == -1 ||
fseek (f, 0, SEEK_SET) == -1) {
error = errno;
- _p11_warning ("couldn't seek config file: %s", filename);
+ _p11_message ("couldn't seek config file: %s", filename);
errno = error;
return NULL;
}
if ((config = (char*)malloc (len + 2)) == NULL) {
- _p11_warning ("out of memory");
+ _p11_message ("out of memory");
errno = ENOMEM;
return NULL;
}
@@ -187,7 +188,7 @@ read_config_file (const char* filename, int flags)
/* And read in one block */
if (fread (config, 1, len, f) != len) {
error = errno;
- _p11_warning ("couldn't read config file: %s", filename);
+ _p11_message ("couldn't read config file: %s", filename);
errno = error;
return NULL;
}
@@ -281,7 +282,7 @@ _p11_conf_parse_file (const char* filename, int flags)
/* Look for the break between name: value on the same line */
value = name + strcspn (name, ":");
if (!*value) {
- _p11_warning ("%s: invalid config line: %s", filename, name);
+ _p11_message ("%s: invalid config line: %s", filename, name);
error = EINVAL;
break;
}
@@ -341,7 +342,7 @@ expand_user_path (const char *path)
pwd = getpwuid (getuid ());
if (!pwd) {
error = errno;
- _p11_warning ("couldn't lookup home directory for user %d: %s",
+ _p11_message ("couldn't lookup home directory for user %d: %s",
getuid (), strerror (errno));
errno = error;
return NULL;
@@ -371,7 +372,7 @@ user_config_mode (hash_t *config, int defmode)
} else if (strequal (mode, "override")) {
return CONF_USER_ONLY;
} else {
- _p11_warning ("invalid mode for 'user-config': %s", mode);
+ _p11_message ("invalid mode for 'user-config': %s", mode);
return CONF_USER_INVALID;
}
}
@@ -515,7 +516,8 @@ load_configs_from_directory (const char *directory, hash_t *configs)
error = errno;
if (errno == ENOENT || errno == ENOTDIR)
return 0;
- _p11_warning ("couldn't list directory: %s", directory);
+ _p11_message ("couldn't list directory: %s: %s", directory,
+ strerror (error));
errno = error;
return -1;
}
@@ -537,7 +539,7 @@ load_configs_from_directory (const char *directory, hash_t *configs)
{
if (stat (path, &st) < 0) {
error = errno;
- _p11_warning ("couldn't stat path: %s", path);
+ _p11_message ("couldn't stat path: %s", path);
free (path);
break;
}
diff --git a/p11-kit/modules.c b/p11-kit/modules.c
index 999770d..757b4d2 100644
--- a/p11-kit/modules.c
+++ b/p11-kit/modules.c
@@ -256,7 +256,7 @@ dlopen_and_get_function_list (Module *mod, const char *path)
mod->dl_module = dlopen (path, RTLD_LOCAL | RTLD_NOW);
if (mod->dl_module == NULL) {
- _p11_warning ("couldn't load module: %s: %s", path, dlerror ());
+ _p11_message ("couldn't load module: %s: %s", path, dlerror ());
return CKR_GENERAL_ERROR;
}
@@ -264,14 +264,14 @@ dlopen_and_get_function_list (Module *mod, const char *path)
gfl = dlsym (mod->dl_module, "C_GetFunctionList");
if (!gfl) {
- _p11_warning ("couldn't find C_GetFunctionList entry point in module: %s: %s",
+ _p11_message ("couldn't find C_GetFunctionList entry point in module: %s: %s",
path, dlerror ());
return CKR_GENERAL_ERROR;
}
rv = gfl (&mod->funcs);
if (rv != CKR_OK) {
- _p11_warning ("call to C_GetFunctiontList failed in module: %s: %s",
+ _p11_message ("call to C_GetFunctiontList failed in module: %s: %s",
path, p11_kit_strerror (rv));
return rv;
}
@@ -360,7 +360,7 @@ take_config_and_load_module_unlocked (char **name, hash_t **config)
/* Refuse to load duplicate module */
if (prev) {
- _p11_warning ("duplicate configured module: %s: %s", mod->name, path);
+ _p11_message ("duplicate configured module: %s: %s", mod->name, path);
free_module_unlocked (mod);
return CKR_GENERAL_ERROR;
}
@@ -661,9 +661,13 @@ p11_kit_initialize_registered (void)
_p11_lock ();
+ _p11_kit_clear_message ();
+
/* WARNING: Reentrancy can occur here */
rv = _p11_kit_initialize_registered_unlocked_reentrant ();
+ _p11_kit_default_message (rv);
+
_p11_unlock ();
/* Cleanup any partial initialization */
@@ -739,9 +743,13 @@ p11_kit_finalize_registered (void)
_p11_lock ();
+ _p11_kit_clear_message ();
+
/* WARNING: Reentrant calls can occur here */
rv = _p11_kit_finalize_registered_unlocked_reentrant ();
+ _p11_kit_default_message (rv);
+
_p11_unlock ();
debug ("out: %lu", rv);
@@ -787,6 +795,8 @@ p11_kit_registered_modules (void)
_p11_lock ();
+ _p11_kit_clear_message ();
+
result = _p11_kit_registered_modules_unlocked ();
_p11_unlock ();
@@ -813,12 +823,11 @@ p11_kit_registered_module_to_name (CK_FUNCTION_LIST_PTR module)
Module *mod;
char *name = NULL;
- if (!module)
- return NULL;
-
_p11_lock ();
- mod = gl.modules ? hash_get (gl.modules, module) : NULL;
+ _p11_kit_clear_message ();
+
+ mod = module && gl.modules ? hash_get (gl.modules, module) : NULL;
if (mod && mod->name)
name = strdup (mod->name);
@@ -845,6 +854,8 @@ p11_kit_registered_name_to_module (const char *name)
_p11_lock ();
+ _p11_kit_clear_message ();
+
if (gl.modules) {
mod = find_module_for_name_unlocked (name);
if (mod)
@@ -876,11 +887,10 @@ p11_kit_registered_option (CK_FUNCTION_LIST_PTR module, const char *field)
char *option = NULL;
hash_t *config = NULL;
- if (!field)
- return NULL;
-
_p11_lock ();
+ _p11_kit_clear_message ();
+
if (module == NULL) {
config = gl.config;
@@ -890,7 +900,7 @@ p11_kit_registered_option (CK_FUNCTION_LIST_PTR module, const char *field)
config = mod->config;
}
- if (config) {
+ if (config && field) {
option = hash_get (config, field);
if (option)
option = strdup (option);
@@ -940,6 +950,8 @@ p11_kit_initialize_module (CK_FUNCTION_LIST_PTR module)
_p11_lock ();
+ _p11_kit_clear_message ();
+
rv = init_globals_unlocked ();
if (rv == CKR_OK) {
@@ -962,6 +974,8 @@ p11_kit_initialize_module (CK_FUNCTION_LIST_PTR module)
free (allocated);
}
+ _p11_kit_default_message (rv);
+
_p11_unlock ();
debug ("out: %lu", rv);
@@ -1000,6 +1014,8 @@ p11_kit_finalize_module (CK_FUNCTION_LIST_PTR module)
_p11_lock ();
+ _p11_kit_clear_message ();
+
mod = gl.modules ? hash_get (gl.modules, module) : NULL;
if (mod == NULL) {
debug ("module not found");
@@ -1009,6 +1025,8 @@ p11_kit_finalize_module (CK_FUNCTION_LIST_PTR module)
rv = finalize_module_unlocked_reentrant (mod);
}
+ _p11_kit_default_message (rv);
+
_p11_unlock ();
debug ("out: %lu", rv);
@@ -1057,6 +1075,8 @@ p11_kit_load_initialize_module (const char *module_path,
_p11_lock ();
+ _p11_kit_clear_message ();
+
rv = init_globals_unlocked ();
if (rv == CKR_OK) {
@@ -1071,6 +1091,8 @@ p11_kit_load_initialize_module (const char *module_path,
if (rv == CKR_OK && module)
*module = mod->funcs;
+ _p11_kit_default_message (rv);
+
_p11_unlock ();
debug ("out: %lu", rv);
diff --git a/p11-kit/p11-kit.h b/p11-kit/p11-kit.h
index 96b9df3..cf84db6 100644
--- a/p11-kit/p11-kit.h
+++ b/p11-kit/p11-kit.h
@@ -78,6 +78,14 @@ size_t p11_kit_space_strlen (const unsigned char
char* p11_kit_space_strdup (const unsigned char *string,
size_t max_length);
+#ifdef P11_KIT_FUTURE_UNSTABLE_API
+
+void p11_kit_be_quiet (void);
+
+const char* p11_kit_message (void);
+
+#endif
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/p11-kit/private.h b/p11-kit/private.h
index e8abe93..b5a8653 100644
--- a/p11-kit/private.h
+++ b/p11-kit/private.h
@@ -44,7 +44,7 @@ extern pthread_mutex_t _p11_mutex;
#define _p11_unlock() pthread_mutex_unlock (&_p11_mutex);
-void _p11_warning (const char* msg, ...);
+void _p11_message (const char* msg, ...);
CK_FUNCTION_LIST_PTR_PTR _p11_kit_registered_modules_unlocked (void);
@@ -58,6 +58,8 @@ CK_RV _p11_load_config_files_unlocked (const char *sys
const char *user_conf,
int *user_mode);
-const char* _p11_kit_clear_message (void);
+void _p11_kit_clear_message (void);
+
+void _p11_kit_default_message (CK_RV rv);
#endif /* __P11_KIT_PRIVATE_H__ */
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));
+ }
}