Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178401150
D62522.1796670357.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
D62522.1796670357.diff
View Options
diff --git a/usr.sbin/bhyve/bhyve.8 b/usr.sbin/bhyve/bhyve.8
--- a/usr.sbin/bhyve/bhyve.8
+++ b/usr.sbin/bhyve/bhyve.8
@@ -26,7 +26,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd November 19, 2026
+.Dd December 6, 2026
.Dt BHYVE 8
.Os
.Sh NAME
@@ -986,8 +986,10 @@
Limitations:
.Bl -bullet
.It
-Due to the lack of destructors in
-.Nm ,
+Unless
+.Nm
+runs in monitor mode
+.Pq Fl M ,
sockets on the filesystem must be cleaned up manually after
.Nm
exits.
diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c
--- a/usr.sbin/bhyve/bhyverun.c
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -33,8 +33,8 @@
#include <sys/cpuset.h>
#include <sys/domainset.h>
#include <sys/mman.h>
-#ifdef BHYVE_SNAPSHOT
#include <sys/socket.h>
+#ifdef BHYVE_SNAPSHOT
#include <sys/stat.h>
#endif
#include <sys/time.h>
@@ -85,6 +85,7 @@
#include "mem.h"
#include "mevent.h"
#include "pci_emul.h"
+#include "pci_virtio_console.h"
#ifdef __amd64__
#include "amd64/pci_lpc.h"
#endif
@@ -961,13 +962,20 @@
init_bootrom(ctx);
if (get_config_bool_default("monitor.enabled", false)) {
- int monitor_pipe[2];
+ int cleanup_error, cleanup_socket[2], monitor_pipe[2];
pid_t child;
while (1) {
if (pipe2(monitor_pipe, O_CLOEXEC) == -1)
err(BHYVE_EXIT_ERROR, "pipe2");
+ if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
+ cleanup_socket) == -1)
+ err(BHYVE_EXIT_ERROR, "socketpair");
+ if (setsockopt(cleanup_socket[1], SOL_SOCKET, SO_NOSIGPIPE,
+ &(int){1}, sizeof(int)) == -1)
+ err(BHYVE_EXIT_ERROR, "setsockopt(SO_NOSIGPIPE)");
+
child = fork();
if (child == -1) {
EPRINTLN("Monitor mode fork failed: %s",
@@ -977,10 +985,14 @@
if (child == 0) {
close(monitor_pipe[1]);
monitor_pipe_init(monitor_pipe[0]);
+ close(cleanup_socket[0]);
+ pci_vtcon_set_cleanup_fd(cleanup_socket[1]);
break;
}
close(monitor_pipe[0]);
+ close(cleanup_socket[1]);
+ cleanup_error = pci_vtcon_monitor_collect(cleanup_socket[0]);
while ((error = waitpid(child, &status, 0)) == -1 && errno == EINTR)
;
close(monitor_pipe[1]);
@@ -989,6 +1001,7 @@
strerror(errno));
exit(BHYVE_EXIT_ERROR);
}
+ pci_vtcon_monitor_cleanup();
if (WIFSIGNALED(status)) {
EPRINTLN("Child process was killed by signal %d",
WTERMSIG(status));
@@ -999,6 +1012,9 @@
status != BHYVE_EXIT_RESET)
exit(status);
}
+ if (cleanup_error != 0)
+ errx(BHYVE_EXIT_ERROR, "Socket cleanup registration: %s",
+ strerror(cleanup_error));
if (vm_reinit(ctx) != 0) {
EPRINTLN("Monitor mode reinit failed: %s",
strerror(errno));
@@ -1062,6 +1078,7 @@
strerror(errno));
exit(BHYVE_EXIT_ERROR);
}
+ pci_vtcon_set_cleanup_fd(-1);
if (init_tpm(ctx) != 0) {
EPRINTLN("Failed to init TPM device");
exit(BHYVE_EXIT_ERROR);
diff --git a/usr.sbin/bhyve/pci_virtio_console.h b/usr.sbin/bhyve/pci_virtio_console.h
new file mode 100644
--- /dev/null
+++ b/usr.sbin/bhyve/pci_virtio_console.h
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2026 The FreeBSD Foundation
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#ifndef _PCI_VIRTIO_CONSOLE_H_
+#define _PCI_VIRTIO_CONSOLE_H_
+
+void pci_vtcon_set_cleanup_fd(int fd);
+int pci_vtcon_monitor_collect(int fd);
+void pci_vtcon_monitor_cleanup(void);
+
+#endif
diff --git a/usr.sbin/bhyve/pci_virtio_console.c b/usr.sbin/bhyve/pci_virtio_console.c
--- a/usr.sbin/bhyve/pci_virtio_console.c
+++ b/usr.sbin/bhyve/pci_virtio_console.c
@@ -60,6 +60,7 @@
#include "config.h"
#include "debug.h"
#include "pci_emul.h"
+#include "pci_virtio_console.h"
#include "virtio.h"
#include "mevent.h"
#include "sockstream.h"
@@ -83,6 +84,106 @@
#define VTCON_S_HOSTCAPS \
(VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE)
+struct pci_vtcon_cleanup {
+ struct pci_vtcon_cleanup *next;
+ char *path;
+ int dirfd;
+ int pathfd;
+};
+
+static int pci_vtcon_cleanup_fd = -1;
+static struct pci_vtcon_cleanup *pci_vtcon_cleanup_paths;
+
+void
+pci_vtcon_set_cleanup_fd(int fd)
+{
+ if (pci_vtcon_cleanup_fd != -1)
+ close(pci_vtcon_cleanup_fd);
+ pci_vtcon_cleanup_fd = fd;
+}
+
+int
+pci_vtcon_monitor_collect(int fd)
+{
+ struct pci_vtcon_cleanup *entry;
+ nvlist_t *nvl;
+ int error;
+
+ error = 0;
+ while ((nvl = nvlist_recv(fd, 0)) != NULL) {
+ if (!nvlist_exists_string(nvl, "path") ||
+ !nvlist_exists_descriptor(nvl, "dirfd") ||
+ !nvlist_exists_descriptor(nvl, "pathfd")) {
+ nvlist_destroy(nvl);
+ error = EINVAL;
+ break;
+ }
+ entry = calloc(1, sizeof(*entry));
+ if (entry == NULL) {
+ nvlist_destroy(nvl);
+ error = ENOMEM;
+ break;
+ }
+ entry->path = nvlist_take_string(nvl, "path");
+ entry->dirfd = nvlist_take_descriptor(nvl, "dirfd");
+ entry->pathfd = nvlist_take_descriptor(nvl, "pathfd");
+ entry->next = pci_vtcon_cleanup_paths;
+ pci_vtcon_cleanup_paths = entry;
+ nvlist_destroy(nvl);
+ }
+ if (error == 0 && errno != ENOTCONN)
+ error = errno;
+ close(fd);
+ return (error);
+}
+
+void
+pci_vtcon_monitor_cleanup(void)
+{
+ struct pci_vtcon_cleanup *entry;
+
+ while ((entry = pci_vtcon_cleanup_paths) != NULL) {
+ pci_vtcon_cleanup_paths = entry->next;
+ if (funlinkat(entry->dirfd, entry->path, entry->pathfd, 0) == -1 &&
+ errno != ENOENT && errno != EDEADLK)
+ warn("virtio-console: unlink %s", entry->path);
+ close(entry->pathfd);
+ close(entry->dirfd);
+ free(entry->path);
+ free(entry);
+ }
+}
+
+static int
+pci_vtcon_register_cleanup(int dirfd, const char *path)
+{
+ nvlist_t *nvl;
+ int error, pathfd;
+
+ if (pci_vtcon_cleanup_fd == -1)
+ return (0);
+
+ pathfd = openat(dirfd, path, O_PATH | O_NOFOLLOW | O_CLOEXEC);
+ if (pathfd == -1)
+ return (-1);
+ nvl = nvlist_create(0);
+ if (nvl == NULL) {
+ error = errno;
+ goto out;
+ }
+ nvlist_add_string(nvl, "path", path);
+ nvlist_add_descriptor(nvl, "dirfd", dirfd);
+ nvlist_add_descriptor(nvl, "pathfd", pathfd);
+ error = nvlist_send(pci_vtcon_cleanup_fd, nvl) == -1 ? errno : 0;
+out:
+ if (error != 0)
+ (void)funlinkat(dirfd, path, pathfd, 0);
+ nvlist_destroy(nvl);
+ close(pathfd);
+ errno = error;
+ return (error == 0 ? 0 : -1);
+}
+
static int pci_vtcon_debug;
#define DPRINTF(params) if (pci_vtcon_debug) PRINTLN params
#define WPRINTF(params) PRINTLN params
@@ -336,6 +437,11 @@
goto out;
}
+ if (pci_vtcon_register_cleanup(fd, sun.sun_path) < 0) {
+ error = -1;
+ goto out;
+ }
+
if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
error = -1;
goto out;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Dec 7, 7:05 PM (22 h, 10 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42280291
Default Alt Text
D62522.1796670357.diff (6 KB)
Attached To
Mode
D62522: bhyve: clean up virtio-console sockets on exit
Attached
Detach File
Event Timeline