On Tue, Mar 08, 2011 at 11:29:22AM +0100, Werner Fink wrote:
> On Mon, Mar 07, 2011 at 10:22:55PM -0500, Chet Ramey wrote:
> > 
> > One thing that has changed is that an interactive shell will no longer
> > attempt to write the history file if it's killed by a signal, since that
> > causes many functions to be executed that are not safe to call from a
> > signal handler.  If you're in the habit of trying to exit the shell by
> > closing the terminal window, which causes the shell to be killed by SIGHUP
> > (I think, maybe SIGTERM), the history will not be saved.
> 
> That will cause a lot of bugreports.  Just a question: could an atexit()
> handler an option or an other way the bash could make use use an extra
> (sig)longjmp()/(sig)setjmp() after increasing an atomic control variable
> in the signal handlers for SIGHUP/SIGTERM to run all things done short
> before exit.

Something like the attached patch.

Werner

-- 
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr
--- config.h.in
+++ config.h.in	2011-03-08 11:18:00.139925937 +0000
@@ -530,6 +530,9 @@
 /* Define if you have the asprintf function.  */
 #undef HAVE_ASPRINTF
 
+/* Define if you have the ATEXIT function.  */
+#undef HAVE_ATEXIT
+
 /* Define if you have the bcopy function.  */
 #undef HAVE_BCOPY
 
--- configure
+++ configure	2011-03-08 11:20:57.780426177 +0000
@@ -13497,7 +13497,7 @@ done
 
 
 
-for ac_func in bcopy bzero confstr faccessat fnmatch \
+for ac_func in atexit bcopy bzero confstr faccessat fnmatch \
 		getaddrinfo gethostbyname getservbyname getservent inet_aton \
 		memmove pathconf putenv raise regcomp regexec \
 		setenv setlinebuf setlocale setvbuf siginterrupt strchr \
--- configure.in
+++ configure.in	2011-03-08 11:20:46.020641745 +0000
@@ -727,7 +727,7 @@ AC_CHECK_FUNCS(dup2 eaccess fcntl getdta
 AC_REPLACE_FUNCS(rename)
 
 dnl checks for c library functions
-AC_CHECK_FUNCS(bcopy bzero confstr faccessat fnmatch \
+AC_CHECK_FUNCS(atexit bcopy bzero confstr faccessat fnmatch \
 		getaddrinfo gethostbyname getservbyname getservent inet_aton \
 		memmove pathconf putenv raise regcomp regexec \
 		setenv setlinebuf setlocale setvbuf siginterrupt strchr \
--- sig.c
+++ sig.c	2011-03-08 11:32:29.067927069 +0000
@@ -292,6 +292,11 @@ initialize_terminating_signals ()
 
 #endif /* !HAVE_POSIX_SIGNALS */
 
+#if defined (HISTORY) && defined (HAVE_ATEXIT)
+  if (interactive_shell)
+    atexit(maybe_save_shell_history);
+#endif /* HISTORY && HAVE_ATEXIT */
+
   termsigs_initialized = 1;
 }
 
@@ -504,8 +509,11 @@ termsig_sighandler (sig)
   if (terminate_immediately)
     {
 #if defined (HISTORY)
-      /* XXX - will inhibit history file being written */
-      history_lines_this_session = 0;
+# if defined (HAVE_ATEXIT)
+      if (sig == SIGABRT)
+# endif
+	/* XXX - will inhibit history file being written */
+	history_lines_this_session = 0;
 #endif
       terminate_immediately = 0;
       termsig_handler (sig);
@@ -532,10 +540,10 @@ termsig_handler (sig)
   if (sig == SIGINT && signal_is_trapped (SIGINT))
     run_interrupt_trap ();
 
-#if defined (HISTORY)
+#if defined (HISTORY) && ! defined (HAVE_ATEXIT)
   if (interactive_shell && sig != SIGABRT)
     maybe_save_shell_history ();
-#endif /* HISTORY */
+#endif /* HISTORY && ! HAVE_ATEXIT */
 
 #if defined (JOB_CONTROL)
   if (sig == SIGHUP && (interactive || (subshell_environment & (SUBSHELL_COMSUB|SUBSHELL_PROCSUB))))

Reply via email to