drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 13 | ** This file contains the VFS implementation for unix-like operating systems |
| 14 | ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 15 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 16 | ** There are actually several different VFS implementations in this file. |
| 17 | ** The differences are in the way that file locking is done. The default |
| 18 | ** implementation uses Posix Advisory Locks. Alternative implementations |
| 19 | ** use flock(), dot-files, various proprietary locking schemas, or simply |
| 20 | ** skip locking all together. |
| 21 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 22 | ** This source file is organized into divisions where the logic for various |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 23 | ** subfunctions is contained within the appropriate division. PLEASE |
| 24 | ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed |
| 25 | ** in the correct division and should be clearly labeled. |
| 26 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 27 | ** The layout of divisions is as follows: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 28 | ** |
| 29 | ** * General-purpose declarations and utility functions. |
| 30 | ** * Unique file ID logic used by VxWorks. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 31 | ** * Various locking primitive implementations (all except proxy locking): |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 32 | ** + for Posix Advisory Locks |
| 33 | ** + for no-op locks |
| 34 | ** + for dot-file locks |
| 35 | ** + for flock() locking |
| 36 | ** + for named semaphore locks (VxWorks only) |
| 37 | ** + for AFP filesystem locks (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 38 | ** * sqlite3_file methods not associated with locking. |
| 39 | ** * Definitions of sqlite3_io_methods objects for all locking |
| 40 | ** methods plus "finder" functions for each locking method. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 41 | ** * sqlite3_vfs method implementations. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 42 | ** * Locking primitives for the proxy uber-locking-method. (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 43 | ** * Definitions of sqlite3_vfs objects for all locking methods |
| 44 | ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 45 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 46 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 47 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 48 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 49 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 50 | ** There are various methods for file locking used for concurrency |
| 51 | ** control: |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 52 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 53 | ** 1. POSIX locking (the default), |
| 54 | ** 2. No locking, |
| 55 | ** 3. Dot-file locking, |
| 56 | ** 4. flock() locking, |
| 57 | ** 5. AFP locking (OSX only), |
| 58 | ** 6. Named POSIX semaphores (VXWorks only), |
| 59 | ** 7. proxy locking. (OSX only) |
| 60 | ** |
| 61 | ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |
| 62 | ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |
| 63 | ** selection of the appropriate locking style based on the filesystem |
| 64 | ** where the database is located. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 65 | */ |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 66 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 67 | # if defined(__APPLE__) |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 68 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 69 | # else |
| 70 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 71 | # endif |
| 72 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 73 | |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 74 | /* Use pread() and pwrite() if they are available */ |
drh | 79a2ca3 | 2016-03-04 03:14:39 +0000 | [diff] [blame] | 75 | #if defined(__APPLE__) |
| 76 | # define HAVE_PREAD 1 |
| 77 | # define HAVE_PWRITE 1 |
| 78 | #endif |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 79 | #if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64) |
| 80 | # undef USE_PREAD |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 81 | # define USE_PREAD64 1 |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 82 | #elif defined(HAVE_PREAD) && defined(HAVE_PWRITE) |
drh | 79a2ca3 | 2016-03-04 03:14:39 +0000 | [diff] [blame] | 83 | # undef USE_PREAD64 |
| 84 | # define USE_PREAD 1 |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 87 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 88 | ** standard include files. |
| 89 | */ |
| 90 | #include <sys/types.h> |
| 91 | #include <sys/stat.h> |
| 92 | #include <fcntl.h> |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 93 | #include <sys/ioctl.h> |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 94 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 95 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 96 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 97 | #include <errno.h> |
dan | 32c12fe | 2013-05-02 17:37:31 +0000 | [diff] [blame] | 98 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 99 | # include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 100 | #endif |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 101 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 102 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 103 | # include <sys/ioctl.h> |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 104 | # include <sys/file.h> |
| 105 | # include <sys/param.h> |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 106 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 107 | |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 108 | #if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ |
| 109 | (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) |
| 110 | # if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ |
| 111 | && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0)) |
| 112 | # define HAVE_GETHOSTUUID 1 |
| 113 | # else |
| 114 | # warning "gethostuuid() is disabled." |
| 115 | # endif |
| 116 | #endif |
| 117 | |
| 118 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 119 | #if OS_VXWORKS |
| 120 | # include <sys/ioctl.h> |
| 121 | # include <semaphore.h> |
| 122 | # include <limits.h> |
| 123 | #endif /* OS_VXWORKS */ |
| 124 | |
| 125 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 126 | # include <sys/mount.h> |
| 127 | #endif |
| 128 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 129 | #ifdef HAVE_UTIME |
| 130 | # include <utime.h> |
| 131 | #endif |
| 132 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 133 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 134 | ** Allowed values of unixFile.fsFlags |
| 135 | */ |
| 136 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 137 | |
| 138 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 139 | ** If we are to be thread-safe, include the pthreads header and define |
| 140 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 141 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 142 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 143 | # include <pthread.h> |
| 144 | # define SQLITE_UNIX_THREADS 1 |
| 145 | #endif |
| 146 | |
| 147 | /* |
| 148 | ** Default permissions when creating a new file |
| 149 | */ |
| 150 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 151 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 152 | #endif |
| 153 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 154 | /* |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 155 | ** Default permissions when creating auto proxy dir |
| 156 | */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 157 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 158 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 159 | #endif |
| 160 | |
| 161 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 162 | ** Maximum supported path-length. |
| 163 | */ |
| 164 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 165 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 166 | /* |
| 167 | ** Maximum supported symbolic links |
| 168 | */ |
| 169 | #define SQLITE_MAX_SYMLINKS 100 |
| 170 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 171 | /* Always cast the getpid() return type for compatibility with |
| 172 | ** kernel modules in VxWorks. */ |
| 173 | #define osGetpid(X) (pid_t)getpid() |
| 174 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 175 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 176 | ** Only set the lastErrno if the error code is a real error and not |
| 177 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 178 | */ |
| 179 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 180 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 181 | /* Forward references */ |
| 182 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 183 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 184 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 185 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 188 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 189 | ** cannot be closed immediately. In these cases, instances of the following |
| 190 | ** structure are used to store the file descriptor while waiting for an |
| 191 | ** opportunity to either close or reuse it. |
| 192 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 193 | struct UnixUnusedFd { |
| 194 | int fd; /* File descriptor to close */ |
| 195 | int flags; /* Flags this file descriptor was opened with */ |
| 196 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 197 | }; |
| 198 | |
| 199 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 200 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 201 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 202 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 203 | typedef struct unixFile unixFile; |
| 204 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 205 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 206 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 207 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 208 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 209 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 210 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 211 | int lastErrno; /* The unix errno from last I/O error */ |
| 212 | void *lockingContext; /* Locking style specific state */ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 213 | UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 214 | const char *zPath; /* Name of the file */ |
| 215 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 216 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 217 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 218 | int nFetchOut; /* Number of outstanding xFetch refs */ |
| 219 | sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 220 | sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ |
| 221 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 222 | void *pMapRegion; /* Memory mapped region */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 223 | #endif |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 224 | int sectorSize; /* Device sector size */ |
| 225 | int deviceCharacteristics; /* Precomputed device characteristics */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 226 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 227 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 228 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 229 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 230 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 231 | #endif |
| 232 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 233 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 234 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 235 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 236 | /* The next group of variables are used to track whether or not the |
| 237 | ** transaction counter in bytes 24-27 of database files are updated |
| 238 | ** whenever any part of the database changes. An assertion fault will |
| 239 | ** occur if a file is updated without also updating the transaction |
| 240 | ** counter. This test is made to avoid new problems similar to the |
| 241 | ** one described by ticket #3584. |
| 242 | */ |
| 243 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 244 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 245 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 246 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 247 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 248 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 249 | #ifdef SQLITE_TEST |
| 250 | /* In test mode, increase the size of this structure a bit so that |
| 251 | ** it is larger than the struct CrashFile defined in test6.c. |
| 252 | */ |
| 253 | char aPadding[32]; |
| 254 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 257 | /* This variable holds the process id (pid) from when the xRandomness() |
| 258 | ** method was called. If xOpen() is called from a different process id, |
| 259 | ** indicating that a fork() has occurred, the PRNG will be reset. |
| 260 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 261 | static pid_t randomnessPid = 0; |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 262 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 263 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 264 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 265 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 266 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 267 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 268 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 269 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 270 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 271 | #else |
| 272 | # define UNIXFILE_DIRSYNC 0x00 |
| 273 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 274 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 275 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 276 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 277 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 278 | |
| 279 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 280 | ** Include code that is common to all os_*.c files |
| 281 | */ |
| 282 | #include "os_common.h" |
| 283 | |
| 284 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 285 | ** Define various macros that are missing from some systems. |
| 286 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 287 | #ifndef O_LARGEFILE |
| 288 | # define O_LARGEFILE 0 |
| 289 | #endif |
| 290 | #ifdef SQLITE_DISABLE_LFS |
| 291 | # undef O_LARGEFILE |
| 292 | # define O_LARGEFILE 0 |
| 293 | #endif |
| 294 | #ifndef O_NOFOLLOW |
| 295 | # define O_NOFOLLOW 0 |
| 296 | #endif |
| 297 | #ifndef O_BINARY |
| 298 | # define O_BINARY 0 |
| 299 | #endif |
| 300 | |
| 301 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 302 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 303 | ** testing and debugging only. |
| 304 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 305 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 306 | #define threadid pthread_self() |
| 307 | #else |
| 308 | #define threadid 0 |
| 309 | #endif |
| 310 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 311 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 312 | ** HAVE_MREMAP defaults to true on Linux and false everywhere else. |
| 313 | */ |
| 314 | #if !defined(HAVE_MREMAP) |
| 315 | # if defined(__linux__) && defined(_GNU_SOURCE) |
| 316 | # define HAVE_MREMAP 1 |
| 317 | # else |
| 318 | # define HAVE_MREMAP 0 |
| 319 | # endif |
| 320 | #endif |
| 321 | |
| 322 | /* |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 323 | ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() |
| 324 | ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. |
| 325 | */ |
| 326 | #ifdef __ANDROID__ |
| 327 | # define lseek lseek64 |
| 328 | #endif |
| 329 | |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 330 | #ifdef __linux__ |
| 331 | /* |
| 332 | ** Linux-specific IOCTL magic numbers used for controlling F2FS |
| 333 | */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 334 | #define F2FS_IOCTL_MAGIC 0xf5 |
| 335 | #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1) |
| 336 | #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2) |
| 337 | #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3) |
| 338 | #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5) |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 339 | #define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32) |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 340 | #define F2FS_FEATURE_ATOMIC_WRITE 0x0004 |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 341 | #endif /* __linux__ */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 342 | |
| 343 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 344 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 345 | ** Different Unix systems declare open() in different ways. Same use |
| 346 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 347 | ** The difference is important when using a pointer to the function. |
| 348 | ** |
| 349 | ** The safest way to deal with the problem is to always use this wrapper |
| 350 | ** which always has the same well-defined interface. |
| 351 | */ |
| 352 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 353 | return open(zFile, flags, mode); |
| 354 | } |
| 355 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 356 | /* Forward reference */ |
| 357 | static int openDirectory(const char*, int*); |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 358 | static int unixGetpagesize(void); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 359 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 360 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 361 | ** Many system calls are accessed through pointer-to-functions so that |
| 362 | ** they may be overridden at runtime to facilitate fault injection during |
| 363 | ** testing and sandboxing. The following array holds the names and pointers |
| 364 | ** to all overrideable system calls. |
| 365 | */ |
| 366 | static struct unix_syscall { |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 367 | const char *zName; /* Name of the system call */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 368 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 369 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 370 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 371 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 372 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 373 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 374 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 375 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 376 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 377 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 378 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 379 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 380 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 381 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 382 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 383 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 384 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 385 | |
| 386 | /* |
| 387 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 388 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 389 | ** that always succeeds. This means that locking does not occur under |
| 390 | ** DJGPP. But it is DOS - what did you expect? |
| 391 | */ |
| 392 | #ifdef __DJGPP__ |
| 393 | { "fstat", 0, 0 }, |
| 394 | #define osFstat(a,b,c) 0 |
| 395 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 396 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 397 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 398 | #endif |
| 399 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 400 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 401 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 402 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 403 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 404 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 405 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 406 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 407 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 408 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 409 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 410 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 411 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 412 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 413 | #endif |
| 414 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 415 | |
| 416 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 417 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 418 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 419 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 420 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 421 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 422 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 423 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 424 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 425 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 426 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 427 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 428 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 429 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 430 | #endif |
| 431 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 432 | aSyscall[12].pCurrent) |
| 433 | |
| 434 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 435 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 436 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 437 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 438 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 439 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 440 | aSyscall[13].pCurrent) |
| 441 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 442 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 443 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 444 | |
| 445 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 446 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 447 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 448 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 449 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 450 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 451 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 452 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 453 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 454 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 455 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 456 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 457 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 458 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 459 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 460 | |
| 461 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 462 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 463 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 464 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 465 | { "fchown", (sqlite3_syscall_ptr)fchown, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 466 | #else |
| 467 | { "fchown", (sqlite3_syscall_ptr)0, 0 }, |
| 468 | #endif |
dan | d3eaebd | 2012-02-13 08:50:23 +0000 | [diff] [blame] | 469 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
drh | 23c4b97 | 2012-02-11 23:55:15 +0000 | [diff] [blame] | 470 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 471 | { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, |
| 472 | #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) |
| 473 | |
dan | 4dd5144 | 2013-08-26 14:30:25 +0000 | [diff] [blame] | 474 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 475 | { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, |
| 476 | #else |
| 477 | { "mmap", (sqlite3_syscall_ptr)0, 0 }, |
| 478 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 479 | #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent) |
dan | 893c0ff | 2013-03-25 19:05:07 +0000 | [diff] [blame] | 480 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 481 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 482 | { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 483 | #else |
drh | a829992 | 2016-01-08 22:31:00 +0000 | [diff] [blame] | 484 | { "munmap", (sqlite3_syscall_ptr)0, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 485 | #endif |
drh | 62be1fa | 2017-12-09 01:02:33 +0000 | [diff] [blame] | 486 | #define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 487 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 488 | #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 489 | { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, |
| 490 | #else |
| 491 | { "mremap", (sqlite3_syscall_ptr)0, 0 }, |
| 492 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 493 | #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent) |
| 494 | |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 495 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 496 | { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 497 | #else |
| 498 | { "getpagesize", (sqlite3_syscall_ptr)0, 0 }, |
| 499 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 500 | #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent) |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 501 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 502 | #if defined(HAVE_READLINK) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 503 | { "readlink", (sqlite3_syscall_ptr)readlink, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 504 | #else |
| 505 | { "readlink", (sqlite3_syscall_ptr)0, 0 }, |
| 506 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 507 | #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 508 | |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 509 | #if defined(HAVE_LSTAT) |
| 510 | { "lstat", (sqlite3_syscall_ptr)lstat, 0 }, |
| 511 | #else |
| 512 | { "lstat", (sqlite3_syscall_ptr)0, 0 }, |
| 513 | #endif |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 514 | #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 515 | |
drh | b5d013e | 2017-10-25 16:14:12 +0000 | [diff] [blame] | 516 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 517 | { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 }, |
drh | b5d013e | 2017-10-25 16:14:12 +0000 | [diff] [blame] | 518 | #else |
| 519 | { "ioctl", (sqlite3_syscall_ptr)0, 0 }, |
| 520 | #endif |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 521 | #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 522 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 523 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 524 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | ** On some systems, calls to fchown() will trigger a message in a security |
| 528 | ** log if they come from non-root processes. So avoid calling fchown() if |
| 529 | ** we are not running as root. |
| 530 | */ |
| 531 | static int robustFchown(int fd, uid_t uid, gid_t gid){ |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 532 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 533 | return osGeteuid() ? 0 : osFchown(fd,uid,gid); |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 534 | #else |
| 535 | return 0; |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 536 | #endif |
| 537 | } |
| 538 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 539 | /* |
| 540 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 541 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 542 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 543 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 544 | */ |
| 545 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 546 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 547 | const char *zName, /* Name of system call to override */ |
| 548 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 549 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 550 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 551 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 552 | |
| 553 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 554 | if( zName==0 ){ |
| 555 | /* If no zName is given, restore all system calls to their default |
| 556 | ** settings and return NULL |
| 557 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 558 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 559 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 560 | if( aSyscall[i].pDefault ){ |
| 561 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | }else{ |
| 565 | /* If zName is specified, operate on only the one system call |
| 566 | ** specified. |
| 567 | */ |
| 568 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 569 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 570 | if( aSyscall[i].pDefault==0 ){ |
| 571 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 572 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 573 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 574 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 575 | aSyscall[i].pCurrent = pNewFunc; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | return rc; |
| 581 | } |
| 582 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 583 | /* |
| 584 | ** Return the value of a system call. Return NULL if zName is not a |
| 585 | ** recognized system call name. NULL is also returned if the system call |
| 586 | ** is currently undefined. |
| 587 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 588 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 589 | sqlite3_vfs *pNotUsed, |
| 590 | const char *zName |
| 591 | ){ |
| 592 | unsigned int i; |
| 593 | |
| 594 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 595 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 596 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 597 | } |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | ** Return the name of the first system call after zName. If zName==NULL |
| 603 | ** then return the name of the first system call. Return NULL if zName |
| 604 | ** is the last system call or if zName is not the name of a valid |
| 605 | ** system call. |
| 606 | */ |
| 607 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 608 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 609 | |
| 610 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 611 | if( zName ){ |
| 612 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 613 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 616 | for(i++; i<ArraySize(aSyscall); i++){ |
| 617 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 618 | } |
| 619 | return 0; |
| 620 | } |
| 621 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 622 | /* |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 623 | ** Do not accept any file descriptor less than this value, in order to avoid |
| 624 | ** opening database file using file descriptors that are commonly used for |
| 625 | ** standard input, output, and error. |
| 626 | */ |
| 627 | #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
| 628 | # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3 |
| 629 | #endif |
| 630 | |
| 631 | /* |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 632 | ** Invoke open(). Do so multiple times, until it either succeeds or |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 633 | ** fails for some reason other than EINTR. |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 634 | ** |
| 635 | ** If the file creation mode "m" is 0 then set it to the default for |
| 636 | ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 637 | ** 0644) as modified by the system umask. If m is not 0, then |
| 638 | ** make the file creation mode be exactly m ignoring the umask. |
| 639 | ** |
| 640 | ** The m parameter will be non-zero only when creating -wal, -journal, |
| 641 | ** and -shm files. We want those files to have *exactly* the same |
| 642 | ** permissions as their original database, unadulterated by the umask. |
| 643 | ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 644 | ** transaction crashes and leaves behind hot journals, then any |
| 645 | ** process that is able to write to the database will also be able to |
| 646 | ** recover the hot journals. |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 647 | */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 648 | static int robust_open(const char *z, int f, mode_t m){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 649 | int fd; |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 650 | mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 651 | while(1){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 652 | #if defined(O_CLOEXEC) |
| 653 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 654 | #else |
| 655 | fd = osOpen(z,f,m2); |
| 656 | #endif |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 657 | if( fd<0 ){ |
| 658 | if( errno==EINTR ) continue; |
| 659 | break; |
| 660 | } |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 661 | if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 662 | osClose(fd); |
| 663 | sqlite3_log(SQLITE_WARNING, |
| 664 | "attempt to open \"%s\" as file descriptor %d", z, fd); |
| 665 | fd = -1; |
| 666 | if( osOpen("/dev/null", f, m)<0 ) break; |
| 667 | } |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 668 | if( fd>=0 ){ |
| 669 | if( m!=0 ){ |
| 670 | struct stat statbuf; |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 671 | if( osFstat(fd, &statbuf)==0 |
| 672 | && statbuf.st_size==0 |
drh | cfc1769 | 2013-03-06 01:41:53 +0000 | [diff] [blame] | 673 | && (statbuf.st_mode&0777)!=m |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 674 | ){ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 675 | osFchmod(fd, m); |
| 676 | } |
| 677 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 678 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 679 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 680 | #endif |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 681 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 682 | return fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 683 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 684 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 685 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 686 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 687 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 688 | ** vxworksFileId objects used by this file, all of which may be |
| 689 | ** shared by multiple threads. |
| 690 | ** |
| 691 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 692 | ** is held when required. This function is only used as part of assert() |
| 693 | ** statements. e.g. |
| 694 | ** |
| 695 | ** unixEnterMutex() |
| 696 | ** assert( unixMutexHeld() ); |
| 697 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 698 | */ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame^] | 699 | static sqlite3_mutex *unixBigLock = 0; |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 700 | static void unixEnterMutex(void){ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame^] | 701 | sqlite3_mutex_enter(unixBigLock); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 702 | } |
| 703 | static void unixLeaveMutex(void){ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame^] | 704 | sqlite3_mutex_leave(unixBigLock); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 705 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 706 | #ifdef SQLITE_DEBUG |
| 707 | static int unixMutexHeld(void) { |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame^] | 708 | return sqlite3_mutex_held(unixBigLock); |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 709 | } |
| 710 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 711 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 712 | |
mistachkin | fb383e9 | 2015-04-16 03:24:38 +0000 | [diff] [blame] | 713 | #ifdef SQLITE_HAVE_OS_TRACE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 714 | /* |
| 715 | ** Helper function for printing out trace information from debugging |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 716 | ** binaries. This returns the string representation of the supplied |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 717 | ** integer lock-type. |
| 718 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 719 | static const char *azFileLock(int eFileLock){ |
| 720 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 721 | case NO_LOCK: return "NONE"; |
| 722 | case SHARED_LOCK: return "SHARED"; |
| 723 | case RESERVED_LOCK: return "RESERVED"; |
| 724 | case PENDING_LOCK: return "PENDING"; |
| 725 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 726 | } |
| 727 | return "ERROR"; |
| 728 | } |
| 729 | #endif |
| 730 | |
| 731 | #ifdef SQLITE_LOCK_TRACE |
| 732 | /* |
| 733 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 734 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 735 | ** This routine is used for troubleshooting locks on multithreaded |
| 736 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 737 | ** command-line option on the compiler. This code is normally |
| 738 | ** turned off. |
| 739 | */ |
| 740 | static int lockTrace(int fd, int op, struct flock *p){ |
| 741 | char *zOpName, *zType; |
| 742 | int s; |
| 743 | int savedErrno; |
| 744 | if( op==F_GETLK ){ |
| 745 | zOpName = "GETLK"; |
| 746 | }else if( op==F_SETLK ){ |
| 747 | zOpName = "SETLK"; |
| 748 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 749 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 750 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 751 | return s; |
| 752 | } |
| 753 | if( p->l_type==F_RDLCK ){ |
| 754 | zType = "RDLCK"; |
| 755 | }else if( p->l_type==F_WRLCK ){ |
| 756 | zType = "WRLCK"; |
| 757 | }else if( p->l_type==F_UNLCK ){ |
| 758 | zType = "UNLCK"; |
| 759 | }else{ |
| 760 | assert( 0 ); |
| 761 | } |
| 762 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 763 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 764 | savedErrno = errno; |
| 765 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 766 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 767 | (int)p->l_pid, s); |
| 768 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 769 | struct flock l2; |
| 770 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 771 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 772 | if( l2.l_type==F_RDLCK ){ |
| 773 | zType = "RDLCK"; |
| 774 | }else if( l2.l_type==F_WRLCK ){ |
| 775 | zType = "WRLCK"; |
| 776 | }else if( l2.l_type==F_UNLCK ){ |
| 777 | zType = "UNLCK"; |
| 778 | }else{ |
| 779 | assert( 0 ); |
| 780 | } |
| 781 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 782 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 783 | } |
| 784 | errno = savedErrno; |
| 785 | return s; |
| 786 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 787 | #undef osFcntl |
| 788 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 789 | #endif /* SQLITE_LOCK_TRACE */ |
| 790 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 791 | /* |
| 792 | ** Retry ftruncate() calls that fail due to EINTR |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 793 | ** |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 794 | ** All calls to ftruncate() within this file should be made through |
| 795 | ** this wrapper. On the Android platform, bypassing the logic below |
| 796 | ** could lead to a corrupt database. |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 797 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 798 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 799 | int rc; |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 800 | #ifdef __ANDROID__ |
| 801 | /* On Android, ftruncate() always uses 32-bit offsets, even if |
| 802 | ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to |
dan | 524a733 | 2014-09-06 17:06:13 +0000 | [diff] [blame] | 803 | ** truncate a file to any size larger than 2GiB. Silently ignore any |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 804 | ** such attempts. */ |
| 805 | if( sz>(sqlite3_int64)0x7FFFFFFF ){ |
| 806 | rc = SQLITE_OK; |
| 807 | }else |
| 808 | #endif |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 809 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 810 | return rc; |
| 811 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 812 | |
| 813 | /* |
| 814 | ** This routine translates a standard POSIX errno code into something |
| 815 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 816 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 817 | ** and a variety of "please close the file descriptor NOW" errors into |
| 818 | ** SQLITE_IOERR |
| 819 | ** |
| 820 | ** Errors during initialization of locks, or file system support for locks, |
| 821 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 822 | */ |
| 823 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 824 | assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 825 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 826 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 827 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 828 | switch (posixError) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 829 | case EACCES: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 830 | case EAGAIN: |
| 831 | case ETIMEDOUT: |
| 832 | case EBUSY: |
| 833 | case EINTR: |
| 834 | case ENOLCK: |
| 835 | /* random NFS retry error, unless during file system support |
| 836 | * introspection, in which it actually means what it says */ |
| 837 | return SQLITE_BUSY; |
| 838 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 839 | case EPERM: |
| 840 | return SQLITE_PERM; |
| 841 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 842 | default: |
| 843 | return sqliteIOErr; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 848 | /****************************************************************************** |
| 849 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 850 | ** |
| 851 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 852 | ** the device number and the inode number. But this does not work on VxWorks. |
| 853 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 854 | ** |
| 855 | ** A pointer to an instance of the following structure can be used as a |
| 856 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 857 | ** a copy of the canonical filename. There is also a reference count. |
| 858 | ** The structure is reclaimed when the number of pointers to it drops to |
| 859 | ** zero. |
| 860 | ** |
| 861 | ** There are never very many files open at one time and lookups are not |
| 862 | ** a performance-critical path, so it is sufficient to put these |
| 863 | ** structures on a linked list. |
| 864 | */ |
| 865 | struct vxworksFileId { |
| 866 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 867 | int nRef; /* Number of references to this one */ |
| 868 | int nName; /* Length of the zCanonicalName[] string */ |
| 869 | char *zCanonicalName; /* Canonical filename */ |
| 870 | }; |
| 871 | |
| 872 | #if OS_VXWORKS |
| 873 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 874 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 875 | ** variable: |
| 876 | */ |
| 877 | static struct vxworksFileId *vxworksFileList = 0; |
| 878 | |
| 879 | /* |
| 880 | ** Simplify a filename into its canonical form |
| 881 | ** by making the following changes: |
| 882 | ** |
| 883 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 884 | ** * convert /./ into just / |
| 885 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 886 | ** |
| 887 | ** Changes are made in-place. Return the new name length. |
| 888 | ** |
| 889 | ** The original filename is in z[0..n-1]. Return the number of |
| 890 | ** characters in the simplified name. |
| 891 | */ |
| 892 | static int vxworksSimplifyName(char *z, int n){ |
| 893 | int i, j; |
| 894 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 895 | for(i=j=0; i<n; i++){ |
| 896 | if( z[i]=='/' ){ |
| 897 | if( z[i+1]=='/' ) continue; |
| 898 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 899 | i += 1; |
| 900 | continue; |
| 901 | } |
| 902 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 903 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 904 | if( j>0 ){ j--; } |
| 905 | i += 2; |
| 906 | continue; |
| 907 | } |
| 908 | } |
| 909 | z[j++] = z[i]; |
| 910 | } |
| 911 | z[j] = 0; |
| 912 | return j; |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | ** Find a unique file ID for the given absolute pathname. Return |
| 917 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 918 | ** file ID. |
| 919 | ** |
| 920 | ** The nRef field of the vxworksFileId object is incremented before |
| 921 | ** the object is returned. A new vxworksFileId object is created |
| 922 | ** and added to the global list if necessary. |
| 923 | ** |
| 924 | ** If a memory allocation error occurs, return NULL. |
| 925 | */ |
| 926 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 927 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 928 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 929 | int n; /* Length of zAbsoluteName string */ |
| 930 | |
| 931 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 932 | n = (int)strlen(zAbsoluteName); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 933 | pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 934 | if( pNew==0 ) return 0; |
| 935 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 936 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 937 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 938 | |
| 939 | /* Search for an existing entry that matching the canonical name. |
| 940 | ** If found, increment the reference count and return a pointer to |
| 941 | ** the existing file ID. |
| 942 | */ |
| 943 | unixEnterMutex(); |
| 944 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 945 | if( pCandidate->nName==n |
| 946 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 947 | ){ |
| 948 | sqlite3_free(pNew); |
| 949 | pCandidate->nRef++; |
| 950 | unixLeaveMutex(); |
| 951 | return pCandidate; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | /* No match was found. We will make a new file ID */ |
| 956 | pNew->nRef = 1; |
| 957 | pNew->nName = n; |
| 958 | pNew->pNext = vxworksFileList; |
| 959 | vxworksFileList = pNew; |
| 960 | unixLeaveMutex(); |
| 961 | return pNew; |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | ** Decrement the reference count on a vxworksFileId object. Free |
| 966 | ** the object when the reference count reaches zero. |
| 967 | */ |
| 968 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 969 | unixEnterMutex(); |
| 970 | assert( pId->nRef>0 ); |
| 971 | pId->nRef--; |
| 972 | if( pId->nRef==0 ){ |
| 973 | struct vxworksFileId **pp; |
| 974 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 975 | assert( *pp==pId ); |
| 976 | *pp = pId->pNext; |
| 977 | sqlite3_free(pId); |
| 978 | } |
| 979 | unixLeaveMutex(); |
| 980 | } |
| 981 | #endif /* OS_VXWORKS */ |
| 982 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 983 | ******************************************************************************/ |
| 984 | |
| 985 | |
| 986 | /****************************************************************************** |
| 987 | *************************** Posix Advisory Locking **************************** |
| 988 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 989 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 990 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 991 | ** sets or clears a lock, that operation overrides any prior locks set |
| 992 | ** by the same process. It does not explicitly say so, but this implies |
| 993 | ** that it overrides locks set by the same process using a different |
| 994 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 995 | ** |
| 996 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 997 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 998 | ** |
| 999 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 1000 | ** one is a hard or symbolic link to the other) then if you set |
| 1001 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 1002 | ** on fd2, it works. I would have expected the second lock to |
| 1003 | ** fail since there was already a lock on the file due to fd1. |
| 1004 | ** But not so. Since both locks came from the same process, the |
| 1005 | ** second overrides the first, even though they were on different |
| 1006 | ** file descriptors opened on different file names. |
| 1007 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1008 | ** This means that we cannot use POSIX locks to synchronize file access |
| 1009 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1010 | ** to synchronize access for threads in separate processes, but not |
| 1011 | ** threads within the same process. |
| 1012 | ** |
| 1013 | ** To work around the problem, SQLite has to manage file locks internally |
| 1014 | ** on its own. Whenever a new database is opened, we have to find the |
| 1015 | ** specific inode of the database file (the inode is determined by the |
| 1016 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 1017 | ** and check for locks already existing on that inode. When locks are |
| 1018 | ** created or removed, we have to look at our own internal record of the |
| 1019 | ** locks to see if another thread has previously set a lock on that same |
| 1020 | ** inode. |
| 1021 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1022 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 1023 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 1024 | ** canonical filename and implemented in the previous division.) |
| 1025 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1026 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1027 | ** descriptor. It is now a structure that holds the integer file |
| 1028 | ** descriptor and a pointer to a structure that describes the internal |
| 1029 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1030 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1031 | ** point to the same locking structure. The locking structure keeps |
| 1032 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 1033 | ** field that tells us its internal lock status. cnt==0 means the |
| 1034 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 1035 | ** cnt>0 means there are cnt shared locks on the file. |
| 1036 | ** |
| 1037 | ** Any attempt to lock or unlock a file first checks the locking |
| 1038 | ** structure. The fcntl() system call is only invoked to set a |
| 1039 | ** POSIX lock if the internal lock structure transitions between |
| 1040 | ** a locked and an unlocked state. |
| 1041 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1042 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1043 | ** |
| 1044 | ** If you close a file descriptor that points to a file that has locks, |
| 1045 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1046 | ** released. To work around this problem, each unixInodeInfo object |
| 1047 | ** maintains a count of the number of pending locks on tha inode. |
| 1048 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1049 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1050 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1051 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1052 | ** be closed and that list is walked (and cleared) when the last lock |
| 1053 | ** clears. |
| 1054 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1055 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1056 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1057 | ** Many older versions of linux use the LinuxThreads library which is |
| 1058 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1059 | ** A cannot be modified or overridden by a different thread B. |
| 1060 | ** Only thread A can modify the lock. Locking behavior is correct |
| 1061 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 1062 | ** on linux - with NPTL a lock created by thread A can override locks |
| 1063 | ** in thread B. But there is no way to know at compile-time which |
| 1064 | ** threading library is being used. So there is no way to know at |
| 1065 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1066 | ** One has to do a run-time check to discover the behavior of the |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1067 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1068 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1069 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 1070 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 1071 | ** LinuxThreads provided that (1) there is no more than one connection |
| 1072 | ** per database file in the same process and (2) database connections |
| 1073 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1074 | */ |
| 1075 | |
| 1076 | /* |
| 1077 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1078 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1079 | */ |
| 1080 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1081 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1082 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1083 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1084 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1085 | /* We are told that some versions of Android contain a bug that |
| 1086 | ** sizes ino_t at only 32-bits instead of 64-bits. (See |
| 1087 | ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c) |
| 1088 | ** To work around this, always allocate 64-bits for the inode number. |
| 1089 | ** On small machines that only have 32-bit inodes, this wastes 4 bytes, |
| 1090 | ** but that should not be a big deal. */ |
| 1091 | /* WAS: ino_t ino; */ |
| 1092 | u64 ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1093 | #endif |
| 1094 | }; |
| 1095 | |
| 1096 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1097 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1098 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 1099 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1100 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1101 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1102 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1103 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1104 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1105 | struct unixInodeInfo { |
| 1106 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1107 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1108 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 1109 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1110 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1111 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 1112 | int nLock; /* Number of outstanding file locks */ |
| 1113 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 1114 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 1115 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 1116 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1117 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 1118 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1119 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1120 | sem_t *pSem; /* Named POSIX semaphore */ |
| 1121 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1122 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1123 | }; |
| 1124 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1125 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1126 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1127 | */ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1128 | static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */ |
| 1129 | static unsigned int nUnusedFd = 0; /* Total unused file descriptors */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1130 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1131 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1132 | ** |
drh | aaeaa18 | 2015-11-24 15:12:47 +0000 | [diff] [blame] | 1133 | ** This function - unixLogErrorAtLine(), is only ever called via the macro |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1134 | ** unixLogError(). |
| 1135 | ** |
| 1136 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1137 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1138 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1139 | ** strerror_r(). |
| 1140 | ** |
| 1141 | ** The first argument passed to the macro should be the error code that |
| 1142 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1143 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1144 | ** failed (e.g. "unlink", "open") and the associated file-system path, |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1145 | ** if any. |
| 1146 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1147 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1148 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1149 | int errcode, /* SQLite error code */ |
| 1150 | const char *zFunc, /* Name of OS function that failed */ |
| 1151 | const char *zPath, /* File path associated with error */ |
| 1152 | int iLine /* Source line number where error occurred */ |
| 1153 | ){ |
| 1154 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1155 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1156 | |
| 1157 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1158 | ** the strerror() function to obtain the human-readable error message |
| 1159 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1160 | */ |
| 1161 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1162 | char aErr[80]; |
| 1163 | memset(aErr, 0, sizeof(aErr)); |
| 1164 | zErr = aErr; |
| 1165 | |
| 1166 | /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1167 | ** assume that the system provides the GNU version of strerror_r() that |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1168 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1169 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1170 | ** Otherwise, assume that the system provides the POSIX version of |
| 1171 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1172 | ** |
| 1173 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1174 | ** available, the error message will often be an empty string. Not a |
| 1175 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1176 | ** could lead to a segfault though. |
| 1177 | */ |
| 1178 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1179 | zErr = |
| 1180 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1181 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1182 | |
| 1183 | #elif SQLITE_THREADSAFE |
| 1184 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1185 | zErr = ""; |
| 1186 | #else |
| 1187 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1188 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1189 | #endif |
| 1190 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1191 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1192 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1193 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1194 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1195 | ); |
| 1196 | |
| 1197 | return errcode; |
| 1198 | } |
| 1199 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1200 | /* |
| 1201 | ** Close a file descriptor. |
| 1202 | ** |
| 1203 | ** We assume that close() almost always works, since it is only in a |
| 1204 | ** very sick application or on a very sick platform that it might fail. |
| 1205 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1206 | ** error. |
| 1207 | ** |
| 1208 | ** Note that it is not safe to retry close() after EINTR since the |
| 1209 | ** file descriptor might have already been reused by another thread. |
| 1210 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1211 | ** and move on. |
| 1212 | */ |
| 1213 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1214 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1215 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1216 | pFile ? pFile->zPath : 0, lineno); |
| 1217 | } |
| 1218 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1219 | |
| 1220 | /* |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 1221 | ** Set the pFile->lastErrno. Do this in a subroutine as that provides |
| 1222 | ** a convenient place to set a breakpoint. |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1223 | */ |
| 1224 | static void storeLastErrno(unixFile *pFile, int error){ |
| 1225 | pFile->lastErrno = error; |
| 1226 | } |
| 1227 | |
| 1228 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1229 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1230 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1231 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1232 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1233 | UnixUnusedFd *p; |
| 1234 | UnixUnusedFd *pNext; |
| 1235 | for(p=pInode->pUnused; p; p=pNext){ |
| 1236 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1237 | robust_close(pFile, p->fd, __LINE__); |
| 1238 | sqlite3_free(p); |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1239 | nUnusedFd--; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1240 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1241 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1245 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1246 | ** |
| 1247 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1248 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1249 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1250 | static void releaseInodeInfo(unixFile *pFile){ |
| 1251 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1252 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1253 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1254 | pInode->nRef--; |
| 1255 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1256 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1257 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1258 | if( pInode->pPrev ){ |
| 1259 | assert( pInode->pPrev->pNext==pInode ); |
| 1260 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1261 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1262 | assert( inodeList==pInode ); |
| 1263 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1264 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1265 | if( pInode->pNext ){ |
| 1266 | assert( pInode->pNext->pPrev==pInode ); |
| 1267 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1268 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1269 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1270 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1271 | } |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1272 | assert( inodeList!=0 || nUnusedFd==0 ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1276 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1277 | ** describes that file descriptor. Create a new one if necessary. The |
| 1278 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1279 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1280 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1281 | ** when this function is called. |
| 1282 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1283 | ** Return an appropriate error code. |
| 1284 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1285 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1286 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1287 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1288 | ){ |
| 1289 | int rc; /* System call return code */ |
| 1290 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1291 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1292 | struct stat statbuf; /* Low-level file information */ |
| 1293 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1294 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1295 | assert( unixMutexHeld() ); |
| 1296 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1297 | /* Get low-level information about the file that we can used to |
| 1298 | ** create a unique name for the file. |
| 1299 | */ |
| 1300 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1301 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1302 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1303 | storeLastErrno(pFile, errno); |
drh | 40fe8d3 | 2015-11-30 20:36:26 +0000 | [diff] [blame] | 1304 | #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1305 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1306 | #endif |
| 1307 | return SQLITE_IOERR; |
| 1308 | } |
| 1309 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1310 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1311 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1312 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1313 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1314 | ** we always increase the file size to 1 by writing a single byte |
| 1315 | ** prior to accessing the inode number. The one byte written is |
| 1316 | ** an ASCII 'S' character which also happens to be the first byte |
| 1317 | ** in the header of every SQLite database. In this way, if there |
| 1318 | ** is a race condition such that another thread has already populated |
| 1319 | ** the first page of the database, no damage is done. |
| 1320 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1321 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1322 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1323 | if( rc!=1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1324 | storeLastErrno(pFile, errno); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1325 | return SQLITE_IOERR; |
| 1326 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1327 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1328 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1329 | storeLastErrno(pFile, errno); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1330 | return SQLITE_IOERR; |
| 1331 | } |
| 1332 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1333 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1334 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1335 | memset(&fileId, 0, sizeof(fileId)); |
| 1336 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1337 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1338 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1339 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1340 | fileId.ino = (u64)statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1341 | #endif |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1342 | assert( inodeList!=0 || nUnusedFd==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1343 | pInode = inodeList; |
| 1344 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1345 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1346 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1347 | if( pInode==0 ){ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 1348 | pInode = sqlite3_malloc64( sizeof(*pInode) ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1349 | if( pInode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 1350 | return SQLITE_NOMEM_BKPT; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1351 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1352 | memset(pInode, 0, sizeof(*pInode)); |
| 1353 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1354 | pInode->nRef = 1; |
| 1355 | pInode->pNext = inodeList; |
| 1356 | pInode->pPrev = 0; |
| 1357 | if( inodeList ) inodeList->pPrev = pInode; |
| 1358 | inodeList = pInode; |
| 1359 | }else{ |
| 1360 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1361 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1362 | *ppInode = pInode; |
| 1363 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1364 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1365 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1366 | /* |
| 1367 | ** Return TRUE if pFile has been renamed or unlinked since it was first opened. |
| 1368 | */ |
| 1369 | static int fileHasMoved(unixFile *pFile){ |
drh | 61ffea5 | 2014-08-12 12:19:25 +0000 | [diff] [blame] | 1370 | #if OS_VXWORKS |
| 1371 | return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; |
| 1372 | #else |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1373 | struct stat buf; |
| 1374 | return pFile->pInode!=0 && |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1375 | (osStat(pFile->zPath, &buf)!=0 |
| 1376 | || (u64)buf.st_ino!=pFile->pInode->fileId.ino); |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 1377 | #endif |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1380 | |
| 1381 | /* |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1382 | ** Check a unixFile that is a database. Verify the following: |
| 1383 | ** |
| 1384 | ** (1) There is exactly one hard link on the file |
| 1385 | ** (2) The file is not a symbolic link |
| 1386 | ** (3) The file has not been renamed or unlinked |
| 1387 | ** |
| 1388 | ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. |
| 1389 | */ |
| 1390 | static void verifyDbFile(unixFile *pFile){ |
| 1391 | struct stat buf; |
| 1392 | int rc; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 1393 | |
| 1394 | /* These verifications occurs for the main database only */ |
| 1395 | if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return; |
| 1396 | |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1397 | rc = osFstat(pFile->h, &buf); |
| 1398 | if( rc!=0 ){ |
| 1399 | sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1400 | return; |
| 1401 | } |
drh | 6369bc3 | 2016-03-21 16:06:42 +0000 | [diff] [blame] | 1402 | if( buf.st_nlink==0 ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1403 | sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1404 | return; |
| 1405 | } |
| 1406 | if( buf.st_nlink>1 ){ |
| 1407 | sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1408 | return; |
| 1409 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1410 | if( fileHasMoved(pFile) ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1411 | sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1412 | return; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1418 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1419 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1420 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1421 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1422 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1423 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1424 | int rc = SQLITE_OK; |
| 1425 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1426 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1427 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1428 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1429 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1430 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1431 | assert( pFile->eFileLock<=SHARED_LOCK ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1432 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1433 | |
| 1434 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1435 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1436 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1439 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1440 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1441 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1442 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1443 | struct flock lock; |
| 1444 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1445 | lock.l_start = RESERVED_BYTE; |
| 1446 | lock.l_len = 1; |
| 1447 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1448 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1449 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1450 | storeLastErrno(pFile, errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1451 | } else if( lock.l_type!=F_UNLCK ){ |
| 1452 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1453 | } |
| 1454 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1455 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1456 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1457 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1458 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1459 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1460 | *pResOut = reserved; |
| 1461 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1465 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1466 | ** described by pLock. |
| 1467 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1468 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1469 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1470 | ** the first time any lock is attempted. All subsequent system locking |
| 1471 | ** operations become no-ops. Locking operations still happen internally, |
| 1472 | ** in order to coordinate access between separate database connections |
| 1473 | ** within this process, but all of that is handled in memory and the |
| 1474 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1475 | ** |
| 1476 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1477 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1478 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1479 | ** |
| 1480 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1481 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1482 | */ |
| 1483 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1484 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1485 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1486 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1487 | assert( pInode!=0 ); |
drh | 50358ad | 2015-12-02 01:04:33 +0000 | [diff] [blame] | 1488 | if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1489 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1490 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1491 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1492 | lock.l_whence = SEEK_SET; |
| 1493 | lock.l_start = SHARED_FIRST; |
| 1494 | lock.l_len = SHARED_SIZE; |
| 1495 | lock.l_type = F_WRLCK; |
| 1496 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1497 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1498 | pInode->bProcessLock = 1; |
| 1499 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1500 | }else{ |
| 1501 | rc = 0; |
| 1502 | } |
| 1503 | }else{ |
| 1504 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1505 | } |
| 1506 | return rc; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1510 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1511 | ** of the following: |
| 1512 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1513 | ** (1) SHARED_LOCK |
| 1514 | ** (2) RESERVED_LOCK |
| 1515 | ** (3) PENDING_LOCK |
| 1516 | ** (4) EXCLUSIVE_LOCK |
| 1517 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1518 | ** Sometimes when requesting one lock state, additional lock states |
| 1519 | ** are inserted in between. The locking might fail on one of the later |
| 1520 | ** transitions leaving the lock state different from what it started but |
| 1521 | ** still short of its goal. The following chart shows the allowed |
| 1522 | ** transitions and the inserted intermediate states: |
| 1523 | ** |
| 1524 | ** UNLOCKED -> SHARED |
| 1525 | ** SHARED -> RESERVED |
| 1526 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1527 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1528 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1529 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1530 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1531 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1532 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1533 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1534 | /* The following describes the implementation of the various locks and |
| 1535 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1536 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1537 | ** confusion with SQLite lock names). The algorithms are complicated |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1538 | ** slightly in order to be compatible with Windows95 systems simultaneously |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1539 | ** accessing the same database file, in case that is ever required. |
| 1540 | ** |
| 1541 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1542 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1543 | ** range', a range of 510 bytes at a well known offset. |
| 1544 | ** |
| 1545 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1546 | ** byte'. If this is successful, 'shared byte range' is read-locked |
| 1547 | ** and the lock on the 'pending byte' released. (Legacy note: When |
| 1548 | ** SQLite was first developed, Windows95 systems were still very common, |
| 1549 | ** and Widnows95 lacks a shared-lock capability. So on Windows95, a |
| 1550 | ** single randomly selected by from the 'shared byte range' is locked. |
| 1551 | ** Windows95 is now pretty much extinct, but this work-around for the |
| 1552 | ** lack of shared-locks on Windows95 lives on, for backwards |
| 1553 | ** compatibility.) |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1554 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1555 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1556 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1557 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1558 | ** |
| 1559 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1560 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1561 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1562 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1563 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1564 | ** This property is used by the algorithm for rolling back a journal file |
| 1565 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1566 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1567 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1568 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1569 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1570 | ** within this range, this ensures that no other locks are held on the |
| 1571 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1572 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1573 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1574 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1575 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1576 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1577 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1578 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1579 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1580 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1581 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 1582 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1583 | osGetpid(0))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1584 | |
| 1585 | /* If there is already a lock of this type or more restrictive on the |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1586 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1587 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1588 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1589 | if( pFile->eFileLock>=eFileLock ){ |
| 1590 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1591 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1592 | return SQLITE_OK; |
| 1593 | } |
| 1594 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1595 | /* Make sure the locking sequence is correct. |
| 1596 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1597 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1598 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1599 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1600 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1601 | assert( eFileLock!=PENDING_LOCK ); |
| 1602 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1603 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1604 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1605 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1606 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1607 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1608 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1609 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1610 | ** handle that precludes the requested lock, return BUSY. |
| 1611 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1612 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1613 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1614 | ){ |
| 1615 | rc = SQLITE_BUSY; |
| 1616 | goto end_lock; |
| 1617 | } |
| 1618 | |
| 1619 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1620 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1621 | ** return SQLITE_OK. |
| 1622 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1623 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1624 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1625 | assert( eFileLock==SHARED_LOCK ); |
| 1626 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1627 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1628 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1629 | pInode->nShared++; |
| 1630 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1631 | goto end_lock; |
| 1632 | } |
| 1633 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1634 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1635 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1636 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1637 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1638 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1639 | lock.l_len = 1L; |
| 1640 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1641 | if( eFileLock==SHARED_LOCK |
| 1642 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1643 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1644 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1645 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1646 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1647 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1648 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1649 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1650 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1651 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1652 | goto end_lock; |
| 1653 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | |
| 1657 | /* If control gets to this point, then actually go ahead and make |
| 1658 | ** operating system calls for the specified lock. |
| 1659 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1660 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1661 | assert( pInode->nShared==0 ); |
| 1662 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1663 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1664 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1665 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1666 | lock.l_start = SHARED_FIRST; |
| 1667 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1668 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1669 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1670 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1671 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1672 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1673 | /* Drop the temporary PENDING lock */ |
| 1674 | lock.l_start = PENDING_BYTE; |
| 1675 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1676 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1677 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1678 | /* This could happen with a network mount */ |
| 1679 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1680 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1681 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1682 | |
| 1683 | if( rc ){ |
| 1684 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1685 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1686 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1687 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1688 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1689 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1690 | pInode->nLock++; |
| 1691 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1692 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1693 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1694 | /* We are trying for an exclusive lock but another thread in this |
| 1695 | ** same process is still holding a shared lock. */ |
| 1696 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1697 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1698 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1699 | ** assumed that there is a SHARED or greater lock on the file |
| 1700 | ** already. |
| 1701 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1702 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1703 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1704 | |
| 1705 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1706 | if( eFileLock==RESERVED_LOCK ){ |
| 1707 | lock.l_start = RESERVED_BYTE; |
| 1708 | lock.l_len = 1L; |
| 1709 | }else{ |
| 1710 | lock.l_start = SHARED_FIRST; |
| 1711 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1712 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1713 | |
| 1714 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1715 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1716 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1717 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1718 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1719 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1720 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1721 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1722 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1723 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1724 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1725 | /* Set up the transaction-counter change checking flags when |
| 1726 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1727 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1728 | ** write operation (not a hot journal rollback). |
| 1729 | */ |
| 1730 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1731 | && pFile->eFileLock<=SHARED_LOCK |
| 1732 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1733 | ){ |
| 1734 | pFile->transCntrChng = 0; |
| 1735 | pFile->dbUpdate = 0; |
| 1736 | pFile->inNormalWrite = 1; |
| 1737 | } |
| 1738 | #endif |
| 1739 | |
| 1740 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1741 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1742 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1743 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1744 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1745 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1746 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1747 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1748 | |
| 1749 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1750 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1751 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1752 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1753 | return rc; |
| 1754 | } |
| 1755 | |
| 1756 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1757 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1758 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1759 | */ |
| 1760 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1761 | unixInodeInfo *pInode = pFile->pInode; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1762 | UnixUnusedFd *p = pFile->pPreallocatedUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1763 | p->pNext = pInode->pUnused; |
| 1764 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1765 | pFile->h = -1; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1766 | pFile->pPreallocatedUnused = 0; |
| 1767 | nUnusedFd++; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1771 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1772 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1773 | ** |
| 1774 | ** If the locking level of the file descriptor is already at or below |
| 1775 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1776 | ** |
| 1777 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1778 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1779 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1780 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1781 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1782 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1783 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1784 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1785 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1786 | struct flock lock; |
| 1787 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1788 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1789 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1790 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1791 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1792 | osGetpid(0))); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1793 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1794 | assert( eFileLock<=SHARED_LOCK ); |
| 1795 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1796 | return SQLITE_OK; |
| 1797 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1798 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1799 | pInode = pFile->pInode; |
| 1800 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1801 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1802 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1803 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1804 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1805 | /* When reducing a lock such that other processes can start |
| 1806 | ** reading the database file again, make sure that the |
| 1807 | ** transaction counter was updated if any part of the database |
| 1808 | ** file changed. If the transaction counter is not updated, |
| 1809 | ** other connections to the same file might not realize that |
| 1810 | ** the file has changed and hence might not know to flush their |
| 1811 | ** cache. The use of a stale cache can lead to database corruption. |
| 1812 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1813 | pFile->inNormalWrite = 0; |
| 1814 | #endif |
| 1815 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1816 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1817 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1818 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1819 | ** write lock until the rest is covered by a read lock: |
| 1820 | ** 1: [WWWWW] |
| 1821 | ** 2: [....W] |
| 1822 | ** 3: [RRRRW] |
| 1823 | ** 4: [RRRR.] |
| 1824 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1825 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1826 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1827 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1828 | assert( handleNFSUnlock==0 ); |
| 1829 | #endif |
| 1830 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1831 | if( handleNFSUnlock ){ |
drh | a712b4b | 2015-02-19 16:12:04 +0000 | [diff] [blame] | 1832 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1833 | off_t divSize = SHARED_SIZE - 1; |
| 1834 | |
| 1835 | lock.l_type = F_UNLCK; |
| 1836 | lock.l_whence = SEEK_SET; |
| 1837 | lock.l_start = SHARED_FIRST; |
| 1838 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1839 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1840 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1841 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1842 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1843 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1844 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1845 | lock.l_type = F_RDLCK; |
| 1846 | lock.l_whence = SEEK_SET; |
| 1847 | lock.l_start = SHARED_FIRST; |
| 1848 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1849 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1850 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1851 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1852 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1853 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1854 | } |
| 1855 | goto end_unlock; |
| 1856 | } |
| 1857 | lock.l_type = F_UNLCK; |
| 1858 | lock.l_whence = SEEK_SET; |
| 1859 | lock.l_start = SHARED_FIRST+divSize; |
| 1860 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1861 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1862 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1863 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1864 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1865 | goto end_unlock; |
| 1866 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1867 | }else |
| 1868 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1869 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1870 | lock.l_type = F_RDLCK; |
| 1871 | lock.l_whence = SEEK_SET; |
| 1872 | lock.l_start = SHARED_FIRST; |
| 1873 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1874 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1875 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1876 | ** process is holding an incompatible lock. If it does, this |
| 1877 | ** indicates that the other process is not following the locking |
| 1878 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1879 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1880 | ** an assert to fail). */ |
| 1881 | rc = SQLITE_IOERR_RDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1882 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1883 | goto end_unlock; |
| 1884 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1885 | } |
| 1886 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1887 | lock.l_type = F_UNLCK; |
| 1888 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1889 | lock.l_start = PENDING_BYTE; |
| 1890 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1891 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1892 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1893 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1894 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1895 | storeLastErrno(pFile, errno); |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1896 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1897 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1898 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1899 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1900 | /* Decrement the shared lock counter. Release the lock using an |
| 1901 | ** OS call only when all threads in this same process have released |
| 1902 | ** the lock. |
| 1903 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1904 | pInode->nShared--; |
| 1905 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1906 | lock.l_type = F_UNLCK; |
| 1907 | lock.l_whence = SEEK_SET; |
| 1908 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1909 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1910 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1911 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1912 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1913 | storeLastErrno(pFile, errno); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1914 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1915 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1916 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1919 | /* Decrement the count of locks against this same file. When the |
| 1920 | ** count reaches zero, close any other file descriptors whose close |
| 1921 | ** was deferred because of outstanding locks. |
| 1922 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1923 | pInode->nLock--; |
| 1924 | assert( pInode->nLock>=0 ); |
| 1925 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1926 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1927 | } |
| 1928 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1929 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1930 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1931 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1932 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1933 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1937 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1938 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1939 | ** |
| 1940 | ** If the locking level of the file descriptor is already at or below |
| 1941 | ** the requested locking level, this routine is a no-op. |
| 1942 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1943 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1944 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | a1afc74 | 2013-03-25 13:50:49 +0000 | [diff] [blame] | 1945 | assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1946 | #endif |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1947 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1950 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1951 | static int unixMapfile(unixFile *pFd, i64 nByte); |
| 1952 | static void unixUnmapfile(unixFile *pFd); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1953 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1954 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1955 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1956 | ** This function performs the parts of the "close file" operation |
| 1957 | ** common to all locking schemes. It closes the directory and file |
| 1958 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1959 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1960 | ** |
| 1961 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1962 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1963 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1964 | */ |
| 1965 | static int closeUnixFile(sqlite3_file *id){ |
| 1966 | unixFile *pFile = (unixFile*)id; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1967 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1968 | unixUnmapfile(pFile); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1969 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1970 | if( pFile->h>=0 ){ |
| 1971 | robust_close(pFile, pFile->h, __LINE__); |
| 1972 | pFile->h = -1; |
| 1973 | } |
| 1974 | #if OS_VXWORKS |
| 1975 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 1976 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 1977 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1978 | } |
| 1979 | vxworksReleaseFileId(pFile->pId); |
| 1980 | pFile->pId = 0; |
| 1981 | } |
| 1982 | #endif |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 1983 | #ifdef SQLITE_UNLINK_AFTER_CLOSE |
| 1984 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
| 1985 | osUnlink(pFile->zPath); |
| 1986 | sqlite3_free(*(char**)&pFile->zPath); |
| 1987 | pFile->zPath = 0; |
| 1988 | } |
| 1989 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1990 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1991 | OpenCounter(-1); |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1992 | sqlite3_free(pFile->pPreallocatedUnused); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1993 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1994 | return SQLITE_OK; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1998 | ** Close a file. |
| 1999 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2000 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2001 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2002 | unixFile *pFile = (unixFile *)id; |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 2003 | verifyDbFile(pFile); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2004 | unixUnlock(id, NO_LOCK); |
| 2005 | unixEnterMutex(); |
| 2006 | |
| 2007 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 2008 | ** routine (e.g. nolockClose()) would be called instead. |
| 2009 | */ |
| 2010 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 2011 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 2012 | /* If there are outstanding locks, do not actually close the file just |
| 2013 | ** yet because that would clear those locks. Instead, add the file |
| 2014 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 2015 | ** when the last lock is cleared. |
| 2016 | */ |
| 2017 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2018 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2019 | releaseInodeInfo(pFile); |
| 2020 | rc = closeUnixFile(id); |
| 2021 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2022 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2025 | /************** End of the posix advisory lock implementation ***************** |
| 2026 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2027 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2028 | /****************************************************************************** |
| 2029 | ****************************** No-op Locking ********************************** |
| 2030 | ** |
| 2031 | ** Of the various locking implementations available, this is by far the |
| 2032 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 2033 | ** file for reading or writing. |
| 2034 | ** |
| 2035 | ** This locking mode is appropriate for use on read-only databases |
| 2036 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 2037 | ** also be used if the application employs some external mechanism to |
| 2038 | ** prevent simultaneous access of the same database by two or more |
| 2039 | ** database connections. But there is a serious risk of database |
| 2040 | ** corruption if this locking mode is used in situations where multiple |
| 2041 | ** database connections are accessing the same database file at the same |
| 2042 | ** time and one or more of those connections are writing. |
| 2043 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2044 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2045 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 2046 | UNUSED_PARAMETER(NotUsed); |
| 2047 | *pResOut = 0; |
| 2048 | return SQLITE_OK; |
| 2049 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2050 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2051 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2052 | return SQLITE_OK; |
| 2053 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2054 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2055 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2056 | return SQLITE_OK; |
| 2057 | } |
| 2058 | |
| 2059 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2060 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2061 | */ |
| 2062 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2063 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | /******************* End of the no-op lock implementation ********************* |
| 2067 | ******************************************************************************/ |
| 2068 | |
| 2069 | /****************************************************************************** |
| 2070 | ************************* Begin dot-file Locking ****************************** |
| 2071 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2072 | ** The dotfile locking implementation uses the existence of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2073 | ** files (really a directory) to control access to the database. This works |
| 2074 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2075 | ** |
| 2076 | ** (1) There is zero concurrency. A single reader blocks all other |
| 2077 | ** connections from reading or writing the database. |
| 2078 | ** |
| 2079 | ** (2) An application crash or power loss can leave stale lock files |
| 2080 | ** sitting around that need to be cleared manually. |
| 2081 | ** |
| 2082 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 2083 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2084 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2085 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 2086 | ** the database and with the same name but with a ".lock" extension added. |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2087 | ** The existence of a lock directory implies an EXCLUSIVE lock. All other |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2088 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2089 | */ |
| 2090 | |
| 2091 | /* |
| 2092 | ** The file suffix added to the data base filename in order to create the |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2093 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2094 | */ |
| 2095 | #define DOTLOCK_SUFFIX ".lock" |
| 2096 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2097 | /* |
| 2098 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2099 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2100 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2101 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2102 | ** |
| 2103 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 2104 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 2105 | ** is held on the file and false if the file is unlocked. |
| 2106 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2107 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2108 | int rc = SQLITE_OK; |
| 2109 | int reserved = 0; |
| 2110 | unixFile *pFile = (unixFile*)id; |
| 2111 | |
| 2112 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2113 | |
| 2114 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2115 | reserved = osAccess((const char*)pFile->lockingContext, 0)==0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2116 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2117 | *pResOut = reserved; |
| 2118 | return rc; |
| 2119 | } |
| 2120 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2121 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2122 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2123 | ** of the following: |
| 2124 | ** |
| 2125 | ** (1) SHARED_LOCK |
| 2126 | ** (2) RESERVED_LOCK |
| 2127 | ** (3) PENDING_LOCK |
| 2128 | ** (4) EXCLUSIVE_LOCK |
| 2129 | ** |
| 2130 | ** Sometimes when requesting one lock state, additional lock states |
| 2131 | ** are inserted in between. The locking might fail on one of the later |
| 2132 | ** transitions leaving the lock state different from what it started but |
| 2133 | ** still short of its goal. The following chart shows the allowed |
| 2134 | ** transitions and the inserted intermediate states: |
| 2135 | ** |
| 2136 | ** UNLOCKED -> SHARED |
| 2137 | ** SHARED -> RESERVED |
| 2138 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2139 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2140 | ** PENDING -> EXCLUSIVE |
| 2141 | ** |
| 2142 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2143 | ** routine to lower a locking level. |
| 2144 | ** |
| 2145 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 2146 | ** But we track the other locking levels internally. |
| 2147 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2148 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2149 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2150 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2151 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2152 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2153 | |
| 2154 | /* If we have any lock, then the lock file already exists. All we have |
| 2155 | ** to do is adjust our internal record of the lock level. |
| 2156 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2157 | if( pFile->eFileLock > NO_LOCK ){ |
| 2158 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2159 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 2160 | #ifdef HAVE_UTIME |
| 2161 | utime(zLockFile, NULL); |
| 2162 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2163 | utimes(zLockFile, NULL); |
| 2164 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2165 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2169 | rc = osMkdir(zLockFile, 0777); |
| 2170 | if( rc<0 ){ |
| 2171 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2172 | int tErrno = errno; |
| 2173 | if( EEXIST == tErrno ){ |
| 2174 | rc = SQLITE_BUSY; |
| 2175 | } else { |
| 2176 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2177 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2178 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2179 | } |
| 2180 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2181 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2182 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2183 | |
| 2184 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2185 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2186 | return rc; |
| 2187 | } |
| 2188 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2189 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2190 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2191 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2192 | ** |
| 2193 | ** If the locking level of the file descriptor is already at or below |
| 2194 | ** the requested locking level, this routine is a no-op. |
| 2195 | ** |
| 2196 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2197 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2198 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2199 | unixFile *pFile = (unixFile*)id; |
| 2200 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2201 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2202 | |
| 2203 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2204 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2205 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2206 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2207 | |
| 2208 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2209 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2210 | return SQLITE_OK; |
| 2211 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2212 | |
| 2213 | /* To downgrade to shared, simply update our internal notion of the |
| 2214 | ** lock state. No need to mess with the file on disk. |
| 2215 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2216 | if( eFileLock==SHARED_LOCK ){ |
| 2217 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2218 | return SQLITE_OK; |
| 2219 | } |
| 2220 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2221 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2222 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2223 | rc = osRmdir(zLockFile); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2224 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2225 | int tErrno = errno; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2226 | if( tErrno==ENOENT ){ |
| 2227 | rc = SQLITE_OK; |
| 2228 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2229 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2230 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2231 | } |
| 2232 | return rc; |
| 2233 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2234 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2235 | return SQLITE_OK; |
| 2236 | } |
| 2237 | |
| 2238 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2239 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2240 | */ |
| 2241 | static int dotlockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2242 | unixFile *pFile = (unixFile*)id; |
| 2243 | assert( id!=0 ); |
| 2244 | dotlockUnlock(id, NO_LOCK); |
| 2245 | sqlite3_free(pFile->lockingContext); |
| 2246 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2247 | } |
| 2248 | /****************** End of the dot-file lock implementation ******************* |
| 2249 | ******************************************************************************/ |
| 2250 | |
| 2251 | /****************************************************************************** |
| 2252 | ************************** Begin flock Locking ******************************** |
| 2253 | ** |
| 2254 | ** Use the flock() system call to do file locking. |
| 2255 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2256 | ** flock() locking is like dot-file locking in that the various |
| 2257 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2258 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2259 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2260 | ** still works when you do this, but concurrency is reduced since |
| 2261 | ** only a single process can be reading the database at a time. |
| 2262 | ** |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2263 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2264 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2265 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2266 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2267 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2268 | ** Retry flock() calls that fail with EINTR |
| 2269 | */ |
| 2270 | #ifdef EINTR |
| 2271 | static int robust_flock(int fd, int op){ |
| 2272 | int rc; |
| 2273 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2274 | return rc; |
| 2275 | } |
| 2276 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2277 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2278 | #endif |
| 2279 | |
| 2280 | |
| 2281 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2282 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2283 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2284 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2285 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2286 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2287 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2288 | int rc = SQLITE_OK; |
| 2289 | int reserved = 0; |
| 2290 | unixFile *pFile = (unixFile*)id; |
| 2291 | |
| 2292 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2293 | |
| 2294 | assert( pFile ); |
| 2295 | |
| 2296 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2297 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2298 | reserved = 1; |
| 2299 | } |
| 2300 | |
| 2301 | /* Otherwise see if some other process holds it. */ |
| 2302 | if( !reserved ){ |
| 2303 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2304 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2305 | if( !lrc ){ |
| 2306 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2307 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2308 | if ( lrc ) { |
| 2309 | int tErrno = errno; |
| 2310 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2311 | lrc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2312 | storeLastErrno(pFile, tErrno); |
| 2313 | rc = lrc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2314 | } |
| 2315 | } else { |
| 2316 | int tErrno = errno; |
| 2317 | reserved = 1; |
| 2318 | /* someone else might have it reserved */ |
| 2319 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2320 | if( IS_LOCK_ERROR(lrc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2321 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2322 | rc = lrc; |
| 2323 | } |
| 2324 | } |
| 2325 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2326 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2327 | |
| 2328 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 2329 | if( (rc & 0xff) == SQLITE_IOERR ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2330 | rc = SQLITE_OK; |
| 2331 | reserved=1; |
| 2332 | } |
| 2333 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2334 | *pResOut = reserved; |
| 2335 | return rc; |
| 2336 | } |
| 2337 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2338 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2339 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2340 | ** of the following: |
| 2341 | ** |
| 2342 | ** (1) SHARED_LOCK |
| 2343 | ** (2) RESERVED_LOCK |
| 2344 | ** (3) PENDING_LOCK |
| 2345 | ** (4) EXCLUSIVE_LOCK |
| 2346 | ** |
| 2347 | ** Sometimes when requesting one lock state, additional lock states |
| 2348 | ** are inserted in between. The locking might fail on one of the later |
| 2349 | ** transitions leaving the lock state different from what it started but |
| 2350 | ** still short of its goal. The following chart shows the allowed |
| 2351 | ** transitions and the inserted intermediate states: |
| 2352 | ** |
| 2353 | ** UNLOCKED -> SHARED |
| 2354 | ** SHARED -> RESERVED |
| 2355 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2356 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2357 | ** PENDING -> EXCLUSIVE |
| 2358 | ** |
| 2359 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2360 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2361 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2362 | ** access the file. |
| 2363 | ** |
| 2364 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2365 | ** routine to lower a locking level. |
| 2366 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2367 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2368 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2369 | unixFile *pFile = (unixFile*)id; |
| 2370 | |
| 2371 | assert( pFile ); |
| 2372 | |
| 2373 | /* if we already have a lock, it is exclusive. |
| 2374 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2375 | if (pFile->eFileLock > NO_LOCK) { |
| 2376 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2377 | return SQLITE_OK; |
| 2378 | } |
| 2379 | |
| 2380 | /* grab an exclusive lock */ |
| 2381 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2382 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2383 | int tErrno = errno; |
| 2384 | /* didn't get, must be busy */ |
| 2385 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2386 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2387 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2388 | } |
| 2389 | } else { |
| 2390 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2391 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2392 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2393 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2394 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2395 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 2396 | if( (rc & 0xff) == SQLITE_IOERR ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2397 | rc = SQLITE_BUSY; |
| 2398 | } |
| 2399 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2400 | return rc; |
| 2401 | } |
| 2402 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2403 | |
| 2404 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2405 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2406 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2407 | ** |
| 2408 | ** If the locking level of the file descriptor is already at or below |
| 2409 | ** the requested locking level, this routine is a no-op. |
| 2410 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2411 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2412 | unixFile *pFile = (unixFile*)id; |
| 2413 | |
| 2414 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2415 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2416 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2417 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2418 | |
| 2419 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2420 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2421 | return SQLITE_OK; |
| 2422 | } |
| 2423 | |
| 2424 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2425 | if (eFileLock==SHARED_LOCK) { |
| 2426 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2427 | return SQLITE_OK; |
| 2428 | } |
| 2429 | |
| 2430 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2431 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2432 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2433 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2434 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2435 | return SQLITE_IOERR_UNLOCK; |
| 2436 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2437 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2438 | return SQLITE_OK; |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | /* |
| 2443 | ** Close a file. |
| 2444 | */ |
| 2445 | static int flockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2446 | assert( id!=0 ); |
| 2447 | flockUnlock(id, NO_LOCK); |
| 2448 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2452 | |
| 2453 | /******************* End of the flock lock implementation ********************* |
| 2454 | ******************************************************************************/ |
| 2455 | |
| 2456 | /****************************************************************************** |
| 2457 | ************************ Begin Named Semaphore Locking ************************ |
| 2458 | ** |
| 2459 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2460 | ** |
| 2461 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2462 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2463 | ** the database file at a time. This reduces potential concurrency, but |
| 2464 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2465 | */ |
| 2466 | #if OS_VXWORKS |
| 2467 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2468 | /* |
| 2469 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2470 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2471 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2472 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2473 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2474 | static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2475 | int rc = SQLITE_OK; |
| 2476 | int reserved = 0; |
| 2477 | unixFile *pFile = (unixFile*)id; |
| 2478 | |
| 2479 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2480 | |
| 2481 | assert( pFile ); |
| 2482 | |
| 2483 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2484 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2485 | reserved = 1; |
| 2486 | } |
| 2487 | |
| 2488 | /* Otherwise see if some other process holds it. */ |
| 2489 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2490 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2491 | |
| 2492 | if( sem_trywait(pSem)==-1 ){ |
| 2493 | int tErrno = errno; |
| 2494 | if( EAGAIN != tErrno ){ |
| 2495 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2496 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2497 | } else { |
| 2498 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2499 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2500 | } |
| 2501 | }else{ |
| 2502 | /* we could have it if we want it */ |
| 2503 | sem_post(pSem); |
| 2504 | } |
| 2505 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2506 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2507 | |
| 2508 | *pResOut = reserved; |
| 2509 | return rc; |
| 2510 | } |
| 2511 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2512 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2513 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2514 | ** of the following: |
| 2515 | ** |
| 2516 | ** (1) SHARED_LOCK |
| 2517 | ** (2) RESERVED_LOCK |
| 2518 | ** (3) PENDING_LOCK |
| 2519 | ** (4) EXCLUSIVE_LOCK |
| 2520 | ** |
| 2521 | ** Sometimes when requesting one lock state, additional lock states |
| 2522 | ** are inserted in between. The locking might fail on one of the later |
| 2523 | ** transitions leaving the lock state different from what it started but |
| 2524 | ** still short of its goal. The following chart shows the allowed |
| 2525 | ** transitions and the inserted intermediate states: |
| 2526 | ** |
| 2527 | ** UNLOCKED -> SHARED |
| 2528 | ** SHARED -> RESERVED |
| 2529 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2530 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2531 | ** PENDING -> EXCLUSIVE |
| 2532 | ** |
| 2533 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2534 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2535 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2536 | ** access the file. |
| 2537 | ** |
| 2538 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2539 | ** routine to lower a locking level. |
| 2540 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2541 | static int semXLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2542 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2543 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2544 | int rc = SQLITE_OK; |
| 2545 | |
| 2546 | /* if we already have a lock, it is exclusive. |
| 2547 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2548 | if (pFile->eFileLock > NO_LOCK) { |
| 2549 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2550 | rc = SQLITE_OK; |
| 2551 | goto sem_end_lock; |
| 2552 | } |
| 2553 | |
| 2554 | /* lock semaphore now but bail out when already locked. */ |
| 2555 | if( sem_trywait(pSem)==-1 ){ |
| 2556 | rc = SQLITE_BUSY; |
| 2557 | goto sem_end_lock; |
| 2558 | } |
| 2559 | |
| 2560 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2561 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2562 | |
| 2563 | sem_end_lock: |
| 2564 | return rc; |
| 2565 | } |
| 2566 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2567 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2568 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2569 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2570 | ** |
| 2571 | ** If the locking level of the file descriptor is already at or below |
| 2572 | ** the requested locking level, this routine is a no-op. |
| 2573 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2574 | static int semXUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2575 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2576 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2577 | |
| 2578 | assert( pFile ); |
| 2579 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2580 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2581 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2582 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2583 | |
| 2584 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2585 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2586 | return SQLITE_OK; |
| 2587 | } |
| 2588 | |
| 2589 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2590 | if (eFileLock==SHARED_LOCK) { |
| 2591 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2592 | return SQLITE_OK; |
| 2593 | } |
| 2594 | |
| 2595 | /* no, really unlock. */ |
| 2596 | if ( sem_post(pSem)==-1 ) { |
| 2597 | int rc, tErrno = errno; |
| 2598 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2599 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2600 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2601 | } |
| 2602 | return rc; |
| 2603 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2604 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2605 | return SQLITE_OK; |
| 2606 | } |
| 2607 | |
| 2608 | /* |
| 2609 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2610 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2611 | static int semXClose(sqlite3_file *id) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2612 | if( id ){ |
| 2613 | unixFile *pFile = (unixFile*)id; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2614 | semXUnlock(id, NO_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2615 | assert( pFile ); |
| 2616 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2617 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2618 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2619 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2620 | } |
| 2621 | return SQLITE_OK; |
| 2622 | } |
| 2623 | |
| 2624 | #endif /* OS_VXWORKS */ |
| 2625 | /* |
| 2626 | ** Named semaphore locking is only available on VxWorks. |
| 2627 | ** |
| 2628 | *************** End of the named semaphore lock implementation **************** |
| 2629 | ******************************************************************************/ |
| 2630 | |
| 2631 | |
| 2632 | /****************************************************************************** |
| 2633 | *************************** Begin AFP Locking ********************************* |
| 2634 | ** |
| 2635 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2636 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2637 | ** |
| 2638 | ** Third-party implementations of AFP are available. But this code here |
| 2639 | ** only works on OSX. |
| 2640 | */ |
| 2641 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2642 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2643 | /* |
| 2644 | ** The afpLockingContext structure contains all afp lock specific state |
| 2645 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2646 | typedef struct afpLockingContext afpLockingContext; |
| 2647 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2648 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2649 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2650 | }; |
| 2651 | |
| 2652 | struct ByteRangeLockPB2 |
| 2653 | { |
| 2654 | unsigned long long offset; /* offset to first byte to lock */ |
| 2655 | unsigned long long length; /* nbr of bytes to lock */ |
| 2656 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2657 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2658 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2659 | int fd; /* file desc to assoc this lock with */ |
| 2660 | }; |
| 2661 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2662 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2663 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2664 | /* |
| 2665 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2666 | ** AFP filesystem. |
| 2667 | ** |
| 2668 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2669 | */ |
| 2670 | static int afpSetLock( |
| 2671 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2672 | unixFile *pFile, /* Open file descriptor on path */ |
| 2673 | unsigned long long offset, /* First byte to be locked */ |
| 2674 | unsigned long long length, /* Number of bytes to lock */ |
| 2675 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2676 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2677 | struct ByteRangeLockPB2 pb; |
| 2678 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2679 | |
| 2680 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2681 | pb.startEndFlag = 0; |
| 2682 | pb.offset = offset; |
| 2683 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2684 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2685 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2686 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2687 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2688 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2689 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2690 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2691 | int rc; |
| 2692 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2693 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2694 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2695 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2696 | rc = SQLITE_BUSY; |
| 2697 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2698 | rc = sqliteErrorFromPosixError(tErrno, |
| 2699 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2700 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2701 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2702 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2703 | } |
| 2704 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2705 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2706 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2710 | /* |
| 2711 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2712 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2713 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2714 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2715 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2716 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2717 | int rc = SQLITE_OK; |
| 2718 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2719 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2720 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2721 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2722 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2723 | |
| 2724 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2725 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2726 | if( context->reserved ){ |
| 2727 | *pResOut = 1; |
| 2728 | return SQLITE_OK; |
| 2729 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2730 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2731 | |
| 2732 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2733 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2734 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | /* Otherwise see if some other process holds it. |
| 2738 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2739 | if( !reserved ){ |
| 2740 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2741 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2742 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2743 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2744 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2745 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2746 | } else { |
| 2747 | /* if we failed to get the lock then someone else must have it */ |
| 2748 | reserved = 1; |
| 2749 | } |
| 2750 | if( IS_LOCK_ERROR(lrc) ){ |
| 2751 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2752 | } |
| 2753 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2754 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2755 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2756 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2757 | |
| 2758 | *pResOut = reserved; |
| 2759 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2762 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2763 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2764 | ** of the following: |
| 2765 | ** |
| 2766 | ** (1) SHARED_LOCK |
| 2767 | ** (2) RESERVED_LOCK |
| 2768 | ** (3) PENDING_LOCK |
| 2769 | ** (4) EXCLUSIVE_LOCK |
| 2770 | ** |
| 2771 | ** Sometimes when requesting one lock state, additional lock states |
| 2772 | ** are inserted in between. The locking might fail on one of the later |
| 2773 | ** transitions leaving the lock state different from what it started but |
| 2774 | ** still short of its goal. The following chart shows the allowed |
| 2775 | ** transitions and the inserted intermediate states: |
| 2776 | ** |
| 2777 | ** UNLOCKED -> SHARED |
| 2778 | ** SHARED -> RESERVED |
| 2779 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2780 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2781 | ** PENDING -> EXCLUSIVE |
| 2782 | ** |
| 2783 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2784 | ** routine to lower a locking level. |
| 2785 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2786 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2787 | int rc = SQLITE_OK; |
| 2788 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2789 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2790 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2791 | |
| 2792 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2793 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2794 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2795 | azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2796 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2797 | /* If there is already a lock of this type or more restrictive on the |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2798 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2799 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2800 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2801 | if( pFile->eFileLock>=eFileLock ){ |
| 2802 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2803 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2804 | return SQLITE_OK; |
| 2805 | } |
| 2806 | |
| 2807 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2808 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2809 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2810 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2811 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2812 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2813 | assert( eFileLock!=PENDING_LOCK ); |
| 2814 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2815 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2816 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2817 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2818 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2819 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2820 | |
| 2821 | /* If some thread using this PID has a lock via a different unixFile* |
| 2822 | ** handle that precludes the requested lock, return BUSY. |
| 2823 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2824 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2825 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2826 | ){ |
| 2827 | rc = SQLITE_BUSY; |
| 2828 | goto afp_end_lock; |
| 2829 | } |
| 2830 | |
| 2831 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2832 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2833 | ** return SQLITE_OK. |
| 2834 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2835 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2836 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2837 | assert( eFileLock==SHARED_LOCK ); |
| 2838 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2839 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2840 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2841 | pInode->nShared++; |
| 2842 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2843 | goto afp_end_lock; |
| 2844 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2845 | |
| 2846 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2847 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2848 | ** be released. |
| 2849 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2850 | if( eFileLock==SHARED_LOCK |
| 2851 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2852 | ){ |
| 2853 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2854 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2855 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2856 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2857 | goto afp_end_lock; |
| 2858 | } |
| 2859 | } |
| 2860 | |
| 2861 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2862 | ** operating system calls for the specified lock. |
| 2863 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2864 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2865 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2866 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2867 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2868 | assert( pInode->nShared==0 ); |
| 2869 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2870 | |
| 2871 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2872 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2873 | /* note that the quality of the randomness doesn't matter that much */ |
| 2874 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2875 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2876 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2877 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2878 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2879 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2880 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2881 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2882 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2883 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2884 | if( IS_LOCK_ERROR(lrc1) ) { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2885 | storeLastErrno(pFile, lrc1Errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2886 | rc = lrc1; |
| 2887 | goto afp_end_lock; |
| 2888 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2889 | rc = lrc2; |
| 2890 | goto afp_end_lock; |
| 2891 | } else if( lrc1 != SQLITE_OK ) { |
| 2892 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2893 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2894 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2895 | pInode->nLock++; |
| 2896 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2897 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2898 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2899 | /* We are trying for an exclusive lock but another thread in this |
| 2900 | ** same process is still holding a shared lock. */ |
| 2901 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2902 | }else{ |
| 2903 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2904 | ** assumed that there is a SHARED or greater lock on the file |
| 2905 | ** already. |
| 2906 | */ |
| 2907 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2908 | assert( 0!=pFile->eFileLock ); |
| 2909 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2910 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2911 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2912 | if( !failed ){ |
| 2913 | context->reserved = 1; |
| 2914 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2915 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2916 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2917 | /* Acquire an EXCLUSIVE lock */ |
| 2918 | |
| 2919 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2920 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2921 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2922 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2923 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2924 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2925 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2926 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2927 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2928 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2929 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2930 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2931 | ** a critical I/O error |
| 2932 | */ |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 2933 | rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 : |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2934 | SQLITE_IOERR_LOCK; |
| 2935 | goto afp_end_lock; |
| 2936 | } |
| 2937 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2938 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2939 | } |
| 2940 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2941 | if( failed ){ |
| 2942 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2947 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2948 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2949 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2950 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2951 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2955 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2956 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2957 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2958 | return rc; |
| 2959 | } |
| 2960 | |
| 2961 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2962 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2963 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2964 | ** |
| 2965 | ** If the locking level of the file descriptor is already at or below |
| 2966 | ** the requested locking level, this routine is a no-op. |
| 2967 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2968 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2969 | int rc = SQLITE_OK; |
| 2970 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2971 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2972 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2973 | int skipShared = 0; |
| 2974 | #ifdef SQLITE_TEST |
| 2975 | int h = pFile->h; |
| 2976 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2977 | |
| 2978 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2979 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2980 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2981 | osGetpid(0))); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2982 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2983 | assert( eFileLock<=SHARED_LOCK ); |
| 2984 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2985 | return SQLITE_OK; |
| 2986 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2987 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2988 | pInode = pFile->pInode; |
| 2989 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2990 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2991 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2992 | SimulateIOErrorBenign(1); |
| 2993 | SimulateIOError( h=(-1) ) |
| 2994 | SimulateIOErrorBenign(0); |
| 2995 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 2996 | #ifdef SQLITE_DEBUG |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2997 | /* When reducing a lock such that other processes can start |
| 2998 | ** reading the database file again, make sure that the |
| 2999 | ** transaction counter was updated if any part of the database |
| 3000 | ** file changed. If the transaction counter is not updated, |
| 3001 | ** other connections to the same file might not realize that |
| 3002 | ** the file has changed and hence might not know to flush their |
| 3003 | ** cache. The use of a stale cache can lead to database corruption. |
| 3004 | */ |
| 3005 | assert( pFile->inNormalWrite==0 |
| 3006 | || pFile->dbUpdate==0 |
| 3007 | || pFile->transCntrChng==1 ); |
| 3008 | pFile->inNormalWrite = 0; |
| 3009 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3010 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3011 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3012 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3013 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3014 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3015 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3016 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 3017 | } else { |
| 3018 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3019 | } |
| 3020 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3021 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3022 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3023 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3024 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3025 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 3026 | if( !rc ){ |
| 3027 | context->reserved = 0; |
| 3028 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3029 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3030 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 3031 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3032 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3033 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3034 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3035 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3036 | /* Decrement the shared lock counter. Release the lock using an |
| 3037 | ** OS call only when all threads in this same process have released |
| 3038 | ** the lock. |
| 3039 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3040 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 3041 | pInode->nShared--; |
| 3042 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3043 | SimulateIOErrorBenign(1); |
| 3044 | SimulateIOError( h=(-1) ) |
| 3045 | SimulateIOErrorBenign(0); |
| 3046 | if( !skipShared ){ |
| 3047 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 3048 | } |
| 3049 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3050 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3051 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3052 | } |
| 3053 | } |
| 3054 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3055 | pInode->nLock--; |
| 3056 | assert( pInode->nLock>=0 ); |
| 3057 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3058 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3059 | } |
| 3060 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3061 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3062 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3063 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3064 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3065 | return rc; |
| 3066 | } |
| 3067 | |
| 3068 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3069 | ** Close a file & cleanup AFP specific locking context |
| 3070 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3071 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3072 | int rc = SQLITE_OK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3073 | unixFile *pFile = (unixFile*)id; |
| 3074 | assert( id!=0 ); |
| 3075 | afpUnlock(id, NO_LOCK); |
| 3076 | unixEnterMutex(); |
| 3077 | if( pFile->pInode && pFile->pInode->nLock ){ |
| 3078 | /* If there are outstanding locks, do not actually close the file just |
| 3079 | ** yet because that would clear those locks. Instead, add the file |
| 3080 | ** descriptor to pInode->aPending. It will be automatically closed when |
| 3081 | ** the last lock is cleared. |
| 3082 | */ |
| 3083 | setPendingFd(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3084 | } |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3085 | releaseInodeInfo(pFile); |
| 3086 | sqlite3_free(pFile->lockingContext); |
| 3087 | rc = closeUnixFile(id); |
| 3088 | unixLeaveMutex(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3089 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3092 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3093 | /* |
| 3094 | ** The code above is the AFP lock implementation. The code is specific |
| 3095 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3096 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 3097 | ** VFS is not available. |
| 3098 | ** |
| 3099 | ********************* End of the AFP lock implementation ********************** |
| 3100 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3101 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3102 | /****************************************************************************** |
| 3103 | *************************** Begin NFS Locking ********************************/ |
| 3104 | |
| 3105 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3106 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3107 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3108 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3109 | ** |
| 3110 | ** If the locking level of the file descriptor is already at or below |
| 3111 | ** the requested locking level, this routine is a no-op. |
| 3112 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3113 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 3114 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3118 | /* |
| 3119 | ** The code above is the NFS lock implementation. The code is specific |
| 3120 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3121 | ** is available. |
| 3122 | ** |
| 3123 | ********************* End of the NFS lock implementation ********************** |
| 3124 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3125 | |
| 3126 | /****************************************************************************** |
| 3127 | **************** Non-locking sqlite3_file methods ***************************** |
| 3128 | ** |
| 3129 | ** The next division contains implementations for all methods of the |
| 3130 | ** sqlite3_file object other than the locking methods. The locking |
| 3131 | ** methods were defined in divisions above (one locking method per |
| 3132 | ** division). Those methods that are common to all locking modes |
| 3133 | ** are gather together into this division. |
| 3134 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3135 | |
| 3136 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3137 | ** Seek to the offset passed as the second argument, then read cnt |
| 3138 | ** bytes into pBuf. Return the number of bytes actually read. |
| 3139 | ** |
| 3140 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 3141 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 3142 | ** one system to another. Since SQLite does not define USE_PREAD |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3143 | ** in any form by default, we will not attempt to define _XOPEN_SOURCE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3144 | ** See tickets #2741 and #2681. |
| 3145 | ** |
| 3146 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 3147 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3148 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3149 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 3150 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3151 | int prior = 0; |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3152 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
| 3153 | i64 newOffset; |
| 3154 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3155 | TIMER_START; |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3156 | assert( cnt==(cnt&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3157 | assert( id->h>2 ); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3158 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3159 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3160 | got = osPread(id->h, pBuf, cnt, offset); |
| 3161 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3162 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3163 | got = osPread64(id->h, pBuf, cnt, offset); |
| 3164 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3165 | #else |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3166 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3167 | SimulateIOError( newOffset = -1 ); |
| 3168 | if( newOffset<0 ){ |
| 3169 | storeLastErrno((unixFile*)id, errno); |
| 3170 | return -1; |
| 3171 | } |
| 3172 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3173 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3174 | if( got==cnt ) break; |
| 3175 | if( got<0 ){ |
| 3176 | if( errno==EINTR ){ got = 1; continue; } |
| 3177 | prior = 0; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3178 | storeLastErrno((unixFile*)id, errno); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3179 | break; |
| 3180 | }else if( got>0 ){ |
| 3181 | cnt -= got; |
| 3182 | offset += got; |
| 3183 | prior += got; |
| 3184 | pBuf = (void*)(got + (char*)pBuf); |
| 3185 | } |
| 3186 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3187 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3188 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3189 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3190 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3194 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3195 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3196 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3197 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3198 | static int unixRead( |
| 3199 | sqlite3_file *id, |
| 3200 | void *pBuf, |
| 3201 | int amt, |
| 3202 | sqlite3_int64 offset |
| 3203 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3204 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3205 | int got; |
| 3206 | assert( id ); |
drh | 6cf9d8d | 2013-05-09 18:12:40 +0000 | [diff] [blame] | 3207 | assert( offset>=0 ); |
| 3208 | assert( amt>0 ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3209 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3210 | /* If this is a database file (not a journal, master-journal or temp |
| 3211 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3212 | #if 0 |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 3213 | assert( pFile->pPreallocatedUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3214 | || offset>=PENDING_BYTE+512 |
| 3215 | || offset+amt<=PENDING_BYTE |
| 3216 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3217 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3218 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3219 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 6c56963 | 2013-03-26 18:48:11 +0000 | [diff] [blame] | 3220 | /* Deal with as much of this read request as possible by transfering |
| 3221 | ** data from the memory mapping using memcpy(). */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3222 | if( offset<pFile->mmapSize ){ |
| 3223 | if( offset+amt <= pFile->mmapSize ){ |
| 3224 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 3225 | return SQLITE_OK; |
| 3226 | }else{ |
| 3227 | int nCopy = pFile->mmapSize - offset; |
| 3228 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 3229 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3230 | amt -= nCopy; |
| 3231 | offset += nCopy; |
| 3232 | } |
| 3233 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3234 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3235 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3236 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3237 | if( got==amt ){ |
| 3238 | return SQLITE_OK; |
| 3239 | }else if( got<0 ){ |
| 3240 | /* lastErrno set by seekAndRead */ |
| 3241 | return SQLITE_IOERR_READ; |
| 3242 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3243 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3244 | /* Unread parts of the buffer must be zero-filled */ |
| 3245 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3246 | return SQLITE_IOERR_SHORT_READ; |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | /* |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3251 | ** Attempt to seek the file-descriptor passed as the first argument to |
| 3252 | ** absolute offset iOff, then attempt to write nBuf bytes of data from |
| 3253 | ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, |
| 3254 | ** return the actual number of bytes written (which may be less than |
| 3255 | ** nBuf). |
| 3256 | */ |
| 3257 | static int seekAndWriteFd( |
| 3258 | int fd, /* File descriptor to write to */ |
| 3259 | i64 iOff, /* File offset to begin writing at */ |
| 3260 | const void *pBuf, /* Copy data from this buffer to the file */ |
| 3261 | int nBuf, /* Size of buffer pBuf in bytes */ |
| 3262 | int *piErrno /* OUT: Error number if error occurs */ |
| 3263 | ){ |
| 3264 | int rc = 0; /* Value returned by system call */ |
| 3265 | |
| 3266 | assert( nBuf==(nBuf&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3267 | assert( fd>2 ); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3268 | assert( piErrno!=0 ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3269 | nBuf &= 0x1ffff; |
| 3270 | TIMER_START; |
| 3271 | |
| 3272 | #if defined(USE_PREAD) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3273 | do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3274 | #elif defined(USE_PREAD64) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3275 | do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3276 | #else |
| 3277 | do{ |
| 3278 | i64 iSeek = lseek(fd, iOff, SEEK_SET); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3279 | SimulateIOError( iSeek = -1 ); |
| 3280 | if( iSeek<0 ){ |
| 3281 | rc = -1; |
| 3282 | break; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3283 | } |
| 3284 | rc = osWrite(fd, pBuf, nBuf); |
| 3285 | }while( rc<0 && errno==EINTR ); |
| 3286 | #endif |
| 3287 | |
| 3288 | TIMER_END; |
| 3289 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); |
| 3290 | |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3291 | if( rc<0 ) *piErrno = errno; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3292 | return rc; |
| 3293 | } |
| 3294 | |
| 3295 | |
| 3296 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3297 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3298 | ** Return the number of bytes actually read. Update the offset. |
| 3299 | ** |
| 3300 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3301 | ** is set before returning. |
| 3302 | */ |
| 3303 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3304 | return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | |
| 3308 | /* |
| 3309 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3310 | ** or some other error code on failure. |
| 3311 | */ |
| 3312 | static int unixWrite( |
| 3313 | sqlite3_file *id, |
| 3314 | const void *pBuf, |
| 3315 | int amt, |
| 3316 | sqlite3_int64 offset |
| 3317 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3318 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3319 | int wrote = 0; |
| 3320 | assert( id ); |
| 3321 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3322 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3323 | /* If this is a database file (not a journal, master-journal or temp |
| 3324 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3325 | #if 0 |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 3326 | assert( pFile->pPreallocatedUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3327 | || offset>=PENDING_BYTE+512 |
| 3328 | || offset+amt<=PENDING_BYTE |
| 3329 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3330 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3331 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3332 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3333 | /* If we are doing a normal write to a database file (as opposed to |
| 3334 | ** doing a hot-journal rollback or a write to some file other than a |
| 3335 | ** normal database file) then record the fact that the database |
| 3336 | ** has changed. If the transaction counter is modified, record that |
| 3337 | ** fact too. |
| 3338 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3339 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3340 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3341 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3342 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3343 | char oldCntr[4]; |
| 3344 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3345 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3346 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3347 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3348 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3349 | } |
| 3350 | } |
| 3351 | } |
| 3352 | #endif |
| 3353 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 3354 | #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3355 | /* Deal with as much of this write request as possible by transfering |
| 3356 | ** data from the memory mapping using memcpy(). */ |
| 3357 | if( offset<pFile->mmapSize ){ |
| 3358 | if( offset+amt <= pFile->mmapSize ){ |
| 3359 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 3360 | return SQLITE_OK; |
| 3361 | }else{ |
| 3362 | int nCopy = pFile->mmapSize - offset; |
| 3363 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3364 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3365 | amt -= nCopy; |
| 3366 | offset += nCopy; |
| 3367 | } |
| 3368 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3369 | #endif |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3370 | |
| 3371 | while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3372 | amt -= wrote; |
| 3373 | offset += wrote; |
| 3374 | pBuf = &((char*)pBuf)[wrote]; |
| 3375 | } |
| 3376 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3377 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3378 | |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3379 | if( amt>wrote ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3380 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3381 | /* lastErrno set by seekAndWrite */ |
| 3382 | return SQLITE_IOERR_WRITE; |
| 3383 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3384 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3385 | return SQLITE_FULL; |
| 3386 | } |
| 3387 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3388 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3389 | return SQLITE_OK; |
| 3390 | } |
| 3391 | |
| 3392 | #ifdef SQLITE_TEST |
| 3393 | /* |
| 3394 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3395 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3396 | */ |
| 3397 | int sqlite3_sync_count = 0; |
| 3398 | int sqlite3_fullsync_count = 0; |
| 3399 | #endif |
| 3400 | |
| 3401 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3402 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3403 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3404 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3405 | ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3406 | */ |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3407 | #if !defined(fdatasync) && !HAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3408 | # define fdatasync fsync |
| 3409 | #endif |
| 3410 | |
| 3411 | /* |
| 3412 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3413 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3414 | ** only available on Mac OS X. But that could change. |
| 3415 | */ |
| 3416 | #ifdef F_FULLFSYNC |
| 3417 | # define HAVE_FULLFSYNC 1 |
| 3418 | #else |
| 3419 | # define HAVE_FULLFSYNC 0 |
| 3420 | #endif |
| 3421 | |
| 3422 | |
| 3423 | /* |
| 3424 | ** The fsync() system call does not work as advertised on many |
| 3425 | ** unix systems. The following procedure is an attempt to make |
| 3426 | ** it work better. |
| 3427 | ** |
| 3428 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3429 | ** for testing when we want to run through the test suite quickly. |
| 3430 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3431 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3432 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3433 | ** |
| 3434 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3435 | ** The idea behind dataOnly is that it should only write the file content |
| 3436 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3437 | ** unchanged since the file size is part of the inode. However, |
| 3438 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3439 | ** file size has changed. The only real difference between fdatasync() |
| 3440 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3441 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3442 | ** We only care about the file size, not the other file attributes, so |
| 3443 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3444 | ** So, we always use fdatasync() if it is available, regardless of |
| 3445 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3446 | */ |
| 3447 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3448 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3449 | |
| 3450 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3451 | ** the one below. It is replicated here solely to avoid cluttering |
| 3452 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3453 | */ |
| 3454 | #ifdef SQLITE_NO_SYNC |
| 3455 | UNUSED_PARAMETER(fd); |
| 3456 | UNUSED_PARAMETER(fullSync); |
| 3457 | UNUSED_PARAMETER(dataOnly); |
| 3458 | #elif HAVE_FULLFSYNC |
| 3459 | UNUSED_PARAMETER(dataOnly); |
| 3460 | #else |
| 3461 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3462 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3463 | #endif |
| 3464 | |
| 3465 | /* Record the number of times that we do a normal fsync() and |
| 3466 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3467 | ** gets called with the correct arguments. |
| 3468 | */ |
| 3469 | #ifdef SQLITE_TEST |
| 3470 | if( fullSync ) sqlite3_fullsync_count++; |
| 3471 | sqlite3_sync_count++; |
| 3472 | #endif |
| 3473 | |
| 3474 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3475 | ** no-op. But go ahead and call fstat() to validate the file |
| 3476 | ** descriptor as we need a method to provoke a failure during |
| 3477 | ** coverate testing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3478 | */ |
| 3479 | #ifdef SQLITE_NO_SYNC |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3480 | { |
| 3481 | struct stat buf; |
| 3482 | rc = osFstat(fd, &buf); |
| 3483 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3484 | #elif HAVE_FULLFSYNC |
| 3485 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3486 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3487 | }else{ |
| 3488 | rc = 1; |
| 3489 | } |
| 3490 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3491 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3492 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3493 | ** isn't supported for this file system. So, attempt an fsync |
| 3494 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3495 | ** It'd be better to detect fullfsync support once and avoid |
| 3496 | ** the fcntl call every time sync is called. |
| 3497 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3498 | if( rc ) rc = fsync(fd); |
| 3499 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3500 | #elif defined(__APPLE__) |
| 3501 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3502 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3503 | */ |
| 3504 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3505 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3506 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3507 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3508 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3509 | rc = fsync(fd); |
| 3510 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3511 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3512 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3513 | |
| 3514 | if( OS_VXWORKS && rc!= -1 ){ |
| 3515 | rc = 0; |
| 3516 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3517 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3520 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3521 | ** Open a file descriptor to the directory containing file zFilename. |
| 3522 | ** If successful, *pFd is set to the opened file descriptor and |
| 3523 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3524 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3525 | ** value. |
| 3526 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3527 | ** The directory file descriptor is used for only one thing - to |
| 3528 | ** fsync() a directory to make sure file creation and deletion events |
| 3529 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3530 | ** journaling filesystems, but are required on older filesystems. |
| 3531 | ** |
| 3532 | ** This routine can be overridden using the xSetSysCall interface. |
| 3533 | ** The ability to override this routine was added in support of the |
| 3534 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3535 | ** told) so making it overrideable allows the chromium sandbox to |
| 3536 | ** replace this routine with a harmless no-op. To make this routine |
| 3537 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3538 | ** *pFd set to a negative number. |
| 3539 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3540 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3541 | ** the file descriptor *pFd using close(). |
| 3542 | */ |
| 3543 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3544 | int ii; |
| 3545 | int fd = -1; |
| 3546 | char zDirname[MAX_PATHNAME+1]; |
| 3547 | |
| 3548 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3549 | for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--); |
| 3550 | if( ii>0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3551 | zDirname[ii] = '\0'; |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3552 | }else{ |
| 3553 | if( zDirname[0]!='/' ) zDirname[0] = '.'; |
| 3554 | zDirname[1] = 0; |
| 3555 | } |
| 3556 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3557 | if( fd>=0 ){ |
| 3558 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3559 | } |
| 3560 | *pFd = fd; |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3561 | if( fd>=0 ) return SQLITE_OK; |
| 3562 | return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3566 | ** Make sure all writes to a particular file are committed to disk. |
| 3567 | ** |
| 3568 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3569 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3570 | ** file data is synced. |
| 3571 | ** |
| 3572 | ** Under Unix, also make sure that the directory entry for the file |
| 3573 | ** has been created by fsync-ing the directory that contains the file. |
| 3574 | ** If we do not do this and we encounter a power failure, the directory |
| 3575 | ** entry for the journal might not exist after we reboot. The next |
| 3576 | ** SQLite to access the file will not know that the journal exists (because |
| 3577 | ** the directory entry for the journal was never created) and the transaction |
| 3578 | ** will not roll back - possibly leading to database corruption. |
| 3579 | */ |
| 3580 | static int unixSync(sqlite3_file *id, int flags){ |
| 3581 | int rc; |
| 3582 | unixFile *pFile = (unixFile*)id; |
| 3583 | |
| 3584 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3585 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3586 | |
| 3587 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3588 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3589 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3590 | ); |
| 3591 | |
| 3592 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3593 | ** line is to test that doing so does not cause any problems. |
| 3594 | */ |
| 3595 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3596 | |
| 3597 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3598 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3599 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3600 | SimulateIOError( rc=1 ); |
| 3601 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3602 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3603 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3604 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3605 | |
| 3606 | /* Also fsync the directory containing the file if the DIRSYNC flag |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 3607 | ** is set. This is a one-time occurrence. Many systems (examples: AIX) |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3608 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3609 | */ |
| 3610 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3611 | int dirfd; |
| 3612 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3613 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3614 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3615 | if( rc==SQLITE_OK ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3616 | full_fsync(dirfd, 0, 0); |
| 3617 | robust_close(pFile, dirfd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3618 | }else{ |
| 3619 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3620 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3621 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3622 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3623 | } |
| 3624 | return rc; |
| 3625 | } |
| 3626 | |
| 3627 | /* |
| 3628 | ** Truncate an open file to a specified size |
| 3629 | */ |
| 3630 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3631 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3632 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3633 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3634 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3635 | |
| 3636 | /* If the user has configured a chunk-size for this file, truncate the |
| 3637 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3638 | ** actual file size after the operation may be larger than the requested |
| 3639 | ** size). |
| 3640 | */ |
drh | b8af4b7 | 2012-04-05 20:04:39 +0000 | [diff] [blame] | 3641 | if( pFile->szChunk>0 ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3642 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3643 | } |
| 3644 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 3645 | rc = robust_ftruncate(pFile->h, nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3646 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3647 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3648 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3649 | }else{ |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3650 | #ifdef SQLITE_DEBUG |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3651 | /* If we are doing a normal write to a database file (as opposed to |
| 3652 | ** doing a hot-journal rollback or a write to some file other than a |
| 3653 | ** normal database file) and we truncate the file to zero length, |
| 3654 | ** that effectively updates the change counter. This might happen |
| 3655 | ** when restoring a database using the backup API from a zero-length |
| 3656 | ** source. |
| 3657 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3658 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3659 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3660 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3661 | #endif |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3662 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3663 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3664 | /* If the file was just truncated to a size smaller than the currently |
| 3665 | ** mapped region, reduce the effective mapping size as well. SQLite will |
| 3666 | ** use read() and write() to access data beyond this point from now on. |
| 3667 | */ |
| 3668 | if( nByte<pFile->mmapSize ){ |
| 3669 | pFile->mmapSize = nByte; |
| 3670 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3671 | #endif |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3672 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3673 | return SQLITE_OK; |
| 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | /* |
| 3678 | ** Determine the current size of a file in bytes |
| 3679 | */ |
| 3680 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3681 | int rc; |
| 3682 | struct stat buf; |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 3683 | assert( id ); |
| 3684 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3685 | SimulateIOError( rc=1 ); |
| 3686 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3687 | storeLastErrno((unixFile*)id, errno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3688 | return SQLITE_IOERR_FSTAT; |
| 3689 | } |
| 3690 | *pSize = buf.st_size; |
| 3691 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3692 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3693 | ** writes a single byte into that file in order to work around a bug |
| 3694 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3695 | ** layers, we need to report this file size as zero even though it is |
| 3696 | ** really 1. Ticket #3260. |
| 3697 | */ |
| 3698 | if( *pSize==1 ) *pSize = 0; |
| 3699 | |
| 3700 | |
| 3701 | return SQLITE_OK; |
| 3702 | } |
| 3703 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3704 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3705 | /* |
| 3706 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3707 | ** proxying locking division. |
| 3708 | */ |
| 3709 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3710 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3711 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3712 | /* |
| 3713 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3714 | ** file-control operation. Enlarge the database to nBytes in size |
| 3715 | ** (rounded up to the next chunk-size). If the database is already |
| 3716 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3717 | */ |
| 3718 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3719 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3720 | i64 nSize; /* Required file size */ |
| 3721 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3722 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3723 | if( osFstat(pFile->h, &buf) ){ |
| 3724 | return SQLITE_IOERR_FSTAT; |
| 3725 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3726 | |
| 3727 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3728 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3729 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3730 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3731 | /* The code below is handling the return value of osFallocate() |
| 3732 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3733 | ** or an error number on failure". See the manpage for details. */ |
| 3734 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3735 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3736 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3737 | }while( err==EINTR ); |
| 3738 | if( err ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3739 | #else |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3740 | /* If the OS does not have posix_fallocate(), fake it. Write a |
| 3741 | ** single byte to the last byte in each block that falls entirely |
| 3742 | ** within the extended region. Then, if required, a single byte |
| 3743 | ** at offset (nSize-1), to set the size of the file correctly. |
| 3744 | ** This is a similar technique to that used by glibc on systems |
| 3745 | ** that do not have a real fallocate() call. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3746 | */ |
| 3747 | int nBlk = buf.st_blksize; /* File-system block size */ |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3748 | int nWrite = 0; /* Number of bytes written by seekAndWrite */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3749 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3750 | |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3751 | iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3752 | assert( iWrite>=buf.st_size ); |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3753 | assert( ((iWrite+1)%nBlk)==0 ); |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3754 | for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){ |
| 3755 | if( iWrite>=nSize ) iWrite = nSize - 1; |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3756 | nWrite = seekAndWrite(pFile, iWrite, "", 1); |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3757 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3758 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3759 | #endif |
| 3760 | } |
| 3761 | } |
| 3762 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3763 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3764 | if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3765 | int rc; |
| 3766 | if( pFile->szChunk<=0 ){ |
| 3767 | if( robust_ftruncate(pFile->h, nByte) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3768 | storeLastErrno(pFile, errno); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3769 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | rc = unixMapfile(pFile, nByte); |
| 3774 | return rc; |
| 3775 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3776 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3777 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3778 | return SQLITE_OK; |
| 3779 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3780 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3781 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3782 | ** If *pArg is initially negative then this is a query. Set *pArg to |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3783 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3784 | ** |
| 3785 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3786 | */ |
| 3787 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3788 | if( *pArg<0 ){ |
| 3789 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3790 | }else if( (*pArg)==0 ){ |
| 3791 | pFile->ctrlFlags &= ~mask; |
| 3792 | }else{ |
| 3793 | pFile->ctrlFlags |= mask; |
| 3794 | } |
| 3795 | } |
| 3796 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3797 | /* Forward declaration */ |
| 3798 | static int unixGetTempname(int nBuf, char *zBuf); |
| 3799 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3800 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3801 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3802 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3803 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3804 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3805 | switch( op ){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3806 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3807 | case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: { |
| 3808 | int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3809 | return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3810 | } |
| 3811 | case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: { |
| 3812 | int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3813 | return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3814 | } |
| 3815 | case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { |
| 3816 | int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3817 | return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3818 | } |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3819 | #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3820 | |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3821 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3822 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3823 | return SQLITE_OK; |
| 3824 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3825 | case SQLITE_FCNTL_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3826 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3827 | return SQLITE_OK; |
| 3828 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3829 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3830 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3831 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3832 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3833 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3834 | int rc; |
| 3835 | SimulateIOErrorBenign(1); |
| 3836 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3837 | SimulateIOErrorBenign(0); |
| 3838 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3839 | } |
| 3840 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3841 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3842 | return SQLITE_OK; |
| 3843 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3844 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3845 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3846 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3847 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3848 | case SQLITE_FCNTL_VFSNAME: { |
| 3849 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3850 | return SQLITE_OK; |
| 3851 | } |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3852 | case SQLITE_FCNTL_TEMPFILENAME: { |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3853 | char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3854 | if( zTFile ){ |
| 3855 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 3856 | *(char**)pArg = zTFile; |
| 3857 | } |
| 3858 | return SQLITE_OK; |
| 3859 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 3860 | case SQLITE_FCNTL_HAS_MOVED: { |
| 3861 | *(int*)pArg = fileHasMoved(pFile); |
| 3862 | return SQLITE_OK; |
| 3863 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3864 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3865 | case SQLITE_FCNTL_MMAP_SIZE: { |
drh | 34f7490 | 2013-04-03 13:09:18 +0000 | [diff] [blame] | 3866 | i64 newLimit = *(i64*)pArg; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3867 | int rc = SQLITE_OK; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3868 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 3869 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 3870 | } |
dan | 43c1e62 | 2017-08-07 18:13:28 +0000 | [diff] [blame] | 3871 | |
| 3872 | /* The value of newLimit may be eventually cast to (size_t) and passed |
mistachkin | e35395a | 2017-08-07 19:06:54 +0000 | [diff] [blame] | 3873 | ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a |
| 3874 | ** 64-bit type. */ |
dan | 089df50 | 2017-08-07 18:54:10 +0000 | [diff] [blame] | 3875 | if( newLimit>0 && sizeof(size_t)<8 ){ |
dan | 43c1e62 | 2017-08-07 18:13:28 +0000 | [diff] [blame] | 3876 | newLimit = (newLimit & 0x7FFFFFFF); |
| 3877 | } |
| 3878 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3879 | *(i64*)pArg = pFile->mmapSizeMax; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3880 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3881 | pFile->mmapSizeMax = newLimit; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3882 | if( pFile->mmapSize>0 ){ |
| 3883 | unixUnmapfile(pFile); |
| 3884 | rc = unixMapfile(pFile, -1); |
| 3885 | } |
dan | bcb8a86 | 2013-04-08 15:30:41 +0000 | [diff] [blame] | 3886 | } |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3887 | return rc; |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 3888 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3889 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3890 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3891 | /* The pager calls this method to signal that it has done |
| 3892 | ** a rollback and that the database is therefore unchanged and |
| 3893 | ** it hence it is OK for the transaction change counter to be |
| 3894 | ** unchanged. |
| 3895 | */ |
| 3896 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3897 | ((unixFile*)id)->dbUpdate = 0; |
| 3898 | return SQLITE_OK; |
| 3899 | } |
| 3900 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3901 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3902 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: |
| 3903 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3904 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3905 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3906 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3907 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3908 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | /* |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3912 | ** If pFd->sectorSize is non-zero when this function is called, it is a |
| 3913 | ** no-op. Otherwise, the values of pFd->sectorSize and |
| 3914 | ** pFd->deviceCharacteristics are set according to the file-system |
| 3915 | ** characteristics. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3916 | ** |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3917 | ** There are two versions of this function. One for QNX and one for all |
| 3918 | ** other systems. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3919 | */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3920 | #ifndef __QNXNTO__ |
| 3921 | static void setDeviceCharacteristics(unixFile *pFd){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3922 | assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 ); |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3923 | if( pFd->sectorSize==0 ){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3924 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3925 | int res; |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 3926 | u32 f = 0; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3927 | |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3928 | /* Check for support for F2FS atomic batch writes. */ |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 3929 | res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f); |
| 3930 | if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){ |
dan | 77b4f52 | 2017-07-27 18:34:00 +0000 | [diff] [blame] | 3931 | pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3932 | } |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3933 | #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3934 | |
| 3935 | /* Set the POWERSAFE_OVERWRITE flag if requested. */ |
| 3936 | if( pFd->ctrlFlags & UNIXFILE_PSOW ){ |
| 3937 | pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 3938 | } |
| 3939 | |
| 3940 | pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3941 | } |
| 3942 | } |
| 3943 | #else |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3944 | #include <sys/dcmd_blk.h> |
| 3945 | #include <sys/statvfs.h> |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3946 | static void setDeviceCharacteristics(unixFile *pFile){ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3947 | if( pFile->sectorSize == 0 ){ |
| 3948 | struct statvfs fsInfo; |
| 3949 | |
| 3950 | /* Set defaults for non-supported filesystems */ |
| 3951 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3952 | pFile->deviceCharacteristics = 0; |
| 3953 | if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
drh | a9be508 | 2018-01-15 14:32:37 +0000 | [diff] [blame] | 3954 | return; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
| 3957 | if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 3958 | pFile->sectorSize = fsInfo.f_bsize; |
| 3959 | pFile->deviceCharacteristics = |
| 3960 | SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 3961 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3962 | ** the write succeeds */ |
| 3963 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3964 | ** so it is ordered */ |
| 3965 | 0; |
| 3966 | }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 3967 | pFile->sectorSize = fsInfo.f_bsize; |
| 3968 | pFile->deviceCharacteristics = |
| 3969 | /* etfs cluster size writes are atomic */ |
| 3970 | (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 3971 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3972 | ** the write succeeds */ |
| 3973 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3974 | ** so it is ordered */ |
| 3975 | 0; |
| 3976 | }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 3977 | pFile->sectorSize = fsInfo.f_bsize; |
| 3978 | pFile->deviceCharacteristics = |
| 3979 | SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 3980 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3981 | ** the write succeeds */ |
| 3982 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3983 | ** so it is ordered */ |
| 3984 | 0; |
| 3985 | }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 3986 | pFile->sectorSize = fsInfo.f_bsize; |
| 3987 | pFile->deviceCharacteristics = |
| 3988 | /* full bitset of atomics from max sector size and smaller */ |
| 3989 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3990 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3991 | ** so it is ordered */ |
| 3992 | 0; |
| 3993 | }else if( strstr(fsInfo.f_basetype, "dos") ){ |
| 3994 | pFile->sectorSize = fsInfo.f_bsize; |
| 3995 | pFile->deviceCharacteristics = |
| 3996 | /* full bitset of atomics from max sector size and smaller */ |
| 3997 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3998 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3999 | ** so it is ordered */ |
| 4000 | 0; |
| 4001 | }else{ |
| 4002 | pFile->deviceCharacteristics = |
| 4003 | SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 4004 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4005 | ** the write succeeds */ |
| 4006 | 0; |
| 4007 | } |
| 4008 | } |
| 4009 | /* Last chance verification. If the sector size isn't a multiple of 512 |
| 4010 | ** then it isn't valid.*/ |
| 4011 | if( pFile->sectorSize % 512 != 0 ){ |
| 4012 | pFile->deviceCharacteristics = 0; |
| 4013 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4014 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4015 | } |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4016 | #endif |
| 4017 | |
| 4018 | /* |
| 4019 | ** Return the sector size in bytes of the underlying block device for |
| 4020 | ** the specified file. This is almost always 512 bytes, but may be |
| 4021 | ** larger for some devices. |
| 4022 | ** |
| 4023 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 4024 | ** if two files are created in the same file-system directory (i.e. |
| 4025 | ** a database and its journal file) that the sector size will be the |
| 4026 | ** same for both. |
| 4027 | */ |
| 4028 | static int unixSectorSize(sqlite3_file *id){ |
| 4029 | unixFile *pFd = (unixFile*)id; |
| 4030 | setDeviceCharacteristics(pFd); |
| 4031 | return pFd->sectorSize; |
| 4032 | } |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4033 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4034 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4035 | ** Return the device characteristics for the file. |
| 4036 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4037 | ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default. |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4038 | ** However, that choice is controversial since technically the underlying |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4039 | ** file system does not always provide powersafe overwrites. (In other |
| 4040 | ** words, after a power-loss event, parts of the file that were never |
| 4041 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 4042 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 4043 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 4044 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 4045 | ** available to turn it off and URI query parameter available to turn it off. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4046 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4047 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4048 | unixFile *pFd = (unixFile*)id; |
| 4049 | setDeviceCharacteristics(pFd); |
| 4050 | return pFd->deviceCharacteristics; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4053 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4054 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4055 | /* |
| 4056 | ** Return the system page size. |
| 4057 | ** |
| 4058 | ** This function should not be called directly by other code in this file. |
| 4059 | ** Instead, it should be called via macro osGetpagesize(). |
| 4060 | */ |
| 4061 | static int unixGetpagesize(void){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4062 | #if OS_VXWORKS |
| 4063 | return 1024; |
| 4064 | #elif defined(_BSD_SOURCE) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4065 | return getpagesize(); |
| 4066 | #else |
| 4067 | return (int)sysconf(_SC_PAGESIZE); |
| 4068 | #endif |
| 4069 | } |
| 4070 | |
| 4071 | #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ |
| 4072 | |
| 4073 | #ifndef SQLITE_OMIT_WAL |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4074 | |
| 4075 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4076 | ** Object used to represent an shared memory buffer. |
| 4077 | ** |
| 4078 | ** When multiple threads all reference the same wal-index, each thread |
| 4079 | ** has its own unixShm object, but they all point to a single instance |
| 4080 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 4081 | ** only once per process. |
| 4082 | ** |
| 4083 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 4084 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 4085 | ** every open file that does not use shared memory (in other words, most |
| 4086 | ** open files) would have to carry around this extra information. So |
| 4087 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 4088 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4089 | ** |
| 4090 | ** unixMutexHeld() must be true when creating or destroying |
| 4091 | ** this object or while reading or writing the following fields: |
| 4092 | ** |
| 4093 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4094 | ** |
| 4095 | ** The following fields are read-only after the object is created: |
| 4096 | ** |
| 4097 | ** fid |
| 4098 | ** zFilename |
| 4099 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4100 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4101 | ** unixMutexHeld() is true when reading or writing any other field |
| 4102 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4103 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4104 | struct unixShmNode { |
| 4105 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4106 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4107 | char *zFilename; /* Name of the mmapped file */ |
| 4108 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4109 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4110 | u16 nRegion; /* Size of array apRegion */ |
| 4111 | u8 isReadonly; /* True if read-only */ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4112 | u8 isUnlocked; /* True if no DMS lock held */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4113 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4114 | int nRef; /* Number of unixShm objects pointing to this */ |
| 4115 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4116 | #ifdef SQLITE_DEBUG |
| 4117 | u8 exclMask; /* Mask of exclusive locks held */ |
| 4118 | u8 sharedMask; /* Mask of shared locks held */ |
| 4119 | u8 nextShmId; /* Next available unixShm.id value */ |
| 4120 | #endif |
| 4121 | }; |
| 4122 | |
| 4123 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4124 | ** Structure used internally by this VFS to record the state of an |
| 4125 | ** open shared memory connection. |
| 4126 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4127 | ** The following fields are initialized when this object is created and |
| 4128 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4129 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4130 | ** unixShm.pFile |
| 4131 | ** unixShm.id |
| 4132 | ** |
| 4133 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 4134 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4135 | */ |
| 4136 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4137 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 4138 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4139 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 4140 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4141 | u16 sharedMask; /* Mask of shared locks held */ |
| 4142 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4143 | }; |
| 4144 | |
| 4145 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4146 | ** Constants used for locking |
| 4147 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 4148 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 4149 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4150 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4151 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4152 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4153 | ** |
| 4154 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 4155 | ** otherwise. |
| 4156 | */ |
| 4157 | static int unixShmSystemLock( |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4158 | unixFile *pFile, /* Open connection to the WAL file */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4159 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4160 | int ofst, /* First byte of the locking range */ |
| 4161 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4162 | ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4163 | unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 4164 | struct flock f; /* The posix advisory locking structure */ |
| 4165 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4166 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4167 | /* Access to the unixShmNode object is serialized by the caller */ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4168 | pShmNode = pFile->pInode->pShmNode; |
drh | 37874b5 | 2017-12-13 10:11:09 +0000 | [diff] [blame] | 4169 | assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->mutex) ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4170 | |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4171 | /* Shared locks never span more than one byte */ |
| 4172 | assert( n==1 || lockType!=F_RDLCK ); |
| 4173 | |
| 4174 | /* Locks are within range */ |
| 4175 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 4176 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4177 | if( pShmNode->h>=0 ){ |
| 4178 | /* Initialize the locking parameters */ |
| 4179 | memset(&f, 0, sizeof(f)); |
| 4180 | f.l_type = lockType; |
| 4181 | f.l_whence = SEEK_SET; |
| 4182 | f.l_start = ofst; |
| 4183 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4184 | |
drh | dcfb965 | 2015-12-02 00:05:26 +0000 | [diff] [blame] | 4185 | rc = osFcntl(pShmNode->h, F_SETLK, &f); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4186 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 4187 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4188 | |
| 4189 | /* Update the global lock state and do debug tracing */ |
| 4190 | #ifdef SQLITE_DEBUG |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4191 | { u16 mask; |
| 4192 | OSTRACE(("SHM-LOCK ")); |
| 4193 | mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst); |
| 4194 | if( rc==SQLITE_OK ){ |
| 4195 | if( lockType==F_UNLCK ){ |
| 4196 | OSTRACE(("unlock %d ok", ofst)); |
| 4197 | pShmNode->exclMask &= ~mask; |
| 4198 | pShmNode->sharedMask &= ~mask; |
| 4199 | }else if( lockType==F_RDLCK ){ |
| 4200 | OSTRACE(("read-lock %d ok", ofst)); |
| 4201 | pShmNode->exclMask &= ~mask; |
| 4202 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4203 | }else{ |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4204 | assert( lockType==F_WRLCK ); |
| 4205 | OSTRACE(("write-lock %d ok", ofst)); |
| 4206 | pShmNode->exclMask |= mask; |
| 4207 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4208 | } |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4209 | }else{ |
| 4210 | if( lockType==F_UNLCK ){ |
| 4211 | OSTRACE(("unlock %d failed", ofst)); |
| 4212 | }else if( lockType==F_RDLCK ){ |
| 4213 | OSTRACE(("read-lock failed")); |
| 4214 | }else{ |
| 4215 | assert( lockType==F_WRLCK ); |
| 4216 | OSTRACE(("write-lock %d failed", ofst)); |
| 4217 | } |
| 4218 | } |
| 4219 | OSTRACE((" - afterwards %03x,%03x\n", |
| 4220 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4221 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4222 | #endif |
| 4223 | |
| 4224 | return rc; |
| 4225 | } |
| 4226 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4227 | /* |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4228 | ** Return the minimum number of 32KB shm regions that should be mapped at |
| 4229 | ** a time, assuming that each mapping must be an integer multiple of the |
| 4230 | ** current system page-size. |
| 4231 | ** |
| 4232 | ** Usually, this is 1. The exception seems to be systems that are configured |
| 4233 | ** to use 64KB pages - in this case each mapping must cover at least two |
| 4234 | ** shm regions. |
| 4235 | */ |
| 4236 | static int unixShmRegionPerMap(void){ |
| 4237 | int shmsz = 32*1024; /* SHM region size */ |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4238 | int pgsz = osGetpagesize(); /* System page size */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4239 | assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ |
| 4240 | if( pgsz<shmsz ) return 1; |
| 4241 | return pgsz/shmsz; |
| 4242 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4243 | |
| 4244 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4245 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4246 | ** |
| 4247 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4248 | ** by VFS shared-memory methods. |
| 4249 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4250 | static void unixShmPurge(unixFile *pFd){ |
| 4251 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4252 | assert( unixMutexHeld() ); |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4253 | if( p && ALWAYS(p->nRef==0) ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4254 | int nShmPerMap = unixShmRegionPerMap(); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4255 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4256 | assert( p->pInode==pFd->pInode ); |
drh | df3aa16 | 2011-06-24 11:29:51 +0000 | [diff] [blame] | 4257 | sqlite3_mutex_free(p->mutex); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4258 | for(i=0; i<p->nRegion; i+=nShmPerMap){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4259 | if( p->h>=0 ){ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 4260 | osMunmap(p->apRegion[i], p->szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4261 | }else{ |
| 4262 | sqlite3_free(p->apRegion[i]); |
| 4263 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4264 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4265 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4266 | if( p->h>=0 ){ |
| 4267 | robust_close(pFd, p->h, __LINE__); |
| 4268 | p->h = -1; |
| 4269 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4270 | p->pInode->pShmNode = 0; |
| 4271 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | /* |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4276 | ** The DMS lock has not yet been taken on shm file pShmNode. Attempt to |
| 4277 | ** take it now. Return SQLITE_OK if successful, or an SQLite error |
| 4278 | ** code otherwise. |
| 4279 | ** |
| 4280 | ** If the DMS cannot be locked because this is a readonly_shm=1 |
| 4281 | ** connection and no other process already holds a lock, return |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4282 | ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1. |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4283 | */ |
| 4284 | static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){ |
| 4285 | struct flock lock; |
| 4286 | int rc = SQLITE_OK; |
| 4287 | |
| 4288 | /* Use F_GETLK to determine the locks other processes are holding |
| 4289 | ** on the DMS byte. If it indicates that another process is holding |
| 4290 | ** a SHARED lock, then this process may also take a SHARED lock |
| 4291 | ** and proceed with opening the *-shm file. |
| 4292 | ** |
| 4293 | ** Or, if no other process is holding any lock, then this process |
| 4294 | ** is the first to open it. In this case take an EXCLUSIVE lock on the |
| 4295 | ** DMS byte and truncate the *-shm file to zero bytes in size. Then |
| 4296 | ** downgrade to a SHARED lock on the DMS byte. |
| 4297 | ** |
| 4298 | ** If another process is holding an EXCLUSIVE lock on the DMS byte, |
| 4299 | ** return SQLITE_BUSY to the caller (it will try again). An earlier |
| 4300 | ** version of this code attempted the SHARED lock at this point. But |
| 4301 | ** this introduced a subtle race condition: if the process holding |
| 4302 | ** EXCLUSIVE failed just before truncating the *-shm file, then this |
| 4303 | ** process might open and use the *-shm file without truncating it. |
| 4304 | ** And if the *-shm file has been corrupted by a power failure or |
| 4305 | ** system crash, the database itself may also become corrupt. */ |
| 4306 | lock.l_whence = SEEK_SET; |
| 4307 | lock.l_start = UNIX_SHM_DMS; |
| 4308 | lock.l_len = 1; |
| 4309 | lock.l_type = F_WRLCK; |
| 4310 | if( osFcntl(pShmNode->h, F_GETLK, &lock)!=0 ) { |
| 4311 | rc = SQLITE_IOERR_LOCK; |
| 4312 | }else if( lock.l_type==F_UNLCK ){ |
| 4313 | if( pShmNode->isReadonly ){ |
| 4314 | pShmNode->isUnlocked = 1; |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4315 | rc = SQLITE_READONLY_CANTINIT; |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4316 | }else{ |
| 4317 | rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1); |
| 4318 | if( rc==SQLITE_OK && robust_ftruncate(pShmNode->h, 0) ){ |
| 4319 | rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename); |
| 4320 | } |
| 4321 | } |
| 4322 | }else if( lock.l_type==F_WRLCK ){ |
| 4323 | rc = SQLITE_BUSY; |
| 4324 | } |
| 4325 | |
| 4326 | if( rc==SQLITE_OK ){ |
| 4327 | assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK ); |
| 4328 | rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
| 4329 | } |
| 4330 | return rc; |
| 4331 | } |
| 4332 | |
| 4333 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4334 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4335 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4336 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4337 | ** The file used to implement shared-memory is in the same directory |
| 4338 | ** as the open database file and has the same name as the open database |
| 4339 | ** file with the "-shm" suffix added. For example, if the database file |
| 4340 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4341 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 4342 | ** |
| 4343 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 4344 | ** some other tmpfs mount. But if a file in a different directory |
| 4345 | ** from the database file is used, then differing access permissions |
| 4346 | ** or a chroot() might cause two different processes on the same |
| 4347 | ** database to end up using different files for shared memory - |
| 4348 | ** meaning that their memory would not really be shared - resulting |
| 4349 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 4350 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 4351 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 4352 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 4353 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 4354 | ** same database file at the same time, database corruption will likely |
| 4355 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 4356 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4357 | ** |
| 4358 | ** When opening a new shared-memory file, if no other instances of that |
| 4359 | ** file are currently open, in this process or in other processes, then |
| 4360 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4361 | ** |
| 4362 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 4363 | ** that means that an exclusive lock is held on the database file and |
| 4364 | ** that no other processes are able to read or write the database. In |
| 4365 | ** that case, we do not really need shared memory. No shared memory |
| 4366 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4367 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4368 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 4369 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4370 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4371 | int rc = SQLITE_OK; /* Result code */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4372 | unixInodeInfo *pInode; /* The inode of fd */ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4373 | char *zShm; /* Name of the file used for SHM */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4374 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4375 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4376 | /* Allocate space for the new unixShm object. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4377 | p = sqlite3_malloc64( sizeof(*p) ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4378 | if( p==0 ) return SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4379 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4380 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4381 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4382 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 4383 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4384 | */ |
| 4385 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4386 | pInode = pDbFd->pInode; |
| 4387 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4388 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4389 | struct stat sStat; /* fstat() info for database file */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4390 | #ifndef SQLITE_SHM_DIRECTORY |
| 4391 | const char *zBasePath = pDbFd->zPath; |
| 4392 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4393 | |
| 4394 | /* Call fstat() to figure out the permissions on the database file. If |
| 4395 | ** a new *-shm file is created, an attempt will be made to create it |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 4396 | ** with the same permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4397 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4398 | if( osFstat(pDbFd->h, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4399 | rc = SQLITE_IOERR_FSTAT; |
| 4400 | goto shm_open_err; |
| 4401 | } |
| 4402 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4403 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4404 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4405 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4406 | nShmFilename = 6 + (int)strlen(zBasePath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4407 | #endif |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4408 | pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4409 | if( pShmNode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4410 | rc = SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4411 | goto shm_open_err; |
| 4412 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 4413 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4414 | zShm = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4415 | #ifdef SQLITE_SHM_DIRECTORY |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4416 | sqlite3_snprintf(nShmFilename, zShm, |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4417 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 4418 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 4419 | #else |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4420 | sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath); |
| 4421 | sqlite3FileSuffix3(pDbFd->zPath, zShm); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4422 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4423 | pShmNode->h = -1; |
| 4424 | pDbFd->pInode->pShmNode = pShmNode; |
| 4425 | pShmNode->pInode = pDbFd->pInode; |
drh | 97a7e5e | 2016-04-26 18:58:54 +0000 | [diff] [blame] | 4426 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 4427 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4428 | if( pShmNode->mutex==0 ){ |
| 4429 | rc = SQLITE_NOMEM_BKPT; |
| 4430 | goto shm_open_err; |
| 4431 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4432 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4433 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4434 | if( pInode->bProcessLock==0 ){ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4435 | if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
| 4436 | pShmNode->h = robust_open(zShm, O_RDWR|O_CREAT, (sStat.st_mode&0777)); |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4437 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4438 | if( pShmNode->h<0 ){ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4439 | pShmNode->h = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777)); |
| 4440 | if( pShmNode->h<0 ){ |
| 4441 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm); |
| 4442 | goto shm_open_err; |
| 4443 | } |
| 4444 | pShmNode->isReadonly = 1; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4445 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4446 | |
| 4447 | /* If this process is running as root, make sure that the SHM file |
| 4448 | ** is owned by the same user that owns the original database. Otherwise, |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4449 | ** the original owner will not be able to connect. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4450 | */ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 4451 | robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); |
dan | 176b2a9 | 2017-11-01 06:59:19 +0000 | [diff] [blame] | 4452 | |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4453 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4454 | if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4455 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4458 | /* Make the new connection a child of the unixShmNode */ |
| 4459 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4460 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4461 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4462 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4463 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4464 | pDbFd->pShm = p; |
| 4465 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4466 | |
| 4467 | /* The reference count on pShmNode has already been incremented under |
| 4468 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 4469 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 4470 | ** left to do is to link the new object into the linked list starting |
| 4471 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 4472 | ** mutex. |
| 4473 | */ |
| 4474 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4475 | p->pNext = pShmNode->pFirst; |
| 4476 | pShmNode->pFirst = p; |
| 4477 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4478 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4479 | |
| 4480 | /* Jump here on any error */ |
| 4481 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4482 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4483 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4484 | unixLeaveMutex(); |
| 4485 | return rc; |
| 4486 | } |
| 4487 | |
| 4488 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4489 | ** This function is called to obtain a pointer to region iRegion of the |
| 4490 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4491 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 4492 | ** bytes in size. |
| 4493 | ** |
| 4494 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 4495 | ** |
| 4496 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 4497 | ** region has not been allocated (by any client, including one running in a |
| 4498 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4499 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 4500 | ** been allocated, it is allocated by this function. |
| 4501 | ** |
| 4502 | ** If the shared-memory region has already been allocated or is allocated by |
| 4503 | ** this call as described above, then it is mapped into this processes |
| 4504 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4505 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4506 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4507 | static int unixShmMap( |
| 4508 | sqlite3_file *fd, /* Handle open on database file */ |
| 4509 | int iRegion, /* Region to retrieve */ |
| 4510 | int szRegion, /* Size of regions */ |
| 4511 | int bExtend, /* True to extend file if necessary */ |
| 4512 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4513 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4514 | unixFile *pDbFd = (unixFile*)fd; |
| 4515 | unixShm *p; |
| 4516 | unixShmNode *pShmNode; |
| 4517 | int rc = SQLITE_OK; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4518 | int nShmPerMap = unixShmRegionPerMap(); |
| 4519 | int nReqRegion; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4520 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4521 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4522 | if( pDbFd->pShm==0 ){ |
| 4523 | rc = unixOpenSharedMemory(pDbFd); |
| 4524 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4525 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4526 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4527 | p = pDbFd->pShm; |
| 4528 | pShmNode = p->pShmNode; |
| 4529 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4530 | if( pShmNode->isUnlocked ){ |
| 4531 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
| 4532 | if( rc!=SQLITE_OK ) goto shmpage_out; |
| 4533 | pShmNode->isUnlocked = 0; |
| 4534 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4535 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4536 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4537 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4538 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4539 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4540 | /* Minimum number of regions required to be mapped. */ |
| 4541 | nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; |
| 4542 | |
| 4543 | if( pShmNode->nRegion<nReqRegion ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4544 | char **apNew; /* New apRegion[] array */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4545 | int nByte = nReqRegion*szRegion; /* Minimum required file size */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4546 | struct stat sStat; /* Used by fstat() */ |
| 4547 | |
| 4548 | pShmNode->szRegion = szRegion; |
| 4549 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4550 | if( pShmNode->h>=0 ){ |
| 4551 | /* The requested region is not mapped into this processes address space. |
| 4552 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4553 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4554 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4555 | if( osFstat(pShmNode->h, &sStat) ){ |
| 4556 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4557 | goto shmpage_out; |
| 4558 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4559 | |
| 4560 | if( sStat.st_size<nByte ){ |
| 4561 | /* The requested memory region does not exist. If bExtend is set to |
| 4562 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4563 | */ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4564 | if( !bExtend ){ |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4565 | goto shmpage_out; |
| 4566 | } |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4567 | |
| 4568 | /* Alternatively, if bExtend is true, extend the file. Do this by |
| 4569 | ** writing a single byte to the end of each (OS) page being |
| 4570 | ** allocated or extended. Technically, we need only write to the |
| 4571 | ** last page in order to extend the file. But writing to all new |
| 4572 | ** pages forces the OS to allocate them immediately, which reduces |
| 4573 | ** the chances of SIGBUS while accessing the mapped region later on. |
| 4574 | */ |
| 4575 | else{ |
| 4576 | static const int pgsz = 4096; |
| 4577 | int iPg; |
| 4578 | |
| 4579 | /* Write to the last byte of each newly allocated or extended page */ |
| 4580 | assert( (nByte % pgsz)==0 ); |
| 4581 | for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 4582 | int x = 0; |
| 4583 | if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4584 | const char *zFile = pShmNode->zFilename; |
| 4585 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 4586 | goto shmpage_out; |
| 4587 | } |
| 4588 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4589 | } |
| 4590 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | /* Map the requested memory region into this processes address space. */ |
| 4594 | apNew = (char **)sqlite3_realloc( |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4595 | pShmNode->apRegion, nReqRegion*sizeof(char *) |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4596 | ); |
| 4597 | if( !apNew ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4598 | rc = SQLITE_IOERR_NOMEM_BKPT; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4599 | goto shmpage_out; |
| 4600 | } |
| 4601 | pShmNode->apRegion = apNew; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4602 | while( pShmNode->nRegion<nReqRegion ){ |
| 4603 | int nMap = szRegion*nShmPerMap; |
| 4604 | int i; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4605 | void *pMem; |
| 4606 | if( pShmNode->h>=0 ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4607 | pMem = osMmap(0, nMap, |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4608 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 4609 | MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4610 | ); |
| 4611 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4612 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4613 | goto shmpage_out; |
| 4614 | } |
| 4615 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4616 | pMem = sqlite3_malloc64(szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4617 | if( pMem==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4618 | rc = SQLITE_NOMEM_BKPT; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4619 | goto shmpage_out; |
| 4620 | } |
| 4621 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4622 | } |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4623 | |
| 4624 | for(i=0; i<nShmPerMap; i++){ |
| 4625 | pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; |
| 4626 | } |
| 4627 | pShmNode->nRegion += nShmPerMap; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4628 | } |
| 4629 | } |
| 4630 | |
| 4631 | shmpage_out: |
| 4632 | if( pShmNode->nRegion>iRegion ){ |
| 4633 | *pp = pShmNode->apRegion[iRegion]; |
| 4634 | }else{ |
| 4635 | *pp = 0; |
| 4636 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4637 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4638 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4639 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4640 | } |
| 4641 | |
| 4642 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4643 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4644 | ** |
| 4645 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4646 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4647 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4648 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4649 | */ |
| 4650 | static int unixShmLock( |
| 4651 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4652 | int ofst, /* First lock to acquire or release */ |
| 4653 | int n, /* Number of locks to acquire or release */ |
| 4654 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4655 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4656 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4657 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4658 | unixShm *pX; /* For looping over all siblings */ |
| 4659 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4660 | int rc = SQLITE_OK; /* Result code */ |
| 4661 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4662 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4663 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4664 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4665 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4666 | assert( n>=1 ); |
| 4667 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4668 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4669 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4670 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4671 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4672 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4673 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4674 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4675 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4676 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4677 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4678 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4679 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4680 | |
| 4681 | /* See if any siblings hold this same lock */ |
| 4682 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4683 | if( pX==p ) continue; |
| 4684 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4685 | allMask |= pX->sharedMask; |
| 4686 | } |
| 4687 | |
| 4688 | /* Unlock the system-level locks */ |
| 4689 | if( (mask & allMask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4690 | rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4691 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4692 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4693 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4694 | |
| 4695 | /* Undo the local locks */ |
| 4696 | if( rc==SQLITE_OK ){ |
| 4697 | p->exclMask &= ~mask; |
| 4698 | p->sharedMask &= ~mask; |
| 4699 | } |
| 4700 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4701 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4702 | |
| 4703 | /* Find out which shared locks are already held by sibling connections. |
| 4704 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4705 | ** SQLITE_BUSY. |
| 4706 | */ |
| 4707 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4708 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4709 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4710 | break; |
| 4711 | } |
| 4712 | allShared |= pX->sharedMask; |
| 4713 | } |
| 4714 | |
| 4715 | /* Get shared locks at the system level, if necessary */ |
| 4716 | if( rc==SQLITE_OK ){ |
| 4717 | if( (allShared & mask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4718 | rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4719 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4720 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4721 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4722 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4723 | |
| 4724 | /* Get the local shared locks */ |
| 4725 | if( rc==SQLITE_OK ){ |
| 4726 | p->sharedMask |= mask; |
| 4727 | } |
| 4728 | }else{ |
| 4729 | /* Make sure no sibling connections hold locks that will block this |
| 4730 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4731 | */ |
| 4732 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4733 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4734 | rc = SQLITE_BUSY; |
| 4735 | break; |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | /* Get the exclusive locks at the system level. Then if successful |
| 4740 | ** also mark the local connection as being locked. |
| 4741 | */ |
| 4742 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4743 | rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4744 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4745 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4746 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4747 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4748 | } |
| 4749 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4750 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4751 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 4752 | p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4753 | return rc; |
| 4754 | } |
| 4755 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4756 | /* |
| 4757 | ** Implement a memory barrier or memory fence on shared memory. |
| 4758 | ** |
| 4759 | ** All loads and stores begun before the barrier must complete before |
| 4760 | ** any load or store begun after the barrier. |
| 4761 | */ |
| 4762 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4763 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4764 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4765 | UNUSED_PARAMETER(fd); |
drh | 22c733d | 2015-09-24 12:40:43 +0000 | [diff] [blame] | 4766 | sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ |
| 4767 | unixEnterMutex(); /* Also mutex, for redundancy */ |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4768 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4771 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4772 | ** Close a connection to shared-memory. Delete the underlying |
| 4773 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4774 | ** |
| 4775 | ** If there is no shared memory associated with the connection then this |
| 4776 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4777 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4778 | static int unixShmUnmap( |
| 4779 | sqlite3_file *fd, /* The underlying database file */ |
| 4780 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4781 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4782 | unixShm *p; /* The connection to be closed */ |
| 4783 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4784 | unixShm **pp; /* For looping over sibling connections */ |
| 4785 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4786 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4787 | pDbFd = (unixFile*)fd; |
| 4788 | p = pDbFd->pShm; |
| 4789 | if( p==0 ) return SQLITE_OK; |
| 4790 | pShmNode = p->pShmNode; |
| 4791 | |
| 4792 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4793 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4794 | |
| 4795 | /* Remove connection p from the set of connections associated |
| 4796 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4797 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4798 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4799 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4800 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4801 | /* Free the connection p */ |
| 4802 | sqlite3_free(p); |
| 4803 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4804 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4805 | |
| 4806 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4807 | ** shared-memory file, too */ |
| 4808 | unixEnterMutex(); |
| 4809 | assert( pShmNode->nRef>0 ); |
| 4810 | pShmNode->nRef--; |
| 4811 | if( pShmNode->nRef==0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4812 | if( deleteFlag && pShmNode->h>=0 ){ |
| 4813 | osUnlink(pShmNode->zFilename); |
| 4814 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4815 | unixShmPurge(pDbFd); |
| 4816 | } |
| 4817 | unixLeaveMutex(); |
| 4818 | |
| 4819 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4820 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4821 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4822 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4823 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4824 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4825 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4826 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4827 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4828 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4829 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4830 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4831 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4832 | ** If it is currently memory mapped, unmap file pFd. |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4833 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4834 | static void unixUnmapfile(unixFile *pFd){ |
| 4835 | assert( pFd->nFetchOut==0 ); |
| 4836 | if( pFd->pMapRegion ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4837 | osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4838 | pFd->pMapRegion = 0; |
| 4839 | pFd->mmapSize = 0; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4840 | pFd->mmapSizeActual = 0; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4841 | } |
| 4842 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4843 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4844 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4845 | ** Attempt to set the size of the memory mapping maintained by file |
| 4846 | ** descriptor pFd to nNew bytes. Any existing mapping is discarded. |
| 4847 | ** |
| 4848 | ** If successful, this function sets the following variables: |
| 4849 | ** |
| 4850 | ** unixFile.pMapRegion |
| 4851 | ** unixFile.mmapSize |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4852 | ** unixFile.mmapSizeActual |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4853 | ** |
| 4854 | ** If unsuccessful, an error message is logged via sqlite3_log() and |
| 4855 | ** the three variables above are zeroed. In this case SQLite should |
| 4856 | ** continue accessing the database using the xRead() and xWrite() |
| 4857 | ** methods. |
| 4858 | */ |
| 4859 | static void unixRemapfile( |
| 4860 | unixFile *pFd, /* File descriptor object */ |
| 4861 | i64 nNew /* Required mapping size */ |
| 4862 | ){ |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4863 | const char *zErr = "mmap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4864 | int h = pFd->h; /* File descriptor open on db file */ |
| 4865 | u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4866 | i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4867 | u8 *pNew = 0; /* Location of new mapping */ |
| 4868 | int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 4869 | |
| 4870 | assert( pFd->nFetchOut==0 ); |
| 4871 | assert( nNew>pFd->mmapSize ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4872 | assert( nNew<=pFd->mmapSizeMax ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4873 | assert( nNew>0 ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4874 | assert( pFd->mmapSizeActual>=pFd->mmapSize ); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4875 | assert( MAP_FAILED!=0 ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4876 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 4877 | #ifdef SQLITE_MMAP_READWRITE |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4878 | if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 4879 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4880 | |
| 4881 | if( pOrig ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4882 | #if HAVE_MREMAP |
| 4883 | i64 nReuse = pFd->mmapSize; |
| 4884 | #else |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4885 | const int szSyspage = osGetpagesize(); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4886 | i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4887 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4888 | u8 *pReq = &pOrig[nReuse]; |
| 4889 | |
| 4890 | /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 4891 | if( nReuse!=nOrig ){ |
| 4892 | osMunmap(pReq, nOrig-nReuse); |
| 4893 | } |
| 4894 | |
| 4895 | #if HAVE_MREMAP |
| 4896 | pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4897 | zErr = "mremap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4898 | #else |
| 4899 | pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 4900 | if( pNew!=MAP_FAILED ){ |
| 4901 | if( pNew!=pReq ){ |
| 4902 | osMunmap(pNew, nNew - nReuse); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4903 | pNew = 0; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4904 | }else{ |
| 4905 | pNew = pOrig; |
| 4906 | } |
| 4907 | } |
| 4908 | #endif |
| 4909 | |
dan | 48ccef8 | 2013-04-02 20:55:01 +0000 | [diff] [blame] | 4910 | /* The attempt to extend the existing mapping failed. Free it. */ |
| 4911 | if( pNew==MAP_FAILED || pNew==0 ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4912 | osMunmap(pOrig, nReuse); |
| 4913 | } |
| 4914 | } |
| 4915 | |
| 4916 | /* If pNew is still NULL, try to create an entirely new mapping. */ |
| 4917 | if( pNew==0 ){ |
| 4918 | pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4921 | if( pNew==MAP_FAILED ){ |
| 4922 | pNew = 0; |
| 4923 | nNew = 0; |
| 4924 | unixLogError(SQLITE_OK, zErr, pFd->zPath); |
| 4925 | |
| 4926 | /* If the mmap() above failed, assume that all subsequent mmap() calls |
| 4927 | ** will probably fail too. Fall back to using xRead/xWrite exclusively |
| 4928 | ** in this case. */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4929 | pFd->mmapSizeMax = 0; |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4930 | } |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4931 | pFd->pMapRegion = (void *)pNew; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4932 | pFd->mmapSize = pFd->mmapSizeActual = nNew; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4936 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 4937 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 4938 | ** there already exists a mapping for this file, and there are still |
| 4939 | ** outstanding xFetch() references to it, this function is a no-op. |
| 4940 | ** |
| 4941 | ** If parameter nByte is non-negative, then it is the requested size of |
| 4942 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 4943 | ** requested size is the size of the file on disk. The actual size of the |
| 4944 | ** created mapping is either the requested size or the value configured |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 4945 | ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4946 | ** |
| 4947 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 4948 | ** recreated as a result of outstanding references) or an SQLite error |
| 4949 | ** code otherwise. |
| 4950 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4951 | static int unixMapfile(unixFile *pFd, i64 nMap){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4952 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 4953 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4954 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 4955 | |
| 4956 | if( nMap<0 ){ |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 4957 | struct stat statbuf; /* Low-level file information */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4958 | if( osFstat(pFd->h, &statbuf) ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4959 | return SQLITE_IOERR_FSTAT; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4960 | } |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 4961 | nMap = statbuf.st_size; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4962 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4963 | if( nMap>pFd->mmapSizeMax ){ |
| 4964 | nMap = pFd->mmapSizeMax; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 4967 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4968 | if( nMap!=pFd->mmapSize ){ |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 4969 | unixRemapfile(pFd, nMap); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4972 | return SQLITE_OK; |
| 4973 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4974 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4975 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4976 | /* |
| 4977 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 4978 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 4979 | ** |
| 4980 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 4981 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 4982 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 4983 | ** value of *pp is undefined in this case. |
| 4984 | ** |
| 4985 | ** If this function does return a pointer, the caller must eventually |
| 4986 | ** release the reference by calling unixUnfetch(). |
| 4987 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4988 | static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4989 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4990 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 4991 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4992 | *pp = 0; |
| 4993 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4994 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 4995 | if( pFd->mmapSizeMax>0 ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4996 | if( pFd->pMapRegion==0 ){ |
| 4997 | int rc = unixMapfile(pFd, -1); |
| 4998 | if( rc!=SQLITE_OK ) return rc; |
| 4999 | } |
| 5000 | if( pFd->mmapSize >= iOff+nAmt ){ |
| 5001 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 5002 | pFd->nFetchOut++; |
| 5003 | } |
| 5004 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 5005 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5006 | return SQLITE_OK; |
| 5007 | } |
| 5008 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5009 | /* |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5010 | ** If the third argument is non-NULL, then this function releases a |
| 5011 | ** reference obtained by an earlier call to unixFetch(). The second |
| 5012 | ** argument passed to this function must be the same as the corresponding |
| 5013 | ** argument that was passed to the unixFetch() invocation. |
| 5014 | ** |
| 5015 | ** Or, if the third argument is NULL, then this function is being called |
| 5016 | ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 5017 | ** may now be invalid and should be unmapped. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5018 | */ |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5019 | static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5020 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5021 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 5022 | UNUSED_PARAMETER(iOff); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5023 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5024 | /* If p==0 (unmap the entire file) then there must be no outstanding |
| 5025 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 5026 | ** then there must be at least one outstanding. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5027 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 5028 | |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5029 | /* If p!=0, it must match the iOff value. */ |
| 5030 | assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 5031 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5032 | if( p ){ |
| 5033 | pFd->nFetchOut--; |
| 5034 | }else{ |
| 5035 | unixUnmapfile(pFd); |
| 5036 | } |
| 5037 | |
| 5038 | assert( pFd->nFetchOut>=0 ); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5039 | #else |
| 5040 | UNUSED_PARAMETER(fd); |
| 5041 | UNUSED_PARAMETER(p); |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 5042 | UNUSED_PARAMETER(iOff); |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5043 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5044 | return SQLITE_OK; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5048 | ** Here ends the implementation of all sqlite3_file methods. |
| 5049 | ** |
| 5050 | ********************** End sqlite3_file Methods ******************************* |
| 5051 | ******************************************************************************/ |
| 5052 | |
| 5053 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5054 | ** This division contains definitions of sqlite3_io_methods objects that |
| 5055 | ** implement various file locking strategies. It also contains definitions |
| 5056 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 5057 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 5058 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 5059 | ** the correct finder-function for that VFS. |
| 5060 | ** |
| 5061 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 5062 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 5063 | ** looks at the filesystem type and tries to guess the best locking |
| 5064 | ** strategy from that. |
| 5065 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5066 | ** For finder-function F, two objects are created: |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5067 | ** |
| 5068 | ** (1) The real finder-function named "FImpt()". |
| 5069 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5070 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5071 | ** |
| 5072 | ** |
| 5073 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 5074 | ** objects. We have to do this instead of letting pAppData point |
| 5075 | ** directly at the finder-function since C90 rules prevent a void* |
| 5076 | ** from be cast into a function pointer. |
| 5077 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5078 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5079 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5080 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5081 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 5082 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 5083 | ** |
| 5084 | ** * An I/O method finder function called FINDER that returns a pointer |
| 5085 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5086 | */ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 5087 | #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5088 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5089 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5090 | CLOSE, /* xClose */ \ |
| 5091 | unixRead, /* xRead */ \ |
| 5092 | unixWrite, /* xWrite */ \ |
| 5093 | unixTruncate, /* xTruncate */ \ |
| 5094 | unixSync, /* xSync */ \ |
| 5095 | unixFileSize, /* xFileSize */ \ |
| 5096 | LOCK, /* xLock */ \ |
| 5097 | UNLOCK, /* xUnlock */ \ |
| 5098 | CKLOCK, /* xCheckReservedLock */ \ |
| 5099 | unixFileControl, /* xFileControl */ \ |
| 5100 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5101 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5102 | SHMMAP, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5103 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 5104 | unixShmBarrier, /* xShmBarrier */ \ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5105 | unixShmUnmap, /* xShmUnmap */ \ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5106 | unixFetch, /* xFetch */ \ |
| 5107 | unixUnfetch, /* xUnfetch */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5108 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5109 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 5110 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5111 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5112 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5113 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5114 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5115 | |
| 5116 | /* |
| 5117 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 5118 | ** locking strategies. Functions that return pointers to these methods |
| 5119 | ** are also created. |
| 5120 | */ |
| 5121 | IOMETHODS( |
| 5122 | posixIoFinder, /* Finder function name */ |
| 5123 | posixIoMethods, /* sqlite3_io_methods object name */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5124 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5125 | unixClose, /* xClose method */ |
| 5126 | unixLock, /* xLock method */ |
| 5127 | unixUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5128 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5129 | unixShmMap /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5130 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5131 | IOMETHODS( |
| 5132 | nolockIoFinder, /* Finder function name */ |
| 5133 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 142341c | 2014-09-19 19:00:48 +0000 | [diff] [blame] | 5134 | 3, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5135 | nolockClose, /* xClose method */ |
| 5136 | nolockLock, /* xLock method */ |
| 5137 | nolockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5138 | nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 5139 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5140 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5141 | IOMETHODS( |
| 5142 | dotlockIoFinder, /* Finder function name */ |
| 5143 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5144 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5145 | dotlockClose, /* xClose method */ |
| 5146 | dotlockLock, /* xLock method */ |
| 5147 | dotlockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5148 | dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 5149 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5150 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5151 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5152 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5153 | IOMETHODS( |
| 5154 | flockIoFinder, /* Finder function name */ |
| 5155 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5156 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5157 | flockClose, /* xClose method */ |
| 5158 | flockLock, /* xLock method */ |
| 5159 | flockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5160 | flockCheckReservedLock, /* xCheckReservedLock method */ |
| 5161 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5162 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5163 | #endif |
| 5164 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5165 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5166 | IOMETHODS( |
| 5167 | semIoFinder, /* Finder function name */ |
| 5168 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5169 | 1, /* shared memory is disabled */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5170 | semXClose, /* xClose method */ |
| 5171 | semXLock, /* xLock method */ |
| 5172 | semXUnlock, /* xUnlock method */ |
| 5173 | semXCheckReservedLock, /* xCheckReservedLock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5174 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5175 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5176 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5177 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5178 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5179 | IOMETHODS( |
| 5180 | afpIoFinder, /* Finder function name */ |
| 5181 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5182 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5183 | afpClose, /* xClose method */ |
| 5184 | afpLock, /* xLock method */ |
| 5185 | afpUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5186 | afpCheckReservedLock, /* xCheckReservedLock method */ |
| 5187 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5188 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5189 | #endif |
| 5190 | |
| 5191 | /* |
| 5192 | ** The proxy locking method is a "super-method" in the sense that it |
| 5193 | ** opens secondary file descriptors for the conch and lock files and |
| 5194 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 5195 | ** secondary files. For this reason, the division that implements |
| 5196 | ** proxy locking is located much further down in the file. But we need |
| 5197 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 5198 | ** for proxy locking here. So we forward declare the I/O methods. |
| 5199 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5200 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5201 | static int proxyClose(sqlite3_file*); |
| 5202 | static int proxyLock(sqlite3_file*, int); |
| 5203 | static int proxyUnlock(sqlite3_file*, int); |
| 5204 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5205 | IOMETHODS( |
| 5206 | proxyIoFinder, /* Finder function name */ |
| 5207 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5208 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5209 | proxyClose, /* xClose method */ |
| 5210 | proxyLock, /* xLock method */ |
| 5211 | proxyUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5212 | proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 5213 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5214 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5215 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5216 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5217 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 5218 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5219 | IOMETHODS( |
| 5220 | nfsIoFinder, /* Finder function name */ |
| 5221 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5222 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5223 | unixClose, /* xClose method */ |
| 5224 | unixLock, /* xLock method */ |
| 5225 | nfsUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5226 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5227 | 0 /* xShmMap method */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5228 | ) |
| 5229 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5230 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5231 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5232 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5233 | ** This "finder" function attempts to determine the best locking strategy |
| 5234 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5235 | ** object that implements that strategy. |
| 5236 | ** |
| 5237 | ** This is for MacOSX only. |
| 5238 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5239 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5240 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5241 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5242 | ){ |
| 5243 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5244 | const char *zFilesystem; /* Filesystem type name */ |
| 5245 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5246 | } aMap[] = { |
| 5247 | { "hfs", &posixIoMethods }, |
| 5248 | { "ufs", &posixIoMethods }, |
| 5249 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5250 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5251 | { "webdav", &nolockIoMethods }, |
| 5252 | { 0, 0 } |
| 5253 | }; |
| 5254 | int i; |
| 5255 | struct statfs fsInfo; |
| 5256 | struct flock lockInfo; |
| 5257 | |
| 5258 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5259 | /* If filePath==NULL that means we are dealing with a transient file |
| 5260 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5261 | return &nolockIoMethods; |
| 5262 | } |
| 5263 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 5264 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 5265 | return &nolockIoMethods; |
| 5266 | } |
| 5267 | for(i=0; aMap[i].zFilesystem; i++){ |
| 5268 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 5269 | return aMap[i].pMethods; |
| 5270 | } |
| 5271 | } |
| 5272 | } |
| 5273 | |
| 5274 | /* Default case. Handles, amongst others, "nfs". |
| 5275 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 5276 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5277 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5278 | lockInfo.l_len = 1; |
| 5279 | lockInfo.l_start = 0; |
| 5280 | lockInfo.l_whence = SEEK_SET; |
| 5281 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5282 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5283 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 5284 | return &nfsIoMethods; |
| 5285 | } else { |
| 5286 | return &posixIoMethods; |
| 5287 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5288 | }else{ |
| 5289 | return &dotlockIoMethods; |
| 5290 | } |
| 5291 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5292 | static const sqlite3_io_methods |
| 5293 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5294 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5295 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5296 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5297 | #if OS_VXWORKS |
| 5298 | /* |
| 5299 | ** This "finder" function for VxWorks checks to see if posix advisory |
| 5300 | ** locking works. If it does, then that is what is used. If it does not |
| 5301 | ** work, then fallback to named semaphore locking. |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5302 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5303 | static const sqlite3_io_methods *vxworksIoFinderImpl( |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5304 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5305 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5306 | ){ |
| 5307 | struct flock lockInfo; |
| 5308 | |
| 5309 | if( !filePath ){ |
| 5310 | /* If filePath==NULL that means we are dealing with a transient file |
| 5311 | ** that does not need to be locked. */ |
| 5312 | return &nolockIoMethods; |
| 5313 | } |
| 5314 | |
| 5315 | /* Test if fcntl() is supported and use POSIX style locks. |
| 5316 | ** Otherwise fall back to the named semaphore method. |
| 5317 | */ |
| 5318 | lockInfo.l_len = 1; |
| 5319 | lockInfo.l_start = 0; |
| 5320 | lockInfo.l_whence = SEEK_SET; |
| 5321 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5322 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5323 | return &posixIoMethods; |
| 5324 | }else{ |
| 5325 | return &semIoMethods; |
| 5326 | } |
| 5327 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5328 | static const sqlite3_io_methods |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5329 | *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5330 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5331 | #endif /* OS_VXWORKS */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5332 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5333 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5334 | ** An abstract type for a pointer to an IO method finder function: |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5335 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5336 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5337 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5338 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5339 | /**************************************************************************** |
| 5340 | **************************** sqlite3_vfs methods **************************** |
| 5341 | ** |
| 5342 | ** This division contains the implementation of methods on the |
| 5343 | ** sqlite3_vfs object. |
| 5344 | */ |
| 5345 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 5346 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5347 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5348 | */ |
| 5349 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5350 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5351 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5352 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5353 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5354 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5355 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5356 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5357 | unixFile *pNew = (unixFile *)pId; |
| 5358 | int rc = SQLITE_OK; |
| 5359 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5360 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5361 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5362 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5363 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5364 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5365 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5366 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5367 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5368 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5369 | pNew->ctrlFlags = (u8)ctrlFlags; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5370 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | ede01a9 | 2013-05-17 12:10:52 +0000 | [diff] [blame] | 5371 | pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5372 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5373 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 5374 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5375 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 5376 | } |
drh | 503a686 | 2013-03-01 01:07:17 +0000 | [diff] [blame] | 5377 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5378 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5379 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 5380 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5381 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5382 | pNew->pId = vxworksFindFileId(zFilename); |
| 5383 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5384 | ctrlFlags |= UNIXFILE_NOLOCK; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5385 | rc = SQLITE_NOMEM_BKPT; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5386 | } |
| 5387 | #endif |
| 5388 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5389 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5390 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5391 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5392 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5393 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5394 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 5395 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 5396 | ** zFilename remains valid until file is closed, to support */ |
| 5397 | pNew->lockingContext = (void*)zFilename; |
| 5398 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5399 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5400 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5401 | if( pLockingStyle == &posixIoMethods |
| 5402 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5403 | || pLockingStyle == &nfsIoMethods |
| 5404 | #endif |
| 5405 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5406 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5407 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5408 | if( rc!=SQLITE_OK ){ |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5409 | /* If an error occurred in findInodeInfo(), close the file descriptor |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5410 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5411 | ** in two scenarios: |
| 5412 | ** |
| 5413 | ** (a) A call to fstat() failed. |
| 5414 | ** (b) A malloc failed. |
| 5415 | ** |
| 5416 | ** Scenario (b) may only occur if the process is holding no other |
| 5417 | ** file descriptors open on the same file. If there were other file |
| 5418 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5419 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5420 | ** handle h - as it is guaranteed that no posix locks will be released |
| 5421 | ** by doing so. |
| 5422 | ** |
| 5423 | ** If scenario (a) caused the error then things are not so safe. The |
| 5424 | ** implicit assumption here is that if fstat() fails, things are in |
| 5425 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 5426 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5427 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5428 | h = -1; |
| 5429 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5430 | unixLeaveMutex(); |
| 5431 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5432 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5433 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 5434 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5435 | /* AFP locking uses the file path so it needs to be included in |
| 5436 | ** the afpLockingContext. |
| 5437 | */ |
| 5438 | afpLockingContext *pCtx; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5439 | pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5440 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5441 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5442 | }else{ |
| 5443 | /* NB: zFilename exists and remains valid until the file is closed |
| 5444 | ** according to requirement F11141. So we do not need to make a |
| 5445 | ** copy of the filename. */ |
| 5446 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5447 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5448 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5449 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5450 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5451 | if( rc!=SQLITE_OK ){ |
| 5452 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5453 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5454 | h = -1; |
| 5455 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5456 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5457 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5458 | } |
| 5459 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5460 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5461 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 5462 | /* Dotfile locking uses the file path so it needs to be included in |
| 5463 | ** the dotlockLockingContext |
| 5464 | */ |
| 5465 | char *zLockFile; |
| 5466 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5467 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5468 | nFilename = (int)strlen(zFilename) + 6; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5469 | zLockFile = (char *)sqlite3_malloc64(nFilename); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5470 | if( zLockFile==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5471 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5472 | }else{ |
| 5473 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5474 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5475 | pNew->lockingContext = zLockFile; |
| 5476 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5477 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5478 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5479 | else if( pLockingStyle == &semIoMethods ){ |
| 5480 | /* Named semaphore locking uses the file path so it needs to be |
| 5481 | ** included in the semLockingContext |
| 5482 | */ |
| 5483 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5484 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 5485 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 5486 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5487 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5488 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5489 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5490 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5491 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5492 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 5493 | if( pNew->pInode->pSem == SEM_FAILED ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5494 | rc = SQLITE_NOMEM_BKPT; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5495 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5496 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5497 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5498 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5499 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5500 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 5501 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5502 | storeLastErrno(pNew, 0); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5503 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5504 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5505 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 5506 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5507 | osUnlink(zFilename); |
drh | c579754 | 2013-04-27 12:13:29 +0000 | [diff] [blame] | 5508 | pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5509 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5510 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5511 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5512 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5513 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5514 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5515 | OpenCounter(+1); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5516 | verifyDbFile(pNew); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5517 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5518 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5519 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 5520 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5521 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5522 | ** Return the name of a directory in which to put temporary files. |
| 5523 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5524 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5525 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5526 | static const char *azDirs[] = { |
| 5527 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5528 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5529 | "/var/tmp", |
| 5530 | "/usr/tmp", |
| 5531 | "/tmp", |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5532 | "." |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5533 | }; |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5534 | unsigned int i = 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5535 | struct stat buf; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5536 | const char *zDir = sqlite3_temp_directory; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5537 | |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5538 | if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); |
| 5539 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5540 | while(1){ |
| 5541 | if( zDir!=0 |
| 5542 | && osStat(zDir, &buf)==0 |
| 5543 | && S_ISDIR(buf.st_mode) |
| 5544 | && osAccess(zDir, 03)==0 |
| 5545 | ){ |
| 5546 | return zDir; |
| 5547 | } |
| 5548 | if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break; |
| 5549 | zDir = azDirs[i++]; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5550 | } |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5551 | return 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | /* |
| 5555 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 5556 | ** by the calling process and must be big enough to hold at least |
| 5557 | ** pVfs->mxPathname bytes. |
| 5558 | */ |
| 5559 | static int unixGetTempname(int nBuf, char *zBuf){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5560 | const char *zDir; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5561 | int iLimit = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5562 | |
| 5563 | /* It's odd to simulate an io-error here, but really this is just |
| 5564 | ** using the io-error infrastructure to test that SQLite handles this |
| 5565 | ** function failing. |
| 5566 | */ |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5567 | zBuf[0] = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5568 | SimulateIOError( return SQLITE_IOERR ); |
| 5569 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5570 | zDir = unixTempFileDir(); |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5571 | if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5572 | do{ |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 5573 | u64 r; |
| 5574 | sqlite3_randomness(sizeof(r), &r); |
| 5575 | assert( nBuf>2 ); |
| 5576 | zBuf[nBuf-2] = 0; |
| 5577 | sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", |
| 5578 | zDir, r, 0); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5579 | if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5580 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5581 | return SQLITE_OK; |
| 5582 | } |
| 5583 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5584 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5585 | /* |
| 5586 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 5587 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 5588 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 5589 | */ |
| 5590 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 5591 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5592 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5593 | /* |
| 5594 | ** Search for an unused file descriptor that was opened on the database |
| 5595 | ** file (not a journal or master-journal file) identified by pathname |
| 5596 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 5597 | ** argument to this function. |
| 5598 | ** |
| 5599 | ** Such a file descriptor may exist if a database connection was closed |
| 5600 | ** but the associated file descriptor could not be closed because some |
| 5601 | ** other file descriptor open on the same file is holding a file-lock. |
| 5602 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 5603 | ** describing "Posix Advisory Locking" at the start of this file for |
| 5604 | ** further details. Also, ticket #4018. |
| 5605 | ** |
| 5606 | ** If a suitable file descriptor is found, then it is returned. If no |
| 5607 | ** such file descriptor is located, -1 is returned. |
| 5608 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5609 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 5610 | UnixUnusedFd *pUnused = 0; |
| 5611 | |
| 5612 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 5613 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 5614 | ** but because no way to test it is currently available. It is better |
| 5615 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 5616 | ** feature. */ |
| 5617 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5618 | struct stat sStat; /* Results of stat() call */ |
| 5619 | |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5620 | unixEnterMutex(); |
| 5621 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5622 | /* A stat() call may fail for various reasons. If this happens, it is |
| 5623 | ** almost certain that an open() call on the same path will also fail. |
| 5624 | ** For this reason, if an error occurs in the stat() call here, it is |
| 5625 | ** ignored and -1 is returned. The caller will try to open a new file |
| 5626 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 5627 | ** |
| 5628 | ** Even if a subsequent open() call does succeed, the consequences of |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5629 | ** not searching for a reusable file descriptor are not dire. */ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5630 | if( nUnusedFd>0 && 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 5631 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5632 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5633 | pInode = inodeList; |
| 5634 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 5635 | || pInode->fileId.ino!=(u64)sStat.st_ino) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5636 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 5637 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5638 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5639 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5640 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5641 | pUnused = *pp; |
| 5642 | if( pUnused ){ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5643 | nUnusedFd--; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5644 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5645 | } |
| 5646 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5647 | } |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5648 | unixLeaveMutex(); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5649 | #endif /* if !OS_VXWORKS */ |
| 5650 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5651 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5652 | |
| 5653 | /* |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5654 | ** Find the mode, uid and gid of file zFile. |
| 5655 | */ |
| 5656 | static int getFileMode( |
| 5657 | const char *zFile, /* File name */ |
| 5658 | mode_t *pMode, /* OUT: Permissions of zFile */ |
| 5659 | uid_t *pUid, /* OUT: uid of zFile. */ |
| 5660 | gid_t *pGid /* OUT: gid of zFile. */ |
| 5661 | ){ |
| 5662 | struct stat sStat; /* Output of stat() on database file */ |
| 5663 | int rc = SQLITE_OK; |
| 5664 | if( 0==osStat(zFile, &sStat) ){ |
| 5665 | *pMode = sStat.st_mode & 0777; |
| 5666 | *pUid = sStat.st_uid; |
| 5667 | *pGid = sStat.st_gid; |
| 5668 | }else{ |
| 5669 | rc = SQLITE_IOERR_FSTAT; |
| 5670 | } |
| 5671 | return rc; |
| 5672 | } |
| 5673 | |
| 5674 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5675 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 5676 | ** to create new files with. If no error occurs, then SQLITE_OK is returned |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5677 | ** and a value suitable for passing as the third argument to open(2) is |
| 5678 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 5679 | ** returned and the value of *pMode is not modified. |
| 5680 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5681 | ** In most cases, this routine sets *pMode to 0, which will become |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5682 | ** an indication to robust_open() to create the file using |
| 5683 | ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 5684 | ** But if the file being opened is a WAL or regular journal file, then |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5685 | ** this function queries the file-system for the permissions on the |
| 5686 | ** corresponding database file and sets *pMode to this value. Whenever |
| 5687 | ** possible, WAL and journal files are created using the same permissions |
| 5688 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5689 | ** |
| 5690 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 5691 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 5692 | ** FAT filesystems and permissions do not matter there, so just use |
| 5693 | ** the default permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5694 | */ |
| 5695 | static int findCreateFileMode( |
| 5696 | const char *zPath, /* Path of file (possibly) being created */ |
| 5697 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5698 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 5699 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 5700 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5701 | ){ |
| 5702 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5703 | *pMode = 0; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5704 | *pUid = 0; |
| 5705 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5706 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5707 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 5708 | int nDb; /* Number of valid bytes in zDb */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5709 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5710 | /* zPath is a path to a WAL or journal file. The following block derives |
| 5711 | ** the path to the associated database file from zPath. This block handles |
| 5712 | ** the following naming conventions: |
| 5713 | ** |
| 5714 | ** "<path to db>-journal" |
| 5715 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5716 | ** "<path to db>-journalNN" |
| 5717 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5718 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5719 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5720 | ** used by the test_multiplex.c module. |
| 5721 | */ |
| 5722 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5723 | while( zPath[nDb]!='-' ){ |
dan | 629ec14 | 2017-09-14 20:41:17 +0000 | [diff] [blame] | 5724 | /* In normal operation, the journal file name will always contain |
| 5725 | ** a '-' character. However in 8+3 filename mode, or if a corrupt |
| 5726 | ** rollback journal specifies a master journal with a goofy name, then |
| 5727 | ** the '-' might be missing. */ |
drh | 90e5dda | 2015-12-03 20:42:28 +0000 | [diff] [blame] | 5728 | if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5729 | nDb--; |
| 5730 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5731 | memcpy(zDb, zPath, nDb); |
| 5732 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5733 | |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5734 | rc = getFileMode(zDb, pMode, pUid, pGid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5735 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 5736 | *pMode = 0600; |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5737 | }else if( flags & SQLITE_OPEN_URI ){ |
| 5738 | /* If this is a main database file and the file was opened using a URI |
| 5739 | ** filename, check for the "modeof" parameter. If present, interpret |
| 5740 | ** its value as a filename and try to copy the mode, uid and gid from |
| 5741 | ** that file. */ |
| 5742 | const char *z = sqlite3_uri_parameter(zPath, "modeof"); |
| 5743 | if( z ){ |
| 5744 | rc = getFileMode(z, pMode, pUid, pGid); |
| 5745 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5746 | } |
| 5747 | return rc; |
| 5748 | } |
| 5749 | |
| 5750 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5751 | ** Open the file zPath. |
| 5752 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5753 | ** Previously, the SQLite OS layer used three functions in place of this |
| 5754 | ** one: |
| 5755 | ** |
| 5756 | ** sqlite3OsOpenReadWrite(); |
| 5757 | ** sqlite3OsOpenReadOnly(); |
| 5758 | ** sqlite3OsOpenExclusive(); |
| 5759 | ** |
| 5760 | ** These calls correspond to the following combinations of flags: |
| 5761 | ** |
| 5762 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5763 | ** ReadOnly() -> (READONLY) |
| 5764 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5765 | ** |
| 5766 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5767 | ** true, the file was configured to be automatically deleted when the |
| 5768 | ** file handle closed. To achieve the same effect using this new |
| 5769 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5770 | ** OpenExclusive(). |
| 5771 | */ |
| 5772 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5773 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5774 | const char *zPath, /* Pathname of file to be opened */ |
| 5775 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5776 | int flags, /* Input flags to control the opening */ |
| 5777 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5778 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5779 | unixFile *p = (unixFile *)pFile; |
| 5780 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5781 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5782 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5783 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5784 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5785 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5786 | |
| 5787 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5788 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5789 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5790 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5791 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5792 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5793 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5794 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5795 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5796 | struct statfs fsInfo; |
| 5797 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5798 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5799 | /* If creating a master or main-file journal, this function will open |
| 5800 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5801 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5802 | */ |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 5803 | int isNewJrnl = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5804 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5805 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5806 | || eType==SQLITE_OPEN_WAL |
| 5807 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5808 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5809 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5810 | ** a temporary file. Use this buffer to store the file name in. |
| 5811 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5812 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5813 | const char *zName = zPath; |
| 5814 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5815 | /* Check the following statements are true: |
| 5816 | ** |
| 5817 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5818 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5819 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5820 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5821 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5822 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5823 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5824 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5825 | assert(isDelete==0 || isCreate); |
| 5826 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5827 | /* The main DB, main journal, WAL file and master journal are never |
| 5828 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5829 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5830 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5831 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5832 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5833 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5834 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5835 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5836 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5837 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5838 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5839 | ); |
| 5840 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5841 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 5842 | ** here such that two or more threads all trying to open databases at |
| 5843 | ** the same instant might all reset the PRNG. But multiple resets |
| 5844 | ** are harmless. |
| 5845 | */ |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 5846 | if( randomnessPid!=osGetpid(0) ){ |
| 5847 | randomnessPid = osGetpid(0); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5848 | sqlite3_randomness(0,0); |
| 5849 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5850 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5851 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5852 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5853 | UnixUnusedFd *pUnused; |
| 5854 | pUnused = findReusableFd(zName, flags); |
| 5855 | if( pUnused ){ |
| 5856 | fd = pUnused->fd; |
| 5857 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5858 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5859 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5860 | return SQLITE_NOMEM_BKPT; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5861 | } |
| 5862 | } |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5863 | p->pPreallocatedUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5864 | |
| 5865 | /* Database filenames are double-zero terminated if they are not |
| 5866 | ** URIs with parameters. Hence, they can always be passed into |
| 5867 | ** sqlite3_uri_parameter(). */ |
| 5868 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 5869 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5870 | }else if( !zName ){ |
| 5871 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 5872 | assert(isDelete && !isNewJrnl); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5873 | rc = unixGetTempname(pVfs->mxPathname, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5874 | if( rc!=SQLITE_OK ){ |
| 5875 | return rc; |
| 5876 | } |
| 5877 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5878 | |
| 5879 | /* Generated temporary filenames are always double-zero terminated |
| 5880 | ** for use by sqlite3_uri_parameter(). */ |
| 5881 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5882 | } |
| 5883 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5884 | /* Determine the value of the flags parameter passed to POSIX function |
| 5885 | ** open(). These must be calculated even if open() is not called, as |
| 5886 | ** they may be stored as part of the file handle and used by the |
| 5887 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5888 | if( isReadonly ) openFlags |= O_RDONLY; |
| 5889 | if( isReadWrite ) openFlags |= O_RDWR; |
| 5890 | if( isCreate ) openFlags |= O_CREAT; |
| 5891 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 5892 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5893 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5894 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5895 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5896 | uid_t uid; /* Userid for the file */ |
| 5897 | gid_t gid; /* Groupid for the file */ |
| 5898 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5899 | if( rc!=SQLITE_OK ){ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5900 | assert( !p->pPreallocatedUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5901 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5902 | return rc; |
| 5903 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5904 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5905 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
drh | 5a2d970 | 2015-11-26 02:21:05 +0000 | [diff] [blame] | 5906 | assert( !isExclusive || (openFlags & O_CREAT)!=0 ); |
dan | a688ca5 | 2018-01-10 11:56:03 +0000 | [diff] [blame] | 5907 | if( fd<0 ){ |
| 5908 | if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){ |
| 5909 | /* If unable to create a journal because the directory is not |
| 5910 | ** writable, change the error code to indicate that. */ |
| 5911 | rc = SQLITE_READONLY_DIRECTORY; |
| 5912 | }else if( errno!=EISDIR && isReadWrite ){ |
| 5913 | /* Failed to open the file for read/write access. Try read-only. */ |
| 5914 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
| 5915 | openFlags &= ~(O_RDWR|O_CREAT); |
| 5916 | flags |= SQLITE_OPEN_READONLY; |
| 5917 | openFlags |= O_RDONLY; |
| 5918 | isReadonly = 1; |
| 5919 | fd = robust_open(zName, openFlags, openMode); |
| 5920 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5921 | } |
| 5922 | if( fd<0 ){ |
dan | a688ca5 | 2018-01-10 11:56:03 +0000 | [diff] [blame] | 5923 | int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
| 5924 | if( rc==SQLITE_OK ) rc = rc2; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5925 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5926 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5927 | |
| 5928 | /* If this process is running as root and if creating a new rollback |
| 5929 | ** journal or WAL file, set the ownership of the journal or WAL to be |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 5930 | ** the same as the original database. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5931 | */ |
| 5932 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 5933 | robustFchown(fd, uid, gid); |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5934 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5935 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5936 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5937 | if( pOutFlags ){ |
| 5938 | *pOutFlags = flags; |
| 5939 | } |
| 5940 | |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5941 | if( p->pPreallocatedUnused ){ |
| 5942 | p->pPreallocatedUnused->fd = fd; |
| 5943 | p->pPreallocatedUnused->flags = flags; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5944 | } |
| 5945 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5946 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5947 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5948 | zPath = zName; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 5949 | #elif defined(SQLITE_UNLINK_AFTER_CLOSE) |
| 5950 | zPath = sqlite3_mprintf("%s", zName); |
| 5951 | if( zPath==0 ){ |
| 5952 | robust_close(p, fd, __LINE__); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5953 | return SQLITE_NOMEM_BKPT; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 5954 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5955 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5956 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5957 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5958 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5959 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5960 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5961 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 5962 | } |
| 5963 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5964 | |
| 5965 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5966 | if( fstatfs(fd, &fsInfo) == -1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5967 | storeLastErrno(p, errno); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5968 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5969 | return SQLITE_IOERR_ACCESS; |
| 5970 | } |
| 5971 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5972 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5973 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5974 | if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) { |
| 5975 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5976 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5977 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5978 | |
| 5979 | /* Set up appropriate ctrlFlags */ |
| 5980 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 5981 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 5982 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5983 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 5984 | if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5985 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 5986 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5987 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5988 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5989 | isAutoProxy = 1; |
| 5990 | #endif |
| 5991 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5992 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5993 | int useProxy = 0; |
| 5994 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5995 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5996 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5997 | if( envforce!=NULL ){ |
| 5998 | useProxy = atoi(envforce)>0; |
| 5999 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6000 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 6001 | } |
| 6002 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6003 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6004 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6005 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6006 | if( rc!=SQLITE_OK ){ |
| 6007 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 6008 | ** and clear all the structure's references. Specifically, |
| 6009 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 6010 | */ |
| 6011 | unixClose(pFile); |
| 6012 | return rc; |
| 6013 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6014 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6015 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6016 | } |
| 6017 | } |
| 6018 | #endif |
| 6019 | |
dan | 3ed0f1c | 2017-09-14 21:12:07 +0000 | [diff] [blame] | 6020 | assert( zPath==0 || zPath[0]=='/' |
| 6021 | || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 6022 | ); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6023 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 6024 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6025 | open_finished: |
| 6026 | if( rc!=SQLITE_OK ){ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6027 | sqlite3_free(p->pPreallocatedUnused); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6028 | } |
| 6029 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6030 | } |
| 6031 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6032 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6033 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6034 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 6035 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6036 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6037 | static int unixDelete( |
| 6038 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 6039 | const char *zPath, /* Name of file to be deleted */ |
| 6040 | int dirSync /* If true, fsync() directory after deleting file */ |
| 6041 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6042 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6043 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6044 | SimulateIOError(return SQLITE_IOERR_DELETE); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6045 | if( osUnlink(zPath)==(-1) ){ |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 6046 | if( errno==ENOENT |
| 6047 | #if OS_VXWORKS |
drh | 19541f3 | 2014-09-01 13:37:55 +0000 | [diff] [blame] | 6048 | || osAccess(zPath,0)!=0 |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 6049 | #endif |
| 6050 | ){ |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6051 | rc = SQLITE_IOERR_DELETE_NOENT; |
| 6052 | }else{ |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 6053 | rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6054 | } |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 6055 | return rc; |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 6056 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 6057 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 6058 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6059 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 6060 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6061 | if( rc==SQLITE_OK ){ |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 6062 | if( full_fsync(fd,0,0) ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6063 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6064 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6065 | robust_close(0, fd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 6066 | }else{ |
| 6067 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 6068 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6069 | } |
| 6070 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 6071 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6072 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6073 | } |
| 6074 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6075 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 6076 | ** Test the existence of or access permissions of file zPath. The |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6077 | ** test performed depends on the value of flags: |
| 6078 | ** |
| 6079 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 6080 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 6081 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 6082 | ** |
| 6083 | ** Otherwise return 0. |
| 6084 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6085 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6086 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 6087 | const char *zPath, /* Path of the file to examine */ |
| 6088 | int flags, /* What do we want to learn about the zPath file? */ |
| 6089 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6090 | ){ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6091 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6092 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6093 | assert( pResOut!=0 ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6094 | |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6095 | /* The spec says there are three possible values for flags. But only |
| 6096 | ** two of them are actually used */ |
| 6097 | assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE ); |
| 6098 | |
| 6099 | if( flags==SQLITE_ACCESS_EXISTS ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6100 | struct stat buf; |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6101 | *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0); |
| 6102 | }else{ |
| 6103 | *pResOut = osAccess(zPath, W_OK|R_OK)==0; |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6104 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6105 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6108 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6109 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6110 | */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6111 | static int mkFullPathname( |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6112 | const char *zPath, /* Input path */ |
| 6113 | char *zOut, /* Output buffer */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6114 | int nOut /* Allocated size of buffer zOut */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 6115 | ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6116 | int nPath = sqlite3Strlen30(zPath); |
| 6117 | int iOff = 0; |
| 6118 | if( zPath[0]!='/' ){ |
| 6119 | if( osGetcwd(zOut, nOut-2)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6120 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6121 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6122 | iOff = sqlite3Strlen30(zOut); |
| 6123 | zOut[iOff++] = '/'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6124 | } |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6125 | if( (iOff+nPath+1)>nOut ){ |
| 6126 | /* SQLite assumes that xFullPathname() nul-terminates the output buffer |
| 6127 | ** even if it returns an error. */ |
| 6128 | zOut[iOff] = '\0'; |
| 6129 | return SQLITE_CANTOPEN_BKPT; |
| 6130 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6131 | sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6132 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6135 | /* |
| 6136 | ** Turn a relative pathname into a full pathname. The relative path |
| 6137 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 6138 | ** zPath. |
| 6139 | ** |
| 6140 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 6141 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 6142 | ** this buffer before returning. |
| 6143 | */ |
| 6144 | static int unixFullPathname( |
| 6145 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 6146 | const char *zPath, /* Possibly relative input path */ |
| 6147 | int nOut, /* Size of output buffer in bytes */ |
| 6148 | char *zOut /* Output buffer */ |
| 6149 | ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6150 | #if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT) |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6151 | return mkFullPathname(zPath, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6152 | #else |
| 6153 | int rc = SQLITE_OK; |
| 6154 | int nByte; |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6155 | int nLink = 1; /* Number of symbolic links followed so far */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6156 | const char *zIn = zPath; /* Input path for each iteration of loop */ |
| 6157 | char *zDel = 0; |
| 6158 | |
| 6159 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
| 6160 | UNUSED_PARAMETER(pVfs); |
| 6161 | |
| 6162 | /* It's odd to simulate an io-error here, but really this is just |
| 6163 | ** using the io-error infrastructure to test that SQLite handles this |
| 6164 | ** function failing. This function could fail if, for example, the |
| 6165 | ** current working directory has been unlinked. |
| 6166 | */ |
| 6167 | SimulateIOError( return SQLITE_ERROR ); |
| 6168 | |
| 6169 | do { |
| 6170 | |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6171 | /* Call stat() on path zIn. Set bLink to true if the path is a symbolic |
| 6172 | ** link, or false otherwise. */ |
| 6173 | int bLink = 0; |
| 6174 | struct stat buf; |
| 6175 | if( osLstat(zIn, &buf)!=0 ){ |
| 6176 | if( errno!=ENOENT ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6177 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6178 | } |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6179 | }else{ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6180 | bLink = S_ISLNK(buf.st_mode); |
| 6181 | } |
| 6182 | |
| 6183 | if( bLink ){ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6184 | if( zDel==0 ){ |
| 6185 | zDel = sqlite3_malloc(nOut); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6186 | if( zDel==0 ) rc = SQLITE_NOMEM_BKPT; |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6187 | }else if( ++nLink>SQLITE_MAX_SYMLINKS ){ |
| 6188 | rc = SQLITE_CANTOPEN_BKPT; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6189 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6190 | |
| 6191 | if( rc==SQLITE_OK ){ |
| 6192 | nByte = osReadlink(zIn, zDel, nOut-1); |
| 6193 | if( nByte<0 ){ |
| 6194 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6195 | }else{ |
| 6196 | if( zDel[0]!='/' ){ |
| 6197 | int n; |
| 6198 | for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); |
| 6199 | if( nByte+n+1>nOut ){ |
| 6200 | rc = SQLITE_CANTOPEN_BKPT; |
| 6201 | }else{ |
| 6202 | memmove(&zDel[n], zDel, nByte+1); |
| 6203 | memcpy(zDel, zIn, n); |
| 6204 | nByte += n; |
| 6205 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6206 | } |
| 6207 | zDel[nByte] = '\0'; |
| 6208 | } |
| 6209 | } |
| 6210 | |
| 6211 | zIn = zDel; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6212 | } |
| 6213 | |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6214 | assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' ); |
| 6215 | if( rc==SQLITE_OK && zIn!=zOut ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6216 | rc = mkFullPathname(zIn, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6217 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6218 | if( bLink==0 ) break; |
| 6219 | zIn = zOut; |
| 6220 | }while( rc==SQLITE_OK ); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6221 | |
| 6222 | sqlite3_free(zDel); |
| 6223 | return rc; |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6224 | #endif /* HAVE_READLINK && HAVE_LSTAT */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 6227 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6228 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 6229 | /* |
| 6230 | ** Interfaces for opening a shared library, finding entry points |
| 6231 | ** within the shared library, and closing the shared library. |
| 6232 | */ |
| 6233 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6234 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 6235 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6236 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 6237 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 6238 | |
| 6239 | /* |
| 6240 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 6241 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 6242 | ** message is available, it is written to zBufOut. If no error message |
| 6243 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 6244 | ** error message. |
| 6245 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6246 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 6247 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6248 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6249 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6250 | zErr = dlerror(); |
| 6251 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6252 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6253 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6254 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6255 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6256 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 6257 | /* |
| 6258 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 6259 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 6260 | ** returns a void* which is really a pointer to a function. So how do we |
| 6261 | ** use dlsym() with -pedantic-errors? |
| 6262 | ** |
| 6263 | ** Variable x below is defined to be a pointer to a function taking |
| 6264 | ** parameters void* and const char* and returning a pointer to a function. |
| 6265 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 6266 | ** (That assignment requires a cast.) Then we call the function that |
| 6267 | ** x points to. |
| 6268 | ** |
| 6269 | ** This work-around is unlikely to work correctly on any system where |
| 6270 | ** you really cannot cast a function pointer into void*. But then, on the |
| 6271 | ** other hand, dlsym() will not work on such a system either, so we have |
| 6272 | ** not really lost anything. |
| 6273 | */ |
| 6274 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6275 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6276 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 6277 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6278 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6279 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 6280 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6281 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6282 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6283 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 6284 | #define unixDlOpen 0 |
| 6285 | #define unixDlError 0 |
| 6286 | #define unixDlSym 0 |
| 6287 | #define unixDlClose 0 |
| 6288 | #endif |
| 6289 | |
| 6290 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6291 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6292 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6293 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 6294 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 6295 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6296 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6297 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 6298 | ** errors. The reports issued by valgrind are incorrect - we would |
| 6299 | ** prefer that the randomness be increased by making use of the |
| 6300 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 6301 | ** some users. Rather than argue, it seems easier just to initialize |
| 6302 | ** the whole array and silence valgrind, even if that means less randomness |
| 6303 | ** in the random seed. |
| 6304 | ** |
| 6305 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 6306 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6307 | ** tests repeatable. |
| 6308 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6309 | memset(zBuf, 0, nBuf); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6310 | randomnessPid = osGetpid(0); |
drh | 6a412b8 | 2015-04-30 12:31:49 +0000 | [diff] [blame] | 6311 | #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6312 | { |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6313 | int fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6314 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6315 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 6316 | time_t t; |
| 6317 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6318 | memcpy(zBuf, &t, sizeof(t)); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6319 | memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); |
| 6320 | assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); |
| 6321 | nBuf = sizeof(t) + sizeof(randomnessPid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6322 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 6323 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6324 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6325 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6326 | } |
| 6327 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 6328 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6329 | } |
| 6330 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6331 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6332 | /* |
| 6333 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6334 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6335 | ** The return value is the number of microseconds of sleep actually |
| 6336 | ** requested from the underlying operating system, a number which |
| 6337 | ** might be greater than or equal to the argument, but not less |
| 6338 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6339 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6340 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6341 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6342 | struct timespec sp; |
| 6343 | |
| 6344 | sp.tv_sec = microseconds / 1000000; |
| 6345 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 6346 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6347 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6348 | return microseconds; |
| 6349 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6350 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6351 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6352 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6353 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6354 | int seconds = (microseconds+999999)/1000000; |
| 6355 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6356 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6357 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 6358 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 6359 | } |
| 6360 | |
| 6361 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6362 | ** The following variable, if set to a non-zero value, is interpreted as |
| 6363 | ** the number of seconds since 1970 and is used to set the result of |
| 6364 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6365 | */ |
| 6366 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6367 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6368 | #endif |
| 6369 | |
| 6370 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6371 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 6372 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 6373 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 6374 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 6375 | ** proleptic Gregorian calendar. |
| 6376 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6377 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 6378 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6379 | */ |
| 6380 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 6381 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6382 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6383 | #if defined(NO_GETTOD) |
| 6384 | time_t t; |
| 6385 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 6386 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6387 | #elif OS_VXWORKS |
| 6388 | struct timespec sNow; |
| 6389 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 6390 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 6391 | #else |
| 6392 | struct timeval sNow; |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 6393 | (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ |
| 6394 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6395 | #endif |
| 6396 | |
| 6397 | #ifdef SQLITE_TEST |
| 6398 | if( sqlite3_current_time ){ |
| 6399 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 6400 | } |
| 6401 | #endif |
| 6402 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6403 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
drh | c3dfa5e | 2016-01-22 19:44:03 +0000 | [diff] [blame] | 6406 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6407 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6408 | ** Find the current time (in Universal Coordinated Time). Write the |
| 6409 | ** current time and date as a Julian Day number into *prNow and |
| 6410 | ** return 0. Return 1 if the time and date cannot be found. |
| 6411 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6412 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 6413 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6414 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 6415 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6416 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 6417 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6418 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6419 | } |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 6420 | #else |
| 6421 | # define unixCurrentTime 0 |
| 6422 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6423 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6424 | /* |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6425 | ** The xGetLastError() method is designed to return a better |
| 6426 | ** low-level error message when operating-system problems come up |
| 6427 | ** during SQLite operation. Only the integer return code is currently |
| 6428 | ** used. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6429 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6430 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 6431 | UNUSED_PARAMETER(NotUsed); |
| 6432 | UNUSED_PARAMETER(NotUsed2); |
| 6433 | UNUSED_PARAMETER(NotUsed3); |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6434 | return errno; |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 6435 | } |
| 6436 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6437 | |
| 6438 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6439 | ************************ End of sqlite3_vfs methods *************************** |
| 6440 | ******************************************************************************/ |
| 6441 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6442 | /****************************************************************************** |
| 6443 | ************************** Begin Proxy Locking ******************************** |
| 6444 | ** |
| 6445 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 6446 | ** other locking methods on secondary lock files. Proxy locking is a |
| 6447 | ** meta-layer over top of the primitive locking implemented above. For |
| 6448 | ** this reason, the division that implements of proxy locking is deferred |
| 6449 | ** until late in the file (here) after all of the other I/O methods have |
| 6450 | ** been defined - so that the primitive locking methods are available |
| 6451 | ** as services to help with the implementation of proxy locking. |
| 6452 | ** |
| 6453 | **** |
| 6454 | ** |
| 6455 | ** The default locking schemes in SQLite use byte-range locks on the |
| 6456 | ** database file to coordinate safe, concurrent access by multiple readers |
| 6457 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 6458 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 6459 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 6460 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 6461 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 6462 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 6463 | ** address in the shared range is taken for a SHARED lock, the entire |
| 6464 | ** shared range is taken for an EXCLUSIVE lock): |
| 6465 | ** |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 6466 | ** PENDING_BYTE 0x40000000 |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6467 | ** RESERVED_BYTE 0x40000001 |
| 6468 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 6469 | ** |
| 6470 | ** This works well on the local file system, but shows a nearly 100x |
| 6471 | ** slowdown in read performance on AFP because the AFP client disables |
| 6472 | ** the read cache when byte-range locks are present. Enabling the read |
| 6473 | ** cache exposes a cache coherency problem that is present on all OS X |
| 6474 | ** supported network file systems. NFS and AFP both observe the |
| 6475 | ** close-to-open semantics for ensuring cache coherency |
| 6476 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 6477 | ** address the requirements for concurrent database access by multiple |
| 6478 | ** readers and writers |
| 6479 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 6480 | ** |
| 6481 | ** To address the performance and cache coherency issues, proxy file locking |
| 6482 | ** changes the way database access is controlled by limiting access to a |
| 6483 | ** single host at a time and moving file locks off of the database file |
| 6484 | ** and onto a proxy file on the local file system. |
| 6485 | ** |
| 6486 | ** |
| 6487 | ** Using proxy locks |
| 6488 | ** ----------------- |
| 6489 | ** |
| 6490 | ** C APIs |
| 6491 | ** |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6492 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6493 | ** <proxy_path> | ":auto:"); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6494 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, |
| 6495 | ** &<proxy_path>); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6496 | ** |
| 6497 | ** |
| 6498 | ** SQL pragmas |
| 6499 | ** |
| 6500 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 6501 | ** PRAGMA [database.]lock_proxy_file |
| 6502 | ** |
| 6503 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 6504 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 6505 | ** a proxy path based on the user's temp dir |
| 6506 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 6507 | ** actual proxy file name is generated from the name and path of the |
| 6508 | ** database file. For example: |
| 6509 | ** |
| 6510 | ** For database path "/Users/me/foo.db" |
| 6511 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 6512 | ** |
| 6513 | ** Once a lock proxy is configured for a database connection, it can not |
| 6514 | ** be removed, however it may be switched to a different proxy path via |
| 6515 | ** the above APIs (assuming the conch file is not being held by another |
| 6516 | ** connection or process). |
| 6517 | ** |
| 6518 | ** |
| 6519 | ** How proxy locking works |
| 6520 | ** ----------------------- |
| 6521 | ** |
| 6522 | ** Proxy file locking relies primarily on two new supporting files: |
| 6523 | ** |
| 6524 | ** * conch file to limit access to the database file to a single host |
| 6525 | ** at a time |
| 6526 | ** |
| 6527 | ** * proxy file to act as a proxy for the advisory locks normally |
| 6528 | ** taken on the database |
| 6529 | ** |
| 6530 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 6531 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 6532 | ** contents and comparing the host's unique host ID (see below) and lock |
| 6533 | ** proxy path against the values stored in the conch. The conch file is |
| 6534 | ** stored in the same directory as the database file and the file name |
| 6535 | ** is patterned after the database file name as ".<databasename>-conch". |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 6536 | ** If the conch file does not exist, or its contents do not match the |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6537 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 6538 | ** lock and the conch file contents is updated with the host ID and proxy |
| 6539 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 6540 | ** is held by another process (with a shared lock), the exclusive lock |
| 6541 | ** will fail and SQLITE_BUSY is returned. |
| 6542 | ** |
| 6543 | ** The proxy file - a single-byte file used for all advisory file locks |
| 6544 | ** normally taken on the database file. This allows for safe sharing |
| 6545 | ** of the database file for multiple readers and writers on the same |
| 6546 | ** host (the conch ensures that they all use the same local lock file). |
| 6547 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6548 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 6549 | ** only taken when the first request to lock database file is made. |
| 6550 | ** This matches the semantics of the traditional locking behavior, where |
| 6551 | ** opening a connection to a database file does not take a lock on it. |
| 6552 | ** The shared lock and an open file descriptor are maintained until |
| 6553 | ** the connection to the database is closed. |
| 6554 | ** |
| 6555 | ** The proxy file and the lock file are never deleted so they only need |
| 6556 | ** to be created the first time they are used. |
| 6557 | ** |
| 6558 | ** Configuration options |
| 6559 | ** --------------------- |
| 6560 | ** |
| 6561 | ** SQLITE_PREFER_PROXY_LOCKING |
| 6562 | ** |
| 6563 | ** Database files accessed on non-local file systems are |
| 6564 | ** automatically configured for proxy locking, lock files are |
| 6565 | ** named automatically using the same logic as |
| 6566 | ** PRAGMA lock_proxy_file=":auto:" |
| 6567 | ** |
| 6568 | ** SQLITE_PROXY_DEBUG |
| 6569 | ** |
| 6570 | ** Enables the logging of error messages during host id file |
| 6571 | ** retrieval and creation |
| 6572 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6573 | ** LOCKPROXYDIR |
| 6574 | ** |
| 6575 | ** Overrides the default directory used for lock proxy files that |
| 6576 | ** are named automatically via the ":auto:" setting |
| 6577 | ** |
| 6578 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6579 | ** |
| 6580 | ** Permissions to use when creating a directory for storing the |
| 6581 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 6582 | ** |
| 6583 | ** |
| 6584 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 6585 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 6586 | ** force proxy locking to be used for every database file opened, and 0 |
| 6587 | ** will force automatic proxy locking to be disabled for all database |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6588 | ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6589 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 6590 | */ |
| 6591 | |
| 6592 | /* |
| 6593 | ** Proxy locking is only available on MacOSX |
| 6594 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6595 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6596 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6597 | /* |
| 6598 | ** The proxyLockingContext has the path and file structures for the remote |
| 6599 | ** and local proxy files in it |
| 6600 | */ |
| 6601 | typedef struct proxyLockingContext proxyLockingContext; |
| 6602 | struct proxyLockingContext { |
| 6603 | unixFile *conchFile; /* Open conch file */ |
| 6604 | char *conchFilePath; /* Name of the conch file */ |
| 6605 | unixFile *lockProxy; /* Open proxy lock file */ |
| 6606 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 6607 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6608 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6609 | int nFails; /* Number of conch taking failures */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6610 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 6611 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 6612 | }; |
| 6613 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6614 | /* |
| 6615 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 6616 | ** which must point to valid, writable memory large enough for a maxLen length |
| 6617 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6618 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6619 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 6620 | int len; |
| 6621 | int dbLen; |
| 6622 | int i; |
| 6623 | |
| 6624 | #ifdef LOCKPROXYDIR |
| 6625 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 6626 | #else |
| 6627 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 6628 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6629 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6630 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6631 | lPath, errno, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6632 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6633 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6634 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6635 | } |
| 6636 | # else |
| 6637 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 6638 | # endif |
| 6639 | #endif |
| 6640 | |
| 6641 | if( lPath[len-1]!='/' ){ |
| 6642 | len = strlcat(lPath, "/", maxLen); |
| 6643 | } |
| 6644 | |
| 6645 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6646 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6647 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6648 | char c = dbPath[i]; |
| 6649 | lPath[i+len] = (c=='/')?'_':c; |
| 6650 | } |
| 6651 | lPath[i+len]='\0'; |
| 6652 | strlcat(lPath, ":auto:", maxLen); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6653 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6654 | return SQLITE_OK; |
| 6655 | } |
| 6656 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6657 | /* |
| 6658 | ** Creates the lock file and any missing directories in lockPath |
| 6659 | */ |
| 6660 | static int proxyCreateLockPath(const char *lockPath){ |
| 6661 | int i, len; |
| 6662 | char buf[MAXPATHLEN]; |
| 6663 | int start = 0; |
| 6664 | |
| 6665 | assert(lockPath!=NULL); |
| 6666 | /* try to create all the intermediate directories */ |
| 6667 | len = (int)strlen(lockPath); |
| 6668 | buf[0] = lockPath[0]; |
| 6669 | for( i=1; i<len; i++ ){ |
| 6670 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 6671 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 6672 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 6673 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 6674 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 6675 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6676 | int err=errno; |
| 6677 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6678 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6679 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6680 | buf, strerror(err), lockPath, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6681 | return err; |
| 6682 | } |
| 6683 | } |
| 6684 | } |
| 6685 | start=i+1; |
| 6686 | } |
| 6687 | buf[i] = lockPath[i]; |
| 6688 | } |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 6689 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6690 | return 0; |
| 6691 | } |
| 6692 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6693 | /* |
| 6694 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 6695 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 6696 | ** |
| 6697 | ** The caller is responsible not only for closing the file descriptor |
| 6698 | ** but also for freeing the memory associated with the file descriptor. |
| 6699 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6700 | static int proxyCreateUnixFile( |
| 6701 | const char *path, /* path for the new unixFile */ |
| 6702 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 6703 | int islockfile /* if non zero missing dirs will be created */ |
| 6704 | ) { |
| 6705 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6706 | unixFile *pNew; |
| 6707 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6708 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6709 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6710 | int terrno = 0; |
| 6711 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6712 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6713 | /* 1. first try to open/create the file |
| 6714 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 6715 | ** the parent directories and then try again. |
| 6716 | ** 3. if that fails, try to open the file read-only |
| 6717 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 6718 | */ |
| 6719 | pUnused = findReusableFd(path, openFlags); |
| 6720 | if( pUnused ){ |
| 6721 | fd = pUnused->fd; |
| 6722 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6723 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6724 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6725 | return SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6726 | } |
| 6727 | } |
| 6728 | if( fd<0 ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6729 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6730 | terrno = errno; |
| 6731 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 6732 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6733 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6734 | } |
| 6735 | } |
| 6736 | } |
| 6737 | if( fd<0 ){ |
| 6738 | openFlags = O_RDONLY; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6739 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6740 | terrno = errno; |
| 6741 | } |
| 6742 | if( fd<0 ){ |
| 6743 | if( islockfile ){ |
| 6744 | return SQLITE_BUSY; |
| 6745 | } |
| 6746 | switch (terrno) { |
| 6747 | case EACCES: |
| 6748 | return SQLITE_PERM; |
| 6749 | case EIO: |
| 6750 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 6751 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6752 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6753 | } |
| 6754 | } |
| 6755 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6756 | pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6757 | if( pNew==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6758 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6759 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6760 | } |
| 6761 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6762 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6763 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6764 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6765 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6766 | pUnused->fd = fd; |
| 6767 | pUnused->flags = openFlags; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6768 | pNew->pPreallocatedUnused = pUnused; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6769 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6770 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6771 | if( rc==SQLITE_OK ){ |
| 6772 | *ppFile = pNew; |
| 6773 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6774 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6775 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6776 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6777 | sqlite3_free(pNew); |
| 6778 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6779 | return rc; |
| 6780 | } |
| 6781 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6782 | #ifdef SQLITE_TEST |
| 6783 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6784 | int sqlite3_hostid_num = 0; |
| 6785 | #endif |
| 6786 | |
| 6787 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 6788 | |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6789 | #ifdef HAVE_GETHOSTUUID |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6790 | /* Not always defined in the headers as it ought to be */ |
| 6791 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6792 | #endif |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6793 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6794 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 6795 | ** bytes of writable memory. |
| 6796 | */ |
| 6797 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6798 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 6799 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6800 | #ifdef HAVE_GETHOSTUUID |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6801 | { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6802 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6803 | if( gethostuuid(pHostID, &timeout) ){ |
| 6804 | int err = errno; |
| 6805 | if( pError ){ |
| 6806 | *pError = err; |
| 6807 | } |
| 6808 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6809 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6810 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6811 | #else |
| 6812 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6813 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6814 | #ifdef SQLITE_TEST |
| 6815 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6816 | if( sqlite3_hostid_num != 0){ |
| 6817 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 6818 | } |
| 6819 | #endif |
| 6820 | |
| 6821 | return SQLITE_OK; |
| 6822 | } |
| 6823 | |
| 6824 | /* The conch file contains the header, host id and lock file path |
| 6825 | */ |
| 6826 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 6827 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 6828 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 6829 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 6830 | |
| 6831 | /* |
| 6832 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 6833 | ** it back. The newly created file's file descriptor is assigned to the |
| 6834 | ** conch file structure and finally the original conch file descriptor is |
| 6835 | ** closed. Returns zero if successful. |
| 6836 | */ |
| 6837 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 6838 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6839 | unixFile *conchFile = pCtx->conchFile; |
| 6840 | char tPath[MAXPATHLEN]; |
| 6841 | char buf[PROXY_MAXCONCHLEN]; |
| 6842 | char *cPath = pCtx->conchFilePath; |
| 6843 | size_t readLen = 0; |
| 6844 | size_t pathLen = 0; |
| 6845 | char errmsg[64] = ""; |
| 6846 | int fd = -1; |
| 6847 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6848 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6849 | |
| 6850 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 6851 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 6852 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 6853 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6854 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6855 | goto end_breaklock; |
| 6856 | } |
| 6857 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6858 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6859 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6860 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6861 | goto end_breaklock; |
| 6862 | } |
| 6863 | /* write it out to the temporary break file */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6864 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6865 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6866 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6867 | goto end_breaklock; |
| 6868 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6869 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6870 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6871 | goto end_breaklock; |
| 6872 | } |
| 6873 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6874 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6875 | goto end_breaklock; |
| 6876 | } |
| 6877 | rc = 0; |
| 6878 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6879 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6880 | conchFile->h = fd; |
| 6881 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 6882 | |
| 6883 | end_breaklock: |
| 6884 | if( rc ){ |
| 6885 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6886 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6887 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6888 | } |
| 6889 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 6890 | } |
| 6891 | return rc; |
| 6892 | } |
| 6893 | |
| 6894 | /* Take the requested lock on the conch file and break a stale lock if the |
| 6895 | ** host id matches. |
| 6896 | */ |
| 6897 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 6898 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6899 | unixFile *conchFile = pCtx->conchFile; |
| 6900 | int rc = SQLITE_OK; |
| 6901 | int nTries = 0; |
| 6902 | struct timespec conchModTime; |
| 6903 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6904 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6905 | do { |
| 6906 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6907 | nTries ++; |
| 6908 | if( rc==SQLITE_BUSY ){ |
| 6909 | /* If the lock failed (busy): |
| 6910 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 6911 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 6912 | * 10 sec and try again |
| 6913 | * 3rd try: break the lock unless the mod time has changed. |
| 6914 | */ |
| 6915 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6916 | if( osFstat(conchFile->h, &buf) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6917 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6918 | return SQLITE_IOERR_LOCK; |
| 6919 | } |
| 6920 | |
| 6921 | if( nTries==1 ){ |
| 6922 | conchModTime = buf.st_mtimespec; |
| 6923 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 6924 | continue; |
| 6925 | } |
| 6926 | |
| 6927 | assert( nTries>1 ); |
| 6928 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 6929 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 6930 | return SQLITE_BUSY; |
| 6931 | } |
| 6932 | |
| 6933 | if( nTries==2 ){ |
| 6934 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6935 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6936 | if( len<0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6937 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6938 | return SQLITE_IOERR_LOCK; |
| 6939 | } |
| 6940 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 6941 | /* don't break the lock if the host id doesn't match */ |
| 6942 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 6943 | return SQLITE_BUSY; |
| 6944 | } |
| 6945 | }else{ |
| 6946 | /* don't break the lock on short read or a version mismatch */ |
| 6947 | return SQLITE_BUSY; |
| 6948 | } |
| 6949 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 6950 | continue; |
| 6951 | } |
| 6952 | |
| 6953 | assert( nTries==3 ); |
| 6954 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 6955 | rc = SQLITE_OK; |
| 6956 | if( lockType==EXCLUSIVE_LOCK ){ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 6957 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6958 | } |
| 6959 | if( !rc ){ |
| 6960 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6961 | } |
| 6962 | } |
| 6963 | } |
| 6964 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 6965 | |
| 6966 | return rc; |
| 6967 | } |
| 6968 | |
| 6969 | /* Takes the conch by taking a shared lock and read the contents conch, if |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6970 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 6971 | ** lockPath means that the lockPath in the conch file will be used if the |
| 6972 | ** host IDs match, or a new lock path will be generated automatically |
| 6973 | ** and written to the conch file. |
| 6974 | */ |
| 6975 | static int proxyTakeConch(unixFile *pFile){ |
| 6976 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6977 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6978 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6979 | return SQLITE_OK; |
| 6980 | }else{ |
| 6981 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6982 | uuid_t myHostID; |
| 6983 | int pError = 0; |
| 6984 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6985 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6986 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6987 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6988 | int createConch = 0; |
| 6989 | int hostIdMatch = 0; |
| 6990 | int readLen = 0; |
| 6991 | int tryOldLockPath = 0; |
| 6992 | int forceNewLockPath = 0; |
| 6993 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6994 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 6995 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6996 | osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6997 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6998 | rc = proxyGetHostID(myHostID, &pError); |
| 6999 | if( (rc&0xff)==SQLITE_IOERR ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7000 | storeLastErrno(pFile, pError); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7001 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7002 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7003 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7004 | if( rc!=SQLITE_OK ){ |
| 7005 | goto end_takeconch; |
| 7006 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7007 | /* read the existing conch file */ |
| 7008 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 7009 | if( readLen<0 ){ |
| 7010 | /* I/O error: lastErrno set by seekAndRead */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7011 | storeLastErrno(pFile, conchFile->lastErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7012 | rc = SQLITE_IOERR_READ; |
| 7013 | goto end_takeconch; |
| 7014 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 7015 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 7016 | /* a short read or version format mismatch means we need to create a new |
| 7017 | ** conch file. |
| 7018 | */ |
| 7019 | createConch = 1; |
| 7020 | } |
| 7021 | /* if the host id matches and the lock path already exists in the conch |
| 7022 | ** we'll try to use the path there, if we can't open that path, we'll |
| 7023 | ** retry with a new auto-generated path |
| 7024 | */ |
| 7025 | do { /* in case we need to try again for an :auto: named lock file */ |
| 7026 | |
| 7027 | if( !createConch && !forceNewLockPath ){ |
| 7028 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 7029 | PROXY_HOSTIDLEN); |
| 7030 | /* if the conch has data compare the contents */ |
| 7031 | if( !pCtx->lockProxyPath ){ |
| 7032 | /* for auto-named local lock file, just check the host ID and we'll |
| 7033 | ** use the local lock file path that's already in there |
| 7034 | */ |
| 7035 | if( hostIdMatch ){ |
| 7036 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 7037 | |
| 7038 | if( pathLen>=MAXPATHLEN ){ |
| 7039 | pathLen=MAXPATHLEN-1; |
| 7040 | } |
| 7041 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 7042 | lockPath[pathLen] = 0; |
| 7043 | tempLockPath = lockPath; |
| 7044 | tryOldLockPath = 1; |
| 7045 | /* create a copy of the lock path if the conch is taken */ |
| 7046 | goto end_takeconch; |
| 7047 | } |
| 7048 | }else if( hostIdMatch |
| 7049 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 7050 | readLen-PROXY_PATHINDEX) |
| 7051 | ){ |
| 7052 | /* conch host and lock path match */ |
| 7053 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7054 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7055 | } |
| 7056 | |
| 7057 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 7058 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 7059 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7060 | goto end_takeconch; |
| 7061 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7062 | |
| 7063 | /* either the conch didn't match or we need to create a new one */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7064 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7065 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 7066 | tempLockPath = lockPath; |
| 7067 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7068 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7069 | |
| 7070 | /* update conch with host and path (this will fail if other process |
| 7071 | ** has a shared lock already), if the host id matches, use the big |
| 7072 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7073 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7074 | futimes(conchFile->h, NULL); |
| 7075 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 7076 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7077 | /* We are trying for an exclusive lock but another thread in this |
| 7078 | ** same process is still holding a shared lock. */ |
| 7079 | rc = SQLITE_BUSY; |
| 7080 | } else { |
| 7081 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7082 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7083 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7084 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7085 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7086 | if( rc==SQLITE_OK ){ |
| 7087 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 7088 | int writeSize = 0; |
| 7089 | |
| 7090 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 7091 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 7092 | if( pCtx->lockProxyPath!=NULL ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7093 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, |
| 7094 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7095 | }else{ |
| 7096 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 7097 | } |
| 7098 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7099 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7100 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 7101 | full_fsync(conchFile->h,0,0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7102 | /* If we created a new conch file (not just updated the contents of a |
| 7103 | ** valid conch file), try to match the permissions of the database |
| 7104 | */ |
| 7105 | if( rc==SQLITE_OK && createConch ){ |
| 7106 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7107 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7108 | if( err==0 ){ |
| 7109 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 7110 | S_IROTH|S_IWOTH); |
| 7111 | /* try to match the database file R/W permissions, ignore failure */ |
| 7112 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7113 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7114 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7115 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7116 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7117 | }while( rc==(-1) && errno==EINTR ); |
| 7118 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7119 | int code = errno; |
| 7120 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 7121 | cmode, code, strerror(code)); |
| 7122 | } else { |
| 7123 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 7124 | } |
| 7125 | }else{ |
| 7126 | int code = errno; |
| 7127 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 7128 | err, code, strerror(code)); |
| 7129 | #endif |
| 7130 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7131 | } |
| 7132 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7133 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 7134 | |
| 7135 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7136 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7137 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 7138 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7139 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 7140 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7141 | } |
| 7142 | pFile->h = -1; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7143 | fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7144 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7145 | if( fd>=0 ){ |
| 7146 | pFile->h = fd; |
| 7147 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 7148 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7149 | during locking */ |
| 7150 | } |
| 7151 | } |
| 7152 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 7153 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 7154 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 7155 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 7156 | /* we couldn't create the proxy lock file with the old lock file path |
| 7157 | ** so try again via auto-naming |
| 7158 | */ |
| 7159 | forceNewLockPath = 1; |
| 7160 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 7161 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7162 | } |
| 7163 | } |
| 7164 | if( rc==SQLITE_OK ){ |
| 7165 | /* Need to make a copy of path if we extracted the value |
| 7166 | ** from the conch file or the path was allocated on the stack |
| 7167 | */ |
| 7168 | if( tempLockPath ){ |
| 7169 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 7170 | if( !pCtx->lockProxyPath ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7171 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7172 | } |
| 7173 | } |
| 7174 | } |
| 7175 | if( rc==SQLITE_OK ){ |
| 7176 | pCtx->conchHeld = 1; |
| 7177 | |
| 7178 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 7179 | afpLockingContext *afpCtx; |
| 7180 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 7181 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 7182 | } |
| 7183 | } else { |
| 7184 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7185 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7186 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 7187 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7188 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7189 | } while (1); /* in case we need to retry the :auto: lock file - |
| 7190 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7191 | } |
| 7192 | } |
| 7193 | |
| 7194 | /* |
| 7195 | ** If pFile holds a lock on a conch file, then release that lock. |
| 7196 | */ |
| 7197 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 7198 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7199 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 7200 | unixFile *conchFile; /* Name of the conch file */ |
| 7201 | |
| 7202 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7203 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7204 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7205 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7206 | osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7207 | if( pCtx->conchHeld>0 ){ |
| 7208 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7209 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7210 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7211 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 7212 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7213 | return rc; |
| 7214 | } |
| 7215 | |
| 7216 | /* |
| 7217 | ** Given the name of a database file, compute the name of its conch file. |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7218 | ** Store the conch filename in memory obtained from sqlite3_malloc64(). |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7219 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 7220 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 7221 | ** |
| 7222 | ** The caller is responsible for ensuring that the allocated memory |
| 7223 | ** space is eventually freed. |
| 7224 | ** |
| 7225 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 7226 | */ |
| 7227 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 7228 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7229 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7230 | char *conchPath; /* buffer in which to construct conch name */ |
| 7231 | |
| 7232 | /* Allocate space for the conch filename and initialize the name to |
| 7233 | ** the name of the original database file. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7234 | *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7235 | if( conchPath==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7236 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7237 | } |
| 7238 | memcpy(conchPath, dbPath, len+1); |
| 7239 | |
| 7240 | /* now insert a "." before the last / character */ |
| 7241 | for( i=(len-1); i>=0; i-- ){ |
| 7242 | if( conchPath[i]=='/' ){ |
| 7243 | i++; |
| 7244 | break; |
| 7245 | } |
| 7246 | } |
| 7247 | conchPath[i]='.'; |
| 7248 | while ( i<len ){ |
| 7249 | conchPath[i+1]=dbPath[i]; |
| 7250 | i++; |
| 7251 | } |
| 7252 | |
| 7253 | /* append the "-conch" suffix to the file */ |
| 7254 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7255 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7256 | |
| 7257 | return SQLITE_OK; |
| 7258 | } |
| 7259 | |
| 7260 | |
| 7261 | /* Takes a fully configured proxy locking-style unix file and switches |
| 7262 | ** the local lock file path |
| 7263 | */ |
| 7264 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 7265 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7266 | char *oldPath = pCtx->lockProxyPath; |
| 7267 | int rc = SQLITE_OK; |
| 7268 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7269 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7270 | return SQLITE_BUSY; |
| 7271 | } |
| 7272 | |
| 7273 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 7274 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 7275 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 7276 | return SQLITE_OK; |
| 7277 | }else{ |
| 7278 | unixFile *lockProxy = pCtx->lockProxy; |
| 7279 | pCtx->lockProxy=NULL; |
| 7280 | pCtx->conchHeld = 0; |
| 7281 | if( lockProxy!=NULL ){ |
| 7282 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 7283 | if( rc ) return rc; |
| 7284 | sqlite3_free(lockProxy); |
| 7285 | } |
| 7286 | sqlite3_free(oldPath); |
| 7287 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 7288 | } |
| 7289 | |
| 7290 | return rc; |
| 7291 | } |
| 7292 | |
| 7293 | /* |
| 7294 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 7295 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 7296 | ** |
| 7297 | ** This routine find the filename associated with pFile and writes it |
| 7298 | ** int dbPath. |
| 7299 | */ |
| 7300 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7301 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7302 | if( pFile->pMethod == &afpIoMethods ){ |
| 7303 | /* afp style keeps a reference to the db path in the filePath field |
| 7304 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7305 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7306 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, |
| 7307 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7308 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7309 | #endif |
| 7310 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 7311 | /* dot lock style uses the locking context to store the dot lock |
| 7312 | ** file path */ |
| 7313 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 7314 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 7315 | }else{ |
| 7316 | /* all other styles use the locking context to store the db file path */ |
| 7317 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7318 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7319 | } |
| 7320 | return SQLITE_OK; |
| 7321 | } |
| 7322 | |
| 7323 | /* |
| 7324 | ** Takes an already filled in unix file and alters it so all file locking |
| 7325 | ** will be performed on the local proxy lock file. The following fields |
| 7326 | ** are preserved in the locking context so that they can be restored and |
| 7327 | ** the unix structure properly cleaned up at close time: |
| 7328 | ** ->lockingContext |
| 7329 | ** ->pMethod |
| 7330 | */ |
| 7331 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 7332 | proxyLockingContext *pCtx; |
| 7333 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 7334 | char *lockPath=NULL; |
| 7335 | int rc = SQLITE_OK; |
| 7336 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7337 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7338 | return SQLITE_BUSY; |
| 7339 | } |
| 7340 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 7341 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 7342 | lockPath=NULL; |
| 7343 | }else{ |
| 7344 | lockPath=(char *)path; |
| 7345 | } |
| 7346 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7347 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7348 | (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7349 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7350 | pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7351 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7352 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7353 | } |
| 7354 | memset(pCtx, 0, sizeof(*pCtx)); |
| 7355 | |
| 7356 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 7357 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7358 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 7359 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 7360 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 7361 | ** (c) the file system is read-only, then enable no-locking access. |
| 7362 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 7363 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 7364 | */ |
| 7365 | struct statfs fsInfo; |
| 7366 | struct stat conchInfo; |
| 7367 | int goLockless = 0; |
| 7368 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7369 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7370 | int err = errno; |
| 7371 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 7372 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 7373 | } |
| 7374 | } |
| 7375 | if( goLockless ){ |
| 7376 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 7377 | rc = SQLITE_OK; |
| 7378 | } |
| 7379 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7380 | } |
| 7381 | if( rc==SQLITE_OK && lockPath ){ |
| 7382 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 7383 | } |
| 7384 | |
| 7385 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7386 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 7387 | if( pCtx->dbPath==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7388 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7389 | } |
| 7390 | } |
| 7391 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7392 | /* all memory is allocated, proxys are created and assigned, |
| 7393 | ** switch the locking context and pMethod then return. |
| 7394 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7395 | pCtx->oldLockingContext = pFile->lockingContext; |
| 7396 | pFile->lockingContext = pCtx; |
| 7397 | pCtx->pOldMethod = pFile->pMethod; |
| 7398 | pFile->pMethod = &proxyIoMethods; |
| 7399 | }else{ |
| 7400 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7401 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7402 | sqlite3_free(pCtx->conchFile); |
| 7403 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7404 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7405 | sqlite3_free(pCtx->conchFilePath); |
| 7406 | sqlite3_free(pCtx); |
| 7407 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7408 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 7409 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7410 | return rc; |
| 7411 | } |
| 7412 | |
| 7413 | |
| 7414 | /* |
| 7415 | ** This routine handles sqlite3_file_control() calls that are specific |
| 7416 | ** to proxy locking. |
| 7417 | */ |
| 7418 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 7419 | switch( op ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7420 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7421 | unixFile *pFile = (unixFile*)id; |
| 7422 | if( pFile->pMethod == &proxyIoMethods ){ |
| 7423 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7424 | proxyTakeConch(pFile); |
| 7425 | if( pCtx->lockProxyPath ){ |
| 7426 | *(const char **)pArg = pCtx->lockProxyPath; |
| 7427 | }else{ |
| 7428 | *(const char **)pArg = ":auto: (not held)"; |
| 7429 | } |
| 7430 | } else { |
| 7431 | *(const char **)pArg = NULL; |
| 7432 | } |
| 7433 | return SQLITE_OK; |
| 7434 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7435 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7436 | unixFile *pFile = (unixFile*)id; |
| 7437 | int rc = SQLITE_OK; |
| 7438 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 7439 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 7440 | if( isProxyStyle ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7441 | /* turn off proxy locking - not supported. If support is added for |
| 7442 | ** switching proxy locking mode off then it will need to fail if |
| 7443 | ** the journal mode is WAL mode. |
| 7444 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7445 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 7446 | }else{ |
| 7447 | /* turn off proxy locking - already off - NOOP */ |
| 7448 | rc = SQLITE_OK; |
| 7449 | } |
| 7450 | }else{ |
| 7451 | const char *proxyPath = (const char *)pArg; |
| 7452 | if( isProxyStyle ){ |
| 7453 | proxyLockingContext *pCtx = |
| 7454 | (proxyLockingContext*)pFile->lockingContext; |
| 7455 | if( !strcmp(pArg, ":auto:") |
| 7456 | || (pCtx->lockProxyPath && |
| 7457 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 7458 | ){ |
| 7459 | rc = SQLITE_OK; |
| 7460 | }else{ |
| 7461 | rc = switchLockProxyPath(pFile, proxyPath); |
| 7462 | } |
| 7463 | }else{ |
| 7464 | /* turn on proxy file locking */ |
| 7465 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 7466 | } |
| 7467 | } |
| 7468 | return rc; |
| 7469 | } |
| 7470 | default: { |
| 7471 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 7472 | } |
| 7473 | } |
| 7474 | /*NOTREACHED*/ |
| 7475 | return SQLITE_ERROR; |
| 7476 | } |
| 7477 | |
| 7478 | /* |
| 7479 | ** Within this division (the proxying locking implementation) the procedures |
| 7480 | ** above this point are all utilities. The lock-related methods of the |
| 7481 | ** proxy-locking sqlite3_io_method object follow. |
| 7482 | */ |
| 7483 | |
| 7484 | |
| 7485 | /* |
| 7486 | ** This routine checks if there is a RESERVED lock held on the specified |
| 7487 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 7488 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 7489 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 7490 | */ |
| 7491 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 7492 | unixFile *pFile = (unixFile*)id; |
| 7493 | int rc = proxyTakeConch(pFile); |
| 7494 | if( rc==SQLITE_OK ){ |
| 7495 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7496 | if( pCtx->conchHeld>0 ){ |
| 7497 | unixFile *proxy = pCtx->lockProxy; |
| 7498 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 7499 | }else{ /* conchHeld < 0 is lockless */ |
| 7500 | pResOut=0; |
| 7501 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7502 | } |
| 7503 | return rc; |
| 7504 | } |
| 7505 | |
| 7506 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7507 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7508 | ** of the following: |
| 7509 | ** |
| 7510 | ** (1) SHARED_LOCK |
| 7511 | ** (2) RESERVED_LOCK |
| 7512 | ** (3) PENDING_LOCK |
| 7513 | ** (4) EXCLUSIVE_LOCK |
| 7514 | ** |
| 7515 | ** Sometimes when requesting one lock state, additional lock states |
| 7516 | ** are inserted in between. The locking might fail on one of the later |
| 7517 | ** transitions leaving the lock state different from what it started but |
| 7518 | ** still short of its goal. The following chart shows the allowed |
| 7519 | ** transitions and the inserted intermediate states: |
| 7520 | ** |
| 7521 | ** UNLOCKED -> SHARED |
| 7522 | ** SHARED -> RESERVED |
| 7523 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 7524 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 7525 | ** PENDING -> EXCLUSIVE |
| 7526 | ** |
| 7527 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 7528 | ** routine to lower a locking level. |
| 7529 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7530 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7531 | unixFile *pFile = (unixFile*)id; |
| 7532 | int rc = proxyTakeConch(pFile); |
| 7533 | if( rc==SQLITE_OK ){ |
| 7534 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7535 | if( pCtx->conchHeld>0 ){ |
| 7536 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7537 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 7538 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7539 | }else{ |
| 7540 | /* conchHeld < 0 is lockless */ |
| 7541 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7542 | } |
| 7543 | return rc; |
| 7544 | } |
| 7545 | |
| 7546 | |
| 7547 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7548 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7549 | ** must be either NO_LOCK or SHARED_LOCK. |
| 7550 | ** |
| 7551 | ** If the locking level of the file descriptor is already at or below |
| 7552 | ** the requested locking level, this routine is a no-op. |
| 7553 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7554 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7555 | unixFile *pFile = (unixFile*)id; |
| 7556 | int rc = proxyTakeConch(pFile); |
| 7557 | if( rc==SQLITE_OK ){ |
| 7558 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7559 | if( pCtx->conchHeld>0 ){ |
| 7560 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7561 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 7562 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7563 | }else{ |
| 7564 | /* conchHeld < 0 is lockless */ |
| 7565 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7566 | } |
| 7567 | return rc; |
| 7568 | } |
| 7569 | |
| 7570 | /* |
| 7571 | ** Close a file that uses proxy locks. |
| 7572 | */ |
| 7573 | static int proxyClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 7574 | if( ALWAYS(id) ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7575 | unixFile *pFile = (unixFile*)id; |
| 7576 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7577 | unixFile *lockProxy = pCtx->lockProxy; |
| 7578 | unixFile *conchFile = pCtx->conchFile; |
| 7579 | int rc = SQLITE_OK; |
| 7580 | |
| 7581 | if( lockProxy ){ |
| 7582 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 7583 | if( rc ) return rc; |
| 7584 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 7585 | if( rc ) return rc; |
| 7586 | sqlite3_free(lockProxy); |
| 7587 | pCtx->lockProxy = 0; |
| 7588 | } |
| 7589 | if( conchFile ){ |
| 7590 | if( pCtx->conchHeld ){ |
| 7591 | rc = proxyReleaseConch(pFile); |
| 7592 | if( rc ) return rc; |
| 7593 | } |
| 7594 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 7595 | if( rc ) return rc; |
| 7596 | sqlite3_free(conchFile); |
| 7597 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7598 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7599 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7600 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7601 | /* restore the original locking context and pMethod then close it */ |
| 7602 | pFile->lockingContext = pCtx->oldLockingContext; |
| 7603 | pFile->pMethod = pCtx->pOldMethod; |
| 7604 | sqlite3_free(pCtx); |
| 7605 | return pFile->pMethod->xClose(id); |
| 7606 | } |
| 7607 | return SQLITE_OK; |
| 7608 | } |
| 7609 | |
| 7610 | |
| 7611 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7612 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7613 | /* |
| 7614 | ** The proxy locking style is intended for use with AFP filesystems. |
| 7615 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 7616 | ** restricted to MacOSX. |
| 7617 | ** |
| 7618 | ** |
| 7619 | ******************* End of the proxy lock implementation ********************** |
| 7620 | ******************************************************************************/ |
| 7621 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7622 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7623 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7624 | ** |
| 7625 | ** This routine registers all VFS implementations for unix-like operating |
| 7626 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 7627 | ** should be the only routines in this file that are visible from other |
| 7628 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7629 | ** |
| 7630 | ** This routine is called once during SQLite initialization and by a |
| 7631 | ** single thread. The memory allocation and mutex subsystems have not |
| 7632 | ** necessarily been initialized when this routine is called, and so they |
| 7633 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7634 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7635 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7636 | /* |
| 7637 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7638 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 7639 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 7640 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 7641 | ** and so we have to go through the intermediate pointer to avoid problems |
| 7642 | ** when compiling with -pedantic-errors on GCC.) |
| 7643 | ** |
| 7644 | ** The FINDER parameter to this macro is the name of the pointer to the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7645 | ** finder-function. The finder-function returns a pointer to the |
| 7646 | ** sqlite_io_methods object that implements the desired locking |
| 7647 | ** behaviors. See the division above that contains the IOMETHODS |
| 7648 | ** macro for addition information on finder-functions. |
| 7649 | ** |
| 7650 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 7651 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 7652 | ** more than that; it looks at the filesystem type that hosts the |
| 7653 | ** database file and tries to choose an locking method appropriate for |
| 7654 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7655 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7656 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7657 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7658 | sizeof(unixFile), /* szOsFile */ \ |
| 7659 | MAX_PATHNAME, /* mxPathname */ \ |
| 7660 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7661 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7662 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7663 | unixOpen, /* xOpen */ \ |
| 7664 | unixDelete, /* xDelete */ \ |
| 7665 | unixAccess, /* xAccess */ \ |
| 7666 | unixFullPathname, /* xFullPathname */ \ |
| 7667 | unixDlOpen, /* xDlOpen */ \ |
| 7668 | unixDlError, /* xDlError */ \ |
| 7669 | unixDlSym, /* xDlSym */ \ |
| 7670 | unixDlClose, /* xDlClose */ \ |
| 7671 | unixRandomness, /* xRandomness */ \ |
| 7672 | unixSleep, /* xSleep */ \ |
| 7673 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 7674 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 7675 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7676 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 7677 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 7678 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7679 | } |
| 7680 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7681 | /* |
| 7682 | ** All default VFSes for unix are contained in the following array. |
| 7683 | ** |
| 7684 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 7685 | ** by the SQLite core when the VFS is registered. So the following |
| 7686 | ** array cannot be const. |
| 7687 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7688 | static sqlite3_vfs aVfs[] = { |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7689 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7690 | UNIXVFS("unix", autolockIoFinder ), |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7691 | #elif OS_VXWORKS |
| 7692 | UNIXVFS("unix", vxworksIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7693 | #else |
| 7694 | UNIXVFS("unix", posixIoFinder ), |
| 7695 | #endif |
| 7696 | UNIXVFS("unix-none", nolockIoFinder ), |
| 7697 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7698 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7699 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7700 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7701 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7702 | #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7703 | UNIXVFS("unix-posix", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7704 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7705 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 7706 | UNIXVFS("unix-flock", flockIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7707 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7708 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7709 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7710 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7711 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7712 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7713 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7714 | unsigned int i; /* Loop counter */ |
| 7715 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7716 | /* Double-check that the aSyscall[] array has been constructed |
| 7717 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 7718 | assert( ArraySize(aSyscall)==29 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7719 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7720 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7721 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7722 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7723 | } |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame^] | 7724 | unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7725 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7726 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7727 | |
| 7728 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7729 | ** Shutdown the operating system interface. |
| 7730 | ** |
| 7731 | ** Some operating systems might need to do some cleanup in this routine, |
| 7732 | ** to release dynamically allocated objects. But not on unix. |
| 7733 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7734 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7735 | int sqlite3_os_end(void){ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame^] | 7736 | unixBigLock = 0; |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7737 | return SQLITE_OK; |
| 7738 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 7739 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 7740 | #endif /* SQLITE_OS_UNIX */ |