summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorDaiki Ueno <dueno@redhat.com>2017-06-12 11:09:43 +0200
committerDaiki Ueno <ueno@gnu.org>2017-06-12 11:30:42 +0200
commitefe6dc56c3951c301dda1b548d4cbcd02e074462 (patch)
tree95bf26274ba4208b8d6e7af600ebffa104660387 /common
parentbf3c1a9d8e4ace4c3a92b4af56e4b62657907522 (diff)
debug: Add p11_debug_err to prevent use of strerror
Diffstat (limited to 'common')
-rw-r--r--common/debug.c35
-rw-r--r--common/debug.h15
2 files changed, 48 insertions, 2 deletions
diff --git a/common/debug.c b/common/debug.c
index 47933fa..5f7546e 100644
--- a/common/debug.c
+++ b/common/debug.c
@@ -40,12 +40,17 @@
#include "debug.h"
#include <assert.h>
+#ifdef HAVE_LOCALE_H
+#include <locale.h>
+#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#define P11_DEBUG_MESSAGE_MAX 512
+
struct DebugKey {
const char *name;
int value;
@@ -140,6 +145,36 @@ p11_debug_message (int flag,
}
void
+p11_debug_message_err (int flag,
+ int errnum,
+ const char *format, ...)
+{
+ 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());
+ va_start (args, format);
+ vfprintf (stderr, format, args);
+ 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));
+#else
+ strerror_r (errnum, strerr, sizeof (strerr));
+#endif
+ strerr[P11_DEBUG_MESSAGE_MAX - 1] = 0;
+ fprintf (stderr, ": %s\n", strerr);
+ }
+}
+
+void
p11_debug_precond (const char *format,
...)
{
diff --git a/common/debug.h b/common/debug.h
index 6106f19..4bf7e78 100644
--- a/common/debug.h
+++ b/common/debug.h
@@ -56,6 +56,11 @@ void p11_debug_message (int flag,
const char *format,
...) GNUC_PRINTF (2, 3);
+void p11_debug_message_err (int flag,
+ int errnum,
+ const char *format,
+ ...) GNUC_PRINTF (3, 4);
+
void p11_debug_precond (const char *format,
...) GNUC_PRINTF (1, 2)
CLANG_ANALYZER_NORETURN;
@@ -127,14 +132,20 @@ void p11_debug_precond (const char *format,
p11_debug_message (P11_DEBUG_FLAG, "%s: " format, __PRETTY_FUNCTION__, ##__VA_ARGS__); \
} while (0)
+#undef p11_debug_err
+#define p11_debug_err(errnum, format, ...) do { \
+ if (P11_DEBUG_FLAG & p11_debug_current_flags) \
+ p11_debug_message_err (P11_DEBUG_FLAG, errnum, "%s: " format, __PRETTY_FUNCTION__, ##__VA_ARGS__); \
+ } while (0)
+
#undef p11_debugging
#define p11_debugging \
(P11_DEBUG_FLAG & p11_debug_current_flags)
#else /* !defined (WITH_DEBUG) */
-#undef p11_debug
-#define p11_debug(format, ...) \
+#undef p11_debug_err
+#define p11_debug_err(errnum, format, ...) \
do {} while (false)
#undef p11_debugging