Now that posix_spawn works on native Windows, the modules 'execute' and 'spawn-pipe' can be changed to use this as the new implementation (instead of 'spawnvech', which implements only a special case).
This new implementation surely allocates more temporary memory, and also calls DuplicateHandle more often than the previous implementation. But it is more open to customizations (e.g. call setpgrp or setsid in the future). 2020-12-24 Bruno Haible <br...@clisp.org> spawn-pipe: Use posix_spawn by default on native Windows. * lib/spawn-pipe.c (SPAWN_PIPE_IMPL_AVOID_POSIX_SPAWN): New macro. (create_pipe): Use it to decide among the two possible implementations. * modules/spawn-pipe (Depends-on): Add posix_spawnattr_setpgroup. 2020-12-24 Bruno Haible <br...@clisp.org> unistd-safer: Implement pipe_safer on native Windows. * lib/pipe-safer.c (pipe_safer): Don't test HAVE_PIPE. * modules/unistd-safer (Depends-on): Add pipe-posix. 2020-12-24 Bruno Haible <br...@clisp.org> execute: Use posix_spawn by default on native Windows. * lib/execute.c (EXECUTE_IMPL_AVOID_POSIX_SPAWN): New macro. (execute): Use it to decide among the two possible implementations.
>From 4f766278c569ceebc49f8b8f963acc61cb935af4 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Fri, 25 Dec 2020 02:15:38 +0100 Subject: [PATCH 1/3] execute: Use posix_spawn by default on native Windows. * lib/execute.c (EXECUTE_IMPL_AVOID_POSIX_SPAWN): New macro. (execute): Use it to decide among the two possible implementations. --- ChangeLog | 6 ++++++ lib/execute.c | 21 ++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 30be929..afc5829 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2020-12-24 Bruno Haible <br...@clisp.org> + + execute: Use posix_spawn by default on native Windows. + * lib/execute.c (EXECUTE_IMPL_AVOID_POSIX_SPAWN): New macro. + (execute): Use it to decide among the two possible implementations. + 2020-12-24 Paul Eggert <egg...@cs.ucla.edu> canonicalize-lgpl: merge proposed libc changes diff --git a/lib/execute.c b/lib/execute.c index 4cc779e..24fbbc7 100644 --- a/lib/execute.c +++ b/lib/execute.c @@ -42,7 +42,20 @@ #define _(str) gettext (str) -#if defined _WIN32 && ! defined __CYGWIN__ + +/* Choice of implementation for native Windows. + - Define to 0 to use the posix_spawn facility (modules 'posix_spawn' and + 'posix_spawnp'), that is based on the module 'windows-spawn'. + - Define to 1 to use the older code, that uses the module 'windows-spawn' + directly. + You can set this macro from a Makefile or at configure time, from the + CPPFLAGS. */ +#ifndef EXECUTE_IMPL_AVOID_POSIX_SPAWN +# define EXECUTE_IMPL_AVOID_POSIX_SPAWN 0 +#endif + + +#if (defined _WIN32 && !defined __CYGWIN__) && EXECUTE_IMPL_AVOID_POSIX_SPAWN /* Native Windows API. */ # if GNULIB_MSVC_NOTHROW @@ -61,7 +74,7 @@ #endif -#if defined EINTR && (defined _WIN32 && ! defined __CYGWIN__) +#if defined EINTR && (defined _WIN32 && !defined __CYGWIN__) && EXECUTE_IMPL_AVOID_POSIX_SPAWN /* EINTR handling for close(), open(). These functions can return -1/EINTR even though we don't have any @@ -157,7 +170,7 @@ execute (const char *progname, } } -#if defined _WIN32 && ! defined __CYGWIN__ +#if (defined _WIN32 && !defined __CYGWIN__) && EXECUTE_IMPL_AVOID_POSIX_SPAWN /* Native Windows API. */ @@ -281,6 +294,7 @@ execute (const char *progname, || (directory != NULL && (err = posix_spawn_file_actions_addchdir (&actions, directory))) +# if !(defined _WIN32 && !defined __CYGWIN__) || (slave_process && ((err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, @@ -290,6 +304,7 @@ execute (const char *progname, || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0))) +# endif || (err = (directory != NULL ? posix_spawn (&child, prog_path, &actions, attrs_allocated ? &attrs : NULL, -- 2.7.4
>From 8fede4db2de4fd41ddf7e57b2d2c0e23f5deef67 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Fri, 25 Dec 2020 02:16:18 +0100 Subject: [PATCH 2/3] unistd-safer: Implement pipe_safer on native Windows. * lib/pipe-safer.c (pipe_safer): Don't test HAVE_PIPE. * modules/unistd-safer (Depends-on): Add pipe-posix. --- ChangeLog | 6 ++++++ lib/pipe-safer.c | 8 ++------ modules/unistd-safer | 1 + 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index afc5829..3d8478a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2020-12-24 Bruno Haible <br...@clisp.org> + unistd-safer: Implement pipe_safer on native Windows. + * lib/pipe-safer.c (pipe_safer): Don't test HAVE_PIPE. + * modules/unistd-safer (Depends-on): Add pipe-posix. + +2020-12-24 Bruno Haible <br...@clisp.org> + execute: Use posix_spawn by default on native Windows. * lib/execute.c (EXECUTE_IMPL_AVOID_POSIX_SPAWN): New macro. (execute): Use it to decide among the two possible implementations. diff --git a/lib/pipe-safer.c b/lib/pipe-safer.c index 5a597c5..a90ed81 100644 --- a/lib/pipe-safer.c +++ b/lib/pipe-safer.c @@ -30,7 +30,6 @@ int pipe_safer (int fd[2]) { -#if HAVE_PIPE if (pipe (fd) == 0) { int i; @@ -39,18 +38,15 @@ pipe_safer (int fd[2]) fd[i] = fd_safer (fd[i]); if (fd[i] < 0) { - int e = errno; + int saved_errno = errno; close (fd[1 - i]); - errno = e; + errno = saved_errno; return -1; } } return 0; } -#else - errno = ENOSYS; -#endif return -1; } diff --git a/modules/unistd-safer b/modules/unistd-safer index 2d195ec..d131b9c 100644 --- a/modules/unistd-safer +++ b/modules/unistd-safer @@ -12,6 +12,7 @@ m4/unistd-safer.m4 Depends-on: fcntl unistd +pipe-posix configure.ac: gl_UNISTD_SAFER -- 2.7.4
>From 1504c11f8a5cca19eae97afb71644a4123c343ab Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Fri, 25 Dec 2020 02:16:22 +0100 Subject: [PATCH 3/3] spawn-pipe: Use posix_spawn by default on native Windows. * lib/spawn-pipe.c (SPAWN_PIPE_IMPL_AVOID_POSIX_SPAWN): New macro. (create_pipe): Use it to decide among the two possible implementations. * modules/spawn-pipe (Depends-on): Add posix_spawnattr_setpgroup. --- ChangeLog | 7 +++++++ lib/spawn-pipe.c | 31 ++++++++++++++++++++++++++----- modules/spawn-pipe | 1 + 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3d8478a..8f27b28 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2020-12-24 Bruno Haible <br...@clisp.org> + spawn-pipe: Use posix_spawn by default on native Windows. + * lib/spawn-pipe.c (SPAWN_PIPE_IMPL_AVOID_POSIX_SPAWN): New macro. + (create_pipe): Use it to decide among the two possible implementations. + * modules/spawn-pipe (Depends-on): Add posix_spawnattr_setpgroup. + +2020-12-24 Bruno Haible <br...@clisp.org> + unistd-safer: Implement pipe_safer on native Windows. * lib/pipe-safer.c (pipe_safer): Don't test HAVE_PIPE. * modules/unistd-safer (Depends-on): Add pipe-posix. diff --git a/lib/spawn-pipe.c b/lib/spawn-pipe.c index aedbcb2..768c8b6 100644 --- a/lib/spawn-pipe.c +++ b/lib/spawn-pipe.c @@ -44,7 +44,20 @@ #define _(str) gettext (str) -#if defined _WIN32 && ! defined __CYGWIN__ + +/* Choice of implementation for native Windows. + - Define to 0 to use the posix_spawn facility (modules 'posix_spawn' and + 'posix_spawnp'), that is based on the module 'windows-spawn'. + - Define to 1 to use the older code, that uses the module 'windows-spawn' + directly. + You can set this macro from a Makefile or at configure time, from the + CPPFLAGS. */ +#ifndef SPAWN_PIPE_IMPL_AVOID_POSIX_SPAWN +# define SPAWN_PIPE_IMPL_AVOID_POSIX_SPAWN 0 +#endif + + +#if (defined _WIN32 && !defined __CYGWIN__) && SPAWN_PIPE_IMPL_AVOID_POSIX_SPAWN /* Native Windows API. */ # if GNULIB_MSVC_NOTHROW @@ -89,7 +102,7 @@ nonintr_close (int fd) #undef close /* avoid warning related to gnulib module unistd */ #define close nonintr_close -#if defined _WIN32 && ! defined __CYGWIN__ +#if (defined _WIN32 && !defined __CYGWIN__) && SPAWN_PIPE_IMPL_AVOID_POSIX_SPAWN static int nonintr_open (const char *pathname, int oflag, mode_t mode) { @@ -181,7 +194,7 @@ create_pipe (const char *progname, } } -#if (defined _WIN32 && ! defined __CYGWIN__) || defined __KLIBC__ +#if ((defined _WIN32 && !defined __CYGWIN__) && SPAWN_PIPE_IMPL_AVOID_POSIX_SPAWN) || defined __KLIBC__ /* Native Windows API. This uses _pipe(), dup2(), and _spawnv(). It could also be implemented @@ -217,7 +230,7 @@ create_pipe (const char *progname, child = -1; -# if defined _WIN32 && ! defined __CYGWIN__ +# if (defined _WIN32 && !defined __CYGWIN__) && SPAWN_PIPE_IMPL_AVOID_POSIX_SPAWN bool must_close_ifd1 = pipe_stdout; bool must_close_ofd0 = pipe_stdin; @@ -512,12 +525,20 @@ create_pipe (const char *progname, || (slave_process && ((err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, +# if defined _WIN32 && !defined __CYGWIN__ + (err = posix_spawnattr_setpgroup (&attrs, 0)) != 0 + || (err = posix_spawnattr_setflags (&attrs, + POSIX_SPAWN_SETPGROUP)) + != 0 +# else (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) - != 0))) + != 0 +# endif + ) ) ) || (err = (directory != NULL ? posix_spawn (&child, prog_path, &actions, attrs_allocated ? &attrs : NULL, diff --git a/modules/spawn-pipe b/modules/spawn-pipe index 7ad384b..f0e8daf 100644 --- a/modules/spawn-pipe +++ b/modules/spawn-pipe @@ -31,6 +31,7 @@ posix_spawn_file_actions_addopen posix_spawn_file_actions_addchdir posix_spawn_file_actions_destroy posix_spawnattr_init +posix_spawnattr_setpgroup posix_spawnattr_setsigmask posix_spawnattr_setflags posix_spawnattr_destroy -- 2.7.4