-
Notifications
You must be signed in to change notification settings - Fork 1
/
pal-linux.cpp
123 lines (110 loc) · 2.96 KB
/
pal-linux.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "pal.h"
#include <sys/stat.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <thread>
#include "commandparsertypes.h"
#include "networkcontroller.h"
#include "prime.h"
extern AllPrimebotSettings ProgramSettings;
extern NetworkController* Controller;
extern Primebot* Bot;
void CtrlCHandler(int type)
{
static bool ShutdownInProgress = false;
std::thread(
[type]
{
switch(type)
{
case SIGINT:
case SIGTERM:
if(ProgramSettings.NetworkSettings.Server && Controller != nullptr)
{
if(!ShutdownInProgress)
{
ShutdownInProgress = true;
Controller->Shutdown();
}
// Now that cleanup is complete, exit the program.
exit(EXIT_SUCCESS);
}
else if(Bot != nullptr)
{
Bot->Stop();
// No need to exit here, the program will exit on its own.
}
break;
default:
printf("unrecognized signal: %d", type);
}
}
).detach();
}
bool RegisterSignalHandler()
{
struct sigaction SigIntHandler;
SigIntHandler.sa_handler = CtrlCHandler;
sigemptyset(&SigIntHandler.sa_mask);
SigIntHandler.sa_flags = SA_RESTART;
if(sigaction(SIGINT, &SigIntHandler, nullptr))
{
fprintf(
stderr,
"Failed to register signal handler: %s",
strerror(errno));
return false;
}
if(sigaction(SIGTERM, &SigIntHandler, nullptr))
{
fprintf(
stderr,
"Failed to register signal handler: %s",
strerror(errno));
return false;
}
return true;
}
bool MakeDirectory(const char* Path)
{
char CurrentFolder[PATH_MAX] = { 0 };
const char* End = nullptr;
End = strchr(Path, '/');
while(End != nullptr)
{
strncpy(CurrentFolder, Path, End - Path + 1);
if(mkdir(CurrentFolder, S_IRWXU))
{
if(errno != EEXIST)
{
fprintf(
stderr,
"Failed to create directory %s with error %s",
CurrentFolder,
strerror(errno));
// Don't delete partially-created hierarchies
return false;
}
}
End = strchr(++End, '/');
}
// Create last folder
if(mkdir(Path, S_IRWXU))
{
if(errno != EEXIST)
{
fprintf(
stderr,
"Failed to create directory %s with error %s",
Path,
strerror(errno));
// Don't delete partially-created hierarchies
return false;
}
}
return true;
}