Commit f8886038 by Janne Blomqvist

libfortran/90038: Use posix_spawn instead of fork

    
fork() semantics can be problematic.  Most unix style OS'es have
posix_spawn which can be used to replace fork + exec in many cases.
For more information see
e.g. https://www.microsoft.com/en-us/research/uploads/prod/2019/04/fork-hotos19.pdf
    
This replaces the one use of fork in libgfortran with posix_spawn.
    
2019-05-17  Janne Blomqvist  <jb@gcc.gnu.org>

        PR libfortran/90038
        * configure.ac (AC_CHECK_FUNCS_ONCE): Check for posix_spawn.
        * intrinsics/execute_command_line (execute_command_line): Use
        posix_spawn.
        * Makefile.in: Regenerated.
        * config.h.in: Regenerated.
        * configure: Regenerated.
    
Regtested on x86_64-pc-linux-gnu.

From-SVN: r271340
parent 8ee47828
2019-05-17 Janne Blomqvist <jb@gcc.gnu.org>
PR libfortran/90038
* configure.ac (AC_CHECK_FUNCS_ONCE): Check for posix_spawn.
* intrinsics/execute_command_line (execute_command_line): Use
posix_spawn.
* Makefile.in: Regenerated.
* config.h.in: Regenerated.
* configure: Regenerated.
2019-05-17 Jakub Jelinek <jakub@redhat.com> 2019-05-17 Jakub Jelinek <jakub@redhat.com>
PR fortran/54613 PR fortran/54613
......
...@@ -694,6 +694,7 @@ pdfdir = @pdfdir@ ...@@ -694,6 +694,7 @@ pdfdir = @pdfdir@
prefix = @prefix@ prefix = @prefix@
program_transform_name = @program_transform_name@ program_transform_name = @program_transform_name@
psdir = @psdir@ psdir = @psdir@
runstatedir = @runstatedir@
sbindir = @sbindir@ sbindir = @sbindir@
sharedstatedir = @sharedstatedir@ sharedstatedir = @sharedstatedir@
srcdir = @srcdir@ srcdir = @srcdir@
......
...@@ -627,6 +627,9 @@ ...@@ -627,6 +627,9 @@
/* Define to 1 if we have POSIX getpwuid_r which takes 5 arguments. */ /* Define to 1 if we have POSIX getpwuid_r which takes 5 arguments. */
#undef HAVE_POSIX_GETPWUID_R #undef HAVE_POSIX_GETPWUID_R
/* Define to 1 if you have the `posix_spawn' function. */
#undef HAVE_POSIX_SPAWN
/* Define to 1 if you have the `pow' function. */ /* Define to 1 if you have the `pow' function. */
#undef HAVE_POW #undef HAVE_POW
......
...@@ -780,6 +780,7 @@ infodir ...@@ -780,6 +780,7 @@ infodir
docdir docdir
oldincludedir oldincludedir
includedir includedir
runstatedir
localstatedir localstatedir
sharedstatedir sharedstatedir
sysconfdir sysconfdir
...@@ -870,6 +871,7 @@ datadir='${datarootdir}' ...@@ -870,6 +871,7 @@ datadir='${datarootdir}'
sysconfdir='${prefix}/etc' sysconfdir='${prefix}/etc'
sharedstatedir='${prefix}/com' sharedstatedir='${prefix}/com'
localstatedir='${prefix}/var' localstatedir='${prefix}/var'
runstatedir='${localstatedir}/run'
includedir='${prefix}/include' includedir='${prefix}/include'
oldincludedir='/usr/include' oldincludedir='/usr/include'
docdir='${datarootdir}/doc/${PACKAGE_TARNAME}' docdir='${datarootdir}/doc/${PACKAGE_TARNAME}'
...@@ -1122,6 +1124,15 @@ do ...@@ -1122,6 +1124,15 @@ do
| -silent | --silent | --silen | --sile | --sil) | -silent | --silent | --silen | --sile | --sil)
silent=yes ;; silent=yes ;;
-runstatedir | --runstatedir | --runstatedi | --runstated \
| --runstate | --runstat | --runsta | --runst | --runs \
| --run | --ru | --r)
ac_prev=runstatedir ;;
-runstatedir=* | --runstatedir=* | --runstatedi=* | --runstated=* \
| --runstate=* | --runstat=* | --runsta=* | --runst=* | --runs=* \
| --run=* | --ru=* | --r=*)
runstatedir=$ac_optarg ;;
-sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb) -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb)
ac_prev=sbindir ;; ac_prev=sbindir ;;
-sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \ -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \
...@@ -1259,7 +1270,7 @@ fi ...@@ -1259,7 +1270,7 @@ fi
for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \ for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \
datadir sysconfdir sharedstatedir localstatedir includedir \ datadir sysconfdir sharedstatedir localstatedir includedir \
oldincludedir docdir infodir htmldir dvidir pdfdir psdir \ oldincludedir docdir infodir htmldir dvidir pdfdir psdir \
libdir localedir mandir libdir localedir mandir runstatedir
do do
eval ac_val=\$$ac_var eval ac_val=\$$ac_var
# Remove trailing slashes. # Remove trailing slashes.
...@@ -1412,6 +1423,7 @@ Fine tuning of the installation directories: ...@@ -1412,6 +1423,7 @@ Fine tuning of the installation directories:
--sysconfdir=DIR read-only single-machine data [PREFIX/etc] --sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com] --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var] --localstatedir=DIR modifiable single-machine data [PREFIX/var]
--runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run]
--libdir=DIR object code libraries [EPREFIX/lib] --libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include] --includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include] --oldincludedir=DIR C header files for non-gcc [/usr/include]
...@@ -2629,6 +2641,7 @@ as_fn_append ac_func_list " ttyname" ...@@ -2629,6 +2641,7 @@ as_fn_append ac_func_list " ttyname"
as_fn_append ac_func_list " alarm" as_fn_append ac_func_list " alarm"
as_fn_append ac_func_list " access" as_fn_append ac_func_list " access"
as_fn_append ac_func_list " fork" as_fn_append ac_func_list " fork"
as_fn_append ac_func_list " posix_spawn"
as_fn_append ac_func_list " setmode" as_fn_append ac_func_list " setmode"
as_fn_append ac_func_list " fcntl" as_fn_append ac_func_list " fcntl"
as_fn_append ac_func_list " writev" as_fn_append ac_func_list " writev"
...@@ -12685,7 +12698,7 @@ else ...@@ -12685,7 +12698,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF cat > conftest.$ac_ext <<_LT_EOF
#line 12688 "configure" #line 12701 "configure"
#include "confdefs.h" #include "confdefs.h"
#if HAVE_DLFCN_H #if HAVE_DLFCN_H
...@@ -12791,7 +12804,7 @@ else ...@@ -12791,7 +12804,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF cat > conftest.$ac_ext <<_LT_EOF
#line 12794 "configure" #line 12807 "configure"
#include "confdefs.h" #include "confdefs.h"
#if HAVE_DLFCN_H #if HAVE_DLFCN_H
...@@ -16040,7 +16053,7 @@ else ...@@ -16040,7 +16053,7 @@ else
We can't simply define LARGE_OFF_T to be 9223372036854775807, We can't simply define LARGE_OFF_T to be 9223372036854775807,
since some C++ compilers masquerading as C compilers since some C++ compilers masquerading as C compilers
incorrectly reject 9223372036854775807. */ incorrectly reject 9223372036854775807. */
#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
&& LARGE_OFF_T % 2147483647 == 1) && LARGE_OFF_T % 2147483647 == 1)
? 1 : -1]; ? 1 : -1];
...@@ -16086,7 +16099,7 @@ else ...@@ -16086,7 +16099,7 @@ else
We can't simply define LARGE_OFF_T to be 9223372036854775807, We can't simply define LARGE_OFF_T to be 9223372036854775807,
since some C++ compilers masquerading as C compilers since some C++ compilers masquerading as C compilers
incorrectly reject 9223372036854775807. */ incorrectly reject 9223372036854775807. */
#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
&& LARGE_OFF_T % 2147483647 == 1) && LARGE_OFF_T % 2147483647 == 1)
? 1 : -1]; ? 1 : -1];
...@@ -16110,7 +16123,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext ...@@ -16110,7 +16123,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
We can't simply define LARGE_OFF_T to be 9223372036854775807, We can't simply define LARGE_OFF_T to be 9223372036854775807,
since some C++ compilers masquerading as C compilers since some C++ compilers masquerading as C compilers
incorrectly reject 9223372036854775807. */ incorrectly reject 9223372036854775807. */
#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
&& LARGE_OFF_T % 2147483647 == 1) && LARGE_OFF_T % 2147483647 == 1)
? 1 : -1]; ? 1 : -1];
...@@ -16155,7 +16168,7 @@ else ...@@ -16155,7 +16168,7 @@ else
We can't simply define LARGE_OFF_T to be 9223372036854775807, We can't simply define LARGE_OFF_T to be 9223372036854775807,
since some C++ compilers masquerading as C compilers since some C++ compilers masquerading as C compilers
incorrectly reject 9223372036854775807. */ incorrectly reject 9223372036854775807. */
#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
&& LARGE_OFF_T % 2147483647 == 1) && LARGE_OFF_T % 2147483647 == 1)
? 1 : -1]; ? 1 : -1];
...@@ -16179,7 +16192,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext ...@@ -16179,7 +16192,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
We can't simply define LARGE_OFF_T to be 9223372036854775807, We can't simply define LARGE_OFF_T to be 9223372036854775807,
since some C++ compilers masquerading as C compilers since some C++ compilers masquerading as C compilers
incorrectly reject 9223372036854775807. */ incorrectly reject 9223372036854775807. */
#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
&& LARGE_OFF_T % 2147483647 == 1) && LARGE_OFF_T % 2147483647 == 1)
? 1 : -1]; ? 1 : -1];
...@@ -16961,6 +16974,8 @@ done ...@@ -16961,6 +16974,8 @@ done
fi fi
# Check strerror_r, cannot be above as versions with two and three arguments exist # Check strerror_r, cannot be above as versions with two and three arguments exist
......
...@@ -315,7 +315,7 @@ else ...@@ -315,7 +315,7 @@ else
AC_CHECK_FUNCS_ONCE(getrusage times mkstemp strtof strtold snprintf \ AC_CHECK_FUNCS_ONCE(getrusage times mkstemp strtof strtold snprintf \
ftruncate chsize chdir getentropy getlogin gethostname kill link symlink \ ftruncate chsize chdir getentropy getlogin gethostname kill link symlink \
sleep ttyname \ sleep ttyname \
alarm access fork setmode fcntl writev \ alarm access fork posix_spawn setmode fcntl writev \
gettimeofday stat fstat lstat getpwuid vsnprintf dup \ gettimeofday stat fstat lstat getpwuid vsnprintf dup \
getcwd localtime_r gmtime_r getpwuid_r ttyname_r clock_gettime \ getcwd localtime_r gmtime_r getpwuid_r ttyname_r clock_gettime \
getgid getpid getuid geteuid umask getegid \ getgid getpid getuid geteuid umask getegid \
......
...@@ -32,7 +32,10 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see ...@@ -32,7 +32,10 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
#ifdef HAVE_SYS_WAIT_H #ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h> #include <sys/wait.h>
#endif #endif
#ifdef HAVE_POSIX_SPAWN
#include <spawn.h>
extern char **environ;
#endif
enum { EXEC_SYNCHRONOUS = -2, EXEC_NOERROR = 0, EXEC_SYSTEMFAILED, enum { EXEC_SYNCHRONOUS = -2, EXEC_NOERROR = 0, EXEC_SYSTEMFAILED,
EXEC_CHILDFAILED, EXEC_INVALIDCOMMAND }; EXEC_CHILDFAILED, EXEC_INVALIDCOMMAND };
...@@ -71,7 +74,7 @@ execute_command_line (const char *command, bool wait, int *exitstat, ...@@ -71,7 +74,7 @@ execute_command_line (const char *command, bool wait, int *exitstat,
/* Flush all I/O units before executing the command. */ /* Flush all I/O units before executing the command. */
flush_all_units(); flush_all_units();
#if defined(HAVE_FORK) #if defined(HAVE_POSIX_SPAWN) || defined(HAVE_FORK)
if (!wait) if (!wait)
{ {
/* Asynchronous execution. */ /* Asynchronous execution. */
...@@ -79,14 +82,21 @@ execute_command_line (const char *command, bool wait, int *exitstat, ...@@ -79,14 +82,21 @@ execute_command_line (const char *command, bool wait, int *exitstat,
set_cmdstat (cmdstat, EXEC_NOERROR); set_cmdstat (cmdstat, EXEC_NOERROR);
if ((pid = fork()) < 0) #ifdef HAVE_POSIX_SPAWN
const char * const argv[] = {"sh", "-c", cmd, NULL};
if (posix_spawn (&pid, "/bin/sh", NULL, NULL,
(char * const* restrict) argv, environ))
set_cmdstat (cmdstat, EXEC_CHILDFAILED); set_cmdstat (cmdstat, EXEC_CHILDFAILED);
#elif defined(HAVE_FORK)
if ((pid = fork()) < 0)
set_cmdstat (cmdstat, EXEC_CHILDFAILED);
else if (pid == 0) else if (pid == 0)
{ {
/* Child process. */ /* Child process. */
int res = system (cmd); int res = system (cmd);
_exit (WIFEXITED(res) ? WEXITSTATUS(res) : res); _exit (WIFEXITED(res) ? WEXITSTATUS(res) : res);
} }
#endif
} }
else else
#endif #endif
...@@ -96,7 +106,7 @@ execute_command_line (const char *command, bool wait, int *exitstat, ...@@ -96,7 +106,7 @@ execute_command_line (const char *command, bool wait, int *exitstat,
if (res == -1) if (res == -1)
set_cmdstat (cmdstat, EXEC_SYSTEMFAILED); set_cmdstat (cmdstat, EXEC_SYSTEMFAILED);
#ifndef HAVE_FORK #if !defined(HAVE_POSIX_SPAWN) && !defined(HAVE_FORK)
else if (!wait) else if (!wait)
set_cmdstat (cmdstat, EXEC_SYNCHRONOUS); set_cmdstat (cmdstat, EXEC_SYNCHRONOUS);
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment