git: 690b7ea08179 - main - bhyve/snapshot: ..back to SOCK_STREAM

From: Robert Wing <rew_at_FreeBSD.org>
Date: Thu, 28 Apr 2022 15:44:52 UTC
The branch main has been updated by rew:

URL: https://cgit.FreeBSD.org/src/commit/?id=690b7ea081790eef2c890f63a4fe7e195cf51df0

commit 690b7ea081790eef2c890f63a4fe7e195cf51df0
Author:     Robert Wing <rew@FreeBSD.org>
AuthorDate: 2022-04-28 15:43:01 +0000
Commit:     Robert Wing <rew@FreeBSD.org>
CommitDate: 2022-04-28 15:43:01 +0000

    bhyve/snapshot: ..back to SOCK_STREAM
    
    Now that nvlist_send()/nvlist_recv() are being used, ditch the datagram
    socket.
    
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D34863
---
 usr.sbin/bhyve/snapshot.c    | 14 +++++++++++---
 usr.sbin/bhyvectl/bhyvectl.c |  9 +++++----
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/usr.sbin/bhyve/snapshot.c b/usr.sbin/bhyve/snapshot.c
index f17a3f2dd2df..37486ba368e3 100644
--- a/usr.sbin/bhyve/snapshot.c
+++ b/usr.sbin/bhyve/snapshot.c
@@ -1450,19 +1450,21 @@ handle_message(struct vmctx *ctx, nvlist_t *nvl)
 void *
 checkpoint_thread(void *param)
 {
+	int fd;
 	struct checkpoint_thread_info *thread_info;
 	nvlist_t *nvl;
 
 	pthread_set_name_np(pthread_self(), "checkpoint thread");
 	thread_info = (struct checkpoint_thread_info *)param;
 
-	for (;;) {
-		nvl = nvlist_recv(thread_info->socket_fd, 0);
+	while ((fd = accept(thread_info->socket_fd, NULL, NULL)) != -1) {
+		nvl = nvlist_recv(fd, 0);
 		if (nvl != NULL)
 			handle_message(thread_info->ctx, nvl);
 		else
 			EPRINTLN("nvlist_recv() failed: %s", strerror(errno));
 
+		close(fd);
 		nvlist_destroy(nvl);
 	}
 
@@ -1515,7 +1517,7 @@ init_checkpoint_thread(struct vmctx *ctx)
 
 	memset(&addr, 0, sizeof(addr));
 
-	socket_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
+	socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
 	if (socket_fd < 0) {
 		EPRINTLN("Socket creation failed: %s", strerror(errno));
 		err = -1;
@@ -1536,6 +1538,12 @@ init_checkpoint_thread(struct vmctx *ctx)
 		goto fail;
 	}
 
+	if (listen(socket_fd, 10) < 0) {
+		EPRINTLN("ipc socket listen: %s\n", strerror(errno));
+		err = errno;
+		goto fail;
+	}
+
 	checkpoint_info = calloc(1, sizeof(*checkpoint_info));
 	checkpoint_info->ctx = ctx;
 	checkpoint_info->socket_fd = socket_fd;
diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c
index 06ab05cd48e1..14ab6c7ad33e 100644
--- a/usr.sbin/bhyvectl/bhyvectl.c
+++ b/usr.sbin/bhyvectl/bhyvectl.c
@@ -1687,7 +1687,7 @@ send_message(const char *vmname, nvlist_t *nvl)
 	struct sockaddr_un addr;
 	int err, socket_fd;
 
-	socket_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
+	socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
 	if (socket_fd < 0) {
 		perror("Error creating bhyvectl socket");
 		err = -1;
@@ -1695,11 +1695,12 @@ send_message(const char *vmname, nvlist_t *nvl)
 	}
 
 	memset(&addr, 0, sizeof(struct sockaddr_un));
+	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s%s",
+	    BHYVE_RUN_DIR, vmname);
 	addr.sun_family = AF_UNIX;
+	addr.sun_len = SUN_LEN(&addr);
 
-	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s%s", BHYVE_RUN_DIR, vmname);
-
-	if (connect(socket_fd, (struct sockaddr *)&addr, SUN_LEN(&addr)) != 0) {
+	if (connect(socket_fd, (struct sockaddr *)&addr, addr.sun_len) != 0) {
 		perror("connect() failed");
 		err = errno;
 		goto done;