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