blob: e478502f429dd485b4fbe708de07d0870bd21cd2 [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**
22** This source file is group into divisions where the logic for various
23** 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**
27** The current set of divisions is as follows:
28**
29** * General-purpose declarations and utility functions.
30** * Unique file ID logic used by VxWorks.
31** * Various locking primitive implementations:
32** + 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)
38** + for proxy locks (MacOSX only)
39** * The routine used to detect an appropriate locking style
40** * sqlite3_file methods not associated with locking
41** * Implementations of sqlite3_os_init() and sqlite3_os_end()
42**
drh7708e972008-11-29 00:56:52 +000043** $Id: os_unix.c,v 1.223 2008/11/29 00:56:53 drh Exp $
drhbbd42a62004-05-22 17:41:58 +000044*/
drhbbd42a62004-05-22 17:41:58 +000045#include "sqliteInt.h"
danielk197729bafea2008-06-26 10:41:19 +000046#if SQLITE_OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000047
danielk1977e339d652008-06-28 11:23:00 +000048/*
drh734c9862008-11-28 15:37:20 +000049** This module implements the following locking styles:
danielk1977e339d652008-06-28 11:23:00 +000050**
drh734c9862008-11-28 15:37:20 +000051** 1. POSIX locking (the default),
52** 2. No locking,
53** 3. Dot-file locking,
54** 4. flock() locking,
55** 5. AFP locking (OSX only),
56** 6. Named POSIX semaphores (VXWorks only),
57** 7. proxy locking. (OSX only)
58**
59** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
60** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
61** selection of the appropriate locking style based on the filesystem
62** where the database is located.
drh40bbb0a2008-09-23 10:23:26 +000063**
64** SQLITE_ENABLE_LOCKING_STYLE only works on a Mac. It is turned on by
65** default on a Mac and disabled on all other posix platforms.
danielk1977e339d652008-06-28 11:23:00 +000066*/
drh40bbb0a2008-09-23 10:23:26 +000067#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
68# if defined(__DARWIN__)
69# define SQLITE_ENABLE_LOCKING_STYLE 1
70# else
71# define SQLITE_ENABLE_LOCKING_STYLE 0
72# endif
73#endif
drhbfe66312006-10-03 17:40:40 +000074
drh9cbe6352005-11-29 03:13:21 +000075/*
drh6c7d5c52008-11-21 20:32:33 +000076** Define the OS_VXWORKS pre-processor macro to 1 if building on
danielk1977397d65f2008-11-19 11:35:39 +000077** vxworks, or 0 otherwise.
78*/
drh6c7d5c52008-11-21 20:32:33 +000079#ifndef OS_VXWORKS
80# if defined(__RTP__) || defined(_WRS_KERNEL)
81# define OS_VXWORKS 1
82# else
83# define OS_VXWORKS 0
84# endif
danielk1977397d65f2008-11-19 11:35:39 +000085#endif
86
87/*
drh9cbe6352005-11-29 03:13:21 +000088** These #defines should enable >2GB file support on Posix if the
89** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000090** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000091**
92** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
93** on the compiler command line. This is necessary if you are compiling
94** on a recent machine (ex: RedHat 7.2) but you want your code to work
95** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
96** without this option, LFS is enable. But LFS does not exist in the kernel
97** in RedHat 6.0, so the code won't work. Hence, for maximum binary
98** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000099*/
100#ifndef SQLITE_DISABLE_LFS
101# define _LARGE_FILE 1
102# ifndef _FILE_OFFSET_BITS
103# define _FILE_OFFSET_BITS 64
104# endif
105# define _LARGEFILE_SOURCE 1
106#endif
drhbbd42a62004-05-22 17:41:58 +0000107
drh9cbe6352005-11-29 03:13:21 +0000108/*
109** standard include files.
110*/
111#include <sys/types.h>
112#include <sys/stat.h>
113#include <fcntl.h>
114#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +0000115#include <time.h>
drh19e2d372005-08-29 23:00:03 +0000116#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +0000117#include <errno.h>
danielk1977e339d652008-06-28 11:23:00 +0000118
drh40bbb0a2008-09-23 10:23:26 +0000119#if SQLITE_ENABLE_LOCKING_STYLE
danielk1977c70dfc42008-11-19 13:52:30 +0000120# include <sys/ioctl.h>
drh6c7d5c52008-11-21 20:32:33 +0000121# if OS_VXWORKS
danielk1977c70dfc42008-11-19 13:52:30 +0000122# define lstat stat
123# include <semaphore.h>
124# include <limits.h>
125# else
126# include <sys/param.h>
127# include <sys/mount.h>
128# endif
drhbfe66312006-10-03 17:40:40 +0000129#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +0000130
131/*
drhf1a221e2006-01-15 17:27:17 +0000132** If we are to be thread-safe, include the pthreads header and define
133** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +0000134*/
drhd677b3d2007-08-20 22:48:41 +0000135#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000136# include <pthread.h>
137# define SQLITE_UNIX_THREADS 1
138#endif
139
140/*
141** Default permissions when creating a new file
142*/
143#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
144# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
145#endif
146
danielk1977b4b47412007-08-17 15:53:36 +0000147/*
aswiftaebf4132008-11-21 00:10:35 +0000148 ** Default permissions when creating auto proxy dir
149 */
150#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
151# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
152#endif
153
154/*
danielk1977b4b47412007-08-17 15:53:36 +0000155** Maximum supported path-length.
156*/
157#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +0000158
drh734c9862008-11-28 15:37:20 +0000159/*
drh734c9862008-11-28 15:37:20 +0000160** Only set the lastErrno if the error code is a real error and not
161** a normal expected return code of SQLITE_BUSY or SQLITE_OK
162*/
163#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
164
drh9cbe6352005-11-29 03:13:21 +0000165
166/*
danielk1977ad94b582007-08-20 06:44:22 +0000167** The unixFile structure is subclass of sqlite3_file specific for the unix
drh054889e2005-11-30 03:20:31 +0000168** protability layer.
drh9cbe6352005-11-29 03:13:21 +0000169*/
drh054889e2005-11-30 03:20:31 +0000170typedef struct unixFile unixFile;
171struct unixFile {
danielk197762079062007-08-15 17:08:46 +0000172 sqlite3_io_methods const *pMethod; /* Always the first entry */
drh6c7d5c52008-11-21 20:32:33 +0000173 struct unixOpenCnt *pOpen; /* Info about all open fd's on this inode */
174 struct unixLockInfo *pLock; /* Info about locks on this inode */
175 int h; /* The file descriptor */
176 int dirfd; /* File descriptor for the directory */
177 unsigned char locktype; /* The type of lock held on this fd */
178 int lastErrno; /* The unix errno from the last I/O error */
drh6c7d5c52008-11-21 20:32:33 +0000179 void *lockingContext; /* Locking style specific state */
drh734c9862008-11-28 15:37:20 +0000180 int openFlags; /* The flags specified at open */
181#if SQLITE_THREADSAFE && defined(__linux__)
drh6c7d5c52008-11-21 20:32:33 +0000182 pthread_t tid; /* The thread that "owns" this unixFile */
183#endif
184#if OS_VXWORKS
185 int isDelete; /* Delete on close if true */
drh107886a2008-11-21 22:21:50 +0000186 struct vxworksFileId *pId; /* Unique file ID */
drh6c7d5c52008-11-21 20:32:33 +0000187#endif
danielk1977967a4a12007-08-20 14:23:44 +0000188#ifdef SQLITE_TEST
189 /* In test mode, increase the size of this structure a bit so that
190 ** it is larger than the struct CrashFile defined in test6.c.
191 */
192 char aPadding[32];
193#endif
drh9cbe6352005-11-29 03:13:21 +0000194};
195
drh0ccebe72005-06-07 22:22:50 +0000196/*
drh198bf392006-01-06 21:52:49 +0000197** Include code that is common to all os_*.c files
198*/
199#include "os_common.h"
200
201/*
drh0ccebe72005-06-07 22:22:50 +0000202** Define various macros that are missing from some systems.
203*/
drhbbd42a62004-05-22 17:41:58 +0000204#ifndef O_LARGEFILE
205# define O_LARGEFILE 0
206#endif
207#ifdef SQLITE_DISABLE_LFS
208# undef O_LARGEFILE
209# define O_LARGEFILE 0
210#endif
211#ifndef O_NOFOLLOW
212# define O_NOFOLLOW 0
213#endif
214#ifndef O_BINARY
215# define O_BINARY 0
216#endif
217
218/*
219** The DJGPP compiler environment looks mostly like Unix, but it
220** lacks the fcntl() system call. So redefine fcntl() to be something
221** that always succeeds. This means that locking does not occur under
drh85b623f2007-12-13 21:54:09 +0000222** DJGPP. But it is DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000223*/
224#ifdef __DJGPP__
225# define fcntl(A,B,C) 0
226#endif
227
228/*
drh2b4b5962005-06-15 17:47:55 +0000229** The threadid macro resolves to the thread-id or to 0. Used for
230** testing and debugging only.
231*/
drhd677b3d2007-08-20 22:48:41 +0000232#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000233#define threadid pthread_self()
234#else
235#define threadid 0
236#endif
237
danielk197713adf8a2004-06-03 16:08:41 +0000238
drh107886a2008-11-21 22:21:50 +0000239/*
240** Helper functions to obtain and relinquish the global mutex.
241*/
242static void unixEnterMutex(void){
243 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
244}
245static void unixLeaveMutex(void){
246 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
247}
248
drh734c9862008-11-28 15:37:20 +0000249
250#ifdef SQLITE_DEBUG
251/*
252** Helper function for printing out trace information from debugging
253** binaries. This returns the string represetation of the supplied
254** integer lock-type.
255*/
256static const char *locktypeName(int locktype){
257 switch( locktype ){
258 case NO_LOCK: return "NONE";
259 case SHARED_LOCK: return "SHARED";
260 case RESERVED_LOCK: return "RESERVED";
261 case PENDING_LOCK: return "PENDING";
262 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
263 }
264 return "ERROR";
265}
266#endif
267
268#ifdef SQLITE_LOCK_TRACE
269/*
270** Print out information about all locking operations.
drh6c7d5c52008-11-21 20:32:33 +0000271**
drh734c9862008-11-28 15:37:20 +0000272** This routine is used for troubleshooting locks on multithreaded
273** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
274** command-line option on the compiler. This code is normally
275** turned off.
276*/
277static int lockTrace(int fd, int op, struct flock *p){
278 char *zOpName, *zType;
279 int s;
280 int savedErrno;
281 if( op==F_GETLK ){
282 zOpName = "GETLK";
283 }else if( op==F_SETLK ){
284 zOpName = "SETLK";
285 }else{
286 s = fcntl(fd, op, p);
287 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
288 return s;
289 }
290 if( p->l_type==F_RDLCK ){
291 zType = "RDLCK";
292 }else if( p->l_type==F_WRLCK ){
293 zType = "WRLCK";
294 }else if( p->l_type==F_UNLCK ){
295 zType = "UNLCK";
296 }else{
297 assert( 0 );
298 }
299 assert( p->l_whence==SEEK_SET );
300 s = fcntl(fd, op, p);
301 savedErrno = errno;
302 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
303 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
304 (int)p->l_pid, s);
305 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
306 struct flock l2;
307 l2 = *p;
308 fcntl(fd, F_GETLK, &l2);
309 if( l2.l_type==F_RDLCK ){
310 zType = "RDLCK";
311 }else if( l2.l_type==F_WRLCK ){
312 zType = "WRLCK";
313 }else if( l2.l_type==F_UNLCK ){
314 zType = "UNLCK";
315 }else{
316 assert( 0 );
317 }
318 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
319 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
320 }
321 errno = savedErrno;
322 return s;
323}
324#define fcntl lockTrace
325#endif /* SQLITE_LOCK_TRACE */
326
327
328
329/*
330** This routine translates a standard POSIX errno code into something
331** useful to the clients of the sqlite3 functions. Specifically, it is
332** intended to translate a variety of "try again" errors into SQLITE_BUSY
333** and a variety of "please close the file descriptor NOW" errors into
334** SQLITE_IOERR
335**
336** Errors during initialization of locks, or file system support for locks,
337** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
338*/
339static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
340 switch (posixError) {
341 case 0:
342 return SQLITE_OK;
343
344 case EAGAIN:
345 case ETIMEDOUT:
346 case EBUSY:
347 case EINTR:
348 case ENOLCK:
349 /* random NFS retry error, unless during file system support
350 * introspection, in which it actually means what it says */
351 return SQLITE_BUSY;
352
353 case EACCES:
354 /* EACCES is like EAGAIN during locking operations, but not any other time*/
355 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
356 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
357 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
358 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
359 return SQLITE_BUSY;
360 }
361 /* else fall through */
362 case EPERM:
363 return SQLITE_PERM;
364
365 case EDEADLK:
366 return SQLITE_IOERR_BLOCKED;
367
368#if EOPNOTSUPP!=ENOTSUP
369 case EOPNOTSUPP:
370 /* something went terribly awry, unless during file system support
371 * introspection, in which it actually means what it says */
372#endif
373#ifdef ENOTSUP
374 case ENOTSUP:
375 /* invalid fd, unless during file system support introspection, in which
376 * it actually means what it says */
377#endif
378 case EIO:
379 case EBADF:
380 case EINVAL:
381 case ENOTCONN:
382 case ENODEV:
383 case ENXIO:
384 case ENOENT:
385 case ESTALE:
386 case ENOSYS:
387 /* these should force the client to close the file and reconnect */
388
389 default:
390 return sqliteIOErr;
391 }
392}
393
394
395
396/******************************************************************************
397****************** Begin Unique File ID Utility Used By VxWorks ***************
398**
399** On most versions of unix, we can get a unique ID for a file by concatenating
400** the device number and the inode number. But this does not work on VxWorks.
401** On VxWorks, a unique file id must be based on the canonical filename.
402**
403** A pointer to an instance of the following structure can be used as a
404** unique file ID in VxWorks. Each instance of this structure contains
405** a copy of the canonical filename. There is also a reference count.
406** The structure is reclaimed when the number of pointers to it drops to
407** zero.
408**
409** There are never very many files open at one time and lookups are not
410** a performance-critical path, so it is sufficient to put these
411** structures on a linked list.
412*/
413struct vxworksFileId {
414 struct vxworksFileId *pNext; /* Next in a list of them all */
415 int nRef; /* Number of references to this one */
416 int nName; /* Length of the zCanonicalName[] string */
417 char *zCanonicalName; /* Canonical filename */
418};
419
420#if OS_VXWORKS
421/*
422** All unique filesname are held on a linked list headed by this
423** variable:
424*/
425static struct vxworksFileId *vxworksFileList = 0;
426
427/*
428** Simplify a filename into its canonical form
429** by making the following changes:
430**
431** * removing any trailing and duplicate /
432** * removing /./
433** * removing /A/../
434**
435** Changes are made in-place. Return the new name length.
436**
437** The original filename is in z[0..n-1]. Return the number of
438** characters in the simplified name.
439*/
440static int vxworksSimplifyName(char *z, int n){
441 int i, j;
442 while( n>1 && z[n-1]=='/' ){ n--; }
443 for(i=j=0; i<n; i++){
444 if( z[i]=='/' ){
445 if( z[i+1]=='/' ) continue;
446 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
447 i += 1;
448 continue;
449 }
450 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
451 while( j>0 && z[j-1]!='/' ){ j--; }
452 if( j>0 ){ j--; }
453 i += 2;
454 continue;
455 }
456 }
457 z[j++] = z[i];
458 }
459 z[j] = 0;
460 return j;
461}
462
463/*
464** Find a unique file ID for the given absolute pathname. Return
465** a pointer to the vxworksFileId object. This pointer is the unique
466** file ID.
467**
468** The nRef field of the vxworksFileId object is incremented before
469** the object is returned. A new vxworksFileId object is created
470** and added to the global list if necessary.
471**
472** If a memory allocation error occurs, return NULL.
473*/
474static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
475 struct vxworksFileId *pNew; /* search key and new file ID */
476 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
477 int n; /* Length of zAbsoluteName string */
478
479 assert( zAbsoluteName[0]=='/' );
480 n = strlen(zAbsoluteName);
481 pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
482 if( pNew==0 ) return 0;
483 pNew->zCanonicalName = (char*)&pNew[1];
484 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
485 n = vxworksSimplifyName(pNew->zCanonicalName, n);
486
487 /* Search for an existing entry that matching the canonical name.
488 ** If found, increment the reference count and return a pointer to
489 ** the existing file ID.
490 */
491 unixEnterMutex();
492 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
493 if( pCandidate->nName==n
494 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
495 ){
496 sqlite3_free(pNew);
497 pCandidate->nRef++;
498 unixLeaveMutex();
499 return pCandidate;
500 }
501 }
502
503 /* No match was found. We will make a new file ID */
504 pNew->nRef = 1;
505 pNew->nName = n;
506 pNew->pNext = vxworksFileList;
507 vxworksFileList = pNew;
508 unixLeaveMutex();
509 return pNew;
510}
511
512/*
513** Decrement the reference count on a vxworksFileId object. Free
514** the object when the reference count reaches zero.
515*/
516static void vxworksReleaseFileId(struct vxworksFileId *pId){
517 unixEnterMutex();
518 assert( pId->nRef>0 );
519 pId->nRef--;
520 if( pId->nRef==0 ){
521 struct vxworksFileId **pp;
522 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
523 assert( *pp==pId );
524 *pp = pId->pNext;
525 sqlite3_free(pId);
526 }
527 unixLeaveMutex();
528}
529#endif /* OS_VXWORKS */
530/*************** End of Unique File ID Utility Used By VxWorks ****************
531******************************************************************************/
532
533
534/******************************************************************************
535*************************** Posix Advisory Locking ****************************
536**
537** POSIX advisory locks broken by design. ANSI STD 1003.1 (1996)
drhbbd42a62004-05-22 17:41:58 +0000538** section 6.5.2.2 lines 483 through 490 specify that when a process
539** sets or clears a lock, that operation overrides any prior locks set
540** by the same process. It does not explicitly say so, but this implies
541** that it overrides locks set by the same process using a different
542** file descriptor. Consider this test case:
drh6c7d5c52008-11-21 20:32:33 +0000543**
544** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
drhbbd42a62004-05-22 17:41:58 +0000545** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
546**
547** Suppose ./file1 and ./file2 are really the same file (because
548** one is a hard or symbolic link to the other) then if you set
549** an exclusive lock on fd1, then try to get an exclusive lock
550** on fd2, it works. I would have expected the second lock to
551** fail since there was already a lock on the file due to fd1.
552** But not so. Since both locks came from the same process, the
553** second overrides the first, even though they were on different
554** file descriptors opened on different file names.
555**
drh734c9862008-11-28 15:37:20 +0000556** This means that we cannot use POSIX locks to synchronize file access
557** among competing threads of the same process. POSIX locks will work fine
drhbbd42a62004-05-22 17:41:58 +0000558** to synchronize access for threads in separate processes, but not
559** threads within the same process.
560**
561** To work around the problem, SQLite has to manage file locks internally
562** on its own. Whenever a new database is opened, we have to find the
563** specific inode of the database file (the inode is determined by the
564** st_dev and st_ino fields of the stat structure that fstat() fills in)
565** and check for locks already existing on that inode. When locks are
566** created or removed, we have to look at our own internal record of the
567** locks to see if another thread has previously set a lock on that same
568** inode.
569**
danielk1977ad94b582007-08-20 06:44:22 +0000570** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000571** descriptor. It is now a structure that holds the integer file
572** descriptor and a pointer to a structure that describes the internal
573** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000574** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000575** point to the same locking structure. The locking structure keeps
576** a reference count (so we will know when to delete it) and a "cnt"
577** field that tells us its internal lock status. cnt==0 means the
578** file is unlocked. cnt==-1 means the file has an exclusive lock.
579** cnt>0 means there are cnt shared locks on the file.
580**
581** Any attempt to lock or unlock a file first checks the locking
582** structure. The fcntl() system call is only invoked to set a
583** POSIX lock if the internal lock structure transitions between
584** a locked and an unlocked state.
585**
drh734c9862008-11-28 15:37:20 +0000586** But wait: there are yet more problems with POSIX advisory locks.
drhbbd42a62004-05-22 17:41:58 +0000587**
588** If you close a file descriptor that points to a file that has locks,
589** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000590** released. To work around this problem, each unixFile structure contains
drh6c7d5c52008-11-21 20:32:33 +0000591** a pointer to an unixOpenCnt structure. There is one unixOpenCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000592** per open inode, which means that multiple unixFile can point to a single
drh6c7d5c52008-11-21 20:32:33 +0000593** unixOpenCnt. When an attempt is made to close an unixFile, if there are
danielk1977ad94b582007-08-20 06:44:22 +0000594** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000595** to close() the file descriptor is deferred until all of the locks clear.
drh6c7d5c52008-11-21 20:32:33 +0000596** The unixOpenCnt structure keeps a list of file descriptors that need to
drhbbd42a62004-05-22 17:41:58 +0000597** be closed and that list is walked (and cleared) when the last lock
598** clears.
599**
drh734c9862008-11-28 15:37:20 +0000600** Yet another problem with posix locks and threads:
drh5fdae772004-06-29 03:29:00 +0000601**
drh734c9862008-11-28 15:37:20 +0000602** Many older versions of linux us the LinuxThreads library which is
603** not posix compliant. Under LinuxThreads, a lock created thread
604** A cannot be modified or overridden by a different thread B.
605** Only thread A can modify the lock. Locking behavior is correct
606** if the appliation uses the newer Native Posix Thread Library (NPTL)
607** on linux - with NPTL a lock created by thread A can override locks
608** in thread B. But there is no way to know at compile-time which
609** threading library is being used. So there is no way to know at
610** compile-time whether or not thread A can override locks on thread B.
611** We have to do a run-time check to discover the behavior of the
612** current process.
drh5fdae772004-06-29 03:29:00 +0000613**
drh734c9862008-11-28 15:37:20 +0000614** On systems where thread A is unable to modify locks created by
615** thread B, we have to keep track of which thread created each
616** lock. So there is an extra field in the key to the unixLockInfo
617** structure to record this information. And on those systems it
618** is illegal to begin a transaction in one thread and finish it
619** in another. For this latter restriction, there is no work-around.
620** It is a limitation of LinuxThreads.
drhbbd42a62004-05-22 17:41:58 +0000621*/
622
623/*
drh6c7d5c52008-11-21 20:32:33 +0000624** Set or check the unixFile.tid field. This field is set when an unixFile
625** is first opened. All subsequent uses of the unixFile verify that the
626** same thread is operating on the unixFile. Some operating systems do
627** not allow locks to be overridden by other threads and that restriction
628** means that sqlite3* database handles cannot be moved from one thread
drh734c9862008-11-28 15:37:20 +0000629** to another while locks are held.
drh6c7d5c52008-11-21 20:32:33 +0000630**
631** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
632** another as long as we are running on a system that supports threads
drh734c9862008-11-28 15:37:20 +0000633** overriding each others locks (which is now the most common behavior)
drh6c7d5c52008-11-21 20:32:33 +0000634** or if no locks are held. But the unixFile.pLock field needs to be
635** recomputed because its key includes the thread-id. See the
636** transferOwnership() function below for additional information
637*/
drh734c9862008-11-28 15:37:20 +0000638#if SQLITE_THREADSAFE && defined(__linux__)
drh6c7d5c52008-11-21 20:32:33 +0000639# define SET_THREADID(X) (X)->tid = pthread_self()
640# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
641 !pthread_equal((X)->tid, pthread_self()))
642#else
643# define SET_THREADID(X)
644# define CHECK_THREADID(X) 0
645#endif
646
647/*
drhbbd42a62004-05-22 17:41:58 +0000648** An instance of the following structure serves as the key used
drh6c7d5c52008-11-21 20:32:33 +0000649** to locate a particular unixOpenCnt structure given its inode. This
650** is the same as the unixLockKey except that the thread ID is omitted.
651*/
652struct unixFileId {
drh107886a2008-11-21 22:21:50 +0000653 dev_t dev; /* Device number */
drh6c7d5c52008-11-21 20:32:33 +0000654#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +0000655 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
drh6c7d5c52008-11-21 20:32:33 +0000656#else
drh107886a2008-11-21 22:21:50 +0000657 ino_t ino; /* Inode number */
drh6c7d5c52008-11-21 20:32:33 +0000658#endif
659};
660
661/*
662** An instance of the following structure serves as the key used
663** to locate a particular unixLockInfo structure given its inode.
drh5fdae772004-06-29 03:29:00 +0000664**
drh734c9862008-11-28 15:37:20 +0000665** If threads cannot override each others locks (LinuxThreads), then we
666** set the unixLockKey.tid field to the thread ID. If threads can override
667** each others locks (Posix and NPTL) then tid is always set to zero.
668** tid is omitted if we compile without threading support or on an OS
669** other than linux.
drhbbd42a62004-05-22 17:41:58 +0000670*/
drh6c7d5c52008-11-21 20:32:33 +0000671struct unixLockKey {
672 struct unixFileId fid; /* Unique identifier for the file */
drh734c9862008-11-28 15:37:20 +0000673#if SQLITE_THREADSAFE && defined(__linux__)
674 pthread_t tid; /* Thread ID of lock owner. Zero if not using LinuxThreads */
drh5fdae772004-06-29 03:29:00 +0000675#endif
drhbbd42a62004-05-22 17:41:58 +0000676};
677
678/*
679** An instance of the following structure is allocated for each open
680** inode on each thread with a different process ID. (Threads have
drh734c9862008-11-28 15:37:20 +0000681** different process IDs on some versions of linux, but not on most
682** other unixes.)
drhbbd42a62004-05-22 17:41:58 +0000683**
danielk1977ad94b582007-08-20 06:44:22 +0000684** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000685** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000686** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000687*/
drh6c7d5c52008-11-21 20:32:33 +0000688struct unixLockInfo {
drh734c9862008-11-28 15:37:20 +0000689 struct unixLockKey lockKey; /* The lookup key */
690 int cnt; /* Number of SHARED locks held */
691 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
692 int nRef; /* Number of pointers to this structure */
693 struct unixLockInfo *pNext; /* List of all unixLockInfo objects */
694 struct unixLockInfo *pPrev; /* .... doubly linked */
drhbbd42a62004-05-22 17:41:58 +0000695};
696
697/*
698** An instance of the following structure is allocated for each open
699** inode. This structure keeps track of the number of locks on that
700** inode. If a close is attempted against an inode that is holding
701** locks, the close is deferred until all locks clear by adding the
702** file descriptor to be closed to the pending list.
703*/
drh6c7d5c52008-11-21 20:32:33 +0000704struct unixOpenCnt {
705 struct unixFileId fileId; /* The lookup key */
706 int nRef; /* Number of pointers to this structure */
707 int nLock; /* Number of outstanding locks */
708 int nPending; /* Number of pending close() operations */
709 int *aPending; /* Malloced space holding fd's awaiting a close() */
710#if OS_VXWORKS
711 sem_t *pSem; /* Named POSIX semaphore */
chw97185482008-11-17 08:05:31 +0000712 char aSemName[MAX_PATHNAME+1]; /* Name of that semaphore */
713#endif
drh6c7d5c52008-11-21 20:32:33 +0000714 struct unixOpenCnt *pNext, *pPrev; /* List of all unixOpenCnt objects */
drhbbd42a62004-05-22 17:41:58 +0000715};
716
drhda0e7682008-07-30 15:27:54 +0000717/*
drh6c7d5c52008-11-21 20:32:33 +0000718** List of all unixLockInfo and unixOpenCnt objects. This used to be a hash
drhda0e7682008-07-30 15:27:54 +0000719** table. But the number of objects is rarely more than a dozen and
720** never exceeds a few thousand. And lookup is not on a critical
drh6c7d5c52008-11-21 20:32:33 +0000721** path so a simple linked list will suffice.
drhbbd42a62004-05-22 17:41:58 +0000722*/
drh6c7d5c52008-11-21 20:32:33 +0000723static struct unixLockInfo *lockList = 0;
724static struct unixOpenCnt *openList = 0;
drh5fdae772004-06-29 03:29:00 +0000725
drh5fdae772004-06-29 03:29:00 +0000726/*
727** This variable records whether or not threads can override each others
728** locks.
729**
730** 0: No. Threads cannot override each others locks.
731** 1: Yes. Threads can override each others locks.
732** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000733**
drh5062d3a2006-01-31 23:03:35 +0000734** On some systems, we know at compile-time if threads can override each
735** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
736** will be set appropriately. On other systems, we have to check at
737** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
738** undefined.
739**
drhf1a221e2006-01-15 17:27:17 +0000740** This variable normally has file scope only. But during testing, we make
741** it a global so that the test code can change its value in order to verify
742** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000743*/
drh5062d3a2006-01-31 23:03:35 +0000744#ifndef SQLITE_THREAD_OVERRIDE_LOCK
745# define SQLITE_THREAD_OVERRIDE_LOCK -1
746#endif
drh029b44b2006-01-15 00:13:15 +0000747#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000748int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000749#else
drh5062d3a2006-01-31 23:03:35 +0000750static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000751#endif
drh5fdae772004-06-29 03:29:00 +0000752
753/*
754** This structure holds information passed into individual test
755** threads by the testThreadLockingBehavior() routine.
756*/
757struct threadTestData {
758 int fd; /* File to be locked */
759 struct flock lock; /* The locking operation */
760 int result; /* Result of the locking operation */
761};
762
drh6c7d5c52008-11-21 20:32:33 +0000763#if SQLITE_THREADSAFE && defined(__linux__)
drh5fdae772004-06-29 03:29:00 +0000764/*
danielk197741a6a612008-11-11 18:34:35 +0000765** This function is used as the main routine for a thread launched by
766** testThreadLockingBehavior(). It tests whether the shared-lock obtained
767** by the main thread in testThreadLockingBehavior() conflicts with a
768** hypothetical write-lock obtained by this thread on the same file.
769**
770** The write-lock is not actually acquired, as this is not possible if
771** the file is open in read-only mode (see ticket #3472).
772*/
drh5fdae772004-06-29 03:29:00 +0000773static void *threadLockingTest(void *pArg){
774 struct threadTestData *pData = (struct threadTestData*)pArg;
danielk197741a6a612008-11-11 18:34:35 +0000775 pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
drh5fdae772004-06-29 03:29:00 +0000776 return pArg;
777}
drh6c7d5c52008-11-21 20:32:33 +0000778#endif /* SQLITE_THREADSAFE && defined(__linux__) */
drh5fdae772004-06-29 03:29:00 +0000779
drh6c7d5c52008-11-21 20:32:33 +0000780
781#if SQLITE_THREADSAFE && defined(__linux__)
drh5fdae772004-06-29 03:29:00 +0000782/*
783** This procedure attempts to determine whether or not threads
784** can override each others locks then sets the
785** threadsOverrideEachOthersLocks variable appropriately.
786*/
danielk19774d5238f2006-01-27 06:32:00 +0000787static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000788 int fd;
danielk197741a6a612008-11-11 18:34:35 +0000789 int rc;
790 struct threadTestData d;
791 struct flock l;
792 pthread_t t;
drh5fdae772004-06-29 03:29:00 +0000793
794 fd = dup(fd_orig);
795 if( fd<0 ) return;
danielk197741a6a612008-11-11 18:34:35 +0000796 memset(&l, 0, sizeof(l));
797 l.l_type = F_RDLCK;
798 l.l_len = 1;
799 l.l_start = 0;
800 l.l_whence = SEEK_SET;
801 rc = fcntl(fd_orig, F_SETLK, &l);
802 if( rc!=0 ) return;
803 memset(&d, 0, sizeof(d));
804 d.fd = fd;
805 d.lock = l;
806 d.lock.l_type = F_WRLCK;
807 pthread_create(&t, 0, threadLockingTest, &d);
808 pthread_join(t, 0);
drh5fdae772004-06-29 03:29:00 +0000809 close(fd);
danielk197741a6a612008-11-11 18:34:35 +0000810 if( d.result!=0 ) return;
811 threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
drh5fdae772004-06-29 03:29:00 +0000812}
drh734c9862008-11-28 15:37:20 +0000813#elif SQLITE_THREADSAFE
danielk197741a6a612008-11-11 18:34:35 +0000814/*
815** On anything other than linux, assume threads override each others locks.
816*/
817static void testThreadLockingBehavior(int fd_orig){
drh6c7d5c52008-11-21 20:32:33 +0000818 UNUSED_PARAMETER(fd_orig);
danielk197741a6a612008-11-11 18:34:35 +0000819 threadsOverrideEachOthersLocks = 1;
820}
drh6c7d5c52008-11-21 20:32:33 +0000821#endif /* SQLITE_THERADSAFE && defined(__linux__) */
drh5fdae772004-06-29 03:29:00 +0000822
drhbbd42a62004-05-22 17:41:58 +0000823/*
drh6c7d5c52008-11-21 20:32:33 +0000824** Release a unixLockInfo structure previously allocated by findLockInfo().
825*/
826static void releaseLockInfo(struct unixLockInfo *pLock){
danielk1977e339d652008-06-28 11:23:00 +0000827 if( pLock ){
828 pLock->nRef--;
829 if( pLock->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000830 if( pLock->pPrev ){
831 assert( pLock->pPrev->pNext==pLock );
832 pLock->pPrev->pNext = pLock->pNext;
833 }else{
834 assert( lockList==pLock );
835 lockList = pLock->pNext;
836 }
837 if( pLock->pNext ){
838 assert( pLock->pNext->pPrev==pLock );
839 pLock->pNext->pPrev = pLock->pPrev;
840 }
danielk1977e339d652008-06-28 11:23:00 +0000841 sqlite3_free(pLock);
842 }
drhbbd42a62004-05-22 17:41:58 +0000843 }
844}
845
846/*
drh6c7d5c52008-11-21 20:32:33 +0000847** Release a unixOpenCnt structure previously allocated by findLockInfo().
drhbbd42a62004-05-22 17:41:58 +0000848*/
drh6c7d5c52008-11-21 20:32:33 +0000849static void releaseOpenCnt(struct unixOpenCnt *pOpen){
danielk1977e339d652008-06-28 11:23:00 +0000850 if( pOpen ){
851 pOpen->nRef--;
852 if( pOpen->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000853 if( pOpen->pPrev ){
854 assert( pOpen->pPrev->pNext==pOpen );
855 pOpen->pPrev->pNext = pOpen->pNext;
856 }else{
857 assert( openList==pOpen );
858 openList = pOpen->pNext;
859 }
860 if( pOpen->pNext ){
861 assert( pOpen->pNext->pPrev==pOpen );
862 pOpen->pNext->pPrev = pOpen->pPrev;
863 }
864 sqlite3_free(pOpen->aPending);
danielk1977e339d652008-06-28 11:23:00 +0000865 sqlite3_free(pOpen);
866 }
drhbbd42a62004-05-22 17:41:58 +0000867 }
868}
869
drh6c7d5c52008-11-21 20:32:33 +0000870
871/*
872** Given a file descriptor, locate unixLockInfo and unixOpenCnt structures that
873** describes that file descriptor. Create new ones if necessary. The
874** return values might be uninitialized if an error occurs.
875**
876** Return an appropriate error code.
877*/
878static int findLockInfo(
879 unixFile *pFile, /* Unix file with file desc used in the key */
880 struct unixLockInfo **ppLock, /* Return the unixLockInfo structure here */
881 struct unixOpenCnt **ppOpen /* Return the unixOpenCnt structure here */
882){
883 int rc; /* System call return code */
884 int fd; /* The file descriptor for pFile */
885 struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */
886 struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */
887 struct stat statbuf; /* Low-level file information */
888 struct unixLockInfo *pLock; /* Candidate unixLockInfo object */
889 struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */
890
891 /* Get low-level information about the file that we can used to
892 ** create a unique name for the file.
893 */
894 fd = pFile->h;
895 rc = fstat(fd, &statbuf);
896 if( rc!=0 ){
897 pFile->lastErrno = errno;
898#ifdef EOVERFLOW
899 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
900#endif
901 return SQLITE_IOERR;
902 }
903
904 /* On OS X on an msdos filesystem, the inode number is reported
905 ** incorrectly for zero-size files. See ticket #3260. To work
906 ** around this problem (we consider it a bug in OS X, not SQLite)
907 ** we always increase the file size to 1 by writing a single byte
908 ** prior to accessing the inode number. The one byte written is
909 ** an ASCII 'S' character which also happens to be the first byte
910 ** in the header of every SQLite database. In this way, if there
911 ** is a race condition such that another thread has already populated
912 ** the first page of the database, no damage is done.
913 */
914 if( statbuf.st_size==0 ){
915 write(fd, "S", 1);
916 rc = fstat(fd, &statbuf);
917 if( rc!=0 ){
918 pFile->lastErrno = errno;
919 return SQLITE_IOERR;
920 }
921 }
922
923 memset(&lockKey, 0, sizeof(lockKey));
924 lockKey.fid.dev = statbuf.st_dev;
925#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +0000926 lockKey.fid.pId = pFile->pId;
drh6c7d5c52008-11-21 20:32:33 +0000927#else
928 lockKey.fid.ino = statbuf.st_ino;
929#endif
drh734c9862008-11-28 15:37:20 +0000930#if SQLITE_THREADSAFE && defined(__linux__)
drh6c7d5c52008-11-21 20:32:33 +0000931 if( threadsOverrideEachOthersLocks<0 ){
932 testThreadLockingBehavior(fd);
933 }
934 lockKey.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
935#endif
936 fileId = lockKey.fid;
937 if( ppLock!=0 ){
938 pLock = lockList;
939 while( pLock && memcmp(&lockKey, &pLock->lockKey, sizeof(lockKey)) ){
940 pLock = pLock->pNext;
941 }
942 if( pLock==0 ){
943 pLock = sqlite3_malloc( sizeof(*pLock) );
944 if( pLock==0 ){
945 rc = SQLITE_NOMEM;
946 goto exit_findlockinfo;
947 }
948 pLock->lockKey = lockKey;
949 pLock->nRef = 1;
950 pLock->cnt = 0;
951 pLock->locktype = 0;
952 pLock->pNext = lockList;
953 pLock->pPrev = 0;
954 if( lockList ) lockList->pPrev = pLock;
955 lockList = pLock;
956 }else{
957 pLock->nRef++;
958 }
959 *ppLock = pLock;
960 }
961 if( ppOpen!=0 ){
962 pOpen = openList;
963 while( pOpen && memcmp(&fileId, &pOpen->fileId, sizeof(fileId)) ){
964 pOpen = pOpen->pNext;
965 }
966 if( pOpen==0 ){
967 pOpen = sqlite3_malloc( sizeof(*pOpen) );
968 if( pOpen==0 ){
969 releaseLockInfo(pLock);
970 rc = SQLITE_NOMEM;
971 goto exit_findlockinfo;
972 }
973 pOpen->fileId = fileId;
974 pOpen->nRef = 1;
975 pOpen->nLock = 0;
976 pOpen->nPending = 0;
977 pOpen->aPending = 0;
978 pOpen->pNext = openList;
979 pOpen->pPrev = 0;
980 if( openList ) openList->pPrev = pOpen;
981 openList = pOpen;
982#if OS_VXWORKS
983 pOpen->pSem = NULL;
984 pOpen->aSemName[0] = '\0';
985#endif
986 }else{
987 pOpen->nRef++;
988 }
989 *ppOpen = pOpen;
990 }
991
992exit_findlockinfo:
993 return rc;
994}
drh6c7d5c52008-11-21 20:32:33 +0000995
drh7708e972008-11-29 00:56:52 +0000996/*
997** If we are currently in a different thread than the thread that the
998** unixFile argument belongs to, then transfer ownership of the unixFile
999** over to the current thread.
1000**
1001** A unixFile is only owned by a thread on systems that use LinuxThreads.
1002**
1003** Ownership transfer is only allowed if the unixFile is currently unlocked.
1004** If the unixFile is locked and an ownership is wrong, then return
1005** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
1006*/
1007#if SQLITE_THREADSAFE && defined(__linux__)
1008static int transferOwnership(unixFile *pFile){
1009 int rc;
1010 pthread_t hSelf;
1011 if( threadsOverrideEachOthersLocks ){
1012 /* Ownership transfers not needed on this system */
1013 return SQLITE_OK;
1014 }
1015 hSelf = pthread_self();
1016 if( pthread_equal(pFile->tid, hSelf) ){
1017 /* We are still in the same thread */
1018 OSTRACE1("No-transfer, same thread\n");
1019 return SQLITE_OK;
1020 }
1021 if( pFile->locktype!=NO_LOCK ){
1022 /* We cannot change ownership while we are holding a lock! */
1023 return SQLITE_MISUSE;
1024 }
1025 OSTRACE4("Transfer ownership of %d from %d to %d\n",
1026 pFile->h, pFile->tid, hSelf);
1027 pFile->tid = hSelf;
1028 if (pFile->pLock != NULL) {
1029 releaseLockInfo(pFile->pLock);
1030 rc = findLockInfo(pFile, &pFile->pLock, 0);
1031 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
1032 locktypeName(pFile->locktype),
1033 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
1034 return rc;
1035 } else {
1036 return SQLITE_OK;
1037 }
1038}
1039#else /* if not SQLITE_THREADSAFE */
1040 /* On single-threaded builds, ownership transfer is a no-op */
1041# define transferOwnership(X) SQLITE_OK
1042#endif /* SQLITE_THREADSAFE */
1043
aswift5b1a2562008-08-22 00:22:35 +00001044
1045/*
danielk197713adf8a2004-06-03 16:08:41 +00001046** This routine checks if there is a RESERVED lock held on the specified
aswift5b1a2562008-08-22 00:22:35 +00001047** file by this or any other process. If such a lock is held, set *pResOut
1048** to a non-zero value otherwise *pResOut is set to zero. The return value
1049** is set to SQLITE_OK unless an I/O error occurs during lock checking.
danielk197713adf8a2004-06-03 16:08:41 +00001050*/
danielk1977861f7452008-06-05 11:39:11 +00001051static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001052 int rc = SQLITE_OK;
1053 int reserved = 0;
drh054889e2005-11-30 03:20:31 +00001054 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001055
danielk1977861f7452008-06-05 11:39:11 +00001056 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1057
drh054889e2005-11-30 03:20:31 +00001058 assert( pFile );
drh6c7d5c52008-11-21 20:32:33 +00001059 unixEnterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001060
1061 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001062 if( pFile->pLock->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001063 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001064 }
1065
drh2ac3ee92004-06-07 16:27:46 +00001066 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001067 */
aswift5b1a2562008-08-22 00:22:35 +00001068 if( !reserved ){
danielk197713adf8a2004-06-03 16:08:41 +00001069 struct flock lock;
1070 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001071 lock.l_start = RESERVED_BYTE;
1072 lock.l_len = 1;
1073 lock.l_type = F_WRLCK;
aswift5b1a2562008-08-22 00:22:35 +00001074 if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
1075 int tErrno = errno;
1076 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1077 pFile->lastErrno = tErrno;
1078 } else if( lock.l_type!=F_UNLCK ){
1079 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001080 }
1081 }
1082
drh6c7d5c52008-11-21 20:32:33 +00001083 unixLeaveMutex();
aswift5b1a2562008-08-22 00:22:35 +00001084 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk197713adf8a2004-06-03 16:08:41 +00001085
aswift5b1a2562008-08-22 00:22:35 +00001086 *pResOut = reserved;
1087 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001088}
1089
1090/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001091** Lock the file with the lock specified by parameter locktype - one
1092** of the following:
1093**
drh2ac3ee92004-06-07 16:27:46 +00001094** (1) SHARED_LOCK
1095** (2) RESERVED_LOCK
1096** (3) PENDING_LOCK
1097** (4) EXCLUSIVE_LOCK
1098**
drhb3e04342004-06-08 00:47:47 +00001099** Sometimes when requesting one lock state, additional lock states
1100** are inserted in between. The locking might fail on one of the later
1101** transitions leaving the lock state different from what it started but
1102** still short of its goal. The following chart shows the allowed
1103** transitions and the inserted intermediate states:
1104**
1105** UNLOCKED -> SHARED
1106** SHARED -> RESERVED
1107** SHARED -> (PENDING) -> EXCLUSIVE
1108** RESERVED -> (PENDING) -> EXCLUSIVE
1109** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001110**
drha6abd042004-06-09 17:37:22 +00001111** This routine will only increase a lock. Use the sqlite3OsUnlock()
1112** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001113*/
danielk197762079062007-08-15 17:08:46 +00001114static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001115 /* The following describes the implementation of the various locks and
1116 ** lock transitions in terms of the POSIX advisory shared and exclusive
1117 ** lock primitives (called read-locks and write-locks below, to avoid
1118 ** confusion with SQLite lock names). The algorithms are complicated
1119 ** slightly in order to be compatible with windows systems simultaneously
1120 ** accessing the same database file, in case that is ever required.
1121 **
1122 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1123 ** byte', each single bytes at well known offsets, and the 'shared byte
1124 ** range', a range of 510 bytes at a well known offset.
1125 **
1126 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1127 ** byte'. If this is successful, a random byte from the 'shared byte
1128 ** range' is read-locked and the lock on the 'pending byte' released.
1129 **
danielk197790ba3bd2004-06-25 08:32:25 +00001130 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1131 ** A RESERVED lock is implemented by grabbing a write-lock on the
1132 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001133 **
1134 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001135 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1136 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1137 ** obtained, but existing SHARED locks are allowed to persist. A process
1138 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1139 ** This property is used by the algorithm for rolling back a journal file
1140 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001141 **
danielk197790ba3bd2004-06-25 08:32:25 +00001142 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1143 ** implemented by obtaining a write-lock on the entire 'shared byte
1144 ** range'. Since all other locks require a read-lock on one of the bytes
1145 ** within this range, this ensures that no other locks are held on the
1146 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001147 **
1148 ** The reason a single byte cannot be used instead of the 'shared byte
1149 ** range' is that some versions of windows do not support read-locks. By
1150 ** locking a random byte from a range, concurrent SHARED locks may exist
1151 ** even if the locking primitive used is always a write-lock.
1152 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001153 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001154 unixFile *pFile = (unixFile*)id;
drh6c7d5c52008-11-21 20:32:33 +00001155 struct unixLockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001156 struct flock lock;
1157 int s;
1158
drh054889e2005-11-30 03:20:31 +00001159 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001160 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001161 locktypeName(locktype), locktypeName(pFile->locktype),
1162 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001163
1164 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001165 ** unixFile, do nothing. Don't use the end_lock: exit path, as
drh6c7d5c52008-11-21 20:32:33 +00001166 ** unixEnterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001167 */
drh054889e2005-11-30 03:20:31 +00001168 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001169 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001170 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001171 return SQLITE_OK;
1172 }
1173
drhb3e04342004-06-08 00:47:47 +00001174 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001175 */
drh054889e2005-11-30 03:20:31 +00001176 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001177 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001178 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001179
drh054889e2005-11-30 03:20:31 +00001180 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001181 */
drh6c7d5c52008-11-21 20:32:33 +00001182 unixEnterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001183
drh029b44b2006-01-15 00:13:15 +00001184 /* Make sure the current thread owns the pFile.
1185 */
1186 rc = transferOwnership(pFile);
1187 if( rc!=SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00001188 unixLeaveMutex();
drh029b44b2006-01-15 00:13:15 +00001189 return rc;
1190 }
drh64b1bea2006-01-15 02:30:57 +00001191 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001192
danielk1977ad94b582007-08-20 06:44:22 +00001193 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001194 ** handle that precludes the requested lock, return BUSY.
1195 */
drh054889e2005-11-30 03:20:31 +00001196 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001197 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001198 ){
1199 rc = SQLITE_BUSY;
1200 goto end_lock;
1201 }
1202
1203 /* If a SHARED lock is requested, and some thread using this PID already
1204 ** has a SHARED or RESERVED lock, then increment reference counts and
1205 ** return SQLITE_OK.
1206 */
1207 if( locktype==SHARED_LOCK &&
1208 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1209 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001210 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001211 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001212 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001213 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001214 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001215 goto end_lock;
1216 }
1217
danielk197713adf8a2004-06-03 16:08:41 +00001218 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001219
danielk19779a1d0ab2004-06-01 14:09:28 +00001220 lock.l_whence = SEEK_SET;
1221
drh3cde3bb2004-06-12 02:17:14 +00001222 /* A PENDING lock is needed before acquiring a SHARED lock and before
1223 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1224 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001225 */
drh3cde3bb2004-06-12 02:17:14 +00001226 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001227 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001228 ){
danielk1977489468c2004-06-28 08:25:47 +00001229 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001230 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001231 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001232 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001233 int tErrno = errno;
1234 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1235 if( IS_LOCK_ERROR(rc) ){
1236 pFile->lastErrno = tErrno;
1237 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001238 goto end_lock;
1239 }
drh3cde3bb2004-06-12 02:17:14 +00001240 }
1241
1242
1243 /* If control gets to this point, then actually go ahead and make
1244 ** operating system calls for the specified lock.
1245 */
1246 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001247 int tErrno = 0;
drh3cde3bb2004-06-12 02:17:14 +00001248 assert( pLock->cnt==0 );
1249 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001250
drh2ac3ee92004-06-07 16:27:46 +00001251 /* Now get the read-lock */
1252 lock.l_start = SHARED_FIRST;
1253 lock.l_len = SHARED_SIZE;
aswift5b1a2562008-08-22 00:22:35 +00001254 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
1255 tErrno = errno;
1256 }
drh2ac3ee92004-06-07 16:27:46 +00001257 /* Drop the temporary PENDING lock */
1258 lock.l_start = PENDING_BYTE;
1259 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001260 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001261 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
aswift5b1a2562008-08-22 00:22:35 +00001262 if( s != -1 ){
1263 /* This could happen with a network mount */
1264 tErrno = errno;
1265 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1266 if( IS_LOCK_ERROR(rc) ){
1267 pFile->lastErrno = tErrno;
1268 }
1269 goto end_lock;
1270 }
drh2b4b5962005-06-15 17:47:55 +00001271 }
drhe2396a12007-03-29 20:19:58 +00001272 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001273 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1274 if( IS_LOCK_ERROR(rc) ){
1275 pFile->lastErrno = tErrno;
1276 }
drhbbd42a62004-05-22 17:41:58 +00001277 }else{
drh054889e2005-11-30 03:20:31 +00001278 pFile->locktype = SHARED_LOCK;
1279 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001280 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001281 }
drh3cde3bb2004-06-12 02:17:14 +00001282 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1283 /* We are trying for an exclusive lock but another thread in this
1284 ** same process is still holding a shared lock. */
1285 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001286 }else{
drh3cde3bb2004-06-12 02:17:14 +00001287 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001288 ** assumed that there is a SHARED or greater lock on the file
1289 ** already.
1290 */
drh054889e2005-11-30 03:20:31 +00001291 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001292 lock.l_type = F_WRLCK;
1293 switch( locktype ){
1294 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001295 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001296 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001297 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001298 lock.l_start = SHARED_FIRST;
1299 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001300 break;
1301 default:
1302 assert(0);
1303 }
drh054889e2005-11-30 03:20:31 +00001304 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001305 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001306 int tErrno = errno;
1307 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1308 if( IS_LOCK_ERROR(rc) ){
1309 pFile->lastErrno = tErrno;
1310 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001311 }
drhbbd42a62004-05-22 17:41:58 +00001312 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001313
danielk1977ecb2a962004-06-02 06:30:16 +00001314 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001315 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001316 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001317 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001318 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001319 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001320 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001321
1322end_lock:
drh6c7d5c52008-11-21 20:32:33 +00001323 unixLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001324 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001325 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001326 return rc;
1327}
1328
1329/*
drh054889e2005-11-30 03:20:31 +00001330** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001331** must be either NO_LOCK or SHARED_LOCK.
1332**
1333** If the locking level of the file descriptor is already at or below
1334** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001335*/
danielk197762079062007-08-15 17:08:46 +00001336static int unixUnlock(sqlite3_file *id, int locktype){
drh6c7d5c52008-11-21 20:32:33 +00001337 struct unixLockInfo *pLock;
drha6abd042004-06-09 17:37:22 +00001338 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001339 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001340 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001341 int h;
drha6abd042004-06-09 17:37:22 +00001342
drh054889e2005-11-30 03:20:31 +00001343 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001344 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001345 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001346
1347 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001348 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001349 return SQLITE_OK;
1350 }
drhf1a221e2006-01-15 17:27:17 +00001351 if( CHECK_THREADID(pFile) ){
1352 return SQLITE_MISUSE;
1353 }
drh6c7d5c52008-11-21 20:32:33 +00001354 unixEnterMutex();
drh1aa5af12008-03-07 19:51:14 +00001355 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001356 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001357 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001358 if( pFile->locktype>SHARED_LOCK ){
1359 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001360 SimulateIOErrorBenign(1);
1361 SimulateIOError( h=(-1) )
1362 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001363 if( locktype==SHARED_LOCK ){
1364 lock.l_type = F_RDLCK;
1365 lock.l_whence = SEEK_SET;
1366 lock.l_start = SHARED_FIRST;
1367 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001368 if( fcntl(h, F_SETLK, &lock)==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001369 int tErrno = errno;
1370 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1371 if( IS_LOCK_ERROR(rc) ){
1372 pFile->lastErrno = tErrno;
1373 }
1374 goto end_unlock;
drh9c105bb2004-10-02 20:38:28 +00001375 }
1376 }
drhbbd42a62004-05-22 17:41:58 +00001377 lock.l_type = F_UNLCK;
1378 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001379 lock.l_start = PENDING_BYTE;
1380 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001381 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001382 pLock->locktype = SHARED_LOCK;
1383 }else{
aswift5b1a2562008-08-22 00:22:35 +00001384 int tErrno = errno;
1385 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1386 if( IS_LOCK_ERROR(rc) ){
1387 pFile->lastErrno = tErrno;
1388 }
1389 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001390 }
drhbbd42a62004-05-22 17:41:58 +00001391 }
drha6abd042004-06-09 17:37:22 +00001392 if( locktype==NO_LOCK ){
drh6c7d5c52008-11-21 20:32:33 +00001393 struct unixOpenCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001394
drha6abd042004-06-09 17:37:22 +00001395 /* Decrement the shared lock counter. Release the lock using an
1396 ** OS call only when all threads in this same process have released
1397 ** the lock.
1398 */
1399 pLock->cnt--;
1400 if( pLock->cnt==0 ){
1401 lock.l_type = F_UNLCK;
1402 lock.l_whence = SEEK_SET;
1403 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001404 SimulateIOErrorBenign(1);
1405 SimulateIOError( h=(-1) )
1406 SimulateIOErrorBenign(0);
1407 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001408 pLock->locktype = NO_LOCK;
1409 }else{
aswift5b1a2562008-08-22 00:22:35 +00001410 int tErrno = errno;
danielk19775ad6a882008-09-15 04:20:31 +00001411 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
aswift5b1a2562008-08-22 00:22:35 +00001412 if( IS_LOCK_ERROR(rc) ){
1413 pFile->lastErrno = tErrno;
1414 }
drh1aa5af12008-03-07 19:51:14 +00001415 pLock->cnt = 1;
aswift5b1a2562008-08-22 00:22:35 +00001416 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001417 }
drha6abd042004-06-09 17:37:22 +00001418 }
1419
drhbbd42a62004-05-22 17:41:58 +00001420 /* Decrement the count of locks against this same file. When the
1421 ** count reaches zero, close any other file descriptors whose close
1422 ** was deferred because of outstanding locks.
1423 */
drh1aa5af12008-03-07 19:51:14 +00001424 if( rc==SQLITE_OK ){
1425 pOpen = pFile->pOpen;
1426 pOpen->nLock--;
1427 assert( pOpen->nLock>=0 );
1428 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1429 int i;
1430 for(i=0; i<pOpen->nPending; i++){
aswiftaebf4132008-11-21 00:10:35 +00001431 /* close pending fds, but if closing fails don't free the array
1432 ** assign -1 to the successfully closed descriptors and record the
1433 ** error. The next attempt to unlock will try again. */
1434 if( pOpen->aPending[i] < 0 ) continue;
1435 if( close(pOpen->aPending[i]) ){
1436 pFile->lastErrno = errno;
1437 rc = SQLITE_IOERR_CLOSE;
1438 }else{
1439 pOpen->aPending[i] = -1;
1440 }
drh1aa5af12008-03-07 19:51:14 +00001441 }
aswiftaebf4132008-11-21 00:10:35 +00001442 if( rc==SQLITE_OK ){
1443 sqlite3_free(pOpen->aPending);
1444 pOpen->nPending = 0;
1445 pOpen->aPending = 0;
1446 }
drhbbd42a62004-05-22 17:41:58 +00001447 }
drhbbd42a62004-05-22 17:41:58 +00001448 }
1449 }
aswift5b1a2562008-08-22 00:22:35 +00001450
1451end_unlock:
drh6c7d5c52008-11-21 20:32:33 +00001452 unixLeaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001453 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001454 return rc;
drhbbd42a62004-05-22 17:41:58 +00001455}
1456
1457/*
danielk1977e339d652008-06-28 11:23:00 +00001458** This function performs the parts of the "close file" operation
1459** common to all locking schemes. It closes the directory and file
1460** handles, if they are valid, and sets all fields of the unixFile
1461** structure to 0.
1462*/
1463static int closeUnixFile(sqlite3_file *id){
1464 unixFile *pFile = (unixFile*)id;
1465 if( pFile ){
1466 if( pFile->dirfd>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001467 int err = close(pFile->dirfd);
1468 if( err ){
1469 pFile->lastErrno = errno;
1470 return SQLITE_IOERR_DIR_CLOSE;
1471 }else{
1472 pFile->dirfd=-1;
1473 }
danielk1977e339d652008-06-28 11:23:00 +00001474 }
1475 if( pFile->h>=0 ){
aswiftaebf4132008-11-21 00:10:35 +00001476 int err = close(pFile->h);
1477 if( err ){
1478 pFile->lastErrno = errno;
1479 return SQLITE_IOERR_CLOSE;
1480 }
danielk1977e339d652008-06-28 11:23:00 +00001481 }
drh6c7d5c52008-11-21 20:32:33 +00001482#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +00001483 if( pFile->pId ){
1484 if( pFile->isDelete ){
1485 unlink(pFile->pId->zCanonicalName);
chw97185482008-11-17 08:05:31 +00001486 }
drh107886a2008-11-21 22:21:50 +00001487 vxworksReleaseFileId(pFile->pId);
1488 pFile->pId = 0;
chw97185482008-11-17 08:05:31 +00001489 }
1490#endif
danielk1977e339d652008-06-28 11:23:00 +00001491 OSTRACE2("CLOSE %-3d\n", pFile->h);
1492 OpenCounter(-1);
1493 memset(pFile, 0, sizeof(unixFile));
1494 }
1495 return SQLITE_OK;
1496}
1497
1498/*
danielk1977e3026632004-06-22 11:29:02 +00001499** Close a file.
1500*/
danielk197762079062007-08-15 17:08:46 +00001501static int unixClose(sqlite3_file *id){
aswiftaebf4132008-11-21 00:10:35 +00001502 int rc = SQLITE_OK;
danielk1977e339d652008-06-28 11:23:00 +00001503 if( id ){
1504 unixFile *pFile = (unixFile *)id;
1505 unixUnlock(id, NO_LOCK);
drh6c7d5c52008-11-21 20:32:33 +00001506 unixEnterMutex();
danielk19776cb427f2008-06-30 10:16:04 +00001507 if( pFile->pOpen && pFile->pOpen->nLock ){
danielk1977e339d652008-06-28 11:23:00 +00001508 /* If there are outstanding locks, do not actually close the file just
1509 ** yet because that would clear those locks. Instead, add the file
1510 ** descriptor to pOpen->aPending. It will be automatically closed when
1511 ** the last lock is cleared.
1512 */
1513 int *aNew;
drh6c7d5c52008-11-21 20:32:33 +00001514 struct unixOpenCnt *pOpen = pFile->pOpen;
drhda0e7682008-07-30 15:27:54 +00001515 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e339d652008-06-28 11:23:00 +00001516 if( aNew==0 ){
1517 /* If a malloc fails, just leak the file descriptor */
1518 }else{
1519 pOpen->aPending = aNew;
1520 pOpen->aPending[pOpen->nPending] = pFile->h;
1521 pOpen->nPending++;
1522 pFile->h = -1;
1523 }
danielk1977e3026632004-06-22 11:29:02 +00001524 }
danielk1977e339d652008-06-28 11:23:00 +00001525 releaseLockInfo(pFile->pLock);
1526 releaseOpenCnt(pFile->pOpen);
aswiftaebf4132008-11-21 00:10:35 +00001527 rc = closeUnixFile(id);
drh6c7d5c52008-11-21 20:32:33 +00001528 unixLeaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00001529 }
aswiftaebf4132008-11-21 00:10:35 +00001530 return rc;
danielk1977e3026632004-06-22 11:29:02 +00001531}
1532
drh734c9862008-11-28 15:37:20 +00001533/************** End of the posix advisory lock implementation *****************
1534******************************************************************************/
drhbfe66312006-10-03 17:40:40 +00001535
drh734c9862008-11-28 15:37:20 +00001536/******************************************************************************
1537****************************** No-op Locking **********************************
1538**
1539** Of the various locking implementations available, this is by far the
1540** simplest: locking is ignored. No attempt is made to lock the database
1541** file for reading or writing.
1542**
1543** This locking mode is appropriate for use on read-only databases
1544** (ex: databases that are burned into CD-ROM, for example.) It can
1545** also be used if the application employs some external mechanism to
1546** prevent simultaneous access of the same database by two or more
1547** database connections. But there is a serious risk of database
1548** corruption if this locking mode is used in situations where multiple
1549** database connections are accessing the same database file at the same
1550** time and one or more of those connections are writing.
1551*/
drhbfe66312006-10-03 17:40:40 +00001552
1553/*
drh734c9862008-11-28 15:37:20 +00001554** The nolockLockingContext is void
1555*/
1556typedef void nolockLockingContext;
1557
1558static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
1559 UNUSED_PARAMETER(NotUsed);
1560 *pResOut = 0;
1561 return SQLITE_OK;
1562}
1563
1564static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
1565 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1566 return SQLITE_OK;
1567}
1568
1569static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
1570 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1571 return SQLITE_OK;
1572}
1573
1574/*
1575** Close a file.
1576*/
1577static int nolockClose(sqlite3_file *id) {
1578 int rc;
1579 if( OS_VXWORKS ) unixEnterMutex();
1580 rc = closeUnixFile(id);
1581 if( OS_VXWORKS ) unixLeaveMutex();
1582 return rc;
1583}
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/*
1769** Close a file.
1770*/
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 }
1778 if( OS_VXWORKS ) unixEnterMutex();
1779 rc = closeUnixFile(id);
1780 if( OS_VXWORKS ) unixLeaveMutex();
1781 return rc;
1782}
1783/****************** End of the dot-file lock implementation *******************
1784******************************************************************************/
1785
1786/******************************************************************************
1787************************** Begin flock Locking ********************************
1788**
1789** Use the flock() system call to do file locking.
1790**
1791** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
1792** compiling for VXWORKS.
1793*/
1794#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00001795#include <sys/file.h>
drh734c9862008-11-28 15:37:20 +00001796
1797/*
1798** The flockLockingContext is not used
1799*/
1800typedef void flockLockingContext;
1801
1802/* flock-style reserved lock checking following the behavior of
1803 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
1804static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
1805 int rc = SQLITE_OK;
1806 int reserved = 0;
1807 unixFile *pFile = (unixFile*)id;
1808
1809 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1810
1811 assert( pFile );
1812
1813 /* Check if a thread in this process holds such a lock */
1814 if( pFile->locktype>SHARED_LOCK ){
1815 reserved = 1;
1816 }
1817
1818 /* Otherwise see if some other process holds it. */
1819 if( !reserved ){
1820 /* attempt to get the lock */
1821 int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
1822 if( !lrc ){
1823 /* got the lock, unlock it */
1824 lrc = flock(pFile->h, LOCK_UN);
1825 if ( lrc ) {
1826 int tErrno = errno;
1827 /* unlock failed with an error */
1828 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1829 if( IS_LOCK_ERROR(lrc) ){
1830 pFile->lastErrno = tErrno;
1831 rc = lrc;
1832 }
1833 }
1834 } else {
1835 int tErrno = errno;
1836 reserved = 1;
1837 /* someone else might have it reserved */
1838 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1839 if( IS_LOCK_ERROR(lrc) ){
1840 pFile->lastErrno = tErrno;
1841 rc = lrc;
1842 }
1843 }
1844 }
1845 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
1846
1847#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
1848 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
1849 rc = SQLITE_OK;
1850 reserved=1;
1851 }
1852#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
1853 *pResOut = reserved;
1854 return rc;
1855}
1856
1857static int flockLock(sqlite3_file *id, int locktype) {
1858 int rc = SQLITE_OK;
drh734c9862008-11-28 15:37:20 +00001859 unixFile *pFile = (unixFile*)id;
1860
1861 assert( pFile );
1862
1863 /* if we already have a lock, it is exclusive.
1864 ** Just adjust level and punt on outta here. */
1865 if (pFile->locktype > NO_LOCK) {
1866 pFile->locktype = locktype;
1867 return SQLITE_OK;
1868 }
1869
1870 /* grab an exclusive lock */
1871
1872 if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
1873 int tErrno = errno;
1874 /* didn't get, must be busy */
1875 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1876 if( IS_LOCK_ERROR(rc) ){
1877 pFile->lastErrno = tErrno;
1878 }
1879 } else {
1880 /* got it, set the type and return ok */
1881 pFile->locktype = locktype;
1882 }
1883 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
1884 rc==SQLITE_OK ? "ok" : "failed");
1885#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
1886 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
1887 rc = SQLITE_BUSY;
1888 }
1889#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
1890 return rc;
1891}
1892
1893static int flockUnlock(sqlite3_file *id, int locktype) {
1894 unixFile *pFile = (unixFile*)id;
1895
1896 assert( pFile );
1897 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
1898 pFile->locktype, getpid());
1899 assert( locktype<=SHARED_LOCK );
1900
1901 /* no-op if possible */
1902 if( pFile->locktype==locktype ){
1903 return SQLITE_OK;
1904 }
1905
1906 /* shared can just be set because we always have an exclusive */
1907 if (locktype==SHARED_LOCK) {
1908 pFile->locktype = locktype;
1909 return SQLITE_OK;
1910 }
1911
1912 /* no, really, unlock. */
1913 int rc = flock(pFile->h, LOCK_UN);
1914 if (rc) {
1915 int r, tErrno = errno;
1916 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1917 if( IS_LOCK_ERROR(r) ){
1918 pFile->lastErrno = tErrno;
1919 }
1920#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
1921 if( (r & SQLITE_IOERR) == SQLITE_IOERR ){
1922 r = SQLITE_BUSY;
1923 }
1924#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
1925
1926 return r;
1927 } else {
1928 pFile->locktype = NO_LOCK;
1929 return SQLITE_OK;
1930 }
1931}
1932
1933/*
1934** Close a file.
1935*/
1936static int flockClose(sqlite3_file *id) {
1937 if( id ){
1938 flockUnlock(id, NO_LOCK);
1939 }
1940 return closeUnixFile(id);
1941}
1942
1943#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
1944
1945/******************* End of the flock lock implementation *********************
1946******************************************************************************/
1947
1948/******************************************************************************
1949************************ Begin Named Semaphore Locking ************************
1950**
1951** Named semaphore locking is only supported on VxWorks.
1952*/
1953#if OS_VXWORKS
1954
1955/* Namedsem-style reserved lock checking following the behavior of
1956** unixCheckReservedLock, see the unixCheckReservedLock function comments */
1957static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
1958 int rc = SQLITE_OK;
1959 int reserved = 0;
1960 unixFile *pFile = (unixFile*)id;
1961
1962 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1963
1964 assert( pFile );
1965
1966 /* Check if a thread in this process holds such a lock */
1967 if( pFile->locktype>SHARED_LOCK ){
1968 reserved = 1;
1969 }
1970
1971 /* Otherwise see if some other process holds it. */
1972 if( !reserved ){
1973 sem_t *pSem = pFile->pOpen->pSem;
1974 struct stat statBuf;
1975
1976 if( sem_trywait(pSem)==-1 ){
1977 int tErrno = errno;
1978 if( EAGAIN != tErrno ){
1979 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1980 pFile->lastErrno = tErrno;
1981 } else {
1982 /* someone else has the lock when we are in NO_LOCK */
1983 reserved = (pFile->locktype < SHARED_LOCK);
1984 }
1985 }else{
1986 /* we could have it if we want it */
1987 sem_post(pSem);
1988 }
1989 }
1990 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
1991
1992 *pResOut = reserved;
1993 return rc;
1994}
1995
1996static int semLock(sqlite3_file *id, int locktype) {
1997 unixFile *pFile = (unixFile*)id;
1998 int fd;
1999 sem_t *pSem = pFile->pOpen->pSem;
2000 int rc = SQLITE_OK;
2001
2002 /* if we already have a lock, it is exclusive.
2003 ** Just adjust level and punt on outta here. */
2004 if (pFile->locktype > NO_LOCK) {
2005 pFile->locktype = locktype;
2006 rc = SQLITE_OK;
2007 goto sem_end_lock;
2008 }
2009
2010 /* lock semaphore now but bail out when already locked. */
2011 if( sem_trywait(pSem)==-1 ){
2012 rc = SQLITE_BUSY;
2013 goto sem_end_lock;
2014 }
2015
2016 /* got it, set the type and return ok */
2017 pFile->locktype = locktype;
2018
2019 sem_end_lock:
2020 return rc;
2021}
2022
2023static int semUnlock(sqlite3_file *id, int locktype) {
2024 unixFile *pFile = (unixFile*)id;
2025 sem_t *pSem = pFile->pOpen->pSem;
2026
2027 assert( pFile );
2028 assert( pSem );
2029 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2030 pFile->locktype, getpid());
2031 assert( locktype<=SHARED_LOCK );
2032
2033 /* no-op if possible */
2034 if( pFile->locktype==locktype ){
2035 return SQLITE_OK;
2036 }
2037
2038 /* shared can just be set because we always have an exclusive */
2039 if (locktype==SHARED_LOCK) {
2040 pFile->locktype = locktype;
2041 return SQLITE_OK;
2042 }
2043
2044 /* no, really unlock. */
2045 if ( sem_post(pSem)==-1 ) {
2046 int rc, tErrno = errno;
2047 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2048 if( IS_LOCK_ERROR(rc) ){
2049 pFile->lastErrno = tErrno;
2050 }
2051 return rc;
2052 }
2053 pFile->locktype = NO_LOCK;
2054 return SQLITE_OK;
2055}
2056
2057/*
2058 ** Close a file.
drhbfe66312006-10-03 17:40:40 +00002059 */
drh734c9862008-11-28 15:37:20 +00002060static int semClose(sqlite3_file *id) {
2061 if( id ){
2062 unixFile *pFile = (unixFile*)id;
2063 semUnlock(id, NO_LOCK);
2064 assert( pFile );
2065 unixEnterMutex();
2066 releaseLockInfo(pFile->pLock);
2067 releaseOpenCnt(pFile->pOpen);
2068 closeUnixFile(id);
2069 unixLeaveMutex();
2070 }
2071 return SQLITE_OK;
2072}
2073
2074#endif /* OS_VXWORKS */
2075/*
2076** Named semaphore locking is only available on VxWorks.
2077**
2078*************** End of the named semaphore lock implementation ****************
2079******************************************************************************/
2080
2081
2082/******************************************************************************
2083*************************** Begin AFP Locking *********************************
2084**
2085** AFP is the Apple Filing Protocol. AFP is a network filesystem found
2086** on Apple Macintosh computers - both OS9 and OSX.
2087**
2088** Third-party implementations of AFP are available. But this code here
2089** only works on OSX.
2090*/
2091
2092#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
2093/*
2094** The afpLockingContext structure contains all afp lock specific state
2095*/
drhbfe66312006-10-03 17:40:40 +00002096typedef struct afpLockingContext afpLockingContext;
2097struct afpLockingContext {
aswiftaebf4132008-11-21 00:10:35 +00002098 unsigned long long sharedByte;
2099 const char *dbPath;
drhbfe66312006-10-03 17:40:40 +00002100};
2101
2102struct ByteRangeLockPB2
2103{
2104 unsigned long long offset; /* offset to first byte to lock */
2105 unsigned long long length; /* nbr of bytes to lock */
2106 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
2107 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
2108 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
2109 int fd; /* file desc to assoc this lock with */
2110};
2111
drhfd131da2007-08-07 17:13:03 +00002112#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00002113
danielk1977ad94b582007-08-20 06:44:22 +00002114/*
aswift5b1a2562008-08-22 00:22:35 +00002115 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
2116 */
danielk1977ad94b582007-08-20 06:44:22 +00002117static int _AFPFSSetLock(
2118 const char *path,
aswift5b1a2562008-08-22 00:22:35 +00002119 unixFile *pFile,
danielk1977ad94b582007-08-20 06:44:22 +00002120 unsigned long long offset,
2121 unsigned long long length,
2122 int setLockFlag
2123){
drhfd131da2007-08-07 17:13:03 +00002124 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00002125 int err;
2126
2127 pb.unLockFlag = setLockFlag ? 0 : 1;
2128 pb.startEndFlag = 0;
2129 pb.offset = offset;
2130 pb.length = length;
aswift5b1a2562008-08-22 00:22:35 +00002131 pb.fd = pFile->h;
aswiftaebf4132008-11-21 00:10:35 +00002132 //SimulateIOErrorBenign(1);
2133 //SimulateIOError( pb.fd=(-1) )
2134 //SimulateIOErrorBenign(0);
2135
2136 OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
drh734c9862008-11-28 15:37:20 +00002137 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
2138 offset, length);
drhbfe66312006-10-03 17:40:40 +00002139 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
2140 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00002141 int rc;
2142 int tErrno = errno;
drh734c9862008-11-28 15:37:20 +00002143 OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
2144 path, tErrno, strerror(tErrno));
aswiftaebf4132008-11-21 00:10:35 +00002145#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
2146 rc = SQLITE_BUSY;
2147#else
drh734c9862008-11-28 15:37:20 +00002148 rc = sqliteErrorFromPosixError(tErrno,
2149 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
aswiftaebf4132008-11-21 00:10:35 +00002150#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
aswift5b1a2562008-08-22 00:22:35 +00002151 if( IS_LOCK_ERROR(rc) ){
2152 pFile->lastErrno = tErrno;
2153 }
2154 return rc;
drhbfe66312006-10-03 17:40:40 +00002155 } else {
aswift5b1a2562008-08-22 00:22:35 +00002156 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002157 }
2158}
2159
aswift5b1a2562008-08-22 00:22:35 +00002160/* AFP-style reserved lock checking following the behavior of
2161** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002162static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00002163 int rc = SQLITE_OK;
2164 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002165 unixFile *pFile = (unixFile*)id;
2166
aswift5b1a2562008-08-22 00:22:35 +00002167 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2168
2169 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00002170 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2171
2172 /* Check if a thread in this process holds such a lock */
2173 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002174 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00002175 }
2176
2177 /* Otherwise see if some other process holds it.
2178 */
aswift5b1a2562008-08-22 00:22:35 +00002179 if( !reserved ){
2180 /* lock the RESERVED byte */
aswiftaebf4132008-11-21 00:10:35 +00002181 int lrc = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
aswift5b1a2562008-08-22 00:22:35 +00002182 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00002183 /* if we succeeded in taking the reserved lock, unlock it to restore
2184 ** the original state */
aswiftaebf4132008-11-21 00:10:35 +00002185 lrc = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
aswift5b1a2562008-08-22 00:22:35 +00002186 } else {
2187 /* if we failed to get the lock then someone else must have it */
2188 reserved = 1;
2189 }
2190 if( IS_LOCK_ERROR(lrc) ){
2191 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00002192 }
2193 }
drhbfe66312006-10-03 17:40:40 +00002194
aswift5b1a2562008-08-22 00:22:35 +00002195 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
2196
2197 *pResOut = reserved;
2198 return rc;
drhbfe66312006-10-03 17:40:40 +00002199}
2200
2201/* AFP-style locking following the behavior of unixLock, see the unixLock
2202** function comments for details of lock management. */
danielk1977e339d652008-06-28 11:23:00 +00002203static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00002204 int rc = SQLITE_OK;
2205 unixFile *pFile = (unixFile*)id;
2206 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002207
2208 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002209 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00002210 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
2211
drhbfe66312006-10-03 17:40:40 +00002212 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00002213 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
drh6c7d5c52008-11-21 20:32:33 +00002214 ** unixEnterMutex() hasn't been called yet.
drh339eb0b2008-03-07 15:34:11 +00002215 */
drhbfe66312006-10-03 17:40:40 +00002216 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00002217 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00002218 locktypeName(locktype));
2219 return SQLITE_OK;
2220 }
2221
2222 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00002223 */
drhbfe66312006-10-03 17:40:40 +00002224 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
2225 assert( locktype!=PENDING_LOCK );
2226 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
2227
2228 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00002229 */
drh6c7d5c52008-11-21 20:32:33 +00002230 unixEnterMutex();
drhbfe66312006-10-03 17:40:40 +00002231
2232 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00002233 */
drhbfe66312006-10-03 17:40:40 +00002234 rc = transferOwnership(pFile);
2235 if( rc!=SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00002236 unixLeaveMutex();
drhbfe66312006-10-03 17:40:40 +00002237 return rc;
2238 }
2239
2240 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00002241 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2242 ** be released.
2243 */
drhbfe66312006-10-03 17:40:40 +00002244 if( locktype==SHARED_LOCK
2245 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00002246 ){
2247 int failed;
aswiftaebf4132008-11-21 00:10:35 +00002248 failed = _AFPFSSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00002249 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00002250 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002251 goto afp_end_lock;
2252 }
2253 }
2254
2255 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00002256 ** operating system calls for the specified lock.
2257 */
drhbfe66312006-10-03 17:40:40 +00002258 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00002259 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00002260
aswift5b1a2562008-08-22 00:22:35 +00002261 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00002262 /* note that the quality of the randomness doesn't matter that much */
2263 lk = random();
aswiftaebf4132008-11-21 00:10:35 +00002264 context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
2265 lrc1 = _AFPFSSetLock(context->dbPath, pFile,
2266 SHARED_FIRST+context->sharedByte, 1, 1);
aswift5b1a2562008-08-22 00:22:35 +00002267 if( IS_LOCK_ERROR(lrc1) ){
2268 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00002269 }
aswift5b1a2562008-08-22 00:22:35 +00002270 /* Drop the temporary PENDING lock */
aswiftaebf4132008-11-21 00:10:35 +00002271 lrc2 = _AFPFSSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00002272
aswift5b1a2562008-08-22 00:22:35 +00002273 if( IS_LOCK_ERROR(lrc1) ) {
2274 pFile->lastErrno = lrc1Errno;
2275 rc = lrc1;
2276 goto afp_end_lock;
2277 } else if( IS_LOCK_ERROR(lrc2) ){
2278 rc = lrc2;
2279 goto afp_end_lock;
2280 } else if( lrc1 != SQLITE_OK ) {
2281 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00002282 } else {
2283 pFile->locktype = SHARED_LOCK;
aswiftaebf4132008-11-21 00:10:35 +00002284 pFile->pOpen->nLock++;
drhbfe66312006-10-03 17:40:40 +00002285 }
2286 }else{
2287 /* The request was for a RESERVED or EXCLUSIVE lock. It is
2288 ** assumed that there is a SHARED or greater lock on the file
2289 ** already.
2290 */
2291 int failed = 0;
2292 assert( 0!=pFile->locktype );
2293 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
2294 /* Acquire a RESERVED lock */
aswiftaebf4132008-11-21 00:10:35 +00002295 failed = _AFPFSSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00002296 }
2297 if (!failed && locktype == EXCLUSIVE_LOCK) {
2298 /* Acquire an EXCLUSIVE lock */
2299
2300 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00002301 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00002302 */
aswiftaebf4132008-11-21 00:10:35 +00002303 if( !(failed = _AFPFSSetLock(context->dbPath, pFile, SHARED_FIRST +
2304 context->sharedByte, 1, 0)) ){
2305 int failed2 = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002306 /* now attemmpt to get the exclusive lock range */
aswiftaebf4132008-11-21 00:10:35 +00002307 failed = _AFPFSSetLock(context->dbPath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00002308 SHARED_SIZE, 1);
aswiftaebf4132008-11-21 00:10:35 +00002309 if( failed && (failed2 = _AFPFSSetLock(context->dbPath, pFile,
2310 SHARED_FIRST + context->sharedByte, 1, 1)) ){
2311 /* Can't reestablish the shared lock. Sqlite can't deal, this is
2312 ** a critical I/O error
2313 */
2314 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
2315 SQLITE_IOERR_LOCK;
2316 goto afp_end_lock;
2317 }
2318 }else{
aswift5b1a2562008-08-22 00:22:35 +00002319 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002320 }
2321 }
aswift5b1a2562008-08-22 00:22:35 +00002322 if( failed ){
2323 rc = failed;
drhbfe66312006-10-03 17:40:40 +00002324 }
2325 }
2326
2327 if( rc==SQLITE_OK ){
2328 pFile->locktype = locktype;
2329 }else if( locktype==EXCLUSIVE_LOCK ){
2330 pFile->locktype = PENDING_LOCK;
2331 }
2332
2333afp_end_lock:
drh6c7d5c52008-11-21 20:32:33 +00002334 unixLeaveMutex();
drh4f0c5872007-03-26 22:05:01 +00002335 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00002336 rc==SQLITE_OK ? "ok" : "failed");
2337 return rc;
2338}
2339
2340/*
drh339eb0b2008-03-07 15:34:11 +00002341** Lower the locking level on file descriptor pFile to locktype. locktype
2342** must be either NO_LOCK or SHARED_LOCK.
2343**
2344** If the locking level of the file descriptor is already at or below
2345** the requested locking level, this routine is a no-op.
2346*/
danielk1977e339d652008-06-28 11:23:00 +00002347static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002348 int rc = SQLITE_OK;
2349 unixFile *pFile = (unixFile*)id;
aswiftaebf4132008-11-21 00:10:35 +00002350 afpLockingContext *pCtx = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002351
2352 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00002353 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00002354 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00002355
drhbfe66312006-10-03 17:40:40 +00002356 assert( locktype<=SHARED_LOCK );
2357 if( pFile->locktype<=locktype ){
2358 return SQLITE_OK;
2359 }
2360 if( CHECK_THREADID(pFile) ){
2361 return SQLITE_MISUSE;
2362 }
drh6c7d5c52008-11-21 20:32:33 +00002363 unixEnterMutex();
drhbfe66312006-10-03 17:40:40 +00002364 if( pFile->locktype>SHARED_LOCK ){
aswiftaebf4132008-11-21 00:10:35 +00002365
2366 if( pFile->locktype==EXCLUSIVE_LOCK ){
2367 rc = _AFPFSSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
2368 if( rc==SQLITE_OK && locktype==SHARED_LOCK ){
2369 /* only re-establish the shared lock if necessary */
2370 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
2371 rc = _AFPFSSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1);
2372 }
2373 }
2374 if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
2375 rc = _AFPFSSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0);
2376 }
2377 if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){
2378 rc = _AFPFSSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0);
2379 }
2380 }else if( locktype==NO_LOCK ){
2381 /* clear the shared lock */
2382 int sharedLockByte = SHARED_FIRST+pCtx->sharedByte;
2383 rc = _AFPFSSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0);
2384 }
drhbfe66312006-10-03 17:40:40 +00002385
aswiftaebf4132008-11-21 00:10:35 +00002386 if( rc==SQLITE_OK ){
2387 if( locktype==NO_LOCK ){
drh6c7d5c52008-11-21 20:32:33 +00002388 struct unixOpenCnt *pOpen = pFile->pOpen;
aswiftaebf4132008-11-21 00:10:35 +00002389 pOpen->nLock--;
2390 assert( pOpen->nLock>=0 );
2391 if( pOpen->nLock==0 && pOpen->nPending>0 ){
2392 int i;
2393 for(i=0; i<pOpen->nPending; i++){
2394 if( pOpen->aPending[i] < 0 ) continue;
2395 if( close(pOpen->aPending[i]) ){
2396 pFile->lastErrno = errno;
2397 rc = SQLITE_IOERR_CLOSE;
2398 }else{
2399 pOpen->aPending[i] = -1;
drhbfe66312006-10-03 17:40:40 +00002400 }
aswiftaebf4132008-11-21 00:10:35 +00002401 }
2402 if( rc==SQLITE_OK ){
2403 sqlite3_free(pOpen->aPending);
2404 pOpen->nPending = 0;
2405 pOpen->aPending = 0;
2406 }
drhbfe66312006-10-03 17:40:40 +00002407 }
2408 }
drhbfe66312006-10-03 17:40:40 +00002409 }
aswiftaebf4132008-11-21 00:10:35 +00002410end_afpunlock:
drh6c7d5c52008-11-21 20:32:33 +00002411 unixLeaveMutex();
aswiftaebf4132008-11-21 00:10:35 +00002412 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002413 return rc;
2414}
2415
2416/*
drh339eb0b2008-03-07 15:34:11 +00002417** Close a file & cleanup AFP specific locking context
2418*/
danielk1977e339d652008-06-28 11:23:00 +00002419static int afpClose(sqlite3_file *id) {
2420 if( id ){
2421 unixFile *pFile = (unixFile*)id;
2422 afpUnlock(id, NO_LOCK);
drh6c7d5c52008-11-21 20:32:33 +00002423 unixEnterMutex();
aswiftaebf4132008-11-21 00:10:35 +00002424 if( pFile->pOpen && pFile->pOpen->nLock ){
2425 /* If there are outstanding locks, do not actually close the file just
drh734c9862008-11-28 15:37:20 +00002426 ** yet because that would clear those locks. Instead, add the file
2427 ** descriptor to pOpen->aPending. It will be automatically closed when
2428 ** the last lock is cleared.
2429 */
aswiftaebf4132008-11-21 00:10:35 +00002430 int *aNew;
drh6c7d5c52008-11-21 20:32:33 +00002431 struct unixOpenCnt *pOpen = pFile->pOpen;
aswiftaebf4132008-11-21 00:10:35 +00002432 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
2433 if( aNew==0 ){
2434 /* If a malloc fails, just leak the file descriptor */
2435 }else{
2436 pOpen->aPending = aNew;
2437 pOpen->aPending[pOpen->nPending] = pFile->h;
2438 pOpen->nPending++;
2439 pFile->h = -1;
2440 }
2441 }
2442 releaseOpenCnt(pFile->pOpen);
danielk1977e339d652008-06-28 11:23:00 +00002443 sqlite3_free(pFile->lockingContext);
aswiftaebf4132008-11-21 00:10:35 +00002444 closeUnixFile(id);
drh6c7d5c52008-11-21 20:32:33 +00002445 unixLeaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00002446 }
aswiftaebf4132008-11-21 00:10:35 +00002447 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002448}
2449
drh734c9862008-11-28 15:37:20 +00002450#endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
2451/*
2452** The code above is the AFP lock implementation. The code is specific
2453** to MacOSX and does not work on other unix platforms. No alternative
2454** is available. If you don't compile for a mac, then the "unix-afp"
2455** VFS is not available.
2456**
2457********************* End of the AFP lock implementation **********************
2458******************************************************************************/
drhbfe66312006-10-03 17:40:40 +00002459
drh734c9862008-11-28 15:37:20 +00002460/******************************************************************************
2461************************** Begin Proxy Locking ********************************
2462**
2463**
2464** The default locking schemes in SQLite use byte-range locks on the
2465** database file to coordinate safe, concurrent access by multiple readers
2466** and writers [http://sqlite.org/lockingv3.html]. The five file locking
2467** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
2468** as POSIX read & write locks over fixed set of locations (via fsctl),
2469** on AFP and SMB only exclusive byte-range locks are available via fsctl
2470** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
2471** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
2472** address in the shared range is taken for a SHARED lock, the entire
2473** shared range is taken for an EXCLUSIVE lock):
2474**
2475** PENDING_BYTE 0x40000000
2476** RESERVED_BYTE 0x40000001
2477** SHARED_RANGE 0x40000002 -> 0x40000200
2478**
2479** This works well on the local file system, but shows a nearly 100x
2480** slowdown in read performance on AFP because the AFP client disables
2481** the read cache when byte-range locks are present. Enabling the read
2482** cache exposes a cache coherency problem that is present on all OS X
2483** supported network file systems. NFS and AFP both observe the
2484** close-to-open semantics for ensuring cache coherency
2485** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
2486** address the requirements for concurrent database access by multiple
2487** readers and writers
2488** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
2489**
2490** To address the performance and cache coherency issues, proxy file locking
2491** changes the way database access is controlled by limiting access to a
2492** single host at a time and moving file locks off of the database file
2493** and onto a proxy file on the local file system.
2494**
2495**
2496** Using proxy locks
2497** -----------------
2498**
2499** C APIs
2500**
2501** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
2502** <proxy_path> | ":auto:");
2503** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
2504**
2505**
2506** SQL pragmas
2507**
2508** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
2509** PRAGMA [database.]lock_proxy_file
2510**
2511** Specifying ":auto:" means that if there is a conch file with a matching
2512** host ID in it, the proxy path in the conch file will be used, otherwise
2513** a proxy path based on the user's temp dir
2514** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
2515** actual proxy file name is generated from the name and path of the
2516** database file. For example:
2517**
2518** For database path "/Users/me/foo.db"
2519** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
2520**
2521** Once a lock proxy is configured for a database connection, it can not
2522** be removed, however it may be switched to a different proxy path via
2523** the above APIs (assuming the conch file is not being held by another
2524** connection or process).
2525**
2526**
2527** How proxy locking works
2528** -----------------------
2529**
2530** Proxy file locking relies primarily on two new supporting files:
2531**
2532** * conch file to limit access to the database file to a single host
2533** at a time
2534**
2535** * proxy file to act as a proxy for the advisory locks normally
2536** taken on the database
2537**
2538** The conch file - to use a proxy file, sqlite must first "hold the conch"
2539** by taking an sqlite-style shared lock on the conch file, reading the
2540** contents and comparing the host's unique host ID (see below) and lock
2541** proxy path against the values stored in the conch. The conch file is
2542** stored in the same directory as the database file and the file name
2543** is patterned after the database file name as ".<databasename>-conch".
2544** If the conch file does not exist, or it's contents do not match the
2545** host ID and/or proxy path, then the lock is escalated to an exclusive
2546** lock and the conch file contents is updated with the host ID and proxy
2547** path and the lock is downgraded to a shared lock again. If the conch
2548** is held by another process (with a shared lock), the exclusive lock
2549** will fail and SQLITE_BUSY is returned.
2550**
2551** The proxy file - a single-byte file used for all advisory file locks
2552** normally taken on the database file. This allows for safe sharing
2553** of the database file for multiple readers and writers on the same
2554** host (the conch ensures that they all use the same local lock file).
2555**
2556** There is a third file - the host ID file - used as a persistent record
2557** of a unique identifier for the host, a 128-byte unique host id file
2558** in the path defined by the HOSTIDPATH macro (default value is
2559** /Library/Caches/.com.apple.sqliteConchHostId).
2560**
2561** Requesting the lock proxy does not immediately take the conch, it is
2562** only taken when the first request to lock database file is made.
2563** This matches the semantics of the traditional locking behavior, where
2564** opening a connection to a database file does not take a lock on it.
2565** The shared lock and an open file descriptor are maintained until
2566** the connection to the database is closed.
2567**
2568** The proxy file and the lock file are never deleted so they only need
2569** to be created the first time they are used.
2570**
2571** Configuration options
2572** ---------------------
2573**
2574** SQLITE_PREFER_PROXY_LOCKING
2575**
2576** Database files accessed on non-local file systems are
2577** automatically configured for proxy locking, lock files are
2578** named automatically using the same logic as
2579** PRAGMA lock_proxy_file=":auto:"
2580**
2581** SQLITE_PROXY_DEBUG
2582**
2583** Enables the logging of error messages during host id file
2584** retrieval and creation
2585**
2586** HOSTIDPATH
2587**
2588** Overrides the default host ID file path location
2589**
2590** LOCKPROXYDIR
2591**
2592** Overrides the default directory used for lock proxy files that
2593** are named automatically via the ":auto:" setting
2594**
2595** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
2596**
2597** Permissions to use when creating a directory for storing the
2598** lock proxy files, only used when LOCKPROXYDIR is not set.
2599**
2600**
2601** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
2602** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
2603** force proxy locking to be used for every database file opened, and 0
2604** will force automatic proxy locking to be disabled for all database
2605** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
2606** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
2607*/
drhbfe66312006-10-03 17:40:40 +00002608
2609/*
drh734c9862008-11-28 15:37:20 +00002610** Proxy locking is only available on MacOSX
drh339eb0b2008-03-07 15:34:11 +00002611*/
drh734c9862008-11-28 15:37:20 +00002612#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +00002613
danielk1977861f7452008-06-05 11:39:11 +00002614
drh734c9862008-11-28 15:37:20 +00002615static int getDbPathForUnixFile(unixFile *pFile, char *dbPath);
2616static int getLockPath(const char *dbPath, char *lPath, size_t maxLen);
drh734c9862008-11-28 15:37:20 +00002617static int createProxyUnixFile(const char *path, unixFile **ppFile);
2618static int fillInUnixFile(sqlite3_vfs *pVfs, int h, int dirfd, sqlite3_file *pId, const char *zFilename, int noLock, int isDelete);
2619static int takeConch(unixFile *pFile);
2620static int releaseConch(unixFile *pFile);
2621static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf);
drhbfe66312006-10-03 17:40:40 +00002622
drh339eb0b2008-03-07 15:34:11 +00002623
drh734c9862008-11-28 15:37:20 +00002624#ifdef SQLITE_TEST
2625/* simulate multiple hosts by creating unique hostid file paths */
2626int sqlite3_hostid_num = 0;
chw97185482008-11-17 08:05:31 +00002627#endif
drhbfe66312006-10-03 17:40:40 +00002628
2629/*
drh734c9862008-11-28 15:37:20 +00002630** The proxyLockingContext has the path and file structures for the remote
2631** and local proxy files in it
2632*/
2633typedef struct proxyLockingContext proxyLockingContext;
2634struct proxyLockingContext {
2635 unixFile *conchFile;
2636 char *conchFilePath;
2637 unixFile *lockProxy;
2638 char *lockProxyPath;
2639 char *dbPath;
2640 int conchHeld;
2641 void *oldLockingContext; /* preserve the original locking context for close */
2642 sqlite3_io_methods const *pOldMethod; /* ditto pMethod */
2643};
drhbfe66312006-10-03 17:40:40 +00002644
aswiftaebf4132008-11-21 00:10:35 +00002645
2646static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
2647 unixFile *pFile = (unixFile*)id;
2648 int rc = takeConch(pFile);
2649 if( rc==SQLITE_OK ){
2650 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2651 unixFile *proxy = pCtx->lockProxy;
2652 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
2653 }
2654 return rc;
2655}
2656
2657static int proxyLock(sqlite3_file *id, int locktype) {
2658 unixFile *pFile = (unixFile*)id;
2659 int rc = takeConch(pFile);
2660 if( rc==SQLITE_OK ){
2661 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2662 unixFile *proxy = pCtx->lockProxy;
2663 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
2664 pFile->locktype = proxy->locktype;
2665 }
2666 return rc;
2667}
2668
2669static int proxyUnlock(sqlite3_file *id, int locktype) {
2670 unixFile *pFile = (unixFile*)id;
2671 int rc = takeConch(pFile);
2672 if( rc==SQLITE_OK ){
2673 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2674 unixFile *proxy = pCtx->lockProxy;
2675 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
2676 pFile->locktype = proxy->locktype;
2677 }
2678 return rc;
2679}
2680
2681/*
2682 ** Close a file.
2683 */
2684static int proxyClose(sqlite3_file *id) {
2685 if( id ){
2686 unixFile *pFile = (unixFile*)id;
2687 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2688 unixFile *lockProxy = pCtx->lockProxy;
2689 unixFile *conchFile = pCtx->conchFile;
2690 int rc = SQLITE_OK;
2691
2692 if( lockProxy ){
2693 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
2694 if( rc ) return rc;
2695 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
2696 if( rc ) return rc;
2697 sqlite3_free(lockProxy);
2698 }
2699 if( conchFile ){
2700 if( pCtx->conchHeld ){
2701 rc = releaseConch(pFile);
2702 if( rc ) return rc;
2703 }
2704 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
2705 if( rc ) return rc;
2706 sqlite3_free(conchFile);
2707 }
2708 sqlite3_free(pCtx->lockProxyPath);
2709 sqlite3_free(pCtx->conchFilePath);
2710 sqlite3_free(pCtx->dbPath);
2711 /* restore the original locking context and pMethod then close it */
2712 pFile->lockingContext = pCtx->oldLockingContext;
2713 pFile->pMethod = pCtx->pOldMethod;
2714 sqlite3_free(pCtx);
2715 return pFile->pMethod->xClose(id);
2716 }
2717 return SQLITE_OK;
2718}
2719
2720/* HOSTIDLEN and CONCHLEN both include space for the string
2721** terminating nul
2722*/
2723#define HOSTIDLEN 128
2724#define CONCHLEN (MAXPATHLEN+HOSTIDLEN+1)
2725#ifndef HOSTIDPATH
2726# define HOSTIDPATH "/Library/Caches/.com.apple.sqliteConchHostId"
2727#endif
2728
2729/* basically a copy of unixRandomness with different
2730** test behavior built in */
2731static int genHostID(char *pHostID){
2732 int pid, fd, i, len;
2733 unsigned char *key = (unsigned char *)pHostID;
2734
2735 memset(key, 0, HOSTIDLEN);
2736 len = 0;
2737 fd = open("/dev/urandom", O_RDONLY);
2738 if( fd>=0 ){
2739 len = read(fd, key, HOSTIDLEN);
2740 close(fd); /* silently leak the fd if it fails */
2741 }
2742 if( len < HOSTIDLEN ){
2743 time_t t;
2744 time(&t);
2745 memcpy(key, &t, sizeof(t));
2746 pid = getpid();
2747 memcpy(&key[sizeof(t)], &pid, sizeof(pid));
2748 }
2749
2750#ifdef MAKE_PRETTY_HOSTID
2751 /* filter the bytes into printable ascii characters and NUL terminate */
2752 key[(HOSTIDLEN-1)] = 0x00;
2753 for( i=0; i<(HOSTIDLEN-1); i++ ){
2754 unsigned char pa = key[i]&0x7F;
2755 if( pa<0x20 ){
2756 key[i] = (key[i]&0x80 == 0x80) ? pa+0x40 : pa+0x20;
2757 }else if( pa==0x7F ){
2758 key[i] = (key[i]&0x80 == 0x80) ? pa=0x20 : pa+0x7E;
2759 }
2760 }
2761#endif
2762 return SQLITE_OK;
2763}
2764
aswiftaebf4132008-11-21 00:10:35 +00002765/* writes the host id path to path, path should be an pre-allocated buffer
2766** with enough space for a path */
2767static int getHostIDPath(char *path, size_t len){
2768 strlcpy(path, HOSTIDPATH, len);
2769#ifdef SQLITE_TEST
2770 if( sqlite3_hostid_num>0 ){
2771 char suffix[2] = "1";
2772 suffix[0] = suffix[0] + sqlite3_hostid_num;
2773 strlcat(path, suffix, len);
2774 }
2775#endif
2776 OSTRACE3("GETHOSTIDPATH %s pid=%d\n", path, getpid());
2777}
2778
2779/* get the host ID from a sqlite hostid file stored in the
2780** user-specific tmp directory, create the ID if it's not there already
2781*/
2782static int getHostID(char *pHostID, int *pError){
2783 int fd;
2784 char path[MAXPATHLEN];
2785 size_t len;
2786 int rc=SQLITE_OK;
2787
2788 getHostIDPath(path, MAXPATHLEN);
2789 /* try to create the host ID file, if it already exists read the contents */
2790 fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644);
2791 if( fd<0 ){
2792 int err=errno;
2793
2794 if( err!=EEXIST ){
2795#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
drh734c9862008-11-28 15:37:20 +00002796 fprintf(stderr, "sqlite error creating host ID file %s: %s\n",
2797 path, strerror(err));
aswiftaebf4132008-11-21 00:10:35 +00002798#endif
2799 return SQLITE_PERM;
2800 }
2801 /* couldn't create the file, read it instead */
2802 fd = open(path, O_RDONLY|O_EXCL);
2803 if( fd<0 ){
2804 int err = errno;
2805#ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */
drh734c9862008-11-28 15:37:20 +00002806 fprintf(stderr, "sqlite error opening host ID file %s: %s\n",
2807 path, strerror(err));
aswiftaebf4132008-11-21 00:10:35 +00002808#endif
2809 return SQLITE_PERM;
2810 }
2811 len = pread(fd, pHostID, HOSTIDLEN, 0);
drh734c9862008-11-28 15:37:20 +00002812 if( len<0 ){
2813 *pError = errno;
2814 rc = SQLITE_IOERR_READ;
2815 }else if( len<HOSTIDLEN ){
2816 *pError = 0;
2817 rc = SQLITE_IOERR_SHORT_READ;
2818 }
aswiftaebf4132008-11-21 00:10:35 +00002819 close(fd); /* silently leak the fd if it fails */
2820 OSTRACE3("GETHOSTID read %s pid=%d\n", pHostID, getpid());
2821 return rc;
2822 }else{
2823 int i;
2824 /* we're creating the host ID file (use a random string of bytes) */
2825 genHostID(pHostID);
2826 len = pwrite(fd, pHostID, HOSTIDLEN, 0);
drh734c9862008-11-28 15:37:20 +00002827 if( len<0 ){
2828 *pError = errno;
2829 rc = SQLITE_IOERR_WRITE;
2830 }else if( len<HOSTIDLEN ){
2831 *pError = 0;
2832 rc = SQLITE_IOERR_WRITE;
2833 }
aswiftaebf4132008-11-21 00:10:35 +00002834 close(fd); /* silently leak the fd if it fails */
2835 OSTRACE3("GETHOSTID wrote %s pid=%d\n", pHostID, getpid());
2836 return rc;
2837 }
2838}
2839
2840/* takes the conch by taking a shared lock and read the contents conch, if
2841** lockPath is non-NULL, the host ID and lock file path must match. A NULL
2842** lockPath means that the lockPath in the conch file will be used if the
2843** host IDs match, or a new lock path will be generated automatically
2844** and written to the conch file.
2845*/
2846static int takeConch(unixFile *pFile){
2847 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2848
2849 if( pCtx->conchHeld>0 ){
2850 return SQLITE_OK;
2851 }else{
2852 unixFile *conchFile = pCtx->conchFile;
2853 char testValue[CONCHLEN];
2854 char conchValue[CONCHLEN];
2855 char lockPath[MAXPATHLEN];
2856 char *tLockPath = NULL;
2857 int rc = SQLITE_OK;
2858 int readRc = SQLITE_OK;
2859 int syncPerms = 0;
2860
2861 OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h,
2862 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
2863
2864 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
2865 if( rc==SQLITE_OK ){
2866 int pError = 0;
2867 memset(testValue, 0, CONCHLEN); // conch is fixed size
2868 rc = getHostID(testValue, &pError);
2869 if( rc&SQLITE_IOERR==SQLITE_IOERR ){
2870 pFile->lastErrno = pError;
2871 }
2872 if( pCtx->lockProxyPath ){
2873 strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN);
2874 }
2875 }
2876 if( rc!=SQLITE_OK ){
2877 goto end_takeconch;
2878 }
2879
2880 readRc = unixRead((sqlite3_file *)conchFile, conchValue, CONCHLEN, 0);
2881 if( readRc!=SQLITE_IOERR_SHORT_READ ){
2882 int match = 0;
2883 if( readRc!=SQLITE_OK ){
drh734c9862008-11-28 15:37:20 +00002884 if( rc&SQLITE_IOERR==SQLITE_IOERR ){
2885 pFile->lastErrno = conchFile->lastErrno;
2886 }
aswiftaebf4132008-11-21 00:10:35 +00002887 rc = readRc;
2888 goto end_takeconch;
2889 }
2890 /* if the conch has data compare the contents */
2891 if( !pCtx->lockProxyPath ){
2892 /* for auto-named local lock file, just check the host ID and we'll
2893 ** use the local lock file path that's already in there */
2894 if( !memcmp(testValue, conchValue, HOSTIDLEN) ){
2895 tLockPath = (char *)&conchValue[HOSTIDLEN];
2896 goto end_takeconch;
2897 }
2898 }else{
2899 /* we've got the conch if conchValue matches our path and host ID */
2900 if( !memcmp(testValue, conchValue, CONCHLEN) ){
2901 goto end_takeconch;
2902 }
2903 }
2904 }else{
2905 /* a short read means we're "creating" the conch (even though it could
2906 ** have been user-intervention), if we acquire the exclusive lock,
2907 ** we'll try to match the current on-disk permissions of the database
2908 */
2909 syncPerms = 1;
2910 }
2911
2912 /* either conch was emtpy or didn't match */
2913 if( !pCtx->lockProxyPath ){
2914 getLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
2915 tLockPath = lockPath;
2916 strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN);
2917 }
2918
2919 /* update conch with host and path (this will fail if other process
2920 ** has a shared lock already) */
2921 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
2922 if( rc==SQLITE_OK ){
2923 rc = unixWrite((sqlite3_file *)conchFile, testValue, CONCHLEN, 0);
2924 if( rc==SQLITE_OK && syncPerms ){
2925 struct stat buf;
2926 int err = fstat(pFile->h, &buf);
2927 if( err==0 ){
2928 mode_t mode = buf.st_mode & 0100666;
2929 /* try to match the database file permissions, ignore failure */
2930#ifndef SQLITE_PROXY_DEBUG
2931 fchmod(conchFile->h, buf.st_mode);
2932#else
2933 if( fchmod(conchFile->h, buf.st_mode)!=0 ){
2934 int code = errno;
drh734c9862008-11-28 15:37:20 +00002935 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
2936 buf.st_mode, code, strerror(code));
aswiftaebf4132008-11-21 00:10:35 +00002937 } else {
2938 fprintf(stderr, "fchmod %o SUCCEDED\n",buf.st_mode);
2939 }
2940 }else{
2941 int code = errno;
drh734c9862008-11-28 15:37:20 +00002942 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
2943 err, code, strerror(code));
aswiftaebf4132008-11-21 00:10:35 +00002944#endif
2945 }
2946 }
2947 }
2948 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
2949
2950end_takeconch:
2951 OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h);
drh734c9862008-11-28 15:37:20 +00002952 if( rc==SQLITE_OK && pFile->openFlags ){
aswiftaebf4132008-11-21 00:10:35 +00002953 if( pFile->h>=0 ){
2954#ifdef STRICT_CLOSE_ERROR
2955 if( close(pFile->h) ){
2956 pFile->lastErrno = errno;
2957 return SQLITE_IOERR_CLOSE;
2958 }
2959#else
2960 close(pFile->h); /* silently leak fd if fail */
2961#endif
2962 }
2963 pFile->h = -1;
drh734c9862008-11-28 15:37:20 +00002964 int fd = open(pCtx->dbPath, pFile->openFlags,
2965 SQLITE_DEFAULT_FILE_PERMISSIONS);
aswiftaebf4132008-11-21 00:10:35 +00002966 OSTRACE2("TRANSPROXY: OPEN %d\n", fd);
2967 if( fd>=0 ){
2968 pFile->h = fd;
2969 }else{
2970 rc=SQLITE_CANTOPEN; // SQLITE_BUSY? takeConch called during locking
2971 }
2972 }
2973 if( rc==SQLITE_OK && !pCtx->lockProxy ){
2974 char *path = tLockPath ? tLockPath : pCtx->lockProxyPath;
2975 // ACS: Need to make a copy of path sometimes
2976 rc = createProxyUnixFile(path, &pCtx->lockProxy);
2977 }
2978 if( rc==SQLITE_OK ){
2979 pCtx->conchHeld = 1;
2980
2981 if( tLockPath ){
2982 pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath);
drh7708e972008-11-29 00:56:52 +00002983 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
drh734c9862008-11-28 15:37:20 +00002984 ((afpLockingContext *)pCtx->lockProxy->lockingContext)->dbPath =
2985 pCtx->lockProxyPath;
aswiftaebf4132008-11-21 00:10:35 +00002986 }
2987 }
2988 } else {
2989 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
2990 }
drh734c9862008-11-28 15:37:20 +00002991 OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed");
aswiftaebf4132008-11-21 00:10:35 +00002992 return rc;
2993 }
2994}
2995
2996static int releaseConch(unixFile *pFile){
2997 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2998 int rc;
2999 unixFile *conchFile = pCtx->conchFile;
3000
3001 OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
3002 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
3003 getpid());
3004 pCtx->conchHeld = 0;
3005 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
3006 OSTRACE3("RELEASECONCH %d %s\n", conchFile->h,
3007 (rc==SQLITE_OK ? "ok" : "failed"));
3008 return rc;
3009}
3010
3011static int getConchPathFromDBPath(char *dbPath, char **pConchPath){
3012 int i;
3013 int len = strlen(dbPath);
3014 char *conchPath;
3015
3016 conchPath = (char *)sqlite3_malloc(len + 8);
3017 if( conchPath==0 ){
3018 return SQLITE_NOMEM;
3019 }
3020 strlcpy(conchPath, dbPath, len+1);
3021
3022 /* now insert a "." before the last / character */
3023 for( i=(len-1); i>=0; i-- ){
3024 if( conchPath[i]=='/' ){
3025 i++;
3026 break;
3027 }
3028 }
3029 conchPath[i]='.';
3030 while ( i<len ){
3031 conchPath[i+1]=dbPath[i];
3032 i++;
3033 }
3034 conchPath[i+1]='\0';
3035 strlcat(conchPath, "-conch", len + 8);
3036 *pConchPath = conchPath;
3037 return SQLITE_OK;
3038}
3039
drh734c9862008-11-28 15:37:20 +00003040
aswiftaebf4132008-11-21 00:10:35 +00003041static int getLockPath(const char *dbPath, char *lPath, size_t maxLen){
3042 int len;
3043 int dbLen;
3044 int i;
3045
3046#ifdef LOCKPROXYDIR
3047 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
3048#else
3049# ifdef _CS_DARWIN_USER_TEMP_DIR
3050 {
3051 char utdir[MAXPATHLEN];
3052
3053 confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen);
3054 len = strlcat(lPath, "sqliteplocks", maxLen);
3055 if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
3056 /* if mkdir fails, handle as lock file creation failure */
3057 int err = errno;
3058# ifdef SQLITE_DEBUG
3059 if( err!=EEXIST ){
3060 fprintf(stderr, "getLockPath: mkdir(%s,0%o) error %d %s\n", lPath,
3061 SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err));
3062 }
3063# endif
3064 }else{
3065 OSTRACE3("GETLOCKPATH mkdir %s pid=%d\n", lPath, getpid());
3066 }
3067
3068 }
3069# else
3070 len = strlcpy(lPath, "/tmp/", maxLen);
3071# endif
3072#endif
3073
3074 if( lPath[len-1]!='/' ){
3075 len = strlcat(lPath, "/", maxLen);
3076 }
3077
3078 /* transform the db path to a unique cache name */
3079 dbLen = strlen(dbPath);
3080 for( i=0; i<dbLen && (i+len+7)<maxLen; i++){
3081 char c = dbPath[i];
3082 lPath[i+len] = (c=='/')?'_':c;
3083 }
3084 lPath[i+len]='\0';
3085 strlcat(lPath, ":auto:", maxLen);
3086 return SQLITE_OK;
3087}
3088
3089/* Takes a fully configured proxy locking-style unix file and switches
3090** the local lock file path
3091*/
3092static int switchLockProxyPath(unixFile *pFile, const char *path) {
3093 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
3094 char *oldPath = pCtx->lockProxyPath;
3095 int taken = 0;
3096 int rc = SQLITE_OK;
3097
3098 if( pFile->locktype!=NO_LOCK ){
3099 return SQLITE_BUSY;
3100 }
3101
3102 /* nothing to do if the path is NULL, :auto: or matches the existing path */
3103 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
3104 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
3105 return SQLITE_OK;
3106 }else{
3107 unixFile *lockProxy = pCtx->lockProxy;
3108 pCtx->lockProxy=NULL;
3109 pCtx->conchHeld = 0;
3110 if( lockProxy!=NULL ){
3111 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
3112 if( rc ) return rc;
3113 sqlite3_free(lockProxy);
3114 }
3115 sqlite3_free(oldPath);
3116 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
3117 }
3118
3119 return rc;
3120}
3121
3122/*
3123** Takes an already filled in unix file and alters it so all file locking
3124** will be performed on the local proxy lock file. The following fields
3125** are preserved in the locking context so that they can be restored and
3126** the unix structure properly cleaned up at close time:
3127** ->lockingContext
3128** ->pMethod
3129*/
3130static int transformUnixFileForLockProxy(unixFile *pFile, const char *path) {
3131 proxyLockingContext *pCtx;
3132 char dbPath[MAXPATHLEN];
3133 char *lockPath=NULL;
3134 int rc = SQLITE_OK;
3135
3136 if( pFile->locktype!=NO_LOCK ){
3137 return SQLITE_BUSY;
3138 }
3139 getDbPathForUnixFile(pFile, dbPath);
3140 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
3141 lockPath=NULL;
3142 }else{
3143 lockPath=(char *)path;
3144 }
3145
3146 OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h,
3147 (lockPath ? lockPath : ":auto:"), getpid());
3148
3149 pCtx = sqlite3_malloc( sizeof(*pCtx) );
3150 if( pCtx==0 ){
3151 return SQLITE_NOMEM;
3152 }
3153 memset(pCtx, 0, sizeof(*pCtx));
3154
3155 rc = getConchPathFromDBPath(dbPath, &pCtx->conchFilePath);
3156 if( rc==SQLITE_OK ){
3157 rc = createProxyUnixFile(pCtx->conchFilePath, &pCtx->conchFile);
3158 }
3159 if( rc==SQLITE_OK && lockPath ){
3160 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
3161 }
3162
3163end_transform_file:
3164 if( rc==SQLITE_OK ){
3165 /* all memory is allocated, proxys are created and assigned,
3166 ** switch the locking context and pMethod then return.
3167 */
3168 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
3169 pCtx->oldLockingContext = pFile->lockingContext;
3170 pFile->lockingContext = pCtx;
3171 pCtx->pOldMethod = pFile->pMethod;
drh7708e972008-11-29 00:56:52 +00003172 pFile->pMethod = &proxyIoMethods;
aswiftaebf4132008-11-21 00:10:35 +00003173 }else{
3174 if( pCtx->conchFile ){
3175 rc = pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
3176 if( rc ) return rc;
3177 sqlite3_free(pCtx->conchFile);
3178 }
3179 sqlite3_free(pCtx->conchFilePath);
3180 sqlite3_free(pCtx);
3181 }
3182 OSTRACE3("TRANSPROXY %d %s\n", pFile->h,
3183 (rc==SQLITE_OK ? "ok" : "failed"));
3184 return rc;
3185}
3186
3187static int createProxyUnixFile(const char *path, unixFile **ppFile) {
3188 int fd;
3189 int dirfd = -1;
3190 unixFile *pNew;
3191 int rc = SQLITE_OK;
3192
3193 fd = open(path, O_RDWR | O_CREAT, SQLITE_DEFAULT_FILE_PERMISSIONS);
3194 if( fd<0 ){
3195 return SQLITE_CANTOPEN;
3196 }
3197
3198 pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile));
3199 if( pNew==NULL ){
3200 rc = SQLITE_NOMEM;
3201 goto end_create_proxy;
3202 }
3203 memset(pNew, 0, sizeof(unixFile));
3204
3205 rc = fillInUnixFile(NULL, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0);
3206 if( rc==SQLITE_OK ){
3207 *ppFile = pNew;
3208 return SQLITE_OK;
3209 }
3210end_create_proxy:
3211 close(fd); /* silently leak fd if error, we're already in error */
3212 sqlite3_free(pNew);
3213 return rc;
3214}
3215
3216
drh734c9862008-11-28 15:37:20 +00003217#endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
3218/*
3219** The proxy locking style is intended for use with AFP filesystems.
3220** And since AFP is only supported on MacOSX, the proxy locking is also
3221** restricted to MacOSX.
3222**
3223**
3224******************* End of the proxy lock implementation **********************
3225******************************************************************************/
3226
3227
3228/******************************************************************************
3229**************** Non-locking sqlite3_file methods *****************************
3230**
3231** The next division contains implementations for all methods of the
3232** sqlite3_file object other than the locking methods. The locking
3233** methods were defined in divisions above (one locking method per
3234** division). Those methods that are common to all locking modes
3235** are gather together into this division.
3236*/
drhbfe66312006-10-03 17:40:40 +00003237
3238/*
drh734c9862008-11-28 15:37:20 +00003239** Seek to the offset passed as the second argument, then read cnt
3240** bytes into pBuf. Return the number of bytes actually read.
3241**
3242** NB: If you define USE_PREAD or USE_PREAD64, then it might also
3243** be necessary to define _XOPEN_SOURCE to be 500. This varies from
3244** one system to another. Since SQLite does not define USE_PREAD
3245** any any form by default, we will not attempt to define _XOPEN_SOURCE.
3246** See tickets #2741 and #2681.
3247**
3248** To avoid stomping the errno value on a failed read the lastErrno value
3249** is set before returning.
drh339eb0b2008-03-07 15:34:11 +00003250*/
drh734c9862008-11-28 15:37:20 +00003251static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
3252 int got;
3253 i64 newOffset;
3254 TIMER_START;
3255#if defined(USE_PREAD)
3256 got = pread(id->h, pBuf, cnt, offset);
3257 SimulateIOError( got = -1 );
3258#elif defined(USE_PREAD64)
3259 got = pread64(id->h, pBuf, cnt, offset);
3260 SimulateIOError( got = -1 );
3261#else
3262 newOffset = lseek(id->h, offset, SEEK_SET);
3263 SimulateIOError( newOffset-- );
3264 if( newOffset!=offset ){
3265 if( newOffset == -1 ){
3266 ((unixFile*)id)->lastErrno = errno;
3267 }else{
3268 ((unixFile*)id)->lastErrno = 0;
3269 }
3270 return -1;
3271 }
3272 got = read(id->h, pBuf, cnt);
3273#endif
3274 TIMER_END;
3275 if( got<0 ){
3276 ((unixFile*)id)->lastErrno = errno;
3277 }
3278 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
3279 return got;
drhbfe66312006-10-03 17:40:40 +00003280}
3281
3282/*
drh734c9862008-11-28 15:37:20 +00003283** Read data from a file into a buffer. Return SQLITE_OK if all
3284** bytes were read successfully and SQLITE_IOERR if anything goes
3285** wrong.
drh339eb0b2008-03-07 15:34:11 +00003286*/
drh734c9862008-11-28 15:37:20 +00003287static int unixRead(
3288 sqlite3_file *id,
3289 void *pBuf,
3290 int amt,
3291 sqlite3_int64 offset
3292){
3293 int got;
3294 assert( id );
3295 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
3296 if( got==amt ){
3297 return SQLITE_OK;
3298 }else if( got<0 ){
3299 /* lastErrno set by seekAndRead */
3300 return SQLITE_IOERR_READ;
3301 }else{
3302 ((unixFile*)id)->lastErrno = 0; /* not a system error */
3303 /* Unread parts of the buffer must be zero-filled */
3304 memset(&((char*)pBuf)[got], 0, amt-got);
3305 return SQLITE_IOERR_SHORT_READ;
3306 }
3307}
3308
3309/*
3310** Seek to the offset in id->offset then read cnt bytes into pBuf.
3311** Return the number of bytes actually read. Update the offset.
3312**
3313** To avoid stomping the errno value on a failed write the lastErrno value
3314** is set before returning.
3315*/
3316static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
3317 int got;
3318 i64 newOffset;
3319 TIMER_START;
3320#if defined(USE_PREAD)
3321 got = pwrite(id->h, pBuf, cnt, offset);
3322#elif defined(USE_PREAD64)
3323 got = pwrite64(id->h, pBuf, cnt, offset);
3324#else
3325 newOffset = lseek(id->h, offset, SEEK_SET);
3326 if( newOffset!=offset ){
3327 if( newOffset == -1 ){
3328 ((unixFile*)id)->lastErrno = errno;
3329 }else{
3330 ((unixFile*)id)->lastErrno = 0;
3331 }
3332 return -1;
3333 }
3334 got = write(id->h, pBuf, cnt);
3335#endif
3336 TIMER_END;
3337 if( got<0 ){
3338 ((unixFile*)id)->lastErrno = errno;
3339 }
3340
3341 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
3342 return got;
3343}
3344
3345
3346/*
3347** Write data from a buffer into a file. Return SQLITE_OK on success
3348** or some other error code on failure.
3349*/
3350static int unixWrite(
3351 sqlite3_file *id,
3352 const void *pBuf,
3353 int amt,
3354 sqlite3_int64 offset
3355){
3356 int wrote = 0;
3357 assert( id );
3358 assert( amt>0 );
3359 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
3360 amt -= wrote;
3361 offset += wrote;
3362 pBuf = &((char*)pBuf)[wrote];
3363 }
3364 SimulateIOError(( wrote=(-1), amt=1 ));
3365 SimulateDiskfullError(( wrote=0, amt=1 ));
3366 if( amt>0 ){
3367 if( wrote<0 ){
3368 /* lastErrno set by seekAndWrite */
3369 return SQLITE_IOERR_WRITE;
3370 }else{
3371 ((unixFile*)id)->lastErrno = 0; /* not a system error */
3372 return SQLITE_FULL;
3373 }
3374 }
3375 return SQLITE_OK;
3376}
3377
3378#ifdef SQLITE_TEST
3379/*
3380** Count the number of fullsyncs and normal syncs. This is used to test
3381** that syncs and fullsyncs are occuring at the right times.
3382*/
3383int sqlite3_sync_count = 0;
3384int sqlite3_fullsync_count = 0;
3385#endif
3386
3387/*
3388** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
3389** Otherwise use fsync() in its place.
3390*/
3391#ifndef HAVE_FDATASYNC
3392# define fdatasync fsync
3393#endif
3394
3395/*
3396** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
3397** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
3398** only available on Mac OS X. But that could change.
3399*/
3400#ifdef F_FULLFSYNC
3401# define HAVE_FULLFSYNC 1
3402#else
3403# define HAVE_FULLFSYNC 0
3404#endif
3405
3406
3407/*
3408** The fsync() system call does not work as advertised on many
3409** unix systems. The following procedure is an attempt to make
3410** it work better.
3411**
3412** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
3413** for testing when we want to run through the test suite quickly.
3414** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
3415** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
3416** or power failure will likely corrupt the database file.
3417*/
3418static int full_fsync(int fd, int fullSync, int dataOnly){
chw97185482008-11-17 08:05:31 +00003419 int rc;
drh734c9862008-11-28 15:37:20 +00003420
3421 /* The following "ifdef/elif/else/" block has the same structure as
3422 ** the one below. It is replicated here solely to avoid cluttering
3423 ** up the real code with the UNUSED_PARAMETER() macros.
3424 */
3425#ifdef SQLITE_NO_SYNC
3426 UNUSED_PARAMETER(fd);
3427 UNUSED_PARAMETER(fullSync);
3428 UNUSED_PARAMETER(dataOnly);
3429#elif HAVE_FULLFSYNC
3430 UNUSED_PARAMETER(dataOnly);
3431#else
3432 UNUSED_PARAMETER(fullSync);
3433#endif
3434
3435 /* Record the number of times that we do a normal fsync() and
3436 ** FULLSYNC. This is used during testing to verify that this procedure
3437 ** gets called with the correct arguments.
3438 */
3439#ifdef SQLITE_TEST
3440 if( fullSync ) sqlite3_fullsync_count++;
3441 sqlite3_sync_count++;
3442#endif
3443
3444 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
3445 ** no-op
3446 */
3447#ifdef SQLITE_NO_SYNC
3448 rc = SQLITE_OK;
3449#elif HAVE_FULLFSYNC
3450 if( fullSync ){
3451 rc = fcntl(fd, F_FULLFSYNC, 0);
3452 }else{
3453 rc = 1;
3454 }
3455 /* If the FULLFSYNC failed, fall back to attempting an fsync().
3456 * It shouldn't be possible for fullfsync to fail on the local
3457 * file system (on OSX), so failure indicates that FULLFSYNC
3458 * isn't supported for this file system. So, attempt an fsync
3459 * and (for now) ignore the overhead of a superfluous fcntl call.
3460 * It'd be better to detect fullfsync support once and avoid
3461 * the fcntl call every time sync is called.
3462 */
3463 if( rc ) rc = fsync(fd);
3464
3465#else
3466 if( dataOnly ){
3467 rc = fdatasync(fd);
3468 if( OS_VXWORKS && rc==-1 && errno==ENOTSUP ){
3469 rc = fsync(fd);
3470 }
3471 }else{
3472 rc = fsync(fd);
3473 }
3474#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
3475
3476 if( OS_VXWORKS && rc!= -1 ){
3477 rc = 0;
3478 }
chw97185482008-11-17 08:05:31 +00003479 return rc;
drhbfe66312006-10-03 17:40:40 +00003480}
3481
drh734c9862008-11-28 15:37:20 +00003482/*
3483** Make sure all writes to a particular file are committed to disk.
3484**
3485** If dataOnly==0 then both the file itself and its metadata (file
3486** size, access time, etc) are synced. If dataOnly!=0 then only the
3487** file data is synced.
3488**
3489** Under Unix, also make sure that the directory entry for the file
3490** has been created by fsync-ing the directory that contains the file.
3491** If we do not do this and we encounter a power failure, the directory
3492** entry for the journal might not exist after we reboot. The next
3493** SQLite to access the file will not know that the journal exists (because
3494** the directory entry for the journal was never created) and the transaction
3495** will not roll back - possibly leading to database corruption.
3496*/
3497static int unixSync(sqlite3_file *id, int flags){
3498 int rc;
3499 unixFile *pFile = (unixFile*)id;
3500
3501 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
3502 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
3503
3504 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
3505 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
3506 || (flags&0x0F)==SQLITE_SYNC_FULL
3507 );
3508
3509 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
3510 ** line is to test that doing so does not cause any problems.
3511 */
3512 SimulateDiskfullError( return SQLITE_FULL );
3513
3514 assert( pFile );
3515 OSTRACE2("SYNC %-3d\n", pFile->h);
3516 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
3517 SimulateIOError( rc=1 );
3518 if( rc ){
3519 pFile->lastErrno = errno;
3520 return SQLITE_IOERR_FSYNC;
3521 }
3522 if( pFile->dirfd>=0 ){
3523 int err;
3524 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
3525 HAVE_FULLFSYNC, isFullsync);
3526#ifndef SQLITE_DISABLE_DIRSYNC
3527 /* The directory sync is only attempted if full_fsync is
3528 ** turned off or unavailable. If a full_fsync occurred above,
3529 ** then the directory sync is superfluous.
3530 */
3531 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
3532 /*
3533 ** We have received multiple reports of fsync() returning
3534 ** errors when applied to directories on certain file systems.
3535 ** A failed directory sync is not a big deal. So it seems
3536 ** better to ignore the error. Ticket #1657
3537 */
3538 /* pFile->lastErrno = errno; */
3539 /* return SQLITE_IOERR; */
3540 }
3541#endif
3542 err = close(pFile->dirfd); /* Only need to sync once, so close the */
3543 if( err==0 ){ /* directory when we are done */
3544 pFile->dirfd = -1;
3545 }else{
3546 pFile->lastErrno = errno;
3547 rc = SQLITE_IOERR_DIR_CLOSE;
3548 }
3549 }
3550 return rc;
3551}
3552
3553/*
3554** Truncate an open file to a specified size
3555*/
3556static int unixTruncate(sqlite3_file *id, i64 nByte){
3557 int rc;
3558 assert( id );
3559 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
3560 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
3561 if( rc ){
3562 ((unixFile*)id)->lastErrno = errno;
3563 return SQLITE_IOERR_TRUNCATE;
3564 }else{
3565 return SQLITE_OK;
3566 }
3567}
3568
3569/*
3570** Determine the current size of a file in bytes
3571*/
3572static int unixFileSize(sqlite3_file *id, i64 *pSize){
3573 int rc;
3574 struct stat buf;
3575 assert( id );
3576 rc = fstat(((unixFile*)id)->h, &buf);
3577 SimulateIOError( rc=1 );
3578 if( rc!=0 ){
3579 ((unixFile*)id)->lastErrno = errno;
3580 return SQLITE_IOERR_FSTAT;
3581 }
3582 *pSize = buf.st_size;
3583
3584 /* When opening a zero-size database, the findLockInfo() procedure
3585 ** writes a single byte into that file in order to work around a bug
3586 ** in the OS-X msdos filesystem. In order to avoid problems with upper
3587 ** layers, we need to report this file size as zero even though it is
3588 ** really 1. Ticket #3260.
3589 */
3590 if( *pSize==1 ) *pSize = 0;
3591
3592
3593 return SQLITE_OK;
3594}
3595
danielk1977ad94b582007-08-20 06:44:22 +00003596
danielk1977e3026632004-06-22 11:29:02 +00003597/*
drh9e33c2c2007-08-31 18:34:59 +00003598** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00003599*/
drhcc6bb3e2007-08-31 16:11:35 +00003600static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00003601 switch( op ){
3602 case SQLITE_FCNTL_LOCKSTATE: {
3603 *(int*)pArg = ((unixFile*)id)->locktype;
3604 return SQLITE_OK;
3605 }
drh7708e972008-11-29 00:56:52 +00003606 case SQLITE_LAST_ERRNO: {
3607 *(int*)pArg = ((unixFile*)id)->lastErrno;
3608 return SQLITE_OK;
3609 }
3610#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
aswiftaebf4132008-11-21 00:10:35 +00003611 case SQLITE_GET_LOCKPROXYFILE: {
aswiftaebf4132008-11-21 00:10:35 +00003612 unixFile *pFile = (unixFile*)id;
drh7708e972008-11-29 00:56:52 +00003613 if( pFile->pMethod == &proxyIoMethods ){
aswiftaebf4132008-11-21 00:10:35 +00003614 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
3615 takeConch(pFile);
3616 if( pCtx->lockProxyPath ){
3617 *(const char **)pArg = pCtx->lockProxyPath;
3618 }else{
3619 *(const char **)pArg = ":auto: (not held)";
3620 }
3621 } else {
3622 *(const char **)pArg = NULL;
3623 }
aswiftaebf4132008-11-21 00:10:35 +00003624 return SQLITE_OK;
3625 }
3626 case SQLITE_SET_LOCKPROXYFILE: {
aswiftaebf4132008-11-21 00:10:35 +00003627 unixFile *pFile = (unixFile*)id;
3628 int rc = SQLITE_OK;
drh7708e972008-11-29 00:56:52 +00003629 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
aswiftaebf4132008-11-21 00:10:35 +00003630 if( pArg==NULL || (const char *)pArg==0 ){
3631 if( isProxyStyle ){
drh7708e972008-11-29 00:56:52 +00003632 /* turn off proxy locking - not supported */
aswiftaebf4132008-11-21 00:10:35 +00003633 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
3634 }else{
drh7708e972008-11-29 00:56:52 +00003635 /* turn off proxy locking - already off - NOOP */
aswiftaebf4132008-11-21 00:10:35 +00003636 rc = SQLITE_OK;
3637 }
3638 }else{
3639 const char *proxyPath = (const char *)pArg;
3640 if( isProxyStyle ){
3641 proxyLockingContext *pCtx =
3642 (proxyLockingContext*)pFile->lockingContext;
drh7708e972008-11-29 00:56:52 +00003643 if( !strcmp(pArg, ":auto:")
3644 || (pCtx->lockProxyPath &&
3645 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
3646 ){
aswiftaebf4132008-11-21 00:10:35 +00003647 rc = SQLITE_OK;
3648 }else{
3649 rc = switchLockProxyPath(pFile, proxyPath);
3650 }
3651 }else{
drh7708e972008-11-29 00:56:52 +00003652 /* turn on proxy file locking */
aswiftaebf4132008-11-21 00:10:35 +00003653 rc = transformUnixFileForLockProxy(pFile, proxyPath);
3654 }
3655 }
3656 return rc;
drh7708e972008-11-29 00:56:52 +00003657 }
aswiftaebf4132008-11-21 00:10:35 +00003658#endif
drh9e33c2c2007-08-31 18:34:59 +00003659 }
drhcc6bb3e2007-08-31 16:11:35 +00003660 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00003661}
3662
3663/*
danielk1977a3d4c882007-03-23 10:08:38 +00003664** Return the sector size in bytes of the underlying block device for
3665** the specified file. This is almost always 512 bytes, but may be
3666** larger for some devices.
3667**
3668** SQLite code assumes this function cannot fail. It also assumes that
3669** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00003670** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00003671** same for both.
3672*/
danielk1977397d65f2008-11-19 11:35:39 +00003673static int unixSectorSize(sqlite3_file *NotUsed){
3674 UNUSED_PARAMETER(NotUsed);
drh3ceeb752007-03-29 18:19:52 +00003675 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00003676}
3677
danielk197790949c22007-08-17 16:50:38 +00003678/*
danielk1977397d65f2008-11-19 11:35:39 +00003679** Return the device characteristics for the file. This is always 0 for unix.
danielk197790949c22007-08-17 16:50:38 +00003680*/
danielk1977397d65f2008-11-19 11:35:39 +00003681static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
3682 UNUSED_PARAMETER(NotUsed);
danielk197762079062007-08-15 17:08:46 +00003683 return 0;
3684}
3685
drh734c9862008-11-28 15:37:20 +00003686/*
3687** Here ends the implementation of all sqlite3_file methods.
3688**
3689********************** End sqlite3_file Methods *******************************
3690******************************************************************************/
3691
3692/*
drh7708e972008-11-29 00:56:52 +00003693** Each instance of this macro generates two objects:
drh734c9862008-11-28 15:37:20 +00003694**
drh7708e972008-11-29 00:56:52 +00003695** * A constant sqlite3_io_methods object call METHOD that has locking
3696** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
3697**
3698** * An I/O method finder function called FINDER that returns a pointer
3699** to the METHOD object in the previous bullet.
drh734c9862008-11-28 15:37:20 +00003700*/
drh7708e972008-11-29 00:56:52 +00003701#define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK) \
3702static const sqlite3_io_methods METHOD = { \
3703 1, /* iVersion */ \
3704 CLOSE, /* xClose */ \
3705 unixRead, /* xRead */ \
3706 unixWrite, /* xWrite */ \
3707 unixTruncate, /* xTruncate */ \
3708 unixSync, /* xSync */ \
3709 unixFileSize, /* xFileSize */ \
3710 LOCK, /* xLock */ \
3711 UNLOCK, /* xUnlock */ \
3712 CKLOCK, /* xCheckReservedLock */ \
3713 unixFileControl, /* xFileControl */ \
3714 unixSectorSize, /* xSectorSize */ \
3715 unixDeviceCharacteristics /* xDeviceCapabilities */ \
3716}; \
3717static const sqlite3_io_methods *FINDER(const char *z, int h){ \
3718 UNUSED_PARAMETER(z); UNUSED_PARAMETER(h); \
3719 return &METHOD; \
aswiftaebf4132008-11-21 00:10:35 +00003720}
drh7708e972008-11-29 00:56:52 +00003721
3722/*
3723** Here are all of the sqlite3_io_methods objects for each of the
3724** locking strategies. Functions that return pointers to these methods
3725** are also created.
3726*/
3727IOMETHODS(
3728 posixIoFinder, /* Finder function name */
3729 posixIoMethods, /* sqlite3_io_methods object name */
3730 unixClose, /* xClose method */
3731 unixLock, /* xLock method */
3732 unixUnlock, /* xUnlock method */
3733 unixCheckReservedLock /* xCheckReservedLock method */
3734);
3735IOMETHODS(
3736 nolockIoFinder, /* Finder function name */
3737 nolockIoMethods, /* sqlite3_io_methods object name */
3738 nolockClose, /* xClose method */
3739 nolockLock, /* xLock method */
3740 nolockUnlock, /* xUnlock method */
3741 nolockCheckReservedLock /* xCheckReservedLock method */
3742);
3743IOMETHODS(
3744 dotlockIoFinder, /* Finder function name */
3745 dotlockIoMethods, /* sqlite3_io_methods object name */
3746 dotlockClose, /* xClose method */
3747 dotlockLock, /* xLock method */
3748 dotlockUnlock, /* xUnlock method */
3749 dotlockCheckReservedLock /* xCheckReservedLock method */
3750);
3751
3752#if SQLITE_ENABLE_LOCKING_STYLE
3753IOMETHODS(
3754 flockIoFinder, /* Finder function name */
3755 flockIoMethods, /* sqlite3_io_methods object name */
3756 flockClose, /* xClose method */
3757 flockLock, /* xLock method */
3758 flockUnlock, /* xUnlock method */
3759 flockCheckReservedLock /* xCheckReservedLock method */
3760);
3761#endif
3762
drh6c7d5c52008-11-21 20:32:33 +00003763#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00003764IOMETHODS(
3765 semIoFinder, /* Finder function name */
3766 semIoMethods, /* sqlite3_io_methods object name */
3767 semClose, /* xClose method */
3768 semLock, /* xLock method */
3769 semUnlock, /* xUnlock method */
3770 semCheckReservedLock /* xCheckReservedLock method */
3771);
aswiftaebf4132008-11-21 00:10:35 +00003772#endif
drh7708e972008-11-29 00:56:52 +00003773
drh734c9862008-11-28 15:37:20 +00003774#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00003775IOMETHODS(
3776 afpIoFinder, /* Finder function name */
3777 afpIoMethods, /* sqlite3_io_methods object name */
3778 afpClose, /* xClose method */
3779 afpLock, /* xLock method */
3780 afpUnlock, /* xUnlock method */
3781 afpCheckReservedLock /* xCheckReservedLock method */
3782);
3783IOMETHODS(
3784 proxyIoFinder, /* Finder function name */
3785 proxyIoMethods, /* sqlite3_io_methods object name */
3786 proxyClose, /* xClose method */
3787 proxyLock, /* xLock method */
3788 proxyUnlock, /* xUnlock method */
3789 proxyCheckReservedLock /* xCheckReservedLock method */
3790);
aswiftaebf4132008-11-21 00:10:35 +00003791#endif
drh7708e972008-11-29 00:56:52 +00003792
3793
3794#if defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE
3795/*
3796** This procedure attempts to determine the best locking strategy for
3797** the given database file. It then returns the sqlite3_io_methods
3798** object that implements that strategy.
3799**
3800** This is for MacOSX only.
3801*/
3802static const sqlite3_io_methods *autolockIoFinder(
3803 const char *filePath, /* name of the database file */
3804 int fd /* file descriptor open on the database file */
3805){
3806 static const struct Mapping {
3807 const char *zFilesystem;
3808 const sqlite3_io_methods *pMethods;
3809 } aMap[] = {
3810 { "hfs", &posixIoMethods },
3811 { "ufs", &posixIoMethods },
3812 { "afpfs", &afpIoMethods },
3813#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
3814 { "smbfs", &afpIoMethods },
3815#else
3816 { "smbfs", &flockIoMethods },
3817#endif
3818 { "webdav", &nolockIoMethods },
3819 { 0, 0 }
3820 };
3821 int i;
3822 struct statfs fsInfo;
3823 struct flock lockInfo;
3824
3825 if( !filePath ){
3826 return &nolockIoMethods;
3827 }
3828 if( statfs(filePath, &fsInfo) != -1 ){
3829 if( fsInfo.f_flags & MNT_RDONLY ){
3830 return &nolockIoMethods;
3831 }
3832 for(i=0; aMap[i].zFilesystem; i++){
3833 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
3834 return aMap[i].pMethods;
3835 }
3836 }
3837 }
3838
3839 /* Default case. Handles, amongst others, "nfs".
3840 ** Test byte-range lock using fcntl(). If the call succeeds,
3841 ** assume that the file-system supports POSIX style locks.
drh734c9862008-11-28 15:37:20 +00003842 */
drh7708e972008-11-29 00:56:52 +00003843 lockInfo.l_len = 1;
3844 lockInfo.l_start = 0;
3845 lockInfo.l_whence = SEEK_SET;
3846 lockInfo.l_type = F_RDLCK;
3847 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
3848 return &posixIoMethods;
3849 }else{
3850 return &dotlockIoMethods;
3851 }
3852}
3853#endif /* defined(__DARWIN__) && SQLITE_ENABLE_LOCKING_STYLE */
3854
3855/*
3856** An abstract type for a pointer to a IO method finder function:
3857*/
3858typedef const sqlite3_io_methods *(*finder_type)(const char*,int);
3859
aswiftaebf4132008-11-21 00:10:35 +00003860
drh734c9862008-11-28 15:37:20 +00003861/****************************************************************************
3862**************************** sqlite3_vfs methods ****************************
3863**
3864** This division contains the implementation of methods on the
3865** sqlite3_vfs object.
3866*/
3867
danielk1977a3d4c882007-03-23 10:08:38 +00003868/*
danielk1977e339d652008-06-28 11:23:00 +00003869** Initialize the contents of the unixFile structure pointed to by pId.
danielk1977ad94b582007-08-20 06:44:22 +00003870*/
3871static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00003872 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00003873 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00003874 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00003875 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00003876 const char *zFilename, /* Name of the file being opened */
chw97185482008-11-17 08:05:31 +00003877 int noLock, /* Omit locking if true */
3878 int isDelete /* Delete on close if true */
drhbfe66312006-10-03 17:40:40 +00003879){
drh7708e972008-11-29 00:56:52 +00003880 const sqlite3_io_methods *pLockingStyle;
drhda0e7682008-07-30 15:27:54 +00003881 unixFile *pNew = (unixFile *)pId;
3882 int rc = SQLITE_OK;
3883
danielk197717b90b52008-06-06 11:11:25 +00003884 assert( pNew->pLock==NULL );
3885 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00003886
danielk1977a03396a2008-11-19 14:35:46 +00003887 /* Parameter isDelete is only used on vxworks. Parameter pVfs is only
3888 ** used if ENABLE_LOCKING_STYLE is defined. Express this explicitly
3889 ** here to prevent compiler warnings about unused parameters.
3890 */
drh7708e972008-11-29 00:56:52 +00003891#if !OS_VXWORKS
3892 UNUSED_PARAMETER(isDelete);
3893#endif
3894#if !SQLITE_ENABLE_LOCKING_STYLE
3895 UNUSED_PARAMETER(pVfs);
3896#endif
3897#if !OS_VXWORKS && !SQLITE_ENABLE_LOCKING_STYLE
3898 UNUSED_PARAMETER(zFilename);
3899#endif
danielk1977a03396a2008-11-19 14:35:46 +00003900
drh218c5082008-03-07 00:27:10 +00003901 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00003902 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00003903 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00003904 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00003905
drh6c7d5c52008-11-21 20:32:33 +00003906#if OS_VXWORKS
drh107886a2008-11-21 22:21:50 +00003907 pNew->pId = vxworksFindFileId(zFilename);
3908 if( pNew->pId==0 ){
3909 noLock = 1;
3910 rc = SQLITE_NOMEM;
chw97185482008-11-17 08:05:31 +00003911 }
3912#endif
3913
drhda0e7682008-07-30 15:27:54 +00003914 if( noLock ){
drh7708e972008-11-29 00:56:52 +00003915 pLockingStyle = &nolockIoMethods;
drhda0e7682008-07-30 15:27:54 +00003916 }else{
drh7708e972008-11-29 00:56:52 +00003917 pLockingStyle = (*(finder_type)pVfs->pAppData)(zFilename, h);
aswiftaebf4132008-11-21 00:10:35 +00003918#if SQLITE_ENABLE_LOCKING_STYLE
3919 /* Cache zFilename in the locking context (AFP and dotlock override) for
3920 ** proxyLock activation is possible (remote proxy is based on db name)
3921 ** zFilename remains valid until file is closed, to support */
3922 pNew->lockingContext = (void*)zFilename;
3923#endif
drhda0e7682008-07-30 15:27:54 +00003924 }
danielk1977e339d652008-06-28 11:23:00 +00003925
drh7708e972008-11-29 00:56:52 +00003926 if( pLockingStyle == &posixIoMethods ){
3927 unixEnterMutex();
3928 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
3929 unixLeaveMutex();
3930 }
danielk1977e339d652008-06-28 11:23:00 +00003931
drh7708e972008-11-29 00:56:52 +00003932#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
3933 else if( pLockingStyle == &apfIoMethods ){
3934 /* AFP locking uses the file path so it needs to be included in
3935 ** the afpLockingContext.
3936 */
3937 afpLockingContext *pCtx;
3938 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
3939 if( pCtx==0 ){
3940 rc = SQLITE_NOMEM;
3941 }else{
3942 /* NB: zFilename exists and remains valid until the file is closed
3943 ** according to requirement F11141. So we do not need to make a
3944 ** copy of the filename. */
3945 pCtx->dbPath = zFilename;
3946 srandomdev();
drh6c7d5c52008-11-21 20:32:33 +00003947 unixEnterMutex();
drh7708e972008-11-29 00:56:52 +00003948 rc = findLockInfo(pNew, NULL, &pNew->pOpen);
3949 unixLeaveMutex();
drhbfe66312006-10-03 17:40:40 +00003950 }
drh7708e972008-11-29 00:56:52 +00003951 }
3952#endif
danielk1977e339d652008-06-28 11:23:00 +00003953
drh40bbb0a2008-09-23 10:23:26 +00003954#if SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00003955 else if( pLockingStyle == &dotlockIoMethods ){
3956 /* Dotfile locking uses the file path so it needs to be included in
3957 ** the dotlockLockingContext
3958 */
3959 char *zLockFile;
3960 int nFilename;
3961 nFilename = strlen(zFilename) + 6;
3962 zLockFile = (char *)sqlite3_malloc(nFilename);
3963 if( zLockFile==0 ){
3964 rc = SQLITE_NOMEM;
3965 }else{
3966 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
danielk1977e339d652008-06-28 11:23:00 +00003967 }
drh7708e972008-11-29 00:56:52 +00003968 pNew->lockingContext = zLockFile;
3969 }
chw97185482008-11-17 08:05:31 +00003970#endif
danielk1977e339d652008-06-28 11:23:00 +00003971
drh6c7d5c52008-11-21 20:32:33 +00003972#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00003973 else if( pLockingStyle == &semIoMethods ){
3974 /* Named semaphore locking uses the file path so it needs to be
3975 ** included in the semLockingContext
3976 */
3977 unixEnterMutex();
3978 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
3979 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
3980 char *zSemName = pNew->pOpen->aSemName;
3981 int n;
3982 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem",
3983 pNew->pId->zCanonicalName);
3984 for( n=0; zSemName[n]; n++ )
3985 if( zSemName[n]=='/' ) zSemName[n] = '_';
3986 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
3987 if( pNew->pOpen->pSem == SEM_FAILED ){
3988 rc = SQLITE_NOMEM;
3989 pNew->pOpen->aSemName[0] = '\0';
chw97185482008-11-17 08:05:31 +00003990 }
chw97185482008-11-17 08:05:31 +00003991 }
drh7708e972008-11-29 00:56:52 +00003992 unixLeaveMutex();
danielk1977e339d652008-06-28 11:23:00 +00003993 }
drh7708e972008-11-29 00:56:52 +00003994#endif
aswift5b1a2562008-08-22 00:22:35 +00003995
3996 pNew->lastErrno = 0;
drh6c7d5c52008-11-21 20:32:33 +00003997#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00003998 if( rc!=SQLITE_OK ){
3999 unlink(zFilename);
4000 isDelete = 0;
4001 }
4002 pNew->isDelete = isDelete;
4003#endif
danielk1977e339d652008-06-28 11:23:00 +00004004 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00004005 if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */
drhbfe66312006-10-03 17:40:40 +00004006 close(h);
danielk1977e339d652008-06-28 11:23:00 +00004007 }else{
drh7708e972008-11-29 00:56:52 +00004008 pNew->pMethod = pLockingStyle;
danielk1977e339d652008-06-28 11:23:00 +00004009 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00004010 }
danielk1977e339d652008-06-28 11:23:00 +00004011 return rc;
drh054889e2005-11-30 03:20:31 +00004012}
drh9c06c952005-11-26 00:25:00 +00004013
aswiftaebf4132008-11-21 00:10:35 +00004014#if SQLITE_ENABLE_LOCKING_STYLE
aswiftaebf4132008-11-21 00:10:35 +00004015static int getDbPathForUnixFile(unixFile *pFile, char *dbPath){
drh7708e972008-11-29 00:56:52 +00004016#if defined(__DARWIN__)
4017 if( pFile->pMethod == &afpIoMethods ){
drh734c9862008-11-28 15:37:20 +00004018 /* afp style keeps a reference to the db path in the filePath field
4019 ** of the struct */
drh7708e972008-11-29 00:56:52 +00004020 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
4021 strcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath)
4022 }else
4023#endif
4024 if( pFile->pMethod == &dotlockIoMethods ){
drh734c9862008-11-28 15:37:20 +00004025 /* dot lock style uses the locking context to store the dot lock
4026 ** file path */
aswiftaebf4132008-11-21 00:10:35 +00004027 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
drh7708e972008-11-29 00:56:52 +00004028 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
4029 }else{
4030 /* all other styles use the locking context to store the db file path */
4031 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
4032 strcpy(dbPath, (char *)pFile->lockingContext);
aswiftaebf4132008-11-21 00:10:35 +00004033 }
aswiftaebf4132008-11-21 00:10:35 +00004034 return SQLITE_OK;
4035}
4036#endif
4037
danielk1977ad94b582007-08-20 06:44:22 +00004038/*
4039** Open a file descriptor to the directory containing file zFilename.
4040** If successful, *pFd is set to the opened file descriptor and
4041** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
4042** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
4043** value.
4044**
4045** If SQLITE_OK is returned, the caller is responsible for closing
4046** the file descriptor *pFd using close().
4047*/
danielk1977fee2d252007-08-18 10:59:19 +00004048static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00004049 int ii;
drh777b17a2007-09-20 10:02:54 +00004050 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00004051 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00004052
drh153c62c2007-08-24 03:51:33 +00004053 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00004054 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
4055 if( ii>0 ){
4056 zDirname[ii] = '\0';
4057 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00004058 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00004059#ifdef FD_CLOEXEC
4060 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
4061#endif
4062 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
4063 }
4064 }
danielk1977fee2d252007-08-18 10:59:19 +00004065 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00004066 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00004067}
4068
danielk1977b4b47412007-08-17 15:53:36 +00004069/*
danielk197717b90b52008-06-06 11:11:25 +00004070** Create a temporary file name in zBuf. zBuf must be allocated
4071** by the calling process and must be big enough to hold at least
4072** pVfs->mxPathname bytes.
4073*/
4074static int getTempname(int nBuf, char *zBuf){
4075 static const char *azDirs[] = {
4076 0,
aswiftaebf4132008-11-21 00:10:35 +00004077 0,
danielk197717b90b52008-06-06 11:11:25 +00004078 "/var/tmp",
4079 "/usr/tmp",
4080 "/tmp",
4081 ".",
4082 };
4083 static const unsigned char zChars[] =
4084 "abcdefghijklmnopqrstuvwxyz"
4085 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
4086 "0123456789";
drh41022642008-11-21 00:24:42 +00004087 unsigned int i, j;
danielk197717b90b52008-06-06 11:11:25 +00004088 struct stat buf;
4089 const char *zDir = ".";
4090
4091 /* It's odd to simulate an io-error here, but really this is just
4092 ** using the io-error infrastructure to test that SQLite handles this
4093 ** function failing.
4094 */
4095 SimulateIOError( return SQLITE_IOERR );
4096
4097 azDirs[0] = sqlite3_temp_directory;
aswiftaebf4132008-11-21 00:10:35 +00004098 if (NULL == azDirs[1]) {
4099 azDirs[1] = getenv("TMPDIR");
4100 }
4101
4102 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
danielk197717b90b52008-06-06 11:11:25 +00004103 if( azDirs[i]==0 ) continue;
4104 if( stat(azDirs[i], &buf) ) continue;
4105 if( !S_ISDIR(buf.st_mode) ) continue;
4106 if( access(azDirs[i], 07) ) continue;
4107 zDir = azDirs[i];
4108 break;
4109 }
4110
4111 /* Check that the output buffer is large enough for the temporary file
4112 ** name. If it is not, return SQLITE_ERROR.
4113 */
danielk197700e13612008-11-17 19:18:54 +00004114 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
danielk197717b90b52008-06-06 11:11:25 +00004115 return SQLITE_ERROR;
4116 }
4117
4118 do{
4119 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
4120 j = strlen(zBuf);
4121 sqlite3_randomness(15, &zBuf[j]);
4122 for(i=0; i<15; i++, j++){
4123 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
4124 }
4125 zBuf[j] = 0;
4126 }while( access(zBuf,0)==0 );
4127 return SQLITE_OK;
4128}
4129
4130
4131/*
danielk1977ad94b582007-08-20 06:44:22 +00004132** Open the file zPath.
4133**
danielk1977b4b47412007-08-17 15:53:36 +00004134** Previously, the SQLite OS layer used three functions in place of this
4135** one:
4136**
4137** sqlite3OsOpenReadWrite();
4138** sqlite3OsOpenReadOnly();
4139** sqlite3OsOpenExclusive();
4140**
4141** These calls correspond to the following combinations of flags:
4142**
4143** ReadWrite() -> (READWRITE | CREATE)
4144** ReadOnly() -> (READONLY)
4145** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
4146**
4147** The old OpenExclusive() accepted a boolean argument - "delFlag". If
4148** true, the file was configured to be automatically deleted when the
4149** file handle closed. To achieve the same effect using this new
4150** interface, add the DELETEONCLOSE flag to those specified above for
4151** OpenExclusive().
4152*/
4153static int unixOpen(
drh153c62c2007-08-24 03:51:33 +00004154 sqlite3_vfs *pVfs,
danielk1977b4b47412007-08-17 15:53:36 +00004155 const char *zPath,
4156 sqlite3_file *pFile,
4157 int flags,
4158 int *pOutFlags
4159){
danielk1977fee2d252007-08-18 10:59:19 +00004160 int fd = 0; /* File descriptor returned by open() */
4161 int dirfd = -1; /* Directory file descriptor */
drh734c9862008-11-28 15:37:20 +00004162 int openFlags = 0; /* Flags to pass to open() */
danielk1977fee2d252007-08-18 10:59:19 +00004163 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00004164 int noLock; /* True to omit locking primitives */
aswiftaebf4132008-11-21 00:10:35 +00004165 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00004166
4167 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
4168 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
4169 int isCreate = (flags & SQLITE_OPEN_CREATE);
4170 int isReadonly = (flags & SQLITE_OPEN_READONLY);
4171 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
4172
danielk1977fee2d252007-08-18 10:59:19 +00004173 /* If creating a master or main-file journal, this function will open
4174 ** a file-descriptor on the directory too. The first time unixSync()
4175 ** is called the directory file descriptor will be fsync()ed and close()d.
4176 */
4177 int isOpenDirectory = (isCreate &&
4178 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
4179 );
4180
danielk197717b90b52008-06-06 11:11:25 +00004181 /* If argument zPath is a NULL pointer, this function is required to open
4182 ** a temporary file. Use this buffer to store the file name in.
4183 */
4184 char zTmpname[MAX_PATHNAME+1];
4185 const char *zName = zPath;
4186
danielk1977fee2d252007-08-18 10:59:19 +00004187 /* Check the following statements are true:
4188 **
4189 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
4190 ** (b) if CREATE is set, then READWRITE must also be set, and
4191 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00004192 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00004193 */
danielk1977b4b47412007-08-17 15:53:36 +00004194 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00004195 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00004196 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00004197 assert(isDelete==0 || isCreate);
4198
drh33f4e022007-09-03 15:19:34 +00004199 /* The main DB, main journal, and master journal are never automatically
4200 ** deleted
4201 */
4202 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
4203 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
4204 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00004205
danielk1977fee2d252007-08-18 10:59:19 +00004206 /* Assert that the upper layer has set one of the "file-type" flags. */
4207 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
4208 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
4209 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00004210 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00004211 );
4212
danielk1977e339d652008-06-28 11:23:00 +00004213 memset(pFile, 0, sizeof(unixFile));
4214
danielk197717b90b52008-06-06 11:11:25 +00004215 if( !zName ){
danielk197717b90b52008-06-06 11:11:25 +00004216 assert(isDelete && !isOpenDirectory);
4217 rc = getTempname(MAX_PATHNAME+1, zTmpname);
4218 if( rc!=SQLITE_OK ){
4219 return rc;
4220 }
4221 zName = zTmpname;
4222 }
4223
drh734c9862008-11-28 15:37:20 +00004224 if( isReadonly ) openFlags |= O_RDONLY;
4225 if( isReadWrite ) openFlags |= O_RDWR;
4226 if( isCreate ) openFlags |= O_CREAT;
4227 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
4228 openFlags |= (O_LARGEFILE|O_BINARY);
danielk1977b4b47412007-08-17 15:53:36 +00004229
drh734c9862008-11-28 15:37:20 +00004230 fd = open(zName, openFlags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
4231 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, openFlags);
danielk19772f2d8c72007-08-30 16:13:33 +00004232 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00004233 /* Failed to open the file for read/write access. Try read-only. */
4234 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
4235 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00004236 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00004237 }
4238 if( fd<0 ){
4239 return SQLITE_CANTOPEN;
4240 }
4241 if( isDelete ){
drh6c7d5c52008-11-21 20:32:33 +00004242#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00004243 zPath = zName;
4244#else
danielk197717b90b52008-06-06 11:11:25 +00004245 unlink(zName);
chw97185482008-11-17 08:05:31 +00004246#endif
danielk1977b4b47412007-08-17 15:53:36 +00004247 }
drh41022642008-11-21 00:24:42 +00004248#if SQLITE_ENABLE_LOCKING_STYLE
4249 else{
drh734c9862008-11-28 15:37:20 +00004250 ((unixFile*)pFile)->openFlags = openFlags;
drh41022642008-11-21 00:24:42 +00004251 }
4252#endif
danielk1977b4b47412007-08-17 15:53:36 +00004253 if( pOutFlags ){
4254 *pOutFlags = flags;
4255 }
4256
4257 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00004258 if( isOpenDirectory ){
aswiftaebf4132008-11-21 00:10:35 +00004259 rc = openDirectory(zPath, &dirfd);
danielk1977fee2d252007-08-18 10:59:19 +00004260 if( rc!=SQLITE_OK ){
aswiftaebf4132008-11-21 00:10:35 +00004261 close(fd); /* silently leak if fail, already in error */
danielk1977fee2d252007-08-18 10:59:19 +00004262 return rc;
4263 }
4264 }
danielk1977e339d652008-06-28 11:23:00 +00004265
4266#ifdef FD_CLOEXEC
4267 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
4268#endif
4269
drhda0e7682008-07-30 15:27:54 +00004270 noLock = eType!=SQLITE_OPEN_MAIN_DB;
aswiftaebf4132008-11-21 00:10:35 +00004271
4272#if SQLITE_PREFER_PROXY_LOCKING
4273 if( zPath!=NULL && !noLock ){
4274 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
4275 int useProxy = 0;
4276
4277 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy,
drh7708e972008-11-29 00:56:52 +00004278 ** 0 means never use proxy, NULL means use proxy for non-local files only
4279 */
aswiftaebf4132008-11-21 00:10:35 +00004280 if( envforce!=NULL ){
4281 useProxy = atoi(envforce)>0;
4282 }else{
4283 struct statfs fsInfo;
4284
4285 if( statfs(zPath, &fsInfo) == -1 ){
4286 ((unixFile*)pFile)->lastErrno = errno;
4287 if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */
4288 close(fd); /* silently leak if fail, in error */
4289 return SQLITE_IOERR_ACCESS;
4290 }
4291 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
4292 }
4293 if( useProxy ){
4294 rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
4295 if( rc==SQLITE_OK ){
4296 rc = transformUnixFileForLockProxy((unixFile*)pFile, ":auto:");
4297 }
4298 return rc;
4299 }
4300 }
4301#endif
4302
chw97185482008-11-17 08:05:31 +00004303 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
danielk1977b4b47412007-08-17 15:53:36 +00004304}
4305
4306/*
danielk1977fee2d252007-08-18 10:59:19 +00004307** Delete the file at zPath. If the dirSync argument is true, fsync()
4308** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00004309*/
danielk1977397d65f2008-11-19 11:35:39 +00004310static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync){
danielk1977fee2d252007-08-18 10:59:19 +00004311 int rc = SQLITE_OK;
danielk1977397d65f2008-11-19 11:35:39 +00004312 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00004313 SimulateIOError(return SQLITE_IOERR_DELETE);
4314 unlink(zPath);
danielk1977d39fa702008-10-16 13:27:40 +00004315#ifndef SQLITE_DISABLE_DIRSYNC
danielk1977fee2d252007-08-18 10:59:19 +00004316 if( dirSync ){
4317 int fd;
4318 rc = openDirectory(zPath, &fd);
4319 if( rc==SQLITE_OK ){
drh6c7d5c52008-11-21 20:32:33 +00004320#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00004321 if( fsync(fd)==-1 )
4322#else
4323 if( fsync(fd) )
4324#endif
4325 {
danielk1977fee2d252007-08-18 10:59:19 +00004326 rc = SQLITE_IOERR_DIR_FSYNC;
4327 }
aswiftaebf4132008-11-21 00:10:35 +00004328 if( close(fd)&&!rc ){
4329 rc = SQLITE_IOERR_DIR_CLOSE;
4330 }
danielk1977fee2d252007-08-18 10:59:19 +00004331 }
4332 }
danielk1977d138dd82008-10-15 16:02:48 +00004333#endif
danielk1977fee2d252007-08-18 10:59:19 +00004334 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00004335}
4336
danielk197790949c22007-08-17 16:50:38 +00004337/*
4338** Test the existance of or access permissions of file zPath. The
4339** test performed depends on the value of flags:
4340**
4341** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
4342** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
4343** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
4344**
4345** Otherwise return 0.
4346*/
danielk1977861f7452008-06-05 11:39:11 +00004347static int unixAccess(
danielk1977397d65f2008-11-19 11:35:39 +00004348 sqlite3_vfs *NotUsed,
danielk1977861f7452008-06-05 11:39:11 +00004349 const char *zPath,
4350 int flags,
4351 int *pResOut
4352){
rse25c0d1a2007-09-20 08:38:14 +00004353 int amode = 0;
danielk1977397d65f2008-11-19 11:35:39 +00004354 UNUSED_PARAMETER(NotUsed);
danielk1977861f7452008-06-05 11:39:11 +00004355 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00004356 switch( flags ){
4357 case SQLITE_ACCESS_EXISTS:
4358 amode = F_OK;
4359 break;
4360 case SQLITE_ACCESS_READWRITE:
4361 amode = W_OK|R_OK;
4362 break;
drh50d3f902007-08-27 21:10:36 +00004363 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00004364 amode = R_OK;
4365 break;
4366
4367 default:
4368 assert(!"Invalid flags argument");
4369 }
danielk1977861f7452008-06-05 11:39:11 +00004370 *pResOut = (access(zPath, amode)==0);
4371 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00004372}
4373
danielk1977b4b47412007-08-17 15:53:36 +00004374
4375/*
4376** Turn a relative pathname into a full pathname. The relative path
4377** is stored as a nul-terminated string in the buffer pointed to by
4378** zPath.
4379**
4380** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
4381** (in this case, MAX_PATHNAME bytes). The full-path is written to
4382** this buffer before returning.
4383*/
danielk1977adfb9b02007-09-17 07:02:56 +00004384static int unixFullPathname(
4385 sqlite3_vfs *pVfs, /* Pointer to vfs object */
4386 const char *zPath, /* Possibly relative input path */
4387 int nOut, /* Size of output buffer in bytes */
4388 char *zOut /* Output buffer */
4389){
danielk1977843e65f2007-09-01 16:16:15 +00004390
4391 /* It's odd to simulate an io-error here, but really this is just
4392 ** using the io-error infrastructure to test that SQLite handles this
4393 ** function failing. This function could fail if, for example, the
4394 ** current working directly has been unlinked.
4395 */
4396 SimulateIOError( return SQLITE_ERROR );
4397
drh153c62c2007-08-24 03:51:33 +00004398 assert( pVfs->mxPathname==MAX_PATHNAME );
danielk1977f3d3c272008-11-19 16:52:44 +00004399 UNUSED_PARAMETER(pVfs);
chw97185482008-11-17 08:05:31 +00004400
drh3c7f2dc2007-12-06 13:26:20 +00004401 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00004402 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00004403 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00004404 }else{
4405 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00004406 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00004407 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00004408 }
4409 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00004410 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00004411 }
4412 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00004413}
4414
drh0ccebe72005-06-07 22:22:50 +00004415
drh761df872006-12-21 01:29:22 +00004416#ifndef SQLITE_OMIT_LOAD_EXTENSION
4417/*
4418** Interfaces for opening a shared library, finding entry points
4419** within the shared library, and closing the shared library.
4420*/
4421#include <dlfcn.h>
danielk1977397d65f2008-11-19 11:35:39 +00004422static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
4423 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00004424 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
4425}
danielk197795c8a542007-09-01 06:51:27 +00004426
4427/*
4428** SQLite calls this function immediately after a call to unixDlSym() or
4429** unixDlOpen() fails (returns a null pointer). If a more detailed error
4430** message is available, it is written to zBufOut. If no error message
4431** is available, zBufOut is left unmodified and SQLite uses a default
4432** error message.
4433*/
danielk1977397d65f2008-11-19 11:35:39 +00004434static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00004435 char *zErr;
danielk1977397d65f2008-11-19 11:35:39 +00004436 UNUSED_PARAMETER(NotUsed);
drh6c7d5c52008-11-21 20:32:33 +00004437 unixEnterMutex();
danielk1977b4b47412007-08-17 15:53:36 +00004438 zErr = dlerror();
4439 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00004440 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00004441 }
drh6c7d5c52008-11-21 20:32:33 +00004442 unixLeaveMutex();
danielk1977b4b47412007-08-17 15:53:36 +00004443}
danielk1977397d65f2008-11-19 11:35:39 +00004444static void *unixDlSym(sqlite3_vfs *NotUsed, void *pHandle, const char*zSymbol){
4445 UNUSED_PARAMETER(NotUsed);
drh761df872006-12-21 01:29:22 +00004446 return dlsym(pHandle, zSymbol);
4447}
danielk1977397d65f2008-11-19 11:35:39 +00004448static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
4449 UNUSED_PARAMETER(NotUsed);
danielk1977b4b47412007-08-17 15:53:36 +00004450 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00004451}
danielk1977b4b47412007-08-17 15:53:36 +00004452#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
4453 #define unixDlOpen 0
4454 #define unixDlError 0
4455 #define unixDlSym 0
4456 #define unixDlClose 0
4457#endif
4458
4459/*
danielk197790949c22007-08-17 16:50:38 +00004460** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00004461*/
danielk1977397d65f2008-11-19 11:35:39 +00004462static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
4463 UNUSED_PARAMETER(NotUsed);
danielk197700e13612008-11-17 19:18:54 +00004464 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
danielk197790949c22007-08-17 16:50:38 +00004465
drhbbd42a62004-05-22 17:41:58 +00004466 /* We have to initialize zBuf to prevent valgrind from reporting
4467 ** errors. The reports issued by valgrind are incorrect - we would
4468 ** prefer that the randomness be increased by making use of the
4469 ** uninitialized space in zBuf - but valgrind errors tend to worry
4470 ** some users. Rather than argue, it seems easier just to initialize
4471 ** the whole array and silence valgrind, even if that means less randomness
4472 ** in the random seed.
4473 **
4474 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00004475 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00004476 ** tests repeatable.
4477 */
danielk1977b4b47412007-08-17 15:53:36 +00004478 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00004479#if !defined(SQLITE_TEST)
4480 {
drh842b8642005-01-21 17:53:17 +00004481 int pid, fd;
4482 fd = open("/dev/urandom", O_RDONLY);
4483 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00004484 time_t t;
4485 time(&t);
danielk197790949c22007-08-17 16:50:38 +00004486 memcpy(zBuf, &t, sizeof(t));
4487 pid = getpid();
4488 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
danielk197700e13612008-11-17 19:18:54 +00004489 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
drh72cbd072008-10-14 17:58:38 +00004490 nBuf = sizeof(t) + sizeof(pid);
drh842b8642005-01-21 17:53:17 +00004491 }else{
drh72cbd072008-10-14 17:58:38 +00004492 nBuf = read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00004493 close(fd);
4494 }
drhbbd42a62004-05-22 17:41:58 +00004495 }
4496#endif
drh72cbd072008-10-14 17:58:38 +00004497 return nBuf;
drhbbd42a62004-05-22 17:41:58 +00004498}
4499
danielk1977b4b47412007-08-17 15:53:36 +00004500
drhbbd42a62004-05-22 17:41:58 +00004501/*
4502** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00004503** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00004504** The return value is the number of microseconds of sleep actually
4505** requested from the underlying operating system, a number which
4506** might be greater than or equal to the argument, but not less
4507** than the argument.
drhbbd42a62004-05-22 17:41:58 +00004508*/
danielk1977397d65f2008-11-19 11:35:39 +00004509static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
drh6c7d5c52008-11-21 20:32:33 +00004510#if OS_VXWORKS
chw97185482008-11-17 08:05:31 +00004511 struct timespec sp;
4512
4513 sp.tv_sec = microseconds / 1000000;
4514 sp.tv_nsec = (microseconds % 1000000) * 1000;
4515 nanosleep(&sp, NULL);
danielk1977397d65f2008-11-19 11:35:39 +00004516 return microseconds;
4517#elif defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00004518 usleep(microseconds);
4519 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00004520#else
danielk1977b4b47412007-08-17 15:53:36 +00004521 int seconds = (microseconds+999999)/1000000;
4522 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00004523 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00004524#endif
danielk1977397d65f2008-11-19 11:35:39 +00004525 UNUSED_PARAMETER(NotUsed);
drh88f474a2006-01-02 20:00:12 +00004526}
4527
4528/*
drhbbd42a62004-05-22 17:41:58 +00004529** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00004530** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00004531*/
4532#ifdef SQLITE_TEST
4533int sqlite3_current_time = 0;
4534#endif
4535
4536/*
4537** Find the current time (in Universal Coordinated Time). Write the
4538** current time and date as a Julian Day number into *prNow and
4539** return 0. Return 1 if the time and date cannot be found.
4540*/
danielk1977397d65f2008-11-19 11:35:39 +00004541static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
drh6c7d5c52008-11-21 20:32:33 +00004542#if defined(NO_GETTOD)
drhbbd42a62004-05-22 17:41:58 +00004543 time_t t;
4544 time(&t);
4545 *prNow = t/86400.0 + 2440587.5;
drh6c7d5c52008-11-21 20:32:33 +00004546#elif OS_VXWORKS
4547 struct timespec sNow;
4548 clock_gettime(CLOCK_REALTIME, &sNow);
4549 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
drh19e2d372005-08-29 23:00:03 +00004550#else
4551 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00004552 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00004553 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
4554#endif
danielk1977397d65f2008-11-19 11:35:39 +00004555
drhbbd42a62004-05-22 17:41:58 +00004556#ifdef SQLITE_TEST
4557 if( sqlite3_current_time ){
4558 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
4559 }
4560#endif
danielk1977397d65f2008-11-19 11:35:39 +00004561 UNUSED_PARAMETER(NotUsed);
drhbbd42a62004-05-22 17:41:58 +00004562 return 0;
4563}
danielk1977b4b47412007-08-17 15:53:36 +00004564
danielk1977397d65f2008-11-19 11:35:39 +00004565static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
4566 UNUSED_PARAMETER(NotUsed);
4567 UNUSED_PARAMETER(NotUsed2);
4568 UNUSED_PARAMETER(NotUsed3);
danielk1977bcb97fe2008-06-06 15:49:29 +00004569 return 0;
4570}
4571
drh153c62c2007-08-24 03:51:33 +00004572/*
drh734c9862008-11-28 15:37:20 +00004573************************ End of sqlite3_vfs methods ***************************
4574******************************************************************************/
4575
4576/*
danielk1977e339d652008-06-28 11:23:00 +00004577** Initialize the operating system interface.
drh734c9862008-11-28 15:37:20 +00004578**
4579** This routine registers all VFS implementations for unix-like operating
4580** systems. This routine, and the sqlite3_os_end() routine that follows,
4581** should be the only routines in this file that are visible from other
4582** files.
drh153c62c2007-08-24 03:51:33 +00004583*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004584int sqlite3_os_init(void){
danielk1977e339d652008-06-28 11:23:00 +00004585 /* Macro to define the static contents of an sqlite3_vfs structure for
4586 ** the unix backend. The two parameters are the values to use for
4587 ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
4588 **
4589 */
drh7708e972008-11-29 00:56:52 +00004590 #define UNIXVFS(VFSNAME, FINDER) { \
danielk1977e339d652008-06-28 11:23:00 +00004591 1, /* iVersion */ \
4592 sizeof(unixFile), /* szOsFile */ \
4593 MAX_PATHNAME, /* mxPathname */ \
4594 0, /* pNext */ \
drh7708e972008-11-29 00:56:52 +00004595 VFSNAME, /* zName */ \
4596 (void*)FINDER, /* pAppData */ \
danielk1977e339d652008-06-28 11:23:00 +00004597 unixOpen, /* xOpen */ \
4598 unixDelete, /* xDelete */ \
4599 unixAccess, /* xAccess */ \
4600 unixFullPathname, /* xFullPathname */ \
4601 unixDlOpen, /* xDlOpen */ \
4602 unixDlError, /* xDlError */ \
4603 unixDlSym, /* xDlSym */ \
4604 unixDlClose, /* xDlClose */ \
4605 unixRandomness, /* xRandomness */ \
4606 unixSleep, /* xSleep */ \
4607 unixCurrentTime, /* xCurrentTime */ \
4608 unixGetLastError /* xGetLastError */ \
4609 }
4610
drh7708e972008-11-29 00:56:52 +00004611 unsigned int i;
danielk1977e339d652008-06-28 11:23:00 +00004612 static sqlite3_vfs aVfs[] = {
drh7708e972008-11-29 00:56:52 +00004613#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
4614 UNIXVFS("unix", autolockIoFinder ),
4615#else
4616 UNIXVFS("unix", posixIoFinder ),
4617#endif
4618 UNIXVFS("unix-none", nolockIoFinder ),
4619 UNIXVFS("unix-dotfile", dotlockIoFinder ),
drh734c9862008-11-28 15:37:20 +00004620#if OS_VXWORKS
drh7708e972008-11-29 00:56:52 +00004621 UNIXVFS("unix-namedsem", semIoFinder ),
drh734c9862008-11-28 15:37:20 +00004622#endif
4623#if SQLITE_ENABLE_LOCKING_STYLE
drh7708e972008-11-29 00:56:52 +00004624 UNIXVFS("unix-posix", posixIoFinder ),
4625 UNIXVFS("unix-flock", flockIoFinder ),
drh734c9862008-11-28 15:37:20 +00004626#endif
4627#if SQLITE_ENABLE_LOCKING_STYLE && defined(__DARWIN__)
drh7708e972008-11-29 00:56:52 +00004628 UNIXVFS("unix-afp", afpIoFinder ),
4629 UNIXVFS("unix-proxy", proxyIoFinder ),
drh734c9862008-11-28 15:37:20 +00004630#endif
drh153c62c2007-08-24 03:51:33 +00004631 };
danielk1977e339d652008-06-28 11:23:00 +00004632 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
drh734c9862008-11-28 15:37:20 +00004633 sqlite3_vfs_register(&aVfs[i], i==0);
danielk1977e339d652008-06-28 11:23:00 +00004634 }
danielk1977c0fa4c52008-06-25 17:19:00 +00004635 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00004636}
danielk1977e339d652008-06-28 11:23:00 +00004637
4638/*
4639** Shutdown the operating system interface. This is a no-op for unix.
4640*/
danielk1977c0fa4c52008-06-25 17:19:00 +00004641int sqlite3_os_end(void){
4642 return SQLITE_OK;
4643}
drhdce8bdb2007-08-16 13:01:44 +00004644
danielk197729bafea2008-06-26 10:41:19 +00004645#endif /* SQLITE_OS_UNIX */