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