summaryrefslogtreecommitdiffhomepage
path: root/plugins/fcgi/fastcgiprocess.cpp
blob: dd5158338484cef1dc33b3859b674e38be64bca1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "fastcgiprocess.h"

#include <chrono>
#include <filesystem>
#include <iostream>
#include <string>
#include <thread>

#include <boost/algorithm/string.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/buffers_iterator.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/asio/local/stream_protocol.hpp>

#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>

#include <libreichwein/file.h>
#include <libreichwein/process.h>

using namespace std::string_literals;
namespace fs = std::filesystem;
namespace pt = boost::property_tree;
using namespace Reichwein;

#define FCGI_LISTENSOCK_FILENO 0

FastCGIProcess::FastCGIProcess(const std::filesystem::path& exe_path, const std::string& host, unsigned short port):
 m_pid{},
 m_command{exe_path.generic_string()},
 m_host{host},
 m_port{port}
{
 start();
}

FastCGIProcess::FastCGIProcess(const std::filesystem::path& exe_path, const fs::path& socket_path):
 m_pid{},
 m_command{exe_path.generic_string()},
 m_socket_path{socket_path}
{
 start();
}

FastCGIProcess::~FastCGIProcess()
{
 stop();
}

void FastCGIProcess::start()
{
 if (m_pid != 0)
  throw std::runtime_error("Process already running, so it can't be started");

 m_pid = fork();
 if (m_pid < 0)
  throw std::runtime_error("Fork unsuccessful.");

 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);
  } catch (const std::exception& ex) {
   std::cout << "FastCGI process error: " << ex.what() << std::endl;
  }
  exit(0);
 }

 // wait for server to start up
 if (m_socket_path.empty()) { // tcp connection
  Process::wait_for_pid_listening_on(m_pid, m_port);
 } else { // unix domain socket
  Process::wait_for_pid_listening_on(m_pid, m_socket_path);
 }
 std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

void FastCGIProcess::stop()
{
 if (m_pid == 0)
  throw std::runtime_error("Process not running, so it can't be stopped");
 
 if (kill(m_pid, SIGTERM) != 0)
  throw std::runtime_error("Unable to kill process");

 if (int result = waitpid(m_pid, NULL, 0); result != m_pid)
  throw std::runtime_error("waitpid returned "s + std::to_string(result));

 if (!m_socket_path.empty()) {
  std::error_code ec;
  fs::remove(m_socket_path, ec);
 }

 m_pid = 0;
}

bool FastCGIProcess::is_running()
{
 if (m_pid == 0)
  return false;

 return Process::is_running(m_pid);
}