[PATCH ulogd2,v2 1/6] ulogd: add ulogd_parse_configfile public function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxx>
---
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@xxxxxx/

 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





[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux