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 | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 19 | /* |
| 20 | ** These #defines should enable >2GB file support on Posix if the |
| 21 | ** underlying operating system supports it. If the OS lacks |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 22 | ** large file support, these should be no-ops. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 23 | ** |
| 24 | ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |
| 25 | ** on the compiler command line. This is necessary if you are compiling |
| 26 | ** on a recent machine (ex: RedHat 7.2) but you want your code to work |
| 27 | ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 |
| 28 | ** without this option, LFS is enable. But LFS does not exist in the kernel |
| 29 | ** in RedHat 6.0, so the code won't work. Hence, for maximum binary |
| 30 | ** portability you should omit LFS. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 31 | */ |
| 32 | #ifndef SQLITE_DISABLE_LFS |
| 33 | # define _LARGE_FILE 1 |
| 34 | # ifndef _FILE_OFFSET_BITS |
| 35 | # define _FILE_OFFSET_BITS 64 |
| 36 | # endif |
| 37 | # define _LARGEFILE_SOURCE 1 |
| 38 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 39 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 40 | /* |
| 41 | ** standard include files. |
| 42 | */ |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/stat.h> |
| 45 | #include <fcntl.h> |
| 46 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 47 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 48 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 49 | #include <errno.h> |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 52 | ** If we are to be thread-safe, include the pthreads header and define |
| 53 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 54 | */ |
| 55 | #if defined(THREADSAFE) && THREADSAFE |
| 56 | # include <pthread.h> |
| 57 | # define SQLITE_UNIX_THREADS 1 |
| 58 | #endif |
| 59 | |
| 60 | /* |
| 61 | ** Default permissions when creating a new file |
| 62 | */ |
| 63 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 64 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 65 | #endif |
| 66 | |
| 67 | |
| 68 | |
| 69 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 70 | ** The unixFile structure is subclass of OsFile specific for the unix |
| 71 | ** protability layer. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 72 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 73 | typedef struct unixFile unixFile; |
| 74 | struct unixFile { |
| 75 | IoMethod const *pMethod; /* Always the first entry */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 76 | struct openCnt *pOpen; /* Info about all open fd's on this inode */ |
| 77 | struct lockInfo *pLock; /* Info about locks on this inode */ |
| 78 | int h; /* The file descriptor */ |
| 79 | unsigned char locktype; /* The type of lock held on this fd */ |
| 80 | unsigned char isOpen; /* True if needs to be closed */ |
| 81 | unsigned char fullSync; /* Use F_FULLSYNC if available */ |
| 82 | int dirfd; /* File descriptor for the directory */ |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 83 | i64 offset; /* Seek offset */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 84 | #ifdef SQLITE_UNIX_THREADS |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 85 | pthread_t tid; /* The thread that "owns" this OsFile */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 86 | #endif |
| 87 | }; |
| 88 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 89 | /* |
| 90 | ** Provide the ability to override some OS-layer functions during |
| 91 | ** testing. This is used to simulate OS crashes to verify that |
| 92 | ** commits are atomic even in the event of an OS crash. |
| 93 | */ |
| 94 | #ifdef SQLITE_CRASH_TEST |
| 95 | extern int sqlite3CrashTestEnable; |
| 96 | extern int sqlite3CrashOpenReadWrite(const char*, OsFile**, int*); |
| 97 | extern int sqlite3CrashOpenExclusive(const char*, OsFile**, int); |
| 98 | extern int sqlite3CrashOpenReadOnly(const char*, OsFile**, int); |
| 99 | # define CRASH_TEST_OVERRIDE(X,A,B,C) \ |
| 100 | if(sqlite3CrashTestEnable){ return X(A,B,C); } |
| 101 | #else |
| 102 | # define CRASH_TEST_OVERRIDE(X,A,B,C) /* no-op */ |
| 103 | #endif |
| 104 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 105 | |
| 106 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 107 | ** Include code that is common to all os_*.c files |
| 108 | */ |
| 109 | #include "os_common.h" |
| 110 | |
| 111 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 112 | ** Do not include any of the File I/O interface procedures if the |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 113 | ** SQLITE_OMIT_DISKIO macro is defined (indicating that the database |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 114 | ** will be in-memory only) |
| 115 | */ |
| 116 | #ifndef SQLITE_OMIT_DISKIO |
| 117 | |
| 118 | |
| 119 | /* |
| 120 | ** Define various macros that are missing from some systems. |
| 121 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 122 | #ifndef O_LARGEFILE |
| 123 | # define O_LARGEFILE 0 |
| 124 | #endif |
| 125 | #ifdef SQLITE_DISABLE_LFS |
| 126 | # undef O_LARGEFILE |
| 127 | # define O_LARGEFILE 0 |
| 128 | #endif |
| 129 | #ifndef O_NOFOLLOW |
| 130 | # define O_NOFOLLOW 0 |
| 131 | #endif |
| 132 | #ifndef O_BINARY |
| 133 | # define O_BINARY 0 |
| 134 | #endif |
| 135 | |
| 136 | /* |
| 137 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 138 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 139 | ** that always succeeds. This means that locking does not occur under |
danielk1977 | 26c5d79 | 2005-11-25 09:01:23 +0000 | [diff] [blame] | 140 | ** DJGPP. But it's DOS - what did you expect? |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 141 | */ |
| 142 | #ifdef __DJGPP__ |
| 143 | # define fcntl(A,B,C) 0 |
| 144 | #endif |
| 145 | |
| 146 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 147 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 148 | ** testing and debugging only. |
| 149 | */ |
| 150 | #ifdef SQLITE_UNIX_THREADS |
| 151 | #define threadid pthread_self() |
| 152 | #else |
| 153 | #define threadid 0 |
| 154 | #endif |
| 155 | |
| 156 | /* |
| 157 | ** Set or check the OsFile.tid field. This field is set when an OsFile |
| 158 | ** is first opened. All subsequent uses of the OsFile verify that the |
| 159 | ** same thread is operating on the OsFile. Some operating systems do |
| 160 | ** not allow locks to be overridden by other threads and that restriction |
| 161 | ** means that sqlite3* database handles cannot be moved from one thread |
| 162 | ** to another. This logic makes sure a user does not try to do that |
| 163 | ** by mistake. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 164 | ** |
| 165 | ** Version 3.3.1 (2006-01-15): OsFiles can be moved from one thread to |
| 166 | ** another as long as we are running on a system that supports threads |
| 167 | ** overriding each others locks (which now the most common behavior) |
| 168 | ** or if no locks are held. But the OsFile.pLock field needs to be |
| 169 | ** recomputed because its key includes the thread-id. See the |
| 170 | ** transferOwnership() function below for additional information |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 171 | */ |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 172 | #if defined(SQLITE_UNIX_THREADS) |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 173 | # define SET_THREADID(X) (X)->tid = pthread_self() |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 174 | # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \ |
| 175 | !pthread_equal((X)->tid, pthread_self())) |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 176 | #else |
| 177 | # define SET_THREADID(X) |
| 178 | # define CHECK_THREADID(X) 0 |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 181 | /* |
| 182 | ** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996) |
| 183 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 184 | ** sets or clears a lock, that operation overrides any prior locks set |
| 185 | ** by the same process. It does not explicitly say so, but this implies |
| 186 | ** that it overrides locks set by the same process using a different |
| 187 | ** file descriptor. Consider this test case: |
| 188 | ** |
| 189 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
| 190 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 191 | ** |
| 192 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 193 | ** one is a hard or symbolic link to the other) then if you set |
| 194 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 195 | ** on fd2, it works. I would have expected the second lock to |
| 196 | ** fail since there was already a lock on the file due to fd1. |
| 197 | ** But not so. Since both locks came from the same process, the |
| 198 | ** second overrides the first, even though they were on different |
| 199 | ** file descriptors opened on different file names. |
| 200 | ** |
| 201 | ** Bummer. If you ask me, this is broken. Badly broken. It means |
| 202 | ** that we cannot use POSIX locks to synchronize file access among |
| 203 | ** competing threads of the same process. POSIX locks will work fine |
| 204 | ** to synchronize access for threads in separate processes, but not |
| 205 | ** threads within the same process. |
| 206 | ** |
| 207 | ** To work around the problem, SQLite has to manage file locks internally |
| 208 | ** on its own. Whenever a new database is opened, we have to find the |
| 209 | ** specific inode of the database file (the inode is determined by the |
| 210 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 211 | ** and check for locks already existing on that inode. When locks are |
| 212 | ** created or removed, we have to look at our own internal record of the |
| 213 | ** locks to see if another thread has previously set a lock on that same |
| 214 | ** inode. |
| 215 | ** |
| 216 | ** The OsFile structure for POSIX is no longer just an integer file |
| 217 | ** descriptor. It is now a structure that holds the integer file |
| 218 | ** descriptor and a pointer to a structure that describes the internal |
| 219 | ** locks on the corresponding inode. There is one locking structure |
| 220 | ** per inode, so if the same inode is opened twice, both OsFile structures |
| 221 | ** point to the same locking structure. The locking structure keeps |
| 222 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 223 | ** field that tells us its internal lock status. cnt==0 means the |
| 224 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 225 | ** cnt>0 means there are cnt shared locks on the file. |
| 226 | ** |
| 227 | ** Any attempt to lock or unlock a file first checks the locking |
| 228 | ** structure. The fcntl() system call is only invoked to set a |
| 229 | ** POSIX lock if the internal lock structure transitions between |
| 230 | ** a locked and an unlocked state. |
| 231 | ** |
| 232 | ** 2004-Jan-11: |
| 233 | ** More recent discoveries about POSIX advisory locks. (The more |
| 234 | ** I discover, the more I realize the a POSIX advisory locks are |
| 235 | ** an abomination.) |
| 236 | ** |
| 237 | ** If you close a file descriptor that points to a file that has locks, |
| 238 | ** all locks on that file that are owned by the current process are |
| 239 | ** released. To work around this problem, each OsFile structure contains |
| 240 | ** a pointer to an openCnt structure. There is one openCnt structure |
| 241 | ** per open inode, which means that multiple OsFiles can point to a single |
| 242 | ** openCnt. When an attempt is made to close an OsFile, if there are |
| 243 | ** other OsFiles open on the same inode that are holding locks, the call |
| 244 | ** to close() the file descriptor is deferred until all of the locks clear. |
| 245 | ** The openCnt structure keeps a list of file descriptors that need to |
| 246 | ** be closed and that list is walked (and cleared) when the last lock |
| 247 | ** clears. |
| 248 | ** |
| 249 | ** First, under Linux threads, because each thread has a separate |
| 250 | ** process ID, lock operations in one thread do not override locks |
| 251 | ** to the same file in other threads. Linux threads behave like |
| 252 | ** separate processes in this respect. But, if you close a file |
| 253 | ** descriptor in linux threads, all locks are cleared, even locks |
| 254 | ** on other threads and even though the other threads have different |
| 255 | ** process IDs. Linux threads is inconsistent in this respect. |
| 256 | ** (I'm beginning to think that linux threads is an abomination too.) |
| 257 | ** The consequence of this all is that the hash table for the lockInfo |
| 258 | ** structure has to include the process id as part of its key because |
| 259 | ** locks in different threads are treated as distinct. But the |
| 260 | ** openCnt structure should not include the process id in its |
| 261 | ** key because close() clears lock on all threads, not just the current |
| 262 | ** thread. Were it not for this goofiness in linux threads, we could |
| 263 | ** combine the lockInfo and openCnt structures into a single structure. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 264 | ** |
| 265 | ** 2004-Jun-28: |
| 266 | ** On some versions of linux, threads can override each others locks. |
| 267 | ** On others not. Sometimes you can change the behavior on the same |
| 268 | ** system by setting the LD_ASSUME_KERNEL environment variable. The |
| 269 | ** POSIX standard is silent as to which behavior is correct, as far |
| 270 | ** as I can tell, so other versions of unix might show the same |
| 271 | ** inconsistency. There is no little doubt in my mind that posix |
| 272 | ** advisory locks and linux threads are profoundly broken. |
| 273 | ** |
| 274 | ** To work around the inconsistencies, we have to test at runtime |
| 275 | ** whether or not threads can override each others locks. This test |
| 276 | ** is run once, the first time any lock is attempted. A static |
| 277 | ** variable is set to record the results of this test for future |
| 278 | ** use. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 279 | */ |
| 280 | |
| 281 | /* |
| 282 | ** An instance of the following structure serves as the key used |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 283 | ** to locate a particular lockInfo structure given its inode. |
| 284 | ** |
| 285 | ** If threads cannot override each others locks, then we set the |
| 286 | ** lockKey.tid field to the thread ID. If threads can override |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 287 | ** each others locks then tid is always set to zero. tid is omitted |
| 288 | ** if we compile without threading support. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 289 | */ |
| 290 | struct lockKey { |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 291 | dev_t dev; /* Device number */ |
| 292 | ino_t ino; /* Inode number */ |
| 293 | #ifdef SQLITE_UNIX_THREADS |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 294 | pthread_t tid; /* Thread ID or zero if threads can override each other */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 295 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | /* |
| 299 | ** An instance of the following structure is allocated for each open |
| 300 | ** inode on each thread with a different process ID. (Threads have |
| 301 | ** different process IDs on linux, but not on most other unixes.) |
| 302 | ** |
| 303 | ** A single inode can have multiple file descriptors, so each OsFile |
| 304 | ** structure contains a pointer to an instance of this object and this |
| 305 | ** object keeps a count of the number of OsFiles pointing to it. |
| 306 | */ |
| 307 | struct lockInfo { |
| 308 | struct lockKey key; /* The lookup key */ |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 309 | int cnt; /* Number of SHARED locks held */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 310 | int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 311 | int nRef; /* Number of pointers to this structure */ |
| 312 | }; |
| 313 | |
| 314 | /* |
| 315 | ** An instance of the following structure serves as the key used |
| 316 | ** to locate a particular openCnt structure given its inode. This |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 317 | ** is the same as the lockKey except that the thread ID is omitted. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 318 | */ |
| 319 | struct openKey { |
| 320 | dev_t dev; /* Device number */ |
| 321 | ino_t ino; /* Inode number */ |
| 322 | }; |
| 323 | |
| 324 | /* |
| 325 | ** An instance of the following structure is allocated for each open |
| 326 | ** inode. This structure keeps track of the number of locks on that |
| 327 | ** inode. If a close is attempted against an inode that is holding |
| 328 | ** locks, the close is deferred until all locks clear by adding the |
| 329 | ** file descriptor to be closed to the pending list. |
| 330 | */ |
| 331 | struct openCnt { |
| 332 | struct openKey key; /* The lookup key */ |
| 333 | int nRef; /* Number of pointers to this structure */ |
| 334 | int nLock; /* Number of outstanding locks */ |
| 335 | int nPending; /* Number of pending close() operations */ |
| 336 | int *aPending; /* Malloced space holding fd's awaiting a close() */ |
| 337 | }; |
| 338 | |
| 339 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 340 | ** These hash tables map inodes and file descriptors (really, lockKey and |
| 341 | ** openKey structures) into lockInfo and openCnt structures. Access to |
| 342 | ** these hash tables must be protected by a mutex. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 343 | */ |
danielk1977 | 750b03e | 2006-02-14 10:48:39 +0000 | [diff] [blame] | 344 | static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, |
| 345 | sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0}; |
| 346 | static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, |
| 347 | sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0}; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 348 | |
| 349 | #ifdef SQLITE_UNIX_THREADS |
| 350 | /* |
| 351 | ** This variable records whether or not threads can override each others |
| 352 | ** locks. |
| 353 | ** |
| 354 | ** 0: No. Threads cannot override each others locks. |
| 355 | ** 1: Yes. Threads can override each others locks. |
| 356 | ** -1: We don't know yet. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 357 | ** |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 358 | ** On some systems, we know at compile-time if threads can override each |
| 359 | ** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro |
| 360 | ** will be set appropriately. On other systems, we have to check at |
| 361 | ** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is |
| 362 | ** undefined. |
| 363 | ** |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 364 | ** This variable normally has file scope only. But during testing, we make |
| 365 | ** it a global so that the test code can change its value in order to verify |
| 366 | ** that the right stuff happens in either case. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 367 | */ |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 368 | #ifndef SQLITE_THREAD_OVERRIDE_LOCK |
| 369 | # define SQLITE_THREAD_OVERRIDE_LOCK -1 |
| 370 | #endif |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 371 | #ifdef SQLITE_TEST |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 372 | int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 373 | #else |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 374 | static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 375 | #endif |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 376 | |
| 377 | /* |
| 378 | ** This structure holds information passed into individual test |
| 379 | ** threads by the testThreadLockingBehavior() routine. |
| 380 | */ |
| 381 | struct threadTestData { |
| 382 | int fd; /* File to be locked */ |
| 383 | struct flock lock; /* The locking operation */ |
| 384 | int result; /* Result of the locking operation */ |
| 385 | }; |
| 386 | |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 387 | #ifdef SQLITE_LOCK_TRACE |
| 388 | /* |
| 389 | ** Print out information about all locking operations. |
| 390 | ** |
| 391 | ** This routine is used for troubleshooting locks on multithreaded |
| 392 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 393 | ** command-line option on the compiler. This code is normally |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 394 | ** turned off. |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 395 | */ |
| 396 | static int lockTrace(int fd, int op, struct flock *p){ |
| 397 | char *zOpName, *zType; |
| 398 | int s; |
| 399 | int savedErrno; |
| 400 | if( op==F_GETLK ){ |
| 401 | zOpName = "GETLK"; |
| 402 | }else if( op==F_SETLK ){ |
| 403 | zOpName = "SETLK"; |
| 404 | }else{ |
| 405 | s = fcntl(fd, op, p); |
| 406 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 407 | return s; |
| 408 | } |
| 409 | if( p->l_type==F_RDLCK ){ |
| 410 | zType = "RDLCK"; |
| 411 | }else if( p->l_type==F_WRLCK ){ |
| 412 | zType = "WRLCK"; |
| 413 | }else if( p->l_type==F_UNLCK ){ |
| 414 | zType = "UNLCK"; |
| 415 | }else{ |
| 416 | assert( 0 ); |
| 417 | } |
| 418 | assert( p->l_whence==SEEK_SET ); |
| 419 | s = fcntl(fd, op, p); |
| 420 | savedErrno = errno; |
| 421 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 422 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 423 | (int)p->l_pid, s); |
| 424 | if( s && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 425 | struct flock l2; |
| 426 | l2 = *p; |
| 427 | fcntl(fd, F_GETLK, &l2); |
| 428 | if( l2.l_type==F_RDLCK ){ |
| 429 | zType = "RDLCK"; |
| 430 | }else if( l2.l_type==F_WRLCK ){ |
| 431 | zType = "WRLCK"; |
| 432 | }else if( l2.l_type==F_UNLCK ){ |
| 433 | zType = "UNLCK"; |
| 434 | }else{ |
| 435 | assert( 0 ); |
| 436 | } |
| 437 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 438 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 439 | } |
| 440 | errno = savedErrno; |
| 441 | return s; |
| 442 | } |
| 443 | #define fcntl lockTrace |
| 444 | #endif /* SQLITE_LOCK_TRACE */ |
| 445 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 446 | /* |
| 447 | ** The testThreadLockingBehavior() routine launches two separate |
| 448 | ** threads on this routine. This routine attempts to lock a file |
| 449 | ** descriptor then returns. The success or failure of that attempt |
| 450 | ** allows the testThreadLockingBehavior() procedure to determine |
| 451 | ** whether or not threads can override each others locks. |
| 452 | */ |
| 453 | static void *threadLockingTest(void *pArg){ |
| 454 | struct threadTestData *pData = (struct threadTestData*)pArg; |
| 455 | pData->result = fcntl(pData->fd, F_SETLK, &pData->lock); |
| 456 | return pArg; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | ** This procedure attempts to determine whether or not threads |
| 461 | ** can override each others locks then sets the |
| 462 | ** threadsOverrideEachOthersLocks variable appropriately. |
| 463 | */ |
danielk1977 | 4d5238f | 2006-01-27 06:32:00 +0000 | [diff] [blame] | 464 | static void testThreadLockingBehavior(int fd_orig){ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 465 | int fd; |
| 466 | struct threadTestData d[2]; |
| 467 | pthread_t t[2]; |
| 468 | |
| 469 | fd = dup(fd_orig); |
| 470 | if( fd<0 ) return; |
| 471 | memset(d, 0, sizeof(d)); |
| 472 | d[0].fd = fd; |
| 473 | d[0].lock.l_type = F_RDLCK; |
| 474 | d[0].lock.l_len = 1; |
| 475 | d[0].lock.l_start = 0; |
| 476 | d[0].lock.l_whence = SEEK_SET; |
| 477 | d[1] = d[0]; |
| 478 | d[1].lock.l_type = F_WRLCK; |
| 479 | pthread_create(&t[0], 0, threadLockingTest, &d[0]); |
| 480 | pthread_create(&t[1], 0, threadLockingTest, &d[1]); |
| 481 | pthread_join(t[0], 0); |
| 482 | pthread_join(t[1], 0); |
| 483 | close(fd); |
| 484 | threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0; |
| 485 | } |
| 486 | #endif /* SQLITE_UNIX_THREADS */ |
| 487 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 488 | /* |
| 489 | ** Release a lockInfo structure previously allocated by findLockInfo(). |
| 490 | */ |
| 491 | static void releaseLockInfo(struct lockInfo *pLock){ |
drh | 757b04e | 2006-01-18 17:25:45 +0000 | [diff] [blame] | 492 | assert( sqlite3OsInMutex(1) ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 493 | pLock->nRef--; |
| 494 | if( pLock->nRef==0 ){ |
| 495 | sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0); |
danielk1977 | 750b03e | 2006-02-14 10:48:39 +0000 | [diff] [blame] | 496 | sqlite3ThreadSafeFree(pLock); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | ** Release a openCnt structure previously allocated by findLockInfo(). |
| 502 | */ |
| 503 | static void releaseOpenCnt(struct openCnt *pOpen){ |
drh | 757b04e | 2006-01-18 17:25:45 +0000 | [diff] [blame] | 504 | assert( sqlite3OsInMutex(1) ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 505 | pOpen->nRef--; |
| 506 | if( pOpen->nRef==0 ){ |
| 507 | sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0); |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 508 | free(pOpen->aPending); |
danielk1977 | 750b03e | 2006-02-14 10:48:39 +0000 | [diff] [blame] | 509 | sqlite3ThreadSafeFree(pOpen); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | ** Given a file descriptor, locate lockInfo and openCnt structures that |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 515 | ** describes that file descriptor. Create new ones if necessary. The |
| 516 | ** return values might be uninitialized if an error occurs. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 517 | ** |
| 518 | ** Return the number of errors. |
| 519 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 520 | static int findLockInfo( |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 521 | int fd, /* The file descriptor used in the key */ |
| 522 | struct lockInfo **ppLock, /* Return the lockInfo structure here */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 523 | struct openCnt **ppOpen /* Return the openCnt structure here */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 524 | ){ |
| 525 | int rc; |
| 526 | struct lockKey key1; |
| 527 | struct openKey key2; |
| 528 | struct stat statbuf; |
| 529 | struct lockInfo *pLock; |
| 530 | struct openCnt *pOpen; |
| 531 | rc = fstat(fd, &statbuf); |
| 532 | if( rc!=0 ) return 1; |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 533 | |
drh | 757b04e | 2006-01-18 17:25:45 +0000 | [diff] [blame] | 534 | assert( sqlite3OsInMutex(1) ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 535 | memset(&key1, 0, sizeof(key1)); |
| 536 | key1.dev = statbuf.st_dev; |
| 537 | key1.ino = statbuf.st_ino; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 538 | #ifdef SQLITE_UNIX_THREADS |
| 539 | if( threadsOverrideEachOthersLocks<0 ){ |
| 540 | testThreadLockingBehavior(fd); |
| 541 | } |
| 542 | key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self(); |
| 543 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 544 | memset(&key2, 0, sizeof(key2)); |
| 545 | key2.dev = statbuf.st_dev; |
| 546 | key2.ino = statbuf.st_ino; |
| 547 | pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1)); |
| 548 | if( pLock==0 ){ |
| 549 | struct lockInfo *pOld; |
danielk1977 | 750b03e | 2006-02-14 10:48:39 +0000 | [diff] [blame] | 550 | pLock = sqlite3ThreadSafeMalloc( sizeof(*pLock) ); |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 551 | if( pLock==0 ){ |
| 552 | rc = 1; |
| 553 | goto exit_findlockinfo; |
| 554 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 555 | pLock->key = key1; |
| 556 | pLock->nRef = 1; |
| 557 | pLock->cnt = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 558 | pLock->locktype = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 559 | pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock); |
| 560 | if( pOld!=0 ){ |
| 561 | assert( pOld==pLock ); |
danielk1977 | 750b03e | 2006-02-14 10:48:39 +0000 | [diff] [blame] | 562 | sqlite3ThreadSafeFree(pLock); |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 563 | rc = 1; |
| 564 | goto exit_findlockinfo; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 565 | } |
| 566 | }else{ |
| 567 | pLock->nRef++; |
| 568 | } |
| 569 | *ppLock = pLock; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 570 | if( ppOpen!=0 ){ |
| 571 | pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2)); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 572 | if( pOpen==0 ){ |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 573 | struct openCnt *pOld; |
danielk1977 | 750b03e | 2006-02-14 10:48:39 +0000 | [diff] [blame] | 574 | pOpen = sqlite3ThreadSafeMalloc( sizeof(*pOpen) ); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 575 | if( pOpen==0 ){ |
| 576 | releaseLockInfo(pLock); |
| 577 | rc = 1; |
| 578 | goto exit_findlockinfo; |
| 579 | } |
| 580 | pOpen->key = key2; |
| 581 | pOpen->nRef = 1; |
| 582 | pOpen->nLock = 0; |
| 583 | pOpen->nPending = 0; |
| 584 | pOpen->aPending = 0; |
| 585 | pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen); |
| 586 | if( pOld!=0 ){ |
| 587 | assert( pOld==pOpen ); |
danielk1977 | 750b03e | 2006-02-14 10:48:39 +0000 | [diff] [blame] | 588 | sqlite3ThreadSafeFree(pOpen); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 589 | releaseLockInfo(pLock); |
| 590 | rc = 1; |
| 591 | goto exit_findlockinfo; |
| 592 | } |
| 593 | }else{ |
| 594 | pOpen->nRef++; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 595 | } |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 596 | *ppOpen = pOpen; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 597 | } |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 598 | |
| 599 | exit_findlockinfo: |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 600 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 601 | } |
| 602 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 603 | #ifdef SQLITE_DEBUG |
| 604 | /* |
| 605 | ** Helper function for printing out trace information from debugging |
| 606 | ** binaries. This returns the string represetation of the supplied |
| 607 | ** integer lock-type. |
| 608 | */ |
| 609 | static const char *locktypeName(int locktype){ |
| 610 | switch( locktype ){ |
| 611 | case NO_LOCK: return "NONE"; |
| 612 | case SHARED_LOCK: return "SHARED"; |
| 613 | case RESERVED_LOCK: return "RESERVED"; |
| 614 | case PENDING_LOCK: return "PENDING"; |
| 615 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
| 616 | } |
| 617 | return "ERROR"; |
| 618 | } |
| 619 | #endif |
| 620 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 621 | /* |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 622 | ** If we are currently in a different thread than the thread that the |
| 623 | ** unixFile argument belongs to, then transfer ownership of the unixFile |
| 624 | ** over to the current thread. |
| 625 | ** |
| 626 | ** A unixFile is only owned by a thread on systems where one thread is |
| 627 | ** unable to override locks created by a different thread. RedHat9 is |
| 628 | ** an example of such a system. |
| 629 | ** |
| 630 | ** Ownership transfer is only allowed if the unixFile is currently unlocked. |
| 631 | ** If the unixFile is locked and an ownership is wrong, then return |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 632 | ** SQLITE_MISUSE. SQLITE_OK is returned if everything works. |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 633 | */ |
| 634 | #ifdef SQLITE_UNIX_THREADS |
| 635 | static int transferOwnership(unixFile *pFile){ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 636 | int rc; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 637 | pthread_t hSelf; |
| 638 | if( threadsOverrideEachOthersLocks ){ |
| 639 | /* Ownership transfers not needed on this system */ |
| 640 | return SQLITE_OK; |
| 641 | } |
| 642 | hSelf = pthread_self(); |
| 643 | if( pthread_equal(pFile->tid, hSelf) ){ |
| 644 | /* We are still in the same thread */ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 645 | TRACE1("No-transfer, same thread\n"); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 646 | return SQLITE_OK; |
| 647 | } |
| 648 | if( pFile->locktype!=NO_LOCK ){ |
| 649 | /* We cannot change ownership while we are holding a lock! */ |
| 650 | return SQLITE_MISUSE; |
| 651 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 652 | TRACE4("Transfer ownership of %d from %d to %d\n", pFile->h,pFile->tid,hSelf); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 653 | pFile->tid = hSelf; |
| 654 | releaseLockInfo(pFile->pLock); |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 655 | rc = findLockInfo(pFile->h, &pFile->pLock, 0); |
| 656 | TRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h, |
| 657 | locktypeName(pFile->locktype), |
| 658 | locktypeName(pFile->pLock->locktype), pFile->pLock->cnt); |
| 659 | return rc; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 660 | } |
| 661 | #else |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 662 | /* On single-threaded builds, ownership transfer is a no-op */ |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 663 | # define transferOwnership(X) SQLITE_OK |
| 664 | #endif |
| 665 | |
| 666 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 667 | ** Delete the named file |
| 668 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 669 | int sqlite3UnixDelete(const char *zFilename){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 670 | unlink(zFilename); |
| 671 | return SQLITE_OK; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | ** Return TRUE if the named file exists. |
| 676 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 677 | int sqlite3UnixFileExists(const char *zFilename){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 678 | return access(zFilename, 0)==0; |
| 679 | } |
| 680 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 681 | /* Forward declaration */ |
| 682 | static int allocateUnixFile(unixFile *pInit, OsFile **pId); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 683 | |
| 684 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 685 | ** Attempt to open a file for both reading and writing. If that |
| 686 | ** fails, try opening it read-only. If the file does not exist, |
| 687 | ** try to create it. |
| 688 | ** |
| 689 | ** On success, a handle for the open file is written to *id |
| 690 | ** and *pReadonly is set to 0 if the file was opened for reading and |
| 691 | ** writing or 1 if the file was opened read-only. The function returns |
| 692 | ** SQLITE_OK. |
| 693 | ** |
| 694 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 695 | ** *id and *pReadonly unchanged. |
| 696 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 697 | int sqlite3UnixOpenReadWrite( |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 698 | const char *zFilename, |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 699 | OsFile **pId, |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 700 | int *pReadonly |
| 701 | ){ |
| 702 | int rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 703 | unixFile f; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 704 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 705 | CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadWrite, zFilename, pId, pReadonly); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 706 | assert( 0==*pId ); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 707 | f.h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY, |
drh | 8e85577 | 2005-05-17 11:25:31 +0000 | [diff] [blame] | 708 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 709 | if( f.h<0 ){ |
drh | 6458e39 | 2004-07-20 01:14:13 +0000 | [diff] [blame] | 710 | #ifdef EISDIR |
| 711 | if( errno==EISDIR ){ |
| 712 | return SQLITE_CANTOPEN; |
| 713 | } |
| 714 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 715 | f.h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY); |
| 716 | if( f.h<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 717 | return SQLITE_CANTOPEN; |
| 718 | } |
| 719 | *pReadonly = 1; |
| 720 | }else{ |
| 721 | *pReadonly = 0; |
| 722 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 723 | sqlite3OsEnterMutex(); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 724 | rc = findLockInfo(f.h, &f.pLock, &f.pOpen); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 725 | sqlite3OsLeaveMutex(); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 726 | if( rc ){ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 727 | close(f.h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 728 | return SQLITE_NOMEM; |
| 729 | } |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 730 | TRACE3("OPEN %-3d %s\n", f.h, zFilename); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 731 | return allocateUnixFile(&f, pId); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | |
| 735 | /* |
| 736 | ** Attempt to open a new file for exclusive access by this process. |
| 737 | ** The file will be opened for both reading and writing. To avoid |
| 738 | ** a potential security problem, we do not allow the file to have |
| 739 | ** previously existed. Nor do we allow the file to be a symbolic |
| 740 | ** link. |
| 741 | ** |
| 742 | ** If delFlag is true, then make arrangements to automatically delete |
| 743 | ** the file when it is closed. |
| 744 | ** |
| 745 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 746 | ** |
| 747 | ** On failure, return SQLITE_CANTOPEN. |
| 748 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 749 | int sqlite3UnixOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 750 | int rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 751 | unixFile f; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 752 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 753 | CRASH_TEST_OVERRIDE(sqlite3CrashOpenExclusive, zFilename, pId, delFlag); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 754 | assert( 0==*pId ); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 755 | f.h = open(zFilename, |
drh | d645967 | 2005-08-13 17:17:01 +0000 | [diff] [blame] | 756 | O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY, |
| 757 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 758 | if( f.h<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 759 | return SQLITE_CANTOPEN; |
| 760 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 761 | sqlite3OsEnterMutex(); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 762 | rc = findLockInfo(f.h, &f.pLock, &f.pOpen); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 763 | sqlite3OsLeaveMutex(); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 764 | if( rc ){ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 765 | close(f.h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 766 | unlink(zFilename); |
| 767 | return SQLITE_NOMEM; |
| 768 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 769 | if( delFlag ){ |
| 770 | unlink(zFilename); |
| 771 | } |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 772 | TRACE3("OPEN-EX %-3d %s\n", f.h, zFilename); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 773 | return allocateUnixFile(&f, pId); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
| 777 | ** Attempt to open a new file for read-only access. |
| 778 | ** |
| 779 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 780 | ** |
| 781 | ** On failure, return SQLITE_CANTOPEN. |
| 782 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 783 | int sqlite3UnixOpenReadOnly(const char *zFilename, OsFile **pId){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 784 | int rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 785 | unixFile f; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 786 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 787 | CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadOnly, zFilename, pId, 0); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 788 | assert( 0==*pId ); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 789 | f.h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY); |
| 790 | if( f.h<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 791 | return SQLITE_CANTOPEN; |
| 792 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 793 | sqlite3OsEnterMutex(); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 794 | rc = findLockInfo(f.h, &f.pLock, &f.pOpen); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 795 | sqlite3OsLeaveMutex(); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 796 | if( rc ){ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 797 | close(f.h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 798 | return SQLITE_NOMEM; |
| 799 | } |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 800 | TRACE3("OPEN-RO %-3d %s\n", f.h, zFilename); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 801 | return allocateUnixFile(&f, pId); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /* |
| 805 | ** Attempt to open a file descriptor for the directory that contains a |
| 806 | ** file. This file descriptor can be used to fsync() the directory |
| 807 | ** in order to make sure the creation of a new file is actually written |
| 808 | ** to disk. |
| 809 | ** |
| 810 | ** This routine is only meaningful for Unix. It is a no-op under |
| 811 | ** windows since windows does not support hard links. |
| 812 | ** |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 813 | ** On success, a handle for a previously open file at *id is |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 814 | ** updated with the new directory file descriptor and SQLITE_OK is |
| 815 | ** returned. |
| 816 | ** |
| 817 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 818 | ** *id unchanged. |
| 819 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 820 | static int unixOpenDirectory( |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 821 | OsFile *id, |
| 822 | const char *zDirname |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 823 | ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 824 | unixFile *pFile = (unixFile*)id; |
| 825 | if( pFile==0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 826 | /* Do not open the directory if the corresponding file is not already |
| 827 | ** open. */ |
| 828 | return SQLITE_CANTOPEN; |
| 829 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 830 | SET_THREADID(pFile); |
| 831 | assert( pFile->dirfd<0 ); |
| 832 | pFile->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0); |
| 833 | if( pFile->dirfd<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 834 | return SQLITE_CANTOPEN; |
| 835 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 836 | TRACE3("OPENDIR %-3d %s\n", pFile->dirfd, zDirname); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 837 | return SQLITE_OK; |
| 838 | } |
| 839 | |
| 840 | /* |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 841 | ** If the following global variable points to a string which is the |
| 842 | ** name of a directory, then that directory will be used to store |
| 843 | ** temporary files. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 844 | ** |
| 845 | ** See also the "PRAGMA temp_store_directory" SQL command. |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 846 | */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 847 | char *sqlite3_temp_directory = 0; |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 848 | |
| 849 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 850 | ** Create a temporary file name in zBuf. zBuf must be big enough to |
| 851 | ** hold at least SQLITE_TEMPNAME_SIZE characters. |
| 852 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 853 | int sqlite3UnixTempFileName(char *zBuf){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 854 | static const char *azDirs[] = { |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 855 | 0, |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 856 | "/var/tmp", |
| 857 | "/usr/tmp", |
| 858 | "/tmp", |
| 859 | ".", |
| 860 | }; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 861 | static const unsigned char zChars[] = |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 862 | "abcdefghijklmnopqrstuvwxyz" |
| 863 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 864 | "0123456789"; |
| 865 | int i, j; |
| 866 | struct stat buf; |
| 867 | const char *zDir = "."; |
drh | effd02b | 2004-08-29 23:42:13 +0000 | [diff] [blame] | 868 | azDirs[0] = sqlite3_temp_directory; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 869 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
drh | ab3f9fe | 2004-08-14 17:10:10 +0000 | [diff] [blame] | 870 | if( azDirs[i]==0 ) continue; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 871 | if( stat(azDirs[i], &buf) ) continue; |
| 872 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 873 | if( access(azDirs[i], 07) ) continue; |
| 874 | zDir = azDirs[i]; |
| 875 | break; |
| 876 | } |
| 877 | do{ |
| 878 | sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir); |
| 879 | j = strlen(zBuf); |
| 880 | sqlite3Randomness(15, &zBuf[j]); |
| 881 | for(i=0; i<15; i++, j++){ |
| 882 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 883 | } |
| 884 | zBuf[j] = 0; |
| 885 | }while( access(zBuf,0)==0 ); |
| 886 | return SQLITE_OK; |
| 887 | } |
| 888 | |
| 889 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 890 | ** Check that a given pathname is a directory and is writable |
| 891 | ** |
| 892 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 893 | int sqlite3UnixIsDirWritable(char *zBuf){ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 894 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 895 | struct stat buf; |
| 896 | if( zBuf==0 ) return 0; |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 897 | if( zBuf[0]==0 ) return 0; |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 898 | if( stat(zBuf, &buf) ) return 0; |
| 899 | if( !S_ISDIR(buf.st_mode) ) return 0; |
| 900 | if( access(zBuf, 07) ) return 0; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 901 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 902 | return 1; |
| 903 | } |
| 904 | |
| 905 | /* |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 906 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 907 | ** Return the number of bytes actually read. Update the offset. |
| 908 | */ |
| 909 | static int seekAndRead(unixFile *id, void *pBuf, int cnt){ |
| 910 | int got; |
| 911 | #ifdef USE_PREAD |
| 912 | got = pread(id->h, pBuf, cnt, id->offset); |
| 913 | #else |
| 914 | lseek(id->h, id->offset, SEEK_SET); |
| 915 | got = read(id->h, pBuf, cnt); |
| 916 | #endif |
| 917 | if( got>0 ){ |
| 918 | id->offset += got; |
| 919 | } |
| 920 | return got; |
| 921 | } |
| 922 | |
| 923 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 924 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 925 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 926 | ** wrong. |
| 927 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 928 | static int unixRead(OsFile *id, void *pBuf, int amt){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 929 | int got; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 930 | assert( id ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 931 | TIMER_START; |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 932 | got = seekAndRead((unixFile*)id, pBuf, amt); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 933 | TIMER_END; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 934 | TRACE5("READ %-3d %5d %7d %d\n", ((unixFile*)id)->h, got, |
| 935 | last_page, TIMER_ELAPSED); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 936 | SEEK(0); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 937 | SimulateIOError( got=0 ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 938 | if( got==amt ){ |
| 939 | return SQLITE_OK; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 940 | }else if( got<0 ){ |
| 941 | return SQLITE_IOERR_READ; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 942 | }else{ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 943 | return SQLITE_IOERR_SHORT_READ; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
| 947 | /* |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 948 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 949 | ** Return the number of bytes actually read. Update the offset. |
| 950 | */ |
| 951 | static int seekAndWrite(unixFile *id, const void *pBuf, int cnt){ |
| 952 | int got; |
| 953 | #ifdef USE_PREAD |
| 954 | got = pwrite(id->h, pBuf, cnt, id->offset); |
| 955 | #else |
| 956 | lseek(id->h, id->offset, SEEK_SET); |
| 957 | got = write(id->h, pBuf, cnt); |
| 958 | #endif |
| 959 | if( got>0 ){ |
| 960 | id->offset += got; |
| 961 | } |
| 962 | return got; |
| 963 | } |
| 964 | |
| 965 | |
| 966 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 967 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 968 | ** or some other error code on failure. |
| 969 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 970 | static int unixWrite(OsFile *id, const void *pBuf, int amt){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 971 | int wrote = 0; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 972 | assert( id ); |
drh | 4c7f941 | 2005-02-03 00:29:47 +0000 | [diff] [blame] | 973 | assert( amt>0 ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 974 | TIMER_START; |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 975 | while( amt>0 && (wrote = seekAndWrite((unixFile*)id, pBuf, amt))>0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 976 | amt -= wrote; |
| 977 | pBuf = &((char*)pBuf)[wrote]; |
| 978 | } |
| 979 | TIMER_END; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 980 | TRACE5("WRITE %-3d %5d %7d %d\n", ((unixFile*)id)->h, wrote, |
| 981 | last_page, TIMER_ELAPSED); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 982 | SEEK(0); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 983 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 984 | SimulateDiskfullError(( wrote=0, amt=1 )); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 985 | if( amt>0 ){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 986 | if( wrote<0 ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 987 | return SQLITE_IOERR_WRITE; |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 988 | }else{ |
| 989 | return SQLITE_FULL; |
| 990 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 991 | } |
| 992 | return SQLITE_OK; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | ** Move the read/write pointer in a file. |
| 997 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 998 | static int unixSeek(OsFile *id, i64 offset){ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 999 | assert( id ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1000 | SEEK(offset/1024 + 1); |
drh | b4746b9 | 2005-09-09 01:32:06 +0000 | [diff] [blame] | 1001 | #ifdef SQLITE_TEST |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1002 | if( offset ) SimulateDiskfullError(return SQLITE_FULL); |
drh | b4746b9 | 2005-09-09 01:32:06 +0000 | [diff] [blame] | 1003 | #endif |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 1004 | ((unixFile*)id)->offset = offset; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1005 | return SQLITE_OK; |
| 1006 | } |
| 1007 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 1008 | #ifdef SQLITE_TEST |
| 1009 | /* |
| 1010 | ** Count the number of fullsyncs and normal syncs. This is used to test |
| 1011 | ** that syncs and fullsyncs are occuring at the right times. |
| 1012 | */ |
| 1013 | int sqlite3_sync_count = 0; |
| 1014 | int sqlite3_fullsync_count = 0; |
| 1015 | #endif |
| 1016 | |
drh | f2f2391 | 2005-10-05 10:29:36 +0000 | [diff] [blame] | 1017 | /* |
| 1018 | ** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined. |
| 1019 | ** Otherwise use fsync() in its place. |
| 1020 | */ |
| 1021 | #ifndef HAVE_FDATASYNC |
| 1022 | # define fdatasync fsync |
| 1023 | #endif |
| 1024 | |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1025 | /* |
| 1026 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 1027 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 1028 | ** only available on Mac OS X. But that could change. |
| 1029 | */ |
| 1030 | #ifdef F_FULLFSYNC |
| 1031 | # define HAVE_FULLFSYNC 1 |
| 1032 | #else |
| 1033 | # define HAVE_FULLFSYNC 0 |
| 1034 | #endif |
| 1035 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 1036 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1037 | /* |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 1038 | ** The fsync() system call does not work as advertised on many |
| 1039 | ** unix systems. The following procedure is an attempt to make |
| 1040 | ** it work better. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 1041 | ** |
| 1042 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 1043 | ** for testing when we want to run through the test suite quickly. |
| 1044 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 1045 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 1046 | ** or power failure will likely corrupt the database file. |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 1047 | */ |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 1048 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 1049 | int rc; |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 1050 | |
| 1051 | /* Record the number of times that we do a normal fsync() and |
| 1052 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 1053 | ** gets called with the correct arguments. |
| 1054 | */ |
| 1055 | #ifdef SQLITE_TEST |
| 1056 | if( fullSync ) sqlite3_fullsync_count++; |
| 1057 | sqlite3_sync_count++; |
| 1058 | #endif |
| 1059 | |
| 1060 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 1061 | ** no-op |
| 1062 | */ |
| 1063 | #ifdef SQLITE_NO_SYNC |
| 1064 | rc = SQLITE_OK; |
| 1065 | #else |
| 1066 | |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1067 | #if HAVE_FULLFSYNC |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 1068 | if( fullSync ){ |
drh | f30cc94 | 2005-03-11 17:52:34 +0000 | [diff] [blame] | 1069 | rc = fcntl(fd, F_FULLFSYNC, 0); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 1070 | }else |
| 1071 | #endif /* HAVE_FULLFSYNC */ |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 1072 | if( dataOnly ){ |
| 1073 | rc = fdatasync(fd); |
drh | f2f2391 | 2005-10-05 10:29:36 +0000 | [diff] [blame] | 1074 | }else{ |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 1075 | rc = fsync(fd); |
| 1076 | } |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 1077 | #endif /* defined(SQLITE_NO_SYNC) */ |
| 1078 | |
drh | dd809b0 | 2004-07-17 21:44:57 +0000 | [diff] [blame] | 1079 | return rc; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1083 | ** Make sure all writes to a particular file are committed to disk. |
| 1084 | ** |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 1085 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 1086 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 1087 | ** file data is synced. |
| 1088 | ** |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1089 | ** Under Unix, also make sure that the directory entry for the file |
| 1090 | ** has been created by fsync-ing the directory that contains the file. |
| 1091 | ** If we do not do this and we encounter a power failure, the directory |
| 1092 | ** entry for the journal might not exist after we reboot. The next |
| 1093 | ** SQLite to access the file will not know that the journal exists (because |
| 1094 | ** the directory entry for the journal was never created) and the transaction |
| 1095 | ** will not roll back - possibly leading to database corruption. |
| 1096 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1097 | static int unixSync(OsFile *id, int dataOnly){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1098 | int rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1099 | unixFile *pFile = (unixFile*)id; |
| 1100 | assert( pFile ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1101 | TRACE2("SYNC %-3d\n", pFile->h); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1102 | rc = full_fsync(pFile->h, pFile->fullSync, dataOnly); |
| 1103 | SimulateIOError( rc=1 ); |
| 1104 | if( rc ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 1105 | return SQLITE_IOERR_FSYNC; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1106 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1107 | if( pFile->dirfd>=0 ){ |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1108 | TRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
| 1109 | HAVE_FULLFSYNC, pFile->fullSync); |
danielk1977 | d7c03f7 | 2005-11-25 10:38:22 +0000 | [diff] [blame] | 1110 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1111 | /* The directory sync is only attempted if full_fsync is |
| 1112 | ** turned off or unavailable. If a full_fsync occurred above, |
| 1113 | ** then the directory sync is superfluous. |
| 1114 | */ |
| 1115 | if( (!HAVE_FULLFSYNC || !pFile->fullSync) && full_fsync(pFile->dirfd,0,0) ){ |
| 1116 | /* |
| 1117 | ** We have received multiple reports of fsync() returning |
drh | 86631a5 | 2006-02-09 23:05:51 +0000 | [diff] [blame] | 1118 | ** errors when applied to directories on certain file systems. |
| 1119 | ** A failed directory sync is not a big deal. So it seems |
| 1120 | ** better to ignore the error. Ticket #1657 |
| 1121 | */ |
| 1122 | /* return SQLITE_IOERR; */ |
danielk1977 | 0964b23 | 2005-11-25 08:47:57 +0000 | [diff] [blame] | 1123 | } |
danielk1977 | d7c03f7 | 2005-11-25 10:38:22 +0000 | [diff] [blame] | 1124 | #endif |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1125 | close(pFile->dirfd); /* Only need to sync once, so close the directory */ |
| 1126 | pFile->dirfd = -1; /* when we are done. */ |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 1127 | } |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 1128 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /* |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 1132 | ** Sync the directory zDirname. This is a no-op on operating systems other |
| 1133 | ** than UNIX. |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 1134 | ** |
| 1135 | ** This is used to make sure the master journal file has truely been deleted |
| 1136 | ** before making changes to individual journals on a multi-database commit. |
drh | f30cc94 | 2005-03-11 17:52:34 +0000 | [diff] [blame] | 1137 | ** The F_FULLFSYNC option is not needed here. |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 1138 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1139 | int sqlite3UnixSyncDirectory(const char *zDirname){ |
danielk1977 | d7c03f7 | 2005-11-25 10:38:22 +0000 | [diff] [blame] | 1140 | #ifdef SQLITE_DISABLE_DIRSYNC |
| 1141 | return SQLITE_OK; |
| 1142 | #else |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 1143 | int fd; |
| 1144 | int r; |
drh | 8e85577 | 2005-05-17 11:25:31 +0000 | [diff] [blame] | 1145 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 1146 | TRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname); |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 1147 | if( fd<0 ){ |
| 1148 | return SQLITE_CANTOPEN; |
| 1149 | } |
| 1150 | r = fsync(fd); |
| 1151 | close(fd); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1152 | SimulateIOError( r=1 ); |
| 1153 | if( r ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 1154 | return SQLITE_IOERR_DIR_FSYNC; |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1155 | }else{ |
| 1156 | return SQLITE_OK; |
| 1157 | } |
danielk1977 | d7c03f7 | 2005-11-25 10:38:22 +0000 | [diff] [blame] | 1158 | #endif |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1162 | ** Truncate an open file to a specified size |
| 1163 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1164 | static int unixTruncate(OsFile *id, i64 nByte){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1165 | int rc; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1166 | assert( id ); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1167 | rc = ftruncate(((unixFile*)id)->h, nByte); |
| 1168 | SimulateIOError( rc=1 ); |
| 1169 | if( rc ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 1170 | return SQLITE_IOERR_TRUNCATE; |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1171 | }else{ |
| 1172 | return SQLITE_OK; |
| 1173 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | ** Determine the current size of a file in bytes |
| 1178 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1179 | static int unixFileSize(OsFile *id, i64 *pSize){ |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1180 | int rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1181 | struct stat buf; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1182 | assert( id ); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 1183 | rc = fstat(((unixFile*)id)->h, &buf); |
| 1184 | SimulateIOError( rc=1 ); |
| 1185 | if( rc!=0 ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 1186 | return SQLITE_IOERR_FSTAT; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1187 | } |
| 1188 | *pSize = buf.st_size; |
| 1189 | return SQLITE_OK; |
| 1190 | } |
| 1191 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1192 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1193 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1194 | ** 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] | 1195 | ** non-zero. If the file is unlocked or holds only SHARED locks, then |
| 1196 | ** return zero. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1197 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1198 | static int unixCheckReservedLock(OsFile *id){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1199 | int r = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1200 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1201 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1202 | assert( pFile ); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1203 | sqlite3OsEnterMutex(); /* Because pFile->pLock is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1204 | |
| 1205 | /* Check if a thread in this process holds such a lock */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1206 | if( pFile->pLock->locktype>SHARED_LOCK ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1207 | r = 1; |
| 1208 | } |
| 1209 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1210 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1211 | */ |
| 1212 | if( !r ){ |
| 1213 | struct flock lock; |
| 1214 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1215 | lock.l_start = RESERVED_BYTE; |
| 1216 | lock.l_len = 1; |
| 1217 | lock.l_type = F_WRLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1218 | fcntl(pFile->h, F_GETLK, &lock); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1219 | if( lock.l_type!=F_UNLCK ){ |
| 1220 | r = 1; |
| 1221 | } |
| 1222 | } |
| 1223 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1224 | sqlite3OsLeaveMutex(); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1225 | TRACE3("TEST WR-LOCK %d %d\n", pFile->h, r); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1226 | |
| 1227 | return r; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1231 | ** Lock the file with the lock specified by parameter locktype - one |
| 1232 | ** of the following: |
| 1233 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1234 | ** (1) SHARED_LOCK |
| 1235 | ** (2) RESERVED_LOCK |
| 1236 | ** (3) PENDING_LOCK |
| 1237 | ** (4) EXCLUSIVE_LOCK |
| 1238 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1239 | ** Sometimes when requesting one lock state, additional lock states |
| 1240 | ** are inserted in between. The locking might fail on one of the later |
| 1241 | ** transitions leaving the lock state different from what it started but |
| 1242 | ** still short of its goal. The following chart shows the allowed |
| 1243 | ** transitions and the inserted intermediate states: |
| 1244 | ** |
| 1245 | ** UNLOCKED -> SHARED |
| 1246 | ** SHARED -> RESERVED |
| 1247 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1248 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1249 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1250 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1251 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1252 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1253 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1254 | static int unixLock(OsFile *id, int locktype){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1255 | /* The following describes the implementation of the various locks and |
| 1256 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1257 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1258 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1259 | ** slightly in order to be compatible with windows systems simultaneously |
| 1260 | ** accessing the same database file, in case that is ever required. |
| 1261 | ** |
| 1262 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1263 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1264 | ** range', a range of 510 bytes at a well known offset. |
| 1265 | ** |
| 1266 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1267 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1268 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1269 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1270 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1271 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1272 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1273 | ** |
| 1274 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1275 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1276 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1277 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1278 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1279 | ** This property is used by the algorithm for rolling back a journal file |
| 1280 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1281 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1282 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1283 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1284 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1285 | ** within this range, this ensures that no other locks are held on the |
| 1286 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1287 | ** |
| 1288 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1289 | ** range' is that some versions of windows do not support read-locks. By |
| 1290 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1291 | ** even if the locking primitive used is always a write-lock. |
| 1292 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1293 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1294 | unixFile *pFile = (unixFile*)id; |
| 1295 | struct lockInfo *pLock = pFile->pLock; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1296 | struct flock lock; |
| 1297 | int s; |
| 1298 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1299 | assert( pFile ); |
| 1300 | TRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h, |
| 1301 | locktypeName(locktype), locktypeName(pFile->locktype), |
| 1302 | locktypeName(pLock->locktype), pLock->cnt , getpid()); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1303 | |
| 1304 | /* If there is already a lock of this type or more restrictive on the |
| 1305 | ** OsFile, do nothing. Don't use the end_lock: exit path, as |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1306 | ** sqlite3OsEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1307 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1308 | if( pFile->locktype>=locktype ){ |
| 1309 | TRACE3("LOCK %d %s ok (already held)\n", pFile->h, |
| 1310 | locktypeName(locktype)); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1311 | return SQLITE_OK; |
| 1312 | } |
| 1313 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1314 | /* Make sure the locking sequence is correct |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1315 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1316 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1317 | assert( locktype!=PENDING_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1318 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1319 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1320 | /* This mutex is needed because pFile->pLock is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1321 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1322 | sqlite3OsEnterMutex(); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1323 | |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1324 | /* Make sure the current thread owns the pFile. |
| 1325 | */ |
| 1326 | rc = transferOwnership(pFile); |
| 1327 | if( rc!=SQLITE_OK ){ |
| 1328 | sqlite3OsLeaveMutex(); |
| 1329 | return rc; |
| 1330 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1331 | pLock = pFile->pLock; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1332 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1333 | /* If some thread using this PID has a lock via a different OsFile* |
| 1334 | ** handle that precludes the requested lock, return BUSY. |
| 1335 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1336 | if( (pFile->locktype!=pLock->locktype && |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1337 | (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1338 | ){ |
| 1339 | rc = SQLITE_BUSY; |
| 1340 | goto end_lock; |
| 1341 | } |
| 1342 | |
| 1343 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1344 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1345 | ** return SQLITE_OK. |
| 1346 | */ |
| 1347 | if( locktype==SHARED_LOCK && |
| 1348 | (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){ |
| 1349 | assert( locktype==SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1350 | assert( pFile->locktype==0 ); |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1351 | assert( pLock->cnt>0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1352 | pFile->locktype = SHARED_LOCK; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1353 | pLock->cnt++; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1354 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1355 | goto end_lock; |
| 1356 | } |
| 1357 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1358 | lock.l_len = 1L; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1359 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1360 | lock.l_whence = SEEK_SET; |
| 1361 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1362 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1363 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1364 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1365 | */ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1366 | if( locktype==SHARED_LOCK |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1367 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1368 | ){ |
danielk1977 | 489468c | 2004-06-28 08:25:47 +0000 | [diff] [blame] | 1369 | lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1370 | lock.l_start = PENDING_BYTE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1371 | s = fcntl(pFile->h, F_SETLK, &lock); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1372 | if( s ){ |
| 1373 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 1374 | goto end_lock; |
| 1375 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | |
| 1379 | /* If control gets to this point, then actually go ahead and make |
| 1380 | ** operating system calls for the specified lock. |
| 1381 | */ |
| 1382 | if( locktype==SHARED_LOCK ){ |
| 1383 | assert( pLock->cnt==0 ); |
| 1384 | assert( pLock->locktype==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1385 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1386 | /* Now get the read-lock */ |
| 1387 | lock.l_start = SHARED_FIRST; |
| 1388 | lock.l_len = SHARED_SIZE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1389 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* Drop the temporary PENDING lock */ |
| 1392 | lock.l_start = PENDING_BYTE; |
| 1393 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1394 | lock.l_type = F_UNLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1395 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 1396 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1397 | goto end_lock; |
| 1398 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1399 | if( s ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1400 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 1401 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1402 | pFile->locktype = SHARED_LOCK; |
| 1403 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1404 | pLock->cnt = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1405 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1406 | }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){ |
| 1407 | /* We are trying for an exclusive lock but another thread in this |
| 1408 | ** same process is still holding a shared lock. */ |
| 1409 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1410 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1411 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1412 | ** assumed that there is a SHARED or greater lock on the file |
| 1413 | ** already. |
| 1414 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1415 | assert( 0!=pFile->locktype ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1416 | lock.l_type = F_WRLCK; |
| 1417 | switch( locktype ){ |
| 1418 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1419 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1420 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1421 | case EXCLUSIVE_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1422 | lock.l_start = SHARED_FIRST; |
| 1423 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1424 | break; |
| 1425 | default: |
| 1426 | assert(0); |
| 1427 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1428 | s = fcntl(pFile->h, F_SETLK, &lock); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1429 | if( s ){ |
| 1430 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 1431 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1432 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1433 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1434 | if( rc==SQLITE_OK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1435 | pFile->locktype = locktype; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1436 | pLock->locktype = locktype; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1437 | }else if( locktype==EXCLUSIVE_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1438 | pFile->locktype = PENDING_LOCK; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1439 | pLock->locktype = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1440 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1441 | |
| 1442 | end_lock: |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1443 | sqlite3OsLeaveMutex(); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1444 | TRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 1445 | rc==SQLITE_OK ? "ok" : "failed"); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1446 | return rc; |
| 1447 | } |
| 1448 | |
| 1449 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1450 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1451 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1452 | ** |
| 1453 | ** If the locking level of the file descriptor is already at or below |
| 1454 | ** the requested locking level, this routine is a no-op. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1455 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1456 | static int unixUnlock(OsFile *id, int locktype){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1457 | struct lockInfo *pLock; |
| 1458 | struct flock lock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1459 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1460 | unixFile *pFile = (unixFile*)id; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1461 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1462 | assert( pFile ); |
| 1463 | TRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype, |
| 1464 | pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid()); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1465 | |
| 1466 | assert( locktype<=SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1467 | if( pFile->locktype<=locktype ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1468 | return SQLITE_OK; |
| 1469 | } |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1470 | if( CHECK_THREADID(pFile) ){ |
| 1471 | return SQLITE_MISUSE; |
| 1472 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1473 | sqlite3OsEnterMutex(); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1474 | pLock = pFile->pLock; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1475 | assert( pLock->cnt!=0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1476 | if( pFile->locktype>SHARED_LOCK ){ |
| 1477 | assert( pLock->locktype==pFile->locktype ); |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1478 | if( locktype==SHARED_LOCK ){ |
| 1479 | lock.l_type = F_RDLCK; |
| 1480 | lock.l_whence = SEEK_SET; |
| 1481 | lock.l_start = SHARED_FIRST; |
| 1482 | lock.l_len = SHARED_SIZE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1483 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1484 | /* This should never happen */ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 1485 | rc = SQLITE_IOERR_RDLOCK; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1486 | } |
| 1487 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1488 | lock.l_type = F_UNLCK; |
| 1489 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1490 | lock.l_start = PENDING_BYTE; |
| 1491 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1492 | if( fcntl(pFile->h, F_SETLK, &lock)==0 ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1493 | pLock->locktype = SHARED_LOCK; |
| 1494 | }else{ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 1495 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1496 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1497 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1498 | if( locktype==NO_LOCK ){ |
| 1499 | struct openCnt *pOpen; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1500 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1501 | /* Decrement the shared lock counter. Release the lock using an |
| 1502 | ** OS call only when all threads in this same process have released |
| 1503 | ** the lock. |
| 1504 | */ |
| 1505 | pLock->cnt--; |
| 1506 | if( pLock->cnt==0 ){ |
| 1507 | lock.l_type = F_UNLCK; |
| 1508 | lock.l_whence = SEEK_SET; |
| 1509 | lock.l_start = lock.l_len = 0L; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1510 | if( fcntl(pFile->h, F_SETLK, &lock)==0 ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1511 | pLock->locktype = NO_LOCK; |
| 1512 | }else{ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame^] | 1513 | rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1514 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1517 | /* Decrement the count of locks against this same file. When the |
| 1518 | ** count reaches zero, close any other file descriptors whose close |
| 1519 | ** was deferred because of outstanding locks. |
| 1520 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1521 | pOpen = pFile->pOpen; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1522 | pOpen->nLock--; |
| 1523 | assert( pOpen->nLock>=0 ); |
| 1524 | if( pOpen->nLock==0 && pOpen->nPending>0 ){ |
| 1525 | int i; |
| 1526 | for(i=0; i<pOpen->nPending; i++){ |
| 1527 | close(pOpen->aPending[i]); |
| 1528 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1529 | free(pOpen->aPending); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1530 | pOpen->nPending = 0; |
| 1531 | pOpen->aPending = 0; |
| 1532 | } |
| 1533 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1534 | sqlite3OsLeaveMutex(); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1535 | pFile->locktype = locktype; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1536 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1540 | ** Close a file. |
| 1541 | */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1542 | static int unixClose(OsFile **pId){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1543 | unixFile *id = (unixFile*)*pId; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1544 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1545 | if( !id ) return SQLITE_OK; |
drh | 3832230 | 2006-01-15 02:43:16 +0000 | [diff] [blame] | 1546 | unixUnlock(*pId, NO_LOCK); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1547 | if( id->dirfd>=0 ) close(id->dirfd); |
| 1548 | id->dirfd = -1; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1549 | sqlite3OsEnterMutex(); |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 1550 | |
drh | 3832230 | 2006-01-15 02:43:16 +0000 | [diff] [blame] | 1551 | if( id->pOpen->nLock ){ |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1552 | /* If there are outstanding locks, do not actually close the file just |
| 1553 | ** yet because that would clear those locks. Instead, add the file |
| 1554 | ** descriptor to pOpen->aPending. It will be automatically closed when |
| 1555 | ** the last lock is cleared. |
| 1556 | */ |
| 1557 | int *aNew; |
| 1558 | struct openCnt *pOpen = id->pOpen; |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1559 | aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) ); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1560 | if( aNew==0 ){ |
| 1561 | /* If a malloc fails, just leak the file descriptor */ |
| 1562 | }else{ |
| 1563 | pOpen->aPending = aNew; |
drh | ad81e87 | 2005-08-21 21:45:01 +0000 | [diff] [blame] | 1564 | pOpen->aPending[pOpen->nPending] = id->h; |
| 1565 | pOpen->nPending++; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1566 | } |
| 1567 | }else{ |
| 1568 | /* There are no outstanding locks so we can close the file immediately */ |
| 1569 | close(id->h); |
| 1570 | } |
| 1571 | releaseLockInfo(id->pLock); |
| 1572 | releaseOpenCnt(id->pOpen); |
danielk1977 | 441b09a | 2006-01-05 13:48:29 +0000 | [diff] [blame] | 1573 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1574 | sqlite3OsLeaveMutex(); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1575 | id->isOpen = 0; |
| 1576 | TRACE2("CLOSE %-3d\n", id->h); |
| 1577 | OpenCounter(-1); |
danielk1977 | 750b03e | 2006-02-14 10:48:39 +0000 | [diff] [blame] | 1578 | sqlite3ThreadSafeFree(id); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1579 | *pId = 0; |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 1580 | return SQLITE_OK; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1584 | ** Turn a relative pathname into a full pathname. Return a pointer |
| 1585 | ** to the full pathname stored in space obtained from sqliteMalloc(). |
| 1586 | ** The calling function is responsible for freeing this space once it |
| 1587 | ** is no longer needed. |
| 1588 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1589 | char *sqlite3UnixFullPathname(const char *zRelative){ |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1590 | char *zFull = 0; |
| 1591 | if( zRelative[0]=='/' ){ |
| 1592 | sqlite3SetString(&zFull, zRelative, (char*)0); |
| 1593 | }else{ |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 1594 | char *zBuf = sqliteMalloc(5000); |
| 1595 | if( zBuf==0 ){ |
| 1596 | return 0; |
| 1597 | } |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1598 | zBuf[0] = 0; |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 1599 | sqlite3SetString(&zFull, getcwd(zBuf, 5000), "/", zRelative, |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1600 | (char*)0); |
drh | 79158e1 | 2005-09-06 21:40:45 +0000 | [diff] [blame] | 1601 | sqliteFree(zBuf); |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1602 | } |
drh | 4eb9a97 | 2006-02-13 18:42:21 +0000 | [diff] [blame] | 1603 | |
| 1604 | #if 0 |
drh | 89ea931 | 2006-02-13 17:03:47 +0000 | [diff] [blame] | 1605 | /* |
| 1606 | ** Remove "/./" path elements and convert "/A/./" path elements |
| 1607 | ** to just "/". |
| 1608 | */ |
| 1609 | if( zFull ){ |
drh | 4eb9a97 | 2006-02-13 18:42:21 +0000 | [diff] [blame] | 1610 | int i, j; |
drh | 89ea931 | 2006-02-13 17:03:47 +0000 | [diff] [blame] | 1611 | for(i=j=0; zFull[i]; i++){ |
| 1612 | if( zFull[i]=='/' ){ |
| 1613 | if( zFull[i+1]=='/' ) continue; |
| 1614 | if( zFull[i+1]=='.' && zFull[i+2]=='/' ){ |
| 1615 | i += 1; |
| 1616 | continue; |
| 1617 | } |
| 1618 | if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){ |
| 1619 | while( j>0 && zFull[j-1]!='/' ){ j--; } |
| 1620 | i += 3; |
| 1621 | continue; |
| 1622 | } |
| 1623 | } |
| 1624 | zFull[j++] = zFull[i]; |
| 1625 | } |
| 1626 | zFull[j] = 0; |
| 1627 | } |
drh | 4eb9a97 | 2006-02-13 18:42:21 +0000 | [diff] [blame] | 1628 | #endif |
| 1629 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1630 | return zFull; |
| 1631 | } |
| 1632 | |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 1633 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1634 | ** Change the value of the fullsync flag in the given file descriptor. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 1635 | */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1636 | static void unixSetFullSync(OsFile *id, int v){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1637 | ((unixFile*)id)->fullSync = v; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | ** Return the underlying file handle for an OsFile |
| 1642 | */ |
| 1643 | static int unixFileHandle(OsFile *id){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1644 | return ((unixFile*)id)->h; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | /* |
| 1648 | ** Return an integer that indices the type of lock currently held |
| 1649 | ** by this handle. (Used for testing and analysis only.) |
| 1650 | */ |
| 1651 | static int unixLockState(OsFile *id){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1652 | return ((unixFile*)id)->locktype; |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 1653 | } |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1654 | |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1655 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1656 | ** This vector defines all the methods that can operate on an OsFile |
| 1657 | ** for unix. |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1658 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1659 | static const IoMethod sqlite3UnixIoMethod = { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1660 | unixClose, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1661 | unixOpenDirectory, |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1662 | unixRead, |
| 1663 | unixWrite, |
| 1664 | unixSeek, |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1665 | unixTruncate, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1666 | unixSync, |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1667 | unixSetFullSync, |
| 1668 | unixFileHandle, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1669 | unixFileSize, |
| 1670 | unixLock, |
| 1671 | unixUnlock, |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1672 | unixLockState, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1673 | unixCheckReservedLock, |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1674 | }; |
| 1675 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1676 | /* |
| 1677 | ** Allocate memory for a unixFile. Initialize the new unixFile |
| 1678 | ** to the value given in pInit and return a pointer to the new |
| 1679 | ** OsFile. If we run out of memory, close the file and return NULL. |
| 1680 | */ |
| 1681 | static int allocateUnixFile(unixFile *pInit, OsFile **pId){ |
| 1682 | unixFile *pNew; |
drh | 2f1a4d1 | 2006-01-23 16:24:54 +0000 | [diff] [blame] | 1683 | pInit->dirfd = -1; |
| 1684 | pInit->fullSync = 0; |
| 1685 | pInit->locktype = 0; |
drh | b912b28 | 2006-03-23 22:42:20 +0000 | [diff] [blame] | 1686 | pInit->offset = 0; |
drh | 2f1a4d1 | 2006-01-23 16:24:54 +0000 | [diff] [blame] | 1687 | SET_THREADID(pInit); |
danielk1977 | 750b03e | 2006-02-14 10:48:39 +0000 | [diff] [blame] | 1688 | pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1689 | if( pNew==0 ){ |
| 1690 | close(pInit->h); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1691 | sqlite3OsEnterMutex(); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1692 | releaseLockInfo(pInit->pLock); |
| 1693 | releaseOpenCnt(pInit->pOpen); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1694 | sqlite3OsLeaveMutex(); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1695 | *pId = 0; |
| 1696 | return SQLITE_NOMEM; |
| 1697 | }else{ |
| 1698 | *pNew = *pInit; |
| 1699 | pNew->pMethod = &sqlite3UnixIoMethod; |
| 1700 | *pId = (OsFile*)pNew; |
| 1701 | OpenCounter(+1); |
| 1702 | return SQLITE_OK; |
| 1703 | } |
| 1704 | } |
| 1705 | |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1706 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1707 | #endif /* SQLITE_OMIT_DISKIO */ |
| 1708 | /*************************************************************************** |
| 1709 | ** Everything above deals with file I/O. Everything that follows deals |
| 1710 | ** with other miscellanous aspects of the operating system interface |
| 1711 | ****************************************************************************/ |
| 1712 | |
| 1713 | |
| 1714 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1715 | ** Get information to seed the random number generator. The seed |
| 1716 | ** is written into the buffer zBuf[256]. The calling function must |
| 1717 | ** supply a sufficiently large buffer. |
| 1718 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1719 | int sqlite3UnixRandomSeed(char *zBuf){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1720 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 1721 | ** errors. The reports issued by valgrind are incorrect - we would |
| 1722 | ** prefer that the randomness be increased by making use of the |
| 1723 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 1724 | ** some users. Rather than argue, it seems easier just to initialize |
| 1725 | ** the whole array and silence valgrind, even if that means less randomness |
| 1726 | ** in the random seed. |
| 1727 | ** |
| 1728 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1729 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1730 | ** tests repeatable. |
| 1731 | */ |
| 1732 | memset(zBuf, 0, 256); |
| 1733 | #if !defined(SQLITE_TEST) |
| 1734 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 1735 | int pid, fd; |
| 1736 | fd = open("/dev/urandom", O_RDONLY); |
| 1737 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 1738 | time_t t; |
| 1739 | time(&t); |
| 1740 | memcpy(zBuf, &t, sizeof(t)); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 1741 | pid = getpid(); |
| 1742 | memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid)); |
| 1743 | }else{ |
| 1744 | read(fd, zBuf, 256); |
| 1745 | close(fd); |
| 1746 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1747 | } |
| 1748 | #endif |
| 1749 | return SQLITE_OK; |
| 1750 | } |
| 1751 | |
| 1752 | /* |
| 1753 | ** Sleep for a little while. Return the amount of time slept. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1754 | ** The argument is the number of milliseconds we want to sleep. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1755 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1756 | int sqlite3UnixSleep(int ms){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1757 | #if defined(HAVE_USLEEP) && HAVE_USLEEP |
| 1758 | usleep(ms*1000); |
| 1759 | return ms; |
| 1760 | #else |
| 1761 | sleep((ms+999)/1000); |
| 1762 | return 1000*((ms+999)/1000); |
| 1763 | #endif |
| 1764 | } |
| 1765 | |
| 1766 | /* |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1767 | ** Static variables used for thread synchronization. |
| 1768 | ** |
| 1769 | ** inMutex the nesting depth of the recursive mutex. The thread |
| 1770 | ** holding mutexMain can read this variable at any time. |
| 1771 | ** But is must hold mutexAux to change this variable. Other |
drh | 6a3d670 | 2006-02-10 13:11:32 +0000 | [diff] [blame] | 1772 | ** threads must hold mutexAux to read the variable and can |
| 1773 | ** never write. |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1774 | ** |
| 1775 | ** mutexOwner The thread id of the thread holding mutexMain. Same |
| 1776 | ** access rules as for inMutex. |
| 1777 | ** |
drh | 6a3d670 | 2006-02-10 13:11:32 +0000 | [diff] [blame] | 1778 | ** mutexOwnerValid True if the value in mutexOwner is valid. The same |
| 1779 | ** access rules apply as for inMutex. |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1780 | ** |
| 1781 | ** mutexMain The main mutex. Hold this mutex in order to get exclusive |
| 1782 | ** access to SQLite data structures. |
| 1783 | ** |
| 1784 | ** mutexAux An auxiliary mutex needed to access variables defined above. |
| 1785 | ** |
drh | 6a3d670 | 2006-02-10 13:11:32 +0000 | [diff] [blame] | 1786 | ** Mutexes are always acquired in this order: mutexMain mutexAux. It |
| 1787 | ** is not necessary to acquire mutexMain in order to get mutexAux - just |
| 1788 | ** do not attempt to acquire them in the reverse order: mutexAux mutexMain. |
| 1789 | ** Either get the mutexes with mutexMain first or get mutexAux only. |
| 1790 | ** |
| 1791 | ** When running on a platform where the three variables inMutex, mutexOwner, |
| 1792 | ** and mutexOwnerValid can be set atomically, the mutexAux is not required. |
| 1793 | ** On many systems, all three are 32-bit integers and writing to a 32-bit |
| 1794 | ** integer is atomic. I think. But there are no guarantees. So it seems |
| 1795 | ** safer to protect them using mutexAux. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1796 | */ |
| 1797 | static int inMutex = 0; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1798 | #ifdef SQLITE_UNIX_THREADS |
drh | 6a3d670 | 2006-02-10 13:11:32 +0000 | [diff] [blame] | 1799 | static pthread_t mutexOwner; /* Thread holding mutexMain */ |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1800 | static int mutexOwnerValid = 0; /* True if mutexOwner is valid */ |
| 1801 | static pthread_mutex_t mutexMain = PTHREAD_MUTEX_INITIALIZER; /* The mutex */ |
| 1802 | static pthread_mutex_t mutexAux = PTHREAD_MUTEX_INITIALIZER; /* Aux mutex */ |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1803 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1804 | |
| 1805 | /* |
| 1806 | ** The following pair of routine implement mutual exclusion for |
| 1807 | ** multi-threaded processes. Only a single thread is allowed to |
| 1808 | ** executed code that is surrounded by EnterMutex() and LeaveMutex(). |
| 1809 | ** |
| 1810 | ** SQLite uses only a single Mutex. There is not much critical |
| 1811 | ** code and what little there is executes quickly and without blocking. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1812 | ** |
drh | 757b04e | 2006-01-18 17:25:45 +0000 | [diff] [blame] | 1813 | ** As of version 3.3.2, this mutex must be recursive. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1814 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1815 | void sqlite3UnixEnterMutex(){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1816 | #ifdef SQLITE_UNIX_THREADS |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1817 | pthread_mutex_lock(&mutexAux); |
| 1818 | if( !mutexOwnerValid || !pthread_equal(mutexOwner, pthread_self()) ){ |
| 1819 | pthread_mutex_unlock(&mutexAux); |
| 1820 | pthread_mutex_lock(&mutexMain); |
| 1821 | assert( inMutex==0 ); |
| 1822 | assert( !mutexOwnerValid ); |
| 1823 | pthread_mutex_lock(&mutexAux); |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1824 | mutexOwner = pthread_self(); |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1825 | mutexOwnerValid = 1; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1826 | } |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1827 | inMutex++; |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1828 | pthread_mutex_unlock(&mutexAux); |
| 1829 | #else |
drh | e9565a6 | 2006-02-11 02:03:52 +0000 | [diff] [blame] | 1830 | inMutex++; |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1831 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1832 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1833 | void sqlite3UnixLeaveMutex(){ |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1834 | assert( inMutex>0 ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1835 | #ifdef SQLITE_UNIX_THREADS |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1836 | pthread_mutex_lock(&mutexAux); |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1837 | inMutex--; |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1838 | assert( pthread_equal(mutexOwner, pthread_self()) ); |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1839 | if( inMutex==0 ){ |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1840 | assert( mutexOwnerValid ); |
| 1841 | mutexOwnerValid = 0; |
| 1842 | pthread_mutex_unlock(&mutexMain); |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1843 | } |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1844 | pthread_mutex_unlock(&mutexAux); |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1845 | #else |
| 1846 | inMutex--; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1847 | #endif |
| 1848 | } |
| 1849 | |
| 1850 | /* |
drh | 757b04e | 2006-01-18 17:25:45 +0000 | [diff] [blame] | 1851 | ** Return TRUE if the mutex is currently held. |
| 1852 | ** |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1853 | ** If the thisThrd parameter is true, return true only if the |
drh | 757b04e | 2006-01-18 17:25:45 +0000 | [diff] [blame] | 1854 | ** calling thread holds the mutex. If the parameter is false, return |
| 1855 | ** true if any thread holds the mutex. |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 1856 | */ |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1857 | int sqlite3UnixInMutex(int thisThrd){ |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1858 | #ifdef SQLITE_UNIX_THREADS |
drh | 5c11123 | 2006-02-10 04:33:12 +0000 | [diff] [blame] | 1859 | int rc; |
| 1860 | pthread_mutex_lock(&mutexAux); |
| 1861 | rc = inMutex>0 && (thisThrd==0 || pthread_equal(mutexOwner,pthread_self())); |
| 1862 | pthread_mutex_unlock(&mutexAux); |
| 1863 | return rc; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1864 | #else |
drh | 757b04e | 2006-01-18 17:25:45 +0000 | [diff] [blame] | 1865 | return inMutex>0; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 1866 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | /* |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1870 | ** Remember the number of thread-specific-data blocks allocated. |
| 1871 | ** Use this to verify that we are not leaking thread-specific-data. |
| 1872 | ** Ticket #1601 |
| 1873 | */ |
| 1874 | #ifdef SQLITE_TEST |
| 1875 | int sqlite3_tsd_count = 0; |
| 1876 | # ifdef SQLITE_UNIX_THREADS |
| 1877 | static pthread_mutex_t tsd_counter_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 1878 | # define TSD_COUNTER(N) \ |
| 1879 | pthread_mutex_lock(&tsd_counter_mutex); \ |
| 1880 | sqlite3_tsd_count += N; \ |
| 1881 | pthread_mutex_unlock(&tsd_counter_mutex); |
| 1882 | # else |
| 1883 | # define TSD_COUNTER(N) sqlite3_tsd_count += N |
| 1884 | # endif |
| 1885 | #else |
| 1886 | # define TSD_COUNTER(N) /* no-op */ |
| 1887 | #endif |
| 1888 | |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1889 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1890 | ** If called with allocateFlag>0, then return a pointer to thread |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1891 | ** specific data for the current thread. Allocate and zero the |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1892 | ** thread-specific data if it does not already exist. |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1893 | ** |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1894 | ** If called with allocateFlag==0, then check the current thread |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1895 | ** specific data. Return it if it exists. If it does not exist, |
| 1896 | ** then return NULL. |
| 1897 | ** |
| 1898 | ** If called with allocateFlag<0, check to see if the thread specific |
| 1899 | ** data is allocated and is all zero. If it is then deallocate it. |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1900 | ** Return a pointer to the thread specific data or NULL if it is |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1901 | ** unallocated or gets deallocated. |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1902 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1903 | ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){ |
danielk1977 | 4d5238f | 2006-01-27 06:32:00 +0000 | [diff] [blame] | 1904 | static const ThreadData zeroData = {0}; /* Initializer to silence warnings |
| 1905 | ** from broken compilers */ |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1906 | #ifdef SQLITE_UNIX_THREADS |
| 1907 | static pthread_key_t key; |
| 1908 | static int keyInit = 0; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1909 | ThreadData *pTsd; |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1910 | |
| 1911 | if( !keyInit ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1912 | sqlite3OsEnterMutex(); |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1913 | if( !keyInit ){ |
| 1914 | int rc; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1915 | rc = pthread_key_create(&key, 0); |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1916 | if( rc ){ |
drh | 8c0ca7d | 2006-01-07 04:06:54 +0000 | [diff] [blame] | 1917 | sqlite3OsLeaveMutex(); |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1918 | return 0; |
| 1919 | } |
| 1920 | keyInit = 1; |
| 1921 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1922 | sqlite3OsLeaveMutex(); |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1925 | pTsd = pthread_getspecific(key); |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1926 | if( allocateFlag>0 ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1927 | if( pTsd==0 ){ |
danielk1977 | 76e8d1a | 2006-01-18 18:22:43 +0000 | [diff] [blame] | 1928 | if( !sqlite3TestMallocFail() ){ |
| 1929 | pTsd = sqlite3OsMalloc(sizeof(zeroData)); |
| 1930 | } |
| 1931 | #ifdef SQLITE_MEMDEBUG |
| 1932 | sqlite3_isFail = 0; |
| 1933 | #endif |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1934 | if( pTsd ){ |
| 1935 | *pTsd = zeroData; |
| 1936 | pthread_setspecific(key, pTsd); |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1937 | TSD_COUNTER(+1); |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1938 | } |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1939 | } |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1940 | }else if( pTsd!=0 && allocateFlag<0 |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 1941 | && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1942 | sqlite3OsFree(pTsd); |
| 1943 | pthread_setspecific(key, 0); |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1944 | TSD_COUNTER(-1); |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1945 | pTsd = 0; |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1946 | } |
| 1947 | return pTsd; |
| 1948 | #else |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1949 | static ThreadData *pTsd = 0; |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1950 | if( allocateFlag>0 ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1951 | if( pTsd==0 ){ |
danielk1977 | 76e8d1a | 2006-01-18 18:22:43 +0000 | [diff] [blame] | 1952 | if( !sqlite3TestMallocFail() ){ |
| 1953 | pTsd = sqlite3OsMalloc( sizeof(zeroData) ); |
| 1954 | } |
| 1955 | #ifdef SQLITE_MEMDEBUG |
| 1956 | sqlite3_isFail = 0; |
| 1957 | #endif |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1958 | if( pTsd ){ |
| 1959 | *pTsd = zeroData; |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1960 | TSD_COUNTER(+1); |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1961 | } |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1962 | } |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1963 | }else if( pTsd!=0 && allocateFlag<0 |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 1964 | && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1965 | sqlite3OsFree(pTsd); |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1966 | TSD_COUNTER(-1); |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1967 | pTsd = 0; |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1968 | } |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1969 | return pTsd; |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1970 | #endif |
| 1971 | } |
| 1972 | |
| 1973 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1974 | ** The following variable, if set to a non-zero value, becomes the result |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1975 | ** returned from sqlite3OsCurrentTime(). This is used for testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1976 | */ |
| 1977 | #ifdef SQLITE_TEST |
| 1978 | int sqlite3_current_time = 0; |
| 1979 | #endif |
| 1980 | |
| 1981 | /* |
| 1982 | ** Find the current time (in Universal Coordinated Time). Write the |
| 1983 | ** current time and date as a Julian Day number into *prNow and |
| 1984 | ** return 0. Return 1 if the time and date cannot be found. |
| 1985 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1986 | int sqlite3UnixCurrentTime(double *prNow){ |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1987 | #ifdef NO_GETTOD |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1988 | time_t t; |
| 1989 | time(&t); |
| 1990 | *prNow = t/86400.0 + 2440587.5; |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1991 | #else |
| 1992 | struct timeval sNow; |
| 1993 | struct timezone sTz; /* Not used */ |
| 1994 | gettimeofday(&sNow, &sTz); |
| 1995 | *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0; |
| 1996 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1997 | #ifdef SQLITE_TEST |
| 1998 | if( sqlite3_current_time ){ |
| 1999 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 2000 | } |
| 2001 | #endif |
| 2002 | return 0; |
| 2003 | } |
| 2004 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2005 | #endif /* OS_UNIX */ |