GNU libmicrohttpd  0.9.65
daemon_close_all_connections.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 */
19 
25 #include "internal.h"
26 #include "connection_cleanup.h"
27 #include "connection_close.h"
30 #include "request_resume.h"
31 #include "upgrade_process.h"
32 
33 
42 static void
44 {
45  struct MHD_Daemon *daemon = pos->daemon;
46 
47  if (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_mode)
48  {
50  return; /* must let thread to do the rest */
51  }
54 
56 
57  mhd_assert (! pos->suspended);
58  mhd_assert (! pos->resuming);
59  if (pos->connection_timeout ==
62  daemon->normal_timeout_tail,
63  pos);
64  else
66  daemon->manual_timeout_tail,
67  pos);
69  daemon->connections_tail,
70  pos);
71  DLL_insert (daemon->cleanup_head,
72  daemon->cleanup_tail,
73  pos);
74 
76 }
77 
78 
88 void
90 {
91  struct MHD_Connection *pos;
92  const bool used_thr_p_c = (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_mode);
93 #ifdef UPGRADE_SUPPORT
94  const bool upg_allowed = (! daemon->disallow_upgrade);
95 #endif /* UPGRADE_SUPPORT */
96 #if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
97  struct MHD_UpgradeResponseHandle *urh;
98  struct MHD_UpgradeResponseHandle *urhn;
99  const bool used_tls = (NULL != daemon->tls_api);
100 
101  mhd_assert (NULL == daemon->worker_pool);
102  mhd_assert (daemon->shutdown);
103  /* give upgraded HTTPS connections a chance to finish */
104  /* 'daemon->urh_head' is not used in thread-per-connection mode. */
105  for (urh = daemon->urh_tail; NULL != urh; urh = urhn)
106  {
107  urhn = urh->prev;
108  /* call generic forwarding function for passing data
109  with chance to detect that application is done. */
110  MHD_upgrade_response_handle_process_ (urh);
111  MHD_connection_finish_forward_ (urh->connection);
112  urh->clean_ready = true;
113  /* Resuming will move connection to cleanup list. */
114  MHD_request_resume (&urh->connection->request);
115  }
116 #endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
117 
118  /* Give suspended connections a chance to resume to avoid
119  running into the check for there not being any suspended
120  connections left in case of a tight race with a recently
121  resumed connection. */
122  if (! daemon->disallow_suspend_resume)
123  {
124  daemon->resuming = true; /* Force check for pending resume. */
126  }
127  /* first, make sure all threads are aware of shutdown; need to
128  traverse DLLs in peace... */
130 #ifdef UPGRADE_SUPPORT
131  if (upg_allowed)
132  {
133  struct MHD_Connection * susp;
134 
136  while (NULL != susp)
137  {
138  if (NULL == susp->request.urh) /* "Upgraded" connection? */
139  MHD_PANIC (_("MHD_stop_daemon() called while we have suspended connections.\n"));
140 #ifdef HTTPS_SUPPORT
141  else if (used_tls &&
142  used_thr_p_c &&
143  (! susp->request.urh->clean_ready) )
144  shutdown (susp->request.urh->app.socket,
145  SHUT_RDWR); /* Wake thread by shutdown of app socket. */
146 #endif /* HTTPS_SUPPORT */
147  else
148  {
149 #ifdef HAVE_MESSAGES
150  if (! susp->request.urh->was_closed)
151  MHD_DLOG (daemon,
152  MHD_SC_SHUTDOWN_WITH_OPEN_UPGRADED_CONNECTION,
153  _("Initiated daemon shutdown while \"upgraded\" connection was not closed.\n"));
154 #endif
155  susp->request.urh->was_closed = true;
156  /* If thread-per-connection is used, connection's thread
157  * may still processing "upgrade" (exiting). */
158  if (! used_thr_p_c)
160  /* Do not use MHD_resume_connection() as mutex is
161  * already locked. */
162  susp->resuming = true;
163  daemon->resuming = true;
164  }
165  susp = susp->prev;
166  }
167  }
168  else /* This 'else' is combined with next 'if' */
169 #endif /* UPGRADE_SUPPORT */
171  MHD_PANIC (_("MHD_stop_daemon() called while we have suspended connections.\n"));
172  for (pos = daemon->connections_tail; NULL != pos; pos = pos->prev)
173  {
174  shutdown (pos->socket_fd,
175  SHUT_RDWR);
176 #if MHD_WINSOCK_SOCKETS
177  if ( (used_thr_p_c) &&
178  (MHD_ITC_IS_VALID_(daemon->itc)) &&
179  (! MHD_itc_activate_ (daemon->itc,
180  "e")) )
181  MHD_PANIC (_("Failed to signal shutdown via inter-thread communication channel"));
182 #endif
183  }
184 
185  /* now, collect per-connection threads */
186  if (used_thr_p_c)
187  {
188  pos = daemon->connections_tail;
189  while (NULL != pos)
190  {
191  if (! pos->thread_joined)
192  {
194  if (! MHD_join_thread_ (pos->pid.handle))
195  MHD_PANIC (_("Failed to join a thread\n"));
197  pos->thread_joined = true;
198  /* The thread may have concurrently modified the DLL,
199  need to restart from the beginning */
200  pos = daemon->connections_tail;
201  continue;
202  }
203  pos = pos->prev;
204  }
205  }
207 
208 #ifdef UPGRADE_SUPPORT
209  /* Finished threads with "upgraded" connections need to be moved
210  * to cleanup list by resume_suspended_connections(). */
211  /* "Upgraded" connections that were not closed explicitly by
212  * application should be moved to cleanup list too. */
213  if (upg_allowed)
214  {
215  daemon->resuming = true; /* Force check for pending resume. */
217  }
218 #endif /* UPGRADE_SUPPORT */
219 
220  /* now that we're alone, move everyone to cleanup */
221  while (NULL != (pos = daemon->connections_tail))
222  {
223  if ( (used_thr_p_c) &&
224  (! pos->thread_joined) )
225  MHD_PANIC (_("Failed to join a thread\n"));
226  close_connection (pos);
227  }
229 }
230 
231 /* end of daemon_close_all_connections.c */
bool thread_joined
Definition: internal.h:776
struct MHD_Request request
Definition: internal.h:714
#define MHD_PANIC(msg)
Definition: internal.h:68
struct MHD_Connection * cleanup_head
Definition: internal.h:1174
#define MHD_mutex_unlock_chk_(pmutex)
Definition: mhd_locks.h:177
struct MHD_Connection * manual_timeout_head
Definition: internal.h:1140
MHD_thread_handle_ID_ pid
Definition: internal.h:720
internal shared structures
struct MHD_Daemon * daemon
Definition: internal.h:672
time_t connection_default_timeout
Definition: internal.h:1368
#define DLL_insert(head, tail, element)
Definition: internal.h:1742
struct MHD_Connection * connections_tail
Definition: internal.h:1157
struct MHD_Daemon * worker_pool
Definition: internal.h:1070
functions to close connection
MHD_socket socket_fd
Definition: internal.h:749
static void close_connection(struct MHD_Connection *pos)
struct MHD_Connection * connections_head
Definition: internal.h:1152
function to close all connections open at a daemon
void MHD_connection_mark_closed_(struct MHD_Connection *connection)
void MHD_connection_close_(struct MHD_Connection *connection, enum MHD_RequestTerminationCode rtc)
complete upgrade socket forwarding operation in TLS mode
time_t connection_timeout
Definition: internal.h:742
struct MHD_Connection * manual_timeout_tail
Definition: internal.h:1147
functions to cleanup completed connection
#define NULL
Definition: reason_phrase.c:30
bool disallow_upgrade
Definition: internal.h:1471
struct MHD_Connection * prev
Definition: internal.h:653
#define DLL_remove(head, tail, element)
Definition: internal.h:1762
struct MHD_Connection * normal_timeout_head
Definition: internal.h:1125
void MHD_request_resume(struct MHD_Request *request)
void MHD_daemon_close_all_connections_(struct MHD_Daemon *daemon)
void MHD_connection_finish_forward_(struct MHD_Connection *connection) MHD_NONNULL(1)
#define XDLL_remove(head, tail, element)
Definition: internal.h:1806
struct MHD_Connection * suspended_connections_tail
Definition: internal.h:1169
struct MHD_itc_ itc
Definition: internal.h:1407
#define mhd_assert(CHK)
Definition: mhd_assert.h:39
struct MHD_Connection * cleanup_tail
Definition: internal.h:1179
struct MHD_Connection * normal_timeout_tail
Definition: internal.h:1132
#define MHD_mutex_lock_chk_(pmutex)
Definition: mhd_locks.h:151
bool resuming
Definition: internal.h:1507
void MHD_connection_cleanup_(struct MHD_Daemon *daemon)
#define _(String)
Definition: mhd_options.h:42
bool suspended
Definition: internal.h:761
bool disallow_suspend_resume
Definition: internal.h:1465
struct MHD_Connection * suspended_connections_head
Definition: internal.h:1163
bool MHD_resume_suspended_connections_(struct MHD_Daemon *daemon)
enum MHD_ThreadingMode threading_mode
Definition: internal.h:1414
volatile bool shutdown
Definition: internal.h:1523
MHD_mutex_ cleanup_connection_mutex
Definition: internal.h:1262
function to process upgrade activity (over TLS)
implementation of MHD_request_resume()