8262955: Unify os::fork_and_exec() across Posix platforms

Reviewed-by: dholmes, hseigel
This commit is contained in:
Thomas Stuefe 2021-03-09 06:00:46 +00:00
parent 39b1113838
commit 5b9b170db9
7 changed files with 91 additions and 210 deletions

View file

@ -97,7 +97,6 @@
# include <sys/times.h>
# include <sys/utsname.h>
# include <sys/socket.h>
# include <sys/wait.h>
# include <pwd.h>
# include <poll.h>
# include <fcntl.h>
@ -5219,68 +5218,6 @@ void os::pause() {
}
}
extern char** environ;
// Run the specified command in a separate process. Return its exit value,
// or -1 on failure (e.g. can't fork a new process).
// Unlike system(), this function can be called from signal handler. It
// doesn't block SIGINT et al.
int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {
const char * argv[4] = {"sh", "-c", cmd, NULL};
pid_t pid ;
if (use_vfork_if_available) {
pid = vfork();
} else {
pid = fork();
}
if (pid < 0) {
// fork failed
return -1;
} else if (pid == 0) {
// child process
execve("/bin/sh", (char* const*)argv, environ);
// execve failed
_exit(-1);
} else {
// copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't
// care about the actual exit code, for now.
int status;
// Wait for the child process to exit. This returns immediately if
// the child has already exited. */
while (waitpid(pid, &status, 0) < 0) {
switch (errno) {
case ECHILD: return 0;
case EINTR: break;
default: return -1;
}
}
if (WIFEXITED(status)) {
// The child exited normally; get its exit code.
return WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
// The child exited because of a signal
// The best value to return is 0x80 + signal number,
// because that is what all Unix shells do, and because
// it allows callers to distinguish between process exit and
// process death by signal.
return 0x80 + WTERMSIG(status);
} else {
// Unknown exit code; pass it through
return status;
}
}
}
// Get the default path to the core file
// Returns the length of the string
int os::get_core_path(char* buffer, size_t bufferSize) {