aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_shutdown_select.c
blob: ae88658db348ee7bd1bc12efefd5a36828096e1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
     This file is part of libmicrohttpd
     Copyright (C) 2016 Karlson2k (Evgeny Grin)

     libmicrohttpd is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     libmicrohttpd is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libmicrohttpd; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file microhttpd/test_shutdown_select.c
 * @brief  Test whether shutdown socket triggers select()/poll()
 * @details On some platforms shutting down the socket in one thread
 *          trigger select() or poll() waiting for this socket in
 *          other thread. libmicrohttpd depend on this behavior on
 *          those platforms. This program check whether select()
 *          and poll() (if available) works as expected.
 * @author Karlson2k (Evgeny Grin)
 */

#include "MHD_config.h"
#include "platform.h"
#include "mhd_sockets.h"
#include <stdlib.h>
#include <stdio.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#ifdef HAVE_TIME_H
#include <time.h>
#endif /* HAVE_TIME_H */

#if defined(MHD_USE_POSIX_THREADS)
#include <pthread.h>
#endif /* MHD_USE_POSIX_THREADS */

#if defined(MHD_WINSOCK_SOCKETS)
#include <winsock2.h>
#include <windows.h>
#define sock_errno (WSAGetLastError ())
#elif defined(MHD_POSIX_SOCKETS)
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif /* HAVE_SYS_SOCKET_H */
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif /* HAVE_NETINET_IN_H */
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif /* HAVE_ARPA_INET_H */
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif /* HAVE_SYS_SELECT_H */
#if defined(HAVE_POLL) && defined(HAVE_POLL_H)
#include <poll.h>
#endif /* HAVE_POLL && HAVE_POLL_H */
#define sock_errno (errno)
#endif /* MHD_POSIX_SOCKETS */

#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#endif /* HAVE_STDBOOL_H */

#include "mhd_threads.h"

#ifndef SOMAXCONN
#define SOMAXCONN 511
#endif /* ! SOMAXCONN */

#if ! defined(SHUT_RDWR) && defined(SD_BOTH)
#define SHUT_RDWR SD_BOTH
#endif

static bool check_err;


static bool
has_in_name (const char *prog_name, const char *marker)
{
  size_t name_pos;
  size_t pos;

  if (! prog_name || ! marker)
    return 0;

  pos = 0;
  name_pos = 0;
  while (prog_name[pos])
  {
    if ('/' == prog_name[pos])
      name_pos = pos + 1;
#if defined(_WIN32) || defined(__CYGWIN__)
    else if ('\\' == prog_name[pos])
      name_pos = pos + 1;
#endif /* _WIN32 || __CYGWIN__ */
    pos++;
  }
  if (name_pos == pos)
    return true;
  return strstr (prog_name + name_pos, marker) != NULL;
}


static MHD_socket
start_socket_listen (int domain)
{
/* Create sockets similarly to daemon.c */
  MHD_socket fd;
  int cloexec_set;
  struct sockaddr_in sock_addr;
  socklen_t addrlen;

#ifdef MHD_WINSOCK_SOCKETS
  unsigned long flags = 1;
#else  /* MHD_POSIX_SOCKETS */
  int flags;
#endif /* MHD_POSIX_SOCKETS */

#if defined(MHD_POSIX_SOCKETS) && defined(SOCK_CLOEXEC)
  fd = socket (domain, SOCK_STREAM | SOCK_CLOEXEC, 0);
  cloexec_set = 1;
#elif defined(MHD_WINSOCK_SOCKETS) && defined (WSA_FLAG_NO_HANDLE_INHERIT)
  fd = WSASocketW (domain, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_NO_HANDLE_INHERIT);
  cloexec_set = 1;
#else  /* !SOCK_CLOEXEC */
  fd = socket (domain, SOCK_STREAM, 0);
  cloexec_set = 0;
#endif /* !SOCK_CLOEXEC */
  if ( (MHD_INVALID_SOCKET == fd) && (cloexec_set) )
  {
    fd = socket (domain, SOCK_STREAM, 0);
    cloexec_set = 0;
  }
  if (MHD_INVALID_SOCKET == fd)
  {
    fprintf (stderr, "Can't create socket: %u\n",
             (unsigned) sock_errno);
    return MHD_INVALID_SOCKET;
  }

  if (! cloexec_set)
  {
#ifdef MHD_WINSOCK_SOCKETS
    if (! SetHandleInformation ((HANDLE) fd, HANDLE_FLAG_INHERIT, 0))
      fprintf (stderr, "Failed to make socket non-inheritable: %u\n",
               (unsigned int) GetLastError ());
#else  /* MHD_POSIX_SOCKETS */
    flags = fcntl (fd, F_GETFD);
    if ( ( (-1 == flags) ||
           ( (flags != (flags | FD_CLOEXEC)) &&
             (0 != fcntl (fd, F_SETFD, flags | FD_CLOEXEC)) ) ) )
      fprintf (stderr, "Failed to make socket non-inheritable: %s\n",
               MHD_socket_last_strerr_ ());
#endif /* MHD_POSIX_SOCKETS */
  }

  memset (&sock_addr, 0, sizeof (struct sockaddr_in));
  sock_addr.sin_family = AF_INET;
  sock_addr.sin_port = htons (0);
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  sock_addr.sin_len = sizeof (struct sockaddr_in);
#endif
  addrlen = sizeof (struct sockaddr_in);

  if (bind (fd, (const struct sockaddr*) &sock_addr, addrlen) < 0)
  {
    fprintf (stderr, "Failed to bind socket: %u\n",
             (unsigned) sock_errno);
    MHD_socket_close_chk_ (fd);
    return MHD_INVALID_SOCKET;
  }

#ifdef MHD_WINSOCK_SOCKETS
  if (0 != ioctlsocket (fd, FIONBIO, &flags))
  {
    fprintf (stderr, "Failed to make socket non-blocking: %u\n",
             (unsigned) sock_errno);
    MHD_socket_close_chk_ (fd);
    return MHD_INVALID_SOCKET;
  }
#else  /* MHD_POSIX_SOCKETS */
  flags = fcntl (fd, F_GETFL);
  if ( ( (-1 == flags) ||
         ( (flags != (flags | O_NONBLOCK)) &&
           (0 != fcntl (fd, F_SETFL, flags | O_NONBLOCK)) ) ) )
  {
    fprintf (stderr, "Failed to make socket non-blocking: %s\n",
             MHD_socket_last_strerr_ ());
    MHD_socket_close_chk_ (fd);
    return MHD_INVALID_SOCKET;
  }
#endif /* MHD_POSIX_SOCKETS */

  if (listen (fd, SOMAXCONN) < 0)
  {
    fprintf (stderr, "Failed to listen on socket: %u\n",
             (unsigned) sock_errno);
    MHD_socket_close_chk_ (fd);
    return MHD_INVALID_SOCKET;
  }

  return fd;
}


MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_
select_thread (void*data)
{
  /* use select() like in daemon.c */
  MHD_socket listen_sock = *((MHD_socket*) data);
  fd_set rs, ws;
  struct timeval timeout;

  FD_ZERO (&rs);
  FD_ZERO (&ws);
  FD_SET (listen_sock, &rs);
  timeout.tv_usec = 0;
  timeout.tv_sec = 7;

  check_err = (0 > MHD_SYS_select_ (listen_sock + 1, &rs, &ws, NULL, &timeout));

  return (MHD_THRD_RTRN_TYPE_) 0;
}


#ifdef HAVE_POLL
MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_
poll_thread (void*data)
{
  /* use poll() like in daemon.c */
  struct pollfd p[1];
  MHD_socket listen_sock = *((MHD_socket*) data);

  p[0].fd = listen_sock;
  p[0].events = POLLIN;
  p[0].revents = 0;

  check_err = (0 > MHD_sys_poll_ (p, 1, 7000));

  return (MHD_THRD_RTRN_TYPE_) 0;
}
#endif /* HAVE_POLL */


static void
local_sleep (unsigned seconds)
{
#if defined(_WIN32) && ! defined(__CYGWIN__)
  Sleep (seconds * 1000);
#else
  unsigned seconds_left = seconds;
  do
  {
    seconds_left = sleep (seconds_left);
  } while (seconds_left > 0);
#endif
}


int
main (int argc, char *const *argv)
{
  int i;
  time_t start_t, end_t;
  int result = 0;
  MHD_THRD_RTRN_TYPE_ (MHD_THRD_CALL_SPEC_ * test_func)(void*data);
#ifdef MHD_WINSOCK_SOCKETS
  WORD ver_req;
  WSADATA wsa_data;
  int err;
#endif /* MHD_WINSOCK_SOCKETS */
  bool test_poll;
  bool must_ignore;
  (void) argc; /* Unused. Silent compiler warning. */

  test_poll = has_in_name (argv[0], "_poll");
  must_ignore = has_in_name (argv[0], "_ignore");
  if (! test_poll)
    test_func = &select_thread;
  else
  {
#ifndef HAVE_POLL
    return 77;
#else  /* ! HAVE_POLL */
    test_func = &poll_thread;
#endif /* ! HAVE_POLL */
  }

#ifdef MHD_WINSOCK_SOCKETS
  ver_req = MAKEWORD (2, 2);

  err = WSAStartup (ver_req, &wsa_data);
  if ((err != 0)||(MAKEWORD (2, 2) != wsa_data.wVersion))
  {
    printf ("WSAStartup() failed\n");
    WSACleanup ();
    return 99;
  }
#endif /* MHD_WINSOCK_SOCKETS */

  /* try several times to ensure that accidental incoming connection
   * didn't interfere with test results
   */
  for (i = 0; i < 5 && result == 0; i++)
  {
    MHD_thread_handle_ sel_thrd;
    /* fprintf(stdout, "Creating, binding and listening socket...\n"); */
    MHD_socket listen_socket = start_socket_listen (AF_INET);
    if (MHD_INVALID_SOCKET == listen_socket)
      return 99;

    check_err = true;
    /* fprintf (stdout, "Starting select() thread...\n"); */
#if defined(MHD_USE_POSIX_THREADS)
    if (0 != pthread_create (&sel_thrd, NULL, test_func, &listen_socket))
    {
      MHD_socket_close_chk_ (listen_socket);
      fprintf (stderr, "Can't start thread\n");
      return 99;
    }
#elif defined(MHD_USE_W32_THREADS)
    sel_thrd = (HANDLE) _beginthreadex (NULL, 0, test_func, &listen_socket, 0,
                                        NULL);
    if (0 == (sel_thrd))
    {
      MHD_socket_close_chk_ (listen_socket);
      fprintf (stderr, "Can't start select() thread\n");
      return 99;
    }
#else
#error No threading lib available
#endif
    /* fprintf (stdout, "Waiting...\n"); */
    local_sleep (1);  /* make sure that select() is started */

    /* fprintf (stdout, "Shutting down socket...\n"); */
    start_t = time (NULL);
    shutdown (listen_socket, SHUT_RDWR);

    /* fprintf (stdout, "Waiting for thread to finish...\n"); */
    if (! MHD_join_thread_ (sel_thrd))
    {
      MHD_socket_close_chk_ (listen_socket);
      fprintf (stderr, "Can't join select() thread\n");
      return 99;
    }
    if (check_err)
    {
      MHD_socket_close_chk_ (listen_socket);
      fprintf (stderr, "Error in waiting thread\n");
      return 99;
    }
    end_t = time (NULL);
    /* fprintf (stdout, "Thread finished.\n"); */
    MHD_socket_close_chk_ (listen_socket);

    if ((start_t == (time_t) -1) ||(end_t == (time_t) -1) )
    {
      MHD_socket_close_chk_ (listen_socket);
      fprintf (stderr, "Can't get current time\n");
      return 99;
    }
    if (end_t - start_t > 3)
      result++;
  }

#ifdef MHD_WINSOCK_SOCKETS
  WSACleanup ();
#endif /* MHD_WINSOCK_SOCKETS */

  return must_ignore ? (! result) : (result);
}