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