Make the database code more robust.

This commit is contained in:
Wilco Baan Hofman 2014-01-12 23:06:33 +01:00 committed by root
parent eb041ce5b4
commit 4d34a694b1
3 changed files with 16 additions and 6 deletions

View file

@ -111,7 +111,7 @@ STATUS read_configuration_file(TALLOC_CTX *mem_ctx)
GError *error = NULL; GError *error = NULL;
char *buf, *ptr; char *buf, *ptr;
conf = talloc(mem_ctx, configuration); conf = talloc_zero(mem_ctx, configuration);
NO_MEM_RETURN(conf); NO_MEM_RETURN(conf);
conf->keyfile = g_key_file_new (); conf->keyfile = g_key_file_new ();

View file

@ -26,7 +26,7 @@ static dbi_conn conn;
*/ */
static char *talloc_quoted_string(TALLOC_CTX *mem_ctx, const char *string) { static char *talloc_quoted_string(TALLOC_CTX *mem_ctx, const char *string) {
/* Allocate twice the string length, to be safe and not having to realloc all the time */ /* Allocate twice the string length, to be safe and not having to realloc all the time */
char *ret = talloc_zero_array(mem_ctx, char, strlen(string) * 2 + 1); char *ret = talloc_zero_array(mem_ctx, char, strlen(string) * 2 + 3);
size_t i, j; size_t i, j;
NO_MEM_RETURN_RV(ret, NULL); NO_MEM_RETURN_RV(ret, NULL);
@ -51,6 +51,8 @@ STATUS proper_dbi_queryf(dbi_conn dbconn, const char *query_fmt, ...) {
va_list ap; va_list ap;
int conn_res; int conn_res;
dbi_result dbi_res; dbi_result dbi_res;
TALLOC_CTX *local_ctx;
char *query;
conn_res = dbi_conn_connect(dbconn); conn_res = dbi_conn_connect(dbconn);
if (conn_res != DBI_ERROR_NONE) { if (conn_res != DBI_ERROR_NONE) {
@ -65,10 +67,14 @@ STATUS proper_dbi_queryf(dbi_conn dbconn, const char *query_fmt, ...) {
return ST_DATABASE_FAILURE; return ST_DATABASE_FAILURE;
} }
local_ctx = talloc_init(NULL);
va_start(ap, query_fmt); va_start(ap, query_fmt);
dbi_res = dbi_conn_queryf(dbconn, query_fmt, ap); query = talloc_vasprintf(local_ctx, query_fmt, ap);
DEBUG(0, "Executing query %s.", query);
va_end(ap); va_end(ap);
dbi_res = dbi_conn_query(dbconn, query);
if (dbi_res == NULL) { if (dbi_res == NULL) {
const char *errmsg; const char *errmsg;
int err_res = dbi_conn_error(dbconn, &errmsg); int err_res = dbi_conn_error(dbconn, &errmsg);
@ -78,9 +84,11 @@ STATUS proper_dbi_queryf(dbi_conn dbconn, const char *query_fmt, ...) {
} else { } else {
DEBUG(0, "Database error %d when querying: %s", err_res, errmsg); DEBUG(0, "Database error %d when querying: %s", err_res, errmsg);
} }
talloc_free(local_ctx);
return ST_DATABASE_FAILURE; return ST_DATABASE_FAILURE;
} }
talloc_free(local_ctx);
return ST_OK; return ST_OK;
} }
STATUS log_event_to_database(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description) { STATUS log_event_to_database(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description) {
@ -100,6 +108,8 @@ STATUS log_event_to_database(TALLOC_CTX *mem_ctx, const char *prom, const char *
DEBUG(3, "Storing event: %s %s %s -- %s: %s\n", prom, code, description, sia_code_str(code), sia_code_desc(code)); DEBUG(3, "Storing event: %s %s %s -- %s: %s\n", prom, code, description, sia_code_str(code), sia_code_desc(code));
DEBUG(0, "%s,%s,%s,%s", quoted_prom, quoted_code, quoted_long_code, quoted_description);
proper_dbi_queryf(conn, "INSERT INTO events (timestamp, prom, code, long_code, description) VALUES (NOW(), %s, %s, %s, %s)\n", proper_dbi_queryf(conn, "INSERT INTO events (timestamp, prom, code, long_code, description) VALUES (NOW(), %s, %s, %s, %s)\n",
quoted_prom, quoted_code, quoted_long_code, quoted_description); quoted_prom, quoted_code, quoted_long_code, quoted_description);

View file

@ -24,7 +24,7 @@ static dbi_conn conn;
STATUS spacestate_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description) { STATUS spacestate_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description) {
bool must_close = 0; bool must_close = 0;
bool must_open = 0; bool must_open = 0;
STATUS result; STATUS result = ST_OK;
DEBUG(6, "Got event for spacestate: %s %s %s -- %s: %s\n", prom, code, description, sia_code_str(code), sia_code_desc(code)); DEBUG(6, "Got event for spacestate: %s %s %s -- %s: %s\n", prom, code, description, sia_code_str(code), sia_code_desc(code));