summaryrefslogtreecommitdiffstats
path: root/src/ulogd.c
diff options
context:
space:
mode:
authorCorubba Smith <corubba@gmx.de>2025-03-12 15:52:29 +0100
committerFlorian Westphal <fw@strlen.de>2025-03-12 16:05:14 +0100
commit64699d8103a43e09d2b5df57558c5f5a05583fe8 (patch)
treece9cb5af3065b34bc06efb1779cec15a9880c194 /src/ulogd.c
parentb9f931e2f30e67af4d47f184f94a9d5af29b0df0 (diff)
ulogd: add ulogd_parse_configfile public function
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> Signed-off-by: Florian Westphal <fw@strlen.de>
Diffstat (limited to 'src/ulogd.c')
-rw-r--r--src/ulogd.c97
1 files changed, 48 insertions, 49 deletions
diff --git a/src/ulogd.c b/src/ulogd.c
index 9a0060d..7260aeb 100644
--- a/src/ulogd.c
+++ b/src/ulogd.c
@@ -251,6 +251,53 @@ 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 +1145,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 +1591,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);
}