Skip to content

Commit

Permalink
fix - reading from closed pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
okbob committed Jul 22, 2021
1 parent f2b7887 commit 7606e73
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
20 changes: 17 additions & 3 deletions src/inputs.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,23 @@ _get_pspg_event(NCursesEventData *nced,
{
short revents = fds[1].revents;

if (revents & POLLIN)
if (revents & POLLHUP)
{
/* The pipe cannot be reopened */
if (f_data_opts & STREAM_IS_PIPE)
{
log_row("detected POLLHUP on pipe");
return PSPG_FATAL_EVENT;
}

log_row("force close stream after POLLHUP");
close_data_stream();

/* we don't want to reopen stream too quickly, sleep 100ms */
usleep(1000 * 100);
return PSPG_READ_DATA_EVENT;
}
else if (revents & POLLIN)
{

#ifdef HAVE_INOTIFY
Expand All @@ -372,8 +388,6 @@ _get_pspg_event(NCursesEventData *nced,
*/
while (len > 0)
{


const struct inotify_event *ino_event = (struct inotify_event *) buff;

while (len > 0)
Expand Down
3 changes: 3 additions & 0 deletions src/pspg.c
Original file line number Diff line number Diff line change
Expand Up @@ -3140,6 +3140,9 @@ main(int argc, char *argv[])
{
event = get_pspg_event(&nced, false, opts.watch_time > 0 ? 1000 : -1);

if (event == PSPG_FATAL_EVENT)
break;

event_keycode = (event == PSPG_NCURSES_EVENT) ? nced.keycode : 0;

/*
Expand Down
14 changes: 7 additions & 7 deletions src/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ _getline(char **lineptr, size_t *n, FILE *fp, bool is_nonblocking, bool wait_on_

for (;;)
{
char *str;
char *str;
int len = 0;

errno = 0;
Expand Down Expand Up @@ -426,13 +426,9 @@ _getline(char **lineptr, size_t *n, FILE *fp, bool is_nonblocking, bool wait_on_
errno = _errno;
}

if (errno)
if (errno || feof(fp))
{
if (feof(fp))
{
goto endline_exit;
}
else if (errno == EAGAIN)
if (errno == EAGAIN)
{
struct pollfd fds[1];

Expand All @@ -453,6 +449,10 @@ _getline(char **lineptr, size_t *n, FILE *fp, bool is_nonblocking, bool wait_on_
clearerr(fp);
continue;
}
else if (feof(fp))
{
goto endline_exit;
}
else
{
free(dynbuf);
Expand Down

0 comments on commit 7606e73

Please sign in to comment.