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