Skip to content

Commit

Permalink
Merge pull request #14 from Snawoot/signals_to_eventloop
Browse files Browse the repository at this point in the history
delegated signal handlers to eventloop, removed unneeded includes
  • Loading branch information
Vladislav Yarmak authored Jul 1, 2016
2 parents e3b8c21 + cb0d28b commit f8dfaf0
Showing 5 changed files with 31 additions and 29 deletions.
36 changes: 24 additions & 12 deletions bloom.c
Original file line number Diff line number Diff line change
@@ -29,11 +29,11 @@ int main(int argc, char *argv[])
char *cnv;
int c;

struct event *dump_event = NULL;
struct event_base *base = NULL;
struct event *dump_event, *int_event, *term_event, *timer_event, *child_event;
struct evhttp *http = NULL;
struct evhttp_bound_socket *handle = NULL;

struct event *timer_ev;
struct timeval timer_tv;
timer_tv.tv_sec = 300;
timer_tv.tv_usec = 0;
@@ -111,15 +111,24 @@ int main(int argc, char *argv[])
if (!(handle = evhttp_bind_socket_with_handle(http, bind_address, bind_port)))
crash("couldn't bind to port. Exiting.\n", -1);

if (signal(SIGINT, term_handler) == SIG_ERR)
crash("Unable to set SIGINT handler!", -1);
if ((term_event = evsignal_new(base, SIGTERM, term_handler, base)) == NULL)
crash("Unable to create SIGTERM handler!", -1);
else
if (event_add(term_event, NULL) == -1)
crash("Unable to add SIGTERM handler!", -1);

if ((int_event = evsignal_new(base, SIGINT, term_handler, base)) == NULL)
crash("Unable to create SIGINT handler!", -1);
else
if (event_add(int_event, NULL) == -1)
crash("Unable to add SIGINT handler!", -1);

if (signal(SIGTERM, term_handler) == SIG_ERR)
crash("Unable to set SIGTERM handler!", -1);
if ((child_event = evsignal_new(base, SIGCHLD, child_collector, NULL)) == NULL)
crash("Unable to create SIGCHLD handler!", -1);
else
if (event_add(child_event, NULL) == -1)
crash("Unable to add SIGCHLD handler!", -1);

if (signal(SIGCHLD, child_collector) == SIG_ERR)
crash("Unable to set SIGCHLD handler!", -1);

//This signal handled by event loop in order to avoid malloc deadlock
if ((dump_event = evsignal_new(base, SIGUSR1, dump_handler, NULL)) == NULL)
crash("Unable to create SIGUSR1 handler!", -1);
@@ -128,10 +137,10 @@ int main(int argc, char *argv[])
crash("Unable to add SIGUSR1 handler!", -1);

if (timer_tv.tv_sec) {
if ((timer_ev = event_new(base, -1, EV_PERSIST, dump_handler, NULL)) == NULL)
if ((timer_event = event_new(base, -1, EV_PERSIST, dump_handler, NULL)) == NULL)
crash("Unable to create timer handler!", -1);
else
if (evtimer_add(timer_ev, &timer_tv) == -1)
if (evtimer_add(timer_event, &timer_tv) == -1)
crash("Unable to add timer handler!", -1);
}

@@ -143,7 +152,10 @@ int main(int argc, char *argv[])
evhttp_del_accept_socket(http, handle);
evhttp_free(http);
event_free(dump_event);
event_free(timer_ev);
event_free(term_event);
event_free(int_event);
event_free(timer_event);
event_free(child_event);
event_base_free(base);
bf_destroy(Bloom);
return 0;
7 changes: 0 additions & 7 deletions globals.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
#include <stdbool.h>
#include <evhttp.h>
#include <unistd.h>
#include "bf_types.h"
#include <event.h>

//Server globals
struct event_base *base = NULL;
bloom_filter_t *Bloom = NULL;
char *snap_path = NULL;
bool dumper_active = false;
pid_t dumper;
6 changes: 0 additions & 6 deletions globals.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
//Server globals
#include <stdbool.h>
#include <unistd.h>
#include <event.h>
#include "bf_types.h"

extern struct event_base *base;
extern bloom_filter_t *Bloom;
extern char *snap_path;
extern bool dumper_active;
extern pid_t dumper;
7 changes: 5 additions & 2 deletions sighandlers.c
Original file line number Diff line number Diff line change
@@ -8,11 +8,14 @@
#include "globals.h"
#include "bf_storage.h"

void term_handler(int signo)
void term_handler(evutil_socket_t fd, short which, void *base)
{
event_base_loopexit(base, NULL);
}

bool dumper_active = false;
pid_t dumper;

void dump_handler(evutil_socket_t fd, short which, void *arg)
{
if (dumper_active)
@@ -39,7 +42,7 @@ void dump_handler(evutil_socket_t fd, short which, void *arg)
}
}

void child_collector(int signo)
void child_collector(evutil_socket_t fd, short which, void *arg)
{
int status;
pid_t pid = waitpid(dumper, &status, WNOHANG);
4 changes: 2 additions & 2 deletions sighandlers.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <evhttp.h>
extern void term_handler(int);
extern void term_handler(evutil_socket_t, short, void *);
extern void dump_handler(evutil_socket_t, short, void *);
extern void child_collector(int);
extern void child_collector(evutil_socket_t, short, void *);

0 comments on commit f8dfaf0

Please sign in to comment.