* Add p11_kit_message() function to get last message.
<term>user-config</term>
<listitem><para>This will be equal to one of the following values:
<literal>none</literal>, <literal>merge</literal>,
- <literal>override</literal>.</para></listitem>
+ <literal>only</literal>.</para></listitem>
</varlistentry>
</variablelist>
INCLUDES = \
- -I$(top_srcdir)
+ -I$(top_srcdir) \
+ -DP11_KIT_FUTURE_UNSTABLE_API
incdir = $(includedir)/p11-kit-1/p11-kit
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;
}
(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;
}
/* 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;
}
/* 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;
}
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;
} 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;
}
}
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;
}
{
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;
}
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;
}
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;
}
/* 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;
}
_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 */
_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);
_p11_lock ();
+ _p11_kit_clear_message ();
+
result = _p11_kit_registered_modules_unlocked ();
_p11_unlock ();
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);
_p11_lock ();
+ _p11_kit_clear_message ();
+
if (gl.modules) {
mod = find_module_for_name_unlocked (name);
if (mod)
char *option = NULL;
hash_t *config = NULL;
- if (!field)
- return NULL;
-
_p11_lock ();
+ _p11_kit_clear_message ();
+
if (module == NULL) {
config = gl.config;
config = mod->config;
}
- if (config) {
+ if (config && field) {
option = hash_get (config, field);
if (option)
option = strdup (option);
_p11_lock ();
+ _p11_kit_clear_message ();
+
rv = init_globals_unlocked ();
if (rv == CKR_OK) {
free (allocated);
}
+ _p11_kit_default_message (rv);
+
_p11_unlock ();
debug ("out: %lu", rv);
_p11_lock ();
+ _p11_kit_clear_message ();
+
mod = gl.modules ? hash_get (gl.modules, module) : NULL;
if (mod == NULL) {
debug ("module not found");
rv = finalize_module_unlocked_reentrant (mod);
}
+ _p11_kit_default_message (rv);
+
_p11_unlock ();
debug ("out: %lu", rv);
_p11_lock ();
+ _p11_kit_clear_message ();
+
rv = init_globals_unlocked ();
if (rv == CKR_OK) {
if (rv == CKR_OK && module)
*module = mod->funcs;
+ _p11_kit_default_message (rv);
+
_p11_unlock ();
debug ("out: %lu", rv);
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
#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);
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__ */
#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)
{
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));
+ }
}
-I$(top_srcdir) \
-I$(top_srcdir)/p11-kit \
-I$(srcdir)/cutest \
- -DSRCDIR=\"$(srcdir)\"
+ -DSRCDIR=\"$(srcdir)\" \
+ -DP11_KIT_FUTURE_UNSTABLE_API
noinst_PROGRAMS = \
hash-test \
#include <string.h>
#include "conf.h"
-
-static int n_errors = 0;
+#include "p11-kit.h"
static void
test_parse_conf_1 (CuTest *tc)
{
hash_t *ht;
- n_errors = 0;
ht = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", CONF_IGNORE_MISSING);
CuAssertPtrNotNull (tc, ht);
CuAssertIntEquals (tc, 0, hash_count (ht));
- CuAssertIntEquals (tc, 0, n_errors);
+ CuAssertPtrEquals (tc, NULL, (void*)p11_kit_message ());
hash_free (ht);
}
{
hash_t *ht;
- n_errors = 0;
ht = _p11_conf_parse_file (SRCDIR "/files/non-existant.conf", 0);
CuAssertPtrEquals (tc, ht, NULL);
- CuAssertIntEquals (tc, 1, n_errors);
+ CuAssertPtrNotNull (tc, p11_kit_message ());
}
static void
hash_set (defaults, strdup ("two"), strdup ("default2"));
hash_set (defaults, strdup ("three"), strdup ("default3"));
- if (!_p11_conf_merge_defaults (values, defaults))
+ if (_p11_conf_merge_defaults (values, defaults) < 0)
CuFail (tc, "should not be reached");
hash_free (defaults);
SUITE_ADD_TEST (suite, test_parse_fail_missing);
SUITE_ADD_TEST (suite, test_merge_defaults);
+ p11_kit_be_quiet ();
+
CuSuiteRun (suite);
CuSuiteSummary (suite, output);
CuSuiteDetails (suite, output);