Merge "Ignore SIGPIPE when using api server" am: a4d4207d3c am: bcd2a438d2

Original change: https://android-review.googlesource.com/c/platform/external/wmediumd/+/1849153

Change-Id: I995c9ba9d5e483bd0addc2329ad4088eadaa635a
diff --git a/wmediumd/wmediumd.c b/wmediumd/wmediumd.c
index 8b5d350..43580b5 100644
--- a/wmediumd/wmediumd.c
+++ b/wmediumd/wmediumd.c
@@ -275,6 +275,8 @@
 		usfstl_loop_wait_and_handle();
 }
 
+static void wmediumd_remove_client(struct wmediumd *ctx, struct client *client);
+
 static void wmediumd_notify_frame_start(struct usfstl_job *job)
 {
 	struct frame *frame = container_of(job, struct frame, start_job);
@@ -304,7 +306,11 @@
 		/* must be API socket since flags cannot otherwise be set */
 		assert(client->type == CLIENT_API_SOCK);
 
-		write(client->loop.fd, &msg, sizeof(msg));
+		if (write(client->loop.fd, &msg, sizeof(msg)) < sizeof(msg)) {
+			usfstl_loop_unregister(&client->loop);
+			wmediumd_remove_client(ctx, client);
+			continue;
+		}
 
 		wmediumd_wait_for_client_ack(ctx, client);
 	}
@@ -548,11 +554,22 @@
 		len = nlmsg_total_size(nlmsg_datalen(nlmsg_hdr(msg)));
 		hdr.type = WMEDIUMD_MSG_NETLINK;
 		hdr.data_len = len;
-		write(client->loop.fd, &hdr, sizeof(hdr));
-		write(client->loop.fd, (void *)nlmsg_hdr(msg), len);
+
+		if (write(client->loop.fd, &hdr, sizeof(hdr)) < sizeof(hdr))
+			goto disconnect;
+
+		if (write(client->loop.fd, (void *)nlmsg_hdr(msg), len) < len)
+			goto disconnect;
+
 		wmediumd_wait_for_client_ack(ctx, client);
 		break;
 	}
+
+	return;
+
+	disconnect:
+	usfstl_loop_unregister(&client->loop);
+	wmediumd_remove_client(ctx, client);
 }
 
 static void wmediumd_remove_client(struct wmediumd *ctx, struct client *client)
@@ -1461,8 +1478,10 @@
 			w_logf(&ctx, LOG_NOTICE, "REGISTER SENT!\n");
 	}
 
-	if (api_socket)
+	if (api_socket) {
+		signal(SIGPIPE, SIG_IGN);
 		usfstl_uds_create(api_socket, wmediumd_api_connected, &ctx);
+	}
 
 	if (time_socket) {
 		usfstl_sched_ctrl_start(&ctrl, time_socket,