libmicrohttpd

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

commit 091bf71cb51098586475d44b97bf5e37c4a8824f
parent fa42f6207bf3fe3b4c5f1bee26deb8933878050f
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Thu,  8 Oct 2020 22:44:33 +0300

Added asserts to check correct threads for key functions

Diffstat:
Msrc/microhttpd/connection.c | 13++++++++++++-
Msrc/microhttpd/daemon.c | 40++++++++++++++++++++++++++++++++++++++--
Msrc/microhttpd/mhd_threads.h | 5+++--
Msrc/microhttpd/response.c | 3+++
4 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -687,6 +687,9 @@ MHD_connection_close_ (struct MHD_Connection *connection, struct MHD_Daemon *daemon = connection->daemon; struct MHD_Response *resp = connection->response; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (connection->pid) ); + MHD_connection_mark_closed_ (connection); if (NULL != resp) { @@ -720,6 +723,10 @@ MHD_connection_finish_forward_ (struct MHD_Connection *connection) struct MHD_Daemon *daemon = connection->daemon; struct MHD_UpgradeResponseHandle *urh = connection->urh; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); + if (0 == (daemon->options & MHD_USE_TLS)) return; /* Nothing to do with non-TLS connection. */ @@ -3158,6 +3165,8 @@ static void cleanup_connection (struct MHD_Connection *connection) { struct MHD_Daemon *daemon = connection->daemon; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (connection->pid) ); if (connection->in_cleanup) return; /* Prevent double cleanup. */ @@ -3237,6 +3246,8 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) char *line; size_t line_len; enum MHD_Result ret; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (connection->pid) ); connection->in_idle = true; while (! connection->suspended) @@ -3933,7 +3944,7 @@ MHD_queue_response (struct MHD_Connection *connection, #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) if ( (! connection->suspended) && (0 != (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) && - (! MHD_thread_ID_match_current_ (connection->pid.ID)) ) + (! MHD_thread_ID_match_current_ (connection->pid)) ) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -1336,6 +1336,8 @@ process_urh (struct MHD_UpgradeResponseHandle *urh) * of processing - it will be processed on next iteration. */ bool was_closed; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (connection->pid) ); if (daemon->shutdown) { /* Daemon shutting down, application will not receive any more data. */ @@ -1687,6 +1689,8 @@ thread_main_connection_upgrade (struct MHD_Connection *con) struct MHD_UpgradeResponseHandle *urh = con->urh; struct MHD_Daemon *daemon = con->daemon; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (con->pid) ); /* Here, we need to bi-directionally forward until the application tells us that it is done with the socket; */ @@ -2818,6 +2822,10 @@ internal_suspend_connection_ (struct MHD_Connection *connection) { struct MHD_Daemon *daemon = connection->daemon; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); + #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); #endif @@ -2913,6 +2921,10 @@ MHD_suspend_connection (struct MHD_Connection *connection) { struct MHD_Daemon *const daemon = connection->daemon; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); + if (0 == (daemon->options & MHD_TEST_ALLOW_SUSPEND_RESUME)) MHD_PANIC (_ ( "Cannot suspend connections without enabling MHD_ALLOW_SUSPEND_RESUME!\n")); @@ -2991,6 +3003,9 @@ resume_suspended_connections (struct MHD_Daemon *daemon) #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); #endif + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); + if (daemon->resuming) { prev = daemon->suspended_connections_tail; @@ -3206,6 +3221,9 @@ MHD_accept_connection (struct MHD_Daemon *daemon) MHD_socket fd; bool sk_nonbl; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); + addrlen = sizeof (addrstorage); memset (addr, 0, @@ -3346,6 +3364,8 @@ static void MHD_cleanup_connections (struct MHD_Daemon *daemon) { struct MHD_Connection *pos; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); @@ -3460,6 +3480,9 @@ MHD_get_timeout (struct MHD_Daemon *daemon, struct MHD_Connection *pos; bool have_timeout; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { #ifdef HAVE_MESSAGES @@ -4254,6 +4277,9 @@ run_epoll_for_upgrade (struct MHD_Daemon *daemon) struct MHD_UpgradeResponseHandle *pos; struct MHD_UpgradeResponseHandle *prev; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); + num_events = MAX_EVENTS; while (0 != num_events) { @@ -4733,6 +4759,9 @@ close_connection (struct MHD_Connection *pos) { struct MHD_Daemon *daemon = pos->daemon; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { MHD_connection_mark_closed_ (pos); @@ -5628,8 +5657,7 @@ setup_epoll_fd (struct MHD_Daemon *daemon) /** * Setup epoll() FD for the daemon and initialize it to listen * on the listen FD. - * @remark To be called only from thread that process - * daemon's select()/poll()/etc. + * @remark To be called only from MHD_start_daemon_va() * * @param daemon daemon to initialize for epoll() * @return #MHD_YES on success, #MHD_NO on failure @@ -6693,6 +6721,10 @@ close_all_connections (struct MHD_Daemon *daemon) struct MHD_UpgradeResponseHandle *urhn; const bool used_tls = (0 != (daemon->options & MHD_USE_TLS)); + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || \ + MHD_thread_ID_match_current_ (daemon->pid) ); + #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) mhd_assert (NULL == daemon->worker_pool); #endif @@ -6850,6 +6882,10 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) if (NULL == daemon) return; + if ( (daemon->shutdown) && (NULL == daemon->master) ) + MHD_PANIC (_ ("MHD_stop_daemon() was called twice.")); + /* Slave daemons must be stopped by master daemon. */ + mhd_assert ( (NULL == daemon->master) || (daemon->shutdown) ); daemon->shutdown = true; if (daemon->was_quiesced) diff --git a/src/microhttpd/mhd_threads.h b/src/microhttpd/mhd_threads.h @@ -154,14 +154,15 @@ typedef struct _MHD_thread_handle_ID_ MHD_thread_handle_ID_; * @param ID thread ID to match * @return nonzero on match, zero otherwise */ -#define MHD_thread_ID_match_current_(ID) (pthread_equal ((ID), pthread_self ())) +#define MHD_thread_ID_match_current_(pid) \ + (pthread_equal ((pid).ID, pthread_self ())) #elif defined(MHD_USE_W32_THREADS) /** * Check whether provided thread ID match current thread. * @param ID thread ID to match * @return nonzero on match, zero otherwise */ -#define MHD_thread_ID_match_current_(ID) (GetCurrentThreadId () == (ID)) +#define MHD_thread_ID_match_current_(pid) (GetCurrentThreadId () == (pid).ID) #endif #if defined(MHD_USE_POSIX_THREADS) diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c @@ -969,6 +969,9 @@ MHD_response_execute_upgrade_ (struct MHD_Response *response, struct MHD_UpgradeResponseHandle *urh; size_t rbo; + mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \ + MHD_thread_ID_match_current_ (connection->pid) ); + if (0 == (daemon->options & MHD_ALLOW_UPGRADE)) return MHD_NO;