gplv2/bash/files/bash-history-exit-child-on-...

105 lines
2.7 KiB
Diff

From e3e273f70ea4f8b33f89478020a421bdc203666e Mon Sep 17 00:00:00 2001
From: Scott Little <scott.little@windriver.com>
Date: Fri, 26 Aug 2016 16:04:48 -0400
Subject: [PATCH 2/2] WRS: Patch501:
bash-history-exit-child-on-parent-death.patch
---
shell.c | 16 +-
sig.c | 13 +
sig.h | 2 +
3 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/shell.c b/shell.c
index 7eca4e3..dcba61a 100644
--- a/shell.c
+++ b/shell.c
@@ -75,6 +75,7 @@
#if defined (SYSLOG_HISTORY)
# include <syslog.h>
# include <sys/socket.h>
+# include <sys/prctl.h>
# include "error.h"
#endif
@@ -338,6 +339,7 @@ static void shell_reinitialize __P((void));
static void show_shell_usage __P((FILE *, int));
#if defined (SYSLOG_HISTORY)
+int logger_terminated = 0;
static pid_t make_consumer_process __P(());
#endif
@@ -1687,6 +1689,16 @@ make_consumer_process ()
default_tty_job_signals ();
+ /* handle parent process deaths */
+ set_signal_handler(SIGTERM, sigterm_logger_sighandler);
+ prctl(PR_SET_PDEATHSIG, SIGTERM);
+
+ if (getppid() == 1)
+ {
+ /* parent has already died */
+ exit (0);
+ }
+
close(cmdline_hist_sock[0]);
ret = getsockopt(cmdline_hist_sock[1], SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, &optlen);
@@ -1718,7 +1730,9 @@ make_consumer_process ()
/*syslog this bash command line*/
syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY: PID=%d UID=%d %s",
getpid(), current_user.uid, buffer);
- } while (1);
+ } while (!logger_terminated);
+
+ exit(0);
}
else
diff --git a/sig.c b/sig.c
index d38246d..5ad0c2d 100644
--- a/sig.c
+++ b/sig.c
@@ -561,6 +561,19 @@ termsig_handler (sig)
kill (getpid (), sig);
}
+sighandler
+sigterm_logger_sighandler (sig)
+ int sig;
+{
+#if defined (MUST_REINSTALL_SIGHANDLERS)
+ signal (sig, sigterm_logger_sighandler);
+#endif
+
+ logger_terminated = 1;
+
+ SIGRETURN (0);
+}
+
/* What we really do when SIGINT occurs. */
sighandler
sigint_sighandler (sig)
diff --git a/sig.h b/sig.h
index 540aa3e..8f47c56 100644
--- a/sig.h
+++ b/sig.h
@@ -115,11 +115,13 @@ extern volatile int sigwinch_received;
extern int interrupt_immediately;
extern int terminate_immediately;
+extern int logger_terminated;
/* Functions from sig.c. */
extern sighandler termsig_sighandler __P((int));
extern void termsig_handler __P((int));
extern sighandler sigint_sighandler __P((int));
+extern sighandler sigterm_logger_sighandler __P((int));
extern void initialize_signals __P((int));
extern void initialize_terminating_signals __P((void));
extern void reset_terminating_signals __P((void));
--
1.9.1