From 6a949ed1556f239a84a7ccb8d3b207f99cdf71c8 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Sat, 23 Dec 2017 00:05:21 -0800 Subject: Put special pipe-handling code into libwsutil. Ask, in a comment, why we're doing PeekNamedPipe() when we're trying to read everyting in the pipe, up to the EOF, into a string. On UN*X, do the same "read up to an EOF and then NUL-terminate the result" stuff that we did on Windows; nothing guarantees that, on all UN*Xes, in all circumstances, until the end of time, world without end, amen, we can do one read and get the entire string. Change-Id: I578802b23fec1051139eaefd9a09fe2a6de06a11 Reviewed-on: https://code.wireshark.org/review/24959 Petri-Dish: Guy Harris Tested-by: Petri Dish Buildbot Reviewed-by: Guy Harris --- capchild/capture_sync.c | 41 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 33 deletions(-) (limited to 'capchild') diff --git a/capchild/capture_sync.c b/capchild/capture_sync.c index e8d6fcf10b..43fc2ac173 100644 --- a/capchild/capture_sync.c +++ b/capchild/capture_sync.c @@ -100,7 +100,7 @@ #include /* For spawning child process */ #endif - +#include #ifdef _WIN32 static void create_dummy_signal_pipe(); @@ -1524,37 +1524,12 @@ pipe_read_bytes(int pipe_fd, char *bytes, int required, char **msg) return offset; } -static gboolean pipe_data_available(int pipe_fd) { -#ifdef _WIN32 /* PeekNamedPipe */ - HANDLE hPipe = (HANDLE) _get_osfhandle(pipe_fd); - DWORD bytes_avail; - - if (hPipe == INVALID_HANDLE_VALUE) - return FALSE; - - if (! PeekNamedPipe(hPipe, NULL, 0, NULL, &bytes_avail, NULL)) - return FALSE; - - if (bytes_avail > 0) - return TRUE; - return FALSE; -#else /* select */ - fd_set rfds; - struct timeval timeout; - - FD_ZERO(&rfds); - FD_SET(pipe_fd, &rfds); - timeout.tv_sec = 0; - timeout.tv_usec = 0; - - if (select(pipe_fd+1, &rfds, NULL, NULL, &timeout) > 0) - return TRUE; - - return FALSE; -#endif -} - -/* Read a line from a pipe, similar to fgets */ +/* + * Read a line from a pipe; similar to fgets, but doesn't block. + * + * XXX - just stops reading if there's nothing to be read right now; + * that could conceivably mean that you don't get a complete line. + */ int sync_pipe_gets_nonblock(int pipe_fd, char *bytes, int max) { ssize_t newly; @@ -1562,7 +1537,7 @@ sync_pipe_gets_nonblock(int pipe_fd, char *bytes, int max) { while(offset < max - 1) { offset++; - if (! pipe_data_available(pipe_fd)) + if (! ws_pipe_data_available(pipe_fd)) break; newly = ws_read(pipe_fd, &bytes[offset], 1); if (newly == 0) { -- cgit v1.2.3