netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH ulogd2,v2 1/6] ulogd: add ulogd_parse_configfile public function
@ 2025-03-12 14:52 Corubba Smith
  2025-03-12 14:53 ` [PATCH ulogd2,v2 2/6] all: use config_parse_file function in all plugins Corubba Smith
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Corubba Smith @ 2025-03-12 14:52 UTC (permalink / raw)
  To: netfilter-devel

Provide a new function `ulogd_parse_configfile()` in the public
interface, which wraps `parse_config_file()` to parse a section of the
config file and communicates found errors to the user. It can be used
as a drop-in replacement because arguments and return value are
compatible.

This relieves plugins of the need to translate the individual error
codes to human readable messages, and plugins are mostly interested if
there is any error, not what specific error.

This reuses the existing `parse_conffile()` function with slight
adjustments.

Signed-off-by: Corubba Smith <corubba@gmx.de>
---
Changes in v2:
  - Rebase onto master branch (b9f931e2f30e ("nfct: add icmpv6"))
  - Renumber the patchset since v1 patch #1 and #5 are already applied
  - Reduce indentation of case statements (Florian Westphal)
  - Link to v1: https://lore.kernel.org/netfilter-devel/ca5581f5-5e54-47f5-97c8-bcc788c77781@gmx.de/

 include/ulogd/ulogd.h |   1 +
 src/ulogd.c           | 104 ++++++++++++++++++++++--------------------
 2 files changed, 56 insertions(+), 49 deletions(-)

diff --git a/include/ulogd/ulogd.h b/include/ulogd/ulogd.h
index c7cf402..088d85d 100644
--- a/include/ulogd/ulogd.h
+++ b/include/ulogd/ulogd.h
@@ -362,6 +362,7 @@ void __ulogd_log(int level, char *file, int line, const char *message, ...)

 int ulogd_key_size(struct ulogd_key *key);
 int ulogd_wildcard_inputkeys(struct ulogd_pluginstance *upi);
+int ulogd_parse_configfile(const char *section, struct config_keyset *ce);

 /***********************************************************************
  * file descriptor handling
diff --git a/src/ulogd.c b/src/ulogd.c
index 9a0060d..51aa2b9 100644
--- a/src/ulogd.c
+++ b/src/ulogd.c
@@ -251,6 +251,60 @@ int ulogd_wildcard_inputkeys(struct ulogd_pluginstance *upi)
 	return 0;
 }

+/**
+ * Parse the given section in the config file into the given keyset.
+ * Returns ULOGD_IRET_OK on success, ULOGD_IRET_ERR on error.
+ * If an error occurs, writes a descriptive message to the log.
+ */
+int ulogd_parse_configfile(const char *section, struct config_keyset *ce)
+{
+	int err;
+
+	err = config_parse_file(section, ce);
+
+	switch(err) {
+	case 0:
+		return ULOGD_IRET_OK;
+		break;
+	case -ERROPEN:
+		ulogd_log(ULOGD_ERROR,
+		          "unable to open configfile: %s\n",
+		          ulogd_configfile);
+		break;
+	case -ERRMAND:
+		ulogd_log(ULOGD_ERROR,
+		          "mandatory option \"%s\" not found\n",
+		          config_errce->key);
+		break;
+	case -ERRMULT:
+		ulogd_log(ULOGD_ERROR,
+		          "option \"%s\" occurred more than once\n",
+		          config_errce->key);
+		break;
+	case -ERRUNKN:
+		ulogd_log(ULOGD_ERROR,
+		          "unknown config key \"%s\"\n",
+		          config_errce->key);
+		break;
+	case -ERRSECTION:
+		ulogd_log(ULOGD_ERROR,
+		          "section \"%s\" not found\n",
+		          section);
+		break;
+	case -ERRTOOLONG:
+		if (config_errce != NULL)
+			ulogd_log(ULOGD_ERROR,
+			          "string value too long for key \"%s\"\n",
+			          config_errce->key);
+		else
+			ulogd_log(ULOGD_ERROR,
+			          "string value is too long\n");
+		break;
+	}
+
+	return ULOGD_IRET_ERR;
+}
+

 /***********************************************************************
  * PLUGIN MANAGEMENT
@@ -1098,54 +1152,6 @@ static int logfile_open(const char *name)
 	return 0;
 }

-/* wrapper to handle conffile error codes */
-static int parse_conffile(const char *section, struct config_keyset *ce)
-{
-	int err;
-
-	err = config_parse_file(section, ce);
-
-	switch(err) {
-		case 0:
-			return 0;
-			break;
-		case -ERROPEN:
-			ulogd_log(ULOGD_ERROR,
-				"unable to open configfile: %s\n",
-				ulogd_configfile);
-			break;
-		case -ERRMAND:
-			ulogd_log(ULOGD_ERROR,
-				"mandatory option \"%s\" not found\n",
-				config_errce->key);
-			break;
-		case -ERRMULT:
-			ulogd_log(ULOGD_ERROR,
-				"option \"%s\" occurred more than once\n",
-				config_errce->key);
-			break;
-		case -ERRUNKN:
-			ulogd_log(ULOGD_ERROR,
-				"unknown config key \"%s\"\n",
-				config_errce->key);
-			break;
-		case -ERRSECTION:
-			ulogd_log(ULOGD_ERROR,
-				"section \"%s\" not found\n", section);
-			break;
-		case -ERRTOOLONG:
-			if (config_errce)
-				ulogd_log(ULOGD_ERROR,
-					  "string value too long for key \"%s\"\n",
-					  config_errce->key);
-			else
-				ulogd_log(ULOGD_ERROR,
-					  "string value is too long\n");
-			break;
-	}
-	return 1;
-}
-
 /*
  * Apply F_WRLCK to fd using fcntl().
  *
@@ -1592,7 +1598,7 @@ int main(int argc, char* argv[])
 	}

 	/* parse config file */
-	if (parse_conffile("global", &ulogd_kset)) {
+	if (ulogd_parse_configfile("global", &ulogd_kset)) {
 		ulogd_log(ULOGD_FATAL, "unable to parse config file\n");
 		warn_and_exit(daemonize);
 	}
--
2.48.1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-03-12 18:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-12 14:52 [PATCH ulogd2,v2 1/6] ulogd: add ulogd_parse_configfile public function Corubba Smith
2025-03-12 14:53 ` [PATCH ulogd2,v2 2/6] all: use config_parse_file function in all plugins Corubba Smith
2025-03-12 14:54 ` [PATCH ulogd2,v2 3/6] ulogd: raise error on unknown config key Corubba Smith
2025-03-12 15:21   ` Florian Westphal
2025-03-12 18:48     ` Corubba Smith
2025-03-12 18:55       ` Florian Westphal
2025-03-12 14:55 ` [PATCH ulogd2,v2 4/6] ulogd: improve integer option parsing Corubba Smith
2025-03-12 14:55 ` [PATCH ulogd2,v2 5/6] ulogd: provide default configure implementation Corubba Smith
2025-03-12 14:56 ` [PATCH ulogd2,v2 6/6] all: remove trivial configure hooks Corubba Smith

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).