From ba3c445a4422f8ea676244fdc8662b01734072f7 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 28 Aug 2021 19:15:38 +0200 Subject: [PATCH] execle, execve, execvpe tests: Fix test failures under qemu user-mode. * test-execle-main.c: Include . (get_environ_assignment, create_minimal_env): New functions. (main): Call create_minimal_env. * test-execve-main.c: Likewise. * test-execvpe-main.c: Likewise. --- ChangeLog | 9 +++++++ tests/test-execle-main.c | 53 +++++++++++++++++++++++++++++++-------- tests/test-execve-main.c | 53 +++++++++++++++++++++++++++++++-------- tests/test-execvpe-main.c | 53 +++++++++++++++++++++++++++++++-------- 4 files changed, 135 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index 498525a242..0c1aeceef4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2021-08-28 Bruno Haible + + execle, execve, execvpe tests: Fix test failures under qemu user-mode. + * test-execle-main.c: Include . + (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 base32, base64: prefer signed to unsigned integers diff --git a/tests/test-execle-main.c b/tests/test-execle-main.c index 2c085f6307..f738293c3f 100644 --- a/tests/test-execle-main.c +++ b/tests/test-execle-main.c @@ -25,22 +25,53 @@ SIGNATURE_CHECK (execle, int, (const char *, const char *, ...)); #include +#include + +/* 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", diff --git a/tests/test-execve-main.c b/tests/test-execve-main.c index 970372da18..6199a7d500 100644 --- a/tests/test-execve-main.c +++ b/tests/test-execve-main.c @@ -25,22 +25,51 @@ SIGNATURE_CHECK (execve, int, (const char *, char * const *, char * const *)); #include +#include + +/* 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"); diff --git a/tests/test-execvpe-main.c b/tests/test-execvpe-main.c index 560ae8e690..c7aa0a3a2d 100644 --- a/tests/test-execvpe-main.c +++ b/tests/test-execvpe-main.c @@ -25,22 +25,51 @@ SIGNATURE_CHECK (execvpe, int, (const char *, char * const *, char * const *)); #include +#include + +/* 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"); -- 2.39.5