]> granicus.if.org Git - p11-kit/commitdiff
Store last failure message per thread.
authorStef Walter <stefw@collabora.co.uk>
Thu, 9 Jun 2011 07:42:55 +0000 (09:42 +0200)
committerStef Walter <stefw@collabora.co.uk>
Thu, 9 Jun 2011 07:42:55 +0000 (09:42 +0200)
 * Add p11_kit_message() function to get last message.

doc/p11-kit-config.xml
p11-kit/Makefile.am
p11-kit/conf.c
p11-kit/modules.c
p11-kit/p11-kit.h
p11-kit/private.h
p11-kit/util.c
tests/Makefile.am
tests/conf-test.c

index 2284265fbdce24845c2f384c29a92159cdd9963d..bd19a69dcd46f0b4c712f2fdefae33a0faaefcc7 100644 (file)
@@ -88,7 +88,7 @@
                                <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>
 
index 6f2fc1924187a54dbc738bd5c1f0d49c6c7d4203..9afc6b6ca2fbc6102bcb281a9edcc53f81fb339a 100644 (file)
@@ -1,6 +1,7 @@
 
 INCLUDES = \
-       -I$(top_srcdir)
+       -I$(top_srcdir) \
+       -DP11_KIT_FUTURE_UNSTABLE_API
 
 incdir = $(includedir)/p11-kit-1/p11-kit
 
index 8d4703e0e104d3d423e73b49a24d4ce169f501fe..6b66288bc38df95021b18bbaaa12232d9bb94da4 100644 (file)
@@ -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;
                        }
index 999770dc20dda18db6991791232176a47a0fa740..757b4d2cb927fe2bf220043bc23557cf72d7149f 100644 (file)
@@ -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);
index 96b9df35be0c9a7e1948d4ff64588cab32007614..cf84db63fda718be3d205fc9ec433c78dbfe47fb 100644 (file)
@@ -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
index e8abe93af9eee38c7366b890173e0f174303b489..b5a8653488ab1e54c74be217b902dad624863a7f 100644 (file)
@@ -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__ */
index 7ea125f57876eea19ddce09d2fc46b502ada9e22..dda470323e79a5be7834076d5ec1dc577aebf8e6 100644 (file)
 #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));
+       }
 }
index cabe1f7e251b5556ba66ccbf72787a9cd31d5362..6b988b8faf7fc440dcd74d42429d537b0a9432ee 100644 (file)
@@ -3,7 +3,8 @@ INCLUDES = \
        -I$(top_srcdir) \
        -I$(top_srcdir)/p11-kit \
        -I$(srcdir)/cutest \
-       -DSRCDIR=\"$(srcdir)\"
+       -DSRCDIR=\"$(srcdir)\" \
+       -DP11_KIT_FUTURE_UNSTABLE_API
 
 noinst_PROGRAMS = \
        hash-test \
index 2ef4e5c8c329ea6062fee2a4d640f06cd11dc457..4b2f820f37ea519663e412dd65981785aa1cb997 100644 (file)
@@ -40,8 +40,7 @@
 #include <string.h>
 
 #include "conf.h"
-
-static int n_errors = 0;
+#include "p11-kit.h"
 
 static void
 test_parse_conf_1 (CuTest *tc)
@@ -72,12 +71,11 @@ test_parse_ignore_missing (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);
 }
 
@@ -86,10 +84,9 @@ test_parse_fail_missing (CuTest *tc)
 {
        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
@@ -107,7 +104,7 @@ test_merge_defaults (CuTest *tc)
        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);
@@ -131,6 +128,8 @@ main (void)
        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);