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