>From 8cf7746a2b5c5fe50e8369951619a87ecb3786bd Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 24 Dec 2020 22:14:38 +0100 Subject: [PATCH 2/7] posix_spawn* tests: Add support for native Windows. * tests/test-posix_spawn-open1.c (DATA_FILENAME): Treat native Windows like Cygwin. * tests/test-posix_spawn-dup2-stdin.c (main): Don't assume the signals SIGHUP and SIGPIPE. On native Windows, don't call posix_spawnattr_setsigmask. * tests/test-posix_spawn-dup2-stdout.c (main): Likewise. * tests/test-posix_spawn-fchdir.c (main): Likewise. * tests/test-posix_spawn-chdir.c (test): Likewise. Accept the child output from Cygwin's 'pwd' program. * tests/test-posix_spawn-script.c (main): On native Windows, skip the executable-shell-script part of the test. * tests/test-posix_spawnp-script.c (main): Likewise. * modules/posix_spawn-tests (Depends-on): Add freopen, waitpid. (configure.ac): Don't define the POSIX_SPAWN_PORTED conditional. (Makefile.am): Don't test the POSIX_SPAWN_PORTED conditional. * modules/posix_spawnp-tests (Depends-on): Add waitpid. (configure.ac): Don't define the POSIX_SPAWN_PORTED conditional. (Makefile.am): Don't test the POSIX_SPAWN_PORTED conditional. * modules/posix_spawn_file_actions_addchdir-tests (Makefile.am): Don't test the POSIX_SPAWN_PORTED conditional. * modules/posix_spawn_file_actions_addfchdir-tests (configure.ac): Define the POSIX_SPAWN_PORTED conditional here. --- ChangeLog | 26 +++++++++++++++++ modules/posix_spawn-tests | 12 ++------ modules/posix_spawn_file_actions_addchdir-tests | 13 ++++----- modules/posix_spawn_file_actions_addfchdir-tests | 8 ++++++ modules/posix_spawnp-tests | 11 +------- tests/test-posix_spawn-chdir.c | 36 ++++++++++++++++++------ tests/test-posix_spawn-dup2-stdin.c | 11 +++++++- tests/test-posix_spawn-dup2-stdout.c | 13 +++++++-- tests/test-posix_spawn-fchdir.c | 13 +++++++-- tests/test-posix_spawn-open1.c | 5 ++-- tests/test-posix_spawn-script.c | 7 +++++ tests/test-posix_spawnp-script.c | 7 +++++ 12 files changed, 120 insertions(+), 42 deletions(-) diff --git a/ChangeLog b/ChangeLog index d93aa47..b716a26 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,31 @@ 2020-12-24 Bruno Haible + posix_spawn* tests: Add support for native Windows. + * tests/test-posix_spawn-open1.c (DATA_FILENAME): Treat native Windows + like Cygwin. + * tests/test-posix_spawn-dup2-stdin.c (main): Don't assume the signals + SIGHUP and SIGPIPE. On native Windows, don't call + posix_spawnattr_setsigmask. + * tests/test-posix_spawn-dup2-stdout.c (main): Likewise. + * tests/test-posix_spawn-fchdir.c (main): Likewise. + * tests/test-posix_spawn-chdir.c (test): Likewise. Accept the child + output from Cygwin's 'pwd' program. + * tests/test-posix_spawn-script.c (main): On native Windows, skip the + executable-shell-script part of the test. + * tests/test-posix_spawnp-script.c (main): Likewise. + * modules/posix_spawn-tests (Depends-on): Add freopen, waitpid. + (configure.ac): Don't define the POSIX_SPAWN_PORTED conditional. + (Makefile.am): Don't test the POSIX_SPAWN_PORTED conditional. + * modules/posix_spawnp-tests (Depends-on): Add waitpid. + (configure.ac): Don't define the POSIX_SPAWN_PORTED conditional. + (Makefile.am): Don't test the POSIX_SPAWN_PORTED conditional. + * modules/posix_spawn_file_actions_addchdir-tests (Makefile.am): Don't + test the POSIX_SPAWN_PORTED conditional. + * modules/posix_spawn_file_actions_addfchdir-tests (configure.ac): + Define the POSIX_SPAWN_PORTED conditional here. + +2020-12-24 Bruno Haible + sh-filename: Add support for native Windows. * m4/sh-filename.m4 (gl_SH_FILENAME): Treat native Windows like Cygwin. diff --git a/modules/posix_spawn-tests b/modules/posix_spawn-tests index 06a8434..ee505c0 100644 --- a/modules/posix_spawn-tests +++ b/modules/posix_spawn-tests @@ -18,19 +18,12 @@ unistd sys_wait environ fflush +freopen +waitpid configure.ac: -AC_EGREP_CPP([notposix], [[ -#if defined _MSC_VER || defined __MINGW32__ - notposix -#endif - ]], - [posix_spawn_ported=no], - [posix_spawn_ported=yes]) -AM_CONDITIONAL([POSIX_SPAWN_PORTED], [test $posix_spawn_ported = yes]) Makefile.am: -if POSIX_SPAWN_PORTED TESTS += \ test-posix_spawn-open1 \ test-posix_spawn-open2 \ @@ -44,4 +37,3 @@ check_PROGRAMS += \ test-posix_spawn-inherit1 \ test-posix_spawn-script test_posix_spawn_script_CPPFLAGS = $(AM_CPPFLAGS) -DSRCDIR=\"$(srcdir)/\" -endif diff --git a/modules/posix_spawn_file_actions_addchdir-tests b/modules/posix_spawn_file_actions_addchdir-tests index 400c1f7..1c56b1c 100644 --- a/modules/posix_spawn_file_actions_addchdir-tests +++ b/modules/posix_spawn_file_actions_addchdir-tests @@ -12,10 +12,9 @@ findprog configure.ac: Makefile.am: -TESTS += test-posix_spawn_file_actions_addchdir -check_PROGRAMS += test-posix_spawn_file_actions_addchdir - -if POSIX_SPAWN_PORTED -TESTS += test-posix_spawn-chdir -check_PROGRAMS += test-posix_spawn-chdir -endif +TESTS += \ + test-posix_spawn_file_actions_addchdir \ + test-posix_spawn-chdir +check_PROGRAMS += \ + test-posix_spawn_file_actions_addchdir \ + test-posix_spawn-chdir diff --git a/modules/posix_spawn_file_actions_addfchdir-tests b/modules/posix_spawn_file_actions_addfchdir-tests index 0d8c990..45f4230 100644 --- a/modules/posix_spawn_file_actions_addfchdir-tests +++ b/modules/posix_spawn_file_actions_addfchdir-tests @@ -10,6 +10,14 @@ posix_spawnp-tests findprog configure.ac: +AC_EGREP_CPP([notposix], [[ +#if defined _MSC_VER || defined __MINGW32__ + notposix +#endif + ]], + [posix_spawn_ported=no], + [posix_spawn_ported=yes]) +AM_CONDITIONAL([POSIX_SPAWN_PORTED], [test $posix_spawn_ported = yes]) Makefile.am: TESTS += test-posix_spawn_file_actions_addfchdir diff --git a/modules/posix_spawnp-tests b/modules/posix_spawnp-tests index 887569c..e84d89d 100644 --- a/modules/posix_spawnp-tests +++ b/modules/posix_spawnp-tests @@ -26,19 +26,11 @@ dup environ sh-filename sigprocmask +waitpid configure.ac: -AC_EGREP_CPP([notposix], [[ -#if defined _MSC_VER || defined __MINGW32__ - notposix -#endif - ]], - [posix_spawn_ported=no], - [posix_spawn_ported=yes]) -AM_CONDITIONAL([POSIX_SPAWN_PORTED], [test $posix_spawn_ported = yes]) Makefile.am: -if POSIX_SPAWN_PORTED TESTS += \ test-posix_spawn-dup2-stdout \ test-posix_spawn-dup2-stdin \ @@ -63,4 +55,3 @@ test-posix_spawn-dup2-stdin.sh: test-posix_spawn-dup2-stdin.in.sh MOSTLYCLEANFILES += test-posix_spawn-dup2-stdin.sh test-posix_spawn-dup2-stdin.sh-t test_posix_spawnp_script_CPPFLAGS = $(AM_CPPFLAGS) -DSRCDIR=\"$(srcdir)/\" -endif diff --git a/tests/test-posix_spawn-chdir.c b/tests/test-posix_spawn-chdir.c index 91a5497..62c60ee 100644 --- a/tests/test-posix_spawn-chdir.c +++ b/tests/test-posix_spawn-chdir.c @@ -64,6 +64,7 @@ test (const char *pwd_prog) int fd; FILE *fp; char line[80]; + int line_len; int status; int exitstatus; @@ -76,8 +77,12 @@ test (const char *pwd_prog) sigemptyset (&fatal_signal_set); sigaddset (&fatal_signal_set, SIGINT); sigaddset (&fatal_signal_set, SIGTERM); + #ifdef SIGHUP sigaddset (&fatal_signal_set, SIGHUP); + #endif + #ifdef SIGPIPE sigaddset (&fatal_signal_set, SIGPIPE); + #endif sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL); actions_allocated = false; attrs_allocated = false; @@ -90,8 +95,13 @@ test (const char *pwd_prog) || (err = posix_spawn_file_actions_addchdir (&actions, "/")) != 0 || (err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, + #if defined _WIN32 && !defined __CYGWIN__ + 0 + #else (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0 - || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0) + || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0 + #endif + ) || (err = posix_spawnp (&child, pwd_prog, &actions, &attrs, argv, environ)) != 0)) { if (actions_allocated) @@ -108,22 +118,32 @@ test (const char *pwd_prog) sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL); close (ifd[1]); fd = ifd[0]; - fp = fdopen (fd, "r"); + fp = fdopen (fd, "rb"); if (fp == NULL) { fprintf (stderr, "fdopen() failed\n"); exit (1); } - if (fread (line, 1, 80, fp) < 2) + line_len = fread (line, 1, 80, fp); + if (line_len < 2) { fprintf (stderr, "could not read expected output\n"); exit (1); } - if (memcmp (line, "/\n", 2) != 0) - { - fprintf (stderr, "read output is not the expected output"); - exit (1); - } + if (!(line_len == 2 && memcmp (line, "/\n", 2) == 0)) +#if defined _WIN32 && !defined __CYGWIN__ + /* If the pwd program is Cygwin's pwd, its output in the root directory is + "/cygdrive/N", where N is a lowercase letter. */ + if (!(line_len > 11 + && memcmp (line, "/cygdrive/", 10) == 0 + && line[10] >= 'a' && line[10] <= 'z' + && ((line_len == 12 && line[11] == '\n') + || (line_len == 13 && line[11] == '\r' && line[12] == '\n')))) +#endif + { + fprintf (stderr, "read output is not the expected output\n"); + exit (1); + } fclose (fp); status = 0; while (waitpid (child, &status, 0) != child) diff --git a/tests/test-posix_spawn-dup2-stdin.c b/tests/test-posix_spawn-dup2-stdin.c index 8af1e3f..fe039bf 100644 --- a/tests/test-posix_spawn-dup2-stdin.c +++ b/tests/test-posix_spawn-dup2-stdin.c @@ -76,8 +76,12 @@ main () sigemptyset (&fatal_signal_set); sigaddset (&fatal_signal_set, SIGINT); sigaddset (&fatal_signal_set, SIGTERM); + #ifdef SIGHUP sigaddset (&fatal_signal_set, SIGHUP); + #endif + #ifdef SIGPIPE sigaddset (&fatal_signal_set, SIGPIPE); + #endif sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL); actions_allocated = false; attrs_allocated = false; @@ -88,8 +92,13 @@ main () || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0 || (err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, + #if defined _WIN32 && !defined __CYGWIN__ + 0 + #else (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0 - || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0) + || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0 + #endif + ) || (err = posix_spawnp (&child, BOURNE_SHELL, &actions, &attrs, argv, environ)) != 0)) { if (actions_allocated) diff --git a/tests/test-posix_spawn-dup2-stdout.c b/tests/test-posix_spawn-dup2-stdout.c index c67e143..ce09844 100644 --- a/tests/test-posix_spawn-dup2-stdout.c +++ b/tests/test-posix_spawn-dup2-stdout.c @@ -98,8 +98,12 @@ main () sigemptyset (&fatal_signal_set); sigaddset (&fatal_signal_set, SIGINT); sigaddset (&fatal_signal_set, SIGTERM); + #ifdef SIGHUP sigaddset (&fatal_signal_set, SIGHUP); + #endif + #ifdef SIGPIPE sigaddset (&fatal_signal_set, SIGPIPE); + #endif sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL); actions_allocated = false; attrs_allocated = false; @@ -111,8 +115,13 @@ main () || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0 || (err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, + #if defined _WIN32 && !defined __CYGWIN__ + 0 + #else (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0 - || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0) + || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0 + #endif + ) || (err = posix_spawnp (&child, BOURNE_SHELL, &actions, &attrs, argv, environ)) != 0)) { if (actions_allocated) @@ -142,7 +151,7 @@ main () } if (memcmp (line, "Halle Potta", 11) != 0) { - fprintf (stderr, "read output is not the expected output"); + fprintf (stderr, "read output is not the expected output\n"); exit (1); } fclose (fp); diff --git a/tests/test-posix_spawn-fchdir.c b/tests/test-posix_spawn-fchdir.c index c5ca2a3..3206ad5 100644 --- a/tests/test-posix_spawn-fchdir.c +++ b/tests/test-posix_spawn-fchdir.c @@ -83,8 +83,12 @@ test (const char *pwd_prog) sigemptyset (&fatal_signal_set); sigaddset (&fatal_signal_set, SIGINT); sigaddset (&fatal_signal_set, SIGTERM); + #ifdef SIGHUP sigaddset (&fatal_signal_set, SIGHUP); + #endif + #ifdef SIGPIPE sigaddset (&fatal_signal_set, SIGPIPE); + #endif sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL); actions_allocated = false; attrs_allocated = false; @@ -97,8 +101,13 @@ test (const char *pwd_prog) || (err = posix_spawn_file_actions_addfchdir (&actions, rootfd)) != 0 || (err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, + #if defined _WIN32 && !defined __CYGWIN__ + 0 + #else (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0 - || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0) + || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0 + #endif + ) || (err = posix_spawnp (&child, pwd_prog, &actions, &attrs, argv, environ)) != 0)) { if (actions_allocated) @@ -128,7 +137,7 @@ test (const char *pwd_prog) } if (memcmp (line, "/\n", 2) != 0) { - fprintf (stderr, "read output is not the expected output"); + fprintf (stderr, "read output is not the expected output\n"); exit (1); } fclose (fp); diff --git a/tests/test-posix_spawn-open1.c b/tests/test-posix_spawn-open1.c index e08c113..648fc1d 100644 --- a/tests/test-posix_spawn-open1.c +++ b/tests/test-posix_spawn-open1.c @@ -40,8 +40,9 @@ SIGNATURE_CHECK (posix_spawn, int, (pid_t *, char const *, #define CHILD_PROGRAM_FILENAME "test-posix_spawn-open1" #define DATA_FILENAME "t!#$%&'()*+,-;=?@[\\]^_`{|}~.tmp" -/* On Cygwin, '*' '?' '\\' '|' cannot be used in file names. */ -#if defined __CYGWIN__ +/* On Windows (including Cygwin), '*' '?' '\\' '|' cannot be used in file + names. */ +#if defined _WIN32 || defined __CYGWIN__ # undef DATA_FILENAME # define DATA_FILENAME "t!#$%&'()+,-;=@[]^_`{}~.tmp" #endif diff --git a/tests/test-posix_spawn-script.c b/tests/test-posix_spawn-script.c index e17c3b8..6fa8e48 100644 --- a/tests/test-posix_spawn-script.c +++ b/tests/test-posix_spawn-script.c @@ -90,6 +90,12 @@ main () } } +#if defined _WIN32 && !defined __CYGWIN__ + /* On native Windows, scripts - even with '#!' marker - are not executable. + Only .bat and .cmd files are. */ + fprintf (stderr, "Skipping test: scripts are not executable on this platform.\n"); + return 77; +#else { const char *prog_path = SRCDIR "executable-shell-script"; const char *prog_argv[2] = { prog_path, NULL }; @@ -141,6 +147,7 @@ main () return 1; } } +#endif /* Clean up data file. */ unlink (DATA_FILENAME); diff --git a/tests/test-posix_spawnp-script.c b/tests/test-posix_spawnp-script.c index b48e791..c2c3e57 100644 --- a/tests/test-posix_spawnp-script.c +++ b/tests/test-posix_spawnp-script.c @@ -90,6 +90,12 @@ main () } } +#if defined _WIN32 && !defined __CYGWIN__ + /* On native Windows, scripts - even with '#!' marker - are not executable. + Only .bat and .cmd files are. */ + fprintf (stderr, "Skipping test: scripts are not executable on this platform.\n"); + return 77; +#else { const char *prog_path = SRCDIR "executable-shell-script"; const char *prog_argv[2] = { prog_path, NULL }; @@ -141,6 +147,7 @@ main () return 1; } } +#endif /* Clean up data file. */ unlink (DATA_FILENAME); -- 2.7.4