git: 9e502c25f7ba - stable/13 - bhyve: add nvlist functions for setting unset nodes

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Fri, 28 Jan 2022 10:34:35 UTC
The branch stable/13 has been updated by manu:

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

commit 9e502c25f7ba4843192aabb82c012691d57b2ab8
Author:     Corvin Köhne <CorvinK@beckhoff.com>
AuthorDate: 2022-01-14 10:00:08 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2022-01-28 07:11:03 +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
    
    (cherry picked from commit fe453891d7ccc8e173d9293b67f5b4608c5378dd)
---
 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 1df750e9576c..05eb57e32c8e 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 9f82afb267b2..ae6c8df64c6f 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);