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