-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
eventloop.c
53 lines (46 loc) · 1.45 KB
/
eventloop.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
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <ev.h>
#include <syslog.h>
#include "eventloop.h"
#include "connection.h"
#include "globals.h"
#include "log.h"
static void signal_callback(struct ev_loop* loop, ev_signal* w, int revents)
{
my_log(LOG_DAEMON | LOG_INFO, "Got signal %d, shutting down", w->signum);
ev_break(loop, EVBREAK_ALL);
}
int main_loop(struct globals_t* g)
{
ev_signal sigterm_watcher;
ev_signal sigint_watcher;
ev_signal sigquit_watcher;
ev_io* accept_watchers;
ev_signal_init(&sigterm_watcher, signal_callback, SIGTERM);
ev_signal_init(&sigint_watcher, signal_callback, SIGINT);
ev_signal_init(&sigquit_watcher, signal_callback, SIGQUIT);
ev_signal_start(g->loop, &sigterm_watcher);
ev_signal_start(g->loop, &sigint_watcher);
ev_signal_start(g->loop, &sigquit_watcher);
accept_watchers = calloc(g->nsockets, sizeof(ev_io));
if (!accept_watchers) {
perror("calloc");
return EXIT_FAILURE;
}
for (size_t i=0; i<g->nsockets; ++i) {
ev_io_init(&accept_watchers[i], new_connection, g->sockets[i], EV_READ);
if (g->sockets[i] != -1) {
ev_io_start(g->loop, &accept_watchers[i]);
}
}
ev_run(g->loop, 0);
for (size_t i=0; i<g->nsockets; ++i) {
if (accept_watchers[i].fd != -1) {
ev_io_stop(g->loop, &accept_watchers[i]);
}
}
free(accept_watchers);
return EXIT_SUCCESS;
}