Fix: tests: `pgrep -f` flags unrelated process as lttng-sessiond
[lttng-tools.git] / tests / utils / utils.sh
index d65662e4265a8fc05c429549d8097c231bd39ba3..1a6ea3c424016e4ecca9fa76f1726a168d39839d 100644 (file)
@@ -74,6 +74,42 @@ trap full_cleanup SIGINT SIGTERM
 
 trap null_pipes SIGPIPE
 
+# Check pgrep from env, default to pgrep if none
+if [ -z "$PGREP" ]; then
+       PGREP=pgrep
+fi
+
+# Due to the renaming of threads we need to use the full command (pgrep -f) to
+# identify the pids for multiple lttng related processes. The problem with "pgrep
+# -f" is that it ends up also looking at the arguments. We use a two stage
+# lookup. The first one is using "pgrep -f" yielding potential candidate.
+# The second on perform grep on the basename of the first field of the
+# /proc/pid/cmdline of the previously identified pids. The first field
+# correspond to the actual command.
+function lttng_pgrep ()
+{
+       local pattern=$1
+       local possible_pids
+       local full_command_no_argument
+       local command_basename
+
+       possible_pids=$($PGREP -f "$pattern")
+       if [ -z "$possible_pids" ]; then
+               return 0
+       fi
+
+       while IFS= read -r pid ; do
+               # /proc/pid/cmdline is null separated.
+               if full_command_no_argument=$(cut -d '' -f 1 < /proc/"$pid"/cmdline); then
+                       command_basename=$(basename "$full_command_no_argument")
+                       if grep -q "$pattern" <<< "$command_basename"; then
+                               echo "$pid"
+                       fi
+               fi
+       done <<< "$possible_pids"
+       return 0
+}
+
 function print_ok ()
 {
        # Check if we are a terminal
@@ -401,7 +437,7 @@ function start_lttng_relayd_opt()
 
        DIR=$(readlink -f "$TESTDIR")
 
-       if [ -z $(pgrep -f $RELAYD_MATCH) ]; then
+       if [ -z $(lttng_pgrep "$RELAYD_MATCH") ]; then
                # shellcheck disable=SC2086
                $DIR/../src/bin/lttng-relayd/$RELAYD_BIN $process_mode $opt 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
                #$DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt -vvv >>/tmp/relayd.log 2>&1 &
@@ -450,7 +486,7 @@ function stop_lttng_relayd_opt()
        local retval=0
        local pids=
 
-       pids=$(pgrep -f "$RELAYD_MATCH")
+       pids=$(lttng_pgrep "$RELAYD_MATCH")
        if [ -z "$pids" ]; then
                if [ "$withtap" -eq "1" ]; then
                        pass "No relay daemon to kill"
@@ -469,7 +505,7 @@ function stop_lttng_relayd_opt()
        else
                out=1
                while [ -n "$out" ]; do
-                       out=$(pgrep -f "$RELAYD_MATCH")
+                       out=$(lttng_pgrep "$RELAYD_MATCH")
                        if [ -n "$dtimeleft_s" ]; then
                                if [ $dtimeleft_s -lt 0 ]; then
                                        out=
@@ -547,7 +583,7 @@ function start_lttng_sessiond_opt()
        : "${LTTNG_SESSION_CONFIG_XSD_PATH="${DIR}/../src/common/config/"}"
        export LTTNG_SESSION_CONFIG_XSD_PATH
 
-       if [ -z "$(pgrep -f "${SESSIOND_MATCH}")" ]; then
+       if [ -z "$(lttng_pgrep "${SESSIOND_MATCH}")" ]; then
                # Have a load path ?
                if [ -n "$load_path" ]; then
                        # shellcheck disable=SC2086
@@ -599,10 +635,10 @@ function stop_lttng_sessiond_opt()
        local retval=0
 
        local runas_pids=
-       runas_pids=$(pgrep -f "$RUNAS_MATCH")
+       runas_pids=$(lttng_pgrep "$RUNAS_MATCH")
 
        local pids=
-       pids=$(pgrep -f "$SESSIOND_MATCH")
+       pids=$(lttng_pgrep "$SESSIOND_MATCH")
 
        if [ -n "$runas_pids" ]; then
                pids="$pids $runas_pids"
@@ -626,7 +662,7 @@ function stop_lttng_sessiond_opt()
        else
                out=1
                while [ -n "$out" ]; do
-                       out=$(pgrep -f "${SESSIOND_MATCH}")
+                       out=$(lttng_pgrep "${SESSIOND_MATCH}")
                        if [ -n "$dtimeleft_s" ]; then
                                if [ $dtimeleft_s -lt 0 ]; then
                                        out=
@@ -638,7 +674,7 @@ function stop_lttng_sessiond_opt()
                done
                out=1
                while [ -n "$out" ]; do
-                       out=$(pgrep -f "$CONSUMERD_MATCH")
+                       out=$(lttng_pgrep "$CONSUMERD_MATCH")
                        if [ -n "$dtimeleft_s" ]; then
                                if [ $dtimeleft_s -lt 0 ]; then
                                        out=
@@ -692,7 +728,7 @@ function sigstop_lttng_sessiond_opt()
                return
        fi
 
-       PID_SESSIOND="$(pgrep -f "${SESSIOND_MATCH}") $(pgrep -f "$RUNAS_MATCH")"
+       PID_SESSIOND="$(lttng_pgrep "${SESSIOND_MATCH}") $(lttng_pgrep "$RUNAS_MATCH")"
 
        if [ "$withtap" -eq "1" ]; then
                diag "Sending SIGSTOP to lt-$SESSIOND_BIN and $SESSIOND_BIN pids: $(echo "$PID_SESSIOND" | tr '\n' ' ')"
@@ -706,7 +742,7 @@ function sigstop_lttng_sessiond_opt()
        else
                out=1
                while [ $out -ne 0 ]; do
-                       pid="$(pgrep -f "$SESSIOND_MATCH")"
+                       pid="$(lttng_pgrep "$SESSIOND_MATCH")"
 
                        # Wait until state becomes stopped for session
                        # daemon(s).
@@ -754,7 +790,7 @@ function stop_lttng_consumerd_opt()
 
        local retval=0
 
-       PID_CONSUMERD="$(pgrep -f "$CONSUMERD_MATCH")"
+       PID_CONSUMERD="$(lttng_pgrep "$CONSUMERD_MATCH")"
 
        if [ -z "$PID_CONSUMERD" ]; then
                if [ "$withtap" -eq "1" ]; then
@@ -774,7 +810,7 @@ function stop_lttng_consumerd_opt()
        else
                out=1
                while [ $out -ne 0 ]; do
-                       pid="$(pgrep -f "$CONSUMERD_MATCH")"
+                       pid="$(lttng_pgrep "$CONSUMERD_MATCH")"
 
                        # If consumerds are still present check their status.
                        # A zombie status qualifies the consumerd as *killed*
@@ -821,7 +857,7 @@ function sigstop_lttng_consumerd_opt()
        local withtap=$1
        local signal=SIGSTOP
 
-       PID_CONSUMERD="$(pgrep -f "$CONSUMERD_MATCH")"
+       PID_CONSUMERD="$(lttng_pgrep "$CONSUMERD_MATCH")"
 
        diag "Sending SIGSTOP to $CONSUMERD_BIN pids: $(echo "$PID_CONSUMERD" | tr '\n' ' ')"
 
@@ -837,7 +873,7 @@ function sigstop_lttng_consumerd_opt()
        else
                out=1
                while [ $out -ne 0 ]; do
-                       pid="$(pgrep -f "$CONSUMERD_MATCH")"
+                       pid="$(lttng_pgrep "$CONSUMERD_MATCH")"
 
                        # Wait until state becomes stopped for all
                        # consumers.
This page took 0.026611 seconds and 5 git commands to generate.