This commit is contained in:
manojampalam 2016-03-03 19:29:46 -08:00
parent 1a66801a45
commit bc122a7472
4 changed files with 27 additions and 3 deletions

View File

@ -35,6 +35,12 @@ void file_blocking_io_tests()
TEST_START("pipe read and write"); TEST_START("pipe read and write");
r = pipeio[0]; r = pipeio[0];
w = pipeio[1]; 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)); ret = write(w, small_send_buf, strlen(small_send_buf));
ASSERT_INT_EQ(ret, strlen(small_send_buf)); ASSERT_INT_EQ(ret, strlen(small_send_buf));
ret = read(r, small_recv_buf, SMALL_RECV_BUF_SIZE); ret = read(r, small_recv_buf, SMALL_RECV_BUF_SIZE);

View File

@ -91,7 +91,9 @@ int fileio_pipe(struct w32_io* pio[2]) {
memset(pio_write, 0, sizeof(struct w32_io)); memset(pio_write, 0, sizeof(struct w32_io));
pio_read->handle = read_handle; pio_read->handle = read_handle;
pio_read->internal.state = PIPE_READ_END;
pio_write->handle = write_handle; pio_write->handle = write_handle;
pio_write->internal.state = PIPE_WRITE_END;
pio[0] = pio_read; pio[0] = pio_read;
pio[1] = pio_write; 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 fileio_read(struct w32_io* pio, void *dst, unsigned int max) {
int bytes_copied; int bytes_copied;
if ((pio->type == PIPE_FD) && (pio->internal.state == PIPE_WRITE_END)) {
errno = EBADF;
return -1;
}
//if read is pending //if read is pending
if (pio->read_details.pending) { if (pio->read_details.pending) {
errno = EAGAIN; errno = EAGAIN;
@ -343,6 +350,11 @@ VOID CALLBACK WriteCompletionRoutine(
int fileio_write(struct w32_io* pio, const void *buf, unsigned int max) { int fileio_write(struct w32_io* pio, const void *buf, unsigned int max) {
int bytes_copied; int bytes_copied;
if ((pio->type == PIPE_FD) && (pio->internal.state == PIPE_READ_END)) {
errno = EBADF;
return -1;
}
if (pio->write_details.pending) { if (pio->write_details.pending) {
if (w32_io_is_blocking(pio)) 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) { 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)) if (!pio->read_details.pending && !fileio_is_io_available(pio, rd))
return fileio_ReadFileEx(pio); return fileio_ReadFileEx(pio);
return 0;
} }
@ -489,6 +502,7 @@ int fileio_close(struct w32_io* pio) {
CancelIo(pio->handle); CancelIo(pio->handle);
//let queued APCs (if any) drain //let queued APCs (if any) drain
SleepEx(0, TRUE); SleepEx(0, TRUE);
if (pio->type != STD_IO_FD) //STD handles are never explicitly closed
CloseHandle(pio->handle); CloseHandle(pio->handle);
if (pio->read_details.buf) if (pio->read_details.buf)

View File

@ -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) { int w32_send(int fd, const void *buf, size_t len, int flags) {
debug3("fd:%d", fd); debug3("fd:%d", fd);
CHECK_FD(fd); CHECK_FD(fd);
CHECK_SOCK_IO(fd_table.w32_ios[fd]);
return socketio_send(fd_table.w32_ios[fd], buf, len, flags); return socketio_send(fd_table.w32_ios[fd], buf, len, flags);
} }

View File

@ -21,6 +21,11 @@ enum w32_io_sock_state {
SOCK_CONNECTED = 4 SOCK_CONNECTED = 4
}; };
enum w32_io_pipe_state {
PIPE_READ_END = 1,
PIPE_WRITE_END = 2
};
struct w32_io { struct w32_io {
OVERLAPPED read_overlapped; OVERLAPPED read_overlapped;
OVERLAPPED write_overlapped; OVERLAPPED write_overlapped;