GNU libmicrohttpd  0.9.62
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
connection_cleanup.c
Go to the documentation of this file.
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 */
24 #include "internal.h"
25 #include "connection_cleanup.h"
26 #include "daemon_ip_limit.h"
27 
28 
29 #ifdef UPGRADE_SUPPORT
30 
37 static void
38 connection_cleanup_upgraded (struct MHD_Connection *connection)
39 {
40  struct MHD_UpgradeResponseHandle *urh = connection->request.urh;
41 
42  if (NULL == urh)
43  return;
44 #ifdef HTTPS_SUPPORT
45  /* Signal remote client the end of TLS connection by
46  * gracefully closing TLS session. */
47  {
48  struct MHD_TLS_Plugin *tls;
49 
50  if (NULL != (tls = connection->daemon->tls_api))
51  (void) tls->shutdown_connection (tls->cls,
52  connection->tls_cs);
53  }
54  if (MHD_INVALID_SOCKET != urh->mhd.socket)
55  MHD_socket_close_chk_ (urh->mhd.socket);
56  if (MHD_INVALID_SOCKET != urh->app.socket)
57  MHD_socket_close_chk_ (urh->app.socket);
58 #endif /* HTTPS_SUPPORT */
59  connection->request.urh = NULL;
60  free (urh);
61 }
62 #endif /* UPGRADE_SUPPORT */
63 
64 
75 void
77 {
78  struct MHD_Connection *pos;
79 
81  while (NULL != (pos = daemon->cleanup_tail))
82  {
83  DLL_remove (daemon->cleanup_head,
84  daemon->cleanup_tail,
85  pos);
87 
88  if ( (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_mode) &&
89  (! pos->thread_joined) &&
90  (! MHD_join_thread_ (pos->pid.handle)) )
91  MHD_PANIC (_("Failed to join a thread\n"));
92 #ifdef UPGRADE_SUPPORT
93  connection_cleanup_upgraded (pos);
94 #endif /* UPGRADE_SUPPORT */
95  MHD_pool_destroy (pos->pool);
96 #ifdef HTTPS_SUPPORT
97  {
98  struct MHD_TLS_Plugin *tls;
99 
100  if (NULL != (tls = daemon->tls_api))
101  tls->teardown_connection (tls->cls,
102  pos->tls_cs);
103  }
104 #endif /* HTTPS_SUPPORT */
105 
106  /* clean up the connection */
107  if (NULL != daemon->notify_connection_cb)
109  pos,
111  MHD_ip_limit_del (daemon,
112  (const struct sockaddr *) &pos->addr,
113  pos->addr_len);
114 #ifdef EPOLL_SUPPORT
115  if (MHD_ELS_EPOLL == daemon->event_loop_syscall)
116  {
117  if (0 != (pos->epoll_state & MHD_EPOLL_STATE_IN_EREADY_EDLL))
118  {
119  EDLL_remove (daemon->eready_head,
120  daemon->eready_tail,
121  pos);
122  pos->epoll_state &= ~MHD_EPOLL_STATE_IN_EREADY_EDLL;
123  }
124  if ( (-1 != daemon->epoll_fd) &&
125  (0 != (pos->epoll_state & MHD_EPOLL_STATE_IN_EPOLL_SET)) )
126  {
127  /* epoll documentation suggests that closing a FD
128  automatically removes it from the epoll set; however,
129  this is not true as if we fail to do manually remove it,
130  we are still seeing an event for this fd in epoll,
131  causing grief (use-after-free...) --- at least on my
132  system. */
133  if (0 != epoll_ctl (daemon->epoll_fd,
134  EPOLL_CTL_DEL,
135  pos->socket_fd,
136  NULL))
137  MHD_PANIC (_("Failed to remove FD from epoll set\n"));
138  pos->epoll_state &= ~MHD_EPOLL_STATE_IN_EPOLL_SET;
139  }
140  }
141 #endif
142  if (NULL != pos->request.response)
143  {
145  pos->request.response = NULL;
146  }
147  if (MHD_INVALID_SOCKET != pos->socket_fd)
149  free (pos);
150 
152  daemon->connections--;
153  daemon->at_limit = false;
154  }
156 }
157 
158 /* end of connection_cleanup.c */
bool thread_joined
Definition: internal.h:776
struct MHD_Request request
Definition: internal.h:714
enum MHD_EventLoopSyscall event_loop_syscall
Definition: internal.h:1433
#define MHD_PANIC(msg)
Definition: internal.h:68
void MHD_ip_limit_del(struct MHD_Daemon *daemon, const struct sockaddr *addr, socklen_t addrlen)
DLL_remove(daemon->suspended_connections_head, daemon->suspended_connections_tail, pos)
socklen_t addr_len
Definition: internal.h:730
struct sockaddr_storage addr
Definition: internal.h:725
struct MHD_Connection * cleanup_head
Definition: internal.h:1174
MHD_mutex_lock_chk_ & daemon
#define MHD_mutex_unlock_chk_(pmutex)
Definition: mhd_locks.h:177
MHD_thread_handle_ID_ pid
Definition: internal.h:720
internal shared structures
struct MHD_Daemon * daemon
Definition: internal.h:672
MHD_NotifyConnectionCallback notify_connection_cb
Definition: internal.h:1044
#define MHD_INVALID_SOCKET
Definition: microhttpd.h:182
MHD_socket socket_fd
Definition: internal.h:749
#define MHD_socket_close_chk_(fd)
Definition: mhd_sockets.h:246
#define EDLL_remove(head, tail, element)
Definition: internal.h:1847
functions to cleanup completed connection
#define NULL
Definition: reason_phrase.c:30
unsigned int connections
Definition: internal.h:1358
void MHD_pool_destroy(struct MemoryPool *pool)
Definition: memorypool.c:157
void(* teardown_connection)(void *cls, struct MHD_TLS_ConnectionState *cs)
struct MHD_Connection * cleanup_tail
Definition: internal.h:1179
struct MHD_Response * response
Definition: internal.h:380
#define MHD_mutex_lock_chk_(pmutex)
Definition: mhd_locks.h:151
struct MemoryPool * pool
Definition: internal.h:682
void MHD_connection_cleanup_(struct MHD_Daemon *daemon)
bool at_limit
Definition: internal.h:1480
enum MHD_Bool(* shutdown_connection)(void *cls, struct MHD_TLS_ConnectionState *cs)
#define _(String)
Definition: mhd_options.h:42
counting of connections per IP
void * notify_connection_cb_cls
Definition: internal.h:1049
enum MHD_ThreadingMode threading_mode
Definition: internal.h:1414
MHD_mutex_ cleanup_connection_mutex
Definition: internal.h:1262
void MHD_response_queue_for_destroy(struct MHD_Response *response)
Definition: response.c:88