Skip to content

Commit f1b97f0

Browse files
authored
Merge pull request #4340 from garlick/issue#4338
content-sqlite: verify database integrity during module load
2 parents fe3b799 + e6518a9 commit f1b97f0

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/modules/content-sqlite/content-sqlite.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,14 @@ static int content_sqlite_opendb (struct content_sqlite *ctx, bool truncate)
640640
log_sqlite_error (ctx, "setting sqlite 'locking_mode' pragma");
641641
goto error;
642642
}
643+
if (sqlite3_exec (ctx->db,
644+
"PRAGMA quick_check",
645+
NULL,
646+
NULL,
647+
NULL) != SQLITE_OK) {
648+
log_sqlite_error (ctx, "setting sqlite 'quick_check' pragma");
649+
goto error;
650+
}
643651
if (sqlite3_exec (ctx->db,
644652
sql_create_table,
645653
NULL,
@@ -832,16 +840,16 @@ int mod_main (flux_t *h, int argc, char **argv)
832840
if (content_register_backing_store (h, "content-sqlite") < 0)
833841
goto done;
834842
if (content_register_service (h, "content-backing") < 0)
835-
goto done;
843+
goto done_unreg;
836844
if (content_register_service (h, "kvs-checkpoint") < 0)
837-
goto done;
845+
goto done_unreg;
838846
if (flux_reactor_run (flux_get_reactor (h), 0) < 0) {
839847
flux_log_error (h, "flux_reactor_run");
840-
goto done;
848+
goto done_unreg;
841849
}
842-
if (content_unregister_backing_store (h) < 0)
843-
goto done;
844850
rc = 0;
851+
done_unreg:
852+
(void)content_unregister_backing_store (h);
845853
done:
846854
content_sqlite_closedb (ctx);
847855
content_sqlite_destroy (ctx);

t/t0012-content-sqlite.t

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,39 @@ test_expect_success 'run flux with statedir and verify modes' '
271271
grep "journal_mode=WAL synchronous=NORMAL" logs4
272272
'
273273

274+
# Will create in WAL mode since statedir is set
275+
recreate_database()
276+
{
277+
flux start -o,-Sbroker.rc1_path=,-Sbroker.rc3_path= \
278+
-o,-Sstatedir=$(pwd) bash -c \
279+
"flux module load content-sqlite truncate; \
280+
flux module remove content-sqlite"
281+
}
282+
load_module_xfail()
283+
{
284+
flux start -o,-Sbroker.rc1_path=,-Sbroker.rc3_path= \
285+
-o,-Sstatedir=$(pwd) flux module load content-sqlite
286+
}
287+
288+
# FWIW https://www.sqlite.org/fileformat.html
289+
test_expect_success 'create database with bad header magic' '
290+
recreate_database &&
291+
echo "xxxxxxxxxxxxxxxx" | dd obs=1 count=16 seek=0 of=content.sqlite
292+
'
293+
test_expect_success 'module load fails with corrupt database' '
294+
test_must_fail load_module_xfail
295+
'
296+
test_expect_success 'create database with bad schema format number' '
297+
recreate_database &&
298+
echo "\001\001\001\001" | dd obs=1 count=4 seek=44 of=content.sqlite
299+
'
300+
test_expect_success 'module load fails with corrupt database' '
301+
test_must_fail load_module_xfail
302+
'
303+
test_expect_success 'full instance start fails corrupt database' '
304+
test_must_fail flux start -o,-Sstatedir=$(pwd) /bin/true
305+
'
306+
274307
test_expect_success 'remove content-sqlite module on rank 0' '
275308
flux module remove content-sqlite
276309
'

0 commit comments

Comments
 (0)