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. |
| 14 | */ |
| 15 | #include "os.h" /* Must be first to enable large file support */ |
| 16 | #if OS_UNIX /* This file is used on unix only */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
| 19 | |
| 20 | #include <time.h> |
| 21 | #include <errno.h> |
| 22 | #include <unistd.h> |
| 23 | #ifndef O_LARGEFILE |
| 24 | # define O_LARGEFILE 0 |
| 25 | #endif |
| 26 | #ifdef SQLITE_DISABLE_LFS |
| 27 | # undef O_LARGEFILE |
| 28 | # define O_LARGEFILE 0 |
| 29 | #endif |
| 30 | #ifndef O_NOFOLLOW |
| 31 | # define O_NOFOLLOW 0 |
| 32 | #endif |
| 33 | #ifndef O_BINARY |
| 34 | # define O_BINARY 0 |
| 35 | #endif |
| 36 | |
| 37 | /* |
| 38 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 39 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 40 | ** that always succeeds. This means that locking does not occur under |
| 41 | ** DJGPP. But its DOS - what did you expect? |
| 42 | */ |
| 43 | #ifdef __DJGPP__ |
| 44 | # define fcntl(A,B,C) 0 |
| 45 | #endif |
| 46 | |
| 47 | /* |
| 48 | ** Macros used to determine whether or not to use threads. The |
| 49 | ** SQLITE_UNIX_THREADS macro is defined if we are synchronizing for |
| 50 | ** Posix threads and SQLITE_W32_THREADS is defined if we are |
| 51 | ** synchronizing using Win32 threads. |
| 52 | */ |
| 53 | #if defined(THREADSAFE) && THREADSAFE |
| 54 | # include <pthread.h> |
| 55 | # define SQLITE_UNIX_THREADS 1 |
| 56 | #endif |
| 57 | |
| 58 | |
| 59 | /* |
| 60 | ** Include code that is common to all os_*.c files |
| 61 | */ |
| 62 | #include "os_common.h" |
| 63 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 64 | #if defined(THREADSAFE) && defined(__linux__) |
| 65 | #define getpid pthread_self |
| 66 | #endif |
| 67 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 68 | /* |
| 69 | ** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996) |
| 70 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 71 | ** sets or clears a lock, that operation overrides any prior locks set |
| 72 | ** by the same process. It does not explicitly say so, but this implies |
| 73 | ** that it overrides locks set by the same process using a different |
| 74 | ** file descriptor. Consider this test case: |
| 75 | ** |
| 76 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
| 77 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 78 | ** |
| 79 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 80 | ** one is a hard or symbolic link to the other) then if you set |
| 81 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 82 | ** on fd2, it works. I would have expected the second lock to |
| 83 | ** fail since there was already a lock on the file due to fd1. |
| 84 | ** But not so. Since both locks came from the same process, the |
| 85 | ** second overrides the first, even though they were on different |
| 86 | ** file descriptors opened on different file names. |
| 87 | ** |
| 88 | ** Bummer. If you ask me, this is broken. Badly broken. It means |
| 89 | ** that we cannot use POSIX locks to synchronize file access among |
| 90 | ** competing threads of the same process. POSIX locks will work fine |
| 91 | ** to synchronize access for threads in separate processes, but not |
| 92 | ** threads within the same process. |
| 93 | ** |
| 94 | ** To work around the problem, SQLite has to manage file locks internally |
| 95 | ** on its own. Whenever a new database is opened, we have to find the |
| 96 | ** specific inode of the database file (the inode is determined by the |
| 97 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 98 | ** and check for locks already existing on that inode. When locks are |
| 99 | ** created or removed, we have to look at our own internal record of the |
| 100 | ** locks to see if another thread has previously set a lock on that same |
| 101 | ** inode. |
| 102 | ** |
| 103 | ** The OsFile structure for POSIX is no longer just an integer file |
| 104 | ** descriptor. It is now a structure that holds the integer file |
| 105 | ** descriptor and a pointer to a structure that describes the internal |
| 106 | ** locks on the corresponding inode. There is one locking structure |
| 107 | ** per inode, so if the same inode is opened twice, both OsFile structures |
| 108 | ** point to the same locking structure. The locking structure keeps |
| 109 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 110 | ** field that tells us its internal lock status. cnt==0 means the |
| 111 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 112 | ** cnt>0 means there are cnt shared locks on the file. |
| 113 | ** |
| 114 | ** Any attempt to lock or unlock a file first checks the locking |
| 115 | ** structure. The fcntl() system call is only invoked to set a |
| 116 | ** POSIX lock if the internal lock structure transitions between |
| 117 | ** a locked and an unlocked state. |
| 118 | ** |
| 119 | ** 2004-Jan-11: |
| 120 | ** More recent discoveries about POSIX advisory locks. (The more |
| 121 | ** I discover, the more I realize the a POSIX advisory locks are |
| 122 | ** an abomination.) |
| 123 | ** |
| 124 | ** If you close a file descriptor that points to a file that has locks, |
| 125 | ** all locks on that file that are owned by the current process are |
| 126 | ** released. To work around this problem, each OsFile structure contains |
| 127 | ** a pointer to an openCnt structure. There is one openCnt structure |
| 128 | ** per open inode, which means that multiple OsFiles can point to a single |
| 129 | ** openCnt. When an attempt is made to close an OsFile, if there are |
| 130 | ** other OsFiles open on the same inode that are holding locks, the call |
| 131 | ** to close() the file descriptor is deferred until all of the locks clear. |
| 132 | ** The openCnt structure keeps a list of file descriptors that need to |
| 133 | ** be closed and that list is walked (and cleared) when the last lock |
| 134 | ** clears. |
| 135 | ** |
| 136 | ** First, under Linux threads, because each thread has a separate |
| 137 | ** process ID, lock operations in one thread do not override locks |
| 138 | ** to the same file in other threads. Linux threads behave like |
| 139 | ** separate processes in this respect. But, if you close a file |
| 140 | ** descriptor in linux threads, all locks are cleared, even locks |
| 141 | ** on other threads and even though the other threads have different |
| 142 | ** process IDs. Linux threads is inconsistent in this respect. |
| 143 | ** (I'm beginning to think that linux threads is an abomination too.) |
| 144 | ** The consequence of this all is that the hash table for the lockInfo |
| 145 | ** structure has to include the process id as part of its key because |
| 146 | ** locks in different threads are treated as distinct. But the |
| 147 | ** openCnt structure should not include the process id in its |
| 148 | ** key because close() clears lock on all threads, not just the current |
| 149 | ** thread. Were it not for this goofiness in linux threads, we could |
| 150 | ** combine the lockInfo and openCnt structures into a single structure. |
| 151 | */ |
| 152 | |
| 153 | /* |
| 154 | ** An instance of the following structure serves as the key used |
| 155 | ** to locate a particular lockInfo structure given its inode. Note |
| 156 | ** that we have to include the process ID as part of the key. On some |
| 157 | ** threading implementations (ex: linux), each thread has a separate |
| 158 | ** process ID. |
| 159 | */ |
| 160 | struct lockKey { |
| 161 | dev_t dev; /* Device number */ |
| 162 | ino_t ino; /* Inode number */ |
| 163 | pid_t pid; /* Process ID */ |
| 164 | }; |
| 165 | |
| 166 | /* |
| 167 | ** An instance of the following structure is allocated for each open |
| 168 | ** inode on each thread with a different process ID. (Threads have |
| 169 | ** different process IDs on linux, but not on most other unixes.) |
| 170 | ** |
| 171 | ** A single inode can have multiple file descriptors, so each OsFile |
| 172 | ** structure contains a pointer to an instance of this object and this |
| 173 | ** object keeps a count of the number of OsFiles pointing to it. |
| 174 | */ |
| 175 | struct lockInfo { |
| 176 | struct lockKey key; /* The lookup key */ |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 177 | int cnt; /* Number of SHARED locks held */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 178 | int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 179 | int nRef; /* Number of pointers to this structure */ |
| 180 | }; |
| 181 | |
| 182 | /* |
| 183 | ** An instance of the following structure serves as the key used |
| 184 | ** to locate a particular openCnt structure given its inode. This |
| 185 | ** is the same as the lockKey except that the process ID is omitted. |
| 186 | */ |
| 187 | struct openKey { |
| 188 | dev_t dev; /* Device number */ |
| 189 | ino_t ino; /* Inode number */ |
| 190 | }; |
| 191 | |
| 192 | /* |
| 193 | ** An instance of the following structure is allocated for each open |
| 194 | ** inode. This structure keeps track of the number of locks on that |
| 195 | ** inode. If a close is attempted against an inode that is holding |
| 196 | ** locks, the close is deferred until all locks clear by adding the |
| 197 | ** file descriptor to be closed to the pending list. |
| 198 | */ |
| 199 | struct openCnt { |
| 200 | struct openKey key; /* The lookup key */ |
| 201 | int nRef; /* Number of pointers to this structure */ |
| 202 | int nLock; /* Number of outstanding locks */ |
| 203 | int nPending; /* Number of pending close() operations */ |
| 204 | int *aPending; /* Malloced space holding fd's awaiting a close() */ |
| 205 | }; |
| 206 | |
| 207 | /* |
| 208 | ** These hash table maps inodes and process IDs into lockInfo and openCnt |
| 209 | ** structures. Access to these hash tables must be protected by a mutex. |
| 210 | */ |
| 211 | static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 }; |
| 212 | static Hash openHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 }; |
| 213 | |
| 214 | /* |
| 215 | ** Release a lockInfo structure previously allocated by findLockInfo(). |
| 216 | */ |
| 217 | static void releaseLockInfo(struct lockInfo *pLock){ |
| 218 | pLock->nRef--; |
| 219 | if( pLock->nRef==0 ){ |
| 220 | sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0); |
| 221 | sqliteFree(pLock); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | ** Release a openCnt structure previously allocated by findLockInfo(). |
| 227 | */ |
| 228 | static void releaseOpenCnt(struct openCnt *pOpen){ |
| 229 | pOpen->nRef--; |
| 230 | if( pOpen->nRef==0 ){ |
| 231 | sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0); |
| 232 | sqliteFree(pOpen->aPending); |
| 233 | sqliteFree(pOpen); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | ** Given a file descriptor, locate lockInfo and openCnt structures that |
| 239 | ** describes that file descriptor. Create a new ones if necessary. The |
| 240 | ** return values might be unset if an error occurs. |
| 241 | ** |
| 242 | ** Return the number of errors. |
| 243 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 244 | static int findLockInfo( |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 245 | int fd, /* The file descriptor used in the key */ |
| 246 | struct lockInfo **ppLock, /* Return the lockInfo structure here */ |
| 247 | struct openCnt **ppOpen /* Return the openCnt structure here */ |
| 248 | ){ |
| 249 | int rc; |
| 250 | struct lockKey key1; |
| 251 | struct openKey key2; |
| 252 | struct stat statbuf; |
| 253 | struct lockInfo *pLock; |
| 254 | struct openCnt *pOpen; |
| 255 | rc = fstat(fd, &statbuf); |
| 256 | if( rc!=0 ) return 1; |
| 257 | memset(&key1, 0, sizeof(key1)); |
| 258 | key1.dev = statbuf.st_dev; |
| 259 | key1.ino = statbuf.st_ino; |
| 260 | key1.pid = getpid(); |
| 261 | memset(&key2, 0, sizeof(key2)); |
| 262 | key2.dev = statbuf.st_dev; |
| 263 | key2.ino = statbuf.st_ino; |
| 264 | pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1)); |
| 265 | if( pLock==0 ){ |
| 266 | struct lockInfo *pOld; |
| 267 | pLock = sqliteMallocRaw( sizeof(*pLock) ); |
| 268 | if( pLock==0 ) return 1; |
| 269 | pLock->key = key1; |
| 270 | pLock->nRef = 1; |
| 271 | pLock->cnt = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 272 | pLock->locktype = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 273 | pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock); |
| 274 | if( pOld!=0 ){ |
| 275 | assert( pOld==pLock ); |
| 276 | sqliteFree(pLock); |
| 277 | return 1; |
| 278 | } |
| 279 | }else{ |
| 280 | pLock->nRef++; |
| 281 | } |
| 282 | *ppLock = pLock; |
| 283 | pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2)); |
| 284 | if( pOpen==0 ){ |
| 285 | struct openCnt *pOld; |
| 286 | pOpen = sqliteMallocRaw( sizeof(*pOpen) ); |
| 287 | if( pOpen==0 ){ |
| 288 | releaseLockInfo(pLock); |
| 289 | return 1; |
| 290 | } |
| 291 | pOpen->key = key2; |
| 292 | pOpen->nRef = 1; |
| 293 | pOpen->nLock = 0; |
| 294 | pOpen->nPending = 0; |
| 295 | pOpen->aPending = 0; |
| 296 | pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen); |
| 297 | if( pOld!=0 ){ |
| 298 | assert( pOld==pOpen ); |
| 299 | sqliteFree(pOpen); |
| 300 | releaseLockInfo(pLock); |
| 301 | return 1; |
| 302 | } |
| 303 | }else{ |
| 304 | pOpen->nRef++; |
| 305 | } |
| 306 | *ppOpen = pOpen; |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | ** Delete the named file |
| 312 | */ |
| 313 | int sqlite3OsDelete(const char *zFilename){ |
| 314 | unlink(zFilename); |
| 315 | return SQLITE_OK; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | ** Return TRUE if the named file exists. |
| 320 | */ |
| 321 | int sqlite3OsFileExists(const char *zFilename){ |
| 322 | return access(zFilename, 0)==0; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | ** Attempt to open a file for both reading and writing. If that |
| 327 | ** fails, try opening it read-only. If the file does not exist, |
| 328 | ** try to create it. |
| 329 | ** |
| 330 | ** On success, a handle for the open file is written to *id |
| 331 | ** and *pReadonly is set to 0 if the file was opened for reading and |
| 332 | ** writing or 1 if the file was opened read-only. The function returns |
| 333 | ** SQLITE_OK. |
| 334 | ** |
| 335 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 336 | ** *id and *pReadonly unchanged. |
| 337 | */ |
| 338 | int sqlite3OsOpenReadWrite( |
| 339 | const char *zFilename, |
| 340 | OsFile *id, |
| 341 | int *pReadonly |
| 342 | ){ |
| 343 | int rc; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 344 | assert( !id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 345 | id->dirfd = -1; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 346 | id->h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY, 0644); |
| 347 | if( id->h<0 ){ |
| 348 | id->h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY); |
| 349 | if( id->h<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 350 | return SQLITE_CANTOPEN; |
| 351 | } |
| 352 | *pReadonly = 1; |
| 353 | }else{ |
| 354 | *pReadonly = 0; |
| 355 | } |
| 356 | sqlite3OsEnterMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 357 | rc = findLockInfo(id->h, &id->pLock, &id->pOpen); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 358 | sqlite3OsLeaveMutex(); |
| 359 | if( rc ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 360 | close(id->h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 361 | return SQLITE_NOMEM; |
| 362 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 363 | id->locktype = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 364 | id->isOpen = 1; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 365 | TRACE3("OPEN %-3d %s\n", id->h, zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 366 | OpenCounter(+1); |
| 367 | return SQLITE_OK; |
| 368 | } |
| 369 | |
| 370 | |
| 371 | /* |
| 372 | ** Attempt to open a new file for exclusive access by this process. |
| 373 | ** The file will be opened for both reading and writing. To avoid |
| 374 | ** a potential security problem, we do not allow the file to have |
| 375 | ** previously existed. Nor do we allow the file to be a symbolic |
| 376 | ** link. |
| 377 | ** |
| 378 | ** If delFlag is true, then make arrangements to automatically delete |
| 379 | ** the file when it is closed. |
| 380 | ** |
| 381 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 382 | ** |
| 383 | ** On failure, return SQLITE_CANTOPEN. |
| 384 | */ |
| 385 | int sqlite3OsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){ |
| 386 | int rc; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 387 | assert( !id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 388 | if( access(zFilename, 0)==0 ){ |
| 389 | return SQLITE_CANTOPEN; |
| 390 | } |
| 391 | id->dirfd = -1; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 392 | id->h = open(zFilename, |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 393 | O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY, 0600); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 394 | if( id->h<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 395 | return SQLITE_CANTOPEN; |
| 396 | } |
| 397 | sqlite3OsEnterMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 398 | rc = findLockInfo(id->h, &id->pLock, &id->pOpen); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 399 | sqlite3OsLeaveMutex(); |
| 400 | if( rc ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 401 | close(id->h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 402 | unlink(zFilename); |
| 403 | return SQLITE_NOMEM; |
| 404 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 405 | id->locktype = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 406 | id->isOpen = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 407 | if( delFlag ){ |
| 408 | unlink(zFilename); |
| 409 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 410 | TRACE3("OPEN-EX %-3d %s\n", id->h, zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 411 | OpenCounter(+1); |
| 412 | return SQLITE_OK; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | ** Attempt to open a new file for read-only access. |
| 417 | ** |
| 418 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 419 | ** |
| 420 | ** On failure, return SQLITE_CANTOPEN. |
| 421 | */ |
| 422 | int sqlite3OsOpenReadOnly(const char *zFilename, OsFile *id){ |
| 423 | int rc; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 424 | assert( !id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 425 | id->dirfd = -1; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 426 | id->h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY); |
| 427 | if( id->h<0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 428 | return SQLITE_CANTOPEN; |
| 429 | } |
| 430 | sqlite3OsEnterMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 431 | rc = findLockInfo(id->h, &id->pLock, &id->pOpen); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 432 | sqlite3OsLeaveMutex(); |
| 433 | if( rc ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 434 | close(id->h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 435 | return SQLITE_NOMEM; |
| 436 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 437 | id->locktype = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 438 | id->isOpen = 1; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 439 | TRACE3("OPEN-RO %-3d %s\n", id->h, zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 440 | OpenCounter(+1); |
| 441 | return SQLITE_OK; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | ** Attempt to open a file descriptor for the directory that contains a |
| 446 | ** file. This file descriptor can be used to fsync() the directory |
| 447 | ** in order to make sure the creation of a new file is actually written |
| 448 | ** to disk. |
| 449 | ** |
| 450 | ** This routine is only meaningful for Unix. It is a no-op under |
| 451 | ** windows since windows does not support hard links. |
| 452 | ** |
| 453 | ** On success, a handle for a previously open file is at *id is |
| 454 | ** updated with the new directory file descriptor and SQLITE_OK is |
| 455 | ** returned. |
| 456 | ** |
| 457 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 458 | ** *id unchanged. |
| 459 | */ |
| 460 | int sqlite3OsOpenDirectory( |
| 461 | const char *zDirname, |
| 462 | OsFile *id |
| 463 | ){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 464 | if( !id->isOpen ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 465 | /* Do not open the directory if the corresponding file is not already |
| 466 | ** open. */ |
| 467 | return SQLITE_CANTOPEN; |
| 468 | } |
| 469 | assert( id->dirfd<0 ); |
| 470 | id->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0644); |
| 471 | if( id->dirfd<0 ){ |
| 472 | return SQLITE_CANTOPEN; |
| 473 | } |
| 474 | TRACE3("OPENDIR %-3d %s\n", id->dirfd, zDirname); |
| 475 | return SQLITE_OK; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | ** Create a temporary file name in zBuf. zBuf must be big enough to |
| 480 | ** hold at least SQLITE_TEMPNAME_SIZE characters. |
| 481 | */ |
| 482 | int sqlite3OsTempFileName(char *zBuf){ |
| 483 | static const char *azDirs[] = { |
| 484 | "/var/tmp", |
| 485 | "/usr/tmp", |
| 486 | "/tmp", |
| 487 | ".", |
| 488 | }; |
| 489 | static unsigned char zChars[] = |
| 490 | "abcdefghijklmnopqrstuvwxyz" |
| 491 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 492 | "0123456789"; |
| 493 | int i, j; |
| 494 | struct stat buf; |
| 495 | const char *zDir = "."; |
| 496 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
| 497 | if( stat(azDirs[i], &buf) ) continue; |
| 498 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 499 | if( access(azDirs[i], 07) ) continue; |
| 500 | zDir = azDirs[i]; |
| 501 | break; |
| 502 | } |
| 503 | do{ |
| 504 | sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir); |
| 505 | j = strlen(zBuf); |
| 506 | sqlite3Randomness(15, &zBuf[j]); |
| 507 | for(i=0; i<15; i++, j++){ |
| 508 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 509 | } |
| 510 | zBuf[j] = 0; |
| 511 | }while( access(zBuf,0)==0 ); |
| 512 | return SQLITE_OK; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | ** Close a file. |
| 517 | */ |
| 518 | int sqlite3OsClose(OsFile *id){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 519 | if( !id->isOpen ) return SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 520 | sqlite3OsUnlock(id, NO_LOCK); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 521 | if( id->dirfd>=0 ) close(id->dirfd); |
| 522 | id->dirfd = -1; |
| 523 | sqlite3OsEnterMutex(); |
| 524 | if( id->pOpen->nLock ){ |
| 525 | /* If there are outstanding locks, do not actually close the file just |
| 526 | ** yet because that would clear those locks. Instead, add the file |
| 527 | ** descriptor to pOpen->aPending. It will be automatically closed when |
| 528 | ** the last lock is cleared. |
| 529 | */ |
| 530 | int *aNew; |
| 531 | struct openCnt *pOpen = id->pOpen; |
| 532 | pOpen->nPending++; |
| 533 | aNew = sqliteRealloc( pOpen->aPending, pOpen->nPending*sizeof(int) ); |
| 534 | if( aNew==0 ){ |
| 535 | /* If a malloc fails, just leak the file descriptor */ |
| 536 | }else{ |
| 537 | pOpen->aPending = aNew; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 538 | pOpen->aPending[pOpen->nPending-1] = id->h; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 539 | } |
| 540 | }else{ |
| 541 | /* There are no outstanding locks so we can close the file immediately */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 542 | close(id->h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 543 | } |
| 544 | releaseLockInfo(id->pLock); |
| 545 | releaseOpenCnt(id->pOpen); |
| 546 | sqlite3OsLeaveMutex(); |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 547 | id->isOpen = 0; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 548 | TRACE2("CLOSE %-3d\n", id->h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 549 | OpenCounter(-1); |
| 550 | return SQLITE_OK; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 555 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 556 | ** wrong. |
| 557 | */ |
| 558 | int sqlite3OsRead(OsFile *id, void *pBuf, int amt){ |
| 559 | int got; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 560 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 561 | SimulateIOError(SQLITE_IOERR); |
| 562 | TIMER_START; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 563 | got = read(id->h, pBuf, amt); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 564 | TIMER_END; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 565 | TRACE4("READ %-3d %7d %d\n", id->h, last_page, elapse); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 566 | SEEK(0); |
| 567 | /* if( got<0 ) got = 0; */ |
| 568 | if( got==amt ){ |
| 569 | return SQLITE_OK; |
| 570 | }else{ |
| 571 | return SQLITE_IOERR; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 577 | ** or some other error code on failure. |
| 578 | */ |
| 579 | int sqlite3OsWrite(OsFile *id, const void *pBuf, int amt){ |
| 580 | int wrote = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 581 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 582 | SimulateIOError(SQLITE_IOERR); |
| 583 | TIMER_START; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 584 | while( amt>0 && (wrote = write(id->h, pBuf, amt))>0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 585 | amt -= wrote; |
| 586 | pBuf = &((char*)pBuf)[wrote]; |
| 587 | } |
| 588 | TIMER_END; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 589 | TRACE4("WRITE %-3d %7d %d\n", id->h, last_page, elapse); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 590 | SEEK(0); |
| 591 | if( amt>0 ){ |
| 592 | return SQLITE_FULL; |
| 593 | } |
| 594 | return SQLITE_OK; |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | ** Move the read/write pointer in a file. |
| 599 | */ |
| 600 | int sqlite3OsSeek(OsFile *id, off_t offset){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 601 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 602 | SEEK(offset/1024 + 1); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 603 | lseek(id->h, offset, SEEK_SET); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 604 | return SQLITE_OK; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | ** Make sure all writes to a particular file are committed to disk. |
| 609 | ** |
| 610 | ** Under Unix, also make sure that the directory entry for the file |
| 611 | ** has been created by fsync-ing the directory that contains the file. |
| 612 | ** If we do not do this and we encounter a power failure, the directory |
| 613 | ** entry for the journal might not exist after we reboot. The next |
| 614 | ** SQLite to access the file will not know that the journal exists (because |
| 615 | ** the directory entry for the journal was never created) and the transaction |
| 616 | ** will not roll back - possibly leading to database corruption. |
| 617 | */ |
| 618 | int sqlite3OsSync(OsFile *id){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 619 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 620 | SimulateIOError(SQLITE_IOERR); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 621 | TRACE2("SYNC %-3d\n", id->h); |
| 622 | if( fsync(id->h) ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 623 | return SQLITE_IOERR; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 624 | } |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 625 | if( id->dirfd>=0 ){ |
| 626 | TRACE2("DIRSYNC %-3d\n", id->dirfd); |
| 627 | fsync(id->dirfd); |
| 628 | close(id->dirfd); /* Only need to sync once, so close the directory */ |
| 629 | id->dirfd = -1; /* when we are done. */ |
| 630 | } |
drh | a285422 | 2004-06-17 19:04:17 +0000 | [diff] [blame] | 631 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 635 | ** Sync the directory zDirname. This is a no-op on operating systems other |
| 636 | ** than UNIX. |
| 637 | */ |
| 638 | int sqlite3OsSyncDirectory(const char *zDirname){ |
| 639 | int fd; |
| 640 | int r; |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 641 | SimulateIOError(SQLITE_IOERR); |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 642 | fd = open(zDirname, O_RDONLY|O_BINARY, 0644); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 643 | TRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname); |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 644 | if( fd<0 ){ |
| 645 | return SQLITE_CANTOPEN; |
| 646 | } |
| 647 | r = fsync(fd); |
| 648 | close(fd); |
| 649 | return ((r==0)?SQLITE_OK:SQLITE_IOERR); |
| 650 | } |
| 651 | |
| 652 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 653 | ** Truncate an open file to a specified size |
| 654 | */ |
| 655 | int sqlite3OsTruncate(OsFile *id, off_t nByte){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 656 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 657 | SimulateIOError(SQLITE_IOERR); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 658 | return ftruncate(id->h, nByte)==0 ? SQLITE_OK : SQLITE_IOERR; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
| 662 | ** Determine the current size of a file in bytes |
| 663 | */ |
| 664 | int sqlite3OsFileSize(OsFile *id, off_t *pSize){ |
| 665 | struct stat buf; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 666 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 667 | SimulateIOError(SQLITE_IOERR); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 668 | if( fstat(id->h, &buf)!=0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 669 | return SQLITE_IOERR; |
| 670 | } |
| 671 | *pSize = buf.st_size; |
| 672 | return SQLITE_OK; |
| 673 | } |
| 674 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 675 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 676 | ** This routine checks if there is a RESERVED lock held on the specified |
| 677 | ** file by this or any other process. If such a lock is held, return |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 678 | ** non-zero. If the file is unlocked or holds only SHARED locks, then |
| 679 | ** return zero. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 680 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 681 | int sqlite3OsCheckReservedLock(OsFile *id){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 682 | int r = 0; |
| 683 | |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 684 | assert( id->isOpen ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 685 | sqlite3OsEnterMutex(); /* Needed because id->pLock is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 686 | |
| 687 | /* Check if a thread in this process holds such a lock */ |
| 688 | if( id->pLock->locktype>SHARED_LOCK ){ |
| 689 | r = 1; |
| 690 | } |
| 691 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 692 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 693 | */ |
| 694 | if( !r ){ |
| 695 | struct flock lock; |
| 696 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 697 | lock.l_start = RESERVED_BYTE; |
| 698 | lock.l_len = 1; |
| 699 | lock.l_type = F_WRLCK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 700 | fcntl(id->h, F_GETLK, &lock); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 701 | if( lock.l_type!=F_UNLCK ){ |
| 702 | r = 1; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | sqlite3OsLeaveMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 707 | TRACE3("TEST WR-LOCK %d %d\n", id->h, r); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 708 | |
| 709 | return r; |
| 710 | } |
| 711 | |
| 712 | /* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 713 | ** Lock the file with the lock specified by parameter locktype - one |
| 714 | ** of the following: |
| 715 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 716 | ** (1) SHARED_LOCK |
| 717 | ** (2) RESERVED_LOCK |
| 718 | ** (3) PENDING_LOCK |
| 719 | ** (4) EXCLUSIVE_LOCK |
| 720 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 721 | ** Sometimes when requesting one lock state, additional lock states |
| 722 | ** are inserted in between. The locking might fail on one of the later |
| 723 | ** transitions leaving the lock state different from what it started but |
| 724 | ** still short of its goal. The following chart shows the allowed |
| 725 | ** transitions and the inserted intermediate states: |
| 726 | ** |
| 727 | ** UNLOCKED -> SHARED |
| 728 | ** SHARED -> RESERVED |
| 729 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 730 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 731 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 732 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 733 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 734 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 735 | */ |
| 736 | int sqlite3OsLock(OsFile *id, int locktype){ |
| 737 | int rc = SQLITE_OK; |
| 738 | struct lockInfo *pLock = id->pLock; |
| 739 | struct flock lock; |
| 740 | int s; |
| 741 | |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 742 | assert( id->isOpen ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 743 | TRACE6("LOCK %d %d was %d(%d,%d)\n", |
| 744 | id->h, locktype, id->locktype, pLock->locktype, pLock->cnt); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 745 | |
| 746 | /* If there is already a lock of this type or more restrictive on the |
| 747 | ** OsFile, do nothing. Don't use the end_lock: exit path, as |
| 748 | ** sqlite3OsEnterMutex() hasn't been called yet. |
| 749 | */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 750 | if( id->locktype>=locktype ){ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 751 | return SQLITE_OK; |
| 752 | } |
| 753 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 754 | /* Make sure the locking sequence is correct |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 755 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 756 | assert( id->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 757 | assert( locktype!=PENDING_LOCK ); |
| 758 | assert( locktype!=RESERVED_LOCK || id->locktype==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 759 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 760 | /* This mutex is needed because id->pLock is shared across threads |
| 761 | */ |
| 762 | sqlite3OsEnterMutex(); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 763 | |
| 764 | /* If some thread using this PID has a lock via a different OsFile* |
| 765 | ** handle that precludes the requested lock, return BUSY. |
| 766 | */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 767 | if( (id->locktype!=pLock->locktype && |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 768 | (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 769 | ){ |
| 770 | rc = SQLITE_BUSY; |
| 771 | goto end_lock; |
| 772 | } |
| 773 | |
| 774 | /* If a SHARED lock is requested, and some thread using this PID already |
| 775 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 776 | ** return SQLITE_OK. |
| 777 | */ |
| 778 | if( locktype==SHARED_LOCK && |
| 779 | (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){ |
| 780 | assert( locktype==SHARED_LOCK ); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 781 | assert( id->locktype==0 ); |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 782 | assert( pLock->cnt>0 ); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 783 | id->locktype = SHARED_LOCK; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 784 | pLock->cnt++; |
| 785 | id->pOpen->nLock++; |
| 786 | goto end_lock; |
| 787 | } |
| 788 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 789 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 790 | lock.l_whence = SEEK_SET; |
| 791 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 792 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 793 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 794 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 795 | */ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 796 | if( locktype==SHARED_LOCK |
| 797 | || (locktype==EXCLUSIVE_LOCK && id->locktype<PENDING_LOCK) |
| 798 | ){ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 799 | lock.l_type = F_RDLCK; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 800 | lock.l_start = PENDING_BYTE; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 801 | s = fcntl(id->h, F_SETLK, &lock); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 802 | if( s ){ |
| 803 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 804 | goto end_lock; |
| 805 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | |
| 809 | /* If control gets to this point, then actually go ahead and make |
| 810 | ** operating system calls for the specified lock. |
| 811 | */ |
| 812 | if( locktype==SHARED_LOCK ){ |
| 813 | assert( pLock->cnt==0 ); |
| 814 | assert( pLock->locktype==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 815 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 816 | /* Now get the read-lock */ |
| 817 | lock.l_start = SHARED_FIRST; |
| 818 | lock.l_len = SHARED_SIZE; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 819 | s = fcntl(id->h, F_SETLK, &lock); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 820 | |
| 821 | /* Drop the temporary PENDING lock */ |
| 822 | lock.l_start = PENDING_BYTE; |
| 823 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 824 | lock.l_type = F_UNLCK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 825 | fcntl(id->h, F_SETLK, &lock); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 826 | if( s ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 827 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 828 | }else{ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 829 | id->locktype = SHARED_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 830 | id->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 831 | pLock->cnt = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 832 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 833 | }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){ |
| 834 | /* We are trying for an exclusive lock but another thread in this |
| 835 | ** same process is still holding a shared lock. */ |
| 836 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 837 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 838 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 839 | ** assumed that there is a SHARED or greater lock on the file |
| 840 | ** already. |
| 841 | */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 842 | assert( 0!=id->locktype ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 843 | lock.l_type = F_WRLCK; |
| 844 | switch( locktype ){ |
| 845 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 846 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 847 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 848 | case EXCLUSIVE_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 849 | lock.l_start = SHARED_FIRST; |
| 850 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 851 | break; |
| 852 | default: |
| 853 | assert(0); |
| 854 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 855 | s = fcntl(id->h, F_SETLK, &lock); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 856 | if( s ){ |
| 857 | rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY; |
| 858 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 859 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 860 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 861 | if( rc==SQLITE_OK ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 862 | id->locktype = locktype; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 863 | pLock->locktype = locktype; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 864 | }else if( locktype==EXCLUSIVE_LOCK ){ |
| 865 | id->locktype = PENDING_LOCK; |
| 866 | pLock->locktype = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 867 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 868 | |
| 869 | end_lock: |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 870 | sqlite3OsLeaveMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 871 | TRACE4("LOCK %d %d %s\n", id->h, locktype, rc==SQLITE_OK ? "ok" : "failed"); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 872 | return rc; |
| 873 | } |
| 874 | |
| 875 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 876 | ** Lower the locking level on file descriptor id to locktype. locktype |
| 877 | ** must be either NO_LOCK or SHARED_LOCK. |
| 878 | ** |
| 879 | ** If the locking level of the file descriptor is already at or below |
| 880 | ** the requested locking level, this routine is a no-op. |
| 881 | ** |
| 882 | ** It is not possible for this routine to fail. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 883 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 884 | int sqlite3OsUnlock(OsFile *id, int locktype){ |
| 885 | struct lockInfo *pLock; |
| 886 | struct flock lock; |
| 887 | |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame^] | 888 | assert( id->isOpen ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 889 | TRACE6("UNLOCK %d %d was %d(%d,%d)\n", |
| 890 | id->h, locktype, id->locktype, id->pLock->locktype, id->pLock->cnt); |
| 891 | |
| 892 | assert( locktype<=SHARED_LOCK ); |
| 893 | if( id->locktype<=locktype ){ |
| 894 | return SQLITE_OK; |
| 895 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 896 | sqlite3OsEnterMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 897 | pLock = id->pLock; |
| 898 | assert( pLock->cnt!=0 ); |
| 899 | if( id->locktype>SHARED_LOCK ){ |
| 900 | assert( pLock->locktype==id->locktype ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 901 | lock.l_type = F_UNLCK; |
| 902 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 903 | lock.l_start = PENDING_BYTE; |
| 904 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
| 905 | fcntl(id->h, F_SETLK, &lock); |
| 906 | pLock->locktype = SHARED_LOCK; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 907 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 908 | if( locktype==NO_LOCK ){ |
| 909 | struct openCnt *pOpen; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 910 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 911 | /* Decrement the shared lock counter. Release the lock using an |
| 912 | ** OS call only when all threads in this same process have released |
| 913 | ** the lock. |
| 914 | */ |
| 915 | pLock->cnt--; |
| 916 | if( pLock->cnt==0 ){ |
| 917 | lock.l_type = F_UNLCK; |
| 918 | lock.l_whence = SEEK_SET; |
| 919 | lock.l_start = lock.l_len = 0L; |
| 920 | fcntl(id->h, F_SETLK, &lock); |
| 921 | pLock->locktype = NO_LOCK; |
| 922 | } |
| 923 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 924 | /* Decrement the count of locks against this same file. When the |
| 925 | ** count reaches zero, close any other file descriptors whose close |
| 926 | ** was deferred because of outstanding locks. |
| 927 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 928 | pOpen = id->pOpen; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 929 | pOpen->nLock--; |
| 930 | assert( pOpen->nLock>=0 ); |
| 931 | if( pOpen->nLock==0 && pOpen->nPending>0 ){ |
| 932 | int i; |
| 933 | for(i=0; i<pOpen->nPending; i++){ |
| 934 | close(pOpen->aPending[i]); |
| 935 | } |
| 936 | sqliteFree(pOpen->aPending); |
| 937 | pOpen->nPending = 0; |
| 938 | pOpen->aPending = 0; |
| 939 | } |
| 940 | } |
| 941 | sqlite3OsLeaveMutex(); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 942 | id->locktype = locktype; |
| 943 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /* |
| 947 | ** Get information to seed the random number generator. The seed |
| 948 | ** is written into the buffer zBuf[256]. The calling function must |
| 949 | ** supply a sufficiently large buffer. |
| 950 | */ |
| 951 | int sqlite3OsRandomSeed(char *zBuf){ |
| 952 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 953 | ** errors. The reports issued by valgrind are incorrect - we would |
| 954 | ** prefer that the randomness be increased by making use of the |
| 955 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 956 | ** some users. Rather than argue, it seems easier just to initialize |
| 957 | ** the whole array and silence valgrind, even if that means less randomness |
| 958 | ** in the random seed. |
| 959 | ** |
| 960 | ** When testing, initializing zBuf[] to zero is all we do. That means |
| 961 | ** that we always use the same random number sequence.* This makes the |
| 962 | ** tests repeatable. |
| 963 | */ |
| 964 | memset(zBuf, 0, 256); |
| 965 | #if !defined(SQLITE_TEST) |
| 966 | { |
| 967 | int pid; |
| 968 | time((time_t*)zBuf); |
| 969 | pid = getpid(); |
| 970 | memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid)); |
| 971 | } |
| 972 | #endif |
| 973 | return SQLITE_OK; |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | ** Sleep for a little while. Return the amount of time slept. |
| 978 | */ |
| 979 | int sqlite3OsSleep(int ms){ |
| 980 | #if defined(HAVE_USLEEP) && HAVE_USLEEP |
| 981 | usleep(ms*1000); |
| 982 | return ms; |
| 983 | #else |
| 984 | sleep((ms+999)/1000); |
| 985 | return 1000*((ms+999)/1000); |
| 986 | #endif |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | ** Static variables used for thread synchronization |
| 991 | */ |
| 992 | static int inMutex = 0; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 993 | #ifdef SQLITE_UNIX_THREADS |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 994 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 995 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 996 | |
| 997 | /* |
| 998 | ** The following pair of routine implement mutual exclusion for |
| 999 | ** multi-threaded processes. Only a single thread is allowed to |
| 1000 | ** executed code that is surrounded by EnterMutex() and LeaveMutex(). |
| 1001 | ** |
| 1002 | ** SQLite uses only a single Mutex. There is not much critical |
| 1003 | ** code and what little there is executes quickly and without blocking. |
| 1004 | */ |
| 1005 | void sqlite3OsEnterMutex(){ |
| 1006 | #ifdef SQLITE_UNIX_THREADS |
| 1007 | pthread_mutex_lock(&mutex); |
| 1008 | #endif |
| 1009 | assert( !inMutex ); |
| 1010 | inMutex = 1; |
| 1011 | } |
| 1012 | void sqlite3OsLeaveMutex(){ |
| 1013 | assert( inMutex ); |
| 1014 | inMutex = 0; |
| 1015 | #ifdef SQLITE_UNIX_THREADS |
| 1016 | pthread_mutex_unlock(&mutex); |
| 1017 | #endif |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | ** Turn a relative pathname into a full pathname. Return a pointer |
| 1022 | ** to the full pathname stored in space obtained from sqliteMalloc(). |
| 1023 | ** The calling function is responsible for freeing this space once it |
| 1024 | ** is no longer needed. |
| 1025 | */ |
| 1026 | char *sqlite3OsFullPathname(const char *zRelative){ |
| 1027 | char *zFull = 0; |
| 1028 | if( zRelative[0]=='/' ){ |
| 1029 | sqlite3SetString(&zFull, zRelative, (char*)0); |
| 1030 | }else{ |
| 1031 | char zBuf[5000]; |
| 1032 | sqlite3SetString(&zFull, getcwd(zBuf, sizeof(zBuf)), "/", zRelative, |
| 1033 | (char*)0); |
| 1034 | } |
| 1035 | return zFull; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | ** The following variable, if set to a non-zero value, becomes the result |
| 1040 | ** returned from sqlite3OsCurrentTime(). This is used for testing. |
| 1041 | */ |
| 1042 | #ifdef SQLITE_TEST |
| 1043 | int sqlite3_current_time = 0; |
| 1044 | #endif |
| 1045 | |
| 1046 | /* |
| 1047 | ** Find the current time (in Universal Coordinated Time). Write the |
| 1048 | ** current time and date as a Julian Day number into *prNow and |
| 1049 | ** return 0. Return 1 if the time and date cannot be found. |
| 1050 | */ |
| 1051 | int sqlite3OsCurrentTime(double *prNow){ |
| 1052 | time_t t; |
| 1053 | time(&t); |
| 1054 | *prNow = t/86400.0 + 2440587.5; |
| 1055 | #ifdef SQLITE_TEST |
| 1056 | if( sqlite3_current_time ){ |
| 1057 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 1058 | } |
| 1059 | #endif |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
drh | bf9a7e4 | 2004-06-15 00:29:03 +0000 | [diff] [blame] | 1063 | /* |
| 1064 | ** Find the time that the file was last modified. Write the |
| 1065 | ** modification time and date as a Julian Day number into *prNow and |
| 1066 | ** return SQLITE_OK. Return SQLITE_ERROR if the modification |
| 1067 | ** time cannot be found. |
| 1068 | */ |
| 1069 | int sqlite3OsFileModTime(OsFile *id, double *prNow){ |
| 1070 | int rc; |
| 1071 | struct stat statbuf; |
| 1072 | if( fstat(id->h, &statbuf)==0 ){ |
| 1073 | *prNow = statbuf.st_mtime/86400.0 + 2440587.5; |
| 1074 | rc = SQLITE_OK; |
| 1075 | }else{ |
| 1076 | rc = SQLITE_ERROR; |
| 1077 | } |
| 1078 | return rc; |
| 1079 | } |
| 1080 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1081 | #endif /* OS_UNIX */ |