php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
kqueue.c
Go to the documentation of this file.
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_KQUEUE
23
24#include <sys/types.h>
25#include <sys/event.h>
26#include <sys/time.h>
27
28#include <errno.h>
29
30static int fpm_event_kqueue_init(int max);
31static int fpm_event_kqueue_clean(void);
32static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
33static int fpm_event_kqueue_add(struct fpm_event_s *ev);
34static int fpm_event_kqueue_remove(struct fpm_event_s *ev);
35
36static 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
46static struct kevent *kevents = NULL;
47static int nkevents = 0;
48static int kfd = 0;
49
50#endif /* HAVE_KQUEUE */
51
52/*
53 * Return the module configuration
54 */
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 */
70static 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 = calloc(max, sizeof(struct kevent));
83 if (!kevents) {
84 zlog(ZLOG_ERROR, "kevent: unable to allocate %d events", max);
85 return -1;
86 }
87
88 nkevents = max;
89
90 return 0;
91}
92/* }}} */
93
94/*
95 * release kqueue stuff
96 */
97static int fpm_event_kqueue_clean(void) /* {{{ */
98{
99 if (kevents) {
100 free(kevents);
101 kevents = NULL;
102 }
103
104 nkevents = 0;
105
106 return 0;
107}
108/* }}} */
109
110/*
111 * wait for events or timeout
112 */
113static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout) /* {{{ */
114{
115 struct timespec t;
116 int ret, i;
117
118 /* ensure we have a clean kevents before calling kevent() */
119 memset(kevents, 0, sizeof(struct kevent) * nkevents);
120
121 /* convert ms to timespec struct */
122 t.tv_sec = timeout / 1000;
123 t.tv_nsec = (timeout % 1000) * 1000 * 1000;
124
125 /* wait for incoming event or timeout */
126 ret = kevent(kfd, NULL, 0, kevents, nkevents, &t);
127 if (ret == -1) {
128
129 /* trigger error unless signal interrupt */
130 if (errno != EINTR) {
131 zlog(ZLOG_WARNING, "kevent() returns %d", errno);
132 return -1;
133 }
134 }
135
136 /* fire triggered events */
137 for (i = 0; i < ret; i++) {
138 if (kevents[i].udata) {
139 struct fpm_event_s *ev = (struct fpm_event_s *)kevents[i].udata;
140 fpm_event_fire(ev);
141 /* sanity check */
142 if (fpm_globals.parent_pid != getpid()) {
143 return -2;
144 }
145 }
146 }
147
148 return ret;
149}
150/* }}} */
151
152/*
153 * Add a FD to to kevent queue
154 */
155static int fpm_event_kqueue_add(struct fpm_event_s *ev) /* {{{ */
156{
157 struct kevent k;
158 int flags = EV_ADD;
159
160 if (ev->flags & FPM_EV_EDGE) {
161 flags = flags | EV_CLEAR;
162 }
163
164 EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
165
166 if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
167 zlog(ZLOG_ERROR, "kevent: unable to add event");
168 return -1;
169 }
170
171 /* mark the event as registered */
172 ev->index = ev->fd;
173 return 0;
174}
175/* }}} */
176
177/*
178 * Remove a FD from the kevent queue
179 */
180static int fpm_event_kqueue_remove(struct fpm_event_s *ev) /* {{{ */
181{
182 struct kevent k;
183 int flags = EV_DELETE;
184
185 if (ev->flags & FPM_EV_EDGE) {
186 flags = flags | EV_CLEAR;
187 }
188
189 EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
190
191 if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
192 zlog(ZLOG_ERROR, "kevent: unable to delete event");
193 return -1;
194 }
195
196 /* mark the event as not registered */
197 ev->index = -1;
198 return 0;
199}
200/* }}} */
201
202#endif /* HAVE_KQUEUE */
#define max(a, b)
Definition exif.c:60
memset(ptr, 0, type->size)
struct fpm_globals_s fpm_globals
Definition fpm.c:24
void fpm_event_fire(struct fpm_event_s *ev)
Definition fpm_events.c:472
#define FPM_EV_EDGE
Definition fpm_events.h:9
#define NULL
Definition gdcache.h:45
struct fpm_event_module_s * fpm_event_kqueue_module(void)
Definition kqueue.c:55
#define errno
zval * ret
@ ZLOG_ERROR
Definition zlog.h:45
@ ZLOG_WARNING
Definition zlog.h:44
#define zlog(flags,...)
Definition zlog.h:9