git: fe453891d7cc - main - bhyve: add nvlist functions for setting unset nodes

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Fri, 14 Jan 2022 11:42:12 UTC
The branch main has been updated by manu:

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

commit fe453891d7ccc8e173d9293b67f5b4608c5378dd
Author:     Corvin Köhne <CorvinK@beckhoff.com>
AuthorDate: 2022-01-14 10:00:08 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2022-01-14 11:41:44 +0000

    bhyve: add nvlist functions for setting unset nodes
    
    If an emulation uses those functions instead of set_config_value_node
    or set_config_value, it allows the config values to get
    overwritten. Introducing new functions is much more readable than
    if else statements in the emulation code.
    
    Reviewed by:    khng
    MFC after:      2 weeks
    Sponsored by:   Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D33770
---
 usr.sbin/bhyve/config.c | 21 +++++++++++++++++++++
 usr.sbin/bhyve/config.h | 12 ++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/usr.sbin/bhyve/config.c b/usr.sbin/bhyve/config.c
index c9ac9f0a1905..cdea2f4fed01 100644
--- a/usr.sbin/bhyve/config.c
+++ b/usr.sbin/bhyve/config.c
@@ -135,6 +135,17 @@ set_config_value_node(nvlist_t *parent, const char *name, const char *value)
 	nvlist_add_string(parent, name, value);
 }
 
+void
+set_config_value_node_if_unset(nvlist_t *const parent, const char *const name,
+    const char *const value)
+{
+	if (get_config_value_node(parent, name) != NULL) {
+		return;
+	}
+
+	set_config_value_node(parent, name, value);
+}
+
 void
 set_config_value(const char *path, const char *value)
 {
@@ -167,6 +178,16 @@ set_config_value(const char *path, const char *value)
 	set_config_value_node(nvl, name, value);
 }
 
+void
+set_config_value_if_unset(const char *const path, const char *const value)
+{
+	if (get_config_value(path) != NULL) {
+		return;
+	}
+
+	set_config_value(path, value);
+}
+
 static const char *
 get_raw_config_value(const char *path)
 {
diff --git a/usr.sbin/bhyve/config.h b/usr.sbin/bhyve/config.h
index 574da966df74..8d3f6f90b2dc 100644
--- a/usr.sbin/bhyve/config.h
+++ b/usr.sbin/bhyve/config.h
@@ -99,12 +99,24 @@ nvlist_t *find_relative_config_node(nvlist_t *parent, const char *path);
 void	set_config_value_node(nvlist_t *parent, const char *name,
     const char *value);
 
+/*
+ * Similar to set_config_value_node but only sets value if it's unset yet.
+ */
+void 	set_config_value_node_if_unset(nvlist_t *const parent,
+    const char *const name, const char *const value);
+
 /*
  * Similar to set_config_value_node but expects a full path to the
  * leaf node.
  */
 void	set_config_value(const char *path, const char *value);
 
+/*
+ * Similar to set_config_value but only sets the value if it's unset yet.
+ */
+void 	set_config_value_if_unset(const char *const path,
+    const char *const value);
+
 /* Convenience wrappers for boolean variables. */
 bool	get_config_bool(const char *path);
 bool	get_config_bool_node(const nvlist_t *parent, const char *name);