-
Notifications
You must be signed in to change notification settings - Fork 7
/
sighandlers.c
69 lines (62 loc) · 1.7 KB
/
sighandlers.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
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
#include <evhttp.h>
#include <unistd.h>
#include "globals.h"
#include "bf_storage.h"
#define BYTES_FOR_PID sizeof('.') + sizeof('-') + 2.5 * sizeof(pid_t) + sizeof('\0') + 1
/* bytes for decimal record of int :=
:= ceil(log10(INT_MAX)) + sign + zero-byte (+ 1 to avoid real ceil call)
*/
bool dumper_active = false;
pid_t dumper;
void term_handler(evutil_socket_t fd, short which, void *base)
{
if (dumper_active) {
kill(dumper, SIGKILL); // REDIS STYLE NOW!!!
char *inprogress_fn = malloc(strlen(snap_path) + BYTES_FOR_PID);
sprintf(inprogress_fn, "%s.%d", snap_path, dumper);
unlink(inprogress_fn);
}
event_base_loopexit(base, NULL);
}
void dump_handler(evutil_socket_t fd, short which, void *arg)
{
if (dumper_active)
return;
dumper_active = true;
pid_t pid = fork();
if (pid == 0) {
if (snap_path && Bloom) {
bf_dump_to_file(Bloom, snap_path);
exit(0);
}
else {
fputs("Global pointers not set!\n", stderr);
exit(11);
}
} else {
if (pid == -1) {
fputs("Unable to fork!\n", stderr);
dumper_active = false;
} else
{
dumper = pid;
}
}
}
void child_collector(evutil_socket_t fd, short which, void *arg)
{
int status;
pid_t pid = waitpid(dumper, &status, WNOHANG);
if (pid == dumper) {
dumper_active = false;
fprintf(stderr, "Dumper process %d exited with code %d. Collected him.\n", pid, WEXITSTATUS(status));
}
}