summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2007-04-20 18:59:37 +0200
committerWerner Koch <wk@gnupg.org>2007-04-20 18:59:37 +0200
commitb89d98e3353f6101bc256476c0269f48a92d3f56 (patch)
tree6da6c9dd9e7b78f97ae6e89e54e6603f314cc644 /common
parent * certcheck.c (do_encode_md): Add arg PKEY. Add support for DSA2 (diff)
downloadgnupg2-b89d98e3353f6101bc256476c0269f48a92d3f56.tar.xz
gnupg2-b89d98e3353f6101bc256476c0269f48a92d3f56.zip
Improved logging for error orginating from libgcrypt.
Diffstat (limited to 'common')
-rw-r--r--common/ChangeLog7
-rw-r--r--common/miscellaneous.c67
-rw-r--r--common/util.h4
3 files changed, 78 insertions, 0 deletions
diff --git a/common/ChangeLog b/common/ChangeLog
index e42bebfd4..ab4bec0a4 100644
--- a/common/ChangeLog
+++ b/common/ChangeLog
@@ -1,3 +1,10 @@
+2007-04-20 Werner Koch <wk@g10code.com>
+
+ * miscellaneous.c (my_gcry_logger, my_gcry_outofcore_handler):
+ Moved from gpg-agent to here.
+ (my_gcry_fatalerror_handler): new.
+ (setup_libgcrypt_logging): New.
+
2007-03-19 Werner Koch <wk@g10code.com>
* miscellaneous.c (print_hexstring): New.
diff --git a/common/miscellaneous.c b/common/miscellaneous.c
index 498c2ab60..fd1861083 100644
--- a/common/miscellaneous.c
+++ b/common/miscellaneous.c
@@ -23,8 +23,75 @@
#include <stdlib.h>
#include <errno.h>
+#define JNLIB_NEED_LOG_LOGV
#include "util.h"
#include "iobuf.h"
+#include "i18n.h"
+
+
+/* Used by libgcrypt for logging. */
+static void
+my_gcry_logger (void *dummy, int level, const char *fmt, va_list arg_ptr)
+{
+ /* Map the log levels. */
+ switch (level)
+ {
+ case GCRY_LOG_CONT: level = JNLIB_LOG_CONT; break;
+ case GCRY_LOG_INFO: level = JNLIB_LOG_INFO; break;
+ case GCRY_LOG_WARN: level = JNLIB_LOG_WARN; break;
+ case GCRY_LOG_ERROR:level = JNLIB_LOG_ERROR; break;
+ case GCRY_LOG_FATAL:level = JNLIB_LOG_FATAL; break;
+ case GCRY_LOG_BUG: level = JNLIB_LOG_BUG; break;
+ case GCRY_LOG_DEBUG:level = JNLIB_LOG_DEBUG; break;
+ default: level = JNLIB_LOG_ERROR; break;
+ }
+ log_logv (level, fmt, arg_ptr);
+}
+
+
+/* This function is called by libgcrypt on a fatal error. */
+static void
+my_gcry_fatalerror_handler (void *opaque, int rc, const char *text)
+{
+ log_fatal ("libgcrypt problem: %s\n", text ? text : gpg_strerror (rc));
+ abort ();
+}
+
+
+/* This function is called by libgcrypt if it ran out of core and
+ there is no way to return that error to the caller. We do our own
+ function here to make use of our logging functions. */
+static int
+my_gcry_outofcore_handler (void *opaque, size_t req_n, unsigned int flags)
+{
+ static int been_here; /* Used to protect against recursive calls. */
+
+ if (!been_here)
+ {
+ been_here = 1;
+ if ( (flags & 1) )
+ log_fatal (_("out of core in secure memory "
+ "while allocating %lu bytes"), (unsigned long)req_n);
+ else
+ log_fatal (_("out of core while allocating %lu bytes"),
+ (unsigned long)req_n);
+ }
+ return 0; /* Let libgcrypt call its own fatal error handler.
+ Actually this will turn out to be
+ my_gcry_fatalerror_handler. */
+}
+
+
+/* Setup libgcrypt to use our own logging functions. Should be used
+ early at startup. */
+void
+setup_libgcrypt_logging (void)
+{
+ gcry_set_log_handler (my_gcry_logger, NULL);
+ gcry_set_fatalerror_handler (my_gcry_fatalerror_handler, NULL);
+ gcry_set_outofcore_handler (my_gcry_outofcore_handler, NULL);
+}
+
/* Decide whether the filename is stdout or a real filename and return
diff --git a/common/util.h b/common/util.h
index 2cf6e6cbe..1fc4d2a1e 100644
--- a/common/util.h
+++ b/common/util.h
@@ -171,6 +171,10 @@ void gnupg_rl_initialize (void);
/*-- miscellaneous.c --*/
+/* This function is called at startup to tell libgcrypt to use our own
+ logging subsystem. */
+void setup_libgcrypt_logging (void);
+
/* Same as asprintf but return an allocated buffer suitable to be
freed using xfree. This function simply dies on memory failure,
thus no extra check is required. */