From 5dc2fdac15fd470e9bdf915628061312fdb689e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20H=C3=A5llberg?= Date: Sat, 17 Jan 2009 00:50:23 +0100 Subject: properly handle when tty writes block; fixes possible lock-up introduced by 277a06dbb45f4f249bb3f09d524ae064c842b7c5 diff --git a/tty.c b/tty.c index b0883b9..5e4ff7a 100644 --- a/tty.c +++ b/tty.c @@ -959,9 +959,28 @@ void tty_flush __P ((void)) char *data = tty_write_state.data; while (n > 0) { ssize_t r; - do { + for (;;) { r = write(tty_write_state.fd, data, n); - } while (r < 0 && (errno == EAGAIN || errno == EINTR)); + if (r >= 0) + break; + if (errno == EINTR) + continue; + if (errno != EAGAIN) { + fprintf(stderr, "Cannot write to tty: %s\n", strerror(errno)); + abort(); + } + fd_set wfds; + FD_ZERO(&wfds); + FD_SET(tty_write_state.fd, &wfds); + do { + r = select(tty_write_state.fd + 1, NULL, &wfds, NULL, NULL); + } while (r < 0 && errno == EINTR); + if (r <= 0) { + fprintf(stderr, "Cannot write to tty; select failed: %s\n", + r == 0 ? "returned zero" : strerror(errno)); + abort(); + } + } if (r < 0) { fprintf(stderr, "Cannot write to tty: %s\n", strerror(errno)); abort(); -- cgit v0.10.2