Skip to content

Commit

Permalink
Ensure disk database is detached on SQL issues
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Aug 1, 2023
1 parent 4ac9f12 commit e427795
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/api/queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ int api_queries(struct ftl_conn *api)
int rc = sqlite3_prepare_v2(db, querystr, -1, &read_stmt, NULL);
if( rc != SQLITE_OK )
{
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to prepare SQL query",
Expand All @@ -384,6 +386,8 @@ int api_queries(struct ftl_conn *api)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind domain to SQL query",
Expand All @@ -399,6 +403,8 @@ int api_queries(struct ftl_conn *api)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind cip to SQL query",
Expand All @@ -414,6 +420,8 @@ int api_queries(struct ftl_conn *api)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind client to SQL query",
Expand All @@ -429,6 +437,8 @@ int api_queries(struct ftl_conn *api)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind upstream to SQL query",
Expand All @@ -453,6 +463,8 @@ int api_queries(struct ftl_conn *api)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind type to SQL query",
Expand All @@ -461,6 +473,8 @@ int api_queries(struct ftl_conn *api)
}
else
{
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 400,
"bad_request",
"Requested type is invalid",
Expand All @@ -485,6 +499,8 @@ int api_queries(struct ftl_conn *api)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind status to SQL query",
Expand All @@ -493,6 +509,8 @@ int api_queries(struct ftl_conn *api)
}
else
{
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 400,
"bad_request",
"Requested status is invalid",
Expand All @@ -517,6 +535,8 @@ int api_queries(struct ftl_conn *api)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind reply to SQL query",
Expand All @@ -525,6 +545,8 @@ int api_queries(struct ftl_conn *api)
}
else
{
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 400,
"bad_request",
"Requested reply is invalid",
Expand All @@ -549,6 +571,8 @@ int api_queries(struct ftl_conn *api)
{
sqlite3_reset(read_stmt);
sqlite3_finalize(read_stmt);
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 500,
"internal_error",
"Internal server error, failed to bind dnssec to SQL query",
Expand All @@ -557,6 +581,8 @@ int api_queries(struct ftl_conn *api)
}
else
{
if(disk)
detach_disk_database(NULL);
return send_json_error(api, 400,
"bad_request",
"Requested dnssec is invalid",
Expand Down
7 changes: 7 additions & 0 deletions src/database/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "database/query-table.h"
// set_event()
#include "events.h"
// generate_backtrace()
#include "signals.h"

bool DBdeleteoldqueries = false;
static bool DBerror = false;
Expand Down Expand Up @@ -238,6 +240,11 @@ void SQLite3LogCallback(void *pArg, int iErrCode, const char *zMsg)
// We can safely ignore this warning
return;
}

// Log backtrace if any debug flag is set
if(config.debug.extra.v.b)
generate_backtrace();

if(iErrCode == SQLITE_WARNING)
log_warn("SQLite3 message: %s (%d)", zMsg, iErrCode);
else
Expand Down
13 changes: 12 additions & 1 deletion src/database/query-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ int get_number_of_queries_in_DB(sqlite3 *db, const char *tablename, const bool d
log_err("get_number_of_queries_in_DB(%s): Prepare error: %s",
tablename, sqlite3_errstr(rc));
free(querystr);
if(do_attach)
detach_disk_database(NULL);
return false;
}
rc = sqlite3_step(stmt);
Expand All @@ -363,6 +365,8 @@ int get_number_of_queries_in_DB(sqlite3 *db, const char *tablename, const bool d
tablename, sqlite3_errstr(rc));
free(querystr);
sqlite3_finalize(stmt);
if(do_attach)
detach_disk_database(NULL);
return false;
}
sqlite3_finalize(stmt);
Expand Down Expand Up @@ -394,13 +398,15 @@ bool import_queries_from_disk(void)
if((rc = sqlite3_exec(memdb, "BEGIN TRANSACTION", NULL, NULL, NULL)) != SQLITE_OK)
{
log_err("import_queries_from_disk(): Cannot start transaction: %s", sqlite3_errstr(rc));
detach_disk_database(NULL);
return false;
}

// Prepare SQLite3 statement
sqlite3_stmt *stmt = NULL;
if((rc = sqlite3_prepare_v2(memdb, querystr, -1, &stmt, NULL)) != SQLITE_OK){
log_err("import_queries_from_disk(): SQL error prepare: %s", sqlite3_errstr(rc));
detach_disk_database(NULL);
return false;
}

Expand All @@ -409,6 +415,7 @@ bool import_queries_from_disk(void)
{
log_err("import_queries_from_disk(): Failed to bind type mintime: %s", sqlite3_errstr(rc));
sqlite3_finalize(stmt);
detach_disk_database(NULL);
return false;
}

Expand Down Expand Up @@ -451,6 +458,7 @@ bool import_queries_from_disk(void)
if((rc = sqlite3_exec(memdb, "END TRANSACTION", NULL, NULL, NULL)) != SQLITE_OK)
{
log_err("import_queries_from_disk(): Cannot end transaction: %s", sqlite3_errstr(rc));
detach_disk_database(NULL);
return false;
}

Expand Down Expand Up @@ -492,13 +500,15 @@ bool export_queries_to_disk(bool final)
int rc = sqlite3_prepare_v2(memdb, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK ){
log_err("export_queries_to_disk(): SQL error prepare: %s", sqlite3_errstr(rc));
detach_disk_database(NULL);
return false;
}

// Bind index
if((rc = sqlite3_bind_int64(stmt, 1, last_disk_db_idx)) != SQLITE_OK)
{
log_err("export_queries_to_disk(): Failed to bind id: %s", sqlite3_errstr(rc));
detach_disk_database(NULL);
return false;
}

Expand All @@ -509,6 +519,7 @@ bool export_queries_to_disk(bool final)
if((rc = sqlite3_bind_double(stmt, 2, time)) != SQLITE_OK)
{
log_err("export_queries_to_disk(): Failed to bind time: %s", sqlite3_errstr(rc));
detach_disk_database(NULL);
return false;
}

Expand Down Expand Up @@ -572,6 +583,7 @@ bool export_queries_to_disk(bool final)
if((rc = sqlite3_exec(memdb, "END TRANSACTION", NULL, NULL, NULL)) != SQLITE_OK)
{
log_err("export_queries_to_disk(): Cannot end transaction: %s", sqlite3_errstr(rc));
detach_disk_database(NULL);
return false;
}

Expand Down Expand Up @@ -1146,7 +1158,6 @@ void DB_read_queries(void)
if(!detach_disk_database(NULL))
return;


// Update indices so that the next call to DB_save_queries() skips the
// queries that we just imported from the database
last_mem_db_idx = last_disk_db_idx;
Expand Down

0 comments on commit e427795

Please sign in to comment.