Skip to content

Commit 579b2df

Browse files
committed
Handling SIGINT, 60% of the time it works every time.
1 parent c3ba336 commit 579b2df

File tree

8 files changed

+91
-4
lines changed

8 files changed

+91
-4
lines changed

src/Config.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include <string.h>
66
#include <vector>
77
#include <map>
8+
#include <signal.h>
89
#include "Config.h"
10+
#include "globals.h"
911
#define CONFIG_SIZE 500
1012
/*
1113
* config - loads config file on demand, keeps the config in shared RO memory for fast access
@@ -16,6 +18,8 @@ std::map<string, string> configMap;
1618
Config::Config() {
1719
loadConfigFileToMap();
1820
}
21+
22+
1923
void Config::loadConfigFileToMap(){
2024
int configFileDescriptor = open("./config", O_RDONLY);
2125
char configFileContents[CONFIG_SIZE];

src/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <iostream>
44
#include <exception>
55
#include <vector>
6+
#include <signal.h>
7+
68

79
class Config {
810
private:

src/Router.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Config.h"
77
#include "Router.h"
88
#include "Manager.h"
9+
#include "globals.h"
910

1011
/*
1112
* router - watches the designated port and for launches a new worker for every connection
@@ -22,6 +23,7 @@ Router::Router(int qsize, std::string port) {
2223
}
2324

2425
Router::~Router() {
26+
this->logger->info("Destructor called");
2527
// close our socket
2628
close(this->listening_socket_fd);
2729
// free the address
@@ -48,15 +50,21 @@ void Router::watch() {
4850
Manager manager;
4951

5052
// we work until we're told to stop working
51-
while (true) {
53+
while (!isSigintRecieved) {
5254
// listen for new connections
5355
listen(this->listening_socket_fd, this->queue_size);
5456

5557
addr_size = sizeof client;
5658
handling_socket = accept(this->listening_socket_fd, (struct sockaddr *) &client, &addr_size);
59+
cout << std::strerror(errno) << endl;
5760
if (handling_socket < 0) {
58-
char * err = std::strerror(errno);
59-
throw Router::Exception("Error when accepting connection: " + std::string(err ? err : "unknown error"));
61+
if(errno == EINTR){
62+
this->logger->info("Accept() interrupted by signal");
63+
break;
64+
} else {
65+
char *err = std::strerror(errno);
66+
throw Router::Exception("Error when accepting connection: " + std::string(err ? err : "unknown error"));
67+
}
6068
}
6169

6270
struct sockaddr_in *sin = (struct sockaddr_in *)&client;

src/SignalHandler.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Created by atticus on 12/9/15.
3+
//
4+
5+
#include <string.h>
6+
#include <signal.h>
7+
#include <iostream>
8+
#include "SignalHandler.h"
9+
#include "globals.h"
10+
11+
void SignalHandler::signalHandler(int sig, siginfo_t *siginfo, void *context){
12+
std::cout << "Signal recieved: SignalHandler" << std::endl;
13+
isSigintRecieved = true;
14+
}
15+
16+
SignalHandler::~SignalHandler() {
17+
18+
}
19+
20+
SignalHandler::SignalHandler() {
21+
22+
struct sigaction act;
23+
memset (&act, '\0', sizeof(act));
24+
/* Use the sa_sigaction field because the handles has two additional parameters */
25+
act.sa_sigaction = &signalHandler;
26+
/* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
27+
act.sa_flags = SA_SIGINFO;
28+
29+
if (sigaction(SIGINT, &act, NULL) < 0) {
30+
std::cout <<"Signal error" <<std::endl;
31+
}
32+
}

src/SignalHandler.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Created by atticus on 12/9/15.
3+
//
4+
5+
#ifndef HACKTTP_SIGNALHANDLER_H
6+
#define HACKTTP_SIGNALHANDLER_H
7+
8+
#include <signal.h>
9+
10+
class SignalHandler{
11+
public:
12+
virtual ~SignalHandler();
13+
SignalHandler();
14+
static void signalHandler(int, siginfo_t*, void*);
15+
};
16+
17+
#endif //HACKTTP_SIGNALHANDLER_H

src/Worker.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Worker::Worker(int socket_fd) {
2020

2121
Worker::~Worker() {
2222
// make sure to close the socket when we finish
23+
this->logger->info("Destructor called");
2324
close(this->socket_fd);
2425
delete(this->logger);
2526
}
@@ -32,6 +33,11 @@ void Worker::handle_request() {
3233
// first read the request
3334
ssize_t request_size = recv(this->socket_fd, request, HTTP_REQUEST_LENGTH, 0);
3435
if(request_size < 0) {
36+
// TODO not sure if necessary
37+
if(errno == EINTR){
38+
this->logger->info("Recv() interrupted by signal");
39+
return;
40+
}
3541
// TODO replace with proper HTTP response
3642
char * err = std::strerror(errno);
3743
throw Worker::Exception("Error while reading request: " + std::string(err ? err : "unknown error"));

src/globals.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// Created by atticus on 12/8/15.
3+
//
4+
5+
#ifndef HACKTTP_GLOBALS_H_H
6+
#define HACKTTP_GLOBALS_H_H
7+
8+
extern bool isSigintRecieved;
9+
10+
#endif //HACKTTP_GLOBALS_H_H

src/server.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
#include <unistd.h>
2+
#include <string.h>
13
#include "CmdLine.h"
24
#include "Config.h"
35
#include "Logger.h"
46
#include "Router.h"
7+
#include "globals.h"
8+
#include "SignalHandler.h"
59

610
/*
711
* "main" - should use other modules for everything, that is:
@@ -10,15 +14,18 @@
1014
* - bind the router tho the receiving port and give it control
1115
* - when router finishes, log and shutdown
1216
*/
17+
bool isSigintRecieved;
1318

1419
int main() {
20+
isSigintRecieved = false;
1521
// Init basic classes
22+
SignalHandler signalHandler;
1623
CmdLine cmdline;
1724
Config config;
1825
Logger logger("Server");
1926
//"hello from server.cpp" message, to be removed
2027
logger.info(config.get_str_setting("config_test"));
21-
28+
logger.info("Process id: " + std::to_string(getpid()));
2229
if(config.get_str_setting("start_full_server") == "true") {
2330
// starting the full server
2431
std::string port = config.get_str_setting("port");
@@ -36,6 +43,7 @@ int main() {
3643
logger.info("Running in test mode");
3744
}
3845
// cleanup
46+
3947
logger.info("HackTTP shutting down");
4048

4149
return 0;

0 commit comments

Comments
 (0)