Skip to content

Commit 158831e

Browse files
committed
8274320: os::fork_and_exec() should be using posix_spawn
Reviewed-by: mdoerr, dholmes
1 parent bf2e9ee commit 158831e

File tree

4 files changed

+15
-39
lines changed

4 files changed

+15
-39
lines changed

src/hotspot/os/posix/os_posix.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
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

src/hotspot/os/windows/os_windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5624,7 +5624,7 @@ int os::PlatformMonitor::wait(jlong millis) {
56245624

56255625
// Run the specified command in a separate process. Return its exit value,
56265626
// or -1 on failure (e.g. can't create a new process).
5627-
int os::fork_and_exec(const char* cmd, bool dummy /* ignored */) {
5627+
int os::fork_and_exec(const char* cmd) {
56285628
STARTUPINFO si;
56295629
PROCESS_INFORMATION pi;
56305630
DWORD exit_code;

src/hotspot/share/runtime/os.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,7 @@ class os: AllStatic {
515515

516516
// run cmd in a separate process and return its exit code; or -1 on failures.
517517
// Note: only safe to use in fatal error situations.
518-
// The "prefer_vfork" argument is only used on POSIX platforms to
519-
// indicate whether vfork should be used instead of fork to spawn the
520-
// child process (ignored on AIX, which always uses vfork).
521-
static int fork_and_exec(const char *cmd, bool prefer_vfork = false);
518+
static int fork_and_exec(const char *cmd);
522519

523520
// Call ::exit() on all platforms
524521
static void exit(int num);

src/hotspot/share/utilities/vmError.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ void VM_ReportJavaOutOfMemory::doit() {
17881788
#endif
17891789
tty->print_cr("\"%s\"...", cmd);
17901790

1791-
if (os::fork_and_exec(cmd, true) < 0) {
1791+
if (os::fork_and_exec(cmd) < 0) {
17921792
tty->print_cr("os::fork_and_exec failed: %s (%s=%d)",
17931793
os::strerror(errno), os::errno_name(errno), errno);
17941794
}

0 commit comments

Comments
 (0)