Refactor of the DB output plug-ins
cover Refactor of the DB output plug-ins
In his feedback to my last series of clean-up patches at the beginning of the year, Pablo suggested consolidating some parallel implementations of the same functionality in the SQL output plug-ins. I already had some patches in the works aimed at tidying up the DB API. This patch-set is the result. In addition to the suggested de-duping and other tidy-ups, I have added prep & exec support in order to convert the sqlite3 plug-in to the DB API, and updated the MySQL and PostgreSQL plug-ins to use it as well (DBI doesn't do prep & exec).
This patch-set is structured as follows.
* Patches 1-4 are bug-fixes.
* Patches 5-13 are miscellaneous tidying.
* Patch 14 does the consolidation Pablo suggested.
* Patches 15-26 refactor and clean up the common DB API.
* Patches 27-28 add prep & exec support to the common DB API.
* Patch 29 converts the MySQL plug-in to use prep & exec.
* Patch 30-33 tidy up and convert the PostgreSQL plug-in to use prep & exec.
* Patch 34 converts the SQLite plug-in to use the common DB API.
Jeremy Sowden (34):
ulogd: fix parse-error check
filter: fix buffer overruns in filter plug-ins
output: JSON: remove incorrect config value check
db: fix back-log capacity checks
build: add checks to configure.ac
src: remove some trailing white space
src: remove zero-valued config-key fields
src: parenthesize config-entry macro arguments
src: define constructors and destructors consistently
src: remove TIME_ERR macro
src: remove superfluous casts
conffile: replace malloc+strcpy with strdup
output: remove zero-initialized struct ulogd_plugin members
output: de-duplicate allocation of input keys
db: reorganize source
db: use consistent integer return values to indicate errors
db: change return type of two functions to void
db: open-code _loop_reconnect_db
db: improve calculation of sql statement length
db: refactor configuration
db: refactor ring-buffer initialization
db: refactor ring-buffer
db: refactor backlog
db: use struct db_stmt objects more widely
db: synchronize access to ring-buffer
db: avoid cancelling ring-buffer thread
db, IP2BIN: defer formatting of raw strings
db: add prep & exec support
output: mysql: add prep & exec support
output: pgsql: remove a couple of struct members
output: pgsql: remove variable-length arrays
output: pgsql: tidy up open_db_pgsql and fix memory leak
output: pgsql: add prep & exec support
output: sqlite3: reimplement using the common DB API
cftest/cftest.c | 2 +- configure.ac | 47 +- filter/raw2packet/ulogd_raw2packet_BASE.c | 4 +- filter/ulogd_filter_HWHDR.c | 8 +- filter/ulogd_filter_IFINDEX.c | 4 +- filter/ulogd_filter_IP2BIN.c | 73 +- filter/ulogd_filter_IP2HBIN.c | 4 +- filter/ulogd_filter_IP2STR.c | 10 +- filter/ulogd_filter_MARK.c | 7 +- filter/ulogd_filter_PRINTFLOW.c | 4 +- filter/ulogd_filter_PRINTPKT.c | 4 +- filter/ulogd_filter_PWSNIFF.c | 2 +- include/ulogd/db.h | 121 +- input/flow/ulogd_inpflow_NFCT.c | 37 +- input/packet/ulogd_inppkt_NFLOG.c | 47 +- input/packet/ulogd_inppkt_ULOG.c | 55 +- input/packet/ulogd_inppkt_UNIXSOCK.c | 5 - input/sum/ulogd_inpflow_NFACCT.c | 15 +- libipulog/libipulog.c | 2 +- libipulog/ulog_test.c | 2 +- output/dbi/ulogd_output_DBI.c | 132 +-- output/ipfix/ulogd_output_IPFIX.c | 38 +- output/mysql/ulogd_output_MYSQL.c | 257 +++-- output/pcap/ulogd_output_PCAP.c | 30 +- output/pgsql/ulogd_output_PGSQL.c | 416 ++++--- output/sqlite3/ulogd_output_SQLITE3.c | 480 +++----- output/ulogd_output_GPRINT.c | 7 +- output/ulogd_output_GRAPHITE.c | 9 +- output/ulogd_output_JSON.c | 29 +- output/ulogd_output_LOGEMU.c | 2 - output/ulogd_output_NACCT.c | 5 +- output/ulogd_output_OPRINT.c | 5 +- output/ulogd_output_SYSLOG.c | 16 +- output/ulogd_output_XML.c | 5 - src/conffile.c | 4 +- src/hash.c | 4 +- src/ulogd.c | 80 +- util/db.c | 1244 ++++++++++++++------- 38 files changed, 1829 insertions(+), 1387 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/34 ulogd: fix parse-error check
If config_parse_file returns -ERRTOOLONG, config_errce may be NULL. However, the calling function was checking whether config_errce->key was NULL instead.
--- src/ulogd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/ulogd.c +1 -1
@@ -1134,7 +1134,7 @@ static int parse_conffile(const char *section, struct config_keyset *ce) "section \"%s\" not found\n", section);break ;case -ERRTOOLONG : -if (config_errce->key) +if (config_errce)ulogd_log (ULOGD_ERROR ,"string value too long for key \"%s\"\n" , config_errce->key);
Signature · 1 line
2.35.1
PATCH 2/34 filter: fix buffer overruns in filter plug-ins
In reply to Jeremy SowdenThree of the filter plug-ins define arrays to hold output key values. The arrays are sized based on the values of enums. For example:
enum output_keys {
KEY_MAC_TYPE,
KEY_MAC_PROTOCOL,
KEY_MAC_SADDR,
START_KEY = KEY_MAC_SADDR,
KEY_MAC_DADDR,
KEY_MAC_ADDR,
MAX_KEY = KEY_MAC_ADDR,
};
static char hwmac_str[MAX_KEY - START_KEY][HWADDR_LENGTH];The arrays are indexed by subtracting START_KEY from the enum value of the key currently being processed: hwmac_str[okey - START_KEY]. However, this means that the last key (KEY_MAC_ADDR in this example) will run off the end of the array. Increase the size of the arrays.
Also some small white-space tweaks.
--- filter/ulogd_filter_HWHDR.c | 4 ++-- filter/ulogd_filter_IP2BIN.c | 12 ++++++------ filter/ulogd_filter_IP2STR.c | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-)
filter/ulogd_filter_HWHDR.c +2 -2
@@ -109,7 +109,7 @@ static struct ulogd_key mac2str_keys[] = { }, }; -static char hwmac_str[MAX_KEY -START_KEY ][HWADDR_LENGTH ]; +static char hwmac_str[MAX_KEY -START_KEY +1 ][HWADDR_LENGTH ];static int parse_mac2str (struct ulogd_key *ret,unsigned char *mac,int okey,int len)
@@ -126,7 +126,7 @@ static int parse_mac2str(struct ulogd_key *ret, unsigned char *mac, buf_cur = hwmac_str[okey -START_KEY ];for (i =0 ; i < len; i++) buf_cur +=sprintf (buf_cur,"%02x%c" , mac[i], - i == len -1 ?0 :':' ); + i == len -1 ?0 :':' );okey_set_ptr (&ret[okey], hwmac_str[okey -START_KEY ]);
filter/ulogd_filter_IP2BIN.c +6 -6
@@ -114,7 +114,7 @@ static struct ulogd_key ip2bin_keys[] = { }; -static char ipbin_array[MAX_KEY -START_KEY ][IPADDR_LENGTH ]; +static char ipbin_array[MAX_KEY -START_KEY +1 ][IPADDR_LENGTH ];/** * Convert IPv4 address (as 32-bit unsigned integer) to IPv6 address:
@@ -128,7 +128,7 @@ static inline void uint32_to_ipv6(const uint32_t ipv4, struct in6_addr *ipv6) ipv6->s6_addr32[3 ] = ipv4; } -static int ip2bin (struct ulogd_key * inp,int index,int oindex) +static int ip2bin (struct ulogd_key *inp,int index,int oindex) {char family =ikey_get_u8 (&inp[KEY_OOB_FAMILY ]);char convfamily = family;
@@ -184,7 +184,7 @@ static int ip2bin(struct ulogd_key* inp, int index, int oindex) addr8 = &addr->s6_addr[0 ];for (i =0 ; i <4 ; i++) { written =sprintf (buffer,"%02x%02x%02x%02x" , - addr8[0 ], addr8[1 ], addr8[2 ], addr8[3 ]); + addr8[0 ], addr8[1 ], addr8[2 ], addr8[3 ]);if (written !=2 *4 ) { buffer[0 ] =0 ;return ULOGD_IRET_ERR ;
@@ -207,11 +207,11 @@ static int interp_ip2bin(struct ulogd_pluginstance *pi)/* Iter on all addr fields */ for (i =START_KEY ; i <MAX_KEY ; i++) {if (pp_is_valid (inp, i)) { - fret =ip2bin (inp, i, i-START_KEY ); + fret =ip2bin (inp, i, i -START_KEY );if (fret !=ULOGD_IRET_OK )return fret; -okey_set_ptr (&ret[i-START_KEY ], - ipbin_array[i-START_KEY ]); +okey_set_ptr (&ret[i -START_KEY ], + ipbin_array[i -START_KEY ]); } }
filter/ulogd_filter_IP2STR.c +3 -3
@@ -137,7 +137,7 @@ static struct ulogd_key ip2str_keys[] = { }, }; -static char ipstr_array[MAX_KEY -START_KEY ][IPADDR_LENGTH ]; +static char ipstr_array[MAX_KEY -START_KEY +1 ][IPADDR_LENGTH ];static int ip2str (struct ulogd_key *inp,int index,int oindex) {
@@ -197,10 +197,10 @@ static int interp_ip2str(struct ulogd_pluginstance *pi)/* Iter on all addr fields */ for (i =START_KEY ; i <=MAX_KEY ; i++) {if (pp_is_valid (inp, i)) { - fret =ip2str (inp, i, i-START_KEY ); + fret =ip2str (inp, i, i -START_KEY );if (fret !=ULOGD_IRET_OK )return fret; -okey_set_ptr (&ret[i-START_KEY ], +okey_set_ptr (&ret[i -START_KEY ], ipstr_array[i-START_KEY ]); } }
Signature · 1 line
2.35.1
PATCH 3/34 output: JSON: remove incorrect config value check
In reply to Jeremy SowdenThe u.string member of a config entry is an array, and so never NULL. Output the device string unconditionally.
--- output/ulogd_output_JSON.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
output/ulogd_output_JSON.c +3 -5
@@ -277,7 +277,7 @@ static int json_interp(struct ulogd_pluginstance *upi) {struct json_priv *opi = (struct json_priv *) &upi->private;unsigned int i; -char *buf, *tmp; +char *dvc, *buf, *tmp;size_t buflen;json_t *msg;
@@ -335,10 +335,8 @@ static int json_interp(struct ulogd_pluginstance *upi)json_object_set_new (msg,"timestamp" ,json_string (timestr)); } -if (upi->config_kset->ces[JSON_CONF_DEVICE ].u.string) { -char *dvc = upi->config_kset->ces[JSON_CONF_DEVICE ].u.string; -json_object_set_new (msg,"dvc" ,json_string (dvc)); - } + dvc = upi->config_kset->ces[JSON_CONF_DEVICE ].u.string; +json_object_set_new (msg,"dvc" ,json_string (dvc));for (i =0 ; i < upi->input.num_keys; i++) {struct ulogd_key *key = upi->input.keys[i].u.source;
Signature · 1 line
2.35.1
PATCH 4/34 db: fix back-log capacity checks
In reply to Jeremy SowdenPreviously, when adding queries to the back-log, the memory usage is incremented and decremented by the size of the query structure and the length of the SQL statement, sizeof(struct db_stmt) + len. However, when checking whether there was available capacity to add a new query, the struct size was ignored. Amend the check to include the struct size, and also account for the NUL that terminates the SQL.
--- util/db.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
util/db.c +7 -4
@@ -404,14 +404,17 @@ static void __format_query_db(struct ulogd_pluginstance *upi, char *start)static int __add_to_backlog (struct ulogd_pluginstance *upi,const char *stmt,unsigned int len) {struct db_instance *di = (struct db_instance *) &upi->private; +unsigned int query_size;struct db_stmt *query;/* check if we are using backlog */ if (di->backlog_memcap ==0 )return 0 ; + query_size =sizeof (*query) + len +1 ; +/* check len against backlog */ -if (len + di->backlog_memusage > di->backlog_memcap) { +if (query_size + di->backlog_memcap - di->backlog_memusage) {if (di->backlog_full ==0 )ulogd_log (ULOGD_ERROR ,"Backlog is full starting to reject events.\n" );
@@ -419,7 +422,7 @@ static int __add_to_backlog(struct ulogd_pluginstance *upi, const char *stmt, unreturn -1 ; } - query =malloc (sizeof (struct db_stmt )); + query =malloc (sizeof (*query));if (query ==NULL )return -1 ;
@@ -431,7 +434,7 @@ static int __add_to_backlog(struct ulogd_pluginstance *upi, const char *stmt, unreturn -1 ; } - di->backlog_memusage += len +sizeof (struct db_stmt ); + di->backlog_memusage += query_size; di->backlog_full =0 ;llist_add_tail (&query->list, &di->backlog);
@@ -489,7 +492,7 @@ static int __treat_backlog(struct ulogd_pluginstance *upi) di->driver->close_db (upi);return _init_reconnect (upi); }else { - di->backlog_memusage -= query->len +sizeof (struct db_stmt ); + di->backlog_memusage -=sizeof (*query) + query->len +1 ;llist_del (&query->list);free (query->stmt);free (query);
Signature · 1 line
2.35.1
PATCH 5/34 build: add checks to configure.ac
In reply to Jeremy SowdenAutoscan complains about a number of missing function, header and type checks.
--- configure.ac | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-)
configure.ac +44 -3
@@ -22,17 +22,58 @@ AC_SUBST([libdl_LIBS]) dnl Checks for header files. AC_HEADER_DIRENT -AC_CHECK_HEADERS([fcntl.h unistd.h]) +AC_CHECK_HEADER_STDBOOL +AC_CHECK_HEADERS([arpa/inet.h \ + fcntl.h \ + inttypes.h \ + netdb.h \ + netinet/in.h \ + stdint.h \ + sys/param.h \ + sys/socket.h \ + sys/time.h \ + syslog.h \ + unistd.h]) dnl Checks for typedefs, structures, and compiler characteristics. AC_C_CONST -AC_TYPE_SIZE_T +AC_C_INLINE AC_STRUCT_TM AC_SYS_LARGEFILE +AC_TYPE_INT8_T +AC_TYPE_INT16_T +AC_TYPE_INT32_T +AC_TYPE_INT64_T +AC_TYPE_PID_T +AC_TYPE_SIZE_T +AC_TYPE_SSIZE_T +AC_TYPE_UID_T +AC_TYPE_UINT8_T +AC_TYPE_UINT16_T +AC_TYPE_UINT32_T +AC_TYPE_UINT64_T dnl Checks for library functions. +AC_FUNC_CHOWN +AC_FUNC_MALLOC +AC_FUNC_REALLOC AC_FUNC_VPRINTF -AC_CHECK_FUNCS([socket strerror]) +AC_CHECK_FUNCS([alarm \ + ftruncate \ + gethostname \ + gettimeofday \ + localtime_r \ + memmove \ + memset \ + select \ + socket \ + strcasecmp \ + strchr \ + strdup \ + strerror \ + strncasecmp \ + strndup \ + strtoul]) AC_SEARCH_LIBS([pthread_create], [pthread], [libpthread_LIBS="$LIBS"; LIBS=""]) AC_SUBST([libpthread_LIBS])
Signature · 1 line
2.35.1
PATCH 6/34 src: remove some trailing white space
In reply to Jeremy Sowden--- output/dbi/ulogd_output_DBI.c | 26 +++++----- output/mysql/ulogd_output_MYSQL.c | 24 ++++----- output/pcap/ulogd_output_PCAP.c | 18 +++---- output/pgsql/ulogd_output_PGSQL.c | 34 ++++++------ output/sqlite3/ulogd_output_SQLITE3.c | 18 +++---- src/ulogd.c | 74 +++++++++++++-------------- util/db.c | 8 +-- 7 files changed, 101 insertions(+), 101 deletions(-)
output/dbi/ulogd_output_DBI.c +13 -13
@@ -4,8 +4,8 @@* layer * * (C) 2000-2008 by Pierre Chifflier <[email protected]> -* This software is distributed under the terms of GNU GPL -* +* This software is distributed under the terms of GNU GPL +* * This plugin is based on the PostgreSQL plugin made by Harald Welte. * */
@@ -44,23 +44,23 @@ static struct config_keyset dbi_kset = { .num_ces = DB_CE_NUM + 7, .ces = { DB_CES, - { - .key = "db", + { + .key = "db", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_MANDATORY, }, { - .key = "host", + .key = "host", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_NONE, }, - { - .key = "user", + { + .key = "user", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_MANDATORY, }, { - .key = "pass", + .key = "pass", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_NONE, },
@@ -70,13 +70,13 @@ static struct config_keyset dbi_kset = { .options = CONFIG_OPT_NONE, }, { - .key = "schema", + .key = "schema", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_NONE, .u.string = "public", }, - { - .key = "dbtype", + { + .key = "dbtype", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_MANDATORY, },
@@ -291,8 +291,8 @@ static int configure_dbi(struct ulogd_pluginstance *upi,return ulogd_db_configure (upi, stack); } -static struct ulogd_plugin dbi_plugin = { - .name ="DBI" , +static struct ulogd_plugin dbi_plugin = { + .name ="DBI" , .input = { .keys =NULL , .num_keys =0 ,
output/mysql/ulogd_output_MYSQL.c +12 -12
@@ -5,7 +5,7 @@* (C) 2000-2005 by Harald Welte <[email protected]> * * This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License version 2 +* it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation * * This program is distributed in the hope that it will be useful,
@@ -64,22 +64,22 @@ static struct config_keyset kset_mysql = { .ces = { DB_CES, { - .key = "db", + .key = "db", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_MANDATORY, }, { - .key = "host", + .key = "host", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_MANDATORY, }, { - .key = "user", + .key = "user", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_MANDATORY, }, { - .key = "pass", + .key = "pass", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_MANDATORY, },
@@ -107,7 +107,7 @@ static int get_columns_mysql(struct ulogd_pluginstance *upi)return -1 ; } - result =mysql_list_fields (mi->dbh, + result =mysql_list_fields (mi->dbh,table_ce (upi->config_kset).u.string,NULL );if (!result) { ulogd_log(ULOGD_ERROR, "error in list_fields(): %s\n",
@@ -176,7 +176,7 @@ static int open_db_mysql(struct ulogd_pluginstance *upi)char *db =db_ce (upi->config_kset).u.string;#ifdef MYSQL_OPT_RECONNECT my_bool trueval =1 ; -#endif +#endif mi->dbh =mysql_init (NULL );if (!mi->dbh) {
@@ -185,20 +185,20 @@ static int open_db_mysql(struct ulogd_pluginstance *upi) }if (connect_timeout) -mysql_options (mi->dbh,MYSQL_OPT_CONNECT_TIMEOUT , +mysql_options (mi->dbh,MYSQL_OPT_CONNECT_TIMEOUT , (const char *) &connect_timeout);#ifdef MYSQL_OPT_RECONNECT # if defined(MYSQL_VERSION_ID ) && (MYSQL_VERSION_ID >=50019 )mysql_options (mi->dbh,MYSQL_OPT_RECONNECT , &trueval);# endif -#endif +#endif if (!mysql_real_connect (mi->dbh, server, user, pass, db, port,NULL ,0 )) {ulogd_log (ULOGD_ERROR ,"can't connect to db: %s\n" ,mysql_error (mi->dbh));return -1 ; } - +#ifdef MYSQL_OPT_RECONNECT # if defined(MYSQL_VERSION_ID ) && (MYSQL_VERSION_ID <50019 )mysql_options (mi->dbh,MYSQL_OPT_RECONNECT , &trueval);
@@ -263,7 +263,7 @@ static struct ulogd_plugin plugin_mysql = { .input = { .keys = NULL, .num_keys = 0, - .type =ULOGD_DTYPE_PACKET |ULOGD_DTYPE_FLOW , + .type = ULOGD_DTYPE_PACKET | ULOGD_DTYPE_FLOW, }, .output = { .type = ULOGD_DTYPE_SINK,
@@ -280,7 +280,7 @@ static struct ulogd_plugin plugin_mysql = {void __attribute__ ((constructor))init (void ); -void init (void ) +void init (void ) {ulogd_register_plugin (&plugin_mysql); }
output/pcap/ulogd_output_PCAP.c +9 -9
@@ -5,7 +5,7 @@* (C) 2002-2005 by Harald Welte <[email protected]> * * This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License version 2 +* it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation * * This program is distributed in the hope that it will be useful,
@@ -91,14 +91,14 @@ struct pcap_sf_pkthdr {static struct config_keyset pcap_kset = { .num_ces =2 , .ces = { - { - .key ="file" , - .type =CONFIG_TYPE_STRING , + { + .key ="file" , + .type =CONFIG_TYPE_STRING , .options =CONFIG_OPT_NONE , .u = { .string =ULOGD_PCAP_DEFAULT }, }, - { - .key ="sync" , + { + .key ="sync" , .type =CONFIG_TYPE_INT , .options =CONFIG_OPT_NONE , .u = { .value =ULOGD_PCAP_SYNC_DEFAULT },
@@ -112,7 +112,7 @@ struct pcap_instance {struct intr_id {char * name; -unsigned int id; +unsigned int id; };#define INTR_IDS 7
@@ -242,11 +242,11 @@ static int append_create_outfile(struct ulogd_pluginstance *upi) }else { pi->of =fopen (filename,"a" );if (!pi->of) { -ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , +ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , filename,strerror (errno));return -EPERM ; - } + } }return 0 ;
output/pgsql/ulogd_output_PGSQL.c +17 -17
@@ -2,9 +2,9 @@* * ulogd output plugin for logging to a PGSQL database * -* (C) 2000-2005 by Harald Welte <[email protected]> -* This software is distributed under the terms of GNU GPL -* +* (C) 2000-2005 by Harald Welte <[email protected]> +* This software is distributed under the terms of GNU GPL +* * This plugin is based on the MySQL plugin made by Harald Welte. * The support PostgreSQL were made by Jakab Laszlo. *
@@ -41,23 +41,23 @@ static struct config_keyset pgsql_kset = { .num_ces = DB_CE_NUM + 7, .ces = { DB_CES, - { - .key = "db", + { + .key = "db", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_MANDATORY, }, { - .key = "host", + .key = "host", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_NONE, }, - { - .key = "user", + { + .key = "user", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_MANDATORY, }, { - .key = "pass", + .key = "pass", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_NONE, },
@@ -67,7 +67,7 @@ static struct config_keyset pgsql_kset = { .options = CONFIG_OPT_NONE, }, { - .key = "schema", + .key = "schema", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_NONE, .u.string = "public",
@@ -94,7 +94,7 @@ static struct config_keyset pgsql_kset = {static int pgsql_namespace (struct ulogd_pluginstance *upi) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private; -char pgbuf[strlen (PGSQL_HAVE_NAMESPACE_TEMPLATE ) + +char pgbuf[strlen (PGSQL_HAVE_NAMESPACE_TEMPLATE ) +strlen (schema_ce (upi->config_kset).u.string) +1 ];if (!pi->dbh)
@@ -103,7 +103,7 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi)sprintf (pgbuf,PGSQL_HAVE_NAMESPACE_TEMPLATE ,schema_ce (upi->config_kset).u.string);ulogd_log (ULOGD_DEBUG ,"%s\n" , pgbuf); - + pi->pgres =PQexec (pi->dbh, pgbuf);if (!pi->pgres) {ulogd_log (ULOGD_DEBUG ,"\n result false" );
@@ -126,7 +126,7 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi) }PQclear (pi->pgres); - +return 0 ; }
@@ -141,7 +141,7 @@ static int get_columns_pgsql(struct ulogd_pluginstance *upi) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private;char pgbuf[strlen (PGSQL_GETCOLUMN_TEMPLATE_SCHEMA ) - +strlen (table_ce (upi->config_kset).u.string) + +strlen (table_ce (upi->config_kset).u.string) +strlen (pi->db_inst.schema) +2 ];int i;
@@ -303,7 +303,7 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi)static int escape_string_pgsql (struct ulogd_pluginstance *upi,char *dst,const char *src,unsigned int len) { -return PQescapeString (dst, src,strlen (src)); +return PQescapeString (dst, src,strlen (src)); } static int execute_pgsql(struct ulogd_pluginstance *upi,
@@ -342,8 +342,8 @@ static int configure_pgsql(struct ulogd_pluginstance *upi,return ulogd_db_configure (upi, stack); } -static struct ulogd_plugin pgsql_plugin = { - .name ="PGSQL" , +static struct ulogd_plugin pgsql_plugin = { + .name ="PGSQL" , .input = { .keys =NULL , .num_keys =0 ,
output/sqlite3/ulogd_output_SQLITE3.c +9 -9
@@ -4,7 +4,7 @@* (C) 2005 by Ben La Monica <[email protected]> * * This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License version 2 +* it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation * * This program is distributed in the hope that it will be useful,
@@ -15,7 +15,7 @@* You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -* +* * This module has been adapted from the ulogd_MYSQL.c written by * Harald Welte <[email protected]> * Alex Janssen <[email protected]>
@@ -24,7 +24,7 @@* at http://www.pojo.us/ulogd/ * * 2005-02-09 Harald Welte <[email protected]>: -* - port to ulogd-1.20 +* - port to ulogd-1.20 * * 2006-10-09 Holger Eitzenberger <[email protected]> * - port to ulogd-2.00
@@ -160,11 +160,11 @@ sqlite3_interp(struct ulogd_pluginstance *pi)case ULOGD_RET_INT64 : ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.i64);break ; - +case ULOGD_RET_UINT8 : ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.ui8);break ; - +case ULOGD_RET_UINT16 : ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.ui16);break ;
@@ -205,7 +205,7 @@ sqlite3_interp(struct ulogd_pluginstance *pi) err_bind:ulogd_log (ULOGD_ERROR ,"SQLITE: bind: %s\n" ,sqlite3_errmsg (priv->dbh)); - +return ULOGD_IRET_ERR ; }
@@ -418,8 +418,8 @@ sqlite3_stop(struct ulogd_pluginstance *pi)return 0 ; } -static struct ulogd_plugin sqlite3_plugin = { - .name ="SQLITE3" , +static struct ulogd_plugin sqlite3_plugin = { + .name ="SQLITE3" , .input = { .type =ULOGD_DTYPE_PACKET |ULOGD_DTYPE_FLOW , },
@@ -438,7 +438,7 @@ static struct ulogd_plugin sqlite3_plugin = {static void init (void ) __attribute__((constructor));static void -init (void ) +init (void ) {ulogd_register_plugin (&sqlite3_plugin); }
src/ulogd.c +37 -37
264 lines not on this page · raw message
util/db.c +4 -4
@@ -10,7 +10,7 @@* (C) 2008,2013 Eric Leblond <[email protected]> * * This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License version 2 +* it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation * * This program is distributed in the hope that it will be useful,
@@ -21,7 +21,7 @@* You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -* +* */ #include <unistd.h>
@@ -170,7 +170,7 @@ int ulogd_db_configure(struct ulogd_pluginstance *upi, ret = di->driver->get_columns (upi);if (ret <0 )ulogd_log (ULOGD_ERROR ,"error in get_columns\n" ); - +/* Close database, since ulogd core could just call configure * but abort during input key resolving routines. configure * doesn't have a destructor... */
@@ -315,7 +315,7 @@ static int _init_reconnect(struct ulogd_pluginstance *upi)/* Disable plugin permanently */ ulogd_log (ULOGD_ERROR ,"permanently disabling plugin\n" ); di->interp = &disabled_interp_db; - +return 0 ; }
Signature · 1 line
2.35.1
PATCH 7/34 src: remove zero-valued config-key fields
In reply to Jeremy SowdenStruct members are zero-initialized as a matter of course.
--- filter/ulogd_filter_MARK.c | 3 -- include/ulogd/db.h | 1 - input/flow/ulogd_inpflow_NFCT.c | 14 --------- input/packet/ulogd_inppkt_NFLOG.c | 19 ------------ input/packet/ulogd_inppkt_ULOG.c | 44 ++++++++++++---------------- input/packet/ulogd_inppkt_UNIXSOCK.c | 5 ---- input/sum/ulogd_inpflow_NFACCT.c | 5 ---- output/dbi/ulogd_output_DBI.c | 4 --- output/ipfix/ulogd_output_IPFIX.c | 2 -- output/pcap/ulogd_output_PCAP.c | 2 -- output/pgsql/ulogd_output_PGSQL.c | 5 ---- output/ulogd_output_GPRINT.c | 7 +---- output/ulogd_output_GRAPHITE.c | 3 -- output/ulogd_output_JSON.c | 13 +------- output/ulogd_output_LOGEMU.c | 2 -- output/ulogd_output_NACCT.c | 5 +--- output/ulogd_output_OPRINT.c | 5 +--- output/ulogd_output_SYSLOG.c | 16 +++++----- output/ulogd_output_XML.c | 5 ---- src/ulogd.c | 2 -- 20 files changed, 30 insertions(+), 132 deletions(-)
filter/ulogd_filter_MARK.c +0 -3
@@ -35,13 +35,10 @@ static struct config_keyset libulog_kset = { [MARK_MARK] = { .key = "mark", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, [MARK_MASK] = { .key = "mask", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = 0xffffffff, },
include/ulogd/db.h +0 -1
@@ -90,7 +90,6 @@ struct db_instance { { \ .key = "backlog_memcap", \ .type = CONFIG_TYPE_INT, \ - .u.value = 0, \ }, \ { \ .key = "backlog_oneshot_requests", \
input/flow/ulogd_inpflow_NFCT.c +0 -14
@@ -83,71 +83,57 @@ static struct config_keyset nfct_kset = { { .key = "pollinterval", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "hash_enable", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = 1, }, { .key = "hash_buckets", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = HTABLE_SIZE, }, { .key = "hash_max_entries", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = MAX_ENTRIES, }, { .key = "event_mask", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = EVENT_MASK, }, { .key = "netlink_socket_buffer_size", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = 0, }, { .key = "netlink_socket_buffer_maxsize", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = 0, }, { .key = "netlink_resync_timeout", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = 60, }, { .key = "reliable", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "accept_src_filter", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, { .key = "accept_dst_filter", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, { .key = "accept_proto_filter", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, }, };
input/packet/ulogd_inppkt_NFLOG.c +0 -19
@@ -45,69 +45,50 @@ static struct config_keyset libulog_kset = { { .key = "bufsize", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = NFLOG_BUFSIZE_DEFAULT, }, { .key = "group", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = NFLOG_GROUP_DEFAULT, }, { .key = "unbind", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = 1, }, { .key = "bind", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "seq_local", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "seq_global", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "numeric_label", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "netlink_socket_buffer_size", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "netlink_socket_buffer_maxsize", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "netlink_qthreshold", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "netlink_qtimeout", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "attach_conntrack",
input/packet/ulogd_inppkt_ULOG.c +19 -25
50 lines not on this page · raw message
input/packet/ulogd_inppkt_UNIXSOCK.c +0 -5
@@ -290,30 +290,25 @@ static struct config_keyset libunixsock_kset = { { .key = "socket_path", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, .u.string = UNIXSOCK_UNIXPATH_DEFAULT, }, { .key = "bufsize", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = UNIXSOCK_BUFSIZE_DEFAULT, }, { .key = "perms", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = UNIXSOCK_PERMS_DEFAULT, }, { .key = "owner", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, { .key = "group", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, }, };
input/sum/ulogd_inpflow_NFACCT.c +0 -5
@@ -38,20 +38,15 @@ static struct config_keyset nfacct_kset = { { .key = "pollinterval", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, }, { .key = "zerocounter", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = 1, }, { .key = "timestamp", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u.value = 0, } }, .num_ces = 3,
output/dbi/ulogd_output_DBI.c +0 -4
@@ -52,7 +52,6 @@ static struct config_keyset dbi_kset = { { .key = "host", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, { .key = "user",
@@ -62,17 +61,14 @@ static struct config_keyset dbi_kset = { { .key = "pass", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, { .key = "port", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, }, { .key = "schema", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, .u.string = "public", }, {
output/ipfix/ulogd_output_IPFIX.c +0 -2
@@ -53,12 +53,10 @@ static const struct config_keyset ipfix_kset = { { .key = "oid", .type = CONFIG_TYPE_INT, - .u.value =0 }, { .key = "host", .type = CONFIG_TYPE_STRING, - .u.string ="" }, { .key = "port",
output/pcap/ulogd_output_PCAP.c +0 -2
@@ -94,13 +94,11 @@ static struct config_keyset pcap_kset = { { .key = "file", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, .u = { .string = ULOGD_PCAP_DEFAULT }, }, { .key = "sync", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u = { .value = ULOGD_PCAP_SYNC_DEFAULT }, }, },
output/pgsql/ulogd_output_PGSQL.c +0 -5
@@ -49,7 +49,6 @@ static struct config_keyset pgsql_kset = { { .key = "host", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, { .key = "user",
@@ -59,23 +58,19 @@ static struct config_keyset pgsql_kset = { { .key = "pass", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, { .key = "port", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, }, { .key = "schema", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, .u.string = "public", }, { .key = "connstring", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, }, };
output/ulogd_output_GPRINT.c +1 -6
@@ -51,20 +51,15 @@ static struct config_keyset gprint_kset = { [GPRINT_CONF_FILENAME ] = { .key ="file" , .type =CONFIG_TYPE_STRING , - .options =CONFIG_OPT_NONE , - .u = {.string =ULOGD_GPRINT_DEFAULT }, + .u = { .string =ULOGD_GPRINT_DEFAULT }, }, [GPRINT_CONF_SYNC ] = { .key ="sync" , .type =CONFIG_TYPE_INT , - .options =CONFIG_OPT_NONE , - .u = { .value =0 }, }, [GPRINT_CONF_TIMESTAMP ] = { .key ="timestamp" , .type =CONFIG_TYPE_INT , - .options =CONFIG_OPT_NONE , - .u = { .value =0 }, }, }, };
output/ulogd_output_GRAPHITE.c +0 -3
@@ -67,17 +67,14 @@ static struct config_keyset graphite_kset = { { .key = "host", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, { .key = "port", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, { .key = "prefix", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, }, }, };
output/ulogd_output_JSON.c +1 -12
@@ -87,26 +87,20 @@ static struct config_keyset json_kset = { [JSON_CONF_FILENAME] = { .key = "file", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, - .u = {.string = ULOGD_JSON_DEFAULT }, + .u = { .string = ULOGD_JSON_DEFAULT }, }, [JSON_CONF_SYNC] = { .key = "sync", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u = { .value = 0 }, }, [JSON_CONF_TIMESTAMP] = { .key = "timestamp", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u = { .value = 1 }, }, [JSON_CONF_EVENTV1] = { .key = "eventv1", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u = { .value = 0 }, }, [JSON_CONF_DEVICE] = { .key = "device",
@@ -117,25 +111,20 @@ static struct config_keyset json_kset = { [JSON_CONF_BOOLEAN_LABEL ] = { .key ="boolean_label" , .type =CONFIG_TYPE_INT , - .options =CONFIG_OPT_NONE , - .u = { .value =0 }, }, [JSON_CONF_MODE ] = { .key ="mode" , .type =CONFIG_TYPE_STRING , - .options =CONFIG_OPT_NONE , .u = { .string ="file" }, }, [JSON_CONF_HOST ] = { .key ="host" , .type =CONFIG_TYPE_STRING , - .options =CONFIG_OPT_NONE , .u = { .string ="127.0.0.1" }, }, [JSON_CONF_PORT ] = { .key ="port" , .type =CONFIG_TYPE_STRING , - .options =CONFIG_OPT_NONE , .u = { .string ="12345" }, }, },
output/ulogd_output_LOGEMU.c +0 -2
@@ -64,13 +64,11 @@ static struct config_keyset logemu_kset = { { .key = "file", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, .u = { .string = ULOGD_LOGEMU_DEFAULT }, }, { .key = "sync", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u = { .value = ULOGD_LOGEMU_SYNC_DEFAULT }, }, },
output/ulogd_output_NACCT.c +1 -4
@@ -163,14 +163,11 @@ static struct config_keyset nacct_kset = { { .key = "file", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, - .u = {.string = NACCT_FILE_DEFAULT }, + .u = { .string = NACCT_FILE_DEFAULT }, }, { .key = "sync", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u = { .value = 0 }, }, }, };
output/ulogd_output_OPRINT.c +1 -4
@@ -105,14 +105,11 @@ static struct config_keyset oprint_kset = { { .key = "file", .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, - .u = {.string = ULOGD_OPRINT_DEFAULT }, + .u = { .string = ULOGD_OPRINT_DEFAULT }, }, { .key = "sync", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, - .u = { .value = 0 }, }, }, };
output/ulogd_output_SYSLOG.c +7 -9
@@ -49,16 +49,14 @@ static struct config_keyset syslog_kset = { .num_ces = 2, .ces = { { - .key = "facility", - .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, - .u = { .string = SYSLOG_FACILITY_DEFAULT } + .key = "facility", + .type = CONFIG_TYPE_STRING, + .u = { .string = SYSLOG_FACILITY_DEFAULT } }, - { - .key = "level", - .type = CONFIG_TYPE_STRING, - .options = CONFIG_OPT_NONE, - .u = { .string = SYSLOG_LEVEL_DEFAULT } + { + .key = "level", + .type = CONFIG_TYPE_STRING, + .u = { .string = SYSLOG_LEVEL_DEFAULT } }, }, };
output/ulogd_output_XML.c +0 -5
@@ -75,20 +75,15 @@ static struct config_keyset xml_kset = { [CFG_XML_DIR ] = { .key ="directory" , .type =CONFIG_TYPE_STRING , - .options =CONFIG_OPT_NONE , .u = { .string =ULOGD_XML_DEFAULT_DIR }, }, [CFG_XML_SYNC ] = { .key ="sync" , .type =CONFIG_TYPE_INT , - .options =CONFIG_OPT_NONE , - .u = { .value =0 }, }, [CFG_XML_STDOUT ] = { .key ="stdout" , .type =CONFIG_TYPE_INT , - .options =CONFIG_OPT_NONE , - .u = { .value =0 }, }, }, };
src/ulogd.c +0 -2
@@ -133,7 +133,6 @@ static struct config_keyset ulogd_kset = { { .key = "logfile", .type = CONFIG_TYPE_CALLBACK, - .options = CONFIG_OPT_NONE, .u.parser = &logfile_open, }, {
@@ -145,7 +144,6 @@ static struct config_keyset ulogd_kset = { { .key = "loglevel", .type = CONFIG_TYPE_INT, - .options = CONFIG_OPT_NONE, .u.value = ULOGD_NOTICE, }, {
Signature · 1 line
2.35.1
PATCH 8/34 src: parenthesize config-entry macro arguments
In reply to Jeremy SowdenIt's avoids problems with operator precedence. For example, with no parentheses:
#define blah_ce(x) (x->ces[0])
blah_ce(&whatever)
yields:
(&(whatever->ces[0]))
instead of:
((&whatever)->ces[0])
--- include/ulogd/db.h | 14 +++++++------- input/flow/ulogd_inpflow_NFCT.c | 18 +++++++++--------- input/packet/ulogd_inppkt_NFLOG.c | 24 ++++++++++++------------ input/sum/ulogd_inpflow_NFACCT.c | 6 +++--- output/dbi/ulogd_output_DBI.c | 14 +++++++------- output/ipfix/ulogd_output_IPFIX.c | 12 ++++++------ output/mysql/ulogd_output_MYSQL.c | 10 +++++----- output/pgsql/ulogd_output_PGSQL.c | 14 +++++++------- output/ulogd_output_GRAPHITE.c | 6 +++--- output/ulogd_output_JSON.c | 8 ++++---- 10 files changed, 63 insertions(+), 63 deletions(-)
include/ulogd/db.h +7 -7
@@ -103,13 +103,13 @@ struct db_instance { }#define DB_CE_NUM 7 -#define table_ce (x) (x->ces[0]) -#define reconnect_ce (x) (x->ces[1]) -#define timeout_ce (x) (x->ces[2]) -#define procedure_ce (x) (x->ces[3]) -#define backlog_memcap_ce (x) (x->ces[4]) -#define backlog_oneshot_ce (x) (x->ces[5]) -#define ringsize_ce (x) (x->ces[6]) +#define table_ce (x) ((x)->ces[0]) +#define reconnect_ce (x) ((x)->ces[1]) +#define timeout_ce (x) ((x)->ces[2]) +#define procedure_ce (x) ((x)->ces[3]) +#define backlog_memcap_ce (x) ((x)->ces[4]) +#define backlog_oneshot_ce (x) ((x)->ces[5]) +#define ringsize_ce (x) ((x)->ces[6])void ulogd_db_signal (struct ulogd_pluginstance *upi,int signal);int ulogd_db_start (struct ulogd_pluginstance *upi);
input/flow/ulogd_inpflow_NFCT.c +9 -9
@@ -137,15 +137,15 @@ static struct config_keyset nfct_kset = { }, }, }; -#define pollint_ce (x) (x->ces[0]) -#define usehash_ce (x) (x->ces[1]) -#define buckets_ce (x) (x->ces[2]) -#define maxentries_ce (x) (x->ces[3]) -#define eventmask_ce (x) (x->ces[4]) -#define nlsockbufsize_ce (x) (x->ces[5]) -#define nlsockbufmaxsize_ce (x) (x->ces[6]) -#define nlresynctimeout_ce (x) (x->ces[7]) -#define reliable_ce (x) (x->ces[8]) +#define pollint_ce (x) ((x)->ces[0]) +#define usehash_ce (x) ((x)->ces[1]) +#define buckets_ce (x) ((x)->ces[2]) +#define maxentries_ce (x) ((x)->ces[3]) +#define eventmask_ce (x) ((x)->ces[4]) +#define nlsockbufsize_ce (x) ((x)->ces[5]) +#define nlsockbufmaxsize_ce (x) ((x)->ces[6]) +#define nlresynctimeout_ce (x) ((x)->ces[7]) +#define reliable_ce (x) ((x)->ces[8])#define src_filter_ce (x) ((x)->ces[9])#define dst_filter_ce (x) ((x)->ces[10])#define proto_filter_ce (x) ((x)->ces[11])
input/packet/ulogd_inppkt_NFLOG.c +12 -12
@@ -99,18 +99,18 @@ static struct config_keyset libulog_kset = { } }; -#define bufsiz_ce (x) (x->ces[0]) -#define group_ce (x) (x->ces[1]) -#define unbind_ce (x) (x->ces[2]) -#define bind_ce (x) (x->ces[3]) -#define seq_ce (x) (x->ces[4]) -#define seq_global_ce (x) (x->ces[5]) -#define label_ce (x) (x->ces[6]) -#define nlsockbufsize_ce (x) (x->ces[7]) -#define nlsockbufmaxsize_ce (x) (x->ces[8]) -#define nlthreshold_ce (x) (x->ces[9]) -#define nltimeout_ce (x) (x->ces[10]) -#define attach_conntrack_ce (x) (x->ces[11]) +#define bufsiz_ce (x) ((x)->ces[0]) +#define group_ce (x) ((x)->ces[1]) +#define unbind_ce (x) ((x)->ces[2]) +#define bind_ce (x) ((x)->ces[3]) +#define seq_ce (x) ((x)->ces[4]) +#define seq_global_ce (x) ((x)->ces[5]) +#define label_ce (x) ((x)->ces[6]) +#define nlsockbufsize_ce (x) ((x)->ces[7]) +#define nlsockbufmaxsize_ce (x) ((x)->ces[8]) +#define nlthreshold_ce (x) ((x)->ces[9]) +#define nltimeout_ce (x) ((x)->ces[10]) +#define attach_conntrack_ce (x) ((x)->ces[11])enum nflog_keys {NFLOG_KEY_RAW_MAC =0 ,
input/sum/ulogd_inpflow_NFACCT.c +3 -3
@@ -51,9 +51,9 @@ static struct config_keyset nfacct_kset = { }, .num_ces = 3, }; -#define pollint_ce (x) (x->ces[0]) -#define zerocounter_ce (x) (x->ces[1]) -#define timestamp_ce (x) (x->ces[2]) +#define pollint_ce (x) ((x)->ces[0]) +#define zerocounter_ce (x) ((x)->ces[1]) +#define timestamp_ce (x) ((x)->ces[2])enum ulogd_nfacct_keys {ULOGD_NFACCT_NAME ,
output/dbi/ulogd_output_DBI.c +7 -7
@@ -78,13 +78,13 @@ static struct config_keyset dbi_kset = { }, }, }; -#define db_ce (x) (x->ces[DB_CE_NUM+0]) -#define host_ce (x) (x->ces[DB_CE_NUM+1]) -#define user_ce (x) (x->ces[DB_CE_NUM+2]) -#define pass_ce (x) (x->ces[DB_CE_NUM+3]) -#define port_ce (x) (x->ces[DB_CE_NUM+4]) -#define schema_ce (x) (x->ces[DB_CE_NUM+5]) -#define dbtype_ce (x) (x->ces[DB_CE_NUM+6]) +#define db_ce (x) ((x)->ces[DB_CE_NUM + 0]) +#define host_ce (x) ((x)->ces[DB_CE_NUM + 1]) +#define user_ce (x) ((x)->ces[DB_CE_NUM + 2]) +#define pass_ce (x) ((x)->ces[DB_CE_NUM + 3]) +#define port_ce (x) ((x)->ces[DB_CE_NUM + 4]) +#define schema_ce (x) ((x)->ces[DB_CE_NUM + 5]) +#define dbtype_ce (x) ((x)->ces[DB_CE_NUM + 6])/* find out which columns the table has */
output/ipfix/ulogd_output_IPFIX.c +6 -6
@@ -40,12 +40,12 @@ enum {SEND_TEMPLATE_CE }; -#define oid_ce (x) (x->ces[OID_CE]) -#define host_ce (x) (x->ces[HOST_CE]) -#define port_ce (x) (x->ces[PORT_CE]) -#define proto_ce (x) (x->ces[PROTO_CE]) -#define mtu_ce (x) (x->ces[MTU_CE]) -#define send_template_ce (x) (x->ces[SEND_TEMPLATE_CE]) +#define oid_ce (x) ((x)->ces[OID_CE]) +#define host_ce (x) ((x)->ces[HOST_CE]) +#define port_ce (x) ((x)->ces[PORT_CE]) +#define proto_ce (x) ((x)->ces[PROTO_CE]) +#define mtu_ce (x) ((x)->ces[MTU_CE]) +#define send_template_ce (x) ((x)->ces[SEND_TEMPLATE_CE])static const struct config_keyset ipfix_kset = { .num_ces =6 ,
output/mysql/ulogd_output_MYSQL.c +5 -5
@@ -89,11 +89,11 @@ static struct config_keyset kset_mysql = { }, }, }; -#define db_ce (x) (x->ces[DB_CE_NUM+0]) -#define host_ce (x) (x->ces[DB_CE_NUM+1]) -#define user_ce (x) (x->ces[DB_CE_NUM+2]) -#define pass_ce (x) (x->ces[DB_CE_NUM+3]) -#define port_ce (x) (x->ces[DB_CE_NUM+4]) +#define db_ce (x) ((x)->ces[DB_CE_NUM + 0]) +#define host_ce (x) ((x)->ces[DB_CE_NUM + 1]) +#define user_ce (x) ((x)->ces[DB_CE_NUM + 2]) +#define pass_ce (x) ((x)->ces[DB_CE_NUM + 3]) +#define port_ce (x) ((x)->ces[DB_CE_NUM + 4])/* find out which columns the table has */ static int get_columns_mysql (struct ulogd_pluginstance *upi) {
output/pgsql/ulogd_output_PGSQL.c +7 -7
@@ -74,13 +74,13 @@ static struct config_keyset pgsql_kset = { }, }, }; -#define db_ce (x) (x->ces[DB_CE_NUM+0]) -#define host_ce (x) (x->ces[DB_CE_NUM+1]) -#define user_ce (x) (x->ces[DB_CE_NUM+2]) -#define pass_ce (x) (x->ces[DB_CE_NUM+3]) -#define port_ce (x) (x->ces[DB_CE_NUM+4]) -#define schema_ce (x) (x->ces[DB_CE_NUM+5]) -#define connstr_ce (x) (x->ces[DB_CE_NUM+6]) +#define db_ce (x) ((x)->ces[DB_CE_NUM + 0]) +#define host_ce (x) ((x)->ces[DB_CE_NUM + 1]) +#define user_ce (x) ((x)->ces[DB_CE_NUM + 2]) +#define pass_ce (x) ((x)->ces[DB_CE_NUM + 3]) +#define port_ce (x) ((x)->ces[DB_CE_NUM + 4]) +#define schema_ce (x) ((x)->ces[DB_CE_NUM + 5]) +#define connstr_ce (x) ((x)->ces[DB_CE_NUM + 6])#define PGSQL_HAVE_NAMESPACE_TEMPLATE \ "SELECT nspname FROM pg_namespace n WHERE n.nspname='%s'"
output/ulogd_output_GRAPHITE.c +3 -3
@@ -79,9 +79,9 @@ static struct config_keyset graphite_kset = { }, }; -#define host_ce (x) (x->ces[0]) -#define port_ce (x) (x->ces[1]) -#define prefix_ce (x) (x->ces[2]) +#define host_ce (x) ((x)->ces[0]) +#define port_ce (x) ((x)->ces[1]) +#define prefix_ce (x) ((x)->ces[2])struct graphite_instance {int sck;
output/ulogd_output_JSON.c +4 -4
@@ -45,10 +45,10 @@#define ULOGD_JSON_DEFAULT_DEVICE "Netfilter"#endif -#define host_ce (x) (x->ces[JSON_CONF_HOST]) -#define port_ce (x) (x->ces[JSON_CONF_PORT]) -#define mode_ce (x) (x->ces[JSON_CONF_MODE]) -#define file_ce (x) (x->ces[JSON_CONF_FILENAME]) +#define host_ce (x) ((x)->ces[JSON_CONF_HOST]) +#define port_ce (x) ((x)->ces[JSON_CONF_PORT]) +#define mode_ce (x) ((x)->ces[JSON_CONF_MODE]) +#define file_ce (x) ((x)->ces[JSON_CONF_FILENAME])#define unlikely (x) __builtin_expect((x),0)struct json_priv {
Signature · 1 line
2.35.1
PATCH 9/34 src: define constructors and destructors consistently
In reply to Jeremy SowdenRemove separate declarations and declare the functions static.
--- filter/raw2packet/ulogd_raw2packet_BASE.c | 4 +--- filter/ulogd_filter_HWHDR.c | 4 +--- filter/ulogd_filter_IFINDEX.c | 4 +--- filter/ulogd_filter_IP2BIN.c | 4 +--- filter/ulogd_filter_IP2HBIN.c | 4 +--- filter/ulogd_filter_IP2STR.c | 4 +--- filter/ulogd_filter_MARK.c | 4 +--- filter/ulogd_filter_PRINTFLOW.c | 4 +--- filter/ulogd_filter_PRINTPKT.c | 4 +--- filter/ulogd_filter_PWSNIFF.c | 2 +- input/flow/ulogd_inpflow_NFCT.c | 5 +---- input/packet/ulogd_inppkt_NFLOG.c | 4 +--- input/packet/ulogd_inppkt_ULOG.c | 11 ++++++----- input/sum/ulogd_inpflow_NFACCT.c | 4 +--- output/dbi/ulogd_output_DBI.c | 4 ++-- output/ipfix/ulogd_output_IPFIX.c | 4 +--- output/mysql/ulogd_output_MYSQL.c | 4 +--- output/pcap/ulogd_output_PCAP.c | 4 +--- output/pgsql/ulogd_output_PGSQL.c | 4 +--- output/sqlite3/ulogd_output_SQLITE3.c | 5 +---- 20 files changed, 26 insertions(+), 61 deletions(-)
filter/raw2packet/ulogd_raw2packet_BASE.c +1 -3
@@ -1002,9 +1002,7 @@ static struct ulogd_plugin base_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&base_plugin); }
filter/ulogd_filter_HWHDR.c +1 -3
@@ -236,9 +236,7 @@ static struct ulogd_plugin mac2str_pluging = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static __attribute__ ((constructor))void init (void ) {ulogd_register_plugin (&mac2str_pluging); }
filter/ulogd_filter_IFINDEX.c +1 -3
@@ -149,9 +149,7 @@ static struct ulogd_plugin ifindex_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static __attribute__ ((constructor))void init (void ) {ulogd_register_plugin (&ifindex_plugin); }
filter/ulogd_filter_IP2BIN.c +1 -3
@@ -234,9 +234,7 @@ static struct ulogd_plugin ip2bin_pluging = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static __attribute__ ((constructor))void init (void ) {ulogd_register_plugin (&ip2bin_pluging); }
filter/ulogd_filter_IP2HBIN.c +1 -3
@@ -190,9 +190,7 @@ static struct ulogd_plugin ip2hbin_pluging = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static __attribute__ ((constructor))void init (void ) {ulogd_register_plugin (&ip2hbin_pluging); }
filter/ulogd_filter_IP2STR.c +1 -3
@@ -224,9 +224,7 @@ static struct ulogd_plugin ip2str_pluging = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static __attribute__ ((constructor))void init (void ) {ulogd_register_plugin (&ip2str_pluging); }
filter/ulogd_filter_MARK.c +1 -3
@@ -111,9 +111,7 @@ static struct ulogd_plugin mark_pluging = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static __attribute__ ((constructor))void init (void ) {ulogd_register_plugin (&mark_pluging); }
filter/ulogd_filter_PRINTFLOW.c +1 -3
@@ -57,9 +57,7 @@ static struct ulogd_plugin printflow_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static __attribute__ ((constructor))void init (void ) {ulogd_register_plugin (&printflow_plugin); }
filter/ulogd_filter_PRINTPKT.c +1 -3
@@ -57,9 +57,7 @@ static struct ulogd_plugin printpkt_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static __attribute__ ((constructor))void init (void ) {ulogd_register_plugin (&printpkt_plugin); }
filter/ulogd_filter_PWSNIFF.c +1 -1
@@ -170,7 +170,7 @@ static struct ulogd_plugin pwsniff_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&pwsniff_plugin); }
input/flow/ulogd_inpflow_NFCT.c +1 -4
@@ -1545,10 +1545,7 @@ static struct ulogd_plugin nfct_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&nfct_plugin); } -
input/packet/ulogd_inppkt_NFLOG.c +1 -3
@@ -712,9 +712,7 @@ struct ulogd_plugin libulog_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&libulog_plugin); }
input/packet/ulogd_inppkt_ULOG.c +6 -5
@@ -265,7 +265,8 @@ static int configure(struct ulogd_pluginstance *upi, {return config_parse_file (upi->id, upi->config_kset); } -static int init (struct ulogd_pluginstance *upi) + +static int start (struct ulogd_pluginstance *upi) {struct ulog_input *ui = (struct ulog_input *) &upi->private;
@@ -298,7 +299,7 @@ out_buf:return -1 ; } -static int fini (struct ulogd_pluginstance *pi) +static int stop (struct ulogd_pluginstance *pi) {struct ulog_input *ui = (struct ulog_input *)pi->private;
@@ -320,13 +321,13 @@ struct ulogd_plugin libulog_plugin = { .num_keys = ARRAY_SIZE(output_keys), }, .configure = &configure, - .start = &init, - .stop = &fini, + .start = &start, + .stop = &stop, .config_kset = &libulog_kset, .version = VERSION, }; -void __attribute__ ((constructor))initializer (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&libulog_plugin); }
input/sum/ulogd_inpflow_NFACCT.c +1 -3
@@ -300,9 +300,7 @@ static struct ulogd_plugin nfacct_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&nfacct_plugin); }
output/dbi/ulogd_output_DBI.c +2 -2
@@ -307,14 +307,14 @@ static struct ulogd_plugin dbi_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ) +static void __attribute__ ((constructor))init (void ) {dbi_initialize_r (NULL , &libdbi_instance);ulogd_register_plugin (&dbi_plugin); } -void __attribute__ ((destructor))fini (void ) +static void __attribute__ ((destructor))fini (void ) {dbi_shutdown_r (libdbi_instance); }
output/ipfix/ulogd_output_IPFIX.c +1 -3
@@ -504,9 +504,7 @@ static struct ulogd_plugin ipfix_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&ipfix_plugin); }
output/mysql/ulogd_output_MYSQL.c +1 -3
@@ -278,9 +278,7 @@ static struct ulogd_plugin plugin_mysql = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&plugin_mysql); }
output/pcap/ulogd_output_PCAP.c +1 -3
@@ -307,9 +307,7 @@ static struct ulogd_plugin pcap_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&pcap_plugin); }
output/pgsql/ulogd_output_PGSQL.c +1 -3
@@ -357,9 +357,7 @@ static struct ulogd_plugin pgsql_plugin = { .version = VERSION, }; -void __attribute__ ((constructor))init (void ); - -void init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&pgsql_plugin); }
output/sqlite3/ulogd_output_SQLITE3.c +1 -4
@@ -435,10 +435,7 @@ static struct ulogd_plugin sqlite3_plugin = { .version = VERSION, }; -static void init (void ) __attribute__((constructor)); - -static void -init (void ) +static void __attribute__ ((constructor))init (void ) {ulogd_register_plugin (&sqlite3_plugin); }
Signature · 1 line
2.35.1
PATCH 10/34 src: remove `TIME_ERR` macro
In reply to Jeremy SowdenThe TIME_ERR macro is used to check the return value of time(2). However, two of the definitions of it are never used. The third is used to check the return value of time(NULL). However, time(2) with a NULL argument can never fail, so we don't need to perform the check.
--- include/ulogd/db.h | 2 +- output/dbi/ulogd_output_DBI.c | 1 - output/pgsql/ulogd_output_PGSQL.c | 1 - util/db.c | 14 ++++++-------- 4 files changed, 7 insertions(+), 11 deletions(-)
include/ulogd/db.h +1 -1
@@ -62,7 +62,7 @@ struct db_instance {unsigned char backlog_full;struct llist_head backlog; }; -#define TIME_ERR ((time_t)-1)/* Be paranoid */ +#define RECONNECT_DEFAULT 2#define MAX_ONESHOT_REQUEST 10#define RING_BUFFER_DEFAULT_SIZE 0
output/dbi/ulogd_output_DBI.c +0 -1
@@ -37,7 +37,6 @@ struct dbi_instance {dbi_conn dbh;dbi_result result; }; -#define TIME_ERR ((time_t)-1)/* our configuration directives */ static struct config_keyset dbi_kset = {
output/pgsql/ulogd_output_PGSQL.c +0 -1
@@ -34,7 +34,6 @@ struct pgsql_instance {PGresult *pgres;unsigned char pgsql_have_schemas; }; -#define TIME_ERR ((time_t)-1)/* our configuration directives */ static struct config_keyset pgsql_kset = {
util/db.c +6 -8
@@ -302,14 +302,12 @@ static int _init_reconnect(struct ulogd_pluginstance *upi)if (time (NULL ) < di->reconnect)return -1 ; di->reconnect =time (NULL ); -if (di->reconnect !=TIME_ERR ) { -ulogd_log (ULOGD_ERROR ,"no connection to database, " -"attempting to reconnect after %u seconds\n" , -reconnect_ce (upi->config_kset).u.value); - di->reconnect +=reconnect_ce (upi->config_kset).u.value; - di->interp = &_init_db; -return -1 ; - } +ulogd_log (ULOGD_ERROR , +"no connection to database, attempting to reconnect after %u seconds\n" , +reconnect_ce (upi->config_kset).u.value); + di->reconnect +=reconnect_ce (upi->config_kset).u.value; + di->interp = &_init_db; +return -1 ; }/* Disable plugin permanently */
Signature · 1 line
2.35.1
PATCH 11/34 src: remove superfluous casts
In reply to Jeremy SowdenWe're not writing C++, we don't need to cast void pointers.
--- cftest/cftest.c | 2 +- libipulog/libipulog.c | 2 +- libipulog/ulog_test.c | 2 +- src/conffile.c | 2 +- src/hash.c | 4 ++-- src/ulogd.c | 2 +- util/db.c | 4 ++-- 7 files changed, 9 insertions(+), 9 deletions(-)
cftest/cftest.c +1 -1
@@ -19,7 +19,7 @@ int main()strcpy (f.key,"spalte" ); f.type =CONFIG_TYPE_STRING ; f.options |=CONFIG_OPT_MANDATORY ; - f.u.str.string = (char *)malloc (100 ); + f.u.str.string =malloc (100 ); f.u.str.maxlen =99 ;config_register_key (&f);
libipulog/libipulog.c +1 -1
@@ -129,7 +129,7 @@ struct ipulog_handle *ipulog_create_handle(uint32_t gmask,struct ipulog_handle *h;int status; - h = (struct ipulog_handle *)malloc (sizeof (struct ipulog_handle )); + h =malloc (sizeof (*h));if (h ==NULL ) { ipulog_errno =IPULOG_ERR_HANDLE ;
libipulog/ulog_test.c +1 -1
@@ -51,7 +51,7 @@ int main(int argc, char *argv[]) }/* allocate a receive buffer */ - buf = (unsigned char *)malloc (MYBUFSIZ ); + buf =malloc (MYBUFSIZ );/* create ipulog handle */ h =ipulog_create_handle (ipulog_group2gmask (atoi (argv[2 ])));
src/conffile.c +1 -1
@@ -104,7 +104,7 @@ int config_register_file(const char *file)pr_debug ("%s: registered config file '%s'\n" , __func__, file); - fname = (char *)malloc (strlen (file)+1 ); + fname =malloc (strlen (file)+1 );if (!fname)return -ERROOM ;
src/hash.c +2 -2
@@ -34,9 +34,9 @@ hashtable_create(int hashsize, int limit,int i;struct hashtable *h;int size =sizeof (struct hashtable ) - + hashsize *sizeof (struct llist_head ); + +sizeof (struct llist_head ) * hashsize; - h = (struct hashtable *)calloc (size,1 ); + h =calloc (size,1 );if (h ==NULL ) { errno =ENOMEM ;return NULL ;
src/ulogd.c +1 -1
@@ -720,7 +720,7 @@ static int load_plugin(const char *file)return -1 ; } - ph = (struct ulogd_plugin_handle *)calloc (1 ,sizeof (*ph)); + ph =calloc (1 ,sizeof (*ph)); ph->handle = handle;llist_add (&ph->list, &ulogd_plugins_handle);return 0 ;
util/db.c +2 -2
@@ -85,7 +85,7 @@ static int sql_createstmt(struct ulogd_pluginstance *upi)ulogd_log (ULOGD_DEBUG ,"allocating %u bytes for statement\n" , size); - mi->stmt = (char *)malloc (size); + mi->stmt =malloc (size);if (!mi->stmt) {ulogd_log (ULOGD_ERROR ,"OOM!\n" );return -ENOMEM ;
@@ -575,7 +575,7 @@ static int __loop_reconnect_db(struct ulogd_pluginstance * upi) {static void *__inject_thread (void *gdi) { -struct ulogd_pluginstance *upi = (struct ulogd_pluginstance *) gdi; +struct ulogd_pluginstance *upi = gdi;struct db_instance *di = (struct db_instance *) &upi->private;char *wr_place;
Signature · 1 line
2.35.1
PATCH 12/34 conffile: replace malloc+strcpy with strdup
In reply to Jeremy Sowden--- src/conffile.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
src/conffile.c +1 -3
@@ -104,12 +104,10 @@ int config_register_file(const char *file)pr_debug ("%s: registered config file '%s'\n" , __func__, file); - fname =malloc (strlen (file)+1 ); + fname =strdup (file);if (!fname)return -ERROOM ; -strcpy (fname, file); -return 0 ; }
Signature · 1 line
2.35.1
PATCH 13/34 output: remove zero-initialized `struct ulogd_plugin` members
In reply to Jeremy SowdenStruct members are zero-initialized as a matter of course.
Use consistent formatting.
--- output/dbi/ulogd_output_DBI.c | 10 ++++------ output/ipfix/ulogd_output_IPFIX.c | 20 ++++++++++---------- output/mysql/ulogd_output_MYSQL.c | 22 ++++++++++------------ output/pcap/ulogd_output_PCAP.c | 6 +++--- output/pgsql/ulogd_output_PGSQL.c | 10 ++++------ output/sqlite3/ulogd_output_SQLITE3.c | 18 +++++++++--------- 6 files changed, 40 insertions(+), 46 deletions(-)
output/dbi/ulogd_output_DBI.c +4 -6
@@ -287,16 +287,14 @@ static int configure_dbi(struct ulogd_pluginstance *upi, }static struct ulogd_plugin dbi_plugin = { - .name ="DBI" , - .input = { - .keys =NULL , - .num_keys =0 , + .name ="DBI" , + .input = { .type =ULOGD_DTYPE_PACKET |ULOGD_DTYPE_FLOW , }, - .output = { + .output = { .type =ULOGD_DTYPE_SINK , }, - .config_kset = &dbi_kset, + .config_kset = &dbi_kset, .priv_size =sizeof (struct dbi_instance ), .configure = &configure_dbi, .start = &ulogd_db_start,
output/ipfix/ulogd_output_IPFIX.c +10 -10
@@ -486,22 +486,22 @@ again: }static struct ulogd_plugin ipfix_plugin = { - .name ="IPFIX" , - .input = { + .name ="IPFIX" , + .input = { .keys = ipfix_in_keys, .num_keys =ARRAY_SIZE (ipfix_in_keys), .type =ULOGD_DTYPE_PACKET |ULOGD_DTYPE_FLOW |ULOGD_DTYPE_SUM }, - .output = { + .output = { .type =ULOGD_DTYPE_SINK }, - .config_kset = (struct config_keyset *) &ipfix_kset, - .priv_size =sizeof (struct ipfix_priv ), - .configure = ipfix_configure, - .start = ipfix_start, - .stop = ipfix_stop, - .interp = ipfix_interp, - .version =VERSION , + .config_kset = (struct config_keyset *) &ipfix_kset, + .priv_size =sizeof (struct ipfix_priv ), + .configure = ipfix_configure, + .start = ipfix_start, + .stop = ipfix_stop, + .interp = ipfix_interp, + .version =VERSION , };static void __attribute__ ((constructor))init (void )
output/mysql/ulogd_output_MYSQL.c +10 -12
@@ -259,23 +259,21 @@ static int configure_mysql(struct ulogd_pluginstance *upi, }static struct ulogd_plugin plugin_mysql = { - .name ="MYSQL" , - .input = { - .keys =NULL , - .num_keys =0 , + .name ="MYSQL" , + .input = { .type =ULOGD_DTYPE_PACKET |ULOGD_DTYPE_FLOW , }, - .output = { + .output = { .type =ULOGD_DTYPE_SINK , }, .config_kset = &kset_mysql, - .priv_size =sizeof (struct mysql_instance ), - .configure = &configure_mysql, - .start = &ulogd_db_start, - .stop = &ulogd_db_stop, - .signal = &ulogd_db_signal, - .interp = &ulogd_db_interp, - .version =VERSION , + .priv_size =sizeof (struct mysql_instance ), + .configure = &configure_mysql, + .start = &ulogd_db_start, + .stop = &ulogd_db_stop, + .signal = &ulogd_db_signal, + .interp = &ulogd_db_interp, + .version =VERSION , };static void __attribute__ ((constructor))init (void )
output/pcap/ulogd_output_PCAP.c +3 -3
@@ -287,13 +287,13 @@ static int stop_pcap(struct ulogd_pluginstance *upi) }static struct ulogd_plugin pcap_plugin = { - .name ="PCAP" , - .input = { + .name ="PCAP" , + .input = { .keys = pcap_keys, .num_keys =ARRAY_SIZE (pcap_keys), .type =ULOGD_DTYPE_PACKET , }, - .output = { + .output = { .type =ULOGD_DTYPE_SINK , }, .config_kset = &pcap_kset,
output/pgsql/ulogd_output_PGSQL.c +4 -6
@@ -337,16 +337,14 @@ static int configure_pgsql(struct ulogd_pluginstance *upi, }static struct ulogd_plugin pgsql_plugin = { - .name ="PGSQL" , - .input = { - .keys =NULL , - .num_keys =0 , + .name ="PGSQL" , + .input = { .type =ULOGD_DTYPE_PACKET |ULOGD_DTYPE_FLOW , }, - .output = { + .output = { .type =ULOGD_DTYPE_SINK , }, - .config_kset = &pgsql_kset, + .config_kset = &pgsql_kset, .priv_size =sizeof (struct pgsql_instance ), .configure = &configure_pgsql, .start = &ulogd_db_start,
output/sqlite3/ulogd_output_SQLITE3.c +9 -9
@@ -419,20 +419,20 @@ sqlite3_stop(struct ulogd_pluginstance *pi) }static struct ulogd_plugin sqlite3_plugin = { - .name ="SQLITE3" , - .input = { + .name ="SQLITE3" , + .input = { .type =ULOGD_DTYPE_PACKET |ULOGD_DTYPE_FLOW , }, - .output = { + .output = { .type =ULOGD_DTYPE_SINK , }, .config_kset = &sqlite3_kset, - .priv_size =sizeof (struct sqlite3_priv ), - .configure = sqlite3_configure, - .start = sqlite3_start, - .stop = sqlite3_stop, - .interp = sqlite3_interp, - .version =VERSION , + .priv_size =sizeof (struct sqlite3_priv ), + .configure = sqlite3_configure, + .start = sqlite3_start, + .stop = sqlite3_stop, + .interp = sqlite3_interp, + .version =VERSION , };static void __attribute__ ((constructor))init (void )
Signature · 1 line
2.35.1
PATCH 14/34 output: de-duplicate allocation of input keys
In reply to Jeremy SowdenThe three DB output plug-ins which use the ulogd_db API all derive the names of their input keys from DB column names, and do so in almost identical fashion. Create a common ulogd_db implementation and update them to use it.
--- include/ulogd/db.h | 11 ++++- output/dbi/ulogd_output_DBI.c | 67 ++++++++++++++----------------- output/mysql/ulogd_output_MYSQL.c | 46 ++++++++------------- output/pgsql/ulogd_output_PGSQL.c | 43 ++++++-------------- util/db.c | 48 ++++++++++++++++++++++ 5 files changed, 116 insertions(+), 99 deletions(-)
include/ulogd/db.h +9 -2
@@ -12,13 +12,19 @@#include <ulogd/ulogd.h> struct db_driver { +int (*get_columns)(struct ulogd_pluginstance *upi); +const char *(*get_column)(void *,unsigned int ); +char *(*format_key)(char *); +int (*open_db)(struct ulogd_pluginstance *upi);int (*close_db)(struct ulogd_pluginstance *upi); +int (*escape_string)(struct ulogd_pluginstance *upi,char *dst,const char *src,unsigned int len);int (*execute)(struct ulogd_pluginstance *upi,const char *stmt,unsigned int len); + };enum {
@@ -116,7 +122,8 @@ int ulogd_db_start(struct ulogd_pluginstance *upi);int ulogd_db_stop (struct ulogd_pluginstance *upi);int ulogd_db_interp (struct ulogd_pluginstance *upi);int ulogd_db_configure (struct ulogd_pluginstance *upi, -struct ulogd_pluginstance_stack *stack); - +struct ulogd_pluginstance_stack *stack); +int ulogd_db_alloc_input_keys (struct ulogd_pluginstance *upi, +unsigned int num_keys,void *arg);#endif
output/dbi/ulogd_output_DBI.c +30 -37
91 lines not on this page · raw message
output/mysql/ulogd_output_MYSQL.c +16 -30
73 lines not on this page · raw message
output/pgsql/ulogd_output_PGSQL.c +13 -30
66 lines not on this page · raw message
util/db.c +48 -0
51 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 15/34 db: reorganize source
In reply to Jeremy SowdenThe functions are rather haphazardly ordered and inconsistently named. Group all function declarations first, followed by the public functions (those with extern linkage) next, and then the static ones, grouping those related to the back-log and ring-buffer separately. Extern functions are all prefixed ulogd_db_, static ones are all prefixed with a single underscore.
Rename a couple of struct db_instance variables for consistency.
No functional changes.
--- util/db.c | 623 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 328 insertions(+), 295 deletions(-)
util/db.c +328 -295
@@ -37,115 +37,30 @@#include <ulogd/ulogd.h> #include <ulogd/db.h> -/* generic db layer */ -static int __interp_db (struct ulogd_pluginstance *upi); - -/* this is a wrapper that just calls the current real -* interp function */ -int ulogd_db_interp (struct ulogd_pluginstance *upi) -{ -struct db_instance *dbi = (struct db_instance *) &upi->private; -return dbi->interp (upi); -} - -/* no connection, plugin disabled */ -static int disabled_interp_db (struct ulogd_pluginstance *upi) -{ -return 0 ; -} - -#define SQL_INSERTTEMPL "SELECT P(Y)" -#define SQL_VALSIZE 100 - -/* create the static part of our insert statement */ -static int sql_createstmt (struct ulogd_pluginstance *upi) -{ -struct db_instance *mi = (struct db_instance *) upi->private; -unsigned int size; -unsigned int i; -char *table =table_ce (upi->config_kset).u.string; -char *procedure =procedure_ce (upi->config_kset).u.string; - -if (mi->stmt) -free (mi->stmt); - -/* caclulate the size for the insert statement */ - size =strlen (SQL_INSERTTEMPL ) +strlen (table); - -for (i =0 ; i < upi->input.num_keys; i++) { -if (upi->input.keys[i].flags &ULOGD_KEYF_INACTIVE ) -continue ; -/* we need space for the key and a comma, as well as -* enough space for the values */ - size +=strlen (upi->input.keys[i].name) +1 +SQL_VALSIZE ; - } - size +=strlen (procedure); - -ulogd_log (ULOGD_DEBUG ,"allocating %u bytes for statement\n" , size); - - mi->stmt =malloc (size); -if (!mi->stmt) { -ulogd_log (ULOGD_ERROR ,"OOM!\n" ); -return -ENOMEM ; - } - mi->ring.length = size +1 ; - -if (strncasecmp (procedure,"INSERT" ,strlen ("INSERT" )) ==0 && - (procedure[strlen ("INSERT" )] =='\0' || - procedure[strlen ("INSERT" )] ==' ' )) { -char *stmt_val = mi->stmt; - -if (procedure[6 ] =='\0' ) { -/* procedure == "INSERT" */ -if (mi->schema) - stmt_val +=sprintf (stmt_val, -"insert into %s.%s (" , - mi->schema, table); -else - stmt_val +=sprintf (stmt_val, -"insert into %s (" , table); - }else - stmt_val +=sprintf (stmt_val,"%s (" , procedure); - -for (i =0 ; i < upi->input.num_keys; i++) { -char *underscore; - -if (upi->input.keys[i].flags &ULOGD_KEYF_INACTIVE ) -continue ; - - underscore = stmt_val; - - stmt_val +=sprintf (stmt_val,"%s," , - upi->input.keys[i].name); - -while ((underscore =strchr (underscore,'.' ))) - *underscore ='_' ; - } - *(stmt_val -1 ) =')' ; - -sprintf (stmt_val," values (" ); - }else if (strncasecmp (procedure,"CALL" ,strlen ("CALL" )) ==0 ) { -sprintf (mi->stmt,"CALL %s(" , procedure); - }else { -sprintf (mi->stmt,"SELECT %s(" , procedure); - - } +static int _interp_db_init (struct ulogd_pluginstance *upi); +static int _interp_db_main (struct ulogd_pluginstance *upi); +static int _interp_db_disabled (struct ulogd_pluginstance *upi); +static int _reconnect_db (struct ulogd_pluginstance *upi); +static int _stop_db (struct ulogd_pluginstance *upi); - mi->stmt_offset =strlen (mi->stmt); +static char *_format_key (char *key); +static int _create_sql_stmt (struct ulogd_pluginstance *upi); +static void _bind_sql_stmt (struct ulogd_pluginstance *upi, +char *stmt); -ulogd_log (ULOGD_DEBUG ,"stmt='%s'\n" , mi->stmt); +static int _add_to_backlog (struct ulogd_pluginstance *upi, +const char *stmt,unsigned int len); +static int _process_backlog (struct ulogd_pluginstance *upi); -return 0 ; -} - -static int _init_db (struct ulogd_pluginstance *upi); - -static void *__inject_thread (void *gdi); +static int _add_to_ring (struct ulogd_pluginstance *upi,struct db_instance *di); +static void *_process_ring (void *arg); +static int _loop_reconnect_db (struct ulogd_pluginstance *upi); -int ulogd_db_configure (struct ulogd_pluginstance *upi, -struct ulogd_pluginstance_stack *stack) +int +ulogd_db_configure (struct ulogd_pluginstance *upi, +struct ulogd_pluginstance_stack *stack) {struct db_instance *di = (struct db_instance *) upi->private;int ret;
@@ -199,7 +114,44 @@ int ulogd_db_configure(struct ulogd_pluginstance *upi,return ret; } -int ulogd_db_start (struct ulogd_pluginstance *upi) +int +ulogd_db_alloc_input_keys (struct ulogd_pluginstance *upi, +unsigned int num_keys,void *arg) +{ +struct db_instance *di = (struct db_instance *) &upi->private; +char *(*format_key)(char *) = di->driver->format_key ? : _format_key; +unsigned int i; + +if (upi->input.keys) +free (upi->input.keys); + + upi->input.num_keys = num_keys; +ulogd_log (ULOGD_DEBUG ,"%u fields in table\n" , upi->input.num_keys); + upi->input.keys =calloc (upi->input.num_keys,sizeof (upi->input.keys[0 ])); +if (!upi->input.keys) { + upi->input.num_keys =0 ; +ulogd_log (ULOGD_ERROR ,"ENOMEM\n" ); +return -ENOMEM ; + } + +for (i =0 ; i < num_keys; i++) { +const char *col = di->driver->get_column (arg, i); + +if (!col) +break ; + +snprintf (upi->input.keys[i].name, +sizeof (upi->input.keys[i].name), +"%s" , col); + +format_key (upi->input.keys[i].name); + } + +return 0 ; +} + +int +ulogd_db_start (struct ulogd_pluginstance *upi) {struct db_instance *di = (struct db_instance *) upi->private;int ret;
@@ -211,7 +163,7 @@ int ulogd_db_start(struct ulogd_pluginstance *upi)if (ret <0 )return ret; - ret =sql_createstmt (upi); + ret =_create_sql_stmt (upi);if (ret <0 ) goto db_error;
@@ -239,12 +191,12 @@ int ulogd_db_start(struct ulogd_pluginstance *upi)if (ret !=0 ) goto cond_error;/* create thread */ - ret =pthread_create (&di->db_thread_id,NULL , __inject_thread, upi); + ret =pthread_create (&di->db_thread_id,NULL , _process_ring, upi);if (ret !=0 ) goto mutex_error; } - di->interp = &_init_db; + di->interp = &_interp_db_init;return ret;
@@ -259,30 +211,55 @@ db_error:return ret; } -static int ulogd_db_instance_stop (struct ulogd_pluginstance *upi) +/* this is a wrapper that just calls the current real interp function */ +int +ulogd_db_interp (struct ulogd_pluginstance *upi) { -struct db_instance *di = (struct db_instance *) upi->private; -ulogd_log (ULOGD_NOTICE ,"stopping\n" ); - di->driver->close_db (upi); +struct db_instance *di = (struct db_instance *) &upi->private; +return di->interp (upi); +} -/* try to free the buffer for insert statement */ -if (di->stmt) { -free (di->stmt); - di->stmt =NULL ; - } -if (di->ring.size >0 ) { -pthread_cancel (di->db_thread_id); -free (di->ring.ring); -pthread_cond_destroy (&di->ring.cond); -pthread_mutex_destroy (&di->ring.mutex); - di->ring.ring =NULL ; +void +ulogd_db_signal (struct ulogd_pluginstance *upi,int signal) +{ +struct db_instance *di = (struct db_instance *) &upi->private; + +switch (signal) { +case SIGHUP : +if (!di->ring.size) { +/* reopen database connection */ +_stop_db (upi); +ulogd_db_start (upi); + }else +ulogd_log (ULOGD_ERROR , +"No SIGHUP handling if ring buffer is used\n" ); +break ; +case SIGTERM : +case SIGINT : +if (di->ring.size) { +int s =pthread_cancel (di->db_thread_id); +if (s !=0 ) { +ulogd_log (ULOGD_ERROR , +"Can't cancel injection thread\n" ); +break ; + } + s =pthread_join (di->db_thread_id,NULL ); +if (s !=0 ) { +ulogd_log (ULOGD_ERROR , +"Error waiting for injection thread" +"cancelation\n" ); + } + } +break ; +default : +break ; } -return 0 ; } -int ulogd_db_stop (struct ulogd_pluginstance *upi) +int +ulogd_db_stop (struct ulogd_pluginstance *upi) { -ulogd_db_instance_stop (upi); +_stop_db (upi);/* try to free our dynamically allocated input key array */ if (upi->input.keys) {
@@ -293,8 +270,85 @@ int ulogd_db_stop(struct ulogd_pluginstance *upi)return 0 ; } +/******************************************************************************/ -static int _init_reconnect (struct ulogd_pluginstance *upi) +static int +_interp_db_init (struct ulogd_pluginstance *upi) +{ +struct db_instance *di = (struct db_instance *) upi->private; + +if (di->reconnect && di->reconnect >time (NULL )) { +/* store entry to backlog if it is active */ +if (di->backlog_memcap && !di->backlog_full) { +_bind_sql_stmt (upi, di->stmt); +_add_to_backlog (upi, di->stmt,strlen (di->stmt)); + } +return 0 ; + } + +if (di->driver->open_db (upi)) { +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); + } + +/* enable 'real' logging */ + di->interp = &_interp_db_main; + + di->reconnect =0 ; + +/* call the interpreter function to actually write the +* log line that we wanted to write */ +return _interp_db_main (upi); +} + +/* our main output function, called by ulogd */ +static int +_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); + +_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 (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); + } + +return 0 ; +} + +/* no connection, plugin disabled */ +static int +_interp_db_disabled (struct ulogd_pluginstance *upi) +{ +return 0 ; +} + +static int +_reconnect_db (struct ulogd_pluginstance *upi) {struct db_instance *di = (struct db_instance *) upi->private;
@@ -306,18 +360,137 @@ static int _init_reconnect(struct ulogd_pluginstance *upi)"no connection to database, attempting to reconnect after %u seconds\n" , reconnect_ce(upi->config_kset).u.value); di->reconnect +=reconnect_ce (upi->config_kset).u.value; - di->interp = &_init_db; + di->interp = &_interp_db_init;return -1 ; }/* Disable plugin permanently */ ulogd_log (ULOGD_ERROR ,"permanently disabling plugin\n" ); - di->interp = &disabled_interp_db; + di->interp = &_interp_db_disabled;return 0 ; } -static void __format_query_db (struct ulogd_pluginstance *upi,char *start) +static int +_stop_db (struct ulogd_pluginstance *upi) +{ +struct db_instance *di = (struct db_instance *) upi->private; +ulogd_log (ULOGD_NOTICE ,"stopping\n" ); + di->driver->close_db (upi); + +/* try to free the buffer for insert statement */ +if (di->stmt) { +free (di->stmt); + di->stmt =NULL ; + } +if (di->ring.size >0 ) { +pthread_cancel (di->db_thread_id); +free (di->ring.ring); +pthread_cond_destroy (&di->ring.cond); +pthread_mutex_destroy (&di->ring.mutex); + di->ring.ring =NULL ; + } +return 0 ; +} + +static char * +_format_key (char *key) +{ +char *cp = key; + +/* replace all underscores with dots */ +while ((cp =strchr (cp,'_' ))) + *cp ='.' ; + +return key; +} + +#define SQL_INSERTTEMPL "SELECT P(Y)" +#define SQL_VALSIZE 100 + +/* create the static part of our insert statement */ +static int +_create_sql_stmt (struct ulogd_pluginstance *upi) +{ +struct db_instance *di = (struct db_instance *) upi->private; +unsigned int size; +unsigned int i; +char *table =table_ce (upi->config_kset).u.string; +char *procedure =procedure_ce (upi->config_kset).u.string; + +if (di->stmt) +free (di->stmt); + +/* caclulate the size for the insert statement */ + size =strlen (SQL_INSERTTEMPL ) +strlen (table); + +for (i =0 ; i < upi->input.num_keys; i++) { +if (upi->input.keys[i].flags &ULOGD_KEYF_INACTIVE ) +continue ; +/* we need space for the key and a comma, as well as +* enough space for the values */ + size +=strlen (upi->input.keys[i].name) +1 +SQL_VALSIZE ; + } + size +=strlen (procedure); + +ulogd_log (ULOGD_DEBUG ,"allocating %u bytes for statement\n" , size); + + di->stmt =malloc (size); +if (!di->stmt) { +ulogd_log (ULOGD_ERROR ,"OOM!\n" ); +return -ENOMEM ; + } + di->ring.length = size +1 ; + +if (strncasecmp (procedure,"INSERT" ,strlen ("INSERT" )) ==0 && + (procedure[strlen ("INSERT" )] =='\0' || + procedure[strlen ("INSERT" )] ==' ' )) { +char *stmt_val = di->stmt; + +if (procedure[6 ] =='\0' ) { +/* procedure == "INSERT" */ +if (di->schema) + stmt_val +=sprintf (stmt_val, +"insert into %s.%s (" , + di->schema, table); +else + stmt_val +=sprintf (stmt_val, +"insert into %s (" , table); + }else + stmt_val +=sprintf (stmt_val,"%s (" , procedure); + +for (i =0 ; i < upi->input.num_keys; i++) { +char *underscore; + +if (upi->input.keys[i].flags &ULOGD_KEYF_INACTIVE ) +continue ; + + underscore = stmt_val; + + stmt_val +=sprintf (stmt_val,"%s," , + upi->input.keys[i].name); + +while ((underscore =strchr (underscore,'.' ))) + *underscore ='_' ; + } + *(stmt_val -1 ) =')' ; + +sprintf (stmt_val," values (" ); + }else if (strncasecmp (procedure,"CALL" ,strlen ("CALL" )) ==0 ) { +sprintf (di->stmt,"CALL %s(" , procedure); + }else { +sprintf (di->stmt,"SELECT %s(" , procedure); + } + + di->stmt_offset =strlen (di->stmt); + +ulogd_log (ULOGD_DEBUG ,"stmt='%s'\n" , di->stmt); + +return 0 ; +} + +static void +_bind_sql_stmt (struct ulogd_pluginstance *upi,char *start) {struct db_instance *di = (struct db_instance *) &upi->private;
@@ -399,7 +572,11 @@ static void __format_query_db(struct ulogd_pluginstance *upi, char *start) *(stmt_ins -1 ) =')' ; } -static int __add_to_backlog (struct ulogd_pluginstance *upi,const char *stmt,unsigned int len) +/******************************************************************************/ + +static int +_add_to_backlog (struct ulogd_pluginstance *upi, +const char *stmt,unsigned int len) {struct db_instance *di = (struct db_instance *) &upi->private;unsigned int query_size;
@@ -440,40 +617,8 @@ static int __add_to_backlog(struct ulogd_pluginstance *upi, const char *stmt, unreturn 0 ; } -static int _init_db (struct ulogd_pluginstance *upi) -{ -struct db_instance *di = (struct db_instance *) upi->private; - -if (di->reconnect && di->reconnect >time (NULL )) { -/* store entry to backlog if it is active */ -if (di->backlog_memcap && !di->backlog_full) { -__format_query_db (upi, di->stmt); -__add_to_backlog (upi, di->stmt, -strlen (di->stmt)); - } -return 0 ; - } - -if (di->driver->open_db (upi)) { -ulogd_log (ULOGD_ERROR ,"can't establish database connection\n" ); -if (di->backlog_memcap && !di->backlog_full) { -__format_query_db (upi, di->stmt); -__add_to_backlog (upi, di->stmt,strlen (di->stmt)); - } -return _init_reconnect (upi); - } - -/* enable 'real' logging */ - di->interp = &__interp_db; - - di->reconnect =0 ; - -/* call the interpreter function to actually write the -* log line that we wanted to write */ -return __interp_db (upi); -} - -static int __treat_backlog (struct ulogd_pluginstance *upi) +static int +_process_backlog (struct ulogd_pluginstance *upi) {struct db_instance *di = (struct db_instance *) &upi->private;int i = di->backlog_oneshot;
@@ -488,7 +633,7 @@ static int __treat_backlog(struct ulogd_pluginstance *upi)if (di->driver->execute (upi, query->stmt, query->len) <0 ) {/* error occur, database connexion need to be closed */ di->driver->close_db (upi); -return _init_reconnect (upi); +return _reconnect_db (upi); }else { di->backlog_memusage -=sizeof (*query) + query->len +1 ;llist_del (&query->list);
@@ -501,7 +646,10 @@ static int __treat_backlog(struct ulogd_pluginstance *upi)return 0 ; } -static int __add_to_ring (struct ulogd_pluginstance *upi,struct db_instance *di) +/******************************************************************************/ + +static int +_add_to_ring (struct ulogd_pluginstance *upi,struct db_instance *di) {if (*di->ring.wr_place ==RING_QUERY_READY ) {if (di->ring.full ==0 ) {
@@ -513,7 +661,7 @@ static int __add_to_ring(struct ulogd_pluginstance *upi, struct db_instance *di)ulogd_log (ULOGD_NOTICE ,"Recovered some place in ring\n" ); di->ring.full =0 ; } -__format_query_db (upi, di->ring.wr_place +1 ); +_bind_sql_stmt (upi, di->ring.wr_place +1 ); *di->ring.wr_place =RING_QUERY_READY ;pthread_cond_signal (&di->ring.cond); di->ring.wr_item ++;
@@ -525,55 +673,8 @@ static int __add_to_ring(struct ulogd_pluginstance *upi, struct db_instance *di)return ULOGD_IRET_OK ; } -/* our main output function, called by ulogd */ -static int __interp_db (struct ulogd_pluginstance *upi) -{ -struct db_instance *di = (struct db_instance *) &upi->private; - -if (di->ring.size) -return __add_to_ring (upi, di); - -__format_query_db (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 __treat_backlog (upi); -else { - ret =__treat_backlog (upi); -if (ret) -return ret; -/* try adding once the data to backlog */ -return __add_to_backlog (upi, di->stmt,strlen (di->stmt)); - } - } - -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 _init_reconnect (upi); - } - -return 0 ; -} - -static int __loop_reconnect_db (struct ulogd_pluginstance * upi) { -struct db_instance *di = (struct db_instance *) &upi->private; - - di->driver->close_db (upi); -while (1 ) { -if (di->driver->open_db (upi)) { -sleep (1 ); - }else { -return 0 ; - } - } -return 0 ; -} - -static void *__inject_thread (void *gdi) +static void * +_process_ring (void *gdi) {struct ulogd_pluginstance *upi = gdi;struct db_instance *di = (struct db_instance *) &upi->private;
@@ -587,11 +688,11 @@ static void *__inject_thread(void *gdi)while (*wr_place ==RING_QUERY_READY ) {if (di->driver->execute (upi, wr_place +1 ,strlen (wr_place +1 )) <0 ) { -if (__loop_reconnect_db (upi) !=0 ) { +if (_loop_reconnect_db (upi) !=0 ) {/* loop has failed on unrecoverable error */ ulogd_log (ULOGD_ERROR ,"permanently disabling plugin\n" ); - di->interp = &disabled_interp_db; + di->interp = &_interp_db_disabled;return NULL ; }else /* try to re run query */ continue ;
@@ -609,86 +710,18 @@ static void *__inject_thread(void *gdi)return NULL ; } - -void ulogd_db_signal (struct ulogd_pluginstance *upi,int signal) +static int +_loop_reconnect_db (struct ulogd_pluginstance * upi) {struct db_instance *di = (struct db_instance *) &upi->private; -switch (signal) { -case SIGHUP : -if (!di->ring.size) { -/* reopen database connection */ -ulogd_db_instance_stop (upi); -ulogd_db_start (upi); - }else -ulogd_log (ULOGD_ERROR , -"No SIGHUP handling if ring buffer is used\n" ); -break ; -case SIGTERM : -case SIGINT : -if (di->ring.size) { -int s =pthread_cancel (di->db_thread_id); -if (s !=0 ) { -ulogd_log (ULOGD_ERROR , -"Can't cancel injection thread\n" ); -break ; - } - s =pthread_join (di->db_thread_id,NULL ); -if (s !=0 ) { -ulogd_log (ULOGD_ERROR , -"Error waiting for injection thread" -"cancelation\n" ); - } - } -break ; -default : -break ; - } -} - -static char * -_format_key (char *key) -{ -char *cp = key; - -/* replace all underscores with dots */ -while ((cp =strchr (cp,'_' ))) - *cp ='.' ; - -return key; -} - -int -ulogd_db_alloc_input_keys (struct ulogd_pluginstance *upi, -unsigned int num_keys,void *arg) -{ -struct db_instance *di = (struct db_instance *) &upi->private; -char *(*format_key)(char *) = di->driver->format_key ? : _format_key; -unsigned int i; - -if (upi->input.keys) -free (upi->input.keys); - upi->input.num_keys = num_keys; -ulogd_log (ULOGD_DEBUG ,"%u fields in table\n" , upi->input.num_keys); - upi->input.keys =calloc (upi->input.num_keys,sizeof (upi->input.keys[0 ])); -if (!upi->input.keys) { - upi->input.num_keys =0 ; -ulogd_log (ULOGD_ERROR ,"ENOMEM\n" ); -return -ENOMEM ; - } - -for (i =0 ; i < num_keys; i++) { -const char *col = di->driver->get_column (arg, i); - -if (!col) -break ; - -snprintf (upi->input.keys[i].name, -sizeof (upi->input.keys[i].name), -"%s" , col); - -format_key (upi->input.keys[i].name); + di->driver->close_db (upi); +while (1 ) { +if (di->driver->open_db (upi)) { +sleep (1 ); + }else { +return 0 ; + } } -return 0 ; }
Signature · 1 line
2.35.1
PATCH 16/34 db: use consistent integer return values to indicate errors
In reply to Jeremy SowdenInternally, 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.
--- 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(-)
output/dbi/ulogd_output_DBI.c +1 -1
@@ -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);
output/pgsql/ulogd_output_PGSQL.c +4 -4
@@ -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);
util/db.c +33 -26
148 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 17/34 db: change return type of two functions to `void`
In reply to Jeremy Sowden_stop_db and _loop_reconnect_db always return successfully. Change the return types and remove error-checking from callers.
--- util/db.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-)
util/db.c +9 -15
@@ -43,7 +43,7 @@ static int _interp_db_init(struct ulogd_pluginstance *upi);static int _interp_db_main (struct ulogd_pluginstance *upi);static int _interp_db_disabled (struct ulogd_pluginstance *upi);static int _reconnect_db (struct ulogd_pluginstance *upi); -static int _stop_db (struct ulogd_pluginstance *upi); +static void _stop_db (struct ulogd_pluginstance *upi);static char *_format_key (char *key);static int _create_sql_stmt (struct ulogd_pluginstance *upi);
@@ -56,7 +56,7 @@ static int _process_backlog(struct ulogd_pluginstance *upi);static int _add_to_ring (struct ulogd_pluginstance *upi,struct db_instance *di);static void *_process_ring (void *arg); -static int _loop_reconnect_db (struct ulogd_pluginstance *upi); +static void _loop_reconnect_db (struct ulogd_pluginstance *upi); int ulogd_db_configure(struct ulogd_pluginstance *upi,
@@ -378,7 +378,7 @@ _reconnect_db(struct ulogd_pluginstance *upi)return 0 ; } -static int +static void _stop_db (struct ulogd_pluginstance *upi) {struct db_instance *di = (struct db_instance *) upi->private;
@@ -397,7 +397,6 @@ _stop_db(struct ulogd_pluginstance *upi)pthread_mutex_destroy (&di->ring.mutex); di->ring.ring =NULL ; } -return 0 ; } static char *
@@ -695,14 +694,10 @@ _process_ring(void *gdi)while (*wr_place ==RING_QUERY_READY ) {if (di->driver->execute (upi, wr_place +1 ,strlen (wr_place +1 )) <0 ) { -if (_loop_reconnect_db (upi) !=0 ) { -/* loop has failed on unrecoverable error */ -ulogd_log (ULOGD_ERROR , -"permanently disabling plugin\n" ); - di->interp = &_interp_db_disabled; -return NULL ; - }else /* try to re run query */ -continue ; +_loop_reconnect_db (upi); +/* try to re run query */ +continue ; + } *wr_place =RING_NO_QUERY ; di->ring.rd_item++;
@@ -717,7 +712,7 @@ _process_ring(void *gdi)return NULL ; } -static int +static void _loop_reconnect_db (struct ulogd_pluginstance * upi) {struct db_instance *di = (struct db_instance *) &upi->private;
@@ -727,8 +722,7 @@ _loop_reconnect_db(struct ulogd_pluginstance * upi)if (di->driver->open_db (upi) <0 ) {sleep (1 ); }else { -return 0 ; +return ; } } -return 0 ; }
Signature · 1 line
2.35.1
PATCH 18/34 db: open-code `_loop_reconnect_db`
In reply to Jeremy SowdenNow that _loop_reconnect_db returns void, it can be reduced to while (open_db fails) sleep 1 second, so remove the function altogther and open code the simplified while-loop.
--- util/db.c | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-)
util/db.c +3 -18
@@ -56,7 +56,6 @@ static int _process_backlog(struct ulogd_pluginstance *upi);static int _add_to_ring (struct ulogd_pluginstance *upi,struct db_instance *di);static void *_process_ring (void *arg); -static void _loop_reconnect_db (struct ulogd_pluginstance *upi); int ulogd_db_configure(struct ulogd_pluginstance *upi,
@@ -694,10 +693,11 @@ _process_ring(void *gdi)while (*wr_place ==RING_QUERY_READY ) {if (di->driver->execute (upi, wr_place +1 ,strlen (wr_place +1 )) <0 ) { -_loop_reconnect_db (upi); + di->driver->close_db (upi); +while (di->driver->open_db (upi) <0 ) +sleep (1 );/* try to re run query */ continue ; - } *wr_place =RING_NO_QUERY ; di->ring.rd_item++;
@@ -711,18 +711,3 @@ _process_ring(void *gdi)return NULL ; } - -static void -_loop_reconnect_db (struct ulogd_pluginstance * upi) -{ -struct db_instance *di = (struct db_instance *) &upi->private; - - di->driver->close_db (upi); -while (1 ) { -if (di->driver->open_db (upi) <0 ) { -sleep (1 ); - }else { -return ; - } - } -}
Signature · 1 line
2.35.1
PATCH 19/34 db: improve calculation of sql statement length
In reply to Jeremy SowdenCurrently, we calculate the space required from a prefix which doesn't correspond to anything we use, space for each key, although the SQL statement may not contain the keys, and a fixed amount for each value. However, we can use snprintf to tighten up the estimate by using snprintf(NULL, 0, ...) to tell us how much room we actually need for the parts we know in advance.
Rename a couple of variables and replace strlen with sizeof where appropriate.
Sort included headers.
--- util/db.c | 149 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 105 insertions(+), 44 deletions(-)
util/db.c +105 -44
@@ -24,15 +24,16 @@* */ -#include <unistd.h> -#include <stdlib.h> -#include <string.h> #include <errno.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <time.h> #include <inttypes.h> #include <pthread.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> +#include <arpa/inet.h> +#include <netinet/in.h> #include <ulogd/ulogd.h> #include <ulogd/db.h>
@@ -47,6 +48,10 @@ static void _stop_db(struct ulogd_pluginstance *upi);static char *_format_key (char *key);static int _create_sql_stmt (struct ulogd_pluginstance *upi); +static unsigned int _calc_sql_stmt_size (const char *procedure, +const char *schema,const char *table, +struct ulogd_key *keys, +unsigned int num_keys);static void _bind_sql_stmt (struct ulogd_pluginstance *upi,char *stmt);
@@ -410,33 +415,23 @@ _format_key(char *key)return key; } -#define SQL_INSERTTEMPL "SELECT P(Y)" -#define SQL_VALSIZE 100 -/* create the static part of our insert statement */ static int _create_sql_stmt (struct ulogd_pluginstance *upi) {struct db_instance *di = (struct db_instance *) upi->private; +char *procedure =procedure_ce (upi->config_kset).u.string; +char *table =table_ce (upi->config_kset).u.string;unsigned int size;unsigned int i; -char *table =table_ce (upi->config_kset).u.string; -char *procedure =procedure_ce (upi->config_kset).u.string; +char *stmtp;if (di->stmt)free (di->stmt); -/* caclulate the size for the insert statement */ - size =strlen (SQL_INSERTTEMPL ) +strlen (table); - -for (i =0 ; i < upi->input.num_keys; i++) { -if (upi->input.keys[i].flags &ULOGD_KEYF_INACTIVE ) -continue ; -/* we need space for the key and a comma, as well as -* enough space for the values */ - size +=strlen (upi->input.keys[i].name) +1 +SQL_VALSIZE ; - } - size +=strlen (procedure); +/* calculate the size for the insert statement */ + size =_calc_sql_stmt_size (procedure, di->schema, table, + upi->input.keys, upi->input.num_keys);ulogd_log (ULOGD_DEBUG ,"allocating %u bytes for statement\n" , size);
@@ -447,22 +442,22 @@ _create_sql_stmt(struct ulogd_pluginstance *upi) } di->ring.length = size +1 ; -if (strncasecmp (procedure,"INSERT" ,strlen ("INSERT" )) ==0 && - (procedure[strlen ("INSERT" )] =='\0' || - procedure[strlen ("INSERT" )] ==' ' )) { -char *stmt_val = di->stmt; + stmtp = di->stmt; + +if (strncasecmp (procedure,"INSERT" ,sizeof ("INSERT" ) -1 ) ==0 && + (procedure[sizeof ("INSERT" ) -1 ] =='\0' || + procedure[sizeof ("INSERT" ) -1 ] ==' ' )) { -if (procedure[6 ] =='\0' ) { +if (procedure[sizeof ("INSERT" ) -1 ] =='\0' ) {/* procedure == "INSERT" */ if (di->schema) - stmt_val +=sprintf (stmt_val, -"insert into %s.%s (" , - di->schema, table); + stmtp +=sprintf (stmtp,"insert into %s.%s (" , + di->schema, table);else - stmt_val +=sprintf (stmt_val, -"insert into %s (" , table); + stmtp +=sprintf (stmtp,"insert into %s (" , + table); }else - stmt_val +=sprintf (stmt_val,"%s (" , procedure); + stmtp +=sprintf (stmtp,"%s (" , procedure);for (i =0 ; i < upi->input.num_keys; i++) {char *underscore;
@@ -470,30 +465,96 @@ _create_sql_stmt(struct ulogd_pluginstance *upi)if (upi->input.keys[i].flags &ULOGD_KEYF_INACTIVE )continue ; - underscore = stmt_val; + underscore = stmtp; - stmt_val +=sprintf (stmt_val,"%s," , - upi->input.keys[i].name); + stmtp +=sprintf (stmtp,"%s," , + upi->input.keys[i].name);while ((underscore =strchr (underscore,'.' ))) *underscore ='_' ; } - *(stmt_val -1 ) =')' ; + stmtp --; -sprintf (stmt_val," values (" ); - }else if (strncasecmp (procedure,"CALL" ,strlen ("CALL" )) ==0 ) { -sprintf (di->stmt,"CALL %s(" , procedure); - }else { -sprintf (di->stmt,"SELECT %s(" , procedure); - } + stmtp +=sprintf (stmtp,") values (" ); + + }else if (strncasecmp (procedure,"CALL" ,sizeof ("CALL" ) -1 ) ==0 ) + stmtp +=sprintf (stmtp,"CALL %s(" , procedure); +else + stmtp +=sprintf (stmtp,"SELECT %s(" , procedure); - di->stmt_offset =strlen (di->stmt); + di->stmt_offset = stmtp - di->stmt;ulogd_log (ULOGD_DEBUG ,"stmt='%s'\n" , di->stmt);return 0 ; } +#define SQL_VALSIZE 100 + +static unsigned int +_calc_sql_stmt_size (const char *procedure, +const char *schema,const char *table, +struct ulogd_key *keys,unsigned int num_keys) +{ +unsigned int i, size =0 ; +bool include_keys; + +/* Fixed size bit */ + +if (strncasecmp (procedure,"INSERT" ,sizeof ("INSERT" ) -1 ) ==0 && + (procedure[sizeof ("INSERT" ) -1 ] =='\0' || + procedure[sizeof ("INSERT" ) -1 ] ==' ' )) { + +/* insert into t (k0, k1, ...) values (v0, v1, ...) */ + +if (procedure[sizeof ("INSERT" ) -1 ] =='\0' ) { +/* procedure == "INSERT" */ +if (schema) + size +=snprintf (NULL ,0 , +"insert into %s.%s (" , + schema, table); +else + size +=snprintf (NULL ,0 , +"insert into %s (" , table); + }else + size +=snprintf (NULL ,0 ,"%s (" , procedure); + + size +=snprintf (NULL ,0 ,") values (" ); + + include_keys = true; + + }else { + +/* `call p(v0, v1, ...)` or `select p(v0, v1, ...)` */ + +if (strncasecmp (procedure,"CALL" ,sizeof ("CALL" ) -1 ) ==0 ) + size +=snprintf (NULL ,0 ,"CALL %s(" , procedure); +else + size +=snprintf (NULL ,0 ,"SELECT %s(" , procedure); + + include_keys = false; + + } + +/* Per-field bits. +* +* We need space for the value and a comma or the closing parenthesis. +* We may also need space for the key and a comma. +*/ + +for (i =0 ; i < num_keys; i++) { +if (keys[i].flags &ULOGD_KEYF_INACTIVE ) +continue ; +if (include_keys) + size +=strlen (keys[i].name) +1 ; + size +=SQL_VALSIZE +1 ; + } + + size++;/* Allow for the final NUL */ + +return size; +} +static void _bind_sql_stmt (struct ulogd_pluginstance *upi,char *start) {
Signature · 1 line
2.35.1
PATCH 20/34 db: refactor configuration
In reply to Jeremy SowdenMove DB, back-log and ring-buffer config code into separate functions.
No functional changes.
--- util/db.c | 118 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 35 deletions(-)
util/db.c +83 -35
@@ -40,6 +40,7 @@/* generic db layer */ +static int _configure_db (struct ulogd_pluginstance *upi);static int _interp_db_init (struct ulogd_pluginstance *upi);static int _interp_db_main (struct ulogd_pluginstance *upi);static int _interp_db_disabled (struct ulogd_pluginstance *upi);
@@ -55,10 +56,12 @@ static unsigned int _calc_sql_stmt_size(const char *procedure,static void _bind_sql_stmt (struct ulogd_pluginstance *upi,char *stmt); +static int _configure_backlog (struct ulogd_pluginstance *upi);static int _add_to_backlog (struct ulogd_pluginstance *upi,const char *stmt,unsigned int len);static int _process_backlog (struct ulogd_pluginstance *upi); +static int _configure_ring (struct ulogd_pluginstance *upi);static int _add_to_ring (struct ulogd_pluginstance *upi,struct db_instance *di);static void *_process_ring (void *arg);
@@ -66,53 +69,23 @@ int ulogd_db_configure(struct ulogd_pluginstance *upi, struct ulogd_pluginstance_stack *stack) { - struct db_instance *di = (struct db_instance *) upi->private; int ret;ulogd_log (ULOGD_NOTICE ,"(re)configuring\n" ); -/* First: Parse configuration file section for this instance */ - ret =config_parse_file (upi->id, upi->config_kset); + ret =_configure_db (upi);if (ret <0 ) { -ulogd_log (ULOGD_ERROR ,"error parsing config file\n" );return ret; } -/* Second: Open Database */ - ret = di->driver->open_db (upi); + ret =_configure_backlog (upi);if (ret <0 ) { -ulogd_log (ULOGD_ERROR ,"error in open_db\n" );return ret; } -/* Third: Determine required input keys for given table */ - ret = di->driver->get_columns (upi); -if (ret <0 ) -ulogd_log (ULOGD_ERROR ,"error in get_columns\n" ); - -/* Close database, since ulogd core could just call configure -* but abort during input key resolving routines. configure -* doesn't have a destructor... */ - di->driver->close_db (upi); - -INIT_LLIST_HEAD (&di->backlog); - di->backlog_memusage =0 ; - - di->ring.size =ringsize_ce (upi->config_kset).u.value; - di->backlog_memcap =backlog_memcap_ce (upi->config_kset).u.value; - -if (di->ring.size && di->backlog_memcap) { -ulogd_log (ULOGD_ERROR ,"Ring buffer has precedence over backlog\n" ); - di->backlog_memcap =0 ; - }else if (di->backlog_memcap >0 ) { - di->backlog_oneshot =backlog_oneshot_ce (upi->config_kset).u.value; -if (di->backlog_oneshot <=2 ) { -ulogd_log (ULOGD_ERROR , -"backlog_oneshot_requests must be > 2 to hope" -" cleaning. Setting it to 3.\n" ); - di->backlog_oneshot =3 ; - } - di->backlog_full =0 ; + ret =_configure_ring (upi); +if (ret <0 ) { +return ret; }return 0 ;
@@ -274,6 +247,40 @@ ulogd_db_stop(struct ulogd_pluginstance *upi)/******************************************************************************/ +static int +_configure_db (struct ulogd_pluginstance *upi) +{ +struct db_instance *di = (struct db_instance *) upi->private; +int ret; + +/* First: Parse configuration file section for this instance */ + ret =config_parse_file (upi->id, upi->config_kset); +if (ret <0 ) { +ulogd_log (ULOGD_ERROR ,"error parsing config file\n" ); +return ret; + } + +/* Second: Open Database */ + ret = di->driver->open_db (upi); +if (ret <0 ) { +ulogd_log (ULOGD_ERROR ,"error in open_db\n" ); +return ret; + } + +/* Third: Determine required input keys for given table */ + ret = di->driver->get_columns (upi); +if (ret <0 ) +ulogd_log (ULOGD_ERROR ,"error in get_columns\n" ); + +/* Close database, since ulogd core could just call configure +* but abort during input key resolving routines. configure +* doesn't have a destructor... */ + di->driver->close_db (upi); + +return ret; + +} +static int _interp_db_init (struct ulogd_pluginstance *upi) {
@@ -640,6 +647,37 @@ _bind_sql_stmt(struct ulogd_pluginstance *upi, char *start)/******************************************************************************/ +static int +_configure_backlog (struct ulogd_pluginstance *upi) +{ +struct db_instance *di = (struct db_instance *) &upi->private; + +INIT_LLIST_HEAD (&di->backlog); + + di->backlog_memusage =0 ; + di->backlog_memcap =backlog_memcap_ce (upi->config_kset).u.value; + di->backlog_full =0 ; + +if (di->backlog_memcap ==0 ) +return 0 ; + +if (ringsize_ce (upi->config_kset).u.value) { +ulogd_log (ULOGD_ERROR , +"Ring buffer has precedence over backlog\n" ); + di->backlog_memcap =0 ; +return 0 ; + } + + di->backlog_oneshot =backlog_oneshot_ce (upi->config_kset).u.value; +if (di->backlog_oneshot <=2 ) { +ulogd_log (ULOGD_ERROR , +"backlog_oneshot_requests must be > 2 to be effective. Setting it to 3.\n" ); + di->backlog_oneshot =3 ; + } + +return 0 ; +} +static int _add_to_backlog (struct ulogd_pluginstance *upi,const char *stmt,unsigned int len)
@@ -714,6 +752,16 @@ _process_backlog(struct ulogd_pluginstance *upi)/******************************************************************************/ +static int +_configure_ring (struct ulogd_pluginstance *upi) +{ +struct db_instance *di = (struct db_instance *) &upi->private; + + di->ring.size =ringsize_ce (upi->config_kset).u.value; + +return 0 ; +} +static int _add_to_ring (struct ulogd_pluginstance *upi,struct db_instance *di) {
Signature · 1 line
2.35.1
PATCH 21/34 db: refactor ring-buffer initialization
In reply to Jeremy SowdenMove the code to initialize and start the thread that manages the ring-buffer into a separate function.
No functional changes.
--- util/db.c | 87 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 34 deletions(-)
util/db.c +53 -34
@@ -62,6 +62,7 @@ static int _add_to_backlog(struct ulogd_pluginstance *upi,static int _process_backlog (struct ulogd_pluginstance *upi);static int _configure_ring (struct ulogd_pluginstance *upi); +static int _start_ring (struct ulogd_pluginstance *upi);static int _add_to_ring (struct ulogd_pluginstance *upi,struct db_instance *di);static void *_process_ring (void *arg);
@@ -132,7 +133,6 @@ ulogd_db_start(struct ulogd_pluginstance *upi) {struct db_instance *di = (struct db_instance *) upi->private;int ret; -unsigned int i;ulogd_log (ULOGD_NOTICE ,"starting\n" );
@@ -144,43 +144,13 @@ ulogd_db_start(struct ulogd_pluginstance *upi)if (ret <0 ) goto db_error; -if (di->ring.size >0 ) { -/* allocate */ - di->ring.ring =calloc (di->ring.size,sizeof (char ) * di->ring.length); -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" , - di->ring.size, di->ring.length); -/* init start of query for each element */ -for (i =0 ; i < di->ring.size; i++) { -strcpy (di->ring.ring + di->ring.length * i +1 , - di->stmt); - } -/* init cond & mutex */ - ret =pthread_cond_init (&di->ring.cond,NULL ); -if (ret !=0 ) - goto alloc_error; - ret =pthread_mutex_init (&di->ring.mutex,NULL ); -if (ret !=0 ) - goto cond_error; -/* create thread */ - ret =pthread_create (&di->db_thread_id,NULL , _process_ring, upi); -if (ret !=0 ) - goto mutex_error; - } + ret =_start_ring (upi); +if (ret <0 ) + goto db_error; di->interp = &_interp_db_init; -return 0 ; -mutex_error: -pthread_mutex_destroy (&di->ring.mutex); -cond_error: -pthread_cond_destroy (&di->ring.cond); -alloc_error: -free (di->ring.ring); db_error: di->driver->close_db (upi);return -1 ;
@@ -762,6 +732,55 @@ _configure_ring(struct ulogd_pluginstance *upi)return 0 ; } +static int +_start_ring (struct ulogd_pluginstance *upi) +{ +struct db_instance *di = (struct db_instance *) &upi->private; +unsigned int i; +int ret; + +if (di->ring.size ==0 ) +return 0 ; + +/* allocate */ + di->ring.ring =calloc (di->ring.size,sizeof (char ) * di->ring.length); +if (di->ring.ring ==NULL ) +return -1 ; + di->ring.wr_place = di->ring.ring; +ulogd_log (ULOGD_NOTICE , +"Allocating %d elements of size %d for ring\n" , + di->ring.size, di->ring.length); + +/* init start of query for each element */ +for (i =0 ; i < di->ring.size; i++) +strcpy (di->ring.ring + di->ring.length * i +1 , + di->stmt); + +/* init cond & mutex */ + ret =pthread_cond_init (&di->ring.cond,NULL ); +if (ret !=0 ) + goto alloc_error; + ret =pthread_mutex_init (&di->ring.mutex,NULL ); +if (ret !=0 ) + goto cond_error; + +/* create thread */ + ret =pthread_create (&di->db_thread_id,NULL , _process_ring, upi); +if (ret !=0 ) + goto mutex_error; + +return 0 ; + +mutex_error: +pthread_mutex_destroy (&di->ring.mutex); +cond_error: +pthread_cond_destroy (&di->ring.cond); +alloc_error: +free (di->ring.ring); + +return -1 ; +} +static int _add_to_ring (struct ulogd_pluginstance *upi,struct db_instance *di) {
Signature · 1 line
2.35.1
PATCH 22/34 db: refactor ring-buffer
In reply to Jeremy Sowden * Rename some fields.
* Use uint32_t consistently for all sizes and indices.
* Move thread ID into the ring structure.
* Replace status flag with a count of in-use elements.
--- include/ulogd/db.h | 25 ++++++------ util/db.c | 100 +++++++++++++++++++++++++-------------------- 2 files changed, 68 insertions(+), 57 deletions(-)
include/ulogd/db.h +12 -13
@@ -27,22 +27,22 @@ struct db_driver { }; -enum { -RING_NO_QUERY , -RING_QUERY_READY , -}; -struct db_stmt_ring { -/* Ring buffer: 1 status byte + string */ -char *ring;/* pointer to the ring */ -uint32_t size;/* size of ring buffer in element */ -int length;/* length of one ring buffer element */ -uint32_t wr_item;/* write item in ring buffer */ -uint32_t rd_item;/* read item in ring buffer */ -char *wr_place; + +char *elems;/* Buffer containing `size` strings of `length` bytes */ + +uint32_t size;/* No. of elements in ring buffer */ +uint32_t used;/* No. of elements in ring buffer in use */ +uint32_t length;/* Length of one element in ring buffer */ +uint32_t wr_idx;/* Index of next element to write in ring buffer */ +uint32_t rd_idx;/* Index of next element to read in ring buffer */ + +pthread_t thread_id;pthread_cond_t cond;pthread_mutex_t mutex; +int full; + };struct db_stmt {
@@ -60,7 +60,6 @@ struct db_instance {struct db_driver *driver;/* DB ring buffer */ struct db_stmt_ring ring; -pthread_t db_thread_id;/* Backlog system */ unsigned int backlog_memcap;unsigned int backlog_memusage;
util/db.c +56 -44
190 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 23/34 db: refactor backlog
In reply to Jeremy SowdenMove the backlog fields into a separate structure along the same lines as the ring-buffer.
--- include/ulogd/db.h | 18 +++++++++++++----- util/db.c | 44 ++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 27 deletions(-)
include/ulogd/db.h +13 -5
@@ -45,6 +45,18 @@ struct db_stmt_ring { }; +struct db_stmt_backlog { + +struct llist_head items; + +unsigned int memcap; +unsigned int memusage; +unsigned int oneshot; + +int full; + +}; +struct db_stmt {char *stmt;int len;
@@ -61,11 +73,7 @@ struct db_instance {/* DB ring buffer */ struct db_stmt_ring ring;/* Backlog system */ -unsigned int backlog_memcap; -unsigned int backlog_memusage; -unsigned int backlog_oneshot; -unsigned char backlog_full; -struct llist_head backlog; +struct db_stmt_backlog backlog; };#define RECONNECT_DEFAULT 2
util/db.c +22 -22
117 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 24/34 db: use `struct db_stmt` objects more widely
In reply to Jeremy SowdenThe back-log API uses a struct to encapsulate SQL statements. Elsewhere we pass pairs of string & length arguments. Extend struct db_stmt and reuse where it is useful.
Change references to "queries" to "statements" (we don't run any queries).
--- include/ulogd/db.h | 18 ++- output/dbi/ulogd_output_DBI.c | 6 +- output/mysql/ulogd_output_MYSQL.c | 4 +- output/pgsql/ulogd_output_PGSQL.c | 4 +- util/db.c | 185 +++++++++++++++--------------- 5 files changed, 110 insertions(+), 107 deletions(-)
include/ulogd/db.h +12 -6
@@ -11,6 +11,8 @@#include <ulogd/ulogd.h> +struct db_stmt ; +struct db_driver {int (*get_columns)(struct ulogd_pluginstance *upi);
@@ -23,13 +25,14 @@ struct db_driver {int (*escape_string)(struct ulogd_pluginstance *upi,char *dst,const char *src,unsigned int len);int (*execute)(struct ulogd_pluginstance *upi, -const char *stmt,unsigned int len); +const struct db_stmt *stmt); };struct db_stmt_ring { -char *elems;/* Buffer containing `size` strings of `length` bytes */ +struct db_stmt *elems;/* Buffer containing `size` statements of +* `length` bytes */ uint32_t size;/* No. of elements in ring buffer */ uint32_t used;/* No. of elements in ring buffer in use */
@@ -58,14 +61,17 @@ struct db_stmt_backlog { };struct db_stmt { -char *stmt; -int len; +unsigned int len, size; +char sql[]; +}; + +struct db_stmt_item {struct llist_head list; +struct db_stmt stmt; };struct db_instance { -char *stmt;/* buffer for our insert statement */ -int stmt_offset;/* offset to the beginning of the "VALUES" part */ +struct db_stmt *stmt;/* buffer for our insert statement */ char *schema;time_t reconnect;int (*interp)(struct ulogd_pluginstance *upi);
output/dbi/ulogd_output_DBI.c +3 -3
@@ -239,18 +239,18 @@ static int escape_string_dbi(struct ulogd_pluginstance *upi, }static int execute_dbi (struct ulogd_pluginstance *upi, -const char *stmt,unsigned int len) +const struct db_stmt *stmt) {struct dbi_instance *pi = (struct dbi_instance *) upi->private; - pi->result =dbi_conn_query (pi->dbh,stmt); + pi->result =dbi_conn_query (pi->dbh, stmt->sql);if (!pi->result) {const char *errptr;dbi_conn_error (pi->dbh, &errptr);ulogd_log (ULOGD_ERROR ,"execute failed (%s)\n" , errptr);ulogd_log (ULOGD_DEBUG ,"failed query: [%s]\n" , - stmt); + stmt->sql);return -1 ; }
output/mysql/ulogd_output_MYSQL.c +2 -2
@@ -206,13 +206,13 @@ static int escape_string_mysql(struct ulogd_pluginstance *upi, }static int execute_mysql (struct ulogd_pluginstance *upi, -const char *stmt,unsigned int len) +const struct db_stmt *stmt) {struct mysql_instance *mi = (struct mysql_instance *) upi->private;int ret;MYSQL_RES * result; - ret =mysql_real_query (mi->dbh, stmt, len); + ret =mysql_real_query (mi->dbh, stmt->sql, stmt->len);if (ret) {ulogd_log (ULOGD_ERROR ,"execute failed (%s)\n" ,mysql_error (mi->dbh));
output/pgsql/ulogd_output_PGSQL.c +2 -2
@@ -283,11 +283,11 @@ static int escape_string_pgsql(struct ulogd_pluginstance *upi, }static int execute_pgsql (struct ulogd_pluginstance *upi, -const char *stmt,unsigned int len) +const struct db_stmt *stmt) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private; - pi->pgres =PQexec (pi->dbh, stmt); + pi->pgres =PQexec (pi->dbh, stmt->sql);if (!(pi->pgres && ((PQresultStatus (pi->pgres) ==PGRES_COMMAND_OK ) || (PQresultStatus (pi->pgres) ==PGRES_TUPLES_OK )))) { ulogd_log(ULOGD_ERROR, "execute failed (%s)\n",
util/db.c +91 -94
400 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 25/34 db: synchronize access to ring-buffer
In reply to Jeremy SowdenThere are a mutex and condition-variable associated with the ring-buffer. However, they are not used to synchronize access to the buffer, but only to wake the thread that processes the buffer when new statements are added to it. Thus there is nothing to prevent concurrent modifications of the buffer.
Instead, acquire the mutex before adding to the buffer, and, in the processing thread, copy the statement we're about to execute out of the buffer and release the mutex while processing it.
--- include/ulogd/db.h | 2 ++ util/db.c | 63 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 12 deletions(-)
include/ulogd/db.h +2 -0
@@ -34,6 +34,8 @@ struct db_stmt_ring {struct db_stmt *elems;/* Buffer containing `size` statements of * `length` bytes */ +struct db_stmt *stmt;/* Currently executing statement */ +uint32_t size;/* No. of elements in ring buffer */ uint32_t used;/* No. of elements in ring buffer in use */ uint32_t length;/* Length of one element in ring buffer */
util/db.c +51 -12
126 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 26/34 db: avoid cancelling ring-buffer thread
In reply to Jeremy SowdenUsing pthread_cancel can leave the DB driver in an inconsistent state and may lead to undefined behaviour when cleaning up the plug-in. Instead of cancelling the thread, set a flag and signal the condition variable, thereby giving the thread a chance to exit in good order.
--- include/ulogd/db.h | 1 + util/db.c | 30 +++++++++++++----------------- 2 files changed, 14 insertions(+), 17 deletions(-)
include/ulogd/db.h +1 -0
@@ -47,6 +47,7 @@ struct db_stmt_ring {pthread_mutex_t mutex;int full; +volatile sig_atomic_t shut_down; };
util/db.c +13 -17
@@ -184,17 +184,11 @@ ulogd_db_signal(struct ulogd_pluginstance *upi, int signal)case SIGTERM :case SIGINT :if (di->ring.size) { -int s =pthread_cancel (di->ring.thread_id); -if (s !=0 ) { + di->ring.shut_down =1 ; +pthread_cond_signal (&di->ring.cond); +if (pthread_join (di->ring.thread_id,NULL ) !=0 )ulogd_log (ULOGD_ERROR , -"Can't cancel ring-processing thread\n" ); -break ; - } - s =pthread_join (di->ring.thread_id,NULL ); -if (s !=0 ) { -ulogd_log (ULOGD_ERROR , -"Error waiting for ring-processing thread cancellation\n" ); - } +"Error waiting for ring-processing thread exit\n" ); }break ;default :
@@ -371,8 +365,6 @@ _stop_db(struct ulogd_pluginstance *upi) di->stmt =NULL ; }if (di->ring.size >0 ) { -pthread_cancel (di->ring.thread_id); -pthread_cond_destroy (&di->ring.cond);pthread_mutex_destroy (&di->ring.mutex);
@@ -832,11 +824,11 @@ _process_ring(void *arg)pthread_mutex_lock (&di->ring.mutex); -while (1 ) { +while (!di->ring.shut_down) {pthread_cond_wait (&di->ring.cond, &di->ring.mutex); -while (di->ring.used >0 ) { +while (!di->ring.shut_down && di->ring.used >0 ) {memcpy (stmt,_get_ring_elem (&di->ring, di->ring.rd_idx), di->ring.length);
@@ -859,9 +851,13 @@ exec_stmt:* statement. */ di->driver->close_db (upi); -while (di->driver->open_db (upi) <0 ) -sleep (1 ); - goto exec_stmt; +while (!di->ring.shut_down) { +if (di->driver->open_db (upi) <0 ) { +sleep (1 ); +continue ; + } + goto exec_stmt; + } }
Signature · 1 line
2.35.1
PATCH 27/34 db, IP2BIN: defer formatting of raw strings
In reply to Jeremy SowdenCurrently, the only use of the output of IP2BIN is embedding in literal SQL strings, so it converts the binary data to a suitably formatted hexadecimal string and propagates that. However, we will shortly introduce prep & exec support to the DB API, at which point we may want the raw binary data in the output plug-ins. Therefore, in order to avoid having to decode the hex back to binary, defer the hex encoding to where it is used.
At the same time, resize the buffers. IPv6 addresses are 128 *bits*, so the buffers do not need to be 128 *bytes*.
--- filter/ulogd_filter_IP2BIN.c | 61 +++++++++++++----------------------- util/db.c | 10 +++++- 2 files changed, 31 insertions(+), 40 deletions(-)
filter/ulogd_filter_IP2BIN.c +22 -39
@@ -28,8 +28,6 @@#include <ulogd/ulogd.h> #include <netinet/if_ether.h> -#define IPADDR_LENGTH 128 -enum input_keys {KEY_OOB_FAMILY ,KEY_OOB_PROTOCOL ,
@@ -89,32 +87,38 @@ static struct ulogd_key ip2bin_inp[] = {static struct ulogd_key ip2bin_keys[] = { { .type =ULOGD_RET_RAWSTR , + .len =sizeof (struct in6_addr ), .name ="ip.saddr.bin" , }, { .type =ULOGD_RET_RAWSTR , + .len =sizeof (struct in6_addr ), .name ="ip.daddr.bin" , }, { .type =ULOGD_RET_RAWSTR , + .len =sizeof (struct in6_addr ), .name ="orig.ip.saddr.bin" , }, { .type =ULOGD_RET_RAWSTR , + .len =sizeof (struct in6_addr ), .name ="orig.ip.daddr.bin" , }, { .type =ULOGD_RET_RAWSTR , + .len =sizeof (struct in6_addr ), .name ="reply.ip.saddr.bin" , }, { .type =ULOGD_RET_RAWSTR , + .len =sizeof (struct in6_addr ), .name ="reply.ip.daddr.bin" , }, }; -static char ipbin_array[MAX_KEY -START_KEY +1 ][IPADDR_LENGTH ]; +static unsigned char ipbin_array[MAX_KEY -START_KEY +1 ][sizeof (struct in6_addr )];/** * Convert IPv4 address (as 32-bit unsigned integer) to IPv6 address:
@@ -130,13 +134,8 @@ static inline void uint32_to_ipv6(const uint32_t ipv4, struct in6_addr *ipv6)static int ip2bin (struct ulogd_key *inp,int index,int oindex) { -char family =ikey_get_u8 (&inp[KEY_OOB_FAMILY ]); -char convfamily = family; -unsigned char *addr8; -struct in6_addr *addr; -struct in6_addr ip4_addr; -char *buffer; -int i, written; +char family =ikey_get_u8 (&inp[KEY_OOB_FAMILY ]), convfamily = family; +struct in6_addr *addr, ip4_addr;if (family ==AF_BRIDGE ) {if (!pp_is_valid (inp,KEY_OOB_PROTOCOL )) {
@@ -162,37 +161,21 @@ static int ip2bin(struct ulogd_key *inp, int index, int oindex) }switch (convfamily) { -case AF_INET6 : - addr = (struct in6_addr *)ikey_get_u128 (&inp[index]); -break ; -case AF_INET : -/* Convert IPv4 to IPv4 in IPv6 */ - addr = &ip4_addr; -uint32_to_ipv6 (ikey_get_u32 (&inp[index]), addr); -break ; -default : -/* TODO handle error */ -ulogd_log (ULOGD_NOTICE ,"Unknown protocol family\n" ); -return ULOGD_IRET_ERR ; +case AF_INET6 : + addr = (struct in6_addr *)ikey_get_u128 (&inp[index]); +break ; +case AF_INET : +/* Convert IPv4 to IPv4 in IPv6 */ + addr = &ip4_addr; +uint32_to_ipv6 (ikey_get_u32 (&inp[index]), addr); +break ; +default : +/* TODO handle error */ +ulogd_log (ULOGD_NOTICE ,"Unknown protocol family\n" ); +return ULOGD_IRET_ERR ; } - buffer = ipbin_array[oindex]; -/* format IPv6 to BINARY(16) as "0x..." */ - buffer[0 ] ='0' ; - buffer[1 ] ='x' ; - buffer +=2 ; - addr8 = &addr->s6_addr[0 ]; -for (i =0 ; i <4 ; i++) { - written =sprintf (buffer,"%02x%02x%02x%02x" , - addr8[0 ], addr8[1 ], addr8[2 ], addr8[3 ]); -if (written !=2 *4 ) { - buffer[0 ] =0 ; -return ULOGD_IRET_ERR ; - } - buffer += written; - addr8 +=4 ; - } - buffer[0 ] =0 ; +memcpy (ipbin_array[oindex], &addr->s6_addr,sizeof (addr->s6_addr));return ULOGD_IRET_OK ; }
util/db.c +9 -1
@@ -594,7 +594,15 @@ _bind_sql_stmt(struct ulogd_pluginstance *upi, struct db_stmt *stmt) sqlp +=sprintf (sqlp,"'," );break ;case ULOGD_RET_RAWSTR : - sqlp +=sprintf (sqlp,"%s," , (char *) res->u.value.ptr); +strcpy (sqlp,"0x" ); + sqlp +=2 ; + { +unsigned char *cp = res->u.value.ptr; +unsigned int j; +for (j =0 ; j < res->len; ++j) + sqlp +=sprintf (sqlp,"%02x" , cp[j]); + } + *sqlp++ =',' ;break ;case ULOGD_RET_RAW : ulogd_log(ULOGD_NOTICE,
Signature · 1 line
2.35.1
PATCH 28/34 db: add prep & exec support
In reply to Jeremy SowdenCurrently, the common DB API pre-allocates the prefix of the SQL statement, e.g.:
insert into t (col1, col2, col3, col4) values (
and concatenates the literal values for each packet before executing the statement. However, this requires escaping these values, which is not supported by all DB API's. Furthermore, most data-bases offer the ability to prepare statements with place-holders and bind values before executing them since this avoids the need to analyse and optimize the statement on every execution, and can, therefore, be more efficient.
Add the infrastructure to allow output plug-ins to prep & exec if the data-base supports it.
--- include/ulogd/db.h | 33 ++++++ util/db.c | 286 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 287 insertions(+), 32 deletions(-)
include/ulogd/db.h +33 -0
@@ -9,6 +9,7 @@#ifndef _ULOGD_DB_H#define _ULOGD_DB_H +#include <stdbool.h> #include <ulogd/ulogd.h> struct db_stmt ;
@@ -18,12 +19,15 @@ struct db_driver {int (*get_columns)(struct ulogd_pluginstance *upi);const char *(*get_column)(void *,unsigned int );char *(*format_key)(char *); +unsigned int (*format_param)(char *,unsigned int ,unsigned int );int (*open_db)(struct ulogd_pluginstance *upi);int (*close_db)(struct ulogd_pluginstance *upi);int (*escape_string)(struct ulogd_pluginstance *upi,char *dst,const char *src,unsigned int len); +int (*prepare)(struct ulogd_pluginstance *upi, +const struct db_stmt *stmt);int (*execute)(struct ulogd_pluginstance *upi,const struct db_stmt *stmt);
@@ -63,9 +67,34 @@ struct db_stmt_backlog { }; +union db_stmt_arg_value { +uint8_t b; +uint8_t ui8; +uint16_t ui16; +uint32_t ui32; +uint64_t ui64; +int8_t i8; +int16_t i16; +int32_t i32; +int64_t i64; +void *ptr; +}; + +struct db_stmt_arg { +uint32_t len; +uint16_t type; +bool null; +union db_stmt_arg_value value; +}; +struct db_stmt { + +struct db_stmt_arg *args; +unsigned int nr_params; +unsigned int len, size;char sql[]; + };struct db_stmt_item {
@@ -74,15 +103,19 @@ struct db_stmt_item { };struct db_instance { +struct db_stmt *stmt;/* buffer for our insert statement */ char *schema;time_t reconnect;int (*interp)(struct ulogd_pluginstance *upi);struct db_driver *driver; +/* DB ring buffer */ struct db_stmt_ring ring; +/* Backlog system */ struct db_stmt_backlog backlog; + };#define RECONNECT_DEFAULT 2
util/db.c +254 -32
431 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 29/34 output: mysql: add prep & exec support
In reply to Jeremy SowdenNow that the common DB API offers prep & exec support, update the MySQL plug-in to use it.
--- output/mysql/ulogd_output_MYSQL.c | 163 ++++++++++++++++++++++++------ 1 file changed, 131 insertions(+), 32 deletions(-)
output/mysql/ulogd_output_MYSQL.c +131 -32
@@ -37,11 +37,12 @@* Port to ulogd2 (@ 0sec conference, Bern, Suisse) */ +#include <assert.h> +#ifdef DEBUG_MYSQL +#include <stdio.h> +#endif #include <stdlib.h> #include <string.h> -#include <errno.h> -#include <time.h> -#include <arpa/inet.h> #include <mysql/mysql.h> #include <ulogd/ulogd.h> #include <ulogd/conffile.h>
@@ -56,6 +57,12 @@struct mysql_instance {struct db_instance db_inst;MYSQL *dbh;/* the database handle we are using */ +MYSQL_STMT *sth; +}; + +struct mysql_buffer { +unsigned long length; +my_bool is_null; };/* our configuration directives */
@@ -193,46 +200,138 @@ static int open_db_mysql(struct ulogd_pluginstance *upi)return 0 ; } -static int escape_string_mysql (struct ulogd_pluginstance *upi, -char *dst,const char *src,unsigned int len) +static int prepare_mysql (struct ulogd_pluginstance *upi, +const struct db_stmt *stmt) {struct mysql_instance *mi = (struct mysql_instance *) upi->private; -#ifdef OLD_MYSQL -return mysql_escape_string (dst, src, len); -#else -return mysql_real_escape_string (mi->dbh, dst, src, len); -#endif /* OLD_MYSQL */ +if (mi->sth) +mysql_stmt_close (mi->sth); + +if (!(mi->sth =mysql_stmt_init (mi->dbh))) { +ulogd_log (ULOGD_ERROR , +"Failed to initialize statement: %s\n" , +mysql_error (mi->dbh)); +return -1 ; + } + +if (mysql_stmt_prepare (mi->sth, stmt->sql, stmt->len) !=0 ) { +ulogd_log (ULOGD_ERROR , +"Failed to prepare statement: %s\n" , +mysql_stmt_error (mi->sth)); +return -1 ; + } + +return 0 ; }static int execute_mysql (struct ulogd_pluginstance *upi,const struct db_stmt *stmt) {struct mysql_instance *mi = (struct mysql_instance *) upi->private; -int ret; -MYSQL_RES * result; +struct mysql_buffer *buffers; +MYSQL_BIND *bindings; +unsigned int i; +int rv =-1 ; - ret =mysql_real_query (mi->dbh, stmt->sql, stmt->len); -if (ret) { -ulogd_log (ULOGD_ERROR ,"execute failed (%s)\n" , -mysql_error (mi->dbh)); -return -1 ; +if (!(bindings =calloc (stmt->nr_params,sizeof (*bindings)))) + goto err_return; + +if (!(buffers =calloc (stmt->nr_params,sizeof (*buffers)))) + goto err_free_bindings; + +for (i =0 ; i < stmt->nr_params; i++) { +struct db_stmt_arg *arg = &stmt->args[i]; +union db_stmt_arg_value *val = &arg->value; + +if (arg->null) { + bindings[i++].buffer_type =MYSQL_TYPE_NULL ; +continue ; + } + +switch (arg->type) { +case ULOGD_RET_BOOL : +case ULOGD_RET_INT8 : +case ULOGD_RET_UINT8 : + bindings[i].buffer_type =MYSQL_TYPE_TINY ; + bindings[i].buffer = val; + bindings[i].is_unsigned = arg->type !=ULOGD_RET_INT8 ; +break ; +case ULOGD_RET_INT16 : +case ULOGD_RET_UINT16 : + bindings[i].buffer_type =MYSQL_TYPE_SHORT ; + bindings[i].buffer = val; + bindings[i].is_unsigned = arg->type !=ULOGD_RET_INT16 ; +break ; +case ULOGD_RET_INT32 : +case ULOGD_RET_UINT32 : +case ULOGD_RET_IPADDR : + bindings[i].buffer_type =MYSQL_TYPE_LONG ; + bindings[i].buffer = val; + bindings[i].is_unsigned = arg->type !=ULOGD_RET_INT32 ; +break ; +case ULOGD_RET_INT64 : +case ULOGD_RET_UINT64 : + bindings[i].buffer_type =MYSQL_TYPE_LONGLONG ; + bindings[i].buffer = val; + bindings[i].is_unsigned = arg->type !=ULOGD_RET_INT64 ; +break ; +case ULOGD_RET_STRING : + bindings[i].buffer_type =MYSQL_TYPE_STRING ; + bindings[i].is_null = &buffers[i].is_null; + bindings[i].length = &buffers[i].length; +if (val->ptr) { + bindings[i].buffer = val->ptr; + bindings[i].buffer_length = arg->len +1 ; + buffers[i].length = arg->len; + }else + buffers[i].is_null =1 ; +break ; +case ULOGD_RET_RAWSTR : + bindings[i].buffer_type =MYSQL_TYPE_BLOB ; + bindings[i].is_null = &buffers[i].is_null; + bindings[i].length = &buffers[i].length; +if (val->ptr) { + bindings[i].buffer = val->ptr; + bindings[i].buffer_length = arg->len; + buffers[i].length = arg->len; + }else + buffers[i].is_null =1 ; +break ; + } } - result =mysql_use_result (mi->dbh); -if (result) { -mysql_free_result (result); + +if (mysql_stmt_bind_param (mi->sth, bindings) !=0 ) { +ulogd_log (ULOGD_ERROR , +"Failed to bind statement parameters: %s\n" , +mysql_stmt_error (mi->sth)); + goto err_free_buffers; } -return 0 ; +if (mysql_stmt_execute (mi->sth) !=0 ) { +ulogd_log (ULOGD_ERROR , +"Failed to execute statement: %s\n" , +mysql_stmt_error (mi->sth)); + goto err_free_buffers; + } + + rv =0 ; + +err_free_buffers: +free (buffers); +err_free_bindings: +free (bindings); +err_return: +return rv; }static struct db_driver db_driver_mysql = { - .get_columns = &get_columns_mysql, - .get_column = &get_column_mysql, - .open_db = &open_db_mysql, - .close_db = &close_db_mysql, - .escape_string = &escape_string_mysql, - .execute = &execute_mysql, + .get_columns = get_columns_mysql, + .get_column = get_column_mysql, + .open_db = open_db_mysql, + .close_db = close_db_mysql, + .prepare = prepare_mysql, + .execute = execute_mysql, }; static int configure_mysql(struct ulogd_pluginstance *upi,
@@ -254,11 +353,11 @@ static struct ulogd_plugin plugin_mysql = { }, .config_kset = &kset_mysql, .priv_size = sizeof(struct mysql_instance), - .configure = &configure_mysql, - .start = &ulogd_db_start, - .stop = &ulogd_db_stop, - .signal = &ulogd_db_signal, - .interp = &ulogd_db_interp, + .configure = configure_mysql, + .start = ulogd_db_start, + .stop = ulogd_db_stop, + .signal = ulogd_db_signal, + .interp = ulogd_db_interp, .version = VERSION, };
Signature · 1 line
2.35.1
PATCH 30/34 output: pgsql: remove a couple of struct members
In reply to Jeremy SowdenThe pgsql_have_schemas of struct pgsql_instance is never used. The pgres member is always assigned and cleared in the same function.
--- output/pgsql/ulogd_output_PGSQL.c | 46 ++++++++++++++++--------------- 1 file changed, 24 insertions(+), 22 deletions(-)
output/pgsql/ulogd_output_PGSQL.c +24 -22
@@ -31,8 +31,6 @@ struct pgsql_instance {struct db_instance db_inst;PGconn *dbh; -PGresult *pgres; -unsigned char pgsql_have_schemas; };/* our configuration directives */
@@ -90,6 +88,7 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi)struct pgsql_instance *pi = (struct pgsql_instance *) upi->private;char pgbuf[strlen (PGSQL_HAVE_NAMESPACE_TEMPLATE ) +strlen (schema_ce (upi->config_kset).u.string) +1 ]; +PGresult *pgres;if (!pi->dbh)return -1 ;
@@ -98,28 +97,28 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi) schema_ce(upi->config_kset).u.string);ulogd_log (ULOGD_DEBUG ,"%s\n" , pgbuf); - pi->pgres =PQexec (pi->dbh, pgbuf); -if (!pi->pgres) { + pgres =PQexec (pi->dbh, pgbuf); +if (!pgres) {ulogd_log (ULOGD_DEBUG ,"\n result false" );return -1 ; } -if (PQresultStatus (pi->pgres) ==PGRES_TUPLES_OK ) { -if (PQntuples (pi->pgres)) { +if (PQresultStatus (pgres) ==PGRES_TUPLES_OK ) { +if (PQntuples (pgres)) {ulogd_log (ULOGD_DEBUG ,"using schema %s\n" ,schema_ce (upi->config_kset).u.string); pi->db_inst.schema =schema_ce (upi->config_kset).u.string; }else {ulogd_log (ULOGD_ERROR ,"schema %s not found: %s\n" ,schema_ce (upi->config_kset).u.string,PQerrorMessage (pi->dbh)); -PQclear (pi->pgres); +PQclear (pgres);return -1 ; } }else { pi->db_inst.schema =NULL ; } -PQclear (pi->pgres); +PQclear (pgres);return 0 ; }
@@ -137,6 +136,7 @@ static int get_columns_pgsql(struct ulogd_pluginstance *upi)char pgbuf[strlen (PGSQL_GETCOLUMN_TEMPLATE_SCHEMA ) +strlen (table_ce (upi->config_kset).u.string) +strlen (pi->db_inst.schema) +2 ]; +PGresult *pgres;int rv;if (!pi->dbh) {
@@ -156,27 +156,27 @@ static int get_columns_pgsql(struct ulogd_pluginstance *upi)ulogd_log (ULOGD_DEBUG ,"%s\n" , pgbuf); - pi->pgres =PQexec (pi->dbh, pgbuf); -if (!pi->pgres) { + pgres =PQexec (pi->dbh, pgbuf); +if (!pgres) {ulogd_log (ULOGD_DEBUG ,"result false (%s)" ,PQerrorMessage (pi->dbh));return -1 ; } -if (PQresultStatus (pi->pgres) !=PGRES_TUPLES_OK ) { +if (PQresultStatus (pgres) !=PGRES_TUPLES_OK ) {ulogd_log (ULOGD_DEBUG ,"pres_command_not_ok (%s)" ,PQerrorMessage (pi->dbh)); -PQclear (pi->pgres); +PQclear (pgres);return -1 ; } - rv =ulogd_db_alloc_input_keys (upi,PQntuples (pi->pgres), pi->pgres); + rv =ulogd_db_alloc_input_keys (upi,PQntuples (pgres), pgres);/* ID (starting by '.') is a sequence */ if (!rv && upi->input.keys[0 ].name[0 ] =='.' ) upi->input.keys[0 ].flags |=ULOGD_KEYF_INACTIVE ; -PQclear (pi->pgres); +PQclear (pgres);return rv; }
@@ -207,6 +207,7 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi)char *connstr =connstr_ce (upi->config_kset).u.string;char *schema =NULL ;char pgbuf[128 ]; +PGresult *pgres;if (!connstr[0 ]) {char *server =host_ce (upi->config_kset).u.string;
@@ -260,13 +261,13 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi)if (!(schema ==NULL ) && (strcmp (schema,"public" ))) {snprintf (pgbuf,128 ,"SET search_path='%.63s', \"$user\", 'public'" , schema); - pi->pgres =PQexec (pi->dbh, pgbuf); -if ((PQresultStatus (pi->pgres) ==PGRES_COMMAND_OK )) { -PQclear (pi->pgres); + pgres =PQexec (pi->dbh, pgbuf); +if ((PQresultStatus (pgres) ==PGRES_COMMAND_OK )) { +PQclear (pgres); }else {ulogd_log (ULOGD_ERROR ,"could not set search path to (%s): %s\n" , schema,PQerrorMessage (pi->dbh)); -PQclear (pi->pgres); +PQclear (pgres);close_db_pgsql (upi);return -1 ; }
@@ -286,16 +287,17 @@ static int execute_pgsql(struct ulogd_pluginstance *upi, const struct db_stmt *stmt) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private; +PGresult *pgres; - pi->pgres =PQexec (pi->dbh, stmt->sql); -if (!(pi->pgres && ((PQresultStatus (pi->pgres) ==PGRES_COMMAND_OK ) - || (PQresultStatus (pi->pgres) ==PGRES_TUPLES_OK )))) { + pgres =PQexec (pi->dbh, stmt->sql); +if (!(pgres && ((PQresultStatus (pgres) ==PGRES_COMMAND_OK ) + || (PQresultStatus (pgres) ==PGRES_TUPLES_OK )))) {ulogd_log (ULOGD_ERROR ,"execute failed (%s)\n" ,PQerrorMessage (pi->dbh));return -1 ; } -PQclear (pi->pgres); +PQclear (pgres);return 0 ; }
Signature · 1 line
2.35.1
PATCH 31/34 output: pgsql: remove variable-length arrays
In reply to Jeremy SowdenVLA's are deprecated.
Group all the SQL macros at the top of the file and format them.
--- output/pgsql/ulogd_output_PGSQL.c | 95 ++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 33 deletions(-)
output/pgsql/ulogd_output_PGSQL.c +62 -33
@@ -79,20 +79,46 @@ static struct config_keyset pgsql_kset = {#define schema_ce (x) ((x)->ces[DB_CE_NUM + 5])#define connstr_ce (x) ((x)->ces[DB_CE_NUM + 6]) -#define PGSQL_HAVE_NAMESPACE_TEMPLATE \ - "SELECT nspname FROM pg_namespace n WHERE n.nspname='%s'" +#define PGSQL_HAVE_NAMESPACE_TEMPLATE \ + "SELECT nspname " \ + "FROM pg_namespace n " \ + "WHERE n.nspname = '%s'" + +#define PGSQL_GETCOLUMN_TEMPLATE \ + "SELECT a.attname " \ + "FROM pg_class c, pg_attribute a " \ + "WHERE c.relname = '%s' " \ + "AND a.attnum > 0 " \ + "AND a.attrelid = c.oid " \ + "ORDER BY a.attnum" + +#define PGSQL_GETCOLUMN_TEMPLATE_SCHEMA \ + "SELECT a.attname " \ + "FROM pg_attribute a, pg_class c " \ + "LEFT JOIN pg_namespace n ON c.relnamespace = n.oid " \ + "WHERE c.relname = '%s' " \ + "AND n.nspname = '%s' " \ + "AND a.attnum > 0 " \ + "AND a.attrelid = c.oid " \ + "AND a.attisdropped = FALSE " \ + "ORDER BY a.attnum"/* Determine if server support schemas */ static int pgsql_namespace (struct ulogd_pluginstance *upi) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private; -char pgbuf[strlen (PGSQL_HAVE_NAMESPACE_TEMPLATE ) + -strlen (schema_ce (upi->config_kset).u.string) +1 ];PGresult *pgres; +char *pgbuf; +int rv =-1 ;if (!pi->dbh)return -1 ; + pgbuf =malloc (sizeof (PGSQL_HAVE_NAMESPACE_TEMPLATE ) + +strlen (schema_ce (upi->config_kset).u.string)); +if (!pgbuf) +return -1 ; +sprintf (pgbuf,PGSQL_HAVE_NAMESPACE_TEMPLATE ,schema_ce (upi->config_kset).u.string);ulogd_log (ULOGD_DEBUG ,"%s\n" , pgbuf);
@@ -100,7 +126,7 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi) pgres =PQexec (pi->dbh, pgbuf);if (!pgres) {ulogd_log (ULOGD_DEBUG ,"\n result false" ); -return -1 ; + goto err_free_buf; }if (PQresultStatus (pgres) ==PGRES_TUPLES_OK ) {
@@ -108,51 +134,50 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi)ulogd_log (ULOGD_DEBUG ,"using schema %s\n" ,schema_ce (upi->config_kset).u.string); pi->db_inst.schema =schema_ce (upi->config_kset).u.string; - }else { + rv =0 ; + }else ulogd_log (ULOGD_ERROR ,"schema %s not found: %s\n" , -schema_ce (upi->config_kset).u.string,PQerrorMessage (pi->dbh)); -PQclear (pgres); -return -1 ; - } +schema_ce (upi->config_kset).u.string, +PQerrorMessage (pi->dbh)); }else { pi->db_inst.schema =NULL ; + rv =0 ; }PQclear (pgres); -return 0 ; -} - -#define PGSQL_GETCOLUMN_TEMPLATE \ - "SELECT a.attname FROM pg_class c, pg_attribute a WHERE c.relname ='%s' AND a.attnum>0 AND a.attrelid=c.oid ORDER BY a.attnum" +err_free_buf: +free (pgbuf); -#define PGSQL_GETCOLUMN_TEMPLATE_SCHEMA \ - "SELECT a.attname FROM pg_attribute a, pg_class c LEFT JOIN pg_namespace n ON c.relnamespace=n.oid WHERE c.relname ='%s' AND n.nspname='%s' AND a.attnum>0 AND a.attrelid=c.oid AND a.attisdropped=FALSE ORDER BY a.attnum" +return rv; +}/* find out which columns the table has */ static int get_columns_pgsql (struct ulogd_pluginstance *upi) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private; -char pgbuf[strlen (PGSQL_GETCOLUMN_TEMPLATE_SCHEMA ) - +strlen (table_ce (upi->config_kset).u.string) - +strlen (pi->db_inst.schema) +2 ];PGresult *pgres; -int rv; +char *pgbuf; +int rv =-1 ;if (!pi->dbh) {ulogd_log (ULOGD_ERROR ,"no database handle\n" );return -1 ; } -if (pi->db_inst.schema) { -snprintf (pgbuf,sizeof (pgbuf)-1 , -PGSQL_GETCOLUMN_TEMPLATE_SCHEMA , -table_ce (upi->config_kset).u.string, - pi->db_inst.schema); - }else { -snprintf (pgbuf,sizeof (pgbuf)-1 ,PGSQL_GETCOLUMN_TEMPLATE , -table_ce (upi->config_kset).u.string); - } + pgbuf =malloc (sizeof (PGSQL_GETCOLUMN_TEMPLATE_SCHEMA ) + +strlen (table_ce (upi->config_kset).u.string) + +strlen (pi->db_inst.schema)); +if (!pgbuf) +return -1 ; + +if (pi->db_inst.schema) +sprintf (pgbuf,PGSQL_GETCOLUMN_TEMPLATE_SCHEMA , +table_ce (upi->config_kset).u.string, + pi->db_inst.schema); +else +sprintf (pgbuf,PGSQL_GETCOLUMN_TEMPLATE , +table_ce (upi->config_kset).u.string);ulogd_log (ULOGD_DEBUG ,"%s\n" , pgbuf);
@@ -160,14 +185,13 @@ static int get_columns_pgsql(struct ulogd_pluginstance *upi)if (!pgres) {ulogd_log (ULOGD_DEBUG ,"result false (%s)" ,PQerrorMessage (pi->dbh)); -return -1 ; + goto err_free_buf; }if (PQresultStatus (pgres) !=PGRES_TUPLES_OK ) {ulogd_log (ULOGD_DEBUG ,"pres_command_not_ok (%s)" ,PQerrorMessage (pi->dbh)); -PQclear (pgres); -return -1 ; + goto err_clear_res; } rv =ulogd_db_alloc_input_keys (upi,PQntuples (pgres), pgres);
@@ -176,7 +200,12 @@ static int get_columns_pgsql(struct ulogd_pluginstance *upi)if (!rv && upi->input.keys[0 ].name[0 ] =='.' ) upi->input.keys[0 ].flags |=ULOGD_KEYF_INACTIVE ; +err_clear_res:PQclear (pgres); + +err_free_buf: +free (pgbuf); +return rv; }
Signature · 1 line
2.35.1
PATCH 32/34 output: pgsql: tidy up `open_db_pgsql` and fix memory leak
In reply to Jeremy Sowden * Remove some excess parentheses and a superfluous local variable.
* Tighten up the scope of a couple of other variables.
* Streamline the error-handling.
* Free the connexion string.
--- output/pgsql/ulogd_output_PGSQL.c | 57 +++++++++++++++++-------------- 1 file changed, 32 insertions(+), 25 deletions(-)
output/pgsql/ulogd_output_PGSQL.c +32 -25
@@ -232,11 +232,9 @@ static int close_db_pgsql(struct ulogd_pluginstance *upi)static int open_db_pgsql (struct ulogd_pluginstance *upi) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private; -int len;char *connstr =connstr_ce (upi->config_kset).u.string; -char *schema =NULL ; -char pgbuf[128 ]; -PGresult *pgres; +PGresult *pgres =NULL ; +int rv =-1 ;if (!connstr[0 ]) {char *server =host_ce (upi->config_kset).u.string;
@@ -245,10 +243,11 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi)char *pass =pass_ce (upi->config_kset).u.string;char *db =db_ce (upi->config_kset).u.string;char *cp; +/* 80 is more than what we need for the fixed parts below */ - len =80 +strlen (user) +strlen (db); +size_t len =80 +strlen (user) +strlen (db); -/* hostname and and password are the only optionals */ +/* hostname and and password are the only optionals */ if (server[0 ]) len +=strlen (server);if (pass[0 ])
@@ -271,39 +270,47 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi)if (pass[0 ]) cp +=sprintf (cp," password=%s" , pass); } + pi->dbh =PQconnectdb (connstr);if (PQstatus (pi->dbh) !=CONNECTION_OK ) {ulogd_log (ULOGD_ERROR ,"unable to connect to db (%s): %s\n" , connstr,PQerrorMessage (pi->dbh)); -close_db_pgsql (upi); -return -1 ; + goto err_close_db; } -if (pgsql_namespace (upi)) { +if (pgsql_namespace (upi) <0 ) {ulogd_log (ULOGD_ERROR ,"problem testing for pgsql schemas\n" ); -close_db_pgsql (upi); -return -1 ; + goto err_close_db; } - pi=(struct pgsql_instance *)upi->private; - schema = pi->db_inst.schema; +if (pi->db_inst.schema !=NULL &&strcmp (pi->db_inst.schema,"public" ) !=0 ) { +char pgbuf[128 ]; -if (!(schema ==NULL ) && (strcmp (schema,"public" ))) { -snprintf (pgbuf,128 ,"SET search_path='%.63s', \"$user\", 'public'" , schema); +snprintf (pgbuf,sizeof (pgbuf), +"SET search_path='%.63s', \"$user\", 'public'" , + pi->db_inst.schema); pgres =PQexec (pi->dbh, pgbuf); -if ((PQresultStatus (pgres) ==PGRES_COMMAND_OK )) { -PQclear (pgres); - }else { -ulogd_log (ULOGD_ERROR ,"could not set search path to (%s): %s\n" , - schema,PQerrorMessage (pi->dbh)); -PQclear (pgres); -close_db_pgsql (upi); -return -1 ; +if (PQresultStatus (pgres) !=PGRES_COMMAND_OK ) { +ulogd_log (ULOGD_ERROR , +"could not set search path to (%s): %s\n" , + pi->db_inst.schema,PQerrorMessage (pi->dbh)); + goto err_free_result; } - } -return 0 ; + rv =0 ; + +err_free_result: +PQclear (pgres); + +err_close_db: +if (rv ==-1 ) +close_db_pgsql (upi); + +if (connstr !=connstr_ce (upi->config_kset).u.string) +free (connstr); + +return rv; } static int escape_string_pgsql(struct ulogd_pluginstance *upi,
Signature · 1 line
2.35.1
PATCH 33/34 output: pgsql: add prep & exec support
In reply to Jeremy SowdenNow that the common DB API offers prep-exec support, update the Postgres plug-in to use it.
--- output/pgsql/ulogd_output_PGSQL.c | 145 +++++++++++++++++++++++++----- 1 file changed, 123 insertions(+), 22 deletions(-)
output/pgsql/ulogd_output_PGSQL.c +123 -22
@@ -10,6 +10,7 @@* */ +#include <limits.h> #include <stdlib.h> #include <string.h> #include <errno.h>
@@ -27,6 +28,8 @@ #define DEBUGP(x, args...)#endif +#define BUF_SIZE (sizeof(uint64_t) * CHAR_BIT / 3 + 1) +struct pgsql_instance {struct db_instance db_inst;
@@ -217,6 +220,12 @@ get_column_pgsql(void *vp, unsigned int i)return PQgetvalue (pgres, i,0 ); } +static unsigned int +format_param_pgsql (char *buf,unsigned int size,unsigned int idx) +{ +return snprintf (buf, size,"$%u," , idx +1 ); +} +static int close_db_pgsql (struct ulogd_pluginstance *upi) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private;
@@ -313,38 +322,130 @@ err_close_db:return rv; } -static int escape_string_pgsql (struct ulogd_pluginstance *upi, -char *dst,const char *src,unsigned int len) +static int +prepare_pgsql (struct ulogd_pluginstance *upi,const struct db_stmt *stmt) { -return PQescapeString (dst, src,strlen (src)); +struct pgsql_instance *pi = (struct pgsql_instance *) upi->private; +PGresult *pgres; +int rv; + + pgres =PQprepare (pi->dbh,procedure_ce (upi->config_kset).u.string, + stmt->sql,0 ,NULL ); +if (PQresultStatus (pgres) !=PGRES_COMMAND_OK ) { +ulogd_log (ULOGD_ERROR ,"Failed to prepare statement: %s\n" , +PQerrorMessage (pi->dbh)); + rv =-1 ; + }else + rv =0 ; + +PQclear (pgres); +return rv; } -static int execute_pgsql (struct ulogd_pluginstance *upi, -const struct db_stmt *stmt) +static int +execute_pgsql (struct ulogd_pluginstance *upi,const struct db_stmt *stmt) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private; +char **bindings, *buffers;PGresult *pgres; +unsigned int i; +int rv =-1 ; - pgres =PQexec (pi->dbh, stmt->sql); -if (!(pgres && ((PQresultStatus (pgres) ==PGRES_COMMAND_OK ) - || (PQresultStatus (pgres) ==PGRES_TUPLES_OK )))) { -ulogd_log (ULOGD_ERROR ,"execute failed (%s)\n" , -PQerrorMessage (pi->dbh)); +if (!(bindings =malloc (stmt->nr_params *sizeof (*bindings)))) { +ulogd_log (ULOGD_ERROR ,"Memory allocation failure\n" );return -1 ; } +if (!(buffers =malloc (stmt->nr_params *BUF_SIZE ))) { +ulogd_log (ULOGD_ERROR ,"Memory allocation failure\n" ); + goto err_free_bindings; + } + +for (i =0 ; i < stmt->nr_params; i++) { +struct db_stmt_arg *arg = &stmt->args[i]; +union db_stmt_arg_value *val = &arg->value; + +if (arg->null) { + bindings[i] =NULL ; +continue ; + } + +switch (arg->type) { +case ULOGD_RET_STRING : +case ULOGD_RET_RAWSTR : + bindings[i] = val->ptr; +continue ; + } + + bindings[i] = &buffers[i *BUF_SIZE ]; + +switch (arg->type) { +case ULOGD_RET_BOOL : +snprintf (bindings[i],BUF_SIZE ,"%d" , val->b); +break ; +case ULOGD_RET_INT8 : +snprintf (bindings[i],BUF_SIZE ,"%" PRId8, val->i8); +break ; +case ULOGD_RET_UINT8 : +snprintf (bindings[i],BUF_SIZE ,"%" PRIu8, val->ui8); +break ; +case ULOGD_RET_INT16 : +snprintf (bindings[i],BUF_SIZE ,"%" PRId16, val->i16); +break ; +case ULOGD_RET_UINT16 : +snprintf (bindings[i],BUF_SIZE ,"%" PRIu16, val->ui16); +break ; +case ULOGD_RET_INT32 : +snprintf (bindings[i],BUF_SIZE ,"%" PRId32, val->i32); +break ; +case ULOGD_RET_IPADDR : +case ULOGD_RET_UINT32 : +snprintf (bindings[i],BUF_SIZE ,"%" PRIu32, val->ui32); +break ; +case ULOGD_RET_INT64 : +snprintf (bindings[i],BUF_SIZE ,"%" PRId64, val->i64); +break ; +case ULOGD_RET_UINT64 : +snprintf (bindings[i],BUF_SIZE ,"%" PRIu64, val->ui64); +break ; + } + } + + pgres =PQexecPrepared (pi->dbh,procedure_ce (upi->config_kset).u.string, + stmt->nr_params, (const char **) bindings,NULL , +NULL ,0 ); + +switch (PQresultStatus (pgres)) { +case PGRES_COMMAND_OK : +case PGRES_TUPLES_OK : + rv =0 ; +break ; +default : +ulogd_log (ULOGD_ERROR , +"Failed to execute statement: status = %s, error = %s\n" , +PQresStatus (PQresultStatus (pgres)), +PQerrorMessage (pi->dbh)); +break ; + } +PQclear (pgres); -return 0 ; +free (buffers); + +err_free_bindings: +free (bindings); + +return rv; }static struct db_driver db_driver_pgsql = { - .get_columns = &get_columns_pgsql, - .get_column = &get_column_pgsql, - .open_db = &open_db_pgsql, - .close_db = &close_db_pgsql, - .escape_string = &escape_string_pgsql, - .execute = &execute_pgsql, + .get_columns = get_columns_pgsql, + .get_column = get_column_pgsql, + .format_param = format_param_pgsql, + .open_db = open_db_pgsql, + .close_db = close_db_pgsql, + .prepare = prepare_pgsql, + .execute = execute_pgsql, }; static int configure_pgsql(struct ulogd_pluginstance *upi,
@@ -367,11 +468,11 @@ static struct ulogd_plugin pgsql_plugin = { }, .config_kset = &pgsql_kset, .priv_size = sizeof(struct pgsql_instance), - .configure = &configure_pgsql, - .start = &ulogd_db_start, - .stop = &ulogd_db_stop, - .signal = &ulogd_db_signal, - .interp = &ulogd_db_interp, + .configure = configure_pgsql, + .start = ulogd_db_start, + .stop = ulogd_db_stop, + .signal = ulogd_db_signal, + .interp = ulogd_db_interp, .version = VERSION, };
Signature · 1 line
2.35.1
PATCH 34/34 output: sqlite3: reimplement using the common DB API
In reply to Jeremy SowdenSQLite doesn't have support for escaping strings to embed them in SQL statements. Therefore, the sqlite3 plug-in has not used the util_db API. Now that the common API supports prep & exec, convert the sqlite3 plug-in to use it.
--- output/sqlite3/ulogd_output_SQLITE3.c | 459 +++++++++----------------- 1 file changed, 150 insertions(+), 309 deletions(-)
output/sqlite3/ulogd_output_SQLITE3.c +150 -309
@@ -30,392 +30,232 @@* - port to ulogd-2.00 */ +#ifdef DEBUG_SQLITE3 +#include <stdio.h> +#endif #include <stdlib.h> #include <string.h> -#include <arpa/inet.h> +#include <sqlite3.h> #include <ulogd/ulogd.h> #include <ulogd/conffile.h> -#include <sqlite3.h> -#include <sys/queue.h> +#include <ulogd/db.h> -#define CFG_BUFFER_DEFAULT 10 - -#if 0 +#ifdef DEBUG_SQLITE3 #define DEBUGP(x, args...) fprintf(stderr, x, ## args)#else #define DEBUGP(x, args...)#endif -struct field { -TAILQ_ENTRY (field) link; -char name[ULOGD_MAX_KEYLEN +1 ]; -struct ulogd_key *key; -}; - -TAILQ_HEAD (field_lh, field); - -#define tailq_for_each (pos, head, link) \ - for (pos = (head).tqh_first; pos != NULL; pos = pos->link.tqe_next) - +#define SQLITE3_BUSY_TIMEOUT 300 -struct sqlite3_priv { +struct sqlite3_instance { +struct db_instance db_inst;sqlite3 *dbh;/* database handle we are using */ -struct field_lh fields; -char *stmt;sqlite3_stmt *p_stmt; -struct { -unsigned err_tbl_busy;/* "Table busy" */ - } stats; }; -static struct config_keyset sqlite3_kset = { - .num_ces =3 , +static struct config_keyset kset_sqlite3 = { + .num_ces =DB_CE_NUM +1 , .ces = { +DB_CES , { .key ="db" , .type =CONFIG_TYPE_STRING , .options =CONFIG_OPT_MANDATORY , }, - { - .key ="table" , - .type =CONFIG_TYPE_STRING , - .options =CONFIG_OPT_MANDATORY , - }, }, }; -#define db_ce (pi) (pi)->config_kset->ces[0].u.string -#define table_ce (pi) (pi)->config_kset->ces[1].u.string - -/* forward declarations */ -static int sqlite3_createstmt (struct ulogd_pluginstance *); +#define db_ce (x) ((x)->ces[DB_CE_NUM + 0]) +#define SELECT_ALL_FROM "select * from "static int -add_row (struct ulogd_pluginstance *pi) +get_columns_sqlite3 (struct ulogd_pluginstance *upi) { -struct sqlite3_priv *priv = (void *)pi->private; -int ret; - - ret =sqlite3_step (priv->p_stmt); -if (ret ==SQLITE_BUSY ) - priv->stats.err_tbl_busy++; -else if (ret ==SQLITE_ERROR ) { - ret =sqlite3_finalize (priv->p_stmt); - priv->p_stmt =NULL ; - -if (ret !=SQLITE_SCHEMA ) { -ulogd_log (ULOGD_ERROR ,"SQLITE3: step: %s\n" , -sqlite3_errmsg (priv->dbh)); - goto err_reset; - } -if (sqlite3_createstmt (pi) <0 ) { -ulogd_log (ULOGD_ERROR , -"SQLITE3: Could not create statement.\n" ); - goto err_reset; - } - } +struct sqlite3_instance *si = (struct sqlite3_instance *) upi->private; +char query[sizeof (SELECT_ALL_FROM ) +CONFIG_VAL_STRING_LEN ]; +sqlite3_stmt *stmt; +int rv; - ret =sqlite3_reset (priv->p_stmt); +snprintf (query,sizeof (query),SELECT_ALL_FROM "%s" , +table_ce (upi->config_kset).u.string); -return 0 ; +if (sqlite3_prepare_v2 (si->dbh, query,-1 , &stmt,NULL ) !=SQLITE_OK ) { +ulogd_log (ULOGD_ERROR ,"%s: prepare failed: %s\n" , + __func__,sqlite3_errmsg (si->dbh)); +return -1 ; + } - err_reset: -sqlite3_reset (priv->p_stmt); + rv =ulogd_db_alloc_input_keys (upi,sqlite3_column_count (stmt), stmt); -return -1 ; +sqlite3_finalize (stmt); +return rv; } - -/* our main output function, called by ulogd */ -static int -sqlite3_interp (struct ulogd_pluginstance *pi) +static const char * +get_column_sqlite3 (void *vp,unsigned int i) { -struct sqlite3_priv *priv = (void *)pi->private; -struct field *f; -int ret, i =1 ; - -tailq_for_each (f, priv->fields, link) { -struct ulogd_key *k_ret = f->key->u.source; - -if (f->key ==NULL || !IS_VALID (*k_ret)) { -sqlite3_bind_null (priv->p_stmt, i); - i++; -continue ; - } - -switch (f->key->type) { -case ULOGD_RET_INT8 : - ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.i8); -break ; - -case ULOGD_RET_INT16 : - ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.i16); -break ; - -case ULOGD_RET_INT32 : - ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.i32); -break ; - -case ULOGD_RET_INT64 : - ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.i64); -break ; +sqlite3_stmt *schema_stmt = vp; -case ULOGD_RET_UINT8 : - ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.ui8); -break ; - -case ULOGD_RET_UINT16 : - ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.ui16); -break ; - -case ULOGD_RET_UINT32 : - ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.ui32); -break ; - -case ULOGD_RET_IPADDR : -case ULOGD_RET_UINT64 : - ret =sqlite3_bind_int64 (priv->p_stmt, i, k_ret->u.value.ui64); -break ; - -case ULOGD_RET_BOOL : - ret =sqlite3_bind_int (priv->p_stmt, i, k_ret->u.value.b); -break ; - -case ULOGD_RET_STRING : - ret =sqlite3_bind_text (priv->p_stmt, i, k_ret->u.value.ptr, -strlen (k_ret->u.value.ptr),SQLITE_STATIC ); -break ; - -default : - ret =SQLITE_OK ; -ulogd_log (ULOGD_NOTICE ,"unknown type %d for %s\n" , - f->key->type, f->key->name); - } -if (ret !=SQLITE_OK ) - goto err_bind; - - i++; - } - -if (add_row (pi) <0 ) -return ULOGD_IRET_ERR ; - -return ULOGD_IRET_OK ; - - err_bind: -ulogd_log (ULOGD_ERROR ,"SQLITE: bind: %s\n" ,sqlite3_errmsg (priv->dbh)); - -return ULOGD_IRET_ERR ; +return sqlite3_column_name (schema_stmt, i); } -#define _SQLITE3_INSERTTEMPL "insert into X (Y) values (Z)" - -/* create the static part of our insert statement */ static int -sqlite3_createstmt (struct ulogd_pluginstance *pi) +open_db_sqlite3 (struct ulogd_pluginstance *upi) { -struct sqlite3_priv *priv = (void *)pi->private; -struct field *f; -int i, cols =0 ; -char *stmt_pos; - -if (priv->stmt !=NULL ) -free (priv->stmt); - -if ((priv->stmt =calloc (1 ,1024 )) ==NULL ) { -ulogd_log (ULOGD_ERROR ,"SQLITE3: out of memory\n" ); -return -1 ; - } - stmt_pos = priv->stmt; - - stmt_pos +=sprintf (stmt_pos,"insert into %s (" ,table_ce (pi)); - -tailq_for_each (f, priv->fields, link) { - stmt_pos +=sprintf (stmt_pos,"%s," , f->name); - cols++; - } - - *(stmt_pos -1 ) =')' ; - - stmt_pos +=sprintf (stmt_pos," values (" ); - -for (i =0 ; i < cols -1 ; i++) - stmt_pos +=sprintf (stmt_pos,"?," ); - -sprintf (stmt_pos,"?)" ); -ulogd_log (ULOGD_DEBUG ,"%s: stmt='%s'\n" , pi->id, priv->stmt); +struct sqlite3_instance *si = (struct sqlite3_instance *) upi->private; -DEBUGP ("about to prepare statement.\n" ); - -sqlite3_prepare (priv->dbh, priv->stmt,-1 , &priv->p_stmt,0 ); -if (priv->p_stmt ==NULL ) { -ulogd_log (ULOGD_ERROR ,"SQLITE3: prepare: %s\n" , -sqlite3_errmsg (priv->dbh)); +if (sqlite3_open (db_ce (upi->config_kset).u.string, + &si->dbh) !=SQLITE_OK ) { +ulogd_log (ULOGD_ERROR ,"%s: open failed: %s\n" , + __func__,sqlite3_errmsg (si->dbh));return -1 ; } -DEBUGP ("statement prepared.\n" ); +/* Set the timeout so that we don't automatically fail if the table is +* busy. +*/ +sqlite3_busy_timeout (si->dbh,SQLITE3_BUSY_TIMEOUT );return 0 ; } - -static struct ulogd_key * -ulogd_find_key (struct ulogd_pluginstance *pi,const char *name) +static int +close_db_sqlite3 (struct ulogd_pluginstance *upi) { -char buf[ULOGD_MAX_KEYLEN +1 ] ="" ; -unsigned int i; +struct sqlite3_instance *si = (struct sqlite3_instance *) upi->private; -/* replace all underscores with dots */ -for (i =0 ; i <sizeof (buf) -1 && name[i]; ++i) - buf[i] = name[i] !='_' ? name[i] :'.' ; +if (si->p_stmt) { +sqlite3_finalize (si->p_stmt); + si->p_stmt =NULL ; + } -for (i =0 ; i < pi->input.num_keys; i++) { -if (strcmp (pi->input.keys[i].name, buf) ==0 ) -return &pi->input.keys[i]; +if (si->dbh) { +sqlite3_close (si->dbh); + si->dbh =NULL ; } -return NULL ; +return 0 ; } -#define SELECT_ALL_STR "select * from " -#define SELECT_ALL_LEN sizeof(SELECT_ALL_STR) -static int -db_count_cols (struct ulogd_pluginstance *pi,sqlite3_stmt **stmt) +prepare_sqlite3 (struct ulogd_pluginstance *upi,const struct db_stmt *stmt) { -struct sqlite3_priv *priv = (void *)pi->private; -char query[SELECT_ALL_LEN +CONFIG_VAL_STRING_LEN ] =SELECT_ALL_STR ; +struct sqlite3_instance *si = (struct sqlite3_instance *) upi->private; -strncat (query,table_ce (pi),sizeof (query) -strlen (query) -1 ); +if (si->p_stmt) { +sqlite3_finalize (si->p_stmt); + si->p_stmt =NULL ; + } -if (sqlite3_prepare (priv->dbh, query,-1 , stmt,0 ) !=SQLITE_OK ) +if (sqlite3_prepare_v2 (si->dbh, stmt->sql, stmt->len +1 , + &si->p_stmt,NULL ) !=SQLITE_OK ) { +ulogd_log (ULOGD_ERROR ,"%s: prepare failed: %s\n" , + __func__,sqlite3_errmsg (si->dbh));return -1 ; + } -return sqlite3_column_count (*stmt); +return 0 ; } -/* initialize DB, possibly creating it */ static int -sqlite3_init_db (struct ulogd_pluginstance *pi) +execute_sqlite3 (struct ulogd_pluginstance *upi,const struct db_stmt *stmt) { -struct sqlite3_priv *priv = (void *)pi->private; -sqlite3_stmt *schema_stmt; -int col, num_cols; - -if (priv->dbh ==NULL ) { -ulogd_log (ULOGD_ERROR ,"SQLITE3: No database handle.\n" ); -return -1 ; - } - - num_cols =db_count_cols (pi, &schema_stmt); -if (num_cols <=0 ) { -ulogd_log (ULOGD_ERROR ,"table `%s' is empty or missing in " -"file `%s'. Did you created this " -"table in the database file? Please, " -"see ulogd2 documentation.\n" , -table_ce (pi),db_ce (pi)); -return -1 ; - } +struct sqlite3_instance *si = (struct sqlite3_instance *) upi->private; +unsigned int i; +int rv; -for (col =0 ; col < num_cols; col++) { -struct field *f; +for (i =0 ; i < stmt->nr_params; i++) { +struct db_stmt_arg *arg = &stmt->args[i]; +union db_stmt_arg_value *val = &arg->value; -/* prepend it to the linked list */ -if ((f =calloc (1 ,sizeof (struct field ))) ==NULL ) { -ulogd_log (ULOGD_ERROR ,"SQLITE3: out of memory\n" ); -return -1 ; +if (arg->null) { +sqlite3_bind_null (si->p_stmt, i +1 ); +continue ; } -snprintf (f->name,sizeof (f->name), -"%s" ,sqlite3_column_name (schema_stmt, col)); - -DEBUGP ("field '%s' found\n" , f->name); -if ((f->key =ulogd_find_key (pi, f->name)) ==NULL ) { -ulogd_log (ULOGD_ERROR , -"SQLITE3: unknown input key: %s\n" , f->name); -free (f); -return -1 ; +switch (arg->type) { +case ULOGD_RET_BOOL : + rv =sqlite3_bind_int (si->p_stmt, i +1 , val->b); +break ; +case ULOGD_RET_INT8 : + rv =sqlite3_bind_int (si->p_stmt, i +1 , val->i8); +break ; +case ULOGD_RET_INT16 : + rv =sqlite3_bind_int (si->p_stmt, i +1 , val->i16); +break ; +case ULOGD_RET_INT32 : + rv =sqlite3_bind_int (si->p_stmt, i +1 , val->i32); +break ; +case ULOGD_RET_INT64 : + rv =sqlite3_bind_int (si->p_stmt, i +1 , val->i64); +break ; +case ULOGD_RET_UINT8 : + rv =sqlite3_bind_int (si->p_stmt, i +1 , val->ui8); +break ; +case ULOGD_RET_UINT16 : + rv =sqlite3_bind_int (si->p_stmt, i +1 , val->ui16); +break ; +case ULOGD_RET_UINT32 : + rv =sqlite3_bind_int (si->p_stmt, i +1 , val->ui32); +break ; +case ULOGD_RET_IPADDR : +case ULOGD_RET_UINT64 : + rv =sqlite3_bind_int64 (si->p_stmt, i +1 , val->ui64); +break ; +case ULOGD_RET_STRING : + rv =sqlite3_bind_text (si->p_stmt, i +1 , val->ptr, + arg->len,SQLITE_STATIC ); +break ; +case ULOGD_RET_RAWSTR : + rv =sqlite3_bind_blob (si->p_stmt, i +1 , val->ptr, + arg->len,SQLITE_STATIC ); +break ; } -TAILQ_INSERT_TAIL (&priv->fields, f, link); +if (rv !=SQLITE_OK ) { +ulogd_log (ULOGD_ERROR ,"%s: bind %u failed: %s\n" , + __func__, i +1 ,sqlite3_errmsg (si->dbh)); + rv =-1 ; + goto err_clear_bindings; + } } -sqlite3_finalize (schema_stmt); - -return 0 ; -} + rv =sqlite3_step (si->p_stmt); -#define SQLITE3_BUSY_TIMEOUT 300 +if (rv !=SQLITE_DONE ) { +ulogd_log (ULOGD_ERROR ,"%s: step failed: %s\n" , + __func__,sqlite3_errmsg (si->dbh)); + rv =-1 ; + }else + rv =0 ; -static int -sqlite3_configure (struct ulogd_pluginstance *pi, -struct ulogd_pluginstance_stack *stack) -{ -/* struct sqlite_priv *priv = (void *)pi->private; */ +sqlite3_reset (si->p_stmt); -config_parse_file (pi->id, pi->config_kset); +err_clear_bindings: +sqlite3_clear_bindings (si->p_stmt); -if (ulogd_wildcard_inputkeys (pi) <0 ) -return -1 ; - -DEBUGP ("%s: db='%s' table='%s'\n" , pi->id,db_ce (pi),table_ce (pi)); - -return 0 ; +return rv; } -static int -sqlite3_start (struct ulogd_pluginstance *pi) -{ -struct sqlite3_priv *priv = (void *)pi->private; - -TAILQ_INIT (&priv->fields); - -if (sqlite3_open (db_ce (pi), &priv->dbh) !=SQLITE_OK ) { -ulogd_log (ULOGD_ERROR ,"SQLITE3: %s\n" ,sqlite3_errmsg (priv->dbh)); -return -1 ; - } - -/* set the timeout so that we don't automatically fail -if the table is busy */ -sqlite3_busy_timeout (priv->dbh,SQLITE3_BUSY_TIMEOUT ); - -/* read the fieldnames to know which values to insert */ -if (sqlite3_init_db (pi) <0 ) { -ulogd_log (ULOGD_ERROR ,"SQLITE3: Could not read database fieldnames.\n" ); -return -1 ; - } - -/* create and prepare the actual insert statement */ -if (sqlite3_createstmt (pi) <0 ) { -ulogd_log (ULOGD_ERROR ,"SQLITE3: Could not create statement.\n" ); -return -1 ; - } - -return 0 ; -} +static struct db_driver sqlite3_db_driver = { + .get_columns = get_columns_sqlite3, + .get_column = get_column_sqlite3, + .open_db = open_db_sqlite3, + .close_db = close_db_sqlite3, + .prepare = prepare_sqlite3, + .execute = execute_sqlite3, +}; -/* give us an opportunity to close the database down properly */ static int -sqlite3_stop (struct ulogd_pluginstance *pi) +configure_sqlite3 (struct ulogd_pluginstance *upi, +struct ulogd_pluginstance_stack *stack) { -struct sqlite3_priv *priv = (void *)pi->private; +struct db_instance *di = (struct db_instance *) &upi->private; + di->driver = &sqlite3_db_driver; -/* free up our prepared statements so we can close the db */ -if (priv->p_stmt) { -sqlite3_finalize (priv->p_stmt); -DEBUGP ("prepared statement finalized\n" ); - } - -if (priv->dbh ==NULL ) -return -1 ; - -sqlite3_close (priv->dbh); - - priv->dbh =NULL ; - -return 0 ; +return ulogd_db_configure (upi, stack); }static struct ulogd_plugin sqlite3_plugin = {
@@ -426,12 +266,13 @@ static struct ulogd_plugin sqlite3_plugin = { .output = { .type = ULOGD_DTYPE_SINK, }, - .config_kset = &sqlite3_kset, - .priv_size = sizeof(struct sqlite3_priv), - .configure = sqlite3_configure, - .start = sqlite3_start, - .stop = sqlite3_stop, - .interp = sqlite3_interp, + .config_kset = &kset_sqlite3, + .priv_size = sizeof(struct sqlite3_instance), + .configure = configure_sqlite3, + .start = ulogd_db_start, + .stop = ulogd_db_stop, + .signal = ulogd_db_signal, + .interp = ulogd_db_interp, .version = VERSION, };
Signature · 1 line
2.35.1