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 | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 139 | ** If we are to be thread-safe, include the pthreads header. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 140 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 141 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 142 | # include <pthread.h> |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 143 | #endif |
| 144 | |
| 145 | /* |
| 146 | ** Default permissions when creating a new file |
| 147 | */ |
| 148 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 149 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 150 | #endif |
| 151 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 152 | /* |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 153 | ** Default permissions when creating auto proxy dir |
| 154 | */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 155 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 156 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 157 | #endif |
| 158 | |
| 159 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 160 | ** Maximum supported path-length. |
| 161 | */ |
| 162 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 163 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 164 | /* |
| 165 | ** Maximum supported symbolic links |
| 166 | */ |
| 167 | #define SQLITE_MAX_SYMLINKS 100 |
| 168 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 169 | /* Always cast the getpid() return type for compatibility with |
| 170 | ** kernel modules in VxWorks. */ |
| 171 | #define osGetpid(X) (pid_t)getpid() |
| 172 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 173 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 174 | ** Only set the lastErrno if the error code is a real error and not |
| 175 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 176 | */ |
| 177 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 178 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 179 | /* Forward references */ |
| 180 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 181 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 182 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 183 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 184 | |
| 185 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 186 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 187 | ** cannot be closed immediately. In these cases, instances of the following |
| 188 | ** structure are used to store the file descriptor while waiting for an |
| 189 | ** opportunity to either close or reuse it. |
| 190 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 191 | struct UnixUnusedFd { |
| 192 | int fd; /* File descriptor to close */ |
| 193 | int flags; /* Flags this file descriptor was opened with */ |
| 194 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 195 | }; |
| 196 | |
| 197 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 198 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 199 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 200 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 201 | typedef struct unixFile unixFile; |
| 202 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 203 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 204 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 205 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 206 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 207 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 208 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 209 | int lastErrno; /* The unix errno from last I/O error */ |
| 210 | void *lockingContext; /* Locking style specific state */ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 211 | UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 212 | const char *zPath; /* Name of the file */ |
| 213 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 214 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 215 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 216 | int nFetchOut; /* Number of outstanding xFetch refs */ |
| 217 | sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 218 | sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ |
| 219 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 220 | void *pMapRegion; /* Memory mapped region */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 221 | #endif |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 222 | int sectorSize; /* Device sector size */ |
| 223 | int deviceCharacteristics; /* Precomputed device characteristics */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 224 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 225 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 226 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 227 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 228 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 229 | #endif |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 230 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 231 | unsigned iBusyTimeout; /* Wait this many millisec on locks */ |
| 232 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 233 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 234 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 235 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 236 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 237 | /* The next group of variables are used to track whether or not the |
| 238 | ** transaction counter in bytes 24-27 of database files are updated |
| 239 | ** whenever any part of the database changes. An assertion fault will |
| 240 | ** occur if a file is updated without also updating the transaction |
| 241 | ** counter. This test is made to avoid new problems similar to the |
| 242 | ** one described by ticket #3584. |
| 243 | */ |
| 244 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 245 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 246 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 247 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 248 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 249 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 250 | #ifdef SQLITE_TEST |
| 251 | /* In test mode, increase the size of this structure a bit so that |
| 252 | ** it is larger than the struct CrashFile defined in test6.c. |
| 253 | */ |
| 254 | char aPadding[32]; |
| 255 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 258 | /* This variable holds the process id (pid) from when the xRandomness() |
| 259 | ** method was called. If xOpen() is called from a different process id, |
| 260 | ** indicating that a fork() has occurred, the PRNG will be reset. |
| 261 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 262 | static pid_t randomnessPid = 0; |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 263 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 264 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 265 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 266 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 267 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 268 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 269 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 270 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 271 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 272 | #else |
| 273 | # define UNIXFILE_DIRSYNC 0x00 |
| 274 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 275 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 276 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 277 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 278 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 279 | |
| 280 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 281 | ** Include code that is common to all os_*.c files |
| 282 | */ |
| 283 | #include "os_common.h" |
| 284 | |
| 285 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 286 | ** Define various macros that are missing from some systems. |
| 287 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 288 | #ifndef O_LARGEFILE |
| 289 | # define O_LARGEFILE 0 |
| 290 | #endif |
| 291 | #ifdef SQLITE_DISABLE_LFS |
| 292 | # undef O_LARGEFILE |
| 293 | # define O_LARGEFILE 0 |
| 294 | #endif |
| 295 | #ifndef O_NOFOLLOW |
| 296 | # define O_NOFOLLOW 0 |
| 297 | #endif |
| 298 | #ifndef O_BINARY |
| 299 | # define O_BINARY 0 |
| 300 | #endif |
| 301 | |
| 302 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 303 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 304 | ** testing and debugging only. |
| 305 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 306 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 307 | #define threadid pthread_self() |
| 308 | #else |
| 309 | #define threadid 0 |
| 310 | #endif |
| 311 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 312 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 313 | ** HAVE_MREMAP defaults to true on Linux and false everywhere else. |
| 314 | */ |
| 315 | #if !defined(HAVE_MREMAP) |
| 316 | # if defined(__linux__) && defined(_GNU_SOURCE) |
| 317 | # define HAVE_MREMAP 1 |
| 318 | # else |
| 319 | # define HAVE_MREMAP 0 |
| 320 | # endif |
| 321 | #endif |
| 322 | |
| 323 | /* |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 324 | ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() |
| 325 | ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. |
| 326 | */ |
| 327 | #ifdef __ANDROID__ |
| 328 | # define lseek lseek64 |
| 329 | #endif |
| 330 | |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 331 | #ifdef __linux__ |
| 332 | /* |
| 333 | ** Linux-specific IOCTL magic numbers used for controlling F2FS |
| 334 | */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 335 | #define F2FS_IOCTL_MAGIC 0xf5 |
| 336 | #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1) |
| 337 | #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2) |
| 338 | #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3) |
| 339 | #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5) |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 340 | #define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32) |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 341 | #define F2FS_FEATURE_ATOMIC_WRITE 0x0004 |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 342 | #endif /* __linux__ */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 343 | |
| 344 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 345 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 346 | ** Different Unix systems declare open() in different ways. Same use |
| 347 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 348 | ** The difference is important when using a pointer to the function. |
| 349 | ** |
| 350 | ** The safest way to deal with the problem is to always use this wrapper |
| 351 | ** which always has the same well-defined interface. |
| 352 | */ |
| 353 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 354 | return open(zFile, flags, mode); |
| 355 | } |
| 356 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 357 | /* Forward reference */ |
| 358 | static int openDirectory(const char*, int*); |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 359 | static int unixGetpagesize(void); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 360 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 361 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 362 | ** Many system calls are accessed through pointer-to-functions so that |
| 363 | ** they may be overridden at runtime to facilitate fault injection during |
| 364 | ** testing and sandboxing. The following array holds the names and pointers |
| 365 | ** to all overrideable system calls. |
| 366 | */ |
| 367 | static struct unix_syscall { |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 368 | const char *zName; /* Name of the system call */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 369 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 370 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 371 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 372 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 373 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 374 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 375 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 376 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 377 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 378 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 379 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 380 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 381 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 382 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 383 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 384 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 385 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 386 | |
| 387 | /* |
| 388 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 389 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 390 | ** that always succeeds. This means that locking does not occur under |
| 391 | ** DJGPP. But it is DOS - what did you expect? |
| 392 | */ |
| 393 | #ifdef __DJGPP__ |
| 394 | { "fstat", 0, 0 }, |
| 395 | #define osFstat(a,b,c) 0 |
| 396 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 397 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 398 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 399 | #endif |
| 400 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 401 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 402 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 403 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 404 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 405 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 406 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 407 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 408 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 409 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 410 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 411 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 412 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 413 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 414 | #endif |
| 415 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 416 | |
| 417 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 418 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 419 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 420 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 421 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 422 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 423 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 424 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 425 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 426 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 427 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 428 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 429 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 430 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 431 | #endif |
| 432 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 433 | aSyscall[12].pCurrent) |
| 434 | |
| 435 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 436 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 437 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 438 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 439 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 440 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 441 | aSyscall[13].pCurrent) |
| 442 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 443 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 444 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 445 | |
| 446 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 447 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 448 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 449 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 450 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 451 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 452 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 453 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 454 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 455 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 456 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 457 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 458 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 459 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 460 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 461 | |
| 462 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 463 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 464 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 465 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 466 | { "fchown", (sqlite3_syscall_ptr)fchown, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 467 | #else |
| 468 | { "fchown", (sqlite3_syscall_ptr)0, 0 }, |
| 469 | #endif |
dan | d3eaebd | 2012-02-13 08:50:23 +0000 | [diff] [blame] | 470 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
drh | 23c4b97 | 2012-02-11 23:55:15 +0000 | [diff] [blame] | 471 | |
drh | 26f625f | 2018-02-19 16:34:31 +0000 | [diff] [blame] | 472 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 473 | { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, |
drh | 26f625f | 2018-02-19 16:34:31 +0000 | [diff] [blame] | 474 | #else |
| 475 | { "geteuid", (sqlite3_syscall_ptr)0, 0 }, |
| 476 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 477 | #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) |
| 478 | |
dan | 4dd5144 | 2013-08-26 14:30:25 +0000 | [diff] [blame] | 479 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 480 | { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, |
| 481 | #else |
| 482 | { "mmap", (sqlite3_syscall_ptr)0, 0 }, |
| 483 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 484 | #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] | 485 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 486 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 487 | { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 488 | #else |
drh | a829992 | 2016-01-08 22:31:00 +0000 | [diff] [blame] | 489 | { "munmap", (sqlite3_syscall_ptr)0, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 490 | #endif |
drh | 62be1fa | 2017-12-09 01:02:33 +0000 | [diff] [blame] | 491 | #define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 492 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 493 | #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 494 | { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, |
| 495 | #else |
| 496 | { "mremap", (sqlite3_syscall_ptr)0, 0 }, |
| 497 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 498 | #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent) |
| 499 | |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 500 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 501 | { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 502 | #else |
| 503 | { "getpagesize", (sqlite3_syscall_ptr)0, 0 }, |
| 504 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 505 | #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent) |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 506 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 507 | #if defined(HAVE_READLINK) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 508 | { "readlink", (sqlite3_syscall_ptr)readlink, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 509 | #else |
| 510 | { "readlink", (sqlite3_syscall_ptr)0, 0 }, |
| 511 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 512 | #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 513 | |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 514 | #if defined(HAVE_LSTAT) |
| 515 | { "lstat", (sqlite3_syscall_ptr)lstat, 0 }, |
| 516 | #else |
| 517 | { "lstat", (sqlite3_syscall_ptr)0, 0 }, |
| 518 | #endif |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 519 | #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 520 | |
drh | b5d013e | 2017-10-25 16:14:12 +0000 | [diff] [blame] | 521 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | 16f39b6 | 2018-09-18 19:40:18 +0000 | [diff] [blame] | 522 | # ifdef __ANDROID__ |
| 523 | { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 }, |
dan | ec9b2a1 | 2019-07-15 07:58:28 +0000 | [diff] [blame] | 524 | #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent) |
dan | 16f39b6 | 2018-09-18 19:40:18 +0000 | [diff] [blame] | 525 | # else |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 526 | { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 }, |
dan | ec9b2a1 | 2019-07-15 07:58:28 +0000 | [diff] [blame] | 527 | #define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent) |
dan | 16f39b6 | 2018-09-18 19:40:18 +0000 | [diff] [blame] | 528 | # endif |
drh | b5d013e | 2017-10-25 16:14:12 +0000 | [diff] [blame] | 529 | #else |
| 530 | { "ioctl", (sqlite3_syscall_ptr)0, 0 }, |
| 531 | #endif |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 532 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 533 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 534 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | ** On some systems, calls to fchown() will trigger a message in a security |
| 538 | ** log if they come from non-root processes. So avoid calling fchown() if |
| 539 | ** we are not running as root. |
| 540 | */ |
| 541 | static int robustFchown(int fd, uid_t uid, gid_t gid){ |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 542 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 543 | return osGeteuid() ? 0 : osFchown(fd,uid,gid); |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 544 | #else |
| 545 | return 0; |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 546 | #endif |
| 547 | } |
| 548 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 549 | /* |
| 550 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 551 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 552 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 553 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 554 | */ |
| 555 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 556 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 557 | const char *zName, /* Name of system call to override */ |
| 558 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 559 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 560 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 561 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 562 | |
| 563 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 564 | if( zName==0 ){ |
| 565 | /* If no zName is given, restore all system calls to their default |
| 566 | ** settings and return NULL |
| 567 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 568 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 569 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 570 | if( aSyscall[i].pDefault ){ |
| 571 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | }else{ |
| 575 | /* If zName is specified, operate on only the one system call |
| 576 | ** specified. |
| 577 | */ |
| 578 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 579 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 580 | if( aSyscall[i].pDefault==0 ){ |
| 581 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 582 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 583 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 584 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 585 | aSyscall[i].pCurrent = pNewFunc; |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | return rc; |
| 591 | } |
| 592 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 593 | /* |
| 594 | ** Return the value of a system call. Return NULL if zName is not a |
| 595 | ** recognized system call name. NULL is also returned if the system call |
| 596 | ** is currently undefined. |
| 597 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 598 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 599 | sqlite3_vfs *pNotUsed, |
| 600 | const char *zName |
| 601 | ){ |
| 602 | unsigned int i; |
| 603 | |
| 604 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 605 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 606 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 607 | } |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | ** Return the name of the first system call after zName. If zName==NULL |
| 613 | ** then return the name of the first system call. Return NULL if zName |
| 614 | ** is the last system call or if zName is not the name of a valid |
| 615 | ** system call. |
| 616 | */ |
| 617 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 618 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 619 | |
| 620 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 621 | if( zName ){ |
| 622 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 623 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 626 | for(i++; i<ArraySize(aSyscall); i++){ |
| 627 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 632 | /* |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 633 | ** Do not accept any file descriptor less than this value, in order to avoid |
| 634 | ** opening database file using file descriptors that are commonly used for |
| 635 | ** standard input, output, and error. |
| 636 | */ |
| 637 | #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
| 638 | # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3 |
| 639 | #endif |
| 640 | |
| 641 | /* |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 642 | ** Invoke open(). Do so multiple times, until it either succeeds or |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 643 | ** fails for some reason other than EINTR. |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 644 | ** |
| 645 | ** If the file creation mode "m" is 0 then set it to the default for |
| 646 | ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 647 | ** 0644) as modified by the system umask. If m is not 0, then |
| 648 | ** make the file creation mode be exactly m ignoring the umask. |
| 649 | ** |
| 650 | ** The m parameter will be non-zero only when creating -wal, -journal, |
| 651 | ** and -shm files. We want those files to have *exactly* the same |
| 652 | ** permissions as their original database, unadulterated by the umask. |
| 653 | ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 654 | ** transaction crashes and leaves behind hot journals, then any |
| 655 | ** process that is able to write to the database will also be able to |
| 656 | ** recover the hot journals. |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 657 | */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 658 | static int robust_open(const char *z, int f, mode_t m){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 659 | int fd; |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 660 | mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 661 | while(1){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 662 | #if defined(O_CLOEXEC) |
| 663 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 664 | #else |
| 665 | fd = osOpen(z,f,m2); |
| 666 | #endif |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 667 | if( fd<0 ){ |
| 668 | if( errno==EINTR ) continue; |
| 669 | break; |
| 670 | } |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 671 | if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 672 | osClose(fd); |
| 673 | sqlite3_log(SQLITE_WARNING, |
| 674 | "attempt to open \"%s\" as file descriptor %d", z, fd); |
| 675 | fd = -1; |
| 676 | if( osOpen("/dev/null", f, m)<0 ) break; |
| 677 | } |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 678 | if( fd>=0 ){ |
| 679 | if( m!=0 ){ |
| 680 | struct stat statbuf; |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 681 | if( osFstat(fd, &statbuf)==0 |
| 682 | && statbuf.st_size==0 |
drh | cfc1769 | 2013-03-06 01:41:53 +0000 | [diff] [blame] | 683 | && (statbuf.st_mode&0777)!=m |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 684 | ){ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 685 | osFchmod(fd, m); |
| 686 | } |
| 687 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 688 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 689 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 690 | #endif |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 691 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 692 | return fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 693 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 694 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 695 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 696 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 697 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 698 | ** vxworksFileId objects used by this file, all of which may be |
| 699 | ** shared by multiple threads. |
| 700 | ** |
| 701 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 702 | ** is held when required. This function is only used as part of assert() |
| 703 | ** statements. e.g. |
| 704 | ** |
| 705 | ** unixEnterMutex() |
| 706 | ** assert( unixMutexHeld() ); |
| 707 | ** unixEnterLeave() |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 708 | ** |
| 709 | ** To prevent deadlock, the global unixBigLock must must be acquired |
| 710 | ** before the unixInodeInfo.pLockMutex mutex, if both are held. It is |
| 711 | ** OK to get the pLockMutex without holding unixBigLock first, but if |
| 712 | ** that happens, the unixBigLock mutex must not be acquired until after |
| 713 | ** pLockMutex is released. |
| 714 | ** |
| 715 | ** OK: enter(unixBigLock), enter(pLockInfo) |
| 716 | ** OK: enter(unixBigLock) |
| 717 | ** OK: enter(pLockInfo) |
| 718 | ** ERROR: enter(pLockInfo), enter(unixBigLock) |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 719 | */ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 720 | static sqlite3_mutex *unixBigLock = 0; |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 721 | static void unixEnterMutex(void){ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 722 | assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 723 | sqlite3_mutex_enter(unixBigLock); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 724 | } |
| 725 | static void unixLeaveMutex(void){ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 726 | assert( sqlite3_mutex_held(unixBigLock) ); |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 727 | sqlite3_mutex_leave(unixBigLock); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 728 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 729 | #ifdef SQLITE_DEBUG |
| 730 | static int unixMutexHeld(void) { |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 731 | return sqlite3_mutex_held(unixBigLock); |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 732 | } |
| 733 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 734 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 735 | |
mistachkin | fb383e9 | 2015-04-16 03:24:38 +0000 | [diff] [blame] | 736 | #ifdef SQLITE_HAVE_OS_TRACE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 737 | /* |
| 738 | ** Helper function for printing out trace information from debugging |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 739 | ** binaries. This returns the string representation of the supplied |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 740 | ** integer lock-type. |
| 741 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 742 | static const char *azFileLock(int eFileLock){ |
| 743 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 744 | case NO_LOCK: return "NONE"; |
| 745 | case SHARED_LOCK: return "SHARED"; |
| 746 | case RESERVED_LOCK: return "RESERVED"; |
| 747 | case PENDING_LOCK: return "PENDING"; |
| 748 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 749 | } |
| 750 | return "ERROR"; |
| 751 | } |
| 752 | #endif |
| 753 | |
| 754 | #ifdef SQLITE_LOCK_TRACE |
| 755 | /* |
| 756 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 757 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 758 | ** This routine is used for troubleshooting locks on multithreaded |
| 759 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 760 | ** command-line option on the compiler. This code is normally |
| 761 | ** turned off. |
| 762 | */ |
| 763 | static int lockTrace(int fd, int op, struct flock *p){ |
| 764 | char *zOpName, *zType; |
| 765 | int s; |
| 766 | int savedErrno; |
| 767 | if( op==F_GETLK ){ |
| 768 | zOpName = "GETLK"; |
| 769 | }else if( op==F_SETLK ){ |
| 770 | zOpName = "SETLK"; |
| 771 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 772 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 773 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 774 | return s; |
| 775 | } |
| 776 | if( p->l_type==F_RDLCK ){ |
| 777 | zType = "RDLCK"; |
| 778 | }else if( p->l_type==F_WRLCK ){ |
| 779 | zType = "WRLCK"; |
| 780 | }else if( p->l_type==F_UNLCK ){ |
| 781 | zType = "UNLCK"; |
| 782 | }else{ |
| 783 | assert( 0 ); |
| 784 | } |
| 785 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 786 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 787 | savedErrno = errno; |
| 788 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 789 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 790 | (int)p->l_pid, s); |
| 791 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 792 | struct flock l2; |
| 793 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 794 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 795 | if( l2.l_type==F_RDLCK ){ |
| 796 | zType = "RDLCK"; |
| 797 | }else if( l2.l_type==F_WRLCK ){ |
| 798 | zType = "WRLCK"; |
| 799 | }else if( l2.l_type==F_UNLCK ){ |
| 800 | zType = "UNLCK"; |
| 801 | }else{ |
| 802 | assert( 0 ); |
| 803 | } |
| 804 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 805 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 806 | } |
| 807 | errno = savedErrno; |
| 808 | return s; |
| 809 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 810 | #undef osFcntl |
| 811 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 812 | #endif /* SQLITE_LOCK_TRACE */ |
| 813 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 814 | /* |
| 815 | ** Retry ftruncate() calls that fail due to EINTR |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 816 | ** |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 817 | ** All calls to ftruncate() within this file should be made through |
| 818 | ** this wrapper. On the Android platform, bypassing the logic below |
| 819 | ** could lead to a corrupt database. |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 820 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 821 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 822 | int rc; |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 823 | #ifdef __ANDROID__ |
| 824 | /* On Android, ftruncate() always uses 32-bit offsets, even if |
| 825 | ** _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] | 826 | ** truncate a file to any size larger than 2GiB. Silently ignore any |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 827 | ** such attempts. */ |
| 828 | if( sz>(sqlite3_int64)0x7FFFFFFF ){ |
| 829 | rc = SQLITE_OK; |
| 830 | }else |
| 831 | #endif |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 832 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 833 | return rc; |
| 834 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 835 | |
| 836 | /* |
| 837 | ** This routine translates a standard POSIX errno code into something |
| 838 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 839 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 840 | ** and a variety of "please close the file descriptor NOW" errors into |
| 841 | ** SQLITE_IOERR |
| 842 | ** |
| 843 | ** Errors during initialization of locks, or file system support for locks, |
| 844 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 845 | */ |
| 846 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 847 | assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 848 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 849 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 850 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 851 | switch (posixError) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 852 | case EACCES: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 853 | case EAGAIN: |
| 854 | case ETIMEDOUT: |
| 855 | case EBUSY: |
| 856 | case EINTR: |
| 857 | case ENOLCK: |
| 858 | /* random NFS retry error, unless during file system support |
| 859 | * introspection, in which it actually means what it says */ |
| 860 | return SQLITE_BUSY; |
| 861 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 862 | case EPERM: |
| 863 | return SQLITE_PERM; |
| 864 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 865 | default: |
| 866 | return sqliteIOErr; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 871 | /****************************************************************************** |
| 872 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 873 | ** |
| 874 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 875 | ** the device number and the inode number. But this does not work on VxWorks. |
| 876 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 877 | ** |
| 878 | ** A pointer to an instance of the following structure can be used as a |
| 879 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 880 | ** a copy of the canonical filename. There is also a reference count. |
| 881 | ** The structure is reclaimed when the number of pointers to it drops to |
| 882 | ** zero. |
| 883 | ** |
| 884 | ** There are never very many files open at one time and lookups are not |
| 885 | ** a performance-critical path, so it is sufficient to put these |
| 886 | ** structures on a linked list. |
| 887 | */ |
| 888 | struct vxworksFileId { |
| 889 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 890 | int nRef; /* Number of references to this one */ |
| 891 | int nName; /* Length of the zCanonicalName[] string */ |
| 892 | char *zCanonicalName; /* Canonical filename */ |
| 893 | }; |
| 894 | |
| 895 | #if OS_VXWORKS |
| 896 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 897 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 898 | ** variable: |
| 899 | */ |
| 900 | static struct vxworksFileId *vxworksFileList = 0; |
| 901 | |
| 902 | /* |
| 903 | ** Simplify a filename into its canonical form |
| 904 | ** by making the following changes: |
| 905 | ** |
| 906 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 907 | ** * convert /./ into just / |
| 908 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 909 | ** |
| 910 | ** Changes are made in-place. Return the new name length. |
| 911 | ** |
| 912 | ** The original filename is in z[0..n-1]. Return the number of |
| 913 | ** characters in the simplified name. |
| 914 | */ |
| 915 | static int vxworksSimplifyName(char *z, int n){ |
| 916 | int i, j; |
| 917 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 918 | for(i=j=0; i<n; i++){ |
| 919 | if( z[i]=='/' ){ |
| 920 | if( z[i+1]=='/' ) continue; |
| 921 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 922 | i += 1; |
| 923 | continue; |
| 924 | } |
| 925 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 926 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 927 | if( j>0 ){ j--; } |
| 928 | i += 2; |
| 929 | continue; |
| 930 | } |
| 931 | } |
| 932 | z[j++] = z[i]; |
| 933 | } |
| 934 | z[j] = 0; |
| 935 | return j; |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | ** Find a unique file ID for the given absolute pathname. Return |
| 940 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 941 | ** file ID. |
| 942 | ** |
| 943 | ** The nRef field of the vxworksFileId object is incremented before |
| 944 | ** the object is returned. A new vxworksFileId object is created |
| 945 | ** and added to the global list if necessary. |
| 946 | ** |
| 947 | ** If a memory allocation error occurs, return NULL. |
| 948 | */ |
| 949 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 950 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 951 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 952 | int n; /* Length of zAbsoluteName string */ |
| 953 | |
| 954 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 955 | n = (int)strlen(zAbsoluteName); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 956 | pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 957 | if( pNew==0 ) return 0; |
| 958 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 959 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 960 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 961 | |
| 962 | /* Search for an existing entry that matching the canonical name. |
| 963 | ** If found, increment the reference count and return a pointer to |
| 964 | ** the existing file ID. |
| 965 | */ |
| 966 | unixEnterMutex(); |
| 967 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 968 | if( pCandidate->nName==n |
| 969 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 970 | ){ |
| 971 | sqlite3_free(pNew); |
| 972 | pCandidate->nRef++; |
| 973 | unixLeaveMutex(); |
| 974 | return pCandidate; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | /* No match was found. We will make a new file ID */ |
| 979 | pNew->nRef = 1; |
| 980 | pNew->nName = n; |
| 981 | pNew->pNext = vxworksFileList; |
| 982 | vxworksFileList = pNew; |
| 983 | unixLeaveMutex(); |
| 984 | return pNew; |
| 985 | } |
| 986 | |
| 987 | /* |
| 988 | ** Decrement the reference count on a vxworksFileId object. Free |
| 989 | ** the object when the reference count reaches zero. |
| 990 | */ |
| 991 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 992 | unixEnterMutex(); |
| 993 | assert( pId->nRef>0 ); |
| 994 | pId->nRef--; |
| 995 | if( pId->nRef==0 ){ |
| 996 | struct vxworksFileId **pp; |
| 997 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 998 | assert( *pp==pId ); |
| 999 | *pp = pId->pNext; |
| 1000 | sqlite3_free(pId); |
| 1001 | } |
| 1002 | unixLeaveMutex(); |
| 1003 | } |
| 1004 | #endif /* OS_VXWORKS */ |
| 1005 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 1006 | ******************************************************************************/ |
| 1007 | |
| 1008 | |
| 1009 | /****************************************************************************** |
| 1010 | *************************** Posix Advisory Locking **************************** |
| 1011 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1012 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1013 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 1014 | ** sets or clears a lock, that operation overrides any prior locks set |
| 1015 | ** by the same process. It does not explicitly say so, but this implies |
| 1016 | ** that it overrides locks set by the same process using a different |
| 1017 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1018 | ** |
| 1019 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1020 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 1021 | ** |
| 1022 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 1023 | ** one is a hard or symbolic link to the other) then if you set |
| 1024 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 1025 | ** on fd2, it works. I would have expected the second lock to |
| 1026 | ** fail since there was already a lock on the file due to fd1. |
| 1027 | ** But not so. Since both locks came from the same process, the |
| 1028 | ** second overrides the first, even though they were on different |
| 1029 | ** file descriptors opened on different file names. |
| 1030 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1031 | ** This means that we cannot use POSIX locks to synchronize file access |
| 1032 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1033 | ** to synchronize access for threads in separate processes, but not |
| 1034 | ** threads within the same process. |
| 1035 | ** |
| 1036 | ** To work around the problem, SQLite has to manage file locks internally |
| 1037 | ** on its own. Whenever a new database is opened, we have to find the |
| 1038 | ** specific inode of the database file (the inode is determined by the |
| 1039 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 1040 | ** and check for locks already existing on that inode. When locks are |
| 1041 | ** created or removed, we have to look at our own internal record of the |
| 1042 | ** locks to see if another thread has previously set a lock on that same |
| 1043 | ** inode. |
| 1044 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1045 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 1046 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 1047 | ** canonical filename and implemented in the previous division.) |
| 1048 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1049 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1050 | ** descriptor. It is now a structure that holds the integer file |
| 1051 | ** descriptor and a pointer to a structure that describes the internal |
| 1052 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1053 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1054 | ** point to the same locking structure. The locking structure keeps |
| 1055 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 1056 | ** field that tells us its internal lock status. cnt==0 means the |
| 1057 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 1058 | ** cnt>0 means there are cnt shared locks on the file. |
| 1059 | ** |
| 1060 | ** Any attempt to lock or unlock a file first checks the locking |
| 1061 | ** structure. The fcntl() system call is only invoked to set a |
| 1062 | ** POSIX lock if the internal lock structure transitions between |
| 1063 | ** a locked and an unlocked state. |
| 1064 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1065 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1066 | ** |
| 1067 | ** If you close a file descriptor that points to a file that has locks, |
| 1068 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1069 | ** released. To work around this problem, each unixInodeInfo object |
| 1070 | ** maintains a count of the number of pending locks on tha inode. |
| 1071 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1072 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1073 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1074 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1075 | ** be closed and that list is walked (and cleared) when the last lock |
| 1076 | ** clears. |
| 1077 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1078 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1079 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1080 | ** Many older versions of linux use the LinuxThreads library which is |
| 1081 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1082 | ** A cannot be modified or overridden by a different thread B. |
| 1083 | ** Only thread A can modify the lock. Locking behavior is correct |
| 1084 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 1085 | ** on linux - with NPTL a lock created by thread A can override locks |
| 1086 | ** in thread B. But there is no way to know at compile-time which |
| 1087 | ** threading library is being used. So there is no way to know at |
| 1088 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1089 | ** 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] | 1090 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1091 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1092 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 1093 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 1094 | ** LinuxThreads provided that (1) there is no more than one connection |
| 1095 | ** per database file in the same process and (2) database connections |
| 1096 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1097 | */ |
| 1098 | |
| 1099 | /* |
| 1100 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1101 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1102 | */ |
| 1103 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1104 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1105 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1106 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1107 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1108 | /* We are told that some versions of Android contain a bug that |
| 1109 | ** sizes ino_t at only 32-bits instead of 64-bits. (See |
| 1110 | ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c) |
| 1111 | ** To work around this, always allocate 64-bits for the inode number. |
| 1112 | ** On small machines that only have 32-bit inodes, this wastes 4 bytes, |
| 1113 | ** but that should not be a big deal. */ |
| 1114 | /* WAS: ino_t ino; */ |
| 1115 | u64 ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1116 | #endif |
| 1117 | }; |
| 1118 | |
| 1119 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1120 | ** An instance of the following structure is allocated for each open |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1121 | ** inode. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1122 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1123 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1124 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1125 | ** object keeps a count of the number of unixFile pointing to it. |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1126 | ** |
| 1127 | ** Mutex rules: |
| 1128 | ** |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1129 | ** (1) Only the pLockMutex mutex must be held in order to read or write |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1130 | ** any of the locking fields: |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1131 | ** nShared, nLock, eFileLock, bProcessLock, pUnused |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1132 | ** |
| 1133 | ** (2) When nRef>0, then the following fields are unchanging and can |
| 1134 | ** be read (but not written) without holding any mutex: |
| 1135 | ** fileId, pLockMutex |
| 1136 | ** |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1137 | ** (3) With the exceptions above, all the fields may only be read |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1138 | ** or written while holding the global unixBigLock mutex. |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1139 | ** |
| 1140 | ** Deadlock prevention: The global unixBigLock mutex may not |
| 1141 | ** be acquired while holding the pLockMutex mutex. If both unixBigLock |
| 1142 | ** and pLockMutex are needed, then unixBigLock must be acquired first. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1143 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1144 | struct unixInodeInfo { |
| 1145 | struct unixFileId fileId; /* The lookup key */ |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1146 | sqlite3_mutex *pLockMutex; /* Hold this mutex for... */ |
| 1147 | int nShared; /* Number of SHARED locks held */ |
| 1148 | int nLock; /* Number of outstanding file locks */ |
| 1149 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 1150 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1151 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1152 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1153 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1154 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 1155 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 1156 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1157 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 1158 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1159 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1160 | sem_t *pSem; /* Named POSIX semaphore */ |
| 1161 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1162 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1163 | }; |
| 1164 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1165 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1166 | ** A lists of all unixInodeInfo objects. |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1167 | ** |
| 1168 | ** Must hold unixBigLock in order to read or write this variable. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1169 | */ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1170 | static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1171 | |
| 1172 | #ifdef SQLITE_DEBUG |
| 1173 | /* |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1174 | ** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not. |
| 1175 | ** This routine is used only within assert() to help verify correct mutex |
| 1176 | ** usage. |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1177 | */ |
| 1178 | int unixFileMutexHeld(unixFile *pFile){ |
| 1179 | assert( pFile->pInode ); |
| 1180 | return sqlite3_mutex_held(pFile->pInode->pLockMutex); |
| 1181 | } |
| 1182 | int unixFileMutexNotheld(unixFile *pFile){ |
| 1183 | assert( pFile->pInode ); |
| 1184 | return sqlite3_mutex_notheld(pFile->pInode->pLockMutex); |
| 1185 | } |
| 1186 | #endif |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1187 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1188 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1189 | ** |
drh | aaeaa18 | 2015-11-24 15:12:47 +0000 | [diff] [blame] | 1190 | ** This function - unixLogErrorAtLine(), is only ever called via the macro |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1191 | ** unixLogError(). |
| 1192 | ** |
| 1193 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1194 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1195 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1196 | ** strerror_r(). |
| 1197 | ** |
| 1198 | ** The first argument passed to the macro should be the error code that |
| 1199 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1200 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1201 | ** failed (e.g. "unlink", "open") and the associated file-system path, |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1202 | ** if any. |
| 1203 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1204 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1205 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1206 | int errcode, /* SQLite error code */ |
| 1207 | const char *zFunc, /* Name of OS function that failed */ |
| 1208 | const char *zPath, /* File path associated with error */ |
| 1209 | int iLine /* Source line number where error occurred */ |
| 1210 | ){ |
| 1211 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1212 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1213 | |
| 1214 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1215 | ** the strerror() function to obtain the human-readable error message |
| 1216 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1217 | */ |
| 1218 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1219 | char aErr[80]; |
| 1220 | memset(aErr, 0, sizeof(aErr)); |
| 1221 | zErr = aErr; |
| 1222 | |
| 1223 | /* 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] | 1224 | ** assume that the system provides the GNU version of strerror_r() that |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1225 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1226 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1227 | ** Otherwise, assume that the system provides the POSIX version of |
| 1228 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1229 | ** |
| 1230 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1231 | ** available, the error message will often be an empty string. Not a |
| 1232 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1233 | ** could lead to a segfault though. |
| 1234 | */ |
| 1235 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1236 | zErr = |
| 1237 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1238 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1239 | |
| 1240 | #elif SQLITE_THREADSAFE |
| 1241 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1242 | zErr = ""; |
| 1243 | #else |
| 1244 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1245 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1246 | #endif |
| 1247 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1248 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1249 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1250 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1251 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1252 | ); |
| 1253 | |
| 1254 | return errcode; |
| 1255 | } |
| 1256 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1257 | /* |
| 1258 | ** Close a file descriptor. |
| 1259 | ** |
| 1260 | ** We assume that close() almost always works, since it is only in a |
| 1261 | ** very sick application or on a very sick platform that it might fail. |
| 1262 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1263 | ** error. |
| 1264 | ** |
| 1265 | ** Note that it is not safe to retry close() after EINTR since the |
| 1266 | ** file descriptor might have already been reused by another thread. |
| 1267 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1268 | ** and move on. |
| 1269 | */ |
| 1270 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1271 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1272 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1273 | pFile ? pFile->zPath : 0, lineno); |
| 1274 | } |
| 1275 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1276 | |
| 1277 | /* |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 1278 | ** Set the pFile->lastErrno. Do this in a subroutine as that provides |
| 1279 | ** a convenient place to set a breakpoint. |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1280 | */ |
| 1281 | static void storeLastErrno(unixFile *pFile, int error){ |
| 1282 | pFile->lastErrno = error; |
| 1283 | } |
| 1284 | |
| 1285 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1286 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1287 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1288 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1289 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1290 | UnixUnusedFd *p; |
| 1291 | UnixUnusedFd *pNext; |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1292 | assert( unixFileMutexHeld(pFile) ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1293 | for(p=pInode->pUnused; p; p=pNext){ |
| 1294 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1295 | robust_close(pFile, p->fd, __LINE__); |
| 1296 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1297 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1298 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1302 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1303 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1304 | ** The global mutex must be held when this routine is called, but the mutex |
| 1305 | ** on the inode being deleted must NOT be held. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1306 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1307 | static void releaseInodeInfo(unixFile *pFile){ |
| 1308 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1309 | assert( unixMutexHeld() ); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1310 | assert( unixFileMutexNotheld(pFile) ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1311 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1312 | pInode->nRef--; |
| 1313 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1314 | assert( pInode->pShmNode==0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1315 | sqlite3_mutex_enter(pInode->pLockMutex); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1316 | closePendingFds(pFile); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1317 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1318 | if( pInode->pPrev ){ |
| 1319 | assert( pInode->pPrev->pNext==pInode ); |
| 1320 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1321 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1322 | assert( inodeList==pInode ); |
| 1323 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1324 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1325 | if( pInode->pNext ){ |
| 1326 | assert( pInode->pNext->pPrev==pInode ); |
| 1327 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1328 | } |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1329 | sqlite3_mutex_free(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1330 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1331 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1336 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1337 | ** describes that file descriptor. Create a new one if necessary. The |
| 1338 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1339 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1340 | ** The global mutex must held when calling this routine. |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1341 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1342 | ** Return an appropriate error code. |
| 1343 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1344 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1345 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1346 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1347 | ){ |
| 1348 | int rc; /* System call return code */ |
| 1349 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1350 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1351 | struct stat statbuf; /* Low-level file information */ |
| 1352 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1353 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1354 | assert( unixMutexHeld() ); |
| 1355 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1356 | /* Get low-level information about the file that we can used to |
| 1357 | ** create a unique name for the file. |
| 1358 | */ |
| 1359 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1360 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1361 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1362 | storeLastErrno(pFile, errno); |
drh | 40fe8d3 | 2015-11-30 20:36:26 +0000 | [diff] [blame] | 1363 | #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1364 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1365 | #endif |
| 1366 | return SQLITE_IOERR; |
| 1367 | } |
| 1368 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1369 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1370 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1371 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1372 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1373 | ** we always increase the file size to 1 by writing a single byte |
| 1374 | ** prior to accessing the inode number. The one byte written is |
| 1375 | ** an ASCII 'S' character which also happens to be the first byte |
| 1376 | ** in the header of every SQLite database. In this way, if there |
| 1377 | ** is a race condition such that another thread has already populated |
| 1378 | ** the first page of the database, no damage is done. |
| 1379 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1380 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1381 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1382 | if( rc!=1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1383 | storeLastErrno(pFile, errno); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1384 | return SQLITE_IOERR; |
| 1385 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1386 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1387 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1388 | storeLastErrno(pFile, errno); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1389 | return SQLITE_IOERR; |
| 1390 | } |
| 1391 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1392 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1393 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1394 | memset(&fileId, 0, sizeof(fileId)); |
| 1395 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1396 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1397 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1398 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1399 | fileId.ino = (u64)statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1400 | #endif |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1401 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1402 | pInode = inodeList; |
| 1403 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1404 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1405 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1406 | if( pInode==0 ){ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 1407 | pInode = sqlite3_malloc64( sizeof(*pInode) ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1408 | if( pInode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 1409 | return SQLITE_NOMEM_BKPT; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1410 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1411 | memset(pInode, 0, sizeof(*pInode)); |
| 1412 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
drh | 6886d6d | 2018-07-23 22:55:10 +0000 | [diff] [blame] | 1413 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 1414 | pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 1415 | if( pInode->pLockMutex==0 ){ |
| 1416 | sqlite3_free(pInode); |
| 1417 | return SQLITE_NOMEM_BKPT; |
| 1418 | } |
| 1419 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1420 | pInode->nRef = 1; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1421 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1422 | pInode->pNext = inodeList; |
| 1423 | pInode->pPrev = 0; |
| 1424 | if( inodeList ) inodeList->pPrev = pInode; |
| 1425 | inodeList = pInode; |
| 1426 | }else{ |
| 1427 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1428 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1429 | *ppInode = pInode; |
| 1430 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1431 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1432 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1433 | /* |
| 1434 | ** Return TRUE if pFile has been renamed or unlinked since it was first opened. |
| 1435 | */ |
| 1436 | static int fileHasMoved(unixFile *pFile){ |
drh | 61ffea5 | 2014-08-12 12:19:25 +0000 | [diff] [blame] | 1437 | #if OS_VXWORKS |
| 1438 | return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; |
| 1439 | #else |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1440 | struct stat buf; |
| 1441 | return pFile->pInode!=0 && |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1442 | (osStat(pFile->zPath, &buf)!=0 |
| 1443 | || (u64)buf.st_ino!=pFile->pInode->fileId.ino); |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 1444 | #endif |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1447 | |
| 1448 | /* |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1449 | ** Check a unixFile that is a database. Verify the following: |
| 1450 | ** |
| 1451 | ** (1) There is exactly one hard link on the file |
| 1452 | ** (2) The file is not a symbolic link |
| 1453 | ** (3) The file has not been renamed or unlinked |
| 1454 | ** |
| 1455 | ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. |
| 1456 | */ |
| 1457 | static void verifyDbFile(unixFile *pFile){ |
| 1458 | struct stat buf; |
| 1459 | int rc; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 1460 | |
| 1461 | /* These verifications occurs for the main database only */ |
| 1462 | if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return; |
| 1463 | |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1464 | rc = osFstat(pFile->h, &buf); |
| 1465 | if( rc!=0 ){ |
| 1466 | sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1467 | return; |
| 1468 | } |
drh | 6369bc3 | 2016-03-21 16:06:42 +0000 | [diff] [blame] | 1469 | if( buf.st_nlink==0 ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1470 | sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1471 | return; |
| 1472 | } |
| 1473 | if( buf.st_nlink>1 ){ |
| 1474 | sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1475 | return; |
| 1476 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1477 | if( fileHasMoved(pFile) ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1478 | sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1479 | return; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | |
| 1484 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1485 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1486 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1487 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1488 | ** 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] | 1489 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1490 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1491 | int rc = SQLITE_OK; |
| 1492 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1493 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1494 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1495 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1496 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1497 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1498 | assert( pFile->eFileLock<=SHARED_LOCK ); |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1499 | sqlite3_mutex_enter(pFile->pInode->pLockMutex); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1500 | |
| 1501 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1502 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1503 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1506 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1507 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1508 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1509 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1510 | struct flock lock; |
| 1511 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1512 | lock.l_start = RESERVED_BYTE; |
| 1513 | lock.l_len = 1; |
| 1514 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1515 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1516 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1517 | storeLastErrno(pFile, errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1518 | } else if( lock.l_type!=F_UNLCK ){ |
| 1519 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1520 | } |
| 1521 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1522 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1523 | |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1524 | sqlite3_mutex_leave(pFile->pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1525 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1526 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1527 | *pResOut = reserved; |
| 1528 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | /* |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1532 | ** Set a posix-advisory-lock. |
| 1533 | ** |
| 1534 | ** There are two versions of this routine. If compiled with |
| 1535 | ** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter |
| 1536 | ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout |
| 1537 | ** value is set, then it is the number of milliseconds to wait before |
| 1538 | ** failing the lock. The iBusyTimeout value is always reset back to |
| 1539 | ** zero on each call. |
| 1540 | ** |
| 1541 | ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking |
| 1542 | ** attempt to set the lock. |
| 1543 | */ |
| 1544 | #ifndef SQLITE_ENABLE_SETLK_TIMEOUT |
| 1545 | # define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x) |
| 1546 | #else |
| 1547 | static int osSetPosixAdvisoryLock( |
| 1548 | int h, /* The file descriptor on which to take the lock */ |
| 1549 | struct flock *pLock, /* The description of the lock */ |
| 1550 | unixFile *pFile /* Structure holding timeout value */ |
| 1551 | ){ |
| 1552 | int rc = osFcntl(h,F_SETLK,pLock); |
drh | fd72563 | 2018-03-26 20:43:05 +0000 | [diff] [blame] | 1553 | while( rc<0 && pFile->iBusyTimeout>0 ){ |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1554 | /* On systems that support some kind of blocking file lock with a timeout, |
| 1555 | ** make appropriate changes here to invoke that blocking file lock. On |
| 1556 | ** generic posix, however, there is no such API. So we simply try the |
| 1557 | ** lock once every millisecond until either the timeout expires, or until |
| 1558 | ** the lock is obtained. */ |
drh | fd72563 | 2018-03-26 20:43:05 +0000 | [diff] [blame] | 1559 | usleep(1000); |
| 1560 | rc = osFcntl(h,F_SETLK,pLock); |
| 1561 | pFile->iBusyTimeout--; |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1562 | } |
| 1563 | return rc; |
| 1564 | } |
| 1565 | #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 1566 | |
| 1567 | |
| 1568 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1569 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1570 | ** described by pLock. |
| 1571 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1572 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1573 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1574 | ** the first time any lock is attempted. All subsequent system locking |
| 1575 | ** operations become no-ops. Locking operations still happen internally, |
| 1576 | ** in order to coordinate access between separate database connections |
| 1577 | ** within this process, but all of that is handled in memory and the |
| 1578 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1579 | ** |
| 1580 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1581 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1582 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1583 | ** |
| 1584 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1585 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1586 | */ |
| 1587 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1588 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1589 | unixInodeInfo *pInode = pFile->pInode; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1590 | assert( pInode!=0 ); |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1591 | assert( sqlite3_mutex_held(pInode->pLockMutex) ); |
drh | 50358ad | 2015-12-02 01:04:33 +0000 | [diff] [blame] | 1592 | if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1593 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1594 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1595 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1596 | lock.l_whence = SEEK_SET; |
| 1597 | lock.l_start = SHARED_FIRST; |
| 1598 | lock.l_len = SHARED_SIZE; |
| 1599 | lock.l_type = F_WRLCK; |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1600 | rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1601 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1602 | pInode->bProcessLock = 1; |
| 1603 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1604 | }else{ |
| 1605 | rc = 0; |
| 1606 | } |
| 1607 | }else{ |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1608 | rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1609 | } |
| 1610 | return rc; |
| 1611 | } |
| 1612 | |
| 1613 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1614 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1615 | ** of the following: |
| 1616 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1617 | ** (1) SHARED_LOCK |
| 1618 | ** (2) RESERVED_LOCK |
| 1619 | ** (3) PENDING_LOCK |
| 1620 | ** (4) EXCLUSIVE_LOCK |
| 1621 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1622 | ** Sometimes when requesting one lock state, additional lock states |
| 1623 | ** are inserted in between. The locking might fail on one of the later |
| 1624 | ** transitions leaving the lock state different from what it started but |
| 1625 | ** still short of its goal. The following chart shows the allowed |
| 1626 | ** transitions and the inserted intermediate states: |
| 1627 | ** |
| 1628 | ** UNLOCKED -> SHARED |
| 1629 | ** SHARED -> RESERVED |
| 1630 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1631 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1632 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1633 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1634 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1635 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1636 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1637 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1638 | /* The following describes the implementation of the various locks and |
| 1639 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1640 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1641 | ** confusion with SQLite lock names). The algorithms are complicated |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1642 | ** slightly in order to be compatible with Windows95 systems simultaneously |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1643 | ** accessing the same database file, in case that is ever required. |
| 1644 | ** |
| 1645 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1646 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1647 | ** range', a range of 510 bytes at a well known offset. |
| 1648 | ** |
| 1649 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1650 | ** byte'. If this is successful, 'shared byte range' is read-locked |
| 1651 | ** and the lock on the 'pending byte' released. (Legacy note: When |
| 1652 | ** SQLite was first developed, Windows95 systems were still very common, |
| 1653 | ** and Widnows95 lacks a shared-lock capability. So on Windows95, a |
| 1654 | ** single randomly selected by from the 'shared byte range' is locked. |
| 1655 | ** Windows95 is now pretty much extinct, but this work-around for the |
| 1656 | ** lack of shared-locks on Windows95 lives on, for backwards |
| 1657 | ** compatibility.) |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1658 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1659 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1660 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1661 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1662 | ** |
| 1663 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1664 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1665 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1666 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1667 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1668 | ** This property is used by the algorithm for rolling back a journal file |
| 1669 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1670 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1671 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1672 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1673 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1674 | ** within this range, this ensures that no other locks are held on the |
| 1675 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1676 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1677 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1678 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1679 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1680 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1681 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1682 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1683 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1684 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1685 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 1686 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1687 | osGetpid(0))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1688 | |
| 1689 | /* 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] | 1690 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1691 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1692 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1693 | if( pFile->eFileLock>=eFileLock ){ |
| 1694 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1695 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1696 | return SQLITE_OK; |
| 1697 | } |
| 1698 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1699 | /* Make sure the locking sequence is correct. |
| 1700 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1701 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1702 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1703 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1704 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1705 | assert( eFileLock!=PENDING_LOCK ); |
| 1706 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1707 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1708 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1709 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1710 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1711 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1712 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1713 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1714 | ** handle that precludes the requested lock, return BUSY. |
| 1715 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1716 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1717 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1718 | ){ |
| 1719 | rc = SQLITE_BUSY; |
| 1720 | goto end_lock; |
| 1721 | } |
| 1722 | |
| 1723 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1724 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1725 | ** return SQLITE_OK. |
| 1726 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1727 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1728 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1729 | assert( eFileLock==SHARED_LOCK ); |
| 1730 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1731 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1732 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1733 | pInode->nShared++; |
| 1734 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1735 | goto end_lock; |
| 1736 | } |
| 1737 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1738 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1739 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1740 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1741 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1742 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1743 | lock.l_len = 1L; |
| 1744 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1745 | if( eFileLock==SHARED_LOCK |
| 1746 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1747 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1748 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1749 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1750 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1751 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1752 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1753 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1754 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1755 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1756 | goto end_lock; |
| 1757 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | |
| 1761 | /* If control gets to this point, then actually go ahead and make |
| 1762 | ** operating system calls for the specified lock. |
| 1763 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1764 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1765 | assert( pInode->nShared==0 ); |
| 1766 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1767 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1768 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1769 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1770 | lock.l_start = SHARED_FIRST; |
| 1771 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1772 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1773 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1774 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1775 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1776 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1777 | /* Drop the temporary PENDING lock */ |
| 1778 | lock.l_start = PENDING_BYTE; |
| 1779 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1780 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1781 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1782 | /* This could happen with a network mount */ |
| 1783 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1784 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1785 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1786 | |
| 1787 | if( rc ){ |
| 1788 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1789 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1790 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1791 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1792 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1793 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1794 | pInode->nLock++; |
| 1795 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1796 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1797 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1798 | /* We are trying for an exclusive lock but another thread in this |
| 1799 | ** same process is still holding a shared lock. */ |
| 1800 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1801 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1802 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1803 | ** assumed that there is a SHARED or greater lock on the file |
| 1804 | ** already. |
| 1805 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1806 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1807 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1808 | |
| 1809 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1810 | if( eFileLock==RESERVED_LOCK ){ |
| 1811 | lock.l_start = RESERVED_BYTE; |
| 1812 | lock.l_len = 1L; |
| 1813 | }else{ |
| 1814 | lock.l_start = SHARED_FIRST; |
| 1815 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1816 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1817 | |
| 1818 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1819 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1820 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1821 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1822 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1823 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1824 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1825 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1826 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1827 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1828 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1829 | /* Set up the transaction-counter change checking flags when |
| 1830 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1831 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1832 | ** write operation (not a hot journal rollback). |
| 1833 | */ |
| 1834 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1835 | && pFile->eFileLock<=SHARED_LOCK |
| 1836 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1837 | ){ |
| 1838 | pFile->transCntrChng = 0; |
| 1839 | pFile->dbUpdate = 0; |
| 1840 | pFile->inNormalWrite = 1; |
| 1841 | } |
| 1842 | #endif |
| 1843 | |
| 1844 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1845 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1846 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1847 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1848 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1849 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1850 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1851 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1852 | |
| 1853 | end_lock: |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1854 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1855 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1856 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1857 | return rc; |
| 1858 | } |
| 1859 | |
| 1860 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1861 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1862 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1863 | */ |
| 1864 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1865 | unixInodeInfo *pInode = pFile->pInode; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1866 | UnixUnusedFd *p = pFile->pPreallocatedUnused; |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1867 | assert( unixFileMutexHeld(pFile) ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1868 | p->pNext = pInode->pUnused; |
| 1869 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1870 | pFile->h = -1; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1871 | pFile->pPreallocatedUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1875 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1876 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1877 | ** |
| 1878 | ** If the locking level of the file descriptor is already at or below |
| 1879 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1880 | ** |
| 1881 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1882 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1883 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1884 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1885 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1886 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1887 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1888 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1889 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1890 | struct flock lock; |
| 1891 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1892 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1893 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1894 | 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] | 1895 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1896 | osGetpid(0))); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1897 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1898 | assert( eFileLock<=SHARED_LOCK ); |
| 1899 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1900 | return SQLITE_OK; |
| 1901 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1902 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1903 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1904 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1905 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1906 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1907 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1908 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1909 | /* When reducing a lock such that other processes can start |
| 1910 | ** reading the database file again, make sure that the |
| 1911 | ** transaction counter was updated if any part of the database |
| 1912 | ** file changed. If the transaction counter is not updated, |
| 1913 | ** other connections to the same file might not realize that |
| 1914 | ** the file has changed and hence might not know to flush their |
| 1915 | ** cache. The use of a stale cache can lead to database corruption. |
| 1916 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1917 | pFile->inNormalWrite = 0; |
| 1918 | #endif |
| 1919 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1920 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1921 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1922 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1923 | ** write lock until the rest is covered by a read lock: |
| 1924 | ** 1: [WWWWW] |
| 1925 | ** 2: [....W] |
| 1926 | ** 3: [RRRRW] |
| 1927 | ** 4: [RRRR.] |
| 1928 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1929 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1930 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1931 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1932 | assert( handleNFSUnlock==0 ); |
| 1933 | #endif |
| 1934 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1935 | if( handleNFSUnlock ){ |
drh | a712b4b | 2015-02-19 16:12:04 +0000 | [diff] [blame] | 1936 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1937 | off_t divSize = SHARED_SIZE - 1; |
| 1938 | |
| 1939 | lock.l_type = F_UNLCK; |
| 1940 | lock.l_whence = SEEK_SET; |
| 1941 | lock.l_start = SHARED_FIRST; |
| 1942 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1943 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1944 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1945 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1946 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1947 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1948 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1949 | lock.l_type = F_RDLCK; |
| 1950 | lock.l_whence = SEEK_SET; |
| 1951 | lock.l_start = SHARED_FIRST; |
| 1952 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1953 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1954 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1955 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1956 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1957 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1958 | } |
| 1959 | goto end_unlock; |
| 1960 | } |
| 1961 | lock.l_type = F_UNLCK; |
| 1962 | lock.l_whence = SEEK_SET; |
| 1963 | lock.l_start = SHARED_FIRST+divSize; |
| 1964 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1965 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1966 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1967 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1968 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1969 | goto end_unlock; |
| 1970 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1971 | }else |
| 1972 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1973 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1974 | lock.l_type = F_RDLCK; |
| 1975 | lock.l_whence = SEEK_SET; |
| 1976 | lock.l_start = SHARED_FIRST; |
| 1977 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1978 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1979 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1980 | ** process is holding an incompatible lock. If it does, this |
| 1981 | ** indicates that the other process is not following the locking |
| 1982 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1983 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1984 | ** an assert to fail). */ |
| 1985 | rc = SQLITE_IOERR_RDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1986 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1987 | goto end_unlock; |
| 1988 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1989 | } |
| 1990 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1991 | lock.l_type = F_UNLCK; |
| 1992 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1993 | lock.l_start = PENDING_BYTE; |
| 1994 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1995 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1996 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1997 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1998 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1999 | storeLastErrno(pFile, errno); |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 2000 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2001 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2002 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2003 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2004 | /* Decrement the shared lock counter. Release the lock using an |
| 2005 | ** OS call only when all threads in this same process have released |
| 2006 | ** the lock. |
| 2007 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2008 | pInode->nShared--; |
| 2009 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2010 | lock.l_type = F_UNLCK; |
| 2011 | lock.l_whence = SEEK_SET; |
| 2012 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2013 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2014 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2015 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2016 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2017 | storeLastErrno(pFile, errno); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2018 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2019 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2020 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2023 | /* Decrement the count of locks against this same file. When the |
| 2024 | ** count reaches zero, close any other file descriptors whose close |
| 2025 | ** was deferred because of outstanding locks. |
| 2026 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2027 | pInode->nLock--; |
| 2028 | assert( pInode->nLock>=0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2029 | if( pInode->nLock==0 ) closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2030 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2031 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2032 | end_unlock: |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2033 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2034 | if( rc==SQLITE_OK ){ |
| 2035 | pFile->eFileLock = eFileLock; |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2036 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 2037 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2041 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2042 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2043 | ** |
| 2044 | ** If the locking level of the file descriptor is already at or below |
| 2045 | ** the requested locking level, this routine is a no-op. |
| 2046 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2047 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 2048 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | a1afc74 | 2013-03-25 13:50:49 +0000 | [diff] [blame] | 2049 | assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 2050 | #endif |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 2051 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2054 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 2055 | static int unixMapfile(unixFile *pFd, i64 nByte); |
| 2056 | static void unixUnmapfile(unixFile *pFd); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2057 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 2058 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2059 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2060 | ** This function performs the parts of the "close file" operation |
| 2061 | ** common to all locking schemes. It closes the directory and file |
| 2062 | ** handles, if they are valid, and sets all fields of the unixFile |
| 2063 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2064 | ** |
| 2065 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 2066 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 2067 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2068 | */ |
| 2069 | static int closeUnixFile(sqlite3_file *id){ |
| 2070 | unixFile *pFile = (unixFile*)id; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2071 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 2072 | unixUnmapfile(pFile); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2073 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2074 | if( pFile->h>=0 ){ |
| 2075 | robust_close(pFile, pFile->h, __LINE__); |
| 2076 | pFile->h = -1; |
| 2077 | } |
| 2078 | #if OS_VXWORKS |
| 2079 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 2080 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 2081 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2082 | } |
| 2083 | vxworksReleaseFileId(pFile->pId); |
| 2084 | pFile->pId = 0; |
| 2085 | } |
| 2086 | #endif |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 2087 | #ifdef SQLITE_UNLINK_AFTER_CLOSE |
| 2088 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
| 2089 | osUnlink(pFile->zPath); |
| 2090 | sqlite3_free(*(char**)&pFile->zPath); |
| 2091 | pFile->zPath = 0; |
| 2092 | } |
| 2093 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2094 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 2095 | OpenCounter(-1); |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 2096 | sqlite3_free(pFile->pPreallocatedUnused); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2097 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2098 | return SQLITE_OK; |
| 2099 | } |
| 2100 | |
| 2101 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2102 | ** Close a file. |
| 2103 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2104 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2105 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2106 | unixFile *pFile = (unixFile *)id; |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2107 | unixInodeInfo *pInode = pFile->pInode; |
| 2108 | |
| 2109 | assert( pInode!=0 ); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 2110 | verifyDbFile(pFile); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2111 | unixUnlock(id, NO_LOCK); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2112 | assert( unixFileMutexNotheld(pFile) ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2113 | unixEnterMutex(); |
| 2114 | |
| 2115 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 2116 | ** routine (e.g. nolockClose()) would be called instead. |
| 2117 | */ |
| 2118 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2119 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 3fcef1a | 2018-08-16 16:24:24 +0000 | [diff] [blame] | 2120 | if( pInode->nLock ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2121 | /* If there are outstanding locks, do not actually close the file just |
| 2122 | ** yet because that would clear those locks. Instead, add the file |
| 2123 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 2124 | ** when the last lock is cleared. |
| 2125 | */ |
| 2126 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2127 | } |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2128 | sqlite3_mutex_leave(pInode->pLockMutex); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2129 | releaseInodeInfo(pFile); |
| 2130 | rc = closeUnixFile(id); |
| 2131 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2132 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2135 | /************** End of the posix advisory lock implementation ***************** |
| 2136 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2137 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2138 | /****************************************************************************** |
| 2139 | ****************************** No-op Locking ********************************** |
| 2140 | ** |
| 2141 | ** Of the various locking implementations available, this is by far the |
| 2142 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 2143 | ** file for reading or writing. |
| 2144 | ** |
| 2145 | ** This locking mode is appropriate for use on read-only databases |
| 2146 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 2147 | ** also be used if the application employs some external mechanism to |
| 2148 | ** prevent simultaneous access of the same database by two or more |
| 2149 | ** database connections. But there is a serious risk of database |
| 2150 | ** corruption if this locking mode is used in situations where multiple |
| 2151 | ** database connections are accessing the same database file at the same |
| 2152 | ** time and one or more of those connections are writing. |
| 2153 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2154 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2155 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 2156 | UNUSED_PARAMETER(NotUsed); |
| 2157 | *pResOut = 0; |
| 2158 | return SQLITE_OK; |
| 2159 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2160 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2161 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2162 | return SQLITE_OK; |
| 2163 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2164 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2165 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2166 | return SQLITE_OK; |
| 2167 | } |
| 2168 | |
| 2169 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2170 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2171 | */ |
| 2172 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2173 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | /******************* End of the no-op lock implementation ********************* |
| 2177 | ******************************************************************************/ |
| 2178 | |
| 2179 | /****************************************************************************** |
| 2180 | ************************* Begin dot-file Locking ****************************** |
| 2181 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2182 | ** The dotfile locking implementation uses the existence of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2183 | ** files (really a directory) to control access to the database. This works |
| 2184 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2185 | ** |
| 2186 | ** (1) There is zero concurrency. A single reader blocks all other |
| 2187 | ** connections from reading or writing the database. |
| 2188 | ** |
| 2189 | ** (2) An application crash or power loss can leave stale lock files |
| 2190 | ** sitting around that need to be cleared manually. |
| 2191 | ** |
| 2192 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 2193 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2194 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2195 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 2196 | ** the database and with the same name but with a ".lock" extension added. |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2197 | ** The existence of a lock directory implies an EXCLUSIVE lock. All other |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2198 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2199 | */ |
| 2200 | |
| 2201 | /* |
| 2202 | ** 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] | 2203 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2204 | */ |
| 2205 | #define DOTLOCK_SUFFIX ".lock" |
| 2206 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2207 | /* |
| 2208 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2209 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2210 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2211 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2212 | ** |
| 2213 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 2214 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 2215 | ** is held on the file and false if the file is unlocked. |
| 2216 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2217 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2218 | int rc = SQLITE_OK; |
| 2219 | int reserved = 0; |
| 2220 | unixFile *pFile = (unixFile*)id; |
| 2221 | |
| 2222 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2223 | |
| 2224 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2225 | reserved = osAccess((const char*)pFile->lockingContext, 0)==0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2226 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2227 | *pResOut = reserved; |
| 2228 | return rc; |
| 2229 | } |
| 2230 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2231 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2232 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2233 | ** of the following: |
| 2234 | ** |
| 2235 | ** (1) SHARED_LOCK |
| 2236 | ** (2) RESERVED_LOCK |
| 2237 | ** (3) PENDING_LOCK |
| 2238 | ** (4) EXCLUSIVE_LOCK |
| 2239 | ** |
| 2240 | ** Sometimes when requesting one lock state, additional lock states |
| 2241 | ** are inserted in between. The locking might fail on one of the later |
| 2242 | ** transitions leaving the lock state different from what it started but |
| 2243 | ** still short of its goal. The following chart shows the allowed |
| 2244 | ** transitions and the inserted intermediate states: |
| 2245 | ** |
| 2246 | ** UNLOCKED -> SHARED |
| 2247 | ** SHARED -> RESERVED |
| 2248 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2249 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2250 | ** PENDING -> EXCLUSIVE |
| 2251 | ** |
| 2252 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2253 | ** routine to lower a locking level. |
| 2254 | ** |
| 2255 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 2256 | ** But we track the other locking levels internally. |
| 2257 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2258 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2259 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2260 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2261 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2262 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2263 | |
| 2264 | /* If we have any lock, then the lock file already exists. All we have |
| 2265 | ** to do is adjust our internal record of the lock level. |
| 2266 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2267 | if( pFile->eFileLock > NO_LOCK ){ |
| 2268 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2269 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 2270 | #ifdef HAVE_UTIME |
| 2271 | utime(zLockFile, NULL); |
| 2272 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2273 | utimes(zLockFile, NULL); |
| 2274 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2275 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2279 | rc = osMkdir(zLockFile, 0777); |
| 2280 | if( rc<0 ){ |
| 2281 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2282 | int tErrno = errno; |
| 2283 | if( EEXIST == tErrno ){ |
| 2284 | rc = SQLITE_BUSY; |
| 2285 | } else { |
| 2286 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2287 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2288 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2289 | } |
| 2290 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2291 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2292 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2293 | |
| 2294 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2295 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2296 | return rc; |
| 2297 | } |
| 2298 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2299 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2300 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2301 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2302 | ** |
| 2303 | ** If the locking level of the file descriptor is already at or below |
| 2304 | ** the requested locking level, this routine is a no-op. |
| 2305 | ** |
| 2306 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2307 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2308 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2309 | unixFile *pFile = (unixFile*)id; |
| 2310 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2311 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2312 | |
| 2313 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2314 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2315 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2316 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2317 | |
| 2318 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2319 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2320 | return SQLITE_OK; |
| 2321 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2322 | |
| 2323 | /* To downgrade to shared, simply update our internal notion of the |
| 2324 | ** lock state. No need to mess with the file on disk. |
| 2325 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2326 | if( eFileLock==SHARED_LOCK ){ |
| 2327 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2328 | return SQLITE_OK; |
| 2329 | } |
| 2330 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2331 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2332 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2333 | rc = osRmdir(zLockFile); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2334 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2335 | int tErrno = errno; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2336 | if( tErrno==ENOENT ){ |
| 2337 | rc = SQLITE_OK; |
| 2338 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2339 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2340 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2341 | } |
| 2342 | return rc; |
| 2343 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2344 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2345 | return SQLITE_OK; |
| 2346 | } |
| 2347 | |
| 2348 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2349 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2350 | */ |
| 2351 | static int dotlockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2352 | unixFile *pFile = (unixFile*)id; |
| 2353 | assert( id!=0 ); |
| 2354 | dotlockUnlock(id, NO_LOCK); |
| 2355 | sqlite3_free(pFile->lockingContext); |
| 2356 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2357 | } |
| 2358 | /****************** End of the dot-file lock implementation ******************* |
| 2359 | ******************************************************************************/ |
| 2360 | |
| 2361 | /****************************************************************************** |
| 2362 | ************************** Begin flock Locking ******************************** |
| 2363 | ** |
| 2364 | ** Use the flock() system call to do file locking. |
| 2365 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2366 | ** flock() locking is like dot-file locking in that the various |
| 2367 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2368 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2369 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2370 | ** still works when you do this, but concurrency is reduced since |
| 2371 | ** only a single process can be reading the database at a time. |
| 2372 | ** |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2373 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2374 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2375 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2376 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2377 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2378 | ** Retry flock() calls that fail with EINTR |
| 2379 | */ |
| 2380 | #ifdef EINTR |
| 2381 | static int robust_flock(int fd, int op){ |
| 2382 | int rc; |
| 2383 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2384 | return rc; |
| 2385 | } |
| 2386 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2387 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2388 | #endif |
| 2389 | |
| 2390 | |
| 2391 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2392 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2393 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2394 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2395 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2396 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2397 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2398 | int rc = SQLITE_OK; |
| 2399 | int reserved = 0; |
| 2400 | unixFile *pFile = (unixFile*)id; |
| 2401 | |
| 2402 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2403 | |
| 2404 | assert( pFile ); |
| 2405 | |
| 2406 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2407 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2408 | reserved = 1; |
| 2409 | } |
| 2410 | |
| 2411 | /* Otherwise see if some other process holds it. */ |
| 2412 | if( !reserved ){ |
| 2413 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2414 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2415 | if( !lrc ){ |
| 2416 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2417 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2418 | if ( lrc ) { |
| 2419 | int tErrno = errno; |
| 2420 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2421 | lrc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2422 | storeLastErrno(pFile, tErrno); |
| 2423 | rc = lrc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2424 | } |
| 2425 | } else { |
| 2426 | int tErrno = errno; |
| 2427 | reserved = 1; |
| 2428 | /* someone else might have it reserved */ |
| 2429 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2430 | if( IS_LOCK_ERROR(lrc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2431 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2432 | rc = lrc; |
| 2433 | } |
| 2434 | } |
| 2435 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2436 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2437 | |
| 2438 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 2439 | if( (rc & 0xff) == SQLITE_IOERR ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2440 | rc = SQLITE_OK; |
| 2441 | reserved=1; |
| 2442 | } |
| 2443 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2444 | *pResOut = reserved; |
| 2445 | return rc; |
| 2446 | } |
| 2447 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2448 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2449 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2450 | ** of the following: |
| 2451 | ** |
| 2452 | ** (1) SHARED_LOCK |
| 2453 | ** (2) RESERVED_LOCK |
| 2454 | ** (3) PENDING_LOCK |
| 2455 | ** (4) EXCLUSIVE_LOCK |
| 2456 | ** |
| 2457 | ** Sometimes when requesting one lock state, additional lock states |
| 2458 | ** are inserted in between. The locking might fail on one of the later |
| 2459 | ** transitions leaving the lock state different from what it started but |
| 2460 | ** still short of its goal. The following chart shows the allowed |
| 2461 | ** transitions and the inserted intermediate states: |
| 2462 | ** |
| 2463 | ** UNLOCKED -> SHARED |
| 2464 | ** SHARED -> RESERVED |
| 2465 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2466 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2467 | ** PENDING -> EXCLUSIVE |
| 2468 | ** |
| 2469 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2470 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2471 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2472 | ** access the file. |
| 2473 | ** |
| 2474 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2475 | ** routine to lower a locking level. |
| 2476 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2477 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2478 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2479 | unixFile *pFile = (unixFile*)id; |
| 2480 | |
| 2481 | assert( pFile ); |
| 2482 | |
| 2483 | /* if we already have a lock, it is exclusive. |
| 2484 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2485 | if (pFile->eFileLock > NO_LOCK) { |
| 2486 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2487 | return SQLITE_OK; |
| 2488 | } |
| 2489 | |
| 2490 | /* grab an exclusive lock */ |
| 2491 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2492 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2493 | int tErrno = errno; |
| 2494 | /* didn't get, must be busy */ |
| 2495 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2496 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2497 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2498 | } |
| 2499 | } else { |
| 2500 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2501 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2502 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2503 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2504 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2505 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 2506 | if( (rc & 0xff) == SQLITE_IOERR ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2507 | rc = SQLITE_BUSY; |
| 2508 | } |
| 2509 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2510 | return rc; |
| 2511 | } |
| 2512 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2513 | |
| 2514 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2515 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2516 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2517 | ** |
| 2518 | ** If the locking level of the file descriptor is already at or below |
| 2519 | ** the requested locking level, this routine is a no-op. |
| 2520 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2521 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2522 | unixFile *pFile = (unixFile*)id; |
| 2523 | |
| 2524 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2525 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2526 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2527 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2528 | |
| 2529 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2530 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2531 | return SQLITE_OK; |
| 2532 | } |
| 2533 | |
| 2534 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2535 | if (eFileLock==SHARED_LOCK) { |
| 2536 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2537 | return SQLITE_OK; |
| 2538 | } |
| 2539 | |
| 2540 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2541 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2542 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2543 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2544 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2545 | return SQLITE_IOERR_UNLOCK; |
| 2546 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2547 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2548 | return SQLITE_OK; |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | /* |
| 2553 | ** Close a file. |
| 2554 | */ |
| 2555 | static int flockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2556 | assert( id!=0 ); |
| 2557 | flockUnlock(id, NO_LOCK); |
| 2558 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2562 | |
| 2563 | /******************* End of the flock lock implementation ********************* |
| 2564 | ******************************************************************************/ |
| 2565 | |
| 2566 | /****************************************************************************** |
| 2567 | ************************ Begin Named Semaphore Locking ************************ |
| 2568 | ** |
| 2569 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2570 | ** |
| 2571 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2572 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2573 | ** the database file at a time. This reduces potential concurrency, but |
| 2574 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2575 | */ |
| 2576 | #if OS_VXWORKS |
| 2577 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2578 | /* |
| 2579 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2580 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2581 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2582 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2583 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2584 | static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2585 | int rc = SQLITE_OK; |
| 2586 | int reserved = 0; |
| 2587 | unixFile *pFile = (unixFile*)id; |
| 2588 | |
| 2589 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2590 | |
| 2591 | assert( pFile ); |
| 2592 | |
| 2593 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2594 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2595 | reserved = 1; |
| 2596 | } |
| 2597 | |
| 2598 | /* Otherwise see if some other process holds it. */ |
| 2599 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2600 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2601 | |
| 2602 | if( sem_trywait(pSem)==-1 ){ |
| 2603 | int tErrno = errno; |
| 2604 | if( EAGAIN != tErrno ){ |
| 2605 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2606 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2607 | } else { |
| 2608 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2609 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2610 | } |
| 2611 | }else{ |
| 2612 | /* we could have it if we want it */ |
| 2613 | sem_post(pSem); |
| 2614 | } |
| 2615 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2616 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2617 | |
| 2618 | *pResOut = reserved; |
| 2619 | return rc; |
| 2620 | } |
| 2621 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2622 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2623 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2624 | ** of the following: |
| 2625 | ** |
| 2626 | ** (1) SHARED_LOCK |
| 2627 | ** (2) RESERVED_LOCK |
| 2628 | ** (3) PENDING_LOCK |
| 2629 | ** (4) EXCLUSIVE_LOCK |
| 2630 | ** |
| 2631 | ** Sometimes when requesting one lock state, additional lock states |
| 2632 | ** are inserted in between. The locking might fail on one of the later |
| 2633 | ** transitions leaving the lock state different from what it started but |
| 2634 | ** still short of its goal. The following chart shows the allowed |
| 2635 | ** transitions and the inserted intermediate states: |
| 2636 | ** |
| 2637 | ** UNLOCKED -> SHARED |
| 2638 | ** SHARED -> RESERVED |
| 2639 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2640 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2641 | ** PENDING -> EXCLUSIVE |
| 2642 | ** |
| 2643 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2644 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2645 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2646 | ** access the file. |
| 2647 | ** |
| 2648 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2649 | ** routine to lower a locking level. |
| 2650 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2651 | static int semXLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2652 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2653 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2654 | int rc = SQLITE_OK; |
| 2655 | |
| 2656 | /* if we already have a lock, it is exclusive. |
| 2657 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2658 | if (pFile->eFileLock > NO_LOCK) { |
| 2659 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2660 | rc = SQLITE_OK; |
| 2661 | goto sem_end_lock; |
| 2662 | } |
| 2663 | |
| 2664 | /* lock semaphore now but bail out when already locked. */ |
| 2665 | if( sem_trywait(pSem)==-1 ){ |
| 2666 | rc = SQLITE_BUSY; |
| 2667 | goto sem_end_lock; |
| 2668 | } |
| 2669 | |
| 2670 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2671 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2672 | |
| 2673 | sem_end_lock: |
| 2674 | return rc; |
| 2675 | } |
| 2676 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2677 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2678 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2679 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2680 | ** |
| 2681 | ** If the locking level of the file descriptor is already at or below |
| 2682 | ** the requested locking level, this routine is a no-op. |
| 2683 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2684 | static int semXUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2685 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2686 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2687 | |
| 2688 | assert( pFile ); |
| 2689 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2690 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2691 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2692 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2693 | |
| 2694 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2695 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2696 | return SQLITE_OK; |
| 2697 | } |
| 2698 | |
| 2699 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2700 | if (eFileLock==SHARED_LOCK) { |
| 2701 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2702 | return SQLITE_OK; |
| 2703 | } |
| 2704 | |
| 2705 | /* no, really unlock. */ |
| 2706 | if ( sem_post(pSem)==-1 ) { |
| 2707 | int rc, tErrno = errno; |
| 2708 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2709 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2710 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2711 | } |
| 2712 | return rc; |
| 2713 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2714 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2715 | return SQLITE_OK; |
| 2716 | } |
| 2717 | |
| 2718 | /* |
| 2719 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2720 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2721 | static int semXClose(sqlite3_file *id) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2722 | if( id ){ |
| 2723 | unixFile *pFile = (unixFile*)id; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2724 | semXUnlock(id, NO_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2725 | assert( pFile ); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2726 | assert( unixFileMutexNotheld(pFile) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2727 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2728 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2729 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2730 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2731 | } |
| 2732 | return SQLITE_OK; |
| 2733 | } |
| 2734 | |
| 2735 | #endif /* OS_VXWORKS */ |
| 2736 | /* |
| 2737 | ** Named semaphore locking is only available on VxWorks. |
| 2738 | ** |
| 2739 | *************** End of the named semaphore lock implementation **************** |
| 2740 | ******************************************************************************/ |
| 2741 | |
| 2742 | |
| 2743 | /****************************************************************************** |
| 2744 | *************************** Begin AFP Locking ********************************* |
| 2745 | ** |
| 2746 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2747 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2748 | ** |
| 2749 | ** Third-party implementations of AFP are available. But this code here |
| 2750 | ** only works on OSX. |
| 2751 | */ |
| 2752 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2753 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2754 | /* |
| 2755 | ** The afpLockingContext structure contains all afp lock specific state |
| 2756 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2757 | typedef struct afpLockingContext afpLockingContext; |
| 2758 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2759 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2760 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2761 | }; |
| 2762 | |
| 2763 | struct ByteRangeLockPB2 |
| 2764 | { |
| 2765 | unsigned long long offset; /* offset to first byte to lock */ |
| 2766 | unsigned long long length; /* nbr of bytes to lock */ |
| 2767 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2768 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2769 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2770 | int fd; /* file desc to assoc this lock with */ |
| 2771 | }; |
| 2772 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2773 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2774 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2775 | /* |
| 2776 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2777 | ** AFP filesystem. |
| 2778 | ** |
| 2779 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2780 | */ |
| 2781 | static int afpSetLock( |
| 2782 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2783 | unixFile *pFile, /* Open file descriptor on path */ |
| 2784 | unsigned long long offset, /* First byte to be locked */ |
| 2785 | unsigned long long length, /* Number of bytes to lock */ |
| 2786 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2787 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2788 | struct ByteRangeLockPB2 pb; |
| 2789 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2790 | |
| 2791 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2792 | pb.startEndFlag = 0; |
| 2793 | pb.offset = offset; |
| 2794 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2795 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2796 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2797 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2798 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2799 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2800 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2801 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2802 | int rc; |
| 2803 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2804 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2805 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2806 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2807 | rc = SQLITE_BUSY; |
| 2808 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2809 | rc = sqliteErrorFromPosixError(tErrno, |
| 2810 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2811 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2812 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2813 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2814 | } |
| 2815 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2816 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2817 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2821 | /* |
| 2822 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2823 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2824 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2825 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2826 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2827 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2828 | int rc = SQLITE_OK; |
| 2829 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2830 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2831 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2832 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2833 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2834 | |
| 2835 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2836 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2837 | if( context->reserved ){ |
| 2838 | *pResOut = 1; |
| 2839 | return SQLITE_OK; |
| 2840 | } |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2841 | sqlite3_mutex_enter(pFile->pInode->pLockMutex); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2842 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2843 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2844 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | /* Otherwise see if some other process holds it. |
| 2848 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2849 | if( !reserved ){ |
| 2850 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2851 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2852 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2853 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2854 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2855 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2856 | } else { |
| 2857 | /* if we failed to get the lock then someone else must have it */ |
| 2858 | reserved = 1; |
| 2859 | } |
| 2860 | if( IS_LOCK_ERROR(lrc) ){ |
| 2861 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2862 | } |
| 2863 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2864 | |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2865 | sqlite3_mutex_leave(pFile->pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2866 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2867 | |
| 2868 | *pResOut = reserved; |
| 2869 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2872 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2873 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2874 | ** of the following: |
| 2875 | ** |
| 2876 | ** (1) SHARED_LOCK |
| 2877 | ** (2) RESERVED_LOCK |
| 2878 | ** (3) PENDING_LOCK |
| 2879 | ** (4) EXCLUSIVE_LOCK |
| 2880 | ** |
| 2881 | ** Sometimes when requesting one lock state, additional lock states |
| 2882 | ** are inserted in between. The locking might fail on one of the later |
| 2883 | ** transitions leaving the lock state different from what it started but |
| 2884 | ** still short of its goal. The following chart shows the allowed |
| 2885 | ** transitions and the inserted intermediate states: |
| 2886 | ** |
| 2887 | ** UNLOCKED -> SHARED |
| 2888 | ** SHARED -> RESERVED |
| 2889 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2890 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2891 | ** PENDING -> EXCLUSIVE |
| 2892 | ** |
| 2893 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2894 | ** routine to lower a locking level. |
| 2895 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2896 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2897 | int rc = SQLITE_OK; |
| 2898 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2899 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2900 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2901 | |
| 2902 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2903 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2904 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2905 | azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2906 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2907 | /* 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] | 2908 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2909 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2910 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2911 | if( pFile->eFileLock>=eFileLock ){ |
| 2912 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2913 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2914 | return SQLITE_OK; |
| 2915 | } |
| 2916 | |
| 2917 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2918 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2919 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2920 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2921 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2922 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2923 | assert( eFileLock!=PENDING_LOCK ); |
| 2924 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2925 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2926 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2927 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2928 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2929 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2930 | |
| 2931 | /* If some thread using this PID has a lock via a different unixFile* |
| 2932 | ** handle that precludes the requested lock, return BUSY. |
| 2933 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2934 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2935 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2936 | ){ |
| 2937 | rc = SQLITE_BUSY; |
| 2938 | goto afp_end_lock; |
| 2939 | } |
| 2940 | |
| 2941 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2942 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2943 | ** return SQLITE_OK. |
| 2944 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2945 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2946 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2947 | assert( eFileLock==SHARED_LOCK ); |
| 2948 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2949 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2950 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2951 | pInode->nShared++; |
| 2952 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2953 | goto afp_end_lock; |
| 2954 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2955 | |
| 2956 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2957 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2958 | ** be released. |
| 2959 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2960 | if( eFileLock==SHARED_LOCK |
| 2961 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2962 | ){ |
| 2963 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2964 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2965 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2966 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2967 | goto afp_end_lock; |
| 2968 | } |
| 2969 | } |
| 2970 | |
| 2971 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2972 | ** operating system calls for the specified lock. |
| 2973 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2974 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2975 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2976 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2977 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2978 | assert( pInode->nShared==0 ); |
| 2979 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2980 | |
| 2981 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2982 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2983 | /* note that the quality of the randomness doesn't matter that much */ |
| 2984 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2985 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2986 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2987 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2988 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2989 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2990 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2991 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2992 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2993 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2994 | if( IS_LOCK_ERROR(lrc1) ) { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2995 | storeLastErrno(pFile, lrc1Errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2996 | rc = lrc1; |
| 2997 | goto afp_end_lock; |
| 2998 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2999 | rc = lrc2; |
| 3000 | goto afp_end_lock; |
| 3001 | } else if( lrc1 != SQLITE_OK ) { |
| 3002 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3003 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3004 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3005 | pInode->nLock++; |
| 3006 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3007 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3008 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3009 | /* We are trying for an exclusive lock but another thread in this |
| 3010 | ** same process is still holding a shared lock. */ |
| 3011 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3012 | }else{ |
| 3013 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 3014 | ** assumed that there is a SHARED or greater lock on the file |
| 3015 | ** already. |
| 3016 | */ |
| 3017 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3018 | assert( 0!=pFile->eFileLock ); |
| 3019 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3020 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3021 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3022 | if( !failed ){ |
| 3023 | context->reserved = 1; |
| 3024 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3025 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3026 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3027 | /* Acquire an EXCLUSIVE lock */ |
| 3028 | |
| 3029 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3030 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3031 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3032 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3033 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3034 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3035 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3036 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3037 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3038 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3039 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3040 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 3041 | ** a critical I/O error |
| 3042 | */ |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 3043 | rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 : |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3044 | SQLITE_IOERR_LOCK; |
| 3045 | goto afp_end_lock; |
| 3046 | } |
| 3047 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3048 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3049 | } |
| 3050 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3051 | if( failed ){ |
| 3052 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3057 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3058 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3059 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 3060 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3061 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | afp_end_lock: |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 3065 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3066 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 3067 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3068 | return rc; |
| 3069 | } |
| 3070 | |
| 3071 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3072 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3073 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3074 | ** |
| 3075 | ** If the locking level of the file descriptor is already at or below |
| 3076 | ** the requested locking level, this routine is a no-op. |
| 3077 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3078 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3079 | int rc = SQLITE_OK; |
| 3080 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3081 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3082 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 3083 | int skipShared = 0; |
| 3084 | #ifdef SQLITE_TEST |
| 3085 | int h = pFile->h; |
| 3086 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3087 | |
| 3088 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3089 | 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] | 3090 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 3091 | osGetpid(0))); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3092 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3093 | assert( eFileLock<=SHARED_LOCK ); |
| 3094 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3095 | return SQLITE_OK; |
| 3096 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3097 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 3098 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3099 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3100 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3101 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3102 | SimulateIOErrorBenign(1); |
| 3103 | SimulateIOError( h=(-1) ) |
| 3104 | SimulateIOErrorBenign(0); |
| 3105 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3106 | #ifdef SQLITE_DEBUG |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3107 | /* When reducing a lock such that other processes can start |
| 3108 | ** reading the database file again, make sure that the |
| 3109 | ** transaction counter was updated if any part of the database |
| 3110 | ** file changed. If the transaction counter is not updated, |
| 3111 | ** other connections to the same file might not realize that |
| 3112 | ** the file has changed and hence might not know to flush their |
| 3113 | ** cache. The use of a stale cache can lead to database corruption. |
| 3114 | */ |
| 3115 | assert( pFile->inNormalWrite==0 |
| 3116 | || pFile->dbUpdate==0 |
| 3117 | || pFile->transCntrChng==1 ); |
| 3118 | pFile->inNormalWrite = 0; |
| 3119 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3120 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3121 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3122 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3123 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3124 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3125 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3126 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 3127 | } else { |
| 3128 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3129 | } |
| 3130 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3131 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3132 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3133 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3134 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3135 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 3136 | if( !rc ){ |
| 3137 | context->reserved = 0; |
| 3138 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3139 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3140 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 3141 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3142 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3143 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3144 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3145 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3146 | /* Decrement the shared lock counter. Release the lock using an |
| 3147 | ** OS call only when all threads in this same process have released |
| 3148 | ** the lock. |
| 3149 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3150 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 3151 | pInode->nShared--; |
| 3152 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3153 | SimulateIOErrorBenign(1); |
| 3154 | SimulateIOError( h=(-1) ) |
| 3155 | SimulateIOErrorBenign(0); |
| 3156 | if( !skipShared ){ |
| 3157 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 3158 | } |
| 3159 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3160 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3161 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3165 | pInode->nLock--; |
| 3166 | assert( pInode->nLock>=0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 3167 | if( pInode->nLock==0 ) closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3168 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3169 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3170 | |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 3171 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 3172 | if( rc==SQLITE_OK ){ |
| 3173 | pFile->eFileLock = eFileLock; |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 3174 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3175 | return rc; |
| 3176 | } |
| 3177 | |
| 3178 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3179 | ** Close a file & cleanup AFP specific locking context |
| 3180 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3181 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3182 | int rc = SQLITE_OK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3183 | unixFile *pFile = (unixFile*)id; |
| 3184 | assert( id!=0 ); |
| 3185 | afpUnlock(id, NO_LOCK); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 3186 | assert( unixFileMutexNotheld(pFile) ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3187 | unixEnterMutex(); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 3188 | if( pFile->pInode ){ |
| 3189 | unixInodeInfo *pInode = pFile->pInode; |
| 3190 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | cb4e4b0 | 2018-09-06 19:36:29 +0000 | [diff] [blame] | 3191 | if( pInode->nLock ){ |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 3192 | /* If there are outstanding locks, do not actually close the file just |
| 3193 | ** yet because that would clear those locks. Instead, add the file |
| 3194 | ** descriptor to pInode->aPending. It will be automatically closed when |
| 3195 | ** the last lock is cleared. |
| 3196 | */ |
| 3197 | setPendingFd(pFile); |
| 3198 | } |
| 3199 | sqlite3_mutex_leave(pInode->pLockMutex); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3200 | } |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3201 | releaseInodeInfo(pFile); |
| 3202 | sqlite3_free(pFile->lockingContext); |
| 3203 | rc = closeUnixFile(id); |
| 3204 | unixLeaveMutex(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3205 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3208 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3209 | /* |
| 3210 | ** The code above is the AFP lock implementation. The code is specific |
| 3211 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3212 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 3213 | ** VFS is not available. |
| 3214 | ** |
| 3215 | ********************* End of the AFP lock implementation ********************** |
| 3216 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3217 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3218 | /****************************************************************************** |
| 3219 | *************************** Begin NFS Locking ********************************/ |
| 3220 | |
| 3221 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3222 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3223 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3224 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3225 | ** |
| 3226 | ** If the locking level of the file descriptor is already at or below |
| 3227 | ** the requested locking level, this routine is a no-op. |
| 3228 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3229 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 3230 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3234 | /* |
| 3235 | ** The code above is the NFS lock implementation. The code is specific |
| 3236 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3237 | ** is available. |
| 3238 | ** |
| 3239 | ********************* End of the NFS lock implementation ********************** |
| 3240 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3241 | |
| 3242 | /****************************************************************************** |
| 3243 | **************** Non-locking sqlite3_file methods ***************************** |
| 3244 | ** |
| 3245 | ** The next division contains implementations for all methods of the |
| 3246 | ** sqlite3_file object other than the locking methods. The locking |
| 3247 | ** methods were defined in divisions above (one locking method per |
| 3248 | ** division). Those methods that are common to all locking modes |
| 3249 | ** are gather together into this division. |
| 3250 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3251 | |
| 3252 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3253 | ** Seek to the offset passed as the second argument, then read cnt |
| 3254 | ** bytes into pBuf. Return the number of bytes actually read. |
| 3255 | ** |
| 3256 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 3257 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 3258 | ** one system to another. Since SQLite does not define USE_PREAD |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3259 | ** in any form by default, we will not attempt to define _XOPEN_SOURCE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3260 | ** See tickets #2741 and #2681. |
| 3261 | ** |
| 3262 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 3263 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3264 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3265 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 3266 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3267 | int prior = 0; |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3268 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
| 3269 | i64 newOffset; |
| 3270 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3271 | TIMER_START; |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3272 | assert( cnt==(cnt&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3273 | assert( id->h>2 ); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3274 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3275 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3276 | got = osPread(id->h, pBuf, cnt, offset); |
| 3277 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3278 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3279 | got = osPread64(id->h, pBuf, cnt, offset); |
| 3280 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3281 | #else |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3282 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3283 | SimulateIOError( newOffset = -1 ); |
| 3284 | if( newOffset<0 ){ |
| 3285 | storeLastErrno((unixFile*)id, errno); |
| 3286 | return -1; |
| 3287 | } |
| 3288 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3289 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3290 | if( got==cnt ) break; |
| 3291 | if( got<0 ){ |
| 3292 | if( errno==EINTR ){ got = 1; continue; } |
| 3293 | prior = 0; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3294 | storeLastErrno((unixFile*)id, errno); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3295 | break; |
| 3296 | }else if( got>0 ){ |
| 3297 | cnt -= got; |
| 3298 | offset += got; |
| 3299 | prior += got; |
| 3300 | pBuf = (void*)(got + (char*)pBuf); |
| 3301 | } |
| 3302 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3303 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3304 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3305 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3306 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3310 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3311 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3312 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3313 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3314 | static int unixRead( |
| 3315 | sqlite3_file *id, |
| 3316 | void *pBuf, |
| 3317 | int amt, |
| 3318 | sqlite3_int64 offset |
| 3319 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3320 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3321 | int got; |
| 3322 | assert( id ); |
drh | 6cf9d8d | 2013-05-09 18:12:40 +0000 | [diff] [blame] | 3323 | assert( offset>=0 ); |
| 3324 | assert( amt>0 ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3325 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3326 | /* If this is a database file (not a journal, master-journal or temp |
| 3327 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3328 | #if 0 |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 3329 | assert( pFile->pPreallocatedUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3330 | || offset>=PENDING_BYTE+512 |
| 3331 | || offset+amt<=PENDING_BYTE |
| 3332 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3333 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3334 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3335 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 6c56963 | 2013-03-26 18:48:11 +0000 | [diff] [blame] | 3336 | /* Deal with as much of this read request as possible by transfering |
| 3337 | ** data from the memory mapping using memcpy(). */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3338 | if( offset<pFile->mmapSize ){ |
| 3339 | if( offset+amt <= pFile->mmapSize ){ |
| 3340 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 3341 | return SQLITE_OK; |
| 3342 | }else{ |
| 3343 | int nCopy = pFile->mmapSize - offset; |
| 3344 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 3345 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3346 | amt -= nCopy; |
| 3347 | offset += nCopy; |
| 3348 | } |
| 3349 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3350 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3351 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3352 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3353 | if( got==amt ){ |
| 3354 | return SQLITE_OK; |
| 3355 | }else if( got<0 ){ |
| 3356 | /* lastErrno set by seekAndRead */ |
| 3357 | return SQLITE_IOERR_READ; |
| 3358 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3359 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3360 | /* Unread parts of the buffer must be zero-filled */ |
| 3361 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3362 | return SQLITE_IOERR_SHORT_READ; |
| 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | /* |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3367 | ** Attempt to seek the file-descriptor passed as the first argument to |
| 3368 | ** absolute offset iOff, then attempt to write nBuf bytes of data from |
| 3369 | ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, |
| 3370 | ** return the actual number of bytes written (which may be less than |
| 3371 | ** nBuf). |
| 3372 | */ |
| 3373 | static int seekAndWriteFd( |
| 3374 | int fd, /* File descriptor to write to */ |
| 3375 | i64 iOff, /* File offset to begin writing at */ |
| 3376 | const void *pBuf, /* Copy data from this buffer to the file */ |
| 3377 | int nBuf, /* Size of buffer pBuf in bytes */ |
| 3378 | int *piErrno /* OUT: Error number if error occurs */ |
| 3379 | ){ |
| 3380 | int rc = 0; /* Value returned by system call */ |
| 3381 | |
| 3382 | assert( nBuf==(nBuf&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3383 | assert( fd>2 ); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3384 | assert( piErrno!=0 ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3385 | nBuf &= 0x1ffff; |
| 3386 | TIMER_START; |
| 3387 | |
| 3388 | #if defined(USE_PREAD) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3389 | do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3390 | #elif defined(USE_PREAD64) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3391 | do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3392 | #else |
| 3393 | do{ |
| 3394 | i64 iSeek = lseek(fd, iOff, SEEK_SET); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3395 | SimulateIOError( iSeek = -1 ); |
| 3396 | if( iSeek<0 ){ |
| 3397 | rc = -1; |
| 3398 | break; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3399 | } |
| 3400 | rc = osWrite(fd, pBuf, nBuf); |
| 3401 | }while( rc<0 && errno==EINTR ); |
| 3402 | #endif |
| 3403 | |
| 3404 | TIMER_END; |
| 3405 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); |
| 3406 | |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3407 | if( rc<0 ) *piErrno = errno; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3408 | return rc; |
| 3409 | } |
| 3410 | |
| 3411 | |
| 3412 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3413 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3414 | ** Return the number of bytes actually read. Update the offset. |
| 3415 | ** |
| 3416 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3417 | ** is set before returning. |
| 3418 | */ |
| 3419 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3420 | return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | |
| 3424 | /* |
| 3425 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3426 | ** or some other error code on failure. |
| 3427 | */ |
| 3428 | static int unixWrite( |
| 3429 | sqlite3_file *id, |
| 3430 | const void *pBuf, |
| 3431 | int amt, |
| 3432 | sqlite3_int64 offset |
| 3433 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3434 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3435 | int wrote = 0; |
| 3436 | assert( id ); |
| 3437 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3438 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3439 | /* If this is a database file (not a journal, master-journal or temp |
| 3440 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3441 | #if 0 |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 3442 | assert( pFile->pPreallocatedUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3443 | || offset>=PENDING_BYTE+512 |
| 3444 | || offset+amt<=PENDING_BYTE |
| 3445 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3446 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3447 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3448 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3449 | /* If we are doing a normal write to a database file (as opposed to |
| 3450 | ** doing a hot-journal rollback or a write to some file other than a |
| 3451 | ** normal database file) then record the fact that the database |
| 3452 | ** has changed. If the transaction counter is modified, record that |
| 3453 | ** fact too. |
| 3454 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3455 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3456 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3457 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3458 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3459 | char oldCntr[4]; |
| 3460 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3461 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3462 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3463 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3464 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3465 | } |
| 3466 | } |
| 3467 | } |
| 3468 | #endif |
| 3469 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 3470 | #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3471 | /* Deal with as much of this write request as possible by transfering |
| 3472 | ** data from the memory mapping using memcpy(). */ |
| 3473 | if( offset<pFile->mmapSize ){ |
| 3474 | if( offset+amt <= pFile->mmapSize ){ |
| 3475 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 3476 | return SQLITE_OK; |
| 3477 | }else{ |
| 3478 | int nCopy = pFile->mmapSize - offset; |
| 3479 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3480 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3481 | amt -= nCopy; |
| 3482 | offset += nCopy; |
| 3483 | } |
| 3484 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3485 | #endif |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3486 | |
| 3487 | while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3488 | amt -= wrote; |
| 3489 | offset += wrote; |
| 3490 | pBuf = &((char*)pBuf)[wrote]; |
| 3491 | } |
| 3492 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3493 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3494 | |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3495 | if( amt>wrote ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3496 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3497 | /* lastErrno set by seekAndWrite */ |
| 3498 | return SQLITE_IOERR_WRITE; |
| 3499 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3500 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3501 | return SQLITE_FULL; |
| 3502 | } |
| 3503 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3504 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3505 | return SQLITE_OK; |
| 3506 | } |
| 3507 | |
| 3508 | #ifdef SQLITE_TEST |
| 3509 | /* |
| 3510 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3511 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3512 | */ |
| 3513 | int sqlite3_sync_count = 0; |
| 3514 | int sqlite3_fullsync_count = 0; |
| 3515 | #endif |
| 3516 | |
| 3517 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3518 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3519 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3520 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3521 | ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3522 | */ |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3523 | #if !defined(fdatasync) && !HAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3524 | # define fdatasync fsync |
| 3525 | #endif |
| 3526 | |
| 3527 | /* |
| 3528 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3529 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3530 | ** only available on Mac OS X. But that could change. |
| 3531 | */ |
| 3532 | #ifdef F_FULLFSYNC |
| 3533 | # define HAVE_FULLFSYNC 1 |
| 3534 | #else |
| 3535 | # define HAVE_FULLFSYNC 0 |
| 3536 | #endif |
| 3537 | |
| 3538 | |
| 3539 | /* |
| 3540 | ** The fsync() system call does not work as advertised on many |
| 3541 | ** unix systems. The following procedure is an attempt to make |
| 3542 | ** it work better. |
| 3543 | ** |
| 3544 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3545 | ** for testing when we want to run through the test suite quickly. |
| 3546 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3547 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3548 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3549 | ** |
| 3550 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3551 | ** The idea behind dataOnly is that it should only write the file content |
| 3552 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3553 | ** unchanged since the file size is part of the inode. However, |
| 3554 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3555 | ** file size has changed. The only real difference between fdatasync() |
| 3556 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3557 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3558 | ** We only care about the file size, not the other file attributes, so |
| 3559 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3560 | ** So, we always use fdatasync() if it is available, regardless of |
| 3561 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3562 | */ |
| 3563 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3564 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3565 | |
| 3566 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3567 | ** the one below. It is replicated here solely to avoid cluttering |
| 3568 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3569 | */ |
| 3570 | #ifdef SQLITE_NO_SYNC |
| 3571 | UNUSED_PARAMETER(fd); |
| 3572 | UNUSED_PARAMETER(fullSync); |
| 3573 | UNUSED_PARAMETER(dataOnly); |
| 3574 | #elif HAVE_FULLFSYNC |
| 3575 | UNUSED_PARAMETER(dataOnly); |
| 3576 | #else |
| 3577 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3578 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3579 | #endif |
| 3580 | |
| 3581 | /* Record the number of times that we do a normal fsync() and |
| 3582 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3583 | ** gets called with the correct arguments. |
| 3584 | */ |
| 3585 | #ifdef SQLITE_TEST |
| 3586 | if( fullSync ) sqlite3_fullsync_count++; |
| 3587 | sqlite3_sync_count++; |
| 3588 | #endif |
| 3589 | |
| 3590 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3591 | ** no-op. But go ahead and call fstat() to validate the file |
| 3592 | ** descriptor as we need a method to provoke a failure during |
| 3593 | ** coverate testing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3594 | */ |
| 3595 | #ifdef SQLITE_NO_SYNC |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3596 | { |
| 3597 | struct stat buf; |
| 3598 | rc = osFstat(fd, &buf); |
| 3599 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3600 | #elif HAVE_FULLFSYNC |
| 3601 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3602 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3603 | }else{ |
| 3604 | rc = 1; |
| 3605 | } |
| 3606 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3607 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3608 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3609 | ** isn't supported for this file system. So, attempt an fsync |
| 3610 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3611 | ** It'd be better to detect fullfsync support once and avoid |
| 3612 | ** the fcntl call every time sync is called. |
| 3613 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3614 | if( rc ) rc = fsync(fd); |
| 3615 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3616 | #elif defined(__APPLE__) |
| 3617 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3618 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3619 | */ |
| 3620 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3621 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3622 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3623 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3624 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3625 | rc = fsync(fd); |
| 3626 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3627 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3628 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3629 | |
| 3630 | if( OS_VXWORKS && rc!= -1 ){ |
| 3631 | rc = 0; |
| 3632 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3633 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3636 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3637 | ** Open a file descriptor to the directory containing file zFilename. |
| 3638 | ** If successful, *pFd is set to the opened file descriptor and |
| 3639 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3640 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3641 | ** value. |
| 3642 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3643 | ** The directory file descriptor is used for only one thing - to |
| 3644 | ** fsync() a directory to make sure file creation and deletion events |
| 3645 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3646 | ** journaling filesystems, but are required on older filesystems. |
| 3647 | ** |
| 3648 | ** This routine can be overridden using the xSetSysCall interface. |
| 3649 | ** The ability to override this routine was added in support of the |
| 3650 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3651 | ** told) so making it overrideable allows the chromium sandbox to |
| 3652 | ** replace this routine with a harmless no-op. To make this routine |
| 3653 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3654 | ** *pFd set to a negative number. |
| 3655 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3656 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3657 | ** the file descriptor *pFd using close(). |
| 3658 | */ |
| 3659 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3660 | int ii; |
| 3661 | int fd = -1; |
| 3662 | char zDirname[MAX_PATHNAME+1]; |
| 3663 | |
| 3664 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3665 | for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--); |
| 3666 | if( ii>0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3667 | zDirname[ii] = '\0'; |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3668 | }else{ |
| 3669 | if( zDirname[0]!='/' ) zDirname[0] = '.'; |
| 3670 | zDirname[1] = 0; |
| 3671 | } |
| 3672 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3673 | if( fd>=0 ){ |
| 3674 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3675 | } |
| 3676 | *pFd = fd; |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3677 | if( fd>=0 ) return SQLITE_OK; |
| 3678 | return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3682 | ** Make sure all writes to a particular file are committed to disk. |
| 3683 | ** |
| 3684 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3685 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3686 | ** file data is synced. |
| 3687 | ** |
| 3688 | ** Under Unix, also make sure that the directory entry for the file |
| 3689 | ** has been created by fsync-ing the directory that contains the file. |
| 3690 | ** If we do not do this and we encounter a power failure, the directory |
| 3691 | ** entry for the journal might not exist after we reboot. The next |
| 3692 | ** SQLite to access the file will not know that the journal exists (because |
| 3693 | ** the directory entry for the journal was never created) and the transaction |
| 3694 | ** will not roll back - possibly leading to database corruption. |
| 3695 | */ |
| 3696 | static int unixSync(sqlite3_file *id, int flags){ |
| 3697 | int rc; |
| 3698 | unixFile *pFile = (unixFile*)id; |
| 3699 | |
| 3700 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3701 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3702 | |
| 3703 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3704 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3705 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3706 | ); |
| 3707 | |
| 3708 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3709 | ** line is to test that doing so does not cause any problems. |
| 3710 | */ |
| 3711 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3712 | |
| 3713 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3714 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3715 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3716 | SimulateIOError( rc=1 ); |
| 3717 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3718 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3719 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3720 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3721 | |
| 3722 | /* Also fsync the directory containing the file if the DIRSYNC flag |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 3723 | ** is set. This is a one-time occurrence. Many systems (examples: AIX) |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3724 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3725 | */ |
| 3726 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3727 | int dirfd; |
| 3728 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3729 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3730 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3731 | if( rc==SQLITE_OK ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3732 | full_fsync(dirfd, 0, 0); |
| 3733 | robust_close(pFile, dirfd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3734 | }else{ |
| 3735 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3736 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3737 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3738 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3739 | } |
| 3740 | return rc; |
| 3741 | } |
| 3742 | |
| 3743 | /* |
| 3744 | ** Truncate an open file to a specified size |
| 3745 | */ |
| 3746 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3747 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3748 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3749 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3750 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3751 | |
| 3752 | /* If the user has configured a chunk-size for this file, truncate the |
| 3753 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3754 | ** actual file size after the operation may be larger than the requested |
| 3755 | ** size). |
| 3756 | */ |
drh | b8af4b7 | 2012-04-05 20:04:39 +0000 | [diff] [blame] | 3757 | if( pFile->szChunk>0 ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3758 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3759 | } |
| 3760 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 3761 | rc = robust_ftruncate(pFile->h, nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3762 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3763 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3764 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3765 | }else{ |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3766 | #ifdef SQLITE_DEBUG |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3767 | /* If we are doing a normal write to a database file (as opposed to |
| 3768 | ** doing a hot-journal rollback or a write to some file other than a |
| 3769 | ** normal database file) and we truncate the file to zero length, |
| 3770 | ** that effectively updates the change counter. This might happen |
| 3771 | ** when restoring a database using the backup API from a zero-length |
| 3772 | ** source. |
| 3773 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3774 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3775 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3776 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3777 | #endif |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3778 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3779 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3780 | /* If the file was just truncated to a size smaller than the currently |
| 3781 | ** mapped region, reduce the effective mapping size as well. SQLite will |
| 3782 | ** use read() and write() to access data beyond this point from now on. |
| 3783 | */ |
| 3784 | if( nByte<pFile->mmapSize ){ |
| 3785 | pFile->mmapSize = nByte; |
| 3786 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3787 | #endif |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3788 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3789 | return SQLITE_OK; |
| 3790 | } |
| 3791 | } |
| 3792 | |
| 3793 | /* |
| 3794 | ** Determine the current size of a file in bytes |
| 3795 | */ |
| 3796 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3797 | int rc; |
| 3798 | struct stat buf; |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 3799 | assert( id ); |
| 3800 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3801 | SimulateIOError( rc=1 ); |
| 3802 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3803 | storeLastErrno((unixFile*)id, errno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3804 | return SQLITE_IOERR_FSTAT; |
| 3805 | } |
| 3806 | *pSize = buf.st_size; |
| 3807 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3808 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3809 | ** writes a single byte into that file in order to work around a bug |
| 3810 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3811 | ** layers, we need to report this file size as zero even though it is |
| 3812 | ** really 1. Ticket #3260. |
| 3813 | */ |
| 3814 | if( *pSize==1 ) *pSize = 0; |
| 3815 | |
| 3816 | |
| 3817 | return SQLITE_OK; |
| 3818 | } |
| 3819 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3820 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3821 | /* |
| 3822 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3823 | ** proxying locking division. |
| 3824 | */ |
| 3825 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3826 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3827 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3828 | /* |
| 3829 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3830 | ** file-control operation. Enlarge the database to nBytes in size |
| 3831 | ** (rounded up to the next chunk-size). If the database is already |
| 3832 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3833 | */ |
| 3834 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3835 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3836 | i64 nSize; /* Required file size */ |
| 3837 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3838 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3839 | if( osFstat(pFile->h, &buf) ){ |
| 3840 | return SQLITE_IOERR_FSTAT; |
| 3841 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3842 | |
| 3843 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3844 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3845 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3846 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3847 | /* The code below is handling the return value of osFallocate() |
| 3848 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3849 | ** or an error number on failure". See the manpage for details. */ |
| 3850 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3851 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3852 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3853 | }while( err==EINTR ); |
drh | 789df14 | 2018-06-02 14:37:39 +0000 | [diff] [blame] | 3854 | if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3855 | #else |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3856 | /* If the OS does not have posix_fallocate(), fake it. Write a |
| 3857 | ** single byte to the last byte in each block that falls entirely |
| 3858 | ** within the extended region. Then, if required, a single byte |
| 3859 | ** at offset (nSize-1), to set the size of the file correctly. |
| 3860 | ** This is a similar technique to that used by glibc on systems |
| 3861 | ** that do not have a real fallocate() call. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3862 | */ |
| 3863 | int nBlk = buf.st_blksize; /* File-system block size */ |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3864 | int nWrite = 0; /* Number of bytes written by seekAndWrite */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3865 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3866 | |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3867 | iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3868 | assert( iWrite>=buf.st_size ); |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3869 | assert( ((iWrite+1)%nBlk)==0 ); |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3870 | for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){ |
| 3871 | if( iWrite>=nSize ) iWrite = nSize - 1; |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3872 | nWrite = seekAndWrite(pFile, iWrite, "", 1); |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3873 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3874 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3875 | #endif |
| 3876 | } |
| 3877 | } |
| 3878 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3879 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3880 | if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3881 | int rc; |
| 3882 | if( pFile->szChunk<=0 ){ |
| 3883 | if( robust_ftruncate(pFile->h, nByte) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3884 | storeLastErrno(pFile, errno); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3885 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | rc = unixMapfile(pFile, nByte); |
| 3890 | return rc; |
| 3891 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3892 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3893 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3894 | return SQLITE_OK; |
| 3895 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3896 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3897 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3898 | ** If *pArg is initially negative then this is a query. Set *pArg to |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3899 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3900 | ** |
| 3901 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3902 | */ |
| 3903 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3904 | if( *pArg<0 ){ |
| 3905 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3906 | }else if( (*pArg)==0 ){ |
| 3907 | pFile->ctrlFlags &= ~mask; |
| 3908 | }else{ |
| 3909 | pFile->ctrlFlags |= mask; |
| 3910 | } |
| 3911 | } |
| 3912 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3913 | /* Forward declaration */ |
| 3914 | static int unixGetTempname(int nBuf, char *zBuf); |
| 3915 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3916 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3917 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3918 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3919 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3920 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3921 | switch( op ){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3922 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3923 | case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: { |
| 3924 | int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3925 | return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3926 | } |
| 3927 | case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: { |
| 3928 | int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3929 | return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3930 | } |
| 3931 | case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { |
| 3932 | int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3933 | return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3934 | } |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3935 | #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3936 | |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3937 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3938 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3939 | return SQLITE_OK; |
| 3940 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3941 | case SQLITE_FCNTL_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3942 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3943 | return SQLITE_OK; |
| 3944 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3945 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3946 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3947 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3948 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3949 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3950 | int rc; |
| 3951 | SimulateIOErrorBenign(1); |
| 3952 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3953 | SimulateIOErrorBenign(0); |
| 3954 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3955 | } |
| 3956 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3957 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3958 | return SQLITE_OK; |
| 3959 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3960 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3961 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3962 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3963 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3964 | case SQLITE_FCNTL_VFSNAME: { |
| 3965 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3966 | return SQLITE_OK; |
| 3967 | } |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3968 | case SQLITE_FCNTL_TEMPFILENAME: { |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3969 | char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3970 | if( zTFile ){ |
| 3971 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 3972 | *(char**)pArg = zTFile; |
| 3973 | } |
| 3974 | return SQLITE_OK; |
| 3975 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 3976 | case SQLITE_FCNTL_HAS_MOVED: { |
| 3977 | *(int*)pArg = fileHasMoved(pFile); |
| 3978 | return SQLITE_OK; |
| 3979 | } |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 3980 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 3981 | case SQLITE_FCNTL_LOCK_TIMEOUT: { |
| 3982 | pFile->iBusyTimeout = *(int*)pArg; |
| 3983 | return SQLITE_OK; |
| 3984 | } |
| 3985 | #endif |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3986 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3987 | case SQLITE_FCNTL_MMAP_SIZE: { |
drh | 34f7490 | 2013-04-03 13:09:18 +0000 | [diff] [blame] | 3988 | i64 newLimit = *(i64*)pArg; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3989 | int rc = SQLITE_OK; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3990 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 3991 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 3992 | } |
dan | 43c1e62 | 2017-08-07 18:13:28 +0000 | [diff] [blame] | 3993 | |
| 3994 | /* The value of newLimit may be eventually cast to (size_t) and passed |
mistachkin | e35395a | 2017-08-07 19:06:54 +0000 | [diff] [blame] | 3995 | ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a |
| 3996 | ** 64-bit type. */ |
dan | 089df50 | 2017-08-07 18:54:10 +0000 | [diff] [blame] | 3997 | if( newLimit>0 && sizeof(size_t)<8 ){ |
dan | 43c1e62 | 2017-08-07 18:13:28 +0000 | [diff] [blame] | 3998 | newLimit = (newLimit & 0x7FFFFFFF); |
| 3999 | } |
| 4000 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4001 | *(i64*)pArg = pFile->mmapSizeMax; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4002 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4003 | pFile->mmapSizeMax = newLimit; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4004 | if( pFile->mmapSize>0 ){ |
| 4005 | unixUnmapfile(pFile); |
| 4006 | rc = unixMapfile(pFile, -1); |
| 4007 | } |
dan | bcb8a86 | 2013-04-08 15:30:41 +0000 | [diff] [blame] | 4008 | } |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4009 | return rc; |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 4010 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4011 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 4012 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 4013 | /* The pager calls this method to signal that it has done |
| 4014 | ** a rollback and that the database is therefore unchanged and |
| 4015 | ** it hence it is OK for the transaction change counter to be |
| 4016 | ** unchanged. |
| 4017 | */ |
| 4018 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 4019 | ((unixFile*)id)->dbUpdate = 0; |
| 4020 | return SQLITE_OK; |
| 4021 | } |
| 4022 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4023 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4024 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: |
| 4025 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4026 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4027 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4028 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 4029 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 4030 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 4031 | } |
| 4032 | |
| 4033 | /* |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4034 | ** If pFd->sectorSize is non-zero when this function is called, it is a |
| 4035 | ** no-op. Otherwise, the values of pFd->sectorSize and |
| 4036 | ** pFd->deviceCharacteristics are set according to the file-system |
| 4037 | ** characteristics. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4038 | ** |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4039 | ** There are two versions of this function. One for QNX and one for all |
| 4040 | ** other systems. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4041 | */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4042 | #ifndef __QNXNTO__ |
| 4043 | static void setDeviceCharacteristics(unixFile *pFd){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 4044 | assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 ); |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4045 | if( pFd->sectorSize==0 ){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 4046 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4047 | int res; |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 4048 | u32 f = 0; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4049 | |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4050 | /* Check for support for F2FS atomic batch writes. */ |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 4051 | res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f); |
| 4052 | if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){ |
dan | 77b4f52 | 2017-07-27 18:34:00 +0000 | [diff] [blame] | 4053 | pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4054 | } |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 4055 | #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4056 | |
| 4057 | /* Set the POWERSAFE_OVERWRITE flag if requested. */ |
| 4058 | if( pFd->ctrlFlags & UNIXFILE_PSOW ){ |
| 4059 | pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 4060 | } |
| 4061 | |
| 4062 | pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4063 | } |
| 4064 | } |
| 4065 | #else |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4066 | #include <sys/dcmd_blk.h> |
| 4067 | #include <sys/statvfs.h> |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4068 | static void setDeviceCharacteristics(unixFile *pFile){ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4069 | if( pFile->sectorSize == 0 ){ |
| 4070 | struct statvfs fsInfo; |
| 4071 | |
| 4072 | /* Set defaults for non-supported filesystems */ |
| 4073 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4074 | pFile->deviceCharacteristics = 0; |
| 4075 | if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
drh | a9be508 | 2018-01-15 14:32:37 +0000 | [diff] [blame] | 4076 | return; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 4080 | pFile->sectorSize = fsInfo.f_bsize; |
| 4081 | pFile->deviceCharacteristics = |
| 4082 | SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 4083 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4084 | ** the write succeeds */ |
| 4085 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4086 | ** so it is ordered */ |
| 4087 | 0; |
| 4088 | }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 4089 | pFile->sectorSize = fsInfo.f_bsize; |
| 4090 | pFile->deviceCharacteristics = |
| 4091 | /* etfs cluster size writes are atomic */ |
| 4092 | (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 4093 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4094 | ** the write succeeds */ |
| 4095 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4096 | ** so it is ordered */ |
| 4097 | 0; |
| 4098 | }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 4099 | pFile->sectorSize = fsInfo.f_bsize; |
| 4100 | pFile->deviceCharacteristics = |
| 4101 | SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 4102 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4103 | ** the write succeeds */ |
| 4104 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4105 | ** so it is ordered */ |
| 4106 | 0; |
| 4107 | }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 4108 | pFile->sectorSize = fsInfo.f_bsize; |
| 4109 | pFile->deviceCharacteristics = |
| 4110 | /* full bitset of atomics from max sector size and smaller */ |
| 4111 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 4112 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4113 | ** so it is ordered */ |
| 4114 | 0; |
| 4115 | }else if( strstr(fsInfo.f_basetype, "dos") ){ |
| 4116 | pFile->sectorSize = fsInfo.f_bsize; |
| 4117 | pFile->deviceCharacteristics = |
| 4118 | /* full bitset of atomics from max sector size and smaller */ |
| 4119 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 4120 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4121 | ** so it is ordered */ |
| 4122 | 0; |
| 4123 | }else{ |
| 4124 | pFile->deviceCharacteristics = |
| 4125 | SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 4126 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4127 | ** the write succeeds */ |
| 4128 | 0; |
| 4129 | } |
| 4130 | } |
| 4131 | /* Last chance verification. If the sector size isn't a multiple of 512 |
| 4132 | ** then it isn't valid.*/ |
| 4133 | if( pFile->sectorSize % 512 != 0 ){ |
| 4134 | pFile->deviceCharacteristics = 0; |
| 4135 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4136 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4137 | } |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4138 | #endif |
| 4139 | |
| 4140 | /* |
| 4141 | ** Return the sector size in bytes of the underlying block device for |
| 4142 | ** the specified file. This is almost always 512 bytes, but may be |
| 4143 | ** larger for some devices. |
| 4144 | ** |
| 4145 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 4146 | ** if two files are created in the same file-system directory (i.e. |
| 4147 | ** a database and its journal file) that the sector size will be the |
| 4148 | ** same for both. |
| 4149 | */ |
| 4150 | static int unixSectorSize(sqlite3_file *id){ |
| 4151 | unixFile *pFd = (unixFile*)id; |
| 4152 | setDeviceCharacteristics(pFd); |
| 4153 | return pFd->sectorSize; |
| 4154 | } |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4155 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4156 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4157 | ** Return the device characteristics for the file. |
| 4158 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4159 | ** 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] | 4160 | ** However, that choice is controversial since technically the underlying |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4161 | ** file system does not always provide powersafe overwrites. (In other |
| 4162 | ** words, after a power-loss event, parts of the file that were never |
| 4163 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 4164 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 4165 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 4166 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 4167 | ** 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] | 4168 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4169 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4170 | unixFile *pFd = (unixFile*)id; |
| 4171 | setDeviceCharacteristics(pFd); |
| 4172 | return pFd->deviceCharacteristics; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4175 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4176 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4177 | /* |
| 4178 | ** Return the system page size. |
| 4179 | ** |
| 4180 | ** This function should not be called directly by other code in this file. |
| 4181 | ** Instead, it should be called via macro osGetpagesize(). |
| 4182 | */ |
| 4183 | static int unixGetpagesize(void){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4184 | #if OS_VXWORKS |
| 4185 | return 1024; |
| 4186 | #elif defined(_BSD_SOURCE) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4187 | return getpagesize(); |
| 4188 | #else |
| 4189 | return (int)sysconf(_SC_PAGESIZE); |
| 4190 | #endif |
| 4191 | } |
| 4192 | |
| 4193 | #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ |
| 4194 | |
| 4195 | #ifndef SQLITE_OMIT_WAL |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4196 | |
| 4197 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4198 | ** Object used to represent an shared memory buffer. |
| 4199 | ** |
| 4200 | ** When multiple threads all reference the same wal-index, each thread |
| 4201 | ** has its own unixShm object, but they all point to a single instance |
| 4202 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 4203 | ** only once per process. |
| 4204 | ** |
| 4205 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 4206 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 4207 | ** every open file that does not use shared memory (in other words, most |
| 4208 | ** open files) would have to carry around this extra information. So |
| 4209 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 4210 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4211 | ** |
| 4212 | ** unixMutexHeld() must be true when creating or destroying |
| 4213 | ** this object or while reading or writing the following fields: |
| 4214 | ** |
| 4215 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4216 | ** |
| 4217 | ** The following fields are read-only after the object is created: |
| 4218 | ** |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4219 | ** hShm |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4220 | ** zFilename |
| 4221 | ** |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4222 | ** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4223 | ** unixMutexHeld() is true when reading or writing any other field |
| 4224 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4225 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4226 | struct unixShmNode { |
| 4227 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4228 | sqlite3_mutex *pShmMutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4229 | char *zFilename; /* Name of the mmapped file */ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4230 | int hShm; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4231 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4232 | u16 nRegion; /* Size of array apRegion */ |
| 4233 | u8 isReadonly; /* True if read-only */ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4234 | u8 isUnlocked; /* True if no DMS lock held */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4235 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4236 | int nRef; /* Number of unixShm objects pointing to this */ |
| 4237 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4238 | #ifdef SQLITE_DEBUG |
| 4239 | u8 exclMask; /* Mask of exclusive locks held */ |
| 4240 | u8 sharedMask; /* Mask of shared locks held */ |
| 4241 | u8 nextShmId; /* Next available unixShm.id value */ |
| 4242 | #endif |
| 4243 | }; |
| 4244 | |
| 4245 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4246 | ** Structure used internally by this VFS to record the state of an |
| 4247 | ** open shared memory connection. |
| 4248 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4249 | ** The following fields are initialized when this object is created and |
| 4250 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4251 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4252 | ** unixShm.pShmNode |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4253 | ** unixShm.id |
| 4254 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4255 | ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must |
| 4256 | ** be held while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4257 | */ |
| 4258 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4259 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 4260 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4261 | u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 4262 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4263 | u16 sharedMask; /* Mask of shared locks held */ |
| 4264 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4265 | }; |
| 4266 | |
| 4267 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4268 | ** Constants used for locking |
| 4269 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 4270 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 4271 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4272 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4273 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4274 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4275 | ** |
| 4276 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 4277 | ** otherwise. |
| 4278 | */ |
| 4279 | static int unixShmSystemLock( |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4280 | unixFile *pFile, /* Open connection to the WAL file */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4281 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4282 | int ofst, /* First byte of the locking range */ |
| 4283 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4284 | ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4285 | unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 4286 | struct flock f; /* The posix advisory locking structure */ |
| 4287 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4288 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4289 | /* Access to the unixShmNode object is serialized by the caller */ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4290 | pShmNode = pFile->pInode->pShmNode; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4291 | assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) ); |
drh | 9b7e8e1 | 2018-10-02 20:16:41 +0000 | [diff] [blame] | 4292 | assert( pShmNode->nRef>0 || unixMutexHeld() ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4293 | |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4294 | /* Shared locks never span more than one byte */ |
| 4295 | assert( n==1 || lockType!=F_RDLCK ); |
| 4296 | |
| 4297 | /* Locks are within range */ |
| 4298 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 4299 | |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4300 | if( pShmNode->hShm>=0 ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4301 | /* Initialize the locking parameters */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4302 | f.l_type = lockType; |
| 4303 | f.l_whence = SEEK_SET; |
| 4304 | f.l_start = ofst; |
| 4305 | f.l_len = n; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4306 | rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4307 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 4308 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4309 | |
| 4310 | /* Update the global lock state and do debug tracing */ |
| 4311 | #ifdef SQLITE_DEBUG |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4312 | { u16 mask; |
| 4313 | OSTRACE(("SHM-LOCK ")); |
| 4314 | mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst); |
| 4315 | if( rc==SQLITE_OK ){ |
| 4316 | if( lockType==F_UNLCK ){ |
| 4317 | OSTRACE(("unlock %d ok", ofst)); |
| 4318 | pShmNode->exclMask &= ~mask; |
| 4319 | pShmNode->sharedMask &= ~mask; |
| 4320 | }else if( lockType==F_RDLCK ){ |
| 4321 | OSTRACE(("read-lock %d ok", ofst)); |
| 4322 | pShmNode->exclMask &= ~mask; |
| 4323 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4324 | }else{ |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4325 | assert( lockType==F_WRLCK ); |
| 4326 | OSTRACE(("write-lock %d ok", ofst)); |
| 4327 | pShmNode->exclMask |= mask; |
| 4328 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4329 | } |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4330 | }else{ |
| 4331 | if( lockType==F_UNLCK ){ |
| 4332 | OSTRACE(("unlock %d failed", ofst)); |
| 4333 | }else if( lockType==F_RDLCK ){ |
| 4334 | OSTRACE(("read-lock failed")); |
| 4335 | }else{ |
| 4336 | assert( lockType==F_WRLCK ); |
| 4337 | OSTRACE(("write-lock %d failed", ofst)); |
| 4338 | } |
| 4339 | } |
| 4340 | OSTRACE((" - afterwards %03x,%03x\n", |
| 4341 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4342 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4343 | #endif |
| 4344 | |
| 4345 | return rc; |
| 4346 | } |
| 4347 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4348 | /* |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4349 | ** Return the minimum number of 32KB shm regions that should be mapped at |
| 4350 | ** a time, assuming that each mapping must be an integer multiple of the |
| 4351 | ** current system page-size. |
| 4352 | ** |
| 4353 | ** Usually, this is 1. The exception seems to be systems that are configured |
| 4354 | ** to use 64KB pages - in this case each mapping must cover at least two |
| 4355 | ** shm regions. |
| 4356 | */ |
| 4357 | static int unixShmRegionPerMap(void){ |
| 4358 | int shmsz = 32*1024; /* SHM region size */ |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4359 | int pgsz = osGetpagesize(); /* System page size */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4360 | assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ |
| 4361 | if( pgsz<shmsz ) return 1; |
| 4362 | return pgsz/shmsz; |
| 4363 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4364 | |
| 4365 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4366 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4367 | ** |
| 4368 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4369 | ** by VFS shared-memory methods. |
| 4370 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4371 | static void unixShmPurge(unixFile *pFd){ |
| 4372 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4373 | assert( unixMutexHeld() ); |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4374 | if( p && ALWAYS(p->nRef==0) ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4375 | int nShmPerMap = unixShmRegionPerMap(); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4376 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4377 | assert( p->pInode==pFd->pInode ); |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4378 | sqlite3_mutex_free(p->pShmMutex); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4379 | for(i=0; i<p->nRegion; i+=nShmPerMap){ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4380 | if( p->hShm>=0 ){ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 4381 | osMunmap(p->apRegion[i], p->szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4382 | }else{ |
| 4383 | sqlite3_free(p->apRegion[i]); |
| 4384 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4385 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4386 | sqlite3_free(p->apRegion); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4387 | if( p->hShm>=0 ){ |
| 4388 | robust_close(pFd, p->hShm, __LINE__); |
| 4389 | p->hShm = -1; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4390 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4391 | p->pInode->pShmNode = 0; |
| 4392 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4393 | } |
| 4394 | } |
| 4395 | |
| 4396 | /* |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4397 | ** The DMS lock has not yet been taken on shm file pShmNode. Attempt to |
| 4398 | ** take it now. Return SQLITE_OK if successful, or an SQLite error |
| 4399 | ** code otherwise. |
| 4400 | ** |
| 4401 | ** If the DMS cannot be locked because this is a readonly_shm=1 |
| 4402 | ** connection and no other process already holds a lock, return |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4403 | ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1. |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4404 | */ |
| 4405 | static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){ |
| 4406 | struct flock lock; |
| 4407 | int rc = SQLITE_OK; |
| 4408 | |
| 4409 | /* Use F_GETLK to determine the locks other processes are holding |
| 4410 | ** on the DMS byte. If it indicates that another process is holding |
| 4411 | ** a SHARED lock, then this process may also take a SHARED lock |
| 4412 | ** and proceed with opening the *-shm file. |
| 4413 | ** |
| 4414 | ** Or, if no other process is holding any lock, then this process |
| 4415 | ** is the first to open it. In this case take an EXCLUSIVE lock on the |
| 4416 | ** DMS byte and truncate the *-shm file to zero bytes in size. Then |
| 4417 | ** downgrade to a SHARED lock on the DMS byte. |
| 4418 | ** |
| 4419 | ** If another process is holding an EXCLUSIVE lock on the DMS byte, |
| 4420 | ** return SQLITE_BUSY to the caller (it will try again). An earlier |
| 4421 | ** version of this code attempted the SHARED lock at this point. But |
| 4422 | ** this introduced a subtle race condition: if the process holding |
| 4423 | ** EXCLUSIVE failed just before truncating the *-shm file, then this |
| 4424 | ** process might open and use the *-shm file without truncating it. |
| 4425 | ** And if the *-shm file has been corrupted by a power failure or |
| 4426 | ** system crash, the database itself may also become corrupt. */ |
| 4427 | lock.l_whence = SEEK_SET; |
| 4428 | lock.l_start = UNIX_SHM_DMS; |
| 4429 | lock.l_len = 1; |
| 4430 | lock.l_type = F_WRLCK; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4431 | if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) { |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4432 | rc = SQLITE_IOERR_LOCK; |
| 4433 | }else if( lock.l_type==F_UNLCK ){ |
| 4434 | if( pShmNode->isReadonly ){ |
| 4435 | pShmNode->isUnlocked = 1; |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4436 | rc = SQLITE_READONLY_CANTINIT; |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4437 | }else{ |
| 4438 | rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1); |
drh | f7f2a82 | 2018-10-11 13:51:48 +0000 | [diff] [blame] | 4439 | /* The first connection to attach must truncate the -shm file. We |
| 4440 | ** truncate to 3 bytes (an arbitrary small number, less than the |
| 4441 | ** -shm header size) rather than 0 as a system debugging aid, to |
| 4442 | ** help detect if a -shm file truncation is legitimate or is the work |
| 4443 | ** or a rogue process. */ |
| 4444 | if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4445 | rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename); |
| 4446 | } |
| 4447 | } |
| 4448 | }else if( lock.l_type==F_WRLCK ){ |
| 4449 | rc = SQLITE_BUSY; |
| 4450 | } |
| 4451 | |
| 4452 | if( rc==SQLITE_OK ){ |
| 4453 | assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK ); |
| 4454 | rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
| 4455 | } |
| 4456 | return rc; |
| 4457 | } |
| 4458 | |
| 4459 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4460 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4461 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4462 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4463 | ** The file used to implement shared-memory is in the same directory |
| 4464 | ** as the open database file and has the same name as the open database |
| 4465 | ** file with the "-shm" suffix added. For example, if the database file |
| 4466 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4467 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 4468 | ** |
| 4469 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 4470 | ** some other tmpfs mount. But if a file in a different directory |
| 4471 | ** from the database file is used, then differing access permissions |
| 4472 | ** or a chroot() might cause two different processes on the same |
| 4473 | ** database to end up using different files for shared memory - |
| 4474 | ** meaning that their memory would not really be shared - resulting |
| 4475 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 4476 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 4477 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 4478 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 4479 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 4480 | ** same database file at the same time, database corruption will likely |
| 4481 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 4482 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4483 | ** |
| 4484 | ** When opening a new shared-memory file, if no other instances of that |
| 4485 | ** file are currently open, in this process or in other processes, then |
| 4486 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4487 | ** |
| 4488 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 4489 | ** that means that an exclusive lock is held on the database file and |
| 4490 | ** that no other processes are able to read or write the database. In |
| 4491 | ** that case, we do not really need shared memory. No shared memory |
| 4492 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4493 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4494 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 4495 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4496 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4497 | int rc = SQLITE_OK; /* Result code */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4498 | unixInodeInfo *pInode; /* The inode of fd */ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4499 | char *zShm; /* Name of the file used for SHM */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4500 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4501 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4502 | /* Allocate space for the new unixShm object. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4503 | p = sqlite3_malloc64( sizeof(*p) ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4504 | if( p==0 ) return SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4505 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4506 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4507 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4508 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 4509 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4510 | */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 4511 | assert( unixFileMutexNotheld(pDbFd) ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4512 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4513 | pInode = pDbFd->pInode; |
| 4514 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4515 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4516 | struct stat sStat; /* fstat() info for database file */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4517 | #ifndef SQLITE_SHM_DIRECTORY |
| 4518 | const char *zBasePath = pDbFd->zPath; |
| 4519 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4520 | |
| 4521 | /* Call fstat() to figure out the permissions on the database file. If |
| 4522 | ** 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] | 4523 | ** with the same permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4524 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4525 | if( osFstat(pDbFd->h, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4526 | rc = SQLITE_IOERR_FSTAT; |
| 4527 | goto shm_open_err; |
| 4528 | } |
| 4529 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4530 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4531 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4532 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4533 | nShmFilename = 6 + (int)strlen(zBasePath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4534 | #endif |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4535 | pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4536 | if( pShmNode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4537 | rc = SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4538 | goto shm_open_err; |
| 4539 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 4540 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4541 | zShm = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4542 | #ifdef SQLITE_SHM_DIRECTORY |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4543 | sqlite3_snprintf(nShmFilename, zShm, |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4544 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 4545 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 4546 | #else |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4547 | sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath); |
| 4548 | sqlite3FileSuffix3(pDbFd->zPath, zShm); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4549 | #endif |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4550 | pShmNode->hShm = -1; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4551 | pDbFd->pInode->pShmNode = pShmNode; |
| 4552 | pShmNode->pInode = pDbFd->pInode; |
drh | 97a7e5e | 2016-04-26 18:58:54 +0000 | [diff] [blame] | 4553 | if( sqlite3GlobalConfig.bCoreMutex ){ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4554 | pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4555 | if( pShmNode->pShmMutex==0 ){ |
drh | 97a7e5e | 2016-04-26 18:58:54 +0000 | [diff] [blame] | 4556 | rc = SQLITE_NOMEM_BKPT; |
| 4557 | goto shm_open_err; |
| 4558 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4559 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4560 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4561 | if( pInode->bProcessLock==0 ){ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4562 | if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4563 | pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT,(sStat.st_mode&0777)); |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4564 | } |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4565 | if( pShmNode->hShm<0 ){ |
| 4566 | pShmNode->hShm = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777)); |
| 4567 | if( pShmNode->hShm<0 ){ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4568 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm); |
| 4569 | goto shm_open_err; |
| 4570 | } |
| 4571 | pShmNode->isReadonly = 1; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4572 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4573 | |
| 4574 | /* If this process is running as root, make sure that the SHM file |
| 4575 | ** is owned by the same user that owns the original database. Otherwise, |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4576 | ** the original owner will not be able to connect. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4577 | */ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4578 | robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid); |
dan | 176b2a9 | 2017-11-01 06:59:19 +0000 | [diff] [blame] | 4579 | |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4580 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4581 | if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4582 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4585 | /* Make the new connection a child of the unixShmNode */ |
| 4586 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4587 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4588 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4589 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4590 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4591 | pDbFd->pShm = p; |
| 4592 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4593 | |
| 4594 | /* The reference count on pShmNode has already been incremented under |
| 4595 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 4596 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 4597 | ** left to do is to link the new object into the linked list starting |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4598 | ** at pShmNode->pFirst. This must be done while holding the |
| 4599 | ** pShmNode->pShmMutex. |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4600 | */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4601 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4602 | p->pNext = pShmNode->pFirst; |
| 4603 | pShmNode->pFirst = p; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4604 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4605 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4606 | |
| 4607 | /* Jump here on any error */ |
| 4608 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4609 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4610 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4611 | unixLeaveMutex(); |
| 4612 | return rc; |
| 4613 | } |
| 4614 | |
| 4615 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4616 | ** This function is called to obtain a pointer to region iRegion of the |
| 4617 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4618 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 4619 | ** bytes in size. |
| 4620 | ** |
| 4621 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 4622 | ** |
| 4623 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 4624 | ** region has not been allocated (by any client, including one running in a |
| 4625 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4626 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 4627 | ** been allocated, it is allocated by this function. |
| 4628 | ** |
| 4629 | ** If the shared-memory region has already been allocated or is allocated by |
| 4630 | ** this call as described above, then it is mapped into this processes |
| 4631 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4632 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4633 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4634 | static int unixShmMap( |
| 4635 | sqlite3_file *fd, /* Handle open on database file */ |
| 4636 | int iRegion, /* Region to retrieve */ |
| 4637 | int szRegion, /* Size of regions */ |
| 4638 | int bExtend, /* True to extend file if necessary */ |
| 4639 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4640 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4641 | unixFile *pDbFd = (unixFile*)fd; |
| 4642 | unixShm *p; |
| 4643 | unixShmNode *pShmNode; |
| 4644 | int rc = SQLITE_OK; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4645 | int nShmPerMap = unixShmRegionPerMap(); |
| 4646 | int nReqRegion; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4647 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4648 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4649 | if( pDbFd->pShm==0 ){ |
| 4650 | rc = unixOpenSharedMemory(pDbFd); |
| 4651 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4652 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4653 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4654 | p = pDbFd->pShm; |
| 4655 | pShmNode = p->pShmNode; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4656 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4657 | if( pShmNode->isUnlocked ){ |
| 4658 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
| 4659 | if( rc!=SQLITE_OK ) goto shmpage_out; |
| 4660 | pShmNode->isUnlocked = 0; |
| 4661 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4662 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4663 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4664 | assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4665 | assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4666 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4667 | /* Minimum number of regions required to be mapped. */ |
| 4668 | nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; |
| 4669 | |
| 4670 | if( pShmNode->nRegion<nReqRegion ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4671 | char **apNew; /* New apRegion[] array */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4672 | int nByte = nReqRegion*szRegion; /* Minimum required file size */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4673 | struct stat sStat; /* Used by fstat() */ |
| 4674 | |
| 4675 | pShmNode->szRegion = szRegion; |
| 4676 | |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4677 | if( pShmNode->hShm>=0 ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4678 | /* The requested region is not mapped into this processes address space. |
| 4679 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4680 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4681 | */ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4682 | if( osFstat(pShmNode->hShm, &sStat) ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4683 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4684 | goto shmpage_out; |
| 4685 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4686 | |
| 4687 | if( sStat.st_size<nByte ){ |
| 4688 | /* The requested memory region does not exist. If bExtend is set to |
| 4689 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4690 | */ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4691 | if( !bExtend ){ |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4692 | goto shmpage_out; |
| 4693 | } |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4694 | |
| 4695 | /* Alternatively, if bExtend is true, extend the file. Do this by |
| 4696 | ** writing a single byte to the end of each (OS) page being |
| 4697 | ** allocated or extended. Technically, we need only write to the |
| 4698 | ** last page in order to extend the file. But writing to all new |
| 4699 | ** pages forces the OS to allocate them immediately, which reduces |
| 4700 | ** the chances of SIGBUS while accessing the mapped region later on. |
| 4701 | */ |
| 4702 | else{ |
| 4703 | static const int pgsz = 4096; |
| 4704 | int iPg; |
| 4705 | |
| 4706 | /* Write to the last byte of each newly allocated or extended page */ |
| 4707 | assert( (nByte % pgsz)==0 ); |
| 4708 | for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 4709 | int x = 0; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4710 | if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4711 | const char *zFile = pShmNode->zFilename; |
| 4712 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 4713 | goto shmpage_out; |
| 4714 | } |
| 4715 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4716 | } |
| 4717 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | /* Map the requested memory region into this processes address space. */ |
| 4721 | apNew = (char **)sqlite3_realloc( |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4722 | pShmNode->apRegion, nReqRegion*sizeof(char *) |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4723 | ); |
| 4724 | if( !apNew ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4725 | rc = SQLITE_IOERR_NOMEM_BKPT; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4726 | goto shmpage_out; |
| 4727 | } |
| 4728 | pShmNode->apRegion = apNew; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4729 | while( pShmNode->nRegion<nReqRegion ){ |
| 4730 | int nMap = szRegion*nShmPerMap; |
| 4731 | int i; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4732 | void *pMem; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4733 | if( pShmNode->hShm>=0 ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4734 | pMem = osMmap(0, nMap, |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4735 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4736 | MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4737 | ); |
| 4738 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4739 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4740 | goto shmpage_out; |
| 4741 | } |
| 4742 | }else{ |
drh | b6c4d59 | 2018-10-11 02:39:11 +0000 | [diff] [blame] | 4743 | pMem = sqlite3_malloc64(nMap); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4744 | if( pMem==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4745 | rc = SQLITE_NOMEM_BKPT; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4746 | goto shmpage_out; |
| 4747 | } |
drh | b6c4d59 | 2018-10-11 02:39:11 +0000 | [diff] [blame] | 4748 | memset(pMem, 0, nMap); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4749 | } |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4750 | |
| 4751 | for(i=0; i<nShmPerMap; i++){ |
| 4752 | pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; |
| 4753 | } |
| 4754 | pShmNode->nRegion += nShmPerMap; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4755 | } |
| 4756 | } |
| 4757 | |
| 4758 | shmpage_out: |
| 4759 | if( pShmNode->nRegion>iRegion ){ |
| 4760 | *pp = pShmNode->apRegion[iRegion]; |
| 4761 | }else{ |
| 4762 | *pp = 0; |
| 4763 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4764 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4765 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4766 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4770 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4771 | ** |
| 4772 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4773 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4774 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4775 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4776 | */ |
| 4777 | static int unixShmLock( |
| 4778 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4779 | int ofst, /* First lock to acquire or release */ |
| 4780 | int n, /* Number of locks to acquire or release */ |
| 4781 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4782 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4783 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4784 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4785 | unixShm *pX; /* For looping over all siblings */ |
| 4786 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4787 | int rc = SQLITE_OK; /* Result code */ |
| 4788 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4789 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4790 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4791 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4792 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4793 | assert( n>=1 ); |
| 4794 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4795 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4796 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4797 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4798 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4799 | assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4800 | assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4801 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4802 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4803 | assert( n>1 || mask==(1<<ofst) ); |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4804 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4805 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4806 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4807 | |
| 4808 | /* See if any siblings hold this same lock */ |
| 4809 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4810 | if( pX==p ) continue; |
| 4811 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4812 | allMask |= pX->sharedMask; |
| 4813 | } |
| 4814 | |
| 4815 | /* Unlock the system-level locks */ |
| 4816 | if( (mask & allMask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4817 | rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4818 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4819 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4820 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4821 | |
| 4822 | /* Undo the local locks */ |
| 4823 | if( rc==SQLITE_OK ){ |
| 4824 | p->exclMask &= ~mask; |
| 4825 | p->sharedMask &= ~mask; |
| 4826 | } |
| 4827 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4828 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4829 | |
| 4830 | /* Find out which shared locks are already held by sibling connections. |
| 4831 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4832 | ** SQLITE_BUSY. |
| 4833 | */ |
| 4834 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4835 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4836 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4837 | break; |
| 4838 | } |
| 4839 | allShared |= pX->sharedMask; |
| 4840 | } |
| 4841 | |
| 4842 | /* Get shared locks at the system level, if necessary */ |
| 4843 | if( rc==SQLITE_OK ){ |
| 4844 | if( (allShared & mask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4845 | rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4846 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4847 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4848 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4849 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4850 | |
| 4851 | /* Get the local shared locks */ |
| 4852 | if( rc==SQLITE_OK ){ |
| 4853 | p->sharedMask |= mask; |
| 4854 | } |
| 4855 | }else{ |
| 4856 | /* Make sure no sibling connections hold locks that will block this |
| 4857 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4858 | */ |
| 4859 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4860 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4861 | rc = SQLITE_BUSY; |
| 4862 | break; |
| 4863 | } |
| 4864 | } |
| 4865 | |
| 4866 | /* Get the exclusive locks at the system level. Then if successful |
| 4867 | ** also mark the local connection as being locked. |
| 4868 | */ |
| 4869 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4870 | rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4871 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4872 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4873 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4874 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4875 | } |
| 4876 | } |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4877 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4878 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 4879 | p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4880 | return rc; |
| 4881 | } |
| 4882 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4883 | /* |
| 4884 | ** Implement a memory barrier or memory fence on shared memory. |
| 4885 | ** |
| 4886 | ** All loads and stores begun before the barrier must complete before |
| 4887 | ** any load or store begun after the barrier. |
| 4888 | */ |
| 4889 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4890 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4891 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4892 | UNUSED_PARAMETER(fd); |
drh | 22c733d | 2015-09-24 12:40:43 +0000 | [diff] [blame] | 4893 | sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ |
dan | a86acc2 | 2018-09-12 20:32:19 +0000 | [diff] [blame] | 4894 | assert( fd->pMethods->xLock==nolockLock |
| 4895 | || unixFileMutexNotheld((unixFile*)fd) |
| 4896 | ); |
drh | 22c733d | 2015-09-24 12:40:43 +0000 | [diff] [blame] | 4897 | unixEnterMutex(); /* Also mutex, for redundancy */ |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4898 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4901 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4902 | ** Close a connection to shared-memory. Delete the underlying |
| 4903 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4904 | ** |
| 4905 | ** If there is no shared memory associated with the connection then this |
| 4906 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4907 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4908 | static int unixShmUnmap( |
| 4909 | sqlite3_file *fd, /* The underlying database file */ |
| 4910 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4911 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4912 | unixShm *p; /* The connection to be closed */ |
| 4913 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4914 | unixShm **pp; /* For looping over sibling connections */ |
| 4915 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4916 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4917 | pDbFd = (unixFile*)fd; |
| 4918 | p = pDbFd->pShm; |
| 4919 | if( p==0 ) return SQLITE_OK; |
| 4920 | pShmNode = p->pShmNode; |
| 4921 | |
| 4922 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4923 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4924 | |
| 4925 | /* Remove connection p from the set of connections associated |
| 4926 | ** with pShmNode */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4927 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4928 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4929 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4930 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4931 | /* Free the connection p */ |
| 4932 | sqlite3_free(p); |
| 4933 | pDbFd->pShm = 0; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4934 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4935 | |
| 4936 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4937 | ** shared-memory file, too */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 4938 | assert( unixFileMutexNotheld(pDbFd) ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4939 | unixEnterMutex(); |
| 4940 | assert( pShmNode->nRef>0 ); |
| 4941 | pShmNode->nRef--; |
| 4942 | if( pShmNode->nRef==0 ){ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4943 | if( deleteFlag && pShmNode->hShm>=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4944 | osUnlink(pShmNode->zFilename); |
| 4945 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4946 | unixShmPurge(pDbFd); |
| 4947 | } |
| 4948 | unixLeaveMutex(); |
| 4949 | |
| 4950 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4951 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4952 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4953 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4954 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4955 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4956 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4957 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4958 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4959 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4960 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4961 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4962 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4963 | ** If it is currently memory mapped, unmap file pFd. |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4964 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4965 | static void unixUnmapfile(unixFile *pFd){ |
| 4966 | assert( pFd->nFetchOut==0 ); |
| 4967 | if( pFd->pMapRegion ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4968 | osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4969 | pFd->pMapRegion = 0; |
| 4970 | pFd->mmapSize = 0; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4971 | pFd->mmapSizeActual = 0; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4972 | } |
| 4973 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4974 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4975 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4976 | ** Attempt to set the size of the memory mapping maintained by file |
| 4977 | ** descriptor pFd to nNew bytes. Any existing mapping is discarded. |
| 4978 | ** |
| 4979 | ** If successful, this function sets the following variables: |
| 4980 | ** |
| 4981 | ** unixFile.pMapRegion |
| 4982 | ** unixFile.mmapSize |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4983 | ** unixFile.mmapSizeActual |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4984 | ** |
| 4985 | ** If unsuccessful, an error message is logged via sqlite3_log() and |
| 4986 | ** the three variables above are zeroed. In this case SQLite should |
| 4987 | ** continue accessing the database using the xRead() and xWrite() |
| 4988 | ** methods. |
| 4989 | */ |
| 4990 | static void unixRemapfile( |
| 4991 | unixFile *pFd, /* File descriptor object */ |
| 4992 | i64 nNew /* Required mapping size */ |
| 4993 | ){ |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4994 | const char *zErr = "mmap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4995 | int h = pFd->h; /* File descriptor open on db file */ |
| 4996 | u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4997 | i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4998 | u8 *pNew = 0; /* Location of new mapping */ |
| 4999 | int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 5000 | |
| 5001 | assert( pFd->nFetchOut==0 ); |
| 5002 | assert( nNew>pFd->mmapSize ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5003 | assert( nNew<=pFd->mmapSizeMax ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5004 | assert( nNew>0 ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5005 | assert( pFd->mmapSizeActual>=pFd->mmapSize ); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5006 | assert( MAP_FAILED!=0 ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5007 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 5008 | #ifdef SQLITE_MMAP_READWRITE |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5009 | if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 5010 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5011 | |
| 5012 | if( pOrig ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 5013 | #if HAVE_MREMAP |
| 5014 | i64 nReuse = pFd->mmapSize; |
| 5015 | #else |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 5016 | const int szSyspage = osGetpagesize(); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5017 | i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 5018 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5019 | u8 *pReq = &pOrig[nReuse]; |
| 5020 | |
| 5021 | /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 5022 | if( nReuse!=nOrig ){ |
| 5023 | osMunmap(pReq, nOrig-nReuse); |
| 5024 | } |
| 5025 | |
| 5026 | #if HAVE_MREMAP |
| 5027 | pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5028 | zErr = "mremap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5029 | #else |
| 5030 | pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 5031 | if( pNew!=MAP_FAILED ){ |
| 5032 | if( pNew!=pReq ){ |
| 5033 | osMunmap(pNew, nNew - nReuse); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5034 | pNew = 0; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5035 | }else{ |
| 5036 | pNew = pOrig; |
| 5037 | } |
| 5038 | } |
| 5039 | #endif |
| 5040 | |
dan | 48ccef8 | 2013-04-02 20:55:01 +0000 | [diff] [blame] | 5041 | /* The attempt to extend the existing mapping failed. Free it. */ |
| 5042 | if( pNew==MAP_FAILED || pNew==0 ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5043 | osMunmap(pOrig, nReuse); |
| 5044 | } |
| 5045 | } |
| 5046 | |
| 5047 | /* If pNew is still NULL, try to create an entirely new mapping. */ |
| 5048 | if( pNew==0 ){ |
| 5049 | pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5052 | if( pNew==MAP_FAILED ){ |
| 5053 | pNew = 0; |
| 5054 | nNew = 0; |
| 5055 | unixLogError(SQLITE_OK, zErr, pFd->zPath); |
| 5056 | |
| 5057 | /* If the mmap() above failed, assume that all subsequent mmap() calls |
| 5058 | ** will probably fail too. Fall back to using xRead/xWrite exclusively |
| 5059 | ** in this case. */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5060 | pFd->mmapSizeMax = 0; |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5061 | } |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5062 | pFd->pMapRegion = (void *)pNew; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5063 | pFd->mmapSize = pFd->mmapSizeActual = nNew; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5067 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 5068 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 5069 | ** there already exists a mapping for this file, and there are still |
| 5070 | ** outstanding xFetch() references to it, this function is a no-op. |
| 5071 | ** |
| 5072 | ** If parameter nByte is non-negative, then it is the requested size of |
| 5073 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 5074 | ** requested size is the size of the file on disk. The actual size of the |
| 5075 | ** created mapping is either the requested size or the value configured |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 5076 | ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5077 | ** |
| 5078 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 5079 | ** recreated as a result of outstanding references) or an SQLite error |
| 5080 | ** code otherwise. |
| 5081 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 5082 | static int unixMapfile(unixFile *pFd, i64 nMap){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5083 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 5084 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5085 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 5086 | |
| 5087 | if( nMap<0 ){ |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 5088 | struct stat statbuf; /* Low-level file information */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 5089 | if( osFstat(pFd->h, &statbuf) ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5090 | return SQLITE_IOERR_FSTAT; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 5091 | } |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 5092 | nMap = statbuf.st_size; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5093 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5094 | if( nMap>pFd->mmapSizeMax ){ |
| 5095 | nMap = pFd->mmapSizeMax; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 5098 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5099 | if( nMap!=pFd->mmapSize ){ |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 5100 | unixRemapfile(pFd, nMap); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5103 | return SQLITE_OK; |
| 5104 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 5105 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5106 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5107 | /* |
| 5108 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 5109 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 5110 | ** |
| 5111 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 5112 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 5113 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 5114 | ** value of *pp is undefined in this case. |
| 5115 | ** |
| 5116 | ** If this function does return a pointer, the caller must eventually |
| 5117 | ** release the reference by calling unixUnfetch(). |
| 5118 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5119 | static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5120 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5121 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5122 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5123 | *pp = 0; |
| 5124 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5125 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 5126 | if( pFd->mmapSizeMax>0 ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5127 | if( pFd->pMapRegion==0 ){ |
| 5128 | int rc = unixMapfile(pFd, -1); |
| 5129 | if( rc!=SQLITE_OK ) return rc; |
| 5130 | } |
| 5131 | if( pFd->mmapSize >= iOff+nAmt ){ |
| 5132 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 5133 | pFd->nFetchOut++; |
| 5134 | } |
| 5135 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 5136 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5137 | return SQLITE_OK; |
| 5138 | } |
| 5139 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5140 | /* |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5141 | ** If the third argument is non-NULL, then this function releases a |
| 5142 | ** reference obtained by an earlier call to unixFetch(). The second |
| 5143 | ** argument passed to this function must be the same as the corresponding |
| 5144 | ** argument that was passed to the unixFetch() invocation. |
| 5145 | ** |
| 5146 | ** Or, if the third argument is NULL, then this function is being called |
| 5147 | ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 5148 | ** may now be invalid and should be unmapped. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5149 | */ |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5150 | static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5151 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5152 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 5153 | UNUSED_PARAMETER(iOff); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5154 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5155 | /* If p==0 (unmap the entire file) then there must be no outstanding |
| 5156 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 5157 | ** then there must be at least one outstanding. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5158 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 5159 | |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5160 | /* If p!=0, it must match the iOff value. */ |
| 5161 | assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 5162 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5163 | if( p ){ |
| 5164 | pFd->nFetchOut--; |
| 5165 | }else{ |
| 5166 | unixUnmapfile(pFd); |
| 5167 | } |
| 5168 | |
| 5169 | assert( pFd->nFetchOut>=0 ); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5170 | #else |
| 5171 | UNUSED_PARAMETER(fd); |
| 5172 | UNUSED_PARAMETER(p); |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 5173 | UNUSED_PARAMETER(iOff); |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5174 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5175 | return SQLITE_OK; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5179 | ** Here ends the implementation of all sqlite3_file methods. |
| 5180 | ** |
| 5181 | ********************** End sqlite3_file Methods ******************************* |
| 5182 | ******************************************************************************/ |
| 5183 | |
| 5184 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5185 | ** This division contains definitions of sqlite3_io_methods objects that |
| 5186 | ** implement various file locking strategies. It also contains definitions |
| 5187 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 5188 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 5189 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 5190 | ** the correct finder-function for that VFS. |
| 5191 | ** |
| 5192 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 5193 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 5194 | ** looks at the filesystem type and tries to guess the best locking |
| 5195 | ** strategy from that. |
| 5196 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5197 | ** For finder-function F, two objects are created: |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5198 | ** |
| 5199 | ** (1) The real finder-function named "FImpt()". |
| 5200 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5201 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5202 | ** |
| 5203 | ** |
| 5204 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 5205 | ** objects. We have to do this instead of letting pAppData point |
| 5206 | ** directly at the finder-function since C90 rules prevent a void* |
| 5207 | ** from be cast into a function pointer. |
| 5208 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5209 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5210 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5211 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5212 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 5213 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 5214 | ** |
| 5215 | ** * An I/O method finder function called FINDER that returns a pointer |
| 5216 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5217 | */ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 5218 | #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5219 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5220 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5221 | CLOSE, /* xClose */ \ |
| 5222 | unixRead, /* xRead */ \ |
| 5223 | unixWrite, /* xWrite */ \ |
| 5224 | unixTruncate, /* xTruncate */ \ |
| 5225 | unixSync, /* xSync */ \ |
| 5226 | unixFileSize, /* xFileSize */ \ |
| 5227 | LOCK, /* xLock */ \ |
| 5228 | UNLOCK, /* xUnlock */ \ |
| 5229 | CKLOCK, /* xCheckReservedLock */ \ |
| 5230 | unixFileControl, /* xFileControl */ \ |
| 5231 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5232 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5233 | SHMMAP, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5234 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 5235 | unixShmBarrier, /* xShmBarrier */ \ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5236 | unixShmUnmap, /* xShmUnmap */ \ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5237 | unixFetch, /* xFetch */ \ |
| 5238 | unixUnfetch, /* xUnfetch */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5239 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5240 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 5241 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5242 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5243 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5244 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5245 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5246 | |
| 5247 | /* |
| 5248 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 5249 | ** locking strategies. Functions that return pointers to these methods |
| 5250 | ** are also created. |
| 5251 | */ |
| 5252 | IOMETHODS( |
| 5253 | posixIoFinder, /* Finder function name */ |
| 5254 | posixIoMethods, /* sqlite3_io_methods object name */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5255 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5256 | unixClose, /* xClose method */ |
| 5257 | unixLock, /* xLock method */ |
| 5258 | unixUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5259 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5260 | unixShmMap /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5261 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5262 | IOMETHODS( |
| 5263 | nolockIoFinder, /* Finder function name */ |
| 5264 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 3e2c842 | 2018-08-13 11:32:07 +0000 | [diff] [blame] | 5265 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5266 | nolockClose, /* xClose method */ |
| 5267 | nolockLock, /* xLock method */ |
| 5268 | nolockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5269 | nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 5270 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5271 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5272 | IOMETHODS( |
| 5273 | dotlockIoFinder, /* Finder function name */ |
| 5274 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5275 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5276 | dotlockClose, /* xClose method */ |
| 5277 | dotlockLock, /* xLock method */ |
| 5278 | dotlockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5279 | dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 5280 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5281 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5282 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5283 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5284 | IOMETHODS( |
| 5285 | flockIoFinder, /* Finder function name */ |
| 5286 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5287 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5288 | flockClose, /* xClose method */ |
| 5289 | flockLock, /* xLock method */ |
| 5290 | flockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5291 | flockCheckReservedLock, /* xCheckReservedLock method */ |
| 5292 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5293 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5294 | #endif |
| 5295 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5296 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5297 | IOMETHODS( |
| 5298 | semIoFinder, /* Finder function name */ |
| 5299 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5300 | 1, /* shared memory is disabled */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5301 | semXClose, /* xClose method */ |
| 5302 | semXLock, /* xLock method */ |
| 5303 | semXUnlock, /* xUnlock method */ |
| 5304 | semXCheckReservedLock, /* xCheckReservedLock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5305 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5306 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5307 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5308 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5309 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5310 | IOMETHODS( |
| 5311 | afpIoFinder, /* Finder function name */ |
| 5312 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5313 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5314 | afpClose, /* xClose method */ |
| 5315 | afpLock, /* xLock method */ |
| 5316 | afpUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5317 | afpCheckReservedLock, /* xCheckReservedLock method */ |
| 5318 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5319 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5320 | #endif |
| 5321 | |
| 5322 | /* |
| 5323 | ** The proxy locking method is a "super-method" in the sense that it |
| 5324 | ** opens secondary file descriptors for the conch and lock files and |
| 5325 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 5326 | ** secondary files. For this reason, the division that implements |
| 5327 | ** proxy locking is located much further down in the file. But we need |
| 5328 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 5329 | ** for proxy locking here. So we forward declare the I/O methods. |
| 5330 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5331 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5332 | static int proxyClose(sqlite3_file*); |
| 5333 | static int proxyLock(sqlite3_file*, int); |
| 5334 | static int proxyUnlock(sqlite3_file*, int); |
| 5335 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5336 | IOMETHODS( |
| 5337 | proxyIoFinder, /* Finder function name */ |
| 5338 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5339 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5340 | proxyClose, /* xClose method */ |
| 5341 | proxyLock, /* xLock method */ |
| 5342 | proxyUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5343 | proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 5344 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5345 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5346 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5347 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5348 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 5349 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5350 | IOMETHODS( |
| 5351 | nfsIoFinder, /* Finder function name */ |
| 5352 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5353 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5354 | unixClose, /* xClose method */ |
| 5355 | unixLock, /* xLock method */ |
| 5356 | nfsUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5357 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5358 | 0 /* xShmMap method */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5359 | ) |
| 5360 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5361 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5362 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5363 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5364 | ** This "finder" function attempts to determine the best locking strategy |
| 5365 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5366 | ** object that implements that strategy. |
| 5367 | ** |
| 5368 | ** This is for MacOSX only. |
| 5369 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5370 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5371 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5372 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5373 | ){ |
| 5374 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5375 | const char *zFilesystem; /* Filesystem type name */ |
| 5376 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5377 | } aMap[] = { |
| 5378 | { "hfs", &posixIoMethods }, |
| 5379 | { "ufs", &posixIoMethods }, |
| 5380 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5381 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5382 | { "webdav", &nolockIoMethods }, |
| 5383 | { 0, 0 } |
| 5384 | }; |
| 5385 | int i; |
| 5386 | struct statfs fsInfo; |
| 5387 | struct flock lockInfo; |
| 5388 | |
| 5389 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5390 | /* If filePath==NULL that means we are dealing with a transient file |
| 5391 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5392 | return &nolockIoMethods; |
| 5393 | } |
| 5394 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 5395 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 5396 | return &nolockIoMethods; |
| 5397 | } |
| 5398 | for(i=0; aMap[i].zFilesystem; i++){ |
| 5399 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 5400 | return aMap[i].pMethods; |
| 5401 | } |
| 5402 | } |
| 5403 | } |
| 5404 | |
| 5405 | /* Default case. Handles, amongst others, "nfs". |
| 5406 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 5407 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5408 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5409 | lockInfo.l_len = 1; |
| 5410 | lockInfo.l_start = 0; |
| 5411 | lockInfo.l_whence = SEEK_SET; |
| 5412 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5413 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5414 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 5415 | return &nfsIoMethods; |
| 5416 | } else { |
| 5417 | return &posixIoMethods; |
| 5418 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5419 | }else{ |
| 5420 | return &dotlockIoMethods; |
| 5421 | } |
| 5422 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5423 | static const sqlite3_io_methods |
| 5424 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5425 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5426 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5427 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5428 | #if OS_VXWORKS |
| 5429 | /* |
| 5430 | ** This "finder" function for VxWorks checks to see if posix advisory |
| 5431 | ** locking works. If it does, then that is what is used. If it does not |
| 5432 | ** work, then fallback to named semaphore locking. |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5433 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5434 | static const sqlite3_io_methods *vxworksIoFinderImpl( |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5435 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5436 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5437 | ){ |
| 5438 | struct flock lockInfo; |
| 5439 | |
| 5440 | if( !filePath ){ |
| 5441 | /* If filePath==NULL that means we are dealing with a transient file |
| 5442 | ** that does not need to be locked. */ |
| 5443 | return &nolockIoMethods; |
| 5444 | } |
| 5445 | |
| 5446 | /* Test if fcntl() is supported and use POSIX style locks. |
| 5447 | ** Otherwise fall back to the named semaphore method. |
| 5448 | */ |
| 5449 | lockInfo.l_len = 1; |
| 5450 | lockInfo.l_start = 0; |
| 5451 | lockInfo.l_whence = SEEK_SET; |
| 5452 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5453 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5454 | return &posixIoMethods; |
| 5455 | }else{ |
| 5456 | return &semIoMethods; |
| 5457 | } |
| 5458 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5459 | static const sqlite3_io_methods |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5460 | *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5461 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5462 | #endif /* OS_VXWORKS */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5463 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5464 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5465 | ** An abstract type for a pointer to an IO method finder function: |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5466 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5467 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5468 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5469 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5470 | /**************************************************************************** |
| 5471 | **************************** sqlite3_vfs methods **************************** |
| 5472 | ** |
| 5473 | ** This division contains the implementation of methods on the |
| 5474 | ** sqlite3_vfs object. |
| 5475 | */ |
| 5476 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 5477 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5478 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5479 | */ |
| 5480 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5481 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5482 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5483 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5484 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5485 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5486 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5487 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5488 | unixFile *pNew = (unixFile *)pId; |
| 5489 | int rc = SQLITE_OK; |
| 5490 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5491 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5492 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5493 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5494 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5495 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5496 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5497 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5498 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5499 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5500 | pNew->ctrlFlags = (u8)ctrlFlags; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5501 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | ede01a9 | 2013-05-17 12:10:52 +0000 | [diff] [blame] | 5502 | pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5503 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5504 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 5505 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5506 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 5507 | } |
drh | 503a686 | 2013-03-01 01:07:17 +0000 | [diff] [blame] | 5508 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5509 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5510 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 5511 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5512 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5513 | pNew->pId = vxworksFindFileId(zFilename); |
| 5514 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5515 | ctrlFlags |= UNIXFILE_NOLOCK; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5516 | rc = SQLITE_NOMEM_BKPT; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5517 | } |
| 5518 | #endif |
| 5519 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5520 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5521 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5522 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5523 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5524 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5525 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 5526 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 5527 | ** zFilename remains valid until file is closed, to support */ |
| 5528 | pNew->lockingContext = (void*)zFilename; |
| 5529 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5530 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5531 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5532 | if( pLockingStyle == &posixIoMethods |
| 5533 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5534 | || pLockingStyle == &nfsIoMethods |
| 5535 | #endif |
| 5536 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5537 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5538 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5539 | if( rc!=SQLITE_OK ){ |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5540 | /* If an error occurred in findInodeInfo(), close the file descriptor |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5541 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5542 | ** in two scenarios: |
| 5543 | ** |
| 5544 | ** (a) A call to fstat() failed. |
| 5545 | ** (b) A malloc failed. |
| 5546 | ** |
| 5547 | ** Scenario (b) may only occur if the process is holding no other |
| 5548 | ** file descriptors open on the same file. If there were other file |
| 5549 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5550 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5551 | ** handle h - as it is guaranteed that no posix locks will be released |
| 5552 | ** by doing so. |
| 5553 | ** |
| 5554 | ** If scenario (a) caused the error then things are not so safe. The |
| 5555 | ** implicit assumption here is that if fstat() fails, things are in |
| 5556 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 5557 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5558 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5559 | h = -1; |
| 5560 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5561 | unixLeaveMutex(); |
| 5562 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5563 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5564 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 5565 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5566 | /* AFP locking uses the file path so it needs to be included in |
| 5567 | ** the afpLockingContext. |
| 5568 | */ |
| 5569 | afpLockingContext *pCtx; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5570 | pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5571 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5572 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5573 | }else{ |
| 5574 | /* NB: zFilename exists and remains valid until the file is closed |
| 5575 | ** according to requirement F11141. So we do not need to make a |
| 5576 | ** copy of the filename. */ |
| 5577 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5578 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5579 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5580 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5581 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5582 | if( rc!=SQLITE_OK ){ |
| 5583 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5584 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5585 | h = -1; |
| 5586 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5587 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5588 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5589 | } |
| 5590 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5591 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5592 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 5593 | /* Dotfile locking uses the file path so it needs to be included in |
| 5594 | ** the dotlockLockingContext |
| 5595 | */ |
| 5596 | char *zLockFile; |
| 5597 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5598 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5599 | nFilename = (int)strlen(zFilename) + 6; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5600 | zLockFile = (char *)sqlite3_malloc64(nFilename); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5601 | if( zLockFile==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5602 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5603 | }else{ |
| 5604 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5605 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5606 | pNew->lockingContext = zLockFile; |
| 5607 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5608 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5609 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5610 | else if( pLockingStyle == &semIoMethods ){ |
| 5611 | /* Named semaphore locking uses the file path so it needs to be |
| 5612 | ** included in the semLockingContext |
| 5613 | */ |
| 5614 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5615 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 5616 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 5617 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5618 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5619 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5620 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5621 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5622 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5623 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 5624 | if( pNew->pInode->pSem == SEM_FAILED ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5625 | rc = SQLITE_NOMEM_BKPT; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5626 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5627 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5628 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5629 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5630 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5631 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 5632 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5633 | storeLastErrno(pNew, 0); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5634 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5635 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5636 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 5637 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5638 | osUnlink(zFilename); |
drh | c579754 | 2013-04-27 12:13:29 +0000 | [diff] [blame] | 5639 | pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5640 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5641 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5642 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5643 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5644 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5645 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5646 | OpenCounter(+1); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5647 | verifyDbFile(pNew); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5648 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5649 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5650 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 5651 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5652 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5653 | ** Return the name of a directory in which to put temporary files. |
| 5654 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5655 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5656 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5657 | static const char *azDirs[] = { |
| 5658 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5659 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5660 | "/var/tmp", |
| 5661 | "/usr/tmp", |
| 5662 | "/tmp", |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5663 | "." |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5664 | }; |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5665 | unsigned int i = 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5666 | struct stat buf; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5667 | const char *zDir = sqlite3_temp_directory; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5668 | |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5669 | if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); |
| 5670 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5671 | while(1){ |
| 5672 | if( zDir!=0 |
| 5673 | && osStat(zDir, &buf)==0 |
| 5674 | && S_ISDIR(buf.st_mode) |
| 5675 | && osAccess(zDir, 03)==0 |
| 5676 | ){ |
| 5677 | return zDir; |
| 5678 | } |
| 5679 | if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break; |
| 5680 | zDir = azDirs[i++]; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5681 | } |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5682 | return 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
| 5685 | /* |
| 5686 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 5687 | ** by the calling process and must be big enough to hold at least |
| 5688 | ** pVfs->mxPathname bytes. |
| 5689 | */ |
| 5690 | static int unixGetTempname(int nBuf, char *zBuf){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5691 | const char *zDir; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5692 | int iLimit = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5693 | |
| 5694 | /* It's odd to simulate an io-error here, but really this is just |
| 5695 | ** using the io-error infrastructure to test that SQLite handles this |
| 5696 | ** function failing. |
| 5697 | */ |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5698 | zBuf[0] = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5699 | SimulateIOError( return SQLITE_IOERR ); |
| 5700 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5701 | zDir = unixTempFileDir(); |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5702 | if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5703 | do{ |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 5704 | u64 r; |
| 5705 | sqlite3_randomness(sizeof(r), &r); |
| 5706 | assert( nBuf>2 ); |
| 5707 | zBuf[nBuf-2] = 0; |
| 5708 | sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", |
| 5709 | zDir, r, 0); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5710 | if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5711 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5712 | return SQLITE_OK; |
| 5713 | } |
| 5714 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5715 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5716 | /* |
| 5717 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 5718 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 5719 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 5720 | */ |
| 5721 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 5722 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5723 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5724 | /* |
| 5725 | ** Search for an unused file descriptor that was opened on the database |
| 5726 | ** file (not a journal or master-journal file) identified by pathname |
| 5727 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 5728 | ** argument to this function. |
| 5729 | ** |
| 5730 | ** Such a file descriptor may exist if a database connection was closed |
| 5731 | ** but the associated file descriptor could not be closed because some |
| 5732 | ** other file descriptor open on the same file is holding a file-lock. |
| 5733 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 5734 | ** describing "Posix Advisory Locking" at the start of this file for |
| 5735 | ** further details. Also, ticket #4018. |
| 5736 | ** |
| 5737 | ** If a suitable file descriptor is found, then it is returned. If no |
| 5738 | ** such file descriptor is located, -1 is returned. |
| 5739 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5740 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 5741 | UnixUnusedFd *pUnused = 0; |
| 5742 | |
| 5743 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 5744 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 5745 | ** but because no way to test it is currently available. It is better |
| 5746 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 5747 | ** feature. */ |
| 5748 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5749 | struct stat sStat; /* Results of stat() call */ |
| 5750 | |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5751 | unixEnterMutex(); |
| 5752 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5753 | /* A stat() call may fail for various reasons. If this happens, it is |
| 5754 | ** almost certain that an open() call on the same path will also fail. |
| 5755 | ** For this reason, if an error occurs in the stat() call here, it is |
| 5756 | ** ignored and -1 is returned. The caller will try to open a new file |
| 5757 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 5758 | ** |
| 5759 | ** Even if a subsequent open() call does succeed, the consequences of |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5760 | ** not searching for a reusable file descriptor are not dire. */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5761 | if( inodeList!=0 && 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 5762 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5763 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5764 | pInode = inodeList; |
| 5765 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 5766 | || pInode->fileId.ino!=(u64)sStat.st_ino) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5767 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 5768 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5769 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5770 | UnixUnusedFd **pp; |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5771 | assert( sqlite3_mutex_notheld(pInode->pLockMutex) ); |
| 5772 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 55220a6 | 2019-08-06 20:55:06 +0000 | [diff] [blame] | 5773 | flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5774 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5775 | pUnused = *pp; |
| 5776 | if( pUnused ){ |
| 5777 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5778 | } |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5779 | sqlite3_mutex_leave(pInode->pLockMutex); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5780 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5781 | } |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5782 | unixLeaveMutex(); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5783 | #endif /* if !OS_VXWORKS */ |
| 5784 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5785 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5786 | |
| 5787 | /* |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5788 | ** Find the mode, uid and gid of file zFile. |
| 5789 | */ |
| 5790 | static int getFileMode( |
| 5791 | const char *zFile, /* File name */ |
| 5792 | mode_t *pMode, /* OUT: Permissions of zFile */ |
| 5793 | uid_t *pUid, /* OUT: uid of zFile. */ |
| 5794 | gid_t *pGid /* OUT: gid of zFile. */ |
| 5795 | ){ |
| 5796 | struct stat sStat; /* Output of stat() on database file */ |
| 5797 | int rc = SQLITE_OK; |
| 5798 | if( 0==osStat(zFile, &sStat) ){ |
| 5799 | *pMode = sStat.st_mode & 0777; |
| 5800 | *pUid = sStat.st_uid; |
| 5801 | *pGid = sStat.st_gid; |
| 5802 | }else{ |
| 5803 | rc = SQLITE_IOERR_FSTAT; |
| 5804 | } |
| 5805 | return rc; |
| 5806 | } |
| 5807 | |
| 5808 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5809 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 5810 | ** 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] | 5811 | ** and a value suitable for passing as the third argument to open(2) is |
| 5812 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 5813 | ** returned and the value of *pMode is not modified. |
| 5814 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5815 | ** In most cases, this routine sets *pMode to 0, which will become |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5816 | ** an indication to robust_open() to create the file using |
| 5817 | ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 5818 | ** 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] | 5819 | ** this function queries the file-system for the permissions on the |
| 5820 | ** corresponding database file and sets *pMode to this value. Whenever |
| 5821 | ** possible, WAL and journal files are created using the same permissions |
| 5822 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5823 | ** |
| 5824 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 5825 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 5826 | ** FAT filesystems and permissions do not matter there, so just use |
drh | 1116b17 | 2019-09-25 10:36:31 +0000 | [diff] [blame^] | 5827 | ** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5828 | */ |
| 5829 | static int findCreateFileMode( |
| 5830 | const char *zPath, /* Path of file (possibly) being created */ |
| 5831 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5832 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 5833 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 5834 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5835 | ){ |
| 5836 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5837 | *pMode = 0; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5838 | *pUid = 0; |
| 5839 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5840 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5841 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 5842 | int nDb; /* Number of valid bytes in zDb */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5843 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5844 | /* zPath is a path to a WAL or journal file. The following block derives |
| 5845 | ** the path to the associated database file from zPath. This block handles |
| 5846 | ** the following naming conventions: |
| 5847 | ** |
| 5848 | ** "<path to db>-journal" |
| 5849 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5850 | ** "<path to db>-journalNN" |
| 5851 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5852 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5853 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5854 | ** used by the test_multiplex.c module. |
| 5855 | */ |
| 5856 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5857 | while( zPath[nDb]!='-' ){ |
dan | 629ec14 | 2017-09-14 20:41:17 +0000 | [diff] [blame] | 5858 | /* In normal operation, the journal file name will always contain |
| 5859 | ** a '-' character. However in 8+3 filename mode, or if a corrupt |
| 5860 | ** rollback journal specifies a master journal with a goofy name, then |
| 5861 | ** the '-' might be missing. */ |
drh | 90e5dda | 2015-12-03 20:42:28 +0000 | [diff] [blame] | 5862 | if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5863 | nDb--; |
| 5864 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5865 | memcpy(zDb, zPath, nDb); |
| 5866 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5867 | |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5868 | rc = getFileMode(zDb, pMode, pUid, pGid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5869 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 5870 | *pMode = 0600; |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5871 | }else if( flags & SQLITE_OPEN_URI ){ |
| 5872 | /* If this is a main database file and the file was opened using a URI |
| 5873 | ** filename, check for the "modeof" parameter. If present, interpret |
| 5874 | ** its value as a filename and try to copy the mode, uid and gid from |
| 5875 | ** that file. */ |
| 5876 | const char *z = sqlite3_uri_parameter(zPath, "modeof"); |
| 5877 | if( z ){ |
| 5878 | rc = getFileMode(z, pMode, pUid, pGid); |
| 5879 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5880 | } |
| 5881 | return rc; |
| 5882 | } |
| 5883 | |
| 5884 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5885 | ** Open the file zPath. |
| 5886 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5887 | ** Previously, the SQLite OS layer used three functions in place of this |
| 5888 | ** one: |
| 5889 | ** |
| 5890 | ** sqlite3OsOpenReadWrite(); |
| 5891 | ** sqlite3OsOpenReadOnly(); |
| 5892 | ** sqlite3OsOpenExclusive(); |
| 5893 | ** |
| 5894 | ** These calls correspond to the following combinations of flags: |
| 5895 | ** |
| 5896 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5897 | ** ReadOnly() -> (READONLY) |
| 5898 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5899 | ** |
| 5900 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5901 | ** true, the file was configured to be automatically deleted when the |
| 5902 | ** file handle closed. To achieve the same effect using this new |
| 5903 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5904 | ** OpenExclusive(). |
| 5905 | */ |
| 5906 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5907 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5908 | const char *zPath, /* Pathname of file to be opened */ |
| 5909 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5910 | int flags, /* Input flags to control the opening */ |
| 5911 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5912 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5913 | unixFile *p = (unixFile *)pFile; |
| 5914 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5915 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5916 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5917 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5918 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5919 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5920 | |
| 5921 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5922 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5923 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5924 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5925 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5926 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5927 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5928 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5929 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5930 | struct statfs fsInfo; |
| 5931 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5932 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5933 | /* If creating a master or main-file journal, this function will open |
| 5934 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5935 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5936 | */ |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 5937 | int isNewJrnl = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5938 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5939 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5940 | || eType==SQLITE_OPEN_WAL |
| 5941 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5942 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5943 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5944 | ** a temporary file. Use this buffer to store the file name in. |
| 5945 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5946 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5947 | const char *zName = zPath; |
| 5948 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5949 | /* Check the following statements are true: |
| 5950 | ** |
| 5951 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5952 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5953 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5954 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5955 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5956 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5957 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5958 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5959 | assert(isDelete==0 || isCreate); |
| 5960 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5961 | /* The main DB, main journal, WAL file and master journal are never |
| 5962 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5963 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5964 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5965 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5966 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5967 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5968 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5969 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5970 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5971 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5972 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5973 | ); |
| 5974 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5975 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 5976 | ** here such that two or more threads all trying to open databases at |
| 5977 | ** the same instant might all reset the PRNG. But multiple resets |
| 5978 | ** are harmless. |
| 5979 | */ |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 5980 | if( randomnessPid!=osGetpid(0) ){ |
| 5981 | randomnessPid = osGetpid(0); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5982 | sqlite3_randomness(0,0); |
| 5983 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5984 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5985 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5986 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5987 | UnixUnusedFd *pUnused; |
| 5988 | pUnused = findReusableFd(zName, flags); |
| 5989 | if( pUnused ){ |
| 5990 | fd = pUnused->fd; |
| 5991 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5992 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5993 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5994 | return SQLITE_NOMEM_BKPT; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5995 | } |
| 5996 | } |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5997 | p->pPreallocatedUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5998 | |
| 5999 | /* Database filenames are double-zero terminated if they are not |
| 6000 | ** URIs with parameters. Hence, they can always be passed into |
| 6001 | ** sqlite3_uri_parameter(). */ |
| 6002 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 6003 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6004 | }else if( !zName ){ |
| 6005 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 6006 | assert(isDelete && !isNewJrnl); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 6007 | rc = unixGetTempname(pVfs->mxPathname, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 6008 | if( rc!=SQLITE_OK ){ |
| 6009 | return rc; |
| 6010 | } |
| 6011 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6012 | |
| 6013 | /* Generated temporary filenames are always double-zero terminated |
| 6014 | ** for use by sqlite3_uri_parameter(). */ |
| 6015 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6018 | /* Determine the value of the flags parameter passed to POSIX function |
| 6019 | ** open(). These must be calculated even if open() is not called, as |
| 6020 | ** they may be stored as part of the file handle and used by the |
| 6021 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6022 | if( isReadonly ) openFlags |= O_RDONLY; |
| 6023 | if( isReadWrite ) openFlags |= O_RDWR; |
| 6024 | if( isCreate ) openFlags |= O_CREAT; |
| 6025 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 6026 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6027 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6028 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6029 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6030 | uid_t uid; /* Userid for the file */ |
| 6031 | gid_t gid; /* Groupid for the file */ |
| 6032 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6033 | if( rc!=SQLITE_OK ){ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6034 | assert( !p->pPreallocatedUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 6035 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6036 | return rc; |
| 6037 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6038 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6039 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
drh | 5a2d970 | 2015-11-26 02:21:05 +0000 | [diff] [blame] | 6040 | assert( !isExclusive || (openFlags & O_CREAT)!=0 ); |
dan | a688ca5 | 2018-01-10 11:56:03 +0000 | [diff] [blame] | 6041 | if( fd<0 ){ |
| 6042 | if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){ |
| 6043 | /* If unable to create a journal because the directory is not |
| 6044 | ** writable, change the error code to indicate that. */ |
| 6045 | rc = SQLITE_READONLY_DIRECTORY; |
| 6046 | }else if( errno!=EISDIR && isReadWrite ){ |
| 6047 | /* Failed to open the file for read/write access. Try read-only. */ |
| 6048 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
| 6049 | openFlags &= ~(O_RDWR|O_CREAT); |
| 6050 | flags |= SQLITE_OPEN_READONLY; |
| 6051 | openFlags |= O_RDONLY; |
| 6052 | isReadonly = 1; |
| 6053 | fd = robust_open(zName, openFlags, openMode); |
| 6054 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6055 | } |
| 6056 | if( fd<0 ){ |
dan | a688ca5 | 2018-01-10 11:56:03 +0000 | [diff] [blame] | 6057 | int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
| 6058 | if( rc==SQLITE_OK ) rc = rc2; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6059 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6060 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6061 | |
drh | 1116b17 | 2019-09-25 10:36:31 +0000 | [diff] [blame^] | 6062 | /* The owner of the rollback journal or WAL file should always be the |
| 6063 | ** same as the owner of the database file. Try to ensure that this is |
| 6064 | ** the case. The chown() system call will be a no-op if the current |
| 6065 | ** process lacks root privileges, be we should at least try. Without |
| 6066 | ** this step, if a root process opens a database file, it can leave |
| 6067 | ** behinds a journal/WAL that is owned by root and hence make the |
| 6068 | ** database inaccessible to unprivileged processes. |
| 6069 | ** |
| 6070 | ** If openFlags==0, then that means uid and gid are not set correctly |
| 6071 | ** (probably because SQLite is configured to use 8+3 filename mode) and |
| 6072 | ** in that case we do not want to attempt the chown(). |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6073 | */ |
drh | 1116b17 | 2019-09-25 10:36:31 +0000 | [diff] [blame^] | 6074 | if( openFlags && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))!=0 ){ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 6075 | robustFchown(fd, uid, gid); |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6076 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6077 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6078 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6079 | if( pOutFlags ){ |
| 6080 | *pOutFlags = flags; |
| 6081 | } |
| 6082 | |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6083 | if( p->pPreallocatedUnused ){ |
| 6084 | p->pPreallocatedUnused->fd = fd; |
drh | 55220a6 | 2019-08-06 20:55:06 +0000 | [diff] [blame] | 6085 | p->pPreallocatedUnused->flags = |
| 6086 | flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6089 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6090 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6091 | zPath = zName; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 6092 | #elif defined(SQLITE_UNLINK_AFTER_CLOSE) |
| 6093 | zPath = sqlite3_mprintf("%s", zName); |
| 6094 | if( zPath==0 ){ |
| 6095 | robust_close(p, fd, __LINE__); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6096 | return SQLITE_NOMEM_BKPT; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 6097 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6098 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6099 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6100 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6101 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 6102 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 6103 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6104 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 6105 | } |
| 6106 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6107 | |
| 6108 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6109 | if( fstatfs(fd, &fsInfo) == -1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6110 | storeLastErrno(p, errno); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6111 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6112 | return SQLITE_IOERR_ACCESS; |
| 6113 | } |
| 6114 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 6115 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 6116 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6117 | if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) { |
| 6118 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 6119 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6120 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6121 | |
| 6122 | /* Set up appropriate ctrlFlags */ |
| 6123 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 6124 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 6125 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6126 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 6127 | if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6128 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 6129 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6130 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6131 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6132 | isAutoProxy = 1; |
| 6133 | #endif |
| 6134 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6135 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 6136 | int useProxy = 0; |
| 6137 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6138 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 6139 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6140 | if( envforce!=NULL ){ |
| 6141 | useProxy = atoi(envforce)>0; |
| 6142 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6143 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 6144 | } |
| 6145 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6146 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6147 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6148 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6149 | if( rc!=SQLITE_OK ){ |
| 6150 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 6151 | ** and clear all the structure's references. Specifically, |
| 6152 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 6153 | */ |
| 6154 | unixClose(pFile); |
| 6155 | return rc; |
| 6156 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6157 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6158 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6159 | } |
| 6160 | } |
| 6161 | #endif |
| 6162 | |
dan | 3ed0f1c | 2017-09-14 21:12:07 +0000 | [diff] [blame] | 6163 | assert( zPath==0 || zPath[0]=='/' |
| 6164 | || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 6165 | ); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6166 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 6167 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6168 | open_finished: |
| 6169 | if( rc!=SQLITE_OK ){ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6170 | sqlite3_free(p->pPreallocatedUnused); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6171 | } |
| 6172 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6173 | } |
| 6174 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6175 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6176 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6177 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 6178 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6179 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6180 | static int unixDelete( |
| 6181 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 6182 | const char *zPath, /* Name of file to be deleted */ |
| 6183 | int dirSync /* If true, fsync() directory after deleting file */ |
| 6184 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6185 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6186 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6187 | SimulateIOError(return SQLITE_IOERR_DELETE); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6188 | if( osUnlink(zPath)==(-1) ){ |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 6189 | if( errno==ENOENT |
| 6190 | #if OS_VXWORKS |
drh | 19541f3 | 2014-09-01 13:37:55 +0000 | [diff] [blame] | 6191 | || osAccess(zPath,0)!=0 |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 6192 | #endif |
| 6193 | ){ |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6194 | rc = SQLITE_IOERR_DELETE_NOENT; |
| 6195 | }else{ |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 6196 | rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6197 | } |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 6198 | return rc; |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 6199 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 6200 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 6201 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6202 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 6203 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6204 | if( rc==SQLITE_OK ){ |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 6205 | if( full_fsync(fd,0,0) ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6206 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6207 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6208 | robust_close(0, fd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 6209 | }else{ |
| 6210 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 6211 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6212 | } |
| 6213 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 6214 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6215 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6216 | } |
| 6217 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6218 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 6219 | ** Test the existence of or access permissions of file zPath. The |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6220 | ** test performed depends on the value of flags: |
| 6221 | ** |
| 6222 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 6223 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 6224 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 6225 | ** |
| 6226 | ** Otherwise return 0. |
| 6227 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6228 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6229 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 6230 | const char *zPath, /* Path of the file to examine */ |
| 6231 | int flags, /* What do we want to learn about the zPath file? */ |
| 6232 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6233 | ){ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6234 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6235 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6236 | assert( pResOut!=0 ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6237 | |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6238 | /* The spec says there are three possible values for flags. But only |
| 6239 | ** two of them are actually used */ |
| 6240 | assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE ); |
| 6241 | |
| 6242 | if( flags==SQLITE_ACCESS_EXISTS ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6243 | struct stat buf; |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6244 | *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0); |
| 6245 | }else{ |
| 6246 | *pResOut = osAccess(zPath, W_OK|R_OK)==0; |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6247 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6248 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6249 | } |
| 6250 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6251 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6252 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6253 | */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6254 | static int mkFullPathname( |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6255 | const char *zPath, /* Input path */ |
| 6256 | char *zOut, /* Output buffer */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6257 | int nOut /* Allocated size of buffer zOut */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 6258 | ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6259 | int nPath = sqlite3Strlen30(zPath); |
| 6260 | int iOff = 0; |
| 6261 | if( zPath[0]!='/' ){ |
| 6262 | if( osGetcwd(zOut, nOut-2)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6263 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6264 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6265 | iOff = sqlite3Strlen30(zOut); |
| 6266 | zOut[iOff++] = '/'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6267 | } |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6268 | if( (iOff+nPath+1)>nOut ){ |
| 6269 | /* SQLite assumes that xFullPathname() nul-terminates the output buffer |
| 6270 | ** even if it returns an error. */ |
| 6271 | zOut[iOff] = '\0'; |
| 6272 | return SQLITE_CANTOPEN_BKPT; |
| 6273 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6274 | sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6275 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6276 | } |
| 6277 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6278 | /* |
| 6279 | ** Turn a relative pathname into a full pathname. The relative path |
| 6280 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 6281 | ** zPath. |
| 6282 | ** |
| 6283 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 6284 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 6285 | ** this buffer before returning. |
| 6286 | */ |
| 6287 | static int unixFullPathname( |
| 6288 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 6289 | const char *zPath, /* Possibly relative input path */ |
| 6290 | int nOut, /* Size of output buffer in bytes */ |
| 6291 | char *zOut /* Output buffer */ |
| 6292 | ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6293 | #if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT) |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6294 | return mkFullPathname(zPath, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6295 | #else |
| 6296 | int rc = SQLITE_OK; |
| 6297 | int nByte; |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6298 | int nLink = 1; /* Number of symbolic links followed so far */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6299 | const char *zIn = zPath; /* Input path for each iteration of loop */ |
| 6300 | char *zDel = 0; |
| 6301 | |
| 6302 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
| 6303 | UNUSED_PARAMETER(pVfs); |
| 6304 | |
| 6305 | /* It's odd to simulate an io-error here, but really this is just |
| 6306 | ** using the io-error infrastructure to test that SQLite handles this |
| 6307 | ** function failing. This function could fail if, for example, the |
| 6308 | ** current working directory has been unlinked. |
| 6309 | */ |
| 6310 | SimulateIOError( return SQLITE_ERROR ); |
| 6311 | |
| 6312 | do { |
| 6313 | |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6314 | /* Call stat() on path zIn. Set bLink to true if the path is a symbolic |
| 6315 | ** link, or false otherwise. */ |
| 6316 | int bLink = 0; |
| 6317 | struct stat buf; |
| 6318 | if( osLstat(zIn, &buf)!=0 ){ |
| 6319 | if( errno!=ENOENT ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6320 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6321 | } |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6322 | }else{ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6323 | bLink = S_ISLNK(buf.st_mode); |
| 6324 | } |
| 6325 | |
| 6326 | if( bLink ){ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6327 | if( zDel==0 ){ |
| 6328 | zDel = sqlite3_malloc(nOut); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6329 | if( zDel==0 ) rc = SQLITE_NOMEM_BKPT; |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6330 | }else if( ++nLink>SQLITE_MAX_SYMLINKS ){ |
| 6331 | rc = SQLITE_CANTOPEN_BKPT; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6332 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6333 | |
| 6334 | if( rc==SQLITE_OK ){ |
| 6335 | nByte = osReadlink(zIn, zDel, nOut-1); |
| 6336 | if( nByte<0 ){ |
| 6337 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6338 | }else{ |
| 6339 | if( zDel[0]!='/' ){ |
| 6340 | int n; |
| 6341 | for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); |
| 6342 | if( nByte+n+1>nOut ){ |
| 6343 | rc = SQLITE_CANTOPEN_BKPT; |
| 6344 | }else{ |
| 6345 | memmove(&zDel[n], zDel, nByte+1); |
| 6346 | memcpy(zDel, zIn, n); |
| 6347 | nByte += n; |
| 6348 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6349 | } |
| 6350 | zDel[nByte] = '\0'; |
| 6351 | } |
| 6352 | } |
| 6353 | |
| 6354 | zIn = zDel; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6357 | assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' ); |
| 6358 | if( rc==SQLITE_OK && zIn!=zOut ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6359 | rc = mkFullPathname(zIn, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6360 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6361 | if( bLink==0 ) break; |
| 6362 | zIn = zOut; |
| 6363 | }while( rc==SQLITE_OK ); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6364 | |
| 6365 | sqlite3_free(zDel); |
| 6366 | return rc; |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6367 | #endif /* HAVE_READLINK && HAVE_LSTAT */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6368 | } |
| 6369 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 6370 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6371 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 6372 | /* |
| 6373 | ** Interfaces for opening a shared library, finding entry points |
| 6374 | ** within the shared library, and closing the shared library. |
| 6375 | */ |
| 6376 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6377 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 6378 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6379 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 6380 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 6381 | |
| 6382 | /* |
| 6383 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 6384 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 6385 | ** message is available, it is written to zBufOut. If no error message |
| 6386 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 6387 | ** error message. |
| 6388 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6389 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 6390 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6391 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6392 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6393 | zErr = dlerror(); |
| 6394 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6395 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6396 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6397 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6398 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6399 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 6400 | /* |
| 6401 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 6402 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 6403 | ** returns a void* which is really a pointer to a function. So how do we |
| 6404 | ** use dlsym() with -pedantic-errors? |
| 6405 | ** |
| 6406 | ** Variable x below is defined to be a pointer to a function taking |
| 6407 | ** parameters void* and const char* and returning a pointer to a function. |
| 6408 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 6409 | ** (That assignment requires a cast.) Then we call the function that |
| 6410 | ** x points to. |
| 6411 | ** |
| 6412 | ** This work-around is unlikely to work correctly on any system where |
| 6413 | ** you really cannot cast a function pointer into void*. But then, on the |
| 6414 | ** other hand, dlsym() will not work on such a system either, so we have |
| 6415 | ** not really lost anything. |
| 6416 | */ |
| 6417 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6418 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6419 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 6420 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6421 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6422 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 6423 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6424 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6425 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6426 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 6427 | #define unixDlOpen 0 |
| 6428 | #define unixDlError 0 |
| 6429 | #define unixDlSym 0 |
| 6430 | #define unixDlClose 0 |
| 6431 | #endif |
| 6432 | |
| 6433 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6434 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6435 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6436 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 6437 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 6438 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6439 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6440 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 6441 | ** errors. The reports issued by valgrind are incorrect - we would |
| 6442 | ** prefer that the randomness be increased by making use of the |
| 6443 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 6444 | ** some users. Rather than argue, it seems easier just to initialize |
| 6445 | ** the whole array and silence valgrind, even if that means less randomness |
| 6446 | ** in the random seed. |
| 6447 | ** |
| 6448 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 6449 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6450 | ** tests repeatable. |
| 6451 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6452 | memset(zBuf, 0, nBuf); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6453 | randomnessPid = osGetpid(0); |
drh | 6a412b8 | 2015-04-30 12:31:49 +0000 | [diff] [blame] | 6454 | #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6455 | { |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6456 | int fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6457 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6458 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 6459 | time_t t; |
| 6460 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6461 | memcpy(zBuf, &t, sizeof(t)); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6462 | memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); |
| 6463 | assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); |
| 6464 | nBuf = sizeof(t) + sizeof(randomnessPid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6465 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 6466 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6467 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6468 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6469 | } |
| 6470 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 6471 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6474 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6475 | /* |
| 6476 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6477 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6478 | ** The return value is the number of microseconds of sleep actually |
| 6479 | ** requested from the underlying operating system, a number which |
| 6480 | ** might be greater than or equal to the argument, but not less |
| 6481 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6482 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6483 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6484 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6485 | struct timespec sp; |
| 6486 | |
| 6487 | sp.tv_sec = microseconds / 1000000; |
| 6488 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 6489 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6490 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6491 | return microseconds; |
| 6492 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6493 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6494 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6495 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6496 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6497 | int seconds = (microseconds+999999)/1000000; |
| 6498 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6499 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6500 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 6501 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 6502 | } |
| 6503 | |
| 6504 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6505 | ** The following variable, if set to a non-zero value, is interpreted as |
| 6506 | ** the number of seconds since 1970 and is used to set the result of |
| 6507 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6508 | */ |
| 6509 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6510 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6511 | #endif |
| 6512 | |
| 6513 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6514 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 6515 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 6516 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 6517 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 6518 | ** proleptic Gregorian calendar. |
| 6519 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6520 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 6521 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6522 | */ |
| 6523 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 6524 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6525 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6526 | #if defined(NO_GETTOD) |
| 6527 | time_t t; |
| 6528 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 6529 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6530 | #elif OS_VXWORKS |
| 6531 | struct timespec sNow; |
| 6532 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 6533 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 6534 | #else |
| 6535 | struct timeval sNow; |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 6536 | (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ |
| 6537 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6538 | #endif |
| 6539 | |
| 6540 | #ifdef SQLITE_TEST |
| 6541 | if( sqlite3_current_time ){ |
| 6542 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 6543 | } |
| 6544 | #endif |
| 6545 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6546 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6547 | } |
| 6548 | |
drh | c3dfa5e | 2016-01-22 19:44:03 +0000 | [diff] [blame] | 6549 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6550 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6551 | ** Find the current time (in Universal Coordinated Time). Write the |
| 6552 | ** current time and date as a Julian Day number into *prNow and |
| 6553 | ** return 0. Return 1 if the time and date cannot be found. |
| 6554 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6555 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 6556 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6557 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 6558 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6559 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 6560 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6561 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6562 | } |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 6563 | #else |
| 6564 | # define unixCurrentTime 0 |
| 6565 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6566 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6567 | /* |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6568 | ** The xGetLastError() method is designed to return a better |
| 6569 | ** low-level error message when operating-system problems come up |
| 6570 | ** during SQLite operation. Only the integer return code is currently |
| 6571 | ** used. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6572 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6573 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 6574 | UNUSED_PARAMETER(NotUsed); |
| 6575 | UNUSED_PARAMETER(NotUsed2); |
| 6576 | UNUSED_PARAMETER(NotUsed3); |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6577 | return errno; |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 6578 | } |
| 6579 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6580 | |
| 6581 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6582 | ************************ End of sqlite3_vfs methods *************************** |
| 6583 | ******************************************************************************/ |
| 6584 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6585 | /****************************************************************************** |
| 6586 | ************************** Begin Proxy Locking ******************************** |
| 6587 | ** |
| 6588 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 6589 | ** other locking methods on secondary lock files. Proxy locking is a |
| 6590 | ** meta-layer over top of the primitive locking implemented above. For |
| 6591 | ** this reason, the division that implements of proxy locking is deferred |
| 6592 | ** until late in the file (here) after all of the other I/O methods have |
| 6593 | ** been defined - so that the primitive locking methods are available |
| 6594 | ** as services to help with the implementation of proxy locking. |
| 6595 | ** |
| 6596 | **** |
| 6597 | ** |
| 6598 | ** The default locking schemes in SQLite use byte-range locks on the |
| 6599 | ** database file to coordinate safe, concurrent access by multiple readers |
| 6600 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 6601 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 6602 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 6603 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 6604 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 6605 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 6606 | ** address in the shared range is taken for a SHARED lock, the entire |
| 6607 | ** shared range is taken for an EXCLUSIVE lock): |
| 6608 | ** |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 6609 | ** PENDING_BYTE 0x40000000 |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6610 | ** RESERVED_BYTE 0x40000001 |
| 6611 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 6612 | ** |
| 6613 | ** This works well on the local file system, but shows a nearly 100x |
| 6614 | ** slowdown in read performance on AFP because the AFP client disables |
| 6615 | ** the read cache when byte-range locks are present. Enabling the read |
| 6616 | ** cache exposes a cache coherency problem that is present on all OS X |
| 6617 | ** supported network file systems. NFS and AFP both observe the |
| 6618 | ** close-to-open semantics for ensuring cache coherency |
| 6619 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 6620 | ** address the requirements for concurrent database access by multiple |
| 6621 | ** readers and writers |
| 6622 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 6623 | ** |
| 6624 | ** To address the performance and cache coherency issues, proxy file locking |
| 6625 | ** changes the way database access is controlled by limiting access to a |
| 6626 | ** single host at a time and moving file locks off of the database file |
| 6627 | ** and onto a proxy file on the local file system. |
| 6628 | ** |
| 6629 | ** |
| 6630 | ** Using proxy locks |
| 6631 | ** ----------------- |
| 6632 | ** |
| 6633 | ** C APIs |
| 6634 | ** |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6635 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6636 | ** <proxy_path> | ":auto:"); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6637 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, |
| 6638 | ** &<proxy_path>); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6639 | ** |
| 6640 | ** |
| 6641 | ** SQL pragmas |
| 6642 | ** |
| 6643 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 6644 | ** PRAGMA [database.]lock_proxy_file |
| 6645 | ** |
| 6646 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 6647 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 6648 | ** a proxy path based on the user's temp dir |
| 6649 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 6650 | ** actual proxy file name is generated from the name and path of the |
| 6651 | ** database file. For example: |
| 6652 | ** |
| 6653 | ** For database path "/Users/me/foo.db" |
| 6654 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 6655 | ** |
| 6656 | ** Once a lock proxy is configured for a database connection, it can not |
| 6657 | ** be removed, however it may be switched to a different proxy path via |
| 6658 | ** the above APIs (assuming the conch file is not being held by another |
| 6659 | ** connection or process). |
| 6660 | ** |
| 6661 | ** |
| 6662 | ** How proxy locking works |
| 6663 | ** ----------------------- |
| 6664 | ** |
| 6665 | ** Proxy file locking relies primarily on two new supporting files: |
| 6666 | ** |
| 6667 | ** * conch file to limit access to the database file to a single host |
| 6668 | ** at a time |
| 6669 | ** |
| 6670 | ** * proxy file to act as a proxy for the advisory locks normally |
| 6671 | ** taken on the database |
| 6672 | ** |
| 6673 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 6674 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 6675 | ** contents and comparing the host's unique host ID (see below) and lock |
| 6676 | ** proxy path against the values stored in the conch. The conch file is |
| 6677 | ** stored in the same directory as the database file and the file name |
| 6678 | ** is patterned after the database file name as ".<databasename>-conch". |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 6679 | ** 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] | 6680 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 6681 | ** lock and the conch file contents is updated with the host ID and proxy |
| 6682 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 6683 | ** is held by another process (with a shared lock), the exclusive lock |
| 6684 | ** will fail and SQLITE_BUSY is returned. |
| 6685 | ** |
| 6686 | ** The proxy file - a single-byte file used for all advisory file locks |
| 6687 | ** normally taken on the database file. This allows for safe sharing |
| 6688 | ** of the database file for multiple readers and writers on the same |
| 6689 | ** host (the conch ensures that they all use the same local lock file). |
| 6690 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6691 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 6692 | ** only taken when the first request to lock database file is made. |
| 6693 | ** This matches the semantics of the traditional locking behavior, where |
| 6694 | ** opening a connection to a database file does not take a lock on it. |
| 6695 | ** The shared lock and an open file descriptor are maintained until |
| 6696 | ** the connection to the database is closed. |
| 6697 | ** |
| 6698 | ** The proxy file and the lock file are never deleted so they only need |
| 6699 | ** to be created the first time they are used. |
| 6700 | ** |
| 6701 | ** Configuration options |
| 6702 | ** --------------------- |
| 6703 | ** |
| 6704 | ** SQLITE_PREFER_PROXY_LOCKING |
| 6705 | ** |
| 6706 | ** Database files accessed on non-local file systems are |
| 6707 | ** automatically configured for proxy locking, lock files are |
| 6708 | ** named automatically using the same logic as |
| 6709 | ** PRAGMA lock_proxy_file=":auto:" |
| 6710 | ** |
| 6711 | ** SQLITE_PROXY_DEBUG |
| 6712 | ** |
| 6713 | ** Enables the logging of error messages during host id file |
| 6714 | ** retrieval and creation |
| 6715 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6716 | ** LOCKPROXYDIR |
| 6717 | ** |
| 6718 | ** Overrides the default directory used for lock proxy files that |
| 6719 | ** are named automatically via the ":auto:" setting |
| 6720 | ** |
| 6721 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6722 | ** |
| 6723 | ** Permissions to use when creating a directory for storing the |
| 6724 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 6725 | ** |
| 6726 | ** |
| 6727 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 6728 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 6729 | ** force proxy locking to be used for every database file opened, and 0 |
| 6730 | ** will force automatic proxy locking to be disabled for all database |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6731 | ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6732 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 6733 | */ |
| 6734 | |
| 6735 | /* |
| 6736 | ** Proxy locking is only available on MacOSX |
| 6737 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6738 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6739 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6740 | /* |
| 6741 | ** The proxyLockingContext has the path and file structures for the remote |
| 6742 | ** and local proxy files in it |
| 6743 | */ |
| 6744 | typedef struct proxyLockingContext proxyLockingContext; |
| 6745 | struct proxyLockingContext { |
| 6746 | unixFile *conchFile; /* Open conch file */ |
| 6747 | char *conchFilePath; /* Name of the conch file */ |
| 6748 | unixFile *lockProxy; /* Open proxy lock file */ |
| 6749 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 6750 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6751 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6752 | int nFails; /* Number of conch taking failures */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6753 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 6754 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 6755 | }; |
| 6756 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6757 | /* |
| 6758 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 6759 | ** which must point to valid, writable memory large enough for a maxLen length |
| 6760 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6761 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6762 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 6763 | int len; |
| 6764 | int dbLen; |
| 6765 | int i; |
| 6766 | |
| 6767 | #ifdef LOCKPROXYDIR |
| 6768 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 6769 | #else |
| 6770 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 6771 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6772 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6773 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6774 | lPath, errno, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6775 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6776 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6777 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6778 | } |
| 6779 | # else |
| 6780 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 6781 | # endif |
| 6782 | #endif |
| 6783 | |
| 6784 | if( lPath[len-1]!='/' ){ |
| 6785 | len = strlcat(lPath, "/", maxLen); |
| 6786 | } |
| 6787 | |
| 6788 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6789 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6790 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6791 | char c = dbPath[i]; |
| 6792 | lPath[i+len] = (c=='/')?'_':c; |
| 6793 | } |
| 6794 | lPath[i+len]='\0'; |
| 6795 | strlcat(lPath, ":auto:", maxLen); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6796 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6797 | return SQLITE_OK; |
| 6798 | } |
| 6799 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6800 | /* |
| 6801 | ** Creates the lock file and any missing directories in lockPath |
| 6802 | */ |
| 6803 | static int proxyCreateLockPath(const char *lockPath){ |
| 6804 | int i, len; |
| 6805 | char buf[MAXPATHLEN]; |
| 6806 | int start = 0; |
| 6807 | |
| 6808 | assert(lockPath!=NULL); |
| 6809 | /* try to create all the intermediate directories */ |
| 6810 | len = (int)strlen(lockPath); |
| 6811 | buf[0] = lockPath[0]; |
| 6812 | for( i=1; i<len; i++ ){ |
| 6813 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 6814 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 6815 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 6816 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 6817 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 6818 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6819 | int err=errno; |
| 6820 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6821 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6822 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6823 | buf, strerror(err), lockPath, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6824 | return err; |
| 6825 | } |
| 6826 | } |
| 6827 | } |
| 6828 | start=i+1; |
| 6829 | } |
| 6830 | buf[i] = lockPath[i]; |
| 6831 | } |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 6832 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6833 | return 0; |
| 6834 | } |
| 6835 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6836 | /* |
| 6837 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 6838 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 6839 | ** |
| 6840 | ** The caller is responsible not only for closing the file descriptor |
| 6841 | ** but also for freeing the memory associated with the file descriptor. |
| 6842 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6843 | static int proxyCreateUnixFile( |
| 6844 | const char *path, /* path for the new unixFile */ |
| 6845 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 6846 | int islockfile /* if non zero missing dirs will be created */ |
| 6847 | ) { |
| 6848 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6849 | unixFile *pNew; |
| 6850 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6851 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6852 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6853 | int terrno = 0; |
| 6854 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6855 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6856 | /* 1. first try to open/create the file |
| 6857 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 6858 | ** the parent directories and then try again. |
| 6859 | ** 3. if that fails, try to open the file read-only |
| 6860 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 6861 | */ |
| 6862 | pUnused = findReusableFd(path, openFlags); |
| 6863 | if( pUnused ){ |
| 6864 | fd = pUnused->fd; |
| 6865 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6866 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6867 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6868 | return SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6869 | } |
| 6870 | } |
| 6871 | if( fd<0 ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6872 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6873 | terrno = errno; |
| 6874 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 6875 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6876 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6877 | } |
| 6878 | } |
| 6879 | } |
| 6880 | if( fd<0 ){ |
| 6881 | openFlags = O_RDONLY; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6882 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6883 | terrno = errno; |
| 6884 | } |
| 6885 | if( fd<0 ){ |
| 6886 | if( islockfile ){ |
| 6887 | return SQLITE_BUSY; |
| 6888 | } |
| 6889 | switch (terrno) { |
| 6890 | case EACCES: |
| 6891 | return SQLITE_PERM; |
| 6892 | case EIO: |
| 6893 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 6894 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6895 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6896 | } |
| 6897 | } |
| 6898 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6899 | pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6900 | if( pNew==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6901 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6902 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6903 | } |
| 6904 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6905 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6906 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6907 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6908 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6909 | pUnused->fd = fd; |
| 6910 | pUnused->flags = openFlags; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6911 | pNew->pPreallocatedUnused = pUnused; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6912 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6913 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6914 | if( rc==SQLITE_OK ){ |
| 6915 | *ppFile = pNew; |
| 6916 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6917 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6918 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6919 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6920 | sqlite3_free(pNew); |
| 6921 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6922 | return rc; |
| 6923 | } |
| 6924 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6925 | #ifdef SQLITE_TEST |
| 6926 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6927 | int sqlite3_hostid_num = 0; |
| 6928 | #endif |
| 6929 | |
| 6930 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 6931 | |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6932 | #ifdef HAVE_GETHOSTUUID |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6933 | /* Not always defined in the headers as it ought to be */ |
| 6934 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6935 | #endif |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6936 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6937 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 6938 | ** bytes of writable memory. |
| 6939 | */ |
| 6940 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6941 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 6942 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6943 | #ifdef HAVE_GETHOSTUUID |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6944 | { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6945 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6946 | if( gethostuuid(pHostID, &timeout) ){ |
| 6947 | int err = errno; |
| 6948 | if( pError ){ |
| 6949 | *pError = err; |
| 6950 | } |
| 6951 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6952 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6953 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6954 | #else |
| 6955 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6956 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6957 | #ifdef SQLITE_TEST |
| 6958 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6959 | if( sqlite3_hostid_num != 0){ |
| 6960 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 6961 | } |
| 6962 | #endif |
| 6963 | |
| 6964 | return SQLITE_OK; |
| 6965 | } |
| 6966 | |
| 6967 | /* The conch file contains the header, host id and lock file path |
| 6968 | */ |
| 6969 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 6970 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 6971 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 6972 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 6973 | |
| 6974 | /* |
| 6975 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 6976 | ** it back. The newly created file's file descriptor is assigned to the |
| 6977 | ** conch file structure and finally the original conch file descriptor is |
| 6978 | ** closed. Returns zero if successful. |
| 6979 | */ |
| 6980 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 6981 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6982 | unixFile *conchFile = pCtx->conchFile; |
| 6983 | char tPath[MAXPATHLEN]; |
| 6984 | char buf[PROXY_MAXCONCHLEN]; |
| 6985 | char *cPath = pCtx->conchFilePath; |
| 6986 | size_t readLen = 0; |
| 6987 | size_t pathLen = 0; |
| 6988 | char errmsg[64] = ""; |
| 6989 | int fd = -1; |
| 6990 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6991 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6992 | |
| 6993 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 6994 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 6995 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 6996 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6997 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6998 | goto end_breaklock; |
| 6999 | } |
| 7000 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7001 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7002 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7003 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7004 | goto end_breaklock; |
| 7005 | } |
| 7006 | /* write it out to the temporary break file */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7007 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7008 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7009 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7010 | goto end_breaklock; |
| 7011 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7012 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7013 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7014 | goto end_breaklock; |
| 7015 | } |
| 7016 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7017 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7018 | goto end_breaklock; |
| 7019 | } |
| 7020 | rc = 0; |
| 7021 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 7022 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7023 | conchFile->h = fd; |
| 7024 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 7025 | |
| 7026 | end_breaklock: |
| 7027 | if( rc ){ |
| 7028 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 7029 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 7030 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7031 | } |
| 7032 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 7033 | } |
| 7034 | return rc; |
| 7035 | } |
| 7036 | |
| 7037 | /* Take the requested lock on the conch file and break a stale lock if the |
| 7038 | ** host id matches. |
| 7039 | */ |
| 7040 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 7041 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7042 | unixFile *conchFile = pCtx->conchFile; |
| 7043 | int rc = SQLITE_OK; |
| 7044 | int nTries = 0; |
| 7045 | struct timespec conchModTime; |
| 7046 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 7047 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7048 | do { |
| 7049 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 7050 | nTries ++; |
| 7051 | if( rc==SQLITE_BUSY ){ |
| 7052 | /* If the lock failed (busy): |
| 7053 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 7054 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 7055 | * 10 sec and try again |
| 7056 | * 3rd try: break the lock unless the mod time has changed. |
| 7057 | */ |
| 7058 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7059 | if( osFstat(conchFile->h, &buf) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7060 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7061 | return SQLITE_IOERR_LOCK; |
| 7062 | } |
| 7063 | |
| 7064 | if( nTries==1 ){ |
| 7065 | conchModTime = buf.st_mtimespec; |
| 7066 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 7067 | continue; |
| 7068 | } |
| 7069 | |
| 7070 | assert( nTries>1 ); |
| 7071 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 7072 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 7073 | return SQLITE_BUSY; |
| 7074 | } |
| 7075 | |
| 7076 | if( nTries==2 ){ |
| 7077 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7078 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7079 | if( len<0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7080 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7081 | return SQLITE_IOERR_LOCK; |
| 7082 | } |
| 7083 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 7084 | /* don't break the lock if the host id doesn't match */ |
| 7085 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 7086 | return SQLITE_BUSY; |
| 7087 | } |
| 7088 | }else{ |
| 7089 | /* don't break the lock on short read or a version mismatch */ |
| 7090 | return SQLITE_BUSY; |
| 7091 | } |
| 7092 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 7093 | continue; |
| 7094 | } |
| 7095 | |
| 7096 | assert( nTries==3 ); |
| 7097 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 7098 | rc = SQLITE_OK; |
| 7099 | if( lockType==EXCLUSIVE_LOCK ){ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 7100 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7101 | } |
| 7102 | if( !rc ){ |
| 7103 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 7104 | } |
| 7105 | } |
| 7106 | } |
| 7107 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 7108 | |
| 7109 | return rc; |
| 7110 | } |
| 7111 | |
| 7112 | /* 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] | 7113 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 7114 | ** lockPath means that the lockPath in the conch file will be used if the |
| 7115 | ** host IDs match, or a new lock path will be generated automatically |
| 7116 | ** and written to the conch file. |
| 7117 | */ |
| 7118 | static int proxyTakeConch(unixFile *pFile){ |
| 7119 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7120 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7121 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7122 | return SQLITE_OK; |
| 7123 | }else{ |
| 7124 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7125 | uuid_t myHostID; |
| 7126 | int pError = 0; |
| 7127 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7128 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7129 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7130 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7131 | int createConch = 0; |
| 7132 | int hostIdMatch = 0; |
| 7133 | int readLen = 0; |
| 7134 | int tryOldLockPath = 0; |
| 7135 | int forceNewLockPath = 0; |
| 7136 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7137 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 7138 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7139 | osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7140 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7141 | rc = proxyGetHostID(myHostID, &pError); |
| 7142 | if( (rc&0xff)==SQLITE_IOERR ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7143 | storeLastErrno(pFile, pError); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7144 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7145 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7146 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7147 | if( rc!=SQLITE_OK ){ |
| 7148 | goto end_takeconch; |
| 7149 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7150 | /* read the existing conch file */ |
| 7151 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 7152 | if( readLen<0 ){ |
| 7153 | /* I/O error: lastErrno set by seekAndRead */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7154 | storeLastErrno(pFile, conchFile->lastErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7155 | rc = SQLITE_IOERR_READ; |
| 7156 | goto end_takeconch; |
| 7157 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 7158 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 7159 | /* a short read or version format mismatch means we need to create a new |
| 7160 | ** conch file. |
| 7161 | */ |
| 7162 | createConch = 1; |
| 7163 | } |
| 7164 | /* if the host id matches and the lock path already exists in the conch |
| 7165 | ** we'll try to use the path there, if we can't open that path, we'll |
| 7166 | ** retry with a new auto-generated path |
| 7167 | */ |
| 7168 | do { /* in case we need to try again for an :auto: named lock file */ |
| 7169 | |
| 7170 | if( !createConch && !forceNewLockPath ){ |
| 7171 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 7172 | PROXY_HOSTIDLEN); |
| 7173 | /* if the conch has data compare the contents */ |
| 7174 | if( !pCtx->lockProxyPath ){ |
| 7175 | /* for auto-named local lock file, just check the host ID and we'll |
| 7176 | ** use the local lock file path that's already in there |
| 7177 | */ |
| 7178 | if( hostIdMatch ){ |
| 7179 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 7180 | |
| 7181 | if( pathLen>=MAXPATHLEN ){ |
| 7182 | pathLen=MAXPATHLEN-1; |
| 7183 | } |
| 7184 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 7185 | lockPath[pathLen] = 0; |
| 7186 | tempLockPath = lockPath; |
| 7187 | tryOldLockPath = 1; |
| 7188 | /* create a copy of the lock path if the conch is taken */ |
| 7189 | goto end_takeconch; |
| 7190 | } |
| 7191 | }else if( hostIdMatch |
| 7192 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 7193 | readLen-PROXY_PATHINDEX) |
| 7194 | ){ |
| 7195 | /* conch host and lock path match */ |
| 7196 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7197 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7198 | } |
| 7199 | |
| 7200 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 7201 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 7202 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7203 | goto end_takeconch; |
| 7204 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7205 | |
| 7206 | /* 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] | 7207 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7208 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 7209 | tempLockPath = lockPath; |
| 7210 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7211 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7212 | |
| 7213 | /* update conch with host and path (this will fail if other process |
| 7214 | ** has a shared lock already), if the host id matches, use the big |
| 7215 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7216 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7217 | futimes(conchFile->h, NULL); |
| 7218 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 7219 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7220 | /* We are trying for an exclusive lock but another thread in this |
| 7221 | ** same process is still holding a shared lock. */ |
| 7222 | rc = SQLITE_BUSY; |
| 7223 | } else { |
| 7224 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7225 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7226 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7227 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7228 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7229 | if( rc==SQLITE_OK ){ |
| 7230 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 7231 | int writeSize = 0; |
| 7232 | |
| 7233 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 7234 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 7235 | if( pCtx->lockProxyPath!=NULL ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7236 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, |
| 7237 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7238 | }else{ |
| 7239 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 7240 | } |
| 7241 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7242 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7243 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 7244 | full_fsync(conchFile->h,0,0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7245 | /* If we created a new conch file (not just updated the contents of a |
| 7246 | ** valid conch file), try to match the permissions of the database |
| 7247 | */ |
| 7248 | if( rc==SQLITE_OK && createConch ){ |
| 7249 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7250 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7251 | if( err==0 ){ |
| 7252 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 7253 | S_IROTH|S_IWOTH); |
| 7254 | /* try to match the database file R/W permissions, ignore failure */ |
| 7255 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7256 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7257 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7258 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7259 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7260 | }while( rc==(-1) && errno==EINTR ); |
| 7261 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7262 | int code = errno; |
| 7263 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 7264 | cmode, code, strerror(code)); |
| 7265 | } else { |
| 7266 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 7267 | } |
| 7268 | }else{ |
| 7269 | int code = errno; |
| 7270 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 7271 | err, code, strerror(code)); |
| 7272 | #endif |
| 7273 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7274 | } |
| 7275 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7276 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 7277 | |
| 7278 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7279 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7280 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 7281 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7282 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 7283 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7284 | } |
| 7285 | pFile->h = -1; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7286 | fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7287 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7288 | if( fd>=0 ){ |
| 7289 | pFile->h = fd; |
| 7290 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 7291 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7292 | during locking */ |
| 7293 | } |
| 7294 | } |
| 7295 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 7296 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 7297 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 7298 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 7299 | /* we couldn't create the proxy lock file with the old lock file path |
| 7300 | ** so try again via auto-naming |
| 7301 | */ |
| 7302 | forceNewLockPath = 1; |
| 7303 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 7304 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7305 | } |
| 7306 | } |
| 7307 | if( rc==SQLITE_OK ){ |
| 7308 | /* Need to make a copy of path if we extracted the value |
| 7309 | ** from the conch file or the path was allocated on the stack |
| 7310 | */ |
| 7311 | if( tempLockPath ){ |
| 7312 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 7313 | if( !pCtx->lockProxyPath ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7314 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7315 | } |
| 7316 | } |
| 7317 | } |
| 7318 | if( rc==SQLITE_OK ){ |
| 7319 | pCtx->conchHeld = 1; |
| 7320 | |
| 7321 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 7322 | afpLockingContext *afpCtx; |
| 7323 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 7324 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 7325 | } |
| 7326 | } else { |
| 7327 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7328 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7329 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 7330 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7331 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7332 | } while (1); /* in case we need to retry the :auto: lock file - |
| 7333 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7334 | } |
| 7335 | } |
| 7336 | |
| 7337 | /* |
| 7338 | ** If pFile holds a lock on a conch file, then release that lock. |
| 7339 | */ |
| 7340 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 7341 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7342 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 7343 | unixFile *conchFile; /* Name of the conch file */ |
| 7344 | |
| 7345 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7346 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7347 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7348 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7349 | osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7350 | if( pCtx->conchHeld>0 ){ |
| 7351 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7352 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7353 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7354 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 7355 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7356 | return rc; |
| 7357 | } |
| 7358 | |
| 7359 | /* |
| 7360 | ** 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] | 7361 | ** Store the conch filename in memory obtained from sqlite3_malloc64(). |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7362 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 7363 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 7364 | ** |
| 7365 | ** The caller is responsible for ensuring that the allocated memory |
| 7366 | ** space is eventually freed. |
| 7367 | ** |
| 7368 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 7369 | */ |
| 7370 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 7371 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7372 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7373 | char *conchPath; /* buffer in which to construct conch name */ |
| 7374 | |
| 7375 | /* Allocate space for the conch filename and initialize the name to |
| 7376 | ** the name of the original database file. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7377 | *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7378 | if( conchPath==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7379 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7380 | } |
| 7381 | memcpy(conchPath, dbPath, len+1); |
| 7382 | |
| 7383 | /* now insert a "." before the last / character */ |
| 7384 | for( i=(len-1); i>=0; i-- ){ |
| 7385 | if( conchPath[i]=='/' ){ |
| 7386 | i++; |
| 7387 | break; |
| 7388 | } |
| 7389 | } |
| 7390 | conchPath[i]='.'; |
| 7391 | while ( i<len ){ |
| 7392 | conchPath[i+1]=dbPath[i]; |
| 7393 | i++; |
| 7394 | } |
| 7395 | |
| 7396 | /* append the "-conch" suffix to the file */ |
| 7397 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7398 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7399 | |
| 7400 | return SQLITE_OK; |
| 7401 | } |
| 7402 | |
| 7403 | |
| 7404 | /* Takes a fully configured proxy locking-style unix file and switches |
| 7405 | ** the local lock file path |
| 7406 | */ |
| 7407 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 7408 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7409 | char *oldPath = pCtx->lockProxyPath; |
| 7410 | int rc = SQLITE_OK; |
| 7411 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7412 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7413 | return SQLITE_BUSY; |
| 7414 | } |
| 7415 | |
| 7416 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 7417 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 7418 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 7419 | return SQLITE_OK; |
| 7420 | }else{ |
| 7421 | unixFile *lockProxy = pCtx->lockProxy; |
| 7422 | pCtx->lockProxy=NULL; |
| 7423 | pCtx->conchHeld = 0; |
| 7424 | if( lockProxy!=NULL ){ |
| 7425 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 7426 | if( rc ) return rc; |
| 7427 | sqlite3_free(lockProxy); |
| 7428 | } |
| 7429 | sqlite3_free(oldPath); |
| 7430 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 7431 | } |
| 7432 | |
| 7433 | return rc; |
| 7434 | } |
| 7435 | |
| 7436 | /* |
| 7437 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 7438 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 7439 | ** |
| 7440 | ** This routine find the filename associated with pFile and writes it |
| 7441 | ** int dbPath. |
| 7442 | */ |
| 7443 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7444 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7445 | if( pFile->pMethod == &afpIoMethods ){ |
| 7446 | /* afp style keeps a reference to the db path in the filePath field |
| 7447 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7448 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7449 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, |
| 7450 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7451 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7452 | #endif |
| 7453 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 7454 | /* dot lock style uses the locking context to store the dot lock |
| 7455 | ** file path */ |
| 7456 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 7457 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 7458 | }else{ |
| 7459 | /* all other styles use the locking context to store the db file path */ |
| 7460 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7461 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7462 | } |
| 7463 | return SQLITE_OK; |
| 7464 | } |
| 7465 | |
| 7466 | /* |
| 7467 | ** Takes an already filled in unix file and alters it so all file locking |
| 7468 | ** will be performed on the local proxy lock file. The following fields |
| 7469 | ** are preserved in the locking context so that they can be restored and |
| 7470 | ** the unix structure properly cleaned up at close time: |
| 7471 | ** ->lockingContext |
| 7472 | ** ->pMethod |
| 7473 | */ |
| 7474 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 7475 | proxyLockingContext *pCtx; |
| 7476 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 7477 | char *lockPath=NULL; |
| 7478 | int rc = SQLITE_OK; |
| 7479 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7480 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7481 | return SQLITE_BUSY; |
| 7482 | } |
| 7483 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 7484 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 7485 | lockPath=NULL; |
| 7486 | }else{ |
| 7487 | lockPath=(char *)path; |
| 7488 | } |
| 7489 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7490 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7491 | (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7492 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7493 | pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7494 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7495 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7496 | } |
| 7497 | memset(pCtx, 0, sizeof(*pCtx)); |
| 7498 | |
| 7499 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 7500 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7501 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 7502 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 7503 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 7504 | ** (c) the file system is read-only, then enable no-locking access. |
| 7505 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 7506 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 7507 | */ |
| 7508 | struct statfs fsInfo; |
| 7509 | struct stat conchInfo; |
| 7510 | int goLockless = 0; |
| 7511 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7512 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7513 | int err = errno; |
| 7514 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 7515 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 7516 | } |
| 7517 | } |
| 7518 | if( goLockless ){ |
| 7519 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 7520 | rc = SQLITE_OK; |
| 7521 | } |
| 7522 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7523 | } |
| 7524 | if( rc==SQLITE_OK && lockPath ){ |
| 7525 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 7526 | } |
| 7527 | |
| 7528 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7529 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 7530 | if( pCtx->dbPath==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7531 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7532 | } |
| 7533 | } |
| 7534 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7535 | /* all memory is allocated, proxys are created and assigned, |
| 7536 | ** switch the locking context and pMethod then return. |
| 7537 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7538 | pCtx->oldLockingContext = pFile->lockingContext; |
| 7539 | pFile->lockingContext = pCtx; |
| 7540 | pCtx->pOldMethod = pFile->pMethod; |
| 7541 | pFile->pMethod = &proxyIoMethods; |
| 7542 | }else{ |
| 7543 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7544 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7545 | sqlite3_free(pCtx->conchFile); |
| 7546 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7547 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7548 | sqlite3_free(pCtx->conchFilePath); |
| 7549 | sqlite3_free(pCtx); |
| 7550 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7551 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 7552 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7553 | return rc; |
| 7554 | } |
| 7555 | |
| 7556 | |
| 7557 | /* |
| 7558 | ** This routine handles sqlite3_file_control() calls that are specific |
| 7559 | ** to proxy locking. |
| 7560 | */ |
| 7561 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 7562 | switch( op ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7563 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7564 | unixFile *pFile = (unixFile*)id; |
| 7565 | if( pFile->pMethod == &proxyIoMethods ){ |
| 7566 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7567 | proxyTakeConch(pFile); |
| 7568 | if( pCtx->lockProxyPath ){ |
| 7569 | *(const char **)pArg = pCtx->lockProxyPath; |
| 7570 | }else{ |
| 7571 | *(const char **)pArg = ":auto: (not held)"; |
| 7572 | } |
| 7573 | } else { |
| 7574 | *(const char **)pArg = NULL; |
| 7575 | } |
| 7576 | return SQLITE_OK; |
| 7577 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7578 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7579 | unixFile *pFile = (unixFile*)id; |
| 7580 | int rc = SQLITE_OK; |
| 7581 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 7582 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 7583 | if( isProxyStyle ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7584 | /* turn off proxy locking - not supported. If support is added for |
| 7585 | ** switching proxy locking mode off then it will need to fail if |
| 7586 | ** the journal mode is WAL mode. |
| 7587 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7588 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 7589 | }else{ |
| 7590 | /* turn off proxy locking - already off - NOOP */ |
| 7591 | rc = SQLITE_OK; |
| 7592 | } |
| 7593 | }else{ |
| 7594 | const char *proxyPath = (const char *)pArg; |
| 7595 | if( isProxyStyle ){ |
| 7596 | proxyLockingContext *pCtx = |
| 7597 | (proxyLockingContext*)pFile->lockingContext; |
| 7598 | if( !strcmp(pArg, ":auto:") |
| 7599 | || (pCtx->lockProxyPath && |
| 7600 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 7601 | ){ |
| 7602 | rc = SQLITE_OK; |
| 7603 | }else{ |
| 7604 | rc = switchLockProxyPath(pFile, proxyPath); |
| 7605 | } |
| 7606 | }else{ |
| 7607 | /* turn on proxy file locking */ |
| 7608 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 7609 | } |
| 7610 | } |
| 7611 | return rc; |
| 7612 | } |
| 7613 | default: { |
| 7614 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 7615 | } |
| 7616 | } |
drh | 8616cff | 2019-07-13 16:15:23 +0000 | [diff] [blame] | 7617 | /*NOTREACHED*/ assert(0); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7618 | return SQLITE_ERROR; |
| 7619 | } |
| 7620 | |
| 7621 | /* |
| 7622 | ** Within this division (the proxying locking implementation) the procedures |
| 7623 | ** above this point are all utilities. The lock-related methods of the |
| 7624 | ** proxy-locking sqlite3_io_method object follow. |
| 7625 | */ |
| 7626 | |
| 7627 | |
| 7628 | /* |
| 7629 | ** This routine checks if there is a RESERVED lock held on the specified |
| 7630 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 7631 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 7632 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 7633 | */ |
| 7634 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 7635 | unixFile *pFile = (unixFile*)id; |
| 7636 | int rc = proxyTakeConch(pFile); |
| 7637 | if( rc==SQLITE_OK ){ |
| 7638 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7639 | if( pCtx->conchHeld>0 ){ |
| 7640 | unixFile *proxy = pCtx->lockProxy; |
| 7641 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 7642 | }else{ /* conchHeld < 0 is lockless */ |
| 7643 | pResOut=0; |
| 7644 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7645 | } |
| 7646 | return rc; |
| 7647 | } |
| 7648 | |
| 7649 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7650 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7651 | ** of the following: |
| 7652 | ** |
| 7653 | ** (1) SHARED_LOCK |
| 7654 | ** (2) RESERVED_LOCK |
| 7655 | ** (3) PENDING_LOCK |
| 7656 | ** (4) EXCLUSIVE_LOCK |
| 7657 | ** |
| 7658 | ** Sometimes when requesting one lock state, additional lock states |
| 7659 | ** are inserted in between. The locking might fail on one of the later |
| 7660 | ** transitions leaving the lock state different from what it started but |
| 7661 | ** still short of its goal. The following chart shows the allowed |
| 7662 | ** transitions and the inserted intermediate states: |
| 7663 | ** |
| 7664 | ** UNLOCKED -> SHARED |
| 7665 | ** SHARED -> RESERVED |
| 7666 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 7667 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 7668 | ** PENDING -> EXCLUSIVE |
| 7669 | ** |
| 7670 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 7671 | ** routine to lower a locking level. |
| 7672 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7673 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7674 | unixFile *pFile = (unixFile*)id; |
| 7675 | int rc = proxyTakeConch(pFile); |
| 7676 | if( rc==SQLITE_OK ){ |
| 7677 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7678 | if( pCtx->conchHeld>0 ){ |
| 7679 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7680 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 7681 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7682 | }else{ |
| 7683 | /* conchHeld < 0 is lockless */ |
| 7684 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7685 | } |
| 7686 | return rc; |
| 7687 | } |
| 7688 | |
| 7689 | |
| 7690 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7691 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7692 | ** must be either NO_LOCK or SHARED_LOCK. |
| 7693 | ** |
| 7694 | ** If the locking level of the file descriptor is already at or below |
| 7695 | ** the requested locking level, this routine is a no-op. |
| 7696 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7697 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7698 | unixFile *pFile = (unixFile*)id; |
| 7699 | int rc = proxyTakeConch(pFile); |
| 7700 | if( rc==SQLITE_OK ){ |
| 7701 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7702 | if( pCtx->conchHeld>0 ){ |
| 7703 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7704 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 7705 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7706 | }else{ |
| 7707 | /* conchHeld < 0 is lockless */ |
| 7708 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7709 | } |
| 7710 | return rc; |
| 7711 | } |
| 7712 | |
| 7713 | /* |
| 7714 | ** Close a file that uses proxy locks. |
| 7715 | */ |
| 7716 | static int proxyClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 7717 | if( ALWAYS(id) ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7718 | unixFile *pFile = (unixFile*)id; |
| 7719 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7720 | unixFile *lockProxy = pCtx->lockProxy; |
| 7721 | unixFile *conchFile = pCtx->conchFile; |
| 7722 | int rc = SQLITE_OK; |
| 7723 | |
| 7724 | if( lockProxy ){ |
| 7725 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 7726 | if( rc ) return rc; |
| 7727 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 7728 | if( rc ) return rc; |
| 7729 | sqlite3_free(lockProxy); |
| 7730 | pCtx->lockProxy = 0; |
| 7731 | } |
| 7732 | if( conchFile ){ |
| 7733 | if( pCtx->conchHeld ){ |
| 7734 | rc = proxyReleaseConch(pFile); |
| 7735 | if( rc ) return rc; |
| 7736 | } |
| 7737 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 7738 | if( rc ) return rc; |
| 7739 | sqlite3_free(conchFile); |
| 7740 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7741 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7742 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7743 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7744 | /* restore the original locking context and pMethod then close it */ |
| 7745 | pFile->lockingContext = pCtx->oldLockingContext; |
| 7746 | pFile->pMethod = pCtx->pOldMethod; |
| 7747 | sqlite3_free(pCtx); |
| 7748 | return pFile->pMethod->xClose(id); |
| 7749 | } |
| 7750 | return SQLITE_OK; |
| 7751 | } |
| 7752 | |
| 7753 | |
| 7754 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7755 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7756 | /* |
| 7757 | ** The proxy locking style is intended for use with AFP filesystems. |
| 7758 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 7759 | ** restricted to MacOSX. |
| 7760 | ** |
| 7761 | ** |
| 7762 | ******************* End of the proxy lock implementation ********************** |
| 7763 | ******************************************************************************/ |
| 7764 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7765 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7766 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7767 | ** |
| 7768 | ** This routine registers all VFS implementations for unix-like operating |
| 7769 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 7770 | ** should be the only routines in this file that are visible from other |
| 7771 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7772 | ** |
| 7773 | ** This routine is called once during SQLite initialization and by a |
| 7774 | ** single thread. The memory allocation and mutex subsystems have not |
| 7775 | ** necessarily been initialized when this routine is called, and so they |
| 7776 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7777 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7778 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7779 | /* |
| 7780 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7781 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 7782 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 7783 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 7784 | ** and so we have to go through the intermediate pointer to avoid problems |
| 7785 | ** when compiling with -pedantic-errors on GCC.) |
| 7786 | ** |
| 7787 | ** 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] | 7788 | ** finder-function. The finder-function returns a pointer to the |
| 7789 | ** sqlite_io_methods object that implements the desired locking |
| 7790 | ** behaviors. See the division above that contains the IOMETHODS |
| 7791 | ** macro for addition information on finder-functions. |
| 7792 | ** |
| 7793 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 7794 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 7795 | ** more than that; it looks at the filesystem type that hosts the |
| 7796 | ** database file and tries to choose an locking method appropriate for |
| 7797 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7798 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7799 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7800 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7801 | sizeof(unixFile), /* szOsFile */ \ |
| 7802 | MAX_PATHNAME, /* mxPathname */ \ |
| 7803 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7804 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7805 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7806 | unixOpen, /* xOpen */ \ |
| 7807 | unixDelete, /* xDelete */ \ |
| 7808 | unixAccess, /* xAccess */ \ |
| 7809 | unixFullPathname, /* xFullPathname */ \ |
| 7810 | unixDlOpen, /* xDlOpen */ \ |
| 7811 | unixDlError, /* xDlError */ \ |
| 7812 | unixDlSym, /* xDlSym */ \ |
| 7813 | unixDlClose, /* xDlClose */ \ |
| 7814 | unixRandomness, /* xRandomness */ \ |
| 7815 | unixSleep, /* xSleep */ \ |
| 7816 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 7817 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 7818 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7819 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 7820 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 7821 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7822 | } |
| 7823 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7824 | /* |
| 7825 | ** All default VFSes for unix are contained in the following array. |
| 7826 | ** |
| 7827 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 7828 | ** by the SQLite core when the VFS is registered. So the following |
| 7829 | ** array cannot be const. |
| 7830 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7831 | static sqlite3_vfs aVfs[] = { |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7832 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7833 | UNIXVFS("unix", autolockIoFinder ), |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7834 | #elif OS_VXWORKS |
| 7835 | UNIXVFS("unix", vxworksIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7836 | #else |
| 7837 | UNIXVFS("unix", posixIoFinder ), |
| 7838 | #endif |
| 7839 | UNIXVFS("unix-none", nolockIoFinder ), |
| 7840 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7841 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7842 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7843 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7844 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7845 | #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7846 | UNIXVFS("unix-posix", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7847 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7848 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 7849 | UNIXVFS("unix-flock", flockIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7850 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7851 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7852 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7853 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7854 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7855 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7856 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7857 | unsigned int i; /* Loop counter */ |
| 7858 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7859 | /* Double-check that the aSyscall[] array has been constructed |
| 7860 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 7861 | assert( ArraySize(aSyscall)==29 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7862 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7863 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7864 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7865 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7866 | } |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 7867 | unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7868 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7869 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7870 | |
| 7871 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7872 | ** Shutdown the operating system interface. |
| 7873 | ** |
| 7874 | ** Some operating systems might need to do some cleanup in this routine, |
| 7875 | ** to release dynamically allocated objects. But not on unix. |
| 7876 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7877 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7878 | int sqlite3_os_end(void){ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 7879 | unixBigLock = 0; |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7880 | return SQLITE_OK; |
| 7881 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 7882 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 7883 | #endif /* SQLITE_OS_UNIX */ |