ipa: Allow signalling fd destroyed in ipa_client_read

Similar to what we do in other osmo_fd cb, check for read_cb returning
-EBADF and in that case don't attempt using the osmo_fd anymore, either
because the fd was already closed (no need to then signal WRITE) or
because osmo_fd struct itself has been freed.

Change-Id: I0e449a2bdf7f0411feeccd262cd731ca6fba3fc1
diff --git a/src/input/ipa.c b/src/input/ipa.c
index 554644c..d0363e2 100644
--- a/src/input/ipa.c
+++ b/src/input/ipa.c
@@ -47,7 +47,7 @@
 	link->pending_msg = NULL;
 }
 
-static void ipa_client_read(struct ipa_client_conn *link)
+static int ipa_client_read(struct ipa_client_conn *link)
 {
 	struct osmo_fd *ofd = link->ofd;
 	struct msgb *msg;
@@ -58,7 +58,7 @@
 	ret = ipa_msg_recv_buffered(ofd->fd, &msg, &link->pending_msg);
 	if (ret <= 0) {
 		if (ret == -EAGAIN)
-			return;
+			return 0;
 		else if (ret == -EPIPE || ret == -ECONNRESET)
 			LOGIPA(link, LOGL_ERROR, "lost connection with server\n");
 		else if (ret == 0)
@@ -66,10 +66,11 @@
 		ipa_client_conn_close(link);
 		if (link->updown_cb)
 			link->updown_cb(link, 0);
-		return;
+		return -EBADF;
 	}
 	if (link->read_cb)
-		link->read_cb(link, msg);
+		return link->read_cb(link, msg);
+	return 0;
 }
 
 static void ipa_client_write(struct ipa_client_conn *link)
@@ -111,7 +112,7 @@
 static int ipa_client_fd_cb(struct osmo_fd *ofd, unsigned int what)
 {
 	struct ipa_client_conn *link = ofd->data;
-	int error, ret;
+	int error, ret = 0;
 	socklen_t len = sizeof(error);
 
 	switch(link->state) {
@@ -132,9 +133,9 @@
 	case IPA_CLIENT_LINK_STATE_CONNECTED:
 		if (what & BSC_FD_READ) {
 			LOGIPA(link, LOGL_DEBUG, "connected read\n");
-			ipa_client_read(link);
+			ret = ipa_client_read(link);
 		}
-		if (what & BSC_FD_WRITE) {
+		if (ret != -EBADF && (what & BSC_FD_WRITE)) {
 			LOGIPA(link, LOGL_DEBUG, "connected write\n");
 			ipa_client_write(link);
 		}