From: Bruno Haible Date: Thu, 17 Dec 2020 01:47:40 +0000 (+0100) Subject: posix_spawn-tests: Rename test. X-Git-Tag: v1.0~3364 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=7860f4b9c1a17d00a77498f11deb2de866f108e8;p=gnulib.git posix_spawn-tests: Rename test. * tests/test-posix_spawn-open1.c: Renamed from tests/test-posix_spawn3.c. * modules/posix_spawn-tests (Files, Makefile.am): Update. --- diff --git a/ChangeLog b/ChangeLog index 8d959997bb..46d584be5d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2020-12-16 Bruno Haible + posix_spawn-tests: Rename test. + * tests/test-posix_spawn-open1.c: Renamed from + tests/test-posix_spawn3.c. + * modules/posix_spawn-tests (Files, Makefile.am): Update. + posix_spawnp-tests: Rename test. * tests/test-posix_spawn-dup2-stdin.c: Renamed from tests/test-posix_spawn2.c. diff --git a/modules/posix_spawn-tests b/modules/posix_spawn-tests index 55c4efd790..4b20afa7ce 100644 --- a/modules/posix_spawn-tests +++ b/modules/posix_spawn-tests @@ -1,5 +1,5 @@ Files: -tests/test-posix_spawn3.c +tests/test-posix_spawn-open1.c tests/signature.h Depends-on: @@ -23,6 +23,6 @@ AM_CONDITIONAL([POSIX_SPAWN_PORTED], [test $posix_spawn_ported = yes]) Makefile.am: if POSIX_SPAWN_PORTED -TESTS += test-posix_spawn3 -check_PROGRAMS += test-posix_spawn3 +TESTS += test-posix_spawn-open1 +check_PROGRAMS += test-posix_spawn-open1 endif diff --git a/tests/test-posix_spawn-open1.c b/tests/test-posix_spawn-open1.c new file mode 100644 index 0000000000..31fee70ecd --- /dev/null +++ b/tests/test-posix_spawn-open1.c @@ -0,0 +1,162 @@ +/* Test of posix_spawn() function. + Copyright (C) 2008-2020 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible , 2008. */ + +/* Test whether posix_spawn_file_actions_addopen supports filename arguments + that contain special characters such as '*'. */ + +#include + +#include + +#include "signature.h" +SIGNATURE_CHECK (posix_spawn, int, (pid_t *, char const *, + posix_spawn_file_actions_t const *, + posix_spawnattr_t const *, + char *const[], char *const[])); +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CHILD_PROGRAM_FILENAME "test-posix_spawn-open1" +#define DATA_FILENAME "t!#$%&'()*+,-;=?@[\\]^_`{|}~.tmp" +/* On Cygwin, '*' '?' '\\' '|' cannot be used in file names. */ +#if defined __CYGWIN__ +# undef DATA_FILENAME +# define DATA_FILENAME "t!#$%&'()+,-;=@[]^_`{}~.tmp" +#endif + +static int +parent_main (void) +{ + FILE *fp; + char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL }; + posix_spawn_file_actions_t actions; + bool actions_allocated; + int err; + pid_t child; + int status; + int exitstatus; + + /* Create a data file with specific contents. */ + fp = fopen (DATA_FILENAME, "wb"); + if (fp == NULL) + { + perror ("cannot create data file"); + return 1; + } + fwrite ("Halle Potta", 1, 11, fp); + if (fflush (fp) || fclose (fp)) + { + perror ("cannot prepare data file"); + return 1; + } + + /* Avoid reading from our stdin, as it could block. */ + if (freopen ("/dev/null", "rb", stdin) == NULL) + { + perror ("cannot redirect stdin"); + return 1; + } + + /* Test whether posix_spawn_file_actions_addopen with this file name + actually works, by spawning a child that reads from this file. */ + actions_allocated = false; + if ((err = posix_spawn_file_actions_init (&actions)) != 0 + || (actions_allocated = true, + (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0 + || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0)) + { + if (actions_allocated) + posix_spawn_file_actions_destroy (&actions); + errno = err; + perror ("subprocess failed"); + return 1; + } + posix_spawn_file_actions_destroy (&actions); + status = 0; + while (waitpid (child, &status, 0) != child) + ; + if (!WIFEXITED (status)) + { + fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status); + return 1; + } + exitstatus = WEXITSTATUS (status); + if (exitstatus != 0) + { + fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus); + return 1; + } + return 0; +} + +static int +child_main (void) +{ + char buf[1024]; + + /* See if reading from STDIN_FILENO yields the expected contents. */ + if (fread (buf, 1, sizeof (buf), stdin) == 11 + && memcmp (buf, "Halle Potta", 11) == 0) + return 0; + else + return 2; +} + +static void +cleanup_then_die (int sig) +{ + /* Clean up data file. */ + unlink (DATA_FILENAME); + + /* Re-raise the signal and die from it. */ + signal (sig, SIG_DFL); + raise (sig); +} + +int +main (int argc, char *argv[]) +{ + int exitstatus; + + if (!(argc > 1 && strcmp (argv[1], "-child") == 0)) + { + /* This is the parent process. */ + signal (SIGINT, cleanup_then_die); + signal (SIGTERM, cleanup_then_die); + #ifdef SIGHUP + signal (SIGHUP, cleanup_then_die); + #endif + + exitstatus = parent_main (); + } + else + { + /* This is the child process. */ + + exitstatus = child_main (); + } + unlink (DATA_FILENAME); + return exitstatus; +} diff --git a/tests/test-posix_spawn3.c b/tests/test-posix_spawn3.c deleted file mode 100644 index 7aac788e70..0000000000 --- a/tests/test-posix_spawn3.c +++ /dev/null @@ -1,162 +0,0 @@ -/* Test of posix_spawn() function. - Copyright (C) 2008-2020 Free Software Foundation, Inc. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . */ - -/* Written by Bruno Haible , 2008. */ - -/* Test whether posix_spawn_file_actions_addopen supports filename arguments - that contain special characters such as '*'. */ - -#include - -#include - -#include "signature.h" -SIGNATURE_CHECK (posix_spawn, int, (pid_t *, char const *, - posix_spawn_file_actions_t const *, - posix_spawnattr_t const *, - char *const[], char *const[])); -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define CHILD_PROGRAM_FILENAME "test-posix_spawn3" -#define DATA_FILENAME "t!#$%&'()*+,-;=?@[\\]^_`{|}~.tmp" -/* On Cygwin, '*' '?' '\\' '|' cannot be used in file names. */ -#if defined __CYGWIN__ -# undef DATA_FILENAME -# define DATA_FILENAME "t!#$%&'()+,-;=@[]^_`{}~.tmp" -#endif - -static int -parent_main (void) -{ - FILE *fp; - char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL }; - posix_spawn_file_actions_t actions; - bool actions_allocated; - int err; - pid_t child; - int status; - int exitstatus; - - /* Create a data file with specific contents. */ - fp = fopen (DATA_FILENAME, "wb"); - if (fp == NULL) - { - perror ("cannot create data file"); - return 1; - } - fwrite ("Halle Potta", 1, 11, fp); - if (fflush (fp) || fclose (fp)) - { - perror ("cannot prepare data file"); - return 1; - } - - /* Avoid reading from our stdin, as it could block. */ - if (freopen ("/dev/null", "rb", stdin) == NULL) - { - perror ("cannot redirect stdin"); - return 1; - } - - /* Test whether posix_spawn_file_actions_addopen with this file name - actually works, by spawning a child that reads from this file. */ - actions_allocated = false; - if ((err = posix_spawn_file_actions_init (&actions)) != 0 - || (actions_allocated = true, - (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, DATA_FILENAME, O_RDONLY, 0600)) != 0 - || (err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, &actions, NULL, argv, environ)) != 0)) - { - if (actions_allocated) - posix_spawn_file_actions_destroy (&actions); - errno = err; - perror ("subprocess failed"); - return 1; - } - posix_spawn_file_actions_destroy (&actions); - status = 0; - while (waitpid (child, &status, 0) != child) - ; - if (!WIFEXITED (status)) - { - fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status); - return 1; - } - exitstatus = WEXITSTATUS (status); - if (exitstatus != 0) - { - fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus); - return 1; - } - return 0; -} - -static int -child_main (void) -{ - char buf[1024]; - - /* See if reading from STDIN_FILENO yields the expected contents. */ - if (fread (buf, 1, sizeof (buf), stdin) == 11 - && memcmp (buf, "Halle Potta", 11) == 0) - return 0; - else - return 2; -} - -static void -cleanup_then_die (int sig) -{ - /* Clean up data file. */ - unlink (DATA_FILENAME); - - /* Re-raise the signal and die from it. */ - signal (sig, SIG_DFL); - raise (sig); -} - -int -main (int argc, char *argv[]) -{ - int exitstatus; - - if (!(argc > 1 && strcmp (argv[1], "-child") == 0)) - { - /* This is the parent process. */ - signal (SIGINT, cleanup_then_die); - signal (SIGTERM, cleanup_then_die); - #ifdef SIGHUP - signal (SIGHUP, cleanup_then_die); - #endif - - exitstatus = parent_main (); - } - else - { - /* This is the child process. */ - - exitstatus = child_main (); - } - unlink (DATA_FILENAME); - return exitstatus; -}