xref: /php-src/sapi/fpm/fpm/events/epoll.c (revision 819df032)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Jerome Loyet <jerome@loyet.net>                             |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "../fpm_config.h"
18 #include "../fpm_events.h"
19 #include "../fpm.h"
20 #include "../zlog.h"
21 
22 #ifdef HAVE_EPOLL
23 
24 #include <sys/epoll.h>
25 #include <errno.h>
26 
27 static int fpm_event_epoll_init(int max);
28 static int fpm_event_epoll_clean(void);
29 static int fpm_event_epoll_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
30 static int fpm_event_epoll_add(struct fpm_event_s *ev);
31 static int fpm_event_epoll_remove(struct fpm_event_s *ev);
32 
33 static struct fpm_event_module_s epoll_module = {
34 	.name = "epoll",
35 	.support_edge_trigger = 1,
36 	.init = fpm_event_epoll_init,
37 	.clean = fpm_event_epoll_clean,
38 	.wait = fpm_event_epoll_wait,
39 	.add = fpm_event_epoll_add,
40 	.remove = fpm_event_epoll_remove,
41 };
42 
43 static struct epoll_event *epollfds = NULL;
44 static int nepollfds = 0;
45 static int epollfd = -1;
46 
47 #endif /* HAVE_EPOLL */
48 
fpm_event_epoll_module(void)49 struct fpm_event_module_s *fpm_event_epoll_module(void)
50 {
51 #ifdef HAVE_EPOLL
52 	return &epoll_module;
53 #else
54 	return NULL;
55 #endif /* HAVE_EPOLL */
56 }
57 
58 #ifdef HAVE_EPOLL
59 
60 /*
61  * Init the module
62  */
fpm_event_epoll_init(int max)63 static int fpm_event_epoll_init(int max) /* {{{ */
64 {
65 	if (max < 1) {
66 		return 0;
67 	}
68 
69 	/* init epoll */
70 	epollfd = epoll_create(max + 1);
71 	if (epollfd < 0) {
72 		zlog(ZLOG_ERROR, "epoll: unable to initialize");
73 		return -1;
74 	}
75 
76 	/* allocate fds */
77 	epollfds = malloc(sizeof(struct epoll_event) * max);
78 	if (!epollfds) {
79 		zlog(ZLOG_ERROR, "epoll: unable to allocate %d events", max);
80 		return -1;
81 	}
82 	memset(epollfds, 0, sizeof(struct epoll_event) * max);
83 
84 	/* save max */
85 	nepollfds = max;
86 
87 	return 0;
88 }
89 /* }}} */
90 
91 /*
92  * Clean the module
93  */
fpm_event_epoll_clean(void)94 static int fpm_event_epoll_clean(void)
95 {
96 	/* free epollfds */
97 	if (epollfds) {
98 		free(epollfds);
99 		epollfds = NULL;
100 	}
101 	if (epollfd != -1) {
102 		close(epollfd);
103 		epollfd = -1;
104 	}
105 
106 	nepollfds = 0;
107 
108 	return 0;
109 }
110 
111 /*
112  * wait for events or timeout
113  */
fpm_event_epoll_wait(struct fpm_event_queue_s * queue,unsigned long int timeout)114 static int fpm_event_epoll_wait(struct fpm_event_queue_s *queue, unsigned long int timeout) /* {{{ */
115 {
116 	int ret, i;
117 
118 	/* ensure we have a clean epoolfds before calling epoll_wait() */
119 	memset(epollfds, 0, sizeof(struct epoll_event) * nepollfds);
120 
121 	/* wait for incoming event or timeout */
122 	ret = epoll_wait(epollfd, epollfds, nepollfds, timeout);
123 	if (ret == -1) {
124 
125 		/* trigger error unless signal interrupt */
126 		if (errno != EINTR) {
127 			zlog(ZLOG_WARNING, "epoll_wait() returns %d", errno);
128 			return -1;
129 		}
130 	}
131 
132 	/* events have been triggered, let's fire them */
133 	for (i = 0; i < ret; i++) {
134 
135 		/* do we have a valid ev ptr ? */
136 		if (!epollfds[i].data.ptr) {
137 			continue;
138 		}
139 
140 		/* fire the event */
141 		fpm_event_fire((struct fpm_event_s *)epollfds[i].data.ptr);
142 
143 		/* sanity check */
144 		if (fpm_globals.parent_pid != getpid()) {
145 			return -2;
146 		}
147 	}
148 
149 	return ret;
150 }
151 /* }}} */
152 
153 /*
154  * Add a FD to the fd set
155  */
fpm_event_epoll_add(struct fpm_event_s * ev)156 static int fpm_event_epoll_add(struct fpm_event_s *ev) /* {{{ */
157 {
158 	struct epoll_event e;
159 
160 	/* fill epoll struct */
161 #if SIZEOF_SIZE_T == 4
162 	/* Completely initialize event data to prevent valgrind reports */
163 	e.data.u64 = 0;
164 #endif
165 	e.events = EPOLLIN;
166 	e.data.fd = ev->fd;
167 	e.data.ptr = (void *)ev;
168 
169 	if (ev->flags & FPM_EV_EDGE) {
170 		e.events = e.events | EPOLLET;
171 	}
172 
173 	/* add the event to epoll internal queue */
174 	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ev->fd, &e) == -1) {
175 		zlog(ZLOG_ERROR, "epoll: unable to add fd %d", ev->fd);
176 		return -1;
177 	}
178 
179 	/* mark the event as registered */
180 	ev->index = ev->fd;
181 	return 0;
182 }
183 /* }}} */
184 
185 /*
186  * Remove a FD from the fd set
187  */
fpm_event_epoll_remove(struct fpm_event_s * ev)188 static int fpm_event_epoll_remove(struct fpm_event_s *ev) /* {{{ */
189 {
190 	struct epoll_event e;
191 
192 	/* fill epoll struct the same way we did in fpm_event_epoll_add() */
193 	e.events = EPOLLIN;
194 	e.data.fd = ev->fd;
195 	e.data.ptr = (void *)ev;
196 
197 	if (ev->flags & FPM_EV_EDGE) {
198 		e.events = e.events | EPOLLET;
199 	}
200 
201 	/* remove the event from epoll internal queue */
202 	if (epoll_ctl(epollfd, EPOLL_CTL_DEL, ev->fd, &e) == -1) {
203 		zlog(ZLOG_ERROR, "epoll: unable to remove fd %d", ev->fd);
204 		return -1;
205 	}
206 
207 	/* mark the event as not registered */
208 	ev->index = -1;
209 	return 0;
210 }
211 /* }}} */
212 
213 #endif /* HAVE_EPOLL */
214