git: ac0068384a22 - stable/15 - virtual_oss(8): Make sndstat FD global

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Thu, 18 Jun 2026 19:33:43 UTC
The branch stable/15 has been updated by christos:

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

commit ac0068384a2228670327fe56bef9e8168c6996d0
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-06-09 13:36:48 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-06-18 19:33:35 +0000

    virtual_oss(8): Make sndstat FD global
    
    There is no reason to have per-profile copies, plus this way we open
    /dev/sndstat multiple times if more than 1 profile is created.
    
    Also close the FD on exit to avoid leaking.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Reviewed by:    jrm
    Pull-Request:   https://ron-dev.freebsd.org/FreeBSD/src/pulls/41
    
    (cherry picked from commit 93a234a694f37d373acf303a247d129dda28044e)
---
 usr.sbin/virtual_oss/virtual_oss/int.h  |  1 -
 usr.sbin/virtual_oss/virtual_oss/main.c | 16 +++++++++-------
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/usr.sbin/virtual_oss/virtual_oss/int.h b/usr.sbin/virtual_oss/virtual_oss/int.h
index dbe35dbefd58..141aa8af09f5 100644
--- a/usr.sbin/virtual_oss/virtual_oss/int.h
+++ b/usr.sbin/virtual_oss/virtual_oss/int.h
@@ -142,7 +142,6 @@ struct virtual_profile {
 	double rx_compressor_gain[VMAX_CHAN];
 	uint8_t synchronized;
 	uint32_t rec_delay;
-	int fd_sta;
 	struct {
 		const char * host;
 		const char * port;
diff --git a/usr.sbin/virtual_oss/virtual_oss/main.c b/usr.sbin/virtual_oss/virtual_oss/main.c
index a6cf691e7fbb..a9ff7d59e584 100644
--- a/usr.sbin/virtual_oss/virtual_oss/main.c
+++ b/usr.sbin/virtual_oss/virtual_oss/main.c
@@ -1638,6 +1638,7 @@ struct voss_backend *voss_tx_backend;
 static int voss_dups;
 static int voss_ntds;
 static pthread_t *voss_tds;
+static int voss_fd_sta = -1;
 
 /* XXX I do not like the prefix argument... */
 static struct voss_backend *
@@ -1843,7 +1844,7 @@ init_sndstat(vprofile_t *ptr)
 		warn("Failed to pack nvlist");
 		goto done;
 	}
-	err = ioctl(ptr->fd_sta, SNDSTIOC_ADD_USER_DEVS, &arg);
+	err = ioctl(voss_fd_sta, SNDSTIOC_ADD_USER_DEVS, &arg);
 	free(arg.buf);
 	if (err != 0) {
 		warn("Failed to issue ioctl(SNDSTIOC_ADD_USER_DEVS)");
@@ -1910,7 +1911,6 @@ dup_profile(vprofile_t *pvp, int *pamp, int pol, int rx_mute,
 	memcpy(ptr, pvp, sizeof(*ptr));
 
 	ptr->synchronized = synchronized;
-	ptr->fd_sta = -1;
 	TAILQ_INIT(&ptr->head);
 
 	for (x = 0; x != ptr->channels; x++) {
@@ -1951,12 +1951,13 @@ dup_profile(vprofile_t *pvp, int *pamp, int pol, int rx_mute,
 		ptr->oss_dev = pdev;
 
 		/* register to sndstat */
-		ptr->fd_sta = open("/dev/sndstat", O_WRONLY);
-		if (ptr->fd_sta < 0) {
-			warn("Could not open /dev/sndstat");
-		} else {
-			init_sndstat(ptr);
+		if (voss_fd_sta < 0) {
+			if ((voss_fd_sta = open("/dev/sndstat", O_WRONLY)) < 0) {
+				errstr = "Could not open /dev/sndstat";
+				goto err;
+			}
 		}
+		init_sndstat(ptr);
 	}
 	/* create WAV device */
 	if (ptr->wav_name[0] != 0) {
@@ -2676,6 +2677,7 @@ main(int argc, char **argv)
 	}
 
 	cuse_uninit();
+	close(voss_fd_sta);
 
 	return (0);
 }