diff options
author | Gerald Combs <gerald@wireshark.org> | 2018-12-03 17:08:26 +0000 |
---|---|---|
committer | Gerald Combs <gerald@wireshark.org> | 2018-12-03 17:08:39 +0000 |
commit | 46ee43aae2164048c8f0d7ee5d6ab22ea573d21d (patch) | |
tree | 459a7d2d022d1c5adef4c41d9019d60a1977c26f /epan/maxmind_db.c | |
parent | d8c1c228370c7faa1875d3e4764e8968e31b6514 (diff) |
Revert "maxmind: Simplify our read logic."
This reverts commit 4154e35cde9c863ace57598cabc37034dab2ab1c.
Apparently we do need to call PeekNamedPipe on Windows.
Change-Id: I9c9bbcb56bf1e1c2e6ae240ac5056b8a80674f15
Reviewed-on: https://code.wireshark.org/review/30900
Reviewed-by: Gerald Combs <gerald@wireshark.org>
Diffstat (limited to 'epan/maxmind_db.c')
-rw-r--r-- | epan/maxmind_db.c | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/epan/maxmind_db.c b/epan/maxmind_db.c index 661ed4f663..9170cb23a1 100644 --- a/epan/maxmind_db.c +++ b/epan/maxmind_db.c @@ -180,10 +180,23 @@ write_mmdbr_stdin_worker(gpointer sifd_data) { return NULL; } +static ssize_t mmdbr_pipe_read_one(char *ch_p) { + ssize_t status = -1; + g_rw_lock_reader_lock(&mmdbr_pipe_mtx); + if (ws_pipe_valid(&mmdbr_pipe) && ws_pipe_data_available(mmdbr_pipe.stdout_fd)) { + status = ws_read(mmdbr_pipe.stdout_fd, ch_p, 1); + } + g_rw_lock_reader_unlock(&mmdbr_pipe_mtx); + return status; +} + // We need to read a series of lines from mmdbresolve's stdout. Trying to -// use fgets is problematic because it blocks on Windows. Doing so in a -// thread is even worse since it locks the I/O stream and if the main +// use fgets is problematic because it blocks on Windows blocks. Doing so +// in a thread is even worse since it locks the I/O stream and if the main // thread calls fclose while fgets is blocking, it will block as well. +// +// Read our input one character at a time and only after we've ensured +// that data is available. #define MAX_MMDB_LINE_LEN 2000 static gpointer read_mmdbr_stdout_worker(gpointer data _U_) { @@ -199,17 +212,11 @@ read_mmdbr_stdout_worker(gpointer data _U_) { while (1) { // Start of line char cur_addr[WS_INET6_ADDRSTRLEN]; char ch; - ssize_t resp_status; + ssize_t status; g_string_truncate(line_buf, 0); - if (!mmdbr_pipe_valid()) { - // Should be due to mmdb_resolve_stop. - MMDB_DEBUG("invalid mmdbr stdout pipe. exiting thread."); - break; - } - - while((resp_status = ws_read(mmdbr_pipe.stdout_fd, &ch, 1)) == 1) { + while((status = mmdbr_pipe_read_one(&ch)) == 1) { if (ch == '\n') { break; } @@ -217,14 +224,21 @@ read_mmdbr_stdout_worker(gpointer data _U_) { g_string_append_c(line_buf, ch); if (line_buf->len > MAX_MMDB_LINE_LEN) { - MMDB_DEBUG("long line. marking invalid."); + MMDB_DEBUG("long line"); g_string_printf(line_buf, "%s", RES_INVALID_LINE); } } - if (resp_status != 1) { - MMDB_DEBUG("read error %s. exiting thread.", g_strerror(errno)); - break; + if (status != 1) { + if (!mmdbr_pipe_valid()) { + // Should be due to mmdb_resolve_stop. + MMDB_DEBUG("invalid mmdbr stdout pipe. exiting thread."); + break; + } + + MMDB_DEBUG("no pipe data"); + g_usleep(MMDB_WAIT_TIME); + continue; } char *line = g_strstrip(line_buf->str); |