This repository was archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
204 lines (160 loc) · 5.51 KB
/
main.c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* BSD-2-Clause
*
* Copyright (c) 2020 Tristan
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/resource.h>
#include <sys/time.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "base/global_state.h"
#include "cache/cache.h"
#include "core/security.h"
#include "core/server.h"
#include "misc/default.h"
#include "misc/options.h"
#include "misc/statistics.h"
#include "redir/server.h"
/* Clean up functions */
size_t cufIndex = 0;
void (*cleanUpFunctions[4])(void);
/* Prototypes */
static void CatchSignal(int);
static void StopWithError(const char *, const char *);
int main(void) {
pthread_attr_t attribs;
size_t lastCount;
int ret;
struct sigaction act;
struct timespec time;
cleanUpFunctions[cufIndex++] = GSDestroy;
/* Setup GlobalState */
if (!GSInit())
StopWithError("GlobalState", "Failed to GSInit().");
/* Setup Signal Catching */
memset(&act, 0, sizeof(struct sigaction));
if (sigemptyset(&act.sa_mask) == -1) {
perror("[Main] sigemptyset() failed");
StopWithError("SignalSetup", "Failed to set signal handler.");
}
act.sa_handler = CatchSignal;
act.sa_flags = 0;
if (sigaction(SIGINT, &act, NULL) == -1) {
perror("[Main] sigaction() failed");
StopWithError("SignalSetup", "Failed to set signal handler.");
}
/* Ignore the SIGPIPE signal. */
signal(SIGPIPE, SIG_IGN);
/* Setup options/configuration */
if (!OMSetup())
StopWithError("OptionsManager", "Failed to OMSetup().");
cleanUpFunctions[cufIndex++] = OMDestroy;
/* Start security */
if (CSSetupSecurityManager() <= 0)
StopWithError("CoreSecurity", "Failed to setup the SecurityManager.");
cleanUpFunctions[cufIndex++] = CSDestroySecurityManager;
/* Start cache */
if (!FCSetup())
StopWithError("FileCache", "Failed to setup the FileCache (FCSetup).");
cleanUpFunctions[cufIndex++] = FCDestroy;
/* Start services. */
if (pthread_attr_init(&attribs) != 0)
StopWithError("Services", "pthread_attr_init failed.");
if (pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE) != 0)
StopWithError("Services", "pthread_attr_setdetachstate failed.");
GSRedirThreadState = 1;
if (pthread_create(&GSRedirThread, &attribs, &RSEntrypoint, NULL) != 0)
StopWithError("Services", "Failed to start GSRedirThread.");
GSCoreThreadState = 1;
if (pthread_create(&GSCoreThread, &attribs, &CSEntrypoint, NULL) != 0) {
pthread_cancel(GSRedirThread);
StopWithError("Services", "Failed to start GSCoreThread.");
}
if (pthread_attr_destroy(&attribs) != 0)
warn("[Main] W: Failed to destroy thread attributes.");
lastCount = 0;
SMBegin();
fputs("[Main] Initialization was "ANSI_COLOR_GREEN"succesful"
ANSI_COLOR_RESET".\n", stdout);
while (GSMainLoop) {
size_t currentCount;
time.tv_sec = 1;
time.tv_nsec = 0;
nanosleep(&time, NULL);
currentCount = SMGetPageTraffic();
if (currentCount != lastCount) {
printf(ANSI_COLOR_CYAN"Traffic> "ANSI_COLOR_MAGENTA"%zu"
ANSI_COLOR_RESETLN, currentCount);
lastCount = currentCount;
}
}
/* Newline for ^C */
putchar('\n');
SMEnd();
GSDestroy();
/* Send signal to stop */
ret = pthread_kill(GSCoreThread, SIGINT);
if (ret != 0 && ret != ESRCH)
fprintf(stderr, ANSI_COLOR_RED"[Main] [ShutdownProcedure] E: "
"CoreThreadKill=%i"ANSI_COLOR_RESETLN, ret);
ret = pthread_kill(GSRedirThread, SIGINT);
if (ret != 0 && ret != ESRCH)
fprintf(stderr, ANSI_COLOR_RED"[Main] [ShutdownProcedure] E: "
"RedirThreadKill=%i"ANSI_COLOR_RESETLN, ret);
/* Stop threads */
time.tv_nsec = 100000000; /* 100 ms */
nanosleep(&time, NULL);
pthread_join(GSCoreThread, NULL);
pthread_join(GSRedirThread, NULL);
/* After all other threads have stopped: */
CSDestroySecurityManager();
FCDestroy();
OMDestroy();
fclose(stdin);
fclose(stdout);
fclose(stderr);
return EXIT_SUCCESS;
}
static void CatchSignal(int signo) {
if (signo == SIGINT)
GSNotify(GSA_INTERRUPT);
}
static void StopWithError(const char *section, const char *message) {
size_t i;
for (i = 0; i < cufIndex; i++)
cleanUpFunctions[i]();
fclose(stdin);
fclose(stdout);
fprintf(stderr, ANSI_COLOR_RED"[Main] [%s] %s"ANSI_COLOR_RESETLN, section,
message);
fclose(stderr);
exit(EXIT_FAILURE);
}