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