-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.c
210 lines (173 loc) · 4.05 KB
/
event.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "event.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <syslog.h>
#include <sys/epoll.h>
#include <sys/queue.h>
/* Event */
struct callback {
void *data;
int fd;
int (*callback)(int fd, int event, void *data);
void (*destroy)(void *data);
LIST_ENTRY(callback) list;
};
/* Global event handle */
struct event_handle {
int epollfd;
int curfds;
int maxfds;
LIST_HEAD(evlist_head, callback) head;
};
/* Static function prototypes */
static struct callback * event_search(int fd);
static struct event_handle eh = { -1, 0, EVENT_MAXFDS };
/* Returns an event handle from searching by fd */
static struct callback * event_search(
int fd)
{
struct callback *ev;
for (ev = eh.head.lh_first; ev != NULL; ev = ev->list.le_next) {
if (ev->fd == fd)
return ev;
}
return NULL;
}
/* Initialize the event handle */
void event_init(
void)
{
int fd = epoll_create1(EPOLL_CLOEXEC);
if (fd < 0)
err(EXIT_FAILURE, "Cannot initialize event handler");
eh.epollfd = fd;
LIST_INIT(&eh.head);
}
/* Perform the event loop */
int event_loop(
int max,
int timeout)
{
int cnt = 0;
int rc, i;
struct callback *cb;
assert(max < EVENT_MAXFDS && max > 0);
assert(timeout >= -1);
struct epoll_event *events = calloc(max, sizeof(struct epoll_event));
if (!events) {
syslog(LOG_ERR, "Cannot allocate memory for events: %s", strerror(errno));
goto fail;
}
restart:
/* Do the epoll, safely handle interrupts */
rc = epoll_wait(eh.epollfd, events, max, timeout);
if (rc < 0) {
if (errno == EINTR)
goto restart;
else {
goto fail;
}
}
for (i=0; i < rc; i++) {
cb = (struct callback *)events[i].data.ptr;
assert(cb->callback);
if (cb->callback(cb->fd, events[i].events, cb->data) < 0) {
event_del_fd(cb->fd);
}
else {
cnt++;
}
}
free(events);
return cnt;
fail:
if (events)
free(events);
return -1;
}
/* Delete an FD from the events, is idempotent */
void event_del_fd(
int fd)
{
struct callback *ev = event_search(fd);
if (!ev)
return;
/* Remove from the list */
LIST_REMOVE(ev, list);
/* Call the objects destructor */
if (ev->destroy)
ev->destroy(ev->data);
/* Remove from the epoll */
epoll_ctl(eh.epollfd, EPOLL_CTL_DEL, ev->fd, NULL);
/* WARNING WARNING, cb->data MAY BE ALLOCATED */
free(ev);
}
/* Modify the event mask of an existing cli */
int event_mod_event(
int fd,
int event)
{
assert(fd > -1);
struct epoll_event ev;
struct callback *cb;
if ((cb = event_search(fd)) == NULL) {
return 0;
}
ev.events = event;
ev.data.ptr = cb;
if (epoll_ctl(eh.epollfd, EPOLL_CTL_MOD, fd, &ev) < 0)
syslog(LOG_WARNING, "Unable to modify event mask: %s", strerror(errno));
return -1;
return 0;
}
/* Add a FD onto the events */
int event_add_fd(
int fd,
int (*callback)(int fd, int event, void *data),
void (*destructor),
void *data,
int event)
{
assert(fd >= 0);
assert(callback);
struct epoll_event ep_ev;
struct callback *ev = malloc(sizeof(*ev));
struct callback *tmp;
if (!ev) {
syslog(LOG_ERR, "Cannot allocate memory for callback: %s", strerror(errno));
goto fail;
}
memset(&ep_ev, 0, sizeof(ep_ev));
/* Initialize callback and epoll event */
ev->fd = fd;
ev->data = data;
ev->callback = callback;
ev->destroy = destructor;
ep_ev.events = event;
ep_ev.data.ptr = ev;
/* Insert the FD onto our list */
LIST_INSERT_HEAD(&eh.head, ev, list);
/* Register the fd with the epoll handler */
if (epoll_ctl(eh.epollfd, EPOLL_CTL_ADD, fd, &ep_ev) < 0) {
syslog(LOG_ERR, "Cannot add event to epoll: %s", strerror(errno));
goto fail;
}
if (eh.curfds + 1 > eh.maxfds) {
syslog(LOG_ERR, "Adding FD maximum number of descriptors to monitor: %s", strerror(errno));
goto fail;
}
eh.curfds++;
return 0;
fail:
tmp = event_search(fd);
if (tmp) {
LIST_REMOVE(tmp, list);
}
if (ev)
free(ev);
return -1;
}