Commit 566f9616 authored by Pavan Balaji's avatar Pavan Balaji
Browse files

[svn-r6550] Bug fix in the stdin reading code. Try reading from stdin; if we

can't, disable it.
parent 9ccdb3f7
......@@ -20,28 +20,24 @@ HYD_status HYDT_dmx_init(char **demux)
if (!(*demux)) { /* user didn't specify anything */
#if defined HAVE_POLL
HYDT_dmxu_fns.wait_for_event = HYDT_dmxu_poll_wait_for_event;
HYDT_dmxu_fns.stdin_valid = HYDT_dmxu_poll_stdin_valid;
*demux = HYDU_strdup("poll");
#elif defined HAVE_SELECT
HYDT_dmxu_fns.wait_for_event = HYDT_dmxu_select_wait_for_event;
HYDT_dmxu_fns.stdin_valid = HYDT_dmxu_select_stdin_valid;
*demux = HYDU_strdup("select");
#endif /* HAVE_SELECT */
}
else if (!strcmp(*demux, "poll")) { /* user wants to use poll */
#if defined HAVE_POLL
HYDT_dmxu_fns.wait_for_event = HYDT_dmxu_poll_wait_for_event;
HYDT_dmxu_fns.stdin_valid = HYDT_dmxu_poll_stdin_valid;
#endif /* HAVE_POLL */
}
else if (!strcmp(*demux, "select")) { /* user wants to use select */
#if defined HAVE_SELECT
HYDT_dmxu_fns.wait_for_event = HYDT_dmxu_select_wait_for_event;
HYDT_dmxu_fns.stdin_valid = HYDT_dmxu_select_stdin_valid;
#endif /* HAVE_SELECT */
}
if (HYDT_dmxu_fns.wait_for_event == NULL || HYDT_dmxu_fns.stdin_valid == NULL) {
if (HYDT_dmxu_fns.wait_for_event == NULL) {
/* We couldn't find anything; return an error */
HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR,
"cannot find an appropriate demux engine\n");
......@@ -201,5 +197,21 @@ HYD_status HYDT_dmx_finalize(void)
HYD_status HYDT_dmx_stdin_valid(int *out)
{
return HYDT_dmxu_fns.stdin_valid(out);
char buf[1];
HYD_status status = HYD_SUCCESS;
HYDU_FUNC_ENTER();
/* Try to read from stdin. If we can't read it, disable stdin */
if (read(STDIN_FILENO, buf, 0) < 0)
*out = 0;
else
*out = 1;
fn_exit:
HYDU_FUNC_EXIT();
return status;
fn_fail:
goto fn_exit;
}
......@@ -25,17 +25,14 @@ extern struct HYDT_dmxu_callback *HYDT_dmxu_cb_list;
struct HYDT_dmxu_fns {
HYD_status(*wait_for_event) (int wtime);
HYD_status(*stdin_valid) (int *out);
};
#if defined HAVE_POLL
HYD_status HYDT_dmxu_poll_wait_for_event(int wtime);
HYD_status HYDT_dmxu_poll_stdin_valid(int *out);
#endif /* HAVE_POLL */
#if defined HAVE_SELECT
HYD_status HYDT_dmxu_select_wait_for_event(int wtime);
HYD_status HYDT_dmxu_select_stdin_valid(int *out);
#endif /* HAVE_SELECT */
#endif /* DEMUX_INTERNAL_H_INCLUDED */
......@@ -99,32 +99,3 @@ HYD_status HYDT_dmxu_poll_wait_for_event(int wtime)
fn_fail:
goto fn_exit;
}
HYD_status HYDT_dmxu_poll_stdin_valid(int *out)
{
struct pollfd fd[1];
int ret;
HYD_status status = HYD_SUCCESS;
HYDU_FUNC_ENTER();
fd[0].fd = STDIN_FILENO;
fd[0].events = POLLIN;
/* Check if poll on stdin returns any errors; on Darwin this is
* broken */
ret = poll(fd, 1, 0);
HYDU_ASSERT((ret >= 0), status);
if (fd[0].revents & ~POLLIN)
*out = 0;
else
*out = 1;
fn_exit:
HYDU_FUNC_EXIT();
return status;
fn_fail:
goto fn_exit;
}
......@@ -91,32 +91,3 @@ HYD_status HYDT_dmxu_select_wait_for_event(int wtime)
fn_fail:
goto fn_exit;
}
HYD_status HYDT_dmxu_select_stdin_valid(int *out)
{
fd_set exceptfds;
int ret;
struct timeval zero = { 0, 0 };
HYD_status status = HYD_SUCCESS;
HYDU_FUNC_ENTER();
FD_ZERO(&exceptfds);
FD_SET(STDIN_FILENO, &exceptfds);
/* Check if select on stdin returns any errors */
ret = select(STDIN_FILENO + 1, NULL, NULL, &exceptfds, &zero);
HYDU_ASSERT((ret >= 0), status);
if (FD_ISSET(STDIN_FILENO, &exceptfds))
*out = 0;
else
*out = 1;
fn_exit:
HYDU_FUNC_EXIT();
return status;
fn_fail:
goto fn_exit;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment