From: Corubba Smith <corubba@gmx.de>
To: netfilter-devel@vger.kernel.org
Subject: [PATCH ulogd2,v2 1/6] ulogd: add ulogd_parse_configfile public function
Date: Wed, 12 Mar 2025 15:52:29 +0100 [thread overview]
Message-ID: <1a5fff4d-4cef-48e3-a77c-bb4f7098f35b@gmx.de> (raw)
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
next reply other threads:[~2025-03-12 14:52 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-12 14:52 Corubba Smith [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1a5fff4d-4cef-48e3-a77c-bb4f7098f35b@gmx.de \
--to=corubba@gmx.de \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).