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