diff --git a/src/modules/content-sqlite/content-sqlite.c b/src/modules/content-sqlite/content-sqlite.c index 3674159846bc..cb9a3ade95f3 100644 --- a/src/modules/content-sqlite/content-sqlite.c +++ b/src/modules/content-sqlite/content-sqlite.c @@ -640,6 +640,14 @@ static int content_sqlite_opendb (struct content_sqlite *ctx, bool truncate) log_sqlite_error (ctx, "setting sqlite 'locking_mode' pragma"); goto error; } + if (sqlite3_exec (ctx->db, + "PRAGMA quick_check", + NULL, + NULL, + NULL) != SQLITE_OK) { + log_sqlite_error (ctx, "setting sqlite 'quick_check' pragma"); + goto error; + } if (sqlite3_exec (ctx->db, sql_create_table, NULL, @@ -832,16 +840,16 @@ int mod_main (flux_t *h, int argc, char **argv) if (content_register_backing_store (h, "content-sqlite") < 0) goto done; if (content_register_service (h, "content-backing") < 0) - goto done; + goto done_unreg; if (content_register_service (h, "kvs-checkpoint") < 0) - goto done; + goto done_unreg; if (flux_reactor_run (flux_get_reactor (h), 0) < 0) { flux_log_error (h, "flux_reactor_run"); - goto done; + goto done_unreg; } - if (content_unregister_backing_store (h) < 0) - goto done; rc = 0; +done_unreg: + (void)content_unregister_backing_store (h); done: content_sqlite_closedb (ctx); content_sqlite_destroy (ctx); diff --git a/t/t0012-content-sqlite.t b/t/t0012-content-sqlite.t index 89d17f940018..16b6e5502bd8 100755 --- a/t/t0012-content-sqlite.t +++ b/t/t0012-content-sqlite.t @@ -271,6 +271,39 @@ test_expect_success 'run flux with statedir and verify modes' ' grep "journal_mode=WAL synchronous=NORMAL" logs4 ' +# Will create in WAL mode since statedir is set +recreate_database() +{ + flux start -o,-Sbroker.rc1_path=,-Sbroker.rc3_path= \ + -o,-Sstatedir=$(pwd) bash -c \ + "flux module load content-sqlite truncate; \ + flux module remove content-sqlite" +} +load_module_xfail() +{ + flux start -o,-Sbroker.rc1_path=,-Sbroker.rc3_path= \ + -o,-Sstatedir=$(pwd) flux module load content-sqlite +} + +# FWIW https://www.sqlite.org/fileformat.html +test_expect_success 'create database with bad header magic' ' + recreate_database && + echo "xxxxxxxxxxxxxxxx" | dd obs=1 count=16 seek=0 of=content.sqlite +' +test_expect_success 'module load fails with corrupt database' ' + test_must_fail load_module_xfail +' +test_expect_success 'create database with bad schema format number' ' + recreate_database && + echo "\001\001\001\001" | dd obs=1 count=4 seek=44 of=content.sqlite +' +test_expect_success 'module load fails with corrupt database' ' + test_must_fail load_module_xfail +' +test_expect_success 'full instance start fails corrupt database' ' + test_must_fail flux start -o,-Sstatedir=$(pwd) /bin/true +' + test_expect_success 'remove content-sqlite module on rank 0' ' flux module remove content-sqlite '