git: 69141e71f3ee - main - bhyve: disable SIGPIPE for IPC connections

From: Roman Bogorodskiy <novel_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 17:21:21 UTC
The branch main has been updated by novel:

URL: https://cgit.FreeBSD.org/src/commit/?id=69141e71f3ee189ae465e91da4bda439fbbba2bf

commit 69141e71f3ee189ae465e91da4bda439fbbba2bf
Author:     Roman Bogorodskiy <novel@FreeBSD.org>
AuthorDate: 2026-11-24 18:48:12 +0000
Commit:     Roman Bogorodskiy <novel@FreeBSD.org>
CommitDate: 2026-11-26 17:18:37 +0000

    bhyve: disable SIGPIPE for IPC connections
    
    Currently, if an IPC client (e.g. bhyvectl --get-vm-pid) exits
    before reading the response, the VM crashes with SIGPIPE.
    Set SO_NOSIGPIPE on a socket to prevent that.
    
    While here, update other error paths in init_ipc_thread()
    to return -1 on error instead of an error number to make error
    handling consistent.
    
    Reviewed by:    markj
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D62000
---
 usr.sbin/bhyve/ipc.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/usr.sbin/bhyve/ipc.c b/usr.sbin/bhyve/ipc.c
index ba938e99aeb5..09333acc12b6 100644
--- a/usr.sbin/bhyve/ipc.c
+++ b/usr.sbin/bhyve/ipc.c
@@ -153,6 +153,14 @@ init_ipc_thread(struct vmctx *ctx, const char *bhyve_run_dir)
 		goto fail;
 	}
 
+	if (setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1},
+	    sizeof(int)) == -1) {
+		err = -1;
+		EPRINTLN("Failed to disable SIGPIPE for IPC connection: %s",
+		    strerror(errno));
+		goto fail;
+	}
+
 	addr.sun_family = AF_UNIX;
 
 	ret = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
@@ -175,7 +183,7 @@ init_ipc_thread(struct vmctx *ctx, const char *bhyve_run_dir)
 
 	if (listen(socket_fd, 10) < 0) {
 		EPRINTLN("ipc socket listen: %s\n", strerror(errno));
-		err = errno;
+		err = -1;
 		goto fail;
 	}
 
@@ -190,10 +198,10 @@ init_ipc_thread(struct vmctx *ctx, const char *bhyve_run_dir)
 	thr_ctx.vmctx = ctx;
 	thr_ctx.sockfd = socket_fd;
 
-	err = pthread_create(&ipc_pthread, NULL, ipc_thread,
-		&thr_ctx);
-	if (err != 0)
+	if (pthread_create(&ipc_pthread, NULL, ipc_thread, &thr_ctx) != 0) {
+		err = -1;
 		goto fail;
+	}
 
 	return (0);
 fail: