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