From bc122a7472e98b3396b54761ca995622e8c4552c Mon Sep 17 00:00:00 2001 From: manojampalam Date: Thu, 3 Mar 2016 19:29:46 -0800 Subject: [PATCH] 3-3 C1 --- .../win32posix/UnitTests/file_tests.c | 6 ++++++ .../win32posix/win32posix/fileio.c | 18 ++++++++++++++++-- .../win32posix/win32posix/w32fd.c | 1 - .../win32posix/win32posix/w32fd.h | 5 +++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/contrib/win32/w32-posix-prototype/win32posix/UnitTests/file_tests.c b/contrib/win32/w32-posix-prototype/win32posix/UnitTests/file_tests.c index ff39445..ff6ae40 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/UnitTests/file_tests.c +++ b/contrib/win32/w32-posix-prototype/win32posix/UnitTests/file_tests.c @@ -35,6 +35,12 @@ void file_blocking_io_tests() TEST_START("pipe read and write"); r = pipeio[0]; w = pipeio[1]; + ret = write(r, small_send_buf, strlen(small_send_buf)); + ASSERT_INT_EQ(ret, -1); + ASSERT_INT_EQ(errno, EBADF); + ret = read(w, small_recv_buf, SMALL_RECV_BUF_SIZE); + ASSERT_INT_EQ(ret, -1); + ASSERT_INT_EQ(errno, EBADF); ret = write(w, small_send_buf, strlen(small_send_buf)); ASSERT_INT_EQ(ret, strlen(small_send_buf)); ret = read(r, small_recv_buf, SMALL_RECV_BUF_SIZE); diff --git a/contrib/win32/w32-posix-prototype/win32posix/win32posix/fileio.c b/contrib/win32/w32-posix-prototype/win32posix/win32posix/fileio.c index fd71cdb..72e3518 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/win32posix/fileio.c +++ b/contrib/win32/w32-posix-prototype/win32posix/win32posix/fileio.c @@ -91,7 +91,9 @@ int fileio_pipe(struct w32_io* pio[2]) { memset(pio_write, 0, sizeof(struct w32_io)); pio_read->handle = read_handle; + pio_read->internal.state = PIPE_READ_END; pio_write->handle = write_handle; + pio_write->internal.state = PIPE_WRITE_END; pio[0] = pio_read; pio[1] = pio_write; @@ -269,6 +271,11 @@ int fileio_ReadFileEx(struct w32_io* pio) { int fileio_read(struct w32_io* pio, void *dst, unsigned int max) { int bytes_copied; + if ((pio->type == PIPE_FD) && (pio->internal.state == PIPE_WRITE_END)) { + errno = EBADF; + return -1; + } + //if read is pending if (pio->read_details.pending) { errno = EAGAIN; @@ -343,6 +350,11 @@ VOID CALLBACK WriteCompletionRoutine( int fileio_write(struct w32_io* pio, const void *buf, unsigned int max) { int bytes_copied; + if ((pio->type == PIPE_FD) && (pio->internal.state == PIPE_READ_END)) { + errno = EBADF; + return -1; + } + if (pio->write_details.pending) { if (w32_io_is_blocking(pio)) { @@ -431,7 +443,7 @@ int fileio_fstat(struct w32_io* pio, struct stat *buf) { int fileio_isatty(struct w32_io* pio) { - return (pio->type == CONSOLE_FD) ? TRUE : FALSE; + return (GetFileType(pio->handle) == FILE_TYPE_CHAR) ? TRUE : FALSE; } @@ -481,6 +493,7 @@ int fileio_on_select(struct w32_io* pio, BOOL rd) { if (!pio->read_details.pending && !fileio_is_io_available(pio, rd)) return fileio_ReadFileEx(pio); + return 0; } @@ -489,7 +502,8 @@ int fileio_close(struct w32_io* pio) { CancelIo(pio->handle); //let queued APCs (if any) drain SleepEx(0, TRUE); - CloseHandle(pio->handle); + if (pio->type != STD_IO_FD) //STD handles are never explicitly closed + CloseHandle(pio->handle); if (pio->read_details.buf) free(pio->read_details.buf); diff --git a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c index ae566f6..7b56aa2 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c +++ b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c @@ -229,7 +229,6 @@ int w32_recv(int fd, void *buf, size_t len, int flags) { int w32_send(int fd, const void *buf, size_t len, int flags) { debug3("fd:%d", fd); CHECK_FD(fd); - CHECK_SOCK_IO(fd_table.w32_ios[fd]); return socketio_send(fd_table.w32_ios[fd], buf, len, flags); } diff --git a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.h b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.h index 653dbf1..2881da2 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.h +++ b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.h @@ -21,6 +21,11 @@ enum w32_io_sock_state { SOCK_CONNECTED = 4 }; +enum w32_io_pipe_state { + PIPE_READ_END = 1, + PIPE_WRITE_END = 2 +}; + struct w32_io { OVERLAPPED read_overlapped; OVERLAPPED write_overlapped;