nghttpx: Replace posix_spawn functions with fork + dup2 + execve

Although posx_spawn is very convenient and useful, we have platform
which don't have these functions (e.g., Android NDK r10d).
This commit is contained in:
Tatsuhiro Tsujikawa 2015-04-09 00:55:22 +09:00
parent 09c485e712
commit bc53c81616
1 changed files with 19 additions and 24 deletions

View File

@ -30,9 +30,6 @@
#include <cerrno> #include <cerrno>
#include <thread> #include <thread>
#ifndef NOTHREADS
#include <spawn.h>
#endif // !NOTHREADS
#include "shrpx_client_handler.h" #include "shrpx_client_handler.h"
#include "shrpx_ssl.h" #include "shrpx_ssl.h"
@ -392,36 +389,34 @@ int ConnectionHandler::start_ocsp_update(const char *cert_file) {
} }
}); });
// posix_spawn family functions are really interesting. They makes auto pid = fork();
// fork + dup2 + execve pattern easier. if (pid == -1) {
auto error = errno;
posix_spawn_file_actions_t file_actions; LOG(WARN) << "Could not execute ocsp query command: " << argv[0]
<< ", fork() failed, errno=" << error;
if (posix_spawn_file_actions_init(&file_actions) != 0) {
return -1; return -1;
} }
auto file_actions_del = if (pid == 0) {
defer(posix_spawn_file_actions_destroy, &file_actions); // child process
dup2(pfd[1], 1);
close(pfd[0]);
if (posix_spawn_file_actions_adddup2(&file_actions, pfd[1], 1) != 0) { rv = execve(argv[0], argv, envp);
return -1; if (rv == -1) {
} auto error = errno;
LOG(WARN) << "Could not execute ocsp query command: " << argv[0]
if (posix_spawn_file_actions_addclose(&file_actions, pfd[0]) != 0) { << ", execve() faild, errno=" << error;
return -1; _Exit(EXIT_FAILURE);
} }
// unreachable
rv = posix_spawn(&ocsp_.pid, argv[0], &file_actions, nullptr, argv, envp);
if (rv != 0) {
LOG(WARN) << "Cannot execute ocsp query command: " << argv[0]
<< ", errno=" << rv;
return -1;
} }
// parent process
close(pfd[1]); close(pfd[1]);
pfd[1] = -1; pfd[1] = -1;
ocsp_.pid = pid;
ocsp_.fd = pfd[0]; ocsp_.fd = pfd[0];
pfd[0] = -1; pfd[0] = -1;