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 | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 74 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 75 | ** standard include files. |
| 76 | */ |
| 77 | #include <sys/types.h> |
| 78 | #include <sys/stat.h> |
| 79 | #include <fcntl.h> |
| 80 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 81 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 82 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 83 | #include <errno.h> |
dan | 32c12fe | 2013-05-02 17:37:31 +0000 | [diff] [blame] | 84 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 85 | # include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 86 | #endif |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 87 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 88 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 89 | # include <sys/ioctl.h> |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 90 | # include <sys/file.h> |
| 91 | # include <sys/param.h> |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 92 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 93 | |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 94 | #if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ |
| 95 | (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) |
| 96 | # if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ |
| 97 | && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0)) |
| 98 | # define HAVE_GETHOSTUUID 1 |
| 99 | # else |
| 100 | # warning "gethostuuid() is disabled." |
| 101 | # endif |
| 102 | #endif |
| 103 | |
| 104 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 105 | #if OS_VXWORKS |
| 106 | # include <sys/ioctl.h> |
| 107 | # include <semaphore.h> |
| 108 | # include <limits.h> |
| 109 | #endif /* OS_VXWORKS */ |
| 110 | |
| 111 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 112 | # include <sys/mount.h> |
| 113 | #endif |
| 114 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 115 | #ifdef HAVE_UTIME |
| 116 | # include <utime.h> |
| 117 | #endif |
| 118 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 119 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 120 | ** Allowed values of unixFile.fsFlags |
| 121 | */ |
| 122 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 123 | |
| 124 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 125 | ** If we are to be thread-safe, include the pthreads header and define |
| 126 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 127 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 128 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 129 | # include <pthread.h> |
| 130 | # define SQLITE_UNIX_THREADS 1 |
| 131 | #endif |
| 132 | |
| 133 | /* |
| 134 | ** Default permissions when creating a new file |
| 135 | */ |
| 136 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 137 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 138 | #endif |
| 139 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 140 | /* |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 141 | ** Default permissions when creating auto proxy dir |
| 142 | */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 143 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 144 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 145 | #endif |
| 146 | |
| 147 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 148 | ** Maximum supported path-length. |
| 149 | */ |
| 150 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 151 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 152 | /* Always cast the getpid() return type for compatibility with |
| 153 | ** kernel modules in VxWorks. */ |
| 154 | #define osGetpid(X) (pid_t)getpid() |
| 155 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 156 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 157 | ** Only set the lastErrno if the error code is a real error and not |
| 158 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 159 | */ |
| 160 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 161 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 162 | /* Forward references */ |
| 163 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 164 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 165 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 166 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 169 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 170 | ** cannot be closed immediately. In these cases, instances of the following |
| 171 | ** structure are used to store the file descriptor while waiting for an |
| 172 | ** opportunity to either close or reuse it. |
| 173 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 174 | struct UnixUnusedFd { |
| 175 | int fd; /* File descriptor to close */ |
| 176 | int flags; /* Flags this file descriptor was opened with */ |
| 177 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 178 | }; |
| 179 | |
| 180 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 181 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 182 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 183 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 184 | typedef struct unixFile unixFile; |
| 185 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 186 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 187 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 188 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 189 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 190 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 191 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 192 | int lastErrno; /* The unix errno from last I/O error */ |
| 193 | void *lockingContext; /* Locking style specific state */ |
| 194 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 195 | const char *zPath; /* Name of the file */ |
| 196 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 197 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 198 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 199 | int nFetchOut; /* Number of outstanding xFetch refs */ |
| 200 | sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 201 | sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ |
| 202 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 203 | void *pMapRegion; /* Memory mapped region */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 204 | #endif |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 205 | #ifdef __QNXNTO__ |
| 206 | int sectorSize; /* Device sector size */ |
| 207 | int deviceCharacteristics; /* Precomputed device characteristics */ |
| 208 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 209 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 210 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 211 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 212 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 213 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 214 | #endif |
| 215 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 216 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 217 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 218 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 219 | /* The next group of variables are used to track whether or not the |
| 220 | ** transaction counter in bytes 24-27 of database files are updated |
| 221 | ** whenever any part of the database changes. An assertion fault will |
| 222 | ** occur if a file is updated without also updating the transaction |
| 223 | ** counter. This test is made to avoid new problems similar to the |
| 224 | ** one described by ticket #3584. |
| 225 | */ |
| 226 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 227 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 228 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 229 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 230 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 231 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 232 | #ifdef SQLITE_TEST |
| 233 | /* In test mode, increase the size of this structure a bit so that |
| 234 | ** it is larger than the struct CrashFile defined in test6.c. |
| 235 | */ |
| 236 | char aPadding[32]; |
| 237 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 240 | /* This variable holds the process id (pid) from when the xRandomness() |
| 241 | ** method was called. If xOpen() is called from a different process id, |
| 242 | ** indicating that a fork() has occurred, the PRNG will be reset. |
| 243 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 244 | static pid_t randomnessPid = 0; |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 245 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 246 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 247 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 248 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 249 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 250 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 251 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 252 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 253 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 254 | #else |
| 255 | # define UNIXFILE_DIRSYNC 0x00 |
| 256 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 257 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 258 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 259 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 260 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 261 | #define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings issued */ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 262 | #define UNIXFILE_BLOCK 0x0200 /* Next SHM lock might block */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 263 | |
| 264 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 265 | ** Include code that is common to all os_*.c files |
| 266 | */ |
| 267 | #include "os_common.h" |
| 268 | |
| 269 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 270 | ** Define various macros that are missing from some systems. |
| 271 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 272 | #ifndef O_LARGEFILE |
| 273 | # define O_LARGEFILE 0 |
| 274 | #endif |
| 275 | #ifdef SQLITE_DISABLE_LFS |
| 276 | # undef O_LARGEFILE |
| 277 | # define O_LARGEFILE 0 |
| 278 | #endif |
| 279 | #ifndef O_NOFOLLOW |
| 280 | # define O_NOFOLLOW 0 |
| 281 | #endif |
| 282 | #ifndef O_BINARY |
| 283 | # define O_BINARY 0 |
| 284 | #endif |
| 285 | |
| 286 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 287 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 288 | ** testing and debugging only. |
| 289 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 290 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 291 | #define threadid pthread_self() |
| 292 | #else |
| 293 | #define threadid 0 |
| 294 | #endif |
| 295 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 296 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 297 | ** HAVE_MREMAP defaults to true on Linux and false everywhere else. |
| 298 | */ |
| 299 | #if !defined(HAVE_MREMAP) |
| 300 | # if defined(__linux__) && defined(_GNU_SOURCE) |
| 301 | # define HAVE_MREMAP 1 |
| 302 | # else |
| 303 | # define HAVE_MREMAP 0 |
| 304 | # endif |
| 305 | #endif |
| 306 | |
| 307 | /* |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 308 | ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() |
| 309 | ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. |
| 310 | */ |
| 311 | #ifdef __ANDROID__ |
| 312 | # define lseek lseek64 |
| 313 | #endif |
| 314 | |
| 315 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 316 | ** Different Unix systems declare open() in different ways. Same use |
| 317 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 318 | ** The difference is important when using a pointer to the function. |
| 319 | ** |
| 320 | ** The safest way to deal with the problem is to always use this wrapper |
| 321 | ** which always has the same well-defined interface. |
| 322 | */ |
| 323 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 324 | return open(zFile, flags, mode); |
| 325 | } |
| 326 | |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 327 | /* |
| 328 | ** On some systems, calls to fchown() will trigger a message in a security |
| 329 | ** log if they come from non-root processes. So avoid calling fchown() if |
| 330 | ** we are not running as root. |
| 331 | */ |
| 332 | static int posixFchown(int fd, uid_t uid, gid_t gid){ |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 333 | #if OS_VXWORKS |
| 334 | return 0; |
| 335 | #else |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 336 | return geteuid() ? 0 : fchown(fd,uid,gid); |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 337 | #endif |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 338 | } |
| 339 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 340 | /* Forward reference */ |
| 341 | static int openDirectory(const char*, int*); |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 342 | static int unixGetpagesize(void); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 343 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 344 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 345 | ** Many system calls are accessed through pointer-to-functions so that |
| 346 | ** they may be overridden at runtime to facilitate fault injection during |
| 347 | ** testing and sandboxing. The following array holds the names and pointers |
| 348 | ** to all overrideable system calls. |
| 349 | */ |
| 350 | static struct unix_syscall { |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 351 | const char *zName; /* Name of the system call */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 352 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 353 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 354 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 355 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 356 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 357 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 358 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 359 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 360 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 361 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 362 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 363 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 364 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 365 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 366 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 367 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 368 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 369 | |
| 370 | /* |
| 371 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 372 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 373 | ** that always succeeds. This means that locking does not occur under |
| 374 | ** DJGPP. But it is DOS - what did you expect? |
| 375 | */ |
| 376 | #ifdef __DJGPP__ |
| 377 | { "fstat", 0, 0 }, |
| 378 | #define osFstat(a,b,c) 0 |
| 379 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 380 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 381 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 382 | #endif |
| 383 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 384 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 385 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 386 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 387 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 388 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 389 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 390 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 391 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 392 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 393 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 394 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 395 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 396 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 397 | #endif |
| 398 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 399 | |
| 400 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 401 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 402 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 403 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 404 | #endif |
| 405 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 406 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 407 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 408 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 409 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 410 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 411 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 412 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 413 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 414 | #endif |
| 415 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 416 | aSyscall[12].pCurrent) |
| 417 | |
| 418 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 419 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 420 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 421 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 422 | #endif |
| 423 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 424 | aSyscall[13].pCurrent) |
| 425 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 426 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 427 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 428 | |
| 429 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 430 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 431 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 432 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 433 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 434 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 435 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 436 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 437 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 438 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 439 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 440 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 441 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 442 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 443 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 444 | |
| 445 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 446 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 447 | |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 448 | { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 }, |
dan | d3eaebd | 2012-02-13 08:50:23 +0000 | [diff] [blame] | 449 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
drh | 23c4b97 | 2012-02-11 23:55:15 +0000 | [diff] [blame] | 450 | |
dan | 4dd5144 | 2013-08-26 14:30:25 +0000 | [diff] [blame] | 451 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
dan | 893c0ff | 2013-03-25 19:05:07 +0000 | [diff] [blame] | 452 | { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, |
| 453 | #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent) |
| 454 | |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 455 | { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, |
| 456 | #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent) |
| 457 | |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 458 | #if HAVE_MREMAP |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 459 | { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, |
| 460 | #else |
| 461 | { "mremap", (sqlite3_syscall_ptr)0, 0 }, |
| 462 | #endif |
| 463 | #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent) |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 464 | { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, |
| 465 | #define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent) |
| 466 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 467 | #endif |
| 468 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 469 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 473 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 474 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 475 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 476 | */ |
| 477 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 478 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 479 | const char *zName, /* Name of system call to override */ |
| 480 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 481 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 482 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 483 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 484 | |
| 485 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 486 | if( zName==0 ){ |
| 487 | /* If no zName is given, restore all system calls to their default |
| 488 | ** settings and return NULL |
| 489 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 490 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 491 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 492 | if( aSyscall[i].pDefault ){ |
| 493 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | }else{ |
| 497 | /* If zName is specified, operate on only the one system call |
| 498 | ** specified. |
| 499 | */ |
| 500 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 501 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 502 | if( aSyscall[i].pDefault==0 ){ |
| 503 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 504 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 505 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 506 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 507 | aSyscall[i].pCurrent = pNewFunc; |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | return rc; |
| 513 | } |
| 514 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 515 | /* |
| 516 | ** Return the value of a system call. Return NULL if zName is not a |
| 517 | ** recognized system call name. NULL is also returned if the system call |
| 518 | ** is currently undefined. |
| 519 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 520 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 521 | sqlite3_vfs *pNotUsed, |
| 522 | const char *zName |
| 523 | ){ |
| 524 | unsigned int i; |
| 525 | |
| 526 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 527 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 528 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | ** Return the name of the first system call after zName. If zName==NULL |
| 535 | ** then return the name of the first system call. Return NULL if zName |
| 536 | ** is the last system call or if zName is not the name of a valid |
| 537 | ** system call. |
| 538 | */ |
| 539 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 540 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 541 | |
| 542 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 543 | if( zName ){ |
| 544 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 545 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 548 | for(i++; i<ArraySize(aSyscall); i++){ |
| 549 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 550 | } |
| 551 | return 0; |
| 552 | } |
| 553 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 554 | /* |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 555 | ** Do not accept any file descriptor less than this value, in order to avoid |
| 556 | ** opening database file using file descriptors that are commonly used for |
| 557 | ** standard input, output, and error. |
| 558 | */ |
| 559 | #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
| 560 | # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3 |
| 561 | #endif |
| 562 | |
| 563 | /* |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 564 | ** Invoke open(). Do so multiple times, until it either succeeds or |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 565 | ** fails for some reason other than EINTR. |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 566 | ** |
| 567 | ** If the file creation mode "m" is 0 then set it to the default for |
| 568 | ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 569 | ** 0644) as modified by the system umask. If m is not 0, then |
| 570 | ** make the file creation mode be exactly m ignoring the umask. |
| 571 | ** |
| 572 | ** The m parameter will be non-zero only when creating -wal, -journal, |
| 573 | ** and -shm files. We want those files to have *exactly* the same |
| 574 | ** permissions as their original database, unadulterated by the umask. |
| 575 | ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 576 | ** transaction crashes and leaves behind hot journals, then any |
| 577 | ** process that is able to write to the database will also be able to |
| 578 | ** recover the hot journals. |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 579 | */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 580 | static int robust_open(const char *z, int f, mode_t m){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 581 | int fd; |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 582 | mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 583 | while(1){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 584 | #if defined(O_CLOEXEC) |
| 585 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 586 | #else |
| 587 | fd = osOpen(z,f,m2); |
| 588 | #endif |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 589 | if( fd<0 ){ |
| 590 | if( errno==EINTR ) continue; |
| 591 | break; |
| 592 | } |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 593 | if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 594 | osClose(fd); |
| 595 | sqlite3_log(SQLITE_WARNING, |
| 596 | "attempt to open \"%s\" as file descriptor %d", z, fd); |
| 597 | fd = -1; |
| 598 | if( osOpen("/dev/null", f, m)<0 ) break; |
| 599 | } |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 600 | if( fd>=0 ){ |
| 601 | if( m!=0 ){ |
| 602 | struct stat statbuf; |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 603 | if( osFstat(fd, &statbuf)==0 |
| 604 | && statbuf.st_size==0 |
drh | cfc1769 | 2013-03-06 01:41:53 +0000 | [diff] [blame] | 605 | && (statbuf.st_mode&0777)!=m |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 606 | ){ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 607 | osFchmod(fd, m); |
| 608 | } |
| 609 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 610 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 611 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 612 | #endif |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 613 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 614 | return fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 615 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 616 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 617 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 618 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 619 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 620 | ** vxworksFileId objects used by this file, all of which may be |
| 621 | ** shared by multiple threads. |
| 622 | ** |
| 623 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 624 | ** is held when required. This function is only used as part of assert() |
| 625 | ** statements. e.g. |
| 626 | ** |
| 627 | ** unixEnterMutex() |
| 628 | ** assert( unixMutexHeld() ); |
| 629 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 630 | */ |
| 631 | static void unixEnterMutex(void){ |
| 632 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 633 | } |
| 634 | static void unixLeaveMutex(void){ |
| 635 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 636 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 637 | #ifdef SQLITE_DEBUG |
| 638 | static int unixMutexHeld(void) { |
| 639 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 640 | } |
| 641 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 642 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 643 | |
mistachkin | fb383e9 | 2015-04-16 03:24:38 +0000 | [diff] [blame] | 644 | #ifdef SQLITE_HAVE_OS_TRACE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 645 | /* |
| 646 | ** Helper function for printing out trace information from debugging |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 647 | ** binaries. This returns the string representation of the supplied |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 648 | ** integer lock-type. |
| 649 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 650 | static const char *azFileLock(int eFileLock){ |
| 651 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 652 | case NO_LOCK: return "NONE"; |
| 653 | case SHARED_LOCK: return "SHARED"; |
| 654 | case RESERVED_LOCK: return "RESERVED"; |
| 655 | case PENDING_LOCK: return "PENDING"; |
| 656 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 657 | } |
| 658 | return "ERROR"; |
| 659 | } |
| 660 | #endif |
| 661 | |
| 662 | #ifdef SQLITE_LOCK_TRACE |
| 663 | /* |
| 664 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 665 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 666 | ** This routine is used for troubleshooting locks on multithreaded |
| 667 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 668 | ** command-line option on the compiler. This code is normally |
| 669 | ** turned off. |
| 670 | */ |
| 671 | static int lockTrace(int fd, int op, struct flock *p){ |
| 672 | char *zOpName, *zType; |
| 673 | int s; |
| 674 | int savedErrno; |
| 675 | if( op==F_GETLK ){ |
| 676 | zOpName = "GETLK"; |
| 677 | }else if( op==F_SETLK ){ |
| 678 | zOpName = "SETLK"; |
| 679 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 680 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 681 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 682 | return s; |
| 683 | } |
| 684 | if( p->l_type==F_RDLCK ){ |
| 685 | zType = "RDLCK"; |
| 686 | }else if( p->l_type==F_WRLCK ){ |
| 687 | zType = "WRLCK"; |
| 688 | }else if( p->l_type==F_UNLCK ){ |
| 689 | zType = "UNLCK"; |
| 690 | }else{ |
| 691 | assert( 0 ); |
| 692 | } |
| 693 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 694 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 695 | savedErrno = errno; |
| 696 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 697 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 698 | (int)p->l_pid, s); |
| 699 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 700 | struct flock l2; |
| 701 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 702 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 703 | if( l2.l_type==F_RDLCK ){ |
| 704 | zType = "RDLCK"; |
| 705 | }else if( l2.l_type==F_WRLCK ){ |
| 706 | zType = "WRLCK"; |
| 707 | }else if( l2.l_type==F_UNLCK ){ |
| 708 | zType = "UNLCK"; |
| 709 | }else{ |
| 710 | assert( 0 ); |
| 711 | } |
| 712 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 713 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 714 | } |
| 715 | errno = savedErrno; |
| 716 | return s; |
| 717 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 718 | #undef osFcntl |
| 719 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 720 | #endif /* SQLITE_LOCK_TRACE */ |
| 721 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 722 | /* |
| 723 | ** Retry ftruncate() calls that fail due to EINTR |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 724 | ** |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 725 | ** All calls to ftruncate() within this file should be made through |
| 726 | ** this wrapper. On the Android platform, bypassing the logic below |
| 727 | ** could lead to a corrupt database. |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 728 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 729 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 730 | int rc; |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 731 | #ifdef __ANDROID__ |
| 732 | /* On Android, ftruncate() always uses 32-bit offsets, even if |
| 733 | ** _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] | 734 | ** truncate a file to any size larger than 2GiB. Silently ignore any |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 735 | ** such attempts. */ |
| 736 | if( sz>(sqlite3_int64)0x7FFFFFFF ){ |
| 737 | rc = SQLITE_OK; |
| 738 | }else |
| 739 | #endif |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 740 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 741 | return rc; |
| 742 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 743 | |
| 744 | /* |
| 745 | ** This routine translates a standard POSIX errno code into something |
| 746 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 747 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 748 | ** and a variety of "please close the file descriptor NOW" errors into |
| 749 | ** SQLITE_IOERR |
| 750 | ** |
| 751 | ** Errors during initialization of locks, or file system support for locks, |
| 752 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 753 | */ |
| 754 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 755 | switch (posixError) { |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 756 | #if 0 |
| 757 | /* At one point this code was not commented out. In theory, this branch |
| 758 | ** should never be hit, as this function should only be called after |
| 759 | ** a locking-related function (i.e. fcntl()) has returned non-zero with |
| 760 | ** the value of errno as the first argument. Since a system call has failed, |
| 761 | ** errno should be non-zero. |
| 762 | ** |
| 763 | ** Despite this, if errno really is zero, we still don't want to return |
| 764 | ** SQLITE_OK. The system call failed, and *some* SQLite error should be |
| 765 | ** propagated back to the caller. Commenting this branch out means errno==0 |
| 766 | ** will be handled by the "default:" case below. |
| 767 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 768 | case 0: |
| 769 | return SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 770 | #endif |
| 771 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 772 | case EAGAIN: |
| 773 | case ETIMEDOUT: |
| 774 | case EBUSY: |
| 775 | case EINTR: |
| 776 | case ENOLCK: |
| 777 | /* random NFS retry error, unless during file system support |
| 778 | * introspection, in which it actually means what it says */ |
| 779 | return SQLITE_BUSY; |
| 780 | |
| 781 | case EACCES: |
| 782 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 783 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 784 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 785 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 786 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 787 | return SQLITE_BUSY; |
| 788 | } |
| 789 | /* else fall through */ |
| 790 | case EPERM: |
| 791 | return SQLITE_PERM; |
| 792 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 793 | #if EOPNOTSUPP!=ENOTSUP |
| 794 | case EOPNOTSUPP: |
| 795 | /* something went terribly awry, unless during file system support |
| 796 | * introspection, in which it actually means what it says */ |
| 797 | #endif |
| 798 | #ifdef ENOTSUP |
| 799 | case ENOTSUP: |
| 800 | /* invalid fd, unless during file system support introspection, in which |
| 801 | * it actually means what it says */ |
| 802 | #endif |
| 803 | case EIO: |
| 804 | case EBADF: |
| 805 | case EINVAL: |
| 806 | case ENOTCONN: |
| 807 | case ENODEV: |
| 808 | case ENXIO: |
| 809 | case ENOENT: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 810 | #ifdef ESTALE /* ESTALE is not defined on Interix systems */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 811 | case ESTALE: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 812 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 813 | case ENOSYS: |
| 814 | /* these should force the client to close the file and reconnect */ |
| 815 | |
| 816 | default: |
| 817 | return sqliteIOErr; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 822 | /****************************************************************************** |
| 823 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 824 | ** |
| 825 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 826 | ** the device number and the inode number. But this does not work on VxWorks. |
| 827 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 828 | ** |
| 829 | ** A pointer to an instance of the following structure can be used as a |
| 830 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 831 | ** a copy of the canonical filename. There is also a reference count. |
| 832 | ** The structure is reclaimed when the number of pointers to it drops to |
| 833 | ** zero. |
| 834 | ** |
| 835 | ** There are never very many files open at one time and lookups are not |
| 836 | ** a performance-critical path, so it is sufficient to put these |
| 837 | ** structures on a linked list. |
| 838 | */ |
| 839 | struct vxworksFileId { |
| 840 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 841 | int nRef; /* Number of references to this one */ |
| 842 | int nName; /* Length of the zCanonicalName[] string */ |
| 843 | char *zCanonicalName; /* Canonical filename */ |
| 844 | }; |
| 845 | |
| 846 | #if OS_VXWORKS |
| 847 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 848 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 849 | ** variable: |
| 850 | */ |
| 851 | static struct vxworksFileId *vxworksFileList = 0; |
| 852 | |
| 853 | /* |
| 854 | ** Simplify a filename into its canonical form |
| 855 | ** by making the following changes: |
| 856 | ** |
| 857 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 858 | ** * convert /./ into just / |
| 859 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 860 | ** |
| 861 | ** Changes are made in-place. Return the new name length. |
| 862 | ** |
| 863 | ** The original filename is in z[0..n-1]. Return the number of |
| 864 | ** characters in the simplified name. |
| 865 | */ |
| 866 | static int vxworksSimplifyName(char *z, int n){ |
| 867 | int i, j; |
| 868 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 869 | for(i=j=0; i<n; i++){ |
| 870 | if( z[i]=='/' ){ |
| 871 | if( z[i+1]=='/' ) continue; |
| 872 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 873 | i += 1; |
| 874 | continue; |
| 875 | } |
| 876 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 877 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 878 | if( j>0 ){ j--; } |
| 879 | i += 2; |
| 880 | continue; |
| 881 | } |
| 882 | } |
| 883 | z[j++] = z[i]; |
| 884 | } |
| 885 | z[j] = 0; |
| 886 | return j; |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | ** Find a unique file ID for the given absolute pathname. Return |
| 891 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 892 | ** file ID. |
| 893 | ** |
| 894 | ** The nRef field of the vxworksFileId object is incremented before |
| 895 | ** the object is returned. A new vxworksFileId object is created |
| 896 | ** and added to the global list if necessary. |
| 897 | ** |
| 898 | ** If a memory allocation error occurs, return NULL. |
| 899 | */ |
| 900 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 901 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 902 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 903 | int n; /* Length of zAbsoluteName string */ |
| 904 | |
| 905 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 906 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 907 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 908 | if( pNew==0 ) return 0; |
| 909 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 910 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 911 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 912 | |
| 913 | /* Search for an existing entry that matching the canonical name. |
| 914 | ** If found, increment the reference count and return a pointer to |
| 915 | ** the existing file ID. |
| 916 | */ |
| 917 | unixEnterMutex(); |
| 918 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 919 | if( pCandidate->nName==n |
| 920 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 921 | ){ |
| 922 | sqlite3_free(pNew); |
| 923 | pCandidate->nRef++; |
| 924 | unixLeaveMutex(); |
| 925 | return pCandidate; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | /* No match was found. We will make a new file ID */ |
| 930 | pNew->nRef = 1; |
| 931 | pNew->nName = n; |
| 932 | pNew->pNext = vxworksFileList; |
| 933 | vxworksFileList = pNew; |
| 934 | unixLeaveMutex(); |
| 935 | return pNew; |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | ** Decrement the reference count on a vxworksFileId object. Free |
| 940 | ** the object when the reference count reaches zero. |
| 941 | */ |
| 942 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 943 | unixEnterMutex(); |
| 944 | assert( pId->nRef>0 ); |
| 945 | pId->nRef--; |
| 946 | if( pId->nRef==0 ){ |
| 947 | struct vxworksFileId **pp; |
| 948 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 949 | assert( *pp==pId ); |
| 950 | *pp = pId->pNext; |
| 951 | sqlite3_free(pId); |
| 952 | } |
| 953 | unixLeaveMutex(); |
| 954 | } |
| 955 | #endif /* OS_VXWORKS */ |
| 956 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 957 | ******************************************************************************/ |
| 958 | |
| 959 | |
| 960 | /****************************************************************************** |
| 961 | *************************** Posix Advisory Locking **************************** |
| 962 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 963 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 964 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 965 | ** sets or clears a lock, that operation overrides any prior locks set |
| 966 | ** by the same process. It does not explicitly say so, but this implies |
| 967 | ** that it overrides locks set by the same process using a different |
| 968 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 969 | ** |
| 970 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 971 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 972 | ** |
| 973 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 974 | ** one is a hard or symbolic link to the other) then if you set |
| 975 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 976 | ** on fd2, it works. I would have expected the second lock to |
| 977 | ** fail since there was already a lock on the file due to fd1. |
| 978 | ** But not so. Since both locks came from the same process, the |
| 979 | ** second overrides the first, even though they were on different |
| 980 | ** file descriptors opened on different file names. |
| 981 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 982 | ** This means that we cannot use POSIX locks to synchronize file access |
| 983 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 984 | ** to synchronize access for threads in separate processes, but not |
| 985 | ** threads within the same process. |
| 986 | ** |
| 987 | ** To work around the problem, SQLite has to manage file locks internally |
| 988 | ** on its own. Whenever a new database is opened, we have to find the |
| 989 | ** specific inode of the database file (the inode is determined by the |
| 990 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 991 | ** and check for locks already existing on that inode. When locks are |
| 992 | ** created or removed, we have to look at our own internal record of the |
| 993 | ** locks to see if another thread has previously set a lock on that same |
| 994 | ** inode. |
| 995 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 996 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 997 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 998 | ** canonical filename and implemented in the previous division.) |
| 999 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1000 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1001 | ** descriptor. It is now a structure that holds the integer file |
| 1002 | ** descriptor and a pointer to a structure that describes the internal |
| 1003 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1004 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1005 | ** point to the same locking structure. The locking structure keeps |
| 1006 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 1007 | ** field that tells us its internal lock status. cnt==0 means the |
| 1008 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 1009 | ** cnt>0 means there are cnt shared locks on the file. |
| 1010 | ** |
| 1011 | ** Any attempt to lock or unlock a file first checks the locking |
| 1012 | ** structure. The fcntl() system call is only invoked to set a |
| 1013 | ** POSIX lock if the internal lock structure transitions between |
| 1014 | ** a locked and an unlocked state. |
| 1015 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1016 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1017 | ** |
| 1018 | ** If you close a file descriptor that points to a file that has locks, |
| 1019 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1020 | ** released. To work around this problem, each unixInodeInfo object |
| 1021 | ** maintains a count of the number of pending locks on tha inode. |
| 1022 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1023 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1024 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1025 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1026 | ** be closed and that list is walked (and cleared) when the last lock |
| 1027 | ** clears. |
| 1028 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1029 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1030 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1031 | ** Many older versions of linux use the LinuxThreads library which is |
| 1032 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1033 | ** A cannot be modified or overridden by a different thread B. |
| 1034 | ** Only thread A can modify the lock. Locking behavior is correct |
| 1035 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 1036 | ** on linux - with NPTL a lock created by thread A can override locks |
| 1037 | ** in thread B. But there is no way to know at compile-time which |
| 1038 | ** threading library is being used. So there is no way to know at |
| 1039 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1040 | ** 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] | 1041 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1042 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1043 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 1044 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 1045 | ** LinuxThreads provided that (1) there is no more than one connection |
| 1046 | ** per database file in the same process and (2) database connections |
| 1047 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1048 | */ |
| 1049 | |
| 1050 | /* |
| 1051 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1052 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1053 | */ |
| 1054 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1055 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1056 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1057 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1058 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1059 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1060 | #endif |
| 1061 | }; |
| 1062 | |
| 1063 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1064 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1065 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 1066 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1067 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1068 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1069 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1070 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1071 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1072 | struct unixInodeInfo { |
| 1073 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1074 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1075 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 1076 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1077 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1078 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 1079 | int nLock; /* Number of outstanding file locks */ |
| 1080 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 1081 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 1082 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 1083 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1084 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 1085 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1086 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1087 | sem_t *pSem; /* Named POSIX semaphore */ |
| 1088 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1089 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1090 | }; |
| 1091 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1092 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1093 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1094 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1095 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1096 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1097 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1098 | ** |
| 1099 | ** This function - unixLogError_x(), is only ever called via the macro |
| 1100 | ** unixLogError(). |
| 1101 | ** |
| 1102 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1103 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1104 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1105 | ** strerror_r(). |
| 1106 | ** |
| 1107 | ** The first argument passed to the macro should be the error code that |
| 1108 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1109 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1110 | ** failed (e.g. "unlink", "open") and the associated file-system path, |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1111 | ** if any. |
| 1112 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1113 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1114 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1115 | int errcode, /* SQLite error code */ |
| 1116 | const char *zFunc, /* Name of OS function that failed */ |
| 1117 | const char *zPath, /* File path associated with error */ |
| 1118 | int iLine /* Source line number where error occurred */ |
| 1119 | ){ |
| 1120 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1121 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1122 | |
| 1123 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1124 | ** the strerror() function to obtain the human-readable error message |
| 1125 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1126 | */ |
| 1127 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1128 | char aErr[80]; |
| 1129 | memset(aErr, 0, sizeof(aErr)); |
| 1130 | zErr = aErr; |
| 1131 | |
| 1132 | /* 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] | 1133 | ** assume that the system provides the GNU version of strerror_r() that |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1134 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1135 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1136 | ** Otherwise, assume that the system provides the POSIX version of |
| 1137 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1138 | ** |
| 1139 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1140 | ** available, the error message will often be an empty string. Not a |
| 1141 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1142 | ** could lead to a segfault though. |
| 1143 | */ |
| 1144 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1145 | zErr = |
| 1146 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1147 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1148 | |
| 1149 | #elif SQLITE_THREADSAFE |
| 1150 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1151 | zErr = ""; |
| 1152 | #else |
| 1153 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1154 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1155 | #endif |
| 1156 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1157 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1158 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1159 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1160 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1161 | ); |
| 1162 | |
| 1163 | return errcode; |
| 1164 | } |
| 1165 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1166 | /* |
| 1167 | ** Close a file descriptor. |
| 1168 | ** |
| 1169 | ** We assume that close() almost always works, since it is only in a |
| 1170 | ** very sick application or on a very sick platform that it might fail. |
| 1171 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1172 | ** error. |
| 1173 | ** |
| 1174 | ** Note that it is not safe to retry close() after EINTR since the |
| 1175 | ** file descriptor might have already been reused by another thread. |
| 1176 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1177 | ** and move on. |
| 1178 | */ |
| 1179 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1180 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1181 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1182 | pFile ? pFile->zPath : 0, lineno); |
| 1183 | } |
| 1184 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1185 | |
| 1186 | /* |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 1187 | ** Set the pFile->lastErrno. Do this in a subroutine as that provides |
| 1188 | ** a convenient place to set a breakpoint. |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1189 | */ |
| 1190 | static void storeLastErrno(unixFile *pFile, int error){ |
| 1191 | pFile->lastErrno = error; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1195 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1196 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1197 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1198 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1199 | UnixUnusedFd *p; |
| 1200 | UnixUnusedFd *pNext; |
| 1201 | for(p=pInode->pUnused; p; p=pNext){ |
| 1202 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1203 | robust_close(pFile, p->fd, __LINE__); |
| 1204 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1205 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1206 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1210 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1211 | ** |
| 1212 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1213 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1214 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1215 | static void releaseInodeInfo(unixFile *pFile){ |
| 1216 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1217 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1218 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1219 | pInode->nRef--; |
| 1220 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1221 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1222 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1223 | if( pInode->pPrev ){ |
| 1224 | assert( pInode->pPrev->pNext==pInode ); |
| 1225 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1226 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1227 | assert( inodeList==pInode ); |
| 1228 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1229 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1230 | if( pInode->pNext ){ |
| 1231 | assert( pInode->pNext->pPrev==pInode ); |
| 1232 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1233 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1234 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1235 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1240 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1241 | ** describes that file descriptor. Create a new one if necessary. The |
| 1242 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1243 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1244 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1245 | ** when this function is called. |
| 1246 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1247 | ** Return an appropriate error code. |
| 1248 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1249 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1250 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1251 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1252 | ){ |
| 1253 | int rc; /* System call return code */ |
| 1254 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1255 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1256 | struct stat statbuf; /* Low-level file information */ |
| 1257 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1258 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1259 | assert( unixMutexHeld() ); |
| 1260 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1261 | /* Get low-level information about the file that we can used to |
| 1262 | ** create a unique name for the file. |
| 1263 | */ |
| 1264 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1265 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1266 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1267 | storeLastErrno(pFile, errno); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1268 | #ifdef EOVERFLOW |
| 1269 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1270 | #endif |
| 1271 | return SQLITE_IOERR; |
| 1272 | } |
| 1273 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1274 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1275 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1276 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1277 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1278 | ** we always increase the file size to 1 by writing a single byte |
| 1279 | ** prior to accessing the inode number. The one byte written is |
| 1280 | ** an ASCII 'S' character which also happens to be the first byte |
| 1281 | ** in the header of every SQLite database. In this way, if there |
| 1282 | ** is a race condition such that another thread has already populated |
| 1283 | ** the first page of the database, no damage is done. |
| 1284 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1285 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1286 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1287 | if( rc!=1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1288 | storeLastErrno(pFile, errno); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1289 | return SQLITE_IOERR; |
| 1290 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1291 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1292 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1293 | storeLastErrno(pFile, errno); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1294 | return SQLITE_IOERR; |
| 1295 | } |
| 1296 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1297 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1298 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1299 | memset(&fileId, 0, sizeof(fileId)); |
| 1300 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1301 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1302 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1303 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1304 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1305 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1306 | pInode = inodeList; |
| 1307 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1308 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1309 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1310 | if( pInode==0 ){ |
| 1311 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 1312 | if( pInode==0 ){ |
| 1313 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1314 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1315 | memset(pInode, 0, sizeof(*pInode)); |
| 1316 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1317 | pInode->nRef = 1; |
| 1318 | pInode->pNext = inodeList; |
| 1319 | pInode->pPrev = 0; |
| 1320 | if( inodeList ) inodeList->pPrev = pInode; |
| 1321 | inodeList = pInode; |
| 1322 | }else{ |
| 1323 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1324 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1325 | *ppInode = pInode; |
| 1326 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1327 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1328 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1329 | /* |
| 1330 | ** Return TRUE if pFile has been renamed or unlinked since it was first opened. |
| 1331 | */ |
| 1332 | static int fileHasMoved(unixFile *pFile){ |
drh | 61ffea5 | 2014-08-12 12:19:25 +0000 | [diff] [blame] | 1333 | #if OS_VXWORKS |
| 1334 | return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; |
| 1335 | #else |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1336 | struct stat buf; |
| 1337 | return pFile->pInode!=0 && |
drh | 61ffea5 | 2014-08-12 12:19:25 +0000 | [diff] [blame] | 1338 | (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino); |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 1339 | #endif |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1342 | |
| 1343 | /* |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1344 | ** Check a unixFile that is a database. Verify the following: |
| 1345 | ** |
| 1346 | ** (1) There is exactly one hard link on the file |
| 1347 | ** (2) The file is not a symbolic link |
| 1348 | ** (3) The file has not been renamed or unlinked |
| 1349 | ** |
| 1350 | ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. |
| 1351 | */ |
| 1352 | static void verifyDbFile(unixFile *pFile){ |
| 1353 | struct stat buf; |
| 1354 | int rc; |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 1355 | if( pFile->ctrlFlags & UNIXFILE_WARNED ){ |
| 1356 | /* One or more of the following warnings have already been issued. Do not |
| 1357 | ** repeat them so as not to clutter the error log */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1358 | return; |
| 1359 | } |
| 1360 | rc = osFstat(pFile->h, &buf); |
| 1361 | if( rc!=0 ){ |
| 1362 | sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); |
| 1363 | pFile->ctrlFlags |= UNIXFILE_WARNED; |
| 1364 | return; |
| 1365 | } |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 1366 | if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1367 | sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); |
| 1368 | pFile->ctrlFlags |= UNIXFILE_WARNED; |
| 1369 | return; |
| 1370 | } |
| 1371 | if( buf.st_nlink>1 ){ |
| 1372 | sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); |
| 1373 | pFile->ctrlFlags |= UNIXFILE_WARNED; |
| 1374 | return; |
| 1375 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1376 | if( fileHasMoved(pFile) ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1377 | sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); |
| 1378 | pFile->ctrlFlags |= UNIXFILE_WARNED; |
| 1379 | return; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1385 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1386 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1387 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1388 | ** 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] | 1389 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1390 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1391 | int rc = SQLITE_OK; |
| 1392 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1393 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1394 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1395 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1396 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1397 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1398 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1399 | |
| 1400 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1401 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1402 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1405 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1406 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1407 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1408 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1409 | struct flock lock; |
| 1410 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1411 | lock.l_start = RESERVED_BYTE; |
| 1412 | lock.l_len = 1; |
| 1413 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1414 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1415 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1416 | storeLastErrno(pFile, errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1417 | } else if( lock.l_type!=F_UNLCK ){ |
| 1418 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1419 | } |
| 1420 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1421 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1422 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1423 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1424 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1425 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1426 | *pResOut = reserved; |
| 1427 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1431 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1432 | ** described by pLock. |
| 1433 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1434 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1435 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1436 | ** the first time any lock is attempted. All subsequent system locking |
| 1437 | ** operations become no-ops. Locking operations still happen internally, |
| 1438 | ** in order to coordinate access between separate database connections |
| 1439 | ** within this process, but all of that is handled in memory and the |
| 1440 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1441 | ** |
| 1442 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1443 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1444 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1445 | ** |
| 1446 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1447 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1448 | */ |
| 1449 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1450 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1451 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1452 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1453 | assert( pInode!=0 ); |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1454 | if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock) |
| 1455 | && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0) |
| 1456 | ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1457 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1458 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1459 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1460 | lock.l_whence = SEEK_SET; |
| 1461 | lock.l_start = SHARED_FIRST; |
| 1462 | lock.l_len = SHARED_SIZE; |
| 1463 | lock.l_type = F_WRLCK; |
| 1464 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1465 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1466 | pInode->bProcessLock = 1; |
| 1467 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1468 | }else{ |
| 1469 | rc = 0; |
| 1470 | } |
| 1471 | }else{ |
| 1472 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1473 | } |
| 1474 | return rc; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1478 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1479 | ** of the following: |
| 1480 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1481 | ** (1) SHARED_LOCK |
| 1482 | ** (2) RESERVED_LOCK |
| 1483 | ** (3) PENDING_LOCK |
| 1484 | ** (4) EXCLUSIVE_LOCK |
| 1485 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1486 | ** Sometimes when requesting one lock state, additional lock states |
| 1487 | ** are inserted in between. The locking might fail on one of the later |
| 1488 | ** transitions leaving the lock state different from what it started but |
| 1489 | ** still short of its goal. The following chart shows the allowed |
| 1490 | ** transitions and the inserted intermediate states: |
| 1491 | ** |
| 1492 | ** UNLOCKED -> SHARED |
| 1493 | ** SHARED -> RESERVED |
| 1494 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1495 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1496 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1497 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1498 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1499 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1500 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1501 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1502 | /* The following describes the implementation of the various locks and |
| 1503 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1504 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1505 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1506 | ** slightly in order to be compatible with windows systems simultaneously |
| 1507 | ** accessing the same database file, in case that is ever required. |
| 1508 | ** |
| 1509 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1510 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1511 | ** range', a range of 510 bytes at a well known offset. |
| 1512 | ** |
| 1513 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1514 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1515 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1516 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1517 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1518 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1519 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1520 | ** |
| 1521 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1522 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1523 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1524 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1525 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1526 | ** This property is used by the algorithm for rolling back a journal file |
| 1527 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1528 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1529 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1530 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1531 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1532 | ** within this range, this ensures that no other locks are held on the |
| 1533 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1534 | ** |
| 1535 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1536 | ** range' is that some versions of windows do not support read-locks. By |
| 1537 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1538 | ** even if the locking primitive used is always a write-lock. |
| 1539 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1540 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1541 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1542 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1543 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1544 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1545 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1546 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1547 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1548 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 1549 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1550 | osGetpid(0))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* 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] | 1553 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1554 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1555 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1556 | if( pFile->eFileLock>=eFileLock ){ |
| 1557 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1558 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1559 | return SQLITE_OK; |
| 1560 | } |
| 1561 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1562 | /* Make sure the locking sequence is correct. |
| 1563 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1564 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1565 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1566 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1567 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1568 | assert( eFileLock!=PENDING_LOCK ); |
| 1569 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1570 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1571 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1572 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1573 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1574 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1575 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1576 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1577 | ** handle that precludes the requested lock, return BUSY. |
| 1578 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1579 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1580 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1581 | ){ |
| 1582 | rc = SQLITE_BUSY; |
| 1583 | goto end_lock; |
| 1584 | } |
| 1585 | |
| 1586 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1587 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1588 | ** return SQLITE_OK. |
| 1589 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1590 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1591 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1592 | assert( eFileLock==SHARED_LOCK ); |
| 1593 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1594 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1595 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1596 | pInode->nShared++; |
| 1597 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1598 | goto end_lock; |
| 1599 | } |
| 1600 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1601 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1602 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1603 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1604 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1605 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1606 | lock.l_len = 1L; |
| 1607 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1608 | if( eFileLock==SHARED_LOCK |
| 1609 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1610 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1611 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1612 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1613 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1614 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1615 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1616 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1617 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1618 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1619 | goto end_lock; |
| 1620 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | |
| 1624 | /* If control gets to this point, then actually go ahead and make |
| 1625 | ** operating system calls for the specified lock. |
| 1626 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1627 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1628 | assert( pInode->nShared==0 ); |
| 1629 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1630 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1631 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1632 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1633 | lock.l_start = SHARED_FIRST; |
| 1634 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1635 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1636 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1637 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1638 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1639 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1640 | /* Drop the temporary PENDING lock */ |
| 1641 | lock.l_start = PENDING_BYTE; |
| 1642 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1643 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1644 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1645 | /* This could happen with a network mount */ |
| 1646 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1647 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1648 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1649 | |
| 1650 | if( rc ){ |
| 1651 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1652 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1653 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1654 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1655 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1656 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1657 | pInode->nLock++; |
| 1658 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1659 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1660 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1661 | /* We are trying for an exclusive lock but another thread in this |
| 1662 | ** same process is still holding a shared lock. */ |
| 1663 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1664 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1665 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1666 | ** assumed that there is a SHARED or greater lock on the file |
| 1667 | ** already. |
| 1668 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1669 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1670 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1671 | |
| 1672 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1673 | if( eFileLock==RESERVED_LOCK ){ |
| 1674 | lock.l_start = RESERVED_BYTE; |
| 1675 | lock.l_len = 1L; |
| 1676 | }else{ |
| 1677 | lock.l_start = SHARED_FIRST; |
| 1678 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1679 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1680 | |
| 1681 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1682 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1683 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1684 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1685 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1686 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1687 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1688 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1689 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1690 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1691 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1692 | /* Set up the transaction-counter change checking flags when |
| 1693 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1694 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1695 | ** write operation (not a hot journal rollback). |
| 1696 | */ |
| 1697 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1698 | && pFile->eFileLock<=SHARED_LOCK |
| 1699 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1700 | ){ |
| 1701 | pFile->transCntrChng = 0; |
| 1702 | pFile->dbUpdate = 0; |
| 1703 | pFile->inNormalWrite = 1; |
| 1704 | } |
| 1705 | #endif |
| 1706 | |
| 1707 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1708 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1709 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1710 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1711 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1712 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1713 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1714 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1715 | |
| 1716 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1717 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1718 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1719 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1720 | return rc; |
| 1721 | } |
| 1722 | |
| 1723 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1724 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1725 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1726 | */ |
| 1727 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1728 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1729 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1730 | p->pNext = pInode->pUnused; |
| 1731 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1732 | pFile->h = -1; |
| 1733 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1737 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1738 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1739 | ** |
| 1740 | ** If the locking level of the file descriptor is already at or below |
| 1741 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1742 | ** |
| 1743 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1744 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1745 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1746 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1747 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1748 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1749 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1750 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1751 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1752 | struct flock lock; |
| 1753 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1754 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1755 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1756 | 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] | 1757 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1758 | osGetpid(0))); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1759 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1760 | assert( eFileLock<=SHARED_LOCK ); |
| 1761 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1762 | return SQLITE_OK; |
| 1763 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1764 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1765 | pInode = pFile->pInode; |
| 1766 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1767 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1768 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1769 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1770 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1771 | /* When reducing a lock such that other processes can start |
| 1772 | ** reading the database file again, make sure that the |
| 1773 | ** transaction counter was updated if any part of the database |
| 1774 | ** file changed. If the transaction counter is not updated, |
| 1775 | ** other connections to the same file might not realize that |
| 1776 | ** the file has changed and hence might not know to flush their |
| 1777 | ** cache. The use of a stale cache can lead to database corruption. |
| 1778 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1779 | pFile->inNormalWrite = 0; |
| 1780 | #endif |
| 1781 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1782 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1783 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1784 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1785 | ** write lock until the rest is covered by a read lock: |
| 1786 | ** 1: [WWWWW] |
| 1787 | ** 2: [....W] |
| 1788 | ** 3: [RRRRW] |
| 1789 | ** 4: [RRRR.] |
| 1790 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1791 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1792 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1793 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1794 | assert( handleNFSUnlock==0 ); |
| 1795 | #endif |
| 1796 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1797 | if( handleNFSUnlock ){ |
drh | a712b4b | 2015-02-19 16:12:04 +0000 | [diff] [blame] | 1798 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1799 | off_t divSize = SHARED_SIZE - 1; |
| 1800 | |
| 1801 | lock.l_type = F_UNLCK; |
| 1802 | lock.l_whence = SEEK_SET; |
| 1803 | lock.l_start = SHARED_FIRST; |
| 1804 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1805 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1806 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1807 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1808 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1809 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1810 | } |
| 1811 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1812 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1813 | lock.l_type = F_RDLCK; |
| 1814 | lock.l_whence = SEEK_SET; |
| 1815 | lock.l_start = SHARED_FIRST; |
| 1816 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1817 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1818 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1819 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1820 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1821 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1822 | } |
| 1823 | goto end_unlock; |
| 1824 | } |
| 1825 | lock.l_type = F_UNLCK; |
| 1826 | lock.l_whence = SEEK_SET; |
| 1827 | lock.l_start = SHARED_FIRST+divSize; |
| 1828 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1829 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1830 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1831 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1832 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1833 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1834 | } |
| 1835 | goto end_unlock; |
| 1836 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1837 | }else |
| 1838 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1839 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1840 | lock.l_type = F_RDLCK; |
| 1841 | lock.l_whence = SEEK_SET; |
| 1842 | lock.l_start = SHARED_FIRST; |
| 1843 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1844 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1845 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1846 | ** process is holding an incompatible lock. If it does, this |
| 1847 | ** indicates that the other process is not following the locking |
| 1848 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1849 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1850 | ** an assert to fail). */ |
| 1851 | rc = SQLITE_IOERR_RDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1852 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1853 | goto end_unlock; |
| 1854 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1855 | } |
| 1856 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1857 | lock.l_type = F_UNLCK; |
| 1858 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1859 | lock.l_start = PENDING_BYTE; |
| 1860 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1861 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1862 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1863 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1864 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1865 | storeLastErrno(pFile, errno); |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1866 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1867 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1868 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1869 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1870 | /* Decrement the shared lock counter. Release the lock using an |
| 1871 | ** OS call only when all threads in this same process have released |
| 1872 | ** the lock. |
| 1873 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1874 | pInode->nShared--; |
| 1875 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1876 | lock.l_type = F_UNLCK; |
| 1877 | lock.l_whence = SEEK_SET; |
| 1878 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1879 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1880 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1881 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1882 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1883 | storeLastErrno(pFile, errno); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1884 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1885 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1886 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1889 | /* Decrement the count of locks against this same file. When the |
| 1890 | ** count reaches zero, close any other file descriptors whose close |
| 1891 | ** was deferred because of outstanding locks. |
| 1892 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1893 | pInode->nLock--; |
| 1894 | assert( pInode->nLock>=0 ); |
| 1895 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1896 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1897 | } |
| 1898 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1899 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1900 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1901 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1902 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1903 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1907 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1908 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1909 | ** |
| 1910 | ** If the locking level of the file descriptor is already at or below |
| 1911 | ** the requested locking level, this routine is a no-op. |
| 1912 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1913 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1914 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | a1afc74 | 2013-03-25 13:50:49 +0000 | [diff] [blame] | 1915 | assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1916 | #endif |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1917 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1920 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1921 | static int unixMapfile(unixFile *pFd, i64 nByte); |
| 1922 | static void unixUnmapfile(unixFile *pFd); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1923 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1924 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1925 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1926 | ** This function performs the parts of the "close file" operation |
| 1927 | ** common to all locking schemes. It closes the directory and file |
| 1928 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1929 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1930 | ** |
| 1931 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1932 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1933 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1934 | */ |
| 1935 | static int closeUnixFile(sqlite3_file *id){ |
| 1936 | unixFile *pFile = (unixFile*)id; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1937 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1938 | unixUnmapfile(pFile); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1939 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1940 | if( pFile->h>=0 ){ |
| 1941 | robust_close(pFile, pFile->h, __LINE__); |
| 1942 | pFile->h = -1; |
| 1943 | } |
| 1944 | #if OS_VXWORKS |
| 1945 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 1946 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 1947 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1948 | } |
| 1949 | vxworksReleaseFileId(pFile->pId); |
| 1950 | pFile->pId = 0; |
| 1951 | } |
| 1952 | #endif |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 1953 | #ifdef SQLITE_UNLINK_AFTER_CLOSE |
| 1954 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
| 1955 | osUnlink(pFile->zPath); |
| 1956 | sqlite3_free(*(char**)&pFile->zPath); |
| 1957 | pFile->zPath = 0; |
| 1958 | } |
| 1959 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1960 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1961 | OpenCounter(-1); |
| 1962 | sqlite3_free(pFile->pUnused); |
| 1963 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1964 | return SQLITE_OK; |
| 1965 | } |
| 1966 | |
| 1967 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1968 | ** Close a file. |
| 1969 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1970 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1971 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1972 | unixFile *pFile = (unixFile *)id; |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1973 | verifyDbFile(pFile); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1974 | unixUnlock(id, NO_LOCK); |
| 1975 | unixEnterMutex(); |
| 1976 | |
| 1977 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 1978 | ** routine (e.g. nolockClose()) would be called instead. |
| 1979 | */ |
| 1980 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 1981 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 1982 | /* If there are outstanding locks, do not actually close the file just |
| 1983 | ** yet because that would clear those locks. Instead, add the file |
| 1984 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 1985 | ** when the last lock is cleared. |
| 1986 | */ |
| 1987 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1988 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1989 | releaseInodeInfo(pFile); |
| 1990 | rc = closeUnixFile(id); |
| 1991 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1992 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1995 | /************** End of the posix advisory lock implementation ***************** |
| 1996 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1997 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1998 | /****************************************************************************** |
| 1999 | ****************************** No-op Locking ********************************** |
| 2000 | ** |
| 2001 | ** Of the various locking implementations available, this is by far the |
| 2002 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 2003 | ** file for reading or writing. |
| 2004 | ** |
| 2005 | ** This locking mode is appropriate for use on read-only databases |
| 2006 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 2007 | ** also be used if the application employs some external mechanism to |
| 2008 | ** prevent simultaneous access of the same database by two or more |
| 2009 | ** database connections. But there is a serious risk of database |
| 2010 | ** corruption if this locking mode is used in situations where multiple |
| 2011 | ** database connections are accessing the same database file at the same |
| 2012 | ** time and one or more of those connections are writing. |
| 2013 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2014 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2015 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 2016 | UNUSED_PARAMETER(NotUsed); |
| 2017 | *pResOut = 0; |
| 2018 | return SQLITE_OK; |
| 2019 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2020 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2021 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2022 | return SQLITE_OK; |
| 2023 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2024 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2025 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2026 | return SQLITE_OK; |
| 2027 | } |
| 2028 | |
| 2029 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2030 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2031 | */ |
| 2032 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2033 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | /******************* End of the no-op lock implementation ********************* |
| 2037 | ******************************************************************************/ |
| 2038 | |
| 2039 | /****************************************************************************** |
| 2040 | ************************* Begin dot-file Locking ****************************** |
| 2041 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2042 | ** The dotfile locking implementation uses the existence of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2043 | ** files (really a directory) to control access to the database. This works |
| 2044 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2045 | ** |
| 2046 | ** (1) There is zero concurrency. A single reader blocks all other |
| 2047 | ** connections from reading or writing the database. |
| 2048 | ** |
| 2049 | ** (2) An application crash or power loss can leave stale lock files |
| 2050 | ** sitting around that need to be cleared manually. |
| 2051 | ** |
| 2052 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 2053 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2054 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2055 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 2056 | ** the database and with the same name but with a ".lock" extension added. |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2057 | ** The existence of a lock directory implies an EXCLUSIVE lock. All other |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2058 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2059 | */ |
| 2060 | |
| 2061 | /* |
| 2062 | ** 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] | 2063 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2064 | */ |
| 2065 | #define DOTLOCK_SUFFIX ".lock" |
| 2066 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2067 | /* |
| 2068 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2069 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2070 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2071 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2072 | ** |
| 2073 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 2074 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 2075 | ** is held on the file and false if the file is unlocked. |
| 2076 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2077 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2078 | int rc = SQLITE_OK; |
| 2079 | int reserved = 0; |
| 2080 | unixFile *pFile = (unixFile*)id; |
| 2081 | |
| 2082 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2083 | |
| 2084 | assert( pFile ); |
| 2085 | |
| 2086 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2087 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2088 | /* Either this connection or some other connection in the same process |
| 2089 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2090 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2091 | }else{ |
| 2092 | /* The lock is held if and only if the lockfile exists */ |
| 2093 | const char *zLockFile = (const char*)pFile->lockingContext; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 2094 | reserved = osAccess(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2095 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2096 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2097 | *pResOut = reserved; |
| 2098 | return rc; |
| 2099 | } |
| 2100 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2101 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2102 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2103 | ** of the following: |
| 2104 | ** |
| 2105 | ** (1) SHARED_LOCK |
| 2106 | ** (2) RESERVED_LOCK |
| 2107 | ** (3) PENDING_LOCK |
| 2108 | ** (4) EXCLUSIVE_LOCK |
| 2109 | ** |
| 2110 | ** Sometimes when requesting one lock state, additional lock states |
| 2111 | ** are inserted in between. The locking might fail on one of the later |
| 2112 | ** transitions leaving the lock state different from what it started but |
| 2113 | ** still short of its goal. The following chart shows the allowed |
| 2114 | ** transitions and the inserted intermediate states: |
| 2115 | ** |
| 2116 | ** UNLOCKED -> SHARED |
| 2117 | ** SHARED -> RESERVED |
| 2118 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2119 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2120 | ** PENDING -> EXCLUSIVE |
| 2121 | ** |
| 2122 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2123 | ** routine to lower a locking level. |
| 2124 | ** |
| 2125 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 2126 | ** But we track the other locking levels internally. |
| 2127 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2128 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2129 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2130 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2131 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2132 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2133 | |
| 2134 | /* If we have any lock, then the lock file already exists. All we have |
| 2135 | ** to do is adjust our internal record of the lock level. |
| 2136 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2137 | if( pFile->eFileLock > NO_LOCK ){ |
| 2138 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2139 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 2140 | #ifdef HAVE_UTIME |
| 2141 | utime(zLockFile, NULL); |
| 2142 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2143 | utimes(zLockFile, NULL); |
| 2144 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2145 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2149 | rc = osMkdir(zLockFile, 0777); |
| 2150 | if( rc<0 ){ |
| 2151 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2152 | int tErrno = errno; |
| 2153 | if( EEXIST == tErrno ){ |
| 2154 | rc = SQLITE_BUSY; |
| 2155 | } else { |
| 2156 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2157 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2158 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2159 | } |
| 2160 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2161 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2162 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2163 | |
| 2164 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2165 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2166 | return rc; |
| 2167 | } |
| 2168 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2169 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2170 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2171 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2172 | ** |
| 2173 | ** If the locking level of the file descriptor is already at or below |
| 2174 | ** the requested locking level, this routine is a no-op. |
| 2175 | ** |
| 2176 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2177 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2178 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2179 | unixFile *pFile = (unixFile*)id; |
| 2180 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2181 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2182 | |
| 2183 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2184 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2185 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2186 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2187 | |
| 2188 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2189 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2190 | return SQLITE_OK; |
| 2191 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2192 | |
| 2193 | /* To downgrade to shared, simply update our internal notion of the |
| 2194 | ** lock state. No need to mess with the file on disk. |
| 2195 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2196 | if( eFileLock==SHARED_LOCK ){ |
| 2197 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2198 | return SQLITE_OK; |
| 2199 | } |
| 2200 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2201 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2202 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2203 | rc = osRmdir(zLockFile); |
| 2204 | if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile); |
| 2205 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2206 | int tErrno = errno; |
drh | 13e0ea9 | 2011-12-11 02:29:25 +0000 | [diff] [blame] | 2207 | rc = 0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2208 | if( ENOENT != tErrno ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2209 | rc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2210 | } |
| 2211 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2212 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2213 | } |
| 2214 | return rc; |
| 2215 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2216 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2217 | return SQLITE_OK; |
| 2218 | } |
| 2219 | |
| 2220 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2221 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2222 | */ |
| 2223 | static int dotlockClose(sqlite3_file *id) { |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2224 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2225 | if( id ){ |
| 2226 | unixFile *pFile = (unixFile*)id; |
| 2227 | dotlockUnlock(id, NO_LOCK); |
| 2228 | sqlite3_free(pFile->lockingContext); |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2229 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2230 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2231 | return rc; |
| 2232 | } |
| 2233 | /****************** End of the dot-file lock implementation ******************* |
| 2234 | ******************************************************************************/ |
| 2235 | |
| 2236 | /****************************************************************************** |
| 2237 | ************************** Begin flock Locking ******************************** |
| 2238 | ** |
| 2239 | ** Use the flock() system call to do file locking. |
| 2240 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2241 | ** flock() locking is like dot-file locking in that the various |
| 2242 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2243 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2244 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2245 | ** still works when you do this, but concurrency is reduced since |
| 2246 | ** only a single process can be reading the database at a time. |
| 2247 | ** |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2248 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2249 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2250 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2251 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2252 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2253 | ** Retry flock() calls that fail with EINTR |
| 2254 | */ |
| 2255 | #ifdef EINTR |
| 2256 | static int robust_flock(int fd, int op){ |
| 2257 | int rc; |
| 2258 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2259 | return rc; |
| 2260 | } |
| 2261 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2262 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2263 | #endif |
| 2264 | |
| 2265 | |
| 2266 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2267 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2268 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2269 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2270 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2271 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2272 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2273 | int rc = SQLITE_OK; |
| 2274 | int reserved = 0; |
| 2275 | unixFile *pFile = (unixFile*)id; |
| 2276 | |
| 2277 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2278 | |
| 2279 | assert( pFile ); |
| 2280 | |
| 2281 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2282 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2283 | reserved = 1; |
| 2284 | } |
| 2285 | |
| 2286 | /* Otherwise see if some other process holds it. */ |
| 2287 | if( !reserved ){ |
| 2288 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2289 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2290 | if( !lrc ){ |
| 2291 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2292 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2293 | if ( lrc ) { |
| 2294 | int tErrno = errno; |
| 2295 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2296 | lrc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2297 | if( IS_LOCK_ERROR(lrc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2298 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2299 | rc = lrc; |
| 2300 | } |
| 2301 | } |
| 2302 | } else { |
| 2303 | int tErrno = errno; |
| 2304 | reserved = 1; |
| 2305 | /* someone else might have it reserved */ |
| 2306 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2307 | if( IS_LOCK_ERROR(lrc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2308 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2309 | rc = lrc; |
| 2310 | } |
| 2311 | } |
| 2312 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2313 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2314 | |
| 2315 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2316 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2317 | rc = SQLITE_OK; |
| 2318 | reserved=1; |
| 2319 | } |
| 2320 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2321 | *pResOut = reserved; |
| 2322 | return rc; |
| 2323 | } |
| 2324 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2325 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2326 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2327 | ** of the following: |
| 2328 | ** |
| 2329 | ** (1) SHARED_LOCK |
| 2330 | ** (2) RESERVED_LOCK |
| 2331 | ** (3) PENDING_LOCK |
| 2332 | ** (4) EXCLUSIVE_LOCK |
| 2333 | ** |
| 2334 | ** Sometimes when requesting one lock state, additional lock states |
| 2335 | ** are inserted in between. The locking might fail on one of the later |
| 2336 | ** transitions leaving the lock state different from what it started but |
| 2337 | ** still short of its goal. The following chart shows the allowed |
| 2338 | ** transitions and the inserted intermediate states: |
| 2339 | ** |
| 2340 | ** UNLOCKED -> SHARED |
| 2341 | ** SHARED -> RESERVED |
| 2342 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2343 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2344 | ** PENDING -> EXCLUSIVE |
| 2345 | ** |
| 2346 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2347 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2348 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2349 | ** access the file. |
| 2350 | ** |
| 2351 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2352 | ** routine to lower a locking level. |
| 2353 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2354 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2355 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2356 | unixFile *pFile = (unixFile*)id; |
| 2357 | |
| 2358 | assert( pFile ); |
| 2359 | |
| 2360 | /* if we already have a lock, it is exclusive. |
| 2361 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2362 | if (pFile->eFileLock > NO_LOCK) { |
| 2363 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2364 | return SQLITE_OK; |
| 2365 | } |
| 2366 | |
| 2367 | /* grab an exclusive lock */ |
| 2368 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2369 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2370 | int tErrno = errno; |
| 2371 | /* didn't get, must be busy */ |
| 2372 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2373 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2374 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } else { |
| 2377 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2378 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2379 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2380 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2381 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2382 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2383 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2384 | rc = SQLITE_BUSY; |
| 2385 | } |
| 2386 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2387 | return rc; |
| 2388 | } |
| 2389 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2390 | |
| 2391 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2392 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2393 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2394 | ** |
| 2395 | ** If the locking level of the file descriptor is already at or below |
| 2396 | ** the requested locking level, this routine is a no-op. |
| 2397 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2398 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2399 | unixFile *pFile = (unixFile*)id; |
| 2400 | |
| 2401 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2402 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2403 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2404 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2405 | |
| 2406 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2407 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2408 | return SQLITE_OK; |
| 2409 | } |
| 2410 | |
| 2411 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2412 | if (eFileLock==SHARED_LOCK) { |
| 2413 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2414 | return SQLITE_OK; |
| 2415 | } |
| 2416 | |
| 2417 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2418 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2419 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2420 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2421 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2422 | return SQLITE_IOERR_UNLOCK; |
| 2423 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2424 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2425 | return SQLITE_OK; |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | /* |
| 2430 | ** Close a file. |
| 2431 | */ |
| 2432 | static int flockClose(sqlite3_file *id) { |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2433 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2434 | if( id ){ |
| 2435 | flockUnlock(id, NO_LOCK); |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2436 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2437 | } |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2438 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2442 | |
| 2443 | /******************* End of the flock lock implementation ********************* |
| 2444 | ******************************************************************************/ |
| 2445 | |
| 2446 | /****************************************************************************** |
| 2447 | ************************ Begin Named Semaphore Locking ************************ |
| 2448 | ** |
| 2449 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2450 | ** |
| 2451 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2452 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2453 | ** the database file at a time. This reduces potential concurrency, but |
| 2454 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2455 | */ |
| 2456 | #if OS_VXWORKS |
| 2457 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2458 | /* |
| 2459 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2460 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2461 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2462 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2463 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2464 | static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2465 | int rc = SQLITE_OK; |
| 2466 | int reserved = 0; |
| 2467 | unixFile *pFile = (unixFile*)id; |
| 2468 | |
| 2469 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2470 | |
| 2471 | assert( pFile ); |
| 2472 | |
| 2473 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2474 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2475 | reserved = 1; |
| 2476 | } |
| 2477 | |
| 2478 | /* Otherwise see if some other process holds it. */ |
| 2479 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2480 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2481 | |
| 2482 | if( sem_trywait(pSem)==-1 ){ |
| 2483 | int tErrno = errno; |
| 2484 | if( EAGAIN != tErrno ){ |
| 2485 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2486 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2487 | } else { |
| 2488 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2489 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2490 | } |
| 2491 | }else{ |
| 2492 | /* we could have it if we want it */ |
| 2493 | sem_post(pSem); |
| 2494 | } |
| 2495 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2496 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2497 | |
| 2498 | *pResOut = reserved; |
| 2499 | return rc; |
| 2500 | } |
| 2501 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2502 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2503 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2504 | ** of the following: |
| 2505 | ** |
| 2506 | ** (1) SHARED_LOCK |
| 2507 | ** (2) RESERVED_LOCK |
| 2508 | ** (3) PENDING_LOCK |
| 2509 | ** (4) EXCLUSIVE_LOCK |
| 2510 | ** |
| 2511 | ** Sometimes when requesting one lock state, additional lock states |
| 2512 | ** are inserted in between. The locking might fail on one of the later |
| 2513 | ** transitions leaving the lock state different from what it started but |
| 2514 | ** still short of its goal. The following chart shows the allowed |
| 2515 | ** transitions and the inserted intermediate states: |
| 2516 | ** |
| 2517 | ** UNLOCKED -> SHARED |
| 2518 | ** SHARED -> RESERVED |
| 2519 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2520 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2521 | ** PENDING -> EXCLUSIVE |
| 2522 | ** |
| 2523 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2524 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2525 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2526 | ** access the file. |
| 2527 | ** |
| 2528 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2529 | ** routine to lower a locking level. |
| 2530 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2531 | static int semXLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2532 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2533 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2534 | int rc = SQLITE_OK; |
| 2535 | |
| 2536 | /* if we already have a lock, it is exclusive. |
| 2537 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2538 | if (pFile->eFileLock > NO_LOCK) { |
| 2539 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2540 | rc = SQLITE_OK; |
| 2541 | goto sem_end_lock; |
| 2542 | } |
| 2543 | |
| 2544 | /* lock semaphore now but bail out when already locked. */ |
| 2545 | if( sem_trywait(pSem)==-1 ){ |
| 2546 | rc = SQLITE_BUSY; |
| 2547 | goto sem_end_lock; |
| 2548 | } |
| 2549 | |
| 2550 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2551 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2552 | |
| 2553 | sem_end_lock: |
| 2554 | return rc; |
| 2555 | } |
| 2556 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2557 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2558 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2559 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2560 | ** |
| 2561 | ** If the locking level of the file descriptor is already at or below |
| 2562 | ** the requested locking level, this routine is a no-op. |
| 2563 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2564 | static int semXUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2565 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2566 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2567 | |
| 2568 | assert( pFile ); |
| 2569 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2570 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2571 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2572 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2573 | |
| 2574 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2575 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2576 | return SQLITE_OK; |
| 2577 | } |
| 2578 | |
| 2579 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2580 | if (eFileLock==SHARED_LOCK) { |
| 2581 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2582 | return SQLITE_OK; |
| 2583 | } |
| 2584 | |
| 2585 | /* no, really unlock. */ |
| 2586 | if ( sem_post(pSem)==-1 ) { |
| 2587 | int rc, tErrno = errno; |
| 2588 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2589 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2590 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2591 | } |
| 2592 | return rc; |
| 2593 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2594 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2595 | return SQLITE_OK; |
| 2596 | } |
| 2597 | |
| 2598 | /* |
| 2599 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2600 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2601 | static int semXClose(sqlite3_file *id) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2602 | if( id ){ |
| 2603 | unixFile *pFile = (unixFile*)id; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2604 | semXUnlock(id, NO_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2605 | assert( pFile ); |
| 2606 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2607 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2608 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2609 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2610 | } |
| 2611 | return SQLITE_OK; |
| 2612 | } |
| 2613 | |
| 2614 | #endif /* OS_VXWORKS */ |
| 2615 | /* |
| 2616 | ** Named semaphore locking is only available on VxWorks. |
| 2617 | ** |
| 2618 | *************** End of the named semaphore lock implementation **************** |
| 2619 | ******************************************************************************/ |
| 2620 | |
| 2621 | |
| 2622 | /****************************************************************************** |
| 2623 | *************************** Begin AFP Locking ********************************* |
| 2624 | ** |
| 2625 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2626 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2627 | ** |
| 2628 | ** Third-party implementations of AFP are available. But this code here |
| 2629 | ** only works on OSX. |
| 2630 | */ |
| 2631 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2632 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2633 | /* |
| 2634 | ** The afpLockingContext structure contains all afp lock specific state |
| 2635 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2636 | typedef struct afpLockingContext afpLockingContext; |
| 2637 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2638 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2639 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2640 | }; |
| 2641 | |
| 2642 | struct ByteRangeLockPB2 |
| 2643 | { |
| 2644 | unsigned long long offset; /* offset to first byte to lock */ |
| 2645 | unsigned long long length; /* nbr of bytes to lock */ |
| 2646 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2647 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2648 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2649 | int fd; /* file desc to assoc this lock with */ |
| 2650 | }; |
| 2651 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2652 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2653 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2654 | /* |
| 2655 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2656 | ** AFP filesystem. |
| 2657 | ** |
| 2658 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2659 | */ |
| 2660 | static int afpSetLock( |
| 2661 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2662 | unixFile *pFile, /* Open file descriptor on path */ |
| 2663 | unsigned long long offset, /* First byte to be locked */ |
| 2664 | unsigned long long length, /* Number of bytes to lock */ |
| 2665 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2666 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2667 | struct ByteRangeLockPB2 pb; |
| 2668 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2669 | |
| 2670 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2671 | pb.startEndFlag = 0; |
| 2672 | pb.offset = offset; |
| 2673 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2674 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2675 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2676 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2677 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2678 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2679 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2680 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2681 | int rc; |
| 2682 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2683 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2684 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2685 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2686 | rc = SQLITE_BUSY; |
| 2687 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2688 | rc = sqliteErrorFromPosixError(tErrno, |
| 2689 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2690 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2691 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2692 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2693 | } |
| 2694 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2695 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2696 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2697 | } |
| 2698 | } |
| 2699 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2700 | /* |
| 2701 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2702 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2703 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2704 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2705 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2706 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2707 | int rc = SQLITE_OK; |
| 2708 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2709 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2710 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2711 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2712 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2713 | |
| 2714 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2715 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2716 | if( context->reserved ){ |
| 2717 | *pResOut = 1; |
| 2718 | return SQLITE_OK; |
| 2719 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2720 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2721 | |
| 2722 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2723 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2724 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | /* Otherwise see if some other process holds it. |
| 2728 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2729 | if( !reserved ){ |
| 2730 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2731 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2732 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2733 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2734 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2735 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2736 | } else { |
| 2737 | /* if we failed to get the lock then someone else must have it */ |
| 2738 | reserved = 1; |
| 2739 | } |
| 2740 | if( IS_LOCK_ERROR(lrc) ){ |
| 2741 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2742 | } |
| 2743 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2744 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2745 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2746 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2747 | |
| 2748 | *pResOut = reserved; |
| 2749 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2752 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2753 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2754 | ** of the following: |
| 2755 | ** |
| 2756 | ** (1) SHARED_LOCK |
| 2757 | ** (2) RESERVED_LOCK |
| 2758 | ** (3) PENDING_LOCK |
| 2759 | ** (4) EXCLUSIVE_LOCK |
| 2760 | ** |
| 2761 | ** Sometimes when requesting one lock state, additional lock states |
| 2762 | ** are inserted in between. The locking might fail on one of the later |
| 2763 | ** transitions leaving the lock state different from what it started but |
| 2764 | ** still short of its goal. The following chart shows the allowed |
| 2765 | ** transitions and the inserted intermediate states: |
| 2766 | ** |
| 2767 | ** UNLOCKED -> SHARED |
| 2768 | ** SHARED -> RESERVED |
| 2769 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2770 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2771 | ** PENDING -> EXCLUSIVE |
| 2772 | ** |
| 2773 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2774 | ** routine to lower a locking level. |
| 2775 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2776 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2777 | int rc = SQLITE_OK; |
| 2778 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2779 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2780 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2781 | |
| 2782 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2783 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2784 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2785 | azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2786 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2787 | /* 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] | 2788 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2789 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2790 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2791 | if( pFile->eFileLock>=eFileLock ){ |
| 2792 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2793 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2794 | return SQLITE_OK; |
| 2795 | } |
| 2796 | |
| 2797 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2798 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2799 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2800 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2801 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2802 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2803 | assert( eFileLock!=PENDING_LOCK ); |
| 2804 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2805 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2806 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2807 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2808 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2809 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2810 | |
| 2811 | /* If some thread using this PID has a lock via a different unixFile* |
| 2812 | ** handle that precludes the requested lock, return BUSY. |
| 2813 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2814 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2815 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2816 | ){ |
| 2817 | rc = SQLITE_BUSY; |
| 2818 | goto afp_end_lock; |
| 2819 | } |
| 2820 | |
| 2821 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2822 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2823 | ** return SQLITE_OK. |
| 2824 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2825 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2826 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2827 | assert( eFileLock==SHARED_LOCK ); |
| 2828 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2829 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2830 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2831 | pInode->nShared++; |
| 2832 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2833 | goto afp_end_lock; |
| 2834 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2835 | |
| 2836 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2837 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2838 | ** be released. |
| 2839 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2840 | if( eFileLock==SHARED_LOCK |
| 2841 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2842 | ){ |
| 2843 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2844 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2845 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2846 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2847 | goto afp_end_lock; |
| 2848 | } |
| 2849 | } |
| 2850 | |
| 2851 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2852 | ** operating system calls for the specified lock. |
| 2853 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2854 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2855 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2856 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2857 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2858 | assert( pInode->nShared==0 ); |
| 2859 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2860 | |
| 2861 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2862 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2863 | /* note that the quality of the randomness doesn't matter that much */ |
| 2864 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2865 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2866 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2867 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2868 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2869 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2870 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2871 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2872 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2873 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2874 | if( IS_LOCK_ERROR(lrc1) ) { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2875 | storeLastErrno(pFile, lrc1Errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2876 | rc = lrc1; |
| 2877 | goto afp_end_lock; |
| 2878 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2879 | rc = lrc2; |
| 2880 | goto afp_end_lock; |
| 2881 | } else if( lrc1 != SQLITE_OK ) { |
| 2882 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2883 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2884 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2885 | pInode->nLock++; |
| 2886 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2887 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2888 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2889 | /* We are trying for an exclusive lock but another thread in this |
| 2890 | ** same process is still holding a shared lock. */ |
| 2891 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2892 | }else{ |
| 2893 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2894 | ** assumed that there is a SHARED or greater lock on the file |
| 2895 | ** already. |
| 2896 | */ |
| 2897 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2898 | assert( 0!=pFile->eFileLock ); |
| 2899 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2900 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2901 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2902 | if( !failed ){ |
| 2903 | context->reserved = 1; |
| 2904 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2905 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2906 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2907 | /* Acquire an EXCLUSIVE lock */ |
| 2908 | |
| 2909 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2910 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2911 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2912 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2913 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2914 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2915 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2916 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2917 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2918 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2919 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2920 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2921 | ** a critical I/O error |
| 2922 | */ |
| 2923 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2924 | SQLITE_IOERR_LOCK; |
| 2925 | goto afp_end_lock; |
| 2926 | } |
| 2927 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2928 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2929 | } |
| 2930 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2931 | if( failed ){ |
| 2932 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2937 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2938 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2939 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2940 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2941 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2945 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2946 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2947 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2948 | return rc; |
| 2949 | } |
| 2950 | |
| 2951 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2952 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2953 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2954 | ** |
| 2955 | ** If the locking level of the file descriptor is already at or below |
| 2956 | ** the requested locking level, this routine is a no-op. |
| 2957 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2958 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2959 | int rc = SQLITE_OK; |
| 2960 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2961 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2962 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2963 | int skipShared = 0; |
| 2964 | #ifdef SQLITE_TEST |
| 2965 | int h = pFile->h; |
| 2966 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2967 | |
| 2968 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2969 | 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] | 2970 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2971 | osGetpid(0))); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2972 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2973 | assert( eFileLock<=SHARED_LOCK ); |
| 2974 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2975 | return SQLITE_OK; |
| 2976 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2977 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2978 | pInode = pFile->pInode; |
| 2979 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2980 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2981 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2982 | SimulateIOErrorBenign(1); |
| 2983 | SimulateIOError( h=(-1) ) |
| 2984 | SimulateIOErrorBenign(0); |
| 2985 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 2986 | #ifdef SQLITE_DEBUG |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2987 | /* When reducing a lock such that other processes can start |
| 2988 | ** reading the database file again, make sure that the |
| 2989 | ** transaction counter was updated if any part of the database |
| 2990 | ** file changed. If the transaction counter is not updated, |
| 2991 | ** other connections to the same file might not realize that |
| 2992 | ** the file has changed and hence might not know to flush their |
| 2993 | ** cache. The use of a stale cache can lead to database corruption. |
| 2994 | */ |
| 2995 | assert( pFile->inNormalWrite==0 |
| 2996 | || pFile->dbUpdate==0 |
| 2997 | || pFile->transCntrChng==1 ); |
| 2998 | pFile->inNormalWrite = 0; |
| 2999 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3000 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3001 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3002 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3003 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3004 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3005 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3006 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 3007 | } else { |
| 3008 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3009 | } |
| 3010 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3011 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3012 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3013 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3014 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3015 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 3016 | if( !rc ){ |
| 3017 | context->reserved = 0; |
| 3018 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3019 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3020 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 3021 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3022 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3023 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3024 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3025 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3026 | /* Decrement the shared lock counter. Release the lock using an |
| 3027 | ** OS call only when all threads in this same process have released |
| 3028 | ** the lock. |
| 3029 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3030 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 3031 | pInode->nShared--; |
| 3032 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3033 | SimulateIOErrorBenign(1); |
| 3034 | SimulateIOError( h=(-1) ) |
| 3035 | SimulateIOErrorBenign(0); |
| 3036 | if( !skipShared ){ |
| 3037 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 3038 | } |
| 3039 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3040 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3041 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3042 | } |
| 3043 | } |
| 3044 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3045 | pInode->nLock--; |
| 3046 | assert( pInode->nLock>=0 ); |
| 3047 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3048 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3049 | } |
| 3050 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3051 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3052 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3053 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3054 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3055 | return rc; |
| 3056 | } |
| 3057 | |
| 3058 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3059 | ** Close a file & cleanup AFP specific locking context |
| 3060 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3061 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3062 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3063 | if( id ){ |
| 3064 | unixFile *pFile = (unixFile*)id; |
| 3065 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3066 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3067 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3068 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3069 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3070 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3071 | ** the last lock is cleared. |
| 3072 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3073 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3074 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 3075 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3076 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3077 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3078 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3079 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3080 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3083 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3084 | /* |
| 3085 | ** The code above is the AFP lock implementation. The code is specific |
| 3086 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3087 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 3088 | ** VFS is not available. |
| 3089 | ** |
| 3090 | ********************* End of the AFP lock implementation ********************** |
| 3091 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3092 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3093 | /****************************************************************************** |
| 3094 | *************************** Begin NFS Locking ********************************/ |
| 3095 | |
| 3096 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3097 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3098 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3099 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3100 | ** |
| 3101 | ** If the locking level of the file descriptor is already at or below |
| 3102 | ** the requested locking level, this routine is a no-op. |
| 3103 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3104 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 3105 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3109 | /* |
| 3110 | ** The code above is the NFS lock implementation. The code is specific |
| 3111 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3112 | ** is available. |
| 3113 | ** |
| 3114 | ********************* End of the NFS lock implementation ********************** |
| 3115 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3116 | |
| 3117 | /****************************************************************************** |
| 3118 | **************** Non-locking sqlite3_file methods ***************************** |
| 3119 | ** |
| 3120 | ** The next division contains implementations for all methods of the |
| 3121 | ** sqlite3_file object other than the locking methods. The locking |
| 3122 | ** methods were defined in divisions above (one locking method per |
| 3123 | ** division). Those methods that are common to all locking modes |
| 3124 | ** are gather together into this division. |
| 3125 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3126 | |
| 3127 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3128 | ** Seek to the offset passed as the second argument, then read cnt |
| 3129 | ** bytes into pBuf. Return the number of bytes actually read. |
| 3130 | ** |
| 3131 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 3132 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 3133 | ** one system to another. Since SQLite does not define USE_PREAD |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3134 | ** in any form by default, we will not attempt to define _XOPEN_SOURCE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3135 | ** See tickets #2741 and #2681. |
| 3136 | ** |
| 3137 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 3138 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3139 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3140 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 3141 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3142 | int prior = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3143 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3144 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3145 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3146 | TIMER_START; |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3147 | assert( cnt==(cnt&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3148 | assert( id->h>2 ); |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3149 | cnt &= 0x1ffff; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3150 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3151 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3152 | got = osPread(id->h, pBuf, cnt, offset); |
| 3153 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3154 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3155 | got = osPread64(id->h, pBuf, cnt, offset); |
| 3156 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3157 | #else |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3158 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3159 | SimulateIOError( newOffset-- ); |
| 3160 | if( newOffset!=offset ){ |
| 3161 | if( newOffset == -1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3162 | storeLastErrno((unixFile*)id, errno); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3163 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3164 | storeLastErrno((unixFile*)id, 0); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3165 | } |
| 3166 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3167 | } |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3168 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3169 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3170 | if( got==cnt ) break; |
| 3171 | if( got<0 ){ |
| 3172 | if( errno==EINTR ){ got = 1; continue; } |
| 3173 | prior = 0; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3174 | storeLastErrno((unixFile*)id, errno); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3175 | break; |
| 3176 | }else if( got>0 ){ |
| 3177 | cnt -= got; |
| 3178 | offset += got; |
| 3179 | prior += got; |
| 3180 | pBuf = (void*)(got + (char*)pBuf); |
| 3181 | } |
| 3182 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3183 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3184 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3185 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3186 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3190 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3191 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3192 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3193 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3194 | static int unixRead( |
| 3195 | sqlite3_file *id, |
| 3196 | void *pBuf, |
| 3197 | int amt, |
| 3198 | sqlite3_int64 offset |
| 3199 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3200 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3201 | int got; |
| 3202 | assert( id ); |
drh | 6cf9d8d | 2013-05-09 18:12:40 +0000 | [diff] [blame] | 3203 | assert( offset>=0 ); |
| 3204 | assert( amt>0 ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3205 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3206 | /* If this is a database file (not a journal, master-journal or temp |
| 3207 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3208 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3209 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3210 | || offset>=PENDING_BYTE+512 |
| 3211 | || offset+amt<=PENDING_BYTE |
| 3212 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3213 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3214 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3215 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 6c56963 | 2013-03-26 18:48:11 +0000 | [diff] [blame] | 3216 | /* Deal with as much of this read request as possible by transfering |
| 3217 | ** data from the memory mapping using memcpy(). */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3218 | if( offset<pFile->mmapSize ){ |
| 3219 | if( offset+amt <= pFile->mmapSize ){ |
| 3220 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 3221 | return SQLITE_OK; |
| 3222 | }else{ |
| 3223 | int nCopy = pFile->mmapSize - offset; |
| 3224 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 3225 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3226 | amt -= nCopy; |
| 3227 | offset += nCopy; |
| 3228 | } |
| 3229 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3230 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3231 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3232 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3233 | if( got==amt ){ |
| 3234 | return SQLITE_OK; |
| 3235 | }else if( got<0 ){ |
| 3236 | /* lastErrno set by seekAndRead */ |
| 3237 | return SQLITE_IOERR_READ; |
| 3238 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3239 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3240 | /* Unread parts of the buffer must be zero-filled */ |
| 3241 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3242 | return SQLITE_IOERR_SHORT_READ; |
| 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | /* |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3247 | ** Attempt to seek the file-descriptor passed as the first argument to |
| 3248 | ** absolute offset iOff, then attempt to write nBuf bytes of data from |
| 3249 | ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, |
| 3250 | ** return the actual number of bytes written (which may be less than |
| 3251 | ** nBuf). |
| 3252 | */ |
| 3253 | static int seekAndWriteFd( |
| 3254 | int fd, /* File descriptor to write to */ |
| 3255 | i64 iOff, /* File offset to begin writing at */ |
| 3256 | const void *pBuf, /* Copy data from this buffer to the file */ |
| 3257 | int nBuf, /* Size of buffer pBuf in bytes */ |
| 3258 | int *piErrno /* OUT: Error number if error occurs */ |
| 3259 | ){ |
| 3260 | int rc = 0; /* Value returned by system call */ |
| 3261 | |
| 3262 | assert( nBuf==(nBuf&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3263 | assert( fd>2 ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3264 | nBuf &= 0x1ffff; |
| 3265 | TIMER_START; |
| 3266 | |
| 3267 | #if defined(USE_PREAD) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3268 | do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3269 | #elif defined(USE_PREAD64) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3270 | do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3271 | #else |
| 3272 | do{ |
| 3273 | i64 iSeek = lseek(fd, iOff, SEEK_SET); |
| 3274 | SimulateIOError( iSeek-- ); |
| 3275 | |
| 3276 | if( iSeek!=iOff ){ |
| 3277 | if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0); |
| 3278 | return -1; |
| 3279 | } |
| 3280 | rc = osWrite(fd, pBuf, nBuf); |
| 3281 | }while( rc<0 && errno==EINTR ); |
| 3282 | #endif |
| 3283 | |
| 3284 | TIMER_END; |
| 3285 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); |
| 3286 | |
| 3287 | if( rc<0 && piErrno ) *piErrno = errno; |
| 3288 | return rc; |
| 3289 | } |
| 3290 | |
| 3291 | |
| 3292 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3293 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3294 | ** Return the number of bytes actually read. Update the offset. |
| 3295 | ** |
| 3296 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3297 | ** is set before returning. |
| 3298 | */ |
| 3299 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3300 | return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | |
| 3304 | /* |
| 3305 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3306 | ** or some other error code on failure. |
| 3307 | */ |
| 3308 | static int unixWrite( |
| 3309 | sqlite3_file *id, |
| 3310 | const void *pBuf, |
| 3311 | int amt, |
| 3312 | sqlite3_int64 offset |
| 3313 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3314 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3315 | int wrote = 0; |
| 3316 | assert( id ); |
| 3317 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3318 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3319 | /* If this is a database file (not a journal, master-journal or temp |
| 3320 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3321 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3322 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3323 | || offset>=PENDING_BYTE+512 |
| 3324 | || offset+amt<=PENDING_BYTE |
| 3325 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3326 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3327 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3328 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3329 | /* If we are doing a normal write to a database file (as opposed to |
| 3330 | ** doing a hot-journal rollback or a write to some file other than a |
| 3331 | ** normal database file) then record the fact that the database |
| 3332 | ** has changed. If the transaction counter is modified, record that |
| 3333 | ** fact too. |
| 3334 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3335 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3336 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3337 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3338 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3339 | char oldCntr[4]; |
| 3340 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3341 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3342 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3343 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3344 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3345 | } |
| 3346 | } |
| 3347 | } |
| 3348 | #endif |
| 3349 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3350 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3351 | /* Deal with as much of this write request as possible by transfering |
| 3352 | ** data from the memory mapping using memcpy(). */ |
| 3353 | if( offset<pFile->mmapSize ){ |
| 3354 | if( offset+amt <= pFile->mmapSize ){ |
| 3355 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 3356 | return SQLITE_OK; |
| 3357 | }else{ |
| 3358 | int nCopy = pFile->mmapSize - offset; |
| 3359 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3360 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3361 | amt -= nCopy; |
| 3362 | offset += nCopy; |
| 3363 | } |
| 3364 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3365 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3366 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3367 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3368 | amt -= wrote; |
| 3369 | offset += wrote; |
| 3370 | pBuf = &((char*)pBuf)[wrote]; |
| 3371 | } |
| 3372 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3373 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3374 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3375 | if( amt>0 ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3376 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3377 | /* lastErrno set by seekAndWrite */ |
| 3378 | return SQLITE_IOERR_WRITE; |
| 3379 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3380 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3381 | return SQLITE_FULL; |
| 3382 | } |
| 3383 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3384 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3385 | return SQLITE_OK; |
| 3386 | } |
| 3387 | |
| 3388 | #ifdef SQLITE_TEST |
| 3389 | /* |
| 3390 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3391 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3392 | */ |
| 3393 | int sqlite3_sync_count = 0; |
| 3394 | int sqlite3_fullsync_count = 0; |
| 3395 | #endif |
| 3396 | |
| 3397 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3398 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3399 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3400 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3401 | ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3402 | */ |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3403 | #if !defined(fdatasync) && !HAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3404 | # define fdatasync fsync |
| 3405 | #endif |
| 3406 | |
| 3407 | /* |
| 3408 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3409 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3410 | ** only available on Mac OS X. But that could change. |
| 3411 | */ |
| 3412 | #ifdef F_FULLFSYNC |
| 3413 | # define HAVE_FULLFSYNC 1 |
| 3414 | #else |
| 3415 | # define HAVE_FULLFSYNC 0 |
| 3416 | #endif |
| 3417 | |
| 3418 | |
| 3419 | /* |
| 3420 | ** The fsync() system call does not work as advertised on many |
| 3421 | ** unix systems. The following procedure is an attempt to make |
| 3422 | ** it work better. |
| 3423 | ** |
| 3424 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3425 | ** for testing when we want to run through the test suite quickly. |
| 3426 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3427 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3428 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3429 | ** |
| 3430 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3431 | ** The idea behind dataOnly is that it should only write the file content |
| 3432 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3433 | ** unchanged since the file size is part of the inode. However, |
| 3434 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3435 | ** file size has changed. The only real difference between fdatasync() |
| 3436 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3437 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3438 | ** We only care about the file size, not the other file attributes, so |
| 3439 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3440 | ** So, we always use fdatasync() if it is available, regardless of |
| 3441 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3442 | */ |
| 3443 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3444 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3445 | |
| 3446 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3447 | ** the one below. It is replicated here solely to avoid cluttering |
| 3448 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3449 | */ |
| 3450 | #ifdef SQLITE_NO_SYNC |
| 3451 | UNUSED_PARAMETER(fd); |
| 3452 | UNUSED_PARAMETER(fullSync); |
| 3453 | UNUSED_PARAMETER(dataOnly); |
| 3454 | #elif HAVE_FULLFSYNC |
| 3455 | UNUSED_PARAMETER(dataOnly); |
| 3456 | #else |
| 3457 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3458 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3459 | #endif |
| 3460 | |
| 3461 | /* Record the number of times that we do a normal fsync() and |
| 3462 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3463 | ** gets called with the correct arguments. |
| 3464 | */ |
| 3465 | #ifdef SQLITE_TEST |
| 3466 | if( fullSync ) sqlite3_fullsync_count++; |
| 3467 | sqlite3_sync_count++; |
| 3468 | #endif |
| 3469 | |
| 3470 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 3471 | ** no-op |
| 3472 | */ |
| 3473 | #ifdef SQLITE_NO_SYNC |
| 3474 | rc = SQLITE_OK; |
| 3475 | #elif HAVE_FULLFSYNC |
| 3476 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3477 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3478 | }else{ |
| 3479 | rc = 1; |
| 3480 | } |
| 3481 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3482 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3483 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3484 | ** isn't supported for this file system. So, attempt an fsync |
| 3485 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3486 | ** It'd be better to detect fullfsync support once and avoid |
| 3487 | ** the fcntl call every time sync is called. |
| 3488 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3489 | if( rc ) rc = fsync(fd); |
| 3490 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3491 | #elif defined(__APPLE__) |
| 3492 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3493 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3494 | */ |
| 3495 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3496 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3497 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3498 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3499 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3500 | rc = fsync(fd); |
| 3501 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3502 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3503 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3504 | |
| 3505 | if( OS_VXWORKS && rc!= -1 ){ |
| 3506 | rc = 0; |
| 3507 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3508 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3511 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3512 | ** Open a file descriptor to the directory containing file zFilename. |
| 3513 | ** If successful, *pFd is set to the opened file descriptor and |
| 3514 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3515 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3516 | ** value. |
| 3517 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3518 | ** The directory file descriptor is used for only one thing - to |
| 3519 | ** fsync() a directory to make sure file creation and deletion events |
| 3520 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3521 | ** journaling filesystems, but are required on older filesystems. |
| 3522 | ** |
| 3523 | ** This routine can be overridden using the xSetSysCall interface. |
| 3524 | ** The ability to override this routine was added in support of the |
| 3525 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3526 | ** told) so making it overrideable allows the chromium sandbox to |
| 3527 | ** replace this routine with a harmless no-op. To make this routine |
| 3528 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3529 | ** *pFd set to a negative number. |
| 3530 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3531 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3532 | ** the file descriptor *pFd using close(). |
| 3533 | */ |
| 3534 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3535 | int ii; |
| 3536 | int fd = -1; |
| 3537 | char zDirname[MAX_PATHNAME+1]; |
| 3538 | |
| 3539 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
| 3540 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
| 3541 | if( ii>0 ){ |
| 3542 | zDirname[ii] = '\0'; |
| 3543 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3544 | if( fd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3545 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
| 3546 | } |
| 3547 | } |
| 3548 | *pFd = fd; |
| 3549 | return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); |
| 3550 | } |
| 3551 | |
| 3552 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3553 | ** Make sure all writes to a particular file are committed to disk. |
| 3554 | ** |
| 3555 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3556 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3557 | ** file data is synced. |
| 3558 | ** |
| 3559 | ** Under Unix, also make sure that the directory entry for the file |
| 3560 | ** has been created by fsync-ing the directory that contains the file. |
| 3561 | ** If we do not do this and we encounter a power failure, the directory |
| 3562 | ** entry for the journal might not exist after we reboot. The next |
| 3563 | ** SQLite to access the file will not know that the journal exists (because |
| 3564 | ** the directory entry for the journal was never created) and the transaction |
| 3565 | ** will not roll back - possibly leading to database corruption. |
| 3566 | */ |
| 3567 | static int unixSync(sqlite3_file *id, int flags){ |
| 3568 | int rc; |
| 3569 | unixFile *pFile = (unixFile*)id; |
| 3570 | |
| 3571 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3572 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3573 | |
| 3574 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3575 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3576 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3577 | ); |
| 3578 | |
| 3579 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3580 | ** line is to test that doing so does not cause any problems. |
| 3581 | */ |
| 3582 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3583 | |
| 3584 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3585 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3586 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3587 | SimulateIOError( rc=1 ); |
| 3588 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3589 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3590 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3591 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3592 | |
| 3593 | /* Also fsync the directory containing the file if the DIRSYNC flag |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 3594 | ** is set. This is a one-time occurrence. Many systems (examples: AIX) |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3595 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3596 | */ |
| 3597 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3598 | int dirfd; |
| 3599 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3600 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3601 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
| 3602 | if( rc==SQLITE_OK && dirfd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3603 | full_fsync(dirfd, 0, 0); |
| 3604 | robust_close(pFile, dirfd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3605 | }else if( rc==SQLITE_CANTOPEN ){ |
| 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 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3737 | iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3738 | assert( iWrite>=buf.st_size ); |
| 3739 | assert( (iWrite/nBlk)==((buf.st_size+nBlk-1)/nBlk) ); |
| 3740 | assert( ((iWrite+1)%nBlk)==0 ); |
| 3741 | for(/*no-op*/; iWrite<nSize; iWrite+=nBlk ){ |
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 | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3745 | if( nWrite==0 || (nSize%nBlk) ){ |
| 3746 | nWrite = seekAndWrite(pFile, nSize-1, "", 1); |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3747 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | d348c66 | 2014-12-30 14:40:53 +0000 | [diff] [blame] | 3748 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3749 | #endif |
| 3750 | } |
| 3751 | } |
| 3752 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3753 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3754 | if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3755 | int rc; |
| 3756 | if( pFile->szChunk<=0 ){ |
| 3757 | if( robust_ftruncate(pFile->h, nByte) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3758 | storeLastErrno(pFile, errno); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3759 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 3760 | } |
| 3761 | } |
| 3762 | |
| 3763 | rc = unixMapfile(pFile, nByte); |
| 3764 | return rc; |
| 3765 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3766 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3767 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3768 | return SQLITE_OK; |
| 3769 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3770 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3771 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3772 | ** If *pArg is initially negative then this is a query. Set *pArg to |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3773 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3774 | ** |
| 3775 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3776 | */ |
| 3777 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3778 | if( *pArg<0 ){ |
| 3779 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3780 | }else if( (*pArg)==0 ){ |
| 3781 | pFile->ctrlFlags &= ~mask; |
| 3782 | }else{ |
| 3783 | pFile->ctrlFlags |= mask; |
| 3784 | } |
| 3785 | } |
| 3786 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3787 | /* Forward declaration */ |
| 3788 | static int unixGetTempname(int nBuf, char *zBuf); |
| 3789 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3790 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3791 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3792 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3793 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3794 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3795 | switch( op ){ |
drh | c435cf7 | 2015-03-21 16:36:03 +0000 | [diff] [blame] | 3796 | case SQLITE_FCNTL_WAL_BLOCK: { |
drh | 62ca61e | 2015-04-03 20:33:33 +0000 | [diff] [blame] | 3797 | /* pFile->ctrlFlags |= UNIXFILE_BLOCK; // Deferred feature */ |
drh | c435cf7 | 2015-03-21 16:36:03 +0000 | [diff] [blame] | 3798 | return SQLITE_OK; |
| 3799 | } |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3800 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3801 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3802 | return SQLITE_OK; |
| 3803 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3804 | case SQLITE_FCNTL_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3805 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3806 | return SQLITE_OK; |
| 3807 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3808 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3809 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3810 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3811 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3812 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3813 | int rc; |
| 3814 | SimulateIOErrorBenign(1); |
| 3815 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3816 | SimulateIOErrorBenign(0); |
| 3817 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3818 | } |
| 3819 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3820 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3821 | return SQLITE_OK; |
| 3822 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3823 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3824 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3825 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3826 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3827 | case SQLITE_FCNTL_VFSNAME: { |
| 3828 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3829 | return SQLITE_OK; |
| 3830 | } |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3831 | case SQLITE_FCNTL_TEMPFILENAME: { |
| 3832 | char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname ); |
| 3833 | if( zTFile ){ |
| 3834 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 3835 | *(char**)pArg = zTFile; |
| 3836 | } |
| 3837 | return SQLITE_OK; |
| 3838 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 3839 | case SQLITE_FCNTL_HAS_MOVED: { |
| 3840 | *(int*)pArg = fileHasMoved(pFile); |
| 3841 | return SQLITE_OK; |
| 3842 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3843 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3844 | case SQLITE_FCNTL_MMAP_SIZE: { |
drh | 34f7490 | 2013-04-03 13:09:18 +0000 | [diff] [blame] | 3845 | i64 newLimit = *(i64*)pArg; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3846 | int rc = SQLITE_OK; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3847 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 3848 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 3849 | } |
| 3850 | *(i64*)pArg = pFile->mmapSizeMax; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3851 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3852 | pFile->mmapSizeMax = newLimit; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3853 | if( pFile->mmapSize>0 ){ |
| 3854 | unixUnmapfile(pFile); |
| 3855 | rc = unixMapfile(pFile, -1); |
| 3856 | } |
dan | bcb8a86 | 2013-04-08 15:30:41 +0000 | [diff] [blame] | 3857 | } |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3858 | return rc; |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 3859 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3860 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3861 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3862 | /* The pager calls this method to signal that it has done |
| 3863 | ** a rollback and that the database is therefore unchanged and |
| 3864 | ** it hence it is OK for the transaction change counter to be |
| 3865 | ** unchanged. |
| 3866 | */ |
| 3867 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3868 | ((unixFile*)id)->dbUpdate = 0; |
| 3869 | return SQLITE_OK; |
| 3870 | } |
| 3871 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3872 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3873 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: |
| 3874 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3875 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3876 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3877 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3878 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3879 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3883 | ** Return the sector size in bytes of the underlying block device for |
| 3884 | ** the specified file. This is almost always 512 bytes, but may be |
| 3885 | ** larger for some devices. |
| 3886 | ** |
| 3887 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3888 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3889 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3890 | ** same for both. |
| 3891 | */ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3892 | #ifndef __QNXNTO__ |
| 3893 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3894 | UNUSED_PARAMETER(NotUsed); |
drh | 8942d41 | 2012-01-02 18:20:14 +0000 | [diff] [blame] | 3895 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3896 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3897 | #endif |
| 3898 | |
| 3899 | /* |
| 3900 | ** The following version of unixSectorSize() is optimized for QNX. |
| 3901 | */ |
| 3902 | #ifdef __QNXNTO__ |
| 3903 | #include <sys/dcmd_blk.h> |
| 3904 | #include <sys/statvfs.h> |
| 3905 | static int unixSectorSize(sqlite3_file *id){ |
| 3906 | unixFile *pFile = (unixFile*)id; |
| 3907 | if( pFile->sectorSize == 0 ){ |
| 3908 | struct statvfs fsInfo; |
| 3909 | |
| 3910 | /* Set defaults for non-supported filesystems */ |
| 3911 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3912 | pFile->deviceCharacteristics = 0; |
| 3913 | if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
| 3914 | return pFile->sectorSize; |
| 3915 | } |
| 3916 | |
| 3917 | if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 3918 | pFile->sectorSize = fsInfo.f_bsize; |
| 3919 | pFile->deviceCharacteristics = |
| 3920 | SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 3921 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3922 | ** the write succeeds */ |
| 3923 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3924 | ** so it is ordered */ |
| 3925 | 0; |
| 3926 | }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 3927 | pFile->sectorSize = fsInfo.f_bsize; |
| 3928 | pFile->deviceCharacteristics = |
| 3929 | /* etfs cluster size writes are atomic */ |
| 3930 | (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 3931 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3932 | ** the write succeeds */ |
| 3933 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3934 | ** so it is ordered */ |
| 3935 | 0; |
| 3936 | }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 3937 | pFile->sectorSize = fsInfo.f_bsize; |
| 3938 | pFile->deviceCharacteristics = |
| 3939 | SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 3940 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3941 | ** the write succeeds */ |
| 3942 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3943 | ** so it is ordered */ |
| 3944 | 0; |
| 3945 | }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 3946 | pFile->sectorSize = fsInfo.f_bsize; |
| 3947 | pFile->deviceCharacteristics = |
| 3948 | /* full bitset of atomics from max sector size and smaller */ |
| 3949 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3950 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3951 | ** so it is ordered */ |
| 3952 | 0; |
| 3953 | }else if( strstr(fsInfo.f_basetype, "dos") ){ |
| 3954 | pFile->sectorSize = fsInfo.f_bsize; |
| 3955 | pFile->deviceCharacteristics = |
| 3956 | /* full bitset of atomics from max sector size and smaller */ |
| 3957 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3958 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3959 | ** so it is ordered */ |
| 3960 | 0; |
| 3961 | }else{ |
| 3962 | pFile->deviceCharacteristics = |
| 3963 | SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 3964 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3965 | ** the write succeeds */ |
| 3966 | 0; |
| 3967 | } |
| 3968 | } |
| 3969 | /* Last chance verification. If the sector size isn't a multiple of 512 |
| 3970 | ** then it isn't valid.*/ |
| 3971 | if( pFile->sectorSize % 512 != 0 ){ |
| 3972 | pFile->deviceCharacteristics = 0; |
| 3973 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3974 | } |
| 3975 | return pFile->sectorSize; |
| 3976 | } |
| 3977 | #endif /* __QNXNTO__ */ |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3978 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3979 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3980 | ** Return the device characteristics for the file. |
| 3981 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3982 | ** 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] | 3983 | ** However, that choice is controversial since technically the underlying |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3984 | ** file system does not always provide powersafe overwrites. (In other |
| 3985 | ** words, after a power-loss event, parts of the file that were never |
| 3986 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 3987 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 3988 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 3989 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 3990 | ** 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] | 3991 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3992 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
| 3993 | unixFile *p = (unixFile*)id; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3994 | int rc = 0; |
| 3995 | #ifdef __QNXNTO__ |
| 3996 | if( p->sectorSize==0 ) unixSectorSize(id); |
| 3997 | rc = p->deviceCharacteristics; |
| 3998 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3999 | if( p->ctrlFlags & UNIXFILE_PSOW ){ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4000 | rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4001 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4002 | return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4005 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4006 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4007 | /* |
| 4008 | ** Return the system page size. |
| 4009 | ** |
| 4010 | ** This function should not be called directly by other code in this file. |
| 4011 | ** Instead, it should be called via macro osGetpagesize(). |
| 4012 | */ |
| 4013 | static int unixGetpagesize(void){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4014 | #if OS_VXWORKS |
| 4015 | return 1024; |
| 4016 | #elif defined(_BSD_SOURCE) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4017 | return getpagesize(); |
| 4018 | #else |
| 4019 | return (int)sysconf(_SC_PAGESIZE); |
| 4020 | #endif |
| 4021 | } |
| 4022 | |
| 4023 | #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ |
| 4024 | |
| 4025 | #ifndef SQLITE_OMIT_WAL |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4026 | |
| 4027 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4028 | ** Object used to represent an shared memory buffer. |
| 4029 | ** |
| 4030 | ** When multiple threads all reference the same wal-index, each thread |
| 4031 | ** has its own unixShm object, but they all point to a single instance |
| 4032 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 4033 | ** only once per process. |
| 4034 | ** |
| 4035 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 4036 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 4037 | ** every open file that does not use shared memory (in other words, most |
| 4038 | ** open files) would have to carry around this extra information. So |
| 4039 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 4040 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4041 | ** |
| 4042 | ** unixMutexHeld() must be true when creating or destroying |
| 4043 | ** this object or while reading or writing the following fields: |
| 4044 | ** |
| 4045 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4046 | ** |
| 4047 | ** The following fields are read-only after the object is created: |
| 4048 | ** |
| 4049 | ** fid |
| 4050 | ** zFilename |
| 4051 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4052 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4053 | ** unixMutexHeld() is true when reading or writing any other field |
| 4054 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4055 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4056 | struct unixShmNode { |
| 4057 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4058 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4059 | char *zFilename; /* Name of the mmapped file */ |
| 4060 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4061 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4062 | u16 nRegion; /* Size of array apRegion */ |
| 4063 | u8 isReadonly; /* True if read-only */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4064 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4065 | int nRef; /* Number of unixShm objects pointing to this */ |
| 4066 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4067 | #ifdef SQLITE_DEBUG |
| 4068 | u8 exclMask; /* Mask of exclusive locks held */ |
| 4069 | u8 sharedMask; /* Mask of shared locks held */ |
| 4070 | u8 nextShmId; /* Next available unixShm.id value */ |
| 4071 | #endif |
| 4072 | }; |
| 4073 | |
| 4074 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4075 | ** Structure used internally by this VFS to record the state of an |
| 4076 | ** open shared memory connection. |
| 4077 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4078 | ** The following fields are initialized when this object is created and |
| 4079 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4080 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4081 | ** unixShm.pFile |
| 4082 | ** unixShm.id |
| 4083 | ** |
| 4084 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 4085 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4086 | */ |
| 4087 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4088 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 4089 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4090 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 4091 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4092 | u16 sharedMask; /* Mask of shared locks held */ |
| 4093 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4094 | }; |
| 4095 | |
| 4096 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4097 | ** Constants used for locking |
| 4098 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 4099 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 4100 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4101 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4102 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4103 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4104 | ** |
| 4105 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 4106 | ** otherwise. |
| 4107 | */ |
| 4108 | static int unixShmSystemLock( |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4109 | unixFile *pFile, /* Open connection to the WAL file */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4110 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4111 | int ofst, /* First byte of the locking range */ |
| 4112 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4113 | ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4114 | unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 4115 | struct flock f; /* The posix advisory locking structure */ |
| 4116 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4117 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4118 | /* Access to the unixShmNode object is serialized by the caller */ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4119 | pShmNode = pFile->pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4120 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4121 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4122 | /* Shared locks never span more than one byte */ |
| 4123 | assert( n==1 || lockType!=F_RDLCK ); |
| 4124 | |
| 4125 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4126 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4127 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4128 | if( pShmNode->h>=0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4129 | int lkType; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4130 | /* Initialize the locking parameters */ |
| 4131 | memset(&f, 0, sizeof(f)); |
| 4132 | f.l_type = lockType; |
| 4133 | f.l_whence = SEEK_SET; |
| 4134 | f.l_start = ofst; |
| 4135 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4136 | |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4137 | lkType = (pFile->ctrlFlags & UNIXFILE_BLOCK)!=0 ? F_SETLKW : F_SETLK; |
| 4138 | rc = osFcntl(pShmNode->h, lkType, &f); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4139 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4140 | pFile->ctrlFlags &= ~UNIXFILE_BLOCK; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4141 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4142 | |
| 4143 | /* Update the global lock state and do debug tracing */ |
| 4144 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4145 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4146 | OSTRACE(("SHM-LOCK ")); |
drh | 693e671 | 2014-01-24 22:58:00 +0000 | [diff] [blame] | 4147 | mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4148 | if( rc==SQLITE_OK ){ |
| 4149 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4150 | OSTRACE(("unlock %d ok", ofst)); |
| 4151 | pShmNode->exclMask &= ~mask; |
| 4152 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4153 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4154 | OSTRACE(("read-lock %d ok", ofst)); |
| 4155 | pShmNode->exclMask &= ~mask; |
| 4156 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4157 | }else{ |
| 4158 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4159 | OSTRACE(("write-lock %d ok", ofst)); |
| 4160 | pShmNode->exclMask |= mask; |
| 4161 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4162 | } |
| 4163 | }else{ |
| 4164 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4165 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4166 | }else if( lockType==F_RDLCK ){ |
| 4167 | OSTRACE(("read-lock failed")); |
| 4168 | }else{ |
| 4169 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4170 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4171 | } |
| 4172 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4173 | OSTRACE((" - afterwards %03x,%03x\n", |
| 4174 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4175 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4176 | #endif |
| 4177 | |
| 4178 | return rc; |
| 4179 | } |
| 4180 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4181 | /* |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4182 | ** Return the minimum number of 32KB shm regions that should be mapped at |
| 4183 | ** a time, assuming that each mapping must be an integer multiple of the |
| 4184 | ** current system page-size. |
| 4185 | ** |
| 4186 | ** Usually, this is 1. The exception seems to be systems that are configured |
| 4187 | ** to use 64KB pages - in this case each mapping must cover at least two |
| 4188 | ** shm regions. |
| 4189 | */ |
| 4190 | static int unixShmRegionPerMap(void){ |
| 4191 | int shmsz = 32*1024; /* SHM region size */ |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4192 | int pgsz = osGetpagesize(); /* System page size */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4193 | assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ |
| 4194 | if( pgsz<shmsz ) return 1; |
| 4195 | return pgsz/shmsz; |
| 4196 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4197 | |
| 4198 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4199 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4200 | ** |
| 4201 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4202 | ** by VFS shared-memory methods. |
| 4203 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4204 | static void unixShmPurge(unixFile *pFd){ |
| 4205 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4206 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4207 | if( p && p->nRef==0 ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4208 | int nShmPerMap = unixShmRegionPerMap(); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4209 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4210 | assert( p->pInode==pFd->pInode ); |
drh | df3aa16 | 2011-06-24 11:29:51 +0000 | [diff] [blame] | 4211 | sqlite3_mutex_free(p->mutex); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4212 | for(i=0; i<p->nRegion; i+=nShmPerMap){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4213 | if( p->h>=0 ){ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 4214 | osMunmap(p->apRegion[i], p->szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4215 | }else{ |
| 4216 | sqlite3_free(p->apRegion[i]); |
| 4217 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4218 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4219 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4220 | if( p->h>=0 ){ |
| 4221 | robust_close(pFd, p->h, __LINE__); |
| 4222 | p->h = -1; |
| 4223 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4224 | p->pInode->pShmNode = 0; |
| 4225 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4230 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4231 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4232 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4233 | ** The file used to implement shared-memory is in the same directory |
| 4234 | ** as the open database file and has the same name as the open database |
| 4235 | ** file with the "-shm" suffix added. For example, if the database file |
| 4236 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4237 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 4238 | ** |
| 4239 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 4240 | ** some other tmpfs mount. But if a file in a different directory |
| 4241 | ** from the database file is used, then differing access permissions |
| 4242 | ** or a chroot() might cause two different processes on the same |
| 4243 | ** database to end up using different files for shared memory - |
| 4244 | ** meaning that their memory would not really be shared - resulting |
| 4245 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 4246 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 4247 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 4248 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 4249 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 4250 | ** same database file at the same time, database corruption will likely |
| 4251 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 4252 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4253 | ** |
| 4254 | ** When opening a new shared-memory file, if no other instances of that |
| 4255 | ** file are currently open, in this process or in other processes, then |
| 4256 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4257 | ** |
| 4258 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 4259 | ** that means that an exclusive lock is held on the database file and |
| 4260 | ** that no other processes are able to read or write the database. In |
| 4261 | ** that case, we do not really need shared memory. No shared memory |
| 4262 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4263 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4264 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 4265 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4266 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 4267 | int rc; /* Result code */ |
| 4268 | unixInodeInfo *pInode; /* The inode of fd */ |
| 4269 | char *zShmFilename; /* Name of the file used for SHM */ |
| 4270 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4271 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4272 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4273 | p = sqlite3_malloc( sizeof(*p) ); |
| 4274 | if( p==0 ) return SQLITE_NOMEM; |
| 4275 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4276 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4277 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4278 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 4279 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4280 | */ |
| 4281 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4282 | pInode = pDbFd->pInode; |
| 4283 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4284 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4285 | struct stat sStat; /* fstat() info for database file */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4286 | #ifndef SQLITE_SHM_DIRECTORY |
| 4287 | const char *zBasePath = pDbFd->zPath; |
| 4288 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4289 | |
| 4290 | /* Call fstat() to figure out the permissions on the database file. If |
| 4291 | ** 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] | 4292 | ** with the same permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4293 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4294 | if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4295 | rc = SQLITE_IOERR_FSTAT; |
| 4296 | goto shm_open_err; |
| 4297 | } |
| 4298 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4299 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4300 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4301 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4302 | nShmFilename = 6 + (int)strlen(zBasePath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4303 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4304 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4305 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4306 | rc = SQLITE_NOMEM; |
| 4307 | goto shm_open_err; |
| 4308 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 4309 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4310 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4311 | #ifdef SQLITE_SHM_DIRECTORY |
| 4312 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 4313 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 4314 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 4315 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4316 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath); |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4317 | sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4318 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4319 | pShmNode->h = -1; |
| 4320 | pDbFd->pInode->pShmNode = pShmNode; |
| 4321 | pShmNode->pInode = pDbFd->pInode; |
| 4322 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4323 | if( pShmNode->mutex==0 ){ |
| 4324 | rc = SQLITE_NOMEM; |
| 4325 | goto shm_open_err; |
| 4326 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4327 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4328 | if( pInode->bProcessLock==0 ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4329 | int openFlags = O_RDWR | O_CREAT; |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 4330 | if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4331 | openFlags = O_RDONLY; |
| 4332 | pShmNode->isReadonly = 1; |
| 4333 | } |
| 4334 | pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4335 | if( pShmNode->h<0 ){ |
drh | c96d1e7 | 2012-02-11 18:51:34 +0000 | [diff] [blame] | 4336 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 4337 | goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4338 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4339 | |
| 4340 | /* If this process is running as root, make sure that the SHM file |
| 4341 | ** is owned by the same user that owns the original database. Otherwise, |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4342 | ** the original owner will not be able to connect. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4343 | */ |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4344 | osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4345 | |
| 4346 | /* Check to see if another process is holding the dead-man switch. |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4347 | ** If not, truncate the file to zero length. |
| 4348 | */ |
| 4349 | rc = SQLITE_OK; |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4350 | if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4351 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 4352 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4353 | } |
| 4354 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4355 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4356 | rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4357 | } |
| 4358 | if( rc ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4359 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4360 | } |
| 4361 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4362 | /* Make the new connection a child of the unixShmNode */ |
| 4363 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4364 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4365 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4366 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4367 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4368 | pDbFd->pShm = p; |
| 4369 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4370 | |
| 4371 | /* The reference count on pShmNode has already been incremented under |
| 4372 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 4373 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 4374 | ** left to do is to link the new object into the linked list starting |
| 4375 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 4376 | ** mutex. |
| 4377 | */ |
| 4378 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4379 | p->pNext = pShmNode->pFirst; |
| 4380 | pShmNode->pFirst = p; |
| 4381 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4382 | return SQLITE_OK; |
| 4383 | |
| 4384 | /* Jump here on any error */ |
| 4385 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4386 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4387 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4388 | unixLeaveMutex(); |
| 4389 | return rc; |
| 4390 | } |
| 4391 | |
| 4392 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4393 | ** This function is called to obtain a pointer to region iRegion of the |
| 4394 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4395 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 4396 | ** bytes in size. |
| 4397 | ** |
| 4398 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 4399 | ** |
| 4400 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 4401 | ** region has not been allocated (by any client, including one running in a |
| 4402 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4403 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 4404 | ** been allocated, it is allocated by this function. |
| 4405 | ** |
| 4406 | ** If the shared-memory region has already been allocated or is allocated by |
| 4407 | ** this call as described above, then it is mapped into this processes |
| 4408 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4409 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4410 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4411 | static int unixShmMap( |
| 4412 | sqlite3_file *fd, /* Handle open on database file */ |
| 4413 | int iRegion, /* Region to retrieve */ |
| 4414 | int szRegion, /* Size of regions */ |
| 4415 | int bExtend, /* True to extend file if necessary */ |
| 4416 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4417 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4418 | unixFile *pDbFd = (unixFile*)fd; |
| 4419 | unixShm *p; |
| 4420 | unixShmNode *pShmNode; |
| 4421 | int rc = SQLITE_OK; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4422 | int nShmPerMap = unixShmRegionPerMap(); |
| 4423 | int nReqRegion; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4424 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4425 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4426 | if( pDbFd->pShm==0 ){ |
| 4427 | rc = unixOpenSharedMemory(pDbFd); |
| 4428 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4429 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4430 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4431 | p = pDbFd->pShm; |
| 4432 | pShmNode = p->pShmNode; |
| 4433 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4434 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4435 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4436 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4437 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4438 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4439 | /* Minimum number of regions required to be mapped. */ |
| 4440 | nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; |
| 4441 | |
| 4442 | if( pShmNode->nRegion<nReqRegion ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4443 | char **apNew; /* New apRegion[] array */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4444 | int nByte = nReqRegion*szRegion; /* Minimum required file size */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4445 | struct stat sStat; /* Used by fstat() */ |
| 4446 | |
| 4447 | pShmNode->szRegion = szRegion; |
| 4448 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4449 | if( pShmNode->h>=0 ){ |
| 4450 | /* The requested region is not mapped into this processes address space. |
| 4451 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4452 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4453 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4454 | if( osFstat(pShmNode->h, &sStat) ){ |
| 4455 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4456 | goto shmpage_out; |
| 4457 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4458 | |
| 4459 | if( sStat.st_size<nByte ){ |
| 4460 | /* The requested memory region does not exist. If bExtend is set to |
| 4461 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4462 | */ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4463 | if( !bExtend ){ |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4464 | goto shmpage_out; |
| 4465 | } |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4466 | |
| 4467 | /* Alternatively, if bExtend is true, extend the file. Do this by |
| 4468 | ** writing a single byte to the end of each (OS) page being |
| 4469 | ** allocated or extended. Technically, we need only write to the |
| 4470 | ** last page in order to extend the file. But writing to all new |
| 4471 | ** pages forces the OS to allocate them immediately, which reduces |
| 4472 | ** the chances of SIGBUS while accessing the mapped region later on. |
| 4473 | */ |
| 4474 | else{ |
| 4475 | static const int pgsz = 4096; |
| 4476 | int iPg; |
| 4477 | |
| 4478 | /* Write to the last byte of each newly allocated or extended page */ |
| 4479 | assert( (nByte % pgsz)==0 ); |
| 4480 | for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
| 4481 | if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){ |
| 4482 | const char *zFile = pShmNode->zFilename; |
| 4483 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 4484 | goto shmpage_out; |
| 4485 | } |
| 4486 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4487 | } |
| 4488 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | /* Map the requested memory region into this processes address space. */ |
| 4492 | apNew = (char **)sqlite3_realloc( |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4493 | pShmNode->apRegion, nReqRegion*sizeof(char *) |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4494 | ); |
| 4495 | if( !apNew ){ |
| 4496 | rc = SQLITE_IOERR_NOMEM; |
| 4497 | goto shmpage_out; |
| 4498 | } |
| 4499 | pShmNode->apRegion = apNew; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4500 | while( pShmNode->nRegion<nReqRegion ){ |
| 4501 | int nMap = szRegion*nShmPerMap; |
| 4502 | int i; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4503 | void *pMem; |
| 4504 | if( pShmNode->h>=0 ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4505 | pMem = osMmap(0, nMap, |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4506 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 4507 | MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4508 | ); |
| 4509 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4510 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4511 | goto shmpage_out; |
| 4512 | } |
| 4513 | }else{ |
| 4514 | pMem = sqlite3_malloc(szRegion); |
| 4515 | if( pMem==0 ){ |
| 4516 | rc = SQLITE_NOMEM; |
| 4517 | goto shmpage_out; |
| 4518 | } |
| 4519 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4520 | } |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4521 | |
| 4522 | for(i=0; i<nShmPerMap; i++){ |
| 4523 | pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; |
| 4524 | } |
| 4525 | pShmNode->nRegion += nShmPerMap; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | shmpage_out: |
| 4530 | if( pShmNode->nRegion>iRegion ){ |
| 4531 | *pp = pShmNode->apRegion[iRegion]; |
| 4532 | }else{ |
| 4533 | *pp = 0; |
| 4534 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4535 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4536 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4537 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4541 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4542 | ** |
| 4543 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4544 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4545 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4546 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4547 | */ |
| 4548 | static int unixShmLock( |
| 4549 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4550 | int ofst, /* First lock to acquire or release */ |
| 4551 | int n, /* Number of locks to acquire or release */ |
| 4552 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4553 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4554 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4555 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4556 | unixShm *pX; /* For looping over all siblings */ |
| 4557 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4558 | int rc = SQLITE_OK; /* Result code */ |
| 4559 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4560 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4561 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4562 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4563 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4564 | assert( n>=1 ); |
| 4565 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4566 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4567 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4568 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4569 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4570 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4571 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4572 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4573 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4574 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4575 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4576 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4577 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4578 | |
| 4579 | /* See if any siblings hold this same lock */ |
| 4580 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4581 | if( pX==p ) continue; |
| 4582 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4583 | allMask |= pX->sharedMask; |
| 4584 | } |
| 4585 | |
| 4586 | /* Unlock the system-level locks */ |
| 4587 | if( (mask & allMask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4588 | rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4589 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4590 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4591 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4592 | |
| 4593 | /* Undo the local locks */ |
| 4594 | if( rc==SQLITE_OK ){ |
| 4595 | p->exclMask &= ~mask; |
| 4596 | p->sharedMask &= ~mask; |
| 4597 | } |
| 4598 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4599 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4600 | |
| 4601 | /* Find out which shared locks are already held by sibling connections. |
| 4602 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4603 | ** SQLITE_BUSY. |
| 4604 | */ |
| 4605 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4606 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4607 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4608 | break; |
| 4609 | } |
| 4610 | allShared |= pX->sharedMask; |
| 4611 | } |
| 4612 | |
| 4613 | /* Get shared locks at the system level, if necessary */ |
| 4614 | if( rc==SQLITE_OK ){ |
| 4615 | if( (allShared & mask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4616 | rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4617 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4618 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4619 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4620 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4621 | |
| 4622 | /* Get the local shared locks */ |
| 4623 | if( rc==SQLITE_OK ){ |
| 4624 | p->sharedMask |= mask; |
| 4625 | } |
| 4626 | }else{ |
| 4627 | /* Make sure no sibling connections hold locks that will block this |
| 4628 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4629 | */ |
| 4630 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4631 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4632 | rc = SQLITE_BUSY; |
| 4633 | break; |
| 4634 | } |
| 4635 | } |
| 4636 | |
| 4637 | /* Get the exclusive locks at the system level. Then if successful |
| 4638 | ** also mark the local connection as being locked. |
| 4639 | */ |
| 4640 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4641 | rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4642 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4643 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4644 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4645 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4646 | } |
| 4647 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4648 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4649 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 4650 | p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4651 | return rc; |
| 4652 | } |
| 4653 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4654 | /* |
| 4655 | ** Implement a memory barrier or memory fence on shared memory. |
| 4656 | ** |
| 4657 | ** All loads and stores begun before the barrier must complete before |
| 4658 | ** any load or store begun after the barrier. |
| 4659 | */ |
| 4660 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4661 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4662 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4663 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4664 | unixEnterMutex(); |
| 4665 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4668 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4669 | ** Close a connection to shared-memory. Delete the underlying |
| 4670 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4671 | ** |
| 4672 | ** If there is no shared memory associated with the connection then this |
| 4673 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4674 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4675 | static int unixShmUnmap( |
| 4676 | sqlite3_file *fd, /* The underlying database file */ |
| 4677 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4678 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4679 | unixShm *p; /* The connection to be closed */ |
| 4680 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4681 | unixShm **pp; /* For looping over sibling connections */ |
| 4682 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4683 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4684 | pDbFd = (unixFile*)fd; |
| 4685 | p = pDbFd->pShm; |
| 4686 | if( p==0 ) return SQLITE_OK; |
| 4687 | pShmNode = p->pShmNode; |
| 4688 | |
| 4689 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4690 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4691 | |
| 4692 | /* Remove connection p from the set of connections associated |
| 4693 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4694 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4695 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4696 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4697 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4698 | /* Free the connection p */ |
| 4699 | sqlite3_free(p); |
| 4700 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4701 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4702 | |
| 4703 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4704 | ** shared-memory file, too */ |
| 4705 | unixEnterMutex(); |
| 4706 | assert( pShmNode->nRef>0 ); |
| 4707 | pShmNode->nRef--; |
| 4708 | if( pShmNode->nRef==0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4709 | if( deleteFlag && pShmNode->h>=0 ){ |
| 4710 | osUnlink(pShmNode->zFilename); |
| 4711 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4712 | unixShmPurge(pDbFd); |
| 4713 | } |
| 4714 | unixLeaveMutex(); |
| 4715 | |
| 4716 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4717 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4718 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4719 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4720 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4721 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4722 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4723 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4724 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4725 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4726 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4727 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4728 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4729 | ** If it is currently memory mapped, unmap file pFd. |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4730 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4731 | static void unixUnmapfile(unixFile *pFd){ |
| 4732 | assert( pFd->nFetchOut==0 ); |
| 4733 | if( pFd->pMapRegion ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4734 | osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4735 | pFd->pMapRegion = 0; |
| 4736 | pFd->mmapSize = 0; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4737 | pFd->mmapSizeActual = 0; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4738 | } |
| 4739 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4740 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4741 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4742 | ** Attempt to set the size of the memory mapping maintained by file |
| 4743 | ** descriptor pFd to nNew bytes. Any existing mapping is discarded. |
| 4744 | ** |
| 4745 | ** If successful, this function sets the following variables: |
| 4746 | ** |
| 4747 | ** unixFile.pMapRegion |
| 4748 | ** unixFile.mmapSize |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4749 | ** unixFile.mmapSizeActual |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4750 | ** |
| 4751 | ** If unsuccessful, an error message is logged via sqlite3_log() and |
| 4752 | ** the three variables above are zeroed. In this case SQLite should |
| 4753 | ** continue accessing the database using the xRead() and xWrite() |
| 4754 | ** methods. |
| 4755 | */ |
| 4756 | static void unixRemapfile( |
| 4757 | unixFile *pFd, /* File descriptor object */ |
| 4758 | i64 nNew /* Required mapping size */ |
| 4759 | ){ |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4760 | const char *zErr = "mmap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4761 | int h = pFd->h; /* File descriptor open on db file */ |
| 4762 | u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4763 | i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4764 | u8 *pNew = 0; /* Location of new mapping */ |
| 4765 | int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 4766 | |
| 4767 | assert( pFd->nFetchOut==0 ); |
| 4768 | assert( nNew>pFd->mmapSize ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4769 | assert( nNew<=pFd->mmapSizeMax ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4770 | assert( nNew>0 ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4771 | assert( pFd->mmapSizeActual>=pFd->mmapSize ); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4772 | assert( MAP_FAILED!=0 ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4773 | |
| 4774 | if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
| 4775 | |
| 4776 | if( pOrig ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4777 | #if HAVE_MREMAP |
| 4778 | i64 nReuse = pFd->mmapSize; |
| 4779 | #else |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4780 | const int szSyspage = osGetpagesize(); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4781 | i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4782 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4783 | u8 *pReq = &pOrig[nReuse]; |
| 4784 | |
| 4785 | /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 4786 | if( nReuse!=nOrig ){ |
| 4787 | osMunmap(pReq, nOrig-nReuse); |
| 4788 | } |
| 4789 | |
| 4790 | #if HAVE_MREMAP |
| 4791 | pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4792 | zErr = "mremap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4793 | #else |
| 4794 | pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 4795 | if( pNew!=MAP_FAILED ){ |
| 4796 | if( pNew!=pReq ){ |
| 4797 | osMunmap(pNew, nNew - nReuse); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4798 | pNew = 0; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4799 | }else{ |
| 4800 | pNew = pOrig; |
| 4801 | } |
| 4802 | } |
| 4803 | #endif |
| 4804 | |
dan | 48ccef8 | 2013-04-02 20:55:01 +0000 | [diff] [blame] | 4805 | /* The attempt to extend the existing mapping failed. Free it. */ |
| 4806 | if( pNew==MAP_FAILED || pNew==0 ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4807 | osMunmap(pOrig, nReuse); |
| 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | /* If pNew is still NULL, try to create an entirely new mapping. */ |
| 4812 | if( pNew==0 ){ |
| 4813 | pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4816 | if( pNew==MAP_FAILED ){ |
| 4817 | pNew = 0; |
| 4818 | nNew = 0; |
| 4819 | unixLogError(SQLITE_OK, zErr, pFd->zPath); |
| 4820 | |
| 4821 | /* If the mmap() above failed, assume that all subsequent mmap() calls |
| 4822 | ** will probably fail too. Fall back to using xRead/xWrite exclusively |
| 4823 | ** in this case. */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4824 | pFd->mmapSizeMax = 0; |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4825 | } |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4826 | pFd->pMapRegion = (void *)pNew; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4827 | pFd->mmapSize = pFd->mmapSizeActual = nNew; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4828 | } |
| 4829 | |
| 4830 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4831 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 4832 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 4833 | ** there already exists a mapping for this file, and there are still |
| 4834 | ** outstanding xFetch() references to it, this function is a no-op. |
| 4835 | ** |
| 4836 | ** If parameter nByte is non-negative, then it is the requested size of |
| 4837 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 4838 | ** requested size is the size of the file on disk. The actual size of the |
| 4839 | ** created mapping is either the requested size or the value configured |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 4840 | ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4841 | ** |
| 4842 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 4843 | ** recreated as a result of outstanding references) or an SQLite error |
| 4844 | ** code otherwise. |
| 4845 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4846 | static int unixMapfile(unixFile *pFd, i64 nByte){ |
| 4847 | i64 nMap = nByte; |
| 4848 | int rc; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4849 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4850 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
| 4851 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 4852 | |
| 4853 | if( nMap<0 ){ |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 4854 | struct stat statbuf; /* Low-level file information */ |
| 4855 | rc = osFstat(pFd->h, &statbuf); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4856 | if( rc!=SQLITE_OK ){ |
| 4857 | return SQLITE_IOERR_FSTAT; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4858 | } |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 4859 | nMap = statbuf.st_size; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4860 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4861 | if( nMap>pFd->mmapSizeMax ){ |
| 4862 | nMap = pFd->mmapSizeMax; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4865 | if( nMap!=pFd->mmapSize ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4866 | if( nMap>0 ){ |
| 4867 | unixRemapfile(pFd, nMap); |
| 4868 | }else{ |
dan | b7e3a32 | 2013-03-25 20:30:13 +0000 | [diff] [blame] | 4869 | unixUnmapfile(pFd); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4870 | } |
| 4871 | } |
| 4872 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4873 | return SQLITE_OK; |
| 4874 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4875 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4876 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4877 | /* |
| 4878 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 4879 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 4880 | ** |
| 4881 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 4882 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 4883 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 4884 | ** value of *pp is undefined in this case. |
| 4885 | ** |
| 4886 | ** If this function does return a pointer, the caller must eventually |
| 4887 | ** release the reference by calling unixUnfetch(). |
| 4888 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4889 | static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4890 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4891 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 4892 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4893 | *pp = 0; |
| 4894 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4895 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 4896 | if( pFd->mmapSizeMax>0 ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4897 | if( pFd->pMapRegion==0 ){ |
| 4898 | int rc = unixMapfile(pFd, -1); |
| 4899 | if( rc!=SQLITE_OK ) return rc; |
| 4900 | } |
| 4901 | if( pFd->mmapSize >= iOff+nAmt ){ |
| 4902 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 4903 | pFd->nFetchOut++; |
| 4904 | } |
| 4905 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 4906 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4907 | return SQLITE_OK; |
| 4908 | } |
| 4909 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4910 | /* |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4911 | ** If the third argument is non-NULL, then this function releases a |
| 4912 | ** reference obtained by an earlier call to unixFetch(). The second |
| 4913 | ** argument passed to this function must be the same as the corresponding |
| 4914 | ** argument that was passed to the unixFetch() invocation. |
| 4915 | ** |
| 4916 | ** Or, if the third argument is NULL, then this function is being called |
| 4917 | ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 4918 | ** may now be invalid and should be unmapped. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4919 | */ |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4920 | static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 4921 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 4922 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 4923 | UNUSED_PARAMETER(iOff); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 4924 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4925 | /* If p==0 (unmap the entire file) then there must be no outstanding |
| 4926 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 4927 | ** then there must be at least one outstanding. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4928 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 4929 | |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4930 | /* If p!=0, it must match the iOff value. */ |
| 4931 | assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 4932 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4933 | if( p ){ |
| 4934 | pFd->nFetchOut--; |
| 4935 | }else{ |
| 4936 | unixUnmapfile(pFd); |
| 4937 | } |
| 4938 | |
| 4939 | assert( pFd->nFetchOut>=0 ); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 4940 | #else |
| 4941 | UNUSED_PARAMETER(fd); |
| 4942 | UNUSED_PARAMETER(p); |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 4943 | UNUSED_PARAMETER(iOff); |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 4944 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4945 | return SQLITE_OK; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4949 | ** Here ends the implementation of all sqlite3_file methods. |
| 4950 | ** |
| 4951 | ********************** End sqlite3_file Methods ******************************* |
| 4952 | ******************************************************************************/ |
| 4953 | |
| 4954 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4955 | ** This division contains definitions of sqlite3_io_methods objects that |
| 4956 | ** implement various file locking strategies. It also contains definitions |
| 4957 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 4958 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 4959 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 4960 | ** the correct finder-function for that VFS. |
| 4961 | ** |
| 4962 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 4963 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 4964 | ** looks at the filesystem type and tries to guess the best locking |
| 4965 | ** strategy from that. |
| 4966 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4967 | ** For finder-function F, two objects are created: |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4968 | ** |
| 4969 | ** (1) The real finder-function named "FImpt()". |
| 4970 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4971 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4972 | ** |
| 4973 | ** |
| 4974 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 4975 | ** objects. We have to do this instead of letting pAppData point |
| 4976 | ** directly at the finder-function since C90 rules prevent a void* |
| 4977 | ** from be cast into a function pointer. |
| 4978 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4979 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4980 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4981 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4982 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 4983 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 4984 | ** |
| 4985 | ** * An I/O method finder function called FINDER that returns a pointer |
| 4986 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4987 | */ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 4988 | #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4989 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4990 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4991 | CLOSE, /* xClose */ \ |
| 4992 | unixRead, /* xRead */ \ |
| 4993 | unixWrite, /* xWrite */ \ |
| 4994 | unixTruncate, /* xTruncate */ \ |
| 4995 | unixSync, /* xSync */ \ |
| 4996 | unixFileSize, /* xFileSize */ \ |
| 4997 | LOCK, /* xLock */ \ |
| 4998 | UNLOCK, /* xUnlock */ \ |
| 4999 | CKLOCK, /* xCheckReservedLock */ \ |
| 5000 | unixFileControl, /* xFileControl */ \ |
| 5001 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5002 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5003 | SHMMAP, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5004 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 5005 | unixShmBarrier, /* xShmBarrier */ \ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5006 | unixShmUnmap, /* xShmUnmap */ \ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5007 | unixFetch, /* xFetch */ \ |
| 5008 | unixUnfetch, /* xUnfetch */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5009 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5010 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 5011 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5012 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5013 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5014 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5015 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5016 | |
| 5017 | /* |
| 5018 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 5019 | ** locking strategies. Functions that return pointers to these methods |
| 5020 | ** are also created. |
| 5021 | */ |
| 5022 | IOMETHODS( |
| 5023 | posixIoFinder, /* Finder function name */ |
| 5024 | posixIoMethods, /* sqlite3_io_methods object name */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5025 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5026 | unixClose, /* xClose method */ |
| 5027 | unixLock, /* xLock method */ |
| 5028 | unixUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5029 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5030 | unixShmMap /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5031 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5032 | IOMETHODS( |
| 5033 | nolockIoFinder, /* Finder function name */ |
| 5034 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 142341c | 2014-09-19 19:00:48 +0000 | [diff] [blame] | 5035 | 3, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5036 | nolockClose, /* xClose method */ |
| 5037 | nolockLock, /* xLock method */ |
| 5038 | nolockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5039 | nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 5040 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5041 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5042 | IOMETHODS( |
| 5043 | dotlockIoFinder, /* Finder function name */ |
| 5044 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5045 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5046 | dotlockClose, /* xClose method */ |
| 5047 | dotlockLock, /* xLock method */ |
| 5048 | dotlockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5049 | dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 5050 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5051 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5052 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5053 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5054 | IOMETHODS( |
| 5055 | flockIoFinder, /* Finder function name */ |
| 5056 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5057 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5058 | flockClose, /* xClose method */ |
| 5059 | flockLock, /* xLock method */ |
| 5060 | flockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5061 | flockCheckReservedLock, /* xCheckReservedLock method */ |
| 5062 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5063 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5064 | #endif |
| 5065 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5066 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5067 | IOMETHODS( |
| 5068 | semIoFinder, /* Finder function name */ |
| 5069 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5070 | 1, /* shared memory is disabled */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5071 | semXClose, /* xClose method */ |
| 5072 | semXLock, /* xLock method */ |
| 5073 | semXUnlock, /* xUnlock method */ |
| 5074 | semXCheckReservedLock, /* xCheckReservedLock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5075 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5076 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5077 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5078 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5079 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5080 | IOMETHODS( |
| 5081 | afpIoFinder, /* Finder function name */ |
| 5082 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5083 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5084 | afpClose, /* xClose method */ |
| 5085 | afpLock, /* xLock method */ |
| 5086 | afpUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5087 | afpCheckReservedLock, /* xCheckReservedLock method */ |
| 5088 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5089 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5090 | #endif |
| 5091 | |
| 5092 | /* |
| 5093 | ** The proxy locking method is a "super-method" in the sense that it |
| 5094 | ** opens secondary file descriptors for the conch and lock files and |
| 5095 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 5096 | ** secondary files. For this reason, the division that implements |
| 5097 | ** proxy locking is located much further down in the file. But we need |
| 5098 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 5099 | ** for proxy locking here. So we forward declare the I/O methods. |
| 5100 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5101 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5102 | static int proxyClose(sqlite3_file*); |
| 5103 | static int proxyLock(sqlite3_file*, int); |
| 5104 | static int proxyUnlock(sqlite3_file*, int); |
| 5105 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5106 | IOMETHODS( |
| 5107 | proxyIoFinder, /* Finder function name */ |
| 5108 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5109 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5110 | proxyClose, /* xClose method */ |
| 5111 | proxyLock, /* xLock method */ |
| 5112 | proxyUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5113 | proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 5114 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5115 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5116 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5117 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5118 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 5119 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5120 | IOMETHODS( |
| 5121 | nfsIoFinder, /* Finder function name */ |
| 5122 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5123 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5124 | unixClose, /* xClose method */ |
| 5125 | unixLock, /* xLock method */ |
| 5126 | nfsUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5127 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5128 | 0 /* xShmMap method */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5129 | ) |
| 5130 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5131 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5132 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5133 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5134 | ** This "finder" function attempts to determine the best locking strategy |
| 5135 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5136 | ** object that implements that strategy. |
| 5137 | ** |
| 5138 | ** This is for MacOSX only. |
| 5139 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5140 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5141 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5142 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5143 | ){ |
| 5144 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5145 | const char *zFilesystem; /* Filesystem type name */ |
| 5146 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5147 | } aMap[] = { |
| 5148 | { "hfs", &posixIoMethods }, |
| 5149 | { "ufs", &posixIoMethods }, |
| 5150 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5151 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5152 | { "webdav", &nolockIoMethods }, |
| 5153 | { 0, 0 } |
| 5154 | }; |
| 5155 | int i; |
| 5156 | struct statfs fsInfo; |
| 5157 | struct flock lockInfo; |
| 5158 | |
| 5159 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5160 | /* If filePath==NULL that means we are dealing with a transient file |
| 5161 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5162 | return &nolockIoMethods; |
| 5163 | } |
| 5164 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 5165 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 5166 | return &nolockIoMethods; |
| 5167 | } |
| 5168 | for(i=0; aMap[i].zFilesystem; i++){ |
| 5169 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 5170 | return aMap[i].pMethods; |
| 5171 | } |
| 5172 | } |
| 5173 | } |
| 5174 | |
| 5175 | /* Default case. Handles, amongst others, "nfs". |
| 5176 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 5177 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5178 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5179 | lockInfo.l_len = 1; |
| 5180 | lockInfo.l_start = 0; |
| 5181 | lockInfo.l_whence = SEEK_SET; |
| 5182 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5183 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5184 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 5185 | return &nfsIoMethods; |
| 5186 | } else { |
| 5187 | return &posixIoMethods; |
| 5188 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5189 | }else{ |
| 5190 | return &dotlockIoMethods; |
| 5191 | } |
| 5192 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5193 | static const sqlite3_io_methods |
| 5194 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5195 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5196 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5197 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5198 | #if OS_VXWORKS |
| 5199 | /* |
| 5200 | ** This "finder" function for VxWorks checks to see if posix advisory |
| 5201 | ** locking works. If it does, then that is what is used. If it does not |
| 5202 | ** work, then fallback to named semaphore locking. |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5203 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5204 | static const sqlite3_io_methods *vxworksIoFinderImpl( |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5205 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5206 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5207 | ){ |
| 5208 | struct flock lockInfo; |
| 5209 | |
| 5210 | if( !filePath ){ |
| 5211 | /* If filePath==NULL that means we are dealing with a transient file |
| 5212 | ** that does not need to be locked. */ |
| 5213 | return &nolockIoMethods; |
| 5214 | } |
| 5215 | |
| 5216 | /* Test if fcntl() is supported and use POSIX style locks. |
| 5217 | ** Otherwise fall back to the named semaphore method. |
| 5218 | */ |
| 5219 | lockInfo.l_len = 1; |
| 5220 | lockInfo.l_start = 0; |
| 5221 | lockInfo.l_whence = SEEK_SET; |
| 5222 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5223 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5224 | return &posixIoMethods; |
| 5225 | }else{ |
| 5226 | return &semIoMethods; |
| 5227 | } |
| 5228 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5229 | static const sqlite3_io_methods |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5230 | *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5231 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5232 | #endif /* OS_VXWORKS */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5233 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5234 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5235 | ** An abstract type for a pointer to an IO method finder function: |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5236 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5237 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5238 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5239 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5240 | /**************************************************************************** |
| 5241 | **************************** sqlite3_vfs methods **************************** |
| 5242 | ** |
| 5243 | ** This division contains the implementation of methods on the |
| 5244 | ** sqlite3_vfs object. |
| 5245 | */ |
| 5246 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 5247 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5248 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5249 | */ |
| 5250 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5251 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5252 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5253 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5254 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5255 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5256 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5257 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5258 | unixFile *pNew = (unixFile *)pId; |
| 5259 | int rc = SQLITE_OK; |
| 5260 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5261 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5262 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5263 | /* Usually the path zFilename should not be a relative pathname. The |
| 5264 | ** exception is when opening the proxy "conch" file in builds that |
| 5265 | ** include the special Apple locking styles. |
| 5266 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5267 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 5268 | assert( zFilename==0 || zFilename[0]=='/' |
| 5269 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 5270 | #else |
| 5271 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5272 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5273 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5274 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5275 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5276 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5277 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5278 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5279 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5280 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5281 | pNew->ctrlFlags = (u8)ctrlFlags; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5282 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | ede01a9 | 2013-05-17 12:10:52 +0000 | [diff] [blame] | 5283 | pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5284 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5285 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 5286 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5287 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 5288 | } |
drh | 503a686 | 2013-03-01 01:07:17 +0000 | [diff] [blame] | 5289 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5290 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5291 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 5292 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5293 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5294 | pNew->pId = vxworksFindFileId(zFilename); |
| 5295 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5296 | ctrlFlags |= UNIXFILE_NOLOCK; |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5297 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5298 | } |
| 5299 | #endif |
| 5300 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5301 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5302 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5303 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5304 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5305 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5306 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 5307 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 5308 | ** zFilename remains valid until file is closed, to support */ |
| 5309 | pNew->lockingContext = (void*)zFilename; |
| 5310 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5311 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5312 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5313 | if( pLockingStyle == &posixIoMethods |
| 5314 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5315 | || pLockingStyle == &nfsIoMethods |
| 5316 | #endif |
| 5317 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5318 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5319 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5320 | if( rc!=SQLITE_OK ){ |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5321 | /* If an error occurred in findInodeInfo(), close the file descriptor |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5322 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5323 | ** in two scenarios: |
| 5324 | ** |
| 5325 | ** (a) A call to fstat() failed. |
| 5326 | ** (b) A malloc failed. |
| 5327 | ** |
| 5328 | ** Scenario (b) may only occur if the process is holding no other |
| 5329 | ** file descriptors open on the same file. If there were other file |
| 5330 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5331 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5332 | ** handle h - as it is guaranteed that no posix locks will be released |
| 5333 | ** by doing so. |
| 5334 | ** |
| 5335 | ** If scenario (a) caused the error then things are not so safe. The |
| 5336 | ** implicit assumption here is that if fstat() fails, things are in |
| 5337 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 5338 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5339 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5340 | h = -1; |
| 5341 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5342 | unixLeaveMutex(); |
| 5343 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5344 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5345 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 5346 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5347 | /* AFP locking uses the file path so it needs to be included in |
| 5348 | ** the afpLockingContext. |
| 5349 | */ |
| 5350 | afpLockingContext *pCtx; |
| 5351 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 5352 | if( pCtx==0 ){ |
| 5353 | rc = SQLITE_NOMEM; |
| 5354 | }else{ |
| 5355 | /* NB: zFilename exists and remains valid until the file is closed |
| 5356 | ** according to requirement F11141. So we do not need to make a |
| 5357 | ** copy of the filename. */ |
| 5358 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5359 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5360 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5361 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5362 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5363 | if( rc!=SQLITE_OK ){ |
| 5364 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5365 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5366 | h = -1; |
| 5367 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5368 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5369 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5370 | } |
| 5371 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5372 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5373 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 5374 | /* Dotfile locking uses the file path so it needs to be included in |
| 5375 | ** the dotlockLockingContext |
| 5376 | */ |
| 5377 | char *zLockFile; |
| 5378 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5379 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5380 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5381 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 5382 | if( zLockFile==0 ){ |
| 5383 | rc = SQLITE_NOMEM; |
| 5384 | }else{ |
| 5385 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5386 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5387 | pNew->lockingContext = zLockFile; |
| 5388 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5389 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5390 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5391 | else if( pLockingStyle == &semIoMethods ){ |
| 5392 | /* Named semaphore locking uses the file path so it needs to be |
| 5393 | ** included in the semLockingContext |
| 5394 | */ |
| 5395 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5396 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 5397 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 5398 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5399 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5400 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5401 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5402 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5403 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5404 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 5405 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5406 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5407 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5408 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5409 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5410 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5411 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5412 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 5413 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5414 | storeLastErrno(pNew, 0); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5415 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5416 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5417 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 5418 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5419 | osUnlink(zFilename); |
drh | c579754 | 2013-04-27 12:13:29 +0000 | [diff] [blame] | 5420 | pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5421 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5422 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5423 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5424 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5425 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5426 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5427 | OpenCounter(+1); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5428 | verifyDbFile(pNew); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5429 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5430 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5431 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 5432 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5433 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5434 | ** Return the name of a directory in which to put temporary files. |
| 5435 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5436 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5437 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5438 | static const char *azDirs[] = { |
| 5439 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5440 | 0, |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 +0000 | [diff] [blame] | 5441 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5442 | "/var/tmp", |
| 5443 | "/usr/tmp", |
| 5444 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5445 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5446 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5447 | unsigned int i; |
| 5448 | struct stat buf; |
| 5449 | const char *zDir = 0; |
| 5450 | |
| 5451 | azDirs[0] = sqlite3_temp_directory; |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 +0000 | [diff] [blame] | 5452 | if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR"); |
| 5453 | if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 5454 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5455 | if( zDir==0 ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5456 | if( osStat(zDir, &buf) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5457 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5458 | if( osAccess(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5459 | break; |
| 5460 | } |
| 5461 | return zDir; |
| 5462 | } |
| 5463 | |
| 5464 | /* |
| 5465 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 5466 | ** by the calling process and must be big enough to hold at least |
| 5467 | ** pVfs->mxPathname bytes. |
| 5468 | */ |
| 5469 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5470 | static const unsigned char zChars[] = |
| 5471 | "abcdefghijklmnopqrstuvwxyz" |
| 5472 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 5473 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5474 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5475 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5476 | |
| 5477 | /* It's odd to simulate an io-error here, but really this is just |
| 5478 | ** using the io-error infrastructure to test that SQLite handles this |
| 5479 | ** function failing. |
| 5480 | */ |
| 5481 | SimulateIOError( return SQLITE_IOERR ); |
| 5482 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5483 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5484 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5485 | |
| 5486 | /* Check that the output buffer is large enough for the temporary file |
| 5487 | ** name. If it is not, return SQLITE_ERROR. |
| 5488 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5489 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5490 | return SQLITE_ERROR; |
| 5491 | } |
| 5492 | |
| 5493 | do{ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5494 | sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5495 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5496 | sqlite3_randomness(15, &zBuf[j]); |
| 5497 | for(i=0; i<15; i++, j++){ |
| 5498 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 5499 | } |
| 5500 | zBuf[j] = 0; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5501 | zBuf[j+1] = 0; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5502 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5503 | return SQLITE_OK; |
| 5504 | } |
| 5505 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5506 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5507 | /* |
| 5508 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 5509 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 5510 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 5511 | */ |
| 5512 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 5513 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5514 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5515 | /* |
| 5516 | ** Search for an unused file descriptor that was opened on the database |
| 5517 | ** file (not a journal or master-journal file) identified by pathname |
| 5518 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 5519 | ** argument to this function. |
| 5520 | ** |
| 5521 | ** Such a file descriptor may exist if a database connection was closed |
| 5522 | ** but the associated file descriptor could not be closed because some |
| 5523 | ** other file descriptor open on the same file is holding a file-lock. |
| 5524 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 5525 | ** describing "Posix Advisory Locking" at the start of this file for |
| 5526 | ** further details. Also, ticket #4018. |
| 5527 | ** |
| 5528 | ** If a suitable file descriptor is found, then it is returned. If no |
| 5529 | ** such file descriptor is located, -1 is returned. |
| 5530 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5531 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 5532 | UnixUnusedFd *pUnused = 0; |
| 5533 | |
| 5534 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 5535 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 5536 | ** but because no way to test it is currently available. It is better |
| 5537 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 5538 | ** feature. */ |
| 5539 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5540 | struct stat sStat; /* Results of stat() call */ |
| 5541 | |
| 5542 | /* A stat() call may fail for various reasons. If this happens, it is |
| 5543 | ** almost certain that an open() call on the same path will also fail. |
| 5544 | ** For this reason, if an error occurs in the stat() call here, it is |
| 5545 | ** ignored and -1 is returned. The caller will try to open a new file |
| 5546 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 5547 | ** |
| 5548 | ** Even if a subsequent open() call does succeed, the consequences of |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5549 | ** not searching for a reusable file descriptor are not dire. */ |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5550 | if( 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 5551 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5552 | |
| 5553 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5554 | pInode = inodeList; |
| 5555 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 5556 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 5557 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 5558 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5559 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5560 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5561 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5562 | pUnused = *pp; |
| 5563 | if( pUnused ){ |
| 5564 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5565 | } |
| 5566 | } |
| 5567 | unixLeaveMutex(); |
| 5568 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5569 | #endif /* if !OS_VXWORKS */ |
| 5570 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5571 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5572 | |
| 5573 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5574 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 5575 | ** 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] | 5576 | ** and a value suitable for passing as the third argument to open(2) is |
| 5577 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 5578 | ** returned and the value of *pMode is not modified. |
| 5579 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5580 | ** In most cases, this routine sets *pMode to 0, which will become |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5581 | ** an indication to robust_open() to create the file using |
| 5582 | ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 5583 | ** 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] | 5584 | ** this function queries the file-system for the permissions on the |
| 5585 | ** corresponding database file and sets *pMode to this value. Whenever |
| 5586 | ** possible, WAL and journal files are created using the same permissions |
| 5587 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5588 | ** |
| 5589 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 5590 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 5591 | ** FAT filesystems and permissions do not matter there, so just use |
| 5592 | ** the default permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5593 | */ |
| 5594 | static int findCreateFileMode( |
| 5595 | const char *zPath, /* Path of file (possibly) being created */ |
| 5596 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5597 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 5598 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 5599 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5600 | ){ |
| 5601 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5602 | *pMode = 0; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5603 | *pUid = 0; |
| 5604 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5605 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5606 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 5607 | int nDb; /* Number of valid bytes in zDb */ |
| 5608 | struct stat sStat; /* Output of stat() on database file */ |
| 5609 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5610 | /* zPath is a path to a WAL or journal file. The following block derives |
| 5611 | ** the path to the associated database file from zPath. This block handles |
| 5612 | ** the following naming conventions: |
| 5613 | ** |
| 5614 | ** "<path to db>-journal" |
| 5615 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5616 | ** "<path to db>-journalNN" |
| 5617 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5618 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5619 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5620 | ** used by the test_multiplex.c module. |
| 5621 | */ |
| 5622 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5623 | #ifdef SQLITE_ENABLE_8_3_NAMES |
dan | 28a67fd | 2011-12-12 19:48:43 +0000 | [diff] [blame] | 5624 | while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--; |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5625 | if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5626 | #else |
| 5627 | while( zPath[nDb]!='-' ){ |
| 5628 | assert( nDb>0 ); |
| 5629 | assert( zPath[nDb]!='\n' ); |
| 5630 | nDb--; |
| 5631 | } |
| 5632 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5633 | memcpy(zDb, zPath, nDb); |
| 5634 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5635 | |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5636 | if( 0==osStat(zDb, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5637 | *pMode = sStat.st_mode & 0777; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5638 | *pUid = sStat.st_uid; |
| 5639 | *pGid = sStat.st_gid; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5640 | }else{ |
| 5641 | rc = SQLITE_IOERR_FSTAT; |
| 5642 | } |
| 5643 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 5644 | *pMode = 0600; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5645 | } |
| 5646 | return rc; |
| 5647 | } |
| 5648 | |
| 5649 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5650 | ** Open the file zPath. |
| 5651 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5652 | ** Previously, the SQLite OS layer used three functions in place of this |
| 5653 | ** one: |
| 5654 | ** |
| 5655 | ** sqlite3OsOpenReadWrite(); |
| 5656 | ** sqlite3OsOpenReadOnly(); |
| 5657 | ** sqlite3OsOpenExclusive(); |
| 5658 | ** |
| 5659 | ** These calls correspond to the following combinations of flags: |
| 5660 | ** |
| 5661 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5662 | ** ReadOnly() -> (READONLY) |
| 5663 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5664 | ** |
| 5665 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5666 | ** true, the file was configured to be automatically deleted when the |
| 5667 | ** file handle closed. To achieve the same effect using this new |
| 5668 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5669 | ** OpenExclusive(). |
| 5670 | */ |
| 5671 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5672 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5673 | const char *zPath, /* Pathname of file to be opened */ |
| 5674 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5675 | int flags, /* Input flags to control the opening */ |
| 5676 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5677 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5678 | unixFile *p = (unixFile *)pFile; |
| 5679 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5680 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5681 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5682 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5683 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5684 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5685 | |
| 5686 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5687 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5688 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5689 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5690 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5691 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5692 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5693 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5694 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5695 | struct statfs fsInfo; |
| 5696 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5697 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5698 | /* If creating a master or main-file journal, this function will open |
| 5699 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5700 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5701 | */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5702 | int syncDir = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5703 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5704 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5705 | || eType==SQLITE_OPEN_WAL |
| 5706 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5707 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5708 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5709 | ** a temporary file. Use this buffer to store the file name in. |
| 5710 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5711 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5712 | const char *zName = zPath; |
| 5713 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5714 | /* Check the following statements are true: |
| 5715 | ** |
| 5716 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5717 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5718 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5719 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5720 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5721 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5722 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5723 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5724 | assert(isDelete==0 || isCreate); |
| 5725 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5726 | /* The main DB, main journal, WAL file and master journal are never |
| 5727 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5728 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5729 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5730 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5731 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5732 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5733 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5734 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5735 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5736 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5737 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5738 | ); |
| 5739 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5740 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 5741 | ** here such that two or more threads all trying to open databases at |
| 5742 | ** the same instant might all reset the PRNG. But multiple resets |
| 5743 | ** are harmless. |
| 5744 | */ |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 5745 | if( randomnessPid!=osGetpid(0) ){ |
| 5746 | randomnessPid = osGetpid(0); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5747 | sqlite3_randomness(0,0); |
| 5748 | } |
| 5749 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5750 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5751 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5752 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5753 | UnixUnusedFd *pUnused; |
| 5754 | pUnused = findReusableFd(zName, flags); |
| 5755 | if( pUnused ){ |
| 5756 | fd = pUnused->fd; |
| 5757 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 5758 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5759 | if( !pUnused ){ |
| 5760 | return SQLITE_NOMEM; |
| 5761 | } |
| 5762 | } |
| 5763 | p->pUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5764 | |
| 5765 | /* Database filenames are double-zero terminated if they are not |
| 5766 | ** URIs with parameters. Hence, they can always be passed into |
| 5767 | ** sqlite3_uri_parameter(). */ |
| 5768 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 5769 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5770 | }else if( !zName ){ |
| 5771 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5772 | assert(isDelete && !syncDir); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5773 | rc = unixGetTempname(MAX_PATHNAME+2, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5774 | if( rc!=SQLITE_OK ){ |
| 5775 | return rc; |
| 5776 | } |
| 5777 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5778 | |
| 5779 | /* Generated temporary filenames are always double-zero terminated |
| 5780 | ** for use by sqlite3_uri_parameter(). */ |
| 5781 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5782 | } |
| 5783 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5784 | /* Determine the value of the flags parameter passed to POSIX function |
| 5785 | ** open(). These must be calculated even if open() is not called, as |
| 5786 | ** they may be stored as part of the file handle and used by the |
| 5787 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5788 | if( isReadonly ) openFlags |= O_RDONLY; |
| 5789 | if( isReadWrite ) openFlags |= O_RDWR; |
| 5790 | if( isCreate ) openFlags |= O_CREAT; |
| 5791 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 5792 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5793 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5794 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5795 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5796 | uid_t uid; /* Userid for the file */ |
| 5797 | gid_t gid; /* Groupid for the file */ |
| 5798 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5799 | if( rc!=SQLITE_OK ){ |
| 5800 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5801 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5802 | return rc; |
| 5803 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5804 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5805 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5806 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 5807 | /* Failed to open the file for read/write access. Try read-only. */ |
| 5808 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5809 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5810 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5811 | openFlags |= O_RDONLY; |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5812 | isReadonly = 1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5813 | fd = robust_open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5814 | } |
| 5815 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5816 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5817 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5818 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5819 | |
| 5820 | /* If this process is running as root and if creating a new rollback |
| 5821 | ** 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] | 5822 | ** the same as the original database. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5823 | */ |
| 5824 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 5825 | osFchown(fd, uid, gid); |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5826 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5827 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5828 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5829 | if( pOutFlags ){ |
| 5830 | *pOutFlags = flags; |
| 5831 | } |
| 5832 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5833 | if( p->pUnused ){ |
| 5834 | p->pUnused->fd = fd; |
| 5835 | p->pUnused->flags = flags; |
| 5836 | } |
| 5837 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5838 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5839 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5840 | zPath = zName; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 5841 | #elif defined(SQLITE_UNLINK_AFTER_CLOSE) |
| 5842 | zPath = sqlite3_mprintf("%s", zName); |
| 5843 | if( zPath==0 ){ |
| 5844 | robust_close(p, fd, __LINE__); |
| 5845 | return SQLITE_NOMEM; |
| 5846 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5847 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5848 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5849 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5850 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5851 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5852 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5853 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 5854 | } |
| 5855 | #endif |
| 5856 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5857 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5858 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5859 | |
| 5860 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5861 | if( fstatfs(fd, &fsInfo) == -1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5862 | storeLastErrno(p, errno); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5863 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5864 | return SQLITE_IOERR_ACCESS; |
| 5865 | } |
| 5866 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5867 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5868 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5869 | if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) { |
| 5870 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5871 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5872 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5873 | |
| 5874 | /* Set up appropriate ctrlFlags */ |
| 5875 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 5876 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
| 5877 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
| 5878 | if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; |
| 5879 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 5880 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5881 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5882 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5883 | isAutoProxy = 1; |
| 5884 | #endif |
| 5885 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5886 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5887 | int useProxy = 0; |
| 5888 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5889 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5890 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5891 | if( envforce!=NULL ){ |
| 5892 | useProxy = atoi(envforce)>0; |
| 5893 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5894 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 5895 | } |
| 5896 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5897 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5898 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5899 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5900 | if( rc!=SQLITE_OK ){ |
| 5901 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 5902 | ** and clear all the structure's references. Specifically, |
| 5903 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 5904 | */ |
| 5905 | unixClose(pFile); |
| 5906 | return rc; |
| 5907 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5908 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5909 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5910 | } |
| 5911 | } |
| 5912 | #endif |
| 5913 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5914 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 5915 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5916 | open_finished: |
| 5917 | if( rc!=SQLITE_OK ){ |
| 5918 | sqlite3_free(p->pUnused); |
| 5919 | } |
| 5920 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5921 | } |
| 5922 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5923 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5924 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5925 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 5926 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5927 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5928 | static int unixDelete( |
| 5929 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 5930 | const char *zPath, /* Name of file to be deleted */ |
| 5931 | int dirSync /* If true, fsync() directory after deleting file */ |
| 5932 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5933 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5934 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5935 | SimulateIOError(return SQLITE_IOERR_DELETE); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5936 | if( osUnlink(zPath)==(-1) ){ |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 5937 | if( errno==ENOENT |
| 5938 | #if OS_VXWORKS |
drh | 19541f3 | 2014-09-01 13:37:55 +0000 | [diff] [blame] | 5939 | || osAccess(zPath,0)!=0 |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 5940 | #endif |
| 5941 | ){ |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5942 | rc = SQLITE_IOERR_DELETE_NOENT; |
| 5943 | }else{ |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5944 | rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5945 | } |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5946 | return rc; |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5947 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 5948 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 5949 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5950 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 5951 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5952 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5953 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5954 | if( fsync(fd)==-1 ) |
| 5955 | #else |
| 5956 | if( fsync(fd) ) |
| 5957 | #endif |
| 5958 | { |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5959 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5960 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5961 | robust_close(0, fd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 5962 | }else if( rc==SQLITE_CANTOPEN ){ |
| 5963 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5964 | } |
| 5965 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 5966 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5967 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5968 | } |
| 5969 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5970 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5971 | ** Test the existence of or access permissions of file zPath. The |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5972 | ** test performed depends on the value of flags: |
| 5973 | ** |
| 5974 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 5975 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 5976 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 5977 | ** |
| 5978 | ** Otherwise return 0. |
| 5979 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5980 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5981 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 5982 | const char *zPath, /* Path of the file to examine */ |
| 5983 | int flags, /* What do we want to learn about the zPath file? */ |
| 5984 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5985 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 5986 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5987 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5988 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5989 | switch( flags ){ |
| 5990 | case SQLITE_ACCESS_EXISTS: |
| 5991 | amode = F_OK; |
| 5992 | break; |
| 5993 | case SQLITE_ACCESS_READWRITE: |
| 5994 | amode = W_OK|R_OK; |
| 5995 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 5996 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5997 | amode = R_OK; |
| 5998 | break; |
| 5999 | |
| 6000 | default: |
| 6001 | assert(!"Invalid flags argument"); |
| 6002 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6003 | *pResOut = (osAccess(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6004 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 6005 | struct stat buf; |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 6006 | if( 0==osStat(zPath, &buf) && buf.st_size==0 ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6007 | *pResOut = 0; |
| 6008 | } |
| 6009 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6010 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6011 | } |
| 6012 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6013 | |
| 6014 | /* |
| 6015 | ** Turn a relative pathname into a full pathname. The relative path |
| 6016 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 6017 | ** zPath. |
| 6018 | ** |
| 6019 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 6020 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 6021 | ** this buffer before returning. |
| 6022 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 6023 | static int unixFullPathname( |
| 6024 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 6025 | const char *zPath, /* Possibly relative input path */ |
| 6026 | int nOut, /* Size of output buffer in bytes */ |
| 6027 | char *zOut /* Output buffer */ |
| 6028 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 6029 | |
| 6030 | /* It's odd to simulate an io-error here, but really this is just |
| 6031 | ** using the io-error infrastructure to test that SQLite handles this |
| 6032 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6033 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 6034 | */ |
| 6035 | SimulateIOError( return SQLITE_ERROR ); |
| 6036 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6037 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 6038 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6039 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 6040 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6041 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 6042 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6043 | }else{ |
| 6044 | int nCwd; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6045 | if( osGetcwd(zOut, nOut-1)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6046 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6047 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6048 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 6049 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6050 | } |
| 6051 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 6054 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6055 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 6056 | /* |
| 6057 | ** Interfaces for opening a shared library, finding entry points |
| 6058 | ** within the shared library, and closing the shared library. |
| 6059 | */ |
| 6060 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6061 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 6062 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6063 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 6064 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 6065 | |
| 6066 | /* |
| 6067 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 6068 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 6069 | ** message is available, it is written to zBufOut. If no error message |
| 6070 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 6071 | ** error message. |
| 6072 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6073 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 6074 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6075 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6076 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6077 | zErr = dlerror(); |
| 6078 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6079 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6080 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6081 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6082 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6083 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 6084 | /* |
| 6085 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 6086 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 6087 | ** returns a void* which is really a pointer to a function. So how do we |
| 6088 | ** use dlsym() with -pedantic-errors? |
| 6089 | ** |
| 6090 | ** Variable x below is defined to be a pointer to a function taking |
| 6091 | ** parameters void* and const char* and returning a pointer to a function. |
| 6092 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 6093 | ** (That assignment requires a cast.) Then we call the function that |
| 6094 | ** x points to. |
| 6095 | ** |
| 6096 | ** This work-around is unlikely to work correctly on any system where |
| 6097 | ** you really cannot cast a function pointer into void*. But then, on the |
| 6098 | ** other hand, dlsym() will not work on such a system either, so we have |
| 6099 | ** not really lost anything. |
| 6100 | */ |
| 6101 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6102 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6103 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 6104 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6105 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6106 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 6107 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6108 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6109 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6110 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 6111 | #define unixDlOpen 0 |
| 6112 | #define unixDlError 0 |
| 6113 | #define unixDlSym 0 |
| 6114 | #define unixDlClose 0 |
| 6115 | #endif |
| 6116 | |
| 6117 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6118 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6119 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6120 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 6121 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 6122 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6123 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6124 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 6125 | ** errors. The reports issued by valgrind are incorrect - we would |
| 6126 | ** prefer that the randomness be increased by making use of the |
| 6127 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 6128 | ** some users. Rather than argue, it seems easier just to initialize |
| 6129 | ** the whole array and silence valgrind, even if that means less randomness |
| 6130 | ** in the random seed. |
| 6131 | ** |
| 6132 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 6133 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6134 | ** tests repeatable. |
| 6135 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6136 | memset(zBuf, 0, nBuf); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6137 | randomnessPid = osGetpid(0); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6138 | #if !defined(SQLITE_TEST) |
| 6139 | { |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6140 | int fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6141 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6142 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 6143 | time_t t; |
| 6144 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6145 | memcpy(zBuf, &t, sizeof(t)); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6146 | memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); |
| 6147 | assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); |
| 6148 | nBuf = sizeof(t) + sizeof(randomnessPid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6149 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 6150 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6151 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6152 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6153 | } |
| 6154 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 6155 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6156 | } |
| 6157 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6158 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6159 | /* |
| 6160 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6161 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6162 | ** The return value is the number of microseconds of sleep actually |
| 6163 | ** requested from the underlying operating system, a number which |
| 6164 | ** might be greater than or equal to the argument, but not less |
| 6165 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6166 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6167 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6168 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6169 | struct timespec sp; |
| 6170 | |
| 6171 | sp.tv_sec = microseconds / 1000000; |
| 6172 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 6173 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6174 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6175 | return microseconds; |
| 6176 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6177 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6178 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6179 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6180 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6181 | int seconds = (microseconds+999999)/1000000; |
| 6182 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6183 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6184 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 6185 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 6186 | } |
| 6187 | |
| 6188 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6189 | ** The following variable, if set to a non-zero value, is interpreted as |
| 6190 | ** the number of seconds since 1970 and is used to set the result of |
| 6191 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6192 | */ |
| 6193 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6194 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6195 | #endif |
| 6196 | |
| 6197 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6198 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 6199 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 6200 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 6201 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 6202 | ** proleptic Gregorian calendar. |
| 6203 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6204 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 6205 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6206 | */ |
| 6207 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 6208 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6209 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6210 | #if defined(NO_GETTOD) |
| 6211 | time_t t; |
| 6212 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 6213 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6214 | #elif OS_VXWORKS |
| 6215 | struct timespec sNow; |
| 6216 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 6217 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 6218 | #else |
| 6219 | struct timeval sNow; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6220 | if( gettimeofday(&sNow, 0)==0 ){ |
| 6221 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 6222 | }else{ |
| 6223 | rc = SQLITE_ERROR; |
| 6224 | } |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6225 | #endif |
| 6226 | |
| 6227 | #ifdef SQLITE_TEST |
| 6228 | if( sqlite3_current_time ){ |
| 6229 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 6230 | } |
| 6231 | #endif |
| 6232 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6233 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
| 6236 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6237 | ** Find the current time (in Universal Coordinated Time). Write the |
| 6238 | ** current time and date as a Julian Day number into *prNow and |
| 6239 | ** return 0. Return 1 if the time and date cannot be found. |
| 6240 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6241 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 6242 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6243 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 6244 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6245 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 6246 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6247 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6248 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6249 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6250 | /* |
| 6251 | ** We added the xGetLastError() method with the intention of providing |
| 6252 | ** better low-level error messages when operating-system problems come up |
| 6253 | ** during SQLite operation. But so far, none of that has been implemented |
| 6254 | ** in the core. So this routine is never called. For now, it is merely |
| 6255 | ** a place-holder. |
| 6256 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6257 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 6258 | UNUSED_PARAMETER(NotUsed); |
| 6259 | UNUSED_PARAMETER(NotUsed2); |
| 6260 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 6261 | return 0; |
| 6262 | } |
| 6263 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6264 | |
| 6265 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6266 | ************************ End of sqlite3_vfs methods *************************** |
| 6267 | ******************************************************************************/ |
| 6268 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6269 | /****************************************************************************** |
| 6270 | ************************** Begin Proxy Locking ******************************** |
| 6271 | ** |
| 6272 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 6273 | ** other locking methods on secondary lock files. Proxy locking is a |
| 6274 | ** meta-layer over top of the primitive locking implemented above. For |
| 6275 | ** this reason, the division that implements of proxy locking is deferred |
| 6276 | ** until late in the file (here) after all of the other I/O methods have |
| 6277 | ** been defined - so that the primitive locking methods are available |
| 6278 | ** as services to help with the implementation of proxy locking. |
| 6279 | ** |
| 6280 | **** |
| 6281 | ** |
| 6282 | ** The default locking schemes in SQLite use byte-range locks on the |
| 6283 | ** database file to coordinate safe, concurrent access by multiple readers |
| 6284 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 6285 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 6286 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 6287 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 6288 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 6289 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 6290 | ** address in the shared range is taken for a SHARED lock, the entire |
| 6291 | ** shared range is taken for an EXCLUSIVE lock): |
| 6292 | ** |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 6293 | ** PENDING_BYTE 0x40000000 |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6294 | ** RESERVED_BYTE 0x40000001 |
| 6295 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 6296 | ** |
| 6297 | ** This works well on the local file system, but shows a nearly 100x |
| 6298 | ** slowdown in read performance on AFP because the AFP client disables |
| 6299 | ** the read cache when byte-range locks are present. Enabling the read |
| 6300 | ** cache exposes a cache coherency problem that is present on all OS X |
| 6301 | ** supported network file systems. NFS and AFP both observe the |
| 6302 | ** close-to-open semantics for ensuring cache coherency |
| 6303 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 6304 | ** address the requirements for concurrent database access by multiple |
| 6305 | ** readers and writers |
| 6306 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 6307 | ** |
| 6308 | ** To address the performance and cache coherency issues, proxy file locking |
| 6309 | ** changes the way database access is controlled by limiting access to a |
| 6310 | ** single host at a time and moving file locks off of the database file |
| 6311 | ** and onto a proxy file on the local file system. |
| 6312 | ** |
| 6313 | ** |
| 6314 | ** Using proxy locks |
| 6315 | ** ----------------- |
| 6316 | ** |
| 6317 | ** C APIs |
| 6318 | ** |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6319 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6320 | ** <proxy_path> | ":auto:"); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6321 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, |
| 6322 | ** &<proxy_path>); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6323 | ** |
| 6324 | ** |
| 6325 | ** SQL pragmas |
| 6326 | ** |
| 6327 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 6328 | ** PRAGMA [database.]lock_proxy_file |
| 6329 | ** |
| 6330 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 6331 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 6332 | ** a proxy path based on the user's temp dir |
| 6333 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 6334 | ** actual proxy file name is generated from the name and path of the |
| 6335 | ** database file. For example: |
| 6336 | ** |
| 6337 | ** For database path "/Users/me/foo.db" |
| 6338 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 6339 | ** |
| 6340 | ** Once a lock proxy is configured for a database connection, it can not |
| 6341 | ** be removed, however it may be switched to a different proxy path via |
| 6342 | ** the above APIs (assuming the conch file is not being held by another |
| 6343 | ** connection or process). |
| 6344 | ** |
| 6345 | ** |
| 6346 | ** How proxy locking works |
| 6347 | ** ----------------------- |
| 6348 | ** |
| 6349 | ** Proxy file locking relies primarily on two new supporting files: |
| 6350 | ** |
| 6351 | ** * conch file to limit access to the database file to a single host |
| 6352 | ** at a time |
| 6353 | ** |
| 6354 | ** * proxy file to act as a proxy for the advisory locks normally |
| 6355 | ** taken on the database |
| 6356 | ** |
| 6357 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 6358 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 6359 | ** contents and comparing the host's unique host ID (see below) and lock |
| 6360 | ** proxy path against the values stored in the conch. The conch file is |
| 6361 | ** stored in the same directory as the database file and the file name |
| 6362 | ** is patterned after the database file name as ".<databasename>-conch". |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 6363 | ** 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] | 6364 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 6365 | ** lock and the conch file contents is updated with the host ID and proxy |
| 6366 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 6367 | ** is held by another process (with a shared lock), the exclusive lock |
| 6368 | ** will fail and SQLITE_BUSY is returned. |
| 6369 | ** |
| 6370 | ** The proxy file - a single-byte file used for all advisory file locks |
| 6371 | ** normally taken on the database file. This allows for safe sharing |
| 6372 | ** of the database file for multiple readers and writers on the same |
| 6373 | ** host (the conch ensures that they all use the same local lock file). |
| 6374 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6375 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 6376 | ** only taken when the first request to lock database file is made. |
| 6377 | ** This matches the semantics of the traditional locking behavior, where |
| 6378 | ** opening a connection to a database file does not take a lock on it. |
| 6379 | ** The shared lock and an open file descriptor are maintained until |
| 6380 | ** the connection to the database is closed. |
| 6381 | ** |
| 6382 | ** The proxy file and the lock file are never deleted so they only need |
| 6383 | ** to be created the first time they are used. |
| 6384 | ** |
| 6385 | ** Configuration options |
| 6386 | ** --------------------- |
| 6387 | ** |
| 6388 | ** SQLITE_PREFER_PROXY_LOCKING |
| 6389 | ** |
| 6390 | ** Database files accessed on non-local file systems are |
| 6391 | ** automatically configured for proxy locking, lock files are |
| 6392 | ** named automatically using the same logic as |
| 6393 | ** PRAGMA lock_proxy_file=":auto:" |
| 6394 | ** |
| 6395 | ** SQLITE_PROXY_DEBUG |
| 6396 | ** |
| 6397 | ** Enables the logging of error messages during host id file |
| 6398 | ** retrieval and creation |
| 6399 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6400 | ** LOCKPROXYDIR |
| 6401 | ** |
| 6402 | ** Overrides the default directory used for lock proxy files that |
| 6403 | ** are named automatically via the ":auto:" setting |
| 6404 | ** |
| 6405 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6406 | ** |
| 6407 | ** Permissions to use when creating a directory for storing the |
| 6408 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 6409 | ** |
| 6410 | ** |
| 6411 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 6412 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 6413 | ** force proxy locking to be used for every database file opened, and 0 |
| 6414 | ** will force automatic proxy locking to be disabled for all database |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6415 | ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6416 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 6417 | */ |
| 6418 | |
| 6419 | /* |
| 6420 | ** Proxy locking is only available on MacOSX |
| 6421 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6422 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6423 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6424 | /* |
| 6425 | ** The proxyLockingContext has the path and file structures for the remote |
| 6426 | ** and local proxy files in it |
| 6427 | */ |
| 6428 | typedef struct proxyLockingContext proxyLockingContext; |
| 6429 | struct proxyLockingContext { |
| 6430 | unixFile *conchFile; /* Open conch file */ |
| 6431 | char *conchFilePath; /* Name of the conch file */ |
| 6432 | unixFile *lockProxy; /* Open proxy lock file */ |
| 6433 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 6434 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6435 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6436 | int nFails; /* Number of conch taking failures */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6437 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 6438 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 6439 | }; |
| 6440 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6441 | /* |
| 6442 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 6443 | ** which must point to valid, writable memory large enough for a maxLen length |
| 6444 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6445 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6446 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 6447 | int len; |
| 6448 | int dbLen; |
| 6449 | int i; |
| 6450 | |
| 6451 | #ifdef LOCKPROXYDIR |
| 6452 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 6453 | #else |
| 6454 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 6455 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6456 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6457 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6458 | lPath, errno, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6459 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6460 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6461 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6462 | } |
| 6463 | # else |
| 6464 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 6465 | # endif |
| 6466 | #endif |
| 6467 | |
| 6468 | if( lPath[len-1]!='/' ){ |
| 6469 | len = strlcat(lPath, "/", maxLen); |
| 6470 | } |
| 6471 | |
| 6472 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6473 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6474 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6475 | char c = dbPath[i]; |
| 6476 | lPath[i+len] = (c=='/')?'_':c; |
| 6477 | } |
| 6478 | lPath[i+len]='\0'; |
| 6479 | strlcat(lPath, ":auto:", maxLen); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6480 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6481 | return SQLITE_OK; |
| 6482 | } |
| 6483 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6484 | /* |
| 6485 | ** Creates the lock file and any missing directories in lockPath |
| 6486 | */ |
| 6487 | static int proxyCreateLockPath(const char *lockPath){ |
| 6488 | int i, len; |
| 6489 | char buf[MAXPATHLEN]; |
| 6490 | int start = 0; |
| 6491 | |
| 6492 | assert(lockPath!=NULL); |
| 6493 | /* try to create all the intermediate directories */ |
| 6494 | len = (int)strlen(lockPath); |
| 6495 | buf[0] = lockPath[0]; |
| 6496 | for( i=1; i<len; i++ ){ |
| 6497 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 6498 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 6499 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 6500 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 6501 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 6502 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6503 | int err=errno; |
| 6504 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6505 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6506 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6507 | buf, strerror(err), lockPath, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6508 | return err; |
| 6509 | } |
| 6510 | } |
| 6511 | } |
| 6512 | start=i+1; |
| 6513 | } |
| 6514 | buf[i] = lockPath[i]; |
| 6515 | } |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6516 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6517 | return 0; |
| 6518 | } |
| 6519 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6520 | /* |
| 6521 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 6522 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 6523 | ** |
| 6524 | ** The caller is responsible not only for closing the file descriptor |
| 6525 | ** but also for freeing the memory associated with the file descriptor. |
| 6526 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6527 | static int proxyCreateUnixFile( |
| 6528 | const char *path, /* path for the new unixFile */ |
| 6529 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 6530 | int islockfile /* if non zero missing dirs will be created */ |
| 6531 | ) { |
| 6532 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6533 | unixFile *pNew; |
| 6534 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6535 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6536 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6537 | int terrno = 0; |
| 6538 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6539 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6540 | /* 1. first try to open/create the file |
| 6541 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 6542 | ** the parent directories and then try again. |
| 6543 | ** 3. if that fails, try to open the file read-only |
| 6544 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 6545 | */ |
| 6546 | pUnused = findReusableFd(path, openFlags); |
| 6547 | if( pUnused ){ |
| 6548 | fd = pUnused->fd; |
| 6549 | }else{ |
| 6550 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 6551 | if( !pUnused ){ |
| 6552 | return SQLITE_NOMEM; |
| 6553 | } |
| 6554 | } |
| 6555 | if( fd<0 ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6556 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6557 | terrno = errno; |
| 6558 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 6559 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6560 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6561 | } |
| 6562 | } |
| 6563 | } |
| 6564 | if( fd<0 ){ |
| 6565 | openFlags = O_RDONLY; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6566 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6567 | terrno = errno; |
| 6568 | } |
| 6569 | if( fd<0 ){ |
| 6570 | if( islockfile ){ |
| 6571 | return SQLITE_BUSY; |
| 6572 | } |
| 6573 | switch (terrno) { |
| 6574 | case EACCES: |
| 6575 | return SQLITE_PERM; |
| 6576 | case EIO: |
| 6577 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 6578 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6579 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6580 | } |
| 6581 | } |
| 6582 | |
| 6583 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 6584 | if( pNew==NULL ){ |
| 6585 | rc = SQLITE_NOMEM; |
| 6586 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6587 | } |
| 6588 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6589 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6590 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6591 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6592 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6593 | pUnused->fd = fd; |
| 6594 | pUnused->flags = openFlags; |
| 6595 | pNew->pUnused = pUnused; |
| 6596 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6597 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6598 | if( rc==SQLITE_OK ){ |
| 6599 | *ppFile = pNew; |
| 6600 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6601 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6602 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6603 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6604 | sqlite3_free(pNew); |
| 6605 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6606 | return rc; |
| 6607 | } |
| 6608 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6609 | #ifdef SQLITE_TEST |
| 6610 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6611 | int sqlite3_hostid_num = 0; |
| 6612 | #endif |
| 6613 | |
| 6614 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 6615 | |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6616 | #ifdef HAVE_GETHOSTUUID |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6617 | /* Not always defined in the headers as it ought to be */ |
| 6618 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6619 | #endif |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6620 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6621 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 6622 | ** bytes of writable memory. |
| 6623 | */ |
| 6624 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6625 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 6626 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6627 | #ifdef HAVE_GETHOSTUUID |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6628 | { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6629 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6630 | if( gethostuuid(pHostID, &timeout) ){ |
| 6631 | int err = errno; |
| 6632 | if( pError ){ |
| 6633 | *pError = err; |
| 6634 | } |
| 6635 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6636 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6637 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6638 | #else |
| 6639 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6640 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6641 | #ifdef SQLITE_TEST |
| 6642 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6643 | if( sqlite3_hostid_num != 0){ |
| 6644 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 6645 | } |
| 6646 | #endif |
| 6647 | |
| 6648 | return SQLITE_OK; |
| 6649 | } |
| 6650 | |
| 6651 | /* The conch file contains the header, host id and lock file path |
| 6652 | */ |
| 6653 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 6654 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 6655 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 6656 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 6657 | |
| 6658 | /* |
| 6659 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 6660 | ** it back. The newly created file's file descriptor is assigned to the |
| 6661 | ** conch file structure and finally the original conch file descriptor is |
| 6662 | ** closed. Returns zero if successful. |
| 6663 | */ |
| 6664 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 6665 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6666 | unixFile *conchFile = pCtx->conchFile; |
| 6667 | char tPath[MAXPATHLEN]; |
| 6668 | char buf[PROXY_MAXCONCHLEN]; |
| 6669 | char *cPath = pCtx->conchFilePath; |
| 6670 | size_t readLen = 0; |
| 6671 | size_t pathLen = 0; |
| 6672 | char errmsg[64] = ""; |
| 6673 | int fd = -1; |
| 6674 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6675 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6676 | |
| 6677 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 6678 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 6679 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 6680 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6681 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6682 | goto end_breaklock; |
| 6683 | } |
| 6684 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6685 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6686 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6687 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6688 | goto end_breaklock; |
| 6689 | } |
| 6690 | /* write it out to the temporary break file */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6691 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6692 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6693 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6694 | goto end_breaklock; |
| 6695 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6696 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6697 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6698 | goto end_breaklock; |
| 6699 | } |
| 6700 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6701 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6702 | goto end_breaklock; |
| 6703 | } |
| 6704 | rc = 0; |
| 6705 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6706 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6707 | conchFile->h = fd; |
| 6708 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 6709 | |
| 6710 | end_breaklock: |
| 6711 | if( rc ){ |
| 6712 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6713 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6714 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6715 | } |
| 6716 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 6717 | } |
| 6718 | return rc; |
| 6719 | } |
| 6720 | |
| 6721 | /* Take the requested lock on the conch file and break a stale lock if the |
| 6722 | ** host id matches. |
| 6723 | */ |
| 6724 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 6725 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6726 | unixFile *conchFile = pCtx->conchFile; |
| 6727 | int rc = SQLITE_OK; |
| 6728 | int nTries = 0; |
| 6729 | struct timespec conchModTime; |
| 6730 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6731 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6732 | do { |
| 6733 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6734 | nTries ++; |
| 6735 | if( rc==SQLITE_BUSY ){ |
| 6736 | /* If the lock failed (busy): |
| 6737 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 6738 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 6739 | * 10 sec and try again |
| 6740 | * 3rd try: break the lock unless the mod time has changed. |
| 6741 | */ |
| 6742 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6743 | if( osFstat(conchFile->h, &buf) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6744 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6745 | return SQLITE_IOERR_LOCK; |
| 6746 | } |
| 6747 | |
| 6748 | if( nTries==1 ){ |
| 6749 | conchModTime = buf.st_mtimespec; |
| 6750 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 6751 | continue; |
| 6752 | } |
| 6753 | |
| 6754 | assert( nTries>1 ); |
| 6755 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 6756 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 6757 | return SQLITE_BUSY; |
| 6758 | } |
| 6759 | |
| 6760 | if( nTries==2 ){ |
| 6761 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6762 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6763 | if( len<0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6764 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6765 | return SQLITE_IOERR_LOCK; |
| 6766 | } |
| 6767 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 6768 | /* don't break the lock if the host id doesn't match */ |
| 6769 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 6770 | return SQLITE_BUSY; |
| 6771 | } |
| 6772 | }else{ |
| 6773 | /* don't break the lock on short read or a version mismatch */ |
| 6774 | return SQLITE_BUSY; |
| 6775 | } |
| 6776 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 6777 | continue; |
| 6778 | } |
| 6779 | |
| 6780 | assert( nTries==3 ); |
| 6781 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 6782 | rc = SQLITE_OK; |
| 6783 | if( lockType==EXCLUSIVE_LOCK ){ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 6784 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6785 | } |
| 6786 | if( !rc ){ |
| 6787 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6788 | } |
| 6789 | } |
| 6790 | } |
| 6791 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 6792 | |
| 6793 | return rc; |
| 6794 | } |
| 6795 | |
| 6796 | /* 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] | 6797 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 6798 | ** lockPath means that the lockPath in the conch file will be used if the |
| 6799 | ** host IDs match, or a new lock path will be generated automatically |
| 6800 | ** and written to the conch file. |
| 6801 | */ |
| 6802 | static int proxyTakeConch(unixFile *pFile){ |
| 6803 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6804 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6805 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6806 | return SQLITE_OK; |
| 6807 | }else{ |
| 6808 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6809 | uuid_t myHostID; |
| 6810 | int pError = 0; |
| 6811 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6812 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6813 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6814 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6815 | int createConch = 0; |
| 6816 | int hostIdMatch = 0; |
| 6817 | int readLen = 0; |
| 6818 | int tryOldLockPath = 0; |
| 6819 | int forceNewLockPath = 0; |
| 6820 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6821 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 6822 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6823 | osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6824 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6825 | rc = proxyGetHostID(myHostID, &pError); |
| 6826 | if( (rc&0xff)==SQLITE_IOERR ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6827 | storeLastErrno(pFile, pError); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6828 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6829 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6830 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6831 | if( rc!=SQLITE_OK ){ |
| 6832 | goto end_takeconch; |
| 6833 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6834 | /* read the existing conch file */ |
| 6835 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 6836 | if( readLen<0 ){ |
| 6837 | /* I/O error: lastErrno set by seekAndRead */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6838 | storeLastErrno(pFile, conchFile->lastErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6839 | rc = SQLITE_IOERR_READ; |
| 6840 | goto end_takeconch; |
| 6841 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 6842 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 6843 | /* a short read or version format mismatch means we need to create a new |
| 6844 | ** conch file. |
| 6845 | */ |
| 6846 | createConch = 1; |
| 6847 | } |
| 6848 | /* if the host id matches and the lock path already exists in the conch |
| 6849 | ** we'll try to use the path there, if we can't open that path, we'll |
| 6850 | ** retry with a new auto-generated path |
| 6851 | */ |
| 6852 | do { /* in case we need to try again for an :auto: named lock file */ |
| 6853 | |
| 6854 | if( !createConch && !forceNewLockPath ){ |
| 6855 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 6856 | PROXY_HOSTIDLEN); |
| 6857 | /* if the conch has data compare the contents */ |
| 6858 | if( !pCtx->lockProxyPath ){ |
| 6859 | /* for auto-named local lock file, just check the host ID and we'll |
| 6860 | ** use the local lock file path that's already in there |
| 6861 | */ |
| 6862 | if( hostIdMatch ){ |
| 6863 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 6864 | |
| 6865 | if( pathLen>=MAXPATHLEN ){ |
| 6866 | pathLen=MAXPATHLEN-1; |
| 6867 | } |
| 6868 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 6869 | lockPath[pathLen] = 0; |
| 6870 | tempLockPath = lockPath; |
| 6871 | tryOldLockPath = 1; |
| 6872 | /* create a copy of the lock path if the conch is taken */ |
| 6873 | goto end_takeconch; |
| 6874 | } |
| 6875 | }else if( hostIdMatch |
| 6876 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 6877 | readLen-PROXY_PATHINDEX) |
| 6878 | ){ |
| 6879 | /* conch host and lock path match */ |
| 6880 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6881 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 6885 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 6886 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6887 | goto end_takeconch; |
| 6888 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6889 | |
| 6890 | /* 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] | 6891 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6892 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 6893 | tempLockPath = lockPath; |
| 6894 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6895 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6896 | |
| 6897 | /* update conch with host and path (this will fail if other process |
| 6898 | ** has a shared lock already), if the host id matches, use the big |
| 6899 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6900 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6901 | futimes(conchFile->h, NULL); |
| 6902 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 6903 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6904 | /* We are trying for an exclusive lock but another thread in this |
| 6905 | ** same process is still holding a shared lock. */ |
| 6906 | rc = SQLITE_BUSY; |
| 6907 | } else { |
| 6908 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6909 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6910 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6911 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6912 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6913 | if( rc==SQLITE_OK ){ |
| 6914 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 6915 | int writeSize = 0; |
| 6916 | |
| 6917 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 6918 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 6919 | if( pCtx->lockProxyPath!=NULL ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6920 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, |
| 6921 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6922 | }else{ |
| 6923 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 6924 | } |
| 6925 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6926 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6927 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 6928 | fsync(conchFile->h); |
| 6929 | /* If we created a new conch file (not just updated the contents of a |
| 6930 | ** valid conch file), try to match the permissions of the database |
| 6931 | */ |
| 6932 | if( rc==SQLITE_OK && createConch ){ |
| 6933 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6934 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6935 | if( err==0 ){ |
| 6936 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 6937 | S_IROTH|S_IWOTH); |
| 6938 | /* try to match the database file R/W permissions, ignore failure */ |
| 6939 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6940 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6941 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6942 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6943 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6944 | }while( rc==(-1) && errno==EINTR ); |
| 6945 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6946 | int code = errno; |
| 6947 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 6948 | cmode, code, strerror(code)); |
| 6949 | } else { |
| 6950 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 6951 | } |
| 6952 | }else{ |
| 6953 | int code = errno; |
| 6954 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 6955 | err, code, strerror(code)); |
| 6956 | #endif |
| 6957 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6958 | } |
| 6959 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6960 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6961 | |
| 6962 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6963 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6964 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6965 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6966 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 6967 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6968 | } |
| 6969 | pFile->h = -1; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6970 | fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6971 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6972 | if( fd>=0 ){ |
| 6973 | pFile->h = fd; |
| 6974 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6975 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6976 | during locking */ |
| 6977 | } |
| 6978 | } |
| 6979 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 6980 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 6981 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 6982 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 6983 | /* we couldn't create the proxy lock file with the old lock file path |
| 6984 | ** so try again via auto-naming |
| 6985 | */ |
| 6986 | forceNewLockPath = 1; |
| 6987 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 6988 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6989 | } |
| 6990 | } |
| 6991 | if( rc==SQLITE_OK ){ |
| 6992 | /* Need to make a copy of path if we extracted the value |
| 6993 | ** from the conch file or the path was allocated on the stack |
| 6994 | */ |
| 6995 | if( tempLockPath ){ |
| 6996 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 6997 | if( !pCtx->lockProxyPath ){ |
| 6998 | rc = SQLITE_NOMEM; |
| 6999 | } |
| 7000 | } |
| 7001 | } |
| 7002 | if( rc==SQLITE_OK ){ |
| 7003 | pCtx->conchHeld = 1; |
| 7004 | |
| 7005 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 7006 | afpLockingContext *afpCtx; |
| 7007 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 7008 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 7009 | } |
| 7010 | } else { |
| 7011 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7012 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7013 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 7014 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7015 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7016 | } while (1); /* in case we need to retry the :auto: lock file - |
| 7017 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7018 | } |
| 7019 | } |
| 7020 | |
| 7021 | /* |
| 7022 | ** If pFile holds a lock on a conch file, then release that lock. |
| 7023 | */ |
| 7024 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 7025 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7026 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 7027 | unixFile *conchFile; /* Name of the conch file */ |
| 7028 | |
| 7029 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7030 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7031 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7032 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7033 | osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7034 | if( pCtx->conchHeld>0 ){ |
| 7035 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7036 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7037 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7038 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 7039 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7040 | return rc; |
| 7041 | } |
| 7042 | |
| 7043 | /* |
| 7044 | ** Given the name of a database file, compute the name of its conch file. |
| 7045 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 7046 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 7047 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 7048 | ** |
| 7049 | ** The caller is responsible for ensuring that the allocated memory |
| 7050 | ** space is eventually freed. |
| 7051 | ** |
| 7052 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 7053 | */ |
| 7054 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 7055 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7056 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7057 | char *conchPath; /* buffer in which to construct conch name */ |
| 7058 | |
| 7059 | /* Allocate space for the conch filename and initialize the name to |
| 7060 | ** the name of the original database file. */ |
| 7061 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 7062 | if( conchPath==0 ){ |
| 7063 | return SQLITE_NOMEM; |
| 7064 | } |
| 7065 | memcpy(conchPath, dbPath, len+1); |
| 7066 | |
| 7067 | /* now insert a "." before the last / character */ |
| 7068 | for( i=(len-1); i>=0; i-- ){ |
| 7069 | if( conchPath[i]=='/' ){ |
| 7070 | i++; |
| 7071 | break; |
| 7072 | } |
| 7073 | } |
| 7074 | conchPath[i]='.'; |
| 7075 | while ( i<len ){ |
| 7076 | conchPath[i+1]=dbPath[i]; |
| 7077 | i++; |
| 7078 | } |
| 7079 | |
| 7080 | /* append the "-conch" suffix to the file */ |
| 7081 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7082 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7083 | |
| 7084 | return SQLITE_OK; |
| 7085 | } |
| 7086 | |
| 7087 | |
| 7088 | /* Takes a fully configured proxy locking-style unix file and switches |
| 7089 | ** the local lock file path |
| 7090 | */ |
| 7091 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 7092 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7093 | char *oldPath = pCtx->lockProxyPath; |
| 7094 | int rc = SQLITE_OK; |
| 7095 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7096 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7097 | return SQLITE_BUSY; |
| 7098 | } |
| 7099 | |
| 7100 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 7101 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 7102 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 7103 | return SQLITE_OK; |
| 7104 | }else{ |
| 7105 | unixFile *lockProxy = pCtx->lockProxy; |
| 7106 | pCtx->lockProxy=NULL; |
| 7107 | pCtx->conchHeld = 0; |
| 7108 | if( lockProxy!=NULL ){ |
| 7109 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 7110 | if( rc ) return rc; |
| 7111 | sqlite3_free(lockProxy); |
| 7112 | } |
| 7113 | sqlite3_free(oldPath); |
| 7114 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 7115 | } |
| 7116 | |
| 7117 | return rc; |
| 7118 | } |
| 7119 | |
| 7120 | /* |
| 7121 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 7122 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 7123 | ** |
| 7124 | ** This routine find the filename associated with pFile and writes it |
| 7125 | ** int dbPath. |
| 7126 | */ |
| 7127 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7128 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7129 | if( pFile->pMethod == &afpIoMethods ){ |
| 7130 | /* afp style keeps a reference to the db path in the filePath field |
| 7131 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7132 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7133 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, |
| 7134 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7135 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7136 | #endif |
| 7137 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 7138 | /* dot lock style uses the locking context to store the dot lock |
| 7139 | ** file path */ |
| 7140 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 7141 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 7142 | }else{ |
| 7143 | /* all other styles use the locking context to store the db file path */ |
| 7144 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7145 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7146 | } |
| 7147 | return SQLITE_OK; |
| 7148 | } |
| 7149 | |
| 7150 | /* |
| 7151 | ** Takes an already filled in unix file and alters it so all file locking |
| 7152 | ** will be performed on the local proxy lock file. The following fields |
| 7153 | ** are preserved in the locking context so that they can be restored and |
| 7154 | ** the unix structure properly cleaned up at close time: |
| 7155 | ** ->lockingContext |
| 7156 | ** ->pMethod |
| 7157 | */ |
| 7158 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 7159 | proxyLockingContext *pCtx; |
| 7160 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 7161 | char *lockPath=NULL; |
| 7162 | int rc = SQLITE_OK; |
| 7163 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7164 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7165 | return SQLITE_BUSY; |
| 7166 | } |
| 7167 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 7168 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 7169 | lockPath=NULL; |
| 7170 | }else{ |
| 7171 | lockPath=(char *)path; |
| 7172 | } |
| 7173 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7174 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7175 | (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7176 | |
| 7177 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 7178 | if( pCtx==0 ){ |
| 7179 | return SQLITE_NOMEM; |
| 7180 | } |
| 7181 | memset(pCtx, 0, sizeof(*pCtx)); |
| 7182 | |
| 7183 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 7184 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7185 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 7186 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 7187 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 7188 | ** (c) the file system is read-only, then enable no-locking access. |
| 7189 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 7190 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 7191 | */ |
| 7192 | struct statfs fsInfo; |
| 7193 | struct stat conchInfo; |
| 7194 | int goLockless = 0; |
| 7195 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7196 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7197 | int err = errno; |
| 7198 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 7199 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 7200 | } |
| 7201 | } |
| 7202 | if( goLockless ){ |
| 7203 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 7204 | rc = SQLITE_OK; |
| 7205 | } |
| 7206 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7207 | } |
| 7208 | if( rc==SQLITE_OK && lockPath ){ |
| 7209 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 7210 | } |
| 7211 | |
| 7212 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7213 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 7214 | if( pCtx->dbPath==NULL ){ |
| 7215 | rc = SQLITE_NOMEM; |
| 7216 | } |
| 7217 | } |
| 7218 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7219 | /* all memory is allocated, proxys are created and assigned, |
| 7220 | ** switch the locking context and pMethod then return. |
| 7221 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7222 | pCtx->oldLockingContext = pFile->lockingContext; |
| 7223 | pFile->lockingContext = pCtx; |
| 7224 | pCtx->pOldMethod = pFile->pMethod; |
| 7225 | pFile->pMethod = &proxyIoMethods; |
| 7226 | }else{ |
| 7227 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7228 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7229 | sqlite3_free(pCtx->conchFile); |
| 7230 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7231 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7232 | sqlite3_free(pCtx->conchFilePath); |
| 7233 | sqlite3_free(pCtx); |
| 7234 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7235 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 7236 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7237 | return rc; |
| 7238 | } |
| 7239 | |
| 7240 | |
| 7241 | /* |
| 7242 | ** This routine handles sqlite3_file_control() calls that are specific |
| 7243 | ** to proxy locking. |
| 7244 | */ |
| 7245 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 7246 | switch( op ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7247 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7248 | unixFile *pFile = (unixFile*)id; |
| 7249 | if( pFile->pMethod == &proxyIoMethods ){ |
| 7250 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7251 | proxyTakeConch(pFile); |
| 7252 | if( pCtx->lockProxyPath ){ |
| 7253 | *(const char **)pArg = pCtx->lockProxyPath; |
| 7254 | }else{ |
| 7255 | *(const char **)pArg = ":auto: (not held)"; |
| 7256 | } |
| 7257 | } else { |
| 7258 | *(const char **)pArg = NULL; |
| 7259 | } |
| 7260 | return SQLITE_OK; |
| 7261 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7262 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7263 | unixFile *pFile = (unixFile*)id; |
| 7264 | int rc = SQLITE_OK; |
| 7265 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 7266 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 7267 | if( isProxyStyle ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7268 | /* turn off proxy locking - not supported. If support is added for |
| 7269 | ** switching proxy locking mode off then it will need to fail if |
| 7270 | ** the journal mode is WAL mode. |
| 7271 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7272 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 7273 | }else{ |
| 7274 | /* turn off proxy locking - already off - NOOP */ |
| 7275 | rc = SQLITE_OK; |
| 7276 | } |
| 7277 | }else{ |
| 7278 | const char *proxyPath = (const char *)pArg; |
| 7279 | if( isProxyStyle ){ |
| 7280 | proxyLockingContext *pCtx = |
| 7281 | (proxyLockingContext*)pFile->lockingContext; |
| 7282 | if( !strcmp(pArg, ":auto:") |
| 7283 | || (pCtx->lockProxyPath && |
| 7284 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 7285 | ){ |
| 7286 | rc = SQLITE_OK; |
| 7287 | }else{ |
| 7288 | rc = switchLockProxyPath(pFile, proxyPath); |
| 7289 | } |
| 7290 | }else{ |
| 7291 | /* turn on proxy file locking */ |
| 7292 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 7293 | } |
| 7294 | } |
| 7295 | return rc; |
| 7296 | } |
| 7297 | default: { |
| 7298 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 7299 | } |
| 7300 | } |
| 7301 | /*NOTREACHED*/ |
| 7302 | return SQLITE_ERROR; |
| 7303 | } |
| 7304 | |
| 7305 | /* |
| 7306 | ** Within this division (the proxying locking implementation) the procedures |
| 7307 | ** above this point are all utilities. The lock-related methods of the |
| 7308 | ** proxy-locking sqlite3_io_method object follow. |
| 7309 | */ |
| 7310 | |
| 7311 | |
| 7312 | /* |
| 7313 | ** This routine checks if there is a RESERVED lock held on the specified |
| 7314 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 7315 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 7316 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 7317 | */ |
| 7318 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 7319 | unixFile *pFile = (unixFile*)id; |
| 7320 | int rc = proxyTakeConch(pFile); |
| 7321 | if( rc==SQLITE_OK ){ |
| 7322 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7323 | if( pCtx->conchHeld>0 ){ |
| 7324 | unixFile *proxy = pCtx->lockProxy; |
| 7325 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 7326 | }else{ /* conchHeld < 0 is lockless */ |
| 7327 | pResOut=0; |
| 7328 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7329 | } |
| 7330 | return rc; |
| 7331 | } |
| 7332 | |
| 7333 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7334 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7335 | ** of the following: |
| 7336 | ** |
| 7337 | ** (1) SHARED_LOCK |
| 7338 | ** (2) RESERVED_LOCK |
| 7339 | ** (3) PENDING_LOCK |
| 7340 | ** (4) EXCLUSIVE_LOCK |
| 7341 | ** |
| 7342 | ** Sometimes when requesting one lock state, additional lock states |
| 7343 | ** are inserted in between. The locking might fail on one of the later |
| 7344 | ** transitions leaving the lock state different from what it started but |
| 7345 | ** still short of its goal. The following chart shows the allowed |
| 7346 | ** transitions and the inserted intermediate states: |
| 7347 | ** |
| 7348 | ** UNLOCKED -> SHARED |
| 7349 | ** SHARED -> RESERVED |
| 7350 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 7351 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 7352 | ** PENDING -> EXCLUSIVE |
| 7353 | ** |
| 7354 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 7355 | ** routine to lower a locking level. |
| 7356 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7357 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7358 | unixFile *pFile = (unixFile*)id; |
| 7359 | int rc = proxyTakeConch(pFile); |
| 7360 | if( rc==SQLITE_OK ){ |
| 7361 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7362 | if( pCtx->conchHeld>0 ){ |
| 7363 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7364 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 7365 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7366 | }else{ |
| 7367 | /* conchHeld < 0 is lockless */ |
| 7368 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7369 | } |
| 7370 | return rc; |
| 7371 | } |
| 7372 | |
| 7373 | |
| 7374 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7375 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7376 | ** must be either NO_LOCK or SHARED_LOCK. |
| 7377 | ** |
| 7378 | ** If the locking level of the file descriptor is already at or below |
| 7379 | ** the requested locking level, this routine is a no-op. |
| 7380 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7381 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7382 | unixFile *pFile = (unixFile*)id; |
| 7383 | int rc = proxyTakeConch(pFile); |
| 7384 | if( rc==SQLITE_OK ){ |
| 7385 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7386 | if( pCtx->conchHeld>0 ){ |
| 7387 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7388 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 7389 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7390 | }else{ |
| 7391 | /* conchHeld < 0 is lockless */ |
| 7392 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7393 | } |
| 7394 | return rc; |
| 7395 | } |
| 7396 | |
| 7397 | /* |
| 7398 | ** Close a file that uses proxy locks. |
| 7399 | */ |
| 7400 | static int proxyClose(sqlite3_file *id) { |
| 7401 | if( id ){ |
| 7402 | unixFile *pFile = (unixFile*)id; |
| 7403 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7404 | unixFile *lockProxy = pCtx->lockProxy; |
| 7405 | unixFile *conchFile = pCtx->conchFile; |
| 7406 | int rc = SQLITE_OK; |
| 7407 | |
| 7408 | if( lockProxy ){ |
| 7409 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 7410 | if( rc ) return rc; |
| 7411 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 7412 | if( rc ) return rc; |
| 7413 | sqlite3_free(lockProxy); |
| 7414 | pCtx->lockProxy = 0; |
| 7415 | } |
| 7416 | if( conchFile ){ |
| 7417 | if( pCtx->conchHeld ){ |
| 7418 | rc = proxyReleaseConch(pFile); |
| 7419 | if( rc ) return rc; |
| 7420 | } |
| 7421 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 7422 | if( rc ) return rc; |
| 7423 | sqlite3_free(conchFile); |
| 7424 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7425 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7426 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7427 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7428 | /* restore the original locking context and pMethod then close it */ |
| 7429 | pFile->lockingContext = pCtx->oldLockingContext; |
| 7430 | pFile->pMethod = pCtx->pOldMethod; |
| 7431 | sqlite3_free(pCtx); |
| 7432 | return pFile->pMethod->xClose(id); |
| 7433 | } |
| 7434 | return SQLITE_OK; |
| 7435 | } |
| 7436 | |
| 7437 | |
| 7438 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7439 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7440 | /* |
| 7441 | ** The proxy locking style is intended for use with AFP filesystems. |
| 7442 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 7443 | ** restricted to MacOSX. |
| 7444 | ** |
| 7445 | ** |
| 7446 | ******************* End of the proxy lock implementation ********************** |
| 7447 | ******************************************************************************/ |
| 7448 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7449 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7450 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7451 | ** |
| 7452 | ** This routine registers all VFS implementations for unix-like operating |
| 7453 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 7454 | ** should be the only routines in this file that are visible from other |
| 7455 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7456 | ** |
| 7457 | ** This routine is called once during SQLite initialization and by a |
| 7458 | ** single thread. The memory allocation and mutex subsystems have not |
| 7459 | ** necessarily been initialized when this routine is called, and so they |
| 7460 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7461 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7462 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7463 | /* |
| 7464 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7465 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 7466 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 7467 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 7468 | ** and so we have to go through the intermediate pointer to avoid problems |
| 7469 | ** when compiling with -pedantic-errors on GCC.) |
| 7470 | ** |
| 7471 | ** 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] | 7472 | ** finder-function. The finder-function returns a pointer to the |
| 7473 | ** sqlite_io_methods object that implements the desired locking |
| 7474 | ** behaviors. See the division above that contains the IOMETHODS |
| 7475 | ** macro for addition information on finder-functions. |
| 7476 | ** |
| 7477 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 7478 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 7479 | ** more than that; it looks at the filesystem type that hosts the |
| 7480 | ** database file and tries to choose an locking method appropriate for |
| 7481 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7482 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7483 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7484 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7485 | sizeof(unixFile), /* szOsFile */ \ |
| 7486 | MAX_PATHNAME, /* mxPathname */ \ |
| 7487 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7488 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7489 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7490 | unixOpen, /* xOpen */ \ |
| 7491 | unixDelete, /* xDelete */ \ |
| 7492 | unixAccess, /* xAccess */ \ |
| 7493 | unixFullPathname, /* xFullPathname */ \ |
| 7494 | unixDlOpen, /* xDlOpen */ \ |
| 7495 | unixDlError, /* xDlError */ \ |
| 7496 | unixDlSym, /* xDlSym */ \ |
| 7497 | unixDlClose, /* xDlClose */ \ |
| 7498 | unixRandomness, /* xRandomness */ \ |
| 7499 | unixSleep, /* xSleep */ \ |
| 7500 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 7501 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 7502 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7503 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 7504 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 7505 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7506 | } |
| 7507 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7508 | /* |
| 7509 | ** All default VFSes for unix are contained in the following array. |
| 7510 | ** |
| 7511 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 7512 | ** by the SQLite core when the VFS is registered. So the following |
| 7513 | ** array cannot be const. |
| 7514 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7515 | static sqlite3_vfs aVfs[] = { |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7516 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7517 | UNIXVFS("unix", autolockIoFinder ), |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7518 | #elif OS_VXWORKS |
| 7519 | UNIXVFS("unix", vxworksIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7520 | #else |
| 7521 | UNIXVFS("unix", posixIoFinder ), |
| 7522 | #endif |
| 7523 | UNIXVFS("unix-none", nolockIoFinder ), |
| 7524 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7525 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7526 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7527 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7528 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7529 | #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7530 | UNIXVFS("unix-posix", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7531 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7532 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 7533 | UNIXVFS("unix-flock", flockIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7534 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7535 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7536 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7537 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7538 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7539 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7540 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7541 | unsigned int i; /* Loop counter */ |
| 7542 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7543 | /* Double-check that the aSyscall[] array has been constructed |
| 7544 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 7545 | assert( ArraySize(aSyscall)==25 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7546 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7547 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7548 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7549 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7550 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7551 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7552 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7553 | |
| 7554 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7555 | ** Shutdown the operating system interface. |
| 7556 | ** |
| 7557 | ** Some operating systems might need to do some cleanup in this routine, |
| 7558 | ** to release dynamically allocated objects. But not on unix. |
| 7559 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7560 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7561 | int sqlite3_os_end(void){ |
| 7562 | return SQLITE_OK; |
| 7563 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 7564 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 7565 | #endif /* SQLITE_OS_UNIX */ |