-
Notifications
You must be signed in to change notification settings - Fork 1
/
nbio-poll.c
147 lines (118 loc) · 2.48 KB
/
nbio-poll.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
/*
* This file is part of Firestorm NIDS
* Copyright (c) 2004 Gianni Tedesco
* Released under the terms of the GNU GPL version 2
*
* Poll based eventloop
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/poll.h>
#include <errno.h>
#include <compiler.h>
#include <list.h>
#include <nbio.h>
struct poll_priv {
int max_pfd, num_pfd;
struct pollfd *pfd;
};
static int upsize_pfdset(struct poll_priv *p)
{
unsigned int max;
struct pollfd *new;
max = p->max_pfd + 8;
new = realloc(p->pfd, max * sizeof(*p->pfd));
if ( new == NULL )
return 0;
p->max_pfd = max;
p->pfd = new;
return 1;
}
static int poll_init(struct iothread *t)
{
struct poll_priv *p;
p = calloc(1, sizeof(*p));
if ( p == NULL ) {
return 0;
}
if ( !upsize_pfdset(p) ) {
free(p);
return 0;
}
t->priv.ptr = p;
return 1;
}
static void poll_fini(struct iothread *t)
{
struct poll_priv *p = t->priv.ptr;
free(p->pfd);
free(p);
}
static void poll_pump(struct iothread *t, int mto)
{
struct poll_priv *p = t->priv.ptr;
struct nbio *n, *tmp;
int ret;
again:
ret = poll(p->pfd, p->num_pfd, mto);
if ( ret < 0 ) {
if ( errno == EINTR )
goto again;
return;
}
p->num_pfd = 0;
list_for_each_entry_safe(n, tmp, &t->inactive, list) {
struct pollfd *pfd;
if ( n->ev_priv.poll < 0 )
continue;
pfd = &p->pfd[n->ev_priv.poll];
if ( pfd->revents == 0 ) {
p->pfd[p->num_pfd].fd = pfd->fd;
p->pfd[p->num_pfd].events = pfd->events;
p->num_pfd++;
continue;
}
n->flags = 0;
if ( pfd->revents & (POLLIN|POLLHUP) )
n->flags |= NBIO_READ;
if ( pfd->revents & POLLOUT )
n->flags |= NBIO_WRITE;
if ( pfd->revents & POLLERR )
n->flags |= NBIO_ERROR;
n->ev_priv.poll = -1;
list_move_tail(&n->list, &t->active);
}
}
static void poll_inactive(struct iothread *t, struct nbio *n)
{
struct poll_priv *p = t->priv.ptr;
struct pollfd *pfd;
/* EEK */
if ( p->num_pfd >= p->max_pfd && !upsize_pfdset(p) )
return;
pfd = &p->pfd[p->num_pfd];
pfd->fd = n->fd;
pfd->events = POLLERR|POLLHUP;
if ( n->mask & NBIO_READ )
pfd->events |= POLLIN;
if ( n->mask & NBIO_WRITE )
pfd->events |= POLLOUT;
n->ev_priv.poll = p->num_pfd++;
}
static void poll_active(struct iothread *t, struct nbio *n)
{
n->ev_priv.poll = -1;
}
static struct eventloop eventloop_poll = {
.name = "poll",
.init = poll_init,
.fini = poll_fini,
.inactive = poll_inactive,
.active = poll_active,
.pump = poll_pump,
};
void _eventloop_poll_ctor(void)
{
eventloop_add(&eventloop_poll);
}