[PATCH] ash: Write history on SIGHUP

Tomi Leppänen tomi.leppanen at jolla.com
Fri Feb 12 15:51:46 UTC 2021


If FEATURE_EDITING_SAVE_ON_EXIT is enabled, then write history on
SIGHUP. This should allow most terminals to save history when closing
window. Works also on SSH connection.

function                                             old     new   delta
signal_handler                                        85     133     +48
setsignal                                            324     335     +11
optschanged                                          123     133     +10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 69/0)               Total: 69 bytes

Signed-off-by: Tomi Leppänen <tomi.leppanen at jolla.com>
---

We use busybox ash as default shell. Currently it doesn't save history
when Fingerterm (terminal application) or ssh connection is closed. With
this patch the history is saved when interactive session is closed by
closing the app or closing ssh session.

Bash has code to save history also when SIGTERM is received but I feel
that is best added separately if needed at all. Also I'm not totally
sure if this should save history when there is trap set for SIGHUP.

I'm not very familiar with the code and I tried to preserve any existing
behaviour. I would like to receive feedback on this patch and as usual I
aim to get it included in busybox if possible.

Thanks in advance!

 shell/ash.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/shell/ash.c b/shell/ash.c
index fabecdfe0..97ecd67b4 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -3626,6 +3626,19 @@ signal_handler(int signo)
 			return;
 	}
 
+#if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
+	if (signo == SIGHUP) {
+		/*
+		 * Terminal was closed, call exitshell() to write history etc.
+		 * unless there is a trap set, in which case just save the history
+		 */
+		if (!trap[SIGHUP]) {
+			exitshell();
+		} else if (iflag && line_input_state)
+			save_history(line_input_state);
+	}
+#endif
+
 	gotsig[signo - 1] = 1;
 	pending_sig = signo;
 
@@ -3698,6 +3711,14 @@ setsignal(int signo)
 	if (signo == SIGCHLD)
 		new_act = S_CATCH;
 
+#if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
+	/*
+	 * Catch SIGHUP to write history on terminal close
+	 */
+	if (signo == SIGHUP && iflag)
+		new_act = S_CATCH;
+#endif
+
 	t = &sigmode[signo - 1];
 	cur_act = *t;
 	if (cur_act == 0) {
@@ -9545,6 +9566,7 @@ setinteractive(int on)
 	setsignal(SIGINT);
 	setsignal(SIGQUIT);
 	setsignal(SIGTERM);
+	setsignal(SIGHUP);
 	if (is_interactive > 1) {
 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
 		/* Looks like they want an interactive shell */
-- 
2.29.2



More information about the busybox mailing list