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 | ** |
| 13 | ** This file contains code that is specific to Unix systems. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 14 | ** |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 15 | ** $Id: os_unix.c,v 1.201 2008/09/15 04:20:32 danielk1977 Exp $ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 16 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 17 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 18 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 19 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 20 | /* |
| 21 | ** If SQLITE_ENABLE_LOCKING_STYLE is defined, then several different |
| 22 | ** locking implementations are provided: |
| 23 | ** |
| 24 | ** * POSIX locking (the default), |
| 25 | ** * No locking, |
| 26 | ** * Dot-file locking, |
| 27 | ** * flock() locking, |
| 28 | ** * AFP locking (OSX only). |
| 29 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 30 | /* #define SQLITE_ENABLE_LOCKING_STYLE 0 */ |
| 31 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 32 | /* |
| 33 | ** These #defines should enable >2GB file support on Posix if the |
| 34 | ** underlying operating system supports it. If the OS lacks |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 35 | ** large file support, these should be no-ops. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 36 | ** |
| 37 | ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |
| 38 | ** on the compiler command line. This is necessary if you are compiling |
| 39 | ** on a recent machine (ex: RedHat 7.2) but you want your code to work |
| 40 | ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 |
| 41 | ** without this option, LFS is enable. But LFS does not exist in the kernel |
| 42 | ** in RedHat 6.0, so the code won't work. Hence, for maximum binary |
| 43 | ** portability you should omit LFS. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 44 | */ |
| 45 | #ifndef SQLITE_DISABLE_LFS |
| 46 | # define _LARGE_FILE 1 |
| 47 | # ifndef _FILE_OFFSET_BITS |
| 48 | # define _FILE_OFFSET_BITS 64 |
| 49 | # endif |
| 50 | # define _LARGEFILE_SOURCE 1 |
| 51 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 52 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 53 | /* |
| 54 | ** standard include files. |
| 55 | */ |
| 56 | #include <sys/types.h> |
| 57 | #include <sys/stat.h> |
| 58 | #include <fcntl.h> |
| 59 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 60 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 61 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 62 | #include <errno.h> |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 63 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 64 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 65 | #include <sys/ioctl.h> |
| 66 | #include <sys/param.h> |
| 67 | #include <sys/mount.h> |
| 68 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 71 | ** If we are to be thread-safe, include the pthreads header and define |
| 72 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 73 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 74 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 75 | # include <pthread.h> |
| 76 | # define SQLITE_UNIX_THREADS 1 |
| 77 | #endif |
| 78 | |
| 79 | /* |
| 80 | ** Default permissions when creating a new file |
| 81 | */ |
| 82 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 83 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 84 | #endif |
| 85 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 86 | /* |
| 87 | ** Maximum supported path-length. |
| 88 | */ |
| 89 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 93 | ** The unixFile structure is subclass of sqlite3_file specific for the unix |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 94 | ** protability layer. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 95 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 96 | typedef struct unixFile unixFile; |
| 97 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 98 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 99 | #ifdef SQLITE_TEST |
| 100 | /* In test mode, increase the size of this structure a bit so that |
| 101 | ** it is larger than the struct CrashFile defined in test6.c. |
| 102 | */ |
| 103 | char aPadding[32]; |
| 104 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 105 | struct openCnt *pOpen; /* Info about all open fd's on this inode */ |
| 106 | struct lockInfo *pLock; /* Info about locks on this inode */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 107 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 108 | void *lockingContext; /* Locking style specific state */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 109 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 110 | int h; /* The file descriptor */ |
| 111 | unsigned char locktype; /* The type of lock held on this fd */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 112 | int dirfd; /* File descriptor for the directory */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 113 | #if SQLITE_THREADSAFE |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 114 | pthread_t tid; /* The thread that "owns" this unixFile */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 115 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 116 | int lastErrno; /* The unix errno from the last I/O error */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 119 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 120 | ** Include code that is common to all os_*.c files |
| 121 | */ |
| 122 | #include "os_common.h" |
| 123 | |
| 124 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 125 | ** Define various macros that are missing from some systems. |
| 126 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 127 | #ifndef O_LARGEFILE |
| 128 | # define O_LARGEFILE 0 |
| 129 | #endif |
| 130 | #ifdef SQLITE_DISABLE_LFS |
| 131 | # undef O_LARGEFILE |
| 132 | # define O_LARGEFILE 0 |
| 133 | #endif |
| 134 | #ifndef O_NOFOLLOW |
| 135 | # define O_NOFOLLOW 0 |
| 136 | #endif |
| 137 | #ifndef O_BINARY |
| 138 | # define O_BINARY 0 |
| 139 | #endif |
| 140 | |
| 141 | /* |
| 142 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 143 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 144 | ** that always succeeds. This means that locking does not occur under |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 145 | ** DJGPP. But it is DOS - what did you expect? |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 146 | */ |
| 147 | #ifdef __DJGPP__ |
| 148 | # define fcntl(A,B,C) 0 |
| 149 | #endif |
| 150 | |
| 151 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 152 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 153 | ** testing and debugging only. |
| 154 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 155 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 156 | #define threadid pthread_self() |
| 157 | #else |
| 158 | #define threadid 0 |
| 159 | #endif |
| 160 | |
| 161 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 162 | ** Set or check the unixFile.tid field. This field is set when an unixFile |
| 163 | ** is first opened. All subsequent uses of the unixFile verify that the |
| 164 | ** same thread is operating on the unixFile. Some operating systems do |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 165 | ** not allow locks to be overridden by other threads and that restriction |
| 166 | ** means that sqlite3* database handles cannot be moved from one thread |
| 167 | ** to another. This logic makes sure a user does not try to do that |
| 168 | ** by mistake. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 169 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 170 | ** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 171 | ** another as long as we are running on a system that supports threads |
| 172 | ** overriding each others locks (which now the most common behavior) |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 173 | ** or if no locks are held. But the unixFile.pLock field needs to be |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 174 | ** recomputed because its key includes the thread-id. See the |
| 175 | ** transferOwnership() function below for additional information |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 176 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 177 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 178 | # define SET_THREADID(X) (X)->tid = pthread_self() |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 179 | # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \ |
| 180 | !pthread_equal((X)->tid, pthread_self())) |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 181 | #else |
| 182 | # define SET_THREADID(X) |
| 183 | # define CHECK_THREADID(X) 0 |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 184 | #endif |
| 185 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 186 | /* |
| 187 | ** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996) |
| 188 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 189 | ** sets or clears a lock, that operation overrides any prior locks set |
| 190 | ** by the same process. It does not explicitly say so, but this implies |
| 191 | ** that it overrides locks set by the same process using a different |
| 192 | ** file descriptor. Consider this test case: |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 193 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 194 | ** |
| 195 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 196 | ** one is a hard or symbolic link to the other) then if you set |
| 197 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 198 | ** on fd2, it works. I would have expected the second lock to |
| 199 | ** fail since there was already a lock on the file due to fd1. |
| 200 | ** But not so. Since both locks came from the same process, the |
| 201 | ** second overrides the first, even though they were on different |
| 202 | ** file descriptors opened on different file names. |
| 203 | ** |
| 204 | ** Bummer. If you ask me, this is broken. Badly broken. It means |
| 205 | ** that we cannot use POSIX locks to synchronize file access among |
| 206 | ** competing threads of the same process. POSIX locks will work fine |
| 207 | ** to synchronize access for threads in separate processes, but not |
| 208 | ** threads within the same process. |
| 209 | ** |
| 210 | ** To work around the problem, SQLite has to manage file locks internally |
| 211 | ** on its own. Whenever a new database is opened, we have to find the |
| 212 | ** specific inode of the database file (the inode is determined by the |
| 213 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 214 | ** and check for locks already existing on that inode. When locks are |
| 215 | ** created or removed, we have to look at our own internal record of the |
| 216 | ** locks to see if another thread has previously set a lock on that same |
| 217 | ** inode. |
| 218 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 219 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 220 | ** descriptor. It is now a structure that holds the integer file |
| 221 | ** descriptor and a pointer to a structure that describes the internal |
| 222 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 223 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 224 | ** point to the same locking structure. The locking structure keeps |
| 225 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 226 | ** field that tells us its internal lock status. cnt==0 means the |
| 227 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 228 | ** cnt>0 means there are cnt shared locks on the file. |
| 229 | ** |
| 230 | ** Any attempt to lock or unlock a file first checks the locking |
| 231 | ** structure. The fcntl() system call is only invoked to set a |
| 232 | ** POSIX lock if the internal lock structure transitions between |
| 233 | ** a locked and an unlocked state. |
| 234 | ** |
| 235 | ** 2004-Jan-11: |
| 236 | ** More recent discoveries about POSIX advisory locks. (The more |
| 237 | ** I discover, the more I realize the a POSIX advisory locks are |
| 238 | ** an abomination.) |
| 239 | ** |
| 240 | ** If you close a file descriptor that points to a file that has locks, |
| 241 | ** all locks on that file that are owned by the current process are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 242 | ** released. To work around this problem, each unixFile structure contains |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 243 | ** a pointer to an openCnt structure. There is one openCnt structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 244 | ** per open inode, which means that multiple unixFile can point to a single |
| 245 | ** openCnt. When an attempt is made to close an unixFile, if there are |
| 246 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 247 | ** to close() the file descriptor is deferred until all of the locks clear. |
| 248 | ** The openCnt structure keeps a list of file descriptors that need to |
| 249 | ** be closed and that list is walked (and cleared) when the last lock |
| 250 | ** clears. |
| 251 | ** |
| 252 | ** First, under Linux threads, because each thread has a separate |
| 253 | ** process ID, lock operations in one thread do not override locks |
| 254 | ** to the same file in other threads. Linux threads behave like |
| 255 | ** separate processes in this respect. But, if you close a file |
| 256 | ** descriptor in linux threads, all locks are cleared, even locks |
| 257 | ** on other threads and even though the other threads have different |
| 258 | ** process IDs. Linux threads is inconsistent in this respect. |
| 259 | ** (I'm beginning to think that linux threads is an abomination too.) |
| 260 | ** The consequence of this all is that the hash table for the lockInfo |
| 261 | ** structure has to include the process id as part of its key because |
| 262 | ** locks in different threads are treated as distinct. But the |
| 263 | ** openCnt structure should not include the process id in its |
| 264 | ** key because close() clears lock on all threads, not just the current |
| 265 | ** thread. Were it not for this goofiness in linux threads, we could |
| 266 | ** combine the lockInfo and openCnt structures into a single structure. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 267 | ** |
| 268 | ** 2004-Jun-28: |
| 269 | ** On some versions of linux, threads can override each others locks. |
| 270 | ** On others not. Sometimes you can change the behavior on the same |
| 271 | ** system by setting the LD_ASSUME_KERNEL environment variable. The |
| 272 | ** POSIX standard is silent as to which behavior is correct, as far |
| 273 | ** as I can tell, so other versions of unix might show the same |
| 274 | ** inconsistency. There is no little doubt in my mind that posix |
| 275 | ** advisory locks and linux threads are profoundly broken. |
| 276 | ** |
| 277 | ** To work around the inconsistencies, we have to test at runtime |
| 278 | ** whether or not threads can override each others locks. This test |
| 279 | ** is run once, the first time any lock is attempted. A static |
| 280 | ** variable is set to record the results of this test for future |
| 281 | ** use. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 282 | */ |
| 283 | |
| 284 | /* |
| 285 | ** An instance of the following structure serves as the key used |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 286 | ** to locate a particular lockInfo structure given its inode. |
| 287 | ** |
| 288 | ** If threads cannot override each others locks, then we set the |
| 289 | ** lockKey.tid field to the thread ID. If threads can override |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 290 | ** each others locks then tid is always set to zero. tid is omitted |
| 291 | ** if we compile without threading support. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 292 | */ |
| 293 | struct lockKey { |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 294 | dev_t dev; /* Device number */ |
| 295 | ino_t ino; /* Inode number */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 296 | #if SQLITE_THREADSAFE |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 297 | pthread_t tid; /* Thread ID or zero if threads can override each other */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 298 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | /* |
| 302 | ** An instance of the following structure is allocated for each open |
| 303 | ** inode on each thread with a different process ID. (Threads have |
| 304 | ** different process IDs on linux, but not on most other unixes.) |
| 305 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 306 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 307 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 308 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 309 | */ |
| 310 | struct lockInfo { |
| 311 | struct lockKey key; /* The lookup key */ |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 312 | int cnt; /* Number of SHARED locks held */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 313 | int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 314 | int nRef; /* Number of pointers to this structure */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 315 | struct lockInfo *pNext, *pPrev; /* List of all lockInfo objects */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | /* |
| 319 | ** An instance of the following structure serves as the key used |
| 320 | ** to locate a particular openCnt structure given its inode. This |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 321 | ** is the same as the lockKey except that the thread ID is omitted. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 322 | */ |
| 323 | struct openKey { |
| 324 | dev_t dev; /* Device number */ |
| 325 | ino_t ino; /* Inode number */ |
| 326 | }; |
| 327 | |
| 328 | /* |
| 329 | ** An instance of the following structure is allocated for each open |
| 330 | ** inode. This structure keeps track of the number of locks on that |
| 331 | ** inode. If a close is attempted against an inode that is holding |
| 332 | ** locks, the close is deferred until all locks clear by adding the |
| 333 | ** file descriptor to be closed to the pending list. |
| 334 | */ |
| 335 | struct openCnt { |
| 336 | struct openKey key; /* The lookup key */ |
| 337 | int nRef; /* Number of pointers to this structure */ |
| 338 | int nLock; /* Number of outstanding locks */ |
| 339 | int nPending; /* Number of pending close() operations */ |
| 340 | int *aPending; /* Malloced space holding fd's awaiting a close() */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 341 | struct openCnt *pNext, *pPrev; /* List of all openCnt objects */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 344 | /* |
| 345 | ** List of all lockInfo and openCnt objects. This used to be a hash |
| 346 | ** table. But the number of objects is rarely more than a dozen and |
| 347 | ** never exceeds a few thousand. And lookup is not on a critical |
| 348 | ** path oo a simple linked list will suffice. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 349 | */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 350 | static struct lockInfo *lockList = 0; |
| 351 | static struct openCnt *openList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 352 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 353 | /* |
| 354 | ** The locking styles are associated with the different file locking |
| 355 | ** capabilities supported by different file systems. |
| 356 | ** |
| 357 | ** POSIX locking style fully supports shared and exclusive byte-range locks |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 358 | ** AFP locking only supports exclusive byte-range locks |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 359 | ** FLOCK only supports a single file-global exclusive lock |
| 360 | ** DOTLOCK isn't a true locking style, it refers to the use of a special |
| 361 | ** file named the same as the database file with a '.lock' extension, this |
| 362 | ** can be used on file systems that do not offer any reliable file locking |
| 363 | ** NO locking means that no locking will be attempted, this is only used for |
| 364 | ** read-only file systems currently |
| 365 | ** UNSUPPORTED means that no locking will be attempted, this is only used for |
| 366 | ** file systems that are known to be unsupported |
| 367 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 368 | #define LOCKING_STYLE_POSIX 1 |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 369 | #define LOCKING_STYLE_NONE 2 |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 370 | #define LOCKING_STYLE_DOTFILE 3 |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 371 | #define LOCKING_STYLE_FLOCK 4 |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 372 | #define LOCKING_STYLE_AFP 5 |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 373 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 374 | /* |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 375 | ** Only set the lastErrno if the error code is a real error and not |
| 376 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 377 | */ |
| 378 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 379 | |
| 380 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 381 | ** Helper functions to obtain and relinquish the global mutex. |
| 382 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 383 | static void enterMutex(){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 384 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 385 | } |
| 386 | static void leaveMutex(){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 387 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 388 | } |
| 389 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 390 | #if SQLITE_THREADSAFE |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 391 | /* |
| 392 | ** This variable records whether or not threads can override each others |
| 393 | ** locks. |
| 394 | ** |
| 395 | ** 0: No. Threads cannot override each others locks. |
| 396 | ** 1: Yes. Threads can override each others locks. |
| 397 | ** -1: We don't know yet. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 398 | ** |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 399 | ** On some systems, we know at compile-time if threads can override each |
| 400 | ** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro |
| 401 | ** will be set appropriately. On other systems, we have to check at |
| 402 | ** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is |
| 403 | ** undefined. |
| 404 | ** |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 405 | ** This variable normally has file scope only. But during testing, we make |
| 406 | ** it a global so that the test code can change its value in order to verify |
| 407 | ** that the right stuff happens in either case. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 408 | */ |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 409 | #ifndef SQLITE_THREAD_OVERRIDE_LOCK |
| 410 | # define SQLITE_THREAD_OVERRIDE_LOCK -1 |
| 411 | #endif |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 412 | #ifdef SQLITE_TEST |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 413 | int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 414 | #else |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 415 | static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 416 | #endif |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | ** This structure holds information passed into individual test |
| 420 | ** threads by the testThreadLockingBehavior() routine. |
| 421 | */ |
| 422 | struct threadTestData { |
| 423 | int fd; /* File to be locked */ |
| 424 | struct flock lock; /* The locking operation */ |
| 425 | int result; /* Result of the locking operation */ |
| 426 | }; |
| 427 | |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 428 | #ifdef SQLITE_LOCK_TRACE |
| 429 | /* |
| 430 | ** Print out information about all locking operations. |
| 431 | ** |
| 432 | ** This routine is used for troubleshooting locks on multithreaded |
| 433 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 434 | ** command-line option on the compiler. This code is normally |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 435 | ** turned off. |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 436 | */ |
| 437 | static int lockTrace(int fd, int op, struct flock *p){ |
| 438 | char *zOpName, *zType; |
| 439 | int s; |
| 440 | int savedErrno; |
| 441 | if( op==F_GETLK ){ |
| 442 | zOpName = "GETLK"; |
| 443 | }else if( op==F_SETLK ){ |
| 444 | zOpName = "SETLK"; |
| 445 | }else{ |
| 446 | s = fcntl(fd, op, p); |
| 447 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 448 | return s; |
| 449 | } |
| 450 | if( p->l_type==F_RDLCK ){ |
| 451 | zType = "RDLCK"; |
| 452 | }else if( p->l_type==F_WRLCK ){ |
| 453 | zType = "WRLCK"; |
| 454 | }else if( p->l_type==F_UNLCK ){ |
| 455 | zType = "UNLCK"; |
| 456 | }else{ |
| 457 | assert( 0 ); |
| 458 | } |
| 459 | assert( p->l_whence==SEEK_SET ); |
| 460 | s = fcntl(fd, op, p); |
| 461 | savedErrno = errno; |
| 462 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 463 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 464 | (int)p->l_pid, s); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 465 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 466 | struct flock l2; |
| 467 | l2 = *p; |
| 468 | fcntl(fd, F_GETLK, &l2); |
| 469 | if( l2.l_type==F_RDLCK ){ |
| 470 | zType = "RDLCK"; |
| 471 | }else if( l2.l_type==F_WRLCK ){ |
| 472 | zType = "WRLCK"; |
| 473 | }else if( l2.l_type==F_UNLCK ){ |
| 474 | zType = "UNLCK"; |
| 475 | }else{ |
| 476 | assert( 0 ); |
| 477 | } |
| 478 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 479 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 480 | } |
| 481 | errno = savedErrno; |
| 482 | return s; |
| 483 | } |
| 484 | #define fcntl lockTrace |
| 485 | #endif /* SQLITE_LOCK_TRACE */ |
| 486 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 487 | /* |
| 488 | ** The testThreadLockingBehavior() routine launches two separate |
| 489 | ** threads on this routine. This routine attempts to lock a file |
| 490 | ** descriptor then returns. The success or failure of that attempt |
| 491 | ** allows the testThreadLockingBehavior() procedure to determine |
| 492 | ** whether or not threads can override each others locks. |
| 493 | */ |
| 494 | static void *threadLockingTest(void *pArg){ |
| 495 | struct threadTestData *pData = (struct threadTestData*)pArg; |
| 496 | pData->result = fcntl(pData->fd, F_SETLK, &pData->lock); |
| 497 | return pArg; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | ** This procedure attempts to determine whether or not threads |
| 502 | ** can override each others locks then sets the |
| 503 | ** threadsOverrideEachOthersLocks variable appropriately. |
| 504 | */ |
danielk1977 | 4d5238f | 2006-01-27 06:32:00 +0000 | [diff] [blame] | 505 | static void testThreadLockingBehavior(int fd_orig){ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 506 | int fd; |
| 507 | struct threadTestData d[2]; |
| 508 | pthread_t t[2]; |
| 509 | |
| 510 | fd = dup(fd_orig); |
| 511 | if( fd<0 ) return; |
| 512 | memset(d, 0, sizeof(d)); |
| 513 | d[0].fd = fd; |
| 514 | d[0].lock.l_type = F_RDLCK; |
| 515 | d[0].lock.l_len = 1; |
| 516 | d[0].lock.l_start = 0; |
| 517 | d[0].lock.l_whence = SEEK_SET; |
| 518 | d[1] = d[0]; |
| 519 | d[1].lock.l_type = F_WRLCK; |
| 520 | pthread_create(&t[0], 0, threadLockingTest, &d[0]); |
| 521 | pthread_create(&t[1], 0, threadLockingTest, &d[1]); |
| 522 | pthread_join(t[0], 0); |
| 523 | pthread_join(t[1], 0); |
| 524 | close(fd); |
| 525 | threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0; |
| 526 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 527 | #endif /* SQLITE_THREADSAFE */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 528 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 529 | /* |
| 530 | ** Release a lockInfo structure previously allocated by findLockInfo(). |
| 531 | */ |
| 532 | static void releaseLockInfo(struct lockInfo *pLock){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 533 | if( pLock ){ |
| 534 | pLock->nRef--; |
| 535 | if( pLock->nRef==0 ){ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 536 | if( pLock->pPrev ){ |
| 537 | assert( pLock->pPrev->pNext==pLock ); |
| 538 | pLock->pPrev->pNext = pLock->pNext; |
| 539 | }else{ |
| 540 | assert( lockList==pLock ); |
| 541 | lockList = pLock->pNext; |
| 542 | } |
| 543 | if( pLock->pNext ){ |
| 544 | assert( pLock->pNext->pPrev==pLock ); |
| 545 | pLock->pNext->pPrev = pLock->pPrev; |
| 546 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 547 | sqlite3_free(pLock); |
| 548 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | ** Release a openCnt structure previously allocated by findLockInfo(). |
| 554 | */ |
| 555 | static void releaseOpenCnt(struct openCnt *pOpen){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 556 | if( pOpen ){ |
| 557 | pOpen->nRef--; |
| 558 | if( pOpen->nRef==0 ){ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 559 | if( pOpen->pPrev ){ |
| 560 | assert( pOpen->pPrev->pNext==pOpen ); |
| 561 | pOpen->pPrev->pNext = pOpen->pNext; |
| 562 | }else{ |
| 563 | assert( openList==pOpen ); |
| 564 | openList = pOpen->pNext; |
| 565 | } |
| 566 | if( pOpen->pNext ){ |
| 567 | assert( pOpen->pNext->pPrev==pOpen ); |
| 568 | pOpen->pNext->pPrev = pOpen->pPrev; |
| 569 | } |
| 570 | sqlite3_free(pOpen->aPending); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 571 | sqlite3_free(pOpen); |
| 572 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 576 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 577 | /* |
| 578 | ** Tests a byte-range locking query to see if byte range locks are |
| 579 | ** supported, if not we fall back to dotlockLockingStyle. |
| 580 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 581 | static int testLockingStyle(int fd){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 582 | struct flock lockInfo; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 583 | |
| 584 | /* Test byte-range lock using fcntl(). If the call succeeds, |
| 585 | ** assume that the file-system supports POSIX style locks. |
| 586 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 587 | lockInfo.l_len = 1; |
| 588 | lockInfo.l_start = 0; |
| 589 | lockInfo.l_whence = SEEK_SET; |
| 590 | lockInfo.l_type = F_RDLCK; |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 591 | if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) { |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 592 | return LOCKING_STYLE_POSIX; |
| 593 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 594 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 595 | /* Testing for flock() can give false positives. So if if the above |
| 596 | ** test fails, then we fall back to using dot-file style locking. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 597 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 598 | return LOCKING_STYLE_DOTFILE; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 599 | } |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 600 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 601 | |
| 602 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 603 | ** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the |
| 604 | ** f_fstypename entry in the statfs structure as returned by stat() for |
| 605 | ** the file system hosting the database file and selects the appropriate |
| 606 | ** locking style based on its value. These values and assignments are |
| 607 | ** based on Darwin/OSX behavior and have not been thoroughly tested on |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 608 | ** other systems. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 609 | ** |
| 610 | ** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always |
| 611 | ** returns LOCKING_STYLE_POSIX. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 612 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 613 | static int detectLockingStyle( |
| 614 | sqlite3_vfs *pVfs, |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 615 | const char *filePath, |
| 616 | int fd |
| 617 | ){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 618 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 619 | struct Mapping { |
| 620 | const char *zFilesystem; |
| 621 | int eLockingStyle; |
| 622 | } aMap[] = { |
| 623 | { "hfs", LOCKING_STYLE_POSIX }, |
| 624 | { "ufs", LOCKING_STYLE_POSIX }, |
| 625 | { "afpfs", LOCKING_STYLE_AFP }, |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 626 | #ifdef SQLITE_ENABLE_AFP_LOCKING_SMB |
| 627 | { "smbfs", LOCKING_STYLE_AFP }, |
| 628 | #else |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 629 | { "smbfs", LOCKING_STYLE_FLOCK }, |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 630 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 631 | { "msdos", LOCKING_STYLE_DOTFILE }, |
| 632 | { "webdav", LOCKING_STYLE_NONE }, |
| 633 | { 0, 0 } |
| 634 | }; |
| 635 | int i; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 636 | struct statfs fsInfo; |
| 637 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 638 | if( !filePath ){ |
| 639 | return LOCKING_STYLE_NONE; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 640 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 641 | if( pVfs->pAppData ){ |
aswift | f54b1b3 | 2008-08-22 18:41:37 +0000 | [diff] [blame] | 642 | return SQLITE_PTR_TO_INT(pVfs->pAppData); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 643 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 644 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 645 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 646 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 647 | return LOCKING_STYLE_NONE; |
| 648 | } |
| 649 | for(i=0; aMap[i].zFilesystem; i++){ |
| 650 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 651 | return aMap[i].eLockingStyle; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* Default case. Handles, amongst others, "nfs". */ |
| 657 | return testLockingStyle(fd); |
| 658 | #endif |
| 659 | return LOCKING_STYLE_POSIX; |
| 660 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 661 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 662 | /* |
| 663 | ** Given a file descriptor, locate lockInfo and openCnt structures that |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 664 | ** describes that file descriptor. Create new ones if necessary. The |
| 665 | ** return values might be uninitialized if an error occurs. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 666 | ** |
drh | 6559404 | 2008-05-05 16:56:34 +0000 | [diff] [blame] | 667 | ** Return an appropriate error code. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 668 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 669 | static int findLockInfo( |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 670 | int fd, /* The file descriptor used in the key */ |
| 671 | struct lockInfo **ppLock, /* Return the lockInfo structure here */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 672 | struct openCnt **ppOpen /* Return the openCnt structure here */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 673 | ){ |
| 674 | int rc; |
| 675 | struct lockKey key1; |
| 676 | struct openKey key2; |
| 677 | struct stat statbuf; |
| 678 | struct lockInfo *pLock; |
| 679 | struct openCnt *pOpen; |
| 680 | rc = fstat(fd, &statbuf); |
drh | 6559404 | 2008-05-05 16:56:34 +0000 | [diff] [blame] | 681 | if( rc!=0 ){ |
| 682 | #ifdef EOVERFLOW |
| 683 | if( errno==EOVERFLOW ) return SQLITE_NOLFS; |
| 684 | #endif |
| 685 | return SQLITE_IOERR; |
| 686 | } |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 687 | |
drh | 5462624 | 2008-07-30 17:28:04 +0000 | [diff] [blame] | 688 | /* On OS X on an msdos filesystem, the inode number is reported |
| 689 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 690 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 691 | ** we always increase the file size to 1 by writing a single byte |
| 692 | ** prior to accessing the inode number. The one byte written is |
| 693 | ** an ASCII 'S' character which also happens to be the first byte |
| 694 | ** in the header of every SQLite database. In this way, if there |
| 695 | ** is a race condition such that another thread has already populated |
| 696 | ** the first page of the database, no damage is done. |
| 697 | */ |
| 698 | if( statbuf.st_size==0 ){ |
| 699 | write(fd, "S", 1); |
| 700 | rc = fstat(fd, &statbuf); |
| 701 | if( rc!=0 ){ |
| 702 | return SQLITE_IOERR; |
| 703 | } |
| 704 | } |
| 705 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 706 | memset(&key1, 0, sizeof(key1)); |
| 707 | key1.dev = statbuf.st_dev; |
| 708 | key1.ino = statbuf.st_ino; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 709 | #if SQLITE_THREADSAFE |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 710 | if( threadsOverrideEachOthersLocks<0 ){ |
| 711 | testThreadLockingBehavior(fd); |
| 712 | } |
| 713 | key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self(); |
| 714 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 715 | memset(&key2, 0, sizeof(key2)); |
| 716 | key2.dev = statbuf.st_dev; |
| 717 | key2.ino = statbuf.st_ino; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 718 | pLock = lockList; |
| 719 | while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){ |
| 720 | pLock = pLock->pNext; |
| 721 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 722 | if( pLock==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 723 | pLock = sqlite3_malloc( sizeof(*pLock) ); |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 724 | if( pLock==0 ){ |
drh | 6559404 | 2008-05-05 16:56:34 +0000 | [diff] [blame] | 725 | rc = SQLITE_NOMEM; |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 726 | goto exit_findlockinfo; |
| 727 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 728 | pLock->key = key1; |
| 729 | pLock->nRef = 1; |
| 730 | pLock->cnt = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 731 | pLock->locktype = 0; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 732 | pLock->pNext = lockList; |
| 733 | pLock->pPrev = 0; |
| 734 | if( lockList ) lockList->pPrev = pLock; |
| 735 | lockList = pLock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 736 | }else{ |
| 737 | pLock->nRef++; |
| 738 | } |
| 739 | *ppLock = pLock; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 740 | if( ppOpen!=0 ){ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 741 | pOpen = openList; |
| 742 | while( pOpen && memcmp(&key2, &pOpen->key, sizeof(key2)) ){ |
| 743 | pOpen = pOpen->pNext; |
| 744 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 745 | if( pOpen==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 746 | pOpen = sqlite3_malloc( sizeof(*pOpen) ); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 747 | if( pOpen==0 ){ |
| 748 | releaseLockInfo(pLock); |
drh | 6559404 | 2008-05-05 16:56:34 +0000 | [diff] [blame] | 749 | rc = SQLITE_NOMEM; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 750 | goto exit_findlockinfo; |
| 751 | } |
| 752 | pOpen->key = key2; |
| 753 | pOpen->nRef = 1; |
| 754 | pOpen->nLock = 0; |
| 755 | pOpen->nPending = 0; |
| 756 | pOpen->aPending = 0; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 757 | pOpen->pNext = openList; |
| 758 | pOpen->pPrev = 0; |
| 759 | if( openList ) openList->pPrev = pOpen; |
| 760 | openList = pOpen; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 761 | }else{ |
| 762 | pOpen->nRef++; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 763 | } |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 764 | *ppOpen = pOpen; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 765 | } |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 766 | |
| 767 | exit_findlockinfo: |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 768 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 769 | } |
| 770 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 771 | #ifdef SQLITE_DEBUG |
| 772 | /* |
| 773 | ** Helper function for printing out trace information from debugging |
| 774 | ** binaries. This returns the string represetation of the supplied |
| 775 | ** integer lock-type. |
| 776 | */ |
| 777 | static const char *locktypeName(int locktype){ |
| 778 | switch( locktype ){ |
| 779 | case NO_LOCK: return "NONE"; |
| 780 | case SHARED_LOCK: return "SHARED"; |
| 781 | case RESERVED_LOCK: return "RESERVED"; |
| 782 | case PENDING_LOCK: return "PENDING"; |
| 783 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
| 784 | } |
| 785 | return "ERROR"; |
| 786 | } |
| 787 | #endif |
| 788 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 789 | /* |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 790 | ** If we are currently in a different thread than the thread that the |
| 791 | ** unixFile argument belongs to, then transfer ownership of the unixFile |
| 792 | ** over to the current thread. |
| 793 | ** |
| 794 | ** A unixFile is only owned by a thread on systems where one thread is |
| 795 | ** unable to override locks created by a different thread. RedHat9 is |
| 796 | ** an example of such a system. |
| 797 | ** |
| 798 | ** Ownership transfer is only allowed if the unixFile is currently unlocked. |
| 799 | ** If the unixFile is locked and an ownership is wrong, then return |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 800 | ** SQLITE_MISUSE. SQLITE_OK is returned if everything works. |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 801 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 802 | #if SQLITE_THREADSAFE |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 803 | static int transferOwnership(unixFile *pFile){ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 804 | int rc; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 805 | pthread_t hSelf; |
| 806 | if( threadsOverrideEachOthersLocks ){ |
| 807 | /* Ownership transfers not needed on this system */ |
| 808 | return SQLITE_OK; |
| 809 | } |
| 810 | hSelf = pthread_self(); |
| 811 | if( pthread_equal(pFile->tid, hSelf) ){ |
| 812 | /* We are still in the same thread */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 813 | OSTRACE1("No-transfer, same thread\n"); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 814 | return SQLITE_OK; |
| 815 | } |
| 816 | if( pFile->locktype!=NO_LOCK ){ |
| 817 | /* We cannot change ownership while we are holding a lock! */ |
| 818 | return SQLITE_MISUSE; |
| 819 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 820 | OSTRACE4("Transfer ownership of %d from %d to %d\n", |
| 821 | pFile->h, pFile->tid, hSelf); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 822 | pFile->tid = hSelf; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 823 | if (pFile->pLock != NULL) { |
| 824 | releaseLockInfo(pFile->pLock); |
| 825 | rc = findLockInfo(pFile->h, &pFile->pLock, 0); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 826 | OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 827 | locktypeName(pFile->locktype), |
| 828 | locktypeName(pFile->pLock->locktype), pFile->pLock->cnt); |
| 829 | return rc; |
| 830 | } else { |
| 831 | return SQLITE_OK; |
| 832 | } |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 833 | } |
| 834 | #else |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 835 | /* On single-threaded builds, ownership transfer is a no-op */ |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 836 | # define transferOwnership(X) SQLITE_OK |
| 837 | #endif |
| 838 | |
| 839 | /* |
danielk1977 | 2a6bdf6 | 2007-08-20 16:07:00 +0000 | [diff] [blame] | 840 | ** Seek to the offset passed as the second argument, then read cnt |
| 841 | ** bytes into pBuf. Return the number of bytes actually read. |
drh | 9e0ebbf | 2007-10-23 15:59:18 +0000 | [diff] [blame] | 842 | ** |
| 843 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 844 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 845 | ** one system to another. Since SQLite does not define USE_PREAD |
| 846 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 847 | ** See tickets #2741 and #2681. |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 848 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 849 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 850 | int got; |
drh | 8ebf670 | 2007-02-06 11:11:08 +0000 | [diff] [blame] | 851 | i64 newOffset; |
drh | 15d00c4 | 2007-02-27 02:01:14 +0000 | [diff] [blame] | 852 | TIMER_START; |
drh | 8350a21 | 2007-03-22 15:22:06 +0000 | [diff] [blame] | 853 | #if defined(USE_PREAD) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 854 | got = pread(id->h, pBuf, cnt, offset); |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 855 | SimulateIOError( got = -1 ); |
drh | 8350a21 | 2007-03-22 15:22:06 +0000 | [diff] [blame] | 856 | #elif defined(USE_PREAD64) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 857 | got = pread64(id->h, pBuf, cnt, offset); |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 858 | SimulateIOError( got = -1 ); |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 859 | #else |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 860 | newOffset = lseek(id->h, offset, SEEK_SET); |
drh | bb5f18d | 2007-04-06 18:23:17 +0000 | [diff] [blame] | 861 | SimulateIOError( newOffset-- ); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 862 | if( newOffset!=offset ){ |
drh | 8ebf670 | 2007-02-06 11:11:08 +0000 | [diff] [blame] | 863 | return -1; |
| 864 | } |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 865 | got = read(id->h, pBuf, cnt); |
| 866 | #endif |
drh | 15d00c4 | 2007-02-27 02:01:14 +0000 | [diff] [blame] | 867 | TIMER_END; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 868 | OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 869 | return got; |
| 870 | } |
| 871 | |
| 872 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 873 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 874 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 875 | ** wrong. |
| 876 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 877 | static int unixRead( |
| 878 | sqlite3_file *id, |
| 879 | void *pBuf, |
| 880 | int amt, |
| 881 | sqlite3_int64 offset |
| 882 | ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 883 | int got; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 884 | assert( id ); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 885 | got = seekAndRead((unixFile*)id, offset, pBuf, amt); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 886 | if( got==amt ){ |
| 887 | return SQLITE_OK; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 888 | }else if( got<0 ){ |
| 889 | return SQLITE_IOERR_READ; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 890 | }else{ |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 891 | memset(&((char*)pBuf)[got], 0, amt-got); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 892 | return SQLITE_IOERR_SHORT_READ; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | |
| 896 | /* |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 897 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 898 | ** Return the number of bytes actually read. Update the offset. |
| 899 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 900 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 901 | int got; |
drh | 8ebf670 | 2007-02-06 11:11:08 +0000 | [diff] [blame] | 902 | i64 newOffset; |
drh | 15d00c4 | 2007-02-27 02:01:14 +0000 | [diff] [blame] | 903 | TIMER_START; |
drh | 8350a21 | 2007-03-22 15:22:06 +0000 | [diff] [blame] | 904 | #if defined(USE_PREAD) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 905 | got = pwrite(id->h, pBuf, cnt, offset); |
drh | 8350a21 | 2007-03-22 15:22:06 +0000 | [diff] [blame] | 906 | #elif defined(USE_PREAD64) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 907 | got = pwrite64(id->h, pBuf, cnt, offset); |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 908 | #else |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 909 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 910 | if( newOffset!=offset ){ |
drh | 8ebf670 | 2007-02-06 11:11:08 +0000 | [diff] [blame] | 911 | return -1; |
| 912 | } |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 913 | got = write(id->h, pBuf, cnt); |
| 914 | #endif |
drh | 15d00c4 | 2007-02-27 02:01:14 +0000 | [diff] [blame] | 915 | TIMER_END; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 916 | OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 917 | return got; |
| 918 | } |
| 919 | |
| 920 | |
| 921 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 922 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 923 | ** or some other error code on failure. |
| 924 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 925 | static int unixWrite( |
| 926 | sqlite3_file *id, |
| 927 | const void *pBuf, |
| 928 | int amt, |
| 929 | sqlite3_int64 offset |
| 930 | ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 931 | int wrote = 0; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 932 | assert( id ); |
drh | 4c7f941 | 2005-02-03 00:29:47 +0000 | [diff] [blame] | 933 | assert( amt>0 ); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 934 | while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 935 | amt -= wrote; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 936 | offset += wrote; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 937 | pBuf = &((char*)pBuf)[wrote]; |
| 938 | } |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 939 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 940 | SimulateDiskfullError(( wrote=0, amt=1 )); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 941 | if( amt>0 ){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 942 | if( wrote<0 ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 943 | return SQLITE_IOERR_WRITE; |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 944 | }else{ |
| 945 | return SQLITE_FULL; |
| 946 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 947 | } |
| 948 | return SQLITE_OK; |
| 949 | } |
| 950 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 951 | #ifdef SQLITE_TEST |
| 952 | /* |
| 953 | ** Count the number of fullsyncs and normal syncs. This is used to test |
| 954 | ** that syncs and fullsyncs are occuring at the right times. |
| 955 | */ |
| 956 | int sqlite3_sync_count = 0; |
| 957 | int sqlite3_fullsync_count = 0; |
| 958 | #endif |
| 959 | |
drh | f2f2391 | 2005-10-05 10:29:36 +0000 | [diff] [blame] | 960 | /* |
| 961 | ** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined. |
| 962 | ** Otherwise use fsync() in its place. |
| 963 | */ |
| 964 | #ifndef HAVE_FDATASYNC |
| 965 | # define fdatasync fsync |
| 966 | #endif |
| 967 | |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 968 | /* |
| 969 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 970 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 971 | ** only available on Mac OS X. But that could change. |
| 972 | */ |
| 973 | #ifdef F_FULLFSYNC |
| 974 | # define HAVE_FULLFSYNC 1 |
| 975 | #else |
| 976 | # define HAVE_FULLFSYNC 0 |
| 977 | #endif |
| 978 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 979 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 980 | /* |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 981 | ** The fsync() system call does not work as advertised on many |
| 982 | ** unix systems. The following procedure is an attempt to make |
| 983 | ** it work better. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 984 | ** |
| 985 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 986 | ** for testing when we want to run through the test suite quickly. |
| 987 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 988 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 989 | ** or power failure will likely corrupt the database file. |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 990 | */ |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 991 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 992 | int rc; |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 993 | |
| 994 | /* Record the number of times that we do a normal fsync() and |
| 995 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 996 | ** gets called with the correct arguments. |
| 997 | */ |
| 998 | #ifdef SQLITE_TEST |
| 999 | if( fullSync ) sqlite3_fullsync_count++; |
| 1000 | sqlite3_sync_count++; |
| 1001 | #endif |
| 1002 | |
| 1003 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 1004 | ** no-op |
| 1005 | */ |
| 1006 | #ifdef SQLITE_NO_SYNC |
| 1007 | rc = SQLITE_OK; |
| 1008 | #else |
| 1009 | |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1010 | #if HAVE_FULLFSYNC |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 1011 | if( fullSync ){ |
drh | f30cc94 | 2005-03-11 17:52:34 +0000 | [diff] [blame] | 1012 | rc = fcntl(fd, F_FULLFSYNC, 0); |
aswift | ae0943b | 2007-01-31 23:37:07 +0000 | [diff] [blame] | 1013 | }else{ |
| 1014 | rc = 1; |
| 1015 | } |
| 1016 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
| 1017 | * It shouldn't be possible for fullfsync to fail on the local |
| 1018 | * file system (on OSX), so failure indicates that FULLFSYNC |
| 1019 | * isn't supported for this file system. So, attempt an fsync |
| 1020 | * and (for now) ignore the overhead of a superfluous fcntl call. |
| 1021 | * It'd be better to detect fullfsync support once and avoid |
| 1022 | * the fcntl call every time sync is called. |
| 1023 | */ |
| 1024 | if( rc ) rc = fsync(fd); |
| 1025 | |
| 1026 | #else |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 1027 | if( dataOnly ){ |
| 1028 | rc = fdatasync(fd); |
drh | f2f2391 | 2005-10-05 10:29:36 +0000 | [diff] [blame] | 1029 | }else{ |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 1030 | rc = fsync(fd); |
| 1031 | } |
aswift | ae0943b | 2007-01-31 23:37:07 +0000 | [diff] [blame] | 1032 | #endif /* HAVE_FULLFSYNC */ |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 1033 | #endif /* defined(SQLITE_NO_SYNC) */ |
| 1034 | |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 1035 | return rc; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1039 | ** Make sure all writes to a particular file are committed to disk. |
| 1040 | ** |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 1041 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 1042 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 1043 | ** file data is synced. |
| 1044 | ** |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1045 | ** Under Unix, also make sure that the directory entry for the file |
| 1046 | ** has been created by fsync-ing the directory that contains the file. |
| 1047 | ** If we do not do this and we encounter a power failure, the directory |
| 1048 | ** entry for the journal might not exist after we reboot. The next |
| 1049 | ** SQLite to access the file will not know that the journal exists (because |
| 1050 | ** the directory entry for the journal was never created) and the transaction |
| 1051 | ** will not roll back - possibly leading to database corruption. |
| 1052 | */ |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1053 | static int unixSync(sqlite3_file *id, int flags){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1054 | int rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1055 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1056 | |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 1057 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 1058 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 1059 | |
danielk1977 | c16d463 | 2007-08-30 14:49:58 +0000 | [diff] [blame] | 1060 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 1061 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 1062 | || (flags&0x0F)==SQLITE_SYNC_FULL |
danielk1977 | f036aef | 2007-08-20 05:36:51 +0000 | [diff] [blame] | 1063 | ); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1064 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1065 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1066 | OSTRACE2("SYNC %-3d\n", pFile->h); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1067 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1068 | SimulateIOError( rc=1 ); |
| 1069 | if( rc ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1070 | return SQLITE_IOERR_FSYNC; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1071 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1072 | if( pFile->dirfd>=0 ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1073 | OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1074 | HAVE_FULLFSYNC, isFullsync); |
danielk1977 | d7c03f7 | 2005-11-25 10:38:22 +0000 | [diff] [blame] | 1075 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1076 | /* The directory sync is only attempted if full_fsync is |
| 1077 | ** turned off or unavailable. If a full_fsync occurred above, |
| 1078 | ** then the directory sync is superfluous. |
| 1079 | */ |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1080 | if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1081 | /* |
| 1082 | ** We have received multiple reports of fsync() returning |
drh | 86631a5 | 2006-02-09 23:05:51 +0000 | [diff] [blame] | 1083 | ** errors when applied to directories on certain file systems. |
| 1084 | ** A failed directory sync is not a big deal. So it seems |
| 1085 | ** better to ignore the error. Ticket #1657 |
| 1086 | */ |
| 1087 | /* return SQLITE_IOERR; */ |
danielk1977 | 0964b23 | 2005-11-25 08:47:57 +0000 | [diff] [blame] | 1088 | } |
danielk1977 | d7c03f7 | 2005-11-25 10:38:22 +0000 | [diff] [blame] | 1089 | #endif |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1090 | close(pFile->dirfd); /* Only need to sync once, so close the directory */ |
| 1091 | pFile->dirfd = -1; /* when we are done. */ |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 1092 | } |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 1093 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | ** Truncate an open file to a specified size |
| 1098 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1099 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1100 | int rc; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1101 | assert( id ); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1102 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
drh | 63fff5f | 2007-06-19 10:50:38 +0000 | [diff] [blame] | 1103 | rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1104 | if( rc ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1105 | return SQLITE_IOERR_TRUNCATE; |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1106 | }else{ |
| 1107 | return SQLITE_OK; |
| 1108 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | ** Determine the current size of a file in bytes |
| 1113 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1114 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1115 | int rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1116 | struct stat buf; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1117 | assert( id ); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1118 | rc = fstat(((unixFile*)id)->h, &buf); |
| 1119 | SimulateIOError( rc=1 ); |
| 1120 | if( rc!=0 ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1121 | return SQLITE_IOERR_FSTAT; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1122 | } |
| 1123 | *pSize = buf.st_size; |
drh | 5462624 | 2008-07-30 17:28:04 +0000 | [diff] [blame] | 1124 | |
| 1125 | /* When opening a zero-size database, the findLockInfo() procedure |
| 1126 | ** writes a single byte into that file in order to work around a bug |
| 1127 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 1128 | ** layers, we need to report this file size as zero even though it is |
| 1129 | ** really 1. Ticket #3260. |
| 1130 | */ |
| 1131 | if( *pSize==1 ) *pSize = 0; |
| 1132 | |
| 1133 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1134 | return SQLITE_OK; |
| 1135 | } |
| 1136 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1137 | /* |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1138 | ** This routine translates a standard POSIX errno code into something |
| 1139 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 1140 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 1141 | ** and a variety of "please close the file descriptor NOW" errors into |
| 1142 | ** SQLITE_IOERR |
| 1143 | ** |
| 1144 | ** Errors during initialization of locks, or file system support for locks, |
| 1145 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 1146 | */ |
| 1147 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 1148 | switch (posixError) { |
| 1149 | case 0: |
| 1150 | return SQLITE_OK; |
| 1151 | |
| 1152 | case EAGAIN: |
| 1153 | case ETIMEDOUT: |
| 1154 | case EBUSY: |
| 1155 | case EINTR: |
| 1156 | case ENOLCK: |
| 1157 | /* random NFS retry error, unless during file system support |
| 1158 | * introspection, in which it actually means what it says */ |
| 1159 | return SQLITE_BUSY; |
| 1160 | |
| 1161 | case EACCES: |
| 1162 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 1163 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 1164 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 1165 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 1166 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 1167 | return SQLITE_BUSY; |
| 1168 | } |
| 1169 | /* else fall through */ |
| 1170 | case EPERM: |
| 1171 | return SQLITE_PERM; |
| 1172 | |
| 1173 | case EDEADLK: |
| 1174 | return SQLITE_IOERR_BLOCKED; |
| 1175 | |
drh | f489c45 | 2008-08-22 00:47:53 +0000 | [diff] [blame] | 1176 | #if EOPNOTSUPP!=ENOTSUP |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1177 | case EOPNOTSUPP: |
| 1178 | /* something went terribly awry, unless during file system support |
| 1179 | * introspection, in which it actually means what it says */ |
drh | f489c45 | 2008-08-22 00:47:53 +0000 | [diff] [blame] | 1180 | #endif |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 1181 | #ifdef ENOTSUP |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1182 | case ENOTSUP: |
| 1183 | /* invalid fd, unless during file system support introspection, in which |
| 1184 | * it actually means what it says */ |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 1185 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1186 | case EIO: |
| 1187 | case EBADF: |
| 1188 | case EINVAL: |
| 1189 | case ENOTCONN: |
| 1190 | case ENODEV: |
| 1191 | case ENXIO: |
| 1192 | case ENOENT: |
| 1193 | case ESTALE: |
| 1194 | case ENOSYS: |
| 1195 | /* these should force the client to close the file and reconnect */ |
| 1196 | |
| 1197 | default: |
| 1198 | return sqliteIOErr; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1203 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1204 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1205 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1206 | ** 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] | 1207 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1208 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1209 | int rc = SQLITE_OK; |
| 1210 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1211 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1212 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1213 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1214 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1215 | assert( pFile ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1216 | enterMutex(); /* Because pFile->pLock is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1217 | |
| 1218 | /* Check if a thread in this process holds such a lock */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1219 | if( pFile->pLock->locktype>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1220 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1223 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1224 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1225 | if( !reserved ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1226 | struct flock lock; |
| 1227 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1228 | lock.l_start = RESERVED_BYTE; |
| 1229 | lock.l_len = 1; |
| 1230 | lock.l_type = F_WRLCK; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1231 | if (-1 == fcntl(pFile->h, F_GETLK, &lock)) { |
| 1232 | int tErrno = errno; |
| 1233 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 1234 | pFile->lastErrno = tErrno; |
| 1235 | } else if( lock.l_type!=F_UNLCK ){ |
| 1236 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1240 | leaveMutex(); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1241 | OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1242 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1243 | *pResOut = reserved; |
| 1244 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | /* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1248 | ** Lock the file with the lock specified by parameter locktype - one |
| 1249 | ** of the following: |
| 1250 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1251 | ** (1) SHARED_LOCK |
| 1252 | ** (2) RESERVED_LOCK |
| 1253 | ** (3) PENDING_LOCK |
| 1254 | ** (4) EXCLUSIVE_LOCK |
| 1255 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1256 | ** Sometimes when requesting one lock state, additional lock states |
| 1257 | ** are inserted in between. The locking might fail on one of the later |
| 1258 | ** transitions leaving the lock state different from what it started but |
| 1259 | ** still short of its goal. The following chart shows the allowed |
| 1260 | ** transitions and the inserted intermediate states: |
| 1261 | ** |
| 1262 | ** UNLOCKED -> SHARED |
| 1263 | ** SHARED -> RESERVED |
| 1264 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1265 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1266 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1267 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1268 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1269 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1270 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1271 | static int unixLock(sqlite3_file *id, int locktype){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1272 | /* The following describes the implementation of the various locks and |
| 1273 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1274 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1275 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1276 | ** slightly in order to be compatible with windows systems simultaneously |
| 1277 | ** accessing the same database file, in case that is ever required. |
| 1278 | ** |
| 1279 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1280 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1281 | ** range', a range of 510 bytes at a well known offset. |
| 1282 | ** |
| 1283 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1284 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1285 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1286 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1287 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1288 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1289 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1290 | ** |
| 1291 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1292 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1293 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1294 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1295 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1296 | ** This property is used by the algorithm for rolling back a journal file |
| 1297 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1298 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1299 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1300 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1301 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1302 | ** within this range, this ensures that no other locks are held on the |
| 1303 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1304 | ** |
| 1305 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1306 | ** range' is that some versions of windows do not support read-locks. By |
| 1307 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1308 | ** even if the locking primitive used is always a write-lock. |
| 1309 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1310 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1311 | unixFile *pFile = (unixFile*)id; |
| 1312 | struct lockInfo *pLock = pFile->pLock; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1313 | struct flock lock; |
| 1314 | int s; |
| 1315 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1316 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1317 | OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1318 | locktypeName(locktype), locktypeName(pFile->locktype), |
| 1319 | locktypeName(pLock->locktype), pLock->cnt , getpid()); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1320 | |
| 1321 | /* 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] | 1322 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1323 | ** enterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1324 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1325 | if( pFile->locktype>=locktype ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1326 | OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1327 | locktypeName(locktype)); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1328 | return SQLITE_OK; |
| 1329 | } |
| 1330 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1331 | /* Make sure the locking sequence is correct |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1332 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1333 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1334 | assert( locktype!=PENDING_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1335 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1336 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1337 | /* This mutex is needed because pFile->pLock is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1338 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1339 | enterMutex(); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1340 | |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1341 | /* Make sure the current thread owns the pFile. |
| 1342 | */ |
| 1343 | rc = transferOwnership(pFile); |
| 1344 | if( rc!=SQLITE_OK ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1345 | leaveMutex(); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1346 | return rc; |
| 1347 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1348 | pLock = pFile->pLock; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1349 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1350 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1351 | ** handle that precludes the requested lock, return BUSY. |
| 1352 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1353 | if( (pFile->locktype!=pLock->locktype && |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1354 | (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1355 | ){ |
| 1356 | rc = SQLITE_BUSY; |
| 1357 | goto end_lock; |
| 1358 | } |
| 1359 | |
| 1360 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1361 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1362 | ** return SQLITE_OK. |
| 1363 | */ |
| 1364 | if( locktype==SHARED_LOCK && |
| 1365 | (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){ |
| 1366 | assert( locktype==SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1367 | assert( pFile->locktype==0 ); |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1368 | assert( pLock->cnt>0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1369 | pFile->locktype = SHARED_LOCK; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1370 | pLock->cnt++; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1371 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1372 | goto end_lock; |
| 1373 | } |
| 1374 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1375 | lock.l_len = 1L; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1376 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1377 | lock.l_whence = SEEK_SET; |
| 1378 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1379 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1380 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1381 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1382 | */ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1383 | if( locktype==SHARED_LOCK |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1384 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1385 | ){ |
danielk1977 | 489468c | 2004-06-28 08:25:47 +0000 | [diff] [blame] | 1386 | lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1387 | lock.l_start = PENDING_BYTE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1388 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1389 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1390 | int tErrno = errno; |
| 1391 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1392 | if( IS_LOCK_ERROR(rc) ){ |
| 1393 | pFile->lastErrno = tErrno; |
| 1394 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1395 | goto end_lock; |
| 1396 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | |
| 1400 | /* If control gets to this point, then actually go ahead and make |
| 1401 | ** operating system calls for the specified lock. |
| 1402 | */ |
| 1403 | if( locktype==SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1404 | int tErrno = 0; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1405 | assert( pLock->cnt==0 ); |
| 1406 | assert( pLock->locktype==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1407 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1408 | /* Now get the read-lock */ |
| 1409 | lock.l_start = SHARED_FIRST; |
| 1410 | lock.l_len = SHARED_SIZE; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1411 | if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){ |
| 1412 | tErrno = errno; |
| 1413 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1414 | /* Drop the temporary PENDING lock */ |
| 1415 | lock.l_start = PENDING_BYTE; |
| 1416 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1417 | lock.l_type = F_UNLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1418 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1419 | if( s != -1 ){ |
| 1420 | /* This could happen with a network mount */ |
| 1421 | tErrno = errno; |
| 1422 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1423 | if( IS_LOCK_ERROR(rc) ){ |
| 1424 | pFile->lastErrno = tErrno; |
| 1425 | } |
| 1426 | goto end_lock; |
| 1427 | } |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1428 | } |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1429 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1430 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1431 | if( IS_LOCK_ERROR(rc) ){ |
| 1432 | pFile->lastErrno = tErrno; |
| 1433 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1434 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1435 | pFile->locktype = SHARED_LOCK; |
| 1436 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1437 | pLock->cnt = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1438 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1439 | }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){ |
| 1440 | /* We are trying for an exclusive lock but another thread in this |
| 1441 | ** same process is still holding a shared lock. */ |
| 1442 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1443 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1444 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1445 | ** assumed that there is a SHARED or greater lock on the file |
| 1446 | ** already. |
| 1447 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1448 | assert( 0!=pFile->locktype ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1449 | lock.l_type = F_WRLCK; |
| 1450 | switch( locktype ){ |
| 1451 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1452 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1453 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1454 | case EXCLUSIVE_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1455 | lock.l_start = SHARED_FIRST; |
| 1456 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1457 | break; |
| 1458 | default: |
| 1459 | assert(0); |
| 1460 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1461 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1462 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1463 | int tErrno = errno; |
| 1464 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1465 | if( IS_LOCK_ERROR(rc) ){ |
| 1466 | pFile->lastErrno = tErrno; |
| 1467 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1468 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1469 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1470 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1471 | if( rc==SQLITE_OK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1472 | pFile->locktype = locktype; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1473 | pLock->locktype = locktype; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1474 | }else if( locktype==EXCLUSIVE_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1475 | pFile->locktype = PENDING_LOCK; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1476 | pLock->locktype = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1477 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1478 | |
| 1479 | end_lock: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1480 | leaveMutex(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1481 | OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 1482 | rc==SQLITE_OK ? "ok" : "failed"); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1483 | return rc; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1487 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1488 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1489 | ** |
| 1490 | ** If the locking level of the file descriptor is already at or below |
| 1491 | ** the requested locking level, this routine is a no-op. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1492 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1493 | static int unixUnlock(sqlite3_file *id, int locktype){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1494 | struct lockInfo *pLock; |
| 1495 | struct flock lock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1496 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1497 | unixFile *pFile = (unixFile*)id; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1498 | int h; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1499 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1500 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1501 | OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1502 | pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid()); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1503 | |
| 1504 | assert( locktype<=SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1505 | if( pFile->locktype<=locktype ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1506 | return SQLITE_OK; |
| 1507 | } |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1508 | if( CHECK_THREADID(pFile) ){ |
| 1509 | return SQLITE_MISUSE; |
| 1510 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1511 | enterMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1512 | h = pFile->h; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1513 | pLock = pFile->pLock; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1514 | assert( pLock->cnt!=0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1515 | if( pFile->locktype>SHARED_LOCK ){ |
| 1516 | assert( pLock->locktype==pFile->locktype ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1517 | SimulateIOErrorBenign(1); |
| 1518 | SimulateIOError( h=(-1) ) |
| 1519 | SimulateIOErrorBenign(0); |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1520 | if( locktype==SHARED_LOCK ){ |
| 1521 | lock.l_type = F_RDLCK; |
| 1522 | lock.l_whence = SEEK_SET; |
| 1523 | lock.l_start = SHARED_FIRST; |
| 1524 | lock.l_len = SHARED_SIZE; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1525 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1526 | int tErrno = errno; |
| 1527 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1528 | if( IS_LOCK_ERROR(rc) ){ |
| 1529 | pFile->lastErrno = tErrno; |
| 1530 | } |
| 1531 | goto end_unlock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1532 | } |
| 1533 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1534 | lock.l_type = F_UNLCK; |
| 1535 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1536 | lock.l_start = PENDING_BYTE; |
| 1537 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1538 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1539 | pLock->locktype = SHARED_LOCK; |
| 1540 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1541 | int tErrno = errno; |
| 1542 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1543 | if( IS_LOCK_ERROR(rc) ){ |
| 1544 | pFile->lastErrno = tErrno; |
| 1545 | } |
| 1546 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1547 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1548 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1549 | if( locktype==NO_LOCK ){ |
| 1550 | struct openCnt *pOpen; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1551 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1552 | /* Decrement the shared lock counter. Release the lock using an |
| 1553 | ** OS call only when all threads in this same process have released |
| 1554 | ** the lock. |
| 1555 | */ |
| 1556 | pLock->cnt--; |
| 1557 | if( pLock->cnt==0 ){ |
| 1558 | lock.l_type = F_UNLCK; |
| 1559 | lock.l_whence = SEEK_SET; |
| 1560 | lock.l_start = lock.l_len = 0L; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1561 | SimulateIOErrorBenign(1); |
| 1562 | SimulateIOError( h=(-1) ) |
| 1563 | SimulateIOErrorBenign(0); |
| 1564 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1565 | pLock->locktype = NO_LOCK; |
| 1566 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1567 | int tErrno = errno; |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 1568 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1569 | if( IS_LOCK_ERROR(rc) ){ |
| 1570 | pFile->lastErrno = tErrno; |
| 1571 | } |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1572 | pLock->cnt = 1; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1573 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1574 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1577 | /* Decrement the count of locks against this same file. When the |
| 1578 | ** count reaches zero, close any other file descriptors whose close |
| 1579 | ** was deferred because of outstanding locks. |
| 1580 | */ |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1581 | if( rc==SQLITE_OK ){ |
| 1582 | pOpen = pFile->pOpen; |
| 1583 | pOpen->nLock--; |
| 1584 | assert( pOpen->nLock>=0 ); |
| 1585 | if( pOpen->nLock==0 && pOpen->nPending>0 ){ |
| 1586 | int i; |
| 1587 | for(i=0; i<pOpen->nPending; i++){ |
| 1588 | close(pOpen->aPending[i]); |
| 1589 | } |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1590 | sqlite3_free(pOpen->aPending); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1591 | pOpen->nPending = 0; |
| 1592 | pOpen->aPending = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1593 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1594 | } |
| 1595 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1596 | |
| 1597 | end_unlock: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1598 | leaveMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1599 | if( rc==SQLITE_OK ) pFile->locktype = locktype; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1600 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1604 | ** This function performs the parts of the "close file" operation |
| 1605 | ** common to all locking schemes. It closes the directory and file |
| 1606 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1607 | ** structure to 0. |
| 1608 | */ |
| 1609 | static int closeUnixFile(sqlite3_file *id){ |
| 1610 | unixFile *pFile = (unixFile*)id; |
| 1611 | if( pFile ){ |
| 1612 | if( pFile->dirfd>=0 ){ |
| 1613 | close(pFile->dirfd); |
| 1614 | } |
| 1615 | if( pFile->h>=0 ){ |
| 1616 | close(pFile->h); |
| 1617 | } |
| 1618 | OSTRACE2("CLOSE %-3d\n", pFile->h); |
| 1619 | OpenCounter(-1); |
| 1620 | memset(pFile, 0, sizeof(unixFile)); |
| 1621 | } |
| 1622 | return SQLITE_OK; |
| 1623 | } |
| 1624 | |
| 1625 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1626 | ** Close a file. |
| 1627 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1628 | static int unixClose(sqlite3_file *id){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1629 | if( id ){ |
| 1630 | unixFile *pFile = (unixFile *)id; |
| 1631 | unixUnlock(id, NO_LOCK); |
| 1632 | enterMutex(); |
danielk1977 | 6cb427f | 2008-06-30 10:16:04 +0000 | [diff] [blame] | 1633 | if( pFile->pOpen && pFile->pOpen->nLock ){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1634 | /* If there are outstanding locks, do not actually close the file just |
| 1635 | ** yet because that would clear those locks. Instead, add the file |
| 1636 | ** descriptor to pOpen->aPending. It will be automatically closed when |
| 1637 | ** the last lock is cleared. |
| 1638 | */ |
| 1639 | int *aNew; |
| 1640 | struct openCnt *pOpen = pFile->pOpen; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1641 | aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) ); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1642 | if( aNew==0 ){ |
| 1643 | /* If a malloc fails, just leak the file descriptor */ |
| 1644 | }else{ |
| 1645 | pOpen->aPending = aNew; |
| 1646 | pOpen->aPending[pOpen->nPending] = pFile->h; |
| 1647 | pOpen->nPending++; |
| 1648 | pFile->h = -1; |
| 1649 | } |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1650 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1651 | releaseLockInfo(pFile->pLock); |
| 1652 | releaseOpenCnt(pFile->pOpen); |
| 1653 | closeUnixFile(id); |
| 1654 | leaveMutex(); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1655 | } |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 1656 | return SQLITE_OK; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1659 | |
| 1660 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 1661 | #pragma mark AFP Support |
| 1662 | |
| 1663 | /* |
| 1664 | ** The afpLockingContext structure contains all afp lock specific state |
| 1665 | */ |
| 1666 | typedef struct afpLockingContext afpLockingContext; |
| 1667 | struct afpLockingContext { |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1668 | unsigned long long sharedLockByte; |
drh | 308aa32 | 2008-03-07 20:14:38 +0000 | [diff] [blame] | 1669 | const char *filePath; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1670 | }; |
| 1671 | |
| 1672 | struct ByteRangeLockPB2 |
| 1673 | { |
| 1674 | unsigned long long offset; /* offset to first byte to lock */ |
| 1675 | unsigned long long length; /* nbr of bytes to lock */ |
| 1676 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 1677 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 1678 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 1679 | int fd; /* file desc to assoc this lock with */ |
| 1680 | }; |
| 1681 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 1682 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1683 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1684 | /* |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1685 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 1686 | */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1687 | static int _AFPFSSetLock( |
| 1688 | const char *path, |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1689 | unixFile *pFile, |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1690 | unsigned long long offset, |
| 1691 | unsigned long long length, |
| 1692 | int setLockFlag |
| 1693 | ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 1694 | struct ByteRangeLockPB2 pb; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1695 | int err; |
| 1696 | |
| 1697 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 1698 | pb.startEndFlag = 0; |
| 1699 | pb.offset = offset; |
| 1700 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1701 | pb.fd = pFile->h; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1702 | OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n", |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1703 | (setLockFlag?"ON":"OFF"), pFile->h, offset, length); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1704 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 1705 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1706 | int rc; |
| 1707 | int tErrno = errno; |
| 1708 | OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, tErrno, strerror(tErrno)); |
| 1709 | rc = sqliteErrorFromPosixError(tErrno, setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); /* error */ |
| 1710 | if( IS_LOCK_ERROR(rc) ){ |
| 1711 | pFile->lastErrno = tErrno; |
| 1712 | } |
| 1713 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1714 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1715 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1719 | /* AFP-style reserved lock checking following the behavior of |
| 1720 | ** unixCheckReservedLock, see the unixCheckReservedLock function comments */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1721 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1722 | int rc = SQLITE_OK; |
| 1723 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1724 | unixFile *pFile = (unixFile*)id; |
| 1725 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1726 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1727 | |
| 1728 | assert( pFile ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1729 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 1730 | |
| 1731 | /* Check if a thread in this process holds such a lock */ |
| 1732 | if( pFile->locktype>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1733 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | /* Otherwise see if some other process holds it. |
| 1737 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1738 | if( !reserved ){ |
| 1739 | /* lock the RESERVED byte */ |
| 1740 | int lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1); |
| 1741 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1742 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 1743 | ** the original state */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1744 | lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1, 0); |
| 1745 | } else { |
| 1746 | /* if we failed to get the lock then someone else must have it */ |
| 1747 | reserved = 1; |
| 1748 | } |
| 1749 | if( IS_LOCK_ERROR(lrc) ){ |
| 1750 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1751 | } |
| 1752 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1753 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1754 | OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |
| 1755 | |
| 1756 | *pResOut = reserved; |
| 1757 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | /* AFP-style locking following the behavior of unixLock, see the unixLock |
| 1761 | ** function comments for details of lock management. */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1762 | static int afpLock(sqlite3_file *id, int locktype){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1763 | int rc = SQLITE_OK; |
| 1764 | unixFile *pFile = (unixFile*)id; |
| 1765 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1766 | |
| 1767 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1768 | OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h, |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1769 | locktypeName(locktype), locktypeName(pFile->locktype), getpid()); |
| 1770 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1771 | /* 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] | 1772 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
| 1773 | ** enterMutex() hasn't been called yet. |
| 1774 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1775 | if( pFile->locktype>=locktype ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1776 | OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1777 | locktypeName(locktype)); |
| 1778 | return SQLITE_OK; |
| 1779 | } |
| 1780 | |
| 1781 | /* Make sure the locking sequence is correct |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1782 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1783 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 1784 | assert( locktype!=PENDING_LOCK ); |
| 1785 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
| 1786 | |
| 1787 | /* This mutex is needed because pFile->pLock is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1788 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1789 | enterMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1790 | |
| 1791 | /* Make sure the current thread owns the pFile. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1792 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1793 | rc = transferOwnership(pFile); |
| 1794 | if( rc!=SQLITE_OK ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1795 | leaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1796 | return rc; |
| 1797 | } |
| 1798 | |
| 1799 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1800 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1801 | ** be released. |
| 1802 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1803 | if( locktype==SHARED_LOCK |
| 1804 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1805 | ){ |
| 1806 | int failed; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1807 | failed = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1808 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1809 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1810 | goto afp_end_lock; |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1815 | ** operating system calls for the specified lock. |
| 1816 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1817 | if( locktype==SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1818 | int lk, lrc1, lrc2, lrc1Errno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1819 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1820 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1821 | /* note that the quality of the randomness doesn't matter that much */ |
| 1822 | lk = random(); |
| 1823 | context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1824 | lrc1 = _AFPFSSetLock(context->filePath, pFile, |
| 1825 | SHARED_FIRST+context->sharedLockByte, 1, 1); |
| 1826 | if( IS_LOCK_ERROR(lrc1) ){ |
| 1827 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1828 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1829 | /* Drop the temporary PENDING lock */ |
| 1830 | lrc2 = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1831 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1832 | if( IS_LOCK_ERROR(lrc1) ) { |
| 1833 | pFile->lastErrno = lrc1Errno; |
| 1834 | rc = lrc1; |
| 1835 | goto afp_end_lock; |
| 1836 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 1837 | rc = lrc2; |
| 1838 | goto afp_end_lock; |
| 1839 | } else if( lrc1 != SQLITE_OK ) { |
| 1840 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1841 | } else { |
| 1842 | pFile->locktype = SHARED_LOCK; |
| 1843 | } |
| 1844 | }else{ |
| 1845 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 1846 | ** assumed that there is a SHARED or greater lock on the file |
| 1847 | ** already. |
| 1848 | */ |
| 1849 | int failed = 0; |
| 1850 | assert( 0!=pFile->locktype ); |
| 1851 | if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) { |
| 1852 | /* Acquire a RESERVED lock */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1853 | failed = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1854 | } |
| 1855 | if (!failed && locktype == EXCLUSIVE_LOCK) { |
| 1856 | /* Acquire an EXCLUSIVE lock */ |
| 1857 | |
| 1858 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1859 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1860 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1861 | if (!(failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST + |
| 1862 | context->sharedLockByte, 1, 0))) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1863 | /* now attemmpt to get the exclusive lock range */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1864 | failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1865 | SHARED_SIZE, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1866 | if (failed && (failed = _AFPFSSetLock(context->filePath, pFile, |
| 1867 | SHARED_FIRST + context->sharedLockByte, 1, 1))) { |
| 1868 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1869 | } |
| 1870 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1871 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1872 | } |
| 1873 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1874 | if( failed ){ |
| 1875 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | if( rc==SQLITE_OK ){ |
| 1880 | pFile->locktype = locktype; |
| 1881 | }else if( locktype==EXCLUSIVE_LOCK ){ |
| 1882 | pFile->locktype = PENDING_LOCK; |
| 1883 | } |
| 1884 | |
| 1885 | afp_end_lock: |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1886 | leaveMutex(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1887 | OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1888 | rc==SQLITE_OK ? "ok" : "failed"); |
| 1889 | return rc; |
| 1890 | } |
| 1891 | |
| 1892 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1893 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 1894 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1895 | ** |
| 1896 | ** If the locking level of the file descriptor is already at or below |
| 1897 | ** the requested locking level, this routine is a no-op. |
| 1898 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1899 | static int afpUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1900 | int rc = SQLITE_OK; |
| 1901 | unixFile *pFile = (unixFile*)id; |
| 1902 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 1903 | |
| 1904 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1905 | OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1906 | pFile->locktype, getpid()); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1907 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1908 | assert( locktype<=SHARED_LOCK ); |
| 1909 | if( pFile->locktype<=locktype ){ |
| 1910 | return SQLITE_OK; |
| 1911 | } |
| 1912 | if( CHECK_THREADID(pFile) ){ |
| 1913 | return SQLITE_MISUSE; |
| 1914 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1915 | enterMutex(); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1916 | int failed = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1917 | if( pFile->locktype>SHARED_LOCK ){ |
| 1918 | if( locktype==SHARED_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1919 | |
| 1920 | /* unlock the exclusive range - then re-establish the shared lock */ |
| 1921 | if (pFile->locktype==EXCLUSIVE_LOCK) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1922 | failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1923 | SHARED_SIZE, 0); |
| 1924 | if (!failed) { |
| 1925 | /* successfully removed the exclusive lock */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1926 | if ((failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST+ |
| 1927 | context->sharedLockByte, 1, 1))) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1928 | /* failed to re-establish our shared lock */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1929 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1930 | } |
| 1931 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1932 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | } |
| 1936 | if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1937 | if ((failed = _AFPFSSetLock(context->filePath, pFile, |
| 1938 | PENDING_BYTE, 1, 0))){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1939 | /* failed to release the pending lock */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1940 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1944 | if ((failed = _AFPFSSetLock(context->filePath, pFile, |
| 1945 | RESERVED_BYTE, 1, 0))) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1946 | /* failed to release the reserved lock */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1947 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | } |
| 1951 | if( locktype==NO_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1952 | int failed = _AFPFSSetLock(context->filePath, pFile, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1953 | SHARED_FIRST + context->sharedLockByte, 1, 0); |
| 1954 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1955 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | if (rc == SQLITE_OK) |
| 1959 | pFile->locktype = locktype; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1960 | leaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1961 | return rc; |
| 1962 | } |
| 1963 | |
| 1964 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1965 | ** Close a file & cleanup AFP specific locking context |
| 1966 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1967 | static int afpClose(sqlite3_file *id) { |
| 1968 | if( id ){ |
| 1969 | unixFile *pFile = (unixFile*)id; |
| 1970 | afpUnlock(id, NO_LOCK); |
| 1971 | sqlite3_free(pFile->lockingContext); |
| 1972 | } |
| 1973 | return closeUnixFile(id); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | |
| 1977 | #pragma mark flock() style locking |
| 1978 | |
| 1979 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 1980 | ** The flockLockingContext is not used |
| 1981 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1982 | typedef void flockLockingContext; |
| 1983 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1984 | /* flock-style reserved lock checking following the behavior of |
| 1985 | ** unixCheckReservedLock, see the unixCheckReservedLock function comments */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1986 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1987 | int rc = SQLITE_OK; |
| 1988 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1989 | unixFile *pFile = (unixFile*)id; |
| 1990 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1991 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1992 | |
| 1993 | assert( pFile ); |
| 1994 | |
| 1995 | /* Check if a thread in this process holds such a lock */ |
| 1996 | if( pFile->locktype>SHARED_LOCK ){ |
| 1997 | reserved = 1; |
| 1998 | } |
| 1999 | |
| 2000 | /* Otherwise see if some other process holds it. */ |
| 2001 | if( !reserved ){ |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2002 | /* attempt to get the lock */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2003 | int lrc = flock(pFile->h, LOCK_EX | LOCK_NB); |
| 2004 | if( !lrc ){ |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2005 | /* got the lock, unlock it */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2006 | lrc = flock(pFile->h, LOCK_UN); |
| 2007 | if ( lrc ) { |
| 2008 | int tErrno = errno; |
| 2009 | /* unlock failed with an error */ |
| 2010 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2011 | if( IS_LOCK_ERROR(lrc) ){ |
| 2012 | pFile->lastErrno = tErrno; |
| 2013 | rc = lrc; |
| 2014 | } |
| 2015 | } |
| 2016 | } else { |
| 2017 | int tErrno = errno; |
| 2018 | reserved = 1; |
| 2019 | /* someone else might have it reserved */ |
| 2020 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2021 | if( IS_LOCK_ERROR(lrc) ){ |
| 2022 | pFile->lastErrno = tErrno; |
| 2023 | rc = lrc; |
| 2024 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2025 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2026 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2027 | OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 2028 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2029 | *pResOut = reserved; |
| 2030 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2033 | static int flockLock(sqlite3_file *id, int locktype) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2034 | int rc = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2035 | unixFile *pFile = (unixFile*)id; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2036 | |
| 2037 | assert( pFile ); |
| 2038 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2039 | /* if we already have a lock, it is exclusive. |
| 2040 | ** Just adjust level and punt on outta here. */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2041 | if (pFile->locktype > NO_LOCK) { |
| 2042 | pFile->locktype = locktype; |
| 2043 | return SQLITE_OK; |
| 2044 | } |
| 2045 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2046 | /* grab an exclusive lock */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2047 | |
| 2048 | if (flock(pFile->h, LOCK_EX | LOCK_NB)) { |
| 2049 | int tErrno = errno; |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2050 | /* didn't get, must be busy */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2051 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2052 | if( IS_LOCK_ERROR(rc) ){ |
| 2053 | pFile->lastErrno = tErrno; |
| 2054 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2055 | } else { |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2056 | /* got it, set the type and return ok */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2057 | pFile->locktype = locktype; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2058 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2059 | OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), |
| 2060 | rc==SQLITE_OK ? "ok" : "failed"); |
| 2061 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2064 | static int flockUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2065 | unixFile *pFile = (unixFile*)id; |
| 2066 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2067 | assert( pFile ); |
| 2068 | OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype, |
| 2069 | pFile->locktype, getpid()); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2070 | assert( locktype<=SHARED_LOCK ); |
| 2071 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2072 | /* no-op if possible */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2073 | if( pFile->locktype==locktype ){ |
| 2074 | return SQLITE_OK; |
| 2075 | } |
| 2076 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2077 | /* shared can just be set because we always have an exclusive */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2078 | if (locktype==SHARED_LOCK) { |
| 2079 | pFile->locktype = locktype; |
| 2080 | return SQLITE_OK; |
| 2081 | } |
| 2082 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2083 | /* no, really, unlock. */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2084 | int rc = flock(pFile->h, LOCK_UN); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2085 | if (rc) { |
| 2086 | int r, tErrno = errno; |
| 2087 | r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2088 | if( IS_LOCK_ERROR(r) ){ |
| 2089 | pFile->lastErrno = tErrno; |
| 2090 | } |
| 2091 | return r; |
| 2092 | } else { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2093 | pFile->locktype = NO_LOCK; |
| 2094 | return SQLITE_OK; |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2099 | ** Close a file. |
| 2100 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2101 | static int flockClose(sqlite3_file *id) { |
| 2102 | if( id ){ |
| 2103 | flockUnlock(id, NO_LOCK); |
| 2104 | } |
| 2105 | return closeUnixFile(id); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | #pragma mark Old-School .lock file based locking |
| 2109 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2110 | /* Dotlock-style reserved lock checking following the behavior of |
| 2111 | ** unixCheckReservedLock, see the unixCheckReservedLock function comments */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2112 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2113 | int rc = SQLITE_OK; |
| 2114 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2115 | unixFile *pFile = (unixFile*)id; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2116 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2117 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2118 | |
| 2119 | assert( pFile ); |
| 2120 | |
| 2121 | /* Check if a thread in this process holds such a lock */ |
| 2122 | if( pFile->locktype>SHARED_LOCK ){ |
| 2123 | reserved = 1; |
| 2124 | } |
| 2125 | |
| 2126 | /* Otherwise see if some other process holds it. */ |
| 2127 | if( !reserved ){ |
| 2128 | char *zLockFile = (char *)pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2129 | struct stat statBuf; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2130 | |
| 2131 | if( lstat(zLockFile, &statBuf)==0 ){ |
| 2132 | /* file exists, someone else has the lock */ |
| 2133 | reserved = 1; |
| 2134 | }else{ |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2135 | /* file does not exist, we could have it if we want it */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2136 | int tErrno = errno; |
| 2137 | if( ENOENT != tErrno ){ |
| 2138 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2139 | pFile->lastErrno = tErrno; |
| 2140 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2141 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2142 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2143 | OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 2144 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2145 | *pResOut = reserved; |
| 2146 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2149 | static int dotlockLock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2150 | unixFile *pFile = (unixFile*)id; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2151 | int fd; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2152 | char *zLockFile = (char *)pFile->lockingContext; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2153 | int rc=SQLITE_OK; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2154 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2155 | /* if we already have a lock, it is exclusive. |
| 2156 | ** Just adjust level and punt on outta here. */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2157 | if (pFile->locktype > NO_LOCK) { |
| 2158 | pFile->locktype = locktype; |
| 2159 | |
| 2160 | /* Always update the timestamp on the old file */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2161 | utimes(zLockFile, NULL); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2162 | rc = SQLITE_OK; |
| 2163 | goto dotlock_end_lock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2166 | /* check to see if lock file already exists */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2167 | struct stat statBuf; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2168 | if (lstat(zLockFile,&statBuf) == 0){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2169 | rc = SQLITE_BUSY; /* it does, busy */ |
| 2170 | goto dotlock_end_lock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2173 | /* grab an exclusive lock */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2174 | fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2175 | if( fd<0 ){ |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2176 | /* failed to open/create the file, someone else may have stolen the lock */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2177 | int tErrno = errno; |
| 2178 | if( EEXIST == tErrno ){ |
| 2179 | rc = SQLITE_BUSY; |
| 2180 | } else { |
| 2181 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2182 | if( IS_LOCK_ERROR(rc) ){ |
| 2183 | pFile->lastErrno = tErrno; |
| 2184 | } |
| 2185 | } |
| 2186 | goto dotlock_end_lock; |
| 2187 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2188 | close(fd); |
| 2189 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2190 | /* got it, set the type and return ok */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2191 | pFile->locktype = locktype; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2192 | |
| 2193 | dotlock_end_lock: |
| 2194 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2197 | static int dotlockUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2198 | unixFile *pFile = (unixFile*)id; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2199 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2200 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2201 | assert( pFile ); |
| 2202 | OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype, |
| 2203 | pFile->locktype, getpid()); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2204 | assert( locktype<=SHARED_LOCK ); |
| 2205 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2206 | /* no-op if possible */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2207 | if( pFile->locktype==locktype ){ |
| 2208 | return SQLITE_OK; |
| 2209 | } |
| 2210 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2211 | /* shared can just be set because we always have an exclusive */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2212 | if (locktype==SHARED_LOCK) { |
| 2213 | pFile->locktype = locktype; |
| 2214 | return SQLITE_OK; |
| 2215 | } |
| 2216 | |
drh | 3b62b2f | 2007-06-08 18:27:03 +0000 | [diff] [blame] | 2217 | /* no, really, unlock. */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2218 | if (unlink(zLockFile) ) { |
| 2219 | int rc, tErrno = errno; |
| 2220 | if( ENOENT != tErrno ){ |
| 2221 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2222 | } |
| 2223 | if( IS_LOCK_ERROR(rc) ){ |
| 2224 | pFile->lastErrno = tErrno; |
| 2225 | } |
| 2226 | return rc; |
| 2227 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2228 | pFile->locktype = NO_LOCK; |
| 2229 | return SQLITE_OK; |
| 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | ** Close a file. |
| 2234 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2235 | static int dotlockClose(sqlite3_file *id) { |
| 2236 | if( id ){ |
| 2237 | unixFile *pFile = (unixFile*)id; |
| 2238 | dotlockUnlock(id, NO_LOCK); |
| 2239 | sqlite3_free(pFile->lockingContext); |
| 2240 | } |
| 2241 | return closeUnixFile(id); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 2245 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2246 | |
| 2247 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2248 | ** The nolockLockingContext is void |
| 2249 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2250 | typedef void nolockLockingContext; |
| 2251 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2252 | static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 2253 | *pResOut = 0; |
| 2254 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2257 | static int nolockLock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2258 | return SQLITE_OK; |
| 2259 | } |
| 2260 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2261 | static int nolockUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2262 | return SQLITE_OK; |
| 2263 | } |
| 2264 | |
| 2265 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2266 | ** Close a file. |
| 2267 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2268 | static int nolockClose(sqlite3_file *id) { |
| 2269 | return closeUnixFile(id); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2272 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2273 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 2274 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 2275 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 2276 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 2277 | switch( op ){ |
| 2278 | case SQLITE_FCNTL_LOCKSTATE: { |
| 2279 | *(int*)pArg = ((unixFile*)id)->locktype; |
| 2280 | return SQLITE_OK; |
| 2281 | } |
| 2282 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 2283 | return SQLITE_ERROR; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 2287 | ** Return the sector size in bytes of the underlying block device for |
| 2288 | ** the specified file. This is almost always 512 bytes, but may be |
| 2289 | ** larger for some devices. |
| 2290 | ** |
| 2291 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 2292 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2293 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 2294 | ** same for both. |
| 2295 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2296 | static int unixSectorSize(sqlite3_file *id){ |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 2297 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2300 | /* |
| 2301 | ** Return the device characteristics for the file. This is always 0. |
| 2302 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2303 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
| 2304 | return 0; |
| 2305 | } |
| 2306 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 2307 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2308 | ** Initialize the contents of the unixFile structure pointed to by pId. |
| 2309 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2310 | ** When locking extensions are enabled, the filepath and locking style |
| 2311 | ** are needed to determine the unixFile pMethod to use for locking operations. |
| 2312 | ** The locking-style specific lockingContext data structure is created |
| 2313 | ** and assigned here also. |
| 2314 | */ |
| 2315 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2316 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2317 | int h, /* Open file descriptor of file being opened */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2318 | int dirfd, /* Directory file descriptor */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2319 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 2320 | const char *zFilename, /* Name of the file being opened */ |
| 2321 | int noLock /* Omit locking if true */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2322 | ){ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 2323 | int eLockingStyle; |
| 2324 | unixFile *pNew = (unixFile *)pId; |
| 2325 | int rc = SQLITE_OK; |
| 2326 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2327 | /* Macro to define the static contents of an sqlite3_io_methods |
| 2328 | ** structure for a unix backend file. Different locking methods |
| 2329 | ** require different functions for the xClose, xLock, xUnlock and |
| 2330 | ** xCheckReservedLock methods. |
| 2331 | */ |
| 2332 | #define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \ |
| 2333 | 1, /* iVersion */ \ |
| 2334 | xClose, /* xClose */ \ |
| 2335 | unixRead, /* xRead */ \ |
| 2336 | unixWrite, /* xWrite */ \ |
| 2337 | unixTruncate, /* xTruncate */ \ |
| 2338 | unixSync, /* xSync */ \ |
| 2339 | unixFileSize, /* xFileSize */ \ |
| 2340 | xLock, /* xLock */ \ |
| 2341 | xUnlock, /* xUnlock */ \ |
| 2342 | xCheckReservedLock, /* xCheckReservedLock */ \ |
| 2343 | unixFileControl, /* xFileControl */ \ |
| 2344 | unixSectorSize, /* xSectorSize */ \ |
| 2345 | unixDeviceCharacteristics /* xDeviceCapabilities */ \ |
| 2346 | } |
| 2347 | static sqlite3_io_methods aIoMethod[] = { |
| 2348 | IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock) |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2349 | ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock) |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 2350 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 2351 | ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock) |
| 2352 | ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock) |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2353 | ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock) |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2354 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2355 | }; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 2356 | /* The order of the IOMETHODS macros above is important. It must be the |
| 2357 | ** same order as the LOCKING_STYLE numbers |
| 2358 | */ |
| 2359 | assert(LOCKING_STYLE_POSIX==1); |
| 2360 | assert(LOCKING_STYLE_NONE==2); |
| 2361 | assert(LOCKING_STYLE_DOTFILE==3); |
| 2362 | assert(LOCKING_STYLE_FLOCK==4); |
| 2363 | assert(LOCKING_STYLE_AFP==5); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2364 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2365 | assert( pNew->pLock==NULL ); |
| 2366 | assert( pNew->pOpen==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2367 | |
| 2368 | OSTRACE3("OPEN %-3d %s\n", h, zFilename); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2369 | pNew->h = h; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2370 | pNew->dirfd = dirfd; |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2371 | SET_THREADID(pNew); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2372 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 2373 | if( noLock ){ |
| 2374 | eLockingStyle = LOCKING_STYLE_NONE; |
| 2375 | }else{ |
| 2376 | eLockingStyle = detectLockingStyle(pVfs, zFilename, h); |
| 2377 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2378 | |
| 2379 | switch( eLockingStyle ){ |
| 2380 | |
| 2381 | case LOCKING_STYLE_POSIX: { |
| 2382 | enterMutex(); |
| 2383 | rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen); |
| 2384 | leaveMutex(); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2385 | break; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2386 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2387 | |
| 2388 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 2389 | case LOCKING_STYLE_AFP: { |
| 2390 | /* AFP locking uses the file path so it needs to be included in |
| 2391 | ** the afpLockingContext. |
| 2392 | */ |
| 2393 | afpLockingContext *pCtx; |
| 2394 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 2395 | if( pCtx==0 ){ |
| 2396 | rc = SQLITE_NOMEM; |
| 2397 | }else{ |
| 2398 | /* NB: zFilename exists and remains valid until the file is closed |
| 2399 | ** according to requirement F11141. So we do not need to make a |
| 2400 | ** copy of the filename. */ |
| 2401 | pCtx->filePath = zFilename; |
| 2402 | srandomdev(); |
| 2403 | } |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2404 | break; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | case LOCKING_STYLE_DOTFILE: { |
| 2408 | /* Dotfile locking uses the file path so it needs to be included in |
| 2409 | ** the dotlockLockingContext |
| 2410 | */ |
| 2411 | char *zLockFile; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2412 | int nFilename; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2413 | nFilename = strlen(zFilename) + 6; |
| 2414 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 2415 | if( zLockFile==0 ){ |
| 2416 | rc = SQLITE_NOMEM; |
| 2417 | }else{ |
| 2418 | sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2419 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2420 | pNew->lockingContext = zLockFile; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2421 | break; |
| 2422 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2423 | |
| 2424 | case LOCKING_STYLE_FLOCK: |
| 2425 | case LOCKING_STYLE_NONE: |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 2426 | break; |
drh | e78669b | 2007-06-29 12:04:26 +0000 | [diff] [blame] | 2427 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2428 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2429 | |
| 2430 | pNew->lastErrno = 0; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2431 | if( rc!=SQLITE_OK ){ |
danielk1977 | 7c055b9 | 2007-10-30 17:28:51 +0000 | [diff] [blame] | 2432 | if( dirfd>=0 ) close(dirfd); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2433 | close(h); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2434 | }else{ |
danielk1977 | 6cb427f | 2008-06-30 10:16:04 +0000 | [diff] [blame] | 2435 | pNew->pMethod = &aIoMethod[eLockingStyle-1]; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2436 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2437 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2438 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 2439 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 2440 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2441 | /* |
| 2442 | ** Open a file descriptor to the directory containing file zFilename. |
| 2443 | ** If successful, *pFd is set to the opened file descriptor and |
| 2444 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 2445 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 2446 | ** value. |
| 2447 | ** |
| 2448 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 2449 | ** the file descriptor *pFd using close(). |
| 2450 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2451 | static int openDirectory(const char *zFilename, int *pFd){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2452 | int ii; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 2453 | int fd = -1; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2454 | char zDirname[MAX_PATHNAME+1]; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2455 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2456 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2457 | for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--); |
| 2458 | if( ii>0 ){ |
| 2459 | zDirname[ii] = '\0'; |
| 2460 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 2461 | if( fd>=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2462 | #ifdef FD_CLOEXEC |
| 2463 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 2464 | #endif |
| 2465 | OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname); |
| 2466 | } |
| 2467 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2468 | *pFd = fd; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 2469 | return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2472 | /* |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2473 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 2474 | ** by the calling process and must be big enough to hold at least |
| 2475 | ** pVfs->mxPathname bytes. |
| 2476 | */ |
| 2477 | static int getTempname(int nBuf, char *zBuf){ |
| 2478 | static const char *azDirs[] = { |
| 2479 | 0, |
| 2480 | "/var/tmp", |
| 2481 | "/usr/tmp", |
| 2482 | "/tmp", |
| 2483 | ".", |
| 2484 | }; |
| 2485 | static const unsigned char zChars[] = |
| 2486 | "abcdefghijklmnopqrstuvwxyz" |
| 2487 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 2488 | "0123456789"; |
| 2489 | int i, j; |
| 2490 | struct stat buf; |
| 2491 | const char *zDir = "."; |
| 2492 | |
| 2493 | /* It's odd to simulate an io-error here, but really this is just |
| 2494 | ** using the io-error infrastructure to test that SQLite handles this |
| 2495 | ** function failing. |
| 2496 | */ |
| 2497 | SimulateIOError( return SQLITE_IOERR ); |
| 2498 | |
| 2499 | azDirs[0] = sqlite3_temp_directory; |
| 2500 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
| 2501 | if( azDirs[i]==0 ) continue; |
| 2502 | if( stat(azDirs[i], &buf) ) continue; |
| 2503 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 2504 | if( access(azDirs[i], 07) ) continue; |
| 2505 | zDir = azDirs[i]; |
| 2506 | break; |
| 2507 | } |
| 2508 | |
| 2509 | /* Check that the output buffer is large enough for the temporary file |
| 2510 | ** name. If it is not, return SQLITE_ERROR. |
| 2511 | */ |
| 2512 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){ |
| 2513 | return SQLITE_ERROR; |
| 2514 | } |
| 2515 | |
| 2516 | do{ |
| 2517 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
| 2518 | j = strlen(zBuf); |
| 2519 | sqlite3_randomness(15, &zBuf[j]); |
| 2520 | for(i=0; i<15; i++, j++){ |
| 2521 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 2522 | } |
| 2523 | zBuf[j] = 0; |
| 2524 | }while( access(zBuf,0)==0 ); |
| 2525 | return SQLITE_OK; |
| 2526 | } |
| 2527 | |
| 2528 | |
| 2529 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2530 | ** Open the file zPath. |
| 2531 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2532 | ** Previously, the SQLite OS layer used three functions in place of this |
| 2533 | ** one: |
| 2534 | ** |
| 2535 | ** sqlite3OsOpenReadWrite(); |
| 2536 | ** sqlite3OsOpenReadOnly(); |
| 2537 | ** sqlite3OsOpenExclusive(); |
| 2538 | ** |
| 2539 | ** These calls correspond to the following combinations of flags: |
| 2540 | ** |
| 2541 | ** ReadWrite() -> (READWRITE | CREATE) |
| 2542 | ** ReadOnly() -> (READONLY) |
| 2543 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 2544 | ** |
| 2545 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 2546 | ** true, the file was configured to be automatically deleted when the |
| 2547 | ** file handle closed. To achieve the same effect using this new |
| 2548 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 2549 | ** OpenExclusive(). |
| 2550 | */ |
| 2551 | static int unixOpen( |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2552 | sqlite3_vfs *pVfs, |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2553 | const char *zPath, |
| 2554 | sqlite3_file *pFile, |
| 2555 | int flags, |
| 2556 | int *pOutFlags |
| 2557 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2558 | int fd = 0; /* File descriptor returned by open() */ |
| 2559 | int dirfd = -1; /* Directory file descriptor */ |
| 2560 | int oflags = 0; /* Flags to pass to open() */ |
| 2561 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 2562 | int noLock; /* True to omit locking primitives */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2563 | |
| 2564 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 2565 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 2566 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 2567 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 2568 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
| 2569 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2570 | /* If creating a master or main-file journal, this function will open |
| 2571 | ** a file-descriptor on the directory too. The first time unixSync() |
| 2572 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 2573 | */ |
| 2574 | int isOpenDirectory = (isCreate && |
| 2575 | (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL) |
| 2576 | ); |
| 2577 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2578 | /* If argument zPath is a NULL pointer, this function is required to open |
| 2579 | ** a temporary file. Use this buffer to store the file name in. |
| 2580 | */ |
| 2581 | char zTmpname[MAX_PATHNAME+1]; |
| 2582 | const char *zName = zPath; |
| 2583 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2584 | /* Check the following statements are true: |
| 2585 | ** |
| 2586 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 2587 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 2588 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2589 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2590 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2591 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2592 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2593 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2594 | assert(isDelete==0 || isCreate); |
| 2595 | |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2596 | /* The main DB, main journal, and master journal are never automatically |
| 2597 | ** deleted |
| 2598 | */ |
| 2599 | assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete ); |
| 2600 | assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete ); |
| 2601 | assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2602 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2603 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 2604 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 2605 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 2606 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 2607 | || eType==SQLITE_OPEN_TRANSIENT_DB |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2608 | ); |
| 2609 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2610 | memset(pFile, 0, sizeof(unixFile)); |
| 2611 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2612 | if( !zName ){ |
| 2613 | int rc; |
| 2614 | assert(isDelete && !isOpenDirectory); |
| 2615 | rc = getTempname(MAX_PATHNAME+1, zTmpname); |
| 2616 | if( rc!=SQLITE_OK ){ |
| 2617 | return rc; |
| 2618 | } |
| 2619 | zName = zTmpname; |
| 2620 | } |
| 2621 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2622 | if( isReadonly ) oflags |= O_RDONLY; |
| 2623 | if( isReadWrite ) oflags |= O_RDWR; |
| 2624 | if( isCreate ) oflags |= O_CREAT; |
| 2625 | if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW); |
| 2626 | oflags |= (O_LARGEFILE|O_BINARY); |
| 2627 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2628 | fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS); |
danielk1977 | 2f2d8c7 | 2007-08-30 16:13:33 +0000 | [diff] [blame] | 2629 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2630 | /* Failed to open the file for read/write access. Try read-only. */ |
| 2631 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
| 2632 | flags |= SQLITE_OPEN_READONLY; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2633 | return unixOpen(pVfs, zPath, pFile, flags, pOutFlags); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2634 | } |
| 2635 | if( fd<0 ){ |
| 2636 | return SQLITE_CANTOPEN; |
| 2637 | } |
| 2638 | if( isDelete ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 2639 | unlink(zName); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2640 | } |
| 2641 | if( pOutFlags ){ |
| 2642 | *pOutFlags = flags; |
| 2643 | } |
| 2644 | |
| 2645 | assert(fd!=0); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2646 | if( isOpenDirectory ){ |
| 2647 | int rc = openDirectory(zPath, &dirfd); |
| 2648 | if( rc!=SQLITE_OK ){ |
| 2649 | close(fd); |
| 2650 | return rc; |
| 2651 | } |
| 2652 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2653 | |
| 2654 | #ifdef FD_CLOEXEC |
| 2655 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 2656 | #endif |
| 2657 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 2658 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
| 2659 | return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2663 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 2664 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2665 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2666 | static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2667 | int rc = SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2668 | SimulateIOError(return SQLITE_IOERR_DELETE); |
| 2669 | unlink(zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2670 | if( dirSync ){ |
| 2671 | int fd; |
| 2672 | rc = openDirectory(zPath, &fd); |
| 2673 | if( rc==SQLITE_OK ){ |
| 2674 | if( fsync(fd) ){ |
| 2675 | rc = SQLITE_IOERR_DIR_FSYNC; |
| 2676 | } |
| 2677 | close(fd); |
| 2678 | } |
| 2679 | } |
| 2680 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2683 | /* |
| 2684 | ** Test the existance of or access permissions of file zPath. The |
| 2685 | ** test performed depends on the value of flags: |
| 2686 | ** |
| 2687 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 2688 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 2689 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 2690 | ** |
| 2691 | ** Otherwise return 0. |
| 2692 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 2693 | static int unixAccess( |
| 2694 | sqlite3_vfs *pVfs, |
| 2695 | const char *zPath, |
| 2696 | int flags, |
| 2697 | int *pResOut |
| 2698 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 2699 | int amode = 0; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 2700 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2701 | switch( flags ){ |
| 2702 | case SQLITE_ACCESS_EXISTS: |
| 2703 | amode = F_OK; |
| 2704 | break; |
| 2705 | case SQLITE_ACCESS_READWRITE: |
| 2706 | amode = W_OK|R_OK; |
| 2707 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 2708 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2709 | amode = R_OK; |
| 2710 | break; |
| 2711 | |
| 2712 | default: |
| 2713 | assert(!"Invalid flags argument"); |
| 2714 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 2715 | *pResOut = (access(zPath, amode)==0); |
| 2716 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2719 | |
| 2720 | /* |
| 2721 | ** Turn a relative pathname into a full pathname. The relative path |
| 2722 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 2723 | ** zPath. |
| 2724 | ** |
| 2725 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 2726 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 2727 | ** this buffer before returning. |
| 2728 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 2729 | static int unixFullPathname( |
| 2730 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 2731 | const char *zPath, /* Possibly relative input path */ |
| 2732 | int nOut, /* Size of output buffer in bytes */ |
| 2733 | char *zOut /* Output buffer */ |
| 2734 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 2735 | |
| 2736 | /* It's odd to simulate an io-error here, but really this is just |
| 2737 | ** using the io-error infrastructure to test that SQLite handles this |
| 2738 | ** function failing. This function could fail if, for example, the |
| 2739 | ** current working directly has been unlinked. |
| 2740 | */ |
| 2741 | SimulateIOError( return SQLITE_ERROR ); |
| 2742 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2743 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 2744 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2745 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 2746 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2747 | }else{ |
| 2748 | int nCwd; |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 2749 | if( getcwd(zOut, nOut-1)==0 ){ |
drh | 70c0145 | 2007-09-03 17:42:17 +0000 | [diff] [blame] | 2750 | return SQLITE_CANTOPEN; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2751 | } |
| 2752 | nCwd = strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 2753 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2754 | } |
| 2755 | return SQLITE_OK; |
| 2756 | |
| 2757 | #if 0 |
| 2758 | /* |
| 2759 | ** Remove "/./" path elements and convert "/A/./" path elements |
| 2760 | ** to just "/". |
| 2761 | */ |
| 2762 | if( zFull ){ |
| 2763 | int i, j; |
| 2764 | for(i=j=0; zFull[i]; i++){ |
| 2765 | if( zFull[i]=='/' ){ |
| 2766 | if( zFull[i+1]=='/' ) continue; |
| 2767 | if( zFull[i+1]=='.' && zFull[i+2]=='/' ){ |
| 2768 | i += 1; |
| 2769 | continue; |
| 2770 | } |
| 2771 | if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){ |
| 2772 | while( j>0 && zFull[j-1]!='/' ){ j--; } |
| 2773 | i += 3; |
| 2774 | continue; |
| 2775 | } |
| 2776 | } |
| 2777 | zFull[j++] = zFull[i]; |
| 2778 | } |
| 2779 | zFull[j] = 0; |
| 2780 | } |
| 2781 | #endif |
| 2782 | } |
| 2783 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 2784 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 2785 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 2786 | /* |
| 2787 | ** Interfaces for opening a shared library, finding entry points |
| 2788 | ** within the shared library, and closing the shared library. |
| 2789 | */ |
| 2790 | #include <dlfcn.h> |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2791 | static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){ |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 2792 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 2793 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 2794 | |
| 2795 | /* |
| 2796 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 2797 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 2798 | ** message is available, it is written to zBufOut. If no error message |
| 2799 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 2800 | ** error message. |
| 2801 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2802 | static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2803 | char *zErr; |
| 2804 | enterMutex(); |
| 2805 | zErr = dlerror(); |
| 2806 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2807 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2808 | } |
| 2809 | leaveMutex(); |
| 2810 | } |
drh | 46c99e0 | 2007-08-27 23:26:59 +0000 | [diff] [blame] | 2811 | static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){ |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 2812 | return dlsym(pHandle, zSymbol); |
| 2813 | } |
drh | 46c99e0 | 2007-08-27 23:26:59 +0000 | [diff] [blame] | 2814 | static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2815 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 2816 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2817 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 2818 | #define unixDlOpen 0 |
| 2819 | #define unixDlError 0 |
| 2820 | #define unixDlSym 0 |
| 2821 | #define unixDlClose 0 |
| 2822 | #endif |
| 2823 | |
| 2824 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2825 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2826 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2827 | static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2828 | |
| 2829 | assert(nBuf>=(sizeof(time_t)+sizeof(int))); |
| 2830 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2831 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 2832 | ** errors. The reports issued by valgrind are incorrect - we would |
| 2833 | ** prefer that the randomness be increased by making use of the |
| 2834 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 2835 | ** some users. Rather than argue, it seems easier just to initialize |
| 2836 | ** the whole array and silence valgrind, even if that means less randomness |
| 2837 | ** in the random seed. |
| 2838 | ** |
| 2839 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 2840 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2841 | ** tests repeatable. |
| 2842 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2843 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2844 | #if !defined(SQLITE_TEST) |
| 2845 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 2846 | int pid, fd; |
| 2847 | fd = open("/dev/urandom", O_RDONLY); |
| 2848 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 2849 | time_t t; |
| 2850 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 2851 | memcpy(zBuf, &t, sizeof(t)); |
| 2852 | pid = getpid(); |
| 2853 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 2854 | }else{ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2855 | read(fd, zBuf, nBuf); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 2856 | close(fd); |
| 2857 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2858 | } |
| 2859 | #endif |
| 2860 | return SQLITE_OK; |
| 2861 | } |
| 2862 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2863 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2864 | /* |
| 2865 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2866 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 2867 | ** The return value is the number of microseconds of sleep actually |
| 2868 | ** requested from the underlying operating system, a number which |
| 2869 | ** might be greater than or equal to the argument, but not less |
| 2870 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2871 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2872 | static int unixSleep(sqlite3_vfs *pVfs, int microseconds){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2873 | #if defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2874 | usleep(microseconds); |
| 2875 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2876 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2877 | int seconds = (microseconds+999999)/1000000; |
| 2878 | sleep(seconds); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 2879 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 2880 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2884 | ** The following variable, if set to a non-zero value, becomes the result |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 2885 | ** returned from sqlite3OsCurrentTime(). This is used for testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2886 | */ |
| 2887 | #ifdef SQLITE_TEST |
| 2888 | int sqlite3_current_time = 0; |
| 2889 | #endif |
| 2890 | |
| 2891 | /* |
| 2892 | ** Find the current time (in Universal Coordinated Time). Write the |
| 2893 | ** current time and date as a Julian Day number into *prNow and |
| 2894 | ** return 0. Return 1 if the time and date cannot be found. |
| 2895 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2896 | static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2897 | #ifdef NO_GETTOD |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2898 | time_t t; |
| 2899 | time(&t); |
| 2900 | *prNow = t/86400.0 + 2440587.5; |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2901 | #else |
| 2902 | struct timeval sNow; |
drh | bdcc276 | 2007-04-02 18:06:57 +0000 | [diff] [blame] | 2903 | gettimeofday(&sNow, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 2904 | *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0; |
| 2905 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2906 | #ifdef SQLITE_TEST |
| 2907 | if( sqlite3_current_time ){ |
| 2908 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 2909 | } |
| 2910 | #endif |
| 2911 | return 0; |
| 2912 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2913 | |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 2914 | static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
| 2915 | return 0; |
| 2916 | } |
| 2917 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2918 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2919 | ** Initialize the operating system interface. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2920 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 2921 | int sqlite3_os_init(void){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2922 | /* Macro to define the static contents of an sqlite3_vfs structure for |
| 2923 | ** the unix backend. The two parameters are the values to use for |
| 2924 | ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively. |
| 2925 | ** |
| 2926 | */ |
| 2927 | #define UNIXVFS(zVfsName, pVfsAppData) { \ |
| 2928 | 1, /* iVersion */ \ |
| 2929 | sizeof(unixFile), /* szOsFile */ \ |
| 2930 | MAX_PATHNAME, /* mxPathname */ \ |
| 2931 | 0, /* pNext */ \ |
| 2932 | zVfsName, /* zName */ \ |
| 2933 | (void *)pVfsAppData, /* pAppData */ \ |
| 2934 | unixOpen, /* xOpen */ \ |
| 2935 | unixDelete, /* xDelete */ \ |
| 2936 | unixAccess, /* xAccess */ \ |
| 2937 | unixFullPathname, /* xFullPathname */ \ |
| 2938 | unixDlOpen, /* xDlOpen */ \ |
| 2939 | unixDlError, /* xDlError */ \ |
| 2940 | unixDlSym, /* xDlSym */ \ |
| 2941 | unixDlClose, /* xDlClose */ \ |
| 2942 | unixRandomness, /* xRandomness */ \ |
| 2943 | unixSleep, /* xSleep */ \ |
| 2944 | unixCurrentTime, /* xCurrentTime */ \ |
| 2945 | unixGetLastError /* xGetLastError */ \ |
| 2946 | } |
| 2947 | |
| 2948 | static sqlite3_vfs unixVfs = UNIXVFS("unix", 0); |
| 2949 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2950 | int i; |
| 2951 | static sqlite3_vfs aVfs[] = { |
| 2952 | UNIXVFS("unix-posix", LOCKING_STYLE_POSIX), |
| 2953 | UNIXVFS("unix-afp", LOCKING_STYLE_AFP), |
| 2954 | UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK), |
| 2955 | UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE), |
| 2956 | UNIXVFS("unix-none", LOCKING_STYLE_NONE) |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2957 | }; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2958 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
| 2959 | sqlite3_vfs_register(&aVfs[i], 0); |
| 2960 | } |
| 2961 | #endif |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 2962 | sqlite3_vfs_register(&unixVfs, 1); |
| 2963 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2964 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2965 | |
| 2966 | /* |
| 2967 | ** Shutdown the operating system interface. This is a no-op for unix. |
| 2968 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 2969 | int sqlite3_os_end(void){ |
| 2970 | return SQLITE_OK; |
| 2971 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 2972 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 2973 | #endif /* SQLITE_OS_UNIX */ |