Skip to content

Commit ee29878

Browse files
committed
SIGUSR1 reloads Config on next use
1 parent 579b2df commit ee29878

File tree

6 files changed

+66
-17
lines changed

6 files changed

+66
-17
lines changed

src/Config.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@ std::map<string, string> configMap;
1717

1818
Config::Config() {
1919
loadConfigFileToMap();
20+
printMapContents();
2021
}
2122

2223

2324
void Config::loadConfigFileToMap(){
25+
configMap.clear();
26+
std::string configString;
27+
std::string delimiter;
28+
std::string configRelevantString;
29+
std::string configLine;
30+
std::string key;
31+
size_t position;
32+
2433
int configFileDescriptor = open("./config", O_RDONLY);
2534
char configFileContents[CONFIG_SIZE];
2635
read(configFileDescriptor, &configFileContents, CONFIG_SIZE);
@@ -52,11 +61,23 @@ void Config::loadConfigFileToMap(){
5261
}
5362
}
5463

64+
void Config::printMapContents(){
65+
cout << "Currently loaded Config:" << endl;
66+
for (const auto &p : configMap) {
67+
std::cout << "[" << p.first << "] = " << p.second << '\n';
68+
}
69+
}
70+
5571
Config::~Config() {
5672
}
5773

5874

5975
int Config::get_int_setting(std::string setting_name) {
76+
if(isSigusr1Recieved){
77+
Config::loadConfigFileToMap();
78+
Config::printMapContents();
79+
isSigusr1Recieved = false;
80+
}
6081
int returnInt = -1;
6182
try {
6283
returnInt = std::stoi(configMap.at(setting_name));
@@ -70,6 +91,11 @@ int Config::get_int_setting(std::string setting_name) {
7091

7192

7293
std::string Config::get_str_setting(std::string setting_name) {
94+
if(isSigusr1Recieved){
95+
Config::loadConfigFileToMap();
96+
Config::printMapContents();
97+
isSigusr1Recieved = false;
98+
}
7399
std::string returnString;
74100

75101
try{

src/Config.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ class Config {
1010
private:
1111
// to be replaced with some dynamic map struct
1212
int queue_size = 10;
13-
std::string configString;
14-
std::string delimiter;
15-
std::string configRelevantString;
16-
std::string configLine;
17-
std::string key;
18-
size_t position;
19-
void loadConfigFileToMap();
13+
static std::string configString;
14+
static std::string delimiter;
15+
static std::string configRelevantString;
16+
static std::string configLine;
17+
static std::string key;
18+
static size_t position;
19+
static void loadConfigFileToMap();
20+
static void printMapContents();
2021

2122
public:
2223
Config();

src/SignalHandler.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,47 @@
88
#include "SignalHandler.h"
99
#include "globals.h"
1010

11-
void SignalHandler::signalHandler(int sig, siginfo_t *siginfo, void *context){
12-
std::cout << "Signal recieved: SignalHandler" << std::endl;
11+
bool isSigintRecieved;
12+
bool isSigusr1Recieved;
13+
14+
void SignalHandler::sigintHandler(int sig, siginfo_t *siginfo, void *context){
15+
std::cout << "SignalHandler: Signal recieved - SIGINT" << std::endl;
1316
isSigintRecieved = true;
1417
}
1518

19+
void SignalHandler::sigusr1Handler(int sig, siginfo_t *siginfo, void *context){
20+
std::cout << "SignalHandler: Signal recieved - SIGUSR1, Config will be reloaded on next use." << std::endl;
21+
isSigusr1Recieved = true;
22+
}
23+
1624
SignalHandler::~SignalHandler() {
1725

1826
}
1927

2028
SignalHandler::SignalHandler() {
29+
isSigintRecieved = false;
30+
isSigusr1Recieved = false;
31+
32+
struct sigaction sigintStruct;
33+
memset (&sigintStruct, '\0', sizeof(sigintStruct));
34+
/* Use the sa_sigaction field because the handles has two additional parameters */
35+
sigintStruct.sa_sigaction = &sigintHandler;
36+
/* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
37+
sigintStruct.sa_flags = SA_SIGINFO;
2138

22-
struct sigaction act;
23-
memset (&act, '\0', sizeof(act));
39+
if (sigaction(SIGINT, &sigintStruct, NULL) < 0) {
40+
std::cout <<"Signal error" <<std::endl;
41+
}
42+
43+
struct sigaction sigusr1Struct;
44+
memset (&sigusr1Struct, '\0', sizeof(sigusr1Struct));
2445
/* Use the sa_sigaction field because the handles has two additional parameters */
25-
act.sa_sigaction = &signalHandler;
46+
sigusr1Struct.sa_sigaction = &sigusr1Handler;
2647
/* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
27-
act.sa_flags = SA_SIGINFO;
48+
sigusr1Struct.sa_flags = SA_SIGINFO | SA_RESTART;
2849

29-
if (sigaction(SIGINT, &act, NULL) < 0) {
50+
if (sigaction(SIGUSR1, &sigusr1Struct, NULL) < 0) {
3051
std::cout <<"Signal error" <<std::endl;
3152
}
53+
3254
}

src/SignalHandler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class SignalHandler{
1111
public:
1212
virtual ~SignalHandler();
1313
SignalHandler();
14-
static void signalHandler(int, siginfo_t*, void*);
14+
static void sigintHandler(int, siginfo_t *, void *);
15+
static void sigusr1Handler(int, siginfo_t *, void *);
1516
};
1617

1718
#endif //HACKTTP_SIGNALHANDLER_H

src/globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
#define HACKTTP_GLOBALS_H_H
77

88
extern bool isSigintRecieved;
9+
extern bool isSigusr1Recieved;
910

1011
#endif //HACKTTP_GLOBALS_H_H

src/server.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
* - bind the router tho the receiving port and give it control
1515
* - when router finishes, log and shutdown
1616
*/
17-
bool isSigintRecieved;
1817

1918
int main() {
20-
isSigintRecieved = false;
2119
// Init basic classes
2220
SignalHandler signalHandler;
2321
CmdLine cmdline;

0 commit comments

Comments
 (0)