diff options
author | Ivan Zhakov <ivan@apache.org> | 2022-02-15 13:05:37 +0000 |
---|---|---|
committer | Ivan Zhakov <ivan@apache.org> | 2022-02-15 13:05:37 +0000 |
commit | ee6ba053c3b032b1ac7c4c515af6a32b0090c04b (patch) | |
tree | 0c918c8508555eb177419aea175c19bb78df6086 /file_io/win32 | |
parent | 388b2eab44cd1afe6f7af226cc894a15af4ff398 (diff) | |
download | apr-ee6ba053c3b032b1ac7c4c515af6a32b0090c04b.tar.gz |
On '1.8.x-r1897895' branch: Backport r1897895 from trunk.1.8.x-r1897895
git-svn-id: https://svn.apache.org/repos/asf/apr/apr/branches/1.8.x-r1897895@1898104 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'file_io/win32')
-rw-r--r-- | file_io/win32/pipe.c | 208 | ||||
-rw-r--r-- | file_io/win32/readwrite.c | 42 |
2 files changed, 5 insertions, 245 deletions
diff --git a/file_io/win32/pipe.c b/file_io/win32/pipe.c index 7dcbfb00e..e898b2d13 100644 --- a/file_io/win32/pipe.c +++ b/file_io/win32/pipe.c @@ -272,211 +272,3 @@ APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file, { return apr_os_pipe_put_ex(file, thefile, 0, pool); } - -static apr_status_t create_socket_pipe(SOCKET *rd, SOCKET *wr) -{ - static int id = 0; - FD_SET rs; - SOCKET ls; - struct timeval socktm; - struct sockaddr_in pa; - struct sockaddr_in la; - struct sockaddr_in ca; - int nrd; - apr_status_t rv = APR_SUCCESS; - int ll = sizeof(la); - int lc = sizeof(ca); - unsigned long bm = 1; - int uid[2]; - int iid[2]; - - *rd = INVALID_SOCKET; - *wr = INVALID_SOCKET; - - /* Create the unique socket identifier - * so that we know the connection originated - * from us. - */ - uid[0] = getpid(); - uid[1] = id++; - if ((ls = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) { - return apr_get_netos_error(); - } - - pa.sin_family = AF_INET; - pa.sin_port = 0; - pa.sin_addr.s_addr = inet_addr("127.0.0.1"); - - if (bind(ls, (SOCKADDR *)&pa, sizeof(pa)) == SOCKET_ERROR) { - rv = apr_get_netos_error(); - goto cleanup; - } - if (getsockname(ls, (SOCKADDR *)&la, &ll) == SOCKET_ERROR) { - rv = apr_get_netos_error(); - goto cleanup; - } - if (listen(ls, 1) == SOCKET_ERROR) { - rv = apr_get_netos_error(); - goto cleanup; - } - if ((*wr = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) { - rv = apr_get_netos_error(); - goto cleanup; - } - if (connect(*wr, (SOCKADDR *)&la, sizeof(la)) == SOCKET_ERROR) { - rv = apr_get_netos_error(); - goto cleanup; - } - if (send(*wr, (char *)uid, sizeof(uid), 0) != sizeof(uid)) { - if ((rv = apr_get_netos_error()) == 0) { - rv = APR_EINVAL; - } - goto cleanup; - } - if (ioctlsocket(ls, FIONBIO, &bm) == SOCKET_ERROR) { - rv = apr_get_netos_error(); - goto cleanup; - } - for (;;) { - int ns; - int nc = 0; - /* Listening socket is nonblocking by now. - * The accept should create the socket - * immediatelly because we are connected already. - * However on buys systems this can take a while - * until winsock gets a chance to handle the events. - */ - FD_ZERO(&rs); - FD_SET(ls, &rs); - - socktm.tv_sec = 1; - socktm.tv_usec = 0; - if ((ns = select(0, &rs, NULL, NULL, &socktm)) == SOCKET_ERROR) { - /* Accept still not signaled */ - Sleep(100); - continue; - } - if (ns == 0) { - /* No connections in the last second */ - continue; - } - if ((*rd = accept(ls, (SOCKADDR *)&ca, &lc)) == INVALID_SOCKET) { - rv = apr_get_netos_error(); - goto cleanup; - } - /* Verify the connection by reading/waiting for the identification */ - bm = 0; - if (ioctlsocket(*rd, FIONBIO, &bm) == SOCKET_ERROR) { - rv = apr_get_netos_error(); - goto cleanup; - } - nrd = recv(*rd, (char *)iid, sizeof(iid), 0); - if (nrd == SOCKET_ERROR) { - rv = apr_get_netos_error(); - goto cleanup; - } - if (nrd == (int)sizeof(uid) && memcmp(iid, uid, sizeof(uid)) == 0) { - /* Got the right identifier, put the poll()able read side of - * the pipe in nonblocking mode and return. - */ - bm = 1; - if (ioctlsocket(*rd, FIONBIO, &bm) == SOCKET_ERROR) { - rv = apr_get_netos_error(); - goto cleanup; - } - break; - } - closesocket(*rd); - } - /* We don't need the listening socket any more */ - closesocket(ls); - return 0; - -cleanup: - /* Don't leak resources */ - closesocket(ls); - if (*rd != INVALID_SOCKET) - closesocket(*rd); - if (*wr != INVALID_SOCKET) - closesocket(*wr); - - *rd = INVALID_SOCKET; - *wr = INVALID_SOCKET; - return rv; -} - -static apr_status_t socket_pipe_cleanup(void *thefile) -{ - apr_file_t *file = thefile; - if (file->filehand != INVALID_HANDLE_VALUE) { - shutdown((SOCKET)file->filehand, SD_BOTH); - closesocket((SOCKET)file->filehand); - file->filehand = INVALID_HANDLE_VALUE; - } - return APR_SUCCESS; -} - -apr_status_t apr_file_socket_pipe_create(apr_file_t **in, - apr_file_t **out, - apr_pool_t *p) -{ - apr_status_t rv; - SOCKET rd; - SOCKET wr; - - if ((rv = create_socket_pipe(&rd, &wr)) != APR_SUCCESS) { - return rv; - } - (*in) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t)); - (*in)->pool = p; - (*in)->fname = NULL; - (*in)->ftype = APR_FILETYPE_SOCKET; - (*in)->timeout = 0; /* read end of the pipe is non-blocking */ - (*in)->ungetchar = -1; - (*in)->eof_hit = 0; - (*in)->filePtr = 0; - (*in)->bufpos = 0; - (*in)->dataRead = 0; - (*in)->direction = 0; - (*in)->pOverlapped = NULL; - (*in)->filehand = (HANDLE)rd; - - (*out) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t)); - (*out)->pool = p; - (*out)->fname = NULL; - (*out)->ftype = APR_FILETYPE_SOCKET; - (*out)->timeout = -1; - (*out)->ungetchar = -1; - (*out)->eof_hit = 0; - (*out)->filePtr = 0; - (*out)->bufpos = 0; - (*out)->dataRead = 0; - (*out)->direction = 0; - (*out)->pOverlapped = NULL; - (*out)->filehand = (HANDLE)wr; - - apr_pool_cleanup_register(p, (void *)(*in), socket_pipe_cleanup, - apr_pool_cleanup_null); - apr_pool_cleanup_register(p, (void *)(*out), socket_pipe_cleanup, - apr_pool_cleanup_null); - - return rv; -} - -apr_status_t apr_file_socket_pipe_close(apr_file_t *file) -{ - apr_status_t stat; - if (file->ftype != APR_FILETYPE_SOCKET) - return apr_file_close(file); - if ((stat = socket_pipe_cleanup(file)) == APR_SUCCESS) { - apr_pool_cleanup_kill(file->pool, file, socket_pipe_cleanup); - - if (file->mutex) { - apr_thread_mutex_destroy(file->mutex); - } - - return APR_SUCCESS; - } - return stat; -} - diff --git a/file_io/win32/readwrite.c b/file_io/win32/readwrite.c index bd4e87c9e..79a4b2b17 100644 --- a/file_io/win32/readwrite.c +++ b/file_io/win32/readwrite.c @@ -20,7 +20,6 @@ #include "apr_strings.h" #include "apr_lib.h" #include "apr_errno.h" -#include "apr_arch_networkio.h" #include "apr_arch_atime.h" #include "apr_arch_misc.h" @@ -75,23 +74,8 @@ static apr_status_t read_with_timeout(apr_file_t *file, void *buf, apr_size_t le file->pOverlapped->OffsetHigh = (DWORD)(file->filePtr >> 32); } - if (file->ftype == APR_FILETYPE_SOCKET) { - WSABUF wsaData; - DWORD flags = 0; - - wsaData.buf = (char*) buf; - wsaData.len = (u_long)len; - if (WSARecv((SOCKET)file->filehand, &wsaData, 1, &bytesread, - &flags, NULL, NULL) == SOCKET_ERROR) { - rv = apr_get_netos_error(); - bytesread = 0; - } - else { - rv = APR_SUCCESS; - } - } - else if (ReadFile(file->filehand, buf, len, - &bytesread, file->pOverlapped)) { + if (ReadFile(file->filehand, buf, len, + &bytesread, file->pOverlapped)) { rv = APR_SUCCESS; } else { @@ -315,25 +299,9 @@ APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, a if (thefile->flags & APR_FOPEN_XTHREAD) { apr_thread_mutex_unlock(thefile->mutex); } - } - else if (thefile->ftype == APR_FILETYPE_SOCKET) { - WSABUF wsaData; - DWORD flags = 0; - - wsaData.buf = (char*) buf; - wsaData.len = (u_long)*nbytes; - if (WSASend((SOCKET)thefile->filehand, &wsaData, 1, &bwrote, - flags, NULL, NULL) == SOCKET_ERROR) { - rv = apr_get_netos_error(); - bwrote = 0; - } - else { - rv = APR_SUCCESS; - } - *nbytes = bwrote; - } - else { - if (thefile->ftype != APR_FILETYPE_FILE) { + return rv; + } else { + if (thefile->ftype == APR_FILETYPE_PIPE) { rv = WriteFile(thefile->filehand, buf, (DWORD)*nbytes, &bwrote, thefile->pOverlapped); } |