git: 5daa7cf42f45 - main - mixer(8): Use new mixer if we change the default unit

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Mon, 12 Feb 2024 11:00:23 UTC
The branch main has been updated by christos:

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

commit 5daa7cf42f4551cb2f4a452fd038807925320eac
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2024-02-12 10:59:28 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2024-02-12 11:00:05 +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
---
 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);