feat(SIGTERM) is this not to global for you sir?

This commit is contained in:
whaffman 2025-10-26 12:10:17 +01:00
parent be39dfa4c4
commit 5e9243b064
3 changed files with 27 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <iostream> // for ios_base #include <iostream> // for ios_base
#include <string> // for allocator, basic_string, char_traits, operator+, string #include <string> // for allocator, basic_string, char_traits, operator+, string
#include <vector> // for vector #include <vector> // for vector
#include <csignal>
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -37,6 +38,7 @@ int main(int argc, char **argv)
Log::debug("ConfigManager initialized successfully."); Log::debug("ConfigManager initialized successfully.");
Server server(configManager); Server server(configManager);
::signal(SIGINT, Server::signalHandler);
server.run(); server.run();
return 0; return 0;
} }

View File

@ -25,9 +25,12 @@
#include <sys/socket.h> // for send, SOMAXCONN #include <sys/socket.h> // for send, SOMAXCONN
#include <sys/types.h> // for ssize_t #include <sys/types.h> // for ssize_t
#include <unistd.h> // for close #include <unistd.h> // for close
#include <csignal>
class Router; class Router;
volatile sig_atomic_t Server::stop_ = 0;
Server::Server(const ConfigManager &configManager) : epoll_fd_(epoll_create1(O_CLOEXEC)), configManager_(configManager) Server::Server(const ConfigManager &configManager) : epoll_fd_(epoll_create1(O_CLOEXEC)), configManager_(configManager)
{ {
Log::trace(LOCATION); Log::trace(LOCATION);
@ -269,6 +272,11 @@ void Server::handleEpoll(struct epoll_event *events, int max_events)
int nfds = epoll_wait(epoll_fd_, events, max_events, 10); // NOLINT int nfds = epoll_wait(epoll_fd_, events, max_events, 10); // NOLINT
if (nfds == -1) if (nfds == -1)
{ {
if (errno == EINTR)
{
Log::debug("epoll_wait interrupted by signal, continuing...");
return;
}
Log::error("epoll_wait failed"); Log::error("epoll_wait failed");
throw std::runtime_error("epoll_wait failed"); throw std::runtime_error("epoll_wait failed");
} }
@ -306,8 +314,22 @@ void Server::run()
struct epoll_event events[MAX_EVENTS]; // NOLINT struct epoll_event events[MAX_EVENTS]; // NOLINT
while (true) while (true)
{ {
if (stop_ != 0)
{
Log::info("Server stopping...");
break;
}
pollSockets(); pollSockets();
pollClients(); pollClients();
handleEpoll(events, MAX_EVENTS); // NOLINT (cppcoreguidelines-pro-bounds-pointer-arithmetic) handleEpoll(events, MAX_EVENTS); // NOLINT (cppcoreguidelines-pro-bounds-pointer-arithmetic)
} }
} }
void Server::signalHandler(int signum)
{
if (signum == SIGINT)
{
stop_ = signum;
}
}

View File

@ -7,6 +7,7 @@
#include <webserv/socket/ASocket.hpp> #include <webserv/socket/ASocket.hpp>
#include <webserv/socket/ServerSocket.hpp> // for ServerSocket #include <webserv/socket/ServerSocket.hpp> // for ServerSocket
#include <csignal>
#include <cstdint> // for uint32_t #include <cstdint> // for uint32_t
#include <memory> // for unique_ptr #include <memory> // for unique_ptr
#include <set> // for set #include <set> // for set
@ -31,6 +32,7 @@ class Server
Server &operator=(Server &&other) noexcept = delete; Server &operator=(Server &&other) noexcept = delete;
~Server(); ~Server();
static void signalHandler(int signum);
void run(); void run();
void add(ASocket &socket, Client *client = nullptr); void add(ASocket &socket, Client *client = nullptr);
@ -44,6 +46,7 @@ class Server
private: private:
int epoll_fd_; int epoll_fd_;
static volatile sig_atomic_t stop_;
const ConfigManager &configManager_; const ConfigManager &configManager_;
std::vector<std::unique_ptr<ServerSocket>> listeners_; std::vector<std::unique_ptr<ServerSocket>> listeners_;
std::set<int> listener_fds_; std::set<int> listener_fds_;