From: dan Date: Thu, 17 Nov 2016 14:02:50 +0000 (+0000) Subject: When opening the *-shm file for a readonly database, try to open it in read-write... X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cf178fc37ef361aafa7809a39cee1c6bdcfe635c;p=thirdparty%2Fsqlite.git When opening the *-shm file for a readonly database, try to open it in read-write mode before falling back to readonly. This is in case some other read/write connection within the same process uses the same file descriptor. FossilOrigin-Name: a07c581e88aa4d9835f6144c0fd5e58ef42f14ac --- diff --git a/manifest b/manifest index 955adcec98..f8c34d5338 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\supdates\sfor\sversion\s3.15.1. -D 2016-11-04T19:09:39.851 +C When\sopening\sthe\s*-shm\sfile\sfor\sa\sreadonly\sdatabase,\stry\sto\sopen\sit\sin\sread-write\smode\sbefore\sfalling\sback\sto\sreadonly.\sThis\sis\sin\scase\ssome\sother\sread/write\sconnection\swithin\sthe\ssame\sprocess\suses\sthe\ssame\sfile\sdescriptor. +D 2016-11-17T14:02:50.490 F Makefile.in c9c70541089a9755069a9dad0b609cf14a382649 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc a8af814f63c124db048517b63a0b8650c3fc26fc @@ -374,7 +374,7 @@ F src/os.c c03b50496df5815e8f6d45bae4404b73cadf069b F src/os.h 8e976e59eb4ca1c0fca6d35ee803e38951cb0343 F src/os_common.h b2f4707a603e36811d9b1a13278bffd757857b85 F src/os_setup.h 0dbaea40a7d36bf311613d31342e0b99e2536586 -F src/os_unix.c 5f558f9433256f25d2f2dd852c7e93a84c05e1e4 +F src/os_unix.c ab6fb76945f600f7e557ca6d7c59dc23e327be70 F src/os_win.c 4224bff1904dfdf3664680897e1b5077de1fd649 F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a F src/pager.c 3fea5fb3694c36870dc18730f914a52ed838b507 @@ -1402,7 +1402,7 @@ F test/walnoshm.test 559b878f3aab838971d820329ca35f1caa7b038e F test/waloverwrite.test dad2f26567f1b45174e54fbf9a8dc1cb876a7f03 F test/walpersist.test abd956d66e2f36d2d9d05d3a969f48be6d2ddbec F test/walprotocol.test 0b92feb132ccebd855494d917d3f6c2d717ace20 -F test/walro.test 310f5f364e64eaaa2f2233229a7b602ccb038bc9 +F test/walro.test 2baad492f8f52d24143ec8a0a811dc5604d05ae9 F test/walshared.test 04590b10c677f75318701818c50bc0dda5da64ab F test/walslow.test 07a51cbe9d4895d0a90c7af76d14a62d363ac162 F test/walthread.test c13f5a12fbd9d81e58f49875dc9dd8a52a84cf03 @@ -1535,7 +1535,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 06014163475051b7f3b3570e8af434362bbc5b87 b86b79c442a58d10aa005ba4f34095375a88d242 -R bcb3efeba01d0af0aee999d16c1330b7 -U drh -Z 6f3c15f758e9fbdf4e4bd5482bd7aa1f +P 0e5ffd9123d6d2d2b8f3701e8a73cc98a3a7ff5f +R dd715a7090f8ccbb07a44c145e36e97f +U dan +Z dc2784f452c216a6da4c2c53fde33522 diff --git a/manifest.uuid b/manifest.uuid index 9c42a71763..958987df12 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -0e5ffd9123d6d2d2b8f3701e8a73cc98a3a7ff5f \ No newline at end of file +a07c581e88aa4d9835f6144c0fd5e58ef42f14ac \ No newline at end of file diff --git a/src/os_unix.c b/src/os_unix.c index b317f7ac67..3a575881c9 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -5048,20 +5048,33 @@ static int unixOpenSharedMemory(unixFile *pDbFd){ if( pInode->bProcessLock==0 ){ int openFlags = O_RDWR | O_CREAT; - if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) + int fd; /* File descriptor for *-shm file */ + if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ + openFlags = O_RDONLY; + pShmNode->isReadonly = 1; + } + fd = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); + + /* If it was not possible to open the *-shm file in read/write mode, + ** and the database file itself has been opened in read-only mode, + ** try to open the *-shm file in read-only mode as well. Even if the + ** database connection is read-only, it is still better to try opening + ** the *-shm file in read/write mode first, as the same file descriptor + ** may be also be used by a read/write database connection. */ #if defined(SQLITE_ENABLE_PERSIST_WAL)&&(SQLITE_ENABLE_LOCKING_STYLE \ || defined(__APPLE__)) - || (pDbFd->openFlags & O_RDWR) != O_RDWR -#endif - ){ - openFlags = O_RDONLY; + if( fd<0 && errno!=EISDIR && pShmNode->isReadonly==0 + && (pDbFd->openFlags & O_RDWR)!=O_RDWR + ){ + fd = robust_open(zShmFilename, O_RDONLY, (sStat.st_mode&0777)); pShmNode->isReadonly = 1; } - pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); - if( pShmNode->h<0 ){ +#endif + if( fd<0 ){ rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); goto shm_open_err; } + pShmNode->h = fd; /* If this process is running as root, make sure that the SHM file ** is owned by the same user that owns the original database. Otherwise, diff --git a/test/walro.test b/test/walro.test index 4935fda48d..ba8bc3dc44 100644 --- a/test/walro.test +++ b/test/walro.test @@ -306,4 +306,62 @@ do_multiclient_test tn { forcedelete $shmpath +#---------------------------------------------------------------------------- + +catch {db2 close} +reset_db +do_execsql_test 3.1 { + CREATE TABLE t1(a, b); + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(1, 2); +} {wal} +db_save +db close +db_restore + +sqlite3 db test.db -readonly 1 + +do_execsql_test 3.2 { SELECT * FROM t1 } {1 2} +do_catchsql_test 3.3 { + INSERT INTO t1 VALUES(3, 4) +} {1 {attempt to write a readonly database}} + +sqlite3 db2 test.db +do_test 3.4 { + db2 eval { INSERT INTO t1 VALUES(3, 4) } +} {} +do_execsql_test 3.5 { SELECT * FROM t1 } {1 2 3 4} + +db close +db2 close +db_restore +file attributes $shmpath -permissions r--r--r-- +sqlite3 db test.db -readonly 1 +do_execsql_test 3.6 { SELECT * FROM t1 } {1 2} + +db close +db_restore +file attributes $shmpath -permissions r--r--r-- +sqlite3 db test.db +do_test 3.7 { + catchsql { SELECT * FROM t1 } +} {1 {unable to open database file}} + +db close +db_restore +file attributes $shmpath -permissions r--r--r-- +sqlite3 db test.db -readonly 1 +do_execsql_test 3.8 { SELECT * FROM t1 } {1 2} + +sqlite3 db2 test.db +do_test 3.9 { db2 eval { SELECT * FROM t1 } } {1 2} +do_test 3.10 { + catchsql { INSERT INTO t1 VALUES(3, 4) } db2 +} {1 {attempt to write a readonly database}} + +catch { db close } +catch { db2 close } + +forcedelete $shmpath + finish_test