]> Savannah Git Hosting - gnulib.git/commitdiff
execle, execve, execvpe tests: Fix test failures under qemu user-mode.
authorBruno Haible <bruno@clisp.org>
Sat, 28 Aug 2021 17:15:38 +0000 (19:15 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 28 Aug 2021 17:15:38 +0000 (19:15 +0200)
* test-execle-main.c: Include <string.h>.
(get_environ_assignment, create_minimal_env): New functions.
(main): Call create_minimal_env.
* test-execve-main.c: Likewise.
* test-execvpe-main.c: Likewise.

ChangeLog
tests/test-execle-main.c
tests/test-execve-main.c
tests/test-execvpe-main.c

index 498525a24276be92b550f83f620b281708649c4a..0c1aeceef4cfd1386ad8fbe5a37832bd6653d971 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2021-08-28  Bruno Haible  <bruno@clisp.org>
+
+       execle, execve, execvpe tests: Fix test failures under qemu user-mode.
+       * test-execle-main.c: Include <string.h>.
+       (get_environ_assignment, create_minimal_env): New functions.
+       (main): Call create_minimal_env.
+       * test-execve-main.c: Likewise.
+       * test-execvpe-main.c: Likewise.
+
 2021-08-27  Paul Eggert  <eggert@cs.ucla.edu>
 
        base32, base64: prefer signed to unsigned integers
index 2c085f630784f7a2180b45fdd0188f302813d172..f738293c3f000ca325169a7b514b76895c68a65d 100644 (file)
 SIGNATURE_CHECK (execle, int, (const char *, const char *, ...));
 
 #include <stdio.h>
+#include <string.h>
+
+/* Looks up the NAME=VALUE assignment among the environment variables.
+   Returns it, or NULL if not found.  */
+static const char *
+get_environ_assignment (const char *name)
+{
+  size_t name_len = strlen (name);
+  char **p;
+  for (p = environ; *p != NULL; p++)
+    {
+      const char *assignment = *p;
+      if (strncmp (assignment, name, name_len) == 0
+          && assignment[name_len] == '=')
+        return assignment;
+    }
+  return NULL;
+}
+
+/* Creates a minimal environment.  */
+static void
+create_minimal_env (const char *env[5])
+{
+  const char **p = env;
+  *p++ =
+    #ifdef __CYGWIN__
+    /* The Cygwin DLLs needed by the program are in /bin.  */
+    "PATH=.:/bin";
+    #else
+    "PATH=.";
+    #endif
+  *p = get_environ_assignment ("QEMU_LD_PREFIX");
+  if (*p != NULL)
+    p++;
+  *p = get_environ_assignment ("QEMU_CPU");
+  if (*p != NULL)
+    p++;
+  *p++ = "Hommingberg=Gepardenforelle";
+  *p = NULL;
+}
 
 int
 main ()
 {
   const char *progname = "./test-exec-child";
-  const char *env[3] =
-    {
-      #ifdef __CYGWIN__
-      /* The Cygwin DLLs needed by the program are in /bin.  */
-      "PATH=.:/bin",
-      #else
-      "PATH=.",
-      #endif
-      "Hommingberg=Gepardenforelle",
-      NULL
-    };
+  const char *env[5];
+  create_minimal_env (env);
   execle (progname,
           progname,
           "abc def",
index 970372da18a3f510924a3b3e2901d284c628c587..6199a7d5000a68e61c65d9c249b16001190fb311 100644 (file)
 SIGNATURE_CHECK (execve, int, (const char *, char * const *, char * const *));
 
 #include <stdio.h>
+#include <string.h>
+
+/* Looks up the NAME=VALUE assignment among the environment variables.
+   Returns it, or NULL if not found.  */
+static const char *
+get_environ_assignment (const char *name)
+{
+  size_t name_len = strlen (name);
+  char **p;
+  for (p = environ; *p != NULL; p++)
+    {
+      const char *assignment = *p;
+      if (strncmp (assignment, name, name_len) == 0
+          && assignment[name_len] == '=')
+        return assignment;
+    }
+  return NULL;
+}
+
+/* Creates a minimal environment.  */
+static void
+create_minimal_env (const char *env[5])
+{
+  const char **p = env;
+  *p++ =
+    #ifdef __CYGWIN__
+    /* The Cygwin DLLs needed by the program are in /bin.  */
+    "PATH=.:/bin";
+    #else
+    "PATH=.";
+    #endif
+  *p = get_environ_assignment ("QEMU_LD_PREFIX");
+  if (*p != NULL)
+    p++;
+  *p = get_environ_assignment ("QEMU_CPU");
+  if (*p != NULL)
+    p++;
+  *p++ = "Hommingberg=Gepardenforelle";
+  *p = NULL;
+}
 
 int
 main ()
 {
   const char *progname = "./test-exec-child";
-  const char *env[3] =
-    {
-      #ifdef __CYGWIN__
-      /* The Cygwin DLLs needed by the program are in /bin.  */
-      "PATH=.:/bin",
-      #else
-      "PATH=.",
-      #endif
-      "Hommingberg=Gepardenforelle",
-      NULL
-    };
   const char *argv[12] =
     {
       progname,
@@ -56,6 +85,8 @@ main ()
       "",
       NULL
     };
+  const char *env[5];
+  create_minimal_env (env);
   execve (progname, (char * const *) argv, (char * const *) env);
 
   perror ("execve");
index 560ae8e690dc226f538d79e7d20a58af255dbf85..c7aa0a3a2d564fbb0d03a74a0af1e1c2e51d8cbd 100644 (file)
 SIGNATURE_CHECK (execvpe, int, (const char *, char * const *, char * const *));
 
 #include <stdio.h>
+#include <string.h>
+
+/* Looks up the NAME=VALUE assignment among the environment variables.
+   Returns it, or NULL if not found.  */
+static const char *
+get_environ_assignment (const char *name)
+{
+  size_t name_len = strlen (name);
+  char **p;
+  for (p = environ; *p != NULL; p++)
+    {
+      const char *assignment = *p;
+      if (strncmp (assignment, name, name_len) == 0
+          && assignment[name_len] == '=')
+        return assignment;
+    }
+  return NULL;
+}
+
+/* Creates a minimal environment.  */
+static void
+create_minimal_env (const char *env[5])
+{
+  const char **p = env;
+  *p++ =
+    #ifdef __CYGWIN__
+    /* The Cygwin DLLs needed by the program are in /bin.  */
+    "PATH=.:/bin";
+    #else
+    "PATH=.";
+    #endif
+  *p = get_environ_assignment ("QEMU_LD_PREFIX");
+  if (*p != NULL)
+    p++;
+  *p = get_environ_assignment ("QEMU_CPU");
+  if (*p != NULL)
+    p++;
+  *p++ = "Hommingberg=Gepardenforelle";
+  *p = NULL;
+}
 
 int
 main ()
 {
   const char *progname = "test-exec-child";
-  const char *env[3] =
-    {
-      #ifdef __CYGWIN__
-      /* The Cygwin DLLs needed by the program are in /bin.  */
-      "PATH=.:/bin",
-      #else
-      "PATH=.",
-      #endif
-      "Hommingberg=Gepardenforelle",
-      NULL
-    };
   const char *argv[12] =
     {
       progname,
@@ -56,6 +85,8 @@ main ()
       "",
       NULL
     };
+  const char *env[5];
+  create_minimal_env (env);
   execvpe (progname, (char * const *) argv, (char * const *) env);
 
   perror ("execvpe");