diff options
author | Daiki Ueno <dueno@redhat.com> | 2018-04-27 10:00:52 +0200 |
---|---|---|
committer | Daiki Ueno <ueno@gnu.org> | 2018-05-01 13:30:43 +0200 |
commit | a625dfa4f2456b1a866489e5be15fb46578237a5 (patch) | |
tree | d0959e03eeeec6d331aa0451fe6f9199f70eb4b3 | |
parent | 6202903b261dfae740af3f8e985244bab48470ba (diff) |
library: Use dedicated locale object for printing error
-rw-r--r-- | common/debug.c | 14 | ||||
-rw-r--r-- | common/library.c | 13 | ||||
-rw-r--r-- | common/message.c | 14 | ||||
-rw-r--r-- | common/test-message.c | 15 | ||||
-rw-r--r-- | configure.ac | 2 |
5 files changed, 43 insertions, 15 deletions
diff --git a/common/debug.c b/common/debug.c index 5f7546e..c731305 100644 --- a/common/debug.c +++ b/common/debug.c @@ -73,6 +73,10 @@ static bool debug_strict = false; /* global variable exported in debug.h */ int p11_debug_current_flags = ~0; +#ifdef HAVE_LOCALE_H +extern locale_t p11_message_locale; +#endif + static int parse_environ_flags (void) { @@ -151,9 +155,6 @@ p11_debug_message_err (int flag, { va_list args; char strerr[P11_DEBUG_MESSAGE_MAX]; -#ifdef HAVE_STRERROR_L - locale_t loc; -#endif if (flag & p11_debug_current_flags) { fprintf (stderr, "(p11-kit:%d) ", getpid()); @@ -162,10 +163,9 @@ p11_debug_message_err (int flag, va_end (args); snprintf (strerr, sizeof (strerr), "Unknown error %d", errnum); -#ifdef HAVE_STRERROR_L - loc = uselocale ((locale_t) 0); - if (loc != NULL) - strncpy (strerr, strerror_l (errnum, loc), sizeof (strerr)); +#if defined(HAVE_STRERROR_L) && defined(HAVE_NEWLOCALE) + if (p11_message_locale != (locale_t) 0) + strncpy (strerr, strerror_l (errnum, p11_message_locale), sizeof (strerr)); #else strerror_r (errnum, strerr, sizeof (strerr)); #endif diff --git a/common/library.c b/common/library.c index 37cb1c8..52c3347 100644 --- a/common/library.c +++ b/common/library.c @@ -44,6 +44,9 @@ #include "message.h" #include <assert.h> +#ifdef HAVE_LOCALE_H +#include <locale.h> +#endif #include <stdarg.h> #include <stdlib.h> #include <stdio.h> @@ -67,6 +70,10 @@ pthread_once_t p11_library_once = PTHREAD_ONCE_INIT; unsigned int p11_forkid = 1; +#ifdef HAVE_LOCALE_H +extern locale_t p11_message_locale; +#endif + static char * thread_local_message (void) { @@ -123,6 +130,9 @@ p11_library_init_impl (void) p11_mutex_init (&p11_virtual_mutex); pthread_key_create (&thread_local, free); p11_message_storage = thread_local_message; +#ifdef HAVE_NEWLOCALE + p11_message_locale = newlocale (LC_ALL_MASK, "POSIX", (locale_t) 0); +#endif pthread_atfork (NULL, NULL, count_forks); } @@ -142,6 +152,9 @@ p11_library_uninit (void) free (pthread_getspecific (thread_local)); pthread_setspecific (thread_local, NULL); +#ifdef HAVE_NEWLOCALE + freelocale (p11_message_locale); +#endif p11_message_storage = dont_store_message; pthread_key_delete (thread_local); p11_mutex_uninit (&p11_virtual_mutex); diff --git a/common/message.c b/common/message.c index 34e0d5b..e4e00d5 100644 --- a/common/message.c +++ b/common/message.c @@ -60,6 +60,10 @@ static bool print_messages = false; +#ifdef HAVE_LOCALE_H +locale_t p11_message_locale = (locale_t) 0; +#endif + static char * default_message_storage (void) { @@ -104,9 +108,6 @@ p11_message_err (int errnum, char strerr[P11_MESSAGE_MAX]; va_list va; size_t length; -#ifdef HAVE_STRERROR_L - locale_t loc; -#endif va_start (va, msg); length = vsnprintf (buffer, P11_MESSAGE_MAX - 1, msg, va); @@ -118,10 +119,9 @@ p11_message_err (int errnum, buffer[length] = 0; snprintf (strerr, sizeof (strerr), "Unknown error %d", errnum); -#ifdef HAVE_STRERROR_L - loc = uselocale ((locale_t) 0); - if (loc != NULL) - strncpy (strerr, strerror_l (errnum, loc), sizeof (strerr)); +#if defined(HAVE_STRERROR_L) && defined(HAVE_NEWLOCALE) + if (p11_message_locale != (locale_t) 0) + strncpy (strerr, strerror_l (errnum, p11_message_locale), sizeof (strerr)); #else strerror_r (errnum, strerr, sizeof (strerr)); #endif diff --git a/common/test-message.c b/common/test-message.c index 7dd5426..339ad4b 100644 --- a/common/test-message.c +++ b/common/test-message.c @@ -38,15 +38,26 @@ #include "message.h" #include <errno.h> +#ifdef HAVE_LOCALE_H +#include <locale.h> +#endif #include <stdlib.h> #include <stdio.h> +#ifdef HAVE_LOCALE_H +extern locale_t p11_message_locale; +#endif + static void test_with_err (void) { const char *last; char *expected; +#ifdef HAVE_NEWLOCALE + p11_message_locale = newlocale (LC_ALL_MASK, "POSIX", (locale_t) 0); +#endif + errno = E2BIG; p11_message_err (ENOENT, "Details: %s", "value"); last = p11_message_last (); @@ -55,6 +66,10 @@ test_with_err (void) assert_not_reached (); assert_str_eq (expected, last); free (expected); + +#ifdef HAVE_NEWLOCALE + freelocale (p11_message_locale); +#endif } int diff --git a/configure.ac b/configure.ac index 7fcbe01..c4db748 100644 --- a/configure.ac +++ b/configure.ac @@ -101,7 +101,7 @@ if test "$os_unix" = "yes"; then AC_CHECK_MEMBERS([struct dirent.d_type],,,[#include <dirent.h>]) AC_CHECK_FUNCS([getprogname getexecname basename mkstemp mkdtemp]) AC_CHECK_FUNCS([getauxval issetugid getresuid secure_getenv]) - AC_CHECK_FUNCS([strnstr memdup strndup strerror_l strerror_r]) + AC_CHECK_FUNCS([strnstr memdup newlocale strndup strerror_l strerror_r]) AC_CHECK_FUNCS([reallocarray]) AC_CHECK_FUNCS([fdwalk]) AC_CHECK_FUNCS([setenv]) |