[Libguestfs] [PATCH nbdkit 9/9] eval, sh: Define $nbdkit_safe_stdio = 0|1 in scripts.

Richard W.M. Jones
Wed, 15 Apr 2020 16:16:50 -0000

---
 plugins/eval/nbdkit-eval-plugin.pod | 11 +++--------
 plugins/sh/nbdkit-sh-plugin.pod     | 18 +++++++++++++++++-
 plugins/sh/call.c                   |  8 ++++++--
 tests/test-single-sh.sh             |  4 ++++
 4 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/plugins/eval/nbdkit-eval-plugin.pod
b/plugins/eval/nbdkit-eval-plugin.pod
index 602fd3aa..a016aa78 100644
--- a/plugins/eval/nbdkit-eval-plugin.pod
+++ b/plugins/eval/nbdkit-eval-plugin.pod
@@ -154,14 +154,9 @@ defaults to the script "exit 2".

 =head1 ENVIRONMENT VARIABLES

-=over 4
-
-=item C<tmpdir>
-
-This is defined to the name of a temporary directory which can be used
-by the script snippets.  It is deleted when nbdkit exits.
-
-=back
+For environment variables that are set by the plugin running the
+script snippets, see
+L<nbdkit-sh-plugin(3)/Environment variables passed to the script>.

 =head1 FILES

diff --git a/plugins/sh/nbdkit-sh-plugin.pod b/plugins/sh/nbdkit-sh-plugin.pod
index 20a2b785..33bae6e4 100644
--- a/plugins/sh/nbdkit-sh-plugin.pod
+++ b/plugins/sh/nbdkit-sh-plugin.pod
@@ -119,7 +119,14 @@ These exit codes are reserved for future use.

 =back

-=head2 Temporary directory
+=head2 Environment variables passed to the script
+
+When running the script, certain environment variables are set by the
+plugin:
+
+=over 4
+
+=item C<$tmpdir>

 A fresh script is invoked for each method call (ie. scripts are
 stateless), so if the script needs to store state it has to store it
@@ -131,6 +138,15 @@ the script.  This directory persists for the lifetime of
nbdkit and is
 deleted when nbdkit exits.  The name of the directory is passed to
 each script invocation in the C<$tmpdir> environment variable.

+=item C<$nbdkit_stdio_safe>
+
+This is set to C<1> or C<0> depending on whether or not the script
can
+safely use stdin/stdout (eg. for reading passwords from the user).
+See also the discussion of C<nbdkit_stdio_safe> in
+L<nbdkit-plugin(3)>.
+
+=back
+
 =head2 Handles

 Handles are arbitrary strings, but it is best to limit them to short
diff --git a/plugins/sh/call.c b/plugins/sh/call.c
index b0aaf754..ddb7045b 100644
--- a/plugins/sh/call.c
+++ b/plugins/sh/call.c
@@ -107,6 +107,7 @@ call3 (const char *wbuf, size_t wbuflen, /* sent to stdin */
 {
   const char *argv0 = argv[0]; /* script name, used in error messages */
   CLEANUP_FREE_STRING_LIST char **env = NULL;
+  const char *stdio_safe_str = nbdkit_stdio_safe () ? "1" :
"0";
   pid_t pid = -1;
   int status;
   int ret = ERROR;
@@ -123,8 +124,11 @@ call3 (const char *wbuf, size_t wbuflen, /* sent to stdin
*/

   debug_call (argv);

-  /* Copy the environment, and add $tmpdir. */
-  env = copy_environ (environ, "tmpdir", tmpdir, NULL);
+  /* Copy the environment, and add $tmpdir and some other variables. */
+  env = copy_environ (environ,
+                      "tmpdir", tmpdir,
+                      "nbdkit_stdio_safe", stdio_safe_str,
+                      NULL);
   if (env == NULL)
     goto error;

diff --git a/tests/test-single-sh.sh b/tests/test-single-sh.sh
index a1586868..b4cfc9f0 100755
--- a/tests/test-single-sh.sh
+++ b/tests/test-single-sh.sh
@@ -57,6 +57,10 @@ if test -f single-sh.log; then
 fi

 cat >single-sh.script <<\EOF
+if test "$nbdkit_stdio_safe" != "0"; then
+  echo "unexpected value for nbdkit_stdio_safe ($nbdkit_stdio_safe)"
1>&2
+  exit 1
+fi
 case $1 in
   get_size) echo 1m ;;
   pread) dd if=/dev/zero count=$3 iflag=count_bytes ;;
--
2.25.0