aboutsummaryrefslogtreecommitdiff
path: root/src/lib/daemon_epoll.c
blob: 2acb3cc6a44ec632eb86ee5d0d462ed144340905 (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
/*
  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_epoll.c
 * @brief functions to run epoll()-based event loop
 * @author Christian Grothoff
 */
#include "internal.h"
#include "daemon_epoll.h"

#ifdef EPOLL_SUPPORT

/**
 * How many events to we process at most per epoll() call?  Trade-off
 * between required stack-size and number of system calls we have to
 * make; 128 should be way enough to avoid more than one system call
 * for most scenarios, and still be moderate in stack size
 * consumption.  Embedded systems might want to choose a smaller value
 * --- but why use epoll() on such a system in the first place?
 */
#define MAX_EVENTS 128


#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)

/**
 * Checks whether @a urh has some data to process.
 *
 * @param urh upgrade handler to analyse
 * @return 'true' if @a urh has some data to process,
 *         'false' otherwise
 */
static bool
is_urh_ready (struct MHD_UpgradeResponseHandle * const urh)
{
  const struct MHD_Connection * const connection = urh->connection;

  if ( (0 == urh->in_buffer_size) &&
       (0 == urh->out_buffer_size) &&
       (0 == urh->in_buffer_used) &&
       (0 == urh->out_buffer_used) )
    return false;

  if (connection->daemon->shutdown)
    return true;

  if ( ( (0 != (MHD_EPOLL_STATE_READ_READY & urh->app.celi)) ||
         (connection->tls_read_ready) ) &&
       (urh->in_buffer_used < urh->in_buffer_size) )
    return true;

  if ( (0 != (MHD_EPOLL_STATE_READ_READY & urh->mhd.celi)) &&
       (urh->out_buffer_used < urh->out_buffer_size) )
    return true;

  if ( (0 != (MHD_EPOLL_STATE_WRITE_READY & urh->app.celi)) &&
       (urh->out_buffer_used > 0) )
    return true;

  if ( (0 != (MHD_EPOLL_STATE_WRITE_READY & urh->mhd.celi)) &&
         (urh->in_buffer_used > 0) )
    return true;

  return false;
}


/**
 * Do epoll()-based processing for TLS connections that have been
 * upgraded.  This requires a separate epoll() invocation as we
 * cannot use the `struct MHD_Connection` data structures for
 * the `union epoll_data` in this case.
 * @remark To be called only from thread that process
 * daemon's select()/poll()/etc.
 *
 * @param daemon the daemmon for which we process connections
 * @return #MHD_SC_OK on success
 */
static enum MHD_StatusCode
run_epoll_for_upgrade (struct MHD_Daemon *daemon)
{
  struct epoll_event events[MAX_EVENTS];
  int num_events;
  struct MHD_UpgradeResponseHandle * pos;
  struct MHD_UpgradeResponseHandle * prev;

  num_events = MAX_EVENTS;
  while (MAX_EVENTS == num_events)
    {
      unsigned int i;
      
      /* update event masks */
      num_events = epoll_wait (daemon->epoll_upgrade_fd,
			       events,
                               MAX_EVENTS,
                               0);
      if (-1 == num_events)
	{
          const int err = MHD_socket_get_error_ ();
          if (MHD_SCKT_ERR_IS_EINTR_ (err))
	    return MHD_SC_OK;
#ifdef HAVE_MESSAGES
          MHD_DLOG (daemon,
		    MHD_SC_UNEXPECTED_EPOLL_WAIT_ERROR,
                    _("Call to epoll_wait failed: %s\n"),
                    MHD_socket_strerr_ (err));
#endif
	  return MHD_SC_UNEXPECTED_EPOLL_WAIT_ERROR;
	}
      for (i = 0; i < (unsigned int) num_events; i++)
	{
          struct UpgradeEpollHandle * const ueh = events[i].data.ptr;
          struct MHD_UpgradeResponseHandle * const urh = ueh->urh;
          bool new_err_state = false;

          if (urh->clean_ready)
            continue;

          /* Update ueh state based on what is ready according to epoll() */
          if (0 != (events[i].events & EPOLLIN))
            ueh->celi |= MHD_EPOLL_STATE_READ_READY;
          if (0 != (events[i].events & EPOLLOUT))
            ueh->celi |= MHD_EPOLL_STATE_WRITE_READY;
          if (0 != (events[i].events & EPOLLHUP))
            ueh->celi |= MHD_EPOLL_STATE_READ_READY | MHD_EPOLL_STATE_WRITE_READY;

          if ( (0 == (ueh->celi & MHD_EPOLL_STATE_ERROR)) &&
               (0 != (events[i].events & (EPOLLERR | EPOLLPRI))) )
	    {
              /* Process new error state only one time
               * and avoid continuously marking this connection
               * as 'ready'. */
              ueh->celi |= MHD_EPOLL_STATE_ERROR;
              new_err_state = true;
	    }

          if (! urh->in_eready_list)
            {
              if (new_err_state ||
        	  is_urh_ready(urh))
        	{
        	  EDLL_insert (daemon->eready_urh_head,
			       daemon->eready_urh_tail,
			       urh);
        	  urh->in_eready_list = true;
        	}
            }
        }
    }
  prev = daemon->eready_urh_tail;
  while (NULL != (pos = prev))
    {
      prev = pos->prevE;
      process_urh (pos);
      if (! is_urh_ready(pos))
      	{
      	  EDLL_remove (daemon->eready_urh_head,
      		       daemon->eready_urh_tail,
      		       pos);
      	  pos->in_eready_list = false;
      	}
      /* Finished forwarding? */
      if ( (0 == pos->in_buffer_size) &&
           (0 == pos->out_buffer_size) &&
           (0 == pos->in_buffer_used) &&
           (0 == pos->out_buffer_used) )
        {
          MHD_connection_finish_forward_ (pos->connection);
          pos->clean_ready = true;
          /* If 'pos->was_closed' already was set to true, connection
           * will be moved immediately to cleanup list. Otherwise
           * connection will stay in suspended list until 'pos' will
           * be marked with 'was_closed' by application. */
          MHD_resume_connection (pos->connection);
        }
    }

  return MHD_SC_OK;
}
#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */


/**
 * Do epoll()-based processing (this function is allowed to
 * block if @a may_block is set to #MHD_YES).
 *
 * @param daemon daemon to run poll loop for
 * @param may_block true if blocking, false if non-blocking
 * @return #MHD_SC_OK on success
 */
enum MHD_StatusCode
MHD_daemon_epoll_ (struct MHD_Daemon *daemon,
		   bool may_block)
{
#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
  static const char * const upgrade_marker = "upgrade_ptr";
#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
  struct MHD_Connection *pos;
  struct MHD_Connection *prev;
  struct epoll_event events[MAX_EVENTS];
  struct epoll_event event;
  int timeout_ms;
  MHD_UNSIGNED_LONG_LONG timeout_ll;
  int num_events;
  unsigned int i;
  MHD_socket ls;
#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
  bool run_upgraded = false;
#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */

  if (-1 == daemon->epoll_fd)
    return MHD_SC_EPOLL_FD_INVALID; /* we're down! */
  if (daemon->shutdown)
    return MHD_SC_DAEMON_ALREADY_SHUTDOWN;
  if ( (MHD_INVALID_SOCKET != (ls = daemon->listen_socket)) &&
       (! daemon->was_quiesced) &&
       (daemon->connections < daemon->global_connection_limit) &&
       (! daemon->listen_socket_in_epoll) &&
       (! daemon->at_limit) )
    {
      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 ( (daemon->was_quiesced) &&
       (daemon->listen_socket_in_epoll) )
  {
    if (0 != epoll_ctl (daemon->epoll_fd,
                        EPOLL_CTL_DEL,
                        ls,
                        NULL))
      MHD_PANIC ("Failed to remove listen FD from epoll set\n");
    daemon->listen_socket_in_epoll = false;
  }

#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
  if ( (! daemon->upgrade_fd_in_epoll) &&
       (-1 != daemon->epoll_upgrade_fd) )
    {
      event.events = EPOLLIN | EPOLLOUT;
      event.data.ptr = (void *) upgrade_marker;
      if (0 != epoll_ctl (daemon->epoll_fd,
			  EPOLL_CTL_ADD,
			  daemon->epoll_upgrade_fd,
			  &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->upgrade_fd_in_epoll = true;
    }
#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
  if ( (daemon->listen_socket_in_epoll) &&
       ( (daemon->connections == daemon->global_connection_limit) ||
         (daemon->at_limit) ||
         (daemon->was_quiesced) ) )
    {
      /* we're at the connection limit, disable listen socket
	 for event loop for now */
      if (0 != epoll_ctl (daemon->epoll_fd,
			  EPOLL_CTL_DEL,
			  ls,
			  NULL))
	MHD_PANIC (_("Failed to remove listen FD from epoll set\n"));
      daemon->listen_socket_in_epoll = false;
    }

  if ( (! daemon->disallow_suspend_resume) &&
       (MHD_YES == resume_suspended_connections (daemon)) )
    may_block = false;

  if (may_block)
    {
      if (MHD_SC_OK == /* FIXME: distinguish between NO_TIMEOUT and errors */
	  MHD_daemon_get_timeout (daemon,
				  &timeout_ll))
	{
	  if (timeout_ll >= (MHD_UNSIGNED_LONG_LONG) INT_MAX)
	    timeout_ms = INT_MAX;
	  else
	    timeout_ms = (int) timeout_ll;
	}
      else
	timeout_ms = -1;
    }
  else
    timeout_ms = 0;

  /* Reset. New value will be set when connections are processed. */
  /* Note: Used mostly for uniformity here as same situation is
   * signaled in epoll mode by non-empty eready DLL. */
  daemon->data_already_pending = false;

  /* drain 'epoll' event queue; need to iterate as we get at most
     MAX_EVENTS in one system call here; in practice this should
     pretty much mean only one round, but better an extra loop here
     than unfair behavior... */
  num_events = MAX_EVENTS;
  while (MAX_EVENTS == num_events)
    {
      /* update event masks */
      num_events = epoll_wait (daemon->epoll_fd,
			       events,
                               MAX_EVENTS,
                               timeout_ms);
      if (-1 == num_events)
	{
          const int err = MHD_socket_get_error_ ();
          if (MHD_SCKT_ERR_IS_EINTR_ (err))
	    return MHD_SC_OK;
#ifdef HAVE_MESSAGES
          MHD_DLOG (daemon,
		    MHD_SC_UNEXPECTED_EPOLL_WAIT_ERROR,
                    _("Call to epoll_wait failed: %s\n"),
                    MHD_socket_strerr_ (err));
#endif
	  return MHD_SC_UNEXPECTED_EPOLL_WAIT_ERROR;
	}
      for (i=0;i<(unsigned int) num_events;i++)
	{
          /* First, check for the values of `ptr` that would indicate
             that this event is not about a normal connection. */
	  if (NULL == events[i].data.ptr)
	    continue; /* shutdown signal! */
#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
          if (upgrade_marker == events[i].data.ptr)
            {
              /* activity on an upgraded connection, we process
                 those in a separate epoll() */
              run_upgraded = true;
              continue;
            }
#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
          if (daemon->epoll_itc_marker == events[i].data.ptr)
            {
              /* It's OK to clear ITC here as all external
                 conditions will be processed later. */
              MHD_itc_clear_ (daemon->itc);
              continue;
            }
	  if (daemon == events[i].data.ptr)
	    {
              /* Check for error conditions on listen socket. */
              /* FIXME: Initiate MHD_quiesce_daemon() to prevent busy waiting? */
              if (0 == (events[i].events & (EPOLLERR | EPOLLHUP)))
                {
                  unsigned int series_length = 0;
                  /* Run 'accept' until it fails or daemon at limit of connections.
                   * Do not accept more then 10 connections at once. The rest will
                   * be accepted on next turn (level trigger is used for listen
                   * socket). */
                  while ( (MHD_YES ==
			   MHD_accept_connection (daemon)) &&
                          (series_length < 10) &&
                          (daemon->connections < daemon->global_connection_limit) &&
                          (! daemon->at_limit) )
                    series_length++;
	        }
              continue;
	    }
          /* this is an event relating to a 'normal' connection,
             remember the event and if appropriate mark the
             connection as 'eready'. */
          pos = events[i].data.ptr;
          /* normal processing: update read/write data */
          if (0 != (events[i].events & (EPOLLPRI | EPOLLERR | EPOLLHUP)))
            {
              pos->epoll_state |= MHD_EPOLL_STATE_ERROR;
              if (0 == (pos->epoll_state & MHD_EPOLL_STATE_IN_EREADY_EDLL))
                {
                  EDLL_insert (daemon->eready_head,
                               daemon->eready_tail,
                               pos);
                  pos->epoll_state |= MHD_EPOLL_STATE_IN_EREADY_EDLL;
                }
            }
          else
            {
              if (0 != (events[i].events & EPOLLIN))
                {
                  pos->epoll_state |= MHD_EPOLL_STATE_READ_READY;
                  if ( ( (MHD_EVENT_LOOP_INFO_READ == pos->request.event_loop_info) ||
                         (pos->request.read_buffer_size > pos->request.read_buffer_offset) ) &&
                       (0 == (pos->epoll_state & MHD_EPOLL_STATE_IN_EREADY_EDLL) ) )
                    {
                      EDLL_insert (daemon->eready_head,
                                   daemon->eready_tail,
                                   pos);
                      pos->epoll_state |= MHD_EPOLL_STATE_IN_EREADY_EDLL;
                    }
                }
              if (0 != (events[i].events & EPOLLOUT))
                {
                  pos->epoll_state |= MHD_EPOLL_STATE_WRITE_READY;
                  if ( (MHD_EVENT_LOOP_INFO_WRITE == pos->request.event_loop_info) &&
                       (0 == (pos->epoll_state & MHD_EPOLL_STATE_IN_EREADY_EDLL) ) )
                    {
                      EDLL_insert (daemon->eready_head,
                                   daemon->eready_tail,
                                   pos);
                      pos->epoll_state |= MHD_EPOLL_STATE_IN_EREADY_EDLL;
                    }
                }
            }
        }
    }

#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
  if (run_upgraded)
    run_epoll_for_upgrade (daemon); /* FIXME: return value? */
#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */

  /* process events for connections */
  prev = daemon->eready_tail;
  while (NULL != (pos = prev))
    {
      prev = pos->prevE;
      call_handlers (pos,
                     0 != (pos->epoll_state & MHD_EPOLL_STATE_READ_READY),
                     0 != (pos->epoll_state & MHD_EPOLL_STATE_WRITE_READY),
                     0 != (pos->epoll_state & MHD_EPOLL_STATE_ERROR));
      if (MHD_EPOLL_STATE_IN_EREADY_EDLL ==
            (pos->epoll_state & (MHD_EPOLL_STATE_SUSPENDED | MHD_EPOLL_STATE_IN_EREADY_EDLL)))
        {
          if ( (MHD_EVENT_LOOP_INFO_READ == pos->request.event_loop_info &&
		0 == (pos->epoll_state & MHD_EPOLL_STATE_READ_READY) ) ||
               (MHD_EVENT_LOOP_INFO_WRITE == pos->request.event_loop_info &&
		0 == (pos->epoll_state & MHD_EPOLL_STATE_WRITE_READY) ) ||
               MHD_EVENT_LOOP_INFO_CLEANUP == pos->request.event_loop_info)
            {
              EDLL_remove (daemon->eready_head,
                           daemon->eready_tail,
                           pos);
              pos->epoll_state &= ~MHD_EPOLL_STATE_IN_EREADY_EDLL;
            }
        }
    }

  /* Finally, handle timed-out connections; we need to do this here
     as the epoll mechanism won't call the 'MHD_connection_handle_idle()' on everything,
     as the other event loops do.  As timeouts do not get an explicit
     event, we need to find those connections that might have timed out
     here.

     Connections with custom timeouts must all be looked at, as we
     do not bother to sort that (presumably very short) list. */
  prev = daemon->manual_timeout_tail;
  while (NULL != (pos = prev))
    {
      prev = pos->prevX;
      MHD_connection_handle_idle (pos);
    }
  /* Connections with the default timeout are sorted by prepending
     them to the head of the list whenever we touch the connection;
     thus it suffices to iterate from the tail until the first
     connection is NOT timed out */
  prev = daemon->normal_timeout_tail;
  while (NULL != (pos = prev))
    {
      prev = pos->prevX;
      MHD_connection_handle_idle (pos);
      if (MHD_REQUEST_CLOSED != pos->request.state)
	break; /* sorted by timeout, no need to visit the rest! */
    }
  return MHD_SC_OK;
}
#endif

/* end of daemon_epoll.c */