libmicrohttpd

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

commit 011d784f6c8d6c305e29bee1374a240ab9e4d7c7
parent c63ac7db4ebc38713aa5d5e2edf52c27200003d8
Author: Christian Grothoff <christian@grothoff.org>
Date:   Fri, 23 Sep 2016 14:18:06 +0000

add return value checks for a few more system calls in testcases and examples

Diffstat:
Msrc/examples/fileserver_example_external_select.c | 6+++++-
Msrc/examples/post_example.c | 6+++++-
Msrc/examples/timeout.c | 21++++++++++++---------
Msrc/microhttpd/test_upgrade.c | 9+++++----
Msrc/microhttpd/test_upgrade_common.c | 9+++++----
Msrc/testcurl/test_callback.c | 6+++++-
Msrc/testcurl/test_postform.c | 23++++++++++++++---------
Msrc/testcurl/test_put_chunked.c | 9+++++++--
Msrc/testcurl/test_quiesce.c | 184+++++++++++++++++++++++++++++++++++++++++++------------------------------------
9 files changed, 158 insertions(+), 115 deletions(-)

diff --git a/src/examples/fileserver_example_external_select.c b/src/examples/fileserver_example_external_select.c @@ -159,7 +159,11 @@ main (int argc, char *const *argv) tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000LL)) * 1000LL; } } - select (max + 1, &rs, &ws, &es, &tv); + if (-1 == select (max + 1, &rs, &ws, &es, &tv)) + { + if (EINTR != errno) + abort (); + } MHD_run (d); } MHD_stop_daemon (d); diff --git a/src/examples/post_example.c b/src/examples/post_example.c @@ -741,7 +741,11 @@ main (int argc, char *const *argv) } else tvp = NULL; - select (max + 1, &rs, &ws, &es, tvp); + if (-1 == select (max + 1, &rs, &ws, &es, tvp)) + { + if (EINTR != errno) + abort (); + } MHD_run (d); } MHD_stop_daemon (d); diff --git a/src/examples/timeout.c b/src/examples/timeout.c @@ -21,9 +21,9 @@ answer_to_connection(void *cls, response = MHD_create_response_from_buffer (strlen(page), (void *) page, MHD_RESPMEM_PERSISTENT); - MHD_add_response_header(response, - MHD_HTTP_HEADER_CONTENT_TYPE, - "text/html"); + MHD_add_response_header (response, + MHD_HTTP_HEADER_CONTENT_TYPE, + "text/html"); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); @@ -32,15 +32,18 @@ answer_to_connection(void *cls, } -int main() +int +main (int argc, + char **argv) { struct MHD_Daemon *daemon; - daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, - PORT, NULL, NULL, - &answer_to_connection, NULL, - MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 3, - MHD_OPTION_END); + daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION, + PORT, + NULL, NULL, + &answer_to_connection, NULL, + MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 3, + MHD_OPTION_END); if (NULL == daemon) return 1; getchar(); diff --git a/src/microhttpd/test_upgrade.c b/src/microhttpd/test_upgrade.c @@ -77,10 +77,11 @@ test_upgrade (int flags, (struct sockaddr *) &sa, sizeof (sa))) abort (); - pthread_create (&pt_client, - NULL, - &run_usock_client, - &sock); + if (0 !- pthread_create (&pt_client, + NULL, + &run_usock_client, + &sock)) + abort (); if (0 == (flags & (MHD_USE_SELECT_INTERNALLY | MHD_USE_THREAD_PER_CONNECTION)) ) run_mhd_loop (d, flags); diff --git a/src/microhttpd/test_upgrade_common.c b/src/microhttpd/test_upgrade_common.c @@ -301,10 +301,11 @@ upgrade_cb (void *cls, usock = sock; if (0 != extra_in_size) abort (); - pthread_create (&pt, - NULL, - &run_usock, - urh); + if (0 != pthread_create (&pt, + NULL, + &run_usock, + urh)) + abort (); } diff --git a/src/testcurl/test_callback.c b/src/testcurl/test_callback.c @@ -174,7 +174,11 @@ main(int argc, char **argv) } tv.tv_sec = 0; tv.tv_usec = 1000; - select(maxposixs + 1, &rs, &ws, &es, &tv); + if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv)) + { + if (EINTR != errno) + abort (); + } if (NULL != multi) { curl_multi_perform (multi, &running); diff --git a/src/testcurl/test_postform.c b/src/testcurl/test_postform.c @@ -184,8 +184,8 @@ testInternalPost () cbc.size = 2048; cbc.pos = 0; d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, - 1080, NULL, NULL, &ahc_echo, NULL, - MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, + 1080, NULL, NULL, &ahc_echo, NULL, + MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, MHD_OPTION_END); if (d == NULL) return 1; @@ -240,8 +240,8 @@ testMultithreadedPost () cbc.size = 2048; cbc.pos = 0; d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, - 1081, NULL, NULL, &ahc_echo, NULL, - MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, + 1081, NULL, NULL, &ahc_echo, NULL, + MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, MHD_OPTION_END); if (d == NULL) return 16; @@ -298,7 +298,7 @@ testMultithreadedPoolPost () d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, 1081, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_THREAD_POOL_SIZE, CPU_COUNT, - MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, + MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, MHD_OPTION_END); if (d == NULL) return 16; @@ -368,8 +368,8 @@ testExternalPost () cbc.size = 2048; cbc.pos = 0; d = MHD_start_daemon (MHD_USE_DEBUG, - 1082, NULL, NULL, &ahc_echo, NULL, - MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, + 1082, NULL, NULL, &ahc_echo, NULL, + MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, MHD_OPTION_END); if (d == NULL) return 256; @@ -439,7 +439,11 @@ testExternalPost () } tv.tv_sec = 0; tv.tv_usec = 1000; - select (maxposixs + 1, &rs, &ws, &es, &tv); + if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv)) + { + if (EINTR != errno) + abort (); + } curl_multi_perform (multi, &running); if (running == 0) { @@ -452,7 +456,8 @@ testExternalPost () printf ("%s failed at %s:%d: `%s'\n", "curl_multi_perform", __FILE__, - __LINE__, curl_easy_strerror (msg->data.result)); + __LINE__, + curl_easy_strerror (msg->data.result)); curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); diff --git a/src/testcurl/test_put_chunked.c b/src/testcurl/test_put_chunked.c @@ -397,7 +397,11 @@ testExternalPut () } tv.tv_sec = 0; tv.tv_usec = 1000; - select (maxposixs + 1, &rs, &ws, &es, &tv); + if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv)) + { + if (EINTR != errno) + abort (); + } curl_multi_perform (multi, &running); if (running == 0) { @@ -410,7 +414,8 @@ testExternalPut () printf ("%s failed at %s:%d: `%s'\n", "curl_multi_perform", __FILE__, - __LINE__, curl_easy_strerror (msg->data.result)); + __LINE__, + curl_easy_strerror (msg->data.result)); curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); diff --git a/src/testcurl/test_quiesce.c b/src/testcurl/test_quiesce.c @@ -155,7 +155,11 @@ ServeOneRequest(void *param) } tv.tv_sec = 0; tv.tv_usec = 1000; - MHD_SYS_select_ (max + 1, &rs, &ws, &es, &tv); + if (-1 == MHD_SYS_select_ (max + 1, &rs, &ws, &es, &tv)) + { + if (EINTR != errno) + abort (); + } MHD_run (d); } fd = MHD_quiesce_daemon (d); @@ -350,7 +354,10 @@ testExternalGet () cbc.size = 2048; cbc.pos = 0; d = MHD_start_daemon (MHD_USE_DEBUG, - 11080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END); + 11080, + NULL, NULL, + &ahc_echo, "GET", + MHD_OPTION_END); if (d == NULL) return 256; c = setupCURL(&cbc); @@ -371,91 +378,100 @@ testExternalGet () return 1024; } - for (i = 0; i < 2; i++) { - start = time (NULL); - while ((time (NULL) - start < 5) && (multi != NULL)) - { - maxsock = MHD_INVALID_SOCKET; - maxposixs = -1; - FD_ZERO (&rs); - FD_ZERO (&ws); - FD_ZERO (&es); - curl_multi_perform (multi, &running); - mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs); - if (mret != CURLM_OK) - { - curl_multi_remove_handle (multi, c); - curl_multi_cleanup (multi); - curl_easy_cleanup (c); - MHD_stop_daemon (d); - return 2048; - } - if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock)) - { - curl_multi_remove_handle (multi, c); - curl_multi_cleanup (multi); - curl_easy_cleanup (c); - MHD_stop_daemon (d); - return 4096; - } - tv.tv_sec = 0; - tv.tv_usec = 1000; - select (maxposixs + 1, &rs, &ws, &es, &tv); - curl_multi_perform (multi, &running); - if (running == 0) - { - msg = curl_multi_info_read (multi, &running); - if (msg == NULL) - break; - if (msg->msg == CURLMSG_DONE) - { - if (i == 0 && msg->data.result != CURLE_OK) - printf ("%s failed at %s:%d: `%s'\n", - "curl_multi_perform", - __FILE__, - __LINE__, curl_easy_strerror (msg->data.result)); - else if (i == 1 && msg->data.result == CURLE_OK) - printf ("%s should have failed at %s:%d\n", - "curl_multi_perform", - __FILE__, - __LINE__); - curl_multi_remove_handle (multi, c); - curl_multi_cleanup (multi); - curl_easy_cleanup (c); - c = NULL; - multi = NULL; - } - } - MHD_run (d); - } - - if (i == 0) { - /* quiesce the daemon on the 1st iteration, so the 2nd should fail */ - fd = MHD_quiesce_daemon(d); - if (MHD_INVALID_SOCKET == fd) - { - fprintf (stderr, - "MHD_quiesce_daemon failed.\n"); - curl_multi_remove_handle (multi, c); - curl_multi_cleanup (multi); - curl_easy_cleanup (c); - MHD_stop_daemon (d); - return 2; - } - c = setupCURL (&cbc); - multi = curl_multi_init (); - mret = curl_multi_add_handle (multi, c); - if (mret != CURLM_OK) + for (i = 0; i < 2; i++) + { + start = time (NULL); + while ( (time (NULL) - start < 5) && + (NULL != multi) ) + { + maxsock = MHD_INVALID_SOCKET; + maxposixs = -1; + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); + curl_multi_perform (multi, &running); + mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs); + if (mret != CURLM_OK) + { + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 2048; + } + if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock)) + { + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 4096; + } + tv.tv_sec = 0; + tv.tv_usec = 1000; + if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv)) + { + if (EINTR != errno) + abort (); + } + curl_multi_perform (multi, &running); + if (0 == running) + { + msg = curl_multi_info_read (multi, &running); + if (NULL == msg) + break; + if (msg->msg == CURLMSG_DONE) + { + if (i == 0 && msg->data.result != CURLE_OK) + printf ("%s failed at %s:%d: `%s'\n", + "curl_multi_perform", + __FILE__, + __LINE__, + curl_easy_strerror (msg->data.result)); + else if ( (i == 1) && + (msg->data.result == CURLE_OK) ) + printf ("%s should have failed at %s:%d\n", + "curl_multi_perform", + __FILE__, + __LINE__); + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + c = NULL; + multi = NULL; + } + } + MHD_run (d); + } + + if (0 == i) { - curl_multi_remove_handle (multi, c); - curl_multi_cleanup (multi); - curl_easy_cleanup (c); - MHD_stop_daemon (d); - return 32768; + /* quiesce the daemon on the 1st iteration, so the 2nd should fail */ + fd = MHD_quiesce_daemon(d); + if (MHD_INVALID_SOCKET == fd) + { + fprintf (stderr, + "MHD_quiesce_daemon failed.\n"); + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 2; + } + c = setupCURL (&cbc); + multi = curl_multi_init (); + mret = curl_multi_add_handle (multi, c); + if (mret != CURLM_OK) + { + curl_multi_remove_handle (multi, c); + curl_multi_cleanup (multi); + curl_easy_cleanup (c); + MHD_stop_daemon (d); + return 32768; + } } - } } - if (multi != NULL) + if (NULL != multi) { curl_multi_remove_handle (multi, c); curl_easy_cleanup (c);