Skip to content

Commit fd99448

Browse files
committed
Working correctly now, including correct Server thread termination handling
1 parent f5d71d4 commit fd99448

3 files changed

Lines changed: 65 additions & 29 deletions

File tree

src/ResultProcessor.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ void ResultProcessor::terminate(int _ignored)
3131

3232
int ResultProcessor::_getFDFromParent(uint16_t fd)
3333
{
34-
// Send the requested FD number to the parent
34+
// send the requested FD number to the parent
3535
if (write(_FDPassingSocketFD, &fd, sizeof(fd)) != sizeof(fd)) {
3636
ERR("Failed to send FD request to parent");
3737
return -1;
3838
}
39-
39+
4040
// Receive the FD from the parent
4141
int received_fd = Syscall::recvFD(_FDPassingSocketFD);
42-
42+
4343
if (received_fd < 0) {
4444
ERR("Failed to receive FD from parent");
4545
return -1;
@@ -79,12 +79,12 @@ pid_t ResultProcessor::forkProcessResultProcessor(ResultProcessorSHMPointer_t SH
7979
//- connect to parent's FD passing server
8080
const char* socket_path = "/tmp/falcon-fd-passing.sock";
8181
_FDPassingSocketFD = Syscall::connectFDPassingClient(socket_path);
82-
82+
8383
if (_FDPassingSocketFD < 0) {
8484
ERR("ResultProcessor: Failed to connect to FD passing server");
8585
exit(1);
8686
}
87-
87+
8888
DBG(120, "ResultProcessor: Connected to FD passing server");
8989

9090
//- overwrite parent termination handler
@@ -93,6 +93,9 @@ pid_t ResultProcessor::forkProcessResultProcessor(ResultProcessorSHMPointer_t SH
9393
//- spectre userspace protection
9494
prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_INDIRECT_BRANCH, PR_SPEC_ENABLE, 0, 0);
9595

96+
//- drop privileges
97+
Permission::dropPrivileges(RUNAS_USER_DEFAULT, RUNAS_GROUP_DEFAULT);
98+
9699
DBG(120, "Child ResultProcessor Process PID:" << getpid() << " FDPassingSocketFD:" << _FDPassingSocketFD);
97100
DBG(120, "Child ResultProcessor SharedMemAddress:" << SHMAdresses.StaticFSPtr);
98101
DBG(120, "Child ResultProcessor Atomic Address:" << StaticFSLock << " Value:" << *(StaticFSLock));
@@ -148,7 +151,11 @@ pid_t ResultProcessor::forkProcessResultProcessor(ResultProcessorSHMPointer_t SH
148151
}
149152
}
150153

151-
DBG(-1, "Exit Parent ResultProcessor Process.");
154+
DBG(-1, "Parent ResultProcessor closing Unix Domain Socket.");
155+
156+
close(_FDPassingSocketFD);
157+
158+
DBG(-1, "Exit parent ResultProcessor process.");
152159
exit(0);
153160
}
154161
}
@@ -198,7 +205,6 @@ uint16_t ResultProcessor::_processPythonASResults()
198205
{
199206
uint16_t processed = 0;
200207

201-
//for (const auto& Namespace: _Namespaces) {
202208
for (const auto& Namespace: ConfigRef.Namespaces) {
203209
for (const auto &Index: _VHostOffsetsPrecalc.at(Namespace.first)) {
204210
atomic_uint16_t* CanReadAddr = static_cast<atomic_uint16_t*>(getMetaAddress(Index, 0));

src/Server.cpp

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using namespace std;
44

55
static bool RunServer = true;
6+
static std::thread _FDPassingThread;
7+
68
Configuration ConfigRef = Configuration();
79

810
std::vector<pid_t> Server::ChildPIDs;
@@ -41,7 +43,7 @@ void Server::init()
4143
//- set client handler namespaces
4244
setClientHandlerConfig();
4345

44-
//- TODO: just set if exists in config, else default
46+
//- TODO: only set if exists in config, else default
4547
SocketListenAddress = ConfigRef.ServerAddress;
4648
SocketListenPort = ConfigRef.ServerPort;
4749

@@ -67,7 +69,7 @@ void Server::init()
6769

6870
//- fork result processor process
6971
ResultProcessor::setVHostOffsets(ASRequestHandlerRef.getOffsetsPrecalc());
70-
pid_t resultProcessorPID = ResultProcessor::forkProcessResultProcessor( { _SHMStaticFS, _SHMPythonASMeta, _SHMPythonASRequests, _SHMPythonASResults } );
72+
const pid_t resultProcessorPID = ResultProcessor::forkProcessResultProcessor( { _SHMStaticFS, _SHMPythonASMeta, _SHMPythonASRequests, _SHMPythonASResults } );
7173
if (resultProcessorPID > 0) {
7274
addChildPID(resultProcessorPID);
7375
}
@@ -77,7 +79,7 @@ void Server::init()
7779
forkProcessASHandler( { _SHMPythonASMeta, _SHMPythonASRequests, _SHMPythonASResults } );
7880

7981
//- check interpreter count
80-
uint ASInterpreterCount = getASInterpreterCount();
82+
const uint ASInterpreterCount = getASInterpreterCount();
8183
DBG(50, "Sum AS Interpreters:" << ASInterpreterCount);
8284

8385
//- apply cpu bound processing
@@ -116,8 +118,23 @@ void Server::terminateChildren()
116118
void Server::terminate(int _ignored)
117119
{
118120
DBG(-1, "SIGTERM Main Server received, shutting down");
119-
terminateChildren();
120121
RunServer = false;
122+
terminateChildren();
123+
124+
if (_FDPassingThread.joinable()) {
125+
_FDPassingThread.join();
126+
}
127+
128+
std::this_thread::sleep_for(chrono::milliseconds(100));
129+
130+
if (_FDPassingThread.joinable()) {
131+
_FDPassingThread.join();
132+
}
133+
134+
std::this_thread::sleep_for(chrono::milliseconds(100));
135+
if (_FDPassingThread.joinable()) {
136+
_FDPassingThread.join();
137+
}
121138
}
122139

123140
void Server::setupSocket()
@@ -266,7 +283,7 @@ void Server::setupFDPassingServer()
266283

267284
DBG(120, "FD Passing Server socket created at:" << socket_path);
268285

269-
// Start thread to handle FD passing requests
286+
// start thread to handle FD passing requests
270287
_FDPassingThread = std::thread(&Server::handleFDPassingRequests, this);
271288
}
272289

@@ -278,35 +295,36 @@ void Server::handleFDPassingRequests()
278295
int flags = fcntl(_FDPassingServerFD, F_GETFL, 0);
279296
fcntl(_FDPassingServerFD, F_SETFL, flags | O_NONBLOCK);
280297

281-
std::vector<int> client_fds;
298+
std::vector<int> ClientFDs;
282299

283300
while(RunServer) {
284301

285302
struct sockaddr_un client_addr;
286303
socklen_t client_len = sizeof(client_addr);
304+
int NewClientFD = accept(_FDPassingServerFD, (struct sockaddr*)&client_addr, &client_len);
305+
306+
if (NewClientFD >= 0) {
307+
DBG(120, "FD passing new client connected, fd:" << NewClientFD);
287308

288-
int new_client_fd = accept(_FDPassingServerFD, (struct sockaddr*)&client_addr, &client_len);
309+
// set client_fd non-blocking
310+
int flags = fcntl(NewClientFD, F_GETFL, 0);
311+
fcntl(NewClientFD, F_SETFL, flags | O_NONBLOCK);
289312

290-
if (new_client_fd >= 0) {
291-
DBG(120, "FD passing new client connected, fd:" << new_client_fd);
292-
client_fds.push_back(new_client_fd);
313+
ClientFDs.push_back(NewClientFD);
293314
}
294315

295-
auto it = client_fds.begin();
296-
while (it != client_fds.end()) {
316+
auto it = ClientFDs.begin();
317+
while (it != ClientFDs.end()) {
297318
int client_fd = *it;
298319
uint16_t requested_fd;
299-
300320
ssize_t n = read(client_fd, &requested_fd, sizeof(requested_fd));
301-
302321
if (n == sizeof(requested_fd)) {
303322
DBG(120, "FD passing request for FD:" << requested_fd);
304-
305323
// send the requested FD to the client
306324
if (Syscall::sendFD(client_fd, requested_fd) < 0) {
307325
ERR("Failed to send FD:" << requested_fd);
308326
close(client_fd);
309-
it = client_fds.erase(it);
327+
it = ClientFDs.erase(it);
310328
} else {
311329
DBG(120, "Successfully sent FD:" << requested_fd);
312330
++it;
@@ -315,25 +333,38 @@ void Server::handleFDPassingRequests()
315333
// connection closed
316334
DBG(120, "FD passing client disconnected, fd:" << client_fd);
317335
close(client_fd);
318-
it = client_fds.erase(it);
336+
it = ClientFDs.erase(it);
319337
} else if (n < 0) {
320338
if (errno != EAGAIN && errno != EWOULDBLOCK) {
321339
ERR("Failed to read requested FD number:" << strerror(errno));
322340
close(client_fd);
323-
it = client_fds.erase(it);
341+
it = ClientFDs.erase(it);
324342
} else {
325343
++it;
326344
}
327345
}
328346
}
329-
330347
std::this_thread::sleep_for(chrono::microseconds(IDLE_SLEEP_MICROSECONDS));
331348
}
332349

350+
DBG(120, "FD Passing handler thread exiting");
351+
352+
std::this_thread::sleep_for(chrono::milliseconds(50));
353+
354+
DBG(120, "FD Passing handler thread closing client connections");
355+
333356
// clean up
334-
for (int fd : client_fds) {
357+
//close(client_fd);
358+
for (const int& fd : ClientFDs) {
335359
close(fd);
336360
}
337361

338-
DBG(120, "FD Passing handler thread exiting");
362+
std::this_thread::sleep_for(chrono::milliseconds(50));
363+
364+
DBG(120, "FD Passing handler thread closing serve unix domain socket");
365+
366+
close(_FDPassingServerFD);
367+
unlink("/tmp/falcon-fd-passing.sock");
368+
369+
DBG(120, "FD Passing handler thread finally exiting");
339370
}

src/Server.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class Server : private ResultProcessor, private ASProcessHandler, private Client
6262
void* _SHMPythonASResults;
6363

6464
int _FDPassingServerFD;
65-
std::thread _FDPassingThread;
6665

6766
static std::vector<pid_t> ChildPIDs;
6867

0 commit comments

Comments
 (0)