From: Jeremy Sowden <jeremy@azazel.net>
To: Netfilter Devel <netfilter-devel@vger.kernel.org>
Subject: [PATCH ulogd2 v2 v2 16/34] db: use consistent integer return values to indicate errors
Date: Tue, 29 Nov 2022 21:47:31 +0000 [thread overview]
Message-ID: <20221129214749.247878-17-jeremy@azazel.net> (raw)
In-Reply-To: <20221129214749.247878-1-jeremy@azazel.net>
Internally, we use `-1` for error and `0` for success. The `interp`
functions that return to `ulogd_propagate_results` return
`ULOGD_IRET_ERR` and `ULOGD_IRET_OK`.
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
output/dbi/ulogd_output_DBI.c | 2 +-
output/pgsql/ulogd_output_PGSQL.c | 8 ++---
util/db.c | 59 +++++++++++++++++--------------
3 files changed, 38 insertions(+), 31 deletions(-)
diff --git a/output/dbi/ulogd_output_DBI.c b/output/dbi/ulogd_output_DBI.c
index 5c10c787fb6a..3b8d8c325131 100644
--- a/output/dbi/ulogd_output_DBI.c
+++ b/output/dbi/ulogd_output_DBI.c
@@ -96,7 +96,7 @@ static int get_columns_dbi(struct ulogd_pluginstance *upi)
if (!pi->dbh) {
ulogd_log(ULOGD_ERROR, "no database handle\n");
- return 1;
+ return -1;
}
snprintf(query, 256, "SELECT * FROM %s", table);
diff --git a/output/pgsql/ulogd_output_PGSQL.c b/output/pgsql/ulogd_output_PGSQL.c
index c5bbc966d66d..70a4d48459ff 100644
--- a/output/pgsql/ulogd_output_PGSQL.c
+++ b/output/pgsql/ulogd_output_PGSQL.c
@@ -92,7 +92,7 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi)
strlen(schema_ce(upi->config_kset).u.string) + 1];
if (!pi->dbh)
- return 1;
+ return -1;
sprintf(pgbuf, PGSQL_HAVE_NAMESPACE_TEMPLATE,
schema_ce(upi->config_kset).u.string);
@@ -101,7 +101,7 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi)
pi->pgres = PQexec(pi->dbh, pgbuf);
if (!pi->pgres) {
ulogd_log(ULOGD_DEBUG, "\n result false");
- return 1;
+ return -1;
}
if (PQresultStatus(pi->pgres) == PGRES_TUPLES_OK) {
@@ -141,7 +141,7 @@ static int get_columns_pgsql(struct ulogd_pluginstance *upi)
if (!pi->dbh) {
ulogd_log(ULOGD_ERROR, "no database handle\n");
- return 1;
+ return -1;
}
if (pi->db_inst.schema) {
@@ -228,7 +228,7 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi)
cp = connstr = malloc(len);
if (!connstr)
- return -ENOMEM;
+ return -1;
if (server[0])
cp += sprintf(cp, "host=%s ", server);
diff --git a/util/db.c b/util/db.c
index 7a6401f73609..cd9ebef077dd 100644
--- a/util/db.c
+++ b/util/db.c
@@ -111,7 +111,7 @@ ulogd_db_configure(struct ulogd_pluginstance *upi,
di->backlog_full = 0;
}
- return ret;
+ return 0;
}
int
@@ -131,7 +131,7 @@ ulogd_db_alloc_input_keys(struct ulogd_pluginstance *upi,
if (!upi->input.keys) {
upi->input.num_keys = 0;
ulogd_log(ULOGD_ERROR, "ENOMEM\n");
- return -ENOMEM;
+ return -1;
}
for (i = 0; i < num_keys; i++) {
@@ -170,10 +170,8 @@ ulogd_db_start(struct ulogd_pluginstance *upi)
if (di->ring.size > 0) {
/* allocate */
di->ring.ring = calloc(di->ring.size, sizeof(char) * di->ring.length);
- if (di->ring.ring == NULL) {
- ret = -1;
+ if (di->ring.ring == NULL)
goto db_error;
- }
di->ring.wr_place = di->ring.ring;
ulogd_log(ULOGD_NOTICE,
"Allocating %d elements of size %d for ring\n",
@@ -198,7 +196,7 @@ ulogd_db_start(struct ulogd_pluginstance *upi)
di->interp = &_interp_db_init;
- return ret;
+ return 0;
mutex_error:
pthread_mutex_destroy(&di->ring.mutex);
@@ -208,7 +206,7 @@ alloc_error:
free(di->ring.ring);
db_error:
di->driver->close_db(upi);
- return ret;
+ return -1;
}
/* this is a wrapper that just calls the current real interp function */
@@ -283,16 +281,18 @@ _interp_db_init(struct ulogd_pluginstance *upi)
_bind_sql_stmt(upi, di->stmt);
_add_to_backlog(upi, di->stmt, strlen(di->stmt));
}
- return 0;
+ return ULOGD_IRET_OK;
}
- if (di->driver->open_db(upi)) {
+ if (di->driver->open_db(upi) < 0) {
ulogd_log(ULOGD_ERROR, "can't establish database connection\n");
if (di->backlog_memcap && !di->backlog_full) {
_bind_sql_stmt(upi, di->stmt);
_add_to_backlog(upi, di->stmt, strlen(di->stmt));
}
- return _reconnect_db(upi);
+ if (_reconnect_db(upi) < 0)
+ return ULOGD_IRET_ERR;
+ return ULOGD_IRET_OK;
}
/* enable 'real' logging */
@@ -311,40 +311,48 @@ _interp_db_main(struct ulogd_pluginstance *upi)
{
struct db_instance *di = (struct db_instance *) &upi->private;
- if (di->ring.size)
- return _add_to_ring(upi, di);
+ if (di->ring.size) {
+ if (_add_to_ring(upi, di) < 0)
+ return ULOGD_IRET_ERR;
+ return ULOGD_IRET_OK;
+ }
_bind_sql_stmt(upi, di->stmt);
/* if backup log is not empty we add current query to it */
if (!llist_empty(&di->backlog)) {
int ret = _add_to_backlog(upi, di->stmt, strlen(di->stmt));
- if (ret == 0)
- return _process_backlog(upi);
- else {
- ret = _process_backlog(upi);
- if (ret)
- return ret;
- /* try adding once the data to backlog */
- return _add_to_backlog(upi, di->stmt, strlen(di->stmt));
+ if (ret == 0) {
+ if (_process_backlog(upi) < 0)
+ return ULOGD_IRET_ERR;
+ return ULOGD_IRET_OK;
}
+ ret = _process_backlog(upi);
+ if (ret < 0)
+ return ULOGD_IRET_ERR;
+ /* try adding once the data to backlog */
+ if (_add_to_backlog(upi, di->stmt, strlen(di->stmt)) < 0)
+ return ULOGD_IRET_ERR;
+ return ULOGD_IRET_OK;
}
if (di->driver->execute(upi, di->stmt, strlen(di->stmt)) < 0) {
_add_to_backlog(upi, di->stmt, strlen(di->stmt));
/* error occur, database connexion need to be closed */
di->driver->close_db(upi);
- return _reconnect_db(upi);
+ if (_reconnect_db(upi) < 0)
+ return ULOGD_IRET_ERR;
+ return ULOGD_IRET_OK;
}
- return 0;
+ return ULOGD_IRET_OK;
}
/* no connection, plugin disabled */
static int
_interp_db_disabled(struct ulogd_pluginstance *upi)
{
- return 0;
+ return ULOGD_IRET_OK;
}
static int
@@ -367,7 +375,6 @@ _reconnect_db(struct ulogd_pluginstance *upi)
/* Disable plugin permanently */
ulogd_log(ULOGD_ERROR, "permanently disabling plugin\n");
di->interp = &_interp_db_disabled;
-
return 0;
}
@@ -438,7 +445,7 @@ _create_sql_stmt(struct ulogd_pluginstance *upi)
di->stmt = malloc(size);
if (!di->stmt) {
ulogd_log(ULOGD_ERROR, "OOM!\n");
- return -ENOMEM;
+ return -1;
}
di->ring.length = size + 1;
@@ -717,7 +724,7 @@ _loop_reconnect_db(struct ulogd_pluginstance * upi)
di->driver->close_db(upi);
while (1) {
- if (di->driver->open_db(upi)) {
+ if (di->driver->open_db(upi) < 0) {
sleep(1);
} else {
return 0;
--
2.35.1
next prev parent reply other threads:[~2022-11-29 21:58 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-29 21:47 [PATCH ulogd2 v2 v2 00/34] Refactor of the DB output plug-ins Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 01/34] ulogd: fix parse-error check Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 02/34] filter: fix buffer sizes in filter plug-ins Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 03/34] output: JSON: remove incorrect config value check Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 04/34] db: fix back-log capacity checks Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 05/34] build: add checks to configure.ac Jeremy Sowden
2022-11-30 10:04 ` Jan Engelhardt
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 06/34] src: remove some trailing white space Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 07/34] src: remove zero-valued config-key fields Jeremy Sowden
2022-11-30 10:21 ` Jan Engelhardt
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 08/34] src: parenthesize config-entry macro arguments Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 09/34] src: define constructors and destructors consistently Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 10/34] src: remove `TIME_ERR` macro Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 11/34] src: remove superfluous casts Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 12/34] conffile: replace malloc+strcpy with strdup Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 13/34] output: remove zero-initialized `struct ulogd_plugin` members Jeremy Sowden
2022-11-30 10:26 ` Jan Engelhardt
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 14/34] output: de-duplicate allocation of input keys Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 15/34] db: reorganize source Jeremy Sowden
2022-11-29 21:47 ` Jeremy Sowden [this message]
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 17/34] db: change return type of two functions to `void` Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 18/34] db: open-code `_loop_reconnect_db` Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 19/34] db: improve calculation of sql statement length Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 20/34] db: refactor configuration Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 21/34] db: refactor ring-buffer initialization Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 22/34] db: refactor ring-buffer Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 23/34] db: refactor backlog Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 24/34] db: use `struct db_stmt` objects more widely Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 25/34] db: synchronize access to ring-buffer Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 26/34] db: avoid cancelling ring-buffer thread Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 27/34] db, IP2BIN: defer formatting of raw strings Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 28/34] db: add prep & exec support Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 29/34] output: mysql: " Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 30/34] output: pgsql: remove a couple of struct members Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 31/34] output: pgsql: remove variable-length arrays Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 32/34] output: pgsql: tidy up `open_db_pgsql` and fix memory leak Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 33/34] output: pgsql: add prep & exec support Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 34/34] output: sqlite3: reimplement using the common DB API Jeremy Sowden
2022-11-30 10:27 ` [PATCH ulogd2 v2 v2 00/34] Refactor of the DB output plug-ins Pablo Neira Ayuso
2022-11-30 16:03 ` Jeremy Sowden
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=20221129214749.247878-17-jeremy@azazel.net \
--to=jeremy@azazel.net \
--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).