xref: /PHP-8.3/sapi/fpm/fpm/events/devpoll.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_DEVPOLL
23 
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <poll.h>
28 #include <sys/devpoll.h>
29 #include <errno.h>
30 
31 static int fpm_event_devpoll_init(int max);
32 static int fpm_event_devpoll_clean(void);
33 static int fpm_event_devpoll_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
34 static int fpm_event_devpoll_add(struct fpm_event_s *ev);
35 static int fpm_event_devpoll_remove(struct fpm_event_s *ev);
36 
37 static struct fpm_event_module_s devpoll_module = {
38 	.name = "/dev/poll",
39 	.support_edge_trigger = 0,
40 	.init = fpm_event_devpoll_init,
41 	.clean = fpm_event_devpoll_clean,
42 	.wait = fpm_event_devpoll_wait,
43 	.add = fpm_event_devpoll_add,
44 	.remove = fpm_event_devpoll_remove,
45 };
46 
47 int dpfd = -1;
48 static struct pollfd *pollfds = NULL;
49 static struct pollfd *active_pollfds = NULL;
50 static int npollfds = 0;
51 
52 #endif /* HAVE_DEVPOLL */
53 
fpm_event_devpoll_module(void)54 struct fpm_event_module_s *fpm_event_devpoll_module(void)
55 {
56 #ifdef HAVE_DEVPOLL
57 	return &devpoll_module;
58 #else
59 	return NULL;
60 #endif /* HAVE_DEVPOLL */
61 }
62 
63 #ifdef HAVE_DEVPOLL
64 
65 /*
66  * Init module
67  */
fpm_event_devpoll_init(int max)68 static int fpm_event_devpoll_init(int max) /* {{{ */
69 {
70 	int i;
71 
72 	/* open /dev/poll for future usages */
73 	dpfd = open("/dev/poll", O_RDWR);
74 	if (dpfd < 0) {
75 		zlog(ZLOG_ERROR, "Unable to open /dev/poll");
76 		return -1;
77 	}
78 
79 	if (max < 1) {
80 		return 0;
81 	}
82 
83 	/* alloc and clear pollfds */
84 	pollfds = malloc(sizeof(struct pollfd) * max);
85 	if (!pollfds) {
86 		zlog(ZLOG_ERROR, "poll: unable to allocate %d events", max);
87 		return -1;
88 	}
89 	memset(pollfds, 0, sizeof(struct pollfd) * max);
90 
91 	/* set all fd to -1 in order to ensure it's not set */
92 	for (i = 0; i < max; i++) {
93 		pollfds[i].fd = -1;
94 	}
95 
96 	/* alloc and clear active_pollfds */
97 	active_pollfds = malloc(sizeof(struct pollfd) * max);
98 	if (!active_pollfds) {
99 		free(pollfds);
100 		zlog(ZLOG_ERROR, "poll: unable to allocate %d events", max);
101 		return -1;
102 	}
103 	memset(active_pollfds, 0, sizeof(struct pollfd) * max);
104 
105 	/* save max */
106 	npollfds = max;
107 
108 	return 0;
109 }
110 /* }}} */
111 
112 /*
113  * Clean the module
114  */
fpm_event_devpoll_clean(void)115 static int fpm_event_devpoll_clean(void)
116 {
117 	/* close /dev/poll if open */
118 	if (dpfd > -1) {
119 		close(dpfd);
120 		dpfd = -1;
121 	}
122 
123 	/* free pollfds */
124 	if (pollfds) {
125 		free(pollfds);
126 		pollfds = NULL;
127 	}
128 
129 	/* free active_pollfds */
130 	if (active_pollfds) {
131 		free(active_pollfds);
132 		active_pollfds = NULL;
133 	}
134 
135 	npollfds = 0;
136 	return 0;
137 }
138 
139 /*
140  * wait for events or timeout
141  */
fpm_event_devpoll_wait(struct fpm_event_queue_s * queue,unsigned long int timeout)142 static int fpm_event_devpoll_wait(struct fpm_event_queue_s *queue, unsigned long int timeout) /* {{{ */
143 {
144 	int ret, i;
145 	struct fpm_event_queue_s *q;
146 	struct dvpoll dopoll;
147 
148 	/* setup /dev/poll */
149 	dopoll.dp_fds = active_pollfds;
150 	dopoll.dp_nfds = npollfds;
151 	dopoll.dp_timeout = (int)timeout;
152 
153 	/* wait for incoming event or timeout */
154 	ret = ioctl(dpfd, DP_POLL, &dopoll);
155 
156 	if (ret < 0) {
157 
158 		/* trigger error unless signal interrupt */
159 		if (errno != EINTR) {
160 			zlog(ZLOG_WARNING, "/dev/poll: ioctl() returns %d", errno);
161 			return -1;
162 		}
163 	}
164 
165 	/* iterate through triggered events */
166 	for (i = 0; i < ret; i++) {
167 
168 		/* find the corresponding event */
169 		q = queue;
170 		while (q) {
171 
172 			/* found */
173 			if (q->ev && q->ev->fd == active_pollfds[i].fd) {
174 
175 					/* fire the event */
176 					fpm_event_fire(q->ev);
177 
178 					/* sanity check */
179 					if (fpm_globals.parent_pid != getpid()) {
180 						return -2;
181 					}
182 				break; /* next triggered event */
183 			}
184 			q = q->next; /* iterate */
185 		}
186 	}
187 
188 	return ret;
189 }
190 /* }}} */
191 
192 /*
193  * Add a FD from the fd set
194  */
fpm_event_devpoll_add(struct fpm_event_s * ev)195 static int fpm_event_devpoll_add(struct fpm_event_s *ev) /* {{{ */
196 {
197 	struct pollfd pollfd;
198 
199 	/* fill pollfd with event information */
200 	pollfd.fd = ev->fd;
201 	pollfd.events = POLLIN;
202 	pollfd.revents = 0;
203 
204 	/* add the event to the internal queue */
205 	if (write(dpfd, &pollfd, sizeof(struct pollfd)) != sizeof(struct pollfd)) {
206 		zlog(ZLOG_ERROR, "/dev/poll: Unable to add the event in the internal queue");
207 		return -1;
208 	}
209 
210 	/* mark the event as registered */
211 	ev->index = ev->fd;
212 
213 	return 0;
214 }
215 /* }}} */
216 
217 /*
218  * Remove a FD from the fd set
219  */
fpm_event_devpoll_remove(struct fpm_event_s * ev)220 static int fpm_event_devpoll_remove(struct fpm_event_s *ev) /* {{{ */
221 {
222 	struct pollfd pollfd;
223 
224 	/* fill pollfd with the same information as fpm_event_devpoll_add */
225 	pollfd.fd = ev->fd;
226 	pollfd.events = POLLIN | POLLREMOVE;
227 	pollfd.revents = 0;
228 
229 	/* add the event to the internal queue */
230 	if (write(dpfd, &pollfd, sizeof(struct pollfd)) != sizeof(struct pollfd)) {
231 		zlog(ZLOG_ERROR, "/dev/poll: Unable to remove the event in the internal queue");
232 		return -1;
233 	}
234 
235 	/* mark the event as registered */
236 	ev->index = -1;
237 
238 	return 0;
239 }
240 /* }}} */
241 
242 #endif /* HAVE_DEVPOLL */
243