From 78a93ed89f3c9711cd30838823faa8108dc76d08 Mon Sep 17 00:00:00 2001 From: Mike Drob Date: Tue, 17 May 2022 11:09:15 -0500 Subject: [PATCH 1/3] SOLR-16191: Verify that ps supports -p --- solr/CHANGES.txt | 2 ++ solr/bin/solr | 17 +++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index d389961f58b1..105a456da36d 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -97,6 +97,8 @@ Other Changes * SOLR-16154: Event listeners submit through core container executor service instead of separate thread (Mike Drob, Kevin Risden) +* SOLR-16191: Validate that installed ps utility supports -p flag, so that we do not inadvertantly stop the wrong process. (Mike Drob, Michael Gibney) + Build --------------------- * SOLR-16053: Upgrade scriptDepVersions (Kevin Risden) diff --git a/solr/bin/solr b/solr/bin/solr index c1ef2af1e000..c11fe944c25f 100755 --- a/solr/bin/solr +++ b/solr/bin/solr @@ -61,6 +61,11 @@ if [ "${THIS_OS:0:6}" == "CYGWIN" ]; then echo -e "This script does not support cygwin due to severe limitations and lack of adherence\nto BASH standards, such as lack of lsof, curl, and ps options.\n\nPlease use the native solr.cmd script on Windows!" exit 1 fi +# Alpine Linux BusyBox comes with a stripped down ps, make sure we have a fully featured one +if ! ps -p $$ &> /dev/null ; then + echo -e "This script relies on a version of ps that supports the -p flag.\n\nPlease install a POSIX compliant version and try again." + exit 1 +fi # This helps with debugging when running bats tests but not the whole script is compliant yet # set -u @@ -697,7 +702,7 @@ function solr_pid_by_port() { THE_PORT="$1" if [ -e "$SOLR_PID_DIR/solr-$THE_PORT.pid" ]; then PID=`cat "$SOLR_PID_DIR/solr-$THE_PORT.pid"` - CHECK_PID=`ps -o pid='' $PID | tr -d ' '` + CHECK_PID=`ps -o pid='' -p $PID | tr -d ' '` if [ -n "$CHECK_PID" ]; then echo $PID fi @@ -707,7 +712,7 @@ function solr_pid_by_port() { # extract the value of the -Djetty.port parameter from a running Solr process function jetty_port() { SOLR_PID="$1" - SOLR_PROC=`ps auxww | grep -w $SOLR_PID | grep start\.jar | grep jetty\.port` + SOLR_PROC=`ps -o command='' -p "$SOLR_PID" | grep start\.jar | grep jetty\.port` IFS=' ' read -a proc_args <<< "$SOLR_PROC" for arg in "${proc_args[@]}" do @@ -849,7 +854,7 @@ function stop_solr() { # Check if a process is running with the specified PID. # -o stat will output the STAT, where Z indicates a zombie # stat='' removes the header (--no-headers isn't supported on all platforms) - STAT=`(ps -o stat='' $SOLR_PID || :) | tr -d ' '` + STAT=`(ps -o stat='' -p $SOLR_PID || :) | tr -d ' '` if [[ "${STAT:-Z}" != "Z" ]]; then slept=$((loops * 2)) if [ $slept -lt $SOLR_STOP_WAIT ]; then @@ -869,7 +874,7 @@ function stop_solr() { exit 0 fi - STAT=`(ps -o stat='' $SOLR_PID || :) | tr -d ' '` + STAT=`(ps -o stat='' -p $SOLR_PID || :) | tr -d ' '` if [[ "${STAT:-Z}" != "Z" ]]; then if [ -n "{$JSTACK:-}" ]; then echo -e "Solr process $SOLR_PID is still running; jstacking it now." @@ -885,7 +890,7 @@ function stop_solr() { sleep 10 fi - STAT=`(ps -o stat='' $SOLR_PID || :) | tr -d ' '` + STAT=`(ps -o stat='' -p $SOLR_PID || :) | tr -d ' '` if [ "${STAT:-}" == "Z" ]; then # This can happen if, for example, you are running Solr inside a docker container with multiple processes # rather than running it is as the only service. The --init flag on docker avoids that particular problem. @@ -1860,7 +1865,7 @@ if [[ "$SCRIPT_CMD" == "stop" && -z "${SOLR_PORT:-}" ]]; then if [ $numSolrs -eq 1 ]; then # only do this if there is only 1 node running, otherwise they must provide the -p or -all PID="$(cat "$(find "$SOLR_PID_DIR" -name "solr-*.pid" -type f)")" - CHECK_PID=`ps -o pid='' $PID | tr -d ' '` + CHECK_PID=`ps -o pid='' -p $PID | tr -d ' '` if [ "$CHECK_PID" != "" ]; then port=`jetty_port "$CHECK_PID"` if [ "$port" != "" ]; then From 4c5bdf79423bc09fbc81e790a6fb5381eec365a7 Mon Sep 17 00:00:00 2001 From: Mike Drob Date: Wed, 18 May 2022 13:00:57 -0500 Subject: [PATCH 2/3] review feedback --- solr/bin/solr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solr/bin/solr b/solr/bin/solr index c11fe944c25f..531bf96c7b38 100755 --- a/solr/bin/solr +++ b/solr/bin/solr @@ -62,7 +62,7 @@ if [ "${THIS_OS:0:6}" == "CYGWIN" ]; then exit 1 fi # Alpine Linux BusyBox comes with a stripped down ps, make sure we have a fully featured one -if ! ps -p $$ &> /dev/null ; then +if [ $$ -ne $(ps -o pid='' -p $$ || echo 0) ] ; then echo -e "This script relies on a version of ps that supports the -p flag.\n\nPlease install a POSIX compliant version and try again." exit 1 fi From 86d944f76fc54868be972cdaaa53744968d2edd0 Mon Sep 17 00:00:00 2001 From: Mike Drob Date: Wed, 18 May 2022 15:09:08 -0500 Subject: [PATCH 3/3] one more ps --- solr/bin/solr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solr/bin/solr b/solr/bin/solr index 531bf96c7b38..bfee9829acba 100755 --- a/solr/bin/solr +++ b/solr/bin/solr @@ -687,7 +687,7 @@ function spinner() { local pid=$1 local delay=0.5 local spinstr='|/-\' - while [ "$(ps aux | awk '{print $2}' | grep -w $pid)" ]; do + while ps -o pid='' -p $pid &> /dev/null ; do local temp=${spinstr#?} printf " [%c] " "$spinstr" local spinstr=$temp${spinstr%"$temp"}