tty-handler: unify error handling for poller failures

We currently have a bug where we don't unregister from the ringbuffer
poller if we get an error on read(). This change unifies the
error-handling for potential failure cases in the poller handler, and
ensures that we unregister the ringbuffer poller and console poller in
all cases.

Change-Id: If354dd3c7f2a1ec534c6b42020fd9978e53cb20d
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/tty-handler.c b/tty-handler.c
index cd1e0e8..bc10fd9 100644
--- a/tty-handler.c
+++ b/tty-handler.c
@@ -163,11 +163,8 @@
 
 	if (events & POLLIN) {
 		len = read(th->fd, buf, sizeof(buf));
-		if (len <= 0) {
-			th->poller = NULL;
-			close(th->fd);
-			return POLLER_REMOVE;
-		}
+		if (len <= 0)
+			goto err;
 
 		console_data_out(th->console, buf, len);
 	}
@@ -175,13 +172,17 @@
 	if (events & POLLOUT) {
 		tty_set_blocked(th, false);
 		rc = tty_drain_queue(th, 0);
-		if (rc) {
-			ringbuffer_consumer_unregister(th->rbc);
-			return POLLER_REMOVE;
-		}
+		if (rc)
+			goto err;
 	}
 
 	return POLLER_OK;
+
+err:
+	th->poller = NULL;
+	close(th->fd);
+	ringbuffer_consumer_unregister(th->rbc);
+	return POLLER_REMOVE;
 }
 
 static int baud_string_to_speed(speed_t *speed, const char *baud_string) {