summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c41
1 files changed, 29 insertions, 12 deletions
diff --git a/opentracker.c b/opentracker.c
index eb97b42..34b8fd5 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -43,15 +43,18 @@ uint32_t g_tracker_id;
43_Atomic int g_opentracker_running = 1; 43_Atomic int g_opentracker_running = 1;
44int g_self_pipe[2]; 44int g_self_pipe[2];
45 45
46
47static void panic(const char *routine) __attribute__((noreturn)); 46static void panic(const char *routine) __attribute__((noreturn));
48static void panic(const char *routine) { 47static void panic(const char *routine) {
49 fprintf(stderr, "%s: %s\n", routine, strerror(errno)); 48 fprintf(stderr, "%s: %s\n", routine, strerror(errno));
50 exit(111); 49 exit(111);
51} 50}
52 51
52/* Say we are stopping, wake the loop, and let main do the work on its own stack. */
53static void signal_handler(int s) { 53static void signal_handler(int s) {
54 if (s == SIGINT) { 54 if (s == SIGINT) {
55 const char byte = 0;
56 int saved = errno;
57
55 /* Any new interrupt signal quits the application */ 58 /* Any new interrupt signal quits the application */
56 signal(SIGINT, SIG_DFL); 59 signal(SIGINT, SIG_DFL);
57 60
@@ -59,13 +62,8 @@ static void signal_handler(int s) {
59 but cancel their operations and return */ 62 but cancel their operations and return */
60 g_opentracker_running = 0; 63 g_opentracker_running = 0;
61 64
62 trackerlogic_deinit(); 65 (void)!write(g_self_pipe[1], &byte, 1);
63 66 errno = saved;
64#ifdef WANT_SYSLOGS
65 closelog();
66#endif
67
68 exit(0);
69 } 67 }
70} 68}
71 69
@@ -207,7 +205,7 @@ static void handle_write(const int64_t sock) {
207 handle_dead(sock); 205 handle_dead(sock);
208} 206}
209 207
210static void handle_accept(const int64_t serversocket) { 208static void handle_accept(const int64_t serversocket, struct ot_workstruct *ws) {
211 struct http_data *cookie; 209 struct http_data *cookie;
212 int64_t sock; 210 int64_t sock;
213 ot_ip6 ip; 211 ot_ip6 ip;
@@ -231,7 +229,7 @@ static void handle_accept(const int64_t serversocket) {
231 continue; 229 continue;
232 } 230 }
233 231
234 stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); 232 stats_issue_event(ws, EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip);
235 } 233 }
236} 234}
237 235
@@ -248,6 +246,8 @@ static void *server_mainloop(void *args) {
248 (void)args; 246 (void)args;
249 247
250 /* Initialize our "thread local storage" */ 248 /* Initialize our "thread local storage" */
249 memset(&ws, 0, sizeof(ws));
250 stats_counters_register(&ws);
251 ws.inbuf = malloc(G_INBUF_SIZE); 251 ws.inbuf = malloc(G_INBUF_SIZE);
252 ws.outbuf = malloc(G_OUTBUF_SIZE); 252 ws.outbuf = malloc(G_OUTBUF_SIZE);
253#ifdef _DEBUG_HTTPERROR 253#ifdef _DEBUG_HTTPERROR
@@ -259,7 +259,7 @@ static void *server_mainloop(void *args) {
259 259
260 random_bytes(ws.rand48_state, sizeof(ws.rand48_state)); 260 random_bytes(ws.rand48_state, sizeof(ws.rand48_state));
261 261
262 for (;;) { 262 while (g_opentracker_running) {
263 ot_event ready[OT_EVENT_BATCH]; 263 ot_event ready[OT_EVENT_BATCH];
264 int64_t sock; 264 int64_t sock;
265 int count, i; 265 int count, i;
@@ -278,7 +278,7 @@ static void *server_mainloop(void *args) {
278 cookie = fdstate_cookie((int)sock); 278 cookie = fdstate_cookie((int)sock);
279 279
280 if ((intptr_t)cookie == FLAG_TCP) 280 if ((intptr_t)cookie == FLAG_TCP)
281 handle_accept(sock); 281 handle_accept(sock, &ws);
282 else if ((intptr_t)cookie == FLAG_UDP) 282 else if ((intptr_t)cookie == FLAG_UDP)
283 handle_udp6(sock, &ws); 283 handle_udp6(sock, &ws);
284 else if ((intptr_t)cookie == FLAG_SELFPIPE) 284 else if ((intptr_t)cookie == FLAG_SELFPIPE)
@@ -306,6 +306,16 @@ static void *server_mainloop(void *args) {
306 306
307 livesync_ticker(); 307 livesync_ticker();
308 } 308 }
309
310 /* This work struct is about to go out of scope, so hand its tally over
311 before anything can read it there */
312 stats_counters_retire(&ws);
313
314 free(ws.inbuf);
315 free(ws.outbuf);
316#ifdef _DEBUG_HTTPERROR
317 free(ws.debugbuf);
318#endif
309 return 0; 319 return 0;
310} 320}
311 321
@@ -510,6 +520,13 @@ int main(int argc, char **argv) {
510 520
511 server_mainloop(0); 521 server_mainloop(0);
512 522
523 /* The loop has left, so no thread is serving any more and the teardown
524 can run where a debugger and the sanitizers can follow it */
525 trackerlogic_deinit();
526#ifdef WANT_SYSLOGS
527 closelog();
528#endif
529
513 event_deinit(); 530 event_deinit();
514 fdstate_deinit(); 531 fdstate_deinit();
515 return 0; 532 return 0;