SIGINT handling

+ bug with persistent connection, recv() try to read fd 0 (why?)
This commit is contained in:
LuckyLaszlo
2022-07-25 07:40:41 +02:00
parent dcf94c2c61
commit 26dad07a87

View File

@@ -2,6 +2,7 @@
#include "Webserv.hpp" #include "Webserv.hpp"
int g_last_signal; int g_last_signal;
bool g_run;
void signal_handler(int signum) void signal_handler(int signum)
{ {
g_last_signal = signum; g_last_signal = signum;
@@ -19,7 +20,7 @@ Webserv::Webserv()
} }
std::signal(SIGPIPE, signal_handler); std::signal(SIGPIPE, signal_handler);
// std::signal(SIGINT, signal_handler); std::signal(SIGINT, signal_handler);
} }
/* Webserv::Webserv(Webserv const &src) /* Webserv::Webserv(Webserv const &src)
@@ -66,11 +67,18 @@ void Webserv::start()
int nfds; int nfds;
int i; int i;
int count_loop = 0; int count_loop = 0;
std::cerr << ++count_loop << "----loop epoll()\n";
while ( (nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT)) != -1) g_run = true;
while (g_run)
{ {
if (nfds == 0) std::cerr << ++count_loop << "----loop epoll()\n";
nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT);
if (nfds == -1)
{
std::perror("err epoll_wait(): ");
throw std::runtime_error("Epoll wait");
}
else if (nfds == 0)
{ {
if (!_clients.empty()) if (!_clients.empty())
{ {
@@ -78,7 +86,6 @@ void Webserv::start()
_close_all_clients(); _close_all_clients();
} }
} }
i = 0; i = 0;
while (i < nfds) while (i < nfds)
{ {
@@ -92,14 +99,8 @@ void Webserv::start()
++i; ++i;
_actual_client = NULL; _actual_client = NULL;
} }
std::cerr << ++count_loop << "----loop epoll()\n";
} }
if (nfds == -1)
{
std::perror("err epoll_wait(): ");
throw std::runtime_error("Epoll wait");
}
} }
@@ -141,6 +142,13 @@ void Webserv::_read_request(Client *client)
if (ret == -1) if (ret == -1)
{ {
std::perror("err recv(): "); std::perror("err recv(): ");
// if (g_last_signal)
// _handle_last_signal();
// else
// _close_client(client->fd);
std::cerr << "client ptr =" << client << "\n"; // DEBUG
std::cerr << "client.fd =" << client->fd << "\n"; // DEBUG
return ; return ;
} }
/* /*
@@ -170,11 +178,14 @@ void Webserv::_send_response(Client *client)
std::perror("err send(): "); std::perror("err send(): ");
if (g_last_signal) if (g_last_signal)
_handle_last_signal(); _handle_last_signal();
// else
// _close_client(client->fd);
return ; return ;
} }
// if (client->raw_request.find("Connection: keep-alive") == std::string::npos)
_close_client(client->fd); _close_client(client->fd);
// if (client->raw_request.find("Connection: keep-alive") == std::string::npos)
// _close_client(client->fd);
// else // else
// _epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD, client); // _epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD, client);
@@ -224,9 +235,10 @@ void Webserv::_handle_last_signal()
_actual_client = NULL; _actual_client = NULL;
} }
} }
// else if (g_last_signal == SIGINT) else if (g_last_signal == SIGINT)
// { {
// } g_run = false;
}
g_last_signal = 0; g_last_signal = 0;
} }
@@ -237,6 +249,7 @@ void Webserv::_close_client(int fd)
{ {
if (it->fd == fd) if (it->fd == fd)
{ {
// _epoll_update(fd, 0, EPOLL_CTL_DEL); // normalement superflu, DEBUG
if (::close(fd) == -1) if (::close(fd) == -1)
std::perror("err close(): "); std::perror("err close(): ");
else else
@@ -252,6 +265,7 @@ void Webserv::_close_all_clients()
{ {
while (!_clients.empty()) while (!_clients.empty())
{ {
// _epoll_update(_clients.back().fd, 0, EPOLL_CTL_DEL); // normalement superflu, DEBUG
if (::close(_clients.back().fd) == -1) if (::close(_clients.back().fd) == -1)
std::perror("err close(): "); std::perror("err close(): ");
else else