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