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
|
/*
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_destroy.c
* @brief main functions to destroy a daemon
* @author Christian Grothoff
*/
#include "internal.h"
#include "request_resume.h"
#include "daemon_close_all_connections.h"
/**
* Stop all worker threads from the worker pool.
*
* @param daemon master daemon controling the workers
*/
static void
stop_workers (struct MHD_Daemon *daemon)
{
MHD_socket fd;
unsigned int i;
mhd_assert (1 < daemon->worker_pool_size);
mhd_assert (1 < daemon->threading_mode);
if (daemon->was_quiesced)
fd = MHD_INVALID_SOCKET; /* Do not use FD if daemon was quiesced */
else
fd = daemon->listen_socket;
/* Let workers shutdown in parallel. */
for (i = 0; i < daemon->worker_pool_size; i++)
{
daemon->worker_pool[i].shutdown = true;
if (MHD_ITC_IS_VALID_ (daemon->worker_pool[i].itc))
{
if (! MHD_itc_activate_ (daemon->worker_pool[i].itc,
"e"))
MHD_PANIC (_ (
"Failed to signal shutdown via inter-thread communication channel."));
}
else
{
/* Better hope shutdown() works... */
mhd_assert (MHD_INVALID_SOCKET != fd);
}
}
#ifdef HAVE_LISTEN_SHUTDOWN
if (MHD_INVALID_SOCKET != fd)
{
(void) shutdown (fd,
SHUT_RDWR);
}
#endif /* HAVE_LISTEN_SHUTDOWN */
for (i = 0; i < daemon->worker_pool_size; ++i)
{
MHD_daemon_destroy (&daemon->worker_pool[i]);
}
free (daemon->worker_pool);
daemon->worker_pool = NULL;
/* FIXME: does this still hold? */
mhd_assert (MHD_ITC_IS_INVALID_ (daemon->itc));
#ifdef EPOLL_SUPPORT
mhd_assert (-1 == daemon->epoll_fd);
#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
mhd_assert (-1 == daemon->epoll_upgrade_fd);
#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
#endif /* EPOLL_SUPPORT */
}
/**
* Shutdown and destroy an HTTP daemon.
*
* @param daemon daemon to stop
* @ingroup event
*/
void
MHD_daemon_destroy (struct MHD_Daemon *daemon)
{
MHD_socket fd;
daemon->shutdown = true;
if (daemon->was_quiesced)
fd = MHD_INVALID_SOCKET; /* Do not use FD if daemon was quiesced */
else
fd = daemon->listen_socket;
/* FIXME: convert from here to microhttpd2-style API! */
if (NULL != daemon->worker_pool)
{ /* Master daemon with worker pool. */
stop_workers (daemon);
}
else
{
mhd_assert (0 == daemon->worker_pool_size);
/* Worker daemon or single-thread daemon. */
if (MHD_TM_EXTERNAL_EVENT_LOOP != daemon->threading_mode)
{
/* Worker daemon or single daemon with internal thread(s). */
/* Separate thread(s) is used for polling sockets. */
if (MHD_ITC_IS_VALID_ (daemon->itc))
{
if (! MHD_itc_activate_ (daemon->itc,
"e"))
MHD_PANIC (_ (
"Failed to signal shutdown via inter-thread communication channel"));
}
else
{
#ifdef HAVE_LISTEN_SHUTDOWN
if (MHD_INVALID_SOCKET != fd)
{
if (NULL == daemon->master)
(void) shutdown (fd,
SHUT_RDWR);
}
else
#endif /* HAVE_LISTEN_SHUTDOWN */
mhd_assert (false); /* Should never happen */
}
if (! MHD_join_thread_ (daemon->pid.handle))
{
MHD_PANIC (_ ("Failed to join a thread\n"));
}
/* close_all_connections() was called in daemon thread. */
}
else
{
/* No internal threads are used for polling sockets
(external event loop) */
MHD_daemon_close_all_connections_ (daemon);
}
if (MHD_ITC_IS_VALID_ (daemon->itc))
MHD_itc_destroy_chk_ (daemon->itc);
#ifdef EPOLL_SUPPORT
if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
(-1 != daemon->epoll_fd) )
MHD_socket_close_chk_ (daemon->epoll_fd);
#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
(-1 != daemon->epoll_upgrade_fd) )
MHD_socket_close_chk_ (daemon->epoll_upgrade_fd);
#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT */
#endif /* EPOLL_SUPPORT */
MHD_mutex_destroy_chk_ (&daemon->cleanup_connection_mutex);
}
if (NULL != daemon->master)
return;
/* Cleanup that should be done only one time in master/single daemon.
* Do not perform this cleanup in worker daemons. */
if (MHD_INVALID_SOCKET != fd)
MHD_socket_close_chk_ (fd);
/* TLS clean up */
#ifdef HTTPS_SUPPORT
if (NULL != daemon->tls_api)
{
#if FIXME_TLS_API
if (daemon->have_dhparams)
{
gnutls_dh_params_deinit (daemon->https_mem_dhparams);
daemon->have_dhparams = false;
}
gnutls_priority_deinit (daemon->priority_cache);
if (daemon->x509_cred)
gnutls_certificate_free_credentials (daemon->x509_cred);
#endif
}
#endif /* HTTPS_SUPPORT */
#ifdef DAUTH_SUPPORT
free (daemon->nnc);
MHD_mutex_destroy_chk_ (&daemon->nnc_lock);
#endif
MHD_mutex_destroy_chk_ (&daemon->per_ip_connection_mutex);
free (daemon);
}
/* end of daemon_destroy.c */
|