Skip to content

Commit 27a5978

Browse files
author
Yieen
committed
catch Log::write() exceptions and add O_NONBLOCK flags to pipes
1 parent 68b1137 commit 27a5978

File tree

6 files changed

+67
-28
lines changed

6 files changed

+67
-28
lines changed

include/poll/AConnection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef ACONNECTION_HPP
22
#define ACONNECTION_HPP
33

4+
#include <fcntl.h>
5+
46
#include <istream>
57
#include <queue>
68
#include <string>

src/http/Http.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ Http::Http(Address const &client, Address const &host)
1414
}
1515

1616
Http::~Http() {
17-
accessLog_g.write(
18-
toString<Address &>(host) + " -> delete: " + toString<Address &>(client),
19-
DEBUG);
17+
try {
18+
accessLog_g.write(toString<Address &>(host) +
19+
" -> delete: " + toString<Address &>(client),
20+
DEBUG);
21+
} catch (...) {
22+
}
2023
}
2124

2225
void Http::OnRequestRecv(std::string msg) {

src/main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ int main(int argc, char** argv) {
4747
if (!Poll::poll()) break;
4848
}
4949
} catch (const std::exception& e) {
50-
if (errorLog_g.getInitialized() == false) Log::setLogToTerminal(true, true);
51-
errorLog_g.write(e.what(), ERROR);
52-
printHelp(PRINT);
50+
try {
51+
if (errorLog_g.getInitialized() == false)
52+
Log::setLogToTerminal(true, true);
53+
errorLog_g.write(e.what(), ERROR);
54+
printHelp(PRINT);
55+
} catch (...) {
56+
}
5357
return 1;
5458
}
5559
return 0;

src/poll/AConnection.cpp

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ AConnection::~AConnection() {
3333
}
3434
if (_cgiPid != -1 && Poll::lastForkPid() != 0) {
3535
int status;
36-
if (kill(_cgiPid, SIGKILL) == -1)
37-
errorLog_g.write("ERROR: kill()", DEBUG, BRIGHT_RED);
36+
kill(_cgiPid, SIGKILL);
3837
waitpid(_cgiPid, &status, 0);
39-
accessLog_g.write(
40-
"reaped CGI process: " + toString<int>(_cgiPid) + " exit status: " +
41-
toString<int>(WEXITSTATUS(status)) + " reason: object destructed",
42-
DEBUG, BLUE);
38+
try {
39+
accessLog_g.write(
40+
"reaped CGI process: " + toString<int>(_cgiPid) + " exit status: " +
41+
toString<int>(WEXITSTATUS(status)) + " reason: object destructed",
42+
DEBUG, BLUE);
43+
} catch (...) {
44+
}
4345
}
4446
}
4547

@@ -259,8 +261,7 @@ void AConnection::onPipeInPollIn(struct pollfd &pollfd) {
259261
void AConnection::KillCgi() {
260262
int status;
261263

262-
if (kill(_cgiPid, SIGKILL) == -1)
263-
errorLog_g.write("ERROR: kill()", DEBUG, BRIGHT_RED);
264+
kill(_cgiPid, SIGKILL);
264265
waitpid(_cgiPid, &status, 0);
265266
pid_t tmp = _cgiPid;
266267
_cgiPid = -1;
@@ -377,6 +378,34 @@ void AConnection::onNoPollEvent(struct pollfd &) {
377378
Poll::setTimeout(timeout);
378379
}
379380

381+
static bool initPipes(int a[2], int b[2]) {
382+
static int const fdsize = 4;
383+
int fd[fdsize];
384+
int flags[fdsize];
385+
386+
if (pipe(&(fd[0])) == -1) return false;
387+
if (pipe(&(fd[2])) == -1) {
388+
close(fd[0]);
389+
close(fd[1]);
390+
return false;
391+
}
392+
for (int i = 0; i < fdsize; ++i) {
393+
flags[i] = fcntl(fd[i], F_GETFL, 0);
394+
if (flags[i] == -1 || fcntl(fd[i], F_SETFL, flags[i] | O_NONBLOCK) == -1) {
395+
close(fd[0]);
396+
close(fd[1]);
397+
close(fd[2]);
398+
close(fd[3]);
399+
return false;
400+
}
401+
}
402+
a[0] = fd[0];
403+
a[1] = fd[1];
404+
b[0] = fd[2];
405+
b[1] = fd[3];
406+
return true;
407+
}
408+
380409
/**
381410
* The http object is destroyed after calling fork(). The parameters must still
382411
* remain valid. program cannot be a reference to Http::cgiProgramPathname
@@ -388,16 +417,9 @@ void AConnection::runCGI(std::string program,
388417
int pipeInArray[2];
389418
int pipeOutArray[2];
390419

391-
if (pipe(pipeInArray) == -1) {
392-
errorLog_g.write("ERROR: pipe()", DEBUG, BRIGHT_RED);
393-
OnCgiError();
394-
return;
395-
}
396-
397-
if (pipe(pipeOutArray)) {
398-
errorLog_g.write("ERROR: pipe()", DEBUG, BRIGHT_RED);
399-
close(pipeInArray[0]);
400-
close(pipeInArray[1]);
420+
if (initPipes(pipeInArray, pipeOutArray) == false) {
421+
errorLog_g.write(std::string("initPipes(): ") + std::strerror(errno),
422+
ERROR);
401423
OnCgiError();
402424
return;
403425
}

src/poll/ListenSocket.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ void ListenSocket::onPollEvent(struct pollfd &pollfd,
9797
} catch (std::bad_alloc const &e) {
9898
close(newPollfd->fd);
9999
newPollfd->fd = -1;
100-
errorLog_g.write("Failed to accept client: ENOMEM", DEBUG,
101-
BRIGHT_RED); // TODO: implement counter
100+
try {
101+
errorLog_g.write("Failed to accept client: ENOMEM", DEBUG, BRIGHT_RED);
102+
} catch (...) {
103+
}
102104
}
103105
}

src/poll/Poll.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ bool Poll::poll() {
7373
throw std::runtime_error(std::string("Poll::poll(): ") +
7474
std::strerror(errno));
7575

76-
if (ready == 0) errorLog_g.write("Poll: no revents", DEBUG, BRIGHT_RED);
76+
if (ready == 0) try {
77+
errorLog_g.write("Poll: no revents", DEBUG, BRIGHT_RED);
78+
} catch (...) {
79+
}
7780
poll.timeout = -1;
7881
poll.iterate();
7982
return true;
@@ -166,7 +169,10 @@ void Poll::iterate() {
166169
++i;
167170
}
168171
} catch (std::exception const &e) {
169-
accessLog_g.write(std::string(e.what()), DEBUG);
172+
try {
173+
accessLog_g.write(std::string(e.what()), DEBUG);
174+
} catch (...) {
175+
}
170176
remove(callbackObjects[i].ptr);
171177
release(newCallbackObject, newPollfd,
172178
sizeof(newPollfd) / sizeof(*newPollfd));

0 commit comments

Comments
 (0)