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