libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 4acefd453fe913af72310ec8f7203493e2b485ec
parent 4c25471bfdcc8f7568ad58f1fa30da3e9e8e77b5
Author: Christian Grothoff <christian@grothoff.org>
Date:   Thu, 22 Sep 2016 09:25:18 +0000

simplify error handling by baking it into the macros

Diffstat:
MChangeLog | 5+++++
Msrc/include/mhd_options.h | 13+++++++++++--
Msrc/microhttpd/connection.c | 40+++++++++++++++++++---------------------
Msrc/microhttpd/daemon.c | 231++++++++++++++++++++++++++++++++++---------------------------------------------
Msrc/microhttpd/digestauth.c | 10+++++-----
Msrc/microhttpd/internal.h | 9+--------
Msrc/microhttpd/mhd_locks.h | 47+++++++++++++++++------------------------------
Msrc/microhttpd/mhd_sem.c | 14+++++++-------
Msrc/microhttpd/mhd_sockets.h | 13+++++++++----
Msrc/microhttpd/response.c | 35++++++++++++++---------------------
Msrc/microhttpd/test_shutdown_select.c | 9+++++++++
Msrc/microhttpd/test_upgrade_common.c | 9+++++++++
Msrc/testcurl/https/test_https_time_out.c | 10++++++++++
Msrc/testcurl/test_callback.c | 11++++++++---
Msrc/testcurl/test_get.c | 8++++++++
Msrc/testcurl/test_quiesce.c | 8++++++++
16 files changed, 238 insertions(+), 234 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,3 +1,8 @@ +Thu Sep 22 11:03:43 CEST 2016 + Simplify internal error handling logic by folding it into the + MHD_socket_close_, MHD_mutex_lock_, MHD_mutex_unlock_ and + MHD_mutex_destroy_ functions. -CG + Tue Sep 13 22:20:26 MSK 2016 Added autoconf macro to enable maximum platform features. Fixed compiling on Solaris. -EG diff --git a/src/include/mhd_options.h b/src/include/mhd_options.h @@ -33,6 +33,15 @@ #include "MHD_config.h" +/** + * Macro to make it easy to mark text for translation. Note that + * we do not actually call gettext() in MHD, but we do make it + * easy to create a ".po" file so that applications that do want + * to translate error messages can do so. + */ +#define _(String) (String) + + #ifndef _MHD_EXTERN #if defined(BUILDING_MHD_LIB) && defined(_WIN32) && \ @@ -92,7 +101,7 @@ #endif /* HAVE_C11_GMTIME_S */ #if defined(MHD_FAVOR_FAST_CODE) && defined(MHD_FAVOR_SMALL_CODE) -#error MHD_FAVOR_FAST_CODE and MHD_FAVOR_SMALL_CODE are both defined. Cannot favor speed and size at the same time. +#error MHD_FAVOR_FAST_CODE and MHD_FAVOR_SMALL_CODE are both defined. Cannot favor speed and size at the same time. #endif /* MHD_FAVOR_FAST_CODE && MHD_FAVOR_SMALL_CODE */ /* Define MHD_FAVOR_FAST_CODE to force fast code path or @@ -100,7 +109,7 @@ #if !defined(MHD_FAVOR_FAST_CODE) && !defined(MHD_FAVOR_SMALL_CODE) /* Try to detect user preferences */ /* Defined by GCC and many compatible compilers */ -#ifdef __OPTIMIZE_SIZE__ +#ifdef __OPTIMIZE_SIZE__ #define MHD_FAVOR_SMALL_CODE 1 #elif __OPTIMIZE__ #define MHD_FAVOR_FAST_CODE 1 diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -614,7 +614,7 @@ try_ready_normal_body (struct MHD_Connection *connection) { /* either error or http 1.0 transfer, close socket! */ response->total_size = connection->response_write_position; - (void) MHD_mutex_unlock_ (&response->mutex); + MHD_mutex_unlock_ (&response->mutex); if ( ((ssize_t)MHD_CONTENT_READER_END_OF_STREAM) == ret) MHD_connection_close_ (connection, MHD_REQUEST_TERMINATED_COMPLETED_OK); @@ -628,7 +628,7 @@ try_ready_normal_body (struct MHD_Connection *connection) if (0 == ret) { connection->state = MHD_CONNECTION_NORMAL_BODY_UNREADY; - (void) MHD_mutex_unlock_ (&response->mutex); + MHD_mutex_unlock_ (&response->mutex); return MHD_NO; } return MHD_YES; @@ -2457,13 +2457,13 @@ MHD_connection_handle_write (struct MHD_Connection *connection) uint64_t data_write_offset; if (NULL != response->crc) - (void) MHD_mutex_lock_ (&response->mutex); + MHD_mutex_lock_ (&response->mutex); if (MHD_YES != try_ready_normal_body (connection)) - { - if (NULL != response->crc) - (void) MHD_mutex_unlock_ (&response->mutex); - break; - } + { + if (NULL != response->crc) + MHD_mutex_unlock_ (&response->mutex); + break; + } data_write_offset = connection->response_write_position - response->data_start; if (data_write_offset > (uint64_t)SIZE_MAX) @@ -2484,7 +2484,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection) response->data_start]); #endif if (NULL != response->crc) - (void) MHD_mutex_unlock_ (&response->mutex); + MHD_mutex_unlock_ (&response->mutex); if (ret < 0) { if (MHD_SCKT_ERR_IS_EINTR_ (err) || @@ -2570,8 +2570,7 @@ cleanup_connection (struct MHD_Connection *connection) } if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - if (! MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) - MHD_PANIC (_("Failed to acquire cleanup mutex\n")); + MHD_mutex_lock_ (&daemon->cleanup_connection_mutex); } else { @@ -2598,9 +2597,8 @@ cleanup_connection (struct MHD_Connection *connection) connection->suspended = MHD_NO; connection->resuming = MHD_NO; connection->in_idle = MHD_NO; - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_unlock_(&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to release cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_unlock_(&daemon->cleanup_connection_mutex); } @@ -2909,18 +2907,18 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) break; case MHD_CONNECTION_NORMAL_BODY_UNREADY: if (NULL != connection->response->crc) - (void) MHD_mutex_lock_ (&connection->response->mutex); + MHD_mutex_lock_ (&connection->response->mutex); if (0 == connection->response->total_size) { if (NULL != connection->response->crc) - (void) MHD_mutex_unlock_ (&connection->response->mutex); + MHD_mutex_unlock_ (&connection->response->mutex); connection->state = MHD_CONNECTION_BODY_SENT; continue; } if (MHD_YES == try_ready_normal_body (connection)) { if (NULL != connection->response->crc) - (void) MHD_mutex_unlock_ (&connection->response->mutex); + MHD_mutex_unlock_ (&connection->response->mutex); connection->state = MHD_CONNECTION_NORMAL_BODY_READY; /* Buffering for flushable socket was already enabled*/ if (MHD_NO == socket_flush_possible (connection)) @@ -2934,20 +2932,20 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) break; case MHD_CONNECTION_CHUNKED_BODY_UNREADY: if (NULL != connection->response->crc) - (void) MHD_mutex_lock_ (&connection->response->mutex); + MHD_mutex_lock_ (&connection->response->mutex); if ( (0 == connection->response->total_size) || (connection->response_write_position == connection->response->total_size) ) { if (NULL != connection->response->crc) - (void) MHD_mutex_unlock_ (&connection->response->mutex); + MHD_mutex_unlock_ (&connection->response->mutex); connection->state = MHD_CONNECTION_BODY_SENT; continue; } if (MHD_YES == try_ready_chunked_body (connection)) { if (NULL != connection->response->crc) - (void) MHD_mutex_unlock_ (&connection->response->mutex); + MHD_mutex_unlock_ (&connection->response->mutex); connection->state = MHD_CONNECTION_CHUNKED_BODY_READY; /* Buffering for flushable socket was already enabled */ if (MHD_NO == socket_flush_possible (connection)) @@ -2956,7 +2954,7 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) continue; } if (NULL != connection->response->crc) - (void) MHD_mutex_unlock_ (&connection->response->mutex); + MHD_mutex_unlock_ (&connection->response->mutex); break; case MHD_CONNECTION_BODY_SENT: if (MHD_NO == build_header_response (connection)) diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -195,10 +195,7 @@ struct MHD_IPCount static void MHD_ip_count_lock (struct MHD_Daemon *daemon) { - if (! MHD_mutex_lock_(&daemon->per_ip_connection_mutex)) - { - MHD_PANIC (_("Failed to acquire IP connection limit mutex\n")); - } + MHD_mutex_lock_(&daemon->per_ip_connection_mutex); } @@ -210,10 +207,7 @@ MHD_ip_count_lock (struct MHD_Daemon *daemon) static void MHD_ip_count_unlock (struct MHD_Daemon *daemon) { - if (! MHD_mutex_unlock_(&daemon->per_ip_connection_mutex)) - { - MHD_PANIC (_("Failed to release IP connection limit mutex\n")); - } + MHD_mutex_unlock_(&daemon->per_ip_connection_mutex); } @@ -965,8 +959,7 @@ finish_upgrade_close (struct MHD_UpgradeResponseHandle *urh) NULL)) ) MHD_PANIC (_("Failed to remove FD from epoll set\n")); #endif - if (0 != MHD_socket_close_ (urh->mhd.socket)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (urh->mhd.socket); } MHD_resume_connection (connection); MHD_connection_close_ (connection, @@ -1536,8 +1529,7 @@ exit: { shutdown (con->socket_fd, SHUT_WR); - if (0 != MHD_socket_close_ (con->socket_fd)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (con->socket_fd); con->socket_fd = MHD_INVALID_SOCKET; } return (MHD_THRD_RTRN_TYPE_) 0; @@ -1776,8 +1768,7 @@ internal_add_connection (struct MHD_Daemon *daemon, external_add); } /* all pools are at their connection limit, must refuse */ - if (0 != MHD_socket_close_ (client_socket)) - MHD_PANIC ("close failed\n"); + MHD_socket_close_ (client_socket); #if ENFILE errno = ENFILE; #endif @@ -1794,8 +1785,7 @@ internal_add_connection (struct MHD_Daemon *daemon, (int) client_socket, (int) FD_SETSIZE); #endif - if (0 != MHD_socket_close_ (client_socket)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (client_socket); #if EINVAL errno = EINVAL; #endif @@ -1820,8 +1810,7 @@ internal_add_connection (struct MHD_Daemon *daemon, MHD_DLOG (daemon, _("Server reached connection limit. Closing inbound connection.\n")); #endif - if (0 != MHD_socket_close_ (client_socket)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (client_socket); #if ENFILE errno = ENFILE; #endif @@ -1840,8 +1829,7 @@ internal_add_connection (struct MHD_Daemon *daemon, _("Connection rejected by application. Closing connection.\n")); #endif #endif - if (0 != MHD_socket_close_ (client_socket)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (client_socket); MHD_ip_limit_del (daemon, addr, addrlen); @@ -1871,8 +1859,7 @@ internal_add_connection (struct MHD_Daemon *daemon, "Error allocating memory: %s\n", MHD_strerror_ (errno)); #endif - if (0 != MHD_socket_close_ (client_socket)) - MHD_PANIC ("close failed\n"); + MHD_socket_close_ (client_socket); MHD_ip_limit_del (daemon, addr, addrlen); @@ -1890,8 +1877,7 @@ internal_add_connection (struct MHD_Daemon *daemon, _("Error allocating memory: %s\n"), MHD_strerror_ (errno)); #endif - if (0 != MHD_socket_close_ (client_socket)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (client_socket); MHD_ip_limit_del (daemon, addr, addrlen); @@ -1911,8 +1897,7 @@ internal_add_connection (struct MHD_Daemon *daemon, _("Error allocating memory: %s\n"), MHD_strerror_ (errno)); #endif - if (0 != MHD_socket_close_ (client_socket)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (client_socket); MHD_ip_limit_del (daemon, addr, addrlen); @@ -1973,8 +1958,7 @@ internal_add_connection (struct MHD_Daemon *daemon, _("Failed to setup TLS credentials: unknown credential type %d\n"), daemon->cred_type); #endif - if (0 != MHD_socket_close_ (client_socket)) - MHD_PANIC ("close failed\n"); + MHD_socket_close_ (client_socket); MHD_ip_limit_del (daemon, addr, addrlen); @@ -2001,19 +1985,19 @@ internal_add_connection (struct MHD_Daemon *daemon, if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - if (! MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) - MHD_PANIC (_("Failed to acquire cleanup mutex\n")); + MHD_mutex_lock_ (&daemon->cleanup_connection_mutex); } else + { XDLL_insert (daemon->normal_timeout_head, daemon->normal_timeout_tail, connection); + } DLL_insert (daemon->connections_head, daemon->connections_tail, connection); - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to release cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex); if (NULL != daemon->notify_connection) daemon->notify_connection (daemon->notify_connection_cls, @@ -2093,24 +2077,25 @@ internal_add_connection (struct MHD_Daemon *daemon, connection, &connection->socket_context, MHD_CONNECTION_NOTIFY_CLOSED); - if (0 != MHD_socket_close_ (client_socket)) - MHD_PANIC (_("close failed\n")); - MHD_ip_limit_del (daemon, addr, addrlen); + MHD_socket_close_ (client_socket); + MHD_ip_limit_del (daemon, + addr, + addrlen); if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) - { - if (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) - MHD_PANIC (_("Failed to acquire cleanup mutex\n")); - } + { + MHD_mutex_lock_ (&daemon->cleanup_connection_mutex); + } else - XDLL_remove (daemon->normal_timeout_head, - daemon->normal_timeout_tail, - connection); + { + XDLL_remove (daemon->normal_timeout_head, + daemon->normal_timeout_tail, + connection); + } DLL_remove (daemon->connections_head, daemon->connections_tail, connection); - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to release cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex); MHD_pool_destroy (connection->pool); free (connection->addr); free (connection); @@ -2156,8 +2141,7 @@ MHD_suspend_connection (struct MHD_Connection *connection) MHD_PANIC (_("Cannot suspend connections without enabling MHD_USE_SUSPEND_RESUME!\n")); if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - if (! MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) - MHD_PANIC (_("Failed to acquire cleanup mutex\n")); + MHD_mutex_lock_ (&daemon->cleanup_connection_mutex); } else { @@ -2199,9 +2183,8 @@ MHD_suspend_connection (struct MHD_Connection *connection) } #endif connection->suspended = MHD_YES; - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to release cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex); } @@ -2221,9 +2204,8 @@ MHD_resume_connection (struct MHD_Connection *connection) daemon = connection->daemon; if (MHD_USE_SUSPEND_RESUME != (daemon->options & MHD_USE_SUSPEND_RESUME)) MHD_PANIC (_("Cannot resume connections without enabling MHD_USE_SUSPEND_RESUME!\n")); - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to acquire cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_lock_ (&daemon->cleanup_connection_mutex); connection->resuming = MHD_YES; daemon->resuming = MHD_YES; if ( (MHD_INVALID_PIPE_ != daemon->wpipe[1]) && @@ -2234,9 +2216,8 @@ MHD_resume_connection (struct MHD_Connection *connection) _("Failed to signal resume via pipe.")); #endif } - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to release cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex); } @@ -2255,9 +2236,8 @@ resume_suspended_connections (struct MHD_Daemon *daemon) int ret; ret = MHD_NO; - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to acquire cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_lock_ (&daemon->cleanup_connection_mutex); if (MHD_NO != daemon->resuming) next = daemon->suspended_connections_head; @@ -2311,9 +2291,8 @@ resume_suspended_connections (struct MHD_Daemon *daemon) pos->suspended = MHD_NO; pos->resuming = MHD_NO; } - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to release cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex); return ret; } @@ -2442,9 +2421,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon) #endif if (MHD_INVALID_SOCKET != s) { - if (0 != MHD_socket_close_ (s)) - MHD_PANIC (_("close failed\n")); - /* just in case */ + MHD_socket_close_ (s); } if ( MHD_SCKT_ERR_IS_LOW_RESOURCES_ (err) ) { @@ -2518,9 +2495,8 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon) { struct MHD_Connection *pos; - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to acquire cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_lock_ (&daemon->cleanup_connection_mutex); while (NULL != (pos = daemon->cleanup_head)) { DLL_remove (daemon->cleanup_head, @@ -2586,16 +2562,14 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon) } if (MHD_INVALID_SOCKET != pos->socket_fd) { - if (0 != MHD_socket_close_ (pos->socket_fd)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (pos->socket_fd); } if (NULL != pos->addr) free (pos->addr); free (pos); } - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to release cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex); } @@ -4829,8 +4803,7 @@ MHD_start_daemon_va (unsigned int flags, (unsigned int) port, MHD_socket_last_strerr_ ()); #endif - if (0 != MHD_socket_close_ (socket_fd)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (socket_fd); goto free_and_fail; } #ifdef TCP_FASTOPEN @@ -4860,8 +4833,7 @@ MHD_start_daemon_va (unsigned int flags, _("Failed to listen for connections: %s\n"), MHD_socket_last_strerr_ ()); #endif - if (0 != MHD_socket_close_ (socket_fd)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (socket_fd); goto free_and_fail; } } @@ -4883,8 +4855,7 @@ MHD_start_daemon_va (unsigned int flags, /* 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. */ - if (0 != MHD_socket_close_ (socket_fd)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (socket_fd); goto free_and_fail; } } @@ -4898,8 +4869,7 @@ MHD_start_daemon_va (unsigned int flags, socket_fd, FD_SETSIZE); #endif - if (0 != MHD_socket_close_ (socket_fd)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (socket_fd); goto free_and_fail; } @@ -4936,9 +4906,8 @@ MHD_start_daemon_va (unsigned int flags, MHD_DLOG (daemon, _("MHD failed to initialize IP connection limit mutex\n")); #endif - if ( (MHD_INVALID_SOCKET != socket_fd) && - (0 != MHD_socket_close_ (socket_fd)) ) - MHD_PANIC (_("close failed\n")); + if (MHD_INVALID_SOCKET != socket_fd) + MHD_socket_close_ (socket_fd); goto free_and_fail; } if (! MHD_mutex_init_ (&daemon->cleanup_connection_mutex)) @@ -4947,10 +4916,9 @@ MHD_start_daemon_va (unsigned int flags, MHD_DLOG (daemon, _("MHD failed to initialize IP connection limit mutex\n")); #endif - (void) MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); - if ( (MHD_INVALID_SOCKET != socket_fd) && - (0 != MHD_socket_close_ (socket_fd)) ) - MHD_PANIC (_("close failed\n")); + MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); + if (MHD_INVALID_SOCKET != socket_fd) + MHD_socket_close_ (socket_fd); goto free_and_fail; } @@ -4963,11 +4931,10 @@ MHD_start_daemon_va (unsigned int flags, MHD_DLOG (daemon, _("Failed to initialize TLS support\n")); #endif - if ( (MHD_INVALID_SOCKET != socket_fd) && - (0 != MHD_socket_close_ (socket_fd)) ) - MHD_PANIC (_("close failed\n")); - (void) MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); - (void) MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); + if (MHD_INVALID_SOCKET != socket_fd) + MHD_socket_close_ (socket_fd); + MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); + MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); goto free_and_fail; } #endif @@ -4987,11 +4954,10 @@ MHD_start_daemon_va (unsigned int flags, _("Failed to create listen thread: %s\n"), MHD_strerror_ (errno)); #endif - (void) MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); - (void) MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); - if ( (MHD_INVALID_SOCKET != socket_fd) && - (0 != MHD_socket_close_ (socket_fd)) ) - MHD_PANIC (_("close failed\n")); + MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); + MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); + if (MHD_INVALID_SOCKET != socket_fd) + MHD_socket_close_ (socket_fd); goto free_and_fail; } if ( (daemon->worker_pool_size > 0) && @@ -5107,7 +5073,7 @@ MHD_start_daemon_va (unsigned int flags, #endif /* Free memory for this worker; cleanup below handles * all previously-created workers. */ - (void) MHD_mutex_destroy_ (&d->cleanup_connection_mutex); + MHD_mutex_destroy_ (&d->cleanup_connection_mutex); goto thread_failed; } } @@ -5127,11 +5093,10 @@ thread_failed: MHD_USE_SELECT_INTERNALLY mode. */ if (0 == i) { - if ( (MHD_INVALID_SOCKET != socket_fd) && - (0 != MHD_socket_close_ (socket_fd)) ) - MHD_PANIC (_("close failed\n")); - (void) MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); - (void) MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); + if (MHD_INVALID_SOCKET != socket_fd) + MHD_socket_close_ (socket_fd); + MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); + MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); if (NULL != daemon->worker_pool) free (daemon->worker_pool); goto free_and_fail; @@ -5169,7 +5134,7 @@ thread_failed: #endif #ifdef DAUTH_SUPPORT free (daemon->nnc); - (void) MHD_mutex_destroy_ (&daemon->nnc_lock); + MHD_mutex_destroy_ (&daemon->nnc_lock); #endif #if HTTPS_SUPPORT if (0 != (flags & MHD_USE_SSL)) @@ -5231,11 +5196,15 @@ close_all_connections (struct MHD_Daemon *daemon) { struct MHD_Connection *pos; + /* Give suspended connections a chance to resume to avoid + running into the check for there not being any suspended + connections left in case of a tight race with a recently + resumed connection. */ + resume_suspended_connections (daemon); /* first, make sure all threads are aware of shutdown; need to traverse DLLs in peace... */ - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to acquire cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_lock_ (&daemon->cleanup_connection_mutex); if (NULL != daemon->suspended_connections_head) MHD_PANIC (_("MHD_stop_daemon() called while we have suspended connections.\n")); for (pos = daemon->connections_head; NULL != pos; pos = pos->next) @@ -5248,9 +5217,8 @@ close_all_connections (struct MHD_Daemon *daemon) MHD_PANIC (_("Failed to signal shutdown via pipe")); #endif } - if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (! MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) - MHD_PANIC (_("Failed to release cleanup mutex\n")); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex); /* now, collect per-connection threads */ if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) @@ -5392,15 +5360,13 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) if (!MHD_join_thread_ (daemon->worker_pool[i].pid)) MHD_PANIC (_("Failed to join a thread\n")); close_all_connections (&daemon->worker_pool[i]); - (void) MHD_mutex_destroy_ (&daemon->worker_pool[i].cleanup_connection_mutex); + MHD_mutex_destroy_ (&daemon->worker_pool[i].cleanup_connection_mutex); #ifdef EPOLL_SUPPORT - if ( (-1 != daemon->worker_pool[i].epoll_fd) && - (0 != MHD_socket_close_ (daemon->worker_pool[i].epoll_fd)) ) - MHD_PANIC (_("close failed\n")); + if (-1 != daemon->worker_pool[i].epoll_fd) + MHD_socket_close_ (daemon->worker_pool[i].epoll_fd); #if HTTPS_SUPPORT - if ( (-1 != daemon->worker_pool[i].epoll_upgrade_fd) && - (0 != MHD_socket_close_ (daemon->worker_pool[i].epoll_upgrade_fd)) ) - MHD_PANIC (_("close failed\n")); + if (-1 != daemon->worker_pool[i].epoll_upgrade_fd) + MHD_socket_close_ (daemon->worker_pool[i].epoll_upgrade_fd); #endif #endif /* Individual pipes are always used */ @@ -5431,9 +5397,8 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) } } close_all_connections (daemon); - if ( (MHD_INVALID_SOCKET != fd) && - (0 != MHD_socket_close_ (fd)) ) - MHD_PANIC (_("close failed\n")); + if (MHD_INVALID_SOCKET != fd) + MHD_socket_close_ (fd); /* TLS clean up */ #if HTTPS_SUPPORT @@ -5451,23 +5416,21 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) #endif #ifdef EPOLL_SUPPORT if ( (0 != (daemon->options & MHD_USE_EPOLL)) && - (-1 != daemon->epoll_fd) && - (0 != MHD_socket_close_ (daemon->epoll_fd)) ) - MHD_PANIC (_("close failed\n")); + (-1 != daemon->epoll_fd) ) + MHD_socket_close_ (daemon->epoll_fd); #if HTTPS_SUPPORT if ( (0 != (daemon->options & MHD_USE_EPOLL)) && - (-1 != daemon->epoll_upgrade_fd) && - (0 != MHD_socket_close_ (daemon->epoll_upgrade_fd)) ) - MHD_PANIC (_("close failed\n")); + (-1 != daemon->epoll_upgrade_fd) ) + MHD_socket_close_ (daemon->epoll_upgrade_fd); #endif #endif #ifdef DAUTH_SUPPORT free (daemon->nnc); - (void) MHD_mutex_destroy_ (&daemon->nnc_lock); + MHD_mutex_destroy_ (&daemon->nnc_lock); #endif - (void) MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); - (void) MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); + MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); + MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); if (MHD_INVALID_PIPE_ != daemon->wpipe[1]) { @@ -5735,14 +5698,16 @@ gcry_w32_mutex_destroy (void **ppmtx) static int gcry_w32_mutex_lock (void **ppmtx) { - return (MHD_mutex_lock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; + MHD_mutex_lock_ ((MHD_mutex_*)*ppmtx); + return 0; } static int gcry_w32_mutex_unlock (void **ppmtx) { - return (MHD_mutex_unlock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; + MHD_mutex_unlock_ ((MHD_mutex_*)*ppmtx); + return 0; } diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c @@ -408,7 +408,7 @@ check_nonce_nc (struct MHD_Connection *connection, * then only increase the nonce counter by one. */ nn = &daemon->nnc[off]; - (void) MHD_mutex_lock_ (&daemon->nnc_lock); + MHD_mutex_lock_ (&daemon->nnc_lock); if (0 == nc) { /* Fresh nonce, reinitialize array */ @@ -416,7 +416,7 @@ check_nonce_nc (struct MHD_Connection *connection, nonce); nn->nc = 0; nn->nmask = 0; - (void) MHD_mutex_unlock_ (&daemon->nnc_lock); + MHD_mutex_unlock_ (&daemon->nnc_lock); return MHD_YES; } /* Note that we use 64 here, as we do not store the @@ -428,7 +428,7 @@ check_nonce_nc (struct MHD_Connection *connection, { /* Out-of-order nonce, but within 64-bit bitmask, set bit */ nn->nmask |= (1LLU < (nn->nc - nc - 1)); - (void) MHD_mutex_unlock_ (&daemon->nnc_lock); + MHD_mutex_unlock_ (&daemon->nnc_lock); return MHD_YES; } @@ -437,7 +437,7 @@ check_nonce_nc (struct MHD_Connection *connection, nonce)) ) { /* Nonce does not match, fail */ - (void) MHD_mutex_unlock_ (&daemon->nnc_lock); + MHD_mutex_unlock_ (&daemon->nnc_lock); #ifdef HAVE_MESSAGES MHD_DLOG (daemon, _("Stale nonce received. If this happens a lot, you should probably increase the size of the nonce array.\n")); @@ -450,7 +450,7 @@ check_nonce_nc (struct MHD_Connection *connection, else nn->nmask = 0; /* big jump, unset all bits in the mask */ nn->nc = nc; - (void) MHD_mutex_unlock_ (&daemon->nnc_lock); + MHD_mutex_unlock_ (&daemon->nnc_lock); return MHD_YES; } diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h @@ -35,6 +35,7 @@ #include <gnutls/abstract.h> #endif #endif + #include "mhd_threads.h" #include "mhd_locks.h" #include "mhd_sockets.h" @@ -42,14 +43,6 @@ /** - * Macro to make it easy to mark text for translation. Note that - * we do not actually call gettext() in MHD, but we do make it - * easy to create a ".po" file so that applications that do want - * to translate error messages can do so. - */ -#define _(String) String - -/** * Should we perform additional sanity checks at runtime (on our internal * invariants)? This may lead to aborts, but can be useful for debugging. */ diff --git a/src/microhttpd/mhd_locks.h b/src/microhttpd/mhd_locks.h @@ -80,16 +80,19 @@ /** * Destroy previously initialised mutex. * @param pmutex pointer to mutex - * @return nonzero on success, zero otherwise */ -#define MHD_mutex_destroy_(pmutex) (!(pthread_mutex_destroy((pmutex)))) +#define MHD_mutex_destroy_(pmutex) do { \ + if ( (0 != pthread_mutex_destroy((pmutex))) && \ + (EAGAIN != errno) && \ + (EINPROGRESS != errno) ) \ + MHD_PANIC (_("Failed to destroy mutex\n")); \ + } while (0) #elif defined(MHD_W32_MUTEX_) /** * Destroy previously initialised mutex. * @param pmutex pointer to mutex - * @return Always nonzero */ -#define MHD_mutex_destroy_(pmutex) (DeleteCriticalSection((pmutex)), !0) +#define MHD_mutex_destroy_(pmutex) DeleteCriticalSection((pmutex)) #endif #if defined(MHD_PTHREAD_MUTEX_) @@ -98,38 +101,19 @@ * If mutex was already locked by other thread, function * blocks until mutex becomes available. * @param pmutex pointer to mutex - * @return nonzero on success, zero otherwise */ -#define MHD_mutex_lock_(pmutex) (!(pthread_mutex_lock((pmutex)))) +#define MHD_mutex_lock_(pmutex) do { \ + if (0 != pthread_mutex_lock((pmutex))) \ + MHD_PANIC (_("Failed to lock mutex\n")); \ + } while (0) #elif defined(MHD_W32_MUTEX_) /** * Acquire lock on previously initialised mutex. * If mutex was already locked by other thread, function * blocks until mutex becomes available. * @param pmutex pointer to mutex - * @return Always nonzero - */ -#define MHD_mutex_lock_(pmutex) (EnterCriticalSection((pmutex)), !0) -#endif - -#if defined(MHD_PTHREAD_MUTEX_) -/** - * Try to acquire lock on previously initialised mutex. - * Function returns immediately. - * @param pmutex pointer to mutex - * @return nonzero if mutex is locked, zero if - * mutex was not locked. - */ -#define MHD_mutex_trylock_(pmutex) (!(pthread_mutex_trylock((pmutex)))) -#elif defined(MHD_W32_MUTEX_) -/** - * Try to acquire lock on previously initialised mutex. - * Function returns immediately. - * @param pmutex pointer to mutex - * @return nonzero if mutex is locked, zero if - * mutex was not locked. */ -#define MHD_mutex_trylock_(pmutex) (TryEnterCriticalSection((pmutex)))) +#define MHD_mutex_lock_(pmutex) EnterCriticalSection((pmutex)) #endif #if defined(MHD_PTHREAD_MUTEX_) @@ -138,14 +122,17 @@ * @param pmutex pointer to mutex * @return nonzero on success, zero otherwise */ -#define MHD_mutex_unlock_(pmutex) (!(pthread_mutex_unlock((pmutex)))) +#define MHD_mutex_unlock_(pmutex) do { \ + if (0 != pthread_mutex_unlock((pmutex))) \ + MHD_PANIC (_("Failed to unlock mutex\n")); \ + } while (0) #elif defined(MHD_W32_MUTEX_) /** * Unlock previously initialised and locked mutex. * @param pmutex pointer to mutex * @return Always nonzero */ -#define MHD_mutex_unlock_(pmutex) (LeaveCriticalSection((pmutex)), !0) +#define MHD_mutex_unlock_(pmutex) LeaveCriticalSection((pmutex)) #endif diff --git a/src/microhttpd/mhd_sem.c b/src/microhttpd/mhd_sem.c @@ -89,16 +89,16 @@ void MHD_semaphore_down (struct MHD_Semaphore *sem) { if (0 != pthread_mutex_lock (&sem->mutex)) - MHD_PANIC ("pthread_mutex_lock for semaphore failed\n"); + MHD_PANIC (_("Failed to lock mutex\n")); while (0 == sem->counter) { if (0 != pthread_cond_wait (&sem->cv, &sem->mutex)) - MHD_PANIC ("pthread_cond_wait failed\n"); + MHD_PANIC (_("pthread_cond_wait failed\n")); } sem->counter--; if (0 != pthread_mutex_unlock (&sem->mutex)) - MHD_PANIC ("pthread_mutex_unlock for semaphore failed\n"); + MHD_PANIC (_("Failed to unlock mutex\n")); } @@ -111,11 +111,11 @@ void MHD_semaphore_up (struct MHD_Semaphore *sem) { if (0 != pthread_mutex_lock (&sem->mutex)) - MHD_PANIC ("pthread_mutex_lock for semaphore failed\n"); + MHD_PANIC (_("Failed to lock mutex\n")); sem->counter++; pthread_cond_signal (&sem->cv); if (0 != pthread_mutex_unlock (&sem->mutex)) - MHD_PANIC ("pthread_mutex_unlock for semaphore failed\n"); + MHD_PANIC (_("Failed to unlock mutex\n")); } @@ -128,9 +128,9 @@ void MHD_semaphore_destroy (struct MHD_Semaphore *sem) { if (0 != pthread_cond_destroy (&sem->cv)) - MHD_PANIC ("pthread_cond_destroy failed\n"); + MHD_PANIC (_("pthread_cond_destroy failed\n")); if (0 != pthread_mutex_destroy (&sem->mutex)) - MHD_PANIC ("pthread_mutex_destroy failed\n"); + MHD_PANIC (_("Failed to destroy mutex\n")); free (sem); } diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h @@ -195,13 +195,18 @@ * errno is set to EINTR. Do not use HP-UNIX. * * @param fd descriptor to close - * @return 0 on success (error codes like EINTR and EIO are counted as success, - * only EBADF counts as an error!) */ #if !defined(MHD_WINSOCK_SOCKETS) -# define MHD_socket_close_(fd) (((0 != close(fd)) && (EBADF == errno)) ? -1 : 0) +# define MHD_socket_close_(fd) do { \ + if ( (0 != close((fd))) && \ + (EBADF == errno) ) \ + MHD_PANIC (_("close failed\n")); \ + } while (0) #else -# define MHD_socket_close_(fd) closesocket((fd)) +# define MHD_socket_close_(fd) do { \ + if (0 != closesocket((fd)) ) \ + MHD_PANIC (_("close failed\n")); \ + } while (0) #endif /** diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c @@ -568,7 +568,7 @@ MHD_create_response_from_data (size_t size, { if (NULL == (tmp = malloc (size))) { - (void) MHD_mutex_destroy_ (&response->mutex); + MHD_mutex_destroy_ (&response->mutex); free (response); return NULL; } @@ -655,8 +655,7 @@ MHD_upgrade_action (struct MHD_UpgradeResponseHandle *urh, urh->was_closed = MHD_YES; if (MHD_INVALID_SOCKET != urh->app.socket) { - if (0 != MHD_socket_close_ (urh->app.socket)) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (urh->app.socket); urh->app.socket = MHD_INVALID_SOCKET; } return MHD_YES; @@ -743,10 +742,8 @@ MHD_response_execute_upgrade_ (struct MHD_Response *response, (int) sv[1], (int) FD_SETSIZE); #endif - if (0 != MHD_socket_close_ (sv[0])) - MHD_PANIC (_("close failed\n")); - if (0 != MHD_socket_close_ (sv[1])) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (sv[0]); + MHD_socket_close_ (sv[1]); free (urh); return MHD_NO; } @@ -817,10 +814,8 @@ MHD_response_execute_upgrade_ (struct MHD_Response *response, _("Call to epoll_ctl failed: %s\n"), MHD_socket_last_strerr_ ()); #endif - if (0 != MHD_socket_close_ (sv[0])) - MHD_PANIC (_("close failed\n")); - if (0 != MHD_socket_close_ (sv[1])) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (sv[0]); + MHD_socket_close_ (sv[1]); free (urh); return MHD_NO; } @@ -845,10 +840,8 @@ MHD_response_execute_upgrade_ (struct MHD_Response *response, _("Call to epoll_ctl failed: %s\n"), MHD_socket_last_strerr_ ()); #endif - if (0 != MHD_socket_close_ (sv[0])) - MHD_PANIC (_("close failed\n")); - if (0 != MHD_socket_close_ (sv[1])) - MHD_PANIC (_("close failed\n")); + MHD_socket_close_ (sv[0]); + MHD_socket_close_ (sv[1]); free (urh); return MHD_NO; } @@ -993,14 +986,14 @@ MHD_destroy_response (struct MHD_Response *response) if (NULL == response) return; - (void) MHD_mutex_lock_ (&response->mutex); + MHD_mutex_lock_ (&response->mutex); if (0 != --(response->reference_count)) { - (void) MHD_mutex_unlock_ (&response->mutex); + MHD_mutex_unlock_ (&response->mutex); return; } - (void) MHD_mutex_unlock_ (&response->mutex); - (void) MHD_mutex_destroy_ (&response->mutex); + MHD_mutex_unlock_ (&response->mutex); + MHD_mutex_destroy_ (&response->mutex); if (NULL != response->crfc) response->crfc (response->crc_cls); while (NULL != response->first_header) @@ -1023,9 +1016,9 @@ MHD_destroy_response (struct MHD_Response *response) void MHD_increment_response_rc (struct MHD_Response *response) { - (void) MHD_mutex_lock_ (&response->mutex); + MHD_mutex_lock_ (&response->mutex); (response->reference_count)++; - (void) MHD_mutex_unlock_ (&response->mutex); + MHD_mutex_unlock_ (&response->mutex); } diff --git a/src/microhttpd/test_shutdown_select.c b/src/microhttpd/test_shutdown_select.c @@ -89,6 +89,15 @@ static _MHD_bool check_err; + +void +MHD_PANIC (char *msg) +{ + fprintf (stderr, "%s", msg); + abort (); +} + + static _MHD_bool has_in_name(const char *prog_name, const char *marker) { diff --git a/src/microhttpd/test_upgrade_common.c b/src/microhttpd/test_upgrade_common.c @@ -45,6 +45,15 @@ static pthread_t pt_client; */ static int done; + +void +MHD_PANIC (char *msg) +{ + fprintf (stderr, "%s", msg); + abort (); +} + + /** * Change socket to non-blocking. * diff --git a/src/testcurl/https/test_https_time_out.c b/src/testcurl/https/test_https_time_out.c @@ -32,6 +32,16 @@ #include <gcrypt.h> #include "mhd_sockets.h" /* only macros used */ +#undef MHD_PANIC + + +void +MHD_PANIC (char *msg) +{ + fprintf (stderr, "%s", msg); + abort (); +} + #ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN 1 diff --git a/src/testcurl/test_callback.c b/src/testcurl/test_callback.c @@ -73,14 +73,19 @@ callback(void *cls, r = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024, &called_twice, cbc, &free); - MHD_queue_response(connection, MHD_HTTP_OK, r); - MHD_destroy_response(r); + MHD_queue_response (connection, + MHD_HTTP_OK, + r); + MHD_destroy_response (r); return MHD_YES; } static size_t -discard_buffer (void *ptr, size_t size, size_t nmemb, void *ctx) +discard_buffer (void *ptr, + size_t size, + size_t nmemb, + void *ctx) { return size * nmemb; } diff --git a/src/testcurl/test_get.c b/src/testcurl/test_get.c @@ -34,6 +34,14 @@ #include <time.h> #include "mhd_sockets.h" /* only macros used */ +void +MHD_PANIC (char *msg) +{ + fprintf (stderr, "%s", msg); + abort (); +} + + #ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN 1 diff --git a/src/testcurl/test_quiesce.c b/src/testcurl/test_quiesce.c @@ -34,6 +34,14 @@ #include <pthread.h> #include "mhd_sockets.h" /* only macros used */ +void +MHD_PANIC (char *msg) +{ + fprintf (stderr, "%s", msg); + abort (); +} + + #ifndef WINDOWS #include <unistd.h> #include <sys/socket.h>