drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 13 | ** This file contains the VFS implementation for unix-like operating systems |
| 14 | ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 15 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 16 | ** There are actually several different VFS implementations in this file. |
| 17 | ** The differences are in the way that file locking is done. The default |
| 18 | ** implementation uses Posix Advisory Locks. Alternative implementations |
| 19 | ** use flock(), dot-files, various proprietary locking schemas, or simply |
| 20 | ** skip locking all together. |
| 21 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 22 | ** This source file is organized into divisions where the logic for various |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 23 | ** subfunctions is contained within the appropriate division. PLEASE |
| 24 | ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed |
| 25 | ** in the correct division and should be clearly labeled. |
| 26 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 27 | ** The layout of divisions is as follows: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 28 | ** |
| 29 | ** * General-purpose declarations and utility functions. |
| 30 | ** * Unique file ID logic used by VxWorks. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 31 | ** * Various locking primitive implementations (all except proxy locking): |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 32 | ** + for Posix Advisory Locks |
| 33 | ** + for no-op locks |
| 34 | ** + for dot-file locks |
| 35 | ** + for flock() locking |
| 36 | ** + for named semaphore locks (VxWorks only) |
| 37 | ** + for AFP filesystem locks (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 38 | ** * sqlite3_file methods not associated with locking. |
| 39 | ** * Definitions of sqlite3_io_methods objects for all locking |
| 40 | ** methods plus "finder" functions for each locking method. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 41 | ** * sqlite3_vfs method implementations. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 42 | ** * Locking primitives for the proxy uber-locking-method. (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 43 | ** * Definitions of sqlite3_vfs objects for all locking methods |
| 44 | ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 45 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 46 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 47 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 48 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 49 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 50 | ** There are various methods for file locking used for concurrency |
| 51 | ** control: |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 52 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 53 | ** 1. POSIX locking (the default), |
| 54 | ** 2. No locking, |
| 55 | ** 3. Dot-file locking, |
| 56 | ** 4. flock() locking, |
| 57 | ** 5. AFP locking (OSX only), |
| 58 | ** 6. Named POSIX semaphores (VXWorks only), |
| 59 | ** 7. proxy locking. (OSX only) |
| 60 | ** |
| 61 | ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |
| 62 | ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |
| 63 | ** selection of the appropriate locking style based on the filesystem |
| 64 | ** where the database is located. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 65 | */ |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 66 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 67 | # if defined(__APPLE__) |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 68 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 69 | # else |
| 70 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 71 | # endif |
| 72 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 73 | |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 74 | /* Use pread() and pwrite() if they are available */ |
drh | 79a2ca3 | 2016-03-04 03:14:39 +0000 | [diff] [blame] | 75 | #if defined(__APPLE__) |
| 76 | # define HAVE_PREAD 1 |
| 77 | # define HAVE_PWRITE 1 |
| 78 | #endif |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 79 | #if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64) |
| 80 | # undef USE_PREAD |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 81 | # define USE_PREAD64 1 |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 82 | #elif defined(HAVE_PREAD) && defined(HAVE_PWRITE) |
drh | 79a2ca3 | 2016-03-04 03:14:39 +0000 | [diff] [blame] | 83 | # undef USE_PREAD64 |
| 84 | # define USE_PREAD 1 |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 87 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 88 | ** standard include files. |
| 89 | */ |
| 90 | #include <sys/types.h> |
| 91 | #include <sys/stat.h> |
| 92 | #include <fcntl.h> |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 93 | #include <sys/ioctl.h> |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 94 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 95 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 96 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 97 | #include <errno.h> |
dan | 32c12fe | 2013-05-02 17:37:31 +0000 | [diff] [blame] | 98 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 99 | # include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 100 | #endif |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 101 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 102 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 103 | # include <sys/ioctl.h> |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 104 | # include <sys/file.h> |
| 105 | # include <sys/param.h> |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 106 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 107 | |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 108 | #if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ |
| 109 | (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) |
| 110 | # if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ |
| 111 | && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0)) |
| 112 | # define HAVE_GETHOSTUUID 1 |
| 113 | # else |
| 114 | # warning "gethostuuid() is disabled." |
| 115 | # endif |
| 116 | #endif |
| 117 | |
| 118 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 119 | #if OS_VXWORKS |
| 120 | # include <sys/ioctl.h> |
| 121 | # include <semaphore.h> |
| 122 | # include <limits.h> |
| 123 | #endif /* OS_VXWORKS */ |
| 124 | |
| 125 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 126 | # include <sys/mount.h> |
| 127 | #endif |
| 128 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 129 | #ifdef HAVE_UTIME |
| 130 | # include <utime.h> |
| 131 | #endif |
| 132 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 133 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 134 | ** Allowed values of unixFile.fsFlags |
| 135 | */ |
| 136 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 137 | |
| 138 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 139 | ** If we are to be thread-safe, include the pthreads header and define |
| 140 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 141 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 142 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 143 | # include <pthread.h> |
| 144 | # define SQLITE_UNIX_THREADS 1 |
| 145 | #endif |
| 146 | |
| 147 | /* |
| 148 | ** Default permissions when creating a new file |
| 149 | */ |
| 150 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 151 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 152 | #endif |
| 153 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 154 | /* |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 155 | ** Default permissions when creating auto proxy dir |
| 156 | */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 157 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 158 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 159 | #endif |
| 160 | |
| 161 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 162 | ** Maximum supported path-length. |
| 163 | */ |
| 164 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 165 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 166 | /* |
| 167 | ** Maximum supported symbolic links |
| 168 | */ |
| 169 | #define SQLITE_MAX_SYMLINKS 100 |
| 170 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 171 | /* Always cast the getpid() return type for compatibility with |
| 172 | ** kernel modules in VxWorks. */ |
| 173 | #define osGetpid(X) (pid_t)getpid() |
| 174 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 175 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 176 | ** Only set the lastErrno if the error code is a real error and not |
| 177 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 178 | */ |
| 179 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 180 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 181 | /* Forward references */ |
| 182 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 183 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 184 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 185 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 188 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 189 | ** cannot be closed immediately. In these cases, instances of the following |
| 190 | ** structure are used to store the file descriptor while waiting for an |
| 191 | ** opportunity to either close or reuse it. |
| 192 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 193 | struct UnixUnusedFd { |
| 194 | int fd; /* File descriptor to close */ |
| 195 | int flags; /* Flags this file descriptor was opened with */ |
| 196 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 197 | }; |
| 198 | |
| 199 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 200 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 201 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 202 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 203 | typedef struct unixFile unixFile; |
| 204 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 205 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 206 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 207 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 208 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 209 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 210 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 211 | int lastErrno; /* The unix errno from last I/O error */ |
| 212 | void *lockingContext; /* Locking style specific state */ |
| 213 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 214 | const char *zPath; /* Name of the file */ |
| 215 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 216 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 217 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 218 | int nFetchOut; /* Number of outstanding xFetch refs */ |
| 219 | sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 220 | sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ |
| 221 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 222 | void *pMapRegion; /* Memory mapped region */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 223 | #endif |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 224 | int sectorSize; /* Device sector size */ |
| 225 | int deviceCharacteristics; /* Precomputed device characteristics */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 226 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 227 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 228 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 229 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 230 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 231 | #endif |
| 232 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 233 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 234 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 235 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 236 | /* The next group of variables are used to track whether or not the |
| 237 | ** transaction counter in bytes 24-27 of database files are updated |
| 238 | ** whenever any part of the database changes. An assertion fault will |
| 239 | ** occur if a file is updated without also updating the transaction |
| 240 | ** counter. This test is made to avoid new problems similar to the |
| 241 | ** one described by ticket #3584. |
| 242 | */ |
| 243 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 244 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 245 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 246 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 247 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 248 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 249 | #ifdef SQLITE_TEST |
| 250 | /* In test mode, increase the size of this structure a bit so that |
| 251 | ** it is larger than the struct CrashFile defined in test6.c. |
| 252 | */ |
| 253 | char aPadding[32]; |
| 254 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 257 | /* This variable holds the process id (pid) from when the xRandomness() |
| 258 | ** method was called. If xOpen() is called from a different process id, |
| 259 | ** indicating that a fork() has occurred, the PRNG will be reset. |
| 260 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 261 | static pid_t randomnessPid = 0; |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 262 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 263 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 264 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 265 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 266 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 267 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 268 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 269 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 270 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 271 | #else |
| 272 | # define UNIXFILE_DIRSYNC 0x00 |
| 273 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 274 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 275 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 276 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 277 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 278 | |
| 279 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 280 | ** Include code that is common to all os_*.c files |
| 281 | */ |
| 282 | #include "os_common.h" |
| 283 | |
| 284 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 285 | ** Define various macros that are missing from some systems. |
| 286 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 287 | #ifndef O_LARGEFILE |
| 288 | # define O_LARGEFILE 0 |
| 289 | #endif |
| 290 | #ifdef SQLITE_DISABLE_LFS |
| 291 | # undef O_LARGEFILE |
| 292 | # define O_LARGEFILE 0 |
| 293 | #endif |
| 294 | #ifndef O_NOFOLLOW |
| 295 | # define O_NOFOLLOW 0 |
| 296 | #endif |
| 297 | #ifndef O_BINARY |
| 298 | # define O_BINARY 0 |
| 299 | #endif |
| 300 | |
| 301 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 302 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 303 | ** testing and debugging only. |
| 304 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 305 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 306 | #define threadid pthread_self() |
| 307 | #else |
| 308 | #define threadid 0 |
| 309 | #endif |
| 310 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 311 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 312 | ** HAVE_MREMAP defaults to true on Linux and false everywhere else. |
| 313 | */ |
| 314 | #if !defined(HAVE_MREMAP) |
| 315 | # if defined(__linux__) && defined(_GNU_SOURCE) |
| 316 | # define HAVE_MREMAP 1 |
| 317 | # else |
| 318 | # define HAVE_MREMAP 0 |
| 319 | # endif |
| 320 | #endif |
| 321 | |
| 322 | /* |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 323 | ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() |
| 324 | ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. |
| 325 | */ |
| 326 | #ifdef __ANDROID__ |
| 327 | # define lseek lseek64 |
| 328 | #endif |
| 329 | |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 330 | #define F2FS_IOCTL_MAGIC 0xf5 |
| 331 | #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1) |
| 332 | #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2) |
| 333 | #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3) |
| 334 | #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5) |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame^] | 335 | #define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32) |
| 336 | |
| 337 | #define F2FS_FEATURE_ATOMIC_WRITE 0x0004 |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 338 | |
| 339 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 340 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 341 | ** Different Unix systems declare open() in different ways. Same use |
| 342 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 343 | ** The difference is important when using a pointer to the function. |
| 344 | ** |
| 345 | ** The safest way to deal with the problem is to always use this wrapper |
| 346 | ** which always has the same well-defined interface. |
| 347 | */ |
| 348 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 349 | return open(zFile, flags, mode); |
| 350 | } |
| 351 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 352 | /* Forward reference */ |
| 353 | static int openDirectory(const char*, int*); |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 354 | static int unixGetpagesize(void); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 355 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 356 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 357 | ** Many system calls are accessed through pointer-to-functions so that |
| 358 | ** they may be overridden at runtime to facilitate fault injection during |
| 359 | ** testing and sandboxing. The following array holds the names and pointers |
| 360 | ** to all overrideable system calls. |
| 361 | */ |
| 362 | static struct unix_syscall { |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 363 | const char *zName; /* Name of the system call */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 364 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 365 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 366 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 367 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 368 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 369 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 370 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 371 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 372 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 373 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 374 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 375 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 376 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 377 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 378 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 379 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 380 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 381 | |
| 382 | /* |
| 383 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 384 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 385 | ** that always succeeds. This means that locking does not occur under |
| 386 | ** DJGPP. But it is DOS - what did you expect? |
| 387 | */ |
| 388 | #ifdef __DJGPP__ |
| 389 | { "fstat", 0, 0 }, |
| 390 | #define osFstat(a,b,c) 0 |
| 391 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 392 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 393 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 394 | #endif |
| 395 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 396 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 397 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 398 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 399 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 400 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 401 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 402 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 403 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 404 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 405 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 406 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 407 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 408 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 409 | #endif |
| 410 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 411 | |
| 412 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 413 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 414 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 415 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 416 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 417 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 418 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 419 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 420 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 421 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 422 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 423 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 424 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 425 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 426 | #endif |
| 427 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 428 | aSyscall[12].pCurrent) |
| 429 | |
| 430 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 431 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 432 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 433 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 434 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 435 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 436 | aSyscall[13].pCurrent) |
| 437 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 438 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 439 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 440 | |
| 441 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 442 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 443 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 444 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 445 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 446 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 447 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 448 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 449 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 450 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 451 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 452 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 453 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 454 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 455 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 456 | |
| 457 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 458 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 459 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 460 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 461 | { "fchown", (sqlite3_syscall_ptr)fchown, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 462 | #else |
| 463 | { "fchown", (sqlite3_syscall_ptr)0, 0 }, |
| 464 | #endif |
dan | d3eaebd | 2012-02-13 08:50:23 +0000 | [diff] [blame] | 465 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
drh | 23c4b97 | 2012-02-11 23:55:15 +0000 | [diff] [blame] | 466 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 467 | { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, |
| 468 | #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) |
| 469 | |
dan | 4dd5144 | 2013-08-26 14:30:25 +0000 | [diff] [blame] | 470 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 471 | { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, |
| 472 | #else |
| 473 | { "mmap", (sqlite3_syscall_ptr)0, 0 }, |
| 474 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 475 | #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] | 476 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 477 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 478 | { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 479 | #else |
drh | a829992 | 2016-01-08 22:31:00 +0000 | [diff] [blame] | 480 | { "munmap", (sqlite3_syscall_ptr)0, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 481 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 482 | #define osMunmap ((void*(*)(void*,size_t))aSyscall[23].pCurrent) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 483 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 484 | #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 485 | { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, |
| 486 | #else |
| 487 | { "mremap", (sqlite3_syscall_ptr)0, 0 }, |
| 488 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 489 | #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent) |
| 490 | |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 491 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 492 | { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 493 | #else |
| 494 | { "getpagesize", (sqlite3_syscall_ptr)0, 0 }, |
| 495 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 496 | #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent) |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 497 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 498 | #if defined(HAVE_READLINK) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 499 | { "readlink", (sqlite3_syscall_ptr)readlink, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 500 | #else |
| 501 | { "readlink", (sqlite3_syscall_ptr)0, 0 }, |
| 502 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 503 | #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 504 | |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 505 | #if defined(HAVE_LSTAT) |
| 506 | { "lstat", (sqlite3_syscall_ptr)lstat, 0 }, |
| 507 | #else |
| 508 | { "lstat", (sqlite3_syscall_ptr)0, 0 }, |
| 509 | #endif |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 510 | #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 511 | |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 512 | { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 }, |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame^] | 513 | #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 514 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 515 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 516 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | ** On some systems, calls to fchown() will trigger a message in a security |
| 520 | ** log if they come from non-root processes. So avoid calling fchown() if |
| 521 | ** we are not running as root. |
| 522 | */ |
| 523 | static int robustFchown(int fd, uid_t uid, gid_t gid){ |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 524 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 525 | return osGeteuid() ? 0 : osFchown(fd,uid,gid); |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 526 | #else |
| 527 | return 0; |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 528 | #endif |
| 529 | } |
| 530 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 531 | /* |
| 532 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 533 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 534 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 535 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 536 | */ |
| 537 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 538 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 539 | const char *zName, /* Name of system call to override */ |
| 540 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 541 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 542 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 543 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 544 | |
| 545 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 546 | if( zName==0 ){ |
| 547 | /* If no zName is given, restore all system calls to their default |
| 548 | ** settings and return NULL |
| 549 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 550 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 551 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 552 | if( aSyscall[i].pDefault ){ |
| 553 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | }else{ |
| 557 | /* If zName is specified, operate on only the one system call |
| 558 | ** specified. |
| 559 | */ |
| 560 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 561 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 562 | if( aSyscall[i].pDefault==0 ){ |
| 563 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 564 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 565 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 566 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 567 | aSyscall[i].pCurrent = pNewFunc; |
| 568 | break; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | return rc; |
| 573 | } |
| 574 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 575 | /* |
| 576 | ** Return the value of a system call. Return NULL if zName is not a |
| 577 | ** recognized system call name. NULL is also returned if the system call |
| 578 | ** is currently undefined. |
| 579 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 580 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 581 | sqlite3_vfs *pNotUsed, |
| 582 | const char *zName |
| 583 | ){ |
| 584 | unsigned int i; |
| 585 | |
| 586 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 587 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 588 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 589 | } |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | ** Return the name of the first system call after zName. If zName==NULL |
| 595 | ** then return the name of the first system call. Return NULL if zName |
| 596 | ** is the last system call or if zName is not the name of a valid |
| 597 | ** system call. |
| 598 | */ |
| 599 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 600 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 601 | |
| 602 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 603 | if( zName ){ |
| 604 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 605 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 608 | for(i++; i<ArraySize(aSyscall); i++){ |
| 609 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 610 | } |
| 611 | return 0; |
| 612 | } |
| 613 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 614 | /* |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 615 | ** Do not accept any file descriptor less than this value, in order to avoid |
| 616 | ** opening database file using file descriptors that are commonly used for |
| 617 | ** standard input, output, and error. |
| 618 | */ |
| 619 | #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
| 620 | # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3 |
| 621 | #endif |
| 622 | |
| 623 | /* |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 624 | ** Invoke open(). Do so multiple times, until it either succeeds or |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 625 | ** fails for some reason other than EINTR. |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 626 | ** |
| 627 | ** If the file creation mode "m" is 0 then set it to the default for |
| 628 | ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 629 | ** 0644) as modified by the system umask. If m is not 0, then |
| 630 | ** make the file creation mode be exactly m ignoring the umask. |
| 631 | ** |
| 632 | ** The m parameter will be non-zero only when creating -wal, -journal, |
| 633 | ** and -shm files. We want those files to have *exactly* the same |
| 634 | ** permissions as their original database, unadulterated by the umask. |
| 635 | ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 636 | ** transaction crashes and leaves behind hot journals, then any |
| 637 | ** process that is able to write to the database will also be able to |
| 638 | ** recover the hot journals. |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 639 | */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 640 | static int robust_open(const char *z, int f, mode_t m){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 641 | int fd; |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 642 | mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 643 | while(1){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 644 | #if defined(O_CLOEXEC) |
| 645 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 646 | #else |
| 647 | fd = osOpen(z,f,m2); |
| 648 | #endif |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 649 | if( fd<0 ){ |
| 650 | if( errno==EINTR ) continue; |
| 651 | break; |
| 652 | } |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 653 | if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 654 | osClose(fd); |
| 655 | sqlite3_log(SQLITE_WARNING, |
| 656 | "attempt to open \"%s\" as file descriptor %d", z, fd); |
| 657 | fd = -1; |
| 658 | if( osOpen("/dev/null", f, m)<0 ) break; |
| 659 | } |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 660 | if( fd>=0 ){ |
| 661 | if( m!=0 ){ |
| 662 | struct stat statbuf; |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 663 | if( osFstat(fd, &statbuf)==0 |
| 664 | && statbuf.st_size==0 |
drh | cfc1769 | 2013-03-06 01:41:53 +0000 | [diff] [blame] | 665 | && (statbuf.st_mode&0777)!=m |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 666 | ){ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 667 | osFchmod(fd, m); |
| 668 | } |
| 669 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 670 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 671 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 672 | #endif |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 673 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 674 | return fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 675 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 676 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 677 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 678 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 679 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 680 | ** vxworksFileId objects used by this file, all of which may be |
| 681 | ** shared by multiple threads. |
| 682 | ** |
| 683 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 684 | ** is held when required. This function is only used as part of assert() |
| 685 | ** statements. e.g. |
| 686 | ** |
| 687 | ** unixEnterMutex() |
| 688 | ** assert( unixMutexHeld() ); |
| 689 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 690 | */ |
| 691 | static void unixEnterMutex(void){ |
mistachkin | 93de653 | 2015-07-03 21:38:09 +0000 | [diff] [blame] | 692 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 693 | } |
| 694 | static void unixLeaveMutex(void){ |
mistachkin | 93de653 | 2015-07-03 21:38:09 +0000 | [diff] [blame] | 695 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 696 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 697 | #ifdef SQLITE_DEBUG |
| 698 | static int unixMutexHeld(void) { |
mistachkin | 93de653 | 2015-07-03 21:38:09 +0000 | [diff] [blame] | 699 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 700 | } |
| 701 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 702 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 703 | |
mistachkin | fb383e9 | 2015-04-16 03:24:38 +0000 | [diff] [blame] | 704 | #ifdef SQLITE_HAVE_OS_TRACE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 705 | /* |
| 706 | ** Helper function for printing out trace information from debugging |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 707 | ** binaries. This returns the string representation of the supplied |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 708 | ** integer lock-type. |
| 709 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 710 | static const char *azFileLock(int eFileLock){ |
| 711 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 712 | case NO_LOCK: return "NONE"; |
| 713 | case SHARED_LOCK: return "SHARED"; |
| 714 | case RESERVED_LOCK: return "RESERVED"; |
| 715 | case PENDING_LOCK: return "PENDING"; |
| 716 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 717 | } |
| 718 | return "ERROR"; |
| 719 | } |
| 720 | #endif |
| 721 | |
| 722 | #ifdef SQLITE_LOCK_TRACE |
| 723 | /* |
| 724 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 725 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 726 | ** This routine is used for troubleshooting locks on multithreaded |
| 727 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 728 | ** command-line option on the compiler. This code is normally |
| 729 | ** turned off. |
| 730 | */ |
| 731 | static int lockTrace(int fd, int op, struct flock *p){ |
| 732 | char *zOpName, *zType; |
| 733 | int s; |
| 734 | int savedErrno; |
| 735 | if( op==F_GETLK ){ |
| 736 | zOpName = "GETLK"; |
| 737 | }else if( op==F_SETLK ){ |
| 738 | zOpName = "SETLK"; |
| 739 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 740 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 741 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 742 | return s; |
| 743 | } |
| 744 | if( p->l_type==F_RDLCK ){ |
| 745 | zType = "RDLCK"; |
| 746 | }else if( p->l_type==F_WRLCK ){ |
| 747 | zType = "WRLCK"; |
| 748 | }else if( p->l_type==F_UNLCK ){ |
| 749 | zType = "UNLCK"; |
| 750 | }else{ |
| 751 | assert( 0 ); |
| 752 | } |
| 753 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 754 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 755 | savedErrno = errno; |
| 756 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 757 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 758 | (int)p->l_pid, s); |
| 759 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 760 | struct flock l2; |
| 761 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 762 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 763 | if( l2.l_type==F_RDLCK ){ |
| 764 | zType = "RDLCK"; |
| 765 | }else if( l2.l_type==F_WRLCK ){ |
| 766 | zType = "WRLCK"; |
| 767 | }else if( l2.l_type==F_UNLCK ){ |
| 768 | zType = "UNLCK"; |
| 769 | }else{ |
| 770 | assert( 0 ); |
| 771 | } |
| 772 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 773 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 774 | } |
| 775 | errno = savedErrno; |
| 776 | return s; |
| 777 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 778 | #undef osFcntl |
| 779 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 780 | #endif /* SQLITE_LOCK_TRACE */ |
| 781 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 782 | /* |
| 783 | ** Retry ftruncate() calls that fail due to EINTR |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 784 | ** |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 785 | ** All calls to ftruncate() within this file should be made through |
| 786 | ** this wrapper. On the Android platform, bypassing the logic below |
| 787 | ** could lead to a corrupt database. |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 788 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 789 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 790 | int rc; |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 791 | #ifdef __ANDROID__ |
| 792 | /* On Android, ftruncate() always uses 32-bit offsets, even if |
| 793 | ** _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] | 794 | ** truncate a file to any size larger than 2GiB. Silently ignore any |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 795 | ** such attempts. */ |
| 796 | if( sz>(sqlite3_int64)0x7FFFFFFF ){ |
| 797 | rc = SQLITE_OK; |
| 798 | }else |
| 799 | #endif |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 800 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 801 | return rc; |
| 802 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 803 | |
| 804 | /* |
| 805 | ** This routine translates a standard POSIX errno code into something |
| 806 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 807 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 808 | ** and a variety of "please close the file descriptor NOW" errors into |
| 809 | ** SQLITE_IOERR |
| 810 | ** |
| 811 | ** Errors during initialization of locks, or file system support for locks, |
| 812 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 813 | */ |
| 814 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 815 | assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 816 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 817 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 818 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 819 | switch (posixError) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 820 | case EACCES: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 821 | case EAGAIN: |
| 822 | case ETIMEDOUT: |
| 823 | case EBUSY: |
| 824 | case EINTR: |
| 825 | case ENOLCK: |
| 826 | /* random NFS retry error, unless during file system support |
| 827 | * introspection, in which it actually means what it says */ |
| 828 | return SQLITE_BUSY; |
| 829 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 830 | case EPERM: |
| 831 | return SQLITE_PERM; |
| 832 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 833 | default: |
| 834 | return sqliteIOErr; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 839 | /****************************************************************************** |
| 840 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 841 | ** |
| 842 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 843 | ** the device number and the inode number. But this does not work on VxWorks. |
| 844 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 845 | ** |
| 846 | ** A pointer to an instance of the following structure can be used as a |
| 847 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 848 | ** a copy of the canonical filename. There is also a reference count. |
| 849 | ** The structure is reclaimed when the number of pointers to it drops to |
| 850 | ** zero. |
| 851 | ** |
| 852 | ** There are never very many files open at one time and lookups are not |
| 853 | ** a performance-critical path, so it is sufficient to put these |
| 854 | ** structures on a linked list. |
| 855 | */ |
| 856 | struct vxworksFileId { |
| 857 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 858 | int nRef; /* Number of references to this one */ |
| 859 | int nName; /* Length of the zCanonicalName[] string */ |
| 860 | char *zCanonicalName; /* Canonical filename */ |
| 861 | }; |
| 862 | |
| 863 | #if OS_VXWORKS |
| 864 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 865 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 866 | ** variable: |
| 867 | */ |
| 868 | static struct vxworksFileId *vxworksFileList = 0; |
| 869 | |
| 870 | /* |
| 871 | ** Simplify a filename into its canonical form |
| 872 | ** by making the following changes: |
| 873 | ** |
| 874 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 875 | ** * convert /./ into just / |
| 876 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 877 | ** |
| 878 | ** Changes are made in-place. Return the new name length. |
| 879 | ** |
| 880 | ** The original filename is in z[0..n-1]. Return the number of |
| 881 | ** characters in the simplified name. |
| 882 | */ |
| 883 | static int vxworksSimplifyName(char *z, int n){ |
| 884 | int i, j; |
| 885 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 886 | for(i=j=0; i<n; i++){ |
| 887 | if( z[i]=='/' ){ |
| 888 | if( z[i+1]=='/' ) continue; |
| 889 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 890 | i += 1; |
| 891 | continue; |
| 892 | } |
| 893 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 894 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 895 | if( j>0 ){ j--; } |
| 896 | i += 2; |
| 897 | continue; |
| 898 | } |
| 899 | } |
| 900 | z[j++] = z[i]; |
| 901 | } |
| 902 | z[j] = 0; |
| 903 | return j; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | ** Find a unique file ID for the given absolute pathname. Return |
| 908 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 909 | ** file ID. |
| 910 | ** |
| 911 | ** The nRef field of the vxworksFileId object is incremented before |
| 912 | ** the object is returned. A new vxworksFileId object is created |
| 913 | ** and added to the global list if necessary. |
| 914 | ** |
| 915 | ** If a memory allocation error occurs, return NULL. |
| 916 | */ |
| 917 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 918 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 919 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 920 | int n; /* Length of zAbsoluteName string */ |
| 921 | |
| 922 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 923 | n = (int)strlen(zAbsoluteName); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 924 | pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 925 | if( pNew==0 ) return 0; |
| 926 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 927 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 928 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 929 | |
| 930 | /* Search for an existing entry that matching the canonical name. |
| 931 | ** If found, increment the reference count and return a pointer to |
| 932 | ** the existing file ID. |
| 933 | */ |
| 934 | unixEnterMutex(); |
| 935 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 936 | if( pCandidate->nName==n |
| 937 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 938 | ){ |
| 939 | sqlite3_free(pNew); |
| 940 | pCandidate->nRef++; |
| 941 | unixLeaveMutex(); |
| 942 | return pCandidate; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /* No match was found. We will make a new file ID */ |
| 947 | pNew->nRef = 1; |
| 948 | pNew->nName = n; |
| 949 | pNew->pNext = vxworksFileList; |
| 950 | vxworksFileList = pNew; |
| 951 | unixLeaveMutex(); |
| 952 | return pNew; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | ** Decrement the reference count on a vxworksFileId object. Free |
| 957 | ** the object when the reference count reaches zero. |
| 958 | */ |
| 959 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 960 | unixEnterMutex(); |
| 961 | assert( pId->nRef>0 ); |
| 962 | pId->nRef--; |
| 963 | if( pId->nRef==0 ){ |
| 964 | struct vxworksFileId **pp; |
| 965 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 966 | assert( *pp==pId ); |
| 967 | *pp = pId->pNext; |
| 968 | sqlite3_free(pId); |
| 969 | } |
| 970 | unixLeaveMutex(); |
| 971 | } |
| 972 | #endif /* OS_VXWORKS */ |
| 973 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 974 | ******************************************************************************/ |
| 975 | |
| 976 | |
| 977 | /****************************************************************************** |
| 978 | *************************** Posix Advisory Locking **************************** |
| 979 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 980 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 981 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 982 | ** sets or clears a lock, that operation overrides any prior locks set |
| 983 | ** by the same process. It does not explicitly say so, but this implies |
| 984 | ** that it overrides locks set by the same process using a different |
| 985 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 986 | ** |
| 987 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 988 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 989 | ** |
| 990 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 991 | ** one is a hard or symbolic link to the other) then if you set |
| 992 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 993 | ** on fd2, it works. I would have expected the second lock to |
| 994 | ** fail since there was already a lock on the file due to fd1. |
| 995 | ** But not so. Since both locks came from the same process, the |
| 996 | ** second overrides the first, even though they were on different |
| 997 | ** file descriptors opened on different file names. |
| 998 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 999 | ** This means that we cannot use POSIX locks to synchronize file access |
| 1000 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1001 | ** to synchronize access for threads in separate processes, but not |
| 1002 | ** threads within the same process. |
| 1003 | ** |
| 1004 | ** To work around the problem, SQLite has to manage file locks internally |
| 1005 | ** on its own. Whenever a new database is opened, we have to find the |
| 1006 | ** specific inode of the database file (the inode is determined by the |
| 1007 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 1008 | ** and check for locks already existing on that inode. When locks are |
| 1009 | ** created or removed, we have to look at our own internal record of the |
| 1010 | ** locks to see if another thread has previously set a lock on that same |
| 1011 | ** inode. |
| 1012 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1013 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 1014 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 1015 | ** canonical filename and implemented in the previous division.) |
| 1016 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1017 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1018 | ** descriptor. It is now a structure that holds the integer file |
| 1019 | ** descriptor and a pointer to a structure that describes the internal |
| 1020 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1021 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1022 | ** point to the same locking structure. The locking structure keeps |
| 1023 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 1024 | ** field that tells us its internal lock status. cnt==0 means the |
| 1025 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 1026 | ** cnt>0 means there are cnt shared locks on the file. |
| 1027 | ** |
| 1028 | ** Any attempt to lock or unlock a file first checks the locking |
| 1029 | ** structure. The fcntl() system call is only invoked to set a |
| 1030 | ** POSIX lock if the internal lock structure transitions between |
| 1031 | ** a locked and an unlocked state. |
| 1032 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1033 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1034 | ** |
| 1035 | ** If you close a file descriptor that points to a file that has locks, |
| 1036 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1037 | ** released. To work around this problem, each unixInodeInfo object |
| 1038 | ** maintains a count of the number of pending locks on tha inode. |
| 1039 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1040 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1041 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1042 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1043 | ** be closed and that list is walked (and cleared) when the last lock |
| 1044 | ** clears. |
| 1045 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1046 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1047 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1048 | ** Many older versions of linux use the LinuxThreads library which is |
| 1049 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1050 | ** A cannot be modified or overridden by a different thread B. |
| 1051 | ** Only thread A can modify the lock. Locking behavior is correct |
| 1052 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 1053 | ** on linux - with NPTL a lock created by thread A can override locks |
| 1054 | ** in thread B. But there is no way to know at compile-time which |
| 1055 | ** threading library is being used. So there is no way to know at |
| 1056 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1057 | ** 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] | 1058 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1059 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1060 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 1061 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 1062 | ** LinuxThreads provided that (1) there is no more than one connection |
| 1063 | ** per database file in the same process and (2) database connections |
| 1064 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1065 | */ |
| 1066 | |
| 1067 | /* |
| 1068 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1069 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1070 | */ |
| 1071 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1072 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1073 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1074 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1075 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1076 | /* We are told that some versions of Android contain a bug that |
| 1077 | ** sizes ino_t at only 32-bits instead of 64-bits. (See |
| 1078 | ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c) |
| 1079 | ** To work around this, always allocate 64-bits for the inode number. |
| 1080 | ** On small machines that only have 32-bit inodes, this wastes 4 bytes, |
| 1081 | ** but that should not be a big deal. */ |
| 1082 | /* WAS: ino_t ino; */ |
| 1083 | u64 ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1084 | #endif |
| 1085 | }; |
| 1086 | |
| 1087 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1088 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1089 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 1090 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1091 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1092 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1093 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1094 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1095 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1096 | struct unixInodeInfo { |
| 1097 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1098 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1099 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 1100 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1101 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1102 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 1103 | int nLock; /* Number of outstanding file locks */ |
| 1104 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 1105 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 1106 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 1107 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1108 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 1109 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1110 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1111 | sem_t *pSem; /* Named POSIX semaphore */ |
| 1112 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1113 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1114 | }; |
| 1115 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1116 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1117 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1118 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1119 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1120 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1121 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1122 | ** |
drh | aaeaa18 | 2015-11-24 15:12:47 +0000 | [diff] [blame] | 1123 | ** This function - unixLogErrorAtLine(), is only ever called via the macro |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1124 | ** unixLogError(). |
| 1125 | ** |
| 1126 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1127 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1128 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1129 | ** strerror_r(). |
| 1130 | ** |
| 1131 | ** The first argument passed to the macro should be the error code that |
| 1132 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1133 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1134 | ** failed (e.g. "unlink", "open") and the associated file-system path, |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1135 | ** if any. |
| 1136 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1137 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1138 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1139 | int errcode, /* SQLite error code */ |
| 1140 | const char *zFunc, /* Name of OS function that failed */ |
| 1141 | const char *zPath, /* File path associated with error */ |
| 1142 | int iLine /* Source line number where error occurred */ |
| 1143 | ){ |
| 1144 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1145 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1146 | |
| 1147 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1148 | ** the strerror() function to obtain the human-readable error message |
| 1149 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1150 | */ |
| 1151 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1152 | char aErr[80]; |
| 1153 | memset(aErr, 0, sizeof(aErr)); |
| 1154 | zErr = aErr; |
| 1155 | |
| 1156 | /* 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] | 1157 | ** assume that the system provides the GNU version of strerror_r() that |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1158 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1159 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1160 | ** Otherwise, assume that the system provides the POSIX version of |
| 1161 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1162 | ** |
| 1163 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1164 | ** available, the error message will often be an empty string. Not a |
| 1165 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1166 | ** could lead to a segfault though. |
| 1167 | */ |
| 1168 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1169 | zErr = |
| 1170 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1171 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1172 | |
| 1173 | #elif SQLITE_THREADSAFE |
| 1174 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1175 | zErr = ""; |
| 1176 | #else |
| 1177 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1178 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1179 | #endif |
| 1180 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1181 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1182 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1183 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1184 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1185 | ); |
| 1186 | |
| 1187 | return errcode; |
| 1188 | } |
| 1189 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1190 | /* |
| 1191 | ** Close a file descriptor. |
| 1192 | ** |
| 1193 | ** We assume that close() almost always works, since it is only in a |
| 1194 | ** very sick application or on a very sick platform that it might fail. |
| 1195 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1196 | ** error. |
| 1197 | ** |
| 1198 | ** Note that it is not safe to retry close() after EINTR since the |
| 1199 | ** file descriptor might have already been reused by another thread. |
| 1200 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1201 | ** and move on. |
| 1202 | */ |
| 1203 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1204 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1205 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1206 | pFile ? pFile->zPath : 0, lineno); |
| 1207 | } |
| 1208 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1209 | |
| 1210 | /* |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 1211 | ** Set the pFile->lastErrno. Do this in a subroutine as that provides |
| 1212 | ** a convenient place to set a breakpoint. |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1213 | */ |
| 1214 | static void storeLastErrno(unixFile *pFile, int error){ |
| 1215 | pFile->lastErrno = error; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1219 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1220 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1221 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1222 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1223 | UnixUnusedFd *p; |
| 1224 | UnixUnusedFd *pNext; |
| 1225 | for(p=pInode->pUnused; p; p=pNext){ |
| 1226 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1227 | robust_close(pFile, p->fd, __LINE__); |
| 1228 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1229 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1230 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1234 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1235 | ** |
| 1236 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1237 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1238 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1239 | static void releaseInodeInfo(unixFile *pFile){ |
| 1240 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1241 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1242 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1243 | pInode->nRef--; |
| 1244 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1245 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1246 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1247 | if( pInode->pPrev ){ |
| 1248 | assert( pInode->pPrev->pNext==pInode ); |
| 1249 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1250 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1251 | assert( inodeList==pInode ); |
| 1252 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1253 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1254 | if( pInode->pNext ){ |
| 1255 | assert( pInode->pNext->pPrev==pInode ); |
| 1256 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1257 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1258 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1259 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1264 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1265 | ** describes that file descriptor. Create a new one if necessary. The |
| 1266 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1267 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1268 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1269 | ** when this function is called. |
| 1270 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1271 | ** Return an appropriate error code. |
| 1272 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1273 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1274 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1275 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1276 | ){ |
| 1277 | int rc; /* System call return code */ |
| 1278 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1279 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1280 | struct stat statbuf; /* Low-level file information */ |
| 1281 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1282 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1283 | assert( unixMutexHeld() ); |
| 1284 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1285 | /* Get low-level information about the file that we can used to |
| 1286 | ** create a unique name for the file. |
| 1287 | */ |
| 1288 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1289 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1290 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1291 | storeLastErrno(pFile, errno); |
drh | 40fe8d3 | 2015-11-30 20:36:26 +0000 | [diff] [blame] | 1292 | #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1293 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1294 | #endif |
| 1295 | return SQLITE_IOERR; |
| 1296 | } |
| 1297 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1298 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1299 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1300 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1301 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1302 | ** we always increase the file size to 1 by writing a single byte |
| 1303 | ** prior to accessing the inode number. The one byte written is |
| 1304 | ** an ASCII 'S' character which also happens to be the first byte |
| 1305 | ** in the header of every SQLite database. In this way, if there |
| 1306 | ** is a race condition such that another thread has already populated |
| 1307 | ** the first page of the database, no damage is done. |
| 1308 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1309 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1310 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1311 | if( rc!=1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1312 | storeLastErrno(pFile, errno); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1313 | return SQLITE_IOERR; |
| 1314 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1315 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1316 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1317 | storeLastErrno(pFile, errno); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1318 | return SQLITE_IOERR; |
| 1319 | } |
| 1320 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1321 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1322 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1323 | memset(&fileId, 0, sizeof(fileId)); |
| 1324 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1325 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1326 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1327 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1328 | fileId.ino = (u64)statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1329 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1330 | pInode = inodeList; |
| 1331 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1332 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1333 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1334 | if( pInode==0 ){ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 1335 | pInode = sqlite3_malloc64( sizeof(*pInode) ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1336 | if( pInode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 1337 | return SQLITE_NOMEM_BKPT; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1338 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1339 | memset(pInode, 0, sizeof(*pInode)); |
| 1340 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1341 | pInode->nRef = 1; |
| 1342 | pInode->pNext = inodeList; |
| 1343 | pInode->pPrev = 0; |
| 1344 | if( inodeList ) inodeList->pPrev = pInode; |
| 1345 | inodeList = pInode; |
| 1346 | }else{ |
| 1347 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1348 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1349 | *ppInode = pInode; |
| 1350 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1351 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1352 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1353 | /* |
| 1354 | ** Return TRUE if pFile has been renamed or unlinked since it was first opened. |
| 1355 | */ |
| 1356 | static int fileHasMoved(unixFile *pFile){ |
drh | 61ffea5 | 2014-08-12 12:19:25 +0000 | [diff] [blame] | 1357 | #if OS_VXWORKS |
| 1358 | return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; |
| 1359 | #else |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1360 | struct stat buf; |
| 1361 | return pFile->pInode!=0 && |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1362 | (osStat(pFile->zPath, &buf)!=0 |
| 1363 | || (u64)buf.st_ino!=pFile->pInode->fileId.ino); |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 1364 | #endif |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1367 | |
| 1368 | /* |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1369 | ** Check a unixFile that is a database. Verify the following: |
| 1370 | ** |
| 1371 | ** (1) There is exactly one hard link on the file |
| 1372 | ** (2) The file is not a symbolic link |
| 1373 | ** (3) The file has not been renamed or unlinked |
| 1374 | ** |
| 1375 | ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. |
| 1376 | */ |
| 1377 | static void verifyDbFile(unixFile *pFile){ |
| 1378 | struct stat buf; |
| 1379 | int rc; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 1380 | |
| 1381 | /* These verifications occurs for the main database only */ |
| 1382 | if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return; |
| 1383 | |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1384 | rc = osFstat(pFile->h, &buf); |
| 1385 | if( rc!=0 ){ |
| 1386 | sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1387 | return; |
| 1388 | } |
drh | 6369bc3 | 2016-03-21 16:06:42 +0000 | [diff] [blame] | 1389 | if( buf.st_nlink==0 ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1390 | sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1391 | return; |
| 1392 | } |
| 1393 | if( buf.st_nlink>1 ){ |
| 1394 | sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1395 | return; |
| 1396 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1397 | if( fileHasMoved(pFile) ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1398 | sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1399 | return; |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1405 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1406 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1407 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1408 | ** 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] | 1409 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1410 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1411 | int rc = SQLITE_OK; |
| 1412 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1413 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1414 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1415 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1416 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1417 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1418 | assert( pFile->eFileLock<=SHARED_LOCK ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1419 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1420 | |
| 1421 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1422 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1423 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1426 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1427 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1428 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1429 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1430 | struct flock lock; |
| 1431 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1432 | lock.l_start = RESERVED_BYTE; |
| 1433 | lock.l_len = 1; |
| 1434 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1435 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1436 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1437 | storeLastErrno(pFile, errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1438 | } else if( lock.l_type!=F_UNLCK ){ |
| 1439 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1442 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1443 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1444 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1445 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1446 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1447 | *pResOut = reserved; |
| 1448 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1452 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1453 | ** described by pLock. |
| 1454 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1455 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1456 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1457 | ** the first time any lock is attempted. All subsequent system locking |
| 1458 | ** operations become no-ops. Locking operations still happen internally, |
| 1459 | ** in order to coordinate access between separate database connections |
| 1460 | ** within this process, but all of that is handled in memory and the |
| 1461 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1462 | ** |
| 1463 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1464 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1465 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1466 | ** |
| 1467 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1468 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1469 | */ |
| 1470 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1471 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1472 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1473 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1474 | assert( pInode!=0 ); |
drh | 50358ad | 2015-12-02 01:04:33 +0000 | [diff] [blame] | 1475 | if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1476 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1477 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1478 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1479 | lock.l_whence = SEEK_SET; |
| 1480 | lock.l_start = SHARED_FIRST; |
| 1481 | lock.l_len = SHARED_SIZE; |
| 1482 | lock.l_type = F_WRLCK; |
| 1483 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1484 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1485 | pInode->bProcessLock = 1; |
| 1486 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1487 | }else{ |
| 1488 | rc = 0; |
| 1489 | } |
| 1490 | }else{ |
| 1491 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1492 | } |
| 1493 | return rc; |
| 1494 | } |
| 1495 | |
| 1496 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1497 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1498 | ** of the following: |
| 1499 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1500 | ** (1) SHARED_LOCK |
| 1501 | ** (2) RESERVED_LOCK |
| 1502 | ** (3) PENDING_LOCK |
| 1503 | ** (4) EXCLUSIVE_LOCK |
| 1504 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1505 | ** Sometimes when requesting one lock state, additional lock states |
| 1506 | ** are inserted in between. The locking might fail on one of the later |
| 1507 | ** transitions leaving the lock state different from what it started but |
| 1508 | ** still short of its goal. The following chart shows the allowed |
| 1509 | ** transitions and the inserted intermediate states: |
| 1510 | ** |
| 1511 | ** UNLOCKED -> SHARED |
| 1512 | ** SHARED -> RESERVED |
| 1513 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1514 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1515 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1516 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1517 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1518 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1519 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1520 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1521 | /* The following describes the implementation of the various locks and |
| 1522 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1523 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1524 | ** confusion with SQLite lock names). The algorithms are complicated |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1525 | ** slightly in order to be compatible with Windows95 systems simultaneously |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1526 | ** accessing the same database file, in case that is ever required. |
| 1527 | ** |
| 1528 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1529 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1530 | ** range', a range of 510 bytes at a well known offset. |
| 1531 | ** |
| 1532 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1533 | ** byte'. If this is successful, 'shared byte range' is read-locked |
| 1534 | ** and the lock on the 'pending byte' released. (Legacy note: When |
| 1535 | ** SQLite was first developed, Windows95 systems were still very common, |
| 1536 | ** and Widnows95 lacks a shared-lock capability. So on Windows95, a |
| 1537 | ** single randomly selected by from the 'shared byte range' is locked. |
| 1538 | ** Windows95 is now pretty much extinct, but this work-around for the |
| 1539 | ** lack of shared-locks on Windows95 lives on, for backwards |
| 1540 | ** compatibility.) |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1541 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1542 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1543 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1544 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1545 | ** |
| 1546 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1547 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1548 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1549 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1550 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1551 | ** This property is used by the algorithm for rolling back a journal file |
| 1552 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1553 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1554 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1555 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1556 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1557 | ** within this range, this ensures that no other locks are held on the |
| 1558 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1559 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1560 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1561 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1562 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1563 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1564 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1565 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1566 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1567 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1568 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 1569 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1570 | osGetpid(0))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* 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] | 1573 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1574 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1575 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1576 | if( pFile->eFileLock>=eFileLock ){ |
| 1577 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1578 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1579 | return SQLITE_OK; |
| 1580 | } |
| 1581 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1582 | /* Make sure the locking sequence is correct. |
| 1583 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1584 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1585 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1586 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1587 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1588 | assert( eFileLock!=PENDING_LOCK ); |
| 1589 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1590 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1591 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1592 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1593 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1594 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1595 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1596 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1597 | ** handle that precludes the requested lock, return BUSY. |
| 1598 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1599 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1600 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1601 | ){ |
| 1602 | rc = SQLITE_BUSY; |
| 1603 | goto end_lock; |
| 1604 | } |
| 1605 | |
| 1606 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1607 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1608 | ** return SQLITE_OK. |
| 1609 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1610 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1611 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1612 | assert( eFileLock==SHARED_LOCK ); |
| 1613 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1614 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1615 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1616 | pInode->nShared++; |
| 1617 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1618 | goto end_lock; |
| 1619 | } |
| 1620 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1621 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1622 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1623 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1624 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1625 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1626 | lock.l_len = 1L; |
| 1627 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1628 | if( eFileLock==SHARED_LOCK |
| 1629 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1630 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1631 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1632 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1633 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1634 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1635 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1636 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1637 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1638 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1639 | goto end_lock; |
| 1640 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | |
| 1644 | /* If control gets to this point, then actually go ahead and make |
| 1645 | ** operating system calls for the specified lock. |
| 1646 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1647 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1648 | assert( pInode->nShared==0 ); |
| 1649 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1650 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1651 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1652 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1653 | lock.l_start = SHARED_FIRST; |
| 1654 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1655 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1656 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1657 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1658 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1659 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1660 | /* Drop the temporary PENDING lock */ |
| 1661 | lock.l_start = PENDING_BYTE; |
| 1662 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1663 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1664 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1665 | /* This could happen with a network mount */ |
| 1666 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1667 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1668 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1669 | |
| 1670 | if( rc ){ |
| 1671 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1672 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1673 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1674 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1675 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1676 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1677 | pInode->nLock++; |
| 1678 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1679 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1680 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1681 | /* We are trying for an exclusive lock but another thread in this |
| 1682 | ** same process is still holding a shared lock. */ |
| 1683 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1684 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1685 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1686 | ** assumed that there is a SHARED or greater lock on the file |
| 1687 | ** already. |
| 1688 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1689 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1690 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1691 | |
| 1692 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1693 | if( eFileLock==RESERVED_LOCK ){ |
| 1694 | lock.l_start = RESERVED_BYTE; |
| 1695 | lock.l_len = 1L; |
| 1696 | }else{ |
| 1697 | lock.l_start = SHARED_FIRST; |
| 1698 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1699 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1700 | |
| 1701 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1702 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1703 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1704 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1705 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1706 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1707 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1708 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1709 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1710 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1711 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1712 | /* Set up the transaction-counter change checking flags when |
| 1713 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1714 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1715 | ** write operation (not a hot journal rollback). |
| 1716 | */ |
| 1717 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1718 | && pFile->eFileLock<=SHARED_LOCK |
| 1719 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1720 | ){ |
| 1721 | pFile->transCntrChng = 0; |
| 1722 | pFile->dbUpdate = 0; |
| 1723 | pFile->inNormalWrite = 1; |
| 1724 | } |
| 1725 | #endif |
| 1726 | |
| 1727 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1728 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1729 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1730 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1731 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1732 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1733 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1734 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1735 | |
| 1736 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1737 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1738 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1739 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1740 | return rc; |
| 1741 | } |
| 1742 | |
| 1743 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1744 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1745 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1746 | */ |
| 1747 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1748 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1749 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1750 | p->pNext = pInode->pUnused; |
| 1751 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1752 | pFile->h = -1; |
| 1753 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1757 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1758 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1759 | ** |
| 1760 | ** If the locking level of the file descriptor is already at or below |
| 1761 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1762 | ** |
| 1763 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1764 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1765 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1766 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1767 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1768 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1769 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1770 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1771 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1772 | struct flock lock; |
| 1773 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1774 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1775 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1776 | 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] | 1777 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1778 | osGetpid(0))); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1779 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1780 | assert( eFileLock<=SHARED_LOCK ); |
| 1781 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1782 | return SQLITE_OK; |
| 1783 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1784 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1785 | pInode = pFile->pInode; |
| 1786 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1787 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1788 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1789 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1790 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1791 | /* When reducing a lock such that other processes can start |
| 1792 | ** reading the database file again, make sure that the |
| 1793 | ** transaction counter was updated if any part of the database |
| 1794 | ** file changed. If the transaction counter is not updated, |
| 1795 | ** other connections to the same file might not realize that |
| 1796 | ** the file has changed and hence might not know to flush their |
| 1797 | ** cache. The use of a stale cache can lead to database corruption. |
| 1798 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1799 | pFile->inNormalWrite = 0; |
| 1800 | #endif |
| 1801 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1802 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1803 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1804 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1805 | ** write lock until the rest is covered by a read lock: |
| 1806 | ** 1: [WWWWW] |
| 1807 | ** 2: [....W] |
| 1808 | ** 3: [RRRRW] |
| 1809 | ** 4: [RRRR.] |
| 1810 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1811 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1812 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1813 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1814 | assert( handleNFSUnlock==0 ); |
| 1815 | #endif |
| 1816 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1817 | if( handleNFSUnlock ){ |
drh | a712b4b | 2015-02-19 16:12:04 +0000 | [diff] [blame] | 1818 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1819 | off_t divSize = SHARED_SIZE - 1; |
| 1820 | |
| 1821 | lock.l_type = F_UNLCK; |
| 1822 | lock.l_whence = SEEK_SET; |
| 1823 | lock.l_start = SHARED_FIRST; |
| 1824 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1825 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1826 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1827 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1828 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1829 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1830 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1831 | lock.l_type = F_RDLCK; |
| 1832 | lock.l_whence = SEEK_SET; |
| 1833 | lock.l_start = SHARED_FIRST; |
| 1834 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1835 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1836 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1837 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1838 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1839 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1840 | } |
| 1841 | goto end_unlock; |
| 1842 | } |
| 1843 | lock.l_type = F_UNLCK; |
| 1844 | lock.l_whence = SEEK_SET; |
| 1845 | lock.l_start = SHARED_FIRST+divSize; |
| 1846 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1847 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1848 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1849 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1850 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1851 | goto end_unlock; |
| 1852 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1853 | }else |
| 1854 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1855 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1856 | lock.l_type = F_RDLCK; |
| 1857 | lock.l_whence = SEEK_SET; |
| 1858 | lock.l_start = SHARED_FIRST; |
| 1859 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1860 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1861 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1862 | ** process is holding an incompatible lock. If it does, this |
| 1863 | ** indicates that the other process is not following the locking |
| 1864 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1865 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1866 | ** an assert to fail). */ |
| 1867 | rc = SQLITE_IOERR_RDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1868 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1869 | goto end_unlock; |
| 1870 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1873 | lock.l_type = F_UNLCK; |
| 1874 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1875 | lock.l_start = PENDING_BYTE; |
| 1876 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1877 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1878 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1879 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1880 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1881 | storeLastErrno(pFile, errno); |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1882 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1883 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1884 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1885 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1886 | /* Decrement the shared lock counter. Release the lock using an |
| 1887 | ** OS call only when all threads in this same process have released |
| 1888 | ** the lock. |
| 1889 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1890 | pInode->nShared--; |
| 1891 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1892 | lock.l_type = F_UNLCK; |
| 1893 | lock.l_whence = SEEK_SET; |
| 1894 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1895 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1896 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1897 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1898 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1899 | storeLastErrno(pFile, errno); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1900 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1901 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1902 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1905 | /* Decrement the count of locks against this same file. When the |
| 1906 | ** count reaches zero, close any other file descriptors whose close |
| 1907 | ** was deferred because of outstanding locks. |
| 1908 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1909 | pInode->nLock--; |
| 1910 | assert( pInode->nLock>=0 ); |
| 1911 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1912 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1913 | } |
| 1914 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1915 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1916 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1917 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1918 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1919 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1923 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1924 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1925 | ** |
| 1926 | ** If the locking level of the file descriptor is already at or below |
| 1927 | ** the requested locking level, this routine is a no-op. |
| 1928 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1929 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1930 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | a1afc74 | 2013-03-25 13:50:49 +0000 | [diff] [blame] | 1931 | assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1932 | #endif |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1933 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1936 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1937 | static int unixMapfile(unixFile *pFd, i64 nByte); |
| 1938 | static void unixUnmapfile(unixFile *pFd); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1939 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1940 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1941 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1942 | ** This function performs the parts of the "close file" operation |
| 1943 | ** common to all locking schemes. It closes the directory and file |
| 1944 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1945 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1946 | ** |
| 1947 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1948 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1949 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1950 | */ |
| 1951 | static int closeUnixFile(sqlite3_file *id){ |
| 1952 | unixFile *pFile = (unixFile*)id; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1953 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1954 | unixUnmapfile(pFile); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1955 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1956 | if( pFile->h>=0 ){ |
| 1957 | robust_close(pFile, pFile->h, __LINE__); |
| 1958 | pFile->h = -1; |
| 1959 | } |
| 1960 | #if OS_VXWORKS |
| 1961 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 1962 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 1963 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1964 | } |
| 1965 | vxworksReleaseFileId(pFile->pId); |
| 1966 | pFile->pId = 0; |
| 1967 | } |
| 1968 | #endif |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 1969 | #ifdef SQLITE_UNLINK_AFTER_CLOSE |
| 1970 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
| 1971 | osUnlink(pFile->zPath); |
| 1972 | sqlite3_free(*(char**)&pFile->zPath); |
| 1973 | pFile->zPath = 0; |
| 1974 | } |
| 1975 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1976 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1977 | OpenCounter(-1); |
| 1978 | sqlite3_free(pFile->pUnused); |
| 1979 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1980 | return SQLITE_OK; |
| 1981 | } |
| 1982 | |
| 1983 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1984 | ** Close a file. |
| 1985 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1986 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1987 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1988 | unixFile *pFile = (unixFile *)id; |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1989 | verifyDbFile(pFile); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1990 | unixUnlock(id, NO_LOCK); |
| 1991 | unixEnterMutex(); |
| 1992 | |
| 1993 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 1994 | ** routine (e.g. nolockClose()) would be called instead. |
| 1995 | */ |
| 1996 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 1997 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 1998 | /* If there are outstanding locks, do not actually close the file just |
| 1999 | ** yet because that would clear those locks. Instead, add the file |
| 2000 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 2001 | ** when the last lock is cleared. |
| 2002 | */ |
| 2003 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2004 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2005 | releaseInodeInfo(pFile); |
| 2006 | rc = closeUnixFile(id); |
| 2007 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2008 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2011 | /************** End of the posix advisory lock implementation ***************** |
| 2012 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2013 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2014 | /****************************************************************************** |
| 2015 | ****************************** No-op Locking ********************************** |
| 2016 | ** |
| 2017 | ** Of the various locking implementations available, this is by far the |
| 2018 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 2019 | ** file for reading or writing. |
| 2020 | ** |
| 2021 | ** This locking mode is appropriate for use on read-only databases |
| 2022 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 2023 | ** also be used if the application employs some external mechanism to |
| 2024 | ** prevent simultaneous access of the same database by two or more |
| 2025 | ** database connections. But there is a serious risk of database |
| 2026 | ** corruption if this locking mode is used in situations where multiple |
| 2027 | ** database connections are accessing the same database file at the same |
| 2028 | ** time and one or more of those connections are writing. |
| 2029 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2030 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2031 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 2032 | UNUSED_PARAMETER(NotUsed); |
| 2033 | *pResOut = 0; |
| 2034 | return SQLITE_OK; |
| 2035 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2036 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2037 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2038 | return SQLITE_OK; |
| 2039 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2040 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2041 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2042 | return SQLITE_OK; |
| 2043 | } |
| 2044 | |
| 2045 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2046 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2047 | */ |
| 2048 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2049 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /******************* End of the no-op lock implementation ********************* |
| 2053 | ******************************************************************************/ |
| 2054 | |
| 2055 | /****************************************************************************** |
| 2056 | ************************* Begin dot-file Locking ****************************** |
| 2057 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2058 | ** The dotfile locking implementation uses the existence of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2059 | ** files (really a directory) to control access to the database. This works |
| 2060 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2061 | ** |
| 2062 | ** (1) There is zero concurrency. A single reader blocks all other |
| 2063 | ** connections from reading or writing the database. |
| 2064 | ** |
| 2065 | ** (2) An application crash or power loss can leave stale lock files |
| 2066 | ** sitting around that need to be cleared manually. |
| 2067 | ** |
| 2068 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 2069 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2070 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2071 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 2072 | ** the database and with the same name but with a ".lock" extension added. |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2073 | ** The existence of a lock directory implies an EXCLUSIVE lock. All other |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2074 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2075 | */ |
| 2076 | |
| 2077 | /* |
| 2078 | ** 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] | 2079 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2080 | */ |
| 2081 | #define DOTLOCK_SUFFIX ".lock" |
| 2082 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2083 | /* |
| 2084 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2085 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2086 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2087 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2088 | ** |
| 2089 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 2090 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 2091 | ** is held on the file and false if the file is unlocked. |
| 2092 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2093 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2094 | int rc = SQLITE_OK; |
| 2095 | int reserved = 0; |
| 2096 | unixFile *pFile = (unixFile*)id; |
| 2097 | |
| 2098 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2099 | |
| 2100 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2101 | reserved = osAccess((const char*)pFile->lockingContext, 0)==0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2102 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2103 | *pResOut = reserved; |
| 2104 | return rc; |
| 2105 | } |
| 2106 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2107 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2108 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2109 | ** of the following: |
| 2110 | ** |
| 2111 | ** (1) SHARED_LOCK |
| 2112 | ** (2) RESERVED_LOCK |
| 2113 | ** (3) PENDING_LOCK |
| 2114 | ** (4) EXCLUSIVE_LOCK |
| 2115 | ** |
| 2116 | ** Sometimes when requesting one lock state, additional lock states |
| 2117 | ** are inserted in between. The locking might fail on one of the later |
| 2118 | ** transitions leaving the lock state different from what it started but |
| 2119 | ** still short of its goal. The following chart shows the allowed |
| 2120 | ** transitions and the inserted intermediate states: |
| 2121 | ** |
| 2122 | ** UNLOCKED -> SHARED |
| 2123 | ** SHARED -> RESERVED |
| 2124 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2125 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2126 | ** PENDING -> EXCLUSIVE |
| 2127 | ** |
| 2128 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2129 | ** routine to lower a locking level. |
| 2130 | ** |
| 2131 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 2132 | ** But we track the other locking levels internally. |
| 2133 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2134 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2135 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2136 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2137 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2138 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2139 | |
| 2140 | /* If we have any lock, then the lock file already exists. All we have |
| 2141 | ** to do is adjust our internal record of the lock level. |
| 2142 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2143 | if( pFile->eFileLock > NO_LOCK ){ |
| 2144 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2145 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 2146 | #ifdef HAVE_UTIME |
| 2147 | utime(zLockFile, NULL); |
| 2148 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2149 | utimes(zLockFile, NULL); |
| 2150 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2151 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2155 | rc = osMkdir(zLockFile, 0777); |
| 2156 | if( rc<0 ){ |
| 2157 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2158 | int tErrno = errno; |
| 2159 | if( EEXIST == tErrno ){ |
| 2160 | rc = SQLITE_BUSY; |
| 2161 | } else { |
| 2162 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2163 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2164 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2165 | } |
| 2166 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2167 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2168 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2169 | |
| 2170 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2171 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2172 | return rc; |
| 2173 | } |
| 2174 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2175 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2176 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2177 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2178 | ** |
| 2179 | ** If the locking level of the file descriptor is already at or below |
| 2180 | ** the requested locking level, this routine is a no-op. |
| 2181 | ** |
| 2182 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2183 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2184 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2185 | unixFile *pFile = (unixFile*)id; |
| 2186 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2187 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2188 | |
| 2189 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2190 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2191 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2192 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2193 | |
| 2194 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2195 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2196 | return SQLITE_OK; |
| 2197 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2198 | |
| 2199 | /* To downgrade to shared, simply update our internal notion of the |
| 2200 | ** lock state. No need to mess with the file on disk. |
| 2201 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2202 | if( eFileLock==SHARED_LOCK ){ |
| 2203 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2204 | return SQLITE_OK; |
| 2205 | } |
| 2206 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2207 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2208 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2209 | rc = osRmdir(zLockFile); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2210 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2211 | int tErrno = errno; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2212 | if( tErrno==ENOENT ){ |
| 2213 | rc = SQLITE_OK; |
| 2214 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2215 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2216 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2217 | } |
| 2218 | return rc; |
| 2219 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2220 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2221 | return SQLITE_OK; |
| 2222 | } |
| 2223 | |
| 2224 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2225 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2226 | */ |
| 2227 | static int dotlockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2228 | unixFile *pFile = (unixFile*)id; |
| 2229 | assert( id!=0 ); |
| 2230 | dotlockUnlock(id, NO_LOCK); |
| 2231 | sqlite3_free(pFile->lockingContext); |
| 2232 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2233 | } |
| 2234 | /****************** End of the dot-file lock implementation ******************* |
| 2235 | ******************************************************************************/ |
| 2236 | |
| 2237 | /****************************************************************************** |
| 2238 | ************************** Begin flock Locking ******************************** |
| 2239 | ** |
| 2240 | ** Use the flock() system call to do file locking. |
| 2241 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2242 | ** flock() locking is like dot-file locking in that the various |
| 2243 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2244 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2245 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2246 | ** still works when you do this, but concurrency is reduced since |
| 2247 | ** only a single process can be reading the database at a time. |
| 2248 | ** |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2249 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2250 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2251 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2252 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2253 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2254 | ** Retry flock() calls that fail with EINTR |
| 2255 | */ |
| 2256 | #ifdef EINTR |
| 2257 | static int robust_flock(int fd, int op){ |
| 2258 | int rc; |
| 2259 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2260 | return rc; |
| 2261 | } |
| 2262 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2263 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2264 | #endif |
| 2265 | |
| 2266 | |
| 2267 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2268 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2269 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2270 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2271 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2272 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2273 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2274 | int rc = SQLITE_OK; |
| 2275 | int reserved = 0; |
| 2276 | unixFile *pFile = (unixFile*)id; |
| 2277 | |
| 2278 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2279 | |
| 2280 | assert( pFile ); |
| 2281 | |
| 2282 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2283 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2284 | reserved = 1; |
| 2285 | } |
| 2286 | |
| 2287 | /* Otherwise see if some other process holds it. */ |
| 2288 | if( !reserved ){ |
| 2289 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2290 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2291 | if( !lrc ){ |
| 2292 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2293 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2294 | if ( lrc ) { |
| 2295 | int tErrno = errno; |
| 2296 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2297 | lrc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2298 | storeLastErrno(pFile, tErrno); |
| 2299 | rc = lrc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } else { |
| 2302 | int tErrno = errno; |
| 2303 | reserved = 1; |
| 2304 | /* someone else might have it reserved */ |
| 2305 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2306 | if( IS_LOCK_ERROR(lrc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2307 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2308 | rc = lrc; |
| 2309 | } |
| 2310 | } |
| 2311 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2312 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2313 | |
| 2314 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2315 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2316 | rc = SQLITE_OK; |
| 2317 | reserved=1; |
| 2318 | } |
| 2319 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2320 | *pResOut = reserved; |
| 2321 | return rc; |
| 2322 | } |
| 2323 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2324 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2325 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2326 | ** of the following: |
| 2327 | ** |
| 2328 | ** (1) SHARED_LOCK |
| 2329 | ** (2) RESERVED_LOCK |
| 2330 | ** (3) PENDING_LOCK |
| 2331 | ** (4) EXCLUSIVE_LOCK |
| 2332 | ** |
| 2333 | ** Sometimes when requesting one lock state, additional lock states |
| 2334 | ** are inserted in between. The locking might fail on one of the later |
| 2335 | ** transitions leaving the lock state different from what it started but |
| 2336 | ** still short of its goal. The following chart shows the allowed |
| 2337 | ** transitions and the inserted intermediate states: |
| 2338 | ** |
| 2339 | ** UNLOCKED -> SHARED |
| 2340 | ** SHARED -> RESERVED |
| 2341 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2342 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2343 | ** PENDING -> EXCLUSIVE |
| 2344 | ** |
| 2345 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2346 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2347 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2348 | ** access the file. |
| 2349 | ** |
| 2350 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2351 | ** routine to lower a locking level. |
| 2352 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2353 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2354 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2355 | unixFile *pFile = (unixFile*)id; |
| 2356 | |
| 2357 | assert( pFile ); |
| 2358 | |
| 2359 | /* if we already have a lock, it is exclusive. |
| 2360 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2361 | if (pFile->eFileLock > NO_LOCK) { |
| 2362 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2363 | return SQLITE_OK; |
| 2364 | } |
| 2365 | |
| 2366 | /* grab an exclusive lock */ |
| 2367 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2368 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2369 | int tErrno = errno; |
| 2370 | /* didn't get, must be busy */ |
| 2371 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2372 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2373 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2374 | } |
| 2375 | } else { |
| 2376 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2377 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2378 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2379 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2380 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2381 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2382 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2383 | rc = SQLITE_BUSY; |
| 2384 | } |
| 2385 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2386 | return rc; |
| 2387 | } |
| 2388 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2389 | |
| 2390 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2391 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2392 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2393 | ** |
| 2394 | ** If the locking level of the file descriptor is already at or below |
| 2395 | ** the requested locking level, this routine is a no-op. |
| 2396 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2397 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2398 | unixFile *pFile = (unixFile*)id; |
| 2399 | |
| 2400 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2401 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2402 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2403 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2404 | |
| 2405 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2406 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2407 | return SQLITE_OK; |
| 2408 | } |
| 2409 | |
| 2410 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2411 | if (eFileLock==SHARED_LOCK) { |
| 2412 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2413 | return SQLITE_OK; |
| 2414 | } |
| 2415 | |
| 2416 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2417 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2418 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2419 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2420 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2421 | return SQLITE_IOERR_UNLOCK; |
| 2422 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2423 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2424 | return SQLITE_OK; |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | /* |
| 2429 | ** Close a file. |
| 2430 | */ |
| 2431 | static int flockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2432 | assert( id!=0 ); |
| 2433 | flockUnlock(id, NO_LOCK); |
| 2434 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2438 | |
| 2439 | /******************* End of the flock lock implementation ********************* |
| 2440 | ******************************************************************************/ |
| 2441 | |
| 2442 | /****************************************************************************** |
| 2443 | ************************ Begin Named Semaphore Locking ************************ |
| 2444 | ** |
| 2445 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2446 | ** |
| 2447 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2448 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2449 | ** the database file at a time. This reduces potential concurrency, but |
| 2450 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2451 | */ |
| 2452 | #if OS_VXWORKS |
| 2453 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2454 | /* |
| 2455 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2456 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2457 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2458 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2459 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2460 | static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2461 | int rc = SQLITE_OK; |
| 2462 | int reserved = 0; |
| 2463 | unixFile *pFile = (unixFile*)id; |
| 2464 | |
| 2465 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2466 | |
| 2467 | assert( pFile ); |
| 2468 | |
| 2469 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2470 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2471 | reserved = 1; |
| 2472 | } |
| 2473 | |
| 2474 | /* Otherwise see if some other process holds it. */ |
| 2475 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2476 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2477 | |
| 2478 | if( sem_trywait(pSem)==-1 ){ |
| 2479 | int tErrno = errno; |
| 2480 | if( EAGAIN != tErrno ){ |
| 2481 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2482 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2483 | } else { |
| 2484 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2485 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2486 | } |
| 2487 | }else{ |
| 2488 | /* we could have it if we want it */ |
| 2489 | sem_post(pSem); |
| 2490 | } |
| 2491 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2492 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2493 | |
| 2494 | *pResOut = reserved; |
| 2495 | return rc; |
| 2496 | } |
| 2497 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2498 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2499 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2500 | ** of the following: |
| 2501 | ** |
| 2502 | ** (1) SHARED_LOCK |
| 2503 | ** (2) RESERVED_LOCK |
| 2504 | ** (3) PENDING_LOCK |
| 2505 | ** (4) EXCLUSIVE_LOCK |
| 2506 | ** |
| 2507 | ** Sometimes when requesting one lock state, additional lock states |
| 2508 | ** are inserted in between. The locking might fail on one of the later |
| 2509 | ** transitions leaving the lock state different from what it started but |
| 2510 | ** still short of its goal. The following chart shows the allowed |
| 2511 | ** transitions and the inserted intermediate states: |
| 2512 | ** |
| 2513 | ** UNLOCKED -> SHARED |
| 2514 | ** SHARED -> RESERVED |
| 2515 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2516 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2517 | ** PENDING -> EXCLUSIVE |
| 2518 | ** |
| 2519 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2520 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2521 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2522 | ** access the file. |
| 2523 | ** |
| 2524 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2525 | ** routine to lower a locking level. |
| 2526 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2527 | static int semXLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2528 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2529 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2530 | int rc = SQLITE_OK; |
| 2531 | |
| 2532 | /* if we already have a lock, it is exclusive. |
| 2533 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2534 | if (pFile->eFileLock > NO_LOCK) { |
| 2535 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2536 | rc = SQLITE_OK; |
| 2537 | goto sem_end_lock; |
| 2538 | } |
| 2539 | |
| 2540 | /* lock semaphore now but bail out when already locked. */ |
| 2541 | if( sem_trywait(pSem)==-1 ){ |
| 2542 | rc = SQLITE_BUSY; |
| 2543 | goto sem_end_lock; |
| 2544 | } |
| 2545 | |
| 2546 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2547 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2548 | |
| 2549 | sem_end_lock: |
| 2550 | return rc; |
| 2551 | } |
| 2552 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2553 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2554 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2555 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2556 | ** |
| 2557 | ** If the locking level of the file descriptor is already at or below |
| 2558 | ** the requested locking level, this routine is a no-op. |
| 2559 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2560 | static int semXUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2561 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2562 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2563 | |
| 2564 | assert( pFile ); |
| 2565 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2566 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2567 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2568 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2569 | |
| 2570 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2571 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2572 | return SQLITE_OK; |
| 2573 | } |
| 2574 | |
| 2575 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2576 | if (eFileLock==SHARED_LOCK) { |
| 2577 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2578 | return SQLITE_OK; |
| 2579 | } |
| 2580 | |
| 2581 | /* no, really unlock. */ |
| 2582 | if ( sem_post(pSem)==-1 ) { |
| 2583 | int rc, tErrno = errno; |
| 2584 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2585 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2586 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2587 | } |
| 2588 | return rc; |
| 2589 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2590 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2591 | return SQLITE_OK; |
| 2592 | } |
| 2593 | |
| 2594 | /* |
| 2595 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2596 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2597 | static int semXClose(sqlite3_file *id) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2598 | if( id ){ |
| 2599 | unixFile *pFile = (unixFile*)id; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2600 | semXUnlock(id, NO_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2601 | assert( pFile ); |
| 2602 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2603 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2604 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2605 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2606 | } |
| 2607 | return SQLITE_OK; |
| 2608 | } |
| 2609 | |
| 2610 | #endif /* OS_VXWORKS */ |
| 2611 | /* |
| 2612 | ** Named semaphore locking is only available on VxWorks. |
| 2613 | ** |
| 2614 | *************** End of the named semaphore lock implementation **************** |
| 2615 | ******************************************************************************/ |
| 2616 | |
| 2617 | |
| 2618 | /****************************************************************************** |
| 2619 | *************************** Begin AFP Locking ********************************* |
| 2620 | ** |
| 2621 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2622 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2623 | ** |
| 2624 | ** Third-party implementations of AFP are available. But this code here |
| 2625 | ** only works on OSX. |
| 2626 | */ |
| 2627 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2628 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2629 | /* |
| 2630 | ** The afpLockingContext structure contains all afp lock specific state |
| 2631 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2632 | typedef struct afpLockingContext afpLockingContext; |
| 2633 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2634 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2635 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2636 | }; |
| 2637 | |
| 2638 | struct ByteRangeLockPB2 |
| 2639 | { |
| 2640 | unsigned long long offset; /* offset to first byte to lock */ |
| 2641 | unsigned long long length; /* nbr of bytes to lock */ |
| 2642 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2643 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2644 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2645 | int fd; /* file desc to assoc this lock with */ |
| 2646 | }; |
| 2647 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2648 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2649 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2650 | /* |
| 2651 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2652 | ** AFP filesystem. |
| 2653 | ** |
| 2654 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2655 | */ |
| 2656 | static int afpSetLock( |
| 2657 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2658 | unixFile *pFile, /* Open file descriptor on path */ |
| 2659 | unsigned long long offset, /* First byte to be locked */ |
| 2660 | unsigned long long length, /* Number of bytes to lock */ |
| 2661 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2662 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2663 | struct ByteRangeLockPB2 pb; |
| 2664 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2665 | |
| 2666 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2667 | pb.startEndFlag = 0; |
| 2668 | pb.offset = offset; |
| 2669 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2670 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2671 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2672 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2673 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2674 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2675 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2676 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2677 | int rc; |
| 2678 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2679 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2680 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2681 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2682 | rc = SQLITE_BUSY; |
| 2683 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2684 | rc = sqliteErrorFromPosixError(tErrno, |
| 2685 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2686 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2687 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2688 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2689 | } |
| 2690 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2691 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2692 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2696 | /* |
| 2697 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2698 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2699 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2700 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2701 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2702 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2703 | int rc = SQLITE_OK; |
| 2704 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2705 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2706 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2707 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2708 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2709 | |
| 2710 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2711 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2712 | if( context->reserved ){ |
| 2713 | *pResOut = 1; |
| 2714 | return SQLITE_OK; |
| 2715 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2716 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2717 | |
| 2718 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2719 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2720 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | /* Otherwise see if some other process holds it. |
| 2724 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2725 | if( !reserved ){ |
| 2726 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2727 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2728 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2729 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2730 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2731 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2732 | } else { |
| 2733 | /* if we failed to get the lock then someone else must have it */ |
| 2734 | reserved = 1; |
| 2735 | } |
| 2736 | if( IS_LOCK_ERROR(lrc) ){ |
| 2737 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2738 | } |
| 2739 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2740 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2741 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2742 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2743 | |
| 2744 | *pResOut = reserved; |
| 2745 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2748 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2749 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2750 | ** of the following: |
| 2751 | ** |
| 2752 | ** (1) SHARED_LOCK |
| 2753 | ** (2) RESERVED_LOCK |
| 2754 | ** (3) PENDING_LOCK |
| 2755 | ** (4) EXCLUSIVE_LOCK |
| 2756 | ** |
| 2757 | ** Sometimes when requesting one lock state, additional lock states |
| 2758 | ** are inserted in between. The locking might fail on one of the later |
| 2759 | ** transitions leaving the lock state different from what it started but |
| 2760 | ** still short of its goal. The following chart shows the allowed |
| 2761 | ** transitions and the inserted intermediate states: |
| 2762 | ** |
| 2763 | ** UNLOCKED -> SHARED |
| 2764 | ** SHARED -> RESERVED |
| 2765 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2766 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2767 | ** PENDING -> EXCLUSIVE |
| 2768 | ** |
| 2769 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2770 | ** routine to lower a locking level. |
| 2771 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2772 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2773 | int rc = SQLITE_OK; |
| 2774 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2775 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2776 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2777 | |
| 2778 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2779 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2780 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2781 | azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2782 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2783 | /* 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] | 2784 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2785 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2786 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2787 | if( pFile->eFileLock>=eFileLock ){ |
| 2788 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2789 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2790 | return SQLITE_OK; |
| 2791 | } |
| 2792 | |
| 2793 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2794 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2795 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2796 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2797 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2798 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2799 | assert( eFileLock!=PENDING_LOCK ); |
| 2800 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2801 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2802 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2803 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2804 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2805 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2806 | |
| 2807 | /* If some thread using this PID has a lock via a different unixFile* |
| 2808 | ** handle that precludes the requested lock, return BUSY. |
| 2809 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2810 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2811 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2812 | ){ |
| 2813 | rc = SQLITE_BUSY; |
| 2814 | goto afp_end_lock; |
| 2815 | } |
| 2816 | |
| 2817 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2818 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2819 | ** return SQLITE_OK. |
| 2820 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2821 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2822 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2823 | assert( eFileLock==SHARED_LOCK ); |
| 2824 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2825 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2826 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2827 | pInode->nShared++; |
| 2828 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2829 | goto afp_end_lock; |
| 2830 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2831 | |
| 2832 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2833 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2834 | ** be released. |
| 2835 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2836 | if( eFileLock==SHARED_LOCK |
| 2837 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2838 | ){ |
| 2839 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2840 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2841 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2842 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2843 | goto afp_end_lock; |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2848 | ** operating system calls for the specified lock. |
| 2849 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2850 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2851 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2852 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2853 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2854 | assert( pInode->nShared==0 ); |
| 2855 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2856 | |
| 2857 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2858 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2859 | /* note that the quality of the randomness doesn't matter that much */ |
| 2860 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2861 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2862 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2863 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2864 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2865 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2866 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2867 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2868 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2869 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2870 | if( IS_LOCK_ERROR(lrc1) ) { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2871 | storeLastErrno(pFile, lrc1Errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2872 | rc = lrc1; |
| 2873 | goto afp_end_lock; |
| 2874 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2875 | rc = lrc2; |
| 2876 | goto afp_end_lock; |
| 2877 | } else if( lrc1 != SQLITE_OK ) { |
| 2878 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2879 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2880 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2881 | pInode->nLock++; |
| 2882 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2883 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2884 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2885 | /* We are trying for an exclusive lock but another thread in this |
| 2886 | ** same process is still holding a shared lock. */ |
| 2887 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2888 | }else{ |
| 2889 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2890 | ** assumed that there is a SHARED or greater lock on the file |
| 2891 | ** already. |
| 2892 | */ |
| 2893 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2894 | assert( 0!=pFile->eFileLock ); |
| 2895 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2896 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2897 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2898 | if( !failed ){ |
| 2899 | context->reserved = 1; |
| 2900 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2901 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2902 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2903 | /* Acquire an EXCLUSIVE lock */ |
| 2904 | |
| 2905 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2906 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2907 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2908 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2909 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2910 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2911 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2912 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2913 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2914 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2915 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2916 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2917 | ** a critical I/O error |
| 2918 | */ |
| 2919 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2920 | SQLITE_IOERR_LOCK; |
| 2921 | goto afp_end_lock; |
| 2922 | } |
| 2923 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2924 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2925 | } |
| 2926 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2927 | if( failed ){ |
| 2928 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2933 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2934 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2935 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2936 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2937 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2941 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2942 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2943 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2944 | return rc; |
| 2945 | } |
| 2946 | |
| 2947 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2948 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2949 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2950 | ** |
| 2951 | ** If the locking level of the file descriptor is already at or below |
| 2952 | ** the requested locking level, this routine is a no-op. |
| 2953 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2954 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2955 | int rc = SQLITE_OK; |
| 2956 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2957 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2958 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2959 | int skipShared = 0; |
| 2960 | #ifdef SQLITE_TEST |
| 2961 | int h = pFile->h; |
| 2962 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2963 | |
| 2964 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2965 | 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] | 2966 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2967 | osGetpid(0))); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2968 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2969 | assert( eFileLock<=SHARED_LOCK ); |
| 2970 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2971 | return SQLITE_OK; |
| 2972 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2973 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2974 | pInode = pFile->pInode; |
| 2975 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2976 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2977 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2978 | SimulateIOErrorBenign(1); |
| 2979 | SimulateIOError( h=(-1) ) |
| 2980 | SimulateIOErrorBenign(0); |
| 2981 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 2982 | #ifdef SQLITE_DEBUG |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2983 | /* When reducing a lock such that other processes can start |
| 2984 | ** reading the database file again, make sure that the |
| 2985 | ** transaction counter was updated if any part of the database |
| 2986 | ** file changed. If the transaction counter is not updated, |
| 2987 | ** other connections to the same file might not realize that |
| 2988 | ** the file has changed and hence might not know to flush their |
| 2989 | ** cache. The use of a stale cache can lead to database corruption. |
| 2990 | */ |
| 2991 | assert( pFile->inNormalWrite==0 |
| 2992 | || pFile->dbUpdate==0 |
| 2993 | || pFile->transCntrChng==1 ); |
| 2994 | pFile->inNormalWrite = 0; |
| 2995 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2996 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2997 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2998 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2999 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3000 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3001 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3002 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 3003 | } else { |
| 3004 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3005 | } |
| 3006 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3007 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3008 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3009 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3010 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3011 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 3012 | if( !rc ){ |
| 3013 | context->reserved = 0; |
| 3014 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3015 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3016 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 3017 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3018 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3019 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3020 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3021 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3022 | /* Decrement the shared lock counter. Release the lock using an |
| 3023 | ** OS call only when all threads in this same process have released |
| 3024 | ** the lock. |
| 3025 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3026 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 3027 | pInode->nShared--; |
| 3028 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3029 | SimulateIOErrorBenign(1); |
| 3030 | SimulateIOError( h=(-1) ) |
| 3031 | SimulateIOErrorBenign(0); |
| 3032 | if( !skipShared ){ |
| 3033 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 3034 | } |
| 3035 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3036 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3037 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3038 | } |
| 3039 | } |
| 3040 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3041 | pInode->nLock--; |
| 3042 | assert( pInode->nLock>=0 ); |
| 3043 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3044 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3045 | } |
| 3046 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3047 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3048 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3049 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3050 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3051 | return rc; |
| 3052 | } |
| 3053 | |
| 3054 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3055 | ** Close a file & cleanup AFP specific locking context |
| 3056 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3057 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3058 | int rc = SQLITE_OK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3059 | unixFile *pFile = (unixFile*)id; |
| 3060 | assert( id!=0 ); |
| 3061 | afpUnlock(id, NO_LOCK); |
| 3062 | unixEnterMutex(); |
| 3063 | if( pFile->pInode && pFile->pInode->nLock ){ |
| 3064 | /* If there are outstanding locks, do not actually close the file just |
| 3065 | ** yet because that would clear those locks. Instead, add the file |
| 3066 | ** descriptor to pInode->aPending. It will be automatically closed when |
| 3067 | ** the last lock is cleared. |
| 3068 | */ |
| 3069 | setPendingFd(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3070 | } |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3071 | releaseInodeInfo(pFile); |
| 3072 | sqlite3_free(pFile->lockingContext); |
| 3073 | rc = closeUnixFile(id); |
| 3074 | unixLeaveMutex(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3075 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3078 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3079 | /* |
| 3080 | ** The code above is the AFP lock implementation. The code is specific |
| 3081 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3082 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 3083 | ** VFS is not available. |
| 3084 | ** |
| 3085 | ********************* End of the AFP lock implementation ********************** |
| 3086 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3087 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3088 | /****************************************************************************** |
| 3089 | *************************** Begin NFS Locking ********************************/ |
| 3090 | |
| 3091 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3092 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3093 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3094 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3095 | ** |
| 3096 | ** If the locking level of the file descriptor is already at or below |
| 3097 | ** the requested locking level, this routine is a no-op. |
| 3098 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3099 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 3100 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3104 | /* |
| 3105 | ** The code above is the NFS lock implementation. The code is specific |
| 3106 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3107 | ** is available. |
| 3108 | ** |
| 3109 | ********************* End of the NFS lock implementation ********************** |
| 3110 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3111 | |
| 3112 | /****************************************************************************** |
| 3113 | **************** Non-locking sqlite3_file methods ***************************** |
| 3114 | ** |
| 3115 | ** The next division contains implementations for all methods of the |
| 3116 | ** sqlite3_file object other than the locking methods. The locking |
| 3117 | ** methods were defined in divisions above (one locking method per |
| 3118 | ** division). Those methods that are common to all locking modes |
| 3119 | ** are gather together into this division. |
| 3120 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3121 | |
| 3122 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3123 | ** Seek to the offset passed as the second argument, then read cnt |
| 3124 | ** bytes into pBuf. Return the number of bytes actually read. |
| 3125 | ** |
| 3126 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 3127 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 3128 | ** one system to another. Since SQLite does not define USE_PREAD |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3129 | ** in any form by default, we will not attempt to define _XOPEN_SOURCE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3130 | ** See tickets #2741 and #2681. |
| 3131 | ** |
| 3132 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 3133 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3134 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3135 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 3136 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3137 | int prior = 0; |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3138 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
| 3139 | i64 newOffset; |
| 3140 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3141 | TIMER_START; |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3142 | assert( cnt==(cnt&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3143 | assert( id->h>2 ); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3144 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3145 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3146 | got = osPread(id->h, pBuf, cnt, offset); |
| 3147 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3148 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3149 | got = osPread64(id->h, pBuf, cnt, offset); |
| 3150 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3151 | #else |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3152 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3153 | SimulateIOError( newOffset = -1 ); |
| 3154 | if( newOffset<0 ){ |
| 3155 | storeLastErrno((unixFile*)id, errno); |
| 3156 | return -1; |
| 3157 | } |
| 3158 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3159 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3160 | if( got==cnt ) break; |
| 3161 | if( got<0 ){ |
| 3162 | if( errno==EINTR ){ got = 1; continue; } |
| 3163 | prior = 0; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3164 | storeLastErrno((unixFile*)id, errno); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3165 | break; |
| 3166 | }else if( got>0 ){ |
| 3167 | cnt -= got; |
| 3168 | offset += got; |
| 3169 | prior += got; |
| 3170 | pBuf = (void*)(got + (char*)pBuf); |
| 3171 | } |
| 3172 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3173 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3174 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3175 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3176 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3180 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3181 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3182 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3183 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3184 | static int unixRead( |
| 3185 | sqlite3_file *id, |
| 3186 | void *pBuf, |
| 3187 | int amt, |
| 3188 | sqlite3_int64 offset |
| 3189 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3190 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3191 | int got; |
| 3192 | assert( id ); |
drh | 6cf9d8d | 2013-05-09 18:12:40 +0000 | [diff] [blame] | 3193 | assert( offset>=0 ); |
| 3194 | assert( amt>0 ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3195 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3196 | /* If this is a database file (not a journal, master-journal or temp |
| 3197 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3198 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3199 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3200 | || offset>=PENDING_BYTE+512 |
| 3201 | || offset+amt<=PENDING_BYTE |
| 3202 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3203 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3204 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3205 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 6c56963 | 2013-03-26 18:48:11 +0000 | [diff] [blame] | 3206 | /* Deal with as much of this read request as possible by transfering |
| 3207 | ** data from the memory mapping using memcpy(). */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3208 | if( offset<pFile->mmapSize ){ |
| 3209 | if( offset+amt <= pFile->mmapSize ){ |
| 3210 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 3211 | return SQLITE_OK; |
| 3212 | }else{ |
| 3213 | int nCopy = pFile->mmapSize - offset; |
| 3214 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 3215 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3216 | amt -= nCopy; |
| 3217 | offset += nCopy; |
| 3218 | } |
| 3219 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3220 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3221 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3222 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3223 | if( got==amt ){ |
| 3224 | return SQLITE_OK; |
| 3225 | }else if( got<0 ){ |
| 3226 | /* lastErrno set by seekAndRead */ |
| 3227 | return SQLITE_IOERR_READ; |
| 3228 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3229 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3230 | /* Unread parts of the buffer must be zero-filled */ |
| 3231 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3232 | return SQLITE_IOERR_SHORT_READ; |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | /* |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3237 | ** Attempt to seek the file-descriptor passed as the first argument to |
| 3238 | ** absolute offset iOff, then attempt to write nBuf bytes of data from |
| 3239 | ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, |
| 3240 | ** return the actual number of bytes written (which may be less than |
| 3241 | ** nBuf). |
| 3242 | */ |
| 3243 | static int seekAndWriteFd( |
| 3244 | int fd, /* File descriptor to write to */ |
| 3245 | i64 iOff, /* File offset to begin writing at */ |
| 3246 | const void *pBuf, /* Copy data from this buffer to the file */ |
| 3247 | int nBuf, /* Size of buffer pBuf in bytes */ |
| 3248 | int *piErrno /* OUT: Error number if error occurs */ |
| 3249 | ){ |
| 3250 | int rc = 0; /* Value returned by system call */ |
| 3251 | |
| 3252 | assert( nBuf==(nBuf&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3253 | assert( fd>2 ); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3254 | assert( piErrno!=0 ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3255 | nBuf &= 0x1ffff; |
| 3256 | TIMER_START; |
| 3257 | |
| 3258 | #if defined(USE_PREAD) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3259 | do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3260 | #elif defined(USE_PREAD64) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3261 | do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3262 | #else |
| 3263 | do{ |
| 3264 | i64 iSeek = lseek(fd, iOff, SEEK_SET); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3265 | SimulateIOError( iSeek = -1 ); |
| 3266 | if( iSeek<0 ){ |
| 3267 | rc = -1; |
| 3268 | break; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3269 | } |
| 3270 | rc = osWrite(fd, pBuf, nBuf); |
| 3271 | }while( rc<0 && errno==EINTR ); |
| 3272 | #endif |
| 3273 | |
| 3274 | TIMER_END; |
| 3275 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); |
| 3276 | |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3277 | if( rc<0 ) *piErrno = errno; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3278 | return rc; |
| 3279 | } |
| 3280 | |
| 3281 | |
| 3282 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3283 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3284 | ** Return the number of bytes actually read. Update the offset. |
| 3285 | ** |
| 3286 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3287 | ** is set before returning. |
| 3288 | */ |
| 3289 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3290 | return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | |
| 3294 | /* |
| 3295 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3296 | ** or some other error code on failure. |
| 3297 | */ |
| 3298 | static int unixWrite( |
| 3299 | sqlite3_file *id, |
| 3300 | const void *pBuf, |
| 3301 | int amt, |
| 3302 | sqlite3_int64 offset |
| 3303 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3304 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3305 | int wrote = 0; |
| 3306 | assert( id ); |
| 3307 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3308 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3309 | /* If this is a database file (not a journal, master-journal or temp |
| 3310 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3311 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3312 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3313 | || offset>=PENDING_BYTE+512 |
| 3314 | || offset+amt<=PENDING_BYTE |
| 3315 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3316 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3317 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3318 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3319 | /* If we are doing a normal write to a database file (as opposed to |
| 3320 | ** doing a hot-journal rollback or a write to some file other than a |
| 3321 | ** normal database file) then record the fact that the database |
| 3322 | ** has changed. If the transaction counter is modified, record that |
| 3323 | ** fact too. |
| 3324 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3325 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3326 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3327 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3328 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3329 | char oldCntr[4]; |
| 3330 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3331 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3332 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3333 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3334 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3335 | } |
| 3336 | } |
| 3337 | } |
| 3338 | #endif |
| 3339 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 3340 | #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3341 | /* Deal with as much of this write request as possible by transfering |
| 3342 | ** data from the memory mapping using memcpy(). */ |
| 3343 | if( offset<pFile->mmapSize ){ |
| 3344 | if( offset+amt <= pFile->mmapSize ){ |
| 3345 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 3346 | return SQLITE_OK; |
| 3347 | }else{ |
| 3348 | int nCopy = pFile->mmapSize - offset; |
| 3349 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3350 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3351 | amt -= nCopy; |
| 3352 | offset += nCopy; |
| 3353 | } |
| 3354 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3355 | #endif |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3356 | |
| 3357 | while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3358 | amt -= wrote; |
| 3359 | offset += wrote; |
| 3360 | pBuf = &((char*)pBuf)[wrote]; |
| 3361 | } |
| 3362 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3363 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3364 | |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3365 | if( amt>wrote ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3366 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3367 | /* lastErrno set by seekAndWrite */ |
| 3368 | return SQLITE_IOERR_WRITE; |
| 3369 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3370 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3371 | return SQLITE_FULL; |
| 3372 | } |
| 3373 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3374 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3375 | return SQLITE_OK; |
| 3376 | } |
| 3377 | |
| 3378 | #ifdef SQLITE_TEST |
| 3379 | /* |
| 3380 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3381 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3382 | */ |
| 3383 | int sqlite3_sync_count = 0; |
| 3384 | int sqlite3_fullsync_count = 0; |
| 3385 | #endif |
| 3386 | |
| 3387 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3388 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3389 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3390 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3391 | ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3392 | */ |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3393 | #if !defined(fdatasync) && !HAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3394 | # define fdatasync fsync |
| 3395 | #endif |
| 3396 | |
| 3397 | /* |
| 3398 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3399 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3400 | ** only available on Mac OS X. But that could change. |
| 3401 | */ |
| 3402 | #ifdef F_FULLFSYNC |
| 3403 | # define HAVE_FULLFSYNC 1 |
| 3404 | #else |
| 3405 | # define HAVE_FULLFSYNC 0 |
| 3406 | #endif |
| 3407 | |
| 3408 | |
| 3409 | /* |
| 3410 | ** The fsync() system call does not work as advertised on many |
| 3411 | ** unix systems. The following procedure is an attempt to make |
| 3412 | ** it work better. |
| 3413 | ** |
| 3414 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3415 | ** for testing when we want to run through the test suite quickly. |
| 3416 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3417 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3418 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3419 | ** |
| 3420 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3421 | ** The idea behind dataOnly is that it should only write the file content |
| 3422 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3423 | ** unchanged since the file size is part of the inode. However, |
| 3424 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3425 | ** file size has changed. The only real difference between fdatasync() |
| 3426 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3427 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3428 | ** We only care about the file size, not the other file attributes, so |
| 3429 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3430 | ** So, we always use fdatasync() if it is available, regardless of |
| 3431 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3432 | */ |
| 3433 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3434 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3435 | |
| 3436 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3437 | ** the one below. It is replicated here solely to avoid cluttering |
| 3438 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3439 | */ |
| 3440 | #ifdef SQLITE_NO_SYNC |
| 3441 | UNUSED_PARAMETER(fd); |
| 3442 | UNUSED_PARAMETER(fullSync); |
| 3443 | UNUSED_PARAMETER(dataOnly); |
| 3444 | #elif HAVE_FULLFSYNC |
| 3445 | UNUSED_PARAMETER(dataOnly); |
| 3446 | #else |
| 3447 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3448 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3449 | #endif |
| 3450 | |
| 3451 | /* Record the number of times that we do a normal fsync() and |
| 3452 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3453 | ** gets called with the correct arguments. |
| 3454 | */ |
| 3455 | #ifdef SQLITE_TEST |
| 3456 | if( fullSync ) sqlite3_fullsync_count++; |
| 3457 | sqlite3_sync_count++; |
| 3458 | #endif |
| 3459 | |
| 3460 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3461 | ** no-op. But go ahead and call fstat() to validate the file |
| 3462 | ** descriptor as we need a method to provoke a failure during |
| 3463 | ** coverate testing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3464 | */ |
| 3465 | #ifdef SQLITE_NO_SYNC |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3466 | { |
| 3467 | struct stat buf; |
| 3468 | rc = osFstat(fd, &buf); |
| 3469 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3470 | #elif HAVE_FULLFSYNC |
| 3471 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3472 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3473 | }else{ |
| 3474 | rc = 1; |
| 3475 | } |
| 3476 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3477 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3478 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3479 | ** isn't supported for this file system. So, attempt an fsync |
| 3480 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3481 | ** It'd be better to detect fullfsync support once and avoid |
| 3482 | ** the fcntl call every time sync is called. |
| 3483 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3484 | if( rc ) rc = fsync(fd); |
| 3485 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3486 | #elif defined(__APPLE__) |
| 3487 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3488 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3489 | */ |
| 3490 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3491 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3492 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3493 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3494 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3495 | rc = fsync(fd); |
| 3496 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3497 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3498 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3499 | |
| 3500 | if( OS_VXWORKS && rc!= -1 ){ |
| 3501 | rc = 0; |
| 3502 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3503 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3506 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3507 | ** Open a file descriptor to the directory containing file zFilename. |
| 3508 | ** If successful, *pFd is set to the opened file descriptor and |
| 3509 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3510 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3511 | ** value. |
| 3512 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3513 | ** The directory file descriptor is used for only one thing - to |
| 3514 | ** fsync() a directory to make sure file creation and deletion events |
| 3515 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3516 | ** journaling filesystems, but are required on older filesystems. |
| 3517 | ** |
| 3518 | ** This routine can be overridden using the xSetSysCall interface. |
| 3519 | ** The ability to override this routine was added in support of the |
| 3520 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3521 | ** told) so making it overrideable allows the chromium sandbox to |
| 3522 | ** replace this routine with a harmless no-op. To make this routine |
| 3523 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3524 | ** *pFd set to a negative number. |
| 3525 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3526 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3527 | ** the file descriptor *pFd using close(). |
| 3528 | */ |
| 3529 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3530 | int ii; |
| 3531 | int fd = -1; |
| 3532 | char zDirname[MAX_PATHNAME+1]; |
| 3533 | |
| 3534 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3535 | for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--); |
| 3536 | if( ii>0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3537 | zDirname[ii] = '\0'; |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3538 | }else{ |
| 3539 | if( zDirname[0]!='/' ) zDirname[0] = '.'; |
| 3540 | zDirname[1] = 0; |
| 3541 | } |
| 3542 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3543 | if( fd>=0 ){ |
| 3544 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3545 | } |
| 3546 | *pFd = fd; |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3547 | if( fd>=0 ) return SQLITE_OK; |
| 3548 | return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3552 | ** Make sure all writes to a particular file are committed to disk. |
| 3553 | ** |
| 3554 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3555 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3556 | ** file data is synced. |
| 3557 | ** |
| 3558 | ** Under Unix, also make sure that the directory entry for the file |
| 3559 | ** has been created by fsync-ing the directory that contains the file. |
| 3560 | ** If we do not do this and we encounter a power failure, the directory |
| 3561 | ** entry for the journal might not exist after we reboot. The next |
| 3562 | ** SQLite to access the file will not know that the journal exists (because |
| 3563 | ** the directory entry for the journal was never created) and the transaction |
| 3564 | ** will not roll back - possibly leading to database corruption. |
| 3565 | */ |
| 3566 | static int unixSync(sqlite3_file *id, int flags){ |
| 3567 | int rc; |
| 3568 | unixFile *pFile = (unixFile*)id; |
| 3569 | |
| 3570 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3571 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3572 | |
| 3573 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3574 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3575 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3576 | ); |
| 3577 | |
| 3578 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3579 | ** line is to test that doing so does not cause any problems. |
| 3580 | */ |
| 3581 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3582 | |
| 3583 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3584 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3585 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3586 | SimulateIOError( rc=1 ); |
| 3587 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3588 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3589 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3590 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3591 | |
| 3592 | /* Also fsync the directory containing the file if the DIRSYNC flag |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 3593 | ** is set. This is a one-time occurrence. Many systems (examples: AIX) |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3594 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3595 | */ |
| 3596 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3597 | int dirfd; |
| 3598 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3599 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3600 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3601 | if( rc==SQLITE_OK ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3602 | full_fsync(dirfd, 0, 0); |
| 3603 | robust_close(pFile, dirfd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3604 | }else{ |
| 3605 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3606 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3607 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3608 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3609 | } |
| 3610 | return rc; |
| 3611 | } |
| 3612 | |
| 3613 | /* |
| 3614 | ** Truncate an open file to a specified size |
| 3615 | */ |
| 3616 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3617 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3618 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3619 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3620 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3621 | |
| 3622 | /* If the user has configured a chunk-size for this file, truncate the |
| 3623 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3624 | ** actual file size after the operation may be larger than the requested |
| 3625 | ** size). |
| 3626 | */ |
drh | b8af4b7 | 2012-04-05 20:04:39 +0000 | [diff] [blame] | 3627 | if( pFile->szChunk>0 ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3628 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3629 | } |
| 3630 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 3631 | rc = robust_ftruncate(pFile->h, nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3632 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3633 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3634 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3635 | }else{ |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3636 | #ifdef SQLITE_DEBUG |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3637 | /* If we are doing a normal write to a database file (as opposed to |
| 3638 | ** doing a hot-journal rollback or a write to some file other than a |
| 3639 | ** normal database file) and we truncate the file to zero length, |
| 3640 | ** that effectively updates the change counter. This might happen |
| 3641 | ** when restoring a database using the backup API from a zero-length |
| 3642 | ** source. |
| 3643 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3644 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3645 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3646 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3647 | #endif |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3648 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3649 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3650 | /* If the file was just truncated to a size smaller than the currently |
| 3651 | ** mapped region, reduce the effective mapping size as well. SQLite will |
| 3652 | ** use read() and write() to access data beyond this point from now on. |
| 3653 | */ |
| 3654 | if( nByte<pFile->mmapSize ){ |
| 3655 | pFile->mmapSize = nByte; |
| 3656 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3657 | #endif |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3658 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3659 | return SQLITE_OK; |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | /* |
| 3664 | ** Determine the current size of a file in bytes |
| 3665 | */ |
| 3666 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3667 | int rc; |
| 3668 | struct stat buf; |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 3669 | assert( id ); |
| 3670 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3671 | SimulateIOError( rc=1 ); |
| 3672 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3673 | storeLastErrno((unixFile*)id, errno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3674 | return SQLITE_IOERR_FSTAT; |
| 3675 | } |
| 3676 | *pSize = buf.st_size; |
| 3677 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3678 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3679 | ** writes a single byte into that file in order to work around a bug |
| 3680 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3681 | ** layers, we need to report this file size as zero even though it is |
| 3682 | ** really 1. Ticket #3260. |
| 3683 | */ |
| 3684 | if( *pSize==1 ) *pSize = 0; |
| 3685 | |
| 3686 | |
| 3687 | return SQLITE_OK; |
| 3688 | } |
| 3689 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3690 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3691 | /* |
| 3692 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3693 | ** proxying locking division. |
| 3694 | */ |
| 3695 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3696 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3697 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3698 | /* |
| 3699 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3700 | ** file-control operation. Enlarge the database to nBytes in size |
| 3701 | ** (rounded up to the next chunk-size). If the database is already |
| 3702 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3703 | */ |
| 3704 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3705 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3706 | i64 nSize; /* Required file size */ |
| 3707 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3708 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3709 | if( osFstat(pFile->h, &buf) ){ |
| 3710 | return SQLITE_IOERR_FSTAT; |
| 3711 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3712 | |
| 3713 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3714 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3715 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3716 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3717 | /* The code below is handling the return value of osFallocate() |
| 3718 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3719 | ** or an error number on failure". See the manpage for details. */ |
| 3720 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3721 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3722 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3723 | }while( err==EINTR ); |
| 3724 | if( err ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3725 | #else |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3726 | /* If the OS does not have posix_fallocate(), fake it. Write a |
| 3727 | ** single byte to the last byte in each block that falls entirely |
| 3728 | ** within the extended region. Then, if required, a single byte |
| 3729 | ** at offset (nSize-1), to set the size of the file correctly. |
| 3730 | ** This is a similar technique to that used by glibc on systems |
| 3731 | ** that do not have a real fallocate() call. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3732 | */ |
| 3733 | int nBlk = buf.st_blksize; /* File-system block size */ |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3734 | int nWrite = 0; /* Number of bytes written by seekAndWrite */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3735 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3736 | |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3737 | iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3738 | assert( iWrite>=buf.st_size ); |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3739 | assert( ((iWrite+1)%nBlk)==0 ); |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3740 | for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){ |
| 3741 | if( iWrite>=nSize ) iWrite = nSize - 1; |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3742 | nWrite = seekAndWrite(pFile, iWrite, "", 1); |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3743 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3744 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3745 | #endif |
| 3746 | } |
| 3747 | } |
| 3748 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3749 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3750 | if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3751 | int rc; |
| 3752 | if( pFile->szChunk<=0 ){ |
| 3753 | if( robust_ftruncate(pFile->h, nByte) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3754 | storeLastErrno(pFile, errno); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3755 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | rc = unixMapfile(pFile, nByte); |
| 3760 | return rc; |
| 3761 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3762 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3763 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3764 | return SQLITE_OK; |
| 3765 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3766 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3767 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3768 | ** If *pArg is initially negative then this is a query. Set *pArg to |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3769 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3770 | ** |
| 3771 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3772 | */ |
| 3773 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3774 | if( *pArg<0 ){ |
| 3775 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3776 | }else if( (*pArg)==0 ){ |
| 3777 | pFile->ctrlFlags &= ~mask; |
| 3778 | }else{ |
| 3779 | pFile->ctrlFlags |= mask; |
| 3780 | } |
| 3781 | } |
| 3782 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3783 | /* Forward declaration */ |
| 3784 | static int unixGetTempname(int nBuf, char *zBuf); |
| 3785 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3786 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3787 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3788 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3789 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3790 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3791 | switch( op ){ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3792 | case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: { |
| 3793 | int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE); |
| 3794 | return rc ? SQLITE_ERROR : SQLITE_OK; |
| 3795 | } |
| 3796 | case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: { |
| 3797 | int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE); |
| 3798 | return rc ? SQLITE_ERROR : SQLITE_OK; |
| 3799 | } |
| 3800 | case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { |
| 3801 | int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); |
| 3802 | return rc ? SQLITE_ERROR : SQLITE_OK; |
| 3803 | } |
| 3804 | |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3805 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3806 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3807 | return SQLITE_OK; |
| 3808 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3809 | case SQLITE_FCNTL_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3810 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3811 | return SQLITE_OK; |
| 3812 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3813 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3814 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3815 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3816 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3817 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3818 | int rc; |
| 3819 | SimulateIOErrorBenign(1); |
| 3820 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3821 | SimulateIOErrorBenign(0); |
| 3822 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3823 | } |
| 3824 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3825 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3826 | return SQLITE_OK; |
| 3827 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3828 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3829 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3830 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3831 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3832 | case SQLITE_FCNTL_VFSNAME: { |
| 3833 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3834 | return SQLITE_OK; |
| 3835 | } |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3836 | case SQLITE_FCNTL_TEMPFILENAME: { |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3837 | char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3838 | if( zTFile ){ |
| 3839 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 3840 | *(char**)pArg = zTFile; |
| 3841 | } |
| 3842 | return SQLITE_OK; |
| 3843 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 3844 | case SQLITE_FCNTL_HAS_MOVED: { |
| 3845 | *(int*)pArg = fileHasMoved(pFile); |
| 3846 | return SQLITE_OK; |
| 3847 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3848 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3849 | case SQLITE_FCNTL_MMAP_SIZE: { |
drh | 34f7490 | 2013-04-03 13:09:18 +0000 | [diff] [blame] | 3850 | i64 newLimit = *(i64*)pArg; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3851 | int rc = SQLITE_OK; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3852 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 3853 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 3854 | } |
| 3855 | *(i64*)pArg = pFile->mmapSizeMax; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3856 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3857 | pFile->mmapSizeMax = newLimit; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3858 | if( pFile->mmapSize>0 ){ |
| 3859 | unixUnmapfile(pFile); |
| 3860 | rc = unixMapfile(pFile, -1); |
| 3861 | } |
dan | bcb8a86 | 2013-04-08 15:30:41 +0000 | [diff] [blame] | 3862 | } |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3863 | return rc; |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 3864 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3865 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3866 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3867 | /* The pager calls this method to signal that it has done |
| 3868 | ** a rollback and that the database is therefore unchanged and |
| 3869 | ** it hence it is OK for the transaction change counter to be |
| 3870 | ** unchanged. |
| 3871 | */ |
| 3872 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3873 | ((unixFile*)id)->dbUpdate = 0; |
| 3874 | return SQLITE_OK; |
| 3875 | } |
| 3876 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3877 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3878 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: |
| 3879 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3880 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3881 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3882 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3883 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3884 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | /* |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3888 | ** If pFd->sectorSize is non-zero when this function is called, it is a |
| 3889 | ** no-op. Otherwise, the values of pFd->sectorSize and |
| 3890 | ** pFd->deviceCharacteristics are set according to the file-system |
| 3891 | ** characteristics. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3892 | ** |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3893 | ** There are two versions of this function. One for QNX and one for all |
| 3894 | ** other systems. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3895 | */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3896 | #ifndef __QNXNTO__ |
| 3897 | static void setDeviceCharacteristics(unixFile *pFd){ |
| 3898 | if( pFd->sectorSize==0 ){ |
| 3899 | int res; |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame^] | 3900 | u32 f = 0; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3901 | assert( pFd->deviceCharacteristics==0 ); |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3902 | |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3903 | /* Check for support for F2FS atomic batch writes. */ |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame^] | 3904 | res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f); |
| 3905 | if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3906 | pFd->deviceCharacteristics = |
| 3907 | SQLITE_IOCAP_BATCH_ATOMIC | |
| 3908 | SQLITE_IOCAP_ATOMIC | |
| 3909 | SQLITE_IOCAP_SEQUENTIAL | |
| 3910 | SQLITE_IOCAP_SAFE_APPEND; |
| 3911 | } |
| 3912 | |
| 3913 | /* Set the POWERSAFE_OVERWRITE flag if requested. */ |
| 3914 | if( pFd->ctrlFlags & UNIXFILE_PSOW ){ |
| 3915 | pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 3916 | } |
| 3917 | |
| 3918 | pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3919 | } |
| 3920 | } |
| 3921 | #else |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3922 | #include <sys/dcmd_blk.h> |
| 3923 | #include <sys/statvfs.h> |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3924 | static void setDeviceCharacteristics(unixFile *pFile){ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3925 | if( pFile->sectorSize == 0 ){ |
| 3926 | struct statvfs fsInfo; |
| 3927 | |
| 3928 | /* Set defaults for non-supported filesystems */ |
| 3929 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3930 | pFile->deviceCharacteristics = 0; |
| 3931 | if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
| 3932 | return pFile->sectorSize; |
| 3933 | } |
| 3934 | |
| 3935 | if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 3936 | pFile->sectorSize = fsInfo.f_bsize; |
| 3937 | pFile->deviceCharacteristics = |
| 3938 | SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 3939 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3940 | ** the write succeeds */ |
| 3941 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3942 | ** so it is ordered */ |
| 3943 | 0; |
| 3944 | }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 3945 | pFile->sectorSize = fsInfo.f_bsize; |
| 3946 | pFile->deviceCharacteristics = |
| 3947 | /* etfs cluster size writes are atomic */ |
| 3948 | (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 3949 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3950 | ** the write succeeds */ |
| 3951 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3952 | ** so it is ordered */ |
| 3953 | 0; |
| 3954 | }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 3955 | pFile->sectorSize = fsInfo.f_bsize; |
| 3956 | pFile->deviceCharacteristics = |
| 3957 | SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 3958 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3959 | ** the write succeeds */ |
| 3960 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3961 | ** so it is ordered */ |
| 3962 | 0; |
| 3963 | }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 3964 | pFile->sectorSize = fsInfo.f_bsize; |
| 3965 | pFile->deviceCharacteristics = |
| 3966 | /* full bitset of atomics from max sector size and smaller */ |
| 3967 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3968 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3969 | ** so it is ordered */ |
| 3970 | 0; |
| 3971 | }else if( strstr(fsInfo.f_basetype, "dos") ){ |
| 3972 | pFile->sectorSize = fsInfo.f_bsize; |
| 3973 | pFile->deviceCharacteristics = |
| 3974 | /* full bitset of atomics from max sector size and smaller */ |
| 3975 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3976 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3977 | ** so it is ordered */ |
| 3978 | 0; |
| 3979 | }else{ |
| 3980 | pFile->deviceCharacteristics = |
| 3981 | SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 3982 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3983 | ** the write succeeds */ |
| 3984 | 0; |
| 3985 | } |
| 3986 | } |
| 3987 | /* Last chance verification. If the sector size isn't a multiple of 512 |
| 3988 | ** then it isn't valid.*/ |
| 3989 | if( pFile->sectorSize % 512 != 0 ){ |
| 3990 | pFile->deviceCharacteristics = 0; |
| 3991 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3992 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3993 | } |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3994 | #endif |
| 3995 | |
| 3996 | /* |
| 3997 | ** Return the sector size in bytes of the underlying block device for |
| 3998 | ** the specified file. This is almost always 512 bytes, but may be |
| 3999 | ** larger for some devices. |
| 4000 | ** |
| 4001 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 4002 | ** if two files are created in the same file-system directory (i.e. |
| 4003 | ** a database and its journal file) that the sector size will be the |
| 4004 | ** same for both. |
| 4005 | */ |
| 4006 | static int unixSectorSize(sqlite3_file *id){ |
| 4007 | unixFile *pFd = (unixFile*)id; |
| 4008 | setDeviceCharacteristics(pFd); |
| 4009 | return pFd->sectorSize; |
| 4010 | } |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4011 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4012 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4013 | ** Return the device characteristics for the file. |
| 4014 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4015 | ** 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] | 4016 | ** However, that choice is controversial since technically the underlying |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4017 | ** file system does not always provide powersafe overwrites. (In other |
| 4018 | ** words, after a power-loss event, parts of the file that were never |
| 4019 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 4020 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 4021 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 4022 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 4023 | ** 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] | 4024 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4025 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4026 | unixFile *pFd = (unixFile*)id; |
| 4027 | setDeviceCharacteristics(pFd); |
| 4028 | return pFd->deviceCharacteristics; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4031 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4032 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4033 | /* |
| 4034 | ** Return the system page size. |
| 4035 | ** |
| 4036 | ** This function should not be called directly by other code in this file. |
| 4037 | ** Instead, it should be called via macro osGetpagesize(). |
| 4038 | */ |
| 4039 | static int unixGetpagesize(void){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4040 | #if OS_VXWORKS |
| 4041 | return 1024; |
| 4042 | #elif defined(_BSD_SOURCE) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4043 | return getpagesize(); |
| 4044 | #else |
| 4045 | return (int)sysconf(_SC_PAGESIZE); |
| 4046 | #endif |
| 4047 | } |
| 4048 | |
| 4049 | #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ |
| 4050 | |
| 4051 | #ifndef SQLITE_OMIT_WAL |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4052 | |
| 4053 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4054 | ** Object used to represent an shared memory buffer. |
| 4055 | ** |
| 4056 | ** When multiple threads all reference the same wal-index, each thread |
| 4057 | ** has its own unixShm object, but they all point to a single instance |
| 4058 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 4059 | ** only once per process. |
| 4060 | ** |
| 4061 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 4062 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 4063 | ** every open file that does not use shared memory (in other words, most |
| 4064 | ** open files) would have to carry around this extra information. So |
| 4065 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 4066 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4067 | ** |
| 4068 | ** unixMutexHeld() must be true when creating or destroying |
| 4069 | ** this object or while reading or writing the following fields: |
| 4070 | ** |
| 4071 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4072 | ** |
| 4073 | ** The following fields are read-only after the object is created: |
| 4074 | ** |
| 4075 | ** fid |
| 4076 | ** zFilename |
| 4077 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4078 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4079 | ** unixMutexHeld() is true when reading or writing any other field |
| 4080 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4081 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4082 | struct unixShmNode { |
| 4083 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4084 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4085 | char *zFilename; /* Name of the mmapped file */ |
| 4086 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4087 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4088 | u16 nRegion; /* Size of array apRegion */ |
| 4089 | u8 isReadonly; /* True if read-only */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4090 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4091 | int nRef; /* Number of unixShm objects pointing to this */ |
| 4092 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4093 | #ifdef SQLITE_DEBUG |
| 4094 | u8 exclMask; /* Mask of exclusive locks held */ |
| 4095 | u8 sharedMask; /* Mask of shared locks held */ |
| 4096 | u8 nextShmId; /* Next available unixShm.id value */ |
| 4097 | #endif |
| 4098 | }; |
| 4099 | |
| 4100 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4101 | ** Structure used internally by this VFS to record the state of an |
| 4102 | ** open shared memory connection. |
| 4103 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4104 | ** The following fields are initialized when this object is created and |
| 4105 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4106 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4107 | ** unixShm.pFile |
| 4108 | ** unixShm.id |
| 4109 | ** |
| 4110 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 4111 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4112 | */ |
| 4113 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4114 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 4115 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4116 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 4117 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4118 | u16 sharedMask; /* Mask of shared locks held */ |
| 4119 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4120 | }; |
| 4121 | |
| 4122 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4123 | ** Constants used for locking |
| 4124 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 4125 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 4126 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4127 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4128 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4129 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4130 | ** |
| 4131 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 4132 | ** otherwise. |
| 4133 | */ |
| 4134 | static int unixShmSystemLock( |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4135 | unixFile *pFile, /* Open connection to the WAL file */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4136 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4137 | int ofst, /* First byte of the locking range */ |
| 4138 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4139 | ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4140 | unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 4141 | struct flock f; /* The posix advisory locking structure */ |
| 4142 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4143 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4144 | /* Access to the unixShmNode object is serialized by the caller */ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4145 | pShmNode = pFile->pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4146 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4147 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4148 | /* Shared locks never span more than one byte */ |
| 4149 | assert( n==1 || lockType!=F_RDLCK ); |
| 4150 | |
| 4151 | /* Locks are within range */ |
drh | af19f17 | 2015-12-02 17:40:13 +0000 | [diff] [blame] | 4152 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4153 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4154 | if( pShmNode->h>=0 ){ |
| 4155 | /* Initialize the locking parameters */ |
| 4156 | memset(&f, 0, sizeof(f)); |
| 4157 | f.l_type = lockType; |
| 4158 | f.l_whence = SEEK_SET; |
| 4159 | f.l_start = ofst; |
| 4160 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4161 | |
drh | dcfb965 | 2015-12-02 00:05:26 +0000 | [diff] [blame] | 4162 | rc = osFcntl(pShmNode->h, F_SETLK, &f); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4163 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 4164 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4165 | |
| 4166 | /* Update the global lock state and do debug tracing */ |
| 4167 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4168 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4169 | OSTRACE(("SHM-LOCK ")); |
drh | 693e671 | 2014-01-24 22:58:00 +0000 | [diff] [blame] | 4170 | mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4171 | if( rc==SQLITE_OK ){ |
| 4172 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4173 | OSTRACE(("unlock %d ok", ofst)); |
| 4174 | pShmNode->exclMask &= ~mask; |
| 4175 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4176 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4177 | OSTRACE(("read-lock %d ok", ofst)); |
| 4178 | pShmNode->exclMask &= ~mask; |
| 4179 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4180 | }else{ |
| 4181 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4182 | OSTRACE(("write-lock %d ok", ofst)); |
| 4183 | pShmNode->exclMask |= mask; |
| 4184 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4185 | } |
| 4186 | }else{ |
| 4187 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4188 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4189 | }else if( lockType==F_RDLCK ){ |
| 4190 | OSTRACE(("read-lock failed")); |
| 4191 | }else{ |
| 4192 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4193 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4194 | } |
| 4195 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4196 | OSTRACE((" - afterwards %03x,%03x\n", |
| 4197 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4198 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4199 | #endif |
| 4200 | |
| 4201 | return rc; |
| 4202 | } |
| 4203 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4204 | /* |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4205 | ** Return the minimum number of 32KB shm regions that should be mapped at |
| 4206 | ** a time, assuming that each mapping must be an integer multiple of the |
| 4207 | ** current system page-size. |
| 4208 | ** |
| 4209 | ** Usually, this is 1. The exception seems to be systems that are configured |
| 4210 | ** to use 64KB pages - in this case each mapping must cover at least two |
| 4211 | ** shm regions. |
| 4212 | */ |
| 4213 | static int unixShmRegionPerMap(void){ |
| 4214 | int shmsz = 32*1024; /* SHM region size */ |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4215 | int pgsz = osGetpagesize(); /* System page size */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4216 | assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ |
| 4217 | if( pgsz<shmsz ) return 1; |
| 4218 | return pgsz/shmsz; |
| 4219 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4220 | |
| 4221 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4222 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4223 | ** |
| 4224 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4225 | ** by VFS shared-memory methods. |
| 4226 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4227 | static void unixShmPurge(unixFile *pFd){ |
| 4228 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4229 | assert( unixMutexHeld() ); |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4230 | if( p && ALWAYS(p->nRef==0) ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4231 | int nShmPerMap = unixShmRegionPerMap(); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4232 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4233 | assert( p->pInode==pFd->pInode ); |
drh | df3aa16 | 2011-06-24 11:29:51 +0000 | [diff] [blame] | 4234 | sqlite3_mutex_free(p->mutex); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4235 | for(i=0; i<p->nRegion; i+=nShmPerMap){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4236 | if( p->h>=0 ){ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 4237 | osMunmap(p->apRegion[i], p->szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4238 | }else{ |
| 4239 | sqlite3_free(p->apRegion[i]); |
| 4240 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4241 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4242 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4243 | if( p->h>=0 ){ |
| 4244 | robust_close(pFd, p->h, __LINE__); |
| 4245 | p->h = -1; |
| 4246 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4247 | p->pInode->pShmNode = 0; |
| 4248 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4253 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4254 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4255 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4256 | ** The file used to implement shared-memory is in the same directory |
| 4257 | ** as the open database file and has the same name as the open database |
| 4258 | ** file with the "-shm" suffix added. For example, if the database file |
| 4259 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4260 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 4261 | ** |
| 4262 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 4263 | ** some other tmpfs mount. But if a file in a different directory |
| 4264 | ** from the database file is used, then differing access permissions |
| 4265 | ** or a chroot() might cause two different processes on the same |
| 4266 | ** database to end up using different files for shared memory - |
| 4267 | ** meaning that their memory would not really be shared - resulting |
| 4268 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 4269 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 4270 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 4271 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 4272 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 4273 | ** same database file at the same time, database corruption will likely |
| 4274 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 4275 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4276 | ** |
| 4277 | ** When opening a new shared-memory file, if no other instances of that |
| 4278 | ** file are currently open, in this process or in other processes, then |
| 4279 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4280 | ** |
| 4281 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 4282 | ** that means that an exclusive lock is held on the database file and |
| 4283 | ** that no other processes are able to read or write the database. In |
| 4284 | ** that case, we do not really need shared memory. No shared memory |
| 4285 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4286 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4287 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 4288 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4289 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 4290 | int rc; /* Result code */ |
| 4291 | unixInodeInfo *pInode; /* The inode of fd */ |
| 4292 | char *zShmFilename; /* Name of the file used for SHM */ |
| 4293 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4294 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4295 | /* Allocate space for the new unixShm object. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4296 | p = sqlite3_malloc64( sizeof(*p) ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4297 | if( p==0 ) return SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4298 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4299 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4300 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4301 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 4302 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4303 | */ |
| 4304 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4305 | pInode = pDbFd->pInode; |
| 4306 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4307 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4308 | struct stat sStat; /* fstat() info for database file */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4309 | #ifndef SQLITE_SHM_DIRECTORY |
| 4310 | const char *zBasePath = pDbFd->zPath; |
| 4311 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4312 | |
| 4313 | /* Call fstat() to figure out the permissions on the database file. If |
| 4314 | ** 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] | 4315 | ** with the same permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4316 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4317 | if( osFstat(pDbFd->h, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4318 | rc = SQLITE_IOERR_FSTAT; |
| 4319 | goto shm_open_err; |
| 4320 | } |
| 4321 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4322 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4323 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4324 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4325 | nShmFilename = 6 + (int)strlen(zBasePath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4326 | #endif |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4327 | pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4328 | if( pShmNode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4329 | rc = SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4330 | goto shm_open_err; |
| 4331 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 4332 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4333 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4334 | #ifdef SQLITE_SHM_DIRECTORY |
| 4335 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 4336 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 4337 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 4338 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4339 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath); |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4340 | sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4341 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4342 | pShmNode->h = -1; |
| 4343 | pDbFd->pInode->pShmNode = pShmNode; |
| 4344 | pShmNode->pInode = pDbFd->pInode; |
drh | 97a7e5e | 2016-04-26 18:58:54 +0000 | [diff] [blame] | 4345 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 4346 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4347 | if( pShmNode->mutex==0 ){ |
| 4348 | rc = SQLITE_NOMEM_BKPT; |
| 4349 | goto shm_open_err; |
| 4350 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4351 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4352 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4353 | if( pInode->bProcessLock==0 ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4354 | int openFlags = O_RDWR | O_CREAT; |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 4355 | if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4356 | openFlags = O_RDONLY; |
| 4357 | pShmNode->isReadonly = 1; |
| 4358 | } |
| 4359 | pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4360 | if( pShmNode->h<0 ){ |
drh | c96d1e7 | 2012-02-11 18:51:34 +0000 | [diff] [blame] | 4361 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 4362 | goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4363 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4364 | |
| 4365 | /* If this process is running as root, make sure that the SHM file |
| 4366 | ** is owned by the same user that owns the original database. Otherwise, |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4367 | ** the original owner will not be able to connect. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4368 | */ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 4369 | robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4370 | |
| 4371 | /* Check to see if another process is holding the dead-man switch. |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4372 | ** If not, truncate the file to zero length. |
| 4373 | */ |
| 4374 | rc = SQLITE_OK; |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4375 | if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4376 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 4377 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4378 | } |
| 4379 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4380 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4381 | rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4382 | } |
| 4383 | if( rc ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4384 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4387 | /* Make the new connection a child of the unixShmNode */ |
| 4388 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4389 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4390 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4391 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4392 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4393 | pDbFd->pShm = p; |
| 4394 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4395 | |
| 4396 | /* The reference count on pShmNode has already been incremented under |
| 4397 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 4398 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 4399 | ** left to do is to link the new object into the linked list starting |
| 4400 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 4401 | ** mutex. |
| 4402 | */ |
| 4403 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4404 | p->pNext = pShmNode->pFirst; |
| 4405 | pShmNode->pFirst = p; |
| 4406 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4407 | return SQLITE_OK; |
| 4408 | |
| 4409 | /* Jump here on any error */ |
| 4410 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4411 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4412 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4413 | unixLeaveMutex(); |
| 4414 | return rc; |
| 4415 | } |
| 4416 | |
| 4417 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4418 | ** This function is called to obtain a pointer to region iRegion of the |
| 4419 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4420 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 4421 | ** bytes in size. |
| 4422 | ** |
| 4423 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 4424 | ** |
| 4425 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 4426 | ** region has not been allocated (by any client, including one running in a |
| 4427 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4428 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 4429 | ** been allocated, it is allocated by this function. |
| 4430 | ** |
| 4431 | ** If the shared-memory region has already been allocated or is allocated by |
| 4432 | ** this call as described above, then it is mapped into this processes |
| 4433 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4434 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4435 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4436 | static int unixShmMap( |
| 4437 | sqlite3_file *fd, /* Handle open on database file */ |
| 4438 | int iRegion, /* Region to retrieve */ |
| 4439 | int szRegion, /* Size of regions */ |
| 4440 | int bExtend, /* True to extend file if necessary */ |
| 4441 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4442 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4443 | unixFile *pDbFd = (unixFile*)fd; |
| 4444 | unixShm *p; |
| 4445 | unixShmNode *pShmNode; |
| 4446 | int rc = SQLITE_OK; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4447 | int nShmPerMap = unixShmRegionPerMap(); |
| 4448 | int nReqRegion; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4449 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4450 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4451 | if( pDbFd->pShm==0 ){ |
| 4452 | rc = unixOpenSharedMemory(pDbFd); |
| 4453 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4454 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4455 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4456 | p = pDbFd->pShm; |
| 4457 | pShmNode = p->pShmNode; |
| 4458 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4459 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4460 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4461 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4462 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4463 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4464 | /* Minimum number of regions required to be mapped. */ |
| 4465 | nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; |
| 4466 | |
| 4467 | if( pShmNode->nRegion<nReqRegion ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4468 | char **apNew; /* New apRegion[] array */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4469 | int nByte = nReqRegion*szRegion; /* Minimum required file size */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4470 | struct stat sStat; /* Used by fstat() */ |
| 4471 | |
| 4472 | pShmNode->szRegion = szRegion; |
| 4473 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4474 | if( pShmNode->h>=0 ){ |
| 4475 | /* The requested region is not mapped into this processes address space. |
| 4476 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4477 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4478 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4479 | if( osFstat(pShmNode->h, &sStat) ){ |
| 4480 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4481 | goto shmpage_out; |
| 4482 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4483 | |
| 4484 | if( sStat.st_size<nByte ){ |
| 4485 | /* The requested memory region does not exist. If bExtend is set to |
| 4486 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4487 | */ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4488 | if( !bExtend ){ |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4489 | goto shmpage_out; |
| 4490 | } |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4491 | |
| 4492 | /* Alternatively, if bExtend is true, extend the file. Do this by |
| 4493 | ** writing a single byte to the end of each (OS) page being |
| 4494 | ** allocated or extended. Technically, we need only write to the |
| 4495 | ** last page in order to extend the file. But writing to all new |
| 4496 | ** pages forces the OS to allocate them immediately, which reduces |
| 4497 | ** the chances of SIGBUS while accessing the mapped region later on. |
| 4498 | */ |
| 4499 | else{ |
| 4500 | static const int pgsz = 4096; |
| 4501 | int iPg; |
| 4502 | |
| 4503 | /* Write to the last byte of each newly allocated or extended page */ |
| 4504 | assert( (nByte % pgsz)==0 ); |
| 4505 | for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 4506 | int x = 0; |
| 4507 | if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4508 | const char *zFile = pShmNode->zFilename; |
| 4509 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 4510 | goto shmpage_out; |
| 4511 | } |
| 4512 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4513 | } |
| 4514 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | /* Map the requested memory region into this processes address space. */ |
| 4518 | apNew = (char **)sqlite3_realloc( |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4519 | pShmNode->apRegion, nReqRegion*sizeof(char *) |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4520 | ); |
| 4521 | if( !apNew ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4522 | rc = SQLITE_IOERR_NOMEM_BKPT; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4523 | goto shmpage_out; |
| 4524 | } |
| 4525 | pShmNode->apRegion = apNew; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4526 | while( pShmNode->nRegion<nReqRegion ){ |
| 4527 | int nMap = szRegion*nShmPerMap; |
| 4528 | int i; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4529 | void *pMem; |
| 4530 | if( pShmNode->h>=0 ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4531 | pMem = osMmap(0, nMap, |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4532 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 4533 | MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4534 | ); |
| 4535 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4536 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4537 | goto shmpage_out; |
| 4538 | } |
| 4539 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4540 | pMem = sqlite3_malloc64(szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4541 | if( pMem==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4542 | rc = SQLITE_NOMEM_BKPT; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4543 | goto shmpage_out; |
| 4544 | } |
| 4545 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4546 | } |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4547 | |
| 4548 | for(i=0; i<nShmPerMap; i++){ |
| 4549 | pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; |
| 4550 | } |
| 4551 | pShmNode->nRegion += nShmPerMap; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4552 | } |
| 4553 | } |
| 4554 | |
| 4555 | shmpage_out: |
| 4556 | if( pShmNode->nRegion>iRegion ){ |
| 4557 | *pp = pShmNode->apRegion[iRegion]; |
| 4558 | }else{ |
| 4559 | *pp = 0; |
| 4560 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4561 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4562 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4563 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4567 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4568 | ** |
| 4569 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4570 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4571 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4572 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4573 | */ |
| 4574 | static int unixShmLock( |
| 4575 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4576 | int ofst, /* First lock to acquire or release */ |
| 4577 | int n, /* Number of locks to acquire or release */ |
| 4578 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4579 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4580 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4581 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4582 | unixShm *pX; /* For looping over all siblings */ |
| 4583 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4584 | int rc = SQLITE_OK; /* Result code */ |
| 4585 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4586 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4587 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4588 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4589 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4590 | assert( n>=1 ); |
| 4591 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4592 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4593 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4594 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4595 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4596 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4597 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4598 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4599 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4600 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4601 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4602 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4603 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4604 | |
| 4605 | /* See if any siblings hold this same lock */ |
| 4606 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4607 | if( pX==p ) continue; |
| 4608 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4609 | allMask |= pX->sharedMask; |
| 4610 | } |
| 4611 | |
| 4612 | /* Unlock the system-level locks */ |
| 4613 | if( (mask & allMask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4614 | rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4615 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4616 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4617 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4618 | |
| 4619 | /* Undo the local locks */ |
| 4620 | if( rc==SQLITE_OK ){ |
| 4621 | p->exclMask &= ~mask; |
| 4622 | p->sharedMask &= ~mask; |
| 4623 | } |
| 4624 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4625 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4626 | |
| 4627 | /* Find out which shared locks are already held by sibling connections. |
| 4628 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4629 | ** SQLITE_BUSY. |
| 4630 | */ |
| 4631 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4632 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4633 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4634 | break; |
| 4635 | } |
| 4636 | allShared |= pX->sharedMask; |
| 4637 | } |
| 4638 | |
| 4639 | /* Get shared locks at the system level, if necessary */ |
| 4640 | if( rc==SQLITE_OK ){ |
| 4641 | if( (allShared & mask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4642 | rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4643 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4644 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4645 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4646 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4647 | |
| 4648 | /* Get the local shared locks */ |
| 4649 | if( rc==SQLITE_OK ){ |
| 4650 | p->sharedMask |= mask; |
| 4651 | } |
| 4652 | }else{ |
| 4653 | /* Make sure no sibling connections hold locks that will block this |
| 4654 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4655 | */ |
| 4656 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4657 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4658 | rc = SQLITE_BUSY; |
| 4659 | break; |
| 4660 | } |
| 4661 | } |
| 4662 | |
| 4663 | /* Get the exclusive locks at the system level. Then if successful |
| 4664 | ** also mark the local connection as being locked. |
| 4665 | */ |
| 4666 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4667 | rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4668 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4669 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4670 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4671 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4672 | } |
| 4673 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4674 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4675 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 4676 | p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4677 | return rc; |
| 4678 | } |
| 4679 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4680 | /* |
| 4681 | ** Implement a memory barrier or memory fence on shared memory. |
| 4682 | ** |
| 4683 | ** All loads and stores begun before the barrier must complete before |
| 4684 | ** any load or store begun after the barrier. |
| 4685 | */ |
| 4686 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4687 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4688 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4689 | UNUSED_PARAMETER(fd); |
drh | 22c733d | 2015-09-24 12:40:43 +0000 | [diff] [blame] | 4690 | sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ |
| 4691 | unixEnterMutex(); /* Also mutex, for redundancy */ |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4692 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4695 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4696 | ** Close a connection to shared-memory. Delete the underlying |
| 4697 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4698 | ** |
| 4699 | ** If there is no shared memory associated with the connection then this |
| 4700 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4701 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4702 | static int unixShmUnmap( |
| 4703 | sqlite3_file *fd, /* The underlying database file */ |
| 4704 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4705 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4706 | unixShm *p; /* The connection to be closed */ |
| 4707 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4708 | unixShm **pp; /* For looping over sibling connections */ |
| 4709 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4710 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4711 | pDbFd = (unixFile*)fd; |
| 4712 | p = pDbFd->pShm; |
| 4713 | if( p==0 ) return SQLITE_OK; |
| 4714 | pShmNode = p->pShmNode; |
| 4715 | |
| 4716 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4717 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4718 | |
| 4719 | /* Remove connection p from the set of connections associated |
| 4720 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4721 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4722 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4723 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4724 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4725 | /* Free the connection p */ |
| 4726 | sqlite3_free(p); |
| 4727 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4728 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4729 | |
| 4730 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4731 | ** shared-memory file, too */ |
| 4732 | unixEnterMutex(); |
| 4733 | assert( pShmNode->nRef>0 ); |
| 4734 | pShmNode->nRef--; |
| 4735 | if( pShmNode->nRef==0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4736 | if( deleteFlag && pShmNode->h>=0 ){ |
| 4737 | osUnlink(pShmNode->zFilename); |
| 4738 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4739 | unixShmPurge(pDbFd); |
| 4740 | } |
| 4741 | unixLeaveMutex(); |
| 4742 | |
| 4743 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4744 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4745 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4746 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4747 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4748 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4749 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4750 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4751 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4752 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4753 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4754 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4755 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4756 | ** If it is currently memory mapped, unmap file pFd. |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4757 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4758 | static void unixUnmapfile(unixFile *pFd){ |
| 4759 | assert( pFd->nFetchOut==0 ); |
| 4760 | if( pFd->pMapRegion ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4761 | osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4762 | pFd->pMapRegion = 0; |
| 4763 | pFd->mmapSize = 0; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4764 | pFd->mmapSizeActual = 0; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4765 | } |
| 4766 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4767 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4768 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4769 | ** Attempt to set the size of the memory mapping maintained by file |
| 4770 | ** descriptor pFd to nNew bytes. Any existing mapping is discarded. |
| 4771 | ** |
| 4772 | ** If successful, this function sets the following variables: |
| 4773 | ** |
| 4774 | ** unixFile.pMapRegion |
| 4775 | ** unixFile.mmapSize |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4776 | ** unixFile.mmapSizeActual |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4777 | ** |
| 4778 | ** If unsuccessful, an error message is logged via sqlite3_log() and |
| 4779 | ** the three variables above are zeroed. In this case SQLite should |
| 4780 | ** continue accessing the database using the xRead() and xWrite() |
| 4781 | ** methods. |
| 4782 | */ |
| 4783 | static void unixRemapfile( |
| 4784 | unixFile *pFd, /* File descriptor object */ |
| 4785 | i64 nNew /* Required mapping size */ |
| 4786 | ){ |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4787 | const char *zErr = "mmap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4788 | int h = pFd->h; /* File descriptor open on db file */ |
| 4789 | u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4790 | i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4791 | u8 *pNew = 0; /* Location of new mapping */ |
| 4792 | int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 4793 | |
| 4794 | assert( pFd->nFetchOut==0 ); |
| 4795 | assert( nNew>pFd->mmapSize ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4796 | assert( nNew<=pFd->mmapSizeMax ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4797 | assert( nNew>0 ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4798 | assert( pFd->mmapSizeActual>=pFd->mmapSize ); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4799 | assert( MAP_FAILED!=0 ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4800 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 4801 | #ifdef SQLITE_MMAP_READWRITE |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4802 | if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 4803 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4804 | |
| 4805 | if( pOrig ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4806 | #if HAVE_MREMAP |
| 4807 | i64 nReuse = pFd->mmapSize; |
| 4808 | #else |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4809 | const int szSyspage = osGetpagesize(); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4810 | i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4811 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4812 | u8 *pReq = &pOrig[nReuse]; |
| 4813 | |
| 4814 | /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 4815 | if( nReuse!=nOrig ){ |
| 4816 | osMunmap(pReq, nOrig-nReuse); |
| 4817 | } |
| 4818 | |
| 4819 | #if HAVE_MREMAP |
| 4820 | pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4821 | zErr = "mremap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4822 | #else |
| 4823 | pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 4824 | if( pNew!=MAP_FAILED ){ |
| 4825 | if( pNew!=pReq ){ |
| 4826 | osMunmap(pNew, nNew - nReuse); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4827 | pNew = 0; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4828 | }else{ |
| 4829 | pNew = pOrig; |
| 4830 | } |
| 4831 | } |
| 4832 | #endif |
| 4833 | |
dan | 48ccef8 | 2013-04-02 20:55:01 +0000 | [diff] [blame] | 4834 | /* The attempt to extend the existing mapping failed. Free it. */ |
| 4835 | if( pNew==MAP_FAILED || pNew==0 ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4836 | osMunmap(pOrig, nReuse); |
| 4837 | } |
| 4838 | } |
| 4839 | |
| 4840 | /* If pNew is still NULL, try to create an entirely new mapping. */ |
| 4841 | if( pNew==0 ){ |
| 4842 | pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4845 | if( pNew==MAP_FAILED ){ |
| 4846 | pNew = 0; |
| 4847 | nNew = 0; |
| 4848 | unixLogError(SQLITE_OK, zErr, pFd->zPath); |
| 4849 | |
| 4850 | /* If the mmap() above failed, assume that all subsequent mmap() calls |
| 4851 | ** will probably fail too. Fall back to using xRead/xWrite exclusively |
| 4852 | ** in this case. */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4853 | pFd->mmapSizeMax = 0; |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4854 | } |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4855 | pFd->pMapRegion = (void *)pNew; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4856 | pFd->mmapSize = pFd->mmapSizeActual = nNew; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4860 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 4861 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 4862 | ** there already exists a mapping for this file, and there are still |
| 4863 | ** outstanding xFetch() references to it, this function is a no-op. |
| 4864 | ** |
| 4865 | ** If parameter nByte is non-negative, then it is the requested size of |
| 4866 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 4867 | ** requested size is the size of the file on disk. The actual size of the |
| 4868 | ** created mapping is either the requested size or the value configured |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 4869 | ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4870 | ** |
| 4871 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 4872 | ** recreated as a result of outstanding references) or an SQLite error |
| 4873 | ** code otherwise. |
| 4874 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4875 | static int unixMapfile(unixFile *pFd, i64 nMap){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4876 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 4877 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4878 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 4879 | |
| 4880 | if( nMap<0 ){ |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 4881 | struct stat statbuf; /* Low-level file information */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4882 | if( osFstat(pFd->h, &statbuf) ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4883 | return SQLITE_IOERR_FSTAT; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4884 | } |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 4885 | nMap = statbuf.st_size; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4886 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4887 | if( nMap>pFd->mmapSizeMax ){ |
| 4888 | nMap = pFd->mmapSizeMax; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 4891 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4892 | if( nMap!=pFd->mmapSize ){ |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 4893 | unixRemapfile(pFd, nMap); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4894 | } |
| 4895 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4896 | return SQLITE_OK; |
| 4897 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4898 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4899 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4900 | /* |
| 4901 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 4902 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 4903 | ** |
| 4904 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 4905 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 4906 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 4907 | ** value of *pp is undefined in this case. |
| 4908 | ** |
| 4909 | ** If this function does return a pointer, the caller must eventually |
| 4910 | ** release the reference by calling unixUnfetch(). |
| 4911 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4912 | static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4913 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4914 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 4915 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4916 | *pp = 0; |
| 4917 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4918 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 4919 | if( pFd->mmapSizeMax>0 ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4920 | if( pFd->pMapRegion==0 ){ |
| 4921 | int rc = unixMapfile(pFd, -1); |
| 4922 | if( rc!=SQLITE_OK ) return rc; |
| 4923 | } |
| 4924 | if( pFd->mmapSize >= iOff+nAmt ){ |
| 4925 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 4926 | pFd->nFetchOut++; |
| 4927 | } |
| 4928 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 4929 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4930 | return SQLITE_OK; |
| 4931 | } |
| 4932 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4933 | /* |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4934 | ** If the third argument is non-NULL, then this function releases a |
| 4935 | ** reference obtained by an earlier call to unixFetch(). The second |
| 4936 | ** argument passed to this function must be the same as the corresponding |
| 4937 | ** argument that was passed to the unixFetch() invocation. |
| 4938 | ** |
| 4939 | ** Or, if the third argument is NULL, then this function is being called |
| 4940 | ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 4941 | ** may now be invalid and should be unmapped. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4942 | */ |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4943 | static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 4944 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 4945 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 4946 | UNUSED_PARAMETER(iOff); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 4947 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4948 | /* If p==0 (unmap the entire file) then there must be no outstanding |
| 4949 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 4950 | ** then there must be at least one outstanding. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4951 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 4952 | |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4953 | /* If p!=0, it must match the iOff value. */ |
| 4954 | assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 4955 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4956 | if( p ){ |
| 4957 | pFd->nFetchOut--; |
| 4958 | }else{ |
| 4959 | unixUnmapfile(pFd); |
| 4960 | } |
| 4961 | |
| 4962 | assert( pFd->nFetchOut>=0 ); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 4963 | #else |
| 4964 | UNUSED_PARAMETER(fd); |
| 4965 | UNUSED_PARAMETER(p); |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 4966 | UNUSED_PARAMETER(iOff); |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 4967 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4968 | return SQLITE_OK; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4972 | ** Here ends the implementation of all sqlite3_file methods. |
| 4973 | ** |
| 4974 | ********************** End sqlite3_file Methods ******************************* |
| 4975 | ******************************************************************************/ |
| 4976 | |
| 4977 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4978 | ** This division contains definitions of sqlite3_io_methods objects that |
| 4979 | ** implement various file locking strategies. It also contains definitions |
| 4980 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 4981 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 4982 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 4983 | ** the correct finder-function for that VFS. |
| 4984 | ** |
| 4985 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 4986 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 4987 | ** looks at the filesystem type and tries to guess the best locking |
| 4988 | ** strategy from that. |
| 4989 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4990 | ** For finder-function F, two objects are created: |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4991 | ** |
| 4992 | ** (1) The real finder-function named "FImpt()". |
| 4993 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4994 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4995 | ** |
| 4996 | ** |
| 4997 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 4998 | ** objects. We have to do this instead of letting pAppData point |
| 4999 | ** directly at the finder-function since C90 rules prevent a void* |
| 5000 | ** from be cast into a function pointer. |
| 5001 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5002 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5003 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5004 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5005 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 5006 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 5007 | ** |
| 5008 | ** * An I/O method finder function called FINDER that returns a pointer |
| 5009 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5010 | */ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 5011 | #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5012 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5013 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5014 | CLOSE, /* xClose */ \ |
| 5015 | unixRead, /* xRead */ \ |
| 5016 | unixWrite, /* xWrite */ \ |
| 5017 | unixTruncate, /* xTruncate */ \ |
| 5018 | unixSync, /* xSync */ \ |
| 5019 | unixFileSize, /* xFileSize */ \ |
| 5020 | LOCK, /* xLock */ \ |
| 5021 | UNLOCK, /* xUnlock */ \ |
| 5022 | CKLOCK, /* xCheckReservedLock */ \ |
| 5023 | unixFileControl, /* xFileControl */ \ |
| 5024 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5025 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5026 | SHMMAP, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5027 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 5028 | unixShmBarrier, /* xShmBarrier */ \ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5029 | unixShmUnmap, /* xShmUnmap */ \ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5030 | unixFetch, /* xFetch */ \ |
| 5031 | unixUnfetch, /* xUnfetch */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5032 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5033 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 5034 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5035 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5036 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5037 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5038 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5039 | |
| 5040 | /* |
| 5041 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 5042 | ** locking strategies. Functions that return pointers to these methods |
| 5043 | ** are also created. |
| 5044 | */ |
| 5045 | IOMETHODS( |
| 5046 | posixIoFinder, /* Finder function name */ |
| 5047 | posixIoMethods, /* sqlite3_io_methods object name */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5048 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5049 | unixClose, /* xClose method */ |
| 5050 | unixLock, /* xLock method */ |
| 5051 | unixUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5052 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5053 | unixShmMap /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5054 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5055 | IOMETHODS( |
| 5056 | nolockIoFinder, /* Finder function name */ |
| 5057 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 142341c | 2014-09-19 19:00:48 +0000 | [diff] [blame] | 5058 | 3, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5059 | nolockClose, /* xClose method */ |
| 5060 | nolockLock, /* xLock method */ |
| 5061 | nolockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5062 | nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 5063 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5064 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5065 | IOMETHODS( |
| 5066 | dotlockIoFinder, /* Finder function name */ |
| 5067 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5068 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5069 | dotlockClose, /* xClose method */ |
| 5070 | dotlockLock, /* xLock method */ |
| 5071 | dotlockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5072 | dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 5073 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5074 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5075 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5076 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5077 | IOMETHODS( |
| 5078 | flockIoFinder, /* Finder function name */ |
| 5079 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5080 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5081 | flockClose, /* xClose method */ |
| 5082 | flockLock, /* xLock method */ |
| 5083 | flockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5084 | flockCheckReservedLock, /* xCheckReservedLock method */ |
| 5085 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5086 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5087 | #endif |
| 5088 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5089 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5090 | IOMETHODS( |
| 5091 | semIoFinder, /* Finder function name */ |
| 5092 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5093 | 1, /* shared memory is disabled */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5094 | semXClose, /* xClose method */ |
| 5095 | semXLock, /* xLock method */ |
| 5096 | semXUnlock, /* xUnlock method */ |
| 5097 | semXCheckReservedLock, /* xCheckReservedLock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5098 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5099 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5100 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5101 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5102 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5103 | IOMETHODS( |
| 5104 | afpIoFinder, /* Finder function name */ |
| 5105 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5106 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5107 | afpClose, /* xClose method */ |
| 5108 | afpLock, /* xLock method */ |
| 5109 | afpUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5110 | afpCheckReservedLock, /* xCheckReservedLock method */ |
| 5111 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5112 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5113 | #endif |
| 5114 | |
| 5115 | /* |
| 5116 | ** The proxy locking method is a "super-method" in the sense that it |
| 5117 | ** opens secondary file descriptors for the conch and lock files and |
| 5118 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 5119 | ** secondary files. For this reason, the division that implements |
| 5120 | ** proxy locking is located much further down in the file. But we need |
| 5121 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 5122 | ** for proxy locking here. So we forward declare the I/O methods. |
| 5123 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5124 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5125 | static int proxyClose(sqlite3_file*); |
| 5126 | static int proxyLock(sqlite3_file*, int); |
| 5127 | static int proxyUnlock(sqlite3_file*, int); |
| 5128 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5129 | IOMETHODS( |
| 5130 | proxyIoFinder, /* Finder function name */ |
| 5131 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5132 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5133 | proxyClose, /* xClose method */ |
| 5134 | proxyLock, /* xLock method */ |
| 5135 | proxyUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5136 | proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 5137 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5138 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5139 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5140 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5141 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 5142 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5143 | IOMETHODS( |
| 5144 | nfsIoFinder, /* Finder function name */ |
| 5145 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5146 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5147 | unixClose, /* xClose method */ |
| 5148 | unixLock, /* xLock method */ |
| 5149 | nfsUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5150 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5151 | 0 /* xShmMap method */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5152 | ) |
| 5153 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5154 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5155 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5156 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5157 | ** This "finder" function attempts to determine the best locking strategy |
| 5158 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5159 | ** object that implements that strategy. |
| 5160 | ** |
| 5161 | ** This is for MacOSX only. |
| 5162 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5163 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5164 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5165 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5166 | ){ |
| 5167 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5168 | const char *zFilesystem; /* Filesystem type name */ |
| 5169 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5170 | } aMap[] = { |
| 5171 | { "hfs", &posixIoMethods }, |
| 5172 | { "ufs", &posixIoMethods }, |
| 5173 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5174 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5175 | { "webdav", &nolockIoMethods }, |
| 5176 | { 0, 0 } |
| 5177 | }; |
| 5178 | int i; |
| 5179 | struct statfs fsInfo; |
| 5180 | struct flock lockInfo; |
| 5181 | |
| 5182 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5183 | /* If filePath==NULL that means we are dealing with a transient file |
| 5184 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5185 | return &nolockIoMethods; |
| 5186 | } |
| 5187 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 5188 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 5189 | return &nolockIoMethods; |
| 5190 | } |
| 5191 | for(i=0; aMap[i].zFilesystem; i++){ |
| 5192 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 5193 | return aMap[i].pMethods; |
| 5194 | } |
| 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | /* Default case. Handles, amongst others, "nfs". |
| 5199 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 5200 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5201 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5202 | lockInfo.l_len = 1; |
| 5203 | lockInfo.l_start = 0; |
| 5204 | lockInfo.l_whence = SEEK_SET; |
| 5205 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5206 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5207 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 5208 | return &nfsIoMethods; |
| 5209 | } else { |
| 5210 | return &posixIoMethods; |
| 5211 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5212 | }else{ |
| 5213 | return &dotlockIoMethods; |
| 5214 | } |
| 5215 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5216 | static const sqlite3_io_methods |
| 5217 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5218 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5219 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5220 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5221 | #if OS_VXWORKS |
| 5222 | /* |
| 5223 | ** This "finder" function for VxWorks checks to see if posix advisory |
| 5224 | ** locking works. If it does, then that is what is used. If it does not |
| 5225 | ** work, then fallback to named semaphore locking. |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5226 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5227 | static const sqlite3_io_methods *vxworksIoFinderImpl( |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5228 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5229 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5230 | ){ |
| 5231 | struct flock lockInfo; |
| 5232 | |
| 5233 | if( !filePath ){ |
| 5234 | /* If filePath==NULL that means we are dealing with a transient file |
| 5235 | ** that does not need to be locked. */ |
| 5236 | return &nolockIoMethods; |
| 5237 | } |
| 5238 | |
| 5239 | /* Test if fcntl() is supported and use POSIX style locks. |
| 5240 | ** Otherwise fall back to the named semaphore method. |
| 5241 | */ |
| 5242 | lockInfo.l_len = 1; |
| 5243 | lockInfo.l_start = 0; |
| 5244 | lockInfo.l_whence = SEEK_SET; |
| 5245 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5246 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5247 | return &posixIoMethods; |
| 5248 | }else{ |
| 5249 | return &semIoMethods; |
| 5250 | } |
| 5251 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5252 | static const sqlite3_io_methods |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5253 | *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5254 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5255 | #endif /* OS_VXWORKS */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5256 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5257 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5258 | ** An abstract type for a pointer to an IO method finder function: |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5259 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5260 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5261 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5262 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5263 | /**************************************************************************** |
| 5264 | **************************** sqlite3_vfs methods **************************** |
| 5265 | ** |
| 5266 | ** This division contains the implementation of methods on the |
| 5267 | ** sqlite3_vfs object. |
| 5268 | */ |
| 5269 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 5270 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5271 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5272 | */ |
| 5273 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5274 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5275 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5276 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5277 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5278 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5279 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5280 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5281 | unixFile *pNew = (unixFile *)pId; |
| 5282 | int rc = SQLITE_OK; |
| 5283 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5284 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5285 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5286 | /* Usually the path zFilename should not be a relative pathname. The |
| 5287 | ** exception is when opening the proxy "conch" file in builds that |
| 5288 | ** include the special Apple locking styles. |
| 5289 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5290 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 5291 | assert( zFilename==0 || zFilename[0]=='/' |
| 5292 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 5293 | #else |
| 5294 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5295 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5296 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5297 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5298 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5299 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5300 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5301 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5302 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5303 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5304 | pNew->ctrlFlags = (u8)ctrlFlags; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5305 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | ede01a9 | 2013-05-17 12:10:52 +0000 | [diff] [blame] | 5306 | pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5307 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5308 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 5309 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5310 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 5311 | } |
drh | 503a686 | 2013-03-01 01:07:17 +0000 | [diff] [blame] | 5312 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5313 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5314 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 5315 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5316 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5317 | pNew->pId = vxworksFindFileId(zFilename); |
| 5318 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5319 | ctrlFlags |= UNIXFILE_NOLOCK; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5320 | rc = SQLITE_NOMEM_BKPT; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5321 | } |
| 5322 | #endif |
| 5323 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5324 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5325 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5326 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5327 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5328 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5329 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 5330 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 5331 | ** zFilename remains valid until file is closed, to support */ |
| 5332 | pNew->lockingContext = (void*)zFilename; |
| 5333 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5334 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5335 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5336 | if( pLockingStyle == &posixIoMethods |
| 5337 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5338 | || pLockingStyle == &nfsIoMethods |
| 5339 | #endif |
| 5340 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5341 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5342 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5343 | if( rc!=SQLITE_OK ){ |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5344 | /* If an error occurred in findInodeInfo(), close the file descriptor |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5345 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5346 | ** in two scenarios: |
| 5347 | ** |
| 5348 | ** (a) A call to fstat() failed. |
| 5349 | ** (b) A malloc failed. |
| 5350 | ** |
| 5351 | ** Scenario (b) may only occur if the process is holding no other |
| 5352 | ** file descriptors open on the same file. If there were other file |
| 5353 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5354 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5355 | ** handle h - as it is guaranteed that no posix locks will be released |
| 5356 | ** by doing so. |
| 5357 | ** |
| 5358 | ** If scenario (a) caused the error then things are not so safe. The |
| 5359 | ** implicit assumption here is that if fstat() fails, things are in |
| 5360 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 5361 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5362 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5363 | h = -1; |
| 5364 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5365 | unixLeaveMutex(); |
| 5366 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5367 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5368 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 5369 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5370 | /* AFP locking uses the file path so it needs to be included in |
| 5371 | ** the afpLockingContext. |
| 5372 | */ |
| 5373 | afpLockingContext *pCtx; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5374 | pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5375 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5376 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5377 | }else{ |
| 5378 | /* NB: zFilename exists and remains valid until the file is closed |
| 5379 | ** according to requirement F11141. So we do not need to make a |
| 5380 | ** copy of the filename. */ |
| 5381 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5382 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5383 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5384 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5385 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5386 | if( rc!=SQLITE_OK ){ |
| 5387 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5388 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5389 | h = -1; |
| 5390 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5391 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5392 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5393 | } |
| 5394 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5395 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5396 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 5397 | /* Dotfile locking uses the file path so it needs to be included in |
| 5398 | ** the dotlockLockingContext |
| 5399 | */ |
| 5400 | char *zLockFile; |
| 5401 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5402 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5403 | nFilename = (int)strlen(zFilename) + 6; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5404 | zLockFile = (char *)sqlite3_malloc64(nFilename); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5405 | if( zLockFile==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5406 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5407 | }else{ |
| 5408 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5409 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5410 | pNew->lockingContext = zLockFile; |
| 5411 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5412 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5413 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5414 | else if( pLockingStyle == &semIoMethods ){ |
| 5415 | /* Named semaphore locking uses the file path so it needs to be |
| 5416 | ** included in the semLockingContext |
| 5417 | */ |
| 5418 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5419 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 5420 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 5421 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5422 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5423 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5424 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5425 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5426 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5427 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 5428 | if( pNew->pInode->pSem == SEM_FAILED ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5429 | rc = SQLITE_NOMEM_BKPT; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5430 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5431 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5432 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5433 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5434 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5435 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 5436 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5437 | storeLastErrno(pNew, 0); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5438 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5439 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5440 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 5441 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5442 | osUnlink(zFilename); |
drh | c579754 | 2013-04-27 12:13:29 +0000 | [diff] [blame] | 5443 | pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5444 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5445 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5446 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5447 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5448 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5449 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5450 | OpenCounter(+1); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5451 | verifyDbFile(pNew); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5452 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5453 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5454 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 5455 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5456 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5457 | ** Return the name of a directory in which to put temporary files. |
| 5458 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5459 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5460 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5461 | static const char *azDirs[] = { |
| 5462 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5463 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5464 | "/var/tmp", |
| 5465 | "/usr/tmp", |
| 5466 | "/tmp", |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5467 | "." |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5468 | }; |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5469 | unsigned int i = 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5470 | struct stat buf; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5471 | const char *zDir = sqlite3_temp_directory; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5472 | |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5473 | if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); |
| 5474 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5475 | while(1){ |
| 5476 | if( zDir!=0 |
| 5477 | && osStat(zDir, &buf)==0 |
| 5478 | && S_ISDIR(buf.st_mode) |
| 5479 | && osAccess(zDir, 03)==0 |
| 5480 | ){ |
| 5481 | return zDir; |
| 5482 | } |
| 5483 | if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break; |
| 5484 | zDir = azDirs[i++]; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5485 | } |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5486 | return 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | /* |
| 5490 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 5491 | ** by the calling process and must be big enough to hold at least |
| 5492 | ** pVfs->mxPathname bytes. |
| 5493 | */ |
| 5494 | static int unixGetTempname(int nBuf, char *zBuf){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5495 | const char *zDir; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5496 | int iLimit = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5497 | |
| 5498 | /* It's odd to simulate an io-error here, but really this is just |
| 5499 | ** using the io-error infrastructure to test that SQLite handles this |
| 5500 | ** function failing. |
| 5501 | */ |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5502 | zBuf[0] = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5503 | SimulateIOError( return SQLITE_IOERR ); |
| 5504 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5505 | zDir = unixTempFileDir(); |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5506 | if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5507 | do{ |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 5508 | u64 r; |
| 5509 | sqlite3_randomness(sizeof(r), &r); |
| 5510 | assert( nBuf>2 ); |
| 5511 | zBuf[nBuf-2] = 0; |
| 5512 | sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", |
| 5513 | zDir, r, 0); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5514 | if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5515 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5516 | return SQLITE_OK; |
| 5517 | } |
| 5518 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5519 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5520 | /* |
| 5521 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 5522 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 5523 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 5524 | */ |
| 5525 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 5526 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5527 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5528 | /* |
| 5529 | ** Search for an unused file descriptor that was opened on the database |
| 5530 | ** file (not a journal or master-journal file) identified by pathname |
| 5531 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 5532 | ** argument to this function. |
| 5533 | ** |
| 5534 | ** Such a file descriptor may exist if a database connection was closed |
| 5535 | ** but the associated file descriptor could not be closed because some |
| 5536 | ** other file descriptor open on the same file is holding a file-lock. |
| 5537 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 5538 | ** describing "Posix Advisory Locking" at the start of this file for |
| 5539 | ** further details. Also, ticket #4018. |
| 5540 | ** |
| 5541 | ** If a suitable file descriptor is found, then it is returned. If no |
| 5542 | ** such file descriptor is located, -1 is returned. |
| 5543 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5544 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 5545 | UnixUnusedFd *pUnused = 0; |
| 5546 | |
| 5547 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 5548 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 5549 | ** but because no way to test it is currently available. It is better |
| 5550 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 5551 | ** feature. */ |
| 5552 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5553 | struct stat sStat; /* Results of stat() call */ |
| 5554 | |
| 5555 | /* A stat() call may fail for various reasons. If this happens, it is |
| 5556 | ** almost certain that an open() call on the same path will also fail. |
| 5557 | ** For this reason, if an error occurs in the stat() call here, it is |
| 5558 | ** ignored and -1 is returned. The caller will try to open a new file |
| 5559 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 5560 | ** |
| 5561 | ** Even if a subsequent open() call does succeed, the consequences of |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5562 | ** not searching for a reusable file descriptor are not dire. */ |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5563 | if( 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 5564 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5565 | |
| 5566 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5567 | pInode = inodeList; |
| 5568 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 5569 | || pInode->fileId.ino!=(u64)sStat.st_ino) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5570 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 5571 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5572 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5573 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5574 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5575 | pUnused = *pp; |
| 5576 | if( pUnused ){ |
| 5577 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5578 | } |
| 5579 | } |
| 5580 | unixLeaveMutex(); |
| 5581 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5582 | #endif /* if !OS_VXWORKS */ |
| 5583 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5584 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5585 | |
| 5586 | /* |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5587 | ** Find the mode, uid and gid of file zFile. |
| 5588 | */ |
| 5589 | static int getFileMode( |
| 5590 | const char *zFile, /* File name */ |
| 5591 | mode_t *pMode, /* OUT: Permissions of zFile */ |
| 5592 | uid_t *pUid, /* OUT: uid of zFile. */ |
| 5593 | gid_t *pGid /* OUT: gid of zFile. */ |
| 5594 | ){ |
| 5595 | struct stat sStat; /* Output of stat() on database file */ |
| 5596 | int rc = SQLITE_OK; |
| 5597 | if( 0==osStat(zFile, &sStat) ){ |
| 5598 | *pMode = sStat.st_mode & 0777; |
| 5599 | *pUid = sStat.st_uid; |
| 5600 | *pGid = sStat.st_gid; |
| 5601 | }else{ |
| 5602 | rc = SQLITE_IOERR_FSTAT; |
| 5603 | } |
| 5604 | return rc; |
| 5605 | } |
| 5606 | |
| 5607 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5608 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 5609 | ** 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] | 5610 | ** and a value suitable for passing as the third argument to open(2) is |
| 5611 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 5612 | ** returned and the value of *pMode is not modified. |
| 5613 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5614 | ** In most cases, this routine sets *pMode to 0, which will become |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5615 | ** an indication to robust_open() to create the file using |
| 5616 | ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 5617 | ** 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] | 5618 | ** this function queries the file-system for the permissions on the |
| 5619 | ** corresponding database file and sets *pMode to this value. Whenever |
| 5620 | ** possible, WAL and journal files are created using the same permissions |
| 5621 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5622 | ** |
| 5623 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 5624 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 5625 | ** FAT filesystems and permissions do not matter there, so just use |
| 5626 | ** the default permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5627 | */ |
| 5628 | static int findCreateFileMode( |
| 5629 | const char *zPath, /* Path of file (possibly) being created */ |
| 5630 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5631 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 5632 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 5633 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5634 | ){ |
| 5635 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5636 | *pMode = 0; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5637 | *pUid = 0; |
| 5638 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5639 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5640 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 5641 | int nDb; /* Number of valid bytes in zDb */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5642 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5643 | /* zPath is a path to a WAL or journal file. The following block derives |
| 5644 | ** the path to the associated database file from zPath. This block handles |
| 5645 | ** the following naming conventions: |
| 5646 | ** |
| 5647 | ** "<path to db>-journal" |
| 5648 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5649 | ** "<path to db>-journalNN" |
| 5650 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5651 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5652 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5653 | ** used by the test_multiplex.c module. |
| 5654 | */ |
| 5655 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5656 | while( zPath[nDb]!='-' ){ |
drh | 90e5dda | 2015-12-03 20:42:28 +0000 | [diff] [blame] | 5657 | #ifndef SQLITE_ENABLE_8_3_NAMES |
| 5658 | /* In the normal case (8+3 filenames disabled) the journal filename |
| 5659 | ** is guaranteed to contain a '-' character. */ |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5660 | assert( nDb>0 ); |
drh | 90e5dda | 2015-12-03 20:42:28 +0000 | [diff] [blame] | 5661 | assert( sqlite3Isalnum(zPath[nDb]) ); |
| 5662 | #else |
| 5663 | /* If 8+3 names are possible, then the journal file might not contain |
| 5664 | ** a '-' character. So check for that case and return early. */ |
| 5665 | if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; |
| 5666 | #endif |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5667 | nDb--; |
| 5668 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5669 | memcpy(zDb, zPath, nDb); |
| 5670 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5671 | |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5672 | rc = getFileMode(zDb, pMode, pUid, pGid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5673 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 5674 | *pMode = 0600; |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5675 | }else if( flags & SQLITE_OPEN_URI ){ |
| 5676 | /* If this is a main database file and the file was opened using a URI |
| 5677 | ** filename, check for the "modeof" parameter. If present, interpret |
| 5678 | ** its value as a filename and try to copy the mode, uid and gid from |
| 5679 | ** that file. */ |
| 5680 | const char *z = sqlite3_uri_parameter(zPath, "modeof"); |
| 5681 | if( z ){ |
| 5682 | rc = getFileMode(z, pMode, pUid, pGid); |
| 5683 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5684 | } |
| 5685 | return rc; |
| 5686 | } |
| 5687 | |
| 5688 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5689 | ** Open the file zPath. |
| 5690 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5691 | ** Previously, the SQLite OS layer used three functions in place of this |
| 5692 | ** one: |
| 5693 | ** |
| 5694 | ** sqlite3OsOpenReadWrite(); |
| 5695 | ** sqlite3OsOpenReadOnly(); |
| 5696 | ** sqlite3OsOpenExclusive(); |
| 5697 | ** |
| 5698 | ** These calls correspond to the following combinations of flags: |
| 5699 | ** |
| 5700 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5701 | ** ReadOnly() -> (READONLY) |
| 5702 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5703 | ** |
| 5704 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5705 | ** true, the file was configured to be automatically deleted when the |
| 5706 | ** file handle closed. To achieve the same effect using this new |
| 5707 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5708 | ** OpenExclusive(). |
| 5709 | */ |
| 5710 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5711 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5712 | const char *zPath, /* Pathname of file to be opened */ |
| 5713 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5714 | int flags, /* Input flags to control the opening */ |
| 5715 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5716 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5717 | unixFile *p = (unixFile *)pFile; |
| 5718 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5719 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5720 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5721 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5722 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5723 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5724 | |
| 5725 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5726 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5727 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5728 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5729 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5730 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5731 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5732 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5733 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5734 | struct statfs fsInfo; |
| 5735 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5736 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5737 | /* If creating a master or main-file journal, this function will open |
| 5738 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5739 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5740 | */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5741 | int syncDir = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5742 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5743 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5744 | || eType==SQLITE_OPEN_WAL |
| 5745 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5746 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5747 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5748 | ** a temporary file. Use this buffer to store the file name in. |
| 5749 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5750 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5751 | const char *zName = zPath; |
| 5752 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5753 | /* Check the following statements are true: |
| 5754 | ** |
| 5755 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5756 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5757 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5758 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5759 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5760 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5761 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5762 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5763 | assert(isDelete==0 || isCreate); |
| 5764 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5765 | /* The main DB, main journal, WAL file and master journal are never |
| 5766 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5767 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5768 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5769 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5770 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5771 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5772 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5773 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5774 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5775 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5776 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5777 | ); |
| 5778 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5779 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 5780 | ** here such that two or more threads all trying to open databases at |
| 5781 | ** the same instant might all reset the PRNG. But multiple resets |
| 5782 | ** are harmless. |
| 5783 | */ |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 5784 | if( randomnessPid!=osGetpid(0) ){ |
| 5785 | randomnessPid = osGetpid(0); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5786 | sqlite3_randomness(0,0); |
| 5787 | } |
| 5788 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5789 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5790 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5791 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5792 | UnixUnusedFd *pUnused; |
| 5793 | pUnused = findReusableFd(zName, flags); |
| 5794 | if( pUnused ){ |
| 5795 | fd = pUnused->fd; |
| 5796 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5797 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5798 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5799 | return SQLITE_NOMEM_BKPT; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5800 | } |
| 5801 | } |
| 5802 | p->pUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5803 | |
| 5804 | /* Database filenames are double-zero terminated if they are not |
| 5805 | ** URIs with parameters. Hence, they can always be passed into |
| 5806 | ** sqlite3_uri_parameter(). */ |
| 5807 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 5808 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5809 | }else if( !zName ){ |
| 5810 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5811 | assert(isDelete && !syncDir); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5812 | rc = unixGetTempname(pVfs->mxPathname, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5813 | if( rc!=SQLITE_OK ){ |
| 5814 | return rc; |
| 5815 | } |
| 5816 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5817 | |
| 5818 | /* Generated temporary filenames are always double-zero terminated |
| 5819 | ** for use by sqlite3_uri_parameter(). */ |
| 5820 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5821 | } |
| 5822 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5823 | /* Determine the value of the flags parameter passed to POSIX function |
| 5824 | ** open(). These must be calculated even if open() is not called, as |
| 5825 | ** they may be stored as part of the file handle and used by the |
| 5826 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5827 | if( isReadonly ) openFlags |= O_RDONLY; |
| 5828 | if( isReadWrite ) openFlags |= O_RDWR; |
| 5829 | if( isCreate ) openFlags |= O_CREAT; |
| 5830 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 5831 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5832 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5833 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5834 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5835 | uid_t uid; /* Userid for the file */ |
| 5836 | gid_t gid; /* Groupid for the file */ |
| 5837 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5838 | if( rc!=SQLITE_OK ){ |
| 5839 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5840 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5841 | return rc; |
| 5842 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5843 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5844 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
drh | 5a2d970 | 2015-11-26 02:21:05 +0000 | [diff] [blame] | 5845 | assert( !isExclusive || (openFlags & O_CREAT)!=0 ); |
| 5846 | if( fd<0 && errno!=EISDIR && isReadWrite ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5847 | /* Failed to open the file for read/write access. Try read-only. */ |
| 5848 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5849 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5850 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5851 | openFlags |= O_RDONLY; |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5852 | isReadonly = 1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5853 | fd = robust_open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5854 | } |
| 5855 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5856 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5857 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5858 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5859 | |
| 5860 | /* If this process is running as root and if creating a new rollback |
| 5861 | ** journal or WAL file, set the ownership of the journal or WAL to be |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 5862 | ** the same as the original database. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5863 | */ |
| 5864 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 5865 | robustFchown(fd, uid, gid); |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5866 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5867 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5868 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5869 | if( pOutFlags ){ |
| 5870 | *pOutFlags = flags; |
| 5871 | } |
| 5872 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5873 | if( p->pUnused ){ |
| 5874 | p->pUnused->fd = fd; |
| 5875 | p->pUnused->flags = flags; |
| 5876 | } |
| 5877 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5878 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5879 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5880 | zPath = zName; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 5881 | #elif defined(SQLITE_UNLINK_AFTER_CLOSE) |
| 5882 | zPath = sqlite3_mprintf("%s", zName); |
| 5883 | if( zPath==0 ){ |
| 5884 | robust_close(p, fd, __LINE__); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5885 | return SQLITE_NOMEM_BKPT; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 5886 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5887 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5888 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5889 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5890 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5891 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5892 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5893 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 5894 | } |
| 5895 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5896 | |
| 5897 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5898 | if( fstatfs(fd, &fsInfo) == -1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5899 | storeLastErrno(p, errno); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5900 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5901 | return SQLITE_IOERR_ACCESS; |
| 5902 | } |
| 5903 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5904 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5905 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5906 | if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) { |
| 5907 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5908 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5909 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5910 | |
| 5911 | /* Set up appropriate ctrlFlags */ |
| 5912 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 5913 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 5914 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5915 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
| 5916 | if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; |
| 5917 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 5918 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5919 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5920 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5921 | isAutoProxy = 1; |
| 5922 | #endif |
| 5923 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5924 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5925 | int useProxy = 0; |
| 5926 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5927 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5928 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5929 | if( envforce!=NULL ){ |
| 5930 | useProxy = atoi(envforce)>0; |
| 5931 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5932 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 5933 | } |
| 5934 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5935 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5936 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5937 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5938 | if( rc!=SQLITE_OK ){ |
| 5939 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 5940 | ** and clear all the structure's references. Specifically, |
| 5941 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 5942 | */ |
| 5943 | unixClose(pFile); |
| 5944 | return rc; |
| 5945 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5946 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5947 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5948 | } |
| 5949 | } |
| 5950 | #endif |
| 5951 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5952 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 5953 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5954 | open_finished: |
| 5955 | if( rc!=SQLITE_OK ){ |
| 5956 | sqlite3_free(p->pUnused); |
| 5957 | } |
| 5958 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5959 | } |
| 5960 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5961 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5962 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5963 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 5964 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5965 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5966 | static int unixDelete( |
| 5967 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 5968 | const char *zPath, /* Name of file to be deleted */ |
| 5969 | int dirSync /* If true, fsync() directory after deleting file */ |
| 5970 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5971 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5972 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5973 | SimulateIOError(return SQLITE_IOERR_DELETE); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5974 | if( osUnlink(zPath)==(-1) ){ |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 5975 | if( errno==ENOENT |
| 5976 | #if OS_VXWORKS |
drh | 19541f3 | 2014-09-01 13:37:55 +0000 | [diff] [blame] | 5977 | || osAccess(zPath,0)!=0 |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 5978 | #endif |
| 5979 | ){ |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5980 | rc = SQLITE_IOERR_DELETE_NOENT; |
| 5981 | }else{ |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5982 | rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5983 | } |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5984 | return rc; |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5985 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 5986 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 5987 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5988 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 5989 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5990 | if( rc==SQLITE_OK ){ |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 5991 | if( full_fsync(fd,0,0) ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5992 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5993 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5994 | robust_close(0, fd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 5995 | }else{ |
| 5996 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 5997 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5998 | } |
| 5999 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 6000 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6001 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6002 | } |
| 6003 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6004 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 6005 | ** Test the existence of or access permissions of file zPath. The |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6006 | ** test performed depends on the value of flags: |
| 6007 | ** |
| 6008 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 6009 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 6010 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 6011 | ** |
| 6012 | ** Otherwise return 0. |
| 6013 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6014 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6015 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 6016 | const char *zPath, /* Path of the file to examine */ |
| 6017 | int flags, /* What do we want to learn about the zPath file? */ |
| 6018 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6019 | ){ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6020 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6021 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6022 | assert( pResOut!=0 ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6023 | |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6024 | /* The spec says there are three possible values for flags. But only |
| 6025 | ** two of them are actually used */ |
| 6026 | assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE ); |
| 6027 | |
| 6028 | if( flags==SQLITE_ACCESS_EXISTS ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6029 | struct stat buf; |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6030 | *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0); |
| 6031 | }else{ |
| 6032 | *pResOut = osAccess(zPath, W_OK|R_OK)==0; |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6033 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6034 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6035 | } |
| 6036 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6037 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6038 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6039 | */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6040 | static int mkFullPathname( |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6041 | const char *zPath, /* Input path */ |
| 6042 | char *zOut, /* Output buffer */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6043 | int nOut /* Allocated size of buffer zOut */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 6044 | ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6045 | int nPath = sqlite3Strlen30(zPath); |
| 6046 | int iOff = 0; |
| 6047 | if( zPath[0]!='/' ){ |
| 6048 | if( osGetcwd(zOut, nOut-2)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6049 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6050 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6051 | iOff = sqlite3Strlen30(zOut); |
| 6052 | zOut[iOff++] = '/'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6053 | } |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6054 | if( (iOff+nPath+1)>nOut ){ |
| 6055 | /* SQLite assumes that xFullPathname() nul-terminates the output buffer |
| 6056 | ** even if it returns an error. */ |
| 6057 | zOut[iOff] = '\0'; |
| 6058 | return SQLITE_CANTOPEN_BKPT; |
| 6059 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6060 | sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6061 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6062 | } |
| 6063 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6064 | /* |
| 6065 | ** Turn a relative pathname into a full pathname. The relative path |
| 6066 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 6067 | ** zPath. |
| 6068 | ** |
| 6069 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 6070 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 6071 | ** this buffer before returning. |
| 6072 | */ |
| 6073 | static int unixFullPathname( |
| 6074 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 6075 | const char *zPath, /* Possibly relative input path */ |
| 6076 | int nOut, /* Size of output buffer in bytes */ |
| 6077 | char *zOut /* Output buffer */ |
| 6078 | ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6079 | #if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT) |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6080 | return mkFullPathname(zPath, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6081 | #else |
| 6082 | int rc = SQLITE_OK; |
| 6083 | int nByte; |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6084 | int nLink = 1; /* Number of symbolic links followed so far */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6085 | const char *zIn = zPath; /* Input path for each iteration of loop */ |
| 6086 | char *zDel = 0; |
| 6087 | |
| 6088 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
| 6089 | UNUSED_PARAMETER(pVfs); |
| 6090 | |
| 6091 | /* It's odd to simulate an io-error here, but really this is just |
| 6092 | ** using the io-error infrastructure to test that SQLite handles this |
| 6093 | ** function failing. This function could fail if, for example, the |
| 6094 | ** current working directory has been unlinked. |
| 6095 | */ |
| 6096 | SimulateIOError( return SQLITE_ERROR ); |
| 6097 | |
| 6098 | do { |
| 6099 | |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6100 | /* Call stat() on path zIn. Set bLink to true if the path is a symbolic |
| 6101 | ** link, or false otherwise. */ |
| 6102 | int bLink = 0; |
| 6103 | struct stat buf; |
| 6104 | if( osLstat(zIn, &buf)!=0 ){ |
| 6105 | if( errno!=ENOENT ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6106 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6107 | } |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6108 | }else{ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6109 | bLink = S_ISLNK(buf.st_mode); |
| 6110 | } |
| 6111 | |
| 6112 | if( bLink ){ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6113 | if( zDel==0 ){ |
| 6114 | zDel = sqlite3_malloc(nOut); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6115 | if( zDel==0 ) rc = SQLITE_NOMEM_BKPT; |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6116 | }else if( ++nLink>SQLITE_MAX_SYMLINKS ){ |
| 6117 | rc = SQLITE_CANTOPEN_BKPT; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6118 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6119 | |
| 6120 | if( rc==SQLITE_OK ){ |
| 6121 | nByte = osReadlink(zIn, zDel, nOut-1); |
| 6122 | if( nByte<0 ){ |
| 6123 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6124 | }else{ |
| 6125 | if( zDel[0]!='/' ){ |
| 6126 | int n; |
| 6127 | for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); |
| 6128 | if( nByte+n+1>nOut ){ |
| 6129 | rc = SQLITE_CANTOPEN_BKPT; |
| 6130 | }else{ |
| 6131 | memmove(&zDel[n], zDel, nByte+1); |
| 6132 | memcpy(zDel, zIn, n); |
| 6133 | nByte += n; |
| 6134 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6135 | } |
| 6136 | zDel[nByte] = '\0'; |
| 6137 | } |
| 6138 | } |
| 6139 | |
| 6140 | zIn = zDel; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6143 | assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' ); |
| 6144 | if( rc==SQLITE_OK && zIn!=zOut ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6145 | rc = mkFullPathname(zIn, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6146 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6147 | if( bLink==0 ) break; |
| 6148 | zIn = zOut; |
| 6149 | }while( rc==SQLITE_OK ); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6150 | |
| 6151 | sqlite3_free(zDel); |
| 6152 | return rc; |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6153 | #endif /* HAVE_READLINK && HAVE_LSTAT */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 6156 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6157 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 6158 | /* |
| 6159 | ** Interfaces for opening a shared library, finding entry points |
| 6160 | ** within the shared library, and closing the shared library. |
| 6161 | */ |
| 6162 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6163 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 6164 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6165 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 6166 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 6167 | |
| 6168 | /* |
| 6169 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 6170 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 6171 | ** message is available, it is written to zBufOut. If no error message |
| 6172 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 6173 | ** error message. |
| 6174 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6175 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 6176 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6177 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6178 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6179 | zErr = dlerror(); |
| 6180 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6181 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6182 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6183 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6184 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6185 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 6186 | /* |
| 6187 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 6188 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 6189 | ** returns a void* which is really a pointer to a function. So how do we |
| 6190 | ** use dlsym() with -pedantic-errors? |
| 6191 | ** |
| 6192 | ** Variable x below is defined to be a pointer to a function taking |
| 6193 | ** parameters void* and const char* and returning a pointer to a function. |
| 6194 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 6195 | ** (That assignment requires a cast.) Then we call the function that |
| 6196 | ** x points to. |
| 6197 | ** |
| 6198 | ** This work-around is unlikely to work correctly on any system where |
| 6199 | ** you really cannot cast a function pointer into void*. But then, on the |
| 6200 | ** other hand, dlsym() will not work on such a system either, so we have |
| 6201 | ** not really lost anything. |
| 6202 | */ |
| 6203 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6204 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6205 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 6206 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6207 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6208 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 6209 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6210 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6211 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6212 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 6213 | #define unixDlOpen 0 |
| 6214 | #define unixDlError 0 |
| 6215 | #define unixDlSym 0 |
| 6216 | #define unixDlClose 0 |
| 6217 | #endif |
| 6218 | |
| 6219 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6220 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6221 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6222 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 6223 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 6224 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6225 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6226 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 6227 | ** errors. The reports issued by valgrind are incorrect - we would |
| 6228 | ** prefer that the randomness be increased by making use of the |
| 6229 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 6230 | ** some users. Rather than argue, it seems easier just to initialize |
| 6231 | ** the whole array and silence valgrind, even if that means less randomness |
| 6232 | ** in the random seed. |
| 6233 | ** |
| 6234 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 6235 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6236 | ** tests repeatable. |
| 6237 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6238 | memset(zBuf, 0, nBuf); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6239 | randomnessPid = osGetpid(0); |
drh | 6a412b8 | 2015-04-30 12:31:49 +0000 | [diff] [blame] | 6240 | #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6241 | { |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6242 | int fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6243 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6244 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 6245 | time_t t; |
| 6246 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6247 | memcpy(zBuf, &t, sizeof(t)); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6248 | memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); |
| 6249 | assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); |
| 6250 | nBuf = sizeof(t) + sizeof(randomnessPid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6251 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 6252 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6253 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6254 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6255 | } |
| 6256 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 6257 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6260 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6261 | /* |
| 6262 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6263 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6264 | ** The return value is the number of microseconds of sleep actually |
| 6265 | ** requested from the underlying operating system, a number which |
| 6266 | ** might be greater than or equal to the argument, but not less |
| 6267 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6268 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6269 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6270 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6271 | struct timespec sp; |
| 6272 | |
| 6273 | sp.tv_sec = microseconds / 1000000; |
| 6274 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 6275 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6276 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6277 | return microseconds; |
| 6278 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6279 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6280 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6281 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6282 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6283 | int seconds = (microseconds+999999)/1000000; |
| 6284 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6285 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6286 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 6287 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 6288 | } |
| 6289 | |
| 6290 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6291 | ** The following variable, if set to a non-zero value, is interpreted as |
| 6292 | ** the number of seconds since 1970 and is used to set the result of |
| 6293 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6294 | */ |
| 6295 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6296 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6297 | #endif |
| 6298 | |
| 6299 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6300 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 6301 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 6302 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 6303 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 6304 | ** proleptic Gregorian calendar. |
| 6305 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6306 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 6307 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6308 | */ |
| 6309 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 6310 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6311 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6312 | #if defined(NO_GETTOD) |
| 6313 | time_t t; |
| 6314 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 6315 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6316 | #elif OS_VXWORKS |
| 6317 | struct timespec sNow; |
| 6318 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 6319 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 6320 | #else |
| 6321 | struct timeval sNow; |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 6322 | (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ |
| 6323 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6324 | #endif |
| 6325 | |
| 6326 | #ifdef SQLITE_TEST |
| 6327 | if( sqlite3_current_time ){ |
| 6328 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 6329 | } |
| 6330 | #endif |
| 6331 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6332 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6333 | } |
| 6334 | |
drh | c3dfa5e | 2016-01-22 19:44:03 +0000 | [diff] [blame] | 6335 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6336 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6337 | ** Find the current time (in Universal Coordinated Time). Write the |
| 6338 | ** current time and date as a Julian Day number into *prNow and |
| 6339 | ** return 0. Return 1 if the time and date cannot be found. |
| 6340 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6341 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 6342 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6343 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 6344 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6345 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 6346 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6347 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6348 | } |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 6349 | #else |
| 6350 | # define unixCurrentTime 0 |
| 6351 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6352 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6353 | /* |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6354 | ** The xGetLastError() method is designed to return a better |
| 6355 | ** low-level error message when operating-system problems come up |
| 6356 | ** during SQLite operation. Only the integer return code is currently |
| 6357 | ** used. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6358 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6359 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 6360 | UNUSED_PARAMETER(NotUsed); |
| 6361 | UNUSED_PARAMETER(NotUsed2); |
| 6362 | UNUSED_PARAMETER(NotUsed3); |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6363 | return errno; |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 6364 | } |
| 6365 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6366 | |
| 6367 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6368 | ************************ End of sqlite3_vfs methods *************************** |
| 6369 | ******************************************************************************/ |
| 6370 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6371 | /****************************************************************************** |
| 6372 | ************************** Begin Proxy Locking ******************************** |
| 6373 | ** |
| 6374 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 6375 | ** other locking methods on secondary lock files. Proxy locking is a |
| 6376 | ** meta-layer over top of the primitive locking implemented above. For |
| 6377 | ** this reason, the division that implements of proxy locking is deferred |
| 6378 | ** until late in the file (here) after all of the other I/O methods have |
| 6379 | ** been defined - so that the primitive locking methods are available |
| 6380 | ** as services to help with the implementation of proxy locking. |
| 6381 | ** |
| 6382 | **** |
| 6383 | ** |
| 6384 | ** The default locking schemes in SQLite use byte-range locks on the |
| 6385 | ** database file to coordinate safe, concurrent access by multiple readers |
| 6386 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 6387 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 6388 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 6389 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 6390 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 6391 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 6392 | ** address in the shared range is taken for a SHARED lock, the entire |
| 6393 | ** shared range is taken for an EXCLUSIVE lock): |
| 6394 | ** |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 6395 | ** PENDING_BYTE 0x40000000 |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6396 | ** RESERVED_BYTE 0x40000001 |
| 6397 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 6398 | ** |
| 6399 | ** This works well on the local file system, but shows a nearly 100x |
| 6400 | ** slowdown in read performance on AFP because the AFP client disables |
| 6401 | ** the read cache when byte-range locks are present. Enabling the read |
| 6402 | ** cache exposes a cache coherency problem that is present on all OS X |
| 6403 | ** supported network file systems. NFS and AFP both observe the |
| 6404 | ** close-to-open semantics for ensuring cache coherency |
| 6405 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 6406 | ** address the requirements for concurrent database access by multiple |
| 6407 | ** readers and writers |
| 6408 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 6409 | ** |
| 6410 | ** To address the performance and cache coherency issues, proxy file locking |
| 6411 | ** changes the way database access is controlled by limiting access to a |
| 6412 | ** single host at a time and moving file locks off of the database file |
| 6413 | ** and onto a proxy file on the local file system. |
| 6414 | ** |
| 6415 | ** |
| 6416 | ** Using proxy locks |
| 6417 | ** ----------------- |
| 6418 | ** |
| 6419 | ** C APIs |
| 6420 | ** |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6421 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6422 | ** <proxy_path> | ":auto:"); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6423 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, |
| 6424 | ** &<proxy_path>); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6425 | ** |
| 6426 | ** |
| 6427 | ** SQL pragmas |
| 6428 | ** |
| 6429 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 6430 | ** PRAGMA [database.]lock_proxy_file |
| 6431 | ** |
| 6432 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 6433 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 6434 | ** a proxy path based on the user's temp dir |
| 6435 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 6436 | ** actual proxy file name is generated from the name and path of the |
| 6437 | ** database file. For example: |
| 6438 | ** |
| 6439 | ** For database path "/Users/me/foo.db" |
| 6440 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 6441 | ** |
| 6442 | ** Once a lock proxy is configured for a database connection, it can not |
| 6443 | ** be removed, however it may be switched to a different proxy path via |
| 6444 | ** the above APIs (assuming the conch file is not being held by another |
| 6445 | ** connection or process). |
| 6446 | ** |
| 6447 | ** |
| 6448 | ** How proxy locking works |
| 6449 | ** ----------------------- |
| 6450 | ** |
| 6451 | ** Proxy file locking relies primarily on two new supporting files: |
| 6452 | ** |
| 6453 | ** * conch file to limit access to the database file to a single host |
| 6454 | ** at a time |
| 6455 | ** |
| 6456 | ** * proxy file to act as a proxy for the advisory locks normally |
| 6457 | ** taken on the database |
| 6458 | ** |
| 6459 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 6460 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 6461 | ** contents and comparing the host's unique host ID (see below) and lock |
| 6462 | ** proxy path against the values stored in the conch. The conch file is |
| 6463 | ** stored in the same directory as the database file and the file name |
| 6464 | ** is patterned after the database file name as ".<databasename>-conch". |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 6465 | ** 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] | 6466 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 6467 | ** lock and the conch file contents is updated with the host ID and proxy |
| 6468 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 6469 | ** is held by another process (with a shared lock), the exclusive lock |
| 6470 | ** will fail and SQLITE_BUSY is returned. |
| 6471 | ** |
| 6472 | ** The proxy file - a single-byte file used for all advisory file locks |
| 6473 | ** normally taken on the database file. This allows for safe sharing |
| 6474 | ** of the database file for multiple readers and writers on the same |
| 6475 | ** host (the conch ensures that they all use the same local lock file). |
| 6476 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6477 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 6478 | ** only taken when the first request to lock database file is made. |
| 6479 | ** This matches the semantics of the traditional locking behavior, where |
| 6480 | ** opening a connection to a database file does not take a lock on it. |
| 6481 | ** The shared lock and an open file descriptor are maintained until |
| 6482 | ** the connection to the database is closed. |
| 6483 | ** |
| 6484 | ** The proxy file and the lock file are never deleted so they only need |
| 6485 | ** to be created the first time they are used. |
| 6486 | ** |
| 6487 | ** Configuration options |
| 6488 | ** --------------------- |
| 6489 | ** |
| 6490 | ** SQLITE_PREFER_PROXY_LOCKING |
| 6491 | ** |
| 6492 | ** Database files accessed on non-local file systems are |
| 6493 | ** automatically configured for proxy locking, lock files are |
| 6494 | ** named automatically using the same logic as |
| 6495 | ** PRAGMA lock_proxy_file=":auto:" |
| 6496 | ** |
| 6497 | ** SQLITE_PROXY_DEBUG |
| 6498 | ** |
| 6499 | ** Enables the logging of error messages during host id file |
| 6500 | ** retrieval and creation |
| 6501 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6502 | ** LOCKPROXYDIR |
| 6503 | ** |
| 6504 | ** Overrides the default directory used for lock proxy files that |
| 6505 | ** are named automatically via the ":auto:" setting |
| 6506 | ** |
| 6507 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6508 | ** |
| 6509 | ** Permissions to use when creating a directory for storing the |
| 6510 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 6511 | ** |
| 6512 | ** |
| 6513 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 6514 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 6515 | ** force proxy locking to be used for every database file opened, and 0 |
| 6516 | ** will force automatic proxy locking to be disabled for all database |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6517 | ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6518 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 6519 | */ |
| 6520 | |
| 6521 | /* |
| 6522 | ** Proxy locking is only available on MacOSX |
| 6523 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6524 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6525 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6526 | /* |
| 6527 | ** The proxyLockingContext has the path and file structures for the remote |
| 6528 | ** and local proxy files in it |
| 6529 | */ |
| 6530 | typedef struct proxyLockingContext proxyLockingContext; |
| 6531 | struct proxyLockingContext { |
| 6532 | unixFile *conchFile; /* Open conch file */ |
| 6533 | char *conchFilePath; /* Name of the conch file */ |
| 6534 | unixFile *lockProxy; /* Open proxy lock file */ |
| 6535 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 6536 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6537 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6538 | int nFails; /* Number of conch taking failures */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6539 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 6540 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 6541 | }; |
| 6542 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6543 | /* |
| 6544 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 6545 | ** which must point to valid, writable memory large enough for a maxLen length |
| 6546 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6547 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6548 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 6549 | int len; |
| 6550 | int dbLen; |
| 6551 | int i; |
| 6552 | |
| 6553 | #ifdef LOCKPROXYDIR |
| 6554 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 6555 | #else |
| 6556 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 6557 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6558 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6559 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6560 | lPath, errno, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6561 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6562 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6563 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6564 | } |
| 6565 | # else |
| 6566 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 6567 | # endif |
| 6568 | #endif |
| 6569 | |
| 6570 | if( lPath[len-1]!='/' ){ |
| 6571 | len = strlcat(lPath, "/", maxLen); |
| 6572 | } |
| 6573 | |
| 6574 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6575 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6576 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6577 | char c = dbPath[i]; |
| 6578 | lPath[i+len] = (c=='/')?'_':c; |
| 6579 | } |
| 6580 | lPath[i+len]='\0'; |
| 6581 | strlcat(lPath, ":auto:", maxLen); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6582 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6583 | return SQLITE_OK; |
| 6584 | } |
| 6585 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6586 | /* |
| 6587 | ** Creates the lock file and any missing directories in lockPath |
| 6588 | */ |
| 6589 | static int proxyCreateLockPath(const char *lockPath){ |
| 6590 | int i, len; |
| 6591 | char buf[MAXPATHLEN]; |
| 6592 | int start = 0; |
| 6593 | |
| 6594 | assert(lockPath!=NULL); |
| 6595 | /* try to create all the intermediate directories */ |
| 6596 | len = (int)strlen(lockPath); |
| 6597 | buf[0] = lockPath[0]; |
| 6598 | for( i=1; i<len; i++ ){ |
| 6599 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 6600 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 6601 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 6602 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 6603 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 6604 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6605 | int err=errno; |
| 6606 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6607 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6608 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6609 | buf, strerror(err), lockPath, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6610 | return err; |
| 6611 | } |
| 6612 | } |
| 6613 | } |
| 6614 | start=i+1; |
| 6615 | } |
| 6616 | buf[i] = lockPath[i]; |
| 6617 | } |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 6618 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6619 | return 0; |
| 6620 | } |
| 6621 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6622 | /* |
| 6623 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 6624 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 6625 | ** |
| 6626 | ** The caller is responsible not only for closing the file descriptor |
| 6627 | ** but also for freeing the memory associated with the file descriptor. |
| 6628 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6629 | static int proxyCreateUnixFile( |
| 6630 | const char *path, /* path for the new unixFile */ |
| 6631 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 6632 | int islockfile /* if non zero missing dirs will be created */ |
| 6633 | ) { |
| 6634 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6635 | unixFile *pNew; |
| 6636 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6637 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6638 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6639 | int terrno = 0; |
| 6640 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6641 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6642 | /* 1. first try to open/create the file |
| 6643 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 6644 | ** the parent directories and then try again. |
| 6645 | ** 3. if that fails, try to open the file read-only |
| 6646 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 6647 | */ |
| 6648 | pUnused = findReusableFd(path, openFlags); |
| 6649 | if( pUnused ){ |
| 6650 | fd = pUnused->fd; |
| 6651 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6652 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6653 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6654 | return SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6655 | } |
| 6656 | } |
| 6657 | if( fd<0 ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6658 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6659 | terrno = errno; |
| 6660 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 6661 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6662 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6663 | } |
| 6664 | } |
| 6665 | } |
| 6666 | if( fd<0 ){ |
| 6667 | openFlags = O_RDONLY; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6668 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6669 | terrno = errno; |
| 6670 | } |
| 6671 | if( fd<0 ){ |
| 6672 | if( islockfile ){ |
| 6673 | return SQLITE_BUSY; |
| 6674 | } |
| 6675 | switch (terrno) { |
| 6676 | case EACCES: |
| 6677 | return SQLITE_PERM; |
| 6678 | case EIO: |
| 6679 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 6680 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6681 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6682 | } |
| 6683 | } |
| 6684 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6685 | pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6686 | if( pNew==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6687 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6688 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6689 | } |
| 6690 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6691 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6692 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6693 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6694 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6695 | pUnused->fd = fd; |
| 6696 | pUnused->flags = openFlags; |
| 6697 | pNew->pUnused = pUnused; |
| 6698 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6699 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6700 | if( rc==SQLITE_OK ){ |
| 6701 | *ppFile = pNew; |
| 6702 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6703 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6704 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6705 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6706 | sqlite3_free(pNew); |
| 6707 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6708 | return rc; |
| 6709 | } |
| 6710 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6711 | #ifdef SQLITE_TEST |
| 6712 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6713 | int sqlite3_hostid_num = 0; |
| 6714 | #endif |
| 6715 | |
| 6716 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 6717 | |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6718 | #ifdef HAVE_GETHOSTUUID |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6719 | /* Not always defined in the headers as it ought to be */ |
| 6720 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6721 | #endif |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6722 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6723 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 6724 | ** bytes of writable memory. |
| 6725 | */ |
| 6726 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6727 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 6728 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6729 | #ifdef HAVE_GETHOSTUUID |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6730 | { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6731 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6732 | if( gethostuuid(pHostID, &timeout) ){ |
| 6733 | int err = errno; |
| 6734 | if( pError ){ |
| 6735 | *pError = err; |
| 6736 | } |
| 6737 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6738 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6739 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6740 | #else |
| 6741 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6742 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6743 | #ifdef SQLITE_TEST |
| 6744 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6745 | if( sqlite3_hostid_num != 0){ |
| 6746 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 6747 | } |
| 6748 | #endif |
| 6749 | |
| 6750 | return SQLITE_OK; |
| 6751 | } |
| 6752 | |
| 6753 | /* The conch file contains the header, host id and lock file path |
| 6754 | */ |
| 6755 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 6756 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 6757 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 6758 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 6759 | |
| 6760 | /* |
| 6761 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 6762 | ** it back. The newly created file's file descriptor is assigned to the |
| 6763 | ** conch file structure and finally the original conch file descriptor is |
| 6764 | ** closed. Returns zero if successful. |
| 6765 | */ |
| 6766 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 6767 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6768 | unixFile *conchFile = pCtx->conchFile; |
| 6769 | char tPath[MAXPATHLEN]; |
| 6770 | char buf[PROXY_MAXCONCHLEN]; |
| 6771 | char *cPath = pCtx->conchFilePath; |
| 6772 | size_t readLen = 0; |
| 6773 | size_t pathLen = 0; |
| 6774 | char errmsg[64] = ""; |
| 6775 | int fd = -1; |
| 6776 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6777 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6778 | |
| 6779 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 6780 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 6781 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 6782 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6783 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6784 | goto end_breaklock; |
| 6785 | } |
| 6786 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6787 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6788 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6789 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6790 | goto end_breaklock; |
| 6791 | } |
| 6792 | /* write it out to the temporary break file */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6793 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6794 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6795 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6796 | goto end_breaklock; |
| 6797 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6798 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6799 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6800 | goto end_breaklock; |
| 6801 | } |
| 6802 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6803 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6804 | goto end_breaklock; |
| 6805 | } |
| 6806 | rc = 0; |
| 6807 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6808 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6809 | conchFile->h = fd; |
| 6810 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 6811 | |
| 6812 | end_breaklock: |
| 6813 | if( rc ){ |
| 6814 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6815 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6816 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6817 | } |
| 6818 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 6819 | } |
| 6820 | return rc; |
| 6821 | } |
| 6822 | |
| 6823 | /* Take the requested lock on the conch file and break a stale lock if the |
| 6824 | ** host id matches. |
| 6825 | */ |
| 6826 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 6827 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6828 | unixFile *conchFile = pCtx->conchFile; |
| 6829 | int rc = SQLITE_OK; |
| 6830 | int nTries = 0; |
| 6831 | struct timespec conchModTime; |
| 6832 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6833 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6834 | do { |
| 6835 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6836 | nTries ++; |
| 6837 | if( rc==SQLITE_BUSY ){ |
| 6838 | /* If the lock failed (busy): |
| 6839 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 6840 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 6841 | * 10 sec and try again |
| 6842 | * 3rd try: break the lock unless the mod time has changed. |
| 6843 | */ |
| 6844 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6845 | if( osFstat(conchFile->h, &buf) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6846 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6847 | return SQLITE_IOERR_LOCK; |
| 6848 | } |
| 6849 | |
| 6850 | if( nTries==1 ){ |
| 6851 | conchModTime = buf.st_mtimespec; |
| 6852 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 6853 | continue; |
| 6854 | } |
| 6855 | |
| 6856 | assert( nTries>1 ); |
| 6857 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 6858 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 6859 | return SQLITE_BUSY; |
| 6860 | } |
| 6861 | |
| 6862 | if( nTries==2 ){ |
| 6863 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6864 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6865 | if( len<0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6866 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6867 | return SQLITE_IOERR_LOCK; |
| 6868 | } |
| 6869 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 6870 | /* don't break the lock if the host id doesn't match */ |
| 6871 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 6872 | return SQLITE_BUSY; |
| 6873 | } |
| 6874 | }else{ |
| 6875 | /* don't break the lock on short read or a version mismatch */ |
| 6876 | return SQLITE_BUSY; |
| 6877 | } |
| 6878 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 6879 | continue; |
| 6880 | } |
| 6881 | |
| 6882 | assert( nTries==3 ); |
| 6883 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 6884 | rc = SQLITE_OK; |
| 6885 | if( lockType==EXCLUSIVE_LOCK ){ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 6886 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6887 | } |
| 6888 | if( !rc ){ |
| 6889 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6890 | } |
| 6891 | } |
| 6892 | } |
| 6893 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 6894 | |
| 6895 | return rc; |
| 6896 | } |
| 6897 | |
| 6898 | /* 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] | 6899 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 6900 | ** lockPath means that the lockPath in the conch file will be used if the |
| 6901 | ** host IDs match, or a new lock path will be generated automatically |
| 6902 | ** and written to the conch file. |
| 6903 | */ |
| 6904 | static int proxyTakeConch(unixFile *pFile){ |
| 6905 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6906 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6907 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6908 | return SQLITE_OK; |
| 6909 | }else{ |
| 6910 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6911 | uuid_t myHostID; |
| 6912 | int pError = 0; |
| 6913 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6914 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6915 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6916 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6917 | int createConch = 0; |
| 6918 | int hostIdMatch = 0; |
| 6919 | int readLen = 0; |
| 6920 | int tryOldLockPath = 0; |
| 6921 | int forceNewLockPath = 0; |
| 6922 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6923 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 6924 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6925 | osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6926 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6927 | rc = proxyGetHostID(myHostID, &pError); |
| 6928 | if( (rc&0xff)==SQLITE_IOERR ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6929 | storeLastErrno(pFile, pError); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6930 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6931 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6932 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6933 | if( rc!=SQLITE_OK ){ |
| 6934 | goto end_takeconch; |
| 6935 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6936 | /* read the existing conch file */ |
| 6937 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 6938 | if( readLen<0 ){ |
| 6939 | /* I/O error: lastErrno set by seekAndRead */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6940 | storeLastErrno(pFile, conchFile->lastErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6941 | rc = SQLITE_IOERR_READ; |
| 6942 | goto end_takeconch; |
| 6943 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 6944 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 6945 | /* a short read or version format mismatch means we need to create a new |
| 6946 | ** conch file. |
| 6947 | */ |
| 6948 | createConch = 1; |
| 6949 | } |
| 6950 | /* if the host id matches and the lock path already exists in the conch |
| 6951 | ** we'll try to use the path there, if we can't open that path, we'll |
| 6952 | ** retry with a new auto-generated path |
| 6953 | */ |
| 6954 | do { /* in case we need to try again for an :auto: named lock file */ |
| 6955 | |
| 6956 | if( !createConch && !forceNewLockPath ){ |
| 6957 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 6958 | PROXY_HOSTIDLEN); |
| 6959 | /* if the conch has data compare the contents */ |
| 6960 | if( !pCtx->lockProxyPath ){ |
| 6961 | /* for auto-named local lock file, just check the host ID and we'll |
| 6962 | ** use the local lock file path that's already in there |
| 6963 | */ |
| 6964 | if( hostIdMatch ){ |
| 6965 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 6966 | |
| 6967 | if( pathLen>=MAXPATHLEN ){ |
| 6968 | pathLen=MAXPATHLEN-1; |
| 6969 | } |
| 6970 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 6971 | lockPath[pathLen] = 0; |
| 6972 | tempLockPath = lockPath; |
| 6973 | tryOldLockPath = 1; |
| 6974 | /* create a copy of the lock path if the conch is taken */ |
| 6975 | goto end_takeconch; |
| 6976 | } |
| 6977 | }else if( hostIdMatch |
| 6978 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 6979 | readLen-PROXY_PATHINDEX) |
| 6980 | ){ |
| 6981 | /* conch host and lock path match */ |
| 6982 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6983 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6984 | } |
| 6985 | |
| 6986 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 6987 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 6988 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6989 | goto end_takeconch; |
| 6990 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6991 | |
| 6992 | /* 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] | 6993 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6994 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 6995 | tempLockPath = lockPath; |
| 6996 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6997 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6998 | |
| 6999 | /* update conch with host and path (this will fail if other process |
| 7000 | ** has a shared lock already), if the host id matches, use the big |
| 7001 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7002 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7003 | futimes(conchFile->h, NULL); |
| 7004 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 7005 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7006 | /* We are trying for an exclusive lock but another thread in this |
| 7007 | ** same process is still holding a shared lock. */ |
| 7008 | rc = SQLITE_BUSY; |
| 7009 | } else { |
| 7010 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7011 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7012 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7013 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7014 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7015 | if( rc==SQLITE_OK ){ |
| 7016 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 7017 | int writeSize = 0; |
| 7018 | |
| 7019 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 7020 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 7021 | if( pCtx->lockProxyPath!=NULL ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7022 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, |
| 7023 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7024 | }else{ |
| 7025 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 7026 | } |
| 7027 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7028 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7029 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 7030 | full_fsync(conchFile->h,0,0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7031 | /* If we created a new conch file (not just updated the contents of a |
| 7032 | ** valid conch file), try to match the permissions of the database |
| 7033 | */ |
| 7034 | if( rc==SQLITE_OK && createConch ){ |
| 7035 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7036 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7037 | if( err==0 ){ |
| 7038 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 7039 | S_IROTH|S_IWOTH); |
| 7040 | /* try to match the database file R/W permissions, ignore failure */ |
| 7041 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7042 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7043 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7044 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7045 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7046 | }while( rc==(-1) && errno==EINTR ); |
| 7047 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7048 | int code = errno; |
| 7049 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 7050 | cmode, code, strerror(code)); |
| 7051 | } else { |
| 7052 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 7053 | } |
| 7054 | }else{ |
| 7055 | int code = errno; |
| 7056 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 7057 | err, code, strerror(code)); |
| 7058 | #endif |
| 7059 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7060 | } |
| 7061 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7062 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 7063 | |
| 7064 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7065 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7066 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 7067 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7068 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 7069 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7070 | } |
| 7071 | pFile->h = -1; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7072 | fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7073 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7074 | if( fd>=0 ){ |
| 7075 | pFile->h = fd; |
| 7076 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 7077 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7078 | during locking */ |
| 7079 | } |
| 7080 | } |
| 7081 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 7082 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 7083 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 7084 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 7085 | /* we couldn't create the proxy lock file with the old lock file path |
| 7086 | ** so try again via auto-naming |
| 7087 | */ |
| 7088 | forceNewLockPath = 1; |
| 7089 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 7090 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7091 | } |
| 7092 | } |
| 7093 | if( rc==SQLITE_OK ){ |
| 7094 | /* Need to make a copy of path if we extracted the value |
| 7095 | ** from the conch file or the path was allocated on the stack |
| 7096 | */ |
| 7097 | if( tempLockPath ){ |
| 7098 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 7099 | if( !pCtx->lockProxyPath ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7100 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7101 | } |
| 7102 | } |
| 7103 | } |
| 7104 | if( rc==SQLITE_OK ){ |
| 7105 | pCtx->conchHeld = 1; |
| 7106 | |
| 7107 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 7108 | afpLockingContext *afpCtx; |
| 7109 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 7110 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 7111 | } |
| 7112 | } else { |
| 7113 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7114 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7115 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 7116 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7117 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7118 | } while (1); /* in case we need to retry the :auto: lock file - |
| 7119 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7120 | } |
| 7121 | } |
| 7122 | |
| 7123 | /* |
| 7124 | ** If pFile holds a lock on a conch file, then release that lock. |
| 7125 | */ |
| 7126 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 7127 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7128 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 7129 | unixFile *conchFile; /* Name of the conch file */ |
| 7130 | |
| 7131 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7132 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7133 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7134 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7135 | osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7136 | if( pCtx->conchHeld>0 ){ |
| 7137 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7138 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7139 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7140 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 7141 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7142 | return rc; |
| 7143 | } |
| 7144 | |
| 7145 | /* |
| 7146 | ** 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] | 7147 | ** Store the conch filename in memory obtained from sqlite3_malloc64(). |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7148 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 7149 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 7150 | ** |
| 7151 | ** The caller is responsible for ensuring that the allocated memory |
| 7152 | ** space is eventually freed. |
| 7153 | ** |
| 7154 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 7155 | */ |
| 7156 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 7157 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7158 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7159 | char *conchPath; /* buffer in which to construct conch name */ |
| 7160 | |
| 7161 | /* Allocate space for the conch filename and initialize the name to |
| 7162 | ** the name of the original database file. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7163 | *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7164 | if( conchPath==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7165 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7166 | } |
| 7167 | memcpy(conchPath, dbPath, len+1); |
| 7168 | |
| 7169 | /* now insert a "." before the last / character */ |
| 7170 | for( i=(len-1); i>=0; i-- ){ |
| 7171 | if( conchPath[i]=='/' ){ |
| 7172 | i++; |
| 7173 | break; |
| 7174 | } |
| 7175 | } |
| 7176 | conchPath[i]='.'; |
| 7177 | while ( i<len ){ |
| 7178 | conchPath[i+1]=dbPath[i]; |
| 7179 | i++; |
| 7180 | } |
| 7181 | |
| 7182 | /* append the "-conch" suffix to the file */ |
| 7183 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7184 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7185 | |
| 7186 | return SQLITE_OK; |
| 7187 | } |
| 7188 | |
| 7189 | |
| 7190 | /* Takes a fully configured proxy locking-style unix file and switches |
| 7191 | ** the local lock file path |
| 7192 | */ |
| 7193 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 7194 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7195 | char *oldPath = pCtx->lockProxyPath; |
| 7196 | int rc = SQLITE_OK; |
| 7197 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7198 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7199 | return SQLITE_BUSY; |
| 7200 | } |
| 7201 | |
| 7202 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 7203 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 7204 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 7205 | return SQLITE_OK; |
| 7206 | }else{ |
| 7207 | unixFile *lockProxy = pCtx->lockProxy; |
| 7208 | pCtx->lockProxy=NULL; |
| 7209 | pCtx->conchHeld = 0; |
| 7210 | if( lockProxy!=NULL ){ |
| 7211 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 7212 | if( rc ) return rc; |
| 7213 | sqlite3_free(lockProxy); |
| 7214 | } |
| 7215 | sqlite3_free(oldPath); |
| 7216 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 7217 | } |
| 7218 | |
| 7219 | return rc; |
| 7220 | } |
| 7221 | |
| 7222 | /* |
| 7223 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 7224 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 7225 | ** |
| 7226 | ** This routine find the filename associated with pFile and writes it |
| 7227 | ** int dbPath. |
| 7228 | */ |
| 7229 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7230 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7231 | if( pFile->pMethod == &afpIoMethods ){ |
| 7232 | /* afp style keeps a reference to the db path in the filePath field |
| 7233 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7234 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7235 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, |
| 7236 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7237 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7238 | #endif |
| 7239 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 7240 | /* dot lock style uses the locking context to store the dot lock |
| 7241 | ** file path */ |
| 7242 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 7243 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 7244 | }else{ |
| 7245 | /* all other styles use the locking context to store the db file path */ |
| 7246 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7247 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7248 | } |
| 7249 | return SQLITE_OK; |
| 7250 | } |
| 7251 | |
| 7252 | /* |
| 7253 | ** Takes an already filled in unix file and alters it so all file locking |
| 7254 | ** will be performed on the local proxy lock file. The following fields |
| 7255 | ** are preserved in the locking context so that they can be restored and |
| 7256 | ** the unix structure properly cleaned up at close time: |
| 7257 | ** ->lockingContext |
| 7258 | ** ->pMethod |
| 7259 | */ |
| 7260 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 7261 | proxyLockingContext *pCtx; |
| 7262 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 7263 | char *lockPath=NULL; |
| 7264 | int rc = SQLITE_OK; |
| 7265 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7266 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7267 | return SQLITE_BUSY; |
| 7268 | } |
| 7269 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 7270 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 7271 | lockPath=NULL; |
| 7272 | }else{ |
| 7273 | lockPath=(char *)path; |
| 7274 | } |
| 7275 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7276 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7277 | (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7278 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7279 | pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7280 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7281 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7282 | } |
| 7283 | memset(pCtx, 0, sizeof(*pCtx)); |
| 7284 | |
| 7285 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 7286 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7287 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 7288 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 7289 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 7290 | ** (c) the file system is read-only, then enable no-locking access. |
| 7291 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 7292 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 7293 | */ |
| 7294 | struct statfs fsInfo; |
| 7295 | struct stat conchInfo; |
| 7296 | int goLockless = 0; |
| 7297 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7298 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7299 | int err = errno; |
| 7300 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 7301 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 7302 | } |
| 7303 | } |
| 7304 | if( goLockless ){ |
| 7305 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 7306 | rc = SQLITE_OK; |
| 7307 | } |
| 7308 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7309 | } |
| 7310 | if( rc==SQLITE_OK && lockPath ){ |
| 7311 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 7312 | } |
| 7313 | |
| 7314 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7315 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 7316 | if( pCtx->dbPath==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7317 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7318 | } |
| 7319 | } |
| 7320 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7321 | /* all memory is allocated, proxys are created and assigned, |
| 7322 | ** switch the locking context and pMethod then return. |
| 7323 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7324 | pCtx->oldLockingContext = pFile->lockingContext; |
| 7325 | pFile->lockingContext = pCtx; |
| 7326 | pCtx->pOldMethod = pFile->pMethod; |
| 7327 | pFile->pMethod = &proxyIoMethods; |
| 7328 | }else{ |
| 7329 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7330 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7331 | sqlite3_free(pCtx->conchFile); |
| 7332 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7333 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7334 | sqlite3_free(pCtx->conchFilePath); |
| 7335 | sqlite3_free(pCtx); |
| 7336 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7337 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 7338 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7339 | return rc; |
| 7340 | } |
| 7341 | |
| 7342 | |
| 7343 | /* |
| 7344 | ** This routine handles sqlite3_file_control() calls that are specific |
| 7345 | ** to proxy locking. |
| 7346 | */ |
| 7347 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 7348 | switch( op ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7349 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7350 | unixFile *pFile = (unixFile*)id; |
| 7351 | if( pFile->pMethod == &proxyIoMethods ){ |
| 7352 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7353 | proxyTakeConch(pFile); |
| 7354 | if( pCtx->lockProxyPath ){ |
| 7355 | *(const char **)pArg = pCtx->lockProxyPath; |
| 7356 | }else{ |
| 7357 | *(const char **)pArg = ":auto: (not held)"; |
| 7358 | } |
| 7359 | } else { |
| 7360 | *(const char **)pArg = NULL; |
| 7361 | } |
| 7362 | return SQLITE_OK; |
| 7363 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7364 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7365 | unixFile *pFile = (unixFile*)id; |
| 7366 | int rc = SQLITE_OK; |
| 7367 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 7368 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 7369 | if( isProxyStyle ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7370 | /* turn off proxy locking - not supported. If support is added for |
| 7371 | ** switching proxy locking mode off then it will need to fail if |
| 7372 | ** the journal mode is WAL mode. |
| 7373 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7374 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 7375 | }else{ |
| 7376 | /* turn off proxy locking - already off - NOOP */ |
| 7377 | rc = SQLITE_OK; |
| 7378 | } |
| 7379 | }else{ |
| 7380 | const char *proxyPath = (const char *)pArg; |
| 7381 | if( isProxyStyle ){ |
| 7382 | proxyLockingContext *pCtx = |
| 7383 | (proxyLockingContext*)pFile->lockingContext; |
| 7384 | if( !strcmp(pArg, ":auto:") |
| 7385 | || (pCtx->lockProxyPath && |
| 7386 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 7387 | ){ |
| 7388 | rc = SQLITE_OK; |
| 7389 | }else{ |
| 7390 | rc = switchLockProxyPath(pFile, proxyPath); |
| 7391 | } |
| 7392 | }else{ |
| 7393 | /* turn on proxy file locking */ |
| 7394 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 7395 | } |
| 7396 | } |
| 7397 | return rc; |
| 7398 | } |
| 7399 | default: { |
| 7400 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 7401 | } |
| 7402 | } |
| 7403 | /*NOTREACHED*/ |
| 7404 | return SQLITE_ERROR; |
| 7405 | } |
| 7406 | |
| 7407 | /* |
| 7408 | ** Within this division (the proxying locking implementation) the procedures |
| 7409 | ** above this point are all utilities. The lock-related methods of the |
| 7410 | ** proxy-locking sqlite3_io_method object follow. |
| 7411 | */ |
| 7412 | |
| 7413 | |
| 7414 | /* |
| 7415 | ** This routine checks if there is a RESERVED lock held on the specified |
| 7416 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 7417 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 7418 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 7419 | */ |
| 7420 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 7421 | unixFile *pFile = (unixFile*)id; |
| 7422 | int rc = proxyTakeConch(pFile); |
| 7423 | if( rc==SQLITE_OK ){ |
| 7424 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7425 | if( pCtx->conchHeld>0 ){ |
| 7426 | unixFile *proxy = pCtx->lockProxy; |
| 7427 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 7428 | }else{ /* conchHeld < 0 is lockless */ |
| 7429 | pResOut=0; |
| 7430 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7431 | } |
| 7432 | return rc; |
| 7433 | } |
| 7434 | |
| 7435 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7436 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7437 | ** of the following: |
| 7438 | ** |
| 7439 | ** (1) SHARED_LOCK |
| 7440 | ** (2) RESERVED_LOCK |
| 7441 | ** (3) PENDING_LOCK |
| 7442 | ** (4) EXCLUSIVE_LOCK |
| 7443 | ** |
| 7444 | ** Sometimes when requesting one lock state, additional lock states |
| 7445 | ** are inserted in between. The locking might fail on one of the later |
| 7446 | ** transitions leaving the lock state different from what it started but |
| 7447 | ** still short of its goal. The following chart shows the allowed |
| 7448 | ** transitions and the inserted intermediate states: |
| 7449 | ** |
| 7450 | ** UNLOCKED -> SHARED |
| 7451 | ** SHARED -> RESERVED |
| 7452 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 7453 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 7454 | ** PENDING -> EXCLUSIVE |
| 7455 | ** |
| 7456 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 7457 | ** routine to lower a locking level. |
| 7458 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7459 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7460 | unixFile *pFile = (unixFile*)id; |
| 7461 | int rc = proxyTakeConch(pFile); |
| 7462 | if( rc==SQLITE_OK ){ |
| 7463 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7464 | if( pCtx->conchHeld>0 ){ |
| 7465 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7466 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 7467 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7468 | }else{ |
| 7469 | /* conchHeld < 0 is lockless */ |
| 7470 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7471 | } |
| 7472 | return rc; |
| 7473 | } |
| 7474 | |
| 7475 | |
| 7476 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7477 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7478 | ** must be either NO_LOCK or SHARED_LOCK. |
| 7479 | ** |
| 7480 | ** If the locking level of the file descriptor is already at or below |
| 7481 | ** the requested locking level, this routine is a no-op. |
| 7482 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7483 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7484 | unixFile *pFile = (unixFile*)id; |
| 7485 | int rc = proxyTakeConch(pFile); |
| 7486 | if( rc==SQLITE_OK ){ |
| 7487 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7488 | if( pCtx->conchHeld>0 ){ |
| 7489 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7490 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 7491 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7492 | }else{ |
| 7493 | /* conchHeld < 0 is lockless */ |
| 7494 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7495 | } |
| 7496 | return rc; |
| 7497 | } |
| 7498 | |
| 7499 | /* |
| 7500 | ** Close a file that uses proxy locks. |
| 7501 | */ |
| 7502 | static int proxyClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 7503 | if( ALWAYS(id) ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7504 | unixFile *pFile = (unixFile*)id; |
| 7505 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7506 | unixFile *lockProxy = pCtx->lockProxy; |
| 7507 | unixFile *conchFile = pCtx->conchFile; |
| 7508 | int rc = SQLITE_OK; |
| 7509 | |
| 7510 | if( lockProxy ){ |
| 7511 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 7512 | if( rc ) return rc; |
| 7513 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 7514 | if( rc ) return rc; |
| 7515 | sqlite3_free(lockProxy); |
| 7516 | pCtx->lockProxy = 0; |
| 7517 | } |
| 7518 | if( conchFile ){ |
| 7519 | if( pCtx->conchHeld ){ |
| 7520 | rc = proxyReleaseConch(pFile); |
| 7521 | if( rc ) return rc; |
| 7522 | } |
| 7523 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 7524 | if( rc ) return rc; |
| 7525 | sqlite3_free(conchFile); |
| 7526 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7527 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7528 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7529 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7530 | /* restore the original locking context and pMethod then close it */ |
| 7531 | pFile->lockingContext = pCtx->oldLockingContext; |
| 7532 | pFile->pMethod = pCtx->pOldMethod; |
| 7533 | sqlite3_free(pCtx); |
| 7534 | return pFile->pMethod->xClose(id); |
| 7535 | } |
| 7536 | return SQLITE_OK; |
| 7537 | } |
| 7538 | |
| 7539 | |
| 7540 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7541 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7542 | /* |
| 7543 | ** The proxy locking style is intended for use with AFP filesystems. |
| 7544 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 7545 | ** restricted to MacOSX. |
| 7546 | ** |
| 7547 | ** |
| 7548 | ******************* End of the proxy lock implementation ********************** |
| 7549 | ******************************************************************************/ |
| 7550 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7551 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7552 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7553 | ** |
| 7554 | ** This routine registers all VFS implementations for unix-like operating |
| 7555 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 7556 | ** should be the only routines in this file that are visible from other |
| 7557 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7558 | ** |
| 7559 | ** This routine is called once during SQLite initialization and by a |
| 7560 | ** single thread. The memory allocation and mutex subsystems have not |
| 7561 | ** necessarily been initialized when this routine is called, and so they |
| 7562 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7563 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7564 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7565 | /* |
| 7566 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7567 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 7568 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 7569 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 7570 | ** and so we have to go through the intermediate pointer to avoid problems |
| 7571 | ** when compiling with -pedantic-errors on GCC.) |
| 7572 | ** |
| 7573 | ** 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] | 7574 | ** finder-function. The finder-function returns a pointer to the |
| 7575 | ** sqlite_io_methods object that implements the desired locking |
| 7576 | ** behaviors. See the division above that contains the IOMETHODS |
| 7577 | ** macro for addition information on finder-functions. |
| 7578 | ** |
| 7579 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 7580 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 7581 | ** more than that; it looks at the filesystem type that hosts the |
| 7582 | ** database file and tries to choose an locking method appropriate for |
| 7583 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7584 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7585 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7586 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7587 | sizeof(unixFile), /* szOsFile */ \ |
| 7588 | MAX_PATHNAME, /* mxPathname */ \ |
| 7589 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7590 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7591 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7592 | unixOpen, /* xOpen */ \ |
| 7593 | unixDelete, /* xDelete */ \ |
| 7594 | unixAccess, /* xAccess */ \ |
| 7595 | unixFullPathname, /* xFullPathname */ \ |
| 7596 | unixDlOpen, /* xDlOpen */ \ |
| 7597 | unixDlError, /* xDlError */ \ |
| 7598 | unixDlSym, /* xDlSym */ \ |
| 7599 | unixDlClose, /* xDlClose */ \ |
| 7600 | unixRandomness, /* xRandomness */ \ |
| 7601 | unixSleep, /* xSleep */ \ |
| 7602 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 7603 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 7604 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7605 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 7606 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 7607 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7608 | } |
| 7609 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7610 | /* |
| 7611 | ** All default VFSes for unix are contained in the following array. |
| 7612 | ** |
| 7613 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 7614 | ** by the SQLite core when the VFS is registered. So the following |
| 7615 | ** array cannot be const. |
| 7616 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7617 | static sqlite3_vfs aVfs[] = { |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7618 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7619 | UNIXVFS("unix", autolockIoFinder ), |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7620 | #elif OS_VXWORKS |
| 7621 | UNIXVFS("unix", vxworksIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7622 | #else |
| 7623 | UNIXVFS("unix", posixIoFinder ), |
| 7624 | #endif |
| 7625 | UNIXVFS("unix-none", nolockIoFinder ), |
| 7626 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7627 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7628 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7629 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7630 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7631 | #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7632 | UNIXVFS("unix-posix", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7633 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7634 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 7635 | UNIXVFS("unix-flock", flockIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7636 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7637 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7638 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7639 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7640 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7641 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7642 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7643 | unsigned int i; /* Loop counter */ |
| 7644 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7645 | /* Double-check that the aSyscall[] array has been constructed |
| 7646 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 7647 | assert( ArraySize(aSyscall)==29 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7648 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7649 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7650 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7651 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7652 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7653 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7654 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7655 | |
| 7656 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7657 | ** Shutdown the operating system interface. |
| 7658 | ** |
| 7659 | ** Some operating systems might need to do some cleanup in this routine, |
| 7660 | ** to release dynamically allocated objects. But not on unix. |
| 7661 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7662 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7663 | int sqlite3_os_end(void){ |
| 7664 | return SQLITE_OK; |
| 7665 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 7666 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 7667 | #endif /* SQLITE_OS_UNIX */ |