1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 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 /* $Id$ */
20
21 #include "../fpm_config.h"
22 #include "../fpm_events.h"
23 #include "../fpm.h"
24 #include "../zlog.h"
25
26 #if HAVE_SELECT
27
28 /* According to POSIX.1-2001 */
29 #include <sys/select.h>
30
31 /* According to earlier standards */
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include <errno.h>
37
38 static int fpm_event_select_init(int max);
39 static int fpm_event_select_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
40 static int fpm_event_select_add(struct fpm_event_s *ev);
41 static int fpm_event_select_remove(struct fpm_event_s *ev);
42
43 static struct fpm_event_module_s select_module = {
44 .name = "select",
45 .support_edge_trigger = 0,
46 .init = fpm_event_select_init,
47 .clean = NULL,
48 .wait = fpm_event_select_wait,
49 .add = fpm_event_select_add,
50 .remove = fpm_event_select_remove,
51 };
52
53 static fd_set fds;
54
55 #endif /* HAVE_SELECT */
56
57 /*
58 * return the module configuration
59 */
fpm_event_select_module()60 struct fpm_event_module_s *fpm_event_select_module() /* {{{ */
61 {
62 #if HAVE_SELECT
63 return &select_module;
64 #else
65 return NULL;
66 #endif /* HAVE_SELECT */
67 }
68 /* }}} */
69
70 #if HAVE_SELECT
71
72 /*
73 * Init the module
74 */
fpm_event_select_init(int max)75 static int fpm_event_select_init(int max) /* {{{ */
76 {
77 FD_ZERO(&fds);
78 return 0;
79 }
80 /* }}} */
81
82
83 /*
84 * wait for events or timeout
85 */
fpm_event_select_wait(struct fpm_event_queue_s * queue,unsigned long int timeout)86 static int fpm_event_select_wait(struct fpm_event_queue_s *queue, unsigned long int timeout) /* {{{ */
87 {
88 int ret;
89 struct fpm_event_queue_s *q;
90 fd_set current_fds;
91 struct timeval t;
92
93 /* copy fds because select() alters it */
94 current_fds = fds;
95
96 /* fill struct timeval with timeout */
97 t.tv_sec = timeout / 1000;
98 t.tv_usec = (timeout % 1000) * 1000;
99
100 /* wait for inconming event or timeout */
101 ret = select(FD_SETSIZE, ¤t_fds, NULL, NULL, &t);
102 if (ret == -1) {
103
104 /* trigger error unless signal interrupt */
105 if (errno != EINTR) {
106 zlog(ZLOG_WARNING, "poll() returns %d", errno);
107 return -1;
108 }
109 }
110
111 /* events have been triggered */
112 if (ret > 0) {
113
114 /* trigger POLLIN events */
115 q = queue;
116 while (q) {
117 if (q->ev) { /* sanity check */
118
119 /* check if the event has been triggered */
120 if (FD_ISSET(q->ev->fd, ¤t_fds)) {
121
122 /* fire the event */
123 fpm_event_fire(q->ev);
124
125 /* sanity check */
126 if (fpm_globals.parent_pid != getpid()) {
127 return -2;
128 }
129 }
130 }
131 q = q->next; /* iterate */
132 }
133 }
134 return ret;
135
136 }
137 /* }}} */
138
139 /*
140 * Add a FD to the fd set
141 */
fpm_event_select_add(struct fpm_event_s * ev)142 static int fpm_event_select_add(struct fpm_event_s *ev) /* {{{ */
143 {
144 /* check size limitation */
145 if (ev->fd >= FD_SETSIZE) {
146 zlog(ZLOG_ERROR, "select: not enough space in the select fd list (max = %d). Please consider using another event mechanism.", FD_SETSIZE);
147 return -1;
148 }
149
150 /* add the FD if not already in */
151 if (!FD_ISSET(ev->fd, &fds)) {
152 FD_SET(ev->fd, &fds);
153 ev->index = ev->fd;
154 }
155
156 return 0;
157 }
158 /* }}} */
159
160 /*
161 * Remove a FD from the fd set
162 */
fpm_event_select_remove(struct fpm_event_s * ev)163 static int fpm_event_select_remove(struct fpm_event_s *ev) /* {{{ */
164 {
165 /* remove the fd if it's in */
166 if (FD_ISSET(ev->fd, &fds)) {
167 FD_CLR(ev->fd, &fds);
168 ev->index = -1;
169 }
170
171 return 0;
172 }
173 /* }}} */
174
175 #endif /* HAVE_SELECT */
176