diff options
Diffstat (limited to 'src/microhttpd/daemon.c')
-rw-r--r-- | src/microhttpd/daemon.c | 77 |
1 files changed, 41 insertions, 36 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index 7a784daf..68491938 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c | |||
@@ -457,7 +457,7 @@ recv_tls_adapter (struct MHD_Connection *connection, void *other, size_t i) | |||
457 | if ( (GNUTLS_E_AGAIN == res) || | 457 | if ( (GNUTLS_E_AGAIN == res) || |
458 | (GNUTLS_E_INTERRUPTED == res) ) | 458 | (GNUTLS_E_INTERRUPTED == res) ) |
459 | { | 459 | { |
460 | MHD_set_socket_errno_ (EINTR); | 460 | MHD_socket_set_error_ (MHD_SCKT_EINTR_); |
461 | #if EPOLL_SUPPORT | 461 | #if EPOLL_SUPPORT |
462 | connection->epoll_state &= ~MHD_EPOLL_STATE_READ_READY; | 462 | connection->epoll_state &= ~MHD_EPOLL_STATE_READ_READY; |
463 | #endif | 463 | #endif |
@@ -468,7 +468,7 @@ recv_tls_adapter (struct MHD_Connection *connection, void *other, size_t i) | |||
468 | /* Likely 'GNUTLS_E_INVALID_SESSION' (client communication | 468 | /* Likely 'GNUTLS_E_INVALID_SESSION' (client communication |
469 | disrupted); set errno to something caller will interpret | 469 | disrupted); set errno to something caller will interpret |
470 | correctly as a hard error */ | 470 | correctly as a hard error */ |
471 | MHD_set_socket_errno_ (ECONNRESET); | 471 | MHD_socket_set_error_ (MHD_SCKT_ECONNRESET_); |
472 | return res; | 472 | return res; |
473 | } | 473 | } |
474 | if ((size_t)res == i) | 474 | if ((size_t)res == i) |
@@ -498,7 +498,7 @@ send_tls_adapter (struct MHD_Connection *connection, | |||
498 | if ( (GNUTLS_E_AGAIN == res) || | 498 | if ( (GNUTLS_E_AGAIN == res) || |
499 | (GNUTLS_E_INTERRUPTED == res) ) | 499 | (GNUTLS_E_INTERRUPTED == res) ) |
500 | { | 500 | { |
501 | MHD_set_socket_errno_ (EINTR); | 501 | MHD_socket_set_error_ (MHD_SCKT_EINTR_); |
502 | #if EPOLL_SUPPORT | 502 | #if EPOLL_SUPPORT |
503 | connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY; | 503 | connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY; |
504 | #endif | 504 | #endif |
@@ -510,7 +510,7 @@ send_tls_adapter (struct MHD_Connection *connection, | |||
510 | really understand the error (not listed in GnuTLS | 510 | really understand the error (not listed in GnuTLS |
511 | documentation explicitly), we set 'errno' to something that | 511 | documentation explicitly), we set 'errno' to something that |
512 | will cause the connection to fail. */ | 512 | will cause the connection to fail. */ |
513 | MHD_set_socket_errno_ (ECONNRESET); | 513 | MHD_socket_set_error_ (MHD_SCKT_ECONNRESET_); |
514 | return -1; | 514 | return -1; |
515 | } | 515 | } |
516 | return res; | 516 | return res; |
@@ -971,13 +971,14 @@ MHD_handle_connection (void *data) | |||
971 | num_ready = MHD_SYS_select_ (maxsock + 1, &rs, &ws, NULL, tvp); | 971 | num_ready = MHD_SYS_select_ (maxsock + 1, &rs, &ws, NULL, tvp); |
972 | if (num_ready < 0) | 972 | if (num_ready < 0) |
973 | { | 973 | { |
974 | if (EINTR == MHD_socket_errno_) | 974 | const int err = MHD_socket_get_error_(); |
975 | if (MHD_SCKT_ERR_IS_EINTR_(err)) | ||
975 | continue; | 976 | continue; |
976 | #ifdef HAVE_MESSAGES | 977 | #ifdef HAVE_MESSAGES |
977 | MHD_DLOG (con->daemon, | 978 | MHD_DLOG (con->daemon, |
978 | "Error during select (%d): `%s'\n", | 979 | "Error during select (%d): `%s'\n", |
979 | MHD_socket_errno_, | 980 | err, |
980 | MHD_socket_last_strerr_ ()); | 981 | MHD_socket_strerr_ (err)); |
981 | #endif | 982 | #endif |
982 | break; | 983 | break; |
983 | } | 984 | } |
@@ -1039,7 +1040,7 @@ MHD_handle_connection (void *data) | |||
1039 | #endif | 1040 | #endif |
1040 | (NULL == tvp) ? -1 : tv.tv_sec * 1000) < 0) | 1041 | (NULL == tvp) ? -1 : tv.tv_sec * 1000) < 0) |
1041 | { | 1042 | { |
1042 | if (EINTR == MHD_socket_errno_) | 1043 | if (MHD_SCKT_LAST_ERR_IS_(MHD_SCKT_EINTR_)) |
1043 | continue; | 1044 | continue; |
1044 | #ifdef HAVE_MESSAGES | 1045 | #ifdef HAVE_MESSAGES |
1045 | MHD_DLOG (con->daemon, | 1046 | MHD_DLOG (con->daemon, |
@@ -1117,7 +1118,7 @@ recv_param_adapter (struct MHD_Connection *connection, | |||
1117 | if ( (MHD_INVALID_SOCKET == connection->socket_fd) || | 1118 | if ( (MHD_INVALID_SOCKET == connection->socket_fd) || |
1118 | (MHD_CONNECTION_CLOSED == connection->state) ) | 1119 | (MHD_CONNECTION_CLOSED == connection->state) ) |
1119 | { | 1120 | { |
1120 | MHD_set_socket_errno_ (ENOTCONN); | 1121 | MHD_socket_set_error_ (MHD_SCKT_ENOTCONN_); |
1121 | return -1; | 1122 | return -1; |
1122 | } | 1123 | } |
1123 | #ifdef MHD_POSIX_SOCKETS | 1124 | #ifdef MHD_POSIX_SOCKETS |
@@ -1133,7 +1134,7 @@ recv_param_adapter (struct MHD_Connection *connection, | |||
1133 | (_MHD_socket_funcs_size) i, | 1134 | (_MHD_socket_funcs_size) i, |
1134 | MSG_NOSIGNAL); | 1135 | MSG_NOSIGNAL); |
1135 | #if EPOLL_SUPPORT | 1136 | #if EPOLL_SUPPORT |
1136 | if ( (0 > ret) && (EAGAIN == MHD_socket_errno_) ) | 1137 | if ( (0 > ret) && (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ())) ) |
1137 | { | 1138 | { |
1138 | /* Got EAGAIN --- no longer read-ready */ | 1139 | /* Got EAGAIN --- no longer read-ready */ |
1139 | connection->epoll_state &= ~MHD_EPOLL_STATE_READ_READY; | 1140 | connection->epoll_state &= ~MHD_EPOLL_STATE_READ_READY; |
@@ -1157,6 +1158,7 @@ send_param_adapter (struct MHD_Connection *connection, | |||
1157 | size_t i) | 1158 | size_t i) |
1158 | { | 1159 | { |
1159 | ssize_t ret; | 1160 | ssize_t ret; |
1161 | int err; | ||
1160 | #if LINUX | 1162 | #if LINUX |
1161 | MHD_socket fd; | 1163 | MHD_socket fd; |
1162 | #endif | 1164 | #endif |
@@ -1164,7 +1166,7 @@ send_param_adapter (struct MHD_Connection *connection, | |||
1164 | if ( (MHD_INVALID_SOCKET == connection->socket_fd) || | 1166 | if ( (MHD_INVALID_SOCKET == connection->socket_fd) || |
1165 | (MHD_CONNECTION_CLOSED == connection->state) ) | 1167 | (MHD_CONNECTION_CLOSED == connection->state) ) |
1166 | { | 1168 | { |
1167 | MHD_set_socket_errno_ (ENOTCONN); | 1169 | MHD_socket_set_error_ (MHD_SCKT_ENOTCONN_); |
1168 | return -1; | 1170 | return -1; |
1169 | } | 1171 | } |
1170 | #ifdef MHD_POSIX_SOCKETS | 1172 | #ifdef MHD_POSIX_SOCKETS |
@@ -1189,7 +1191,6 @@ send_param_adapter (struct MHD_Connection *connection, | |||
1189 | /* can use sendfile */ | 1191 | /* can use sendfile */ |
1190 | uint64_t left; | 1192 | uint64_t left; |
1191 | uint64_t offsetu64; | 1193 | uint64_t offsetu64; |
1192 | int err; | ||
1193 | #ifndef HAVE_SENDFILE64 | 1194 | #ifndef HAVE_SENDFILE64 |
1194 | off_t offset; | 1195 | off_t offset; |
1195 | #else /* HAVE_SENDFILE64 */ | 1196 | #else /* HAVE_SENDFILE64 */ |
@@ -1198,7 +1199,7 @@ send_param_adapter (struct MHD_Connection *connection, | |||
1198 | offsetu64 = connection->response_write_position + connection->response->fd_off; | 1199 | offsetu64 = connection->response_write_position + connection->response->fd_off; |
1199 | left = connection->response->total_size - connection->response_write_position; | 1200 | left = connection->response->total_size - connection->response_write_position; |
1200 | ret = 0; | 1201 | ret = 0; |
1201 | MHD_set_socket_errno_(ENOMEM); | 1202 | MHD_socket_set_error_to_ENOMEM (); |
1202 | #ifndef HAVE_SENDFILE64 | 1203 | #ifndef HAVE_SENDFILE64 |
1203 | offset = (off_t) offsetu64; | 1204 | offset = (off_t) offsetu64; |
1204 | if ( (offsetu64 <= (uint64_t) OFF_T_MAX) && | 1205 | if ( (offsetu64 <= (uint64_t) OFF_T_MAX) && |
@@ -1212,17 +1213,17 @@ send_param_adapter (struct MHD_Connection *connection, | |||
1212 | /* write successful */ | 1213 | /* write successful */ |
1213 | return ret; | 1214 | return ret; |
1214 | } | 1215 | } |
1215 | err = MHD_socket_errno_; | 1216 | err = MHD_socket_get_error_(); |
1216 | #if EPOLL_SUPPORT | 1217 | #if EPOLL_SUPPORT |
1217 | if ( (0 > ret) && (EAGAIN == err) ) | 1218 | if ( (0 > ret) && (MHD_SCKT_ERR_IS_EAGAIN_(err)) ) |
1218 | { | 1219 | { |
1219 | /* EAGAIN --- no longer write-ready */ | 1220 | /* EAGAIN --- no longer write-ready */ |
1220 | connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY; | 1221 | connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY; |
1221 | } | 1222 | } |
1222 | #endif | 1223 | #endif |
1223 | if ( (EINTR == err) || (EAGAIN == err) || (EWOULDBLOCK == err) ) | 1224 | if (MHD_SCKT_ERR_IS_EINTR_ (err) || MHD_SCKT_ERR_IS_EAGAIN_ (err)) |
1224 | return 0; | 1225 | return 0; |
1225 | if (EBADF == err) | 1226 | if (MHD_SCKT_ERR_IS_(err, MHD_SCKT_EBADF_)) |
1226 | return -1; | 1227 | return -1; |
1227 | /* sendfile() failed with EINVAL if mmap()-like operations are not | 1228 | /* sendfile() failed with EINVAL if mmap()-like operations are not |
1228 | supported for FD or other 'unusual' errors occurred, so we should try | 1229 | supported for FD or other 'unusual' errors occurred, so we should try |
@@ -1232,8 +1233,9 @@ send_param_adapter (struct MHD_Connection *connection, | |||
1232 | } | 1233 | } |
1233 | #endif | 1234 | #endif |
1234 | ret = (ssize_t) send (connection->socket_fd, other, (_MHD_socket_funcs_size)i, MSG_NOSIGNAL); | 1235 | ret = (ssize_t) send (connection->socket_fd, other, (_MHD_socket_funcs_size)i, MSG_NOSIGNAL); |
1236 | err = MHD_socket_get_error_(); | ||
1235 | #if EPOLL_SUPPORT | 1237 | #if EPOLL_SUPPORT |
1236 | if ( (0 > ret) && (EAGAIN == MHD_socket_errno_) ) | 1238 | if ( (0 > ret) && (MHD_SCKT_ERR_IS_EAGAIN_(err)) ) |
1237 | { | 1239 | { |
1238 | /* EAGAIN --- no longer write-ready */ | 1240 | /* EAGAIN --- no longer write-ready */ |
1239 | connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY; | 1241 | connection->epoll_state &= ~MHD_EPOLL_STATE_WRITE_READY; |
@@ -1242,8 +1244,8 @@ send_param_adapter (struct MHD_Connection *connection, | |||
1242 | /* Handle broken kernel / libc, returning -1 but not setting errno; | 1244 | /* Handle broken kernel / libc, returning -1 but not setting errno; |
1243 | kill connection as that should be safe; reported on mailinglist here: | 1245 | kill connection as that should be safe; reported on mailinglist here: |
1244 | http://lists.gnu.org/archive/html/libmicrohttpd/2014-10/msg00023.html */ | 1246 | http://lists.gnu.org/archive/html/libmicrohttpd/2014-10/msg00023.html */ |
1245 | if ( (0 > ret) && (0 == MHD_socket_errno_) ) | 1247 | if ( (0 > ret) && (0 == err) ) |
1246 | MHD_set_socket_errno_(ECONNRESET); | 1248 | MHD_socket_set_error_ (MHD_SCKT_ECONNRESET_); |
1247 | return ret; | 1249 | return ret; |
1248 | } | 1250 | } |
1249 | 1251 | ||
@@ -1970,16 +1972,18 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
1970 | #endif /* ! USE_ACCEPT4 */ | 1972 | #endif /* ! USE_ACCEPT4 */ |
1971 | if ((MHD_INVALID_SOCKET == s) || (addrlen <= 0)) | 1973 | if ((MHD_INVALID_SOCKET == s) || (addrlen <= 0)) |
1972 | { | 1974 | { |
1973 | const int err = MHD_socket_errno_; | 1975 | const int err = MHD_socket_get_error_ (); |
1974 | /* This could be a common occurance with multiple worker threads */ | 1976 | /* This could be a common occurance with multiple worker threads */ |
1975 | if ( (EINVAL == err) && | 1977 | if ( (MHD_SCKT_ERR_IS_ (err, MHD_SCKT_EINVAL_)) && |
1976 | (MHD_INVALID_SOCKET == daemon->socket_fd) ) | 1978 | (MHD_INVALID_SOCKET == daemon->socket_fd) ) |
1977 | return MHD_NO; /* can happen during shutdown */ | 1979 | return MHD_NO; /* can happen during shutdown */ |
1980 | if (MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err)) | ||
1981 | return MHD_NO; /* do not print error if client just disconnected early */ | ||
1978 | #ifdef HAVE_MESSAGES | 1982 | #ifdef HAVE_MESSAGES |
1979 | if ((EAGAIN != err) && (EWOULDBLOCK != err)) | 1983 | if ( !MHD_SCKT_ERR_IS_EAGAIN_ (err) ) |
1980 | MHD_DLOG (daemon, | 1984 | MHD_DLOG (daemon, |
1981 | "Error accepting connection: %s\n", | 1985 | "Error accepting connection: %s\n", |
1982 | MHD_socket_last_strerr_ ()); | 1986 | MHD_socket_strerr_(err)); |
1983 | #endif | 1987 | #endif |
1984 | if (MHD_INVALID_SOCKET != s) | 1988 | if (MHD_INVALID_SOCKET != s) |
1985 | { | 1989 | { |
@@ -1987,10 +1991,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
1987 | MHD_PANIC ("close failed\n"); | 1991 | MHD_PANIC ("close failed\n"); |
1988 | /* just in case */ | 1992 | /* just in case */ |
1989 | } | 1993 | } |
1990 | if ( (EMFILE == err) || | 1994 | if ( MHD_SCKT_ERR_IS_LOW_RESOURCES_ (err) ) |
1991 | (ENFILE == err) || | ||
1992 | (ENOMEM == err) || | ||
1993 | (ENOBUFS == err) ) | ||
1994 | { | 1995 | { |
1995 | /* system/process out of resources */ | 1996 | /* system/process out of resources */ |
1996 | if (0 == daemon->connections) | 1997 | if (0 == daemon->connections) |
@@ -2430,12 +2431,13 @@ MHD_select (struct MHD_Daemon *daemon, | |||
2430 | return MHD_NO; | 2431 | return MHD_NO; |
2431 | if (num_ready < 0) | 2432 | if (num_ready < 0) |
2432 | { | 2433 | { |
2433 | if (EINTR == MHD_socket_errno_) | 2434 | const int err = MHD_socket_get_error_ (); |
2435 | if (MHD_SCKT_ERR_IS_EINTR_(err)) | ||
2434 | return (MHD_NO == err_state) ? MHD_YES : MHD_NO; | 2436 | return (MHD_NO == err_state) ? MHD_YES : MHD_NO; |
2435 | #ifdef HAVE_MESSAGES | 2437 | #ifdef HAVE_MESSAGES |
2436 | MHD_DLOG (daemon, | 2438 | MHD_DLOG (daemon, |
2437 | "select failed: %s\n", | 2439 | "select failed: %s\n", |
2438 | MHD_socket_last_strerr_ ()); | 2440 | MHD_socket_strerr_ (err)); |
2439 | #endif | 2441 | #endif |
2440 | return MHD_NO; | 2442 | return MHD_NO; |
2441 | } | 2443 | } |
@@ -2551,7 +2553,8 @@ MHD_poll_all (struct MHD_Daemon *daemon, | |||
2551 | } | 2553 | } |
2552 | if (MHD_sys_poll_(p, poll_server + num_connections, timeout) < 0) | 2554 | if (MHD_sys_poll_(p, poll_server + num_connections, timeout) < 0) |
2553 | { | 2555 | { |
2554 | if (EINTR == MHD_socket_errno_) | 2556 | const int err = MHD_socket_get_error_ (); |
2557 | if (MHD_SCKT_ERR_IS_EINTR_ (err)) | ||
2555 | { | 2558 | { |
2556 | free(p); | 2559 | free(p); |
2557 | return MHD_YES; | 2560 | return MHD_YES; |
@@ -2559,7 +2562,7 @@ MHD_poll_all (struct MHD_Daemon *daemon, | |||
2559 | #ifdef HAVE_MESSAGES | 2562 | #ifdef HAVE_MESSAGES |
2560 | MHD_DLOG (daemon, | 2563 | MHD_DLOG (daemon, |
2561 | "poll failed: %s\n", | 2564 | "poll failed: %s\n", |
2562 | MHD_socket_last_strerr_ ()); | 2565 | MHD_socket_strerr_ (err)); |
2563 | #endif | 2566 | #endif |
2564 | free(p); | 2567 | free(p); |
2565 | return MHD_NO; | 2568 | return MHD_NO; |
@@ -2645,12 +2648,13 @@ MHD_poll_listen_socket (struct MHD_Daemon *daemon, | |||
2645 | return MHD_YES; | 2648 | return MHD_YES; |
2646 | if (MHD_sys_poll_(p, poll_count, timeout) < 0) | 2649 | if (MHD_sys_poll_(p, poll_count, timeout) < 0) |
2647 | { | 2650 | { |
2648 | if (EINTR == MHD_socket_errno_) | 2651 | const int err = MHD_socket_get_error_ (); |
2652 | if (MHD_SCKT_ERR_IS_EINTR_ (err)) | ||
2649 | return MHD_YES; | 2653 | return MHD_YES; |
2650 | #ifdef HAVE_MESSAGES | 2654 | #ifdef HAVE_MESSAGES |
2651 | MHD_DLOG (daemon, | 2655 | MHD_DLOG (daemon, |
2652 | "poll failed: %s\n", | 2656 | "poll failed: %s\n", |
2653 | MHD_socket_last_strerr_ ()); | 2657 | MHD_socket_strerr_ (err)); |
2654 | #endif | 2658 | #endif |
2655 | return MHD_NO; | 2659 | return MHD_NO; |
2656 | } | 2660 | } |
@@ -2790,12 +2794,13 @@ MHD_epoll (struct MHD_Daemon *daemon, | |||
2790 | events, MAX_EVENTS, timeout_ms); | 2794 | events, MAX_EVENTS, timeout_ms); |
2791 | if (-1 == num_events) | 2795 | if (-1 == num_events) |
2792 | { | 2796 | { |
2793 | if (EINTR == MHD_socket_errno_) | 2797 | const int err = MHD_socket_get_error_ (); |
2798 | if (MHD_SCKT_ERR_IS_EINTR_ (err)) | ||
2794 | return MHD_YES; | 2799 | return MHD_YES; |
2795 | #ifdef HAVE_MESSAGES | 2800 | #ifdef HAVE_MESSAGES |
2796 | MHD_DLOG (daemon, | 2801 | MHD_DLOG (daemon, |
2797 | "Call to epoll_wait failed: %s\n", | 2802 | "Call to epoll_wait failed: %s\n", |
2798 | MHD_socket_last_strerr_ ()); | 2803 | MHD_socket_strerr_ (err)); |
2799 | #endif | 2804 | #endif |
2800 | return MHD_NO; | 2805 | return MHD_NO; |
2801 | } | 2806 | } |