xref: /PHP-7.4/sapi/fpm/fpm/events/kqueue.c (revision 0cf7de1c)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Jerome Loyet <jerome@loyet.net>                             |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "../fpm_config.h"
20 #include "../fpm_events.h"
21 #include "../fpm.h"
22 #include "../zlog.h"
23 
24 #if HAVE_KQUEUE
25 
26 #include <sys/types.h>
27 #include <sys/event.h>
28 #include <sys/time.h>
29 
30 #include <errno.h>
31 
32 static int fpm_event_kqueue_init(int max);
33 static int fpm_event_kqueue_clean();
34 static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
35 static int fpm_event_kqueue_add(struct fpm_event_s *ev);
36 static int fpm_event_kqueue_remove(struct fpm_event_s *ev);
37 
38 static struct fpm_event_module_s kqueue_module = {
39 	.name = "kqueue",
40 	.support_edge_trigger = 1,
41 	.init = fpm_event_kqueue_init,
42 	.clean = fpm_event_kqueue_clean,
43 	.wait = fpm_event_kqueue_wait,
44 	.add = fpm_event_kqueue_add,
45 	.remove = fpm_event_kqueue_remove,
46 };
47 
48 static struct kevent *kevents = NULL;
49 static int nkevents = 0;
50 static int kfd = 0;
51 
52 #endif /* HAVE_KQUEUE */
53 
54 /*
55  * Return the module configuration
56  */
fpm_event_kqueue_module()57 struct fpm_event_module_s *fpm_event_kqueue_module() /* {{{ */
58 {
59 #if HAVE_KQUEUE
60 	return &kqueue_module;
61 #else
62 	return NULL;
63 #endif /* HAVE_KQUEUE */
64 }
65 /* }}} */
66 
67 #if HAVE_KQUEUE
68 
69 /*
70  * init kqueue and stuff
71  */
fpm_event_kqueue_init(int max)72 static int fpm_event_kqueue_init(int max) /* {{{ */
73 {
74 	if (max < 1) {
75 		return 0;
76 	}
77 
78 	kfd = kqueue();
79 	if (kfd < 0) {
80 		zlog(ZLOG_ERROR, "kqueue: unable to initialize");
81 		return -1;
82 	}
83 
84 	kevents = malloc(sizeof(struct kevent) * max);
85 	if (!kevents) {
86 		zlog(ZLOG_ERROR, "epoll: unable to allocate %d events", max);
87 		return -1;
88 	}
89 
90 	memset(kevents, 0, sizeof(struct kevent) * max);
91 
92 	nkevents = max;
93 
94 	return 0;
95 }
96 /* }}} */
97 
98 /*
99  * release kqueue stuff
100  */
fpm_event_kqueue_clean()101 static int fpm_event_kqueue_clean() /* {{{ */
102 {
103 	if (kevents) {
104 		free(kevents);
105 		kevents = NULL;
106 	}
107 
108 	nkevents = 0;
109 
110 	return 0;
111 }
112 /* }}} */
113 
114 /*
115  * wait for events or timeout
116  */
fpm_event_kqueue_wait(struct fpm_event_queue_s * queue,unsigned long int timeout)117 static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout) /* {{{ */
118 {
119 	struct timespec t;
120 	int ret, i;
121 
122 	/* ensure we have a clean kevents before calling kevent() */
123 	memset(kevents, 0, sizeof(struct kevent) * nkevents);
124 
125 	/* convert ms to timespec struct */
126 	t.tv_sec = timeout / 1000;
127 	t.tv_nsec = (timeout % 1000) * 1000 * 1000;
128 
129 	/* wait for incoming event or timeout */
130 	ret = kevent(kfd, NULL, 0, kevents, nkevents, &t);
131 	if (ret == -1) {
132 
133 		/* trigger error unless signal interrupt */
134 		if (errno != EINTR) {
135 			zlog(ZLOG_WARNING, "epoll_wait() returns %d", errno);
136 			return -1;
137 		}
138 	}
139 
140 	/* fire triggered events */
141 	for (i = 0; i < ret; i++) {
142 		if (kevents[i].udata) {
143 			struct fpm_event_s *ev = (struct fpm_event_s *)kevents[i].udata;
144 			fpm_event_fire(ev);
145 			/* sanity check */
146 			if (fpm_globals.parent_pid != getpid()) {
147 				return -2;
148 			}
149 		}
150 	}
151 
152 	return ret;
153 }
154 /* }}} */
155 
156 /*
157  * Add a FD to to kevent queue
158  */
fpm_event_kqueue_add(struct fpm_event_s * ev)159 static int fpm_event_kqueue_add(struct fpm_event_s *ev) /* {{{ */
160 {
161 	struct kevent k;
162 	int flags = EV_ADD;
163 
164 	if (ev->flags & FPM_EV_EDGE) {
165 			flags = flags | EV_CLEAR;
166 	}
167 
168 	EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
169 
170 	if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
171 		zlog(ZLOG_ERROR, "kevent: unable to add event");
172 		return -1;
173 	}
174 
175 	/* mark the event as registered */
176 	ev->index = ev->fd;
177 	return 0;
178 }
179 /* }}} */
180 
181 /*
182  * Remove a FD from the kevent queue
183  */
fpm_event_kqueue_remove(struct fpm_event_s * ev)184 static int fpm_event_kqueue_remove(struct fpm_event_s *ev) /* {{{ */
185 {
186 	struct kevent k;
187 	int flags = EV_DELETE;
188 
189 	if (ev->flags & FPM_EV_EDGE) {
190 			flags = flags | EV_CLEAR;
191 	}
192 
193 	EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
194 
195 	if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
196 		zlog(ZLOG_ERROR, "kevent: unable to add event");
197 		return -1;
198 	}
199 
200 	/* mark the vent as not registered */
201 	ev->index = -1;
202 	return 0;
203 }
204 /* }}} */
205 
206 #endif /* HAVE_KQUEUE */
207