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