-
Notifications
You must be signed in to change notification settings - Fork 1
/
nw_evt.c
82 lines (67 loc) · 1.51 KB
/
nw_evt.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
/*
* Description:
* History: [email protected], 2016/03/19, create
*/
# include <syslog.h>
# include <unistd.h>
# include <limits.h>
# include <libgen.h>
# include "nw_evt.h"
struct ev_loop *nw_default_loop;
static int initialized;
static int running;
static ev_timer break_timer;
static void fatal_error(const char *msg)
{
char exe_path[PATH_MAX];
ssize_t ret = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
exe_path[ret] = 0;
openlog(basename(exe_path), LOG_PERROR | LOG_PID, LOG_DAEMON);
syslog(LOG_ERR, "%s", msg);
closelog();
}
void nw_loop_init(void)
{
if (initialized)
return;
unsigned int flag = 0;
flag |= EVFLAG_AUTO;
flag |= EVFLAG_NOENV;
flag |= EVFLAG_FORKCHECK;
nw_default_loop = ev_loop_new(flag);
ev_set_syserr_cb(fatal_error);
initialized = 1;
}
void nw_loop_run(void)
{
if (!initialized)
return;
running = 1;
ev_run(nw_default_loop, 0);
return;
}
void nw_loop_break(void)
{
if (!initialized)
return;
if (!running)
return;
ev_break(nw_default_loop, EVBREAK_ALL);
running = 0;
return;
}
static void on_break_timer(struct ev_loop *loop, ev_timer *ev, int events)
{
nw_loop_break();
}
void nw_loop_break_later(double waittime)
{
if (!initialized)
return;
if (!running)
return;
if (!ev_is_active(&break_timer)) {
ev_timer_init(&break_timer, on_break_timer, waittime, 0);
ev_timer_start(nw_default_loop, &break_timer);
}
}