[PATCH] ash: Write history on SIGHUP

Tomi Leppänen tomi.leppanen at jolla.com
Tue Feb 16 13:35:26 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.

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 as ash
is not handling SIGTERM at all in interactive mode and I tried to
preserve any existing behaviour. Therefore I think that would be best
added separately if it is needed at all.

Also I'm not totally sure if this should save history when there is trap
set for SIGHUP but I added that too.

Thanks in advance!

Changelog:
v2:
 - Updated the patch against master (previous was 1_31_0 + patches)
 - Fixed some coding style issues
 - Removed line_input_state check as it's not used anymore in master

Also updated bloat-o-meter, default config + FEATURE_EDITING_SAVE_ON_EXIT,
different compiler than last time:

function                                             old     new   delta
signal_handler                                        83     127     +44
setsignal                                            344     360     +16
optschanged                                          120     130     +10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 70/0)               Total: 70 bytes

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

diff --git a/shell/ash.c b/shell/ash.c
index 1aead6df4..bfe318a5b 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -3662,6 +3662,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)
+			save_history(line_input_state); /* may be NULL */
+	}
+#endif
+
 	gotsig[signo - 1] = 1;
 	pending_sig = signo;
 
@@ -3734,6 +3747,12 @@ 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) {
@@ -9584,6 +9603,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