1*c5c4113dSnw141292 2*c5c4113dSnw141292 #pragma ident "%Z%%M% %I% %E% SMI" 3*c5c4113dSnw141292 4*c5c4113dSnw141292 /* 5*c5c4113dSnw141292 ** This program tests the ability of SQLite database to recover from a crash. 6*c5c4113dSnw141292 ** This program runs under Unix only, but the results are applicable to all 7*c5c4113dSnw141292 ** systems. 8*c5c4113dSnw141292 ** 9*c5c4113dSnw141292 ** The main process first constructs a test database, then starts creating 10*c5c4113dSnw141292 ** subprocesses that write to that database. Each subprocess is killed off, 11*c5c4113dSnw141292 ** without a chance to clean up its database connection, after a random 12*c5c4113dSnw141292 ** delay. This killing of the subprocesses simulates a crash or power 13*c5c4113dSnw141292 ** failure. The next subprocess to open the database should rollback 14*c5c4113dSnw141292 ** whatever operation was in process at the time of the simulated crash. 15*c5c4113dSnw141292 ** 16*c5c4113dSnw141292 ** If any problems are encountered, an error is reported and the test stops. 17*c5c4113dSnw141292 ** If no problems are seen after a large number of tests, we assume that 18*c5c4113dSnw141292 ** the rollback mechanism is working. 19*c5c4113dSnw141292 */ 20*c5c4113dSnw141292 #include <stdio.h> 21*c5c4113dSnw141292 #include <unistd.h> 22*c5c4113dSnw141292 #include <sys/types.h> 23*c5c4113dSnw141292 #include <sys/wait.h> 24*c5c4113dSnw141292 #include <signal.h> 25*c5c4113dSnw141292 #include <stdlib.h> 26*c5c4113dSnw141292 #include <string.h> 27*c5c4113dSnw141292 #include <sched.h> 28*c5c4113dSnw141292 #include "sqlite.h" 29*c5c4113dSnw141292 30*c5c4113dSnw141292 static void do_some_sql(int parent){ 31*c5c4113dSnw141292 char *zErr; 32*c5c4113dSnw141292 int rc = SQLITE_OK; 33*c5c4113dSnw141292 sqlite *db; 34*c5c4113dSnw141292 int cnt = 0; 35*c5c4113dSnw141292 static char zBig[] = 36*c5c4113dSnw141292 "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 37*c5c4113dSnw141292 "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 38*c5c4113dSnw141292 39*c5c4113dSnw141292 if( access("./test.db-journal",0)==0 ){ 40*c5c4113dSnw141292 /*printf("pid %d: journal exists. rollback will be required\n",getpid());*/ unlink("test.db-saved"); 41*c5c4113dSnw141292 system("cp test.db test.db-saved"); 42*c5c4113dSnw141292 unlink("test.db-journal-saved"); 43*c5c4113dSnw141292 system("cp test.db-journal test.db-journal-saved"); 44*c5c4113dSnw141292 } 45*c5c4113dSnw141292 db = sqlite_open("./test.db", 0, &zErr); 46*c5c4113dSnw141292 if( db==0 ){ 47*c5c4113dSnw141292 printf("ERROR: %s\n", zErr); 48*c5c4113dSnw141292 if( strcmp(zErr,"database disk image is malformed")==0 ){ 49*c5c4113dSnw141292 kill(parent, SIGKILL); 50*c5c4113dSnw141292 } 51*c5c4113dSnw141292 exit(1); 52*c5c4113dSnw141292 } 53*c5c4113dSnw141292 srand(getpid()); 54*c5c4113dSnw141292 while( rc==SQLITE_OK ){ 55*c5c4113dSnw141292 cnt++; 56*c5c4113dSnw141292 rc = sqlite_exec_printf(db, 57*c5c4113dSnw141292 "INSERT INTO t1 VALUES(%d,'%d%s')", 0, 0, &zErr, 58*c5c4113dSnw141292 rand(), rand(), zBig); 59*c5c4113dSnw141292 } 60*c5c4113dSnw141292 if( rc!=SQLITE_OK ){ 61*c5c4113dSnw141292 printf("ERROR #%d: %s\n", rc, zErr); 62*c5c4113dSnw141292 if( rc==SQLITE_CORRUPT ){ 63*c5c4113dSnw141292 kill(parent, SIGKILL); 64*c5c4113dSnw141292 } 65*c5c4113dSnw141292 } 66*c5c4113dSnw141292 printf("pid %d: cnt=%d\n", getpid(), cnt); 67*c5c4113dSnw141292 } 68*c5c4113dSnw141292 69*c5c4113dSnw141292 70*c5c4113dSnw141292 int main(int argc, char **argv){ 71*c5c4113dSnw141292 int i; 72*c5c4113dSnw141292 sqlite *db; 73*c5c4113dSnw141292 char *zErr; 74*c5c4113dSnw141292 int status; 75*c5c4113dSnw141292 int parent = getpid(); 76*c5c4113dSnw141292 77*c5c4113dSnw141292 unlink("test.db"); 78*c5c4113dSnw141292 unlink("test.db-journal"); 79*c5c4113dSnw141292 db = sqlite_open("test.db", 0, &zErr); 80*c5c4113dSnw141292 if( db==0 ){ 81*c5c4113dSnw141292 printf("Cannot initialize: %s\n", zErr); 82*c5c4113dSnw141292 return 1; 83*c5c4113dSnw141292 } 84*c5c4113dSnw141292 sqlite_exec(db, "CREATE TABLE t1(a,b)", 0, 0, 0); 85*c5c4113dSnw141292 sqlite_close(db); 86*c5c4113dSnw141292 for(i=0; i<10000; i++){ 87*c5c4113dSnw141292 int pid = fork(); 88*c5c4113dSnw141292 if( pid==0 ){ 89*c5c4113dSnw141292 sched_yield(); 90*c5c4113dSnw141292 do_some_sql(parent); 91*c5c4113dSnw141292 return 0; 92*c5c4113dSnw141292 } 93*c5c4113dSnw141292 printf("test %d, pid=%d\n", i, pid); 94*c5c4113dSnw141292 usleep(rand()%10000 + 1000); 95*c5c4113dSnw141292 kill(pid, SIGKILL); 96*c5c4113dSnw141292 waitpid(pid, &status, 0); 97*c5c4113dSnw141292 } 98*c5c4113dSnw141292 return 0; 99*c5c4113dSnw141292 } 100