blob: 0f4bb71a842aa8af92c7d8ee8bbd53e89080172a [file] [log] [blame]
drhbbd42a62004-05-22 17:41:58 +00001/*
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**
drh734c9862008-11-28 15:37:20 +000013** This file contains the VFS implementation for unix-like operating systems
14** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
danielk1977822a5162008-05-16 04:51:54 +000015**
drh734c9862008-11-28 15:37:20 +000016** There are actually several different VFS implementations in this file.
17** The differences are in the way that file locking is done. The default
18** implementation uses Posix Advisory Locks. Alternative implementations
19** use flock(), dot-files, various proprietary locking schemas, or simply
20** skip locking all together.
21**
drh9b35ea62008-11-29 02:20:26 +000022** This source file is organized into divisions where the logic for various
drh734c9862008-11-28 15:37:20 +000023** subfunctions is contained within the appropriate division. PLEASE
24** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
25** in the correct division and should be clearly labeled.
26**
drh6b9d6dd2008-12-03 19:34:47 +000027** The layout of divisions is as follows:
drh734c9862008-11-28 15:37:20 +000028**
29** * General-purpose declarations and utility functions.
30** * Unique file ID logic used by VxWorks.
drh715ff302008-12-03 22:32:44 +000031** * Various locking primitive implementations (all except proxy locking):
drh734c9862008-11-28 15:37:20 +000032** + for Posix Advisory Locks
33** + for no-op locks
34** + for dot-file locks
35** + for flock() locking
36** + for named semaphore locks (VxWorks only)
37** + for AFP filesystem locks (MacOSX only)
drh9b35ea62008-11-29 02:20:26 +000038** * sqlite3_file methods not associated with locking.
39** * Definitions of sqlite3_io_methods objects for all locking
40** methods plus "finder" functions for each locking method.
drh6b9d6dd2008-12-03 19:34:47 +000041** * sqlite3_vfs method implementations.
drh715ff302008-12-03 22:32:44 +000042** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
drh9b35ea62008-11-29 02:20:26 +000043** * Definitions of sqlite3_vfs objects for all locking methods
44** plus implementations of sqlite3_os_init() and sqlite3_os_end().
drh734c9862008-11-28 15:37:20 +000045**
drhc66d5b62008-12-03 22:48:32 +000046** $Id: os_unix.c,v 1.228 2008/12/03 22:48:33 drh Exp $
drhbbd42a62004-05-22 17:41:58 +000047*/
drhbbd42a62004-05-22 17:41:58 +000048#include "sqliteInt.h"
danielk197729bafea2008-06-26 10:41:19 +000049#if SQLITE_OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000050
danielk1977e339d652008-06-28 11:23:00 +000051/*
drh6b9d6dd2008-12-03 19:34:47 +000052** There are various methods for file locking used for concurrency
53** control:
danielk1977e339d652008-06-28 11:23:00 +000054**
drh734c9862008-11-28 15:37:20 +000055** 1. POSIX locking (the default),
56** 2. No locking,
57** 3. Dot-file locking,
58** 4. flock() locking,
59** 5. AFP locking (OSX only),
60** 6. Named POSIX semaphores (VXWorks only),
61** 7. proxy locking. (OSX only)
62**
63** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
64** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
65** selection of the appropriate locking style based on the filesystem
66** where the database is located.
danielk1977e339d652008-06-28 11:23:00 +000067*/
drh40bbb0a2008-09-23 10:23:26 +000068#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
69# if defined(__DARWIN__)
70# define SQLITE_ENABLE_LOCKING_STYLE 1
71# else
72# define SQLITE_ENABLE_LOCKING_STYLE 0
73# endif
74#endif
drhbfe66312006-10-03 17:40:40 +000075
drh9cbe6352005-11-29 03:13:21 +000076/*
drh6c7d5c52008-11-21 20:32:33 +000077** Define the OS_VXWORKS pre-processor macro to 1 if building on
danielk1977397d65f2008-11-19 11:35:39 +000078** vxworks, or 0 otherwise.
79*/
drh6c7d5c52008-11-21 20:32:33 +000080#ifndef OS_VXWORKS
81# if defined(__RTP__) || defined(_WRS_KERNEL)
82# define OS_VXWORKS 1
83# else
84# define OS_VXWORKS 0
85# endif
danielk1977397d65f2008-11-19 11:35:39 +000086#endif
87
88/*
drh9cbe6352005-11-29 03:13:21 +000089** These #defines should enable >2GB file support on Posix if the
90** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000091** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000092**
93** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
94** on the compiler command line. This is necessary if you are compiling
95** on a recent machine (ex: RedHat 7.2) but you want your code to work
96** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
97** without this option, LFS is enable. But LFS does not exist in the kernel
98** in RedHat 6.0, so the code won't work. Hence, for maximum binary
99** portability you should omit LFS.
drh9b35ea62008-11-29 02:20:26 +0000100**
101** The previous paragraph was written in 2005. (This paragraph is written
102** on 2008-11-28.) These days, all Linux kernels support large files, so
103** you should probably leave LFS enabled. But some embedded platforms might
104** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
drh9cbe6352005-11-29 03:13:21 +0000105*/
106#ifndef SQLITE_DISABLE_LFS
107# define _LARGE_FILE 1
108# ifndef _FILE_OFFSET_BITS
109# define _FILE_OFFSET_BITS 64
110# endif
111# define _LARGEFILE_SOURCE 1
112#endif
drhbbd42a62004-05-22 17:41:58 +0000113
drh9cbe6352005-11-29 03:13:21 +0000114/*
115** standard include files.
116*/
117#include <sys/types.h>
118#include <sys/stat.h>
119#include <fcntl.h>
120#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +0000121#include <time.h>
drh19e2d372005-08-29 23:00:03 +0000122#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +0000123#include <errno.h>
danielk1977e339d652008-06-28 11:23:00 +0000124
drh40bbb0a2008-09-23 10:23:26 +0000125#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977c70dfc42008-11-19 13:52:30 +0000126# include <sys/ioctl.h>
drh6c7d5c52008-11-21 20:32:33 +0000127# if OS_VXWORKS
danielk1977c70dfc42008-11-19 13:52:30 +0000128# include <semaphore.h>
129# include <limits.h>
130# else
drh9b35ea62008-11-29 02:20:26 +0000131# include <sys/file.h>
danielk1977c70dfc42008-11-19 13:52:30 +0000132# include <sys/param.h>
133# include <sys/mount.h>
134# endif
drhbfe66312006-10-03 17:40:40 +0000135#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +0000136
137/*
drhf1a221e2006-01-15 17:27:17 +0000138** If we are to be thread-safe, include the pthreads header and define
139** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +0000140*/
drhd677b3d2007-08-20 22:48:41 +0000141#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000142# include <pthread.h>
143# define SQLITE_UNIX_THREADS 1
144#endif
145
146/*
147** Default permissions when creating a new file
148*/
149#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
150# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
151#endif
152
danielk1977b4b47412007-08-17 15:53:36 +0000153/*
aswiftaebf4132008-11-21 00:10:35 +0000154 ** Default permissions when creating auto proxy dir
155 */
156#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
157# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
158#endif
159
160/*
danielk1977b4b47412007-08-17 15:53:36 +0000161** Maximum supported path-length.
162*/
163#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +0000164
drh734c9862008-11-28 15:37:20 +0000165/*
drh734c9862008-11-28 15:37:20 +0000166** Only set the lastErrno if the error code is a real error and not
167** a normal expected return code of SQLITE_BUSY or SQLITE_OK
168*/
169#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
170
drh9cbe6352005-11-29 03:13:21 +0000171
172/*
drh9b35ea62008-11-29 02:20:26 +0000173** The unixFile structure is subclass of sqlite3_file specific to the unix
174** VFS implementations.
drh9cbe6352005-11-29 03:13:21 +0000175*/
drh054889e2005-11-30 03:20:31 +0000176typedef struct unixFile unixFile;
177struct unixFile {
danielk197762079062007-08-15 17:08:46 +0000178 sqlite3_io_methods const *pMethod; /* Always the first entry */
drh6c7d5c52008-11-21 20:32:33 +0000179 struct unixOpenCnt *pOpen; /* Info about all open fd's on this inode */
180 struct unixLockInfo *pLock; /* Info about locks on this inode */
181 int h; /* The file descriptor */
182 int dirfd; /* File descriptor for the directory */
183 unsigned char locktype; /* The type of lock held on this fd */
184 int lastErrno; /* The unix errno from the last I/O error */
drh6c7d5c52008-11-21 20:32:33 +0000185 void *lockingContext; /* Locking style specific state */
drh734c9862008-11-28 15:37:20 +0000186 int openFlags; /* The flags specified at open */
187#if SQLITE_THREADSAFE && defined(__linux__)
drh6c7d5c52008-11-21 20:32:33 +0000188 pthread_t tid; /* The thread that "owns" this unixFile */
189#endif
190#if OS_VXWORKS
191 int isDelete; /* Delete on close if true */
drh107886a2008-11-21 22:21:50 +0000192 struct vxworksFileId *pId; /* Unique file ID */
drh6c7d5c52008-11-21 20:32:33 +0000193#endif
danielk1977967a4a12007-08-20 14:23:44 +0000194#ifdef SQLITE_TEST
195 /* In test mode, increase the size of this structure a bit so that
196 ** it is larger than the struct CrashFile defined in test6.c.
197 */
198 char aPadding[32];
199#endif
drh9cbe6352005-11-29 03:13:21 +0000200};
201
drh0ccebe72005-06-07 22:22:50 +0000202/*
drh198bf392006-01-06 21:52:49 +0000203** Include code that is common to all os_*.c files
204*/
205#include "os_common.h"
206
207/*
drh0ccebe72005-06-07 22:22:50 +0000208** Define various macros that are missing from some systems.
209*/
drhbbd42a62004-05-22 17:41:58 +0000210#ifndef O_LARGEFILE
211# define O_LARGEFILE 0
212#endif
213#ifdef SQLITE_DISABLE_LFS
214# undef O_LARGEFILE
215# define O_LARGEFILE 0
216#endif
217#ifndef O_NOFOLLOW
218# define O_NOFOLLOW 0
219#endif
220#ifndef O_BINARY
221# define O_BINARY 0
222#endif
223
224/*
225** The DJGPP compiler environment looks mostly like Unix, but it
226** lacks the fcntl() system call. So redefine fcntl() to be something
227** that always succeeds. This means that locking does not occur under
drh85b623f2007-12-13 21:54:09 +0000228** DJGPP. But it is DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000229*/
230#ifdef __DJGPP__
231# define fcntl(A,B,C) 0
232#endif
233
234/*
drh2b4b5962005-06-15 17:47:55 +0000235** The threadid macro resolves to the thread-id or to 0. Used for
236** testing and debugging only.
237*/
drhd677b3d2007-08-20 22:48:41 +0000238#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000239#define threadid pthread_self()
240#else
241#define threadid 0
242#endif
243
danielk197713adf8a2004-06-03 16:08:41 +0000244
drh107886a2008-11-21 22:21:50 +0000245/*
246** Helper functions to obtain and relinquish the global mutex.
247*/
248static void unixEnterMutex(void){
249 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
250}
251static void unixLeaveMutex(void){
252 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
253}
254
drh734c9862008-11-28 15:37:20 +0000255
256#ifdef SQLITE_DEBUG
257/*
258** Helper function for printing out trace information from debugging
259** binaries. This returns the string represetation of the supplied
260** integer lock-type.
261*/
262static const char *locktypeName(int locktype){
263 switch( locktype ){
264 case NO_LOCK: return "NONE";
265 case SHARED_LOCK: return "SHARED";
266 case RESERVED_LOCK: return "RESERVED";
267 case PENDING_LOCK: return "PENDING";
268 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
269 }
270 return "ERROR";
271}
272#endif
273
274#ifdef SQLITE_LOCK_TRACE
275/*
276** Print out information about all locking operations.
drh6c7d5c52008-11-21 20:32:33 +0000277**
drh734c9862008-11-28 15:37:20 +0000278** This routine is used for troubleshooting locks on multithreaded
279** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
280** command-line option on the compiler. This code is normally
281** turned off.
282*/
283static int lockTrace(int fd, int op, struct flock *p){
284 char *zOpName, *zType;
285 int s;
286 int savedErrno;
287 if( op==F_GETLK ){
288 zOpName = "GETLK";
289 }else if( op==F_SETLK ){
290 zOpName = "SETLK";
291 }else{
292 s = fcntl(fd, op, p);
293 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
294 return s;
295 }
296 if( p->l_type==F_RDLCK ){
297 zType = "RDLCK";
298 }else if( p->l_type==F_WRLCK ){
299 zType = "WRLCK";
300 }else if( p->l_type==F_UNLCK ){
301 zType = "UNLCK";
302 }else{
303 assert( 0 );
304 }
305 assert( p->l_whence==SEEK_SET );
306 s = fcntl(fd, op, p);
307 savedErrno = errno;
308 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
309 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
310 (int)p->l_pid, s);
311 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
312 struct flock l2;
313 l2 = *p;
314 fcntl(fd, F_GETLK, &l2);
315 if( l2.l_type==F_RDLCK ){
316 zType = "RDLCK";
317 }else if( l2.l_type==F_WRLCK ){
318 zType = "WRLCK";
319 }else if( l2.l_type==F_UNLCK ){
320 zType = "UNLCK";
321 }else{
322 assert( 0 );
323 }
324 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
325 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
326 }
327 errno = savedErrno;
328 return s;
329}
330#define fcntl lockTrace
331#endif /* SQLITE_LOCK_TRACE */
332
333
334
335/*
336** This routine translates a standard POSIX errno code into something
337** useful to the clients of the sqlite3 functions. Specifically, it is
338** intended to translate a variety of "try again" errors into SQLITE_BUSY
339** and a variety of "please close the file descriptor NOW" errors into
340** SQLITE_IOERR
341**
342** Errors during initialization of locks, or file system support for locks,
343** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
344*/
345static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
346 switch (posixError) {
347 case 0:
348 return SQLITE_OK;
349
350 case EAGAIN:
351 case ETIMEDOUT:
352 case EBUSY:
353 case EINTR:
354 case ENOLCK:
355 /* random NFS retry error, unless during file system support
356 * introspection, in which it actually means what it says */
357 return SQLITE_BUSY;
358
359 case EACCES:
360 /* EACCES is like EAGAIN during locking operations, but not any other time*/
361 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
362 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
363 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
364 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
365 return SQLITE_BUSY;
366 }
367 /* else fall through */
368 case EPERM:
369 return SQLITE_PERM;
370
371 case EDEADLK:
372 return SQLITE_IOERR_BLOCKED;
373
374#if EOPNOTSUPP!=ENOTSUP
375 case EOPNOTSUPP:
376 /* something went terribly awry, unless during file system support
377 * introspection, in which it actually means what it says */
378#endif
379#ifdef ENOTSUP
380 case ENOTSUP:
381 /* invalid fd, unless during file system support introspection, in which
382 * it actually means what it says */
383#endif
384 case EIO:
385 case EBADF:
386 case EINVAL:
387 case ENOTCONN:
388 case ENODEV:
389 case ENXIO:
390 case ENOENT:
391 case ESTALE:
392 case ENOSYS:
393 /* these should force the client to close the file and reconnect */
394
395 default:
396 return sqliteIOErr;
397 }
398}
399
400
401
402/******************************************************************************
403****************** Begin Unique File ID Utility Used By VxWorks ***************
404**
405** On most versions of unix, we can get a unique ID for a file by concatenating
406** the device number and the inode number. But this does not work on VxWorks.
407** On VxWorks, a unique file id must be based on the canonical filename.
408**
409** A pointer to an instance of the following structure can be used as a
410** unique file ID in VxWorks. Each instance of this structure contains
411** a copy of the canonical filename. There is also a reference count.
412** The structure is reclaimed when the number of pointers to it drops to
413** zero.
414**
415** There are never very many files open at one time and lookups are not
416** a performance-critical path, so it is sufficient to put these
417** structures on a linked list.
418*/
419struct vxworksFileId {
420 struct vxworksFileId *pNext; /* Next in a list of them all */
421 int nRef; /* Number of references to this one */
422 int nName; /* Length of the zCanonicalName[] string */
423 char *zCanonicalName; /* Canonical filename */
424};
425
426#if OS_VXWORKS
427/*
drh9b35ea62008-11-29 02:20:26 +0000428** All unique filenames are held on a linked list headed by this
drh734c9862008-11-28 15:37:20 +0000429** variable:
430*/
431static struct vxworksFileId *vxworksFileList = 0;
432
433/*
434** Simplify a filename into its canonical form
435** by making the following changes:
436**
437** * removing any trailing and duplicate /
drh9b35ea62008-11-29 02:20:26 +0000438** * convert /./ into just /
439** * convert /A/../ where A is any simple name into just /
drh734c9862008-11-28 15:37:20 +0000440**
441** Changes are made in-place. Return the new name length.
442**
443** The original filename is in z[0..n-1]. Return the number of
444** characters in the simplified name.
445*/
446static int vxworksSimplifyName(char *z, int n){
447 int i, j;
448 while( n>1 && z[n-1]=='/' ){ n--; }
449 for(i=j=0; i<n; i++){
450 if( z[i]=='/' ){
451 if( z[i+1]=='/' ) continue;
452 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
453 i += 1;
454 continue;
455 }
456 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
457 while( j>0 && z[j-1]!='/' ){ j--; }
458 if( j>0 ){ j--; }
459 i += 2;
460 continue;
461 }
462 }
463 z[j++] = z[i];
464 }
465 z[j] = 0;
466 return j;
467}
468
469/*
470** Find a unique file ID for the given absolute pathname. Return
471** a pointer to the vxworksFileId object. This pointer is the unique
472** file ID.
473**
474** The nRef field of the vxworksFileId object is incremented before
475** the object is returned. A new vxworksFileId object is created
476** and added to the global list if necessary.
477**
478** If a memory allocation error occurs, return NULL.
479*/
480static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
481 struct vxworksFileId *pNew; /* search key and new file ID */
482 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
483 int n; /* Length of zAbsoluteName string */
484
485 assert( zAbsoluteName[0]=='/' );
486 n = strlen(zAbsoluteName);
487 pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
488 if( pNew==0 ) return 0;
489 pNew->zCanonicalName = (char*)&pNew[1];
490 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
491 n = vxworksSimplifyName(pNew->zCanonicalName, n);
492
493 /* Search for an existing entry that matching the canonical name.
494 ** If found, increment the reference count and return a pointer to
495 ** the existing file ID.
496 */
497 unixEnterMutex();
498 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
499 if( pCandidate->nName==n
500 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
501 ){
502 sqlite3_free(pNew);
503 pCandidate->nRef++;
504 unixLeaveMutex();
505 return pCandidate;
506 }
507 }
508
509 /* No match was found. We will make a new file ID */
510 pNew->nRef = 1;
511 pNew->nName = n;
512 pNew->pNext = vxworksFileList;
513 vxworksFileList = pNew;
514 unixLeaveMutex();
515 return pNew;
516}
517
518/*
519** Decrement the reference count on a vxworksFileId object. Free
520** the object when the reference count reaches zero.
521*/
522static void vxworksReleaseFileId(struct vxworksFileId *pId){
523 unixEnterMutex();
524 assert( pId->nRef>0 );
525 pId->nRef--;
526 if( pId->nRef==0 ){
527 struct vxworksFileId **pp;
528 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
529 assert( *pp==pId );
530 *pp = pId->pNext;
531 sqlite3_free(pId);
532 }
533 unixLeaveMutex();
534}
535#endif /* OS_VXWORKS */
536/*************** End of Unique File ID Utility Used By VxWorks ****************
537******************************************************************************/
538
539
540/******************************************************************************
541*************************** Posix Advisory Locking ****************************
542**
drh9b35ea62008-11-29 02:20:26 +0000543** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
drhbbd42a62004-05-22 17:41:58 +0000544** section 6.5.2.2 lines 483 through 490 specify that when a process
545** sets or clears a lock, that operation overrides any prior locks set
546** by the same process. It does not explicitly say so, but this implies
547** that it overrides locks set by the same process using a different
548** file descriptor. Consider this test case:
drh6c7d5c52008-11-21 20:32:33 +0000549**
550** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
drhbbd42a62004-05-22 17:41:58 +0000551** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
552**
553** Suppose ./file1 and ./file2 are really the same file (because
554** one is a hard or symbolic link to the other) then if you set
555** an exclusive lock on fd1, then try to get an exclusive lock
556** on fd2, it works. I would have expected the second lock to
557** fail since there was already a lock on the file due to fd1.
558** But not so. Since both locks came from the same process, the
559** second overrides the first, even though they were on different
560** file descriptors opened on different file names.
561**
drh734c9862008-11-28 15:37:20 +0000562** This means that we cannot use POSIX locks to synchronize file access
563** among competing threads of the same process. POSIX locks will work fine
drhbbd42a62004-05-22 17:41:58 +0000564** to synchronize access for threads in separate processes, but not
565** threads within the same process.
566**
567** To work around the problem, SQLite has to manage file locks internally
568** on its own. Whenever a new database is opened, we have to find the
569** specific inode of the database file (the inode is determined by the
570** st_dev and st_ino fields of the stat structure that fstat() fills in)
571** and check for locks already existing on that inode. When locks are
572** created or removed, we have to look at our own internal record of the
573** locks to see if another thread has previously set a lock on that same
574** inode.
575**
drh9b35ea62008-11-29 02:20:26 +0000576** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
577** For VxWorks, we have to use the alternative unique ID system based on
578** canonical filename and implemented in the previous division.)
579**
danielk1977ad94b582007-08-20 06:44:22 +0000580** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000581** descriptor. It is now a structure that holds the integer file
582** descriptor and a pointer to a structure that describes the internal
583** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000584** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000585** point to the same locking structure. The locking structure keeps
586** a reference count (so we will know when to delete it) and a "cnt"
587** field that tells us its internal lock status. cnt==0 means the
588** file is unlocked. cnt==-1 means the file has an exclusive lock.
589** cnt>0 means there are cnt shared locks on the file.
590**
591** Any attempt to lock or unlock a file first checks the locking
592** structure. The fcntl() system call is only invoked to set a
593** POSIX lock if the internal lock structure transitions between
594** a locked and an unlocked state.
595**
drh734c9862008-11-28 15:37:20 +0000596** But wait: there are yet more problems with POSIX advisory locks.
drhbbd42a62004-05-22 17:41:58 +0000597**
598** If you close a file descriptor that points to a file that has locks,
599** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000600** released. To work around this problem, each unixFile structure contains
drh6c7d5c52008-11-21 20:32:33 +0000601** a pointer to an unixOpenCnt structure. There is one unixOpenCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000602** per open inode, which means that multiple unixFile can point to a single
drh6c7d5c52008-11-21 20:32:33 +0000603** unixOpenCnt. When an attempt is made to close an unixFile, if there are
danielk1977ad94b582007-08-20 06:44:22 +0000604** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000605** to close() the file descriptor is deferred until all of the locks clear.
drh6c7d5c52008-11-21 20:32:33 +0000606** The unixOpenCnt structure keeps a list of file descriptors that need to
drhbbd42a62004-05-22 17:41:58 +0000607** be closed and that list is walked (and cleared) when the last lock
608** clears.
609**
drh9b35ea62008-11-29 02:20:26 +0000610** Yet another problem: LinuxThreads do not play well with posix locks.
drh5fdae772004-06-29 03:29:00 +0000611**
drh9b35ea62008-11-29 02:20:26 +0000612** Many older versions of linux use the LinuxThreads library which is
613** not posix compliant. Under LinuxThreads, a lock created by thread
drh734c9862008-11-28 15:37:20 +0000614** A cannot be modified or overridden by a different thread B.
615** Only thread A can modify the lock. Locking behavior is correct
616** if the appliation uses the newer Native Posix Thread Library (NPTL)
617** on linux - with NPTL a lock created by thread A can override locks
618** in thread B. But there is no way to know at compile-time which
619** threading library is being used. So there is no way to know at
620** compile-time whether or not thread A can override locks on thread B.
621** We have to do a run-time check to discover the behavior of the
622** current process.
drh5fdae772004-06-29 03:29:00 +0000623**
drh734c9862008-11-28 15:37:20 +0000624** On systems where thread A is unable to modify locks created by
625** thread B, we have to keep track of which thread created each
drh9b35ea62008-11-29 02:20:26 +0000626** lock. Hence there is an extra field in the key to the unixLockInfo
drh734c9862008-11-28 15:37:20 +0000627** structure to record this information. And on those systems it
628** is illegal to begin a transaction in one thread and finish it
629** in another. For this latter restriction, there is no work-around.
630** It is a limitation of LinuxThreads.
drhbbd42a62004-05-22 17:41:58 +0000631*/
632
633/*
drh6c7d5c52008-11-21 20:32:33 +0000634** Set or check the unixFile.tid field. This field is set when an unixFile
635** is first opened. All subsequent uses of the unixFile verify that the
636** same thread is operating on the unixFile. Some operating systems do
637** not allow locks to be overridden by other threads and that restriction
638** means that sqlite3* database handles cannot be moved from one thread
drh734c9862008-11-28 15:37:20 +0000639** to another while locks are held.
drh6c7d5c52008-11-21 20:32:33 +0000640**
641** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
642** another as long as we are running on a system that supports threads
drh734c9862008-11-28 15:37:20 +0000643** overriding each others locks (which is now the most common behavior)
drh6c7d5c52008-11-21 20:32:33 +0000644** or if no locks are held. But the unixFile.pLock field needs to be
645** recomputed because its key includes the thread-id. See the
646** transferOwnership() function below for additional information
647*/
drh734c9862008-11-28 15:37:20 +0000648#if SQLITE_THREADSAFE && defined(__linux__)
drh6c7d5c52008-11-21 20:32:33 +0000649# define SET_THREADID(X) (X)->tid = pthread_self()
650# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
651 !pthread_equal((X)->tid, pthread_self()))
652#else
653# define SET_THREADID(X)
654# define CHECK_THREADID(X) 0
655#endif
656
657/*
drhbbd42a62004-05-22 17:41:58 +0000658** An instance of the following structure serves as the key used
drh6c7d5c52008-11-21 20:32:33 +0000659** to locate a particular unixOpenCnt structure given its inode. This
660** is the same as the unixLockKey except that the thread ID is omitted.
661*/
662struct unixFileId {
drh107886a2008-11-21 22:21:50 +0000663 dev_t dev; /* Device number */
drh6c7d5c52008-11-21 20:32:33 +0000664#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +0000665 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
drh6c7d5c52008-11-21 20:32:33 +0000666#else
drh107886a2008-11-21 22:21:50 +0000667 ino_t ino; /* Inode number */
drh6c7d5c52008-11-21 20:32:33 +0000668#endif
669};
670
671/*
672** An instance of the following structure serves as the key used
673** to locate a particular unixLockInfo structure given its inode.
drh5fdae772004-06-29 03:29:00 +0000674**
drh734c9862008-11-28 15:37:20 +0000675** If threads cannot override each others locks (LinuxThreads), then we
676** set the unixLockKey.tid field to the thread ID. If threads can override
677** each others locks (Posix and NPTL) then tid is always set to zero.
678** tid is omitted if we compile without threading support or on an OS
679** other than linux.
drhbbd42a62004-05-22 17:41:58 +0000680*/
drh6c7d5c52008-11-21 20:32:33 +0000681struct unixLockKey {
682 struct unixFileId fid; /* Unique identifier for the file */
drh734c9862008-11-28 15:37:20 +0000683#if SQLITE_THREADSAFE && defined(__linux__)
684 pthread_t tid; /* Thread ID of lock owner. Zero if not using LinuxThreads */
drh5fdae772004-06-29 03:29:00 +0000685#endif
drhbbd42a62004-05-22 17:41:58 +0000686};
687
688/*
689** An instance of the following structure is allocated for each open
drh9b35ea62008-11-29 02:20:26 +0000690** inode. Or, on LinuxThreads, there is one of these structures for
691** each inode opened by each thread.
drhbbd42a62004-05-22 17:41:58 +0000692**
danielk1977ad94b582007-08-20 06:44:22 +0000693** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000694** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000695** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000696*/
drh6c7d5c52008-11-21 20:32:33 +0000697struct unixLockInfo {
drh734c9862008-11-28 15:37:20 +0000698 struct unixLockKey lockKey; /* The lookup key */
699 int cnt; /* Number of SHARED locks held */
700 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
701 int nRef; /* Number of pointers to this structure */
702 struct unixLockInfo *pNext; /* List of all unixLockInfo objects */
703 struct unixLockInfo *pPrev; /* .... doubly linked */
drhbbd42a62004-05-22 17:41:58 +0000704};
705
706/*
707** An instance of the following structure is allocated for each open
708** inode. This structure keeps track of the number of locks on that
709** inode. If a close is attempted against an inode that is holding
710** locks, the close is deferred until all locks clear by adding the
711** file descriptor to be closed to the pending list.
drh9b35ea62008-11-29 02:20:26 +0000712**
713** TODO: Consider changing this so that there is only a single file
714** descriptor for each open file, even when it is opened multiple times.
715** The close() system call would only occur when the last database
716** using the file closes.
drhbbd42a62004-05-22 17:41:58 +0000717*/
drh6c7d5c52008-11-21 20:32:33 +0000718struct unixOpenCnt {
719 struct unixFileId fileId; /* The lookup key */
720 int nRef; /* Number of pointers to this structure */
721 int nLock; /* Number of outstanding locks */
722 int nPending; /* Number of pending close() operations */
723 int *aPending; /* Malloced space holding fd's awaiting a close() */
724#if OS_VXWORKS
725 sem_t *pSem; /* Named POSIX semaphore */
chw97185482008-11-17 08:05:31 +0000726 char aSemName[MAX_PATHNAME+1]; /* Name of that semaphore */
727#endif
drh6c7d5c52008-11-21 20:32:33 +0000728 struct unixOpenCnt *pNext, *pPrev; /* List of all unixOpenCnt objects */
drhbbd42a62004-05-22 17:41:58 +0000729};
730
drhda0e7682008-07-30 15:27:54 +0000731/*
drh9b35ea62008-11-29 02:20:26 +0000732** Lists of all unixLockInfo and unixOpenCnt objects. These used to be hash
733** tables. But the number of objects is rarely more than a dozen and
drhda0e7682008-07-30 15:27:54 +0000734** never exceeds a few thousand. And lookup is not on a critical
drh6c7d5c52008-11-21 20:32:33 +0000735** path so a simple linked list will suffice.
drhbbd42a62004-05-22 17:41:58 +0000736*/
drh6c7d5c52008-11-21 20:32:33 +0000737static struct unixLockInfo *lockList = 0;
738static struct unixOpenCnt *openList = 0;
drh5fdae772004-06-29 03:29:00 +0000739
drh5fdae772004-06-29 03:29:00 +0000740/*
drh9b35ea62008-11-29 02:20:26 +0000741** This variable remembers whether or not threads can override each others
drh5fdae772004-06-29 03:29:00 +0000742** locks.
743**
drh9b35ea62008-11-29 02:20:26 +0000744** 0: No. Threads cannot override each others locks. (LinuxThreads)
745** 1: Yes. Threads can override each others locks. (Posix & NLPT)
drh5fdae772004-06-29 03:29:00 +0000746** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000747**
drh5062d3a2006-01-31 23:03:35 +0000748** On some systems, we know at compile-time if threads can override each
749** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
750** will be set appropriately. On other systems, we have to check at
751** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
752** undefined.
753**
drhf1a221e2006-01-15 17:27:17 +0000754** This variable normally has file scope only. But during testing, we make
755** it a global so that the test code can change its value in order to verify
756** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000757*/
drh715ff302008-12-03 22:32:44 +0000758#if SQLITE_THREADSAFE && defined(__linux__)
759# ifndef SQLITE_THREAD_OVERRIDE_LOCK
760# define SQLITE_THREAD_OVERRIDE_LOCK -1
761# endif
762# ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000763int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh715ff302008-12-03 22:32:44 +0000764# else
drh5062d3a2006-01-31 23:03:35 +0000765static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh715ff302008-12-03 22:32:44 +0000766# endif
drh029b44b2006-01-15 00:13:15 +0000767#endif
drh5fdae772004-06-29 03:29:00 +0000768
769/*
770** This structure holds information passed into individual test
771** threads by the testThreadLockingBehavior() routine.
772*/
773struct threadTestData {
774 int fd; /* File to be locked */
775 struct flock lock; /* The locking operation */
776 int result; /* Result of the locking operation */
777};
778
drh6c7d5c52008-11-21 20:32:33 +0000779#if SQLITE_THREADSAFE && defined(__linux__)
drh5fdae772004-06-29 03:29:00 +0000780/*
danielk197741a6a612008-11-11 18:34:35 +0000781** This function is used as the main routine for a thread launched by
782** testThreadLockingBehavior(). It tests whether the shared-lock obtained
783** by the main thread in testThreadLockingBehavior() conflicts with a
784** hypothetical write-lock obtained by this thread on the same file.
785**
786** The write-lock is not actually acquired, as this is not possible if
787** the file is open in read-only mode (see ticket #3472).
788*/
drh5fdae772004-06-29 03:29:00 +0000789static void *threadLockingTest(void *pArg){
790 struct threadTestData *pData = (struct threadTestData*)pArg;
danielk197741a6a612008-11-11 18:34:35 +0000791 pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
drh5fdae772004-06-29 03:29:00 +0000792 return pArg;
793}
drh6c7d5c52008-11-21 20:32:33 +0000794#endif /* SQLITE_THREADSAFE && defined(__linux__) */
drh5fdae772004-06-29 03:29:00 +0000795
drh6c7d5c52008-11-21 20:32:33 +0000796
797#if SQLITE_THREADSAFE && defined(__linux__)
drh5fdae772004-06-29 03:29:00 +0000798/*
799** This procedure attempts to determine whether or not threads
800** can override each others locks then sets the
801** threadsOverrideEachOthersLocks variable appropriately.
802*/
danielk19774d5238f2006-01-27 06:32:00 +0000803static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000804 int fd;
danielk197741a6a612008-11-11 18:34:35 +0000805 int rc;
806 struct threadTestData d;
807 struct flock l;
808 pthread_t t;
drh5fdae772004-06-29 03:29:00 +0000809
810 fd = dup(fd_orig);
811 if( fd<0 ) return;
danielk197741a6a612008-11-11 18:34:35 +0000812 memset(&l, 0, sizeof(l));
813 l.l_type = F_RDLCK;
814 l.l_len = 1;
815 l.l_start = 0;
816 l.l_whence = SEEK_SET;
817 rc = fcntl(fd_orig, F_SETLK, &l);
818 if( rc!=0 ) return;
819 memset(&d, 0, sizeof(d));
820 d.fd = fd;
821 d.lock = l;
822 d.lock.l_type = F_WRLCK;
823 pthread_create(&t, 0, threadLockingTest, &d);
824 pthread_join(t, 0);
drh5fdae772004-06-29 03:29:00 +0000825 close(fd);
danielk197741a6a612008-11-11 18:34:35 +0000826 if( d.result!=0 ) return;
827 threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
drh5fdae772004-06-29 03:29:00 +0000828}
drh6c7d5c52008-11-21 20:32:33 +0000829#endif /* SQLITE_THERADSAFE && defined(__linux__) */
drh5fdae772004-06-29 03:29:00 +0000830
drhbbd42a62004-05-22 17:41:58 +0000831/*
drh6c7d5c52008-11-21 20:32:33 +0000832** Release a unixLockInfo structure previously allocated by findLockInfo().
833*/
834static void releaseLockInfo(struct unixLockInfo *pLock){
danielk1977e339d652008-06-28 11:23:00 +0000835 if( pLock ){
836 pLock->nRef--;
837 if( pLock->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000838 if( pLock->pPrev ){
839 assert( pLock->pPrev->pNext==pLock );
840 pLock->pPrev->pNext = pLock->pNext;
841 }else{
842 assert( lockList==pLock );
843 lockList = pLock->pNext;
844 }
845 if( pLock->pNext ){
846 assert( pLock->pNext->pPrev==pLock );
847 pLock->pNext->pPrev = pLock->pPrev;
848 }
danielk1977e339d652008-06-28 11:23:00 +0000849 sqlite3_free(pLock);
850 }
drhbbd42a62004-05-22 17:41:58 +0000851 }
852}
853
854/*
drh6c7d5c52008-11-21 20:32:33 +0000855** Release a unixOpenCnt structure previously allocated by findLockInfo().
drhbbd42a62004-05-22 17:41:58 +0000856*/
drh6c7d5c52008-11-21 20:32:33 +0000857static void releaseOpenCnt(struct unixOpenCnt *pOpen){
danielk1977e339d652008-06-28 11:23:00 +0000858 if( pOpen ){
859 pOpen->nRef--;
860 if( pOpen->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000861 if( pOpen->pPrev ){
862 assert( pOpen->pPrev->pNext==pOpen );
863 pOpen->pPrev->pNext = pOpen->pNext;
864 }else{
865 assert( openList==pOpen );
866 openList = pOpen->pNext;
867 }
868 if( pOpen->pNext ){
869 assert( pOpen->pNext->pPrev==pOpen );
870 pOpen->pNext->pPrev = pOpen->pPrev;
871 }
872 sqlite3_free(pOpen->aPending);
danielk1977e339d652008-06-28 11:23:00 +0000873 sqlite3_free(pOpen);
874 }
drhbbd42a62004-05-22 17:41:58 +0000875 }
876}
877
drh6c7d5c52008-11-21 20:32:33 +0000878/*
879** Given a file descriptor, locate unixLockInfo and unixOpenCnt structures that
880** describes that file descriptor. Create new ones if necessary. The
881** return values might be uninitialized if an error occurs.
882**
883** Return an appropriate error code.
884*/
885static int findLockInfo(
886 unixFile *pFile, /* Unix file with file desc used in the key */
887 struct unixLockInfo **ppLock, /* Return the unixLockInfo structure here */
888 struct unixOpenCnt **ppOpen /* Return the unixOpenCnt structure here */
889){
890 int rc; /* System call return code */
891 int fd; /* The file descriptor for pFile */
892 struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */
893 struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */
894 struct stat statbuf; /* Low-level file information */
895 struct unixLockInfo *pLock; /* Candidate unixLockInfo object */
896 struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */
897
898 /* Get low-level information about the file that we can used to
899 ** create a unique name for the file.
900 */
901 fd = pFile->h;
902 rc = fstat(fd, &statbuf);
903 if( rc!=0 ){
904 pFile->lastErrno = errno;
905#ifdef EOVERFLOW
906 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
907#endif
908 return SQLITE_IOERR;
909 }
910
911 /* On OS X on an msdos filesystem, the inode number is reported
912 ** incorrectly for zero-size files. See ticket #3260. To work
913 ** around this problem (we consider it a bug in OS X, not SQLite)
914 ** we always increase the file size to 1 by writing a single byte
915 ** prior to accessing the inode number. The one byte written is
916 ** an ASCII 'S' character which also happens to be the first byte
917 ** in the header of every SQLite database. In this way, if there
918 ** is a race condition such that another thread has already populated
919 ** the first page of the database, no damage is done.
920 */
921 if( statbuf.st_size==0 ){
922 write(fd, "S", 1);
923 rc = fstat(fd, &statbuf);
924 if( rc!=0 ){
925 pFile->lastErrno = errno;
926 return SQLITE_IOERR;
927 }
928 }
929
930 memset(&lockKey, 0, sizeof(lockKey));
931 lockKey.fid.dev = statbuf.st_dev;
932#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +0000933 lockKey.fid.pId = pFile->pId;
drh6c7d5c52008-11-21 20:32:33 +0000934#else
935 lockKey.fid.ino = statbuf.st_ino;
936#endif
drh734c9862008-11-28 15:37:20 +0000937#if SQLITE_THREADSAFE && defined(__linux__)
drh6c7d5c52008-11-21 20:32:33 +0000938 if( threadsOverrideEachOthersLocks<0 ){
939 testThreadLockingBehavior(fd);
940 }
941 lockKey.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
942#endif
943 fileId = lockKey.fid;
944 if( ppLock!=0 ){
945 pLock = lockList;
946 while( pLock && memcmp(&lockKey, &pLock->lockKey, sizeof(lockKey)) ){
947 pLock = pLock->pNext;
948 }
949 if( pLock==0 ){
950 pLock = sqlite3_malloc( sizeof(*pLock) );
951 if( pLock==0 ){
952 rc = SQLITE_NOMEM;
953 goto exit_findlockinfo;
954 }
955 pLock->lockKey = lockKey;
956 pLock->nRef = 1;
957 pLock->cnt = 0;
958 pLock->locktype = 0;
959 pLock->pNext = lockList;
960 pLock->pPrev = 0;
961 if( lockList ) lockList->pPrev = pLock;
962 lockList = pLock;
963 }else{
964 pLock->nRef++;
965 }
966 *ppLock = pLock;
967 }
968 if( ppOpen!=0 ){
969 pOpen = openList;
970 while( pOpen && memcmp(&fileId, &pOpen->fileId, sizeof(fileId)) ){
971 pOpen = pOpen->pNext;
972 }
973 if( pOpen==0 ){
974 pOpen = sqlite3_malloc( sizeof(*pOpen) );
975 if( pOpen==0 ){
976 releaseLockInfo(pLock);
977 rc = SQLITE_NOMEM;
978 goto exit_findlockinfo;
979 }
980 pOpen->fileId = fileId;
981 pOpen->nRef = 1;
982 pOpen->nLock = 0;
983 pOpen->nPending = 0;
984 pOpen->aPending = 0;
985 pOpen->pNext = openList;
986 pOpen->pPrev = 0;
987 if( openList ) openList->pPrev = pOpen;
988 openList = pOpen;
989#if OS_VXWORKS
990 pOpen->pSem = NULL;
991 pOpen->aSemName[0] = '\0';
992#endif
993 }else{
994 pOpen->nRef++;
995 }
996 *ppOpen = pOpen;
997 }
998
999exit_findlockinfo:
1000 return rc;
1001}
drh6c7d5c52008-11-21 20:32:33 +00001002
drh7708e972008-11-29 00:56:52 +00001003/*
1004** If we are currently in a different thread than the thread that the
1005** unixFile argument belongs to, then transfer ownership of the unixFile
1006** over to the current thread.
1007**
1008** A unixFile is only owned by a thread on systems that use LinuxThreads.
1009**
1010** Ownership transfer is only allowed if the unixFile is currently unlocked.
1011** If the unixFile is locked and an ownership is wrong, then return
1012** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
1013*/
1014#if SQLITE_THREADSAFE && defined(__linux__)
1015static int transferOwnership(unixFile *pFile){
1016 int rc;
1017 pthread_t hSelf;
1018 if( threadsOverrideEachOthersLocks ){
1019 /* Ownership transfers not needed on this system */
1020 return SQLITE_OK;
1021 }
1022 hSelf = pthread_self();
1023 if( pthread_equal(pFile->tid, hSelf) ){
1024 /* We are still in the same thread */
1025 OSTRACE1("No-transfer, same thread\n");
1026 return SQLITE_OK;
1027 }
1028 if( pFile->locktype!=NO_LOCK ){
1029 /* We cannot change ownership while we are holding a lock! */
1030 return SQLITE_MISUSE;
1031 }
1032 OSTRACE4("Transfer ownership of %d from %d to %d\n",
1033 pFile->h, pFile->tid, hSelf);
1034 pFile->tid = hSelf;
1035 if (pFile->pLock != NULL) {
1036 releaseLockInfo(pFile->pLock);
1037 rc = findLockInfo(pFile, &pFile->pLock, 0);
1038 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
1039 locktypeName(pFile->locktype),
1040 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
1041 return rc;
1042 } else {
1043 return SQLITE_OK;
1044 }
1045}
1046#else /* if not SQLITE_THREADSAFE */
1047 /* On single-threaded builds, ownership transfer is a no-op */
1048# define transferOwnership(X) SQLITE_OK
1049#endif /* SQLITE_THREADSAFE */
1050
aswift5b1a2562008-08-22 00:22:35 +00001051
1052/*
danielk197713adf8a2004-06-03 16:08:41 +00001053** This routine checks if there is a RESERVED lock held on the specified
aswift5b1a2562008-08-22 00:22:35 +00001054** file by this or any other process. If such a lock is held, set *pResOut
1055** to a non-zero value otherwise *pResOut is set to zero. The return value
1056** is set to SQLITE_OK unless an I/O error occurs during lock checking.
danielk197713adf8a2004-06-03 16:08:41 +00001057*/
danielk1977861f7452008-06-05 11:39:11 +00001058static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001059 int rc = SQLITE_OK;
1060 int reserved = 0;
drh054889e2005-11-30 03:20:31 +00001061 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001062
danielk1977861f7452008-06-05 11:39:11 +00001063 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1064
drh054889e2005-11-30 03:20:31 +00001065 assert( pFile );
drh6c7d5c52008-11-21 20:32:33 +00001066 unixEnterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001067
1068 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001069 if( pFile->pLock->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001070 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001071 }
1072
drh2ac3ee92004-06-07 16:27:46 +00001073 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001074 */
aswift5b1a2562008-08-22 00:22:35 +00001075 if( !reserved ){
danielk197713adf8a2004-06-03 16:08:41 +00001076 struct flock lock;
1077 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001078 lock.l_start = RESERVED_BYTE;
1079 lock.l_len = 1;
1080 lock.l_type = F_WRLCK;
aswift5b1a2562008-08-22 00:22:35 +00001081 if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
1082 int tErrno = errno;
1083 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1084 pFile->lastErrno = tErrno;
1085 } else if( lock.l_type!=F_UNLCK ){
1086 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001087 }
1088 }
1089
drh6c7d5c52008-11-21 20:32:33 +00001090 unixLeaveMutex();
aswift5b1a2562008-08-22 00:22:35 +00001091 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk197713adf8a2004-06-03 16:08:41 +00001092
aswift5b1a2562008-08-22 00:22:35 +00001093 *pResOut = reserved;
1094 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001095}
1096
1097/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001098** Lock the file with the lock specified by parameter locktype - one
1099** of the following:
1100**
drh2ac3ee92004-06-07 16:27:46 +00001101** (1) SHARED_LOCK
1102** (2) RESERVED_LOCK
1103** (3) PENDING_LOCK
1104** (4) EXCLUSIVE_LOCK
1105**
drhb3e04342004-06-08 00:47:47 +00001106** Sometimes when requesting one lock state, additional lock states
1107** are inserted in between. The locking might fail on one of the later
1108** transitions leaving the lock state different from what it started but
1109** still short of its goal. The following chart shows the allowed
1110** transitions and the inserted intermediate states:
1111**
1112** UNLOCKED -> SHARED
1113** SHARED -> RESERVED
1114** SHARED -> (PENDING) -> EXCLUSIVE
1115** RESERVED -> (PENDING) -> EXCLUSIVE
1116** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001117**
drha6abd042004-06-09 17:37:22 +00001118** This routine will only increase a lock. Use the sqlite3OsUnlock()
1119** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001120*/
danielk197762079062007-08-15 17:08:46 +00001121static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001122 /* The following describes the implementation of the various locks and
1123 ** lock transitions in terms of the POSIX advisory shared and exclusive
1124 ** lock primitives (called read-locks and write-locks below, to avoid
1125 ** confusion with SQLite lock names). The algorithms are complicated
1126 ** slightly in order to be compatible with windows systems simultaneously
1127 ** accessing the same database file, in case that is ever required.
1128 **
1129 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1130 ** byte', each single bytes at well known offsets, and the 'shared byte
1131 ** range', a range of 510 bytes at a well known offset.
1132 **
1133 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1134 ** byte'. If this is successful, a random byte from the 'shared byte
1135 ** range' is read-locked and the lock on the 'pending byte' released.
1136 **
danielk197790ba3bd2004-06-25 08:32:25 +00001137 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1138 ** A RESERVED lock is implemented by grabbing a write-lock on the
1139 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001140 **
1141 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001142 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1143 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1144 ** obtained, but existing SHARED locks are allowed to persist. A process
1145 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1146 ** This property is used by the algorithm for rolling back a journal file
1147 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001148 **
danielk197790ba3bd2004-06-25 08:32:25 +00001149 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1150 ** implemented by obtaining a write-lock on the entire 'shared byte
1151 ** range'. Since all other locks require a read-lock on one of the bytes
1152 ** within this range, this ensures that no other locks are held on the
1153 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001154 **
1155 ** The reason a single byte cannot be used instead of the 'shared byte
1156 ** range' is that some versions of windows do not support read-locks. By
1157 ** locking a random byte from a range, concurrent SHARED locks may exist
1158 ** even if the locking primitive used is always a write-lock.
1159 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001160 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001161 unixFile *pFile = (unixFile*)id;
drh6c7d5c52008-11-21 20:32:33 +00001162 struct unixLockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001163 struct flock lock;
1164 int s;
1165
drh054889e2005-11-30 03:20:31 +00001166 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001167 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001168 locktypeName(locktype), locktypeName(pFile->locktype),
1169 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001170
1171 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001172 ** unixFile, do nothing. Don't use the end_lock: exit path, as
drh6c7d5c52008-11-21 20:32:33 +00001173 ** unixEnterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001174 */
drh054889e2005-11-30 03:20:31 +00001175 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001176 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001177 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001178 return SQLITE_OK;
1179 }
1180
drhb3e04342004-06-08 00:47:47 +00001181 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001182 */
drh054889e2005-11-30 03:20:31 +00001183 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001184 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001185 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001186
drh054889e2005-11-30 03:20:31 +00001187 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001188 */
drh6c7d5c52008-11-21 20:32:33 +00001189 unixEnterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001190
drh029b44b2006-01-15 00:13:15 +00001191 /* Make sure the current thread owns the pFile.
1192 */
1193 rc = transferOwnership(pFile);
1194 if( rc!=SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00001195 unixLeaveMutex();
drh029b44b2006-01-15 00:13:15 +00001196 return rc;
1197 }
drh64b1bea2006-01-15 02:30:57 +00001198 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001199
danielk1977ad94b582007-08-20 06:44:22 +00001200 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001201 ** handle that precludes the requested lock, return BUSY.
1202 */
drh054889e2005-11-30 03:20:31 +00001203 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001204 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001205 ){
1206 rc = SQLITE_BUSY;
1207 goto end_lock;
1208 }
1209
1210 /* If a SHARED lock is requested, and some thread using this PID already
1211 ** has a SHARED or RESERVED lock, then increment reference counts and
1212 ** return SQLITE_OK.
1213 */
1214 if( locktype==SHARED_LOCK &&
1215 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1216 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001217 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001218 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001219 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001220 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001221 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001222 goto end_lock;
1223 }
1224
danielk197713adf8a2004-06-03 16:08:41 +00001225 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001226
danielk19779a1d0ab2004-06-01 14:09:28 +00001227 lock.l_whence = SEEK_SET;
1228
drh3cde3bb2004-06-12 02:17:14 +00001229 /* A PENDING lock is needed before acquiring a SHARED lock and before
1230 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1231 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001232 */
drh3cde3bb2004-06-12 02:17:14 +00001233 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001234 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001235 ){
danielk1977489468c2004-06-28 08:25:47 +00001236 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001237 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001238 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001239 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001240 int tErrno = errno;
1241 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1242 if( IS_LOCK_ERROR(rc) ){
1243 pFile->lastErrno = tErrno;
1244 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001245 goto end_lock;
1246 }
drh3cde3bb2004-06-12 02:17:14 +00001247 }
1248
1249
1250 /* If control gets to this point, then actually go ahead and make
1251 ** operating system calls for the specified lock.
1252 */
1253 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001254 int tErrno = 0;
drh3cde3bb2004-06-12 02:17:14 +00001255 assert( pLock->cnt==0 );
1256 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001257
drh2ac3ee92004-06-07 16:27:46 +00001258 /* Now get the read-lock */
1259 lock.l_start = SHARED_FIRST;
1260 lock.l_len = SHARED_SIZE;
aswift5b1a2562008-08-22 00:22:35 +00001261 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
1262 tErrno = errno;
1263 }
drh2ac3ee92004-06-07 16:27:46 +00001264 /* Drop the temporary PENDING lock */
1265 lock.l_start = PENDING_BYTE;
1266 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001267 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001268 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
aswift5b1a2562008-08-22 00:22:35 +00001269 if( s != -1 ){
1270 /* This could happen with a network mount */
1271 tErrno = errno;
1272 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1273 if( IS_LOCK_ERROR(rc) ){
1274 pFile->lastErrno = tErrno;
1275 }
1276 goto end_lock;
1277 }
drh2b4b5962005-06-15 17:47:55 +00001278 }
drhe2396a12007-03-29 20:19:58 +00001279 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001280 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1281 if( IS_LOCK_ERROR(rc) ){
1282 pFile->lastErrno = tErrno;
1283 }
drhbbd42a62004-05-22 17:41:58 +00001284 }else{
drh054889e2005-11-30 03:20:31 +00001285 pFile->locktype = SHARED_LOCK;
1286 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001287 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001288 }
drh3cde3bb2004-06-12 02:17:14 +00001289 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1290 /* We are trying for an exclusive lock but another thread in this
1291 ** same process is still holding a shared lock. */
1292 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001293 }else{
drh3cde3bb2004-06-12 02:17:14 +00001294 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001295 ** assumed that there is a SHARED or greater lock on the file
1296 ** already.
1297 */
drh054889e2005-11-30 03:20:31 +00001298 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001299 lock.l_type = F_WRLCK;
1300 switch( locktype ){
1301 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001302 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001303 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001304 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001305 lock.l_start = SHARED_FIRST;
1306 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001307 break;
1308 default:
1309 assert(0);
1310 }
drh054889e2005-11-30 03:20:31 +00001311 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001312 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001313 int tErrno = errno;
1314 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1315 if( IS_LOCK_ERROR(rc) ){
1316 pFile->lastErrno = tErrno;
1317 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001318 }
drhbbd42a62004-05-22 17:41:58 +00001319 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001320
danielk1977ecb2a962004-06-02 06:30:16 +00001321 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001322 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001323 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001324 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001325 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001326 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001327 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001328
1329end_lock:
drh6c7d5c52008-11-21 20:32:33 +00001330 unixLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001331 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001332 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001333 return rc;
1334}
1335
1336/*
drh054889e2005-11-30 03:20:31 +00001337** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001338** must be either NO_LOCK or SHARED_LOCK.
1339**
1340** If the locking level of the file descriptor is already at or below
1341** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001342*/
danielk197762079062007-08-15 17:08:46 +00001343static int unixUnlock(sqlite3_file *id, int locktype){
drh6c7d5c52008-11-21 20:32:33 +00001344 struct unixLockInfo *pLock;
drha6abd042004-06-09 17:37:22 +00001345 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001346 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001347 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001348 int h;
drha6abd042004-06-09 17:37:22 +00001349
drh054889e2005-11-30 03:20:31 +00001350 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001351 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001352 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001353
1354 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001355 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001356 return SQLITE_OK;
1357 }
drhf1a221e2006-01-15 17:27:17 +00001358 if( CHECK_THREADID(pFile) ){
1359 return SQLITE_MISUSE;
1360 }
drh6c7d5c52008-11-21 20:32:33 +00001361 unixEnterMutex();
drh1aa5af12008-03-07 19:51:14 +00001362 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001363 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001364 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001365 if( pFile->locktype>SHARED_LOCK ){
1366 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001367 SimulateIOErrorBenign(1);
1368 SimulateIOError( h=(-1) )
1369 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001370 if( locktype==SHARED_LOCK ){
1371 lock.l_type = F_RDLCK;
1372 lock.l_whence = SEEK_SET;
1373 lock.l_start = SHARED_FIRST;
1374 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001375 if( fcntl(h, F_SETLK, &lock)==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001376 int tErrno = errno;
1377 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1378 if( IS_LOCK_ERROR(rc) ){
1379 pFile->lastErrno = tErrno;
1380 }
1381 goto end_unlock;
drh9c105bb2004-10-02 20:38:28 +00001382 }
1383 }
drhbbd42a62004-05-22 17:41:58 +00001384 lock.l_type = F_UNLCK;
1385 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001386 lock.l_start = PENDING_BYTE;
1387 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001388 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001389 pLock->locktype = SHARED_LOCK;
1390 }else{
aswift5b1a2562008-08-22 00:22:35 +00001391 int tErrno = errno;
1392 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1393 if( IS_LOCK_ERROR(rc) ){
1394 pFile->lastErrno = tErrno;
1395 }
1396 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001397 }
drhbbd42a62004-05-22 17:41:58 +00001398 }
drha6abd042004-06-09 17:37:22 +00001399 if( locktype==NO_LOCK ){
drh6c7d5c52008-11-21 20:32:33 +00001400 struct unixOpenCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001401
drha6abd042004-06-09 17:37:22 +00001402 /* Decrement the shared lock counter. Release the lock using an
1403 ** OS call only when all threads in this same process have released
1404 ** the lock.
1405 */
1406 pLock->cnt--;
1407 if( pLock->cnt==0 ){
1408 lock.l_type = F_UNLCK;
1409 lock.l_whence = SEEK_SET;
1410 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001411 SimulateIOErrorBenign(1);
1412 SimulateIOError( h=(-1) )
1413 SimulateIOErrorBenign(0);
1414 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001415 pLock->locktype = NO_LOCK;
1416 }else{
aswift5b1a2562008-08-22 00:22:35 +00001417 int tErrno = errno;
danielk19775ad6a882008-09-15 04:20:31 +00001418 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
aswift5b1a2562008-08-22 00:22:35 +00001419 if( IS_LOCK_ERROR(rc) ){
1420 pFile->lastErrno = tErrno;
1421 }
drh1aa5af12008-03-07 19:51:14 +00001422 pLock->cnt = 1;
aswift5b1a2562008-08-22 00:22:35 +00001423 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001424 }
drha6abd042004-06-09 17:37:22 +00001425 }
1426
drhbbd42a62004-05-22 17:41:58 +00001427 /* Decrement the count of locks against this same file. When the
1428 ** count reaches zero, close any other file descriptors whose close
1429 ** was deferred because of outstanding locks.
1430 */
drh1aa5af12008-03-07 19:51:14 +00001431 if( rc==SQLITE_OK ){
1432 pOpen = pFile->pOpen;
1433 pOpen->nLock--;
1434 assert( pOpen->nLock>=0 );
1435 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1436 int i;
1437 for(i=0; i<pOpen->nPending; i++){
aswiftaebf4132008-11-21 00:10:35 +00001438 /* close pending fds, but if closing fails don't free the array
1439 ** assign -1 to the successfully closed descriptors and record the
1440 ** error. The next attempt to unlock will try again. */
1441 if( pOpen->aPending[i] < 0 ) continue;
1442 if( close(pOpen->aPending[i]) ){
1443 pFile->lastErrno = errno;
1444 rc = SQLITE_IOERR_CLOSE;
1445 }else{
1446 pOpen->aPending[i] = -1;
1447 }
drh1aa5af12008-03-07 19:51:14 +00001448 }
aswiftaebf4132008-11-21 00:10:35 +00001449 if( rc==SQLITE_OK ){
1450 sqlite3_free(pOpen->aPending);
1451 pOpen->nPending = 0;
1452 pOpen->aPending = 0;
1453 }
drhbbd42a62004-05-22 17:41:58 +00001454 }
drhbbd42a62004-05-22 17:41:58 +00001455 }
1456 }
aswift5b1a2562008-08-22 00:22:35 +00001457
1458end_unlock:
drh6c7d5c52008-11-21 20:32:33 +00001459 unixLeaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001460 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001461 return rc;
drhbbd42a62004-05-22 17:41:58 +00001462}
1463
1464/*
danielk1977e339d652008-06-28 11:23:00 +00001465** This function performs the parts of the "close file" operation
1466** common to all locking schemes. It closes the directory and file
1467** handles, if they are valid, and sets all fields of the unixFile
1468** structure to 0.
drh9b35ea62008-11-29 02:20:26 +00001469**
1470** It is *not* necessary to hold the mutex when this routine is called,
1471** even on VxWorks. A mutex will be acquired on VxWorks by the
1472** vxworksReleaseFileId() routine.
danielk1977e339d652008-06-28 11:23:00 +00001473*/
1474static int closeUnixFile(sqlite3_file *id){
1475 unixFile *pFile = (unixFile*)id;
1476 if( pFile ){
1477 if( pFile->dirfd>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001478 int err = close(pFile->dirfd);
1479 if( err ){
1480 pFile->lastErrno = errno;
1481 return SQLITE_IOERR_DIR_CLOSE;
1482 }else{
1483 pFile->dirfd=-1;
1484 }
danielk1977e339d652008-06-28 11:23:00 +00001485 }
1486 if( pFile->h>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001487 int err = close(pFile->h);
1488 if( err ){
1489 pFile->lastErrno = errno;
1490 return SQLITE_IOERR_CLOSE;
1491 }
danielk1977e339d652008-06-28 11:23:00 +00001492 }
drh6c7d5c52008-11-21 20:32:33 +00001493#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +00001494 if( pFile->pId ){
1495 if( pFile->isDelete ){
drh9b35ea62008-11-29 02:20:26 +00001496 unlink(pFile->pId->zCanonicalName);
chw97185482008-11-17 08:05:31 +00001497 }
drh107886a2008-11-21 22:21:50 +00001498 vxworksReleaseFileId(pFile->pId);
1499 pFile->pId = 0;
chw97185482008-11-17 08:05:31 +00001500 }
1501#endif
danielk1977e339d652008-06-28 11:23:00 +00001502 OSTRACE2("CLOSE %-3d\n", pFile->h);
1503 OpenCounter(-1);
1504 memset(pFile, 0, sizeof(unixFile));
1505 }
1506 return SQLITE_OK;
1507}
1508
1509/*
danielk1977e3026632004-06-22 11:29:02 +00001510** Close a file.
1511*/
danielk197762079062007-08-15 17:08:46 +00001512static int unixClose(sqlite3_file *id){
aswiftaebf4132008-11-21 00:10:35 +00001513 int rc = SQLITE_OK;
danielk1977e339d652008-06-28 11:23:00 +00001514 if( id ){
1515 unixFile *pFile = (unixFile *)id;
1516 unixUnlock(id, NO_LOCK);
drh6c7d5c52008-11-21 20:32:33 +00001517 unixEnterMutex();
danielk19776cb427f2008-06-30 10:16:04 +00001518 if( pFile->pOpen && pFile->pOpen->nLock ){
danielk1977e339d652008-06-28 11:23:00 +00001519 /* If there are outstanding locks, do not actually close the file just
1520 ** yet because that would clear those locks. Instead, add the file
1521 ** descriptor to pOpen->aPending. It will be automatically closed when
1522 ** the last lock is cleared.
1523 */
1524 int *aNew;
drh6c7d5c52008-11-21 20:32:33 +00001525 struct unixOpenCnt *pOpen = pFile->pOpen;
drhda0e7682008-07-30 15:27:54 +00001526 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e339d652008-06-28 11:23:00 +00001527 if( aNew==0 ){
1528 /* If a malloc fails, just leak the file descriptor */
1529 }else{
1530 pOpen->aPending = aNew;
1531 pOpen->aPending[pOpen->nPending] = pFile->h;
1532 pOpen->nPending++;
1533 pFile->h = -1;
1534 }
danielk1977e3026632004-06-22 11:29:02 +00001535 }
danielk1977e339d652008-06-28 11:23:00 +00001536 releaseLockInfo(pFile->pLock);
1537 releaseOpenCnt(pFile->pOpen);
aswiftaebf4132008-11-21 00:10:35 +00001538 rc = closeUnixFile(id);
drh6c7d5c52008-11-21 20:32:33 +00001539 unixLeaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00001540 }
aswiftaebf4132008-11-21 00:10:35 +00001541 return rc;
danielk1977e3026632004-06-22 11:29:02 +00001542}
1543
drh734c9862008-11-28 15:37:20 +00001544/************** End of the posix advisory lock implementation *****************
1545******************************************************************************/
drhbfe66312006-10-03 17:40:40 +00001546
drh734c9862008-11-28 15:37:20 +00001547/******************************************************************************
1548****************************** No-op Locking **********************************
1549**
1550** Of the various locking implementations available, this is by far the
1551** simplest: locking is ignored. No attempt is made to lock the database
1552** file for reading or writing.
1553**
1554** This locking mode is appropriate for use on read-only databases
1555** (ex: databases that are burned into CD-ROM, for example.) It can
1556** also be used if the application employs some external mechanism to
1557** prevent simultaneous access of the same database by two or more
1558** database connections. But there is a serious risk of database
1559** corruption if this locking mode is used in situations where multiple
1560** database connections are accessing the same database file at the same
1561** time and one or more of those connections are writing.
1562*/
drhbfe66312006-10-03 17:40:40 +00001563
drh734c9862008-11-28 15:37:20 +00001564static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
1565 UNUSED_PARAMETER(NotUsed);
1566 *pResOut = 0;
1567 return SQLITE_OK;
1568}
drh734c9862008-11-28 15:37:20 +00001569static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
1570 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1571 return SQLITE_OK;
1572}
drh734c9862008-11-28 15:37:20 +00001573static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
1574 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1575 return SQLITE_OK;
1576}
1577
1578/*
drh9b35ea62008-11-29 02:20:26 +00001579** Close the file.
drh734c9862008-11-28 15:37:20 +00001580*/
1581static int nolockClose(sqlite3_file *id) {
drh9b35ea62008-11-29 02:20:26 +00001582 return closeUnixFile(id);
drh734c9862008-11-28 15:37:20 +00001583}
1584
1585/******************* End of the no-op lock implementation *********************
1586******************************************************************************/
1587
1588/******************************************************************************
1589************************* Begin dot-file Locking ******************************
1590**
1591** The dotfile locking implementation uses the existing of separate lock
1592** files in order to control access to the database. This works on just
1593** about every filesystem imaginable. But there are serious downsides:
1594**
1595** (1) There is zero concurrency. A single reader blocks all other
1596** connections from reading or writing the database.
1597**
1598** (2) An application crash or power loss can leave stale lock files
1599** sitting around that need to be cleared manually.
1600**
1601** Nevertheless, a dotlock is an appropriate locking mode for use if no
1602** other locking strategy is available.
drh7708e972008-11-29 00:56:52 +00001603**
1604** Dotfile locking works by creating a file in the same directory as the
1605** database and with the same name but with a ".lock" extension added.
1606** The existance of a lock file implies an EXCLUSIVE lock. All other lock
1607** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
drh734c9862008-11-28 15:37:20 +00001608*/
1609
1610/*
1611** The file suffix added to the data base filename in order to create the
1612** lock file.
1613*/
1614#define DOTLOCK_SUFFIX ".lock"
1615
drh7708e972008-11-29 00:56:52 +00001616/*
1617** This routine checks if there is a RESERVED lock held on the specified
1618** file by this or any other process. If such a lock is held, set *pResOut
1619** to a non-zero value otherwise *pResOut is set to zero. The return value
1620** is set to SQLITE_OK unless an I/O error occurs during lock checking.
1621**
1622** In dotfile locking, either a lock exists or it does not. So in this
1623** variation of CheckReservedLock(), *pResOut is set to true if any lock
1624** is held on the file and false if the file is unlocked.
1625*/
drh734c9862008-11-28 15:37:20 +00001626static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
1627 int rc = SQLITE_OK;
1628 int reserved = 0;
1629 unixFile *pFile = (unixFile*)id;
1630
1631 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1632
1633 assert( pFile );
1634
1635 /* Check if a thread in this process holds such a lock */
1636 if( pFile->locktype>SHARED_LOCK ){
drh7708e972008-11-29 00:56:52 +00001637 /* Either this connection or some other connection in the same process
1638 ** holds a lock on the file. No need to check further. */
drh734c9862008-11-28 15:37:20 +00001639 reserved = 1;
drh7708e972008-11-29 00:56:52 +00001640 }else{
1641 /* The lock is held if and only if the lockfile exists */
1642 const char *zLockFile = (const char*)pFile->lockingContext;
1643 reserved = access(zLockFile, 0)==0;
drh734c9862008-11-28 15:37:20 +00001644 }
1645 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
drh734c9862008-11-28 15:37:20 +00001646 *pResOut = reserved;
1647 return rc;
1648}
1649
drh7708e972008-11-29 00:56:52 +00001650/*
1651** Lock the file with the lock specified by parameter locktype - one
1652** of the following:
1653**
1654** (1) SHARED_LOCK
1655** (2) RESERVED_LOCK
1656** (3) PENDING_LOCK
1657** (4) EXCLUSIVE_LOCK
1658**
1659** Sometimes when requesting one lock state, additional lock states
1660** are inserted in between. The locking might fail on one of the later
1661** transitions leaving the lock state different from what it started but
1662** still short of its goal. The following chart shows the allowed
1663** transitions and the inserted intermediate states:
1664**
1665** UNLOCKED -> SHARED
1666** SHARED -> RESERVED
1667** SHARED -> (PENDING) -> EXCLUSIVE
1668** RESERVED -> (PENDING) -> EXCLUSIVE
1669** PENDING -> EXCLUSIVE
1670**
1671** This routine will only increase a lock. Use the sqlite3OsUnlock()
1672** routine to lower a locking level.
1673**
1674** With dotfile locking, we really only support state (4): EXCLUSIVE.
1675** But we track the other locking levels internally.
1676*/
drh734c9862008-11-28 15:37:20 +00001677static int dotlockLock(sqlite3_file *id, int locktype) {
1678 unixFile *pFile = (unixFile*)id;
1679 int fd;
1680 char *zLockFile = (char *)pFile->lockingContext;
drh7708e972008-11-29 00:56:52 +00001681 int rc = SQLITE_OK;
drh734c9862008-11-28 15:37:20 +00001682
drh7708e972008-11-29 00:56:52 +00001683
1684 /* If we have any lock, then the lock file already exists. All we have
1685 ** to do is adjust our internal record of the lock level.
1686 */
1687 if( pFile->locktype > NO_LOCK ){
drh734c9862008-11-28 15:37:20 +00001688 pFile->locktype = locktype;
1689#if !OS_VXWORKS
1690 /* Always update the timestamp on the old file */
1691 utimes(zLockFile, NULL);
1692#endif
drh7708e972008-11-29 00:56:52 +00001693 return SQLITE_OK;
drh734c9862008-11-28 15:37:20 +00001694 }
1695
1696 /* grab an exclusive lock */
1697 fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
1698 if( fd<0 ){
1699 /* failed to open/create the file, someone else may have stolen the lock */
1700 int tErrno = errno;
1701 if( EEXIST == tErrno ){
1702 rc = SQLITE_BUSY;
1703 } else {
1704 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1705 if( IS_LOCK_ERROR(rc) ){
1706 pFile->lastErrno = tErrno;
1707 }
1708 }
drh7708e972008-11-29 00:56:52 +00001709 return rc;
drh734c9862008-11-28 15:37:20 +00001710 }
1711 if( close(fd) ){
1712 pFile->lastErrno = errno;
1713 rc = SQLITE_IOERR_CLOSE;
1714 }
1715
1716 /* got it, set the type and return ok */
1717 pFile->locktype = locktype;
drh734c9862008-11-28 15:37:20 +00001718 return rc;
1719}
1720
drh7708e972008-11-29 00:56:52 +00001721/*
1722** Lower the locking level on file descriptor pFile to locktype. locktype
1723** must be either NO_LOCK or SHARED_LOCK.
1724**
1725** If the locking level of the file descriptor is already at or below
1726** the requested locking level, this routine is a no-op.
1727**
1728** When the locking level reaches NO_LOCK, delete the lock file.
1729*/
drh734c9862008-11-28 15:37:20 +00001730static int dotlockUnlock(sqlite3_file *id, int locktype) {
1731 unixFile *pFile = (unixFile*)id;
1732 char *zLockFile = (char *)pFile->lockingContext;
1733
1734 assert( pFile );
1735 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
1736 pFile->locktype, getpid());
1737 assert( locktype<=SHARED_LOCK );
1738
1739 /* no-op if possible */
1740 if( pFile->locktype==locktype ){
1741 return SQLITE_OK;
1742 }
drh7708e972008-11-29 00:56:52 +00001743
1744 /* To downgrade to shared, simply update our internal notion of the
1745 ** lock state. No need to mess with the file on disk.
1746 */
1747 if( locktype==SHARED_LOCK ){
1748 pFile->locktype = SHARED_LOCK;
drh734c9862008-11-28 15:37:20 +00001749 return SQLITE_OK;
1750 }
1751
drh7708e972008-11-29 00:56:52 +00001752 /* To fully unlock the database, delete the lock file */
1753 assert( locktype==NO_LOCK );
1754 if( unlink(zLockFile) ){
drh734c9862008-11-28 15:37:20 +00001755 int rc, tErrno = errno;
1756 if( ENOENT != tErrno ){
1757 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1758 }
1759 if( IS_LOCK_ERROR(rc) ){
1760 pFile->lastErrno = tErrno;
1761 }
1762 return rc;
1763 }
1764 pFile->locktype = NO_LOCK;
1765 return SQLITE_OK;
1766}
1767
1768/*
drh9b35ea62008-11-29 02:20:26 +00001769** Close a file. Make sure the lock has been released before closing.
drh734c9862008-11-28 15:37:20 +00001770*/
1771static int dotlockClose(sqlite3_file *id) {
1772 int rc;
1773 if( id ){
1774 unixFile *pFile = (unixFile*)id;
1775 dotlockUnlock(id, NO_LOCK);
1776 sqlite3_free(pFile->lockingContext);
1777 }
drh734c9862008-11-28 15:37:20 +00001778 rc = closeUnixFile(id);
drh734c9862008-11-28 15:37:20 +00001779 return rc;
1780}
1781/****************** End of the dot-file lock implementation *******************
1782******************************************************************************/
1783
1784/******************************************************************************
1785************************** Begin flock Locking ********************************
1786**
1787** Use the flock() system call to do file locking.
1788**
drh6b9d6dd2008-12-03 19:34:47 +00001789** flock() locking is like dot-file locking in that the various
1790** fine-grain locking levels supported by SQLite are collapsed into
1791** a single exclusive lock. In other words, SHARED, RESERVED, and
1792** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
1793** still works when you do this, but concurrency is reduced since
1794** only a single process can be reading the database at a time.
1795**
drh734c9862008-11-28 15:37:20 +00001796** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
1797** compiling for VXWORKS.
1798*/
1799#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
drh734c9862008-11-28 15:37:20 +00001800
drh6b9d6dd2008-12-03 19:34:47 +00001801/*
1802** This routine checks if there is a RESERVED lock held on the specified
1803** file by this or any other process. If such a lock is held, set *pResOut
1804** to a non-zero value otherwise *pResOut is set to zero. The return value
1805** is set to SQLITE_OK unless an I/O error occurs during lock checking.
1806*/
drh734c9862008-11-28 15:37:20 +00001807static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
1808 int rc = SQLITE_OK;
1809 int reserved = 0;
1810 unixFile *pFile = (unixFile*)id;
1811
1812 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1813
1814 assert( pFile );
1815
1816 /* Check if a thread in this process holds such a lock */
1817 if( pFile->locktype>SHARED_LOCK ){
1818 reserved = 1;
1819 }
1820
1821 /* Otherwise see if some other process holds it. */
1822 if( !reserved ){
1823 /* attempt to get the lock */
1824 int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
1825 if( !lrc ){
1826 /* got the lock, unlock it */
1827 lrc = flock(pFile->h, LOCK_UN);
1828 if ( lrc ) {
1829 int tErrno = errno;
1830 /* unlock failed with an error */
1831 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1832 if( IS_LOCK_ERROR(lrc) ){
1833 pFile->lastErrno = tErrno;
1834 rc = lrc;
1835 }
1836 }
1837 } else {
1838 int tErrno = errno;
1839 reserved = 1;
1840 /* someone else might have it reserved */
1841 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1842 if( IS_LOCK_ERROR(lrc) ){
1843 pFile->lastErrno = tErrno;
1844 rc = lrc;
1845 }
1846 }
1847 }
1848 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
1849
1850#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
1851 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
1852 rc = SQLITE_OK;
1853 reserved=1;
1854 }
1855#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
1856 *pResOut = reserved;
1857 return rc;
1858}
1859
drh6b9d6dd2008-12-03 19:34:47 +00001860/*
1861** Lock the file with the lock specified by parameter locktype - one
1862** of the following:
1863**
1864** (1) SHARED_LOCK
1865** (2) RESERVED_LOCK
1866** (3) PENDING_LOCK
1867** (4) EXCLUSIVE_LOCK
1868**
1869** Sometimes when requesting one lock state, additional lock states
1870** are inserted in between. The locking might fail on one of the later
1871** transitions leaving the lock state different from what it started but
1872** still short of its goal. The following chart shows the allowed
1873** transitions and the inserted intermediate states:
1874**
1875** UNLOCKED -> SHARED
1876** SHARED -> RESERVED
1877** SHARED -> (PENDING) -> EXCLUSIVE
1878** RESERVED -> (PENDING) -> EXCLUSIVE
1879** PENDING -> EXCLUSIVE
1880**
1881** flock() only really support EXCLUSIVE locks. We track intermediate
1882** lock states in the sqlite3_file structure, but all locks SHARED or
1883** above are really EXCLUSIVE locks and exclude all other processes from
1884** access the file.
1885**
1886** This routine will only increase a lock. Use the sqlite3OsUnlock()
1887** routine to lower a locking level.
1888*/
drh734c9862008-11-28 15:37:20 +00001889static int flockLock(sqlite3_file *id, int locktype) {
1890 int rc = SQLITE_OK;
drh734c9862008-11-28 15:37:20 +00001891 unixFile *pFile = (unixFile*)id;
1892
1893 assert( pFile );
1894
1895 /* if we already have a lock, it is exclusive.
1896 ** Just adjust level and punt on outta here. */
1897 if (pFile->locktype > NO_LOCK) {
1898 pFile->locktype = locktype;
1899 return SQLITE_OK;
1900 }
1901
1902 /* grab an exclusive lock */
1903
1904 if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
1905 int tErrno = errno;
1906 /* didn't get, must be busy */
1907 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1908 if( IS_LOCK_ERROR(rc) ){
1909 pFile->lastErrno = tErrno;
1910 }
1911 } else {
1912 /* got it, set the type and return ok */
1913 pFile->locktype = locktype;
1914 }
1915 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
1916 rc==SQLITE_OK ? "ok" : "failed");
1917#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
1918 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
1919 rc = SQLITE_BUSY;
1920 }
1921#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
1922 return rc;
1923}
1924
drh6b9d6dd2008-12-03 19:34:47 +00001925
1926/*
1927** Lower the locking level on file descriptor pFile to locktype. locktype
1928** must be either NO_LOCK or SHARED_LOCK.
1929**
1930** If the locking level of the file descriptor is already at or below
1931** the requested locking level, this routine is a no-op.
1932*/
drh734c9862008-11-28 15:37:20 +00001933static int flockUnlock(sqlite3_file *id, int locktype) {
1934 unixFile *pFile = (unixFile*)id;
1935
1936 assert( pFile );
1937 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
1938 pFile->locktype, getpid());
1939 assert( locktype<=SHARED_LOCK );
1940
1941 /* no-op if possible */
1942 if( pFile->locktype==locktype ){
1943 return SQLITE_OK;
1944 }
1945
1946 /* shared can just be set because we always have an exclusive */
1947 if (locktype==SHARED_LOCK) {
1948 pFile->locktype = locktype;
1949 return SQLITE_OK;
1950 }
1951
1952 /* no, really, unlock. */
1953 int rc = flock(pFile->h, LOCK_UN);
1954 if (rc) {
1955 int r, tErrno = errno;
1956 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1957 if( IS_LOCK_ERROR(r) ){
1958 pFile->lastErrno = tErrno;
1959 }
1960#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
1961 if( (r & SQLITE_IOERR) == SQLITE_IOERR ){
1962 r = SQLITE_BUSY;
1963 }
1964#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
1965
1966 return r;
1967 } else {
1968 pFile->locktype = NO_LOCK;
1969 return SQLITE_OK;
1970 }
1971}
1972
1973/*
1974** Close a file.
1975*/
1976static int flockClose(sqlite3_file *id) {
1977 if( id ){
1978 flockUnlock(id, NO_LOCK);
1979 }
1980 return closeUnixFile(id);
1981}
1982
1983#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
1984
1985/******************* End of the flock lock implementation *********************
1986******************************************************************************/
1987
1988/******************************************************************************
1989************************ Begin Named Semaphore Locking ************************
1990**
1991** Named semaphore locking is only supported on VxWorks.
drh6b9d6dd2008-12-03 19:34:47 +00001992**
1993** Semaphore locking is like dot-lock and flock in that it really only
1994** supports EXCLUSIVE locking. Only a single process can read or write
1995** the database file at a time. This reduces potential concurrency, but
1996** makes the lock implementation much easier.
drh734c9862008-11-28 15:37:20 +00001997*/
1998#if OS_VXWORKS
1999
drh6b9d6dd2008-12-03 19:34:47 +00002000/*
2001** This routine checks if there is a RESERVED lock held on the specified
2002** file by this or any other process. If such a lock is held, set *pResOut
2003** to a non-zero value otherwise *pResOut is set to zero. The return value
2004** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2005*/
drh734c9862008-11-28 15:37:20 +00002006static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
2007 int rc = SQLITE_OK;
2008 int reserved = 0;
2009 unixFile *pFile = (unixFile*)id;
2010
2011 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2012
2013 assert( pFile );
2014
2015 /* Check if a thread in this process holds such a lock */
2016 if( pFile->locktype>SHARED_LOCK ){
2017 reserved = 1;
2018 }
2019
2020 /* Otherwise see if some other process holds it. */
2021 if( !reserved ){
2022 sem_t *pSem = pFile->pOpen->pSem;
2023 struct stat statBuf;
2024
2025 if( sem_trywait(pSem)==-1 ){
2026 int tErrno = errno;
2027 if( EAGAIN != tErrno ){
2028 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2029 pFile->lastErrno = tErrno;
2030 } else {
2031 /* someone else has the lock when we are in NO_LOCK */
2032 reserved = (pFile->locktype < SHARED_LOCK);
2033 }
2034 }else{
2035 /* we could have it if we want it */
2036 sem_post(pSem);
2037 }
2038 }
2039 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2040
2041 *pResOut = reserved;
2042 return rc;
2043}
2044
drh6b9d6dd2008-12-03 19:34:47 +00002045/*
2046** Lock the file with the lock specified by parameter locktype - one
2047** of the following:
2048**
2049** (1) SHARED_LOCK
2050** (2) RESERVED_LOCK
2051** (3) PENDING_LOCK
2052** (4) EXCLUSIVE_LOCK
2053**
2054** Sometimes when requesting one lock state, additional lock states
2055** are inserted in between. The locking might fail on one of the later
2056** transitions leaving the lock state different from what it started but
2057** still short of its goal. The following chart shows the allowed
2058** transitions and the inserted intermediate states:
2059**
2060** UNLOCKED -> SHARED
2061** SHARED -> RESERVED
2062** SHARED -> (PENDING) -> EXCLUSIVE
2063** RESERVED -> (PENDING) -> EXCLUSIVE
2064** PENDING -> EXCLUSIVE
2065**
2066** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
2067** lock states in the sqlite3_file structure, but all locks SHARED or
2068** above are really EXCLUSIVE locks and exclude all other processes from
2069** access the file.
2070**
2071** This routine will only increase a lock. Use the sqlite3OsUnlock()
2072** routine to lower a locking level.
2073*/
drh734c9862008-11-28 15:37:20 +00002074static int semLock(sqlite3_file *id, int locktype) {
2075 unixFile *pFile = (unixFile*)id;
2076 int fd;
2077 sem_t *pSem = pFile->pOpen->pSem;
2078 int rc = SQLITE_OK;
2079
2080 /* if we already have a lock, it is exclusive.
2081 ** Just adjust level and punt on outta here. */
2082 if (pFile->locktype > NO_LOCK) {
2083 pFile->locktype = locktype;
2084 rc = SQLITE_OK;
2085 goto sem_end_lock;
2086 }
2087
2088 /* lock semaphore now but bail out when already locked. */
2089 if( sem_trywait(pSem)==-1 ){
2090 rc = SQLITE_BUSY;
2091 goto sem_end_lock;
2092 }
2093
2094 /* got it, set the type and return ok */
2095 pFile->locktype = locktype;
2096
2097 sem_end_lock:
2098 return rc;
2099}
2100
drh6b9d6dd2008-12-03 19:34:47 +00002101/*
2102** Lower the locking level on file descriptor pFile to locktype. locktype
2103** must be either NO_LOCK or SHARED_LOCK.
2104**
2105** If the locking level of the file descriptor is already at or below
2106** the requested locking level, this routine is a no-op.
2107*/
drh734c9862008-11-28 15:37:20 +00002108static int semUnlock(sqlite3_file *id, int locktype) {
2109 unixFile *pFile = (unixFile*)id;
2110 sem_t *pSem = pFile->pOpen->pSem;
2111
2112 assert( pFile );
2113 assert( pSem );
2114 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2115 pFile->locktype, getpid());
2116 assert( locktype<=SHARED_LOCK );
2117
2118 /* no-op if possible */
2119 if( pFile->locktype==locktype ){
2120 return SQLITE_OK;
2121 }
2122
2123 /* shared can just be set because we always have an exclusive */
2124 if (locktype==SHARED_LOCK) {
2125 pFile->locktype = locktype;
2126 return SQLITE_OK;
2127 }
2128
2129 /* no, really unlock. */
2130 if ( sem_post(pSem)==-1 ) {
2131 int rc, tErrno = errno;
2132 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2133 if( IS_LOCK_ERROR(rc) ){
2134 pFile->lastErrno = tErrno;
2135 }
2136 return rc;
2137 }
2138 pFile->locktype = NO_LOCK;
2139 return SQLITE_OK;
2140}
2141
2142/*
2143 ** Close a file.
drhbfe66312006-10-03 17:40:40 +00002144 */
drh734c9862008-11-28 15:37:20 +00002145static int semClose(sqlite3_file *id) {
2146 if( id ){
2147 unixFile *pFile = (unixFile*)id;
2148 semUnlock(id, NO_LOCK);
2149 assert( pFile );
2150 unixEnterMutex();
2151 releaseLockInfo(pFile->pLock);
2152 releaseOpenCnt(pFile->pOpen);
2153 closeUnixFile(id);
2154 unixLeaveMutex();
2155 }
2156 return SQLITE_OK;
2157}
2158
2159#endif /* OS_VXWORKS */
2160/*
2161** Named semaphore locking is only available on VxWorks.
2162**
2163*************** End of the named semaphore lock implementation ****************
2164******************************************************************************/
2165
2166
2167/******************************************************************************
2168*************************** Begin AFP Locking *********************************
2169**
2170** AFP is the Apple Filing Protocol. AFP is a network filesystem found
2171** on Apple Macintosh computers - both OS9 and OSX.
2172**
2173** Third-party implementations of AFP are available. But this code here
2174** only works on OSX.
2175*/
2176
2177#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
2178/*
2179** The afpLockingContext structure contains all afp lock specific state
2180*/
drhbfe66312006-10-03 17:40:40 +00002181typedef struct afpLockingContext afpLockingContext;
2182struct afpLockingContext {
aswiftaebf4132008-11-21 00:10:35 +00002183 unsigned long long sharedByte;
drh6b9d6dd2008-12-03 19:34:47 +00002184 const char *dbPath; /* Name of the open file */
drhbfe66312006-10-03 17:40:40 +00002185};
2186
2187struct ByteRangeLockPB2
2188{
2189 unsigned long long offset; /* offset to first byte to lock */
2190 unsigned long long length; /* nbr of bytes to lock */
2191 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
2192 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
2193 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
2194 int fd; /* file desc to assoc this lock with */
2195};
2196
drhfd131da2007-08-07 17:13:03 +00002197#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00002198
drh6b9d6dd2008-12-03 19:34:47 +00002199/*
2200** This is a utility for setting or clearing a bit-range lock on an
2201** AFP filesystem.
2202**
2203** Return SQLITE_OK on success, SQLITE_BUSY on failure.
2204*/
2205static int afpSetLock(
2206 const char *path, /* Name of the file to be locked or unlocked */
2207 unixFile *pFile, /* Open file descriptor on path */
2208 unsigned long long offset, /* First byte to be locked */
2209 unsigned long long length, /* Number of bytes to lock */
2210 int setLockFlag /* True to set lock. False to clear lock */
danielk1977ad94b582007-08-20 06:44:22 +00002211){
drh6b9d6dd2008-12-03 19:34:47 +00002212 struct ByteRangeLockPB2 pb;
2213 int err;
drhbfe66312006-10-03 17:40:40 +00002214
2215 pb.unLockFlag = setLockFlag ? 0 : 1;
2216 pb.startEndFlag = 0;
2217 pb.offset = offset;
2218 pb.length = length;
aswift5b1a2562008-08-22 00:22:35 +00002219 pb.fd = pFile->h;
aswiftaebf4132008-11-21 00:10:35 +00002220 //SimulateIOErrorBenign(1);
2221 //SimulateIOError( pb.fd=(-1) )
2222 //SimulateIOErrorBenign(0);
2223
2224 OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
drh734c9862008-11-28 15:37:20 +00002225 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
2226 offset, length);
drhbfe66312006-10-03 17:40:40 +00002227 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
2228 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00002229 int rc;
2230 int tErrno = errno;
drh734c9862008-11-28 15:37:20 +00002231 OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
2232 path, tErrno, strerror(tErrno));
aswiftaebf4132008-11-21 00:10:35 +00002233#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
2234 rc = SQLITE_BUSY;
2235#else
drh734c9862008-11-28 15:37:20 +00002236 rc = sqliteErrorFromPosixError(tErrno,
2237 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
aswiftaebf4132008-11-21 00:10:35 +00002238#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002239 if( IS_LOCK_ERROR(rc) ){
2240 pFile->lastErrno = tErrno;
2241 }
2242 return rc;
drhbfe66312006-10-03 17:40:40 +00002243 } else {
aswift5b1a2562008-08-22 00:22:35 +00002244 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002245 }
2246}
2247
drh6b9d6dd2008-12-03 19:34:47 +00002248/*
2249** This routine checks if there is a RESERVED lock held on the specified
2250** file by this or any other process. If such a lock is held, set *pResOut
2251** to a non-zero value otherwise *pResOut is set to zero. The return value
2252** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2253*/
danielk1977e339d652008-06-28 11:23:00 +00002254static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002255 int rc = SQLITE_OK;
2256 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002257 unixFile *pFile = (unixFile*)id;
2258
aswift5b1a2562008-08-22 00:22:35 +00002259 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2260
2261 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00002262 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2263
2264 /* Check if a thread in this process holds such a lock */
2265 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002266 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00002267 }
2268
2269 /* Otherwise see if some other process holds it.
2270 */
aswift5b1a2562008-08-22 00:22:35 +00002271 if( !reserved ){
2272 /* lock the RESERVED byte */
drh6b9d6dd2008-12-03 19:34:47 +00002273 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
aswift5b1a2562008-08-22 00:22:35 +00002274 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00002275 /* if we succeeded in taking the reserved lock, unlock it to restore
2276 ** the original state */
drh6b9d6dd2008-12-03 19:34:47 +00002277 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
aswift5b1a2562008-08-22 00:22:35 +00002278 } else {
2279 /* if we failed to get the lock then someone else must have it */
2280 reserved = 1;
2281 }
2282 if( IS_LOCK_ERROR(lrc) ){
2283 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00002284 }
2285 }
drhbfe66312006-10-03 17:40:40 +00002286
aswift5b1a2562008-08-22 00:22:35 +00002287 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2288
2289 *pResOut = reserved;
2290 return rc;
drhbfe66312006-10-03 17:40:40 +00002291}
2292
drh6b9d6dd2008-12-03 19:34:47 +00002293/*
2294** Lock the file with the lock specified by parameter locktype - one
2295** of the following:
2296**
2297** (1) SHARED_LOCK
2298** (2) RESERVED_LOCK
2299** (3) PENDING_LOCK
2300** (4) EXCLUSIVE_LOCK
2301**
2302** Sometimes when requesting one lock state, additional lock states
2303** are inserted in between. The locking might fail on one of the later
2304** transitions leaving the lock state different from what it started but
2305** still short of its goal. The following chart shows the allowed
2306** transitions and the inserted intermediate states:
2307**
2308** UNLOCKED -> SHARED
2309** SHARED -> RESERVED
2310** SHARED -> (PENDING) -> EXCLUSIVE
2311** RESERVED -> (PENDING) -> EXCLUSIVE
2312** PENDING -> EXCLUSIVE
2313**
2314** This routine will only increase a lock. Use the sqlite3OsUnlock()
2315** routine to lower a locking level.
2316*/
danielk1977e339d652008-06-28 11:23:00 +00002317static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00002318 int rc = SQLITE_OK;
2319 unixFile *pFile = (unixFile*)id;
2320 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002321
2322 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002323 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00002324 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
2325
drhbfe66312006-10-03 17:40:40 +00002326 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00002327 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
drh6c7d5c52008-11-21 20:32:33 +00002328 ** unixEnterMutex() hasn't been called yet.
drh339eb0b2008-03-07 15:34:11 +00002329 */
drhbfe66312006-10-03 17:40:40 +00002330 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00002331 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00002332 locktypeName(locktype));
2333 return SQLITE_OK;
2334 }
2335
2336 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00002337 */
drhbfe66312006-10-03 17:40:40 +00002338 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
2339 assert( locktype!=PENDING_LOCK );
2340 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
2341
2342 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00002343 */
drh6c7d5c52008-11-21 20:32:33 +00002344 unixEnterMutex();
drhbfe66312006-10-03 17:40:40 +00002345
2346 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00002347 */
drhbfe66312006-10-03 17:40:40 +00002348 rc = transferOwnership(pFile);
2349 if( rc!=SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00002350 unixLeaveMutex();
drhbfe66312006-10-03 17:40:40 +00002351 return rc;
2352 }
2353
2354 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00002355 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2356 ** be released.
2357 */
drhbfe66312006-10-03 17:40:40 +00002358 if( locktype==SHARED_LOCK
2359 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00002360 ){
2361 int failed;
drh6b9d6dd2008-12-03 19:34:47 +00002362 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00002363 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002364 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002365 goto afp_end_lock;
2366 }
2367 }
2368
2369 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00002370 ** operating system calls for the specified lock.
2371 */
drhbfe66312006-10-03 17:40:40 +00002372 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002373 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00002374
aswift5b1a2562008-08-22 00:22:35 +00002375 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00002376 /* note that the quality of the randomness doesn't matter that much */
2377 lk = random();
aswiftaebf4132008-11-21 00:10:35 +00002378 context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
drh6b9d6dd2008-12-03 19:34:47 +00002379 lrc1 = afpSetLock(context->dbPath, pFile,
aswiftaebf4132008-11-21 00:10:35 +00002380 SHARED_FIRST+context->sharedByte, 1, 1);
aswift5b1a2562008-08-22 00:22:35 +00002381 if( IS_LOCK_ERROR(lrc1) ){
2382 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00002383 }
aswift5b1a2562008-08-22 00:22:35 +00002384 /* Drop the temporary PENDING lock */
drh6b9d6dd2008-12-03 19:34:47 +00002385 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00002386
aswift5b1a2562008-08-22 00:22:35 +00002387 if( IS_LOCK_ERROR(lrc1) ) {
2388 pFile->lastErrno = lrc1Errno;
2389 rc = lrc1;
2390 goto afp_end_lock;
2391 } else if( IS_LOCK_ERROR(lrc2) ){
2392 rc = lrc2;
2393 goto afp_end_lock;
2394 } else if( lrc1 != SQLITE_OK ) {
2395 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00002396 } else {
2397 pFile->locktype = SHARED_LOCK;
aswiftaebf4132008-11-21 00:10:35 +00002398 pFile->pOpen->nLock++;
drhbfe66312006-10-03 17:40:40 +00002399 }
2400 }else{
2401 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2402 ** assumed that there is a SHARED or greater lock on the file
2403 ** already.
2404 */
2405 int failed = 0;
2406 assert( 0!=pFile->locktype );
2407 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
2408 /* Acquire a RESERVED lock */
drh6b9d6dd2008-12-03 19:34:47 +00002409 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00002410 }
2411 if (!failed && locktype == EXCLUSIVE_LOCK) {
2412 /* Acquire an EXCLUSIVE lock */
2413
2414 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00002415 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00002416 */
drh6b9d6dd2008-12-03 19:34:47 +00002417 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
aswiftaebf4132008-11-21 00:10:35 +00002418 context->sharedByte, 1, 0)) ){
2419 int failed2 = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002420 /* now attemmpt to get the exclusive lock range */
drh6b9d6dd2008-12-03 19:34:47 +00002421 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002422 SHARED_SIZE, 1);
drh6b9d6dd2008-12-03 19:34:47 +00002423 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
aswiftaebf4132008-11-21 00:10:35 +00002424 SHARED_FIRST + context->sharedByte, 1, 1)) ){
2425 /* Can't reestablish the shared lock. Sqlite can't deal, this is
2426 ** a critical I/O error
2427 */
2428 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
2429 SQLITE_IOERR_LOCK;
2430 goto afp_end_lock;
2431 }
2432 }else{
aswift5b1a2562008-08-22 00:22:35 +00002433 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002434 }
2435 }
aswift5b1a2562008-08-22 00:22:35 +00002436 if( failed ){
2437 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002438 }
2439 }
2440
2441 if( rc==SQLITE_OK ){
2442 pFile->locktype = locktype;
2443 }else if( locktype==EXCLUSIVE_LOCK ){
2444 pFile->locktype = PENDING_LOCK;
2445 }
2446
2447afp_end_lock:
drh6c7d5c52008-11-21 20:32:33 +00002448 unixLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00002449 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00002450 rc==SQLITE_OK ? "ok" : "failed");
2451 return rc;
2452}
2453
2454/*
drh339eb0b2008-03-07 15:34:11 +00002455** Lower the locking level on file descriptor pFile to locktype. locktype
2456** must be either NO_LOCK or SHARED_LOCK.
2457**
2458** If the locking level of the file descriptor is already at or below
2459** the requested locking level, this routine is a no-op.
2460*/
danielk1977e339d652008-06-28 11:23:00 +00002461static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002462 int rc = SQLITE_OK;
2463 unixFile *pFile = (unixFile*)id;
aswiftaebf4132008-11-21 00:10:35 +00002464 afpLockingContext *pCtx = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002465
2466 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002467 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00002468 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00002469
drhbfe66312006-10-03 17:40:40 +00002470 assert( locktype<=SHARED_LOCK );
2471 if( pFile->locktype<=locktype ){
2472 return SQLITE_OK;
2473 }
2474 if( CHECK_THREADID(pFile) ){
2475 return SQLITE_MISUSE;
2476 }
drh6c7d5c52008-11-21 20:32:33 +00002477 unixEnterMutex();
drhbfe66312006-10-03 17:40:40 +00002478 if( pFile->locktype>SHARED_LOCK ){
aswiftaebf4132008-11-21 00:10:35 +00002479
2480 if( pFile->locktype==EXCLUSIVE_LOCK ){
drh6b9d6dd2008-12-03 19:34:47 +00002481 rc = afpSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
aswiftaebf4132008-11-21 00:10:35 +00002482 if( rc==SQLITE_OK && locktype==SHARED_LOCK ){
2483 /* only re-establish the shared lock if necessary */
2484 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
drh6b9d6dd2008-12-03 19:34:47 +00002485 rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1);
aswiftaebf4132008-11-21 00:10:35 +00002486 }
2487 }
2488 if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
drh6b9d6dd2008-12-03 19:34:47 +00002489 rc = afpSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0);
aswiftaebf4132008-11-21 00:10:35 +00002490 }
2491 if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){
drh6b9d6dd2008-12-03 19:34:47 +00002492 rc = afpSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0);
aswiftaebf4132008-11-21 00:10:35 +00002493 }
2494 }else if( locktype==NO_LOCK ){
2495 /* clear the shared lock */
2496 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
drh6b9d6dd2008-12-03 19:34:47 +00002497 rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0);
aswiftaebf4132008-11-21 00:10:35 +00002498 }
drhbfe66312006-10-03 17:40:40 +00002499
aswiftaebf4132008-11-21 00:10:35 +00002500 if( rc==SQLITE_OK ){
2501 if( locktype==NO_LOCK ){
drh6c7d5c52008-11-21 20:32:33 +00002502 struct unixOpenCnt *pOpen = pFile->pOpen;
aswiftaebf4132008-11-21 00:10:35 +00002503 pOpen->nLock--;
2504 assert( pOpen->nLock>=0 );
2505 if( pOpen->nLock==0 && pOpen->nPending>0 ){
2506 int i;
2507 for(i=0; i<pOpen->nPending; i++){
2508 if( pOpen->aPending[i] < 0 ) continue;
2509 if( close(pOpen->aPending[i]) ){
2510 pFile->lastErrno = errno;
2511 rc = SQLITE_IOERR_CLOSE;
2512 }else{
2513 pOpen->aPending[i] = -1;
drhbfe66312006-10-03 17:40:40 +00002514 }
aswiftaebf4132008-11-21 00:10:35 +00002515 }
2516 if( rc==SQLITE_OK ){
2517 sqlite3_free(pOpen->aPending);
2518 pOpen->nPending = 0;
2519 pOpen->aPending = 0;
2520 }
drhbfe66312006-10-03 17:40:40 +00002521 }
2522 }
drhbfe66312006-10-03 17:40:40 +00002523 }
drh6c7d5c52008-11-21 20:32:33 +00002524 unixLeaveMutex();
aswiftaebf4132008-11-21 00:10:35 +00002525 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002526 return rc;
2527}
2528
2529/*
drh339eb0b2008-03-07 15:34:11 +00002530** Close a file & cleanup AFP specific locking context
2531*/
danielk1977e339d652008-06-28 11:23:00 +00002532static int afpClose(sqlite3_file *id) {
2533 if( id ){
2534 unixFile *pFile = (unixFile*)id;
2535 afpUnlock(id, NO_LOCK);
drh6c7d5c52008-11-21 20:32:33 +00002536 unixEnterMutex();
aswiftaebf4132008-11-21 00:10:35 +00002537 if( pFile->pOpen && pFile->pOpen->nLock ){
2538 /* If there are outstanding locks, do not actually close the file just
drh734c9862008-11-28 15:37:20 +00002539 ** yet because that would clear those locks. Instead, add the file
2540 ** descriptor to pOpen->aPending. It will be automatically closed when
2541 ** the last lock is cleared.
2542 */
aswiftaebf4132008-11-21 00:10:35 +00002543 int *aNew;
drh6c7d5c52008-11-21 20:32:33 +00002544 struct unixOpenCnt *pOpen = pFile->pOpen;
aswiftaebf4132008-11-21 00:10:35 +00002545 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
2546 if( aNew==0 ){
2547 /* If a malloc fails, just leak the file descriptor */
2548 }else{
2549 pOpen->aPending = aNew;
2550 pOpen->aPending[pOpen->nPending] = pFile->h;
2551 pOpen->nPending++;
2552 pFile->h = -1;
2553 }
2554 }
2555 releaseOpenCnt(pFile->pOpen);
danielk1977e339d652008-06-28 11:23:00 +00002556 sqlite3_free(pFile->lockingContext);
aswiftaebf4132008-11-21 00:10:35 +00002557 closeUnixFile(id);
drh6c7d5c52008-11-21 20:32:33 +00002558 unixLeaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00002559 }
aswiftaebf4132008-11-21 00:10:35 +00002560 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002561}
2562
drh734c9862008-11-28 15:37:20 +00002563#endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
2564/*
2565** The code above is the AFP lock implementation. The code is specific
2566** to MacOSX and does not work on other unix platforms. No alternative
2567** is available. If you don't compile for a mac, then the "unix-afp"
2568** VFS is not available.
2569**
2570********************* End of the AFP lock implementation **********************
2571******************************************************************************/
drhbfe66312006-10-03 17:40:40 +00002572
drh734c9862008-11-28 15:37:20 +00002573
2574/******************************************************************************
2575**************** Non-locking sqlite3_file methods *****************************
2576**
2577** The next division contains implementations for all methods of the
2578** sqlite3_file object other than the locking methods. The locking
2579** methods were defined in divisions above (one locking method per
2580** division). Those methods that are common to all locking modes
2581** are gather together into this division.
2582*/
drhbfe66312006-10-03 17:40:40 +00002583
2584/*
drh734c9862008-11-28 15:37:20 +00002585** Seek to the offset passed as the second argument, then read cnt
2586** bytes into pBuf. Return the number of bytes actually read.
2587**
2588** NB: If you define USE_PREAD or USE_PREAD64, then it might also
2589** be necessary to define _XOPEN_SOURCE to be 500. This varies from
2590** one system to another. Since SQLite does not define USE_PREAD
2591** any any form by default, we will not attempt to define _XOPEN_SOURCE.
2592** See tickets #2741 and #2681.
2593**
2594** To avoid stomping the errno value on a failed read the lastErrno value
2595** is set before returning.
drh339eb0b2008-03-07 15:34:11 +00002596*/
drh734c9862008-11-28 15:37:20 +00002597static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
2598 int got;
2599 i64 newOffset;
2600 TIMER_START;
2601#if defined(USE_PREAD)
2602 got = pread(id->h, pBuf, cnt, offset);
2603 SimulateIOError( got = -1 );
2604#elif defined(USE_PREAD64)
2605 got = pread64(id->h, pBuf, cnt, offset);
2606 SimulateIOError( got = -1 );
2607#else
2608 newOffset = lseek(id->h, offset, SEEK_SET);
2609 SimulateIOError( newOffset-- );
2610 if( newOffset!=offset ){
2611 if( newOffset == -1 ){
2612 ((unixFile*)id)->lastErrno = errno;
2613 }else{
2614 ((unixFile*)id)->lastErrno = 0;
2615 }
2616 return -1;
2617 }
2618 got = read(id->h, pBuf, cnt);
2619#endif
2620 TIMER_END;
2621 if( got<0 ){
2622 ((unixFile*)id)->lastErrno = errno;
2623 }
2624 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
2625 return got;
drhbfe66312006-10-03 17:40:40 +00002626}
2627
2628/*
drh734c9862008-11-28 15:37:20 +00002629** Read data from a file into a buffer. Return SQLITE_OK if all
2630** bytes were read successfully and SQLITE_IOERR if anything goes
2631** wrong.
drh339eb0b2008-03-07 15:34:11 +00002632*/
drh734c9862008-11-28 15:37:20 +00002633static int unixRead(
2634 sqlite3_file *id,
2635 void *pBuf,
2636 int amt,
2637 sqlite3_int64 offset
2638){
2639 int got;
2640 assert( id );
2641 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
2642 if( got==amt ){
2643 return SQLITE_OK;
2644 }else if( got<0 ){
2645 /* lastErrno set by seekAndRead */
2646 return SQLITE_IOERR_READ;
2647 }else{
2648 ((unixFile*)id)->lastErrno = 0; /* not a system error */
2649 /* Unread parts of the buffer must be zero-filled */
2650 memset(&((char*)pBuf)[got], 0, amt-got);
2651 return SQLITE_IOERR_SHORT_READ;
2652 }
2653}
2654
2655/*
2656** Seek to the offset in id->offset then read cnt bytes into pBuf.
2657** Return the number of bytes actually read. Update the offset.
2658**
2659** To avoid stomping the errno value on a failed write the lastErrno value
2660** is set before returning.
2661*/
2662static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
2663 int got;
2664 i64 newOffset;
2665 TIMER_START;
2666#if defined(USE_PREAD)
2667 got = pwrite(id->h, pBuf, cnt, offset);
2668#elif defined(USE_PREAD64)
2669 got = pwrite64(id->h, pBuf, cnt, offset);
2670#else
2671 newOffset = lseek(id->h, offset, SEEK_SET);
2672 if( newOffset!=offset ){
2673 if( newOffset == -1 ){
2674 ((unixFile*)id)->lastErrno = errno;
2675 }else{
2676 ((unixFile*)id)->lastErrno = 0;
2677 }
2678 return -1;
2679 }
2680 got = write(id->h, pBuf, cnt);
2681#endif
2682 TIMER_END;
2683 if( got<0 ){
2684 ((unixFile*)id)->lastErrno = errno;
2685 }
2686
2687 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
2688 return got;
2689}
2690
2691
2692/*
2693** Write data from a buffer into a file. Return SQLITE_OK on success
2694** or some other error code on failure.
2695*/
2696static int unixWrite(
2697 sqlite3_file *id,
2698 const void *pBuf,
2699 int amt,
2700 sqlite3_int64 offset
2701){
2702 int wrote = 0;
2703 assert( id );
2704 assert( amt>0 );
2705 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
2706 amt -= wrote;
2707 offset += wrote;
2708 pBuf = &((char*)pBuf)[wrote];
2709 }
2710 SimulateIOError(( wrote=(-1), amt=1 ));
2711 SimulateDiskfullError(( wrote=0, amt=1 ));
2712 if( amt>0 ){
2713 if( wrote<0 ){
2714 /* lastErrno set by seekAndWrite */
2715 return SQLITE_IOERR_WRITE;
2716 }else{
2717 ((unixFile*)id)->lastErrno = 0; /* not a system error */
2718 return SQLITE_FULL;
2719 }
2720 }
2721 return SQLITE_OK;
2722}
2723
2724#ifdef SQLITE_TEST
2725/*
2726** Count the number of fullsyncs and normal syncs. This is used to test
drh6b9d6dd2008-12-03 19:34:47 +00002727** that syncs and fullsyncs are occurring at the right times.
drh734c9862008-11-28 15:37:20 +00002728*/
2729int sqlite3_sync_count = 0;
2730int sqlite3_fullsync_count = 0;
2731#endif
2732
2733/*
2734** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
2735** Otherwise use fsync() in its place.
2736*/
2737#ifndef HAVE_FDATASYNC
2738# define fdatasync fsync
2739#endif
2740
2741/*
2742** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
2743** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
2744** only available on Mac OS X. But that could change.
2745*/
2746#ifdef F_FULLFSYNC
2747# define HAVE_FULLFSYNC 1
2748#else
2749# define HAVE_FULLFSYNC 0
2750#endif
2751
2752
2753/*
2754** The fsync() system call does not work as advertised on many
2755** unix systems. The following procedure is an attempt to make
2756** it work better.
2757**
2758** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
2759** for testing when we want to run through the test suite quickly.
2760** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
2761** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
2762** or power failure will likely corrupt the database file.
2763*/
2764static int full_fsync(int fd, int fullSync, int dataOnly){
chw97185482008-11-17 08:05:31 +00002765 int rc;
drh734c9862008-11-28 15:37:20 +00002766
2767 /* The following "ifdef/elif/else/" block has the same structure as
2768 ** the one below. It is replicated here solely to avoid cluttering
2769 ** up the real code with the UNUSED_PARAMETER() macros.
2770 */
2771#ifdef SQLITE_NO_SYNC
2772 UNUSED_PARAMETER(fd);
2773 UNUSED_PARAMETER(fullSync);
2774 UNUSED_PARAMETER(dataOnly);
2775#elif HAVE_FULLFSYNC
2776 UNUSED_PARAMETER(dataOnly);
2777#else
2778 UNUSED_PARAMETER(fullSync);
2779#endif
2780
2781 /* Record the number of times that we do a normal fsync() and
2782 ** FULLSYNC. This is used during testing to verify that this procedure
2783 ** gets called with the correct arguments.
2784 */
2785#ifdef SQLITE_TEST
2786 if( fullSync ) sqlite3_fullsync_count++;
2787 sqlite3_sync_count++;
2788#endif
2789
2790 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
2791 ** no-op
2792 */
2793#ifdef SQLITE_NO_SYNC
2794 rc = SQLITE_OK;
2795#elif HAVE_FULLFSYNC
2796 if( fullSync ){
2797 rc = fcntl(fd, F_FULLFSYNC, 0);
2798 }else{
2799 rc = 1;
2800 }
2801 /* If the FULLFSYNC failed, fall back to attempting an fsync().
drh6b9d6dd2008-12-03 19:34:47 +00002802 ** It shouldn't be possible for fullfsync to fail on the local
2803 ** file system (on OSX), so failure indicates that FULLFSYNC
2804 ** isn't supported for this file system. So, attempt an fsync
2805 ** and (for now) ignore the overhead of a superfluous fcntl call.
2806 ** It'd be better to detect fullfsync support once and avoid
2807 ** the fcntl call every time sync is called.
2808 */
drh734c9862008-11-28 15:37:20 +00002809 if( rc ) rc = fsync(fd);
2810
2811#else
2812 if( dataOnly ){
2813 rc = fdatasync(fd);
2814 if( OS_VXWORKS && rc==-1 && errno==ENOTSUP ){
2815 rc = fsync(fd);
2816 }
2817 }else{
2818 rc = fsync(fd);
2819 }
2820#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
2821
2822 if( OS_VXWORKS && rc!= -1 ){
2823 rc = 0;
2824 }
chw97185482008-11-17 08:05:31 +00002825 return rc;
drhbfe66312006-10-03 17:40:40 +00002826}
2827
drh734c9862008-11-28 15:37:20 +00002828/*
2829** Make sure all writes to a particular file are committed to disk.
2830**
2831** If dataOnly==0 then both the file itself and its metadata (file
2832** size, access time, etc) are synced. If dataOnly!=0 then only the
2833** file data is synced.
2834**
2835** Under Unix, also make sure that the directory entry for the file
2836** has been created by fsync-ing the directory that contains the file.
2837** If we do not do this and we encounter a power failure, the directory
2838** entry for the journal might not exist after we reboot. The next
2839** SQLite to access the file will not know that the journal exists (because
2840** the directory entry for the journal was never created) and the transaction
2841** will not roll back - possibly leading to database corruption.
2842*/
2843static int unixSync(sqlite3_file *id, int flags){
2844 int rc;
2845 unixFile *pFile = (unixFile*)id;
2846
2847 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
2848 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
2849
2850 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
2851 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
2852 || (flags&0x0F)==SQLITE_SYNC_FULL
2853 );
2854
2855 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
2856 ** line is to test that doing so does not cause any problems.
2857 */
2858 SimulateDiskfullError( return SQLITE_FULL );
2859
2860 assert( pFile );
2861 OSTRACE2("SYNC %-3d\n", pFile->h);
2862 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
2863 SimulateIOError( rc=1 );
2864 if( rc ){
2865 pFile->lastErrno = errno;
2866 return SQLITE_IOERR_FSYNC;
2867 }
2868 if( pFile->dirfd>=0 ){
2869 int err;
2870 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
2871 HAVE_FULLFSYNC, isFullsync);
2872#ifndef SQLITE_DISABLE_DIRSYNC
2873 /* The directory sync is only attempted if full_fsync is
2874 ** turned off or unavailable. If a full_fsync occurred above,
2875 ** then the directory sync is superfluous.
2876 */
2877 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
2878 /*
2879 ** We have received multiple reports of fsync() returning
2880 ** errors when applied to directories on certain file systems.
2881 ** A failed directory sync is not a big deal. So it seems
2882 ** better to ignore the error. Ticket #1657
2883 */
2884 /* pFile->lastErrno = errno; */
2885 /* return SQLITE_IOERR; */
2886 }
2887#endif
2888 err = close(pFile->dirfd); /* Only need to sync once, so close the */
2889 if( err==0 ){ /* directory when we are done */
2890 pFile->dirfd = -1;
2891 }else{
2892 pFile->lastErrno = errno;
2893 rc = SQLITE_IOERR_DIR_CLOSE;
2894 }
2895 }
2896 return rc;
2897}
2898
2899/*
2900** Truncate an open file to a specified size
2901*/
2902static int unixTruncate(sqlite3_file *id, i64 nByte){
2903 int rc;
2904 assert( id );
2905 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
2906 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
2907 if( rc ){
2908 ((unixFile*)id)->lastErrno = errno;
2909 return SQLITE_IOERR_TRUNCATE;
2910 }else{
2911 return SQLITE_OK;
2912 }
2913}
2914
2915/*
2916** Determine the current size of a file in bytes
2917*/
2918static int unixFileSize(sqlite3_file *id, i64 *pSize){
2919 int rc;
2920 struct stat buf;
2921 assert( id );
2922 rc = fstat(((unixFile*)id)->h, &buf);
2923 SimulateIOError( rc=1 );
2924 if( rc!=0 ){
2925 ((unixFile*)id)->lastErrno = errno;
2926 return SQLITE_IOERR_FSTAT;
2927 }
2928 *pSize = buf.st_size;
2929
2930 /* When opening a zero-size database, the findLockInfo() procedure
2931 ** writes a single byte into that file in order to work around a bug
2932 ** in the OS-X msdos filesystem. In order to avoid problems with upper
2933 ** layers, we need to report this file size as zero even though it is
2934 ** really 1. Ticket #3260.
2935 */
2936 if( *pSize==1 ) *pSize = 0;
2937
2938
2939 return SQLITE_OK;
2940}
2941
drh715ff302008-12-03 22:32:44 +00002942/*
2943** Handler for proxy-locking file-control verbs. Defined below in the
2944** proxying locking division.
2945*/
2946static int proxyFileControl(sqlite3_file*,int,void*);
2947
danielk1977ad94b582007-08-20 06:44:22 +00002948
danielk1977e3026632004-06-22 11:29:02 +00002949/*
drh9e33c2c2007-08-31 18:34:59 +00002950** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00002951*/
drhcc6bb3e2007-08-31 16:11:35 +00002952static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00002953 switch( op ){
2954 case SQLITE_FCNTL_LOCKSTATE: {
2955 *(int*)pArg = ((unixFile*)id)->locktype;
2956 return SQLITE_OK;
2957 }
drh7708e972008-11-29 00:56:52 +00002958 case SQLITE_LAST_ERRNO: {
2959 *(int*)pArg = ((unixFile*)id)->lastErrno;
2960 return SQLITE_OK;
2961 }
2962#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
drh715ff302008-12-03 22:32:44 +00002963 case SQLITE_SET_LOCKPROXYFILE:
aswiftaebf4132008-11-21 00:10:35 +00002964 case SQLITE_GET_LOCKPROXYFILE: {
drh715ff302008-12-03 22:32:44 +00002965 return proxyFileControl(id,op,pArg);
drh7708e972008-11-29 00:56:52 +00002966 }
drh6b9d6dd2008-12-03 19:34:47 +00002967#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__) */
drh9e33c2c2007-08-31 18:34:59 +00002968 }
drhcc6bb3e2007-08-31 16:11:35 +00002969 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00002970}
2971
2972/*
danielk1977a3d4c882007-03-23 10:08:38 +00002973** Return the sector size in bytes of the underlying block device for
2974** the specified file. This is almost always 512 bytes, but may be
2975** larger for some devices.
2976**
2977** SQLite code assumes this function cannot fail. It also assumes that
2978** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00002979** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00002980** same for both.
2981*/
danielk1977397d65f2008-11-19 11:35:39 +00002982static int unixSectorSize(sqlite3_file *NotUsed){
2983 UNUSED_PARAMETER(NotUsed);
drh3ceeb752007-03-29 18:19:52 +00002984 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002985}
2986
danielk197790949c22007-08-17 16:50:38 +00002987/*
danielk1977397d65f2008-11-19 11:35:39 +00002988** Return the device characteristics for the file. This is always 0 for unix.
danielk197790949c22007-08-17 16:50:38 +00002989*/
danielk1977397d65f2008-11-19 11:35:39 +00002990static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
2991 UNUSED_PARAMETER(NotUsed);
danielk197762079062007-08-15 17:08:46 +00002992 return 0;
2993}
2994
drh734c9862008-11-28 15:37:20 +00002995/*
2996** Here ends the implementation of all sqlite3_file methods.
2997**
2998********************** End sqlite3_file Methods *******************************
2999******************************************************************************/
3000
3001/*
drh6b9d6dd2008-12-03 19:34:47 +00003002** This division contains definitions of sqlite3_io_methods objects that
3003** implement various file locking strategies. It also contains definitions
3004** of "finder" functions. A finder-function is used to locate the appropriate
3005** sqlite3_io_methods object for a particular database file. The pAppData
3006** field of the sqlite3_vfs VFS objects are initialized to be pointers to
3007** the correct finder-function for that VFS.
3008**
3009** Most finder functions return a pointer to a fixed sqlite3_io_methods
3010** object. The only interesting finder-function is autolockIoFinder, which
3011** looks at the filesystem type and tries to guess the best locking
3012** strategy from that.
3013**
3014**
drh7708e972008-11-29 00:56:52 +00003015** Each instance of this macro generates two objects:
drh734c9862008-11-28 15:37:20 +00003016**
drh7708e972008-11-29 00:56:52 +00003017** * A constant sqlite3_io_methods object call METHOD that has locking
3018** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
3019**
3020** * An I/O method finder function called FINDER that returns a pointer
3021** to the METHOD object in the previous bullet.
drh734c9862008-11-28 15:37:20 +00003022*/
drh7708e972008-11-29 00:56:52 +00003023#define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK) \
3024static const sqlite3_io_methods METHOD = { \
3025 1, /* iVersion */ \
3026 CLOSE, /* xClose */ \
3027 unixRead, /* xRead */ \
3028 unixWrite, /* xWrite */ \
3029 unixTruncate, /* xTruncate */ \
3030 unixSync, /* xSync */ \
3031 unixFileSize, /* xFileSize */ \
3032 LOCK, /* xLock */ \
3033 UNLOCK, /* xUnlock */ \
3034 CKLOCK, /* xCheckReservedLock */ \
3035 unixFileControl, /* xFileControl */ \
3036 unixSectorSize, /* xSectorSize */ \
3037 unixDeviceCharacteristics /* xDeviceCapabilities */ \
3038}; \
3039static const sqlite3_io_methods *FINDER(const char *z, int h){ \
3040 UNUSED_PARAMETER(z); UNUSED_PARAMETER(h); \
3041 return &METHOD; \
aswiftaebf4132008-11-21 00:10:35 +00003042}
drh7708e972008-11-29 00:56:52 +00003043
3044/*
3045** Here are all of the sqlite3_io_methods objects for each of the
3046** locking strategies. Functions that return pointers to these methods
3047** are also created.
3048*/
3049IOMETHODS(
3050 posixIoFinder, /* Finder function name */
3051 posixIoMethods, /* sqlite3_io_methods object name */
3052 unixClose, /* xClose method */
3053 unixLock, /* xLock method */
3054 unixUnlock, /* xUnlock method */
3055 unixCheckReservedLock /* xCheckReservedLock method */
3056);
3057IOMETHODS(
3058 nolockIoFinder, /* Finder function name */
3059 nolockIoMethods, /* sqlite3_io_methods object name */
3060 nolockClose, /* xClose method */
3061 nolockLock, /* xLock method */
3062 nolockUnlock, /* xUnlock method */
3063 nolockCheckReservedLock /* xCheckReservedLock method */
3064);
3065IOMETHODS(
3066 dotlockIoFinder, /* Finder function name */
3067 dotlockIoMethods, /* sqlite3_io_methods object name */
3068 dotlockClose, /* xClose method */
3069 dotlockLock, /* xLock method */
3070 dotlockUnlock, /* xUnlock method */
3071 dotlockCheckReservedLock /* xCheckReservedLock method */
3072);
3073
3074#if SQLITE_ENABLE_LOCKING_STYLE
3075IOMETHODS(
3076 flockIoFinder, /* Finder function name */
3077 flockIoMethods, /* sqlite3_io_methods object name */
3078 flockClose, /* xClose method */
3079 flockLock, /* xLock method */
3080 flockUnlock, /* xUnlock method */
3081 flockCheckReservedLock /* xCheckReservedLock method */
3082);
3083#endif
3084
drh6c7d5c52008-11-21 20:32:33 +00003085#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00003086IOMETHODS(
3087 semIoFinder, /* Finder function name */
3088 semIoMethods, /* sqlite3_io_methods object name */
3089 semClose, /* xClose method */
3090 semLock, /* xLock method */
3091 semUnlock, /* xUnlock method */
3092 semCheckReservedLock /* xCheckReservedLock method */
3093);
aswiftaebf4132008-11-21 00:10:35 +00003094#endif
drh7708e972008-11-29 00:56:52 +00003095
drh734c9862008-11-28 15:37:20 +00003096#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00003097IOMETHODS(
3098 afpIoFinder, /* Finder function name */
3099 afpIoMethods, /* sqlite3_io_methods object name */
3100 afpClose, /* xClose method */
3101 afpLock, /* xLock method */
3102 afpUnlock, /* xUnlock method */
3103 afpCheckReservedLock /* xCheckReservedLock method */
3104);
drh715ff302008-12-03 22:32:44 +00003105#endif
3106
3107/*
3108** The proxy locking method is a "super-method" in the sense that it
3109** opens secondary file descriptors for the conch and lock files and
3110** it uses proxy, dot-file, AFP, and flock() locking methods on those
3111** secondary files. For this reason, the division that implements
3112** proxy locking is located much further down in the file. But we need
3113** to go ahead and define the sqlite3_io_methods and finder function
3114** for proxy locking here. So we forward declare the I/O methods.
3115*/
3116#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
3117static int proxyClose(sqlite3_file*);
3118static int proxyLock(sqlite3_file*, int);
3119static int proxyUnlock(sqlite3_file*, int);
3120static int proxyCheckReservedLock(sqlite3_file*, int*);
drh7708e972008-11-29 00:56:52 +00003121IOMETHODS(
3122 proxyIoFinder, /* Finder function name */
3123 proxyIoMethods, /* sqlite3_io_methods object name */
3124 proxyClose, /* xClose method */
3125 proxyLock, /* xLock method */
3126 proxyUnlock, /* xUnlock method */
3127 proxyCheckReservedLock /* xCheckReservedLock method */
3128);
aswiftaebf4132008-11-21 00:10:35 +00003129#endif
drh7708e972008-11-29 00:56:52 +00003130
3131
3132#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
3133/*
drh6b9d6dd2008-12-03 19:34:47 +00003134** This "finder" function attempts to determine the best locking strategy
3135** for the database file "filePath". It then returns the sqlite3_io_methods
drh7708e972008-11-29 00:56:52 +00003136** object that implements that strategy.
3137**
3138** This is for MacOSX only.
3139*/
3140static const sqlite3_io_methods *autolockIoFinder(
3141 const char *filePath, /* name of the database file */
3142 int fd /* file descriptor open on the database file */
3143){
3144 static const struct Mapping {
drh6b9d6dd2008-12-03 19:34:47 +00003145 const char *zFilesystem; /* Filesystem type name */
3146 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
drh7708e972008-11-29 00:56:52 +00003147 } aMap[] = {
3148 { "hfs", &posixIoMethods },
3149 { "ufs", &posixIoMethods },
3150 { "afpfs", &afpIoMethods },
3151#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
3152 { "smbfs", &afpIoMethods },
3153#else
3154 { "smbfs", &flockIoMethods },
3155#endif
3156 { "webdav", &nolockIoMethods },
3157 { 0, 0 }
3158 };
3159 int i;
3160 struct statfs fsInfo;
3161 struct flock lockInfo;
3162
3163 if( !filePath ){
drh6b9d6dd2008-12-03 19:34:47 +00003164 /* If filePath==NULL that means we are dealing with a transient file
3165 ** that does not need to be locked. */
drh7708e972008-11-29 00:56:52 +00003166 return &nolockIoMethods;
3167 }
3168 if( statfs(filePath, &fsInfo) != -1 ){
3169 if( fsInfo.f_flags & MNT_RDONLY ){
3170 return &nolockIoMethods;
3171 }
3172 for(i=0; aMap[i].zFilesystem; i++){
3173 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
3174 return aMap[i].pMethods;
3175 }
3176 }
3177 }
3178
3179 /* Default case. Handles, amongst others, "nfs".
3180 ** Test byte-range lock using fcntl(). If the call succeeds,
3181 ** assume that the file-system supports POSIX style locks.
drh734c9862008-11-28 15:37:20 +00003182 */
drh7708e972008-11-29 00:56:52 +00003183 lockInfo.l_len = 1;
3184 lockInfo.l_start = 0;
3185 lockInfo.l_whence = SEEK_SET;
3186 lockInfo.l_type = F_RDLCK;
3187 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
3188 return &posixIoMethods;
3189 }else{
3190 return &dotlockIoMethods;
3191 }
3192}
3193#endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
3194
3195/*
3196** An abstract type for a pointer to a IO method finder function:
3197*/
3198typedef const sqlite3_io_methods *(*finder_type)(const char*,int);
3199
aswiftaebf4132008-11-21 00:10:35 +00003200
drh734c9862008-11-28 15:37:20 +00003201/****************************************************************************
3202**************************** sqlite3_vfs methods ****************************
3203**
3204** This division contains the implementation of methods on the
3205** sqlite3_vfs object.
3206*/
3207
danielk1977a3d4c882007-03-23 10:08:38 +00003208/*
danielk1977e339d652008-06-28 11:23:00 +00003209** Initialize the contents of the unixFile structure pointed to by pId.
danielk1977ad94b582007-08-20 06:44:22 +00003210*/
3211static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00003212 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00003213 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00003214 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00003215 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00003216 const char *zFilename, /* Name of the file being opened */
chw97185482008-11-17 08:05:31 +00003217 int noLock, /* Omit locking if true */
3218 int isDelete /* Delete on close if true */
drhbfe66312006-10-03 17:40:40 +00003219){
drh7708e972008-11-29 00:56:52 +00003220 const sqlite3_io_methods *pLockingStyle;
drhda0e7682008-07-30 15:27:54 +00003221 unixFile *pNew = (unixFile *)pId;
3222 int rc = SQLITE_OK;
3223
danielk197717b90b52008-06-06 11:11:25 +00003224 assert( pNew->pLock==NULL );
3225 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00003226
drh715ff302008-12-03 22:32:44 +00003227 /* Parameter isDelete is only used on vxworks.
3228 ** Express this explicitly here to prevent compiler warnings
3229 ** about unused parameters.
danielk1977a03396a2008-11-19 14:35:46 +00003230 */
drh7708e972008-11-29 00:56:52 +00003231#if !OS_VXWORKS
3232 UNUSED_PARAMETER(isDelete);
3233#endif
danielk1977a03396a2008-11-19 14:35:46 +00003234
drh218c5082008-03-07 00:27:10 +00003235 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00003236 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00003237 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00003238 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00003239
drh6c7d5c52008-11-21 20:32:33 +00003240#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +00003241 pNew->pId = vxworksFindFileId(zFilename);
3242 if( pNew->pId==0 ){
3243 noLock = 1;
3244 rc = SQLITE_NOMEM;
chw97185482008-11-17 08:05:31 +00003245 }
3246#endif
3247
drhda0e7682008-07-30 15:27:54 +00003248 if( noLock ){
drh7708e972008-11-29 00:56:52 +00003249 pLockingStyle = &nolockIoMethods;
drhda0e7682008-07-30 15:27:54 +00003250 }else{
drh7708e972008-11-29 00:56:52 +00003251 pLockingStyle = (*(finder_type)pVfs->pAppData)(zFilename, h);
aswiftaebf4132008-11-21 00:10:35 +00003252#if SQLITE_ENABLE_LOCKING_STYLE
3253 /* Cache zFilename in the locking context (AFP and dotlock override) for
3254 ** proxyLock activation is possible (remote proxy is based on db name)
3255 ** zFilename remains valid until file is closed, to support */
3256 pNew->lockingContext = (void*)zFilename;
3257#endif
drhda0e7682008-07-30 15:27:54 +00003258 }
danielk1977e339d652008-06-28 11:23:00 +00003259
drh7708e972008-11-29 00:56:52 +00003260 if( pLockingStyle == &posixIoMethods ){
3261 unixEnterMutex();
3262 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
3263 unixLeaveMutex();
3264 }
danielk1977e339d652008-06-28 11:23:00 +00003265
drh7708e972008-11-29 00:56:52 +00003266#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
aswiftf0551ee2008-12-03 21:26:19 +00003267 else if( pLockingStyle == &afpIoMethods ){
drh7708e972008-11-29 00:56:52 +00003268 /* AFP locking uses the file path so it needs to be included in
3269 ** the afpLockingContext.
3270 */
3271 afpLockingContext *pCtx;
3272 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
3273 if( pCtx==0 ){
3274 rc = SQLITE_NOMEM;
3275 }else{
3276 /* NB: zFilename exists and remains valid until the file is closed
3277 ** according to requirement F11141. So we do not need to make a
3278 ** copy of the filename. */
3279 pCtx->dbPath = zFilename;
3280 srandomdev();
drh6c7d5c52008-11-21 20:32:33 +00003281 unixEnterMutex();
drh7708e972008-11-29 00:56:52 +00003282 rc = findLockInfo(pNew, NULL, &pNew->pOpen);
3283 unixLeaveMutex();
drhbfe66312006-10-03 17:40:40 +00003284 }
drh7708e972008-11-29 00:56:52 +00003285 }
3286#endif
danielk1977e339d652008-06-28 11:23:00 +00003287
drh40bbb0a2008-09-23 10:23:26 +00003288#if SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00003289 else if( pLockingStyle == &dotlockIoMethods ){
3290 /* Dotfile locking uses the file path so it needs to be included in
3291 ** the dotlockLockingContext
3292 */
3293 char *zLockFile;
3294 int nFilename;
3295 nFilename = strlen(zFilename) + 6;
3296 zLockFile = (char *)sqlite3_malloc(nFilename);
3297 if( zLockFile==0 ){
3298 rc = SQLITE_NOMEM;
3299 }else{
3300 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
danielk1977e339d652008-06-28 11:23:00 +00003301 }
drh7708e972008-11-29 00:56:52 +00003302 pNew->lockingContext = zLockFile;
3303 }
chw97185482008-11-17 08:05:31 +00003304#endif
danielk1977e339d652008-06-28 11:23:00 +00003305
drh6c7d5c52008-11-21 20:32:33 +00003306#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00003307 else if( pLockingStyle == &semIoMethods ){
3308 /* Named semaphore locking uses the file path so it needs to be
3309 ** included in the semLockingContext
3310 */
3311 unixEnterMutex();
3312 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
3313 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
3314 char *zSemName = pNew->pOpen->aSemName;
3315 int n;
3316 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem",
3317 pNew->pId->zCanonicalName);
3318 for( n=0; zSemName[n]; n++ )
3319 if( zSemName[n]=='/' ) zSemName[n] = '_';
3320 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
3321 if( pNew->pOpen->pSem == SEM_FAILED ){
3322 rc = SQLITE_NOMEM;
3323 pNew->pOpen->aSemName[0] = '\0';
chw97185482008-11-17 08:05:31 +00003324 }
chw97185482008-11-17 08:05:31 +00003325 }
drh7708e972008-11-29 00:56:52 +00003326 unixLeaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00003327 }
drh7708e972008-11-29 00:56:52 +00003328#endif
aswift5b1a2562008-08-22 00:22:35 +00003329
3330 pNew->lastErrno = 0;
drh6c7d5c52008-11-21 20:32:33 +00003331#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003332 if( rc!=SQLITE_OK ){
3333 unlink(zFilename);
3334 isDelete = 0;
3335 }
3336 pNew->isDelete = isDelete;
3337#endif
danielk1977e339d652008-06-28 11:23:00 +00003338 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003339 if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */
drhbfe66312006-10-03 17:40:40 +00003340 close(h);
danielk1977e339d652008-06-28 11:23:00 +00003341 }else{
drh7708e972008-11-29 00:56:52 +00003342 pNew->pMethod = pLockingStyle;
danielk1977e339d652008-06-28 11:23:00 +00003343 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00003344 }
danielk1977e339d652008-06-28 11:23:00 +00003345 return rc;
drh054889e2005-11-30 03:20:31 +00003346}
drh9c06c952005-11-26 00:25:00 +00003347
danielk1977ad94b582007-08-20 06:44:22 +00003348/*
3349** Open a file descriptor to the directory containing file zFilename.
3350** If successful, *pFd is set to the opened file descriptor and
3351** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
3352** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
3353** value.
3354**
3355** If SQLITE_OK is returned, the caller is responsible for closing
3356** the file descriptor *pFd using close().
3357*/
danielk1977fee2d252007-08-18 10:59:19 +00003358static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00003359 int ii;
drh777b17a2007-09-20 10:02:54 +00003360 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00003361 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00003362
drh153c62c2007-08-24 03:51:33 +00003363 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00003364 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
3365 if( ii>0 ){
3366 zDirname[ii] = '\0';
3367 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00003368 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003369#ifdef FD_CLOEXEC
3370 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3371#endif
3372 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
3373 }
3374 }
danielk1977fee2d252007-08-18 10:59:19 +00003375 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00003376 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00003377}
3378
danielk1977b4b47412007-08-17 15:53:36 +00003379/*
danielk197717b90b52008-06-06 11:11:25 +00003380** Create a temporary file name in zBuf. zBuf must be allocated
3381** by the calling process and must be big enough to hold at least
3382** pVfs->mxPathname bytes.
3383*/
3384static int getTempname(int nBuf, char *zBuf){
3385 static const char *azDirs[] = {
3386 0,
aswiftaebf4132008-11-21 00:10:35 +00003387 0,
danielk197717b90b52008-06-06 11:11:25 +00003388 "/var/tmp",
3389 "/usr/tmp",
3390 "/tmp",
3391 ".",
3392 };
3393 static const unsigned char zChars[] =
3394 "abcdefghijklmnopqrstuvwxyz"
3395 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3396 "0123456789";
drh41022642008-11-21 00:24:42 +00003397 unsigned int i, j;
danielk197717b90b52008-06-06 11:11:25 +00003398 struct stat buf;
3399 const char *zDir = ".";
3400
3401 /* It's odd to simulate an io-error here, but really this is just
3402 ** using the io-error infrastructure to test that SQLite handles this
3403 ** function failing.
3404 */
3405 SimulateIOError( return SQLITE_IOERR );
3406
3407 azDirs[0] = sqlite3_temp_directory;
aswiftaebf4132008-11-21 00:10:35 +00003408 if (NULL == azDirs[1]) {
3409 azDirs[1] = getenv("TMPDIR");
3410 }
3411
3412 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
danielk197717b90b52008-06-06 11:11:25 +00003413 if( azDirs[i]==0 ) continue;
3414 if( stat(azDirs[i], &buf) ) continue;
3415 if( !S_ISDIR(buf.st_mode) ) continue;
3416 if( access(azDirs[i], 07) ) continue;
3417 zDir = azDirs[i];
3418 break;
3419 }
3420
3421 /* Check that the output buffer is large enough for the temporary file
3422 ** name. If it is not, return SQLITE_ERROR.
3423 */
danielk197700e13612008-11-17 19:18:54 +00003424 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
danielk197717b90b52008-06-06 11:11:25 +00003425 return SQLITE_ERROR;
3426 }
3427
3428 do{
3429 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
3430 j = strlen(zBuf);
3431 sqlite3_randomness(15, &zBuf[j]);
3432 for(i=0; i<15; i++, j++){
3433 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
3434 }
3435 zBuf[j] = 0;
3436 }while( access(zBuf,0)==0 );
3437 return SQLITE_OK;
3438}
3439
drhc66d5b62008-12-03 22:48:32 +00003440/*
3441** Routine to transform a unixFile into a proxy-locking unixFile.
3442** Implementation in the proxy-lock division, but used by unixOpen()
3443** if SQLITE_PREFER_PROXY_LOCKING is defined.
3444*/
3445static int proxyTransformUnixFile(unixFile*, const char*);
3446
danielk197717b90b52008-06-06 11:11:25 +00003447
3448/*
danielk1977ad94b582007-08-20 06:44:22 +00003449** Open the file zPath.
3450**
danielk1977b4b47412007-08-17 15:53:36 +00003451** Previously, the SQLite OS layer used three functions in place of this
3452** one:
3453**
3454** sqlite3OsOpenReadWrite();
3455** sqlite3OsOpenReadOnly();
3456** sqlite3OsOpenExclusive();
3457**
3458** These calls correspond to the following combinations of flags:
3459**
3460** ReadWrite() -> (READWRITE | CREATE)
3461** ReadOnly() -> (READONLY)
3462** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
3463**
3464** The old OpenExclusive() accepted a boolean argument - "delFlag". If
3465** true, the file was configured to be automatically deleted when the
3466** file handle closed. To achieve the same effect using this new
3467** interface, add the DELETEONCLOSE flag to those specified above for
3468** OpenExclusive().
3469*/
3470static int unixOpen(
drh6b9d6dd2008-12-03 19:34:47 +00003471 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
3472 const char *zPath, /* Pathname of file to be opened */
3473 sqlite3_file *pFile, /* The file descriptor to be filled in */
3474 int flags, /* Input flags to control the opening */
3475 int *pOutFlags /* Output flags returned to SQLite core */
danielk1977b4b47412007-08-17 15:53:36 +00003476){
danielk1977fee2d252007-08-18 10:59:19 +00003477 int fd = 0; /* File descriptor returned by open() */
3478 int dirfd = -1; /* Directory file descriptor */
drh6b9d6dd2008-12-03 19:34:47 +00003479 int openFlags = 0; /* Flags to pass to open() */
danielk1977fee2d252007-08-18 10:59:19 +00003480 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00003481 int noLock; /* True to omit locking primitives */
aswiftaebf4132008-11-21 00:10:35 +00003482 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003483
3484 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
3485 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
3486 int isCreate = (flags & SQLITE_OPEN_CREATE);
3487 int isReadonly = (flags & SQLITE_OPEN_READONLY);
3488 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3489
danielk1977fee2d252007-08-18 10:59:19 +00003490 /* If creating a master or main-file journal, this function will open
3491 ** a file-descriptor on the directory too. The first time unixSync()
3492 ** is called the directory file descriptor will be fsync()ed and close()d.
3493 */
3494 int isOpenDirectory = (isCreate &&
3495 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
3496 );
3497
danielk197717b90b52008-06-06 11:11:25 +00003498 /* If argument zPath is a NULL pointer, this function is required to open
3499 ** a temporary file. Use this buffer to store the file name in.
3500 */
3501 char zTmpname[MAX_PATHNAME+1];
3502 const char *zName = zPath;
3503
danielk1977fee2d252007-08-18 10:59:19 +00003504 /* Check the following statements are true:
3505 **
3506 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
3507 ** (b) if CREATE is set, then READWRITE must also be set, and
3508 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00003509 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00003510 */
danielk1977b4b47412007-08-17 15:53:36 +00003511 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00003512 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00003513 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00003514 assert(isDelete==0 || isCreate);
3515
drh33f4e022007-09-03 15:19:34 +00003516 /* The main DB, main journal, and master journal are never automatically
3517 ** deleted
3518 */
3519 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
3520 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
3521 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00003522
danielk1977fee2d252007-08-18 10:59:19 +00003523 /* Assert that the upper layer has set one of the "file-type" flags. */
3524 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
3525 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
3526 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00003527 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00003528 );
3529
danielk1977e339d652008-06-28 11:23:00 +00003530 memset(pFile, 0, sizeof(unixFile));
3531
danielk197717b90b52008-06-06 11:11:25 +00003532 if( !zName ){
danielk197717b90b52008-06-06 11:11:25 +00003533 assert(isDelete && !isOpenDirectory);
3534 rc = getTempname(MAX_PATHNAME+1, zTmpname);
3535 if( rc!=SQLITE_OK ){
3536 return rc;
3537 }
3538 zName = zTmpname;
3539 }
3540
drh734c9862008-11-28 15:37:20 +00003541 if( isReadonly ) openFlags |= O_RDONLY;
3542 if( isReadWrite ) openFlags |= O_RDWR;
3543 if( isCreate ) openFlags |= O_CREAT;
3544 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
3545 openFlags |= (O_LARGEFILE|O_BINARY);
danielk1977b4b47412007-08-17 15:53:36 +00003546
drh734c9862008-11-28 15:37:20 +00003547 fd = open(zName, openFlags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
3548 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, openFlags);
danielk19772f2d8c72007-08-30 16:13:33 +00003549 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00003550 /* Failed to open the file for read/write access. Try read-only. */
3551 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
3552 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00003553 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00003554 }
3555 if( fd<0 ){
3556 return SQLITE_CANTOPEN;
3557 }
3558 if( isDelete ){
drh6c7d5c52008-11-21 20:32:33 +00003559#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003560 zPath = zName;
3561#else
danielk197717b90b52008-06-06 11:11:25 +00003562 unlink(zName);
chw97185482008-11-17 08:05:31 +00003563#endif
danielk1977b4b47412007-08-17 15:53:36 +00003564 }
drh41022642008-11-21 00:24:42 +00003565#if SQLITE_ENABLE_LOCKING_STYLE
3566 else{
drh734c9862008-11-28 15:37:20 +00003567 ((unixFile*)pFile)->openFlags = openFlags;
drh41022642008-11-21 00:24:42 +00003568 }
3569#endif
danielk1977b4b47412007-08-17 15:53:36 +00003570 if( pOutFlags ){
3571 *pOutFlags = flags;
3572 }
3573
3574 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00003575 if( isOpenDirectory ){
aswiftaebf4132008-11-21 00:10:35 +00003576 rc = openDirectory(zPath, &dirfd);
danielk1977fee2d252007-08-18 10:59:19 +00003577 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00003578 close(fd); /* silently leak if fail, already in error */
danielk1977fee2d252007-08-18 10:59:19 +00003579 return rc;
3580 }
3581 }
danielk1977e339d652008-06-28 11:23:00 +00003582
3583#ifdef FD_CLOEXEC
3584 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3585#endif
3586
drhda0e7682008-07-30 15:27:54 +00003587 noLock = eType!=SQLITE_OPEN_MAIN_DB;
aswiftaebf4132008-11-21 00:10:35 +00003588
3589#if SQLITE_PREFER_PROXY_LOCKING
3590 if( zPath!=NULL && !noLock ){
3591 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
3592 int useProxy = 0;
3593
3594 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy,
drh7708e972008-11-29 00:56:52 +00003595 ** 0 means never use proxy, NULL means use proxy for non-local files only
3596 */
aswiftaebf4132008-11-21 00:10:35 +00003597 if( envforce!=NULL ){
3598 useProxy = atoi(envforce)>0;
3599 }else{
3600 struct statfs fsInfo;
3601
3602 if( statfs(zPath, &fsInfo) == -1 ){
3603 ((unixFile*)pFile)->lastErrno = errno;
3604 if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */
3605 close(fd); /* silently leak if fail, in error */
3606 return SQLITE_IOERR_ACCESS;
3607 }
3608 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
3609 }
3610 if( useProxy ){
3611 rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
3612 if( rc==SQLITE_OK ){
drh715ff302008-12-03 22:32:44 +00003613 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
aswiftaebf4132008-11-21 00:10:35 +00003614 }
3615 return rc;
3616 }
3617 }
3618#endif
3619
chw97185482008-11-17 08:05:31 +00003620 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
danielk1977b4b47412007-08-17 15:53:36 +00003621}
3622
3623/*
danielk1977fee2d252007-08-18 10:59:19 +00003624** Delete the file at zPath. If the dirSync argument is true, fsync()
3625** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00003626*/
drh6b9d6dd2008-12-03 19:34:47 +00003627static int unixDelete(
3628 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
3629 const char *zPath, /* Name of file to be deleted */
3630 int dirSync /* If true, fsync() directory after deleting file */
3631){
danielk1977fee2d252007-08-18 10:59:19 +00003632 int rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00003633 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003634 SimulateIOError(return SQLITE_IOERR_DELETE);
3635 unlink(zPath);
danielk1977d39fa702008-10-16 13:27:40 +00003636#ifndef SQLITE_DISABLE_DIRSYNC
danielk1977fee2d252007-08-18 10:59:19 +00003637 if( dirSync ){
3638 int fd;
3639 rc = openDirectory(zPath, &fd);
3640 if( rc==SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00003641#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003642 if( fsync(fd)==-1 )
3643#else
3644 if( fsync(fd) )
3645#endif
3646 {
danielk1977fee2d252007-08-18 10:59:19 +00003647 rc = SQLITE_IOERR_DIR_FSYNC;
3648 }
aswiftaebf4132008-11-21 00:10:35 +00003649 if( close(fd)&&!rc ){
3650 rc = SQLITE_IOERR_DIR_CLOSE;
3651 }
danielk1977fee2d252007-08-18 10:59:19 +00003652 }
3653 }
danielk1977d138dd82008-10-15 16:02:48 +00003654#endif
danielk1977fee2d252007-08-18 10:59:19 +00003655 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00003656}
3657
danielk197790949c22007-08-17 16:50:38 +00003658/*
3659** Test the existance of or access permissions of file zPath. The
3660** test performed depends on the value of flags:
3661**
3662** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
3663** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
3664** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
3665**
3666** Otherwise return 0.
3667*/
danielk1977861f7452008-06-05 11:39:11 +00003668static int unixAccess(
drh6b9d6dd2008-12-03 19:34:47 +00003669 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
3670 const char *zPath, /* Path of the file to examine */
3671 int flags, /* What do we want to learn about the zPath file? */
3672 int *pResOut /* Write result boolean here */
danielk1977861f7452008-06-05 11:39:11 +00003673){
rse25c0d1a2007-09-20 08:38:14 +00003674 int amode = 0;
danielk1977397d65f2008-11-19 11:35:39 +00003675 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00003676 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00003677 switch( flags ){
3678 case SQLITE_ACCESS_EXISTS:
3679 amode = F_OK;
3680 break;
3681 case SQLITE_ACCESS_READWRITE:
3682 amode = W_OK|R_OK;
3683 break;
drh50d3f902007-08-27 21:10:36 +00003684 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00003685 amode = R_OK;
3686 break;
3687
3688 default:
3689 assert(!"Invalid flags argument");
3690 }
danielk1977861f7452008-06-05 11:39:11 +00003691 *pResOut = (access(zPath, amode)==0);
3692 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003693}
3694
danielk1977b4b47412007-08-17 15:53:36 +00003695
3696/*
3697** Turn a relative pathname into a full pathname. The relative path
3698** is stored as a nul-terminated string in the buffer pointed to by
3699** zPath.
3700**
3701** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
3702** (in this case, MAX_PATHNAME bytes). The full-path is written to
3703** this buffer before returning.
3704*/
danielk1977adfb9b02007-09-17 07:02:56 +00003705static int unixFullPathname(
3706 sqlite3_vfs *pVfs, /* Pointer to vfs object */
3707 const char *zPath, /* Possibly relative input path */
3708 int nOut, /* Size of output buffer in bytes */
3709 char *zOut /* Output buffer */
3710){
danielk1977843e65f2007-09-01 16:16:15 +00003711
3712 /* It's odd to simulate an io-error here, but really this is just
3713 ** using the io-error infrastructure to test that SQLite handles this
3714 ** function failing. This function could fail if, for example, the
drh6b9d6dd2008-12-03 19:34:47 +00003715 ** current working directory has been unlinked.
danielk1977843e65f2007-09-01 16:16:15 +00003716 */
3717 SimulateIOError( return SQLITE_ERROR );
3718
drh153c62c2007-08-24 03:51:33 +00003719 assert( pVfs->mxPathname==MAX_PATHNAME );
danielk1977f3d3c272008-11-19 16:52:44 +00003720 UNUSED_PARAMETER(pVfs);
chw97185482008-11-17 08:05:31 +00003721
drh3c7f2dc2007-12-06 13:26:20 +00003722 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00003723 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00003724 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003725 }else{
3726 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00003727 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00003728 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00003729 }
3730 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00003731 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00003732 }
3733 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00003734}
3735
drh0ccebe72005-06-07 22:22:50 +00003736
drh761df872006-12-21 01:29:22 +00003737#ifndef SQLITE_OMIT_LOAD_EXTENSION
3738/*
3739** Interfaces for opening a shared library, finding entry points
3740** within the shared library, and closing the shared library.
3741*/
3742#include <dlfcn.h>
danielk1977397d65f2008-11-19 11:35:39 +00003743static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
3744 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00003745 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
3746}
danielk197795c8a542007-09-01 06:51:27 +00003747
3748/*
3749** SQLite calls this function immediately after a call to unixDlSym() or
3750** unixDlOpen() fails (returns a null pointer). If a more detailed error
3751** message is available, it is written to zBufOut. If no error message
3752** is available, zBufOut is left unmodified and SQLite uses a default
3753** error message.
3754*/
danielk1977397d65f2008-11-19 11:35:39 +00003755static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00003756 char *zErr;
danielk1977397d65f2008-11-19 11:35:39 +00003757 UNUSED_PARAMETER(NotUsed);
drh6c7d5c52008-11-21 20:32:33 +00003758 unixEnterMutex();
danielk1977b4b47412007-08-17 15:53:36 +00003759 zErr = dlerror();
3760 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00003761 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00003762 }
drh6c7d5c52008-11-21 20:32:33 +00003763 unixLeaveMutex();
danielk1977b4b47412007-08-17 15:53:36 +00003764}
danielk1977397d65f2008-11-19 11:35:39 +00003765static void *unixDlSym(sqlite3_vfs *NotUsed, void *pHandle, const char*zSymbol){
3766 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00003767 return dlsym(pHandle, zSymbol);
3768}
danielk1977397d65f2008-11-19 11:35:39 +00003769static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
3770 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00003771 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00003772}
danielk1977b4b47412007-08-17 15:53:36 +00003773#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
3774 #define unixDlOpen 0
3775 #define unixDlError 0
3776 #define unixDlSym 0
3777 #define unixDlClose 0
3778#endif
3779
3780/*
danielk197790949c22007-08-17 16:50:38 +00003781** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00003782*/
danielk1977397d65f2008-11-19 11:35:39 +00003783static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
3784 UNUSED_PARAMETER(NotUsed);
danielk197700e13612008-11-17 19:18:54 +00003785 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
danielk197790949c22007-08-17 16:50:38 +00003786
drhbbd42a62004-05-22 17:41:58 +00003787 /* We have to initialize zBuf to prevent valgrind from reporting
3788 ** errors. The reports issued by valgrind are incorrect - we would
3789 ** prefer that the randomness be increased by making use of the
3790 ** uninitialized space in zBuf - but valgrind errors tend to worry
3791 ** some users. Rather than argue, it seems easier just to initialize
3792 ** the whole array and silence valgrind, even if that means less randomness
3793 ** in the random seed.
3794 **
3795 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00003796 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00003797 ** tests repeatable.
3798 */
danielk1977b4b47412007-08-17 15:53:36 +00003799 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00003800#if !defined(SQLITE_TEST)
3801 {
drh842b8642005-01-21 17:53:17 +00003802 int pid, fd;
3803 fd = open("/dev/urandom", O_RDONLY);
3804 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00003805 time_t t;
3806 time(&t);
danielk197790949c22007-08-17 16:50:38 +00003807 memcpy(zBuf, &t, sizeof(t));
3808 pid = getpid();
3809 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
danielk197700e13612008-11-17 19:18:54 +00003810 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
drh72cbd072008-10-14 17:58:38 +00003811 nBuf = sizeof(t) + sizeof(pid);
drh842b8642005-01-21 17:53:17 +00003812 }else{
drh72cbd072008-10-14 17:58:38 +00003813 nBuf = read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00003814 close(fd);
3815 }
drhbbd42a62004-05-22 17:41:58 +00003816 }
3817#endif
drh72cbd072008-10-14 17:58:38 +00003818 return nBuf;
drhbbd42a62004-05-22 17:41:58 +00003819}
3820
danielk1977b4b47412007-08-17 15:53:36 +00003821
drhbbd42a62004-05-22 17:41:58 +00003822/*
3823** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00003824** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00003825** The return value is the number of microseconds of sleep actually
3826** requested from the underlying operating system, a number which
3827** might be greater than or equal to the argument, but not less
3828** than the argument.
drhbbd42a62004-05-22 17:41:58 +00003829*/
danielk1977397d65f2008-11-19 11:35:39 +00003830static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
drh6c7d5c52008-11-21 20:32:33 +00003831#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003832 struct timespec sp;
3833
3834 sp.tv_sec = microseconds / 1000000;
3835 sp.tv_nsec = (microseconds % 1000000) * 1000;
3836 nanosleep(&sp, NULL);
danielk1977397d65f2008-11-19 11:35:39 +00003837 return microseconds;
3838#elif defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00003839 usleep(microseconds);
3840 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00003841#else
danielk1977b4b47412007-08-17 15:53:36 +00003842 int seconds = (microseconds+999999)/1000000;
3843 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00003844 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00003845#endif
danielk1977397d65f2008-11-19 11:35:39 +00003846 UNUSED_PARAMETER(NotUsed);
drh88f474a2006-01-02 20:00:12 +00003847}
3848
3849/*
drh6b9d6dd2008-12-03 19:34:47 +00003850** The following variable, if set to a non-zero value, is interpreted as
3851** the number of seconds since 1970 and is used to set the result of
3852** sqlite3OsCurrentTime() during testing.
drhbbd42a62004-05-22 17:41:58 +00003853*/
3854#ifdef SQLITE_TEST
drh6b9d6dd2008-12-03 19:34:47 +00003855int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
drhbbd42a62004-05-22 17:41:58 +00003856#endif
3857
3858/*
3859** Find the current time (in Universal Coordinated Time). Write the
3860** current time and date as a Julian Day number into *prNow and
3861** return 0. Return 1 if the time and date cannot be found.
3862*/
danielk1977397d65f2008-11-19 11:35:39 +00003863static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
drh6c7d5c52008-11-21 20:32:33 +00003864#if defined(NO_GETTOD)
drhbbd42a62004-05-22 17:41:58 +00003865 time_t t;
3866 time(&t);
3867 *prNow = t/86400.0 + 2440587.5;
drh6c7d5c52008-11-21 20:32:33 +00003868#elif OS_VXWORKS
3869 struct timespec sNow;
3870 clock_gettime(CLOCK_REALTIME, &sNow);
3871 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
drh19e2d372005-08-29 23:00:03 +00003872#else
3873 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00003874 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00003875 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
3876#endif
danielk1977397d65f2008-11-19 11:35:39 +00003877
drhbbd42a62004-05-22 17:41:58 +00003878#ifdef SQLITE_TEST
3879 if( sqlite3_current_time ){
3880 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
3881 }
3882#endif
danielk1977397d65f2008-11-19 11:35:39 +00003883 UNUSED_PARAMETER(NotUsed);
drhbbd42a62004-05-22 17:41:58 +00003884 return 0;
3885}
danielk1977b4b47412007-08-17 15:53:36 +00003886
drh6b9d6dd2008-12-03 19:34:47 +00003887/*
3888** We added the xGetLastError() method with the intention of providing
3889** better low-level error messages when operating-system problems come up
3890** during SQLite operation. But so far, none of that has been implemented
3891** in the core. So this routine is never called. For now, it is merely
3892** a place-holder.
3893*/
danielk1977397d65f2008-11-19 11:35:39 +00003894static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
3895 UNUSED_PARAMETER(NotUsed);
3896 UNUSED_PARAMETER(NotUsed2);
3897 UNUSED_PARAMETER(NotUsed3);
danielk1977bcb97fe2008-06-06 15:49:29 +00003898 return 0;
3899}
3900
drh153c62c2007-08-24 03:51:33 +00003901/*
drh734c9862008-11-28 15:37:20 +00003902************************ End of sqlite3_vfs methods ***************************
3903******************************************************************************/
3904
drh715ff302008-12-03 22:32:44 +00003905/******************************************************************************
3906************************** Begin Proxy Locking ********************************
3907**
3908** Proxy locking is a "uber-locking-method" in this sense: It uses the
3909** other locking methods on secondary lock files. Proxy locking is a
3910** meta-layer over top of the primitive locking implemented above. For
3911** this reason, the division that implements of proxy locking is deferred
3912** until late in the file (here) after all of the other I/O methods have
3913** been defined - so that the primitive locking methods are available
3914** as services to help with the implementation of proxy locking.
3915**
3916****
3917**
3918** The default locking schemes in SQLite use byte-range locks on the
3919** database file to coordinate safe, concurrent access by multiple readers
3920** and writers [http://sqlite.org/lockingv3.html]. The five file locking
3921** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
3922** as POSIX read & write locks over fixed set of locations (via fsctl),
3923** on AFP and SMB only exclusive byte-range locks are available via fsctl
3924** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
3925** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
3926** address in the shared range is taken for a SHARED lock, the entire
3927** shared range is taken for an EXCLUSIVE lock):
3928**
3929** PENDING_BYTE 0x40000000
3930** RESERVED_BYTE 0x40000001
3931** SHARED_RANGE 0x40000002 -> 0x40000200
3932**
3933** This works well on the local file system, but shows a nearly 100x
3934** slowdown in read performance on AFP because the AFP client disables
3935** the read cache when byte-range locks are present. Enabling the read
3936** cache exposes a cache coherency problem that is present on all OS X
3937** supported network file systems. NFS and AFP both observe the
3938** close-to-open semantics for ensuring cache coherency
3939** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
3940** address the requirements for concurrent database access by multiple
3941** readers and writers
3942** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
3943**
3944** To address the performance and cache coherency issues, proxy file locking
3945** changes the way database access is controlled by limiting access to a
3946** single host at a time and moving file locks off of the database file
3947** and onto a proxy file on the local file system.
3948**
3949**
3950** Using proxy locks
3951** -----------------
3952**
3953** C APIs
3954**
3955** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
3956** <proxy_path> | ":auto:");
3957** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
3958**
3959**
3960** SQL pragmas
3961**
3962** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
3963** PRAGMA [database.]lock_proxy_file
3964**
3965** Specifying ":auto:" means that if there is a conch file with a matching
3966** host ID in it, the proxy path in the conch file will be used, otherwise
3967** a proxy path based on the user's temp dir
3968** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
3969** actual proxy file name is generated from the name and path of the
3970** database file. For example:
3971**
3972** For database path "/Users/me/foo.db"
3973** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
3974**
3975** Once a lock proxy is configured for a database connection, it can not
3976** be removed, however it may be switched to a different proxy path via
3977** the above APIs (assuming the conch file is not being held by another
3978** connection or process).
3979**
3980**
3981** How proxy locking works
3982** -----------------------
3983**
3984** Proxy file locking relies primarily on two new supporting files:
3985**
3986** * conch file to limit access to the database file to a single host
3987** at a time
3988**
3989** * proxy file to act as a proxy for the advisory locks normally
3990** taken on the database
3991**
3992** The conch file - to use a proxy file, sqlite must first "hold the conch"
3993** by taking an sqlite-style shared lock on the conch file, reading the
3994** contents and comparing the host's unique host ID (see below) and lock
3995** proxy path against the values stored in the conch. The conch file is
3996** stored in the same directory as the database file and the file name
3997** is patterned after the database file name as ".<databasename>-conch".
3998** If the conch file does not exist, or it's contents do not match the
3999** host ID and/or proxy path, then the lock is escalated to an exclusive
4000** lock and the conch file contents is updated with the host ID and proxy
4001** path and the lock is downgraded to a shared lock again. If the conch
4002** is held by another process (with a shared lock), the exclusive lock
4003** will fail and SQLITE_BUSY is returned.
4004**
4005** The proxy file - a single-byte file used for all advisory file locks
4006** normally taken on the database file. This allows for safe sharing
4007** of the database file for multiple readers and writers on the same
4008** host (the conch ensures that they all use the same local lock file).
4009**
4010** There is a third file - the host ID file - used as a persistent record
4011** of a unique identifier for the host, a 128-byte unique host id file
4012** in the path defined by the HOSTIDPATH macro (default value is
4013** /Library/Caches/.com.apple.sqliteConchHostId).
4014**
4015** Requesting the lock proxy does not immediately take the conch, it is
4016** only taken when the first request to lock database file is made.
4017** This matches the semantics of the traditional locking behavior, where
4018** opening a connection to a database file does not take a lock on it.
4019** The shared lock and an open file descriptor are maintained until
4020** the connection to the database is closed.
4021**
4022** The proxy file and the lock file are never deleted so they only need
4023** to be created the first time they are used.
4024**
4025** Configuration options
4026** ---------------------
4027**
4028** SQLITE_PREFER_PROXY_LOCKING
4029**
4030** Database files accessed on non-local file systems are
4031** automatically configured for proxy locking, lock files are
4032** named automatically using the same logic as
4033** PRAGMA lock_proxy_file=":auto:"
4034**
4035** SQLITE_PROXY_DEBUG
4036**
4037** Enables the logging of error messages during host id file
4038** retrieval and creation
4039**
4040** HOSTIDPATH
4041**
4042** Overrides the default host ID file path location
4043**
4044** LOCKPROXYDIR
4045**
4046** Overrides the default directory used for lock proxy files that
4047** are named automatically via the ":auto:" setting
4048**
4049** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
4050**
4051** Permissions to use when creating a directory for storing the
4052** lock proxy files, only used when LOCKPROXYDIR is not set.
4053**
4054**
4055** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
4056** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
4057** force proxy locking to be used for every database file opened, and 0
4058** will force automatic proxy locking to be disabled for all database
4059** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
4060** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
4061*/
4062
4063/*
4064** Proxy locking is only available on MacOSX
4065*/
4066#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
4067
4068#ifdef SQLITE_TEST
4069/* simulate multiple hosts by creating unique hostid file paths */
4070int sqlite3_hostid_num = 0;
4071#endif
4072
4073/*
4074** The proxyLockingContext has the path and file structures for the remote
4075** and local proxy files in it
4076*/
4077typedef struct proxyLockingContext proxyLockingContext;
4078struct proxyLockingContext {
4079 unixFile *conchFile; /* Open conch file */
4080 char *conchFilePath; /* Name of the conch file */
4081 unixFile *lockProxy; /* Open proxy lock file */
4082 char *lockProxyPath; /* Name of the proxy lock file */
4083 char *dbPath; /* Name of the open file */
4084 int conchHeld; /* True if the conch is currently held */
4085 void *oldLockingContext; /* Original lockingcontext to restore on close */
4086 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
4087};
4088
4089/* HOSTIDLEN and CONCHLEN both include space for the string
4090** terminating nul
4091*/
4092#define HOSTIDLEN 128
4093#define CONCHLEN (MAXPATHLEN+HOSTIDLEN+1)
4094#ifndef HOSTIDPATH
4095# define HOSTIDPATH "/Library/Caches/.com.apple.sqliteConchHostId"
4096#endif
4097
4098/* basically a copy of unixRandomness with different
4099** test behavior built in */
4100static int proxyGenerateHostID(char *pHostID){
4101 int pid, fd, len;
4102 unsigned char *key = (unsigned char *)pHostID;
4103
4104 memset(key, 0, HOSTIDLEN);
4105 len = 0;
4106 fd = open("/dev/urandom", O_RDONLY);
4107 if( fd>=0 ){
4108 len = read(fd, key, HOSTIDLEN);
4109 close(fd); /* silently leak the fd if it fails */
4110 }
4111 if( len < HOSTIDLEN ){
4112 time_t t;
4113 time(&t);
4114 memcpy(key, &t, sizeof(t));
4115 pid = getpid();
4116 memcpy(&key[sizeof(t)], &pid, sizeof(pid));
4117 }
4118
4119#ifdef MAKE_PRETTY_HOSTID
4120 {
4121 int i;
4122 /* filter the bytes into printable ascii characters and NUL terminate */
4123 key[(HOSTIDLEN-1)] = 0x00;
4124 for( i=0; i<(HOSTIDLEN-1); i++ ){
4125 unsigned char pa = key[i]&0x7F;
4126 if( pa<0x20 ){
4127 key[i] = (key[i]&0x80 == 0x80) ? pa+0x40 : pa+0x20;
4128 }else if( pa==0x7F ){
4129 key[i] = (key[i]&0x80 == 0x80) ? pa=0x20 : pa+0x7E;
4130 }
4131 }
4132 }
4133#endif
4134 return SQLITE_OK;
4135}
4136
4137/* writes the host id path to path, path should be an pre-allocated buffer
4138** with enough space for a path
4139*/
4140static void proxyGetHostIDPath(char *path, size_t len){
4141 strlcpy(path, HOSTIDPATH, len);
4142#ifdef SQLITE_TEST
4143 if( sqlite3_hostid_num>0 ){
4144 char suffix[2] = "1";
4145 suffix[0] = suffix[0] + sqlite3_hostid_num;
4146 strlcat(path, suffix, len);
4147 }
4148#endif
4149 OSTRACE3("GETHOSTIDPATH %s pid=%d\n", path, getpid());
4150}
4151
4152/* get the host ID from a sqlite hostid file stored in the
4153** user-specific tmp directory, create the ID if it's not there already
4154*/
4155static int proxyGetHostID(char *pHostID, int *pError){
4156 int fd;
4157 char path[MAXPATHLEN];
4158 size_t len;
4159 int rc=SQLITE_OK;
4160
4161 proxyGetHostIDPath(path, MAXPATHLEN);
4162 /* try to create the host ID file, if it already exists read the contents */
4163 fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644);
4164 if( fd<0 ){
4165 int err=errno;
4166
4167 if( err!=EEXIST ){
4168#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
4169 fprintf(stderr, "sqlite error creating host ID file %s: %s\n",
4170 path, strerror(err));
4171#endif
4172 return SQLITE_PERM;
4173 }
4174 /* couldn't create the file, read it instead */
4175 fd = open(path, O_RDONLY|O_EXCL);
4176 if( fd<0 ){
4177#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
4178 int err = errno;
4179 fprintf(stderr, "sqlite error opening host ID file %s: %s\n",
4180 path, strerror(err));
4181#endif
4182 return SQLITE_PERM;
4183 }
4184 len = pread(fd, pHostID, HOSTIDLEN, 0);
4185 if( len<0 ){
4186 *pError = errno;
4187 rc = SQLITE_IOERR_READ;
4188 }else if( len<HOSTIDLEN ){
4189 *pError = 0;
4190 rc = SQLITE_IOERR_SHORT_READ;
4191 }
4192 close(fd); /* silently leak the fd if it fails */
4193 OSTRACE3("GETHOSTID read %s pid=%d\n", pHostID, getpid());
4194 return rc;
4195 }else{
4196 /* we're creating the host ID file (use a random string of bytes) */
4197 proxyGenerateHostID(pHostID);
4198 len = pwrite(fd, pHostID, HOSTIDLEN, 0);
4199 if( len<0 ){
4200 *pError = errno;
4201 rc = SQLITE_IOERR_WRITE;
4202 }else if( len<HOSTIDLEN ){
4203 *pError = 0;
4204 rc = SQLITE_IOERR_WRITE;
4205 }
4206 close(fd); /* silently leak the fd if it fails */
4207 OSTRACE3("GETHOSTID wrote %s pid=%d\n", pHostID, getpid());
4208 return rc;
4209 }
4210}
4211
4212static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
4213 int len;
4214 int dbLen;
4215 int i;
4216
4217#ifdef LOCKPROXYDIR
4218 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
4219#else
4220# ifdef _CS_DARWIN_USER_TEMP_DIR
4221 {
4222 confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen);
4223 len = strlcat(lPath, "sqliteplocks", maxLen);
4224 if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
4225 /* if mkdir fails, handle as lock file creation failure */
4226 int err = errno;
4227# ifdef SQLITE_DEBUG
4228 if( err!=EEXIST ){
4229 fprintf(stderr, "proxyGetLockPath: mkdir(%s,0%o) error %d %s\n", lPath,
4230 SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err));
4231 }
4232# endif
4233 }else{
4234 OSTRACE3("GETLOCKPATH mkdir %s pid=%d\n", lPath, getpid());
4235 }
4236
4237 }
4238# else
4239 len = strlcpy(lPath, "/tmp/", maxLen);
4240# endif
4241#endif
4242
4243 if( lPath[len-1]!='/' ){
4244 len = strlcat(lPath, "/", maxLen);
4245 }
4246
4247 /* transform the db path to a unique cache name */
4248 dbLen = strlen(dbPath);
4249 for( i=0; i<dbLen && (i+len+7)<maxLen; i++){
4250 char c = dbPath[i];
4251 lPath[i+len] = (c=='/')?'_':c;
4252 }
4253 lPath[i+len]='\0';
4254 strlcat(lPath, ":auto:", maxLen);
4255 return SQLITE_OK;
4256}
4257
4258/*
4259** Create a new VFS file descriptor (stored in memory obtained from
4260** sqlite3_malloc) and open the file named "path" in the file descriptor.
4261**
4262** The caller is responsible not only for closing the file descriptor
4263** but also for freeing the memory associated with the file descriptor.
4264*/
4265static int proxyCreateUnixFile(const char *path, unixFile **ppFile) {
4266 int fd;
4267 int dirfd = -1;
4268 unixFile *pNew;
4269 int rc = SQLITE_OK;
4270 sqlite3_vfs dummyVfs;
4271
4272 fd = open(path, O_RDWR | O_CREAT, SQLITE_DEFAULT_FILE_PERMISSIONS);
4273 if( fd<0 ){
4274 return SQLITE_CANTOPEN;
4275 }
4276
4277 pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile));
4278 if( pNew==NULL ){
4279 rc = SQLITE_NOMEM;
4280 goto end_create_proxy;
4281 }
4282 memset(pNew, 0, sizeof(unixFile));
4283
4284 dummyVfs.pAppData = (void*)autolockIoFinder;
4285 rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0);
4286 if( rc==SQLITE_OK ){
4287 *ppFile = pNew;
4288 return SQLITE_OK;
4289 }
4290end_create_proxy:
4291 close(fd); /* silently leak fd if error, we're already in error */
4292 sqlite3_free(pNew);
4293 return rc;
4294}
4295
4296/* takes the conch by taking a shared lock and read the contents conch, if
4297** lockPath is non-NULL, the host ID and lock file path must match. A NULL
4298** lockPath means that the lockPath in the conch file will be used if the
4299** host IDs match, or a new lock path will be generated automatically
4300** and written to the conch file.
4301*/
4302static int proxyTakeConch(unixFile *pFile){
4303 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4304
4305 if( pCtx->conchHeld>0 ){
4306 return SQLITE_OK;
4307 }else{
4308 unixFile *conchFile = pCtx->conchFile;
4309 char testValue[CONCHLEN];
4310 char conchValue[CONCHLEN];
4311 char lockPath[MAXPATHLEN];
4312 char *tLockPath = NULL;
4313 int rc = SQLITE_OK;
4314 int readRc = SQLITE_OK;
4315 int syncPerms = 0;
4316
4317 OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h,
4318 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
4319
4320 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
4321 if( rc==SQLITE_OK ){
4322 int pError = 0;
4323 memset(testValue, 0, CONCHLEN); // conch is fixed size
4324 rc = proxyGetHostID(testValue, &pError);
4325 if( (rc&0xff)==SQLITE_IOERR ){
4326 pFile->lastErrno = pError;
4327 }
4328 if( pCtx->lockProxyPath ){
4329 strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN);
4330 }
4331 }
4332 if( rc!=SQLITE_OK ){
4333 goto end_takeconch;
4334 }
4335
4336 readRc = unixRead((sqlite3_file *)conchFile, conchValue, CONCHLEN, 0);
4337 if( readRc!=SQLITE_IOERR_SHORT_READ ){
4338 if( readRc!=SQLITE_OK ){
4339 if( (rc&0xff)==SQLITE_IOERR ){
4340 pFile->lastErrno = conchFile->lastErrno;
4341 }
4342 rc = readRc;
4343 goto end_takeconch;
4344 }
4345 /* if the conch has data compare the contents */
4346 if( !pCtx->lockProxyPath ){
4347 /* for auto-named local lock file, just check the host ID and we'll
4348 ** use the local lock file path that's already in there */
4349 if( !memcmp(testValue, conchValue, HOSTIDLEN) ){
4350 tLockPath = (char *)&conchValue[HOSTIDLEN];
4351 goto end_takeconch;
4352 }
4353 }else{
4354 /* we've got the conch if conchValue matches our path and host ID */
4355 if( !memcmp(testValue, conchValue, CONCHLEN) ){
4356 goto end_takeconch;
4357 }
4358 }
4359 }else{
4360 /* a short read means we're "creating" the conch (even though it could
4361 ** have been user-intervention), if we acquire the exclusive lock,
4362 ** we'll try to match the current on-disk permissions of the database
4363 */
4364 syncPerms = 1;
4365 }
4366
4367 /* either conch was emtpy or didn't match */
4368 if( !pCtx->lockProxyPath ){
4369 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
4370 tLockPath = lockPath;
4371 strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN);
4372 }
4373
4374 /* update conch with host and path (this will fail if other process
4375 ** has a shared lock already) */
4376 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
4377 if( rc==SQLITE_OK ){
4378 rc = unixWrite((sqlite3_file *)conchFile, testValue, CONCHLEN, 0);
4379 if( rc==SQLITE_OK && syncPerms ){
4380 struct stat buf;
4381 int err = fstat(pFile->h, &buf);
4382 if( err==0 ){
4383 /* try to match the database file permissions, ignore failure */
4384#ifndef SQLITE_PROXY_DEBUG
4385 fchmod(conchFile->h, buf.st_mode);
4386#else
4387 if( fchmod(conchFile->h, buf.st_mode)!=0 ){
4388 int code = errno;
4389 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
4390 buf.st_mode, code, strerror(code));
4391 } else {
4392 fprintf(stderr, "fchmod %o SUCCEDED\n",buf.st_mode);
4393 }
4394 }else{
4395 int code = errno;
4396 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
4397 err, code, strerror(code));
4398#endif
4399 }
4400 }
4401 }
4402 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
4403
4404end_takeconch:
4405 OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h);
4406 if( rc==SQLITE_OK && pFile->openFlags ){
4407 if( pFile->h>=0 ){
4408#ifdef STRICT_CLOSE_ERROR
4409 if( close(pFile->h) ){
4410 pFile->lastErrno = errno;
4411 return SQLITE_IOERR_CLOSE;
4412 }
4413#else
4414 close(pFile->h); /* silently leak fd if fail */
4415#endif
4416 }
4417 pFile->h = -1;
4418 int fd = open(pCtx->dbPath, pFile->openFlags,
4419 SQLITE_DEFAULT_FILE_PERMISSIONS);
4420 OSTRACE2("TRANSPROXY: OPEN %d\n", fd);
4421 if( fd>=0 ){
4422 pFile->h = fd;
4423 }else{
4424 rc=SQLITE_CANTOPEN; // SQLITE_BUSY? proxyTakeConch called during locking
4425 }
4426 }
4427 if( rc==SQLITE_OK && !pCtx->lockProxy ){
4428 char *path = tLockPath ? tLockPath : pCtx->lockProxyPath;
4429 // ACS: Need to make a copy of path sometimes
4430 rc = proxyCreateUnixFile(path, &pCtx->lockProxy);
4431 }
4432 if( rc==SQLITE_OK ){
4433 pCtx->conchHeld = 1;
4434
4435 if( tLockPath ){
4436 pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath);
4437 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
4438 ((afpLockingContext *)pCtx->lockProxy->lockingContext)->dbPath =
4439 pCtx->lockProxyPath;
4440 }
4441 }
4442 } else {
4443 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
4444 }
4445 OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed");
4446 return rc;
4447 }
4448}
4449
4450/*
4451** If pFile holds a lock on a conch file, then release that lock.
4452*/
4453static int proxyReleaseConch(unixFile *pFile){
4454 int rc; /* Subroutine return code */
4455 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
4456 unixFile *conchFile; /* Name of the conch file */
4457
4458 pCtx = (proxyLockingContext *)pFile->lockingContext;
4459 conchFile = pCtx->conchFile;
4460 OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
4461 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
4462 getpid());
4463 pCtx->conchHeld = 0;
4464 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
4465 OSTRACE3("RELEASECONCH %d %s\n", conchFile->h,
4466 (rc==SQLITE_OK ? "ok" : "failed"));
4467 return rc;
4468}
4469
4470/*
4471** Given the name of a database file, compute the name of its conch file.
4472** Store the conch filename in memory obtained from sqlite3_malloc().
4473** Make *pConchPath point to the new name. Return SQLITE_OK on success
4474** or SQLITE_NOMEM if unable to obtain memory.
4475**
4476** The caller is responsible for ensuring that the allocated memory
4477** space is eventually freed.
4478**
4479** *pConchPath is set to NULL if a memory allocation error occurs.
4480*/
4481static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
4482 int i; /* Loop counter */
4483 int len = strlen(dbPath); /* Length of database filename - dbPath */
4484 char *conchPath; /* buffer in which to construct conch name */
4485
4486 /* Allocate space for the conch filename and initialize the name to
4487 ** the name of the original database file. */
4488 *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
4489 if( conchPath==0 ){
4490 return SQLITE_NOMEM;
4491 }
4492 memcpy(conchPath, dbPath, len+1);
4493
4494 /* now insert a "." before the last / character */
4495 for( i=(len-1); i>=0; i-- ){
4496 if( conchPath[i]=='/' ){
4497 i++;
4498 break;
4499 }
4500 }
4501 conchPath[i]='.';
4502 while ( i<len ){
4503 conchPath[i+1]=dbPath[i];
4504 i++;
4505 }
4506
4507 /* append the "-conch" suffix to the file */
4508 memcpy(&conchPath[i+1], "-conch", 7);
4509 assert( strlen(conchPath) == len+7 );
4510
4511 return SQLITE_OK;
4512}
4513
4514
4515/* Takes a fully configured proxy locking-style unix file and switches
4516** the local lock file path
4517*/
4518static int switchLockProxyPath(unixFile *pFile, const char *path) {
4519 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
4520 char *oldPath = pCtx->lockProxyPath;
4521 int rc = SQLITE_OK;
4522
4523 if( pFile->locktype!=NO_LOCK ){
4524 return SQLITE_BUSY;
4525 }
4526
4527 /* nothing to do if the path is NULL, :auto: or matches the existing path */
4528 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
4529 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
4530 return SQLITE_OK;
4531 }else{
4532 unixFile *lockProxy = pCtx->lockProxy;
4533 pCtx->lockProxy=NULL;
4534 pCtx->conchHeld = 0;
4535 if( lockProxy!=NULL ){
4536 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
4537 if( rc ) return rc;
4538 sqlite3_free(lockProxy);
4539 }
4540 sqlite3_free(oldPath);
4541 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
4542 }
4543
4544 return rc;
4545}
4546
4547/*
4548** pFile is a file that has been opened by a prior xOpen call. dbPath
4549** is a string buffer at least MAXPATHLEN+1 characters in size.
4550**
4551** This routine find the filename associated with pFile and writes it
4552** int dbPath.
4553*/
4554static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
4555#if defined(__DARWIN__)
4556 if( pFile->pMethod == &afpIoMethods ){
4557 /* afp style keeps a reference to the db path in the filePath field
4558 ** of the struct */
4559 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
4560 strcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath);
4561 }else
4562#endif
4563 if( pFile->pMethod == &dotlockIoMethods ){
4564 /* dot lock style uses the locking context to store the dot lock
4565 ** file path */
4566 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
4567 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
4568 }else{
4569 /* all other styles use the locking context to store the db file path */
4570 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
4571 strcpy(dbPath, (char *)pFile->lockingContext);
4572 }
4573 return SQLITE_OK;
4574}
4575
4576/*
4577** Takes an already filled in unix file and alters it so all file locking
4578** will be performed on the local proxy lock file. The following fields
4579** are preserved in the locking context so that they can be restored and
4580** the unix structure properly cleaned up at close time:
4581** ->lockingContext
4582** ->pMethod
4583*/
4584static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
4585 proxyLockingContext *pCtx;
4586 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
4587 char *lockPath=NULL;
4588 int rc = SQLITE_OK;
4589
4590 if( pFile->locktype!=NO_LOCK ){
4591 return SQLITE_BUSY;
4592 }
4593 proxyGetDbPathForUnixFile(pFile, dbPath);
4594 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
4595 lockPath=NULL;
4596 }else{
4597 lockPath=(char *)path;
4598 }
4599
4600 OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h,
4601 (lockPath ? lockPath : ":auto:"), getpid());
4602
4603 pCtx = sqlite3_malloc( sizeof(*pCtx) );
4604 if( pCtx==0 ){
4605 return SQLITE_NOMEM;
4606 }
4607 memset(pCtx, 0, sizeof(*pCtx));
4608
4609 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
4610 if( rc==SQLITE_OK ){
4611 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile);
4612 }
4613 if( rc==SQLITE_OK && lockPath ){
4614 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
4615 }
4616
4617 if( rc==SQLITE_OK ){
4618 /* all memory is allocated, proxys are created and assigned,
4619 ** switch the locking context and pMethod then return.
4620 */
4621 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
4622 pCtx->oldLockingContext = pFile->lockingContext;
4623 pFile->lockingContext = pCtx;
4624 pCtx->pOldMethod = pFile->pMethod;
4625 pFile->pMethod = &proxyIoMethods;
4626 }else{
4627 if( pCtx->conchFile ){
4628 rc = pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
4629 if( rc ) return rc;
4630 sqlite3_free(pCtx->conchFile);
4631 }
4632 sqlite3_free(pCtx->conchFilePath);
4633 sqlite3_free(pCtx);
4634 }
4635 OSTRACE3("TRANSPROXY %d %s\n", pFile->h,
4636 (rc==SQLITE_OK ? "ok" : "failed"));
4637 return rc;
4638}
4639
4640
4641/*
4642** This routine handles sqlite3_file_control() calls that are specific
4643** to proxy locking.
4644*/
4645static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
4646 switch( op ){
4647 case SQLITE_GET_LOCKPROXYFILE: {
4648 unixFile *pFile = (unixFile*)id;
4649 if( pFile->pMethod == &proxyIoMethods ){
4650 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
4651 proxyTakeConch(pFile);
4652 if( pCtx->lockProxyPath ){
4653 *(const char **)pArg = pCtx->lockProxyPath;
4654 }else{
4655 *(const char **)pArg = ":auto: (not held)";
4656 }
4657 } else {
4658 *(const char **)pArg = NULL;
4659 }
4660 return SQLITE_OK;
4661 }
4662 case SQLITE_SET_LOCKPROXYFILE: {
4663 unixFile *pFile = (unixFile*)id;
4664 int rc = SQLITE_OK;
4665 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
4666 if( pArg==NULL || (const char *)pArg==0 ){
4667 if( isProxyStyle ){
4668 /* turn off proxy locking - not supported */
4669 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
4670 }else{
4671 /* turn off proxy locking - already off - NOOP */
4672 rc = SQLITE_OK;
4673 }
4674 }else{
4675 const char *proxyPath = (const char *)pArg;
4676 if( isProxyStyle ){
4677 proxyLockingContext *pCtx =
4678 (proxyLockingContext*)pFile->lockingContext;
4679 if( !strcmp(pArg, ":auto:")
4680 || (pCtx->lockProxyPath &&
4681 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
4682 ){
4683 rc = SQLITE_OK;
4684 }else{
4685 rc = switchLockProxyPath(pFile, proxyPath);
4686 }
4687 }else{
4688 /* turn on proxy file locking */
4689 rc = proxyTransformUnixFile(pFile, proxyPath);
4690 }
4691 }
4692 return rc;
4693 }
4694 default: {
4695 assert( 0 ); /* The call assures that only valid opcodes are sent */
4696 }
4697 }
4698 /*NOTREACHED*/
4699 return SQLITE_ERROR;
4700}
4701
4702/*
4703** Within this division (the proxying locking implementation) the procedures
4704** above this point are all utilities. The lock-related methods of the
4705** proxy-locking sqlite3_io_method object follow.
4706*/
4707
4708
4709/*
4710** This routine checks if there is a RESERVED lock held on the specified
4711** file by this or any other process. If such a lock is held, set *pResOut
4712** to a non-zero value otherwise *pResOut is set to zero. The return value
4713** is set to SQLITE_OK unless an I/O error occurs during lock checking.
4714*/
4715static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
4716 unixFile *pFile = (unixFile*)id;
4717 int rc = proxyTakeConch(pFile);
4718 if( rc==SQLITE_OK ){
4719 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4720 unixFile *proxy = pCtx->lockProxy;
4721 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
4722 }
4723 return rc;
4724}
4725
4726/*
4727** Lock the file with the lock specified by parameter locktype - one
4728** of the following:
4729**
4730** (1) SHARED_LOCK
4731** (2) RESERVED_LOCK
4732** (3) PENDING_LOCK
4733** (4) EXCLUSIVE_LOCK
4734**
4735** Sometimes when requesting one lock state, additional lock states
4736** are inserted in between. The locking might fail on one of the later
4737** transitions leaving the lock state different from what it started but
4738** still short of its goal. The following chart shows the allowed
4739** transitions and the inserted intermediate states:
4740**
4741** UNLOCKED -> SHARED
4742** SHARED -> RESERVED
4743** SHARED -> (PENDING) -> EXCLUSIVE
4744** RESERVED -> (PENDING) -> EXCLUSIVE
4745** PENDING -> EXCLUSIVE
4746**
4747** This routine will only increase a lock. Use the sqlite3OsUnlock()
4748** routine to lower a locking level.
4749*/
4750static int proxyLock(sqlite3_file *id, int locktype) {
4751 unixFile *pFile = (unixFile*)id;
4752 int rc = proxyTakeConch(pFile);
4753 if( rc==SQLITE_OK ){
4754 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4755 unixFile *proxy = pCtx->lockProxy;
4756 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
4757 pFile->locktype = proxy->locktype;
4758 }
4759 return rc;
4760}
4761
4762
4763/*
4764** Lower the locking level on file descriptor pFile to locktype. locktype
4765** must be either NO_LOCK or SHARED_LOCK.
4766**
4767** If the locking level of the file descriptor is already at or below
4768** the requested locking level, this routine is a no-op.
4769*/
4770static int proxyUnlock(sqlite3_file *id, int locktype) {
4771 unixFile *pFile = (unixFile*)id;
4772 int rc = proxyTakeConch(pFile);
4773 if( rc==SQLITE_OK ){
4774 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4775 unixFile *proxy = pCtx->lockProxy;
4776 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
4777 pFile->locktype = proxy->locktype;
4778 }
4779 return rc;
4780}
4781
4782/*
4783** Close a file that uses proxy locks.
4784*/
4785static int proxyClose(sqlite3_file *id) {
4786 if( id ){
4787 unixFile *pFile = (unixFile*)id;
4788 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
4789 unixFile *lockProxy = pCtx->lockProxy;
4790 unixFile *conchFile = pCtx->conchFile;
4791 int rc = SQLITE_OK;
4792
4793 if( lockProxy ){
4794 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
4795 if( rc ) return rc;
4796 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
4797 if( rc ) return rc;
4798 sqlite3_free(lockProxy);
4799 pCtx->lockProxy = 0;
4800 }
4801 if( conchFile ){
4802 if( pCtx->conchHeld ){
4803 rc = proxyReleaseConch(pFile);
4804 if( rc ) return rc;
4805 }
4806 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
4807 if( rc ) return rc;
4808 sqlite3_free(conchFile);
4809 }
4810 sqlite3_free(pCtx->lockProxyPath);
4811 sqlite3_free(pCtx->conchFilePath);
4812 sqlite3_free(pCtx->dbPath);
4813 /* restore the original locking context and pMethod then close it */
4814 pFile->lockingContext = pCtx->oldLockingContext;
4815 pFile->pMethod = pCtx->pOldMethod;
4816 sqlite3_free(pCtx);
4817 return pFile->pMethod->xClose(id);
4818 }
4819 return SQLITE_OK;
4820}
4821
4822
4823
4824#endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
4825/*
4826** The proxy locking style is intended for use with AFP filesystems.
4827** And since AFP is only supported on MacOSX, the proxy locking is also
4828** restricted to MacOSX.
4829**
4830**
4831******************* End of the proxy lock implementation **********************
4832******************************************************************************/
4833
drh734c9862008-11-28 15:37:20 +00004834/*
danielk1977e339d652008-06-28 11:23:00 +00004835** Initialize the operating system interface.
drh734c9862008-11-28 15:37:20 +00004836**
4837** This routine registers all VFS implementations for unix-like operating
4838** systems. This routine, and the sqlite3_os_end() routine that follows,
4839** should be the only routines in this file that are visible from other
4840** files.
drh6b9d6dd2008-12-03 19:34:47 +00004841**
4842** This routine is called once during SQLite initialization and by a
4843** single thread. The memory allocation and mutex subsystems have not
4844** necessarily been initialized when this routine is called, and so they
4845** should not be used.
drh153c62c2007-08-24 03:51:33 +00004846*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004847int sqlite3_os_init(void){
drh6b9d6dd2008-12-03 19:34:47 +00004848 /*
4849 ** The following macro defines an initializer for an sqlite3_vfs object.
4850 ** The name of the VFS is NAME. The pAppData is a pointer to a "finder"
4851 ** function. The FINDER parameter to this macro is the name of the
4852 ** finder-function. The finder-function returns a pointer to the
4853 ** sqlite_io_methods object that implements the desired locking
4854 ** behaviors. See the division above that contains the IOMETHODS
4855 ** macro for addition information on finder-functions.
4856 **
4857 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
4858 ** object. But the "autolockIoFinder" available on MacOSX does a little
4859 ** more than that; it looks at the filesystem type that hosts the
4860 ** database file and tries to choose an locking method appropriate for
4861 ** that filesystem time.
danielk1977e339d652008-06-28 11:23:00 +00004862 */
drh7708e972008-11-29 00:56:52 +00004863 #define UNIXVFS(VFSNAME, FINDER) { \
danielk1977e339d652008-06-28 11:23:00 +00004864 1, /* iVersion */ \
4865 sizeof(unixFile), /* szOsFile */ \
4866 MAX_PATHNAME, /* mxPathname */ \
4867 0, /* pNext */ \
drh7708e972008-11-29 00:56:52 +00004868 VFSNAME, /* zName */ \
4869 (void*)FINDER, /* pAppData */ \
danielk1977e339d652008-06-28 11:23:00 +00004870 unixOpen, /* xOpen */ \
4871 unixDelete, /* xDelete */ \
4872 unixAccess, /* xAccess */ \
4873 unixFullPathname, /* xFullPathname */ \
4874 unixDlOpen, /* xDlOpen */ \
4875 unixDlError, /* xDlError */ \
4876 unixDlSym, /* xDlSym */ \
4877 unixDlClose, /* xDlClose */ \
4878 unixRandomness, /* xRandomness */ \
4879 unixSleep, /* xSleep */ \
4880 unixCurrentTime, /* xCurrentTime */ \
4881 unixGetLastError /* xGetLastError */ \
4882 }
4883
drh6b9d6dd2008-12-03 19:34:47 +00004884 /*
4885 ** All default VFSes for unix are contained in the following array.
4886 **
4887 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
4888 ** by the SQLite core when the VFS is registered. So the following
4889 ** array cannot be const.
4890 */
danielk1977e339d652008-06-28 11:23:00 +00004891 static sqlite3_vfs aVfs[] = {
drh7708e972008-11-29 00:56:52 +00004892#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
4893 UNIXVFS("unix", autolockIoFinder ),
4894#else
4895 UNIXVFS("unix", posixIoFinder ),
4896#endif
4897 UNIXVFS("unix-none", nolockIoFinder ),
4898 UNIXVFS("unix-dotfile", dotlockIoFinder ),
drh734c9862008-11-28 15:37:20 +00004899#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00004900 UNIXVFS("unix-namedsem", semIoFinder ),
drh734c9862008-11-28 15:37:20 +00004901#endif
4902#if SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00004903 UNIXVFS("unix-posix", posixIoFinder ),
4904 UNIXVFS("unix-flock", flockIoFinder ),
drh734c9862008-11-28 15:37:20 +00004905#endif
4906#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
drh7708e972008-11-29 00:56:52 +00004907 UNIXVFS("unix-afp", afpIoFinder ),
4908 UNIXVFS("unix-proxy", proxyIoFinder ),
drh734c9862008-11-28 15:37:20 +00004909#endif
drh153c62c2007-08-24 03:51:33 +00004910 };
drh6b9d6dd2008-12-03 19:34:47 +00004911 unsigned int i; /* Loop counter */
4912
4913 /* Register all VFSes defined in the aVfs[] array */
danielk1977e339d652008-06-28 11:23:00 +00004914 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
drh734c9862008-11-28 15:37:20 +00004915 sqlite3_vfs_register(&aVfs[i], i==0);
danielk1977e339d652008-06-28 11:23:00 +00004916 }
danielk1977c0fa4c52008-06-25 17:19:00 +00004917 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00004918}
danielk1977e339d652008-06-28 11:23:00 +00004919
4920/*
drh6b9d6dd2008-12-03 19:34:47 +00004921** Shutdown the operating system interface.
4922**
4923** Some operating systems might need to do some cleanup in this routine,
4924** to release dynamically allocated objects. But not on unix.
4925** This routine is a no-op for unix.
danielk1977e339d652008-06-28 11:23:00 +00004926*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004927int sqlite3_os_end(void){
4928 return SQLITE_OK;
4929}
drhdce8bdb2007-08-16 13:01:44 +00004930
danielk197729bafea2008-06-26 10:41:19 +00004931#endif /* SQLITE_OS_UNIX */