diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-13 17:40:09 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-13 17:40:09 +0100 |
commit | c0d9d61e3330d4f69a9547cc3d0e62970fb7427e (patch) | |
tree | ffc906ba3ddbe97dbf1042b14063caa7debe141d /plugins/fcgi/fastcgiprocess.cpp | |
parent | d14582a1d92e036780166a0b5ec0494d7353cc75 (diff) |
Added webapp-runner
Diffstat (limited to 'plugins/fcgi/fastcgiprocess.cpp')
-rw-r--r-- | plugins/fcgi/fastcgiprocess.cpp | 84 |
1 files changed, 49 insertions, 35 deletions
diff --git a/plugins/fcgi/fastcgiprocess.cpp b/plugins/fcgi/fastcgiprocess.cpp index dd51583..d43fa75 100644 --- a/plugins/fcgi/fastcgiprocess.cpp +++ b/plugins/fcgi/fastcgiprocess.cpp @@ -61,6 +61,51 @@ FastCGIProcess::~FastCGIProcess() stop(); } +void run_fcgi_app(const std::string& command, const std::string& host, unsigned short port) +{ + boost::asio::io_context ioc; + boost::asio::ip::tcp::resolver resolver(ioc); + boost::asio::ip::tcp::acceptor acceptor(ioc); + auto const results = resolver.resolve(host.c_str(), std::to_string(port).c_str()); + if (results.begin() == results.end()) + std::runtime_error("no resolve result"); + boost::asio::ip::tcp::endpoint endpoint{*results.begin()}; + acceptor.open(endpoint.protocol()); + acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); + acceptor.bind(endpoint); + acceptor.listen(); + int fd{acceptor.native_handle()}; + if (fd != FCGI_LISTENSOCK_FILENO) { + close(FCGI_LISTENSOCK_FILENO); + dup2(fd, FCGI_LISTENSOCK_FILENO); + close(fd); + } + + execl(command.c_str(), command.c_str(), (const char*)nullptr); +} + +void run_fcgi_app(const std::string& command, const std::filesystem::path& socket_path) +{ + boost::asio::io_context ioc; + boost::asio::local::stream_protocol::acceptor file_acceptor(ioc); + std::error_code ec; + fs::remove(socket_path, ec); // otherwise we get: "bind: Address already in use" + + boost::asio::local::stream_protocol::endpoint endpoint(socket_path); + file_acceptor.open(endpoint.protocol()); + file_acceptor.set_option(boost::asio::local::stream_protocol::acceptor::reuse_address(true)); + file_acceptor.bind(endpoint); + file_acceptor.listen(); + int fd {file_acceptor.native_handle()}; + if (fd != FCGI_LISTENSOCK_FILENO) { + close(FCGI_LISTENSOCK_FILENO); + dup2(fd, FCGI_LISTENSOCK_FILENO); + close(fd); + } + + execl(command.c_str(), command.c_str(), (const char*)nullptr); +} + void FastCGIProcess::start() { if (m_pid != 0) @@ -72,41 +117,10 @@ void FastCGIProcess::start() if (m_pid == 0) { // child process branch try { - int fd{}; - boost::asio::io_context ioc; - boost::asio::ip::tcp::resolver resolver(ioc); - boost::asio::ip::tcp::acceptor acceptor(ioc); - boost::asio::local::stream_protocol::acceptor file_acceptor(ioc); - - if (m_socket_path.empty()) { // tcp connection - auto const results = resolver.resolve(m_host.c_str(), std::to_string(m_port).c_str()); - if (results.begin() == results.end()) - std::runtime_error("no resolve result"); - boost::asio::ip::tcp::endpoint endpoint{*results.begin()}; - acceptor.open(endpoint.protocol()); - acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); - acceptor.bind(endpoint); - acceptor.listen(); - fd = acceptor.native_handle(); - } else { // unix domain socket - std::error_code ec; - fs::remove(m_socket_path, ec); // otherwise we get: "bind: Address already in use" - - boost::asio::local::stream_protocol::endpoint endpoint(m_socket_path); - file_acceptor.open(endpoint.protocol()); - file_acceptor.set_option(boost::asio::local::stream_protocol::acceptor::reuse_address(true)); - file_acceptor.bind(endpoint); - file_acceptor.listen(); - fd = file_acceptor.native_handle(); - } - - if (fd != FCGI_LISTENSOCK_FILENO) { - close(FCGI_LISTENSOCK_FILENO); - dup2(fd, FCGI_LISTENSOCK_FILENO); - close(fd); - } - - execl(m_command.c_str(), m_command.c_str(), (const char*)nullptr); + if (m_socket_path.empty()) + run_fcgi_app(m_command, m_host, m_port); + else + run_fcgi_app(m_command, m_socket_path); } catch (const std::exception& ex) { std::cout << "FastCGI process error: " << ex.what() << std::endl; } |