bash: add initial version for debian

Add bash 5.1-2 of debian packaging.

Story: 2009221
Task: 43330
Signed-off-by: Li Zhou <li.zhou@windriver.com>
Change-Id: I2cfae24093cad028448eb82303c84c93e9592b21
This commit is contained in:
Li Zhou 2021-09-15 22:30:07 -04:00
parent 1b48d401a6
commit 57f54a92e9
4 changed files with 471 additions and 0 deletions

@ -0,0 +1,7 @@
---
debver: 5.1-2
debname: bash
archive: https://snapshot.debian.org/archive/debian/20210103T144403Z/pool/main/b/bash/
revision:
dist: $STX_DIST
PKG_GITREVCOUNT: true

@ -0,0 +1,354 @@
From ceead94286b655142022d6341b084021f8729b07 Mon Sep 17 00:00:00 2001
From: Li Zhou <li.zhou@windriver.com>
Date: Mon, 13 Sep 2021 11:25:28 +0800
Subject: [PATCH 1/2] WRS: Patch500: bash-history-syslog.patch
[lz: adapted the patch to fix context changes caused by
bash version changes.]
Signed-off-by: Li Zhou <li.zhou@windriver.com>
---
bashhist.c | 98 +++++++++++++++++++++++++++------------------
config-top.h | 2 +-
shell.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 171 insertions(+), 39 deletions(-)
diff --git a/bashhist.c b/bashhist.c
index 2a05a53..e18e93b 100644
--- a/bashhist.c
+++ b/bashhist.c
@@ -38,10 +38,6 @@
#include "bashintl.h"
-#if defined (SYSLOG_HISTORY)
-# include <syslog.h>
-#endif
-
#include "shell.h"
#include "flags.h"
#include "parser.h"
@@ -55,6 +51,10 @@
#include <glob/glob.h>
#include <glob/strmatch.h>
+#if defined (SYSLOG_HISTORY)
+#include <sys/socket.h>
+#endif
+
#if defined (READLINE)
# include "bashline.h"
extern int rl_done, rl_dispatching; /* should really include readline.h */
@@ -73,6 +73,12 @@ static int check_history_control PARAMS((char *));
static void hc_erasedups PARAMS((char *));
static void really_add_history PARAMS((char *));
+
+#if defined (SYSLOG_HISTORY)
+static void send_cmdline_mq PARAMS((const char *));
+int cmdline_hist_sock[2];
+#endif
+
static struct ignorevar histignore =
{
"HISTIGNORE",
@@ -806,7 +812,6 @@ check_add_history (line, force)
#if defined (SYSLOG_HISTORY)
#define SYSLOG_MAXMSG 1024
-#define SYSLOG_MAXLEN SYSLOG_MAXMSG
#define SYSLOG_MAXHDR 256
#ifndef OPENLOG_OPTS
@@ -823,38 +828,7 @@ void
bash_syslog_history (line)
const char *line;
{
- char trunc[SYSLOG_MAXLEN], *msg;
- char loghdr[SYSLOG_MAXHDR];
- char seqbuf[32], *seqnum;
- int hdrlen, msglen, seqlen, chunks, i;
- static int first = 1;
-
- if (first)
- {
- openlog (shell_name, OPENLOG_OPTS, SYSLOG_FACILITY);
- first = 0;
- }
-
- hdrlen = snprintf (loghdr, sizeof(loghdr), "HISTORY: PID=%d UID=%d", getpid(), current_user.uid);
- msglen = strlen (line);
-
- if ((msglen + hdrlen + 1) < SYSLOG_MAXLEN)
- syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "%s %s", loghdr, line);
- else
- {
- chunks = ((msglen + hdrlen) / SYSLOG_MAXLEN) + 1;
- for (msg = line, i = 0; i < chunks; i++)
- {
- seqnum = inttostr (i + 1, seqbuf, sizeof (seqbuf));
- seqlen = STRLEN (seqnum);
-
- /* 7 == "(seq=) " */
- strncpy (trunc, msg, SYSLOG_MAXLEN - hdrlen - seqlen - 7 - 1);
- trunc[SYSLOG_MAXLEN - 1] = '\0';
- syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "%s (seq=%s) %s", loghdr, seqnum, trunc);
- msg += SYSLOG_MAXLEN - hdrlen - seqlen - 8;
- }
- }
+ send_cmdline_mq (line);
}
#endif
@@ -930,6 +904,10 @@ bash_add_history (line)
sprintf (new_line, "%s%s%s", current->line, chars_to_add, line);
offset = where_history ();
old = replace_history_entry (offset, new_line, current->data);
+
+#if defined (SYSLOG_HISTORY)
+ bash_syslog_history (new_line);
+#endif
free (new_line);
if (old)
@@ -943,12 +921,13 @@ bash_add_history (line)
add_it = 0;
if (add_it)
+ {
really_add_history (line);
#if defined (SYSLOG_HISTORY)
- if (syslog_history)
bash_syslog_history (line);
#endif
+ }
using_history ();
}
@@ -1071,4 +1050,47 @@ history_should_ignore (line)
return match;
}
+
+#if defined (SYSLOG_HISTORY)
+
+#define MQ_SEND_MAX_ATTEMPT 2
+
+static void
+send_cmdline_mq (line)
+ const char *line;
+{
+ int ret = 0;
+ int attempt = 0;
+ int fail = 0;
+ size_t len_snd;
+ int sndbuf_size;
+
+ do {
+ if (attempt >= MQ_SEND_MAX_ATTEMPT)
+ {
+ fail = 1;
+ break;
+ }
+
+ len_snd = strlen(line)+1;
+ ret = send (cmdline_hist_sock[0], line, len_snd, MSG_DONTWAIT);
+ if (ret == EMSGSIZE)
+ {
+ socklen_t optlen = sizeof(sndbuf_size);
+ ret = getsockopt(cmdline_hist_sock[0], SOL_SOCKET, SO_SNDBUF,
+ &sndbuf_size, &optlen);
+ if (ret == 0) {
+ len_snd = sndbuf_size - 1;
+ continue;
+ }
+ }
+
+ attempt ++;
+ } while (ret < 0 && (errno == EAGAIN || errno == EINTR));
+
+ return;
+}
+
+#endif /*SYSLOG_HISTORY*/
+
#endif /* HISTORY */
diff --git a/config-top.h b/config-top.h
index abffe0c..6e48778 100644
--- a/config-top.h
+++ b/config-top.h
@@ -119,7 +119,7 @@
/* Define if you want each line saved to the history list in bashhist.c:
bash_add_history() to be sent to syslog(). */
-/* #define SYSLOG_HISTORY */
+#define SYSLOG_HISTORY
#if defined (SYSLOG_HISTORY)
# define SYSLOG_FACILITY LOG_USER
# define SYSLOG_LEVEL LOG_INFO
diff --git a/shell.c b/shell.c
index ce8087f..0da3fc9 100644
--- a/shell.c
+++ b/shell.c
@@ -79,6 +79,12 @@ extern int get_tty_state PARAMS((void));
# include <readline/history.h>
#endif
+#if defined (SYSLOG_HISTORY)
+# include <syslog.h>
+# include <sys/socket.h>
+# include "error.h"
+#endif
+
#if defined (READLINE)
# include <readline/readline.h>
# include "bashline.h"
@@ -104,6 +110,9 @@ extern char **environ; /* used if no third argument to main() */
#endif
extern int gnu_error_format;
+#if defined (SYSLOG_HISTORY)
+extern int cmdline_hist_sock[2];
+#endif
/* Non-zero means that this shell has already been run; i.e. you should
call shell_reinitialize () if you need to start afresh. */
@@ -337,6 +346,10 @@ static void shell_reinitialize PARAMS((void));
static void show_shell_usage PARAMS((FILE *, int));
+#if defined (SYSLOG_HISTORY)
+static pid_t make_consumer_process PARAMS(());
+#endif
+
#ifdef __CYGWIN__
static void
_cygwin32_check_tmp ()
@@ -379,6 +392,11 @@ main (argc, argv, env)
env = environ;
#endif /* __OPENNT */
+
+#if defined (SYSLOG_HISTORY)
+ pid_t con_pid;
+#endif
+
USE_VAR(argc);
USE_VAR(argv);
USE_VAR(env);
@@ -791,6 +809,11 @@ main (argc, argv, env)
/* Initialize terminal state for interactive shells after the
.bash_profile and .bashrc are interpreted. */
get_tty_state ();
+
+#if defined (SYSLOG_HISTORY)
+ /*fork a child for bash history logging consumption*/
+ con_pid = make_consumer_process ();
+#endif
}
#if !defined (ONESHOT)
@@ -809,6 +832,13 @@ main (argc, argv, env)
/* Read commands until exit condition. */
reader_loop ();
+
+#if defined (SYSLOG_HISTORY)
+ if (interactive_shell && con_pid > 0) {
+ kill(con_pid, SIGKILL);
+ }
+#endif
+
exit_shell (last_command_exit_value);
}
@@ -1787,6 +1817,86 @@ set_shell_name (argv0)
shell_name = PROGRAM;
}
+
+#if defined (SYSLOG_HISTORY)
+#define SYSLOG_MAXLEN 1200
+
+/* Fork child process for bash history logging, handling errors.
+ Returns the pid of the newly made child in parent process context
+ and will not return in child process context. */
+static pid_t
+make_consumer_process ()
+{
+ pid_t pid;
+
+ if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, cmdline_hist_sock) == -1)
+ {
+ return -1;
+ }
+
+
+ pid = fork ();
+ if (pid < 0)
+ {
+ return -1;
+ }
+
+ if (pid == 0)
+ {
+ int ret;
+ char *buffer;
+ int rcvbuf_size;
+ socklen_t optlen = sizeof(rcvbuf_size);
+
+#if defined (BUFFERED_INPUT)
+ unset_bash_input (0);
+#endif /* BUFFERED_INPUT */
+
+ default_tty_job_signals ();
+
+ close(cmdline_hist_sock[0]);
+
+ ret = getsockopt(cmdline_hist_sock[1], SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, &optlen);
+ if (ret < 0)
+ {
+ rcvbuf_size = SYSLOG_MAXLEN;
+ }
+
+ buffer = (char *) malloc(rcvbuf_size);
+ if (buffer == NULL)
+ {
+ return -1;
+ }
+
+ do {
+ ret = recv(cmdline_hist_sock[1], buffer, rcvbuf_size, 0);
+
+ if (ret == -1 && errno == EINTR)
+ {
+ continue;
+ }
+ else if (ret < 0)
+ {
+ break;
+ }
+
+ buffer[ret] = '\0';
+
+ /*syslog this bash command line*/
+ syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY: PID=%d UID=%d %s",
+ getpid(), current_user.uid, buffer);
+ } while (1);
+
+ }
+ else
+ {
+ /* In the parent. */
+ close(cmdline_hist_sock[1]);
+ }
+ return (pid);
+}
+#endif /*SYSLOG_HISTORY*/
+
/* Some options are initialized to -1 so we have a way to determine whether
they were set on the command line. This is an issue when listing the option
values at invocation (`bash -o'), so we set the defaults here and reset
--
2.30.2

@ -0,0 +1,108 @@
From 7f792deebbdcecb1b18609aa4e7c9c856b235f7b Mon Sep 17 00:00:00 2001
From: Li Zhou <li.zhou@windriver.com>
Date: Mon, 13 Sep 2021 11:43:52 +0800
Subject: [PATCH 2/2] WRS: Patch501:
bash-history-exit-child-on-parent-death.patch
[lz: adapted the patch to fix context changes caused by
bash version changes.]
Signed-off-by: Li Zhou <li.zhou@windriver.com>
---
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 0da3fc9..52b9718 100644
--- a/shell.c
+++ b/shell.c
@@ -82,6 +82,7 @@ extern int get_tty_state PARAMS((void));
#if defined (SYSLOG_HISTORY)
# include <syslog.h>
# include <sys/socket.h>
+# include <sys/prctl.h>
# include "error.h"
#endif
@@ -347,6 +348,7 @@ static void shell_reinitialize PARAMS((void));
static void show_shell_usage PARAMS((FILE *, int));
#if defined (SYSLOG_HISTORY)
+int logger_terminated = 0;
static pid_t make_consumer_process PARAMS(());
#endif
@@ -1854,6 +1856,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);
@@ -1885,7 +1897,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 6964d86..4903272 100644
--- a/sig.c
+++ b/sig.c
@@ -644,6 +644,19 @@ termsig_handler (sig)
}
#undef XSIG
+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 a564e3c..bbfe038 100644
--- a/sig.h
+++ b/sig.h
@@ -111,11 +111,13 @@ extern volatile sig_atomic_t sigterm_received;
extern int interrupt_immediately; /* no longer used */
extern int terminate_immediately;
+extern int logger_terminated;
/* Functions from sig.c. */
extern sighandler termsig_sighandler PARAMS((int));
extern void termsig_handler PARAMS((int));
extern sighandler sigint_sighandler PARAMS((int));
+extern sighandler sigterm_logger_sighandler PARAMS((int));
extern void initialize_signals PARAMS((int));
extern void initialize_terminating_signals PARAMS((void));
extern void reset_terminating_signals PARAMS((void));
--
2.30.2

@ -0,0 +1,2 @@
0001-WRS-Patch500-bash-history-syslog.patch.patch
0002-WRS-Patch501-bash-history-exit-child-on-parent-death.patch