diff options
Diffstat (limited to 'src/lib/daemon_get_fdset.c')
-rw-r--r-- | src/lib/daemon_get_fdset.c | 269 |
1 files changed, 0 insertions, 269 deletions
diff --git a/src/lib/daemon_get_fdset.c b/src/lib/daemon_get_fdset.c deleted file mode 100644 index c3b0dbd9..00000000 --- a/src/lib/daemon_get_fdset.c +++ /dev/null | |||
@@ -1,269 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @file lib/daemon_get_fdset.c | ||
22 | * @brief function to get select() fdset of a daemon | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "internal.h" | ||
26 | |||
27 | /** | ||
28 | * We defined a macro with the same name as a function we | ||
29 | * are implementing here. Need to undef the macro to avoid | ||
30 | * causing a conflict. | ||
31 | */ | ||
32 | #undef MHD_daemon_get_fdset | ||
33 | |||
34 | /** | ||
35 | * Obtain the `select()` sets for this daemon. Daemon's FDs will be | ||
36 | * added to fd_sets. To get only daemon FDs in fd_sets, call FD_ZERO | ||
37 | * for each fd_set before calling this function. FD_SETSIZE is assumed | ||
38 | * to be platform's default. | ||
39 | * | ||
40 | * This function should only be called in when MHD is configured to | ||
41 | * use external select with 'select()' or with 'epoll'. In the latter | ||
42 | * case, it will only add the single 'epoll()' file descriptor used by | ||
43 | * MHD to the sets. It's necessary to use #MHD_get_timeout() in | ||
44 | * combination with this function. | ||
45 | * | ||
46 | * This function must be called only for daemon started without | ||
47 | * #MHD_USE_INTERNAL_POLLING_THREAD flag. | ||
48 | * | ||
49 | * @param daemon daemon to get sets from | ||
50 | * @param read_fd_set read set | ||
51 | * @param write_fd_set write set | ||
52 | * @param except_fd_set except set | ||
53 | * @param max_fd increased to largest FD added (if larger | ||
54 | * than existing value); can be NULL | ||
55 | * @return #MHD_SC_OK on success, otherwise error code | ||
56 | * @ingroup event | ||
57 | */ | ||
58 | enum MHD_StatusCode | ||
59 | MHD_daemon_get_fdset (struct MHD_Daemon *daemon, | ||
60 | fd_set *read_fd_set, | ||
61 | fd_set *write_fd_set, | ||
62 | fd_set *except_fd_set, | ||
63 | MHD_socket *max_fd) | ||
64 | { | ||
65 | return MHD_daemon_get_fdset2 (daemon, | ||
66 | read_fd_set, | ||
67 | write_fd_set, | ||
68 | except_fd_set, | ||
69 | max_fd, | ||
70 | _MHD_SYS_DEFAULT_FD_SETSIZE); | ||
71 | } | ||
72 | |||
73 | |||
74 | /** | ||
75 | * Internal version of #MHD_daemon_get_fdset2(). | ||
76 | * | ||
77 | * @param daemon daemon to get sets from | ||
78 | * @param read_fd_set read set | ||
79 | * @param write_fd_set write set | ||
80 | * @param except_fd_set except set | ||
81 | * @param max_fd increased to largest FD added (if larger | ||
82 | * than existing value); can be NULL | ||
83 | * @param fd_setsize value of FD_SETSIZE | ||
84 | * @return #MHD_SC_OK on success | ||
85 | * @ingroup event | ||
86 | */ | ||
87 | static enum MHD_StatusCode | ||
88 | internal_get_fdset2 (struct MHD_Daemon *daemon, | ||
89 | fd_set *read_fd_set, | ||
90 | fd_set *write_fd_set, | ||
91 | fd_set *except_fd_set, | ||
92 | MHD_socket *max_fd, | ||
93 | unsigned int fd_setsize) | ||
94 | |||
95 | { | ||
96 | struct MHD_Connection *pos; | ||
97 | struct MHD_Connection *posn; | ||
98 | int result = MHD_YES; | ||
99 | MHD_socket ls; | ||
100 | |||
101 | if (daemon->shutdown) | ||
102 | return MHD_NO; | ||
103 | |||
104 | ls = daemon->listen_socket; | ||
105 | if ( (MHD_INVALID_SOCKET != ls) && | ||
106 | (! daemon->was_quiesced) && | ||
107 | (! MHD_add_to_fd_set_ (ls, | ||
108 | read_fd_set, | ||
109 | max_fd, | ||
110 | fd_setsize)) ) | ||
111 | result = MHD_NO; | ||
112 | |||
113 | /* Add all sockets to 'except_fd_set' as well to watch for | ||
114 | * out-of-band data. However, ignore errors if INFO_READ | ||
115 | * or INFO_WRITE sockets will not fit 'except_fd_set'. */ | ||
116 | /* Start from oldest connections. Make sense for W32 FDSETs. */ | ||
117 | for (pos = daemon->connections_tail; NULL != pos; pos = posn) | ||
118 | { | ||
119 | posn = pos->prev; | ||
120 | |||
121 | switch (pos->request.event_loop_info) | ||
122 | { | ||
123 | case MHD_EVENT_LOOP_INFO_READ: | ||
124 | if (! MHD_add_to_fd_set_ (pos->socket_fd, | ||
125 | read_fd_set, | ||
126 | max_fd, | ||
127 | fd_setsize)) | ||
128 | result = MHD_NO; | ||
129 | #ifdef MHD_POSIX_SOCKETS | ||
130 | MHD_add_to_fd_set_ (pos->socket_fd, | ||
131 | except_fd_set, | ||
132 | max_fd, | ||
133 | fd_setsize); | ||
134 | #endif /* MHD_POSIX_SOCKETS */ | ||
135 | break; | ||
136 | case MHD_EVENT_LOOP_INFO_WRITE: | ||
137 | if (! MHD_add_to_fd_set_ (pos->socket_fd, | ||
138 | write_fd_set, | ||
139 | max_fd, | ||
140 | fd_setsize)) | ||
141 | result = MHD_NO; | ||
142 | #ifdef MHD_POSIX_SOCKETS | ||
143 | MHD_add_to_fd_set_ (pos->socket_fd, | ||
144 | except_fd_set, | ||
145 | max_fd, | ||
146 | fd_setsize); | ||
147 | #endif /* MHD_POSIX_SOCKETS */ | ||
148 | break; | ||
149 | case MHD_EVENT_LOOP_INFO_BLOCK: | ||
150 | if ( (NULL == except_fd_set) || | ||
151 | ! MHD_add_to_fd_set_ (pos->socket_fd, | ||
152 | except_fd_set, | ||
153 | max_fd, | ||
154 | fd_setsize)) | ||
155 | result = MHD_NO; | ||
156 | break; | ||
157 | case MHD_EVENT_LOOP_INFO_CLEANUP: | ||
158 | /* this should never happen */ | ||
159 | break; | ||
160 | } | ||
161 | } | ||
162 | #ifdef MHD_WINSOCK_SOCKETS | ||
163 | /* W32 use limited array for fd_set so add INFO_READ/INFO_WRITE sockets | ||
164 | * only after INFO_BLOCK sockets to ensure that INFO_BLOCK sockets will | ||
165 | * not be pushed out. */ | ||
166 | for (pos = daemon->connections_tail; NULL != pos; pos = posn) | ||
167 | { | ||
168 | posn = pos->prev; | ||
169 | MHD_add_to_fd_set_ (pos->socket_fd, | ||
170 | except_fd_set, | ||
171 | max_fd, | ||
172 | fd_setsize); | ||
173 | } | ||
174 | #endif /* MHD_WINSOCK_SOCKETS */ | ||
175 | #if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT) | ||
176 | { | ||
177 | struct MHD_UpgradeResponseHandle *urh; | ||
178 | |||
179 | for (urh = daemon->urh_tail; NULL != urh; urh = urh->prev) | ||
180 | { | ||
181 | if (MHD_NO == | ||
182 | urh_to_fdset (urh, | ||
183 | read_fd_set, | ||
184 | write_fd_set, | ||
185 | except_fd_set, | ||
186 | max_fd, | ||
187 | fd_setsize)) | ||
188 | result = MHD_NO; | ||
189 | } | ||
190 | } | ||
191 | #endif | ||
192 | #if DEBUG_CONNECT | ||
193 | #ifdef HAVE_MESSAGES | ||
194 | if (NULL != max_fd) | ||
195 | MHD_DLOG (daemon, | ||
196 | _("Maximum socket in select set: %d\n"), | ||
197 | *max_fd); | ||
198 | #endif | ||
199 | #endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */ | ||
200 | return result; | ||
201 | } | ||
202 | |||
203 | |||
204 | /** | ||
205 | * Obtain the `select()` sets for this daemon. Daemon's FDs will be | ||
206 | * added to fd_sets. To get only daemon FDs in fd_sets, call FD_ZERO | ||
207 | * for each fd_set before calling this function. | ||
208 | * | ||
209 | * Passing custom FD_SETSIZE as @a fd_setsize allow usage of | ||
210 | * larger/smaller than platform's default fd_sets. | ||
211 | * | ||
212 | * This function should only be called in when MHD is configured to | ||
213 | * use external select with 'select()' or with 'epoll'. In the latter | ||
214 | * case, it will only add the single 'epoll' file descriptor used by | ||
215 | * MHD to the sets. It's necessary to use #MHD_get_timeout() in | ||
216 | * combination with this function. | ||
217 | * | ||
218 | * This function must be called only for daemon started | ||
219 | * without #MHD_USE_INTERNAL_POLLING_THREAD flag. | ||
220 | * | ||
221 | * @param daemon daemon to get sets from | ||
222 | * @param read_fd_set read set | ||
223 | * @param write_fd_set write set | ||
224 | * @param except_fd_set except set | ||
225 | * @param max_fd increased to largest FD added (if larger | ||
226 | * than existing value); can be NULL | ||
227 | * @param fd_setsize value of FD_SETSIZE | ||
228 | * @return #MHD_SC_OK on success, otherwise error code | ||
229 | * @ingroup event | ||
230 | */ | ||
231 | enum MHD_StatusCode | ||
232 | MHD_daemon_get_fdset2 (struct MHD_Daemon *daemon, | ||
233 | fd_set *read_fd_set, | ||
234 | fd_set *write_fd_set, | ||
235 | fd_set *except_fd_set, | ||
236 | MHD_socket *max_fd, | ||
237 | unsigned int fd_setsize) | ||
238 | { | ||
239 | if ( (MHD_TM_EXTERNAL_EVENT_LOOP != daemon->threading_model) || | ||
240 | (MHD_ELS_POLL == daemon->event_loop_syscall) ) | ||
241 | return MHD_SC_CONFIGURATION_MISSMATCH_FOR_GET_FDSET; | ||
242 | |||
243 | #ifdef EPOLL_SUPPORT | ||
244 | if (MHD_ELS_EPOLL == daemon->event_loop_syscall) | ||
245 | { | ||
246 | if (daemon->shutdown) | ||
247 | return MHD_SC_DAEMON_ALREADY_SHUTDOWN; | ||
248 | |||
249 | /* we're in epoll mode, use the epoll FD as a stand-in for | ||
250 | the entire event set */ | ||
251 | |||
252 | return MHD_add_to_fd_set_ (daemon->epoll_fd, | ||
253 | read_fd_set, | ||
254 | max_fd, | ||
255 | fd_setsize) | ||
256 | ? MHD_SC_OK | ||
257 | : MHD_SC_SOCKET_OUTSIDE_OF_FDSET_RANGE; | ||
258 | } | ||
259 | #endif | ||
260 | |||
261 | return internal_get_fdset2 (daemon, | ||
262 | read_fd_set, | ||
263 | write_fd_set, | ||
264 | except_fd_set, | ||
265 | max_fd, | ||
266 | fd_setsize); | ||
267 | } | ||
268 | |||
269 | /* end of daemon_get_fdset.c */ | ||