summaryrefslogtreecommitdiff
path: root/sapi/fpm/fpm/events/poll.c
blob: ced32d021598df95b308fc11f8b6776974548737 (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
   +----------------------------------------------------------------------+
   | PHP Version 7                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) The PHP Group                                          |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Jerome Loyet <jerome@loyet.net>                             |
   +----------------------------------------------------------------------+
*/

#include "../fpm_config.h"
#include "../fpm_events.h"
#include "../fpm.h"
#include "../zlog.h"

#if HAVE_POLL

#include <poll.h>
#include <errno.h>
#include <string.h>

static int fpm_event_poll_init(int max);
static int fpm_event_poll_clean();
static int fpm_event_poll_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
static int fpm_event_poll_add(struct fpm_event_s *ev);
static int fpm_event_poll_remove(struct fpm_event_s *ev);

static struct fpm_event_module_s poll_module = {
	.name = "poll",
	.support_edge_trigger = 0,
	.init = fpm_event_poll_init,
	.clean = fpm_event_poll_clean,
	.wait = fpm_event_poll_wait,
	.add = fpm_event_poll_add,
	.remove = fpm_event_poll_remove,
};

static struct pollfd *pollfds = NULL;
static struct pollfd *active_pollfds = NULL;
static int npollfds = 0;
static int next_free_slot = 0;
#endif /* HAVE_POLL */

/*
 * return the module configuration
 */
struct fpm_event_module_s *fpm_event_poll_module() /* {{{ */
{
#if HAVE_POLL
	return &poll_module;
#else
	return NULL;
#endif /* HAVE_POLL */
}
/* }}} */

#if HAVE_POLL

/*
 * Init the module
 */
static int fpm_event_poll_init(int max) /* {{{ */
{
	int i;

	if (max < 1) {
		return 0;
	}

	/* alloc and clear pollfds */
	pollfds = malloc(sizeof(struct pollfd) * max);
	if (!pollfds) {
		zlog(ZLOG_ERROR, "poll: unable to allocate %d events", max);
		return -1;
	}
	memset(pollfds, 0, sizeof(struct pollfd) * max);

	/* set all fd to -1 in order to ensure it's not set */
	for (i = 0; i < max; i++) {
			pollfds[i].fd = -1;
	}

	/* alloc and clear active_pollfds */
	active_pollfds = malloc(sizeof(struct pollfd) * max);
	if (!active_pollfds) {
		free(pollfds);
		zlog(ZLOG_ERROR, "poll: unable to allocate %d events", max);
		return -1;
	}
	memset(active_pollfds, 0, sizeof(struct pollfd) * max);

	/* save max */
	npollfds = max;
	return 0;
}
/* }}} */

/*
 * Clean the module
 */
static int fpm_event_poll_clean() /* {{{ */
{
	/* free pollfds */
	if (pollfds) {
		free(pollfds);
		pollfds = NULL;
	}

	/* free active_pollfds */
	if (active_pollfds) {
		free(active_pollfds);
		active_pollfds = NULL;
	}

	npollfds = 0;
	return 0;
}
/* }}} */

/*
 * wait for events or timeout
 */
static int fpm_event_poll_wait(struct fpm_event_queue_s *queue, unsigned long int timeout) /* {{{ */
{
	int ret;
	struct fpm_event_queue_s *q;

	if (npollfds > 0) {
		/* copy pollfds because poll() alters it */
		memcpy(active_pollfds, pollfds, sizeof(struct pollfd) * npollfds);
	}

	/* wait for inconming event or timeout */
	ret = poll(active_pollfds, npollfds, timeout);
	if (ret == -1) {

		/* trigger error unless signal interrupt */
		if (errno != EINTR) {
			zlog(ZLOG_WARNING, "poll() returns %d", errno);
			return -1;
		}
	}

	/* events have been triggered */
	if (ret > 0) {

		/* trigger POLLIN events */
		q = queue;
		while (q) {
			/* ensure ev->index is valid */
			if (q->ev && q->ev->index >= 0 && q->ev->index < npollfds && q->ev->fd == active_pollfds[q->ev->index].fd) {

				/* has the event has been triggered ? */
				if (active_pollfds[q->ev->index].revents & POLLIN) {

					/* fire the event */
					fpm_event_fire(q->ev);

					/* sanity check */
					if (fpm_globals.parent_pid != getpid()) {
						return -2;
					}
				}
			}
			q = q->next; /* iterate */
		}
	}

	return ret;
}
/* }}} */

/*
 * Add a FD to the fd set
 */
static int fpm_event_poll_add(struct fpm_event_s *ev) /* {{{ */
{
	int i;

	/* do we have a direct free slot */
	if (pollfds[next_free_slot].fd == -1) {
		/* register the event */
		pollfds[next_free_slot].fd = ev->fd;
		pollfds[next_free_slot].events = POLLIN;

		/* remember the event place in the fd list and suppose next slot is free */
		ev->index = next_free_slot++;
		if (next_free_slot >= npollfds) {
			next_free_slot = 0;
		}
		return 0;
	}

	/* let's search */
	for (i = 0; i < npollfds; i++) {
		if (pollfds[i].fd != -1) {
			/* not free */
			continue;
		}

		/* register the event */
		pollfds[i].fd = ev->fd;
		pollfds[i].events = POLLIN;

		/* remember the event place in the fd list and suppose next slot is free */
		ev->index = next_free_slot++;
		if (next_free_slot >= npollfds) {
			next_free_slot = 0;
		}
		return 0;
	}

	zlog(ZLOG_ERROR, "poll: not enought space to add event (fd=%d)", ev->fd);
	return -1;
}
/* }}} */

/*
 * Remove a FD from the fd set
 */
static int fpm_event_poll_remove(struct fpm_event_s *ev) /* {{{ */
{
	int i;

	/* do we have a direct access */
	if (ev->index >= 0 && ev->index < npollfds && pollfds[ev->index].fd == ev->fd) {
		/* remember this slot as free */
		next_free_slot = ev->index;

		/* clear event in pollfds */
		pollfds[ev->index].fd = -1;
		pollfds[ev->index].events = 0;

		/* mark the event as not registered */
		ev->index = -1;

		return 0;
	}

	/* let's search */
	for (i = 0; i < npollfds; i++) {

		if (pollfds[i].fd != ev->fd) {
			/* not found */
			continue;
		}

		/* remember this slot as free */
		next_free_slot = i;

		/* clear event in pollfds */
		pollfds[i].fd = -1;
		pollfds[i].events = 0;

		/* mark the event as not registered */
		ev->index = -1;

		return 0;
	}

	zlog(ZLOG_ERROR, "poll: unable to remove event: not found (fd=%d, index=%d)", ev->fd, ev->index);
	return -1;
}
/* }}} */

#endif /* HAVE_POLL */