git: 6ae4ed11c4d3 - stable/14 - mixer(8): Use new mixer if we change the default unit

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Mon, 26 Feb 2024 23:22:48 UTC
The branch stable/14 has been updated by christos:

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

commit 6ae4ed11c4d3f3c082e126eb9193ad535bf462ba
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2024-02-12 10:59:28 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2024-02-26 23:22:31 +0000

    mixer(8): Use new mixer if we change the default unit
    
    If we use the -d option to change the default unit, close the current
    mixer and open the one we set as the default to avoid printing and
    applying changes (if any) to the old one.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      2 weeks
    Reviewed by:    dev_submerge.ch, markj
    Differential Revision:  https://reviews.freebsd.org/D43809
    
    (cherry picked from commit 5daa7cf42f4551cb2f4a452fd038807925320eac)
---
 usr.sbin/mixer/mixer.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/usr.sbin/mixer/mixer.c b/usr.sbin/mixer/mixer.c
index 83e97df19116..47d8e6359b73 100644
--- a/usr.sbin/mixer/mixer.c
+++ b/usr.sbin/mixer/mixer.c
@@ -118,8 +118,21 @@ main(int argc, char *argv[])
 
 	initctls(m);
 
-	if (dflag && set_dunit(m, dunit) < 0)
-		goto parse;
+	if (dflag) {
+		if (set_dunit(m, dunit) < 0)
+			goto parse;
+		else {
+			/*
+			 * Open current mixer since we changed the default
+			 * unit, otherwise we'll print and apply changes to the
+			 * old one.
+			 */
+			(void)mixer_close(m);
+			if ((m = mixer_open(NULL)) == NULL)
+				errx(1, "cannot open default mixer");
+			initctls(m);
+		}
+	}
 	if (sflag) {
 		printrecsrc(m, oflag);
 		(void)mixer_close(m);