60
60
#include < sys/mman.h>
61
61
#include < sys/resource.h>
62
62
#include < sys/socket.h>
63
+ #include < spawn.h>
63
64
#include < sys/time.h>
64
65
#include < sys/times.h>
65
66
#include < sys/types.h>
@@ -1955,40 +1956,16 @@ char** os::get_environ() { return environ; }
1955
1956
// doesn't block SIGINT et al.
1956
1957
// -this function is unsafe to use in non-error situations, mainly
1957
1958
// because the child process will inherit all parent descriptors.
1958
- int os::fork_and_exec (const char * cmd, bool prefer_vfork) {
1959
- const char * argv[4 ] = {" sh" , " -c" , cmd, NULL };
1960
-
1961
- pid_t pid ;
1962
-
1959
+ int os::fork_and_exec (const char * cmd) {
1960
+ const char * argv[4 ] = {" sh" , " -c" , cmd, NULL };
1961
+ pid_t pid = -1 ;
1963
1962
char ** env = os::get_environ ();
1964
-
1965
- // Use always vfork on AIX, since its safe and helps with analyzing OOM situations.
1966
- // Otherwise leave it up to the caller.
1967
- AIX_ONLY (prefer_vfork = true ;)
1968
- #ifdef __APPLE__
1969
- pid = ::fork ();
1970
- #else
1971
- pid = prefer_vfork ? ::vfork () : ::fork ();
1972
- #endif
1973
-
1974
- if (pid < 0 ) {
1975
- // fork failed
1976
- return -1 ;
1977
-
1978
- } else if (pid == 0 ) {
1979
- // child process
1980
-
1981
- ::execve (" /bin/sh" , (char * const *)argv, env);
1982
-
1983
- // execve failed
1984
- ::_exit (-1 );
1985
-
1986
- } else {
1987
- // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't
1988
- // care about the actual exit code, for now.
1989
-
1963
+ // Note: cast is needed because posix_spawn() requires - for compatibility with ancient
1964
+ // C-code - a non-const argv/envp pointer array. But it is fine to hand in literal
1965
+ // strings and just cast the constness away. See also ProcessImpl_md.c.
1966
+ int rc = ::posix_spawn (&pid, " /bin/sh" , NULL , NULL , (char **) argv, env);
1967
+ if (rc == 0 ) {
1990
1968
int status;
1991
-
1992
1969
// Wait for the child process to exit. This returns immediately if
1993
1970
// the child has already exited. */
1994
1971
while (::waitpid (pid, &status, 0 ) < 0 ) {
@@ -1998,7 +1975,6 @@ int os::fork_and_exec(const char* cmd, bool prefer_vfork) {
1998
1975
default : return -1 ;
1999
1976
}
2000
1977
}
2001
-
2002
1978
if (WIFEXITED (status)) {
2003
1979
// The child exited normally; get its exit code.
2004
1980
return WEXITSTATUS (status);
@@ -2013,6 +1989,9 @@ int os::fork_and_exec(const char* cmd, bool prefer_vfork) {
2013
1989
// Unknown exit code; pass it through
2014
1990
return status;
2015
1991
}
1992
+ } else {
1993
+ // Don't log, we are inside error handling
1994
+ return -1 ;
2016
1995
}
2017
1996
}
2018
1997
0 commit comments