From be5c10477c17f0fe4916181b449e7575a96d5fec Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 17 Jun 2010 17:05:53 +0000 Subject: [PATCH 1/3] Apply [b9b11855e8] (the alternate fix to [fc62af4523]) to the trunk. FossilOrigin-Name: 9a949a3a5c32b8bfbb94e10e18d050ec80a25553 --- manifest | 28 +++++++++------------------- manifest.uuid | 2 +- src/pager.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/vdbe.c | 7 ++----- test/jrnlmode.test | 4 ++-- 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/manifest b/manifest index 1ed3a6b60a..eeefca1a8d 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,5 @@ ------BEGIN PGP SIGNED MESSAGE----- -Hash: SHA1 - -C Fix\sthe\stkt-fc62af4523.test\sto\swork\saround\snon-randomness\sof\sthe\nrandomblob()\sfunction\swhen\sin\stesting\smode. -D 2010-06-17T16:08:54 +C Apply\s[b9b11855e8]\s(the\salternate\sfix\sto\s[fc62af4523])\sto\sthe\strunk. +D 2010-06-17T17:05:54 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in a5cad1f8f3e021356bfcc6c77dc16f6f1952bbc3 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -159,7 +156,7 @@ F src/os_common.h a8f95b81eca8a1ab8593d23e94f8a35f35d4078f F src/os_os2.c 665876d5eec7585226b0a1cf5e18098de2b2da19 F src/os_unix.c ae173c9f6afaa58b2833a1c95c6cd32021755c42 F src/os_win.c dfde7d33c446e89dd9a277c036f2c4cc564b3138 -F src/pager.c b5584c5e75e463b4a136e26858d555e83ca97b44 +F src/pager.c 4fe451d68950002eb985e6325d666ab54956a37f F src/pager.h ca1f23c0cf137ac26f8908df2427c8b308361efd F src/parse.y ace5c7a125d9f2a410e431ee3209034105045f7e F src/pcache.c 1e9aa2dbc0845b52e1b51cc39753b6d1e041cb07 @@ -220,7 +217,7 @@ F src/update.c 9859f2056c7739a1db0d9774ccb6c2f0cee6d1de F src/utf.c 1baeeac91707a4df97ccc6141ec0f808278af685 F src/util.c 32aebf04c10e51ad3977a928b7416bed671b620b F src/vacuum.c 241a8386727c1497eba4955933356dfba6ff8c9f -F src/vdbe.c 8a910a19981b8f670f4d67b5b0673459de50ccba +F src/vdbe.c e115585b14d2cc4128cb53a7e42f207750e80f55 F src/vdbe.h 471f6a3dcec4817ca33596fe7f6654d56c0e75f3 F src/vdbeInt.h 19ebc8c2a2e938340051ee65af3f377fb99102d1 F src/vdbeapi.c dc3138f10afbc95ed3c21dd25abb154504b1db9d @@ -469,7 +466,7 @@ F test/join4.test 1a352e4e267114444c29266ce79e941af5885916 F test/join5.test 86675fc2919269aa923c84dd00ee4249b97990fe F test/join6.test bf82cf3f979e9eade83ad0d056a66c5ed71d1901 F test/journal1.test 36f2d1bb9bf03f790f43fbdb439e44c0657fab19 -F test/jrnlmode.test cd0c9c5b2555f3d5bb72ed8aa8d0ad16ecf77e7c +F test/jrnlmode.test 76f94d61528c5ff32102a12f8cf34f4cc36f7849 F test/jrnlmode2.test fe79ea1f0375c926b8de0362ddf94f34a64135fd F test/jrnlmode3.test cfcdb12b90e640a23b92785a002d96c0624c8710 F test/keyword1.test a2400977a2e4fde43bf33754c2929fda34dbca05 @@ -826,14 +823,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 1ec74591a93e8b27ad3ac739914a48a91972e82c -R f22efcb25274595c63db494b4fd0d5e3 -U drh -Z 5de72be9b8f0adddcd3189b98a437ea2 ------BEGIN PGP SIGNATURE----- -Version: GnuPG v1.4.6 (GNU/Linux) - -iD8DBQFMGkiZoxKgR168RlERAqWGAJwJQXBZXrZmTaB1vIkV/5uryay3cACbBcij -ZJYGkT2oDVKZ+q0ThWu3qBk= -=rpt5 ------END PGP SIGNATURE----- +P 7c3a86b9c7e2a35ce755c32b38e911e79d843fad +R 1e4481b965cd36ec30ba83a04296f9cc +U dan +Z b00e14e0e21b5f60f6931c141403c159 diff --git a/manifest.uuid b/manifest.uuid index ac9c423d57..97d2603404 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -7c3a86b9c7e2a35ce755c32b38e911e79d843fad \ No newline at end of file +9a949a3a5c32b8bfbb94e10e18d050ec80a25553 \ No newline at end of file diff --git a/src/pager.c b/src/pager.c index ad1e1e1378..c875dd9dfa 100644 --- a/src/pager.c +++ b/src/pager.c @@ -5877,6 +5877,48 @@ int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ /* Change the journal mode. */ pPager->journalMode = (u8)eMode; + + /* When transistioning from TRUNCATE or PERSIST to any other journal + ** mode except WAL (and we are not in locking_mode=EXCLUSIVE) then + ** delete the journal file. + */ + assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 ); + assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 ); + assert( (PAGER_JOURNALMODE_DELETE & 5)==0 ); + assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 ); + assert( (PAGER_JOURNALMODE_OFF & 5)==0 ); + assert( (PAGER_JOURNALMODE_WAL & 5)==5 ); + + assert( isOpen(pPager->fd) || pPager->exclusiveMode ); + if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){ + + /* In this case we would like to delete the journal file. If it is + ** not possible, then that is not a problem. Deleting the journal file + ** here is an optimization only. + ** + ** Before deleting the journal file, obtain a RESERVED lock on the + ** database file. This ensures that the journal file is not deleted + ** while it is in use by some other client. + */ + int rc = SQLITE_OK; + int state = pPager->state; + if( statestate==PAGER_SHARED ){ + assert( rc==SQLITE_OK ); + rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK); + } + if( rc==SQLITE_OK ){ + sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); + } + if( rc==SQLITE_OK && state==PAGER_SHARED ){ + sqlite3OsUnlock(pPager->fd, SHARED_LOCK); + }else if( state==PAGER_UNLOCK ){ + pager_unlock(pPager); + } + assert( state==pPager->state ); + } } /* Return the new journal mode */ diff --git a/src/vdbe.c b/src/vdbe.c index 14785d35d5..6803beafd3 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -5251,9 +5251,6 @@ case OP_JournalMode: { /* out2-prerelease */ }else if( rc==SQLITE_BUSY && pOp->p5==0 ){ goto abort_due_to_error; } - }else{ - sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_DELETE); - rc = SQLITE_OK; } /* Open a transaction on the database file. Regardless of the journal @@ -5261,8 +5258,7 @@ case OP_JournalMode: { /* out2-prerelease */ */ assert( sqlite3BtreeIsInTrans(pBt)==0 ); if( rc==SQLITE_OK ){ - rc = sqlite3BtreeSetVersion(pBt, - (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); + rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); if( rc==SQLITE_BUSY && pOp->p5==0 ) goto abort_due_to_error; } if( rc==SQLITE_BUSY ){ @@ -5274,6 +5270,7 @@ case OP_JournalMode: { /* out2-prerelease */ #endif /* ifndef SQLITE_OMIT_WAL */ eNew = sqlite3PagerSetJournalMode(pPager, eNew); + pOut = &aMem[pOp->p2]; pOut->flags = MEM_Str|MEM_Static|MEM_Term; pOut->z = (char *)sqlite3JournalModename(eNew); diff --git a/test/jrnlmode.test b/test/jrnlmode.test index 2a6338fc9e..fd3e59f12d 100644 --- a/test/jrnlmode.test +++ b/test/jrnlmode.test @@ -484,7 +484,7 @@ ifcapable pragma { INSERT INTO t4 VALUES(3, 4); } file exists test.db-journal - } {1} + } {0} do_test jrnlmode-6.7 { execsql { COMMIT; @@ -493,7 +493,7 @@ ifcapable pragma { } {1 2 3 4} do_test jrnlmode-6.8 { file exists test.db-journal - } {1} + } {0} do_test jrnlmode-6.9 { execsql { PRAGMA journal_mode = DELETE; From 7234c6d6f6837325fd478f2cd33c1b7e2d01acad Mon Sep 17 00:00:00 2001 From: drh Date: Sat, 19 Jun 2010 15:10:09 +0000 Subject: [PATCH 2/3] Change the unix VFS to always allocate shared-memory using a file in the same directory as the database. Otherwise, a chroot might cause different processes to use different shared memory files resulting in database corruption. FossilOrigin-Name: 2241788bc85fbc48e9cfecb95fe0a858338e37cb --- manifest | 24 ++++++++++++++------ manifest.uuid | 2 +- src/os_unix.c | 62 ++++++++++++++++++++------------------------------- 3 files changed, 42 insertions(+), 46 deletions(-) diff --git a/manifest b/manifest index eeefca1a8d..483d16970a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,8 @@ -C Apply\s[b9b11855e8]\s(the\salternate\sfix\sto\s[fc62af4523])\sto\sthe\strunk. -D 2010-06-17T17:05:54 +-----BEGIN PGP SIGNED MESSAGE----- +Hash: SHA1 + +C Change\sthe\sunix\sVFS\sto\salways\sallocate\sshared-memory\susing\sa\sfile\sin\sthe\nsame\sdirectory\sas\sthe\sdatabase.\s\sOtherwise,\sa\schroot\smight\scause\sdifferent\nprocesses\sto\suse\sdifferent\sshared\smemory\sfiles\sresulting\sin\sdatabase\ncorruption. +D 2010-06-19T15:10:10 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in a5cad1f8f3e021356bfcc6c77dc16f6f1952bbc3 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -154,7 +157,7 @@ F src/os.c 9c4a2f82a50306a33907678ec0187b6ad1486bfe F src/os.h d7775504a51e6e0d40315aa427b3e229ff9ff9ca F src/os_common.h a8f95b81eca8a1ab8593d23e94f8a35f35d4078f F src/os_os2.c 665876d5eec7585226b0a1cf5e18098de2b2da19 -F src/os_unix.c ae173c9f6afaa58b2833a1c95c6cd32021755c42 +F src/os_unix.c a76d1952ac7984574701c48b665220b871c5c9a5 F src/os_win.c dfde7d33c446e89dd9a277c036f2c4cc564b3138 F src/pager.c 4fe451d68950002eb985e6325d666ab54956a37f F src/pager.h ca1f23c0cf137ac26f8908df2427c8b308361efd @@ -823,7 +826,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 7c3a86b9c7e2a35ce755c32b38e911e79d843fad -R 1e4481b965cd36ec30ba83a04296f9cc -U dan -Z b00e14e0e21b5f60f6931c141403c159 +P 9a949a3a5c32b8bfbb94e10e18d050ec80a25553 +R acdbeeb43afddd9408962cb351e87aec +U drh +Z 2d4cd5d2db2524a9b5137cfeafb22e1e +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v1.4.6 (GNU/Linux) + +iD8DBQFMHN3WoxKgR168RlERApgaAJ48Uha0kqQFVEwHVS03zg+AKsjJKwCfSU8L +jmliKcE06t2NitzknAIS5tM= +=X+y+ +-----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index 97d2603404..42d1b1066c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -9a949a3a5c32b8bfbb94e10e18d050ec80a25553 \ No newline at end of file +2241788bc85fbc48e9cfecb95fe0a858338e37cb \ No newline at end of file diff --git a/src/os_unix.c b/src/os_unix.c index 574f48d189..8e3ca66847 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -3274,19 +3274,21 @@ static void unixShmPurge(unixFile *pFd){ } } -/* Forward reference */ -static const char *unixTempFileDir(int); - /* -** Open a shared-memory area. This particular implementation uses -** mmapped files. +** Open a shared-memory area associated with open database file fd. +** This particular implementation uses mmapped files. ** -** zName is a filename used to identify the shared-memory area. The -** implementation does not (and perhaps should not) use this name -** directly, but rather use it as a template for finding an appropriate -** name for the shared-memory storage. In this implementation, the -** string "-index" is appended to zName and used as the name of the -** mmapped file. +** The file used to implement shared-memory is in the same directory +** as the open database file and has the same name as the open database +** file with the "-shm" suffix added. For example, if the database file +** is "/home/user1/config.db" then the file that is created and mmapped +** for shared memory will be called "/home/user1/config.db-shm". We +** experimented with using files in /dev/tmp or an some other tmpfs mount. +** But if a file in a different directory from the database file is used, +** then differing access permissions or a chroot() might cause two different +** processes on the same database to end up using different files for +** shared memory - meaning that their memory would not really be shared - +** resulting in database corruption. ** ** When opening a new shared-memory file, if no other instances of that ** file are currently open, in this process or in other processes, then @@ -3300,8 +3302,8 @@ static int unixShmOpen( int rc; /* Result code */ struct unixFile *pDbFd; /* Underlying database file */ unixInodeInfo *pInode; /* The inode of fd */ - const char *zTempDir; /* Directory for temporary files */ - int nTempDir; /* Size of the zTempDir string */ + char *zShmFilename; /* Name of the file used for SHM */ + int nShmFilename; /* Size of the SHM filename in bytes */ /* Allocate space for the new sqlite3_shm object. */ @@ -3318,23 +3320,15 @@ static int unixShmOpen( pInode = pDbFd->pInode; pShmNode = pInode->pShmNode; if( pShmNode==0 ){ - zTempDir = unixTempFileDir(1); - if( zTempDir==0 ){ - unixLeaveMutex(); - sqlite3_free(p); - return SQLITE_CANTOPEN_NOTEMPDIR; - } - nTempDir = strlen(zTempDir); - pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nTempDir + 50 ); + nShmFilename = 5 + (int)strlen(pDbFd->zPath); + pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); if( pShmNode==0 ){ rc = SQLITE_NOMEM; goto shm_open_err; } memset(pShmNode, 0, sizeof(*pShmNode)); - pShmNode->zFilename = (char*)&pShmNode[1]; - sqlite3_snprintf(nTempDir+50, pShmNode->zFilename, - "%s/sqlite-wi-%x-%x", zTempDir, - (u32)pInode->fileId.dev, (u32)pInode->fileId.ino); + zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; + sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); pShmNode->h = -1; pDbFd->pInode->pShmNode = pShmNode; pShmNode->pInode = pDbFd->pInode; @@ -3344,7 +3338,7 @@ static int unixShmOpen( goto shm_open_err; } - pShmNode->h = open(pShmNode->zFilename, O_RDWR|O_CREAT, 0664); + pShmNode->h = open(zShmFilename, O_RDWR|O_CREAT, 0664); if( pShmNode->h<0 ){ rc = SQLITE_CANTOPEN_BKPT; goto shm_open_err; @@ -4157,7 +4151,7 @@ static int openDirectory(const char *zFilename, int *pFd){ ** Return the name of a directory in which to put temporary files. ** If no suitable temporary file directory can be found, return NULL. */ -static const char *unixTempFileDir(int allowShm){ +static const char *unixTempFileDir(void){ static const char *azDirs[] = { 0, 0, @@ -4172,19 +4166,11 @@ static const char *unixTempFileDir(int allowShm){ azDirs[0] = sqlite3_temp_directory; if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); - - if( allowShm ){ - zDir = "/dev/shm"; - i = 2; /* Skip the app-defined temp locations for shared-memory */ - }else{ - zDir = azDirs[0]; - i = 1; - } - for(; i Date: Sat, 19 Jun 2010 23:53:11 +0000 Subject: [PATCH 3/3] Fix an uninitialized variable in os_unix.c. FossilOrigin-Name: 822a0283c6bc1c75001f3d1c528a4ff89c6b039c --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/os_unix.c | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/manifest b/manifest index 483d16970a..11bbba4ce7 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C Change\sthe\sunix\sVFS\sto\salways\sallocate\sshared-memory\susing\sa\sfile\sin\sthe\nsame\sdirectory\sas\sthe\sdatabase.\s\sOtherwise,\sa\schroot\smight\scause\sdifferent\nprocesses\sto\suse\sdifferent\sshared\smemory\sfiles\sresulting\sin\sdatabase\ncorruption. -D 2010-06-19T15:10:10 +C Fix\san\suninitialized\svariable\sin\sos_unix.c. +D 2010-06-19T23:53:11 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in a5cad1f8f3e021356bfcc6c77dc16f6f1952bbc3 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -157,7 +157,7 @@ F src/os.c 9c4a2f82a50306a33907678ec0187b6ad1486bfe F src/os.h d7775504a51e6e0d40315aa427b3e229ff9ff9ca F src/os_common.h a8f95b81eca8a1ab8593d23e94f8a35f35d4078f F src/os_os2.c 665876d5eec7585226b0a1cf5e18098de2b2da19 -F src/os_unix.c a76d1952ac7984574701c48b665220b871c5c9a5 +F src/os_unix.c 838e0c0cabb722ac573c8cbd666b47bdf44eb8f9 F src/os_win.c dfde7d33c446e89dd9a277c036f2c4cc564b3138 F src/pager.c 4fe451d68950002eb985e6325d666ab54956a37f F src/pager.h ca1f23c0cf137ac26f8908df2427c8b308361efd @@ -826,14 +826,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 9a949a3a5c32b8bfbb94e10e18d050ec80a25553 -R acdbeeb43afddd9408962cb351e87aec +P 2241788bc85fbc48e9cfecb95fe0a858338e37cb +R 519d2c82f8071bfeedb3eae3a50de5e0 U drh -Z 2d4cd5d2db2524a9b5137cfeafb22e1e +Z fb28c7b4632831ff9010ebcbf771882d -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFMHN3WoxKgR168RlERApgaAJ48Uha0kqQFVEwHVS03zg+AKsjJKwCfSU8L -jmliKcE06t2NitzknAIS5tM= -=X+y+ +iD8DBQFMHVhqoxKgR168RlERAl0KAJ0Yf2pBemdWub4Zh3gc9Vg3H0bRrgCfb73m +7DvKYVz1pMDVAwrfS/3PYDY= +=DvU8 -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index 42d1b1066c..3af1e5905f 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -2241788bc85fbc48e9cfecb95fe0a858338e37cb \ No newline at end of file +822a0283c6bc1c75001f3d1c528a4ff89c6b039c \ No newline at end of file diff --git a/src/os_unix.c b/src/os_unix.c index 8e3ca66847..e949c20cd4 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -4166,7 +4166,7 @@ static const char *unixTempFileDir(void){ azDirs[0] = sqlite3_temp_directory; if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); - for(i==0; i