[PATCH] Log message before aborting

Mike Crute mike at crute.us
Thu Jan 12 05:02:23 CET 2023


Signed-off-by: Mike Crute <mike at crute.us>
---

On Thu, Jan 12, 2023 at 03:32:52AM +0100, Ondrej Zajicek wrote:
> That would work (with some minor modifications - abort() should be out 
> of condition, dbgf is FILE *, not fd, fileno() is not async-safe, so 
> we would need keep dbg_fd).
>
> The disadantage is it would not write to log file, but only to debug
> output (enabled with -d / -D option). If that is acceptable to you,
> i would apply necessary changes.

I think it's okay that this only prints to the debug log file/stderr. I 
think a person would try debug mode first when they encouter a crash 
before doing more invasive debugging.

Here's a revised patch that incorporates your notes. Thanks!


  sysdep/unix/io.c  |  2 +-
  sysdep/unix/log.c | 25 +++++++++++++++++++++++++
  2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c
index 810e782d..06930fd7 100644
--- a/sysdep/unix/io.c
+++ b/sysdep/unix/io.c
@@ -2129,7 +2129,7 @@ watchdog_sigalrm(int sig UNUSED)
    io_update_time();
  
    /* We want core dump */
-  abort();
+  watchdog_debug("Watchdog timer timed out.\n");
  }
  
  static inline void
diff --git a/sysdep/unix/log.c b/sysdep/unix/log.c
index 4e9df069..54614143 100644
--- a/sysdep/unix/log.c
+++ b/sysdep/unix/log.c
@@ -31,6 +31,7 @@
  #include "lib/lists.h"
  #include "sysdep/unix/unix.h"
  
+static int dbg_fd;
  static FILE *dbgf;
  static list *current_log_list;
  static char *current_syslog_name; /* NULL -> syslog closed */
@@ -324,6 +325,24 @@ debug(const char *msg, ...)
    va_end(args);
  }
  
+/**
+ * watchdog_debug - async-safe write to debug output
+ * @msg: a string message
+ *
+ * This function prints the message @msg to the debugging output in a
+ * way that is async safe and can be used in signal handlers. No newline
+ * character is appended. It terminates the program after writing.
+ */
+void
+watchdog_debug(const char *msg)
+{
+  if (dbgf)
+    {
+      write(dbg_fd, msg, strlen(msg));
+    }
+  abort();
+}
+
  static list *
  default_log_list(int initial, const char **syslog_name)
  {
@@ -427,7 +446,10 @@ log_init_debug(char *f)
    if (!f)
      dbgf = NULL;
    else if (!*f)
+  {
      dbgf = stderr;
+    dbg_fd = STDERR_FILENO;
+  }
    else if (!(dbgf = fopen(f, "a")))
    {
      /* Cannot use die() nor log() here, logging is not yet initialized */
@@ -435,5 +457,8 @@ log_init_debug(char *f)
      exit(1);
    }
    if (dbgf)
+  {
      setvbuf(dbgf, NULL, _IONBF, 0);
+    dbg_fd = fileno(dbgf);
+  }
  }
-- 
2.39.0



More information about the Bird-users mailing list