xref: /PHP-8.0/sapi/fpm/fpm/events/kqueue.c (revision ff90d42b)
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    | http://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_KQUEUE
23 
24 #include <sys/types.h>
25 #include <sys/event.h>
26 #include <sys/time.h>
27 
28 #include <errno.h>
29 
30 static int fpm_event_kqueue_init(int max);
31 static int fpm_event_kqueue_clean();
32 static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
33 static int fpm_event_kqueue_add(struct fpm_event_s *ev);
34 static int fpm_event_kqueue_remove(struct fpm_event_s *ev);
35 
36 static struct fpm_event_module_s kqueue_module = {
37 	.name = "kqueue",
38 	.support_edge_trigger = 1,
39 	.init = fpm_event_kqueue_init,
40 	.clean = fpm_event_kqueue_clean,
41 	.wait = fpm_event_kqueue_wait,
42 	.add = fpm_event_kqueue_add,
43 	.remove = fpm_event_kqueue_remove,
44 };
45 
46 static struct kevent *kevents = NULL;
47 static int nkevents = 0;
48 static int kfd = 0;
49 
50 #endif /* HAVE_KQUEUE */
51 
52 /*
53  * Return the module configuration
54  */
fpm_event_kqueue_module()55 struct fpm_event_module_s *fpm_event_kqueue_module() /* {{{ */
56 {
57 #ifdef HAVE_KQUEUE
58 	return &kqueue_module;
59 #else
60 	return NULL;
61 #endif /* HAVE_KQUEUE */
62 }
63 /* }}} */
64 
65 #ifdef HAVE_KQUEUE
66 
67 /*
68  * init kqueue and stuff
69  */
fpm_event_kqueue_init(int max)70 static int fpm_event_kqueue_init(int max) /* {{{ */
71 {
72 	if (max < 1) {
73 		return 0;
74 	}
75 
76 	kfd = kqueue();
77 	if (kfd < 0) {
78 		zlog(ZLOG_ERROR, "kqueue: unable to initialize");
79 		return -1;
80 	}
81 
82 	kevents = malloc(sizeof(struct kevent) * max);
83 	if (!kevents) {
84 		zlog(ZLOG_ERROR, "epoll: unable to allocate %d events", max);
85 		return -1;
86 	}
87 
88 	memset(kevents, 0, sizeof(struct kevent) * max);
89 
90 	nkevents = max;
91 
92 	return 0;
93 }
94 /* }}} */
95 
96 /*
97  * release kqueue stuff
98  */
fpm_event_kqueue_clean()99 static int fpm_event_kqueue_clean() /* {{{ */
100 {
101 	if (kevents) {
102 		free(kevents);
103 		kevents = NULL;
104 	}
105 
106 	nkevents = 0;
107 
108 	return 0;
109 }
110 /* }}} */
111 
112 /*
113  * wait for events or timeout
114  */
fpm_event_kqueue_wait(struct fpm_event_queue_s * queue,unsigned long int timeout)115 static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout) /* {{{ */
116 {
117 	struct timespec t;
118 	int ret, i;
119 
120 	/* ensure we have a clean kevents before calling kevent() */
121 	memset(kevents, 0, sizeof(struct kevent) * nkevents);
122 
123 	/* convert ms to timespec struct */
124 	t.tv_sec = timeout / 1000;
125 	t.tv_nsec = (timeout % 1000) * 1000 * 1000;
126 
127 	/* wait for incoming event or timeout */
128 	ret = kevent(kfd, NULL, 0, kevents, nkevents, &t);
129 	if (ret == -1) {
130 
131 		/* trigger error unless signal interrupt */
132 		if (errno != EINTR) {
133 			zlog(ZLOG_WARNING, "epoll_wait() returns %d", errno);
134 			return -1;
135 		}
136 	}
137 
138 	/* fire triggered events */
139 	for (i = 0; i < ret; i++) {
140 		if (kevents[i].udata) {
141 			struct fpm_event_s *ev = (struct fpm_event_s *)kevents[i].udata;
142 			fpm_event_fire(ev);
143 			/* sanity check */
144 			if (fpm_globals.parent_pid != getpid()) {
145 				return -2;
146 			}
147 		}
148 	}
149 
150 	return ret;
151 }
152 /* }}} */
153 
154 /*
155  * Add a FD to to kevent queue
156  */
fpm_event_kqueue_add(struct fpm_event_s * ev)157 static int fpm_event_kqueue_add(struct fpm_event_s *ev) /* {{{ */
158 {
159 	struct kevent k;
160 	int flags = EV_ADD;
161 
162 	if (ev->flags & FPM_EV_EDGE) {
163 			flags = flags | EV_CLEAR;
164 	}
165 
166 	EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
167 
168 	if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
169 		zlog(ZLOG_ERROR, "kevent: unable to add event");
170 		return -1;
171 	}
172 
173 	/* mark the event as registered */
174 	ev->index = ev->fd;
175 	return 0;
176 }
177 /* }}} */
178 
179 /*
180  * Remove a FD from the kevent queue
181  */
fpm_event_kqueue_remove(struct fpm_event_s * ev)182 static int fpm_event_kqueue_remove(struct fpm_event_s *ev) /* {{{ */
183 {
184 	struct kevent k;
185 	int flags = EV_DELETE;
186 
187 	if (ev->flags & FPM_EV_EDGE) {
188 			flags = flags | EV_CLEAR;
189 	}
190 
191 	EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
192 
193 	if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
194 		zlog(ZLOG_ERROR, "kevent: unable to delete event");
195 		return -1;
196 	}
197 
198 	/* mark the event as not registered */
199 	ev->index = -1;
200 	return 0;
201 }
202 /* }}} */
203 
204 #endif /* HAVE_KQUEUE */
205