git: 4c5e0172767d - main - ctld: Replace the auth_name linked list with an unordered_set<>

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Mon, 04 Aug 2025 19:46:43 UTC
The branch main has been updated by jhb:

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

commit 4c5e0172767d6a61d50c475023b81dc3e2049465
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2025-08-04 19:38:06 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2025-08-04 19:38:06 +0000

    ctld: Replace the auth_name linked list with an unordered_set<>
    
    Retire the struct auth_name type entirely and change the ag_names
    member of struct auth_group to be an unordered_set<> of std::string
    objects.
    
    Sponsored by:   Chelsio Communications
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1794
---
 usr.sbin/ctld/ctld.cc | 51 ++++-----------------------------------------------
 usr.sbin/ctld/ctld.hh | 12 ++----------
 2 files changed, 6 insertions(+), 57 deletions(-)

diff --git a/usr.sbin/ctld/ctld.cc b/usr.sbin/ctld/ctld.cc
index 558ddb8ac6aa..3ddccbfdb20e 100644
--- a/usr.sbin/ctld/ctld.cc
+++ b/usr.sbin/ctld/ctld.cc
@@ -226,57 +226,18 @@ auth_new_chap_mutual(struct auth_group *ag, const char *user,
 bool
 auth_name_new(struct auth_group *ag, const char *name)
 {
-	struct auth_name *an;
-
-	an = reinterpret_cast<struct auth_name *>(calloc(1, sizeof(*an)));
-	if (an == NULL)
-		log_err(1, "calloc");
-	an->an_auth_group = ag;
-	an->an_initiator_name = checked_strdup(name);
-	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
+	/* Silently ignore duplicates. */
+	ag->ag_names.emplace(name);
 	return (true);
 }
 
-static void
-auth_name_delete(struct auth_name *an)
-{
-	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
-
-	free(an->an_initiator_name);
-	free(an);
-}
-
-bool
-auth_name_defined(const struct auth_group *ag)
-{
-	if (TAILQ_EMPTY(&ag->ag_names))
-		return (false);
-	return (true);
-}
-
-const struct auth_name *
-auth_name_find(const struct auth_group *ag, const char *name)
-{
-	const struct auth_name *auth_name;
-
-	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
-		if (strcmp(auth_name->an_initiator_name, name) == 0)
-			return (auth_name);
-	}
-
-	return (NULL);
-}
-
 bool
 auth_name_check(const struct auth_group *ag, const char *initiator_name)
 {
-	if (!auth_name_defined(ag))
+	if (ag->ag_names.empty())
 		return (true);
 
-	if (auth_name_find(ag, initiator_name) == NULL)
-		return (false);
-
-	return (true);
+	return (ag->ag_names.count(initiator_name) != 0);
 }
 
 bool
@@ -420,7 +381,6 @@ auth_group_create(struct conf *conf, const char *name, char *label)
 	if (name != NULL)
 		ag->ag_name = checked_strdup(name);
 	ag->ag_label = label;
-	TAILQ_INIT(&ag->ag_names);
 	TAILQ_INIT(&ag->ag_portals);
 	ag->ag_conf = conf;
 	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
@@ -456,13 +416,10 @@ auth_group_new(struct conf *conf, struct target *target)
 void
 auth_group_delete(struct auth_group *ag)
 {
-	struct auth_name *auth_name, *auth_name_tmp;
 	struct auth_portal *auth_portal, *auth_portal_tmp;
 
 	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
 
-	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
-		auth_name_delete(auth_name);
 	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
 	    auth_portal_tmp)
 		auth_portal_delete(auth_portal);
diff --git a/usr.sbin/ctld/ctld.hh b/usr.sbin/ctld/ctld.hh
index a2cedeaf77da..339793e90bf1 100644
--- a/usr.sbin/ctld/ctld.hh
+++ b/usr.sbin/ctld/ctld.hh
@@ -44,6 +44,7 @@
 #include <string>
 #include <string_view>
 #include <unordered_map>
+#include <unordered_set>
 
 #define	DEFAULT_CONFIG_PATH		"/etc/ctl.conf"
 #define	DEFAULT_PIDFILE			"/var/run/ctld.pid"
@@ -72,12 +73,6 @@ private:
 	std::string			a_mutual_secret;
 };
 
-struct auth_name {
-	TAILQ_ENTRY(auth_name)		an_next;
-	struct auth_group		*an_auth_group;
-	char				*an_initiator_name;
-};
-
 struct auth_portal {
 	TAILQ_ENTRY(auth_portal)	ap_next;
 	struct auth_group		*ap_auth_group;
@@ -99,7 +94,7 @@ struct auth_group {
 	char				*ag_label;
 	int				ag_type;
 	std::unordered_map<std::string, auth> ag_auths;
-	TAILQ_HEAD(, auth_name)		ag_names;
+	std::unordered_set<std::string> ag_names;
 	TAILQ_HEAD(, auth_portal)	ag_portals;
 };
 
@@ -288,9 +283,6 @@ const struct auth	*auth_find(const struct auth_group *ag,
 
 bool			auth_name_new(struct auth_group *ag,
 			    const char *initiator_name);
-bool			auth_name_defined(const struct auth_group *ag);
-const struct auth_name	*auth_name_find(const struct auth_group *ag,
-			    const char *initiator_name);
 bool			auth_name_check(const struct auth_group *ag,
 			    const char *initiator_name);