[Libguestfs] [libnbd PATCH 1/3] lib/test-fork-safe-execvpe.sh: generalize "run" to "run0"

Laszlo Ersek lersek at redhat.com
Wed Mar 22 16:53:09 UTC 2023


It turns out that we'll need to put the generality of our
"test-fork-safe-execvpe" binary executable to use, in that it takes
separate "program-to-exec" and "argv0" (for the program-to-exec)
arguments.

Currently, the "run" function duplicates $2 to both "program-to-exec" and
"argv0", for "test-fork-safe-execvpe". Remove the duplication (expect the
caller to provide separate $2 and $3 arguments, respectively) and rename
"run" to "run0". At the same time, reimplement "run" as a simple wrapper
(i.e., with just the duplication) around "run0".

This patch is worth viewing with "git show --color-words" as well.

Signed-off-by: Laszlo Ersek <lersek at redhat.com>
---
 lib/test-fork-safe-execvpe.sh | 28 +++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/lib/test-fork-safe-execvpe.sh b/lib/test-fork-safe-execvpe.sh
index 5d2671946c66..f055f7b05814 100755
--- a/lib/test-fork-safe-execvpe.sh
+++ b/lib/test-fork-safe-execvpe.sh
@@ -32,42 +32,52 @@ execvpe=$(realpath -- "$dname/$bname")
 # If $1 is "_", then the $execvpe helper binary is invoked with PATH unset.
 # Otherwise, the binary is invoked with PATH set to $1.
 #
-# $2 and onward are passed to $execvpe; note that $2 becomes *both*
-# "program-to-exec" for the helper *and* argv[0] for the program executed by the
-# helper.
+# $2 and onward are passed to $execvpe; $2 becomes "program-to-exec" for the
+# helper and $3 becomes argv[0] for the program executed by the helper.
 #
 # The command itself (including the PATH setting) is written to "cmd" (for error
 # reporting purposes only); the standard output and error are saved in "out" and
 # "err" respectively; the exit status is written to "status". This function
 # should never fail; if it does, then that's a bug in this unit test script, or
 # the disk is full etc.
-run()
+run0()
 {
     local pathctl=$1
-    local program=$2
     local exit_status
 
     shift 1
 
     if test _ = "$pathctl"; then
-        printf 'unset PATH; %s %s %s\n' "$execvpe" "$program" "$*" >cmd
+        printf 'unset PATH; %s %s\n' "$execvpe" "$*" >cmd
         set +e
         (
             unset PATH
-            "$execvpe" "$program" "$@" >out 2>err
+            "$execvpe" "$@" >out 2>err
         )
         exit_status=$?
         set -e
     else
-        printf 'PATH=%s %s %s %s\n' "$pathctl" "$execvpe" "$program" "$*" >cmd
+        printf 'PATH=%s %s %s\n' "$pathctl" "$execvpe" "$*" >cmd
         set +e
-        PATH=$pathctl "$execvpe" "$program" "$@" >out 2>err
+        PATH=$pathctl "$execvpe" "$@" >out 2>err
         exit_status=$?
         set -e
     fi
     printf '%d\n' $exit_status >status
 }
 
+# Does the same as "run0", but $2 becomes *both* "program-to-exec" for the the
+# $execvpe helper binary *and* argv[0] for the program executed by the helper.
+run()
+{
+    local pathctl=$1
+    local program=$2
+
+    shift 1
+
+    run0 "$pathctl" "$program" "$@"
+}
+
 # After "run" returns, the following three functions can verify the result.
 #
 # Check if the helper binary failed in nbd_internal_execvpe_init().



More information about the Libguestfs mailing list