New commit. Much changes. Very impress. Wow.

This commit is contained in:
Wilco Baan Hofman 2014-01-12 20:52:43 +01:00 committed by root
parent 6eb419202c
commit eb041ce5b4
5 changed files with 65 additions and 9 deletions

View file

@ -47,6 +47,42 @@ static char *talloc_quoted_string(TALLOC_CTX *mem_ctx, const char *string) {
return ret; return ret;
} }
STATUS proper_dbi_queryf(dbi_conn dbconn, const char *query_fmt, ...) {
va_list ap;
int conn_res;
dbi_result dbi_res;
conn_res = dbi_conn_connect(dbconn);
if (conn_res != DBI_ERROR_NONE) {
const char *errmsg;
int err_res = dbi_conn_error(dbconn, &errmsg);
if (err_res == DBI_ERROR_NONE) {
DEBUG(0, "Strange situation: There was an error, but error buffer is empty");
} else {
DEBUG(0, "Database error %d/%d while connecting: %s", conn_res, err_res, errmsg);
}
return ST_DATABASE_FAILURE;
}
va_start(ap, query_fmt);
dbi_res = dbi_conn_queryf(dbconn, query_fmt, ap);
va_end(ap);
if (dbi_res == NULL) {
const char *errmsg;
int err_res = dbi_conn_error(dbconn, &errmsg);
if (err_res == DBI_ERROR_NONE) {
DEBUG(0, "Strange situation: There was an error, but error buffer is empty");
} else {
DEBUG(0, "Database error %d when querying: %s", err_res, errmsg);
}
return ST_DATABASE_FAILURE;
}
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) {
char *quoted_prom; char *quoted_prom;
char *quoted_code; char *quoted_code;
@ -64,7 +100,7 @@ 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));
dbi_conn_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);
talloc_free(quoted_prom); talloc_free(quoted_prom);
@ -81,6 +117,7 @@ STATUS database_init(void)
{ {
configuration *conf = get_modifiable_conf(); configuration *conf = get_modifiable_conf();
GError *error = NULL; GError *error = NULL;
int conn_res;
conf->database_host = g_key_file_get_string(conf->keyfile, "database", conf->database_host = g_key_file_get_string(conf->keyfile, "database",
"host", &error); "host", &error);
@ -128,8 +165,16 @@ STATUS database_init(void)
dbi_conn_set_option(conn, "dbname", conf->database_name); dbi_conn_set_option(conn, "dbname", conf->database_name);
dbi_conn_set_option(conn, "encoding", "UTF-8"); dbi_conn_set_option(conn, "encoding", "UTF-8");
if (dbi_conn_connect(conn) < 0) { conn_res = dbi_conn_connect(conn);
DEBUG(0, "Could not connect to the database"); if (conn_res != DBI_ERROR_NONE) {
const char *errmsg;
int err_res = dbi_conn_error(conn, &errmsg);
if (err_res == DBI_ERROR_NONE) {
DEBUG(0, "Strange situation: There was an error, but error buffer is empty");
} else {
DEBUG(0, "Database error %d/%d while connecting: %s", conn_res, err_res, errmsg);
}
return ST_DATABASE_FAILURE; return ST_DATABASE_FAILURE;
} }

View file

@ -17,4 +17,5 @@
*/ */
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);
STATUS proper_dbi_queryf(dbi_conn conn, const char *query_fmt, ...);
STATUS database_init(void); STATUS database_init(void);

View file

@ -55,7 +55,7 @@ STATUS parse_siahs_message(TALLOC_CTX *mem_ctx, const char *pkt_prom, const char
} }
/* Dispatch all configured event handlers */ /* Dispatch all configured event handlers */
for (i = 0; conf->event_handlers[i] != NULL; i++) { for (i = 0; i < conf->event_handler_cnt; i++) {
conf->event_handlers[i](message, prom, code, ptr); conf->event_handlers[i](message, prom, code, ptr);
} }

View file

@ -126,8 +126,10 @@ int main(int argc, char **argv) {
if ((pid = fork())) { if ((pid = fork())) {
/* Write PID file */ /* Write PID file */
pidfile = fopen(conf->pid_file, "w"); pidfile = fopen(conf->pid_file, "w");
if (pidfile < 0) if (pidfile == NULL) {
DEBUG(0, "Cannot open pid file");
return ST_LOG_ERR; return ST_LOG_ERR;
}
n = fprintf(pidfile, "%d\n", pid); n = fprintf(pidfile, "%d\n", pid);
fclose(pidfile); fclose(pidfile);

View file

@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "includes.h" #include "includes.h"
#include "database.h"
static dbi_conn conn; static dbi_conn conn;
@ -23,6 +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;
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));
@ -35,7 +37,7 @@ STATUS spacestate_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code
strncmp(code, "CQ", 2) == 0 || strncmp(code, "CQ", 2) == 0 ||
strncmp(code, "CS", 2) == 0) { strncmp(code, "CS", 2) == 0) {
must_close = 1; must_close = 1;
} }
if (strncmp(code, "OP", 2) == 0 || if (strncmp(code, "OP", 2) == 0 ||
strncmp(code, "OA", 2) == 0 || strncmp(code, "OA", 2) == 0 ||
@ -47,9 +49,15 @@ STATUS spacestate_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code
} }
if (must_open) { if (must_open) {
dbi_conn_queryf(conn, "UPDATE space_state set override=0, override_state='open'"); DEBUG(3, "Alarm disarmed. Updating space state override.");
result = proper_dbi_queryf(conn, "UPDATE space_state set override=0, override_state='open';");
} else if (must_close) { } else if (must_close) {
dbi_conn_queryf(conn, "UPDATE space_state set override=1, override_state='closed'"); DEBUG(3, "Alarm armed. Updating space state override.");
result = proper_dbi_queryf(conn, "UPDATE space_state set override=1, override_state='closed';");
}
if (result != ST_OK) {
return result;
} }
return ST_OK; return ST_OK;