aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_shutdown_select.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/test_shutdown_select.c')
-rw-r--r--src/microhttpd/test_shutdown_select.c285
1 files changed, 143 insertions, 142 deletions
diff --git a/src/microhttpd/test_shutdown_select.c b/src/microhttpd/test_shutdown_select.c
index a334f63f..ae88658d 100644
--- a/src/microhttpd/test_shutdown_select.c
+++ b/src/microhttpd/test_shutdown_select.c
@@ -50,7 +50,7 @@
50#if defined(MHD_WINSOCK_SOCKETS) 50#if defined(MHD_WINSOCK_SOCKETS)
51#include <winsock2.h> 51#include <winsock2.h>
52#include <windows.h> 52#include <windows.h>
53#define sock_errno (WSAGetLastError()) 53#define sock_errno (WSAGetLastError ())
54#elif defined(MHD_POSIX_SOCKETS) 54#elif defined(MHD_POSIX_SOCKETS)
55#ifdef HAVE_SYS_TYPES_H 55#ifdef HAVE_SYS_TYPES_H
56#include <sys/types.h> 56#include <sys/types.h>
@@ -83,7 +83,7 @@
83#define SOMAXCONN 511 83#define SOMAXCONN 511
84#endif /* ! SOMAXCONN */ 84#endif /* ! SOMAXCONN */
85 85
86#if !defined(SHUT_RDWR) && defined(SD_BOTH) 86#if ! defined(SHUT_RDWR) && defined(SD_BOTH)
87#define SHUT_RDWR SD_BOTH 87#define SHUT_RDWR SD_BOTH
88#endif 88#endif
89 89
@@ -91,34 +91,34 @@ static bool check_err;
91 91
92 92
93static bool 93static bool
94has_in_name(const char *prog_name, const char *marker) 94has_in_name (const char *prog_name, const char *marker)
95{ 95{
96 size_t name_pos; 96 size_t name_pos;
97 size_t pos; 97 size_t pos;
98 98
99 if (!prog_name || !marker) 99 if (! prog_name || ! marker)
100 return 0; 100 return 0;
101 101
102 pos = 0; 102 pos = 0;
103 name_pos = 0; 103 name_pos = 0;
104 while (prog_name[pos]) 104 while (prog_name[pos])
105 { 105 {
106 if ('/' == prog_name[pos]) 106 if ('/' == prog_name[pos])
107 name_pos = pos + 1; 107 name_pos = pos + 1;
108#if defined(_WIN32) || defined(__CYGWIN__) 108#if defined(_WIN32) || defined(__CYGWIN__)
109 else if ('\\' == prog_name[pos]) 109 else if ('\\' == prog_name[pos])
110 name_pos = pos + 1; 110 name_pos = pos + 1;
111#endif /* _WIN32 || __CYGWIN__ */ 111#endif /* _WIN32 || __CYGWIN__ */
112 pos++; 112 pos++;
113 } 113 }
114 if (name_pos == pos) 114 if (name_pos == pos)
115 return true; 115 return true;
116 return strstr(prog_name + name_pos, marker) != NULL; 116 return strstr (prog_name + name_pos, marker) != NULL;
117} 117}
118 118
119 119
120static MHD_socket 120static MHD_socket
121start_socket_listen(int domain) 121start_socket_listen (int domain)
122{ 122{
123/* Create sockets similarly to daemon.c */ 123/* Create sockets similarly to daemon.c */
124 MHD_socket fd; 124 MHD_socket fd;
@@ -143,23 +143,23 @@ start_socket_listen(int domain)
143 cloexec_set = 0; 143 cloexec_set = 0;
144#endif /* !SOCK_CLOEXEC */ 144#endif /* !SOCK_CLOEXEC */
145 if ( (MHD_INVALID_SOCKET == fd) && (cloexec_set) ) 145 if ( (MHD_INVALID_SOCKET == fd) && (cloexec_set) )
146 { 146 {
147 fd = socket (domain, SOCK_STREAM, 0); 147 fd = socket (domain, SOCK_STREAM, 0);
148 cloexec_set = 0; 148 cloexec_set = 0;
149 } 149 }
150 if (MHD_INVALID_SOCKET == fd) 150 if (MHD_INVALID_SOCKET == fd)
151 { 151 {
152 fprintf (stderr, "Can't create socket: %u\n", 152 fprintf (stderr, "Can't create socket: %u\n",
153 (unsigned)sock_errno); 153 (unsigned) sock_errno);
154 return MHD_INVALID_SOCKET; 154 return MHD_INVALID_SOCKET;
155 } 155 }
156 156
157 if (!cloexec_set) 157 if (! cloexec_set)
158 { 158 {
159#ifdef MHD_WINSOCK_SOCKETS 159#ifdef MHD_WINSOCK_SOCKETS
160 if (!SetHandleInformation ((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) 160 if (! SetHandleInformation ((HANDLE) fd, HANDLE_FLAG_INHERIT, 0))
161 fprintf (stderr, "Failed to make socket non-inheritable: %u\n", 161 fprintf (stderr, "Failed to make socket non-inheritable: %u\n",
162 (unsigned int)GetLastError ()); 162 (unsigned int) GetLastError ());
163#else /* MHD_POSIX_SOCKETS */ 163#else /* MHD_POSIX_SOCKETS */
164 flags = fcntl (fd, F_GETFD); 164 flags = fcntl (fd, F_GETFD);
165 if ( ( (-1 == flags) || 165 if ( ( (-1 == flags) ||
@@ -168,7 +168,7 @@ start_socket_listen(int domain)
168 fprintf (stderr, "Failed to make socket non-inheritable: %s\n", 168 fprintf (stderr, "Failed to make socket non-inheritable: %s\n",
169 MHD_socket_last_strerr_ ()); 169 MHD_socket_last_strerr_ ());
170#endif /* MHD_POSIX_SOCKETS */ 170#endif /* MHD_POSIX_SOCKETS */
171 } 171 }
172 172
173 memset (&sock_addr, 0, sizeof (struct sockaddr_in)); 173 memset (&sock_addr, 0, sizeof (struct sockaddr_in));
174 sock_addr.sin_family = AF_INET; 174 sock_addr.sin_family = AF_INET;
@@ -179,73 +179,73 @@ start_socket_listen(int domain)
179 addrlen = sizeof (struct sockaddr_in); 179 addrlen = sizeof (struct sockaddr_in);
180 180
181 if (bind (fd, (const struct sockaddr*) &sock_addr, addrlen) < 0) 181 if (bind (fd, (const struct sockaddr*) &sock_addr, addrlen) < 0)
182 { 182 {
183 fprintf (stderr, "Failed to bind socket: %u\n", 183 fprintf (stderr, "Failed to bind socket: %u\n",
184 (unsigned)sock_errno); 184 (unsigned) sock_errno);
185 MHD_socket_close_chk_ (fd); 185 MHD_socket_close_chk_ (fd);
186 return MHD_INVALID_SOCKET; 186 return MHD_INVALID_SOCKET;
187 } 187 }
188 188
189#ifdef MHD_WINSOCK_SOCKETS 189#ifdef MHD_WINSOCK_SOCKETS
190 if (0 != ioctlsocket (fd, FIONBIO, &flags)) 190 if (0 != ioctlsocket (fd, FIONBIO, &flags))
191 { 191 {
192 fprintf (stderr, "Failed to make socket non-blocking: %u\n", 192 fprintf (stderr, "Failed to make socket non-blocking: %u\n",
193 (unsigned)sock_errno); 193 (unsigned) sock_errno);
194 MHD_socket_close_chk_ (fd); 194 MHD_socket_close_chk_ (fd);
195 return MHD_INVALID_SOCKET; 195 return MHD_INVALID_SOCKET;
196 } 196 }
197#else /* MHD_POSIX_SOCKETS */ 197#else /* MHD_POSIX_SOCKETS */
198 flags = fcntl (fd, F_GETFL); 198 flags = fcntl (fd, F_GETFL);
199 if ( ( (-1 == flags) || 199 if ( ( (-1 == flags) ||
200 ( (flags != (flags | O_NONBLOCK)) && 200 ( (flags != (flags | O_NONBLOCK)) &&
201 (0 != fcntl (fd, F_SETFL, flags | O_NONBLOCK)) ) ) ) 201 (0 != fcntl (fd, F_SETFL, flags | O_NONBLOCK)) ) ) )
202 { 202 {
203 fprintf (stderr, "Failed to make socket non-blocking: %s\n", 203 fprintf (stderr, "Failed to make socket non-blocking: %s\n",
204 MHD_socket_last_strerr_ ()); 204 MHD_socket_last_strerr_ ());
205 MHD_socket_close_chk_ (fd); 205 MHD_socket_close_chk_ (fd);
206 return MHD_INVALID_SOCKET; 206 return MHD_INVALID_SOCKET;
207 } 207 }
208#endif /* MHD_POSIX_SOCKETS */ 208#endif /* MHD_POSIX_SOCKETS */
209 209
210 if (listen(fd, SOMAXCONN) < 0) 210 if (listen (fd, SOMAXCONN) < 0)
211 { 211 {
212 fprintf (stderr, "Failed to listen on socket: %u\n", 212 fprintf (stderr, "Failed to listen on socket: %u\n",
213 (unsigned)sock_errno); 213 (unsigned) sock_errno);
214 MHD_socket_close_chk_ (fd); 214 MHD_socket_close_chk_ (fd);
215 return MHD_INVALID_SOCKET; 215 return MHD_INVALID_SOCKET;
216 } 216 }
217 217
218 return fd; 218 return fd;
219} 219}
220 220
221 221
222MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_ 222MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_
223select_thread(void* data) 223select_thread (void*data)
224{ 224{
225 /* use select() like in daemon.c */ 225 /* use select() like in daemon.c */
226 MHD_socket listen_sock = *((MHD_socket*)data); 226 MHD_socket listen_sock = *((MHD_socket*) data);
227 fd_set rs, ws; 227 fd_set rs, ws;
228 struct timeval timeout; 228 struct timeval timeout;
229 229
230 FD_ZERO(&rs); 230 FD_ZERO (&rs);
231 FD_ZERO(&ws); 231 FD_ZERO (&ws);
232 FD_SET(listen_sock, &rs); 232 FD_SET (listen_sock, &rs);
233 timeout.tv_usec = 0; 233 timeout.tv_usec = 0;
234 timeout.tv_sec = 7; 234 timeout.tv_sec = 7;
235 235
236 check_err = (0 > MHD_SYS_select_(listen_sock + 1, &rs, &ws, NULL, &timeout)); 236 check_err = (0 > MHD_SYS_select_ (listen_sock + 1, &rs, &ws, NULL, &timeout));
237 237
238 return (MHD_THRD_RTRN_TYPE_)0; 238 return (MHD_THRD_RTRN_TYPE_) 0;
239} 239}
240 240
241 241
242#ifdef HAVE_POLL 242#ifdef HAVE_POLL
243MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_ 243MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_
244poll_thread(void* data) 244poll_thread (void*data)
245{ 245{
246 /* use poll() like in daemon.c */ 246 /* use poll() like in daemon.c */
247 struct pollfd p[1]; 247 struct pollfd p[1];
248 MHD_socket listen_sock = *((MHD_socket*)data); 248 MHD_socket listen_sock = *((MHD_socket*) data);
249 249
250 p[0].fd = listen_sock; 250 p[0].fd = listen_sock;
251 p[0].events = POLLIN; 251 p[0].events = POLLIN;
@@ -253,22 +253,22 @@ poll_thread(void* data)
253 253
254 check_err = (0 > MHD_sys_poll_ (p, 1, 7000)); 254 check_err = (0 > MHD_sys_poll_ (p, 1, 7000));
255 255
256 return (MHD_THRD_RTRN_TYPE_)0; 256 return (MHD_THRD_RTRN_TYPE_) 0;
257} 257}
258#endif /* HAVE_POLL */ 258#endif /* HAVE_POLL */
259 259
260 260
261static void 261static void
262local_sleep(unsigned seconds) 262local_sleep (unsigned seconds)
263{ 263{
264#if defined(_WIN32) && !defined(__CYGWIN__) 264#if defined(_WIN32) && ! defined(__CYGWIN__)
265 Sleep(seconds * 1000); 265 Sleep (seconds * 1000);
266#else 266#else
267 unsigned seconds_left = seconds; 267 unsigned seconds_left = seconds;
268 do 268 do
269 { 269 {
270 seconds_left = sleep(seconds_left); 270 seconds_left = sleep (seconds_left);
271 } while (seconds_left > 0); 271 } while (seconds_left > 0);
272#endif 272#endif
273} 273}
274 274
@@ -279,7 +279,7 @@ main (int argc, char *const *argv)
279 int i; 279 int i;
280 time_t start_t, end_t; 280 time_t start_t, end_t;
281 int result = 0; 281 int result = 0;
282 MHD_THRD_RTRN_TYPE_ (MHD_THRD_CALL_SPEC_ *test_func)(void* data); 282 MHD_THRD_RTRN_TYPE_ (MHD_THRD_CALL_SPEC_ * test_func)(void*data);
283#ifdef MHD_WINSOCK_SOCKETS 283#ifdef MHD_WINSOCK_SOCKETS
284 WORD ver_req; 284 WORD ver_req;
285 WSADATA wsa_data; 285 WSADATA wsa_data;
@@ -287,101 +287,102 @@ main (int argc, char *const *argv)
287#endif /* MHD_WINSOCK_SOCKETS */ 287#endif /* MHD_WINSOCK_SOCKETS */
288 bool test_poll; 288 bool test_poll;
289 bool must_ignore; 289 bool must_ignore;
290 (void)argc; /* Unused. Silent compiler warning. */ 290 (void) argc; /* Unused. Silent compiler warning. */
291 291
292 test_poll = has_in_name(argv[0], "_poll"); 292 test_poll = has_in_name (argv[0], "_poll");
293 must_ignore = has_in_name(argv[0], "_ignore"); 293 must_ignore = has_in_name (argv[0], "_ignore");
294 if (! test_poll) 294 if (! test_poll)
295 test_func = &select_thread; 295 test_func = &select_thread;
296 else 296 else
297 { 297 {
298#ifndef HAVE_POLL 298#ifndef HAVE_POLL
299 return 77; 299 return 77;
300#else /* ! HAVE_POLL */ 300#else /* ! HAVE_POLL */
301 test_func = &poll_thread; 301 test_func = &poll_thread;
302#endif /* ! HAVE_POLL */ 302#endif /* ! HAVE_POLL */
303 } 303 }
304 304
305#ifdef MHD_WINSOCK_SOCKETS 305#ifdef MHD_WINSOCK_SOCKETS
306 ver_req = MAKEWORD(2, 2); 306 ver_req = MAKEWORD (2, 2);
307 307
308 err = WSAStartup(ver_req, &wsa_data); 308 err = WSAStartup (ver_req, &wsa_data);
309 if (err != 0 || MAKEWORD(2, 2) != wsa_data.wVersion) 309 if ((err != 0)||(MAKEWORD (2, 2) != wsa_data.wVersion))
310 { 310 {
311 printf("WSAStartup() failed\n"); 311 printf ("WSAStartup() failed\n");
312 WSACleanup(); 312 WSACleanup ();
313 return 99; 313 return 99;
314 } 314 }
315#endif /* MHD_WINSOCK_SOCKETS */ 315#endif /* MHD_WINSOCK_SOCKETS */
316 316
317 /* try several times to ensure that accidental incoming connection 317 /* try several times to ensure that accidental incoming connection
318 * didn't interfere with test results 318 * didn't interfere with test results
319 */ 319 */
320 for (i = 0; i < 5 && result == 0; i++) 320 for (i = 0; i < 5 && result == 0; i++)
321 { 321 {
322 MHD_thread_handle_ sel_thrd; 322 MHD_thread_handle_ sel_thrd;
323 /* fprintf(stdout, "Creating, binding and listening socket...\n"); */ 323 /* fprintf(stdout, "Creating, binding and listening socket...\n"); */
324 MHD_socket listen_socket = start_socket_listen (AF_INET); 324 MHD_socket listen_socket = start_socket_listen (AF_INET);
325 if (MHD_INVALID_SOCKET == listen_socket) 325 if (MHD_INVALID_SOCKET == listen_socket)
326 return 99; 326 return 99;
327 327
328 check_err = true; 328 check_err = true;
329 /* fprintf (stdout, "Starting select() thread...\n"); */ 329 /* fprintf (stdout, "Starting select() thread...\n"); */
330#if defined(MHD_USE_POSIX_THREADS) 330#if defined(MHD_USE_POSIX_THREADS)
331 if (0 != pthread_create (&sel_thrd, NULL, test_func, &listen_socket)) 331 if (0 != pthread_create (&sel_thrd, NULL, test_func, &listen_socket))
332 { 332 {
333 MHD_socket_close_chk_ (listen_socket); 333 MHD_socket_close_chk_ (listen_socket);
334 fprintf (stderr, "Can't start thread\n"); 334 fprintf (stderr, "Can't start thread\n");
335 return 99; 335 return 99;
336 } 336 }
337#elif defined(MHD_USE_W32_THREADS) 337#elif defined(MHD_USE_W32_THREADS)
338 sel_thrd = (HANDLE)_beginthreadex (NULL, 0, test_func, &listen_socket, 0, NULL); 338 sel_thrd = (HANDLE) _beginthreadex (NULL, 0, test_func, &listen_socket, 0,
339 if (0 == (sel_thrd)) 339 NULL);
340 { 340 if (0 == (sel_thrd))
341 MHD_socket_close_chk_ (listen_socket); 341 {
342 fprintf (stderr, "Can't start select() thread\n"); 342 MHD_socket_close_chk_ (listen_socket);
343 return 99; 343 fprintf (stderr, "Can't start select() thread\n");
344 } 344 return 99;
345 }
345#else 346#else
346#error No threading lib available 347#error No threading lib available
347#endif 348#endif
348 /* fprintf (stdout, "Waiting...\n"); */ 349 /* fprintf (stdout, "Waiting...\n"); */
349 local_sleep(1); /* make sure that select() is started */ 350 local_sleep (1); /* make sure that select() is started */
350 351
351 /* fprintf (stdout, "Shutting down socket...\n"); */ 352 /* fprintf (stdout, "Shutting down socket...\n"); */
352 start_t = time (NULL); 353 start_t = time (NULL);
353 shutdown (listen_socket, SHUT_RDWR); 354 shutdown (listen_socket, SHUT_RDWR);
354 355
355 /* fprintf (stdout, "Waiting for thread to finish...\n"); */ 356 /* fprintf (stdout, "Waiting for thread to finish...\n"); */
356 if (!MHD_join_thread_(sel_thrd)) 357 if (! MHD_join_thread_ (sel_thrd))
357 { 358 {
358 MHD_socket_close_chk_(listen_socket); 359 MHD_socket_close_chk_ (listen_socket);
359 fprintf (stderr, "Can't join select() thread\n"); 360 fprintf (stderr, "Can't join select() thread\n");
360 return 99; 361 return 99;
361 } 362 }
362 if (check_err) 363 if (check_err)
363 { 364 {
364 MHD_socket_close_chk_(listen_socket); 365 MHD_socket_close_chk_ (listen_socket);
365 fprintf (stderr, "Error in waiting thread\n"); 366 fprintf (stderr, "Error in waiting thread\n");
366 return 99; 367 return 99;
367 } 368 }
368 end_t = time (NULL); 369 end_t = time (NULL);
369 /* fprintf (stdout, "Thread finished.\n"); */ 370 /* fprintf (stdout, "Thread finished.\n"); */
370 MHD_socket_close_chk_(listen_socket); 371 MHD_socket_close_chk_ (listen_socket);
371 372
372 if (start_t == (time_t)-1 || end_t == (time_t)-1) 373 if ((start_t == (time_t) -1) ||(end_t == (time_t) -1) )
373 { 374 {
374 MHD_socket_close_chk_(listen_socket); 375 MHD_socket_close_chk_ (listen_socket);
375 fprintf (stderr, "Can't get current time\n"); 376 fprintf (stderr, "Can't get current time\n");
376 return 99; 377 return 99;
377 }
378 if (end_t - start_t > 3)
379 result++;
380 } 378 }
379 if (end_t - start_t > 3)
380 result++;
381 }
381 382
382#ifdef MHD_WINSOCK_SOCKETS 383#ifdef MHD_WINSOCK_SOCKETS
383 WSACleanup(); 384 WSACleanup ();
384#endif /* MHD_WINSOCK_SOCKETS */ 385#endif /* MHD_WINSOCK_SOCKETS */
385 386
386 return must_ignore ? (!result) : (result); 387 return must_ignore ? (! result) : (result);
387} 388}