Skip to content

Commit 52762a2

Browse files
committed
feat: add SIGINT signal handler
1 parent 94baca6 commit 52762a2

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SpaceAfterCStyleCast: true
99
IndentAccessModifiers: false
1010
AccessModifierOffset: -4
1111

12-
AllowShortCaseLabelsOnASingleLine: true
12+
#AllowShortCaseLabelsOnASingleLine: true
1313
AllowShortFunctionsOnASingleLine: InlineOnly
1414
BreakConstructorInitializers: BeforeColon
1515
PackConstructorInitializers: Never

HttpResponse.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ std::string HttpResponse::to_string() const
88
std::string reason;
99

1010
switch (status) {
11-
case 200: reason = "OK"; break;
12-
case 404: reason = "Not Found"; break;
13-
default: reason = "Unknown"; break;
11+
case 200:
12+
reason = "OK";
13+
break;
14+
case 404:
15+
reason = "Not Found";
16+
break;
17+
default:
18+
reason = "Unknown";
19+
break;
1420
}
1521

1622
out << "HTTP/1.1 " << status << " " << reason << "\r\n";

Server.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,19 @@ Server::~Server()
6666

6767
void Server::run()
6868
{
69-
while (true) {
69+
while (!g_sigint_received) {
7070
int n_events = epoll_wait(epoll_fd_, events_, WEBSERV_MAX_EVENTS, -1);
71+
if (n_events == -1) {
72+
switch (errno) {
73+
case EINTR:
74+
// epoll_wait was interrupted by a signal, just move on as if
75+
// nothing happened.
76+
n_events = 0;
77+
break;
78+
default:
79+
throw SyscallError("epoll_wait", errno);
80+
}
81+
}
7182

7283
for (int i = 0; i < n_events; ++i) {
7384
Client* conn = (Client*) events_[i].data.ptr;

Server.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
#include <sys/epoll.h>
88
#include <sys/socket.h>
99

10+
#include <csignal>
11+
1012
#define WEBSERV_MAX_EVENTS 64
1113

14+
extern volatile sig_atomic_t g_sigint_received;
15+
1216
class Server {
1317
public:
1418
Server(in_addr_t ip, in_port_t port, int backlog);

main.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
2-
31
#include "Server.hpp"
42
#include "SyscallError.hpp"
53

64
#include <iostream>
75

6+
volatile sig_atomic_t g_sigint_received = 0;
7+
8+
extern "C" void handle_sigint(int sig)
9+
{
10+
(void) sig;
11+
g_sigint_received = 1;
12+
}
13+
814
int main(void)
915
{
1016
// later params retrieved from config file
1117
Server server(INADDR_ANY, 8080, 1000);
1218

19+
struct sigaction sa;
20+
sa.sa_handler = handle_sigint;
21+
sigemptyset(&sa.sa_mask);
22+
sa.sa_flags = 0;
23+
sigaction(SIGINT, &sa, NULL);
24+
1325
try {
1426
server.run();
1527
}

0 commit comments

Comments
 (0)