6060#include < sys/mman.h>
6161#include < sys/resource.h>
6262#include < sys/socket.h>
63+ #include < spawn.h>
6364#include < sys/time.h>
6465#include < sys/times.h>
6566#include < sys/types.h>
@@ -1955,40 +1956,16 @@ char** os::get_environ() { return environ; }
19551956// doesn't block SIGINT et al.
19561957// -this function is unsafe to use in non-error situations, mainly
19571958// 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 ;
19631962 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 ) {
19901968 int status;
1991-
19921969 // Wait for the child process to exit. This returns immediately if
19931970 // the child has already exited. */
19941971 while (::waitpid (pid, &status, 0 ) < 0 ) {
@@ -1998,7 +1975,6 @@ int os::fork_and_exec(const char* cmd, bool prefer_vfork) {
19981975 default : return -1 ;
19991976 }
20001977 }
2001-
20021978 if (WIFEXITED (status)) {
20031979 // The child exited normally; get its exit code.
20041980 return WEXITSTATUS (status);
@@ -2013,6 +1989,9 @@ int os::fork_and_exec(const char* cmd, bool prefer_vfork) {
20131989 // Unknown exit code; pass it through
20141990 return status;
20151991 }
1992+ } else {
1993+ // Don't log, we are inside error handling
1994+ return -1 ;
20161995 }
20171996}
20181997
0 commit comments