Skip to content

Commit 6fe3afb

Browse files
committed
write_callback: only enable /dev/tty reader if the command is running
This fixes a hang when there is /dev/tty data in a buffer to be flushed by the final call to del_io_events(). We do not want to re-enable the reader when flushing the buffers as part of pty_finish(). See PR #247 for analysis of the problem and how to reproduce it. --HG-- branch : 1.9
1 parent fa2ed7d commit 6fe3afb

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

src/exec_nopty.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,15 @@ write_callback(int fd, int what, void *v)
447447
ev_free_by_fd(evbase, fd);
448448
}
449449
}
450-
/* Enable reader if buffer is not full. */
450+
/*
451+
* Enable reader if buffer is not full but avoid reading
452+
* /dev/tty if the command is no longer running.
453+
*/
451454
if (iob->revent != NULL && iob->len != sizeof(iob->buf)) {
452-
if (sudo_ev_add(evbase, iob->revent, NULL, false) == -1)
453-
sudo_fatal("%s", U_("unable to add event to queue"));
455+
if (!USERTTY_EVENT(iob->revent) || iob->ec->cmnd_pid != -1) {
456+
if (sudo_ev_add(evbase, iob->revent, NULL, false) == -1)
457+
sudo_fatal("%s", U_("unable to add event to queue"));
458+
}
454459
}
455460
}
456461

src/exec_pty.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,13 @@ write_callback(int fd, int what, void *v)
469469
ev_free_by_fd(evbase, fd);
470470
}
471471
}
472-
/* Enable reader if buffer is not full. */
473-
if (iob->revent != NULL &&
474-
(ttymode == TERM_RAW || !USERTTY_EVENT(iob->revent))) {
475-
if (iob->len != sizeof(iob->buf)) {
472+
/*
473+
* Enable reader if buffer is not full but avoid reading /dev/tty
474+
* if not in raw mode or the command is no longer running.
475+
*/
476+
if (iob->revent != NULL && iob->len != sizeof(iob->buf)) {
477+
if (!USERTTY_EVENT(iob->revent) ||
478+
(ttymode == TERM_RAW && iob->ec->cmnd_pid != -1)) {
476479
if (sudo_ev_add(evbase, iob->revent, NULL, false) == -1)
477480
sudo_fatal("%s", U_("unable to add event to queue"));
478481
}

0 commit comments

Comments
 (0)