aboutsummaryrefslogtreecommitdiff
path: root/src/lib/daemon_start.c
blob: c8c498be0631ca66e08d7cee0ba53810f9d7eaff (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/*
  This file is part of libmicrohttpd
  Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff

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

  This library 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
  Lesser General Public License for more details.

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

/**
 * @file lib/daemon_start.c
 * @brief functions to start a daemon
 * @author Christian Grothoff
 */
#include "internal.h"
#include "connection_cleanup.h"
#include "daemon_close_all_connections.h"
#include "daemon_select.h"
#include "daemon_poll.h"
#include "daemon_epoll.h"
#include "request_resume.h"


/**
 * Set listen socket options to allow port rebinding (or not)
 * depending on how MHD was configured.
 *
 * @param[in,out] daemon the daemon with the listen socket to configure
 * @return #MHD_SC_OK on success (or non-fatal errors)
 */
static enum MHD_StatusCode
configure_listen_reuse (struct MHD_Daemon *daemon)
{
  const MHD_SCKT_OPT_BOOL_ on = 1;

  /* Apply the socket options according to
     listening_address_reuse. */
  if (daemon->allow_address_reuse)
  {
    /* User requested to allow reusing listening address:port. */
#ifndef MHD_WINSOCK_SOCKETS
    /* Use SO_REUSEADDR on non-W32 platforms, and do not fail if
     * it doesn't work. */
    if (0 > setsockopt (daemon->listen_socket,
                        SOL_SOCKET,
                        SO_REUSEADDR,
                        (void *) &on,
                        sizeof (on)))
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                MHD_SC_LISTEN_ADDRESS_REUSE_ENABLE_FAILED,
                _ ("setsockopt failed: %s\n"),
                MHD_socket_last_strerr_ ());
#endif
      return MHD_SC_LISTEN_ADDRESS_REUSE_ENABLE_FAILED;
    }
    return MHD_SC_OK;
#endif /* ! MHD_WINSOCK_SOCKETS */
       /* Use SO_REUSEADDR on Windows and SO_REUSEPORT on most platforms.
        * Fail if SO_REUSEPORT is not defined or setsockopt fails.
        */
       /* SO_REUSEADDR on W32 has the same semantics
    as SO_REUSEPORT on BSD/Linux */
#if defined(MHD_WINSOCK_SOCKETS) || defined(SO_REUSEPORT)
    if (0 > setsockopt (daemon->listen_socket,
                        SOL_SOCKET,
#ifndef MHD_WINSOCK_SOCKETS
                        SO_REUSEPORT,
#else  /* MHD_WINSOCK_SOCKETS */
                        SO_REUSEADDR,
#endif /* MHD_WINSOCK_SOCKETS */
                        (void *) &on,
                        sizeof (on)))
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                MHD_SC_LISTEN_ADDRESS_REUSE_ENABLE_FAILED,
                _ ("setsockopt failed: %s\n"),
                MHD_socket_last_strerr_ ());
#endif
      return MHD_SC_LISTEN_ADDRESS_REUSE_ENABLE_FAILED;
    }
    return MHD_SC_OK;
#else  /* !MHD_WINSOCK_SOCKETS && !SO_REUSEPORT */
       /* we're supposed to allow address:port re-use, but
    on this platform we cannot; fail hard */
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_ADDRESS_REUSE_ENABLE_NOT_SUPPORTED,
              _ (
                "Cannot allow listening address reuse: SO_REUSEPORT not defined\n"));
#endif
    return MHD_SC_LISTEN_ADDRESS_REUSE_ENABLE_NOT_SUPPORTED;
#endif /* !MHD_WINSOCK_SOCKETS && !SO_REUSEPORT */
  }

  /* if (! daemon->allow_address_reuse) */
  /* User requested to disallow reusing listening address:port.
   * Do nothing except for Windows where SO_EXCLUSIVEADDRUSE
   * is used and Solaris with SO_EXCLBIND.
   * Fail if MHD was compiled for W32 without SO_EXCLUSIVEADDRUSE
   * or setsockopt fails.
   */
#if (defined(MHD_WINSOCK_SOCKETS) && defined(SO_EXCLUSIVEADDRUSE)) || \
  (defined(__sun) && defined(SO_EXCLBIND))
  if (0 > setsockopt (daemon->listen_socket,
                      SOL_SOCKET,
#ifdef SO_EXCLUSIVEADDRUSE
                      SO_EXCLUSIVEADDRUSE,
#else  /* SO_EXCLBIND */
                      SO_EXCLBIND,
#endif /* SO_EXCLBIND */
                      (void *) &on,
                      sizeof (on)))
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_ADDRESS_REUSE_DISABLE_FAILED,
              _ ("setsockopt failed: %s\n"),
              MHD_socket_last_strerr_ ());
#endif
    return MHD_SC_LISTEN_ADDRESS_REUSE_DISABLE_FAILED;
  }
  return MHD_SC_OK;
#elif defined(MHD_WINSOCK_SOCKETS) /* SO_EXCLUSIVEADDRUSE not defined on W32? */
#ifdef HAVE_MESSAGES
  MHD_DLOG (daemon,
            MHD_SC_LISTEN_ADDRESS_REUSE_DISABLE_NOT_SUPPORTED,
            _ (
              "Cannot disallow listening address reuse: SO_EXCLUSIVEADDRUSE not defined\n"));
#endif
  return MHD_SC_LISTEN_ADDRESS_REUSE_DISABLE_NOT_SUPPORTED;
#endif /* MHD_WINSOCK_SOCKETS */
  /* Not on WINSOCK, simply doing nothing will do */
  return MHD_SC_OK;
}


/**
 * Open, configure and bind the listen socket (if required).
 *
 * @param[in,out] daemon daemon to open the socket for
 * @return #MHD_SC_OK on success
 */
static enum MHD_StatusCode
open_listen_socket (struct MHD_Daemon *daemon)
{
  enum MHD_StatusCode sc;
  socklen_t addrlen;
  struct sockaddr_storage ss;
  const struct sockaddr *sa;
  int pf;
  bool use_v6;

  if (MHD_INVALID_SOCKET != daemon->listen_socket)
    return MHD_SC_OK; /* application opened it for us! */
  pf = -1;
  /* Determine address family */
  switch (daemon->listen_af)
  {
  case MHD_AF_NONE:
    if (0 == daemon->listen_sa_len)
    {
      /* no listening desired, that's OK */
      return MHD_SC_OK;
    }
    /* we have a listen address, get AF from there! */
    switch (daemon->listen_sa.ss_family)
    {
    case AF_INET:
      pf = PF_INET;
      use_v6 = false;
      break;
#ifdef AF_INET6
    case AF_INET6:
      pf = PF_INET6;
      use_v6 = true;
      break;
#endif
#ifdef AF_UNIX
    case AF_UNIX:
      pf = PF_UNIX;
      use_v6 = false;
      break;
#endif
    default:
      return MHD_SC_AF_NOT_SUPPORTED_BY_BUILD;
    } /* switch on ss_family */
    break;   /* MHD_AF_NONE */
  case MHD_AF_AUTO:
#if HAVE_INET6
    pf = PF_INET6;
    use_v6 = true;
#else
    pf = PF_INET;
    use_v6 = false;
#endif
    break;
  case MHD_AF_INET4:
    use_v6 = false;
    pf = PF_INET;
    break;
  case MHD_AF_INET6:
  case MHD_AF_DUAL:
#if HAVE_INET6
    pf = PF_INET6;
    use_v6 = true;
    break;
#else
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_IPV6_NOT_SUPPORTED_BY_BUILD,
              _ ("IPv6 not supported by this build\n"));
#endif
    return MHD_SC_IPV6_NOT_SUPPORTED_BY_BUILD;
#endif
  }
  mhd_assert (-1 != pf);
  /* try to open listen socket */
try_open_listen_socket:
  daemon->listen_socket = MHD_socket_create_listen_ (pf);
  if ( (MHD_INVALID_SOCKET == daemon->listen_socket) &&
       (MHD_AF_AUTO == daemon->listen_af) &&
       (use_v6) )
  {
    use_v6 = false;
    pf = PF_INET;
    goto try_open_listen_socket;
  }
  if (MHD_INVALID_SOCKET == daemon->listen_socket)
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_FAILED_TO_OPEN_LISTEN_SOCKET,
              _ ("Failed to create socket for listening: %s\n"),
              MHD_socket_last_strerr_ ());
#endif
    return MHD_SC_FAILED_TO_OPEN_LISTEN_SOCKET;
  }

  if (MHD_SC_OK !=
      (sc = configure_listen_reuse (daemon)))
    return sc;

  /* configure for dual stack (or not) */
  if (use_v6)
  {
#if defined IPPROTO_IPV6 && defined IPV6_V6ONLY
    /* Note: "IPV6_V6ONLY" is declared by Windows Vista ff., see "IPPROTO_IPV6 Socket Options"
 (http://msdn.microsoft.com/en-us/library/ms738574%28v=VS.85%29.aspx);
 and may also be missing on older POSIX systems; good luck if you have any of those,
 your IPv6 socket may then also bind against IPv4 anyway... */
    const MHD_SCKT_OPT_BOOL_ v6_only =
      (MHD_AF_INET6 == daemon->listen_af);
    if (0 > setsockopt (daemon->listen_socket,
                        IPPROTO_IPV6,
                        IPV6_V6ONLY,
                        (const void *) &v6_only,
                        sizeof (v6_only)))
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                MHD_SC_LISTEN_DUAL_STACK_CONFIGURATION_FAILED,
                _ ("setsockopt failed: %s\n"),
                MHD_socket_last_strerr_ ());
#endif
    }
#else
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_DUAL_STACK_CONFIGURATION_NOT_SUPPORTED,
              _ (
                "Cannot explicitly setup dual stack behavior on this platform\n"));
#endif
#endif
  }

  /* Determine address to bind to */
  if (0 != daemon->listen_sa_len)
  {
    /* Bind address explicitly given */
    sa = (const struct sockaddr *) &daemon->listen_sa;
    addrlen = daemon->listen_sa_len;
  }
  else
  {
    /* Compute bind address based on port and AF */
#if HAVE_INET6
    if (use_v6)
    {
#ifdef IN6ADDR_ANY_INIT
      static const struct in6_addr static_in6any = IN6ADDR_ANY_INIT;
#endif
      struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;

      addrlen = sizeof (struct sockaddr_in6);
      memset (sin6,
              0,
              sizeof (struct sockaddr_in6));
      sin6->sin6_family = AF_INET6;
      sin6->sin6_port = htons (daemon->listen_port);
#ifdef IN6ADDR_ANY_INIT
      sin6->sin6_addr = static_in6any;
#endif
#if HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
      sin6->sin6_len = sizeof (struct sockaddr_in6);
#endif
    }
    else
#endif
    {
      struct sockaddr_in *sin4 = (struct sockaddr_in *) &ss;

      addrlen = sizeof (struct sockaddr_in);
      memset (sin4,
              0,
              sizeof (struct sockaddr_in));
      sin4->sin_family = AF_INET;
      sin4->sin_port = htons (daemon->listen_port);
      if (0 != INADDR_ANY)
        sin4->sin_addr.s_addr = htonl (INADDR_ANY);
#if HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
      sin4->sin_len = sizeof (struct sockaddr_in);
#endif
    }
    sa = (const struct sockaddr *) &ss;
  }

  /* actually do the bind() */
  if (-1 == bind (daemon->listen_socket,
                  sa,
                  addrlen))
  {
#ifdef HAVE_MESSAGES
    unsigned int port = 0;

    switch (sa->sa_family)
    {
    case AF_INET:
      if (addrlen == sizeof (struct sockaddr_in))
        port = ntohs (((const struct sockaddr_in *) sa)->sin_port);
      else
        port = UINT16_MAX + 1; /* indicate size error */
      break;
    case AF_INET6:
      if (addrlen == sizeof (struct sockaddr_in6))
        port = ntohs (((const struct sockaddr_in6 *) sa)->sin6_port);
      else
        port = UINT16_MAX + 1; /* indicate size error */
      break;
    default:
      port = UINT_MAX; /* AF_UNIX? */
      break;
    }
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_SOCKET_BIND_FAILED,
              _ ("Failed to bind to port %u: %s\n"),
              port,
              MHD_socket_last_strerr_ ());
#endif
    return MHD_SC_LISTEN_SOCKET_BIND_FAILED;
  }

  /* setup TCP_FASTOPEN */
#ifdef TCP_FASTOPEN
  if (MHD_FOM_DISABLE != daemon->fast_open_method)
  {
    if (0 != setsockopt (daemon->listen_socket,
                         IPPROTO_TCP,
                         TCP_FASTOPEN,
                         &daemon->fo_queue_length,
                         sizeof (daemon->fo_queue_length)))
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                MHD_SC_FAST_OPEN_FAILURE,
                _ ("setsockopt failed: %s\n"),
                MHD_socket_last_strerr_ ());
#endif
      if (MHD_FOM_REQUIRE == daemon->fast_open_method)
        return MHD_SC_FAST_OPEN_FAILURE;
    }
  }
#endif

  /* setup listening */
  if (0 > listen (daemon->listen_socket,
                  daemon->listen_backlog))
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_FAILURE,
              _ ("Failed to listen for connections: %s\n"),
              MHD_socket_last_strerr_ ());
#endif
    return MHD_SC_LISTEN_FAILURE;
  }
  return MHD_SC_OK;
}


/**
 * Obtain the listen port number from the socket (if it
 * was not explicitly set by us, i.e. if we were given
 * a listen socket or if the port was 0 and the OS picked
 * a free one).
 *
 * @param[in,out] daemon daemon to obtain the port number for
 */
static void
get_listen_port_number (struct MHD_Daemon *daemon)
{
  struct sockaddr_storage servaddr;
  socklen_t addrlen;

  if ( (0 != daemon->listen_port) ||
       (MHD_INVALID_SOCKET == daemon->listen_socket) )
    return; /* nothing to be done */

  memset (&servaddr,
          0,
          sizeof (struct sockaddr_storage));
  addrlen = sizeof (servaddr);
  if (0 != getsockname (daemon->listen_socket,
                        (struct sockaddr *) &servaddr,
                        &addrlen))
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_PORT_INTROSPECTION_FAILURE,
              _ ("Failed to get listen port number: %s\n"),
              MHD_socket_last_strerr_ ());
#endif /* HAVE_MESSAGES */
    return;
  }
#ifdef MHD_POSIX_SOCKETS
  if (sizeof (servaddr) < addrlen)
  {
    /* should be impossible with `struct sockaddr_storage` */
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_PORT_INTROSPECTION_FAILURE,
              _ (
                "Failed to get listen port number (`struct sockaddr_storage` too small!?)\n"));
#endif /* HAVE_MESSAGES */
    return;
  }
#endif /* MHD_POSIX_SOCKETS */
  switch (servaddr.ss_family)
  {
  case AF_INET:
    {
      struct sockaddr_in *s4 = (struct sockaddr_in *) &servaddr;

      daemon->listen_port = ntohs (s4->sin_port);
      break;
    }
#ifdef HAVE_INET6
  case AF_INET6:
    {
      struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &servaddr;

      daemon->listen_port = ntohs (s6->sin6_port);
      break;
    }
#endif /* HAVE_INET6 */
#ifdef AF_UNIX
  case AF_UNIX:
    daemon->listen_port = 0;   /* special value for UNIX domain sockets */
    break;
#endif
  default:
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_PORT_INTROSPECTION_UNKNOWN_AF,
              _ ("Unknown address family!\n"));
#endif
    daemon->listen_port = 0;   /* ugh */
    break;
  }
}


#ifdef EPOLL_SUPPORT
/**
 * Setup file descriptor to be used for epoll() control.
 *
 * @param daemon the daemon to setup epoll FD for
 * @return the epoll() fd to use
 */
static int
setup_epoll_fd (struct MHD_Daemon *daemon)
{
  int fd;

#ifndef HAVE_MESSAGES
  (void) daemon; /* Mute compiler warning. */
#endif /* ! HAVE_MESSAGES */

#ifdef USE_EPOLL_CREATE1
  fd = epoll_create1 (EPOLL_CLOEXEC);
#else  /* ! USE_EPOLL_CREATE1 */
  fd = epoll_create (MAX_EVENTS);
#endif /* ! USE_EPOLL_CREATE1 */
  if (MHD_INVALID_SOCKET == fd)
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_EPOLL_CTL_CREATE_FAILED,
              _ ("Call to epoll_create1 failed: %s\n"),
              MHD_socket_last_strerr_ ());
#endif
    return MHD_INVALID_SOCKET;
  }
#if ! defined(USE_EPOLL_CREATE1)
  if (! MHD_socket_noninheritable_ (fd))
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_EPOLL_CTL_CONFIGURE_NOINHERIT_FAILED,
              _ ("Failed to set noninheritable mode on epoll FD.\n"));
#endif
  }
#endif /* ! USE_EPOLL_CREATE1 */
  return fd;
}


/**
 * Setup epoll() FD for the daemon and initialize it to listen
 * on the listen FD.
 * @remark To be called only from thread that process
 * daemon's select()/poll()/etc.
 *
 * @param daemon daemon to initialize for epoll()
 * @return #MHD_SC_OK on success
 */
static enum MHD_StatusCode
setup_epoll_to_listen (struct MHD_Daemon *daemon)
{
  struct epoll_event event;
  MHD_socket ls;

  /* FIXME: update function! */
  daemon->epoll_fd = setup_epoll_fd (daemon);
  if (-1 == daemon->epoll_fd)
    return MHD_SC_EPOLL_CTL_CREATE_FAILED;
#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
  if (! daemon->disallow_upgrade)
  {
    daemon->epoll_upgrade_fd = setup_epoll_fd (daemon);
    if (MHD_INVALID_SOCKET == daemon->epoll_upgrade_fd)
      return MHD_SC_EPOLL_CTL_CREATE_FAILED;
  }
#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
  if ( (MHD_INVALID_SOCKET == (ls = daemon->listen_socket)) ||
       (daemon->was_quiesced) )
    return MHD_SC_OK; /* non-listening daemon */
  event.events = EPOLLIN;
  event.data.ptr = daemon;
  if (0 != epoll_ctl (daemon->epoll_fd,
                      EPOLL_CTL_ADD,
                      ls,
                      &event))
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_EPOLL_CTL_ADD_FAILED,
              _ ("Call to epoll_ctl failed: %s\n"),
              MHD_socket_last_strerr_ ());
#endif
    return MHD_SC_EPOLL_CTL_ADD_FAILED;
  }
  daemon->listen_socket_in_epoll = true;
  if (MHD_ITC_IS_VALID_ (daemon->itc))
  {
    event.events = EPOLLIN;
    event.data.ptr = (void *) daemon->epoll_itc_marker;
    if (0 != epoll_ctl (daemon->epoll_fd,
                        EPOLL_CTL_ADD,
                        MHD_itc_r_fd_ (daemon->itc),
                        &event))
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                MHD_SC_EPOLL_CTL_ADD_FAILED,
                _ ("Call to epoll_ctl failed: %s\n"),
                MHD_socket_last_strerr_ ());
#endif
      return MHD_SC_EPOLL_CTL_ADD_FAILED;
    }
  }
  return MHD_SC_OK;
}
#endif


/**
 * Thread that runs the polling loop until the daemon
 * is explicitly shut down.
 *
 * @param cls `struct MHD_Deamon` to run select loop in a thread for
 * @return always 0 (on shutdown)
 */
static MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_
MHD_polling_thread (void *cls)
{
  struct MHD_Daemon *daemon = cls;

  MHD_thread_init_ (&daemon->pid);
  while (! daemon->shutdown)
  {
    switch (daemon->event_loop_syscall)
    {
    case MHD_ELS_AUTO:
      MHD_PANIC ("MHD_ELS_AUTO should have been mapped to preferred style");
      break;
    case MHD_ELS_SELECT:
      MHD_daemon_select_ (daemon,
                          MHD_YES);
      break;
    case MHD_ELS_POLL:
#if HAVE_POLL
      MHD_daemon_poll_ (daemon,
                        MHD_YES);
#else
      MHD_PANIC ("MHD_ELS_POLL not supported, should have failed earlier");
#endif
      break;
    case MHD_ELS_EPOLL:
#ifdef EPOLL_SUPPORT
      MHD_daemon_epoll_ (daemon,
                         MHD_YES);
#else
      MHD_PANIC ("MHD_ELS_EPOLL not supported, should have failed earlier");
#endif
      break;
    }
    MHD_connection_cleanup_ (daemon);
  }
  /* Resume any pending for resume connections, join
   * all connection's threads (if any) and finally cleanup
   * everything. */
  if (! daemon->disallow_suspend_resume)
    MHD_resume_suspended_connections_ (daemon);
  MHD_daemon_close_all_connections_ (daemon);

  return (MHD_THRD_RTRN_TYPE_) 0;
}


/**
 * Setup the thread pool (if needed).
 *
 * @param[in,out] daemon daemon to setup thread pool for
 * @return #MHD_SC_OK on success
 */
static enum MHD_StatusCode
setup_thread_pool (struct MHD_Daemon *daemon)
{
  /* Coarse-grained count of connections per thread (note error
   * due to integer division). Also keep track of how many
   * connections are leftover after an equal split. */
  unsigned int conns_per_thread = daemon->global_connection_limit
                                  / daemon->threading_mode;
  unsigned int leftover_conns = daemon->global_connection_limit
                                % daemon->threading_mode;
  int i;
  enum MHD_StatusCode sc;

  /* Allocate memory for pooled objects */
  daemon->worker_pool = MHD_calloc_ (daemon->threading_mode,
                                     sizeof (struct MHD_Daemon));
  if (NULL == daemon->worker_pool)
    return MHD_SC_THREAD_POOL_MALLOC_FAILURE;

  /* Start the workers in the pool */
  for (i = 0; i < daemon->threading_mode; i++)
  {
    /* Create copy of the Daemon object for each worker */
    struct MHD_Daemon *d = &daemon->worker_pool[i];

    memcpy (d,
            daemon,
            sizeof (struct MHD_Daemon));
    /* Adjust pooling params for worker daemons; note that memcpy()
 has already copied MHD_USE_INTERNAL_POLLING_THREAD thread mode into
 the worker threads. */
    d->master = daemon;
    d->worker_pool_size = 0;
    d->worker_pool = NULL;
    /* Divide available connections evenly amongst the threads.
     * Thread indexes in [0, leftover_conns) each get one of the
     * leftover connections. */
    d->global_connection_limit = conns_per_thread;
    if (((unsigned int) i) < leftover_conns)
      ++d->global_connection_limit;

    if (! daemon->disable_itc)
    {
      if (! MHD_itc_init_ (d->itc))
      {
#ifdef HAVE_MESSAGES
        MHD_DLOG (daemon,
                  MHD_SC_ITC_INITIALIZATION_FAILED,
                  _ (
                    "Failed to create worker inter-thread communication channel: %s\n"),
                  MHD_itc_last_strerror_ () );
#endif
        sc = MHD_SC_ITC_INITIALIZATION_FAILED;
        goto thread_failed;
      }
      if ( (MHD_ELS_SELECT == daemon->event_loop_syscall) &&
           (! MHD_SCKT_FD_FITS_FDSET_ (MHD_itc_r_fd_ (d->itc),
                                       NULL)) )
      {
#ifdef HAVE_MESSAGES
        MHD_DLOG (daemon,
                  MHD_SC_ITC_DESCRIPTOR_TOO_LARGE,
                  _ (
                    "File descriptor for inter-thread communication channel exceeds maximum value\n"));
#endif
        MHD_itc_destroy_chk_ (d->itc);
        sc = MHD_SC_ITC_DESCRIPTOR_TOO_LARGE;
        goto thread_failed;
      }
    }
    else
    {
      MHD_itc_set_invalid_ (d->itc);
    }

#ifdef EPOLL_SUPPORT
    if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
         (MHD_SC_OK != (sc = setup_epoll_to_listen (d))) )
      goto thread_failed;
#endif

    /* Must init cleanup connection mutex for each worker */
    if (! MHD_mutex_init_ (&d->cleanup_connection_mutex))
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                MHD_SC_THREAD_POOL_CREATE_MUTEX_FAILURE,
                _ ("MHD failed to initialize cleanup connection mutex\n"));
#endif
      if (! daemon->disable_itc)
        MHD_itc_destroy_chk_ (d->itc);
      sc = MHD_SC_THREAD_POOL_CREATE_MUTEX_FAILURE;
      goto thread_failed;
    }

    /* Spawn the worker thread */
    if (! MHD_create_named_thread_ (&d->pid,
                                    "MHD-worker",
                                    daemon->thread_stack_limit_b,
                                    &MHD_polling_thread,
                                    d))
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                MHD_SC_THREAD_POOL_LAUNCH_FAILURE,
                _ ("Failed to create pool thread: %s\n"),
                MHD_strerror_ (errno));
#endif
      /* Free memory for this worker; cleanup below handles
       * all previously-created workers. */
      if (! daemon->disable_itc)
        MHD_itc_destroy_chk_ (d->itc);
      MHD_mutex_destroy_chk_ (&d->cleanup_connection_mutex);
      sc = MHD_SC_THREAD_POOL_LAUNCH_FAILURE;
      goto thread_failed;
    }
  }   /* end for() */
  return MHD_SC_OK;

thread_failed:
  /* If no worker threads created, then shut down normally. Calling
     MHD_stop_daemon (as we do below) doesn't work here since it
     assumes a 0-sized thread pool means we had been in the default
     MHD_USE_INTERNAL_POLLING_THREAD mode. */
  if (0 == i)
  {
    if (NULL != daemon->worker_pool)
    {
      free (daemon->worker_pool);
      daemon->worker_pool = NULL;
    }
    return MHD_SC_THREAD_LAUNCH_FAILURE;
  }
  /* Shutdown worker threads we've already created. Pretend
     as though we had fully initialized our daemon, but
     with a smaller number of threads than had been
     requested. */
  daemon->worker_pool_size = i;
  daemon->listen_socket = MHD_daemon_quiesce (daemon);
  return sc;
}


/**
 * Start a webserver.
 *
 * @param daemon daemon to start; you can no longer set
 *        options on this daemon after this call!
 * @return #MHD_SC_OK on success
 * @ingroup event
 */
enum MHD_StatusCode
MHD_daemon_start (struct MHD_Daemon *daemon)
{
  enum MHD_StatusCode sc;

  if (MHD_ELS_AUTO == daemon->event_loop_syscall)
  {
#if EPOLL_SUPPORT
    /* We do not support thread-per-connection in combination
 with epoll, so use poll in this case, otherwise prefer
 epoll. */
    if (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_mode)
      daemon->event_loop_syscall = MHD_ELS_POLL;
    else
      daemon->event_loop_syscall = MHD_ELS_EPOLL;
#elif HAVE_POLL
    daemon->event_loop_syscall = MHD_ELS_POLL;
#else
    daemon->event_loop_syscall = MHD_ELS_SELECT;
#endif
  }

#ifdef EPOLL_SUPPORT
  if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
       (0 == daemon->worker_pool_size) &&
       (MHD_INVALID_SOCKET != daemon->listen_socket) &&
       (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_mode) )
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_SYSCALL_THREAD_COMBINATION_INVALID,
              _ (
                "Combining MHD_USE_THREAD_PER_CONNECTION and MHD_USE_EPOLL is not supported.\n"));
#endif
    return MHD_SC_SYSCALL_THREAD_COMBINATION_INVALID;
  }
#endif

  /* Setup ITC */
  if ( (! daemon->disable_itc) &&
       (0 == daemon->worker_pool_size) )
  {
    if (! MHD_itc_init_ (daemon->itc))
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                MHD_SC_ITC_INITIALIZATION_FAILED,
                _ ("Failed to create inter-thread communication channel: %s\n"),
                MHD_itc_last_strerror_ ());
#endif
      return MHD_SC_ITC_INITIALIZATION_FAILED;
    }
    if ( (MHD_ELS_SELECT == daemon->event_loop_syscall) &&
         (! MHD_SCKT_FD_FITS_FDSET_ (MHD_itc_r_fd_ (daemon->itc),
                                     NULL)) )
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                MHD_SC_ITC_DESCRIPTOR_TOO_LARGE,
                _ (
                  "File descriptor for inter-thread communication channel exceeds maximum value\n"));
#endif
      return MHD_SC_ITC_DESCRIPTOR_TOO_LARGE;
    }
  }

  if (MHD_SC_OK != (sc = open_listen_socket (daemon)))
    return sc;

  /* Check listen socket is in range (if we are limited) */
  if ( (MHD_INVALID_SOCKET != daemon->listen_socket) &&
       (MHD_ELS_SELECT == daemon->event_loop_syscall) &&
       (! MHD_SCKT_FD_FITS_FDSET_ (daemon->listen_socket,
                                   NULL)) )
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_SOCKET_TOO_LARGE,
              _ ("Socket descriptor larger than FD_SETSIZE: %d > %d\n"),
              daemon->listen_socket,
              FD_SETSIZE);
#endif
    return MHD_SC_LISTEN_SOCKET_TOO_LARGE;
  }

  /* set listen socket to non-blocking */
  if ( (MHD_INVALID_SOCKET != daemon->listen_socket) &&
       (! MHD_socket_nonblocking_ (daemon->listen_socket)) )
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_LISTEN_SOCKET_NONBLOCKING_FAILURE,
              _ ("Failed to set nonblocking mode on listening socket: %s\n"),
              MHD_socket_last_strerr_ ());
#endif
    if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) ||
         (daemon->worker_pool_size > 0) )
    {
      /* Accept must be non-blocking. Multiple children may wake
       * up to handle a new connection, but only one will win the
       * race.  The others must immediately return. As this is
       * not possible, we must fail hard here. */
      return MHD_SC_LISTEN_SOCKET_NONBLOCKING_FAILURE;
    }
  }

#ifdef EPOLL_SUPPORT
  /* Setup epoll */
  if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
       (0 == daemon->worker_pool_size) &&
       (MHD_INVALID_SOCKET != daemon->listen_socket) &&
       (MHD_SC_OK != (sc = setup_epoll_to_listen (daemon))) )
    return sc;
#endif

  /* Setup main listen thread (only if we have no thread pool or
     external event loop and do have a listen socket) */
  /* FIXME: why no worker thread if we have no listen socket? */
  if ( ( (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_mode) ||
         (1 == daemon->threading_mode) ) &&
       (MHD_INVALID_SOCKET != daemon->listen_socket) &&
       (! MHD_create_named_thread_ (&daemon->pid,
                                    (MHD_TM_THREAD_PER_CONNECTION ==
                                     daemon->threading_mode)
                                    ? "MHD-listen"
                                    : "MHD-single",
                                    daemon->thread_stack_limit_b,
                                    &MHD_polling_thread,
                                    daemon) ) )
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_THREAD_MAIN_LAUNCH_FAILURE,
              _ ("Failed to create listen thread: %s\n"),
              MHD_strerror_ (errno));
#endif
    return MHD_SC_THREAD_MAIN_LAUNCH_FAILURE;
  }

  /* Setup worker threads */
  /* FIXME: why no thread pool if we have no listen socket? */
  if ( (1 < daemon->threading_mode) &&
       (MHD_INVALID_SOCKET != daemon->listen_socket) &&
       (MHD_SC_OK != (sc = setup_thread_pool (daemon))) )
    return sc;

  /* make sure we know our listen port (if any) */
  get_listen_port_number (daemon);

  return MHD_SC_OK;
}


/* end of daemon_start.c */