[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] [PATCH] signals: upon a crash, save changed buffers and res

From: Benno Schulenberg
Subject: [Nano-devel] [PATCH] signals: upon a crash, save changed buffers and reset terminal state
Date: Mon, 30 Apr 2018 17:23:01 +0200

From: Devin Hussey <address@hidden>

Upon a segmentation fault or an abort signal, instead of crashing,
losing all changes, and leaving the terminal in curses mode, nano
now calls die(), to save any changed buffers and to restore the
terminal to a usable state.

For the remote chance that nano segfaults in die(), the handler for
SIGSEGV and for SIGABRT is reset to its default value as soon as the
signal fires, to prevent a crash-handler loop.

Since a core dump is usually more helpful for debugging, the crash
handler is turned off for a debug build.

This addresses

Signed-off-by: Devin Hussey <address@hidden>
 src/nano.c  | 18 ++++++++++++++++++
 src/proto.h |  3 +++
 2 files changed, 21 insertions(+)

diff --git a/src/nano.c b/src/nano.c
index 1ed2e960..ae5f64b8 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1210,6 +1210,16 @@ void signal_init(void)
                sigaction(SIGTSTP, &act, NULL);
+#ifndef DEBUG
+       /* Trap SIGSEGV and SIGABRT to save any changed buffers and
+        * to reset the terminal to a usable state. */
+       act.sa_handler = handle_crash;
+       /* Reset these handlers to their default as soon as they fire. */
+       act.sa_flags |= SA_RESETHAND;
+       sigaction(SIGSEGV, &act, NULL);
+       sigaction(SIGABRT, &act, NULL);
 /* Handler for SIGHUP (hangup) and SIGTERM (terminate). */
@@ -1218,6 +1228,14 @@ RETSIGTYPE handle_hupterm(int signal)
        die(_("Received SIGHUP or SIGTERM\n"));
+#ifndef DEBUG
+/* Handler for SIGSEGV (segfault) and SIGABRT (abort). */
+RETSIGTYPE handle_crash(int signal)
+       die(_("Sorry! Nano crashed!  Code: %d.  Please report a bug.\n"), 
 /* Handler for SIGTSTP (suspend). */
 RETSIGTYPE do_suspend(int signal)
diff --git a/src/proto.h b/src/proto.h
index f2c51f93..440af6a0 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -426,6 +426,9 @@ void window_init(void);
 void do_exit(void);
 void close_and_go(void);
 RETSIGTYPE handle_hupterm(int signal);
+#ifndef DEBUG
+RETSIGTYPE handle_crash(int signal);
 RETSIGTYPE do_suspend(int signal);
 RETSIGTYPE do_continue(int signal);
 #ifndef NANO_TINY

reply via email to

[Prev in Thread] Current Thread [Next in Thread]