1 /*
2 ** 2004 May 22
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** This file contains the VFS implementation for unix-like operating systems
14 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
15 **
16 ** 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 organized 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 layout 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 (all except proxy locking):
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 **   *  sqlite3_file methods not associated with locking.
39 **   *  Definitions of sqlite3_io_methods objects for all locking
40 **      methods plus "finder" functions for each locking method.
41 **   *  sqlite3_vfs method implementations.
42 **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
43 **   *  Definitions of sqlite3_vfs objects for all locking methods
44 **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
45 */
46 #include "sqliteInt.h"
47 #if SQLITE_OS_UNIX              /* This file is used on unix only */
48 
49 /*
50 ** There are various methods for file locking used for concurrency
51 ** control:
52 **
53 **   1. POSIX locking (the default),
54 **   2. No locking,
55 **   3. Dot-file locking,
56 **   4. flock() locking,
57 **   5. AFP locking (OSX only),
58 **   6. Named POSIX semaphores (VXWorks only),
59 **   7. proxy locking. (OSX only)
60 **
61 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
62 ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
63 ** selection of the appropriate locking style based on the filesystem
64 ** where the database is located.
65 */
66 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
67 #  if defined(__APPLE__)
68 #    define SQLITE_ENABLE_LOCKING_STYLE 1
69 #  else
70 #    define SQLITE_ENABLE_LOCKING_STYLE 0
71 #  endif
72 #endif
73 
74 /* Use pread() and pwrite() if they are available */
75 #if defined(__APPLE__)
76 # define HAVE_PREAD 1
77 # define HAVE_PWRITE 1
78 #endif
79 #if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
80 # undef USE_PREAD
81 # define USE_PREAD64 1
82 #elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
83 # undef USE_PREAD64
84 # define USE_PREAD 1
85 #endif
86 
87 /*
88 ** standard include files.
89 */
90 #include <sys/types.h>
91 #include <sys/stat.h>
92 #include <fcntl.h>
93 #include <unistd.h>
94 #include <time.h>
95 #include <sys/time.h>
96 #include <errno.h>
97 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
98 # include <sys/mman.h>
99 #endif
100 
101 #if SQLITE_ENABLE_LOCKING_STYLE
102 # include <sys/ioctl.h>
103 # include <sys/file.h>
104 # include <sys/param.h>
105 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
106 
107 #if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
108                            (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
109 #  if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
110        && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
111 #    define HAVE_GETHOSTUUID 1
112 #  else
113 #    warning "gethostuuid() is disabled."
114 #  endif
115 #endif
116 
117 
118 #if OS_VXWORKS
119 # include <sys/ioctl.h>
120 # include <semaphore.h>
121 # include <limits.h>
122 #endif /* OS_VXWORKS */
123 
124 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
125 # include <sys/mount.h>
126 #endif
127 
128 #ifdef HAVE_UTIME
129 # include <utime.h>
130 #endif
131 
132 /*
133 ** Allowed values of unixFile.fsFlags
134 */
135 #define SQLITE_FSFLAGS_IS_MSDOS     0x1
136 
137 /*
138 ** If we are to be thread-safe, include the pthreads header and define
139 ** the SQLITE_UNIX_THREADS macro.
140 */
141 #if SQLITE_THREADSAFE
142 # include <pthread.h>
143 # define SQLITE_UNIX_THREADS 1
144 #endif
145 
146 /*
147 ** Default permissions when creating a new file
148 */
149 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
150 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
151 #endif
152 
153 /*
154 ** Default permissions when creating auto proxy dir
155 */
156 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
157 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
158 #endif
159 
160 /*
161 ** Maximum supported path-length.
162 */
163 #define MAX_PATHNAME 512
164 
165 /*
166 ** Maximum supported symbolic links
167 */
168 #define SQLITE_MAX_SYMLINKS 100
169 
170 /* Always cast the getpid() return type for compatibility with
171 ** kernel modules in VxWorks. */
172 #define osGetpid(X) (pid_t)getpid()
173 
174 /*
175 ** Only set the lastErrno if the error code is a real error and not
176 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
177 */
178 #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
179 
180 /* Forward references */
181 typedef struct unixShm unixShm;               /* Connection shared memory */
182 typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
183 typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
184 typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
185 
186 /*
187 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
188 ** cannot be closed immediately. In these cases, instances of the following
189 ** structure are used to store the file descriptor while waiting for an
190 ** opportunity to either close or reuse it.
191 */
192 struct UnixUnusedFd {
193   int fd;                   /* File descriptor to close */
194   int flags;                /* Flags this file descriptor was opened with */
195   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
196 };
197 
198 /*
199 ** The unixFile structure is subclass of sqlite3_file specific to the unix
200 ** VFS implementations.
201 */
202 typedef struct unixFile unixFile;
203 struct unixFile {
204   sqlite3_io_methods const *pMethod;  /* Always the first entry */
205   sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
206   unixInodeInfo *pInode;              /* Info about locks on this inode */
207   int h;                              /* The file descriptor */
208   unsigned char eFileLock;            /* The type of lock held on this fd */
209   unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
210   int lastErrno;                      /* The unix errno from last I/O error */
211   void *lockingContext;               /* Locking style specific state */
212   UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
213   const char *zPath;                  /* Name of the file */
214   unixShm *pShm;                      /* Shared memory segment information */
215   int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
216 #if SQLITE_MAX_MMAP_SIZE>0
217   int nFetchOut;                      /* Number of outstanding xFetch refs */
218   sqlite3_int64 mmapSize;             /* Usable size of mapping at pMapRegion */
219   sqlite3_int64 mmapSizeActual;       /* Actual size of mapping at pMapRegion */
220   sqlite3_int64 mmapSizeMax;          /* Configured FCNTL_MMAP_SIZE value */
221   void *pMapRegion;                   /* Memory mapped region */
222 #endif
223 #ifdef __QNXNTO__
224   int sectorSize;                     /* Device sector size */
225   int deviceCharacteristics;          /* Precomputed device characteristics */
226 #endif
227 #if SQLITE_ENABLE_LOCKING_STYLE
228   int openFlags;                      /* The flags specified at open() */
229 #endif
230 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
231   unsigned fsFlags;                   /* cached details from statfs() */
232 #endif
233 #if OS_VXWORKS
234   struct vxworksFileId *pId;          /* Unique file ID */
235 #endif
236 #ifdef SQLITE_DEBUG
237   /* The next group of variables are used to track whether or not the
238   ** transaction counter in bytes 24-27 of database files are updated
239   ** whenever any part of the database changes.  An assertion fault will
240   ** occur if a file is updated without also updating the transaction
241   ** counter.  This test is made to avoid new problems similar to the
242   ** one described by ticket #3584.
243   */
244   unsigned char transCntrChng;   /* True if the transaction counter changed */
245   unsigned char dbUpdate;        /* True if any part of database file changed */
246   unsigned char inNormalWrite;   /* True if in a normal write operation */
247 
248 #endif
249 
250 #ifdef SQLITE_TEST
251   /* In test mode, increase the size of this structure a bit so that
252   ** it is larger than the struct CrashFile defined in test6.c.
253   */
254   char aPadding[32];
255 #endif
256 };
257 
258 /* This variable holds the process id (pid) from when the xRandomness()
259 ** method was called.  If xOpen() is called from a different process id,
260 ** indicating that a fork() has occurred, the PRNG will be reset.
261 */
262 static pid_t randomnessPid = 0;
263 
264 /*
265 ** Allowed values for the unixFile.ctrlFlags bitmask:
266 */
267 #define UNIXFILE_EXCL        0x01     /* Connections from one process only */
268 #define UNIXFILE_RDONLY      0x02     /* Connection is read only */
269 #define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
270 #ifndef SQLITE_DISABLE_DIRSYNC
271 # define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
272 #else
273 # define UNIXFILE_DIRSYNC    0x00
274 #endif
275 #define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
276 #define UNIXFILE_DELETE      0x20     /* Delete on close */
277 #define UNIXFILE_URI         0x40     /* Filename might have query parameters */
278 #define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
279 
280 /*
281 ** Include code that is common to all os_*.c files
282 */
283 #include "os_common.h"
284 
285 /*
286 ** Define various macros that are missing from some systems.
287 */
288 #ifndef O_LARGEFILE
289 # define O_LARGEFILE 0
290 #endif
291 #ifdef SQLITE_DISABLE_LFS
292 # undef O_LARGEFILE
293 # define O_LARGEFILE 0
294 #endif
295 #ifndef O_NOFOLLOW
296 # define O_NOFOLLOW 0
297 #endif
298 #ifndef O_BINARY
299 # define O_BINARY 0
300 #endif
301 
302 /*
303 ** The threadid macro resolves to the thread-id or to 0.  Used for
304 ** testing and debugging only.
305 */
306 #if SQLITE_THREADSAFE
307 #define threadid pthread_self()
308 #else
309 #define threadid 0
310 #endif
311 
312 /*
313 ** HAVE_MREMAP defaults to true on Linux and false everywhere else.
314 */
315 #if !defined(HAVE_MREMAP)
316 # if defined(__linux__) && defined(_GNU_SOURCE)
317 #  define HAVE_MREMAP 1
318 # else
319 #  define HAVE_MREMAP 0
320 # endif
321 #endif
322 
323 /*
324 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
325 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
326 */
327 #ifdef __ANDROID__
328 # define lseek lseek64
329 #endif
330 
331 /*
332 ** Different Unix systems declare open() in different ways.  Same use
333 ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
334 ** The difference is important when using a pointer to the function.
335 **
336 ** The safest way to deal with the problem is to always use this wrapper
337 ** which always has the same well-defined interface.
338 */
posixOpen(const char * zFile,int flags,int mode)339 static int posixOpen(const char *zFile, int flags, int mode){
340   return open(zFile, flags, mode);
341 }
342 
343 /* Forward reference */
344 static int openDirectory(const char*, int*);
345 static int unixGetpagesize(void);
346 
347 /*
348 ** Many system calls are accessed through pointer-to-functions so that
349 ** they may be overridden at runtime to facilitate fault injection during
350 ** testing and sandboxing.  The following array holds the names and pointers
351 ** to all overrideable system calls.
352 */
353 static struct unix_syscall {
354   const char *zName;            /* Name of the system call */
355   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
356   sqlite3_syscall_ptr pDefault; /* Default value */
357 } aSyscall[] = {
358   { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
359 #define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
360 
361   { "close",        (sqlite3_syscall_ptr)close,      0  },
362 #define osClose     ((int(*)(int))aSyscall[1].pCurrent)
363 
364   { "access",       (sqlite3_syscall_ptr)access,     0  },
365 #define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
366 
367   { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
368 #define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
369 
370   { "stat",         (sqlite3_syscall_ptr)stat,       0  },
371 #define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
372 
373 /*
374 ** The DJGPP compiler environment looks mostly like Unix, but it
375 ** lacks the fcntl() system call.  So redefine fcntl() to be something
376 ** that always succeeds.  This means that locking does not occur under
377 ** DJGPP.  But it is DOS - what did you expect?
378 */
379 #ifdef __DJGPP__
380   { "fstat",        0,                 0  },
381 #define osFstat(a,b,c)    0
382 #else
383   { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
384 #define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
385 #endif
386 
387   { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
388 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
389 
390   { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
391 #define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
392 
393   { "read",         (sqlite3_syscall_ptr)read,       0  },
394 #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
395 
396 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
397   { "pread",        (sqlite3_syscall_ptr)pread,      0  },
398 #else
399   { "pread",        (sqlite3_syscall_ptr)0,          0  },
400 #endif
401 #define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
402 
403 #if defined(USE_PREAD64)
404   { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
405 #else
406   { "pread64",      (sqlite3_syscall_ptr)0,          0  },
407 #endif
408 #define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
409 
410   { "write",        (sqlite3_syscall_ptr)write,      0  },
411 #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
412 
413 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
414   { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
415 #else
416   { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
417 #endif
418 #define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
419                     aSyscall[12].pCurrent)
420 
421 #if defined(USE_PREAD64)
422   { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
423 #else
424   { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
425 #endif
426 #define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off64_t))\
427                     aSyscall[13].pCurrent)
428 
429   { "fchmod",       (sqlite3_syscall_ptr)fchmod,          0  },
430 #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
431 
432 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
433   { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
434 #else
435   { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
436 #endif
437 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
438 
439   { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
440 #define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
441 
442   { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
443 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
444 
445   { "mkdir",        (sqlite3_syscall_ptr)mkdir,           0 },
446 #define osMkdir     ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
447 
448   { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
449 #define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
450 
451 #if defined(HAVE_FCHOWN)
452   { "fchown",       (sqlite3_syscall_ptr)fchown,          0 },
453 #else
454   { "fchown",       (sqlite3_syscall_ptr)0,               0 },
455 #endif
456 #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
457 
458   { "geteuid",      (sqlite3_syscall_ptr)geteuid,         0 },
459 #define osGeteuid   ((uid_t(*)(void))aSyscall[21].pCurrent)
460 
461 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
462   { "mmap",         (sqlite3_syscall_ptr)mmap,            0 },
463 #else
464   { "mmap",         (sqlite3_syscall_ptr)0,               0 },
465 #endif
466 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
467 
468 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
469   { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
470 #else
471   { "munmap",       (sqlite3_syscall_ptr)0,               0 },
472 #endif
473 #define osMunmap ((void*(*)(void*,size_t))aSyscall[23].pCurrent)
474 
475 #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
476   { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
477 #else
478   { "mremap",       (sqlite3_syscall_ptr)0,               0 },
479 #endif
480 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
481 
482 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
483   { "getpagesize",  (sqlite3_syscall_ptr)unixGetpagesize, 0 },
484 #else
485   { "getpagesize",  (sqlite3_syscall_ptr)0,               0 },
486 #endif
487 #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
488 
489 #if defined(HAVE_READLINK)
490   { "readlink",     (sqlite3_syscall_ptr)readlink,        0 },
491 #else
492   { "readlink",     (sqlite3_syscall_ptr)0,               0 },
493 #endif
494 #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
495 
496 #if defined(HAVE_LSTAT)
497   { "lstat",         (sqlite3_syscall_ptr)lstat,          0 },
498 #else
499   { "lstat",         (sqlite3_syscall_ptr)0,              0 },
500 #endif
501 #define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
502 
503 }; /* End of the overrideable system calls */
504 
505 
506 /*
507 ** On some systems, calls to fchown() will trigger a message in a security
508 ** log if they come from non-root processes.  So avoid calling fchown() if
509 ** we are not running as root.
510 */
robustFchown(int fd,uid_t uid,gid_t gid)511 static int robustFchown(int fd, uid_t uid, gid_t gid){
512 #if defined(HAVE_FCHOWN)
513   return osGeteuid() ? 0 : osFchown(fd,uid,gid);
514 #else
515   return 0;
516 #endif
517 }
518 
519 /*
520 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
521 ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
522 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
523 ** system call named zName.
524 */
unixSetSystemCall(sqlite3_vfs * pNotUsed,const char * zName,sqlite3_syscall_ptr pNewFunc)525 static int unixSetSystemCall(
526   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
527   const char *zName,            /* Name of system call to override */
528   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
529 ){
530   unsigned int i;
531   int rc = SQLITE_NOTFOUND;
532 
533   UNUSED_PARAMETER(pNotUsed);
534   if( zName==0 ){
535     /* If no zName is given, restore all system calls to their default
536     ** settings and return NULL
537     */
538     rc = SQLITE_OK;
539     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
540       if( aSyscall[i].pDefault ){
541         aSyscall[i].pCurrent = aSyscall[i].pDefault;
542       }
543     }
544   }else{
545     /* If zName is specified, operate on only the one system call
546     ** specified.
547     */
548     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
549       if( strcmp(zName, aSyscall[i].zName)==0 ){
550         if( aSyscall[i].pDefault==0 ){
551           aSyscall[i].pDefault = aSyscall[i].pCurrent;
552         }
553         rc = SQLITE_OK;
554         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
555         aSyscall[i].pCurrent = pNewFunc;
556         break;
557       }
558     }
559   }
560   return rc;
561 }
562 
563 /*
564 ** Return the value of a system call.  Return NULL if zName is not a
565 ** recognized system call name.  NULL is also returned if the system call
566 ** is currently undefined.
567 */
unixGetSystemCall(sqlite3_vfs * pNotUsed,const char * zName)568 static sqlite3_syscall_ptr unixGetSystemCall(
569   sqlite3_vfs *pNotUsed,
570   const char *zName
571 ){
572   unsigned int i;
573 
574   UNUSED_PARAMETER(pNotUsed);
575   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
576     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
577   }
578   return 0;
579 }
580 
581 /*
582 ** Return the name of the first system call after zName.  If zName==NULL
583 ** then return the name of the first system call.  Return NULL if zName
584 ** is the last system call or if zName is not the name of a valid
585 ** system call.
586 */
unixNextSystemCall(sqlite3_vfs * p,const char * zName)587 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
588   int i = -1;
589 
590   UNUSED_PARAMETER(p);
591   if( zName ){
592     for(i=0; i<ArraySize(aSyscall)-1; i++){
593       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
594     }
595   }
596   for(i++; i<ArraySize(aSyscall); i++){
597     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
598   }
599   return 0;
600 }
601 
602 /*
603 ** Do not accept any file descriptor less than this value, in order to avoid
604 ** opening database file using file descriptors that are commonly used for
605 ** standard input, output, and error.
606 */
607 #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
608 # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
609 #endif
610 
611 /*
612 ** Invoke open().  Do so multiple times, until it either succeeds or
613 ** fails for some reason other than EINTR.
614 **
615 ** If the file creation mode "m" is 0 then set it to the default for
616 ** SQLite.  The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
617 ** 0644) as modified by the system umask.  If m is not 0, then
618 ** make the file creation mode be exactly m ignoring the umask.
619 **
620 ** The m parameter will be non-zero only when creating -wal, -journal,
621 ** and -shm files.  We want those files to have *exactly* the same
622 ** permissions as their original database, unadulterated by the umask.
623 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
624 ** transaction crashes and leaves behind hot journals, then any
625 ** process that is able to write to the database will also be able to
626 ** recover the hot journals.
627 */
robust_open(const char * z,int f,mode_t m)628 static int robust_open(const char *z, int f, mode_t m){
629   int fd;
630   mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
631   while(1){
632 #if defined(O_CLOEXEC)
633     fd = osOpen(z,f|O_CLOEXEC,m2);
634 #else
635     fd = osOpen(z,f,m2);
636 #endif
637     if( fd<0 ){
638       if( errno==EINTR ) continue;
639       break;
640     }
641     if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
642     osClose(fd);
643     sqlite3_log(SQLITE_WARNING,
644                 "attempt to open \"%s\" as file descriptor %d", z, fd);
645     fd = -1;
646     if( osOpen("/dev/null", f, m)<0 ) break;
647   }
648   if( fd>=0 ){
649     if( m!=0 ){
650       struct stat statbuf;
651       if( osFstat(fd, &statbuf)==0
652        && statbuf.st_size==0
653        && (statbuf.st_mode&0777)!=m
654       ){
655         osFchmod(fd, m);
656       }
657     }
658 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
659     osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
660 #endif
661   }
662   return fd;
663 }
664 
665 /*
666 ** Helper functions to obtain and relinquish the global mutex. The
667 ** global mutex is used to protect the unixInodeInfo and
668 ** vxworksFileId objects used by this file, all of which may be
669 ** shared by multiple threads.
670 **
671 ** Function unixMutexHeld() is used to assert() that the global mutex
672 ** is held when required. This function is only used as part of assert()
673 ** statements. e.g.
674 **
675 **   unixEnterMutex()
676 **     assert( unixMutexHeld() );
677 **   unixEnterLeave()
678 */
unixEnterMutex(void)679 static void unixEnterMutex(void){
680   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
681 }
unixLeaveMutex(void)682 static void unixLeaveMutex(void){
683   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
684 }
685 #ifdef SQLITE_DEBUG
unixMutexHeld(void)686 static int unixMutexHeld(void) {
687   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
688 }
689 #endif
690 
691 
692 #ifdef SQLITE_HAVE_OS_TRACE
693 /*
694 ** Helper function for printing out trace information from debugging
695 ** binaries. This returns the string representation of the supplied
696 ** integer lock-type.
697 */
azFileLock(int eFileLock)698 static const char *azFileLock(int eFileLock){
699   switch( eFileLock ){
700     case NO_LOCK: return "NONE";
701     case SHARED_LOCK: return "SHARED";
702     case RESERVED_LOCK: return "RESERVED";
703     case PENDING_LOCK: return "PENDING";
704     case EXCLUSIVE_LOCK: return "EXCLUSIVE";
705   }
706   return "ERROR";
707 }
708 #endif
709 
710 #ifdef SQLITE_LOCK_TRACE
711 /*
712 ** Print out information about all locking operations.
713 **
714 ** This routine is used for troubleshooting locks on multithreaded
715 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
716 ** command-line option on the compiler.  This code is normally
717 ** turned off.
718 */
lockTrace(int fd,int op,struct flock * p)719 static int lockTrace(int fd, int op, struct flock *p){
720   char *zOpName, *zType;
721   int s;
722   int savedErrno;
723   if( op==F_GETLK ){
724     zOpName = "GETLK";
725   }else if( op==F_SETLK ){
726     zOpName = "SETLK";
727   }else{
728     s = osFcntl(fd, op, p);
729     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
730     return s;
731   }
732   if( p->l_type==F_RDLCK ){
733     zType = "RDLCK";
734   }else if( p->l_type==F_WRLCK ){
735     zType = "WRLCK";
736   }else if( p->l_type==F_UNLCK ){
737     zType = "UNLCK";
738   }else{
739     assert( 0 );
740   }
741   assert( p->l_whence==SEEK_SET );
742   s = osFcntl(fd, op, p);
743   savedErrno = errno;
744   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
745      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
746      (int)p->l_pid, s);
747   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
748     struct flock l2;
749     l2 = *p;
750     osFcntl(fd, F_GETLK, &l2);
751     if( l2.l_type==F_RDLCK ){
752       zType = "RDLCK";
753     }else if( l2.l_type==F_WRLCK ){
754       zType = "WRLCK";
755     }else if( l2.l_type==F_UNLCK ){
756       zType = "UNLCK";
757     }else{
758       assert( 0 );
759     }
760     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
761        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
762   }
763   errno = savedErrno;
764   return s;
765 }
766 #undef osFcntl
767 #define osFcntl lockTrace
768 #endif /* SQLITE_LOCK_TRACE */
769 
770 /*
771 ** Retry ftruncate() calls that fail due to EINTR
772 **
773 ** All calls to ftruncate() within this file should be made through
774 ** this wrapper.  On the Android platform, bypassing the logic below
775 ** could lead to a corrupt database.
776 */
robust_ftruncate(int h,sqlite3_int64 sz)777 static int robust_ftruncate(int h, sqlite3_int64 sz){
778   int rc;
779 #ifdef __ANDROID__
780   /* On Android, ftruncate() always uses 32-bit offsets, even if
781   ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
782   ** truncate a file to any size larger than 2GiB. Silently ignore any
783   ** such attempts.  */
784   if( sz>(sqlite3_int64)0x7FFFFFFF ){
785     rc = SQLITE_OK;
786   }else
787 #endif
788   do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
789   return rc;
790 }
791 
792 /*
793 ** This routine translates a standard POSIX errno code into something
794 ** useful to the clients of the sqlite3 functions.  Specifically, it is
795 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
796 ** and a variety of "please close the file descriptor NOW" errors into
797 ** SQLITE_IOERR
798 **
799 ** Errors during initialization of locks, or file system support for locks,
800 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
801 */
sqliteErrorFromPosixError(int posixError,int sqliteIOErr)802 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
803   assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
804           (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
805           (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
806           (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
807   switch (posixError) {
808   case EACCES:
809   case EAGAIN:
810   case ETIMEDOUT:
811   case EBUSY:
812   case EINTR:
813   case ENOLCK:
814     /* random NFS retry error, unless during file system support
815      * introspection, in which it actually means what it says */
816     return SQLITE_BUSY;
817 
818   case EPERM:
819     return SQLITE_PERM;
820 
821   default:
822     return sqliteIOErr;
823   }
824 }
825 
826 
827 /******************************************************************************
828 ****************** Begin Unique File ID Utility Used By VxWorks ***************
829 **
830 ** On most versions of unix, we can get a unique ID for a file by concatenating
831 ** the device number and the inode number.  But this does not work on VxWorks.
832 ** On VxWorks, a unique file id must be based on the canonical filename.
833 **
834 ** A pointer to an instance of the following structure can be used as a
835 ** unique file ID in VxWorks.  Each instance of this structure contains
836 ** a copy of the canonical filename.  There is also a reference count.
837 ** The structure is reclaimed when the number of pointers to it drops to
838 ** zero.
839 **
840 ** There are never very many files open at one time and lookups are not
841 ** a performance-critical path, so it is sufficient to put these
842 ** structures on a linked list.
843 */
844 struct vxworksFileId {
845   struct vxworksFileId *pNext;  /* Next in a list of them all */
846   int nRef;                     /* Number of references to this one */
847   int nName;                    /* Length of the zCanonicalName[] string */
848   char *zCanonicalName;         /* Canonical filename */
849 };
850 
851 #if OS_VXWORKS
852 /*
853 ** All unique filenames are held on a linked list headed by this
854 ** variable:
855 */
856 static struct vxworksFileId *vxworksFileList = 0;
857 
858 /*
859 ** Simplify a filename into its canonical form
860 ** by making the following changes:
861 **
862 **  * removing any trailing and duplicate /
863 **  * convert /./ into just /
864 **  * convert /A/../ where A is any simple name into just /
865 **
866 ** Changes are made in-place.  Return the new name length.
867 **
868 ** The original filename is in z[0..n-1].  Return the number of
869 ** characters in the simplified name.
870 */
vxworksSimplifyName(char * z,int n)871 static int vxworksSimplifyName(char *z, int n){
872   int i, j;
873   while( n>1 && z[n-1]=='/' ){ n--; }
874   for(i=j=0; i<n; i++){
875     if( z[i]=='/' ){
876       if( z[i+1]=='/' ) continue;
877       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
878         i += 1;
879         continue;
880       }
881       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
882         while( j>0 && z[j-1]!='/' ){ j--; }
883         if( j>0 ){ j--; }
884         i += 2;
885         continue;
886       }
887     }
888     z[j++] = z[i];
889   }
890   z[j] = 0;
891   return j;
892 }
893 
894 /*
895 ** Find a unique file ID for the given absolute pathname.  Return
896 ** a pointer to the vxworksFileId object.  This pointer is the unique
897 ** file ID.
898 **
899 ** The nRef field of the vxworksFileId object is incremented before
900 ** the object is returned.  A new vxworksFileId object is created
901 ** and added to the global list if necessary.
902 **
903 ** If a memory allocation error occurs, return NULL.
904 */
vxworksFindFileId(const char * zAbsoluteName)905 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
906   struct vxworksFileId *pNew;         /* search key and new file ID */
907   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
908   int n;                              /* Length of zAbsoluteName string */
909 
910   assert( zAbsoluteName[0]=='/' );
911   n = (int)strlen(zAbsoluteName);
912   pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
913   if( pNew==0 ) return 0;
914   pNew->zCanonicalName = (char*)&pNew[1];
915   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
916   n = vxworksSimplifyName(pNew->zCanonicalName, n);
917 
918   /* Search for an existing entry that matching the canonical name.
919   ** If found, increment the reference count and return a pointer to
920   ** the existing file ID.
921   */
922   unixEnterMutex();
923   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
924     if( pCandidate->nName==n
925      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
926     ){
927        sqlite3_free(pNew);
928        pCandidate->nRef++;
929        unixLeaveMutex();
930        return pCandidate;
931     }
932   }
933 
934   /* No match was found.  We will make a new file ID */
935   pNew->nRef = 1;
936   pNew->nName = n;
937   pNew->pNext = vxworksFileList;
938   vxworksFileList = pNew;
939   unixLeaveMutex();
940   return pNew;
941 }
942 
943 /*
944 ** Decrement the reference count on a vxworksFileId object.  Free
945 ** the object when the reference count reaches zero.
946 */
vxworksReleaseFileId(struct vxworksFileId * pId)947 static void vxworksReleaseFileId(struct vxworksFileId *pId){
948   unixEnterMutex();
949   assert( pId->nRef>0 );
950   pId->nRef--;
951   if( pId->nRef==0 ){
952     struct vxworksFileId **pp;
953     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
954     assert( *pp==pId );
955     *pp = pId->pNext;
956     sqlite3_free(pId);
957   }
958   unixLeaveMutex();
959 }
960 #endif /* OS_VXWORKS */
961 /*************** End of Unique File ID Utility Used By VxWorks ****************
962 ******************************************************************************/
963 
964 
965 /******************************************************************************
966 *************************** Posix Advisory Locking ****************************
967 **
968 ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
969 ** section 6.5.2.2 lines 483 through 490 specify that when a process
970 ** sets or clears a lock, that operation overrides any prior locks set
971 ** by the same process.  It does not explicitly say so, but this implies
972 ** that it overrides locks set by the same process using a different
973 ** file descriptor.  Consider this test case:
974 **
975 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
976 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
977 **
978 ** Suppose ./file1 and ./file2 are really the same file (because
979 ** one is a hard or symbolic link to the other) then if you set
980 ** an exclusive lock on fd1, then try to get an exclusive lock
981 ** on fd2, it works.  I would have expected the second lock to
982 ** fail since there was already a lock on the file due to fd1.
983 ** But not so.  Since both locks came from the same process, the
984 ** second overrides the first, even though they were on different
985 ** file descriptors opened on different file names.
986 **
987 ** This means that we cannot use POSIX locks to synchronize file access
988 ** among competing threads of the same process.  POSIX locks will work fine
989 ** to synchronize access for threads in separate processes, but not
990 ** threads within the same process.
991 **
992 ** To work around the problem, SQLite has to manage file locks internally
993 ** on its own.  Whenever a new database is opened, we have to find the
994 ** specific inode of the database file (the inode is determined by the
995 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
996 ** and check for locks already existing on that inode.  When locks are
997 ** created or removed, we have to look at our own internal record of the
998 ** locks to see if another thread has previously set a lock on that same
999 ** inode.
1000 **
1001 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
1002 ** For VxWorks, we have to use the alternative unique ID system based on
1003 ** canonical filename and implemented in the previous division.)
1004 **
1005 ** The sqlite3_file structure for POSIX is no longer just an integer file
1006 ** descriptor.  It is now a structure that holds the integer file
1007 ** descriptor and a pointer to a structure that describes the internal
1008 ** locks on the corresponding inode.  There is one locking structure
1009 ** per inode, so if the same inode is opened twice, both unixFile structures
1010 ** point to the same locking structure.  The locking structure keeps
1011 ** a reference count (so we will know when to delete it) and a "cnt"
1012 ** field that tells us its internal lock status.  cnt==0 means the
1013 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
1014 ** cnt>0 means there are cnt shared locks on the file.
1015 **
1016 ** Any attempt to lock or unlock a file first checks the locking
1017 ** structure.  The fcntl() system call is only invoked to set a
1018 ** POSIX lock if the internal lock structure transitions between
1019 ** a locked and an unlocked state.
1020 **
1021 ** But wait:  there are yet more problems with POSIX advisory locks.
1022 **
1023 ** If you close a file descriptor that points to a file that has locks,
1024 ** all locks on that file that are owned by the current process are
1025 ** released.  To work around this problem, each unixInodeInfo object
1026 ** maintains a count of the number of pending locks on tha inode.
1027 ** When an attempt is made to close an unixFile, if there are
1028 ** other unixFile open on the same inode that are holding locks, the call
1029 ** to close() the file descriptor is deferred until all of the locks clear.
1030 ** The unixInodeInfo structure keeps a list of file descriptors that need to
1031 ** be closed and that list is walked (and cleared) when the last lock
1032 ** clears.
1033 **
1034 ** Yet another problem:  LinuxThreads do not play well with posix locks.
1035 **
1036 ** Many older versions of linux use the LinuxThreads library which is
1037 ** not posix compliant.  Under LinuxThreads, a lock created by thread
1038 ** A cannot be modified or overridden by a different thread B.
1039 ** Only thread A can modify the lock.  Locking behavior is correct
1040 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
1041 ** on linux - with NPTL a lock created by thread A can override locks
1042 ** in thread B.  But there is no way to know at compile-time which
1043 ** threading library is being used.  So there is no way to know at
1044 ** compile-time whether or not thread A can override locks on thread B.
1045 ** One has to do a run-time check to discover the behavior of the
1046 ** current process.
1047 **
1048 ** SQLite used to support LinuxThreads.  But support for LinuxThreads
1049 ** was dropped beginning with version 3.7.0.  SQLite will still work with
1050 ** LinuxThreads provided that (1) there is no more than one connection
1051 ** per database file in the same process and (2) database connections
1052 ** do not move across threads.
1053 */
1054 
1055 /*
1056 ** An instance of the following structure serves as the key used
1057 ** to locate a particular unixInodeInfo object.
1058 */
1059 struct unixFileId {
1060   dev_t dev;                  /* Device number */
1061 #if OS_VXWORKS
1062   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
1063 #else
1064   /* We are told that some versions of Android contain a bug that
1065   ** sizes ino_t at only 32-bits instead of 64-bits. (See
1066   ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
1067   ** To work around this, always allocate 64-bits for the inode number.
1068   ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
1069   ** but that should not be a big deal. */
1070   /* WAS:  ino_t ino;   */
1071   u64 ino;                   /* Inode number */
1072 #endif
1073 };
1074 
1075 /*
1076 ** An instance of the following structure is allocated for each open
1077 ** inode.  Or, on LinuxThreads, there is one of these structures for
1078 ** each inode opened by each thread.
1079 **
1080 ** A single inode can have multiple file descriptors, so each unixFile
1081 ** structure contains a pointer to an instance of this object and this
1082 ** object keeps a count of the number of unixFile pointing to it.
1083 */
1084 struct unixInodeInfo {
1085   struct unixFileId fileId;       /* The lookup key */
1086   int nShared;                    /* Number of SHARED locks held */
1087   unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
1088   unsigned char bProcessLock;     /* An exclusive process lock is held */
1089   int nRef;                       /* Number of pointers to this structure */
1090   unixShmNode *pShmNode;          /* Shared memory associated with this inode */
1091   int nLock;                      /* Number of outstanding file locks */
1092   UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
1093   unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
1094   unixInodeInfo *pPrev;           /*    .... doubly linked */
1095 #if SQLITE_ENABLE_LOCKING_STYLE
1096   unsigned long long sharedByte;  /* for AFP simulated shared lock */
1097 #endif
1098 #if OS_VXWORKS
1099   sem_t *pSem;                    /* Named POSIX semaphore */
1100   char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
1101 #endif
1102 };
1103 
1104 /*
1105 ** A lists of all unixInodeInfo objects.
1106 */
1107 static unixInodeInfo *inodeList = 0;
1108 
1109 /*
1110 **
1111 ** This function - unixLogErrorAtLine(), is only ever called via the macro
1112 ** unixLogError().
1113 **
1114 ** It is invoked after an error occurs in an OS function and errno has been
1115 ** set. It logs a message using sqlite3_log() containing the current value of
1116 ** errno and, if possible, the human-readable equivalent from strerror() or
1117 ** strerror_r().
1118 **
1119 ** The first argument passed to the macro should be the error code that
1120 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
1121 ** The two subsequent arguments should be the name of the OS function that
1122 ** failed (e.g. "unlink", "open") and the associated file-system path,
1123 ** if any.
1124 */
1125 #define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
unixLogErrorAtLine(int errcode,const char * zFunc,const char * zPath,int iLine)1126 static int unixLogErrorAtLine(
1127   int errcode,                    /* SQLite error code */
1128   const char *zFunc,              /* Name of OS function that failed */
1129   const char *zPath,              /* File path associated with error */
1130   int iLine                       /* Source line number where error occurred */
1131 ){
1132   char *zErr;                     /* Message from strerror() or equivalent */
1133   int iErrno = errno;             /* Saved syscall error number */
1134 
1135   /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
1136   ** the strerror() function to obtain the human-readable error message
1137   ** equivalent to errno. Otherwise, use strerror_r().
1138   */
1139 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
1140   char aErr[80];
1141   memset(aErr, 0, sizeof(aErr));
1142   zErr = aErr;
1143 
1144   /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
1145   ** assume that the system provides the GNU version of strerror_r() that
1146   ** returns a pointer to a buffer containing the error message. That pointer
1147   ** may point to aErr[], or it may point to some static storage somewhere.
1148   ** Otherwise, assume that the system provides the POSIX version of
1149   ** strerror_r(), which always writes an error message into aErr[].
1150   **
1151   ** If the code incorrectly assumes that it is the POSIX version that is
1152   ** available, the error message will often be an empty string. Not a
1153   ** huge problem. Incorrectly concluding that the GNU version is available
1154   ** could lead to a segfault though.
1155   */
1156 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
1157   zErr =
1158 # endif
1159   strerror_r(iErrno, aErr, sizeof(aErr)-1);
1160 
1161 #elif SQLITE_THREADSAFE
1162   /* This is a threadsafe build, but strerror_r() is not available. */
1163   zErr = "";
1164 #else
1165   /* Non-threadsafe build, use strerror(). */
1166   zErr = strerror(iErrno);
1167 #endif
1168 
1169   if( zPath==0 ) zPath = "";
1170   sqlite3_log(errcode,
1171       "os_unix.c:%d: (%d) %s(%s) - %s",
1172       iLine, iErrno, zFunc, zPath, zErr
1173   );
1174 
1175   return errcode;
1176 }
1177 
1178 /*
1179 ** Close a file descriptor.
1180 **
1181 ** We assume that close() almost always works, since it is only in a
1182 ** very sick application or on a very sick platform that it might fail.
1183 ** If it does fail, simply leak the file descriptor, but do log the
1184 ** error.
1185 **
1186 ** Note that it is not safe to retry close() after EINTR since the
1187 ** file descriptor might have already been reused by another thread.
1188 ** So we don't even try to recover from an EINTR.  Just log the error
1189 ** and move on.
1190 */
robust_close(unixFile * pFile,int h,int lineno)1191 static void robust_close(unixFile *pFile, int h, int lineno){
1192   if( osClose(h) ){
1193     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
1194                        pFile ? pFile->zPath : 0, lineno);
1195   }
1196 }
1197 
1198 /*
1199 ** Set the pFile->lastErrno.  Do this in a subroutine as that provides
1200 ** a convenient place to set a breakpoint.
1201 */
storeLastErrno(unixFile * pFile,int error)1202 static void storeLastErrno(unixFile *pFile, int error){
1203   pFile->lastErrno = error;
1204 }
1205 
1206 /*
1207 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
1208 */
closePendingFds(unixFile * pFile)1209 static void closePendingFds(unixFile *pFile){
1210   unixInodeInfo *pInode = pFile->pInode;
1211   UnixUnusedFd *p;
1212   UnixUnusedFd *pNext;
1213   for(p=pInode->pUnused; p; p=pNext){
1214     pNext = p->pNext;
1215     robust_close(pFile, p->fd, __LINE__);
1216     sqlite3_free(p);
1217   }
1218   pInode->pUnused = 0;
1219 }
1220 
1221 /*
1222 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
1223 **
1224 ** The mutex entered using the unixEnterMutex() function must be held
1225 ** when this function is called.
1226 */
releaseInodeInfo(unixFile * pFile)1227 static void releaseInodeInfo(unixFile *pFile){
1228   unixInodeInfo *pInode = pFile->pInode;
1229   assert( unixMutexHeld() );
1230   if( ALWAYS(pInode) ){
1231     pInode->nRef--;
1232     if( pInode->nRef==0 ){
1233       assert( pInode->pShmNode==0 );
1234       closePendingFds(pFile);
1235       if( pInode->pPrev ){
1236         assert( pInode->pPrev->pNext==pInode );
1237         pInode->pPrev->pNext = pInode->pNext;
1238       }else{
1239         assert( inodeList==pInode );
1240         inodeList = pInode->pNext;
1241       }
1242       if( pInode->pNext ){
1243         assert( pInode->pNext->pPrev==pInode );
1244         pInode->pNext->pPrev = pInode->pPrev;
1245       }
1246       sqlite3_free(pInode);
1247     }
1248   }
1249 }
1250 
1251 /*
1252 ** Given a file descriptor, locate the unixInodeInfo object that
1253 ** describes that file descriptor.  Create a new one if necessary.  The
1254 ** return value might be uninitialized if an error occurs.
1255 **
1256 ** The mutex entered using the unixEnterMutex() function must be held
1257 ** when this function is called.
1258 **
1259 ** Return an appropriate error code.
1260 */
findInodeInfo(unixFile * pFile,unixInodeInfo ** ppInode)1261 static int findInodeInfo(
1262   unixFile *pFile,               /* Unix file with file desc used in the key */
1263   unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
1264 ){
1265   int rc;                        /* System call return code */
1266   int fd;                        /* The file descriptor for pFile */
1267   struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
1268   struct stat statbuf;           /* Low-level file information */
1269   unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
1270 
1271   assert( unixMutexHeld() );
1272 
1273   /* Get low-level information about the file that we can used to
1274   ** create a unique name for the file.
1275   */
1276   fd = pFile->h;
1277   rc = osFstat(fd, &statbuf);
1278   if( rc!=0 ){
1279     storeLastErrno(pFile, errno);
1280 #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
1281     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
1282 #endif
1283     return SQLITE_IOERR;
1284   }
1285 
1286 #ifdef __APPLE__
1287   /* On OS X on an msdos filesystem, the inode number is reported
1288   ** incorrectly for zero-size files.  See ticket #3260.  To work
1289   ** around this problem (we consider it a bug in OS X, not SQLite)
1290   ** we always increase the file size to 1 by writing a single byte
1291   ** prior to accessing the inode number.  The one byte written is
1292   ** an ASCII 'S' character which also happens to be the first byte
1293   ** in the header of every SQLite database.  In this way, if there
1294   ** is a race condition such that another thread has already populated
1295   ** the first page of the database, no damage is done.
1296   */
1297   if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
1298     do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
1299     if( rc!=1 ){
1300       storeLastErrno(pFile, errno);
1301       return SQLITE_IOERR;
1302     }
1303     rc = osFstat(fd, &statbuf);
1304     if( rc!=0 ){
1305       storeLastErrno(pFile, errno);
1306       return SQLITE_IOERR;
1307     }
1308   }
1309 #endif
1310 
1311   memset(&fileId, 0, sizeof(fileId));
1312   fileId.dev = statbuf.st_dev;
1313 #if OS_VXWORKS
1314   fileId.pId = pFile->pId;
1315 #else
1316   fileId.ino = (u64)statbuf.st_ino;
1317 #endif
1318   pInode = inodeList;
1319   while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
1320     pInode = pInode->pNext;
1321   }
1322   if( pInode==0 ){
1323     pInode = sqlite3_malloc64( sizeof(*pInode) );
1324     if( pInode==0 ){
1325       return SQLITE_NOMEM_BKPT;
1326     }
1327     memset(pInode, 0, sizeof(*pInode));
1328     memcpy(&pInode->fileId, &fileId, sizeof(fileId));
1329     pInode->nRef = 1;
1330     pInode->pNext = inodeList;
1331     pInode->pPrev = 0;
1332     if( inodeList ) inodeList->pPrev = pInode;
1333     inodeList = pInode;
1334   }else{
1335     pInode->nRef++;
1336   }
1337   *ppInode = pInode;
1338   return SQLITE_OK;
1339 }
1340 
1341 /*
1342 ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
1343 */
fileHasMoved(unixFile * pFile)1344 static int fileHasMoved(unixFile *pFile){
1345 #if OS_VXWORKS
1346   return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
1347 #else
1348   struct stat buf;
1349   return pFile->pInode!=0 &&
1350       (osStat(pFile->zPath, &buf)!=0
1351          || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
1352 #endif
1353 }
1354 
1355 
1356 /*
1357 ** Check a unixFile that is a database.  Verify the following:
1358 **
1359 ** (1) There is exactly one hard link on the file
1360 ** (2) The file is not a symbolic link
1361 ** (3) The file has not been renamed or unlinked
1362 **
1363 ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
1364 */
verifyDbFile(unixFile * pFile)1365 static void verifyDbFile(unixFile *pFile){
1366   struct stat buf;
1367   int rc;
1368 
1369   /* These verifications occurs for the main database only */
1370   if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
1371 
1372   rc = osFstat(pFile->h, &buf);
1373   if( rc!=0 ){
1374     sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
1375     return;
1376   }
1377   if( buf.st_nlink==0 ){
1378     sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
1379     return;
1380   }
1381   if( buf.st_nlink>1 ){
1382     sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
1383     return;
1384   }
1385   if( fileHasMoved(pFile) ){
1386     sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
1387     return;
1388   }
1389 }
1390 
1391 
1392 /*
1393 ** This routine checks if there is a RESERVED lock held on the specified
1394 ** file by this or any other process. If such a lock is held, set *pResOut
1395 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
1396 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
1397 */
unixCheckReservedLock(sqlite3_file * id,int * pResOut)1398 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
1399   int rc = SQLITE_OK;
1400   int reserved = 0;
1401   unixFile *pFile = (unixFile*)id;
1402 
1403   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1404 
1405   assert( pFile );
1406   assert( pFile->eFileLock<=SHARED_LOCK );
1407   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
1408 
1409   /* Check if a thread in this process holds such a lock */
1410   if( pFile->pInode->eFileLock>SHARED_LOCK ){
1411     reserved = 1;
1412   }
1413 
1414   /* Otherwise see if some other process holds it.
1415   */
1416 #ifndef __DJGPP__
1417   if( !reserved && !pFile->pInode->bProcessLock ){
1418     struct flock lock;
1419     lock.l_whence = SEEK_SET;
1420     lock.l_start = RESERVED_BYTE;
1421     lock.l_len = 1;
1422     lock.l_type = F_WRLCK;
1423     if( osFcntl(pFile->h, F_GETLK, &lock) ){
1424       rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
1425       storeLastErrno(pFile, errno);
1426     } else if( lock.l_type!=F_UNLCK ){
1427       reserved = 1;
1428     }
1429   }
1430 #endif
1431 
1432   unixLeaveMutex();
1433   OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
1434 
1435   *pResOut = reserved;
1436   return rc;
1437 }
1438 
1439 /*
1440 ** Attempt to set a system-lock on the file pFile.  The lock is
1441 ** described by pLock.
1442 **
1443 ** If the pFile was opened read/write from unix-excl, then the only lock
1444 ** ever obtained is an exclusive lock, and it is obtained exactly once
1445 ** the first time any lock is attempted.  All subsequent system locking
1446 ** operations become no-ops.  Locking operations still happen internally,
1447 ** in order to coordinate access between separate database connections
1448 ** within this process, but all of that is handled in memory and the
1449 ** operating system does not participate.
1450 **
1451 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
1452 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
1453 ** and is read-only.
1454 **
1455 ** Zero is returned if the call completes successfully, or -1 if a call
1456 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
1457 */
unixFileLock(unixFile * pFile,struct flock * pLock)1458 static int unixFileLock(unixFile *pFile, struct flock *pLock){
1459   int rc;
1460   unixInodeInfo *pInode = pFile->pInode;
1461   assert( unixMutexHeld() );
1462   assert( pInode!=0 );
1463   if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
1464     if( pInode->bProcessLock==0 ){
1465       struct flock lock;
1466       assert( pInode->nLock==0 );
1467       lock.l_whence = SEEK_SET;
1468       lock.l_start = SHARED_FIRST;
1469       lock.l_len = SHARED_SIZE;
1470       lock.l_type = F_WRLCK;
1471       rc = osFcntl(pFile->h, F_SETLK, &lock);
1472       if( rc<0 ) return rc;
1473       pInode->bProcessLock = 1;
1474       pInode->nLock++;
1475     }else{
1476       rc = 0;
1477     }
1478   }else{
1479     rc = osFcntl(pFile->h, F_SETLK, pLock);
1480   }
1481   return rc;
1482 }
1483 
1484 /*
1485 ** Lock the file with the lock specified by parameter eFileLock - one
1486 ** of the following:
1487 **
1488 **     (1) SHARED_LOCK
1489 **     (2) RESERVED_LOCK
1490 **     (3) PENDING_LOCK
1491 **     (4) EXCLUSIVE_LOCK
1492 **
1493 ** Sometimes when requesting one lock state, additional lock states
1494 ** are inserted in between.  The locking might fail on one of the later
1495 ** transitions leaving the lock state different from what it started but
1496 ** still short of its goal.  The following chart shows the allowed
1497 ** transitions and the inserted intermediate states:
1498 **
1499 **    UNLOCKED -> SHARED
1500 **    SHARED -> RESERVED
1501 **    SHARED -> (PENDING) -> EXCLUSIVE
1502 **    RESERVED -> (PENDING) -> EXCLUSIVE
1503 **    PENDING -> EXCLUSIVE
1504 **
1505 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
1506 ** routine to lower a locking level.
1507 */
unixLock(sqlite3_file * id,int eFileLock)1508 static int unixLock(sqlite3_file *id, int eFileLock){
1509   /* The following describes the implementation of the various locks and
1510   ** lock transitions in terms of the POSIX advisory shared and exclusive
1511   ** lock primitives (called read-locks and write-locks below, to avoid
1512   ** confusion with SQLite lock names). The algorithms are complicated
1513   ** slightly in order to be compatible with Windows95 systems simultaneously
1514   ** accessing the same database file, in case that is ever required.
1515   **
1516   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1517   ** byte', each single bytes at well known offsets, and the 'shared byte
1518   ** range', a range of 510 bytes at a well known offset.
1519   **
1520   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1521   ** byte'.  If this is successful, 'shared byte range' is read-locked
1522   ** and the lock on the 'pending byte' released.  (Legacy note:  When
1523   ** SQLite was first developed, Windows95 systems were still very common,
1524   ** and Widnows95 lacks a shared-lock capability.  So on Windows95, a
1525   ** single randomly selected by from the 'shared byte range' is locked.
1526   ** Windows95 is now pretty much extinct, but this work-around for the
1527   ** lack of shared-locks on Windows95 lives on, for backwards
1528   ** compatibility.)
1529   **
1530   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1531   ** A RESERVED lock is implemented by grabbing a write-lock on the
1532   ** 'reserved byte'.
1533   **
1534   ** A process may only obtain a PENDING lock after it has obtained a
1535   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1536   ** on the 'pending byte'. This ensures that no new SHARED locks can be
1537   ** obtained, but existing SHARED locks are allowed to persist. A process
1538   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1539   ** This property is used by the algorithm for rolling back a journal file
1540   ** after a crash.
1541   **
1542   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1543   ** implemented by obtaining a write-lock on the entire 'shared byte
1544   ** range'. Since all other locks require a read-lock on one of the bytes
1545   ** within this range, this ensures that no other locks are held on the
1546   ** database.
1547   */
1548   int rc = SQLITE_OK;
1549   unixFile *pFile = (unixFile*)id;
1550   unixInodeInfo *pInode;
1551   struct flock lock;
1552   int tErrno = 0;
1553 
1554   assert( pFile );
1555   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
1556       azFileLock(eFileLock), azFileLock(pFile->eFileLock),
1557       azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
1558       osGetpid(0)));
1559 
1560   /* If there is already a lock of this type or more restrictive on the
1561   ** unixFile, do nothing. Don't use the end_lock: exit path, as
1562   ** unixEnterMutex() hasn't been called yet.
1563   */
1564   if( pFile->eFileLock>=eFileLock ){
1565     OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
1566             azFileLock(eFileLock)));
1567     return SQLITE_OK;
1568   }
1569 
1570   /* Make sure the locking sequence is correct.
1571   **  (1) We never move from unlocked to anything higher than shared lock.
1572   **  (2) SQLite never explicitly requests a pendig lock.
1573   **  (3) A shared lock is always held when a reserve lock is requested.
1574   */
1575   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
1576   assert( eFileLock!=PENDING_LOCK );
1577   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
1578 
1579   /* This mutex is needed because pFile->pInode is shared across threads
1580   */
1581   unixEnterMutex();
1582   pInode = pFile->pInode;
1583 
1584   /* If some thread using this PID has a lock via a different unixFile*
1585   ** handle that precludes the requested lock, return BUSY.
1586   */
1587   if( (pFile->eFileLock!=pInode->eFileLock &&
1588           (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
1589   ){
1590     rc = SQLITE_BUSY;
1591     goto end_lock;
1592   }
1593 
1594   /* If a SHARED lock is requested, and some thread using this PID already
1595   ** has a SHARED or RESERVED lock, then increment reference counts and
1596   ** return SQLITE_OK.
1597   */
1598   if( eFileLock==SHARED_LOCK &&
1599       (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
1600     assert( eFileLock==SHARED_LOCK );
1601     assert( pFile->eFileLock==0 );
1602     assert( pInode->nShared>0 );
1603     pFile->eFileLock = SHARED_LOCK;
1604     pInode->nShared++;
1605     pInode->nLock++;
1606     goto end_lock;
1607   }
1608 
1609 
1610   /* A PENDING lock is needed before acquiring a SHARED lock and before
1611   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
1612   ** be released.
1613   */
1614   lock.l_len = 1L;
1615   lock.l_whence = SEEK_SET;
1616   if( eFileLock==SHARED_LOCK
1617       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
1618   ){
1619     lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
1620     lock.l_start = PENDING_BYTE;
1621     if( unixFileLock(pFile, &lock) ){
1622       tErrno = errno;
1623       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1624       if( rc!=SQLITE_BUSY ){
1625         storeLastErrno(pFile, tErrno);
1626       }
1627       goto end_lock;
1628     }
1629   }
1630 
1631 
1632   /* If control gets to this point, then actually go ahead and make
1633   ** operating system calls for the specified lock.
1634   */
1635   if( eFileLock==SHARED_LOCK ){
1636     assert( pInode->nShared==0 );
1637     assert( pInode->eFileLock==0 );
1638     assert( rc==SQLITE_OK );
1639 
1640     /* Now get the read-lock */
1641     lock.l_start = SHARED_FIRST;
1642     lock.l_len = SHARED_SIZE;
1643     if( unixFileLock(pFile, &lock) ){
1644       tErrno = errno;
1645       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1646     }
1647 
1648     /* Drop the temporary PENDING lock */
1649     lock.l_start = PENDING_BYTE;
1650     lock.l_len = 1L;
1651     lock.l_type = F_UNLCK;
1652     if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
1653       /* This could happen with a network mount */
1654       tErrno = errno;
1655       rc = SQLITE_IOERR_UNLOCK;
1656     }
1657 
1658     if( rc ){
1659       if( rc!=SQLITE_BUSY ){
1660         storeLastErrno(pFile, tErrno);
1661       }
1662       goto end_lock;
1663     }else{
1664       pFile->eFileLock = SHARED_LOCK;
1665       pInode->nLock++;
1666       pInode->nShared = 1;
1667     }
1668   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
1669     /* We are trying for an exclusive lock but another thread in this
1670     ** same process is still holding a shared lock. */
1671     rc = SQLITE_BUSY;
1672   }else{
1673     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
1674     ** assumed that there is a SHARED or greater lock on the file
1675     ** already.
1676     */
1677     assert( 0!=pFile->eFileLock );
1678     lock.l_type = F_WRLCK;
1679 
1680     assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
1681     if( eFileLock==RESERVED_LOCK ){
1682       lock.l_start = RESERVED_BYTE;
1683       lock.l_len = 1L;
1684     }else{
1685       lock.l_start = SHARED_FIRST;
1686       lock.l_len = SHARED_SIZE;
1687     }
1688 
1689     if( unixFileLock(pFile, &lock) ){
1690       tErrno = errno;
1691       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1692       if( rc!=SQLITE_BUSY ){
1693         storeLastErrno(pFile, tErrno);
1694       }
1695     }
1696   }
1697 
1698 
1699 #ifdef SQLITE_DEBUG
1700   /* Set up the transaction-counter change checking flags when
1701   ** transitioning from a SHARED to a RESERVED lock.  The change
1702   ** from SHARED to RESERVED marks the beginning of a normal
1703   ** write operation (not a hot journal rollback).
1704   */
1705   if( rc==SQLITE_OK
1706    && pFile->eFileLock<=SHARED_LOCK
1707    && eFileLock==RESERVED_LOCK
1708   ){
1709     pFile->transCntrChng = 0;
1710     pFile->dbUpdate = 0;
1711     pFile->inNormalWrite = 1;
1712   }
1713 #endif
1714 
1715 
1716   if( rc==SQLITE_OK ){
1717     pFile->eFileLock = eFileLock;
1718     pInode->eFileLock = eFileLock;
1719   }else if( eFileLock==EXCLUSIVE_LOCK ){
1720     pFile->eFileLock = PENDING_LOCK;
1721     pInode->eFileLock = PENDING_LOCK;
1722   }
1723 
1724 end_lock:
1725   unixLeaveMutex();
1726   OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
1727       rc==SQLITE_OK ? "ok" : "failed"));
1728   return rc;
1729 }
1730 
1731 /*
1732 ** Add the file descriptor used by file handle pFile to the corresponding
1733 ** pUnused list.
1734 */
setPendingFd(unixFile * pFile)1735 static void setPendingFd(unixFile *pFile){
1736   unixInodeInfo *pInode = pFile->pInode;
1737   UnixUnusedFd *p = pFile->pUnused;
1738   p->pNext = pInode->pUnused;
1739   pInode->pUnused = p;
1740   pFile->h = -1;
1741   pFile->pUnused = 0;
1742 }
1743 
1744 /*
1745 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
1746 ** must be either NO_LOCK or SHARED_LOCK.
1747 **
1748 ** If the locking level of the file descriptor is already at or below
1749 ** the requested locking level, this routine is a no-op.
1750 **
1751 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
1752 ** the byte range is divided into 2 parts and the first part is unlocked then
1753 ** set to a read lock, then the other part is simply unlocked.  This works
1754 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
1755 ** remove the write lock on a region when a read lock is set.
1756 */
posixUnlock(sqlite3_file * id,int eFileLock,int handleNFSUnlock)1757 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
1758   unixFile *pFile = (unixFile*)id;
1759   unixInodeInfo *pInode;
1760   struct flock lock;
1761   int rc = SQLITE_OK;
1762 
1763   assert( pFile );
1764   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
1765       pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
1766       osGetpid(0)));
1767 
1768   assert( eFileLock<=SHARED_LOCK );
1769   if( pFile->eFileLock<=eFileLock ){
1770     return SQLITE_OK;
1771   }
1772   unixEnterMutex();
1773   pInode = pFile->pInode;
1774   assert( pInode->nShared!=0 );
1775   if( pFile->eFileLock>SHARED_LOCK ){
1776     assert( pInode->eFileLock==pFile->eFileLock );
1777 
1778 #ifdef SQLITE_DEBUG
1779     /* When reducing a lock such that other processes can start
1780     ** reading the database file again, make sure that the
1781     ** transaction counter was updated if any part of the database
1782     ** file changed.  If the transaction counter is not updated,
1783     ** other connections to the same file might not realize that
1784     ** the file has changed and hence might not know to flush their
1785     ** cache.  The use of a stale cache can lead to database corruption.
1786     */
1787     pFile->inNormalWrite = 0;
1788 #endif
1789 
1790     /* downgrading to a shared lock on NFS involves clearing the write lock
1791     ** before establishing the readlock - to avoid a race condition we downgrade
1792     ** the lock in 2 blocks, so that part of the range will be covered by a
1793     ** write lock until the rest is covered by a read lock:
1794     **  1:   [WWWWW]
1795     **  2:   [....W]
1796     **  3:   [RRRRW]
1797     **  4:   [RRRR.]
1798     */
1799     if( eFileLock==SHARED_LOCK ){
1800 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
1801       (void)handleNFSUnlock;
1802       assert( handleNFSUnlock==0 );
1803 #endif
1804 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
1805       if( handleNFSUnlock ){
1806         int tErrno;               /* Error code from system call errors */
1807         off_t divSize = SHARED_SIZE - 1;
1808 
1809         lock.l_type = F_UNLCK;
1810         lock.l_whence = SEEK_SET;
1811         lock.l_start = SHARED_FIRST;
1812         lock.l_len = divSize;
1813         if( unixFileLock(pFile, &lock)==(-1) ){
1814           tErrno = errno;
1815           rc = SQLITE_IOERR_UNLOCK;
1816           storeLastErrno(pFile, tErrno);
1817           goto end_unlock;
1818         }
1819         lock.l_type = F_RDLCK;
1820         lock.l_whence = SEEK_SET;
1821         lock.l_start = SHARED_FIRST;
1822         lock.l_len = divSize;
1823         if( unixFileLock(pFile, &lock)==(-1) ){
1824           tErrno = errno;
1825           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1826           if( IS_LOCK_ERROR(rc) ){
1827             storeLastErrno(pFile, tErrno);
1828           }
1829           goto end_unlock;
1830         }
1831         lock.l_type = F_UNLCK;
1832         lock.l_whence = SEEK_SET;
1833         lock.l_start = SHARED_FIRST+divSize;
1834         lock.l_len = SHARED_SIZE-divSize;
1835         if( unixFileLock(pFile, &lock)==(-1) ){
1836           tErrno = errno;
1837           rc = SQLITE_IOERR_UNLOCK;
1838           storeLastErrno(pFile, tErrno);
1839           goto end_unlock;
1840         }
1841       }else
1842 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
1843       {
1844         lock.l_type = F_RDLCK;
1845         lock.l_whence = SEEK_SET;
1846         lock.l_start = SHARED_FIRST;
1847         lock.l_len = SHARED_SIZE;
1848         if( unixFileLock(pFile, &lock) ){
1849           /* In theory, the call to unixFileLock() cannot fail because another
1850           ** process is holding an incompatible lock. If it does, this
1851           ** indicates that the other process is not following the locking
1852           ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
1853           ** SQLITE_BUSY would confuse the upper layer (in practice it causes
1854           ** an assert to fail). */
1855           rc = SQLITE_IOERR_RDLOCK;
1856           storeLastErrno(pFile, errno);
1857           goto end_unlock;
1858         }
1859       }
1860     }
1861     lock.l_type = F_UNLCK;
1862     lock.l_whence = SEEK_SET;
1863     lock.l_start = PENDING_BYTE;
1864     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
1865     if( unixFileLock(pFile, &lock)==0 ){
1866       pInode->eFileLock = SHARED_LOCK;
1867     }else{
1868       rc = SQLITE_IOERR_UNLOCK;
1869       storeLastErrno(pFile, errno);
1870       goto end_unlock;
1871     }
1872   }
1873   if( eFileLock==NO_LOCK ){
1874     /* Decrement the shared lock counter.  Release the lock using an
1875     ** OS call only when all threads in this same process have released
1876     ** the lock.
1877     */
1878     pInode->nShared--;
1879     if( pInode->nShared==0 ){
1880       lock.l_type = F_UNLCK;
1881       lock.l_whence = SEEK_SET;
1882       lock.l_start = lock.l_len = 0L;
1883       if( unixFileLock(pFile, &lock)==0 ){
1884         pInode->eFileLock = NO_LOCK;
1885       }else{
1886         rc = SQLITE_IOERR_UNLOCK;
1887         storeLastErrno(pFile, errno);
1888         pInode->eFileLock = NO_LOCK;
1889         pFile->eFileLock = NO_LOCK;
1890       }
1891     }
1892 
1893     /* Decrement the count of locks against this same file.  When the
1894     ** count reaches zero, close any other file descriptors whose close
1895     ** was deferred because of outstanding locks.
1896     */
1897     pInode->nLock--;
1898     assert( pInode->nLock>=0 );
1899     if( pInode->nLock==0 ){
1900       closePendingFds(pFile);
1901     }
1902   }
1903 
1904 end_unlock:
1905   unixLeaveMutex();
1906   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
1907   return rc;
1908 }
1909 
1910 /*
1911 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
1912 ** must be either NO_LOCK or SHARED_LOCK.
1913 **
1914 ** If the locking level of the file descriptor is already at or below
1915 ** the requested locking level, this routine is a no-op.
1916 */
unixUnlock(sqlite3_file * id,int eFileLock)1917 static int unixUnlock(sqlite3_file *id, int eFileLock){
1918 #if SQLITE_MAX_MMAP_SIZE>0
1919   assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
1920 #endif
1921   return posixUnlock(id, eFileLock, 0);
1922 }
1923 
1924 #if SQLITE_MAX_MMAP_SIZE>0
1925 static int unixMapfile(unixFile *pFd, i64 nByte);
1926 static void unixUnmapfile(unixFile *pFd);
1927 #endif
1928 
1929 /*
1930 ** This function performs the parts of the "close file" operation
1931 ** common to all locking schemes. It closes the directory and file
1932 ** handles, if they are valid, and sets all fields of the unixFile
1933 ** structure to 0.
1934 **
1935 ** It is *not* necessary to hold the mutex when this routine is called,
1936 ** even on VxWorks.  A mutex will be acquired on VxWorks by the
1937 ** vxworksReleaseFileId() routine.
1938 */
closeUnixFile(sqlite3_file * id)1939 static int closeUnixFile(sqlite3_file *id){
1940   unixFile *pFile = (unixFile*)id;
1941 #if SQLITE_MAX_MMAP_SIZE>0
1942   unixUnmapfile(pFile);
1943 #endif
1944   if( pFile->h>=0 ){
1945     robust_close(pFile, pFile->h, __LINE__);
1946     pFile->h = -1;
1947   }
1948 #if OS_VXWORKS
1949   if( pFile->pId ){
1950     if( pFile->ctrlFlags & UNIXFILE_DELETE ){
1951       osUnlink(pFile->pId->zCanonicalName);
1952     }
1953     vxworksReleaseFileId(pFile->pId);
1954     pFile->pId = 0;
1955   }
1956 #endif
1957 #ifdef SQLITE_UNLINK_AFTER_CLOSE
1958   if( pFile->ctrlFlags & UNIXFILE_DELETE ){
1959     osUnlink(pFile->zPath);
1960     sqlite3_free(*(char**)&pFile->zPath);
1961     pFile->zPath = 0;
1962   }
1963 #endif
1964   OSTRACE(("CLOSE   %-3d\n", pFile->h));
1965   OpenCounter(-1);
1966   sqlite3_free(pFile->pUnused);
1967   memset(pFile, 0, sizeof(unixFile));
1968   return SQLITE_OK;
1969 }
1970 
1971 /*
1972 ** Close a file.
1973 */
unixClose(sqlite3_file * id)1974 static int unixClose(sqlite3_file *id){
1975   int rc = SQLITE_OK;
1976   unixFile *pFile = (unixFile *)id;
1977   verifyDbFile(pFile);
1978   unixUnlock(id, NO_LOCK);
1979   unixEnterMutex();
1980 
1981   /* unixFile.pInode is always valid here. Otherwise, a different close
1982   ** routine (e.g. nolockClose()) would be called instead.
1983   */
1984   assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
1985   if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
1986     /* If there are outstanding locks, do not actually close the file just
1987     ** yet because that would clear those locks.  Instead, add the file
1988     ** descriptor to pInode->pUnused list.  It will be automatically closed
1989     ** when the last lock is cleared.
1990     */
1991     setPendingFd(pFile);
1992   }
1993   releaseInodeInfo(pFile);
1994   rc = closeUnixFile(id);
1995   unixLeaveMutex();
1996   return rc;
1997 }
1998 
1999 /************** End of the posix advisory lock implementation *****************
2000 ******************************************************************************/
2001 
2002 /******************************************************************************
2003 ****************************** No-op Locking **********************************
2004 **
2005 ** Of the various locking implementations available, this is by far the
2006 ** simplest:  locking is ignored.  No attempt is made to lock the database
2007 ** file for reading or writing.
2008 **
2009 ** This locking mode is appropriate for use on read-only databases
2010 ** (ex: databases that are burned into CD-ROM, for example.)  It can
2011 ** also be used if the application employs some external mechanism to
2012 ** prevent simultaneous access of the same database by two or more
2013 ** database connections.  But there is a serious risk of database
2014 ** corruption if this locking mode is used in situations where multiple
2015 ** database connections are accessing the same database file at the same
2016 ** time and one or more of those connections are writing.
2017 */
2018 
nolockCheckReservedLock(sqlite3_file * NotUsed,int * pResOut)2019 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
2020   UNUSED_PARAMETER(NotUsed);
2021   *pResOut = 0;
2022   return SQLITE_OK;
2023 }
nolockLock(sqlite3_file * NotUsed,int NotUsed2)2024 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
2025   UNUSED_PARAMETER2(NotUsed, NotUsed2);
2026   return SQLITE_OK;
2027 }
nolockUnlock(sqlite3_file * NotUsed,int NotUsed2)2028 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
2029   UNUSED_PARAMETER2(NotUsed, NotUsed2);
2030   return SQLITE_OK;
2031 }
2032 
2033 /*
2034 ** Close the file.
2035 */
nolockClose(sqlite3_file * id)2036 static int nolockClose(sqlite3_file *id) {
2037   return closeUnixFile(id);
2038 }
2039 
2040 /******************* End of the no-op lock implementation *********************
2041 ******************************************************************************/
2042 
2043 /******************************************************************************
2044 ************************* Begin dot-file Locking ******************************
2045 **
2046 ** The dotfile locking implementation uses the existence of separate lock
2047 ** files (really a directory) to control access to the database.  This works
2048 ** on just about every filesystem imaginable.  But there are serious downsides:
2049 **
2050 **    (1)  There is zero concurrency.  A single reader blocks all other
2051 **         connections from reading or writing the database.
2052 **
2053 **    (2)  An application crash or power loss can leave stale lock files
2054 **         sitting around that need to be cleared manually.
2055 **
2056 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
2057 ** other locking strategy is available.
2058 **
2059 ** Dotfile locking works by creating a subdirectory in the same directory as
2060 ** the database and with the same name but with a ".lock" extension added.
2061 ** The existence of a lock directory implies an EXCLUSIVE lock.  All other
2062 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
2063 */
2064 
2065 /*
2066 ** The file suffix added to the data base filename in order to create the
2067 ** lock directory.
2068 */
2069 #define DOTLOCK_SUFFIX ".lock"
2070 
2071 /*
2072 ** This routine checks if there is a RESERVED lock held on the specified
2073 ** file by this or any other process. If such a lock is held, set *pResOut
2074 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
2075 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2076 **
2077 ** In dotfile locking, either a lock exists or it does not.  So in this
2078 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
2079 ** is held on the file and false if the file is unlocked.
2080 */
dotlockCheckReservedLock(sqlite3_file * id,int * pResOut)2081 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
2082   int rc = SQLITE_OK;
2083   int reserved = 0;
2084   unixFile *pFile = (unixFile*)id;
2085 
2086   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2087 
2088   assert( pFile );
2089   reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
2090   OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
2091   *pResOut = reserved;
2092   return rc;
2093 }
2094 
2095 /*
2096 ** Lock the file with the lock specified by parameter eFileLock - one
2097 ** of the following:
2098 **
2099 **     (1) SHARED_LOCK
2100 **     (2) RESERVED_LOCK
2101 **     (3) PENDING_LOCK
2102 **     (4) EXCLUSIVE_LOCK
2103 **
2104 ** Sometimes when requesting one lock state, additional lock states
2105 ** are inserted in between.  The locking might fail on one of the later
2106 ** transitions leaving the lock state different from what it started but
2107 ** still short of its goal.  The following chart shows the allowed
2108 ** transitions and the inserted intermediate states:
2109 **
2110 **    UNLOCKED -> SHARED
2111 **    SHARED -> RESERVED
2112 **    SHARED -> (PENDING) -> EXCLUSIVE
2113 **    RESERVED -> (PENDING) -> EXCLUSIVE
2114 **    PENDING -> EXCLUSIVE
2115 **
2116 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
2117 ** routine to lower a locking level.
2118 **
2119 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
2120 ** But we track the other locking levels internally.
2121 */
dotlockLock(sqlite3_file * id,int eFileLock)2122 static int dotlockLock(sqlite3_file *id, int eFileLock) {
2123   unixFile *pFile = (unixFile*)id;
2124   char *zLockFile = (char *)pFile->lockingContext;
2125   int rc = SQLITE_OK;
2126 
2127 
2128   /* If we have any lock, then the lock file already exists.  All we have
2129   ** to do is adjust our internal record of the lock level.
2130   */
2131   if( pFile->eFileLock > NO_LOCK ){
2132     pFile->eFileLock = eFileLock;
2133     /* Always update the timestamp on the old file */
2134 #ifdef HAVE_UTIME
2135     utime(zLockFile, NULL);
2136 #else
2137     utimes(zLockFile, NULL);
2138 #endif
2139     return SQLITE_OK;
2140   }
2141 
2142   /* grab an exclusive lock */
2143   rc = osMkdir(zLockFile, 0777);
2144   if( rc<0 ){
2145     /* failed to open/create the lock directory */
2146     int tErrno = errno;
2147     if( EEXIST == tErrno ){
2148       rc = SQLITE_BUSY;
2149     } else {
2150       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2151       if( rc!=SQLITE_BUSY ){
2152         storeLastErrno(pFile, tErrno);
2153       }
2154     }
2155     return rc;
2156   }
2157 
2158   /* got it, set the type and return ok */
2159   pFile->eFileLock = eFileLock;
2160   return rc;
2161 }
2162 
2163 /*
2164 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
2165 ** must be either NO_LOCK or SHARED_LOCK.
2166 **
2167 ** If the locking level of the file descriptor is already at or below
2168 ** the requested locking level, this routine is a no-op.
2169 **
2170 ** When the locking level reaches NO_LOCK, delete the lock file.
2171 */
dotlockUnlock(sqlite3_file * id,int eFileLock)2172 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
2173   unixFile *pFile = (unixFile*)id;
2174   char *zLockFile = (char *)pFile->lockingContext;
2175   int rc;
2176 
2177   assert( pFile );
2178   OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
2179            pFile->eFileLock, osGetpid(0)));
2180   assert( eFileLock<=SHARED_LOCK );
2181 
2182   /* no-op if possible */
2183   if( pFile->eFileLock==eFileLock ){
2184     return SQLITE_OK;
2185   }
2186 
2187   /* To downgrade to shared, simply update our internal notion of the
2188   ** lock state.  No need to mess with the file on disk.
2189   */
2190   if( eFileLock==SHARED_LOCK ){
2191     pFile->eFileLock = SHARED_LOCK;
2192     return SQLITE_OK;
2193   }
2194 
2195   /* To fully unlock the database, delete the lock file */
2196   assert( eFileLock==NO_LOCK );
2197   rc = osRmdir(zLockFile);
2198   if( rc<0 ){
2199     int tErrno = errno;
2200     if( tErrno==ENOENT ){
2201       rc = SQLITE_OK;
2202     }else{
2203       rc = SQLITE_IOERR_UNLOCK;
2204       storeLastErrno(pFile, tErrno);
2205     }
2206     return rc;
2207   }
2208   pFile->eFileLock = NO_LOCK;
2209   return SQLITE_OK;
2210 }
2211 
2212 /*
2213 ** Close a file.  Make sure the lock has been released before closing.
2214 */
dotlockClose(sqlite3_file * id)2215 static int dotlockClose(sqlite3_file *id) {
2216   unixFile *pFile = (unixFile*)id;
2217   assert( id!=0 );
2218   dotlockUnlock(id, NO_LOCK);
2219   sqlite3_free(pFile->lockingContext);
2220   return closeUnixFile(id);
2221 }
2222 /****************** End of the dot-file lock implementation *******************
2223 ******************************************************************************/
2224 
2225 /******************************************************************************
2226 ************************** Begin flock Locking ********************************
2227 **
2228 ** Use the flock() system call to do file locking.
2229 **
2230 ** flock() locking is like dot-file locking in that the various
2231 ** fine-grain locking levels supported by SQLite are collapsed into
2232 ** a single exclusive lock.  In other words, SHARED, RESERVED, and
2233 ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
2234 ** still works when you do this, but concurrency is reduced since
2235 ** only a single process can be reading the database at a time.
2236 **
2237 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
2238 */
2239 #if SQLITE_ENABLE_LOCKING_STYLE
2240 
2241 /*
2242 ** Retry flock() calls that fail with EINTR
2243 */
2244 #ifdef EINTR
robust_flock(int fd,int op)2245 static int robust_flock(int fd, int op){
2246   int rc;
2247   do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
2248   return rc;
2249 }
2250 #else
2251 # define robust_flock(a,b) flock(a,b)
2252 #endif
2253 
2254 
2255 /*
2256 ** This routine checks if there is a RESERVED lock held on the specified
2257 ** file by this or any other process. If such a lock is held, set *pResOut
2258 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
2259 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2260 */
flockCheckReservedLock(sqlite3_file * id,int * pResOut)2261 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
2262   int rc = SQLITE_OK;
2263   int reserved = 0;
2264   unixFile *pFile = (unixFile*)id;
2265 
2266   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2267 
2268   assert( pFile );
2269 
2270   /* Check if a thread in this process holds such a lock */
2271   if( pFile->eFileLock>SHARED_LOCK ){
2272     reserved = 1;
2273   }
2274 
2275   /* Otherwise see if some other process holds it. */
2276   if( !reserved ){
2277     /* attempt to get the lock */
2278     int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
2279     if( !lrc ){
2280       /* got the lock, unlock it */
2281       lrc = robust_flock(pFile->h, LOCK_UN);
2282       if ( lrc ) {
2283         int tErrno = errno;
2284         /* unlock failed with an error */
2285         lrc = SQLITE_IOERR_UNLOCK;
2286         storeLastErrno(pFile, tErrno);
2287         rc = lrc;
2288       }
2289     } else {
2290       int tErrno = errno;
2291       reserved = 1;
2292       /* someone else might have it reserved */
2293       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2294       if( IS_LOCK_ERROR(lrc) ){
2295         storeLastErrno(pFile, tErrno);
2296         rc = lrc;
2297       }
2298     }
2299   }
2300   OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
2301 
2302 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2303   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
2304     rc = SQLITE_OK;
2305     reserved=1;
2306   }
2307 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
2308   *pResOut = reserved;
2309   return rc;
2310 }
2311 
2312 /*
2313 ** Lock the file with the lock specified by parameter eFileLock - one
2314 ** of the following:
2315 **
2316 **     (1) SHARED_LOCK
2317 **     (2) RESERVED_LOCK
2318 **     (3) PENDING_LOCK
2319 **     (4) EXCLUSIVE_LOCK
2320 **
2321 ** Sometimes when requesting one lock state, additional lock states
2322 ** are inserted in between.  The locking might fail on one of the later
2323 ** transitions leaving the lock state different from what it started but
2324 ** still short of its goal.  The following chart shows the allowed
2325 ** transitions and the inserted intermediate states:
2326 **
2327 **    UNLOCKED -> SHARED
2328 **    SHARED -> RESERVED
2329 **    SHARED -> (PENDING) -> EXCLUSIVE
2330 **    RESERVED -> (PENDING) -> EXCLUSIVE
2331 **    PENDING -> EXCLUSIVE
2332 **
2333 ** flock() only really support EXCLUSIVE locks.  We track intermediate
2334 ** lock states in the sqlite3_file structure, but all locks SHARED or
2335 ** above are really EXCLUSIVE locks and exclude all other processes from
2336 ** access the file.
2337 **
2338 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
2339 ** routine to lower a locking level.
2340 */
flockLock(sqlite3_file * id,int eFileLock)2341 static int flockLock(sqlite3_file *id, int eFileLock) {
2342   int rc = SQLITE_OK;
2343   unixFile *pFile = (unixFile*)id;
2344 
2345   assert( pFile );
2346 
2347   /* if we already have a lock, it is exclusive.
2348   ** Just adjust level and punt on outta here. */
2349   if (pFile->eFileLock > NO_LOCK) {
2350     pFile->eFileLock = eFileLock;
2351     return SQLITE_OK;
2352   }
2353 
2354   /* grab an exclusive lock */
2355 
2356   if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
2357     int tErrno = errno;
2358     /* didn't get, must be busy */
2359     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2360     if( IS_LOCK_ERROR(rc) ){
2361       storeLastErrno(pFile, tErrno);
2362     }
2363   } else {
2364     /* got it, set the type and return ok */
2365     pFile->eFileLock = eFileLock;
2366   }
2367   OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
2368            rc==SQLITE_OK ? "ok" : "failed"));
2369 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2370   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
2371     rc = SQLITE_BUSY;
2372   }
2373 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
2374   return rc;
2375 }
2376 
2377 
2378 /*
2379 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
2380 ** must be either NO_LOCK or SHARED_LOCK.
2381 **
2382 ** If the locking level of the file descriptor is already at or below
2383 ** the requested locking level, this routine is a no-op.
2384 */
flockUnlock(sqlite3_file * id,int eFileLock)2385 static int flockUnlock(sqlite3_file *id, int eFileLock) {
2386   unixFile *pFile = (unixFile*)id;
2387 
2388   assert( pFile );
2389   OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
2390            pFile->eFileLock, osGetpid(0)));
2391   assert( eFileLock<=SHARED_LOCK );
2392 
2393   /* no-op if possible */
2394   if( pFile->eFileLock==eFileLock ){
2395     return SQLITE_OK;
2396   }
2397 
2398   /* shared can just be set because we always have an exclusive */
2399   if (eFileLock==SHARED_LOCK) {
2400     pFile->eFileLock = eFileLock;
2401     return SQLITE_OK;
2402   }
2403 
2404   /* no, really, unlock. */
2405   if( robust_flock(pFile->h, LOCK_UN) ){
2406 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2407     return SQLITE_OK;
2408 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
2409     return SQLITE_IOERR_UNLOCK;
2410   }else{
2411     pFile->eFileLock = NO_LOCK;
2412     return SQLITE_OK;
2413   }
2414 }
2415 
2416 /*
2417 ** Close a file.
2418 */
flockClose(sqlite3_file * id)2419 static int flockClose(sqlite3_file *id) {
2420   assert( id!=0 );
2421   flockUnlock(id, NO_LOCK);
2422   return closeUnixFile(id);
2423 }
2424 
2425 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
2426 
2427 /******************* End of the flock lock implementation *********************
2428 ******************************************************************************/
2429 
2430 /******************************************************************************
2431 ************************ Begin Named Semaphore Locking ************************
2432 **
2433 ** Named semaphore locking is only supported on VxWorks.
2434 **
2435 ** Semaphore locking is like dot-lock and flock in that it really only
2436 ** supports EXCLUSIVE locking.  Only a single process can read or write
2437 ** the database file at a time.  This reduces potential concurrency, but
2438 ** makes the lock implementation much easier.
2439 */
2440 #if OS_VXWORKS
2441 
2442 /*
2443 ** This routine checks if there is a RESERVED lock held on the specified
2444 ** file by this or any other process. If such a lock is held, set *pResOut
2445 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
2446 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2447 */
semXCheckReservedLock(sqlite3_file * id,int * pResOut)2448 static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
2449   int rc = SQLITE_OK;
2450   int reserved = 0;
2451   unixFile *pFile = (unixFile*)id;
2452 
2453   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2454 
2455   assert( pFile );
2456 
2457   /* Check if a thread in this process holds such a lock */
2458   if( pFile->eFileLock>SHARED_LOCK ){
2459     reserved = 1;
2460   }
2461 
2462   /* Otherwise see if some other process holds it. */
2463   if( !reserved ){
2464     sem_t *pSem = pFile->pInode->pSem;
2465 
2466     if( sem_trywait(pSem)==-1 ){
2467       int tErrno = errno;
2468       if( EAGAIN != tErrno ){
2469         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2470         storeLastErrno(pFile, tErrno);
2471       } else {
2472         /* someone else has the lock when we are in NO_LOCK */
2473         reserved = (pFile->eFileLock < SHARED_LOCK);
2474       }
2475     }else{
2476       /* we could have it if we want it */
2477       sem_post(pSem);
2478     }
2479   }
2480   OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
2481 
2482   *pResOut = reserved;
2483   return rc;
2484 }
2485 
2486 /*
2487 ** Lock the file with the lock specified by parameter eFileLock - one
2488 ** of the following:
2489 **
2490 **     (1) SHARED_LOCK
2491 **     (2) RESERVED_LOCK
2492 **     (3) PENDING_LOCK
2493 **     (4) EXCLUSIVE_LOCK
2494 **
2495 ** Sometimes when requesting one lock state, additional lock states
2496 ** are inserted in between.  The locking might fail on one of the later
2497 ** transitions leaving the lock state different from what it started but
2498 ** still short of its goal.  The following chart shows the allowed
2499 ** transitions and the inserted intermediate states:
2500 **
2501 **    UNLOCKED -> SHARED
2502 **    SHARED -> RESERVED
2503 **    SHARED -> (PENDING) -> EXCLUSIVE
2504 **    RESERVED -> (PENDING) -> EXCLUSIVE
2505 **    PENDING -> EXCLUSIVE
2506 **
2507 ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
2508 ** lock states in the sqlite3_file structure, but all locks SHARED or
2509 ** above are really EXCLUSIVE locks and exclude all other processes from
2510 ** access the file.
2511 **
2512 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
2513 ** routine to lower a locking level.
2514 */
semXLock(sqlite3_file * id,int eFileLock)2515 static int semXLock(sqlite3_file *id, int eFileLock) {
2516   unixFile *pFile = (unixFile*)id;
2517   sem_t *pSem = pFile->pInode->pSem;
2518   int rc = SQLITE_OK;
2519 
2520   /* if we already have a lock, it is exclusive.
2521   ** Just adjust level and punt on outta here. */
2522   if (pFile->eFileLock > NO_LOCK) {
2523     pFile->eFileLock = eFileLock;
2524     rc = SQLITE_OK;
2525     goto sem_end_lock;
2526   }
2527 
2528   /* lock semaphore now but bail out when already locked. */
2529   if( sem_trywait(pSem)==-1 ){
2530     rc = SQLITE_BUSY;
2531     goto sem_end_lock;
2532   }
2533 
2534   /* got it, set the type and return ok */
2535   pFile->eFileLock = eFileLock;
2536 
2537  sem_end_lock:
2538   return rc;
2539 }
2540 
2541 /*
2542 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
2543 ** must be either NO_LOCK or SHARED_LOCK.
2544 **
2545 ** If the locking level of the file descriptor is already at or below
2546 ** the requested locking level, this routine is a no-op.
2547 */
semXUnlock(sqlite3_file * id,int eFileLock)2548 static int semXUnlock(sqlite3_file *id, int eFileLock) {
2549   unixFile *pFile = (unixFile*)id;
2550   sem_t *pSem = pFile->pInode->pSem;
2551 
2552   assert( pFile );
2553   assert( pSem );
2554   OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
2555            pFile->eFileLock, osGetpid(0)));
2556   assert( eFileLock<=SHARED_LOCK );
2557 
2558   /* no-op if possible */
2559   if( pFile->eFileLock==eFileLock ){
2560     return SQLITE_OK;
2561   }
2562 
2563   /* shared can just be set because we always have an exclusive */
2564   if (eFileLock==SHARED_LOCK) {
2565     pFile->eFileLock = eFileLock;
2566     return SQLITE_OK;
2567   }
2568 
2569   /* no, really unlock. */
2570   if ( sem_post(pSem)==-1 ) {
2571     int rc, tErrno = errno;
2572     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2573     if( IS_LOCK_ERROR(rc) ){
2574       storeLastErrno(pFile, tErrno);
2575     }
2576     return rc;
2577   }
2578   pFile->eFileLock = NO_LOCK;
2579   return SQLITE_OK;
2580 }
2581 
2582 /*
2583  ** Close a file.
2584  */
semXClose(sqlite3_file * id)2585 static int semXClose(sqlite3_file *id) {
2586   if( id ){
2587     unixFile *pFile = (unixFile*)id;
2588     semXUnlock(id, NO_LOCK);
2589     assert( pFile );
2590     unixEnterMutex();
2591     releaseInodeInfo(pFile);
2592     unixLeaveMutex();
2593     closeUnixFile(id);
2594   }
2595   return SQLITE_OK;
2596 }
2597 
2598 #endif /* OS_VXWORKS */
2599 /*
2600 ** Named semaphore locking is only available on VxWorks.
2601 **
2602 *************** End of the named semaphore lock implementation ****************
2603 ******************************************************************************/
2604 
2605 
2606 /******************************************************************************
2607 *************************** Begin AFP Locking *********************************
2608 **
2609 ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
2610 ** on Apple Macintosh computers - both OS9 and OSX.
2611 **
2612 ** Third-party implementations of AFP are available.  But this code here
2613 ** only works on OSX.
2614 */
2615 
2616 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2617 /*
2618 ** The afpLockingContext structure contains all afp lock specific state
2619 */
2620 typedef struct afpLockingContext afpLockingContext;
2621 struct afpLockingContext {
2622   int reserved;
2623   const char *dbPath;             /* Name of the open file */
2624 };
2625 
2626 struct ByteRangeLockPB2
2627 {
2628   unsigned long long offset;        /* offset to first byte to lock */
2629   unsigned long long length;        /* nbr of bytes to lock */
2630   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
2631   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
2632   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
2633   int fd;                           /* file desc to assoc this lock with */
2634 };
2635 
2636 #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
2637 
2638 /*
2639 ** This is a utility for setting or clearing a bit-range lock on an
2640 ** AFP filesystem.
2641 **
2642 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
2643 */
afpSetLock(const char * path,unixFile * pFile,unsigned long long offset,unsigned long long length,int setLockFlag)2644 static int afpSetLock(
2645   const char *path,              /* Name of the file to be locked or unlocked */
2646   unixFile *pFile,               /* Open file descriptor on path */
2647   unsigned long long offset,     /* First byte to be locked */
2648   unsigned long long length,     /* Number of bytes to lock */
2649   int setLockFlag                /* True to set lock.  False to clear lock */
2650 ){
2651   struct ByteRangeLockPB2 pb;
2652   int err;
2653 
2654   pb.unLockFlag = setLockFlag ? 0 : 1;
2655   pb.startEndFlag = 0;
2656   pb.offset = offset;
2657   pb.length = length;
2658   pb.fd = pFile->h;
2659 
2660   OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
2661     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
2662     offset, length));
2663   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
2664   if ( err==-1 ) {
2665     int rc;
2666     int tErrno = errno;
2667     OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
2668              path, tErrno, strerror(tErrno)));
2669 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
2670     rc = SQLITE_BUSY;
2671 #else
2672     rc = sqliteErrorFromPosixError(tErrno,
2673                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
2674 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
2675     if( IS_LOCK_ERROR(rc) ){
2676       storeLastErrno(pFile, tErrno);
2677     }
2678     return rc;
2679   } else {
2680     return SQLITE_OK;
2681   }
2682 }
2683 
2684 /*
2685 ** This routine checks if there is a RESERVED lock held on the specified
2686 ** file by this or any other process. If such a lock is held, set *pResOut
2687 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
2688 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2689 */
afpCheckReservedLock(sqlite3_file * id,int * pResOut)2690 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
2691   int rc = SQLITE_OK;
2692   int reserved = 0;
2693   unixFile *pFile = (unixFile*)id;
2694   afpLockingContext *context;
2695 
2696   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2697 
2698   assert( pFile );
2699   context = (afpLockingContext *) pFile->lockingContext;
2700   if( context->reserved ){
2701     *pResOut = 1;
2702     return SQLITE_OK;
2703   }
2704   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
2705 
2706   /* Check if a thread in this process holds such a lock */
2707   if( pFile->pInode->eFileLock>SHARED_LOCK ){
2708     reserved = 1;
2709   }
2710 
2711   /* Otherwise see if some other process holds it.
2712    */
2713   if( !reserved ){
2714     /* lock the RESERVED byte */
2715     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
2716     if( SQLITE_OK==lrc ){
2717       /* if we succeeded in taking the reserved lock, unlock it to restore
2718       ** the original state */
2719       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
2720     } else {
2721       /* if we failed to get the lock then someone else must have it */
2722       reserved = 1;
2723     }
2724     if( IS_LOCK_ERROR(lrc) ){
2725       rc=lrc;
2726     }
2727   }
2728 
2729   unixLeaveMutex();
2730   OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
2731 
2732   *pResOut = reserved;
2733   return rc;
2734 }
2735 
2736 /*
2737 ** Lock the file with the lock specified by parameter eFileLock - one
2738 ** of the following:
2739 **
2740 **     (1) SHARED_LOCK
2741 **     (2) RESERVED_LOCK
2742 **     (3) PENDING_LOCK
2743 **     (4) EXCLUSIVE_LOCK
2744 **
2745 ** Sometimes when requesting one lock state, additional lock states
2746 ** are inserted in between.  The locking might fail on one of the later
2747 ** transitions leaving the lock state different from what it started but
2748 ** still short of its goal.  The following chart shows the allowed
2749 ** transitions and the inserted intermediate states:
2750 **
2751 **    UNLOCKED -> SHARED
2752 **    SHARED -> RESERVED
2753 **    SHARED -> (PENDING) -> EXCLUSIVE
2754 **    RESERVED -> (PENDING) -> EXCLUSIVE
2755 **    PENDING -> EXCLUSIVE
2756 **
2757 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
2758 ** routine to lower a locking level.
2759 */
afpLock(sqlite3_file * id,int eFileLock)2760 static int afpLock(sqlite3_file *id, int eFileLock){
2761   int rc = SQLITE_OK;
2762   unixFile *pFile = (unixFile*)id;
2763   unixInodeInfo *pInode = pFile->pInode;
2764   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2765 
2766   assert( pFile );
2767   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
2768            azFileLock(eFileLock), azFileLock(pFile->eFileLock),
2769            azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
2770 
2771   /* If there is already a lock of this type or more restrictive on the
2772   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
2773   ** unixEnterMutex() hasn't been called yet.
2774   */
2775   if( pFile->eFileLock>=eFileLock ){
2776     OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
2777            azFileLock(eFileLock)));
2778     return SQLITE_OK;
2779   }
2780 
2781   /* Make sure the locking sequence is correct
2782   **  (1) We never move from unlocked to anything higher than shared lock.
2783   **  (2) SQLite never explicitly requests a pendig lock.
2784   **  (3) A shared lock is always held when a reserve lock is requested.
2785   */
2786   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
2787   assert( eFileLock!=PENDING_LOCK );
2788   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
2789 
2790   /* This mutex is needed because pFile->pInode is shared across threads
2791   */
2792   unixEnterMutex();
2793   pInode = pFile->pInode;
2794 
2795   /* If some thread using this PID has a lock via a different unixFile*
2796   ** handle that precludes the requested lock, return BUSY.
2797   */
2798   if( (pFile->eFileLock!=pInode->eFileLock &&
2799        (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
2800      ){
2801     rc = SQLITE_BUSY;
2802     goto afp_end_lock;
2803   }
2804 
2805   /* If a SHARED lock is requested, and some thread using this PID already
2806   ** has a SHARED or RESERVED lock, then increment reference counts and
2807   ** return SQLITE_OK.
2808   */
2809   if( eFileLock==SHARED_LOCK &&
2810      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
2811     assert( eFileLock==SHARED_LOCK );
2812     assert( pFile->eFileLock==0 );
2813     assert( pInode->nShared>0 );
2814     pFile->eFileLock = SHARED_LOCK;
2815     pInode->nShared++;
2816     pInode->nLock++;
2817     goto afp_end_lock;
2818   }
2819 
2820   /* A PENDING lock is needed before acquiring a SHARED lock and before
2821   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
2822   ** be released.
2823   */
2824   if( eFileLock==SHARED_LOCK
2825       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
2826   ){
2827     int failed;
2828     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
2829     if (failed) {
2830       rc = failed;
2831       goto afp_end_lock;
2832     }
2833   }
2834 
2835   /* If control gets to this point, then actually go ahead and make
2836   ** operating system calls for the specified lock.
2837   */
2838   if( eFileLock==SHARED_LOCK ){
2839     int lrc1, lrc2, lrc1Errno = 0;
2840     long lk, mask;
2841 
2842     assert( pInode->nShared==0 );
2843     assert( pInode->eFileLock==0 );
2844 
2845     mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
2846     /* Now get the read-lock SHARED_LOCK */
2847     /* note that the quality of the randomness doesn't matter that much */
2848     lk = random();
2849     pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
2850     lrc1 = afpSetLock(context->dbPath, pFile,
2851           SHARED_FIRST+pInode->sharedByte, 1, 1);
2852     if( IS_LOCK_ERROR(lrc1) ){
2853       lrc1Errno = pFile->lastErrno;
2854     }
2855     /* Drop the temporary PENDING lock */
2856     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
2857 
2858     if( IS_LOCK_ERROR(lrc1) ) {
2859       storeLastErrno(pFile, lrc1Errno);
2860       rc = lrc1;
2861       goto afp_end_lock;
2862     } else if( IS_LOCK_ERROR(lrc2) ){
2863       rc = lrc2;
2864       goto afp_end_lock;
2865     } else if( lrc1 != SQLITE_OK ) {
2866       rc = lrc1;
2867     } else {
2868       pFile->eFileLock = SHARED_LOCK;
2869       pInode->nLock++;
2870       pInode->nShared = 1;
2871     }
2872   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
2873     /* We are trying for an exclusive lock but another thread in this
2874      ** same process is still holding a shared lock. */
2875     rc = SQLITE_BUSY;
2876   }else{
2877     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
2878     ** assumed that there is a SHARED or greater lock on the file
2879     ** already.
2880     */
2881     int failed = 0;
2882     assert( 0!=pFile->eFileLock );
2883     if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
2884         /* Acquire a RESERVED lock */
2885         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
2886       if( !failed ){
2887         context->reserved = 1;
2888       }
2889     }
2890     if (!failed && eFileLock == EXCLUSIVE_LOCK) {
2891       /* Acquire an EXCLUSIVE lock */
2892 
2893       /* Remove the shared lock before trying the range.  we'll need to
2894       ** reestablish the shared lock if we can't get the  afpUnlock
2895       */
2896       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
2897                          pInode->sharedByte, 1, 0)) ){
2898         int failed2 = SQLITE_OK;
2899         /* now attemmpt to get the exclusive lock range */
2900         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
2901                                SHARED_SIZE, 1);
2902         if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
2903                        SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
2904           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
2905           ** a critical I/O error
2906           */
2907           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
2908                SQLITE_IOERR_LOCK;
2909           goto afp_end_lock;
2910         }
2911       }else{
2912         rc = failed;
2913       }
2914     }
2915     if( failed ){
2916       rc = failed;
2917     }
2918   }
2919 
2920   if( rc==SQLITE_OK ){
2921     pFile->eFileLock = eFileLock;
2922     pInode->eFileLock = eFileLock;
2923   }else if( eFileLock==EXCLUSIVE_LOCK ){
2924     pFile->eFileLock = PENDING_LOCK;
2925     pInode->eFileLock = PENDING_LOCK;
2926   }
2927 
2928 afp_end_lock:
2929   unixLeaveMutex();
2930   OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
2931          rc==SQLITE_OK ? "ok" : "failed"));
2932   return rc;
2933 }
2934 
2935 /*
2936 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
2937 ** must be either NO_LOCK or SHARED_LOCK.
2938 **
2939 ** If the locking level of the file descriptor is already at or below
2940 ** the requested locking level, this routine is a no-op.
2941 */
afpUnlock(sqlite3_file * id,int eFileLock)2942 static int afpUnlock(sqlite3_file *id, int eFileLock) {
2943   int rc = SQLITE_OK;
2944   unixFile *pFile = (unixFile*)id;
2945   unixInodeInfo *pInode;
2946   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2947   int skipShared = 0;
2948 #ifdef SQLITE_TEST
2949   int h = pFile->h;
2950 #endif
2951 
2952   assert( pFile );
2953   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
2954            pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
2955            osGetpid(0)));
2956 
2957   assert( eFileLock<=SHARED_LOCK );
2958   if( pFile->eFileLock<=eFileLock ){
2959     return SQLITE_OK;
2960   }
2961   unixEnterMutex();
2962   pInode = pFile->pInode;
2963   assert( pInode->nShared!=0 );
2964   if( pFile->eFileLock>SHARED_LOCK ){
2965     assert( pInode->eFileLock==pFile->eFileLock );
2966     SimulateIOErrorBenign(1);
2967     SimulateIOError( h=(-1) )
2968     SimulateIOErrorBenign(0);
2969 
2970 #ifdef SQLITE_DEBUG
2971     /* When reducing a lock such that other processes can start
2972     ** reading the database file again, make sure that the
2973     ** transaction counter was updated if any part of the database
2974     ** file changed.  If the transaction counter is not updated,
2975     ** other connections to the same file might not realize that
2976     ** the file has changed and hence might not know to flush their
2977     ** cache.  The use of a stale cache can lead to database corruption.
2978     */
2979     assert( pFile->inNormalWrite==0
2980            || pFile->dbUpdate==0
2981            || pFile->transCntrChng==1 );
2982     pFile->inNormalWrite = 0;
2983 #endif
2984 
2985     if( pFile->eFileLock==EXCLUSIVE_LOCK ){
2986       rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
2987       if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
2988         /* only re-establish the shared lock if necessary */
2989         int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
2990         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
2991       } else {
2992         skipShared = 1;
2993       }
2994     }
2995     if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
2996       rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
2997     }
2998     if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
2999       rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
3000       if( !rc ){
3001         context->reserved = 0;
3002       }
3003     }
3004     if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
3005       pInode->eFileLock = SHARED_LOCK;
3006     }
3007   }
3008   if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
3009 
3010     /* Decrement the shared lock counter.  Release the lock using an
3011     ** OS call only when all threads in this same process have released
3012     ** the lock.
3013     */
3014     unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
3015     pInode->nShared--;
3016     if( pInode->nShared==0 ){
3017       SimulateIOErrorBenign(1);
3018       SimulateIOError( h=(-1) )
3019       SimulateIOErrorBenign(0);
3020       if( !skipShared ){
3021         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
3022       }
3023       if( !rc ){
3024         pInode->eFileLock = NO_LOCK;
3025         pFile->eFileLock = NO_LOCK;
3026       }
3027     }
3028     if( rc==SQLITE_OK ){
3029       pInode->nLock--;
3030       assert( pInode->nLock>=0 );
3031       if( pInode->nLock==0 ){
3032         closePendingFds(pFile);
3033       }
3034     }
3035   }
3036 
3037   unixLeaveMutex();
3038   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
3039   return rc;
3040 }
3041 
3042 /*
3043 ** Close a file & cleanup AFP specific locking context
3044 */
afpClose(sqlite3_file * id)3045 static int afpClose(sqlite3_file *id) {
3046   int rc = SQLITE_OK;
3047   unixFile *pFile = (unixFile*)id;
3048   assert( id!=0 );
3049   afpUnlock(id, NO_LOCK);
3050   unixEnterMutex();
3051   if( pFile->pInode && pFile->pInode->nLock ){
3052     /* If there are outstanding locks, do not actually close the file just
3053     ** yet because that would clear those locks.  Instead, add the file
3054     ** descriptor to pInode->aPending.  It will be automatically closed when
3055     ** the last lock is cleared.
3056     */
3057     setPendingFd(pFile);
3058   }
3059   releaseInodeInfo(pFile);
3060   sqlite3_free(pFile->lockingContext);
3061   rc = closeUnixFile(id);
3062   unixLeaveMutex();
3063   return rc;
3064 }
3065 
3066 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
3067 /*
3068 ** The code above is the AFP lock implementation.  The code is specific
3069 ** to MacOSX and does not work on other unix platforms.  No alternative
3070 ** is available.  If you don't compile for a mac, then the "unix-afp"
3071 ** VFS is not available.
3072 **
3073 ********************* End of the AFP lock implementation **********************
3074 ******************************************************************************/
3075 
3076 /******************************************************************************
3077 *************************** Begin NFS Locking ********************************/
3078 
3079 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
3080 /*
3081  ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
3082  ** must be either NO_LOCK or SHARED_LOCK.
3083  **
3084  ** If the locking level of the file descriptor is already at or below
3085  ** the requested locking level, this routine is a no-op.
3086  */
nfsUnlock(sqlite3_file * id,int eFileLock)3087 static int nfsUnlock(sqlite3_file *id, int eFileLock){
3088   return posixUnlock(id, eFileLock, 1);
3089 }
3090 
3091 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
3092 /*
3093 ** The code above is the NFS lock implementation.  The code is specific
3094 ** to MacOSX and does not work on other unix platforms.  No alternative
3095 ** is available.
3096 **
3097 ********************* End of the NFS lock implementation **********************
3098 ******************************************************************************/
3099 
3100 /******************************************************************************
3101 **************** Non-locking sqlite3_file methods *****************************
3102 **
3103 ** The next division contains implementations for all methods of the
3104 ** sqlite3_file object other than the locking methods.  The locking
3105 ** methods were defined in divisions above (one locking method per
3106 ** division).  Those methods that are common to all locking modes
3107 ** are gather together into this division.
3108 */
3109 
3110 /*
3111 ** Seek to the offset passed as the second argument, then read cnt
3112 ** bytes into pBuf. Return the number of bytes actually read.
3113 **
3114 ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
3115 ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
3116 ** one system to another.  Since SQLite does not define USE_PREAD
3117 ** in any form by default, we will not attempt to define _XOPEN_SOURCE.
3118 ** See tickets #2741 and #2681.
3119 **
3120 ** To avoid stomping the errno value on a failed read the lastErrno value
3121 ** is set before returning.
3122 */
seekAndRead(unixFile * id,sqlite3_int64 offset,void * pBuf,int cnt)3123 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
3124   int got;
3125   int prior = 0;
3126 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
3127   i64 newOffset;
3128 #endif
3129   TIMER_START;
3130   assert( cnt==(cnt&0x1ffff) );
3131   assert( id->h>2 );
3132   do{
3133 #if defined(USE_PREAD)
3134     got = osPread(id->h, pBuf, cnt, offset);
3135     SimulateIOError( got = -1 );
3136 #elif defined(USE_PREAD64)
3137     got = osPread64(id->h, pBuf, cnt, offset);
3138     SimulateIOError( got = -1 );
3139 #else
3140     newOffset = lseek(id->h, offset, SEEK_SET);
3141     SimulateIOError( newOffset = -1 );
3142     if( newOffset<0 ){
3143       storeLastErrno((unixFile*)id, errno);
3144       return -1;
3145     }
3146     got = osRead(id->h, pBuf, cnt);
3147 #endif
3148     if( got==cnt ) break;
3149     if( got<0 ){
3150       if( errno==EINTR ){ got = 1; continue; }
3151       prior = 0;
3152       storeLastErrno((unixFile*)id,  errno);
3153       break;
3154     }else if( got>0 ){
3155       cnt -= got;
3156       offset += got;
3157       prior += got;
3158       pBuf = (void*)(got + (char*)pBuf);
3159     }
3160   }while( got>0 );
3161   TIMER_END;
3162   OSTRACE(("READ    %-3d %5d %7lld %llu\n",
3163             id->h, got+prior, offset-prior, TIMER_ELAPSED));
3164   return got+prior;
3165 }
3166 
3167 /*
3168 ** Read data from a file into a buffer.  Return SQLITE_OK if all
3169 ** bytes were read successfully and SQLITE_IOERR if anything goes
3170 ** wrong.
3171 */
unixRead(sqlite3_file * id,void * pBuf,int amt,sqlite3_int64 offset)3172 static int unixRead(
3173   sqlite3_file *id,
3174   void *pBuf,
3175   int amt,
3176   sqlite3_int64 offset
3177 ){
3178   unixFile *pFile = (unixFile *)id;
3179   int got;
3180   assert( id );
3181   assert( offset>=0 );
3182   assert( amt>0 );
3183 
3184   /* If this is a database file (not a journal, master-journal or temp
3185   ** file), the bytes in the locking range should never be read or written. */
3186 #if 0
3187   assert( pFile->pUnused==0
3188        || offset>=PENDING_BYTE+512
3189        || offset+amt<=PENDING_BYTE
3190   );
3191 #endif
3192 
3193 #if SQLITE_MAX_MMAP_SIZE>0
3194   /* Deal with as much of this read request as possible by transfering
3195   ** data from the memory mapping using memcpy().  */
3196   if( offset<pFile->mmapSize ){
3197     if( offset+amt <= pFile->mmapSize ){
3198       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
3199       return SQLITE_OK;
3200     }else{
3201       int nCopy = pFile->mmapSize - offset;
3202       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
3203       pBuf = &((u8 *)pBuf)[nCopy];
3204       amt -= nCopy;
3205       offset += nCopy;
3206     }
3207   }
3208 #endif
3209 
3210   got = seekAndRead(pFile, offset, pBuf, amt);
3211   if( got==amt ){
3212     return SQLITE_OK;
3213   }else if( got<0 ){
3214     /* lastErrno set by seekAndRead */
3215     return SQLITE_IOERR_READ;
3216   }else{
3217     storeLastErrno(pFile, 0);   /* not a system error */
3218     /* Unread parts of the buffer must be zero-filled */
3219     memset(&((char*)pBuf)[got], 0, amt-got);
3220     return SQLITE_IOERR_SHORT_READ;
3221   }
3222 }
3223 
3224 /*
3225 ** Attempt to seek the file-descriptor passed as the first argument to
3226 ** absolute offset iOff, then attempt to write nBuf bytes of data from
3227 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
3228 ** return the actual number of bytes written (which may be less than
3229 ** nBuf).
3230 */
seekAndWriteFd(int fd,i64 iOff,const void * pBuf,int nBuf,int * piErrno)3231 static int seekAndWriteFd(
3232   int fd,                         /* File descriptor to write to */
3233   i64 iOff,                       /* File offset to begin writing at */
3234   const void *pBuf,               /* Copy data from this buffer to the file */
3235   int nBuf,                       /* Size of buffer pBuf in bytes */
3236   int *piErrno                    /* OUT: Error number if error occurs */
3237 ){
3238   int rc = 0;                     /* Value returned by system call */
3239 
3240   assert( nBuf==(nBuf&0x1ffff) );
3241   assert( fd>2 );
3242   assert( piErrno!=0 );
3243   nBuf &= 0x1ffff;
3244   TIMER_START;
3245 
3246 #if defined(USE_PREAD)
3247   do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
3248 #elif defined(USE_PREAD64)
3249   do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
3250 #else
3251   do{
3252     i64 iSeek = lseek(fd, iOff, SEEK_SET);
3253     SimulateIOError( iSeek = -1 );
3254     if( iSeek<0 ){
3255       rc = -1;
3256       break;
3257     }
3258     rc = osWrite(fd, pBuf, nBuf);
3259   }while( rc<0 && errno==EINTR );
3260 #endif
3261 
3262   TIMER_END;
3263   OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
3264 
3265   if( rc<0 ) *piErrno = errno;
3266   return rc;
3267 }
3268 
3269 
3270 /*
3271 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
3272 ** Return the number of bytes actually read.  Update the offset.
3273 **
3274 ** To avoid stomping the errno value on a failed write the lastErrno value
3275 ** is set before returning.
3276 */
seekAndWrite(unixFile * id,i64 offset,const void * pBuf,int cnt)3277 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
3278   return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
3279 }
3280 
3281 
3282 /*
3283 ** Write data from a buffer into a file.  Return SQLITE_OK on success
3284 ** or some other error code on failure.
3285 */
unixWrite(sqlite3_file * id,const void * pBuf,int amt,sqlite3_int64 offset)3286 static int unixWrite(
3287   sqlite3_file *id,
3288   const void *pBuf,
3289   int amt,
3290   sqlite3_int64 offset
3291 ){
3292   unixFile *pFile = (unixFile*)id;
3293   int wrote = 0;
3294   assert( id );
3295   assert( amt>0 );
3296 
3297   /* If this is a database file (not a journal, master-journal or temp
3298   ** file), the bytes in the locking range should never be read or written. */
3299 #if 0
3300   assert( pFile->pUnused==0
3301        || offset>=PENDING_BYTE+512
3302        || offset+amt<=PENDING_BYTE
3303   );
3304 #endif
3305 
3306 #ifdef SQLITE_DEBUG
3307   /* If we are doing a normal write to a database file (as opposed to
3308   ** doing a hot-journal rollback or a write to some file other than a
3309   ** normal database file) then record the fact that the database
3310   ** has changed.  If the transaction counter is modified, record that
3311   ** fact too.
3312   */
3313   if( pFile->inNormalWrite ){
3314     pFile->dbUpdate = 1;  /* The database has been modified */
3315     if( offset<=24 && offset+amt>=27 ){
3316       int rc;
3317       char oldCntr[4];
3318       SimulateIOErrorBenign(1);
3319       rc = seekAndRead(pFile, 24, oldCntr, 4);
3320       SimulateIOErrorBenign(0);
3321       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
3322         pFile->transCntrChng = 1;  /* The transaction counter has changed */
3323       }
3324     }
3325   }
3326 #endif
3327 
3328 #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
3329   /* Deal with as much of this write request as possible by transfering
3330   ** data from the memory mapping using memcpy().  */
3331   if( offset<pFile->mmapSize ){
3332     if( offset+amt <= pFile->mmapSize ){
3333       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
3334       return SQLITE_OK;
3335     }else{
3336       int nCopy = pFile->mmapSize - offset;
3337       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
3338       pBuf = &((u8 *)pBuf)[nCopy];
3339       amt -= nCopy;
3340       offset += nCopy;
3341     }
3342   }
3343 #endif
3344 
3345   while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
3346     amt -= wrote;
3347     offset += wrote;
3348     pBuf = &((char*)pBuf)[wrote];
3349   }
3350   SimulateIOError(( wrote=(-1), amt=1 ));
3351   SimulateDiskfullError(( wrote=0, amt=1 ));
3352 
3353   if( amt>wrote ){
3354     if( wrote<0 && pFile->lastErrno!=ENOSPC ){
3355       /* lastErrno set by seekAndWrite */
3356       return SQLITE_IOERR_WRITE;
3357     }else{
3358       storeLastErrno(pFile, 0); /* not a system error */
3359       return SQLITE_FULL;
3360     }
3361   }
3362 
3363   return SQLITE_OK;
3364 }
3365 
3366 #ifdef SQLITE_TEST
3367 /*
3368 ** Count the number of fullsyncs and normal syncs.  This is used to test
3369 ** that syncs and fullsyncs are occurring at the right times.
3370 */
3371 int sqlite3_sync_count = 0;
3372 int sqlite3_fullsync_count = 0;
3373 #endif
3374 
3375 /*
3376 ** We do not trust systems to provide a working fdatasync().  Some do.
3377 ** Others do no.  To be safe, we will stick with the (slightly slower)
3378 ** fsync(). If you know that your system does support fdatasync() correctly,
3379 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
3380 */
3381 #if !defined(fdatasync) && !HAVE_FDATASYNC
3382 # define fdatasync fsync
3383 #endif
3384 
3385 /*
3386 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
3387 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
3388 ** only available on Mac OS X.  But that could change.
3389 */
3390 #ifdef F_FULLFSYNC
3391 # define HAVE_FULLFSYNC 1
3392 #else
3393 # define HAVE_FULLFSYNC 0
3394 #endif
3395 
3396 
3397 /*
3398 ** The fsync() system call does not work as advertised on many
3399 ** unix systems.  The following procedure is an attempt to make
3400 ** it work better.
3401 **
3402 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
3403 ** for testing when we want to run through the test suite quickly.
3404 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
3405 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
3406 ** or power failure will likely corrupt the database file.
3407 **
3408 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
3409 ** The idea behind dataOnly is that it should only write the file content
3410 ** to disk, not the inode.  We only set dataOnly if the file size is
3411 ** unchanged since the file size is part of the inode.  However,
3412 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
3413 ** file size has changed.  The only real difference between fdatasync()
3414 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
3415 ** inode if the mtime or owner or other inode attributes have changed.
3416 ** We only care about the file size, not the other file attributes, so
3417 ** as far as SQLite is concerned, an fdatasync() is always adequate.
3418 ** So, we always use fdatasync() if it is available, regardless of
3419 ** the value of the dataOnly flag.
3420 */
full_fsync(int fd,int fullSync,int dataOnly)3421 static int full_fsync(int fd, int fullSync, int dataOnly){
3422   int rc;
3423 
3424   /* The following "ifdef/elif/else/" block has the same structure as
3425   ** the one below. It is replicated here solely to avoid cluttering
3426   ** up the real code with the UNUSED_PARAMETER() macros.
3427   */
3428 #ifdef SQLITE_NO_SYNC
3429   UNUSED_PARAMETER(fd);
3430   UNUSED_PARAMETER(fullSync);
3431   UNUSED_PARAMETER(dataOnly);
3432 #elif HAVE_FULLFSYNC
3433   UNUSED_PARAMETER(dataOnly);
3434 #else
3435   UNUSED_PARAMETER(fullSync);
3436   UNUSED_PARAMETER(dataOnly);
3437 #endif
3438 
3439   /* Record the number of times that we do a normal fsync() and
3440   ** FULLSYNC.  This is used during testing to verify that this procedure
3441   ** gets called with the correct arguments.
3442   */
3443 #ifdef SQLITE_TEST
3444   if( fullSync ) sqlite3_fullsync_count++;
3445   sqlite3_sync_count++;
3446 #endif
3447 
3448   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
3449   ** no-op.  But go ahead and call fstat() to validate the file
3450   ** descriptor as we need a method to provoke a failure during
3451   ** coverate testing.
3452   */
3453 #ifdef SQLITE_NO_SYNC
3454   {
3455     struct stat buf;
3456     rc = osFstat(fd, &buf);
3457   }
3458 #elif HAVE_FULLFSYNC
3459   if( fullSync ){
3460     rc = osFcntl(fd, F_FULLFSYNC, 0);
3461   }else{
3462     rc = 1;
3463   }
3464   /* If the FULLFSYNC failed, fall back to attempting an fsync().
3465   ** It shouldn't be possible for fullfsync to fail on the local
3466   ** file system (on OSX), so failure indicates that FULLFSYNC
3467   ** isn't supported for this file system. So, attempt an fsync
3468   ** and (for now) ignore the overhead of a superfluous fcntl call.
3469   ** It'd be better to detect fullfsync support once and avoid
3470   ** the fcntl call every time sync is called.
3471   */
3472   if( rc ) rc = fsync(fd);
3473 
3474 #elif defined(__APPLE__)
3475   /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
3476   ** so currently we default to the macro that redefines fdatasync to fsync
3477   */
3478   rc = fsync(fd);
3479 #else
3480   rc = fdatasync(fd);
3481 #if OS_VXWORKS
3482   if( rc==-1 && errno==ENOTSUP ){
3483     rc = fsync(fd);
3484   }
3485 #endif /* OS_VXWORKS */
3486 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
3487 
3488   if( OS_VXWORKS && rc!= -1 ){
3489     rc = 0;
3490   }
3491   return rc;
3492 }
3493 
3494 /*
3495 ** Open a file descriptor to the directory containing file zFilename.
3496 ** If successful, *pFd is set to the opened file descriptor and
3497 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
3498 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
3499 ** value.
3500 **
3501 ** The directory file descriptor is used for only one thing - to
3502 ** fsync() a directory to make sure file creation and deletion events
3503 ** are flushed to disk.  Such fsyncs are not needed on newer
3504 ** journaling filesystems, but are required on older filesystems.
3505 **
3506 ** This routine can be overridden using the xSetSysCall interface.
3507 ** The ability to override this routine was added in support of the
3508 ** chromium sandbox.  Opening a directory is a security risk (we are
3509 ** told) so making it overrideable allows the chromium sandbox to
3510 ** replace this routine with a harmless no-op.  To make this routine
3511 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
3512 ** *pFd set to a negative number.
3513 **
3514 ** If SQLITE_OK is returned, the caller is responsible for closing
3515 ** the file descriptor *pFd using close().
3516 */
openDirectory(const char * zFilename,int * pFd)3517 static int openDirectory(const char *zFilename, int *pFd){
3518   int ii;
3519   int fd = -1;
3520   char zDirname[MAX_PATHNAME+1];
3521 
3522   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
3523   for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
3524   if( ii>0 ){
3525     zDirname[ii] = '\0';
3526   }else{
3527     if( zDirname[0]!='/' ) zDirname[0] = '.';
3528     zDirname[1] = 0;
3529   }
3530   fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
3531   if( fd>=0 ){
3532     OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
3533   }
3534   *pFd = fd;
3535   if( fd>=0 ) return SQLITE_OK;
3536   return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
3537 }
3538 
3539 /*
3540 ** Make sure all writes to a particular file are committed to disk.
3541 **
3542 ** If dataOnly==0 then both the file itself and its metadata (file
3543 ** size, access time, etc) are synced.  If dataOnly!=0 then only the
3544 ** file data is synced.
3545 **
3546 ** Under Unix, also make sure that the directory entry for the file
3547 ** has been created by fsync-ing the directory that contains the file.
3548 ** If we do not do this and we encounter a power failure, the directory
3549 ** entry for the journal might not exist after we reboot.  The next
3550 ** SQLite to access the file will not know that the journal exists (because
3551 ** the directory entry for the journal was never created) and the transaction
3552 ** will not roll back - possibly leading to database corruption.
3553 */
unixSync(sqlite3_file * id,int flags)3554 static int unixSync(sqlite3_file *id, int flags){
3555   int rc;
3556   unixFile *pFile = (unixFile*)id;
3557 
3558   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
3559   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
3560 
3561   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
3562   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
3563       || (flags&0x0F)==SQLITE_SYNC_FULL
3564   );
3565 
3566   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
3567   ** line is to test that doing so does not cause any problems.
3568   */
3569   SimulateDiskfullError( return SQLITE_FULL );
3570 
3571   assert( pFile );
3572   OSTRACE(("SYNC    %-3d\n", pFile->h));
3573   rc = full_fsync(pFile->h, isFullsync, isDataOnly);
3574   SimulateIOError( rc=1 );
3575   if( rc ){
3576     storeLastErrno(pFile, errno);
3577     return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
3578   }
3579 
3580   /* Also fsync the directory containing the file if the DIRSYNC flag
3581   ** is set.  This is a one-time occurrence.  Many systems (examples: AIX)
3582   ** are unable to fsync a directory, so ignore errors on the fsync.
3583   */
3584   if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
3585     int dirfd;
3586     OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
3587             HAVE_FULLFSYNC, isFullsync));
3588     rc = osOpenDirectory(pFile->zPath, &dirfd);
3589     if( rc==SQLITE_OK ){
3590       full_fsync(dirfd, 0, 0);
3591       robust_close(pFile, dirfd, __LINE__);
3592     }else{
3593       assert( rc==SQLITE_CANTOPEN );
3594       rc = SQLITE_OK;
3595     }
3596     pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
3597   }
3598   return rc;
3599 }
3600 
3601 /*
3602 ** Truncate an open file to a specified size
3603 */
unixTruncate(sqlite3_file * id,i64 nByte)3604 static int unixTruncate(sqlite3_file *id, i64 nByte){
3605   unixFile *pFile = (unixFile *)id;
3606   int rc;
3607   assert( pFile );
3608   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
3609 
3610   /* If the user has configured a chunk-size for this file, truncate the
3611   ** file so that it consists of an integer number of chunks (i.e. the
3612   ** actual file size after the operation may be larger than the requested
3613   ** size).
3614   */
3615   if( pFile->szChunk>0 ){
3616     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
3617   }
3618 
3619   rc = robust_ftruncate(pFile->h, nByte);
3620   if( rc ){
3621     storeLastErrno(pFile, errno);
3622     return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
3623   }else{
3624 #ifdef SQLITE_DEBUG
3625     /* If we are doing a normal write to a database file (as opposed to
3626     ** doing a hot-journal rollback or a write to some file other than a
3627     ** normal database file) and we truncate the file to zero length,
3628     ** that effectively updates the change counter.  This might happen
3629     ** when restoring a database using the backup API from a zero-length
3630     ** source.
3631     */
3632     if( pFile->inNormalWrite && nByte==0 ){
3633       pFile->transCntrChng = 1;
3634     }
3635 #endif
3636 
3637 #if SQLITE_MAX_MMAP_SIZE>0
3638     /* If the file was just truncated to a size smaller than the currently
3639     ** mapped region, reduce the effective mapping size as well. SQLite will
3640     ** use read() and write() to access data beyond this point from now on.
3641     */
3642     if( nByte<pFile->mmapSize ){
3643       pFile->mmapSize = nByte;
3644     }
3645 #endif
3646 
3647     return SQLITE_OK;
3648   }
3649 }
3650 
3651 /*
3652 ** Determine the current size of a file in bytes
3653 */
unixFileSize(sqlite3_file * id,i64 * pSize)3654 static int unixFileSize(sqlite3_file *id, i64 *pSize){
3655   int rc;
3656   struct stat buf;
3657   assert( id );
3658   rc = osFstat(((unixFile*)id)->h, &buf);
3659   SimulateIOError( rc=1 );
3660   if( rc!=0 ){
3661     storeLastErrno((unixFile*)id, errno);
3662     return SQLITE_IOERR_FSTAT;
3663   }
3664   *pSize = buf.st_size;
3665 
3666   /* When opening a zero-size database, the findInodeInfo() procedure
3667   ** writes a single byte into that file in order to work around a bug
3668   ** in the OS-X msdos filesystem.  In order to avoid problems with upper
3669   ** layers, we need to report this file size as zero even though it is
3670   ** really 1.   Ticket #3260.
3671   */
3672   if( *pSize==1 ) *pSize = 0;
3673 
3674 
3675   return SQLITE_OK;
3676 }
3677 
3678 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
3679 /*
3680 ** Handler for proxy-locking file-control verbs.  Defined below in the
3681 ** proxying locking division.
3682 */
3683 static int proxyFileControl(sqlite3_file*,int,void*);
3684 #endif
3685 
3686 /*
3687 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
3688 ** file-control operation.  Enlarge the database to nBytes in size
3689 ** (rounded up to the next chunk-size).  If the database is already
3690 ** nBytes or larger, this routine is a no-op.
3691 */
fcntlSizeHint(unixFile * pFile,i64 nByte)3692 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
3693   if( pFile->szChunk>0 ){
3694     i64 nSize;                    /* Required file size */
3695     struct stat buf;              /* Used to hold return values of fstat() */
3696 
3697     if( osFstat(pFile->h, &buf) ){
3698       return SQLITE_IOERR_FSTAT;
3699     }
3700 
3701     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
3702     if( nSize>(i64)buf.st_size ){
3703 
3704 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
3705       /* The code below is handling the return value of osFallocate()
3706       ** correctly. posix_fallocate() is defined to "returns zero on success,
3707       ** or an error number on  failure". See the manpage for details. */
3708       int err;
3709       do{
3710         err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
3711       }while( err==EINTR );
3712       if( err ) return SQLITE_IOERR_WRITE;
3713 #else
3714       /* If the OS does not have posix_fallocate(), fake it. Write a
3715       ** single byte to the last byte in each block that falls entirely
3716       ** within the extended region. Then, if required, a single byte
3717       ** at offset (nSize-1), to set the size of the file correctly.
3718       ** This is a similar technique to that used by glibc on systems
3719       ** that do not have a real fallocate() call.
3720       */
3721       int nBlk = buf.st_blksize;  /* File-system block size */
3722       int nWrite = 0;             /* Number of bytes written by seekAndWrite */
3723       i64 iWrite;                 /* Next offset to write to */
3724 
3725       iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
3726       assert( iWrite>=buf.st_size );
3727       assert( ((iWrite+1)%nBlk)==0 );
3728       for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
3729         if( iWrite>=nSize ) iWrite = nSize - 1;
3730         nWrite = seekAndWrite(pFile, iWrite, "", 1);
3731         if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
3732       }
3733 #endif
3734     }
3735   }
3736 
3737 #if SQLITE_MAX_MMAP_SIZE>0
3738   if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
3739     int rc;
3740     if( pFile->szChunk<=0 ){
3741       if( robust_ftruncate(pFile->h, nByte) ){
3742         storeLastErrno(pFile, errno);
3743         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
3744       }
3745     }
3746 
3747     rc = unixMapfile(pFile, nByte);
3748     return rc;
3749   }
3750 #endif
3751 
3752   return SQLITE_OK;
3753 }
3754 
3755 /*
3756 ** If *pArg is initially negative then this is a query.  Set *pArg to
3757 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
3758 **
3759 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
3760 */
unixModeBit(unixFile * pFile,unsigned char mask,int * pArg)3761 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
3762   if( *pArg<0 ){
3763     *pArg = (pFile->ctrlFlags & mask)!=0;
3764   }else if( (*pArg)==0 ){
3765     pFile->ctrlFlags &= ~mask;
3766   }else{
3767     pFile->ctrlFlags |= mask;
3768   }
3769 }
3770 
3771 /* Forward declaration */
3772 static int unixGetTempname(int nBuf, char *zBuf);
3773 
3774 /*
3775 ** Information and control of an open file handle.
3776 */
unixFileControl(sqlite3_file * id,int op,void * pArg)3777 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
3778   unixFile *pFile = (unixFile*)id;
3779   switch( op ){
3780     case SQLITE_FCNTL_LOCKSTATE: {
3781       *(int*)pArg = pFile->eFileLock;
3782       return SQLITE_OK;
3783     }
3784     case SQLITE_FCNTL_LAST_ERRNO: {
3785       *(int*)pArg = pFile->lastErrno;
3786       return SQLITE_OK;
3787     }
3788     case SQLITE_FCNTL_CHUNK_SIZE: {
3789       pFile->szChunk = *(int *)pArg;
3790       return SQLITE_OK;
3791     }
3792     case SQLITE_FCNTL_SIZE_HINT: {
3793       int rc;
3794       SimulateIOErrorBenign(1);
3795       rc = fcntlSizeHint(pFile, *(i64 *)pArg);
3796       SimulateIOErrorBenign(0);
3797       return rc;
3798     }
3799     case SQLITE_FCNTL_PERSIST_WAL: {
3800       unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
3801       return SQLITE_OK;
3802     }
3803     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
3804       unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
3805       return SQLITE_OK;
3806     }
3807     case SQLITE_FCNTL_VFSNAME: {
3808       *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
3809       return SQLITE_OK;
3810     }
3811     case SQLITE_FCNTL_TEMPFILENAME: {
3812       char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
3813       if( zTFile ){
3814         unixGetTempname(pFile->pVfs->mxPathname, zTFile);
3815         *(char**)pArg = zTFile;
3816       }
3817       return SQLITE_OK;
3818     }
3819     case SQLITE_FCNTL_HAS_MOVED: {
3820       *(int*)pArg = fileHasMoved(pFile);
3821       return SQLITE_OK;
3822     }
3823 #if SQLITE_MAX_MMAP_SIZE>0
3824     case SQLITE_FCNTL_MMAP_SIZE: {
3825       i64 newLimit = *(i64*)pArg;
3826       int rc = SQLITE_OK;
3827       if( newLimit>sqlite3GlobalConfig.mxMmap ){
3828         newLimit = sqlite3GlobalConfig.mxMmap;
3829       }
3830       *(i64*)pArg = pFile->mmapSizeMax;
3831       if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
3832         pFile->mmapSizeMax = newLimit;
3833         if( pFile->mmapSize>0 ){
3834           unixUnmapfile(pFile);
3835           rc = unixMapfile(pFile, -1);
3836         }
3837       }
3838       return rc;
3839     }
3840 #endif
3841 #ifdef SQLITE_DEBUG
3842     /* The pager calls this method to signal that it has done
3843     ** a rollback and that the database is therefore unchanged and
3844     ** it hence it is OK for the transaction change counter to be
3845     ** unchanged.
3846     */
3847     case SQLITE_FCNTL_DB_UNCHANGED: {
3848       ((unixFile*)id)->dbUpdate = 0;
3849       return SQLITE_OK;
3850     }
3851 #endif
3852 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
3853     case SQLITE_FCNTL_SET_LOCKPROXYFILE:
3854     case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
3855       return proxyFileControl(id,op,pArg);
3856     }
3857 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
3858   }
3859   return SQLITE_NOTFOUND;
3860 }
3861 
3862 /*
3863 ** Return the sector size in bytes of the underlying block device for
3864 ** the specified file. This is almost always 512 bytes, but may be
3865 ** larger for some devices.
3866 **
3867 ** SQLite code assumes this function cannot fail. It also assumes that
3868 ** if two files are created in the same file-system directory (i.e.
3869 ** a database and its journal file) that the sector size will be the
3870 ** same for both.
3871 */
3872 #ifndef __QNXNTO__
unixSectorSize(sqlite3_file * NotUsed)3873 static int unixSectorSize(sqlite3_file *NotUsed){
3874   UNUSED_PARAMETER(NotUsed);
3875   return SQLITE_DEFAULT_SECTOR_SIZE;
3876 }
3877 #endif
3878 
3879 /*
3880 ** The following version of unixSectorSize() is optimized for QNX.
3881 */
3882 #ifdef __QNXNTO__
3883 #include <sys/dcmd_blk.h>
3884 #include <sys/statvfs.h>
unixSectorSize(sqlite3_file * id)3885 static int unixSectorSize(sqlite3_file *id){
3886   unixFile *pFile = (unixFile*)id;
3887   if( pFile->sectorSize == 0 ){
3888     struct statvfs fsInfo;
3889 
3890     /* Set defaults for non-supported filesystems */
3891     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
3892     pFile->deviceCharacteristics = 0;
3893     if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
3894       return pFile->sectorSize;
3895     }
3896 
3897     if( !strcmp(fsInfo.f_basetype, "tmp") ) {
3898       pFile->sectorSize = fsInfo.f_bsize;
3899       pFile->deviceCharacteristics =
3900         SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
3901         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
3902                                       ** the write succeeds */
3903         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
3904                                       ** so it is ordered */
3905         0;
3906     }else if( strstr(fsInfo.f_basetype, "etfs") ){
3907       pFile->sectorSize = fsInfo.f_bsize;
3908       pFile->deviceCharacteristics =
3909         /* etfs cluster size writes are atomic */
3910         (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
3911         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
3912                                       ** the write succeeds */
3913         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
3914                                       ** so it is ordered */
3915         0;
3916     }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
3917       pFile->sectorSize = fsInfo.f_bsize;
3918       pFile->deviceCharacteristics =
3919         SQLITE_IOCAP_ATOMIC |         /* All filesystem writes are atomic */
3920         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
3921                                       ** the write succeeds */
3922         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
3923                                       ** so it is ordered */
3924         0;
3925     }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
3926       pFile->sectorSize = fsInfo.f_bsize;
3927       pFile->deviceCharacteristics =
3928         /* full bitset of atomics from max sector size and smaller */
3929         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
3930         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
3931                                       ** so it is ordered */
3932         0;
3933     }else if( strstr(fsInfo.f_basetype, "dos") ){
3934       pFile->sectorSize = fsInfo.f_bsize;
3935       pFile->deviceCharacteristics =
3936         /* full bitset of atomics from max sector size and smaller */
3937         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
3938         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
3939                                       ** so it is ordered */
3940         0;
3941     }else{
3942       pFile->deviceCharacteristics =
3943         SQLITE_IOCAP_ATOMIC512 |      /* blocks are atomic */
3944         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
3945                                       ** the write succeeds */
3946         0;
3947     }
3948   }
3949   /* Last chance verification.  If the sector size isn't a multiple of 512
3950   ** then it isn't valid.*/
3951   if( pFile->sectorSize % 512 != 0 ){
3952     pFile->deviceCharacteristics = 0;
3953     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
3954   }
3955   return pFile->sectorSize;
3956 }
3957 #endif /* __QNXNTO__ */
3958 
3959 /*
3960 ** Return the device characteristics for the file.
3961 **
3962 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
3963 ** However, that choice is controversial since technically the underlying
3964 ** file system does not always provide powersafe overwrites.  (In other
3965 ** words, after a power-loss event, parts of the file that were never
3966 ** written might end up being altered.)  However, non-PSOW behavior is very,
3967 ** very rare.  And asserting PSOW makes a large reduction in the amount
3968 ** of required I/O for journaling, since a lot of padding is eliminated.
3969 **  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
3970 ** available to turn it off and URI query parameter available to turn it off.
3971 */
unixDeviceCharacteristics(sqlite3_file * id)3972 static int unixDeviceCharacteristics(sqlite3_file *id){
3973   unixFile *p = (unixFile*)id;
3974   int rc = 0;
3975 #ifdef __QNXNTO__
3976   if( p->sectorSize==0 ) unixSectorSize(id);
3977   rc = p->deviceCharacteristics;
3978 #endif
3979   if( p->ctrlFlags & UNIXFILE_PSOW ){
3980     rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
3981   }
3982   return rc;
3983 }
3984 
3985 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
3986 
3987 /*
3988 ** Return the system page size.
3989 **
3990 ** This function should not be called directly by other code in this file.
3991 ** Instead, it should be called via macro osGetpagesize().
3992 */
unixGetpagesize(void)3993 static int unixGetpagesize(void){
3994 #if OS_VXWORKS
3995   return 1024;
3996 #elif defined(_BSD_SOURCE)
3997   return getpagesize();
3998 #else
3999   return (int)sysconf(_SC_PAGESIZE);
4000 #endif
4001 }
4002 
4003 #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
4004 
4005 #ifndef SQLITE_OMIT_WAL
4006 
4007 /*
4008 ** Object used to represent an shared memory buffer.
4009 **
4010 ** When multiple threads all reference the same wal-index, each thread
4011 ** has its own unixShm object, but they all point to a single instance
4012 ** of this unixShmNode object.  In other words, each wal-index is opened
4013 ** only once per process.
4014 **
4015 ** Each unixShmNode object is connected to a single unixInodeInfo object.
4016 ** We could coalesce this object into unixInodeInfo, but that would mean
4017 ** every open file that does not use shared memory (in other words, most
4018 ** open files) would have to carry around this extra information.  So
4019 ** the unixInodeInfo object contains a pointer to this unixShmNode object
4020 ** and the unixShmNode object is created only when needed.
4021 **
4022 ** unixMutexHeld() must be true when creating or destroying
4023 ** this object or while reading or writing the following fields:
4024 **
4025 **      nRef
4026 **
4027 ** The following fields are read-only after the object is created:
4028 **
4029 **      fid
4030 **      zFilename
4031 **
4032 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
4033 ** unixMutexHeld() is true when reading or writing any other field
4034 ** in this structure.
4035 */
4036 struct unixShmNode {
4037   unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
4038   sqlite3_mutex *mutex;      /* Mutex to access this object */
4039   char *zFilename;           /* Name of the mmapped file */
4040   int h;                     /* Open file descriptor */
4041   int szRegion;              /* Size of shared-memory regions */
4042   u16 nRegion;               /* Size of array apRegion */
4043   u8 isReadonly;             /* True if read-only */
4044   char **apRegion;           /* Array of mapped shared-memory regions */
4045   int nRef;                  /* Number of unixShm objects pointing to this */
4046   unixShm *pFirst;           /* All unixShm objects pointing to this */
4047 #ifdef SQLITE_DEBUG
4048   u8 exclMask;               /* Mask of exclusive locks held */
4049   u8 sharedMask;             /* Mask of shared locks held */
4050   u8 nextShmId;              /* Next available unixShm.id value */
4051 #endif
4052 };
4053 
4054 /*
4055 ** Structure used internally by this VFS to record the state of an
4056 ** open shared memory connection.
4057 **
4058 ** The following fields are initialized when this object is created and
4059 ** are read-only thereafter:
4060 **
4061 **    unixShm.pFile
4062 **    unixShm.id
4063 **
4064 ** All other fields are read/write.  The unixShm.pFile->mutex must be held
4065 ** while accessing any read/write fields.
4066 */
4067 struct unixShm {
4068   unixShmNode *pShmNode;     /* The underlying unixShmNode object */
4069   unixShm *pNext;            /* Next unixShm with the same unixShmNode */
4070   u8 hasMutex;               /* True if holding the unixShmNode mutex */
4071   u8 id;                     /* Id of this connection within its unixShmNode */
4072   u16 sharedMask;            /* Mask of shared locks held */
4073   u16 exclMask;              /* Mask of exclusive locks held */
4074 };
4075 
4076 /*
4077 ** Constants used for locking
4078 */
4079 #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
4080 #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
4081 
4082 /*
4083 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
4084 **
4085 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
4086 ** otherwise.
4087 */
unixShmSystemLock(unixFile * pFile,int lockType,int ofst,int n)4088 static int unixShmSystemLock(
4089   unixFile *pFile,       /* Open connection to the WAL file */
4090   int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
4091   int ofst,              /* First byte of the locking range */
4092   int n                  /* Number of bytes to lock */
4093 ){
4094   unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
4095   struct flock f;        /* The posix advisory locking structure */
4096   int rc = SQLITE_OK;    /* Result code form fcntl() */
4097 
4098   /* Access to the unixShmNode object is serialized by the caller */
4099   pShmNode = pFile->pInode->pShmNode;
4100   assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
4101 
4102   /* Shared locks never span more than one byte */
4103   assert( n==1 || lockType!=F_RDLCK );
4104 
4105   /* Locks are within range */
4106   assert( n>=1 && n<=SQLITE_SHM_NLOCK );
4107 
4108   if( pShmNode->h>=0 ){
4109     /* Initialize the locking parameters */
4110     memset(&f, 0, sizeof(f));
4111     f.l_type = lockType;
4112     f.l_whence = SEEK_SET;
4113     f.l_start = ofst;
4114     f.l_len = n;
4115 
4116     rc = osFcntl(pShmNode->h, F_SETLK, &f);
4117     rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
4118   }
4119 
4120   /* Update the global lock state and do debug tracing */
4121 #ifdef SQLITE_DEBUG
4122   { u16 mask;
4123   OSTRACE(("SHM-LOCK "));
4124   mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
4125   if( rc==SQLITE_OK ){
4126     if( lockType==F_UNLCK ){
4127       OSTRACE(("unlock %d ok", ofst));
4128       pShmNode->exclMask &= ~mask;
4129       pShmNode->sharedMask &= ~mask;
4130     }else if( lockType==F_RDLCK ){
4131       OSTRACE(("read-lock %d ok", ofst));
4132       pShmNode->exclMask &= ~mask;
4133       pShmNode->sharedMask |= mask;
4134     }else{
4135       assert( lockType==F_WRLCK );
4136       OSTRACE(("write-lock %d ok", ofst));
4137       pShmNode->exclMask |= mask;
4138       pShmNode->sharedMask &= ~mask;
4139     }
4140   }else{
4141     if( lockType==F_UNLCK ){
4142       OSTRACE(("unlock %d failed", ofst));
4143     }else if( lockType==F_RDLCK ){
4144       OSTRACE(("read-lock failed"));
4145     }else{
4146       assert( lockType==F_WRLCK );
4147       OSTRACE(("write-lock %d failed", ofst));
4148     }
4149   }
4150   OSTRACE((" - afterwards %03x,%03x\n",
4151            pShmNode->sharedMask, pShmNode->exclMask));
4152   }
4153 #endif
4154 
4155   return rc;
4156 }
4157 
4158 /*
4159 ** Return the minimum number of 32KB shm regions that should be mapped at
4160 ** a time, assuming that each mapping must be an integer multiple of the
4161 ** current system page-size.
4162 **
4163 ** Usually, this is 1. The exception seems to be systems that are configured
4164 ** to use 64KB pages - in this case each mapping must cover at least two
4165 ** shm regions.
4166 */
unixShmRegionPerMap(void)4167 static int unixShmRegionPerMap(void){
4168   int shmsz = 32*1024;            /* SHM region size */
4169   int pgsz = osGetpagesize();   /* System page size */
4170   assert( ((pgsz-1)&pgsz)==0 );   /* Page size must be a power of 2 */
4171   if( pgsz<shmsz ) return 1;
4172   return pgsz/shmsz;
4173 }
4174 
4175 /*
4176 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
4177 **
4178 ** This is not a VFS shared-memory method; it is a utility function called
4179 ** by VFS shared-memory methods.
4180 */
unixShmPurge(unixFile * pFd)4181 static void unixShmPurge(unixFile *pFd){
4182   unixShmNode *p = pFd->pInode->pShmNode;
4183   assert( unixMutexHeld() );
4184   if( p && ALWAYS(p->nRef==0) ){
4185     int nShmPerMap = unixShmRegionPerMap();
4186     int i;
4187     assert( p->pInode==pFd->pInode );
4188     sqlite3_mutex_free(p->mutex);
4189     for(i=0; i<p->nRegion; i+=nShmPerMap){
4190       if( p->h>=0 ){
4191         osMunmap(p->apRegion[i], p->szRegion);
4192       }else{
4193         sqlite3_free(p->apRegion[i]);
4194       }
4195     }
4196     sqlite3_free(p->apRegion);
4197     if( p->h>=0 ){
4198       robust_close(pFd, p->h, __LINE__);
4199       p->h = -1;
4200     }
4201     p->pInode->pShmNode = 0;
4202     sqlite3_free(p);
4203   }
4204 }
4205 
4206 /*
4207 ** Open a shared-memory area associated with open database file pDbFd.
4208 ** This particular implementation uses mmapped files.
4209 **
4210 ** The file used to implement shared-memory is in the same directory
4211 ** as the open database file and has the same name as the open database
4212 ** file with the "-shm" suffix added.  For example, if the database file
4213 ** is "/home/user1/config.db" then the file that is created and mmapped
4214 ** for shared memory will be called "/home/user1/config.db-shm".
4215 **
4216 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
4217 ** some other tmpfs mount. But if a file in a different directory
4218 ** from the database file is used, then differing access permissions
4219 ** or a chroot() might cause two different processes on the same
4220 ** database to end up using different files for shared memory -
4221 ** meaning that their memory would not really be shared - resulting
4222 ** in database corruption.  Nevertheless, this tmpfs file usage
4223 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
4224 ** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
4225 ** option results in an incompatible build of SQLite;  builds of SQLite
4226 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
4227 ** same database file at the same time, database corruption will likely
4228 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
4229 ** "unsupported" and may go away in a future SQLite release.
4230 **
4231 ** When opening a new shared-memory file, if no other instances of that
4232 ** file are currently open, in this process or in other processes, then
4233 ** the file must be truncated to zero length or have its header cleared.
4234 **
4235 ** If the original database file (pDbFd) is using the "unix-excl" VFS
4236 ** that means that an exclusive lock is held on the database file and
4237 ** that no other processes are able to read or write the database.  In
4238 ** that case, we do not really need shared memory.  No shared memory
4239 ** file is created.  The shared memory will be simulated with heap memory.
4240 */
unixOpenSharedMemory(unixFile * pDbFd)4241 static int unixOpenSharedMemory(unixFile *pDbFd){
4242   struct unixShm *p = 0;          /* The connection to be opened */
4243   struct unixShmNode *pShmNode;   /* The underlying mmapped file */
4244   int rc;                         /* Result code */
4245   unixInodeInfo *pInode;          /* The inode of fd */
4246   char *zShmFilename;             /* Name of the file used for SHM */
4247   int nShmFilename;               /* Size of the SHM filename in bytes */
4248 
4249   /* Allocate space for the new unixShm object. */
4250   p = sqlite3_malloc64( sizeof(*p) );
4251   if( p==0 ) return SQLITE_NOMEM_BKPT;
4252   memset(p, 0, sizeof(*p));
4253   assert( pDbFd->pShm==0 );
4254 
4255   /* Check to see if a unixShmNode object already exists. Reuse an existing
4256   ** one if present. Create a new one if necessary.
4257   */
4258   unixEnterMutex();
4259   pInode = pDbFd->pInode;
4260   pShmNode = pInode->pShmNode;
4261   if( pShmNode==0 ){
4262     struct stat sStat;                 /* fstat() info for database file */
4263 #ifndef SQLITE_SHM_DIRECTORY
4264     const char *zBasePath = pDbFd->zPath;
4265 #endif
4266 
4267     /* Call fstat() to figure out the permissions on the database file. If
4268     ** a new *-shm file is created, an attempt will be made to create it
4269     ** with the same permissions.
4270     */
4271     if( osFstat(pDbFd->h, &sStat) ){
4272       rc = SQLITE_IOERR_FSTAT;
4273       goto shm_open_err;
4274     }
4275 
4276 #ifdef SQLITE_SHM_DIRECTORY
4277     nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
4278 #else
4279     nShmFilename = 6 + (int)strlen(zBasePath);
4280 #endif
4281     pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
4282     if( pShmNode==0 ){
4283       rc = SQLITE_NOMEM_BKPT;
4284       goto shm_open_err;
4285     }
4286     memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
4287     zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
4288 #ifdef SQLITE_SHM_DIRECTORY
4289     sqlite3_snprintf(nShmFilename, zShmFilename,
4290                      SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
4291                      (u32)sStat.st_ino, (u32)sStat.st_dev);
4292 #else
4293     sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath);
4294     sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
4295 #endif
4296     pShmNode->h = -1;
4297     pDbFd->pInode->pShmNode = pShmNode;
4298     pShmNode->pInode = pDbFd->pInode;
4299     if( sqlite3GlobalConfig.bCoreMutex ){
4300       pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
4301       if( pShmNode->mutex==0 ){
4302         rc = SQLITE_NOMEM_BKPT;
4303         goto shm_open_err;
4304       }
4305     }
4306 
4307     if( pInode->bProcessLock==0 ){
4308       int openFlags = O_RDWR | O_CREAT;
4309       if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
4310         openFlags = O_RDONLY;
4311         pShmNode->isReadonly = 1;
4312       }
4313       pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
4314       if( pShmNode->h<0 ){
4315         rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
4316         goto shm_open_err;
4317       }
4318 
4319       /* If this process is running as root, make sure that the SHM file
4320       ** is owned by the same user that owns the original database.  Otherwise,
4321       ** the original owner will not be able to connect.
4322       */
4323       robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
4324 
4325       /* Check to see if another process is holding the dead-man switch.
4326       ** If not, truncate the file to zero length.
4327       */
4328       rc = SQLITE_OK;
4329       if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
4330         if( robust_ftruncate(pShmNode->h, 0) ){
4331           rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
4332         }
4333       }
4334       if( rc==SQLITE_OK ){
4335         rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
4336       }
4337       if( rc ) goto shm_open_err;
4338     }
4339   }
4340 
4341   /* Make the new connection a child of the unixShmNode */
4342   p->pShmNode = pShmNode;
4343 #ifdef SQLITE_DEBUG
4344   p->id = pShmNode->nextShmId++;
4345 #endif
4346   pShmNode->nRef++;
4347   pDbFd->pShm = p;
4348   unixLeaveMutex();
4349 
4350   /* The reference count on pShmNode has already been incremented under
4351   ** the cover of the unixEnterMutex() mutex and the pointer from the
4352   ** new (struct unixShm) object to the pShmNode has been set. All that is
4353   ** left to do is to link the new object into the linked list starting
4354   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
4355   ** mutex.
4356   */
4357   sqlite3_mutex_enter(pShmNode->mutex);
4358   p->pNext = pShmNode->pFirst;
4359   pShmNode->pFirst = p;
4360   sqlite3_mutex_leave(pShmNode->mutex);
4361   return SQLITE_OK;
4362 
4363   /* Jump here on any error */
4364 shm_open_err:
4365   unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
4366   sqlite3_free(p);
4367   unixLeaveMutex();
4368   return rc;
4369 }
4370 
4371 /*
4372 ** This function is called to obtain a pointer to region iRegion of the
4373 ** shared-memory associated with the database file fd. Shared-memory regions
4374 ** are numbered starting from zero. Each shared-memory region is szRegion
4375 ** bytes in size.
4376 **
4377 ** If an error occurs, an error code is returned and *pp is set to NULL.
4378 **
4379 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
4380 ** region has not been allocated (by any client, including one running in a
4381 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
4382 ** bExtend is non-zero and the requested shared-memory region has not yet
4383 ** been allocated, it is allocated by this function.
4384 **
4385 ** If the shared-memory region has already been allocated or is allocated by
4386 ** this call as described above, then it is mapped into this processes
4387 ** address space (if it is not already), *pp is set to point to the mapped
4388 ** memory and SQLITE_OK returned.
4389 */
unixShmMap(sqlite3_file * fd,int iRegion,int szRegion,int bExtend,void volatile ** pp)4390 static int unixShmMap(
4391   sqlite3_file *fd,               /* Handle open on database file */
4392   int iRegion,                    /* Region to retrieve */
4393   int szRegion,                   /* Size of regions */
4394   int bExtend,                    /* True to extend file if necessary */
4395   void volatile **pp              /* OUT: Mapped memory */
4396 ){
4397   unixFile *pDbFd = (unixFile*)fd;
4398   unixShm *p;
4399   unixShmNode *pShmNode;
4400   int rc = SQLITE_OK;
4401   int nShmPerMap = unixShmRegionPerMap();
4402   int nReqRegion;
4403 
4404   /* If the shared-memory file has not yet been opened, open it now. */
4405   if( pDbFd->pShm==0 ){
4406     rc = unixOpenSharedMemory(pDbFd);
4407     if( rc!=SQLITE_OK ) return rc;
4408   }
4409 
4410   p = pDbFd->pShm;
4411   pShmNode = p->pShmNode;
4412   sqlite3_mutex_enter(pShmNode->mutex);
4413   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
4414   assert( pShmNode->pInode==pDbFd->pInode );
4415   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
4416   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
4417 
4418   /* Minimum number of regions required to be mapped. */
4419   nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
4420 
4421   if( pShmNode->nRegion<nReqRegion ){
4422     char **apNew;                      /* New apRegion[] array */
4423     int nByte = nReqRegion*szRegion;   /* Minimum required file size */
4424     struct stat sStat;                 /* Used by fstat() */
4425 
4426     pShmNode->szRegion = szRegion;
4427 
4428     if( pShmNode->h>=0 ){
4429       /* The requested region is not mapped into this processes address space.
4430       ** Check to see if it has been allocated (i.e. if the wal-index file is
4431       ** large enough to contain the requested region).
4432       */
4433       if( osFstat(pShmNode->h, &sStat) ){
4434         rc = SQLITE_IOERR_SHMSIZE;
4435         goto shmpage_out;
4436       }
4437 
4438       if( sStat.st_size<nByte ){
4439         /* The requested memory region does not exist. If bExtend is set to
4440         ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
4441         */
4442         if( !bExtend ){
4443           goto shmpage_out;
4444         }
4445 
4446         /* Alternatively, if bExtend is true, extend the file. Do this by
4447         ** writing a single byte to the end of each (OS) page being
4448         ** allocated or extended. Technically, we need only write to the
4449         ** last page in order to extend the file. But writing to all new
4450         ** pages forces the OS to allocate them immediately, which reduces
4451         ** the chances of SIGBUS while accessing the mapped region later on.
4452         */
4453         else{
4454           static const int pgsz = 4096;
4455           int iPg;
4456 
4457           /* Write to the last byte of each newly allocated or extended page */
4458           assert( (nByte % pgsz)==0 );
4459           for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
4460             int x = 0;
4461             if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){
4462               const char *zFile = pShmNode->zFilename;
4463               rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
4464               goto shmpage_out;
4465             }
4466           }
4467         }
4468       }
4469     }
4470 
4471     /* Map the requested memory region into this processes address space. */
4472     apNew = (char **)sqlite3_realloc(
4473         pShmNode->apRegion, nReqRegion*sizeof(char *)
4474     );
4475     if( !apNew ){
4476       rc = SQLITE_IOERR_NOMEM_BKPT;
4477       goto shmpage_out;
4478     }
4479     pShmNode->apRegion = apNew;
4480     while( pShmNode->nRegion<nReqRegion ){
4481       int nMap = szRegion*nShmPerMap;
4482       int i;
4483       void *pMem;
4484       if( pShmNode->h>=0 ){
4485         pMem = osMmap(0, nMap,
4486             pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
4487             MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
4488         );
4489         if( pMem==MAP_FAILED ){
4490           rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
4491           goto shmpage_out;
4492         }
4493       }else{
4494         pMem = sqlite3_malloc64(szRegion);
4495         if( pMem==0 ){
4496           rc = SQLITE_NOMEM_BKPT;
4497           goto shmpage_out;
4498         }
4499         memset(pMem, 0, szRegion);
4500       }
4501 
4502       for(i=0; i<nShmPerMap; i++){
4503         pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
4504       }
4505       pShmNode->nRegion += nShmPerMap;
4506     }
4507   }
4508 
4509 shmpage_out:
4510   if( pShmNode->nRegion>iRegion ){
4511     *pp = pShmNode->apRegion[iRegion];
4512   }else{
4513     *pp = 0;
4514   }
4515   if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
4516   sqlite3_mutex_leave(pShmNode->mutex);
4517   return rc;
4518 }
4519 
4520 /*
4521 ** Change the lock state for a shared-memory segment.
4522 **
4523 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
4524 ** different here than in posix.  In xShmLock(), one can go from unlocked
4525 ** to shared and back or from unlocked to exclusive and back.  But one may
4526 ** not go from shared to exclusive or from exclusive to shared.
4527 */
unixShmLock(sqlite3_file * fd,int ofst,int n,int flags)4528 static int unixShmLock(
4529   sqlite3_file *fd,          /* Database file holding the shared memory */
4530   int ofst,                  /* First lock to acquire or release */
4531   int n,                     /* Number of locks to acquire or release */
4532   int flags                  /* What to do with the lock */
4533 ){
4534   unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
4535   unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
4536   unixShm *pX;                          /* For looping over all siblings */
4537   unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
4538   int rc = SQLITE_OK;                   /* Result code */
4539   u16 mask;                             /* Mask of locks to take or release */
4540 
4541   assert( pShmNode==pDbFd->pInode->pShmNode );
4542   assert( pShmNode->pInode==pDbFd->pInode );
4543   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
4544   assert( n>=1 );
4545   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
4546        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
4547        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
4548        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
4549   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
4550   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
4551   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
4552 
4553   mask = (1<<(ofst+n)) - (1<<ofst);
4554   assert( n>1 || mask==(1<<ofst) );
4555   sqlite3_mutex_enter(pShmNode->mutex);
4556   if( flags & SQLITE_SHM_UNLOCK ){
4557     u16 allMask = 0; /* Mask of locks held by siblings */
4558 
4559     /* See if any siblings hold this same lock */
4560     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
4561       if( pX==p ) continue;
4562       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
4563       allMask |= pX->sharedMask;
4564     }
4565 
4566     /* Unlock the system-level locks */
4567     if( (mask & allMask)==0 ){
4568       rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
4569     }else{
4570       rc = SQLITE_OK;
4571     }
4572 
4573     /* Undo the local locks */
4574     if( rc==SQLITE_OK ){
4575       p->exclMask &= ~mask;
4576       p->sharedMask &= ~mask;
4577     }
4578   }else if( flags & SQLITE_SHM_SHARED ){
4579     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
4580 
4581     /* Find out which shared locks are already held by sibling connections.
4582     ** If any sibling already holds an exclusive lock, go ahead and return
4583     ** SQLITE_BUSY.
4584     */
4585     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
4586       if( (pX->exclMask & mask)!=0 ){
4587         rc = SQLITE_BUSY;
4588         break;
4589       }
4590       allShared |= pX->sharedMask;
4591     }
4592 
4593     /* Get shared locks at the system level, if necessary */
4594     if( rc==SQLITE_OK ){
4595       if( (allShared & mask)==0 ){
4596         rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
4597       }else{
4598         rc = SQLITE_OK;
4599       }
4600     }
4601 
4602     /* Get the local shared locks */
4603     if( rc==SQLITE_OK ){
4604       p->sharedMask |= mask;
4605     }
4606   }else{
4607     /* Make sure no sibling connections hold locks that will block this
4608     ** lock.  If any do, return SQLITE_BUSY right away.
4609     */
4610     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
4611       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
4612         rc = SQLITE_BUSY;
4613         break;
4614       }
4615     }
4616 
4617     /* Get the exclusive locks at the system level.  Then if successful
4618     ** also mark the local connection as being locked.
4619     */
4620     if( rc==SQLITE_OK ){
4621       rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
4622       if( rc==SQLITE_OK ){
4623         assert( (p->sharedMask & mask)==0 );
4624         p->exclMask |= mask;
4625       }
4626     }
4627   }
4628   sqlite3_mutex_leave(pShmNode->mutex);
4629   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
4630            p->id, osGetpid(0), p->sharedMask, p->exclMask));
4631   return rc;
4632 }
4633 
4634 /*
4635 ** Implement a memory barrier or memory fence on shared memory.
4636 **
4637 ** All loads and stores begun before the barrier must complete before
4638 ** any load or store begun after the barrier.
4639 */
unixShmBarrier(sqlite3_file * fd)4640 static void unixShmBarrier(
4641   sqlite3_file *fd                /* Database file holding the shared memory */
4642 ){
4643   UNUSED_PARAMETER(fd);
4644   sqlite3MemoryBarrier();         /* compiler-defined memory barrier */
4645   unixEnterMutex();               /* Also mutex, for redundancy */
4646   unixLeaveMutex();
4647 }
4648 
4649 /*
4650 ** Close a connection to shared-memory.  Delete the underlying
4651 ** storage if deleteFlag is true.
4652 **
4653 ** If there is no shared memory associated with the connection then this
4654 ** routine is a harmless no-op.
4655 */
unixShmUnmap(sqlite3_file * fd,int deleteFlag)4656 static int unixShmUnmap(
4657   sqlite3_file *fd,               /* The underlying database file */
4658   int deleteFlag                  /* Delete shared-memory if true */
4659 ){
4660   unixShm *p;                     /* The connection to be closed */
4661   unixShmNode *pShmNode;          /* The underlying shared-memory file */
4662   unixShm **pp;                   /* For looping over sibling connections */
4663   unixFile *pDbFd;                /* The underlying database file */
4664 
4665   pDbFd = (unixFile*)fd;
4666   p = pDbFd->pShm;
4667   if( p==0 ) return SQLITE_OK;
4668   pShmNode = p->pShmNode;
4669 
4670   assert( pShmNode==pDbFd->pInode->pShmNode );
4671   assert( pShmNode->pInode==pDbFd->pInode );
4672 
4673   /* Remove connection p from the set of connections associated
4674   ** with pShmNode */
4675   sqlite3_mutex_enter(pShmNode->mutex);
4676   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
4677   *pp = p->pNext;
4678 
4679   /* Free the connection p */
4680   sqlite3_free(p);
4681   pDbFd->pShm = 0;
4682   sqlite3_mutex_leave(pShmNode->mutex);
4683 
4684   /* If pShmNode->nRef has reached 0, then close the underlying
4685   ** shared-memory file, too */
4686   unixEnterMutex();
4687   assert( pShmNode->nRef>0 );
4688   pShmNode->nRef--;
4689   if( pShmNode->nRef==0 ){
4690     if( deleteFlag && pShmNode->h>=0 ){
4691       osUnlink(pShmNode->zFilename);
4692     }
4693     unixShmPurge(pDbFd);
4694   }
4695   unixLeaveMutex();
4696 
4697   return SQLITE_OK;
4698 }
4699 
4700 
4701 #else
4702 # define unixShmMap     0
4703 # define unixShmLock    0
4704 # define unixShmBarrier 0
4705 # define unixShmUnmap   0
4706 #endif /* #ifndef SQLITE_OMIT_WAL */
4707 
4708 #if SQLITE_MAX_MMAP_SIZE>0
4709 /*
4710 ** If it is currently memory mapped, unmap file pFd.
4711 */
unixUnmapfile(unixFile * pFd)4712 static void unixUnmapfile(unixFile *pFd){
4713   assert( pFd->nFetchOut==0 );
4714   if( pFd->pMapRegion ){
4715     osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
4716     pFd->pMapRegion = 0;
4717     pFd->mmapSize = 0;
4718     pFd->mmapSizeActual = 0;
4719   }
4720 }
4721 
4722 /*
4723 ** Attempt to set the size of the memory mapping maintained by file
4724 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
4725 **
4726 ** If successful, this function sets the following variables:
4727 **
4728 **       unixFile.pMapRegion
4729 **       unixFile.mmapSize
4730 **       unixFile.mmapSizeActual
4731 **
4732 ** If unsuccessful, an error message is logged via sqlite3_log() and
4733 ** the three variables above are zeroed. In this case SQLite should
4734 ** continue accessing the database using the xRead() and xWrite()
4735 ** methods.
4736 */
unixRemapfile(unixFile * pFd,i64 nNew)4737 static void unixRemapfile(
4738   unixFile *pFd,                  /* File descriptor object */
4739   i64 nNew                        /* Required mapping size */
4740 ){
4741   const char *zErr = "mmap";
4742   int h = pFd->h;                      /* File descriptor open on db file */
4743   u8 *pOrig = (u8 *)pFd->pMapRegion;   /* Pointer to current file mapping */
4744   i64 nOrig = pFd->mmapSizeActual;     /* Size of pOrig region in bytes */
4745   u8 *pNew = 0;                        /* Location of new mapping */
4746   int flags = PROT_READ;               /* Flags to pass to mmap() */
4747 
4748   assert( pFd->nFetchOut==0 );
4749   assert( nNew>pFd->mmapSize );
4750   assert( nNew<=pFd->mmapSizeMax );
4751   assert( nNew>0 );
4752   assert( pFd->mmapSizeActual>=pFd->mmapSize );
4753   assert( MAP_FAILED!=0 );
4754 
4755 #ifdef SQLITE_MMAP_READWRITE
4756   if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
4757 #endif
4758 
4759   if( pOrig ){
4760 #if HAVE_MREMAP
4761     i64 nReuse = pFd->mmapSize;
4762 #else
4763     const int szSyspage = osGetpagesize();
4764     i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
4765 #endif
4766     u8 *pReq = &pOrig[nReuse];
4767 
4768     /* Unmap any pages of the existing mapping that cannot be reused. */
4769     if( nReuse!=nOrig ){
4770       osMunmap(pReq, nOrig-nReuse);
4771     }
4772 
4773 #if HAVE_MREMAP
4774     pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
4775     zErr = "mremap";
4776 #else
4777     pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
4778     if( pNew!=MAP_FAILED ){
4779       if( pNew!=pReq ){
4780         osMunmap(pNew, nNew - nReuse);
4781         pNew = 0;
4782       }else{
4783         pNew = pOrig;
4784       }
4785     }
4786 #endif
4787 
4788     /* The attempt to extend the existing mapping failed. Free it. */
4789     if( pNew==MAP_FAILED || pNew==0 ){
4790       osMunmap(pOrig, nReuse);
4791     }
4792   }
4793 
4794   /* If pNew is still NULL, try to create an entirely new mapping. */
4795   if( pNew==0 ){
4796     pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
4797   }
4798 
4799   if( pNew==MAP_FAILED ){
4800     pNew = 0;
4801     nNew = 0;
4802     unixLogError(SQLITE_OK, zErr, pFd->zPath);
4803 
4804     /* If the mmap() above failed, assume that all subsequent mmap() calls
4805     ** will probably fail too. Fall back to using xRead/xWrite exclusively
4806     ** in this case.  */
4807     pFd->mmapSizeMax = 0;
4808   }
4809   pFd->pMapRegion = (void *)pNew;
4810   pFd->mmapSize = pFd->mmapSizeActual = nNew;
4811 }
4812 
4813 /*
4814 ** Memory map or remap the file opened by file-descriptor pFd (if the file
4815 ** is already mapped, the existing mapping is replaced by the new). Or, if
4816 ** there already exists a mapping for this file, and there are still
4817 ** outstanding xFetch() references to it, this function is a no-op.
4818 **
4819 ** If parameter nByte is non-negative, then it is the requested size of
4820 ** the mapping to create. Otherwise, if nByte is less than zero, then the
4821 ** requested size is the size of the file on disk. The actual size of the
4822 ** created mapping is either the requested size or the value configured
4823 ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
4824 **
4825 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
4826 ** recreated as a result of outstanding references) or an SQLite error
4827 ** code otherwise.
4828 */
unixMapfile(unixFile * pFd,i64 nMap)4829 static int unixMapfile(unixFile *pFd, i64 nMap){
4830   assert( nMap>=0 || pFd->nFetchOut==0 );
4831   assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
4832   if( pFd->nFetchOut>0 ) return SQLITE_OK;
4833 
4834   if( nMap<0 ){
4835     struct stat statbuf;          /* Low-level file information */
4836     if( osFstat(pFd->h, &statbuf) ){
4837       return SQLITE_IOERR_FSTAT;
4838     }
4839     nMap = statbuf.st_size;
4840   }
4841   if( nMap>pFd->mmapSizeMax ){
4842     nMap = pFd->mmapSizeMax;
4843   }
4844 
4845   assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
4846   if( nMap!=pFd->mmapSize ){
4847     unixRemapfile(pFd, nMap);
4848   }
4849 
4850   return SQLITE_OK;
4851 }
4852 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
4853 
4854 /*
4855 ** If possible, return a pointer to a mapping of file fd starting at offset
4856 ** iOff. The mapping must be valid for at least nAmt bytes.
4857 **
4858 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
4859 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
4860 ** Finally, if an error does occur, return an SQLite error code. The final
4861 ** value of *pp is undefined in this case.
4862 **
4863 ** If this function does return a pointer, the caller must eventually
4864 ** release the reference by calling unixUnfetch().
4865 */
unixFetch(sqlite3_file * fd,i64 iOff,int nAmt,void ** pp)4866 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
4867 #if SQLITE_MAX_MMAP_SIZE>0
4868   unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
4869 #endif
4870   *pp = 0;
4871 
4872 #if SQLITE_MAX_MMAP_SIZE>0
4873   if( pFd->mmapSizeMax>0 ){
4874     if( pFd->pMapRegion==0 ){
4875       int rc = unixMapfile(pFd, -1);
4876       if( rc!=SQLITE_OK ) return rc;
4877     }
4878     if( pFd->mmapSize >= iOff+nAmt ){
4879       *pp = &((u8 *)pFd->pMapRegion)[iOff];
4880       pFd->nFetchOut++;
4881     }
4882   }
4883 #endif
4884   return SQLITE_OK;
4885 }
4886 
4887 /*
4888 ** If the third argument is non-NULL, then this function releases a
4889 ** reference obtained by an earlier call to unixFetch(). The second
4890 ** argument passed to this function must be the same as the corresponding
4891 ** argument that was passed to the unixFetch() invocation.
4892 **
4893 ** Or, if the third argument is NULL, then this function is being called
4894 ** to inform the VFS layer that, according to POSIX, any existing mapping
4895 ** may now be invalid and should be unmapped.
4896 */
unixUnfetch(sqlite3_file * fd,i64 iOff,void * p)4897 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
4898 #if SQLITE_MAX_MMAP_SIZE>0
4899   unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
4900   UNUSED_PARAMETER(iOff);
4901 
4902   /* If p==0 (unmap the entire file) then there must be no outstanding
4903   ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
4904   ** then there must be at least one outstanding.  */
4905   assert( (p==0)==(pFd->nFetchOut==0) );
4906 
4907   /* If p!=0, it must match the iOff value. */
4908   assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
4909 
4910   if( p ){
4911     pFd->nFetchOut--;
4912   }else{
4913     unixUnmapfile(pFd);
4914   }
4915 
4916   assert( pFd->nFetchOut>=0 );
4917 #else
4918   UNUSED_PARAMETER(fd);
4919   UNUSED_PARAMETER(p);
4920   UNUSED_PARAMETER(iOff);
4921 #endif
4922   return SQLITE_OK;
4923 }
4924 
4925 /*
4926 ** Here ends the implementation of all sqlite3_file methods.
4927 **
4928 ********************** End sqlite3_file Methods *******************************
4929 ******************************************************************************/
4930 
4931 /*
4932 ** This division contains definitions of sqlite3_io_methods objects that
4933 ** implement various file locking strategies.  It also contains definitions
4934 ** of "finder" functions.  A finder-function is used to locate the appropriate
4935 ** sqlite3_io_methods object for a particular database file.  The pAppData
4936 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
4937 ** the correct finder-function for that VFS.
4938 **
4939 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
4940 ** object.  The only interesting finder-function is autolockIoFinder, which
4941 ** looks at the filesystem type and tries to guess the best locking
4942 ** strategy from that.
4943 **
4944 ** For finder-function F, two objects are created:
4945 **
4946 **    (1) The real finder-function named "FImpt()".
4947 **
4948 **    (2) A constant pointer to this function named just "F".
4949 **
4950 **
4951 ** A pointer to the F pointer is used as the pAppData value for VFS
4952 ** objects.  We have to do this instead of letting pAppData point
4953 ** directly at the finder-function since C90 rules prevent a void*
4954 ** from be cast into a function pointer.
4955 **
4956 **
4957 ** Each instance of this macro generates two objects:
4958 **
4959 **   *  A constant sqlite3_io_methods object call METHOD that has locking
4960 **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
4961 **
4962 **   *  An I/O method finder function called FINDER that returns a pointer
4963 **      to the METHOD object in the previous bullet.
4964 */
4965 #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP)     \
4966 static const sqlite3_io_methods METHOD = {                                   \
4967    VERSION,                    /* iVersion */                                \
4968    CLOSE,                      /* xClose */                                  \
4969    unixRead,                   /* xRead */                                   \
4970    unixWrite,                  /* xWrite */                                  \
4971    unixTruncate,               /* xTruncate */                               \
4972    unixSync,                   /* xSync */                                   \
4973    unixFileSize,               /* xFileSize */                               \
4974    LOCK,                       /* xLock */                                   \
4975    UNLOCK,                     /* xUnlock */                                 \
4976    CKLOCK,                     /* xCheckReservedLock */                      \
4977    unixFileControl,            /* xFileControl */                            \
4978    unixSectorSize,             /* xSectorSize */                             \
4979    unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
4980    SHMMAP,                     /* xShmMap */                                 \
4981    unixShmLock,                /* xShmLock */                                \
4982    unixShmBarrier,             /* xShmBarrier */                             \
4983    unixShmUnmap,               /* xShmUnmap */                               \
4984    unixFetch,                  /* xFetch */                                  \
4985    unixUnfetch,                /* xUnfetch */                                \
4986 };                                                                           \
4987 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
4988   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
4989   return &METHOD;                                                            \
4990 }                                                                            \
4991 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
4992     = FINDER##Impl;
4993 
4994 /*
4995 ** Here are all of the sqlite3_io_methods objects for each of the
4996 ** locking strategies.  Functions that return pointers to these methods
4997 ** are also created.
4998 */
4999 IOMETHODS(
5000   posixIoFinder,            /* Finder function name */
5001   posixIoMethods,           /* sqlite3_io_methods object name */
5002   3,                        /* shared memory and mmap are enabled */
5003   unixClose,                /* xClose method */
5004   unixLock,                 /* xLock method */
5005   unixUnlock,               /* xUnlock method */
5006   unixCheckReservedLock,    /* xCheckReservedLock method */
5007   unixShmMap                /* xShmMap method */
5008 )
5009 IOMETHODS(
5010   nolockIoFinder,           /* Finder function name */
5011   nolockIoMethods,          /* sqlite3_io_methods object name */
5012   3,                        /* shared memory is disabled */
5013   nolockClose,              /* xClose method */
5014   nolockLock,               /* xLock method */
5015   nolockUnlock,             /* xUnlock method */
5016   nolockCheckReservedLock,  /* xCheckReservedLock method */
5017   0                         /* xShmMap method */
5018 )
5019 IOMETHODS(
5020   dotlockIoFinder,          /* Finder function name */
5021   dotlockIoMethods,         /* sqlite3_io_methods object name */
5022   1,                        /* shared memory is disabled */
5023   dotlockClose,             /* xClose method */
5024   dotlockLock,              /* xLock method */
5025   dotlockUnlock,            /* xUnlock method */
5026   dotlockCheckReservedLock, /* xCheckReservedLock method */
5027   0                         /* xShmMap method */
5028 )
5029 
5030 #if SQLITE_ENABLE_LOCKING_STYLE
5031 IOMETHODS(
5032   flockIoFinder,            /* Finder function name */
5033   flockIoMethods,           /* sqlite3_io_methods object name */
5034   1,                        /* shared memory is disabled */
5035   flockClose,               /* xClose method */
5036   flockLock,                /* xLock method */
5037   flockUnlock,              /* xUnlock method */
5038   flockCheckReservedLock,   /* xCheckReservedLock method */
5039   0                         /* xShmMap method */
5040 )
5041 #endif
5042 
5043 #if OS_VXWORKS
5044 IOMETHODS(
5045   semIoFinder,              /* Finder function name */
5046   semIoMethods,             /* sqlite3_io_methods object name */
5047   1,                        /* shared memory is disabled */
5048   semXClose,                /* xClose method */
5049   semXLock,                 /* xLock method */
5050   semXUnlock,               /* xUnlock method */
5051   semXCheckReservedLock,    /* xCheckReservedLock method */
5052   0                         /* xShmMap method */
5053 )
5054 #endif
5055 
5056 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
5057 IOMETHODS(
5058   afpIoFinder,              /* Finder function name */
5059   afpIoMethods,             /* sqlite3_io_methods object name */
5060   1,                        /* shared memory is disabled */
5061   afpClose,                 /* xClose method */
5062   afpLock,                  /* xLock method */
5063   afpUnlock,                /* xUnlock method */
5064   afpCheckReservedLock,     /* xCheckReservedLock method */
5065   0                         /* xShmMap method */
5066 )
5067 #endif
5068 
5069 /*
5070 ** The proxy locking method is a "super-method" in the sense that it
5071 ** opens secondary file descriptors for the conch and lock files and
5072 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
5073 ** secondary files.  For this reason, the division that implements
5074 ** proxy locking is located much further down in the file.  But we need
5075 ** to go ahead and define the sqlite3_io_methods and finder function
5076 ** for proxy locking here.  So we forward declare the I/O methods.
5077 */
5078 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
5079 static int proxyClose(sqlite3_file*);
5080 static int proxyLock(sqlite3_file*, int);
5081 static int proxyUnlock(sqlite3_file*, int);
5082 static int proxyCheckReservedLock(sqlite3_file*, int*);
5083 IOMETHODS(
5084   proxyIoFinder,            /* Finder function name */
5085   proxyIoMethods,           /* sqlite3_io_methods object name */
5086   1,                        /* shared memory is disabled */
5087   proxyClose,               /* xClose method */
5088   proxyLock,                /* xLock method */
5089   proxyUnlock,              /* xUnlock method */
5090   proxyCheckReservedLock,   /* xCheckReservedLock method */
5091   0                         /* xShmMap method */
5092 )
5093 #endif
5094 
5095 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
5096 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
5097 IOMETHODS(
5098   nfsIoFinder,               /* Finder function name */
5099   nfsIoMethods,              /* sqlite3_io_methods object name */
5100   1,                         /* shared memory is disabled */
5101   unixClose,                 /* xClose method */
5102   unixLock,                  /* xLock method */
5103   nfsUnlock,                 /* xUnlock method */
5104   unixCheckReservedLock,     /* xCheckReservedLock method */
5105   0                          /* xShmMap method */
5106 )
5107 #endif
5108 
5109 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
5110 /*
5111 ** This "finder" function attempts to determine the best locking strategy
5112 ** for the database file "filePath".  It then returns the sqlite3_io_methods
5113 ** object that implements that strategy.
5114 **
5115 ** This is for MacOSX only.
5116 */
autolockIoFinderImpl(const char * filePath,unixFile * pNew)5117 static const sqlite3_io_methods *autolockIoFinderImpl(
5118   const char *filePath,    /* name of the database file */
5119   unixFile *pNew           /* open file object for the database file */
5120 ){
5121   static const struct Mapping {
5122     const char *zFilesystem;              /* Filesystem type name */
5123     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
5124   } aMap[] = {
5125     { "hfs",    &posixIoMethods },
5126     { "ufs",    &posixIoMethods },
5127     { "afpfs",  &afpIoMethods },
5128     { "smbfs",  &afpIoMethods },
5129     { "webdav", &nolockIoMethods },
5130     { 0, 0 }
5131   };
5132   int i;
5133   struct statfs fsInfo;
5134   struct flock lockInfo;
5135 
5136   if( !filePath ){
5137     /* If filePath==NULL that means we are dealing with a transient file
5138     ** that does not need to be locked. */
5139     return &nolockIoMethods;
5140   }
5141   if( statfs(filePath, &fsInfo) != -1 ){
5142     if( fsInfo.f_flags & MNT_RDONLY ){
5143       return &nolockIoMethods;
5144     }
5145     for(i=0; aMap[i].zFilesystem; i++){
5146       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
5147         return aMap[i].pMethods;
5148       }
5149     }
5150   }
5151 
5152   /* Default case. Handles, amongst others, "nfs".
5153   ** Test byte-range lock using fcntl(). If the call succeeds,
5154   ** assume that the file-system supports POSIX style locks.
5155   */
5156   lockInfo.l_len = 1;
5157   lockInfo.l_start = 0;
5158   lockInfo.l_whence = SEEK_SET;
5159   lockInfo.l_type = F_RDLCK;
5160   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
5161     if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
5162       return &nfsIoMethods;
5163     } else {
5164       return &posixIoMethods;
5165     }
5166   }else{
5167     return &dotlockIoMethods;
5168   }
5169 }
5170 static const sqlite3_io_methods
5171   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
5172 
5173 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
5174 
5175 #if OS_VXWORKS
5176 /*
5177 ** This "finder" function for VxWorks checks to see if posix advisory
5178 ** locking works.  If it does, then that is what is used.  If it does not
5179 ** work, then fallback to named semaphore locking.
5180 */
vxworksIoFinderImpl(const char * filePath,unixFile * pNew)5181 static const sqlite3_io_methods *vxworksIoFinderImpl(
5182   const char *filePath,    /* name of the database file */
5183   unixFile *pNew           /* the open file object */
5184 ){
5185   struct flock lockInfo;
5186 
5187   if( !filePath ){
5188     /* If filePath==NULL that means we are dealing with a transient file
5189     ** that does not need to be locked. */
5190     return &nolockIoMethods;
5191   }
5192 
5193   /* Test if fcntl() is supported and use POSIX style locks.
5194   ** Otherwise fall back to the named semaphore method.
5195   */
5196   lockInfo.l_len = 1;
5197   lockInfo.l_start = 0;
5198   lockInfo.l_whence = SEEK_SET;
5199   lockInfo.l_type = F_RDLCK;
5200   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
5201     return &posixIoMethods;
5202   }else{
5203     return &semIoMethods;
5204   }
5205 }
5206 static const sqlite3_io_methods
5207   *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
5208 
5209 #endif /* OS_VXWORKS */
5210 
5211 /*
5212 ** An abstract type for a pointer to an IO method finder function:
5213 */
5214 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
5215 
5216 
5217 /****************************************************************************
5218 **************************** sqlite3_vfs methods ****************************
5219 **
5220 ** This division contains the implementation of methods on the
5221 ** sqlite3_vfs object.
5222 */
5223 
5224 /*
5225 ** Initialize the contents of the unixFile structure pointed to by pId.
5226 */
fillInUnixFile(sqlite3_vfs * pVfs,int h,sqlite3_file * pId,const char * zFilename,int ctrlFlags)5227 static int fillInUnixFile(
5228   sqlite3_vfs *pVfs,      /* Pointer to vfs object */
5229   int h,                  /* Open file descriptor of file being opened */
5230   sqlite3_file *pId,      /* Write to the unixFile structure here */
5231   const char *zFilename,  /* Name of the file being opened */
5232   int ctrlFlags           /* Zero or more UNIXFILE_* values */
5233 ){
5234   const sqlite3_io_methods *pLockingStyle;
5235   unixFile *pNew = (unixFile *)pId;
5236   int rc = SQLITE_OK;
5237 
5238   assert( pNew->pInode==NULL );
5239 
5240   /* Usually the path zFilename should not be a relative pathname. The
5241   ** exception is when opening the proxy "conch" file in builds that
5242   ** include the special Apple locking styles.
5243   */
5244 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
5245   assert( zFilename==0 || zFilename[0]=='/'
5246     || pVfs->pAppData==(void*)&autolockIoFinder );
5247 #else
5248   assert( zFilename==0 || zFilename[0]=='/' );
5249 #endif
5250 
5251   /* No locking occurs in temporary files */
5252   assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
5253 
5254   OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
5255   pNew->h = h;
5256   pNew->pVfs = pVfs;
5257   pNew->zPath = zFilename;
5258   pNew->ctrlFlags = (u8)ctrlFlags;
5259 #if SQLITE_MAX_MMAP_SIZE>0
5260   pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
5261 #endif
5262   if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
5263                            "psow", SQLITE_POWERSAFE_OVERWRITE) ){
5264     pNew->ctrlFlags |= UNIXFILE_PSOW;
5265   }
5266   if( strcmp(pVfs->zName,"unix-excl")==0 ){
5267     pNew->ctrlFlags |= UNIXFILE_EXCL;
5268   }
5269 
5270 #if OS_VXWORKS
5271   pNew->pId = vxworksFindFileId(zFilename);
5272   if( pNew->pId==0 ){
5273     ctrlFlags |= UNIXFILE_NOLOCK;
5274     rc = SQLITE_NOMEM_BKPT;
5275   }
5276 #endif
5277 
5278   if( ctrlFlags & UNIXFILE_NOLOCK ){
5279     pLockingStyle = &nolockIoMethods;
5280   }else{
5281     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
5282 #if SQLITE_ENABLE_LOCKING_STYLE
5283     /* Cache zFilename in the locking context (AFP and dotlock override) for
5284     ** proxyLock activation is possible (remote proxy is based on db name)
5285     ** zFilename remains valid until file is closed, to support */
5286     pNew->lockingContext = (void*)zFilename;
5287 #endif
5288   }
5289 
5290   if( pLockingStyle == &posixIoMethods
5291 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
5292     || pLockingStyle == &nfsIoMethods
5293 #endif
5294   ){
5295     unixEnterMutex();
5296     rc = findInodeInfo(pNew, &pNew->pInode);
5297     if( rc!=SQLITE_OK ){
5298       /* If an error occurred in findInodeInfo(), close the file descriptor
5299       ** immediately, before releasing the mutex. findInodeInfo() may fail
5300       ** in two scenarios:
5301       **
5302       **   (a) A call to fstat() failed.
5303       **   (b) A malloc failed.
5304       **
5305       ** Scenario (b) may only occur if the process is holding no other
5306       ** file descriptors open on the same file. If there were other file
5307       ** descriptors on this file, then no malloc would be required by
5308       ** findInodeInfo(). If this is the case, it is quite safe to close
5309       ** handle h - as it is guaranteed that no posix locks will be released
5310       ** by doing so.
5311       **
5312       ** If scenario (a) caused the error then things are not so safe. The
5313       ** implicit assumption here is that if fstat() fails, things are in
5314       ** such bad shape that dropping a lock or two doesn't matter much.
5315       */
5316       robust_close(pNew, h, __LINE__);
5317       h = -1;
5318     }
5319     unixLeaveMutex();
5320   }
5321 
5322 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
5323   else if( pLockingStyle == &afpIoMethods ){
5324     /* AFP locking uses the file path so it needs to be included in
5325     ** the afpLockingContext.
5326     */
5327     afpLockingContext *pCtx;
5328     pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
5329     if( pCtx==0 ){
5330       rc = SQLITE_NOMEM_BKPT;
5331     }else{
5332       /* NB: zFilename exists and remains valid until the file is closed
5333       ** according to requirement F11141.  So we do not need to make a
5334       ** copy of the filename. */
5335       pCtx->dbPath = zFilename;
5336       pCtx->reserved = 0;
5337       srandomdev();
5338       unixEnterMutex();
5339       rc = findInodeInfo(pNew, &pNew->pInode);
5340       if( rc!=SQLITE_OK ){
5341         sqlite3_free(pNew->lockingContext);
5342         robust_close(pNew, h, __LINE__);
5343         h = -1;
5344       }
5345       unixLeaveMutex();
5346     }
5347   }
5348 #endif
5349 
5350   else if( pLockingStyle == &dotlockIoMethods ){
5351     /* Dotfile locking uses the file path so it needs to be included in
5352     ** the dotlockLockingContext
5353     */
5354     char *zLockFile;
5355     int nFilename;
5356     assert( zFilename!=0 );
5357     nFilename = (int)strlen(zFilename) + 6;
5358     zLockFile = (char *)sqlite3_malloc64(nFilename);
5359     if( zLockFile==0 ){
5360       rc = SQLITE_NOMEM_BKPT;
5361     }else{
5362       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
5363     }
5364     pNew->lockingContext = zLockFile;
5365   }
5366 
5367 #if OS_VXWORKS
5368   else if( pLockingStyle == &semIoMethods ){
5369     /* Named semaphore locking uses the file path so it needs to be
5370     ** included in the semLockingContext
5371     */
5372     unixEnterMutex();
5373     rc = findInodeInfo(pNew, &pNew->pInode);
5374     if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
5375       char *zSemName = pNew->pInode->aSemName;
5376       int n;
5377       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
5378                        pNew->pId->zCanonicalName);
5379       for( n=1; zSemName[n]; n++ )
5380         if( zSemName[n]=='/' ) zSemName[n] = '_';
5381       pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
5382       if( pNew->pInode->pSem == SEM_FAILED ){
5383         rc = SQLITE_NOMEM_BKPT;
5384         pNew->pInode->aSemName[0] = '\0';
5385       }
5386     }
5387     unixLeaveMutex();
5388   }
5389 #endif
5390 
5391   storeLastErrno(pNew, 0);
5392 #if OS_VXWORKS
5393   if( rc!=SQLITE_OK ){
5394     if( h>=0 ) robust_close(pNew, h, __LINE__);
5395     h = -1;
5396     osUnlink(zFilename);
5397     pNew->ctrlFlags |= UNIXFILE_DELETE;
5398   }
5399 #endif
5400   if( rc!=SQLITE_OK ){
5401     if( h>=0 ) robust_close(pNew, h, __LINE__);
5402   }else{
5403     pNew->pMethod = pLockingStyle;
5404     OpenCounter(+1);
5405     verifyDbFile(pNew);
5406   }
5407   return rc;
5408 }
5409 
5410 /*
5411 ** Return the name of a directory in which to put temporary files.
5412 ** If no suitable temporary file directory can be found, return NULL.
5413 */
unixTempFileDir(void)5414 static const char *unixTempFileDir(void){
5415   static const char *azDirs[] = {
5416      0,
5417      0,
5418      "/var/tmp",
5419      "/usr/tmp",
5420      "/tmp",
5421      "."
5422   };
5423   unsigned int i = 0;
5424   struct stat buf;
5425   const char *zDir = sqlite3_temp_directory;
5426 
5427   if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
5428   if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
5429   while(1){
5430     if( zDir!=0
5431      && osStat(zDir, &buf)==0
5432      && S_ISDIR(buf.st_mode)
5433      && osAccess(zDir, 03)==0
5434     ){
5435       return zDir;
5436     }
5437     if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
5438     zDir = azDirs[i++];
5439   }
5440   return 0;
5441 }
5442 
5443 /*
5444 ** Create a temporary file name in zBuf.  zBuf must be allocated
5445 ** by the calling process and must be big enough to hold at least
5446 ** pVfs->mxPathname bytes.
5447 */
unixGetTempname(int nBuf,char * zBuf)5448 static int unixGetTempname(int nBuf, char *zBuf){
5449   const char *zDir;
5450   int iLimit = 0;
5451 
5452   /* It's odd to simulate an io-error here, but really this is just
5453   ** using the io-error infrastructure to test that SQLite handles this
5454   ** function failing.
5455   */
5456   zBuf[0] = 0;
5457   SimulateIOError( return SQLITE_IOERR );
5458 
5459   zDir = unixTempFileDir();
5460   if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
5461   do{
5462     u64 r;
5463     sqlite3_randomness(sizeof(r), &r);
5464     assert( nBuf>2 );
5465     zBuf[nBuf-2] = 0;
5466     sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
5467                      zDir, r, 0);
5468     if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
5469   }while( osAccess(zBuf,0)==0 );
5470   return SQLITE_OK;
5471 }
5472 
5473 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
5474 /*
5475 ** Routine to transform a unixFile into a proxy-locking unixFile.
5476 ** Implementation in the proxy-lock division, but used by unixOpen()
5477 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
5478 */
5479 static int proxyTransformUnixFile(unixFile*, const char*);
5480 #endif
5481 
5482 /*
5483 ** Search for an unused file descriptor that was opened on the database
5484 ** file (not a journal or master-journal file) identified by pathname
5485 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
5486 ** argument to this function.
5487 **
5488 ** Such a file descriptor may exist if a database connection was closed
5489 ** but the associated file descriptor could not be closed because some
5490 ** other file descriptor open on the same file is holding a file-lock.
5491 ** Refer to comments in the unixClose() function and the lengthy comment
5492 ** describing "Posix Advisory Locking" at the start of this file for
5493 ** further details. Also, ticket #4018.
5494 **
5495 ** If a suitable file descriptor is found, then it is returned. If no
5496 ** such file descriptor is located, -1 is returned.
5497 */
findReusableFd(const char * zPath,int flags)5498 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
5499   UnixUnusedFd *pUnused = 0;
5500 
5501   /* Do not search for an unused file descriptor on vxworks. Not because
5502   ** vxworks would not benefit from the change (it might, we're not sure),
5503   ** but because no way to test it is currently available. It is better
5504   ** not to risk breaking vxworks support for the sake of such an obscure
5505   ** feature.  */
5506 #if !OS_VXWORKS
5507   struct stat sStat;                   /* Results of stat() call */
5508 
5509   /* A stat() call may fail for various reasons. If this happens, it is
5510   ** almost certain that an open() call on the same path will also fail.
5511   ** For this reason, if an error occurs in the stat() call here, it is
5512   ** ignored and -1 is returned. The caller will try to open a new file
5513   ** descriptor on the same path, fail, and return an error to SQLite.
5514   **
5515   ** Even if a subsequent open() call does succeed, the consequences of
5516   ** not searching for a reusable file descriptor are not dire.  */
5517   if( 0==osStat(zPath, &sStat) ){
5518     unixInodeInfo *pInode;
5519 
5520     unixEnterMutex();
5521     pInode = inodeList;
5522     while( pInode && (pInode->fileId.dev!=sStat.st_dev
5523                      || pInode->fileId.ino!=(u64)sStat.st_ino) ){
5524        pInode = pInode->pNext;
5525     }
5526     if( pInode ){
5527       UnixUnusedFd **pp;
5528       for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
5529       pUnused = *pp;
5530       if( pUnused ){
5531         *pp = pUnused->pNext;
5532       }
5533     }
5534     unixLeaveMutex();
5535   }
5536 #endif    /* if !OS_VXWORKS */
5537   return pUnused;
5538 }
5539 
5540 /*
5541 ** Find the mode, uid and gid of file zFile.
5542 */
getFileMode(const char * zFile,mode_t * pMode,uid_t * pUid,gid_t * pGid)5543 static int getFileMode(
5544   const char *zFile,              /* File name */
5545   mode_t *pMode,                  /* OUT: Permissions of zFile */
5546   uid_t *pUid,                    /* OUT: uid of zFile. */
5547   gid_t *pGid                     /* OUT: gid of zFile. */
5548 ){
5549   struct stat sStat;              /* Output of stat() on database file */
5550   int rc = SQLITE_OK;
5551   if( 0==osStat(zFile, &sStat) ){
5552     *pMode = sStat.st_mode & 0777;
5553     *pUid = sStat.st_uid;
5554     *pGid = sStat.st_gid;
5555   }else{
5556     rc = SQLITE_IOERR_FSTAT;
5557   }
5558   return rc;
5559 }
5560 
5561 /*
5562 ** This function is called by unixOpen() to determine the unix permissions
5563 ** to create new files with. If no error occurs, then SQLITE_OK is returned
5564 ** and a value suitable for passing as the third argument to open(2) is
5565 ** written to *pMode. If an IO error occurs, an SQLite error code is
5566 ** returned and the value of *pMode is not modified.
5567 **
5568 ** In most cases, this routine sets *pMode to 0, which will become
5569 ** an indication to robust_open() to create the file using
5570 ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
5571 ** But if the file being opened is a WAL or regular journal file, then
5572 ** this function queries the file-system for the permissions on the
5573 ** corresponding database file and sets *pMode to this value. Whenever
5574 ** possible, WAL and journal files are created using the same permissions
5575 ** as the associated database file.
5576 **
5577 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
5578 ** original filename is unavailable.  But 8_3_NAMES is only used for
5579 ** FAT filesystems and permissions do not matter there, so just use
5580 ** the default permissions.
5581 */
findCreateFileMode(const char * zPath,int flags,mode_t * pMode,uid_t * pUid,gid_t * pGid)5582 static int findCreateFileMode(
5583   const char *zPath,              /* Path of file (possibly) being created */
5584   int flags,                      /* Flags passed as 4th argument to xOpen() */
5585   mode_t *pMode,                  /* OUT: Permissions to open file with */
5586   uid_t *pUid,                    /* OUT: uid to set on the file */
5587   gid_t *pGid                     /* OUT: gid to set on the file */
5588 ){
5589   int rc = SQLITE_OK;             /* Return Code */
5590   *pMode = 0;
5591   *pUid = 0;
5592   *pGid = 0;
5593   if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
5594     char zDb[MAX_PATHNAME+1];     /* Database file path */
5595     int nDb;                      /* Number of valid bytes in zDb */
5596 
5597     /* zPath is a path to a WAL or journal file. The following block derives
5598     ** the path to the associated database file from zPath. This block handles
5599     ** the following naming conventions:
5600     **
5601     **   "<path to db>-journal"
5602     **   "<path to db>-wal"
5603     **   "<path to db>-journalNN"
5604     **   "<path to db>-walNN"
5605     **
5606     ** where NN is a decimal number. The NN naming schemes are
5607     ** used by the test_multiplex.c module.
5608     */
5609     nDb = sqlite3Strlen30(zPath) - 1;
5610     while( zPath[nDb]!='-' ){
5611 #ifndef SQLITE_ENABLE_8_3_NAMES
5612       /* In the normal case (8+3 filenames disabled) the journal filename
5613       ** is guaranteed to contain a '-' character. */
5614       assert( nDb>0 );
5615       assert( sqlite3Isalnum(zPath[nDb]) );
5616 #else
5617       /* If 8+3 names are possible, then the journal file might not contain
5618       ** a '-' character.  So check for that case and return early. */
5619       if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
5620 #endif
5621       nDb--;
5622     }
5623     memcpy(zDb, zPath, nDb);
5624     zDb[nDb] = '\0';
5625 
5626     rc = getFileMode(zDb, pMode, pUid, pGid);
5627   }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
5628     *pMode = 0600;
5629   }else if( flags & SQLITE_OPEN_URI ){
5630     /* If this is a main database file and the file was opened using a URI
5631     ** filename, check for the "modeof" parameter. If present, interpret
5632     ** its value as a filename and try to copy the mode, uid and gid from
5633     ** that file.  */
5634     const char *z = sqlite3_uri_parameter(zPath, "modeof");
5635     if( z ){
5636       rc = getFileMode(z, pMode, pUid, pGid);
5637     }
5638   }
5639   return rc;
5640 }
5641 
5642 /*
5643 ** Open the file zPath.
5644 **
5645 ** Previously, the SQLite OS layer used three functions in place of this
5646 ** one:
5647 **
5648 **     sqlite3OsOpenReadWrite();
5649 **     sqlite3OsOpenReadOnly();
5650 **     sqlite3OsOpenExclusive();
5651 **
5652 ** These calls correspond to the following combinations of flags:
5653 **
5654 **     ReadWrite() ->     (READWRITE | CREATE)
5655 **     ReadOnly()  ->     (READONLY)
5656 **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
5657 **
5658 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
5659 ** true, the file was configured to be automatically deleted when the
5660 ** file handle closed. To achieve the same effect using this new
5661 ** interface, add the DELETEONCLOSE flag to those specified above for
5662 ** OpenExclusive().
5663 */
unixOpen(sqlite3_vfs * pVfs,const char * zPath,sqlite3_file * pFile,int flags,int * pOutFlags)5664 static int unixOpen(
5665   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
5666   const char *zPath,           /* Pathname of file to be opened */
5667   sqlite3_file *pFile,         /* The file descriptor to be filled in */
5668   int flags,                   /* Input flags to control the opening */
5669   int *pOutFlags               /* Output flags returned to SQLite core */
5670 ){
5671   unixFile *p = (unixFile *)pFile;
5672   int fd = -1;                   /* File descriptor returned by open() */
5673   int openFlags = 0;             /* Flags to pass to open() */
5674   int eType = flags&0xFFFFFF00;  /* Type of file to open */
5675   int noLock;                    /* True to omit locking primitives */
5676   int rc = SQLITE_OK;            /* Function Return Code */
5677   int ctrlFlags = 0;             /* UNIXFILE_* flags */
5678 
5679   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
5680   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
5681   int isCreate     = (flags & SQLITE_OPEN_CREATE);
5682   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
5683   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
5684 #if SQLITE_ENABLE_LOCKING_STYLE
5685   int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
5686 #endif
5687 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
5688   struct statfs fsInfo;
5689 #endif
5690 
5691   /* If creating a master or main-file journal, this function will open
5692   ** a file-descriptor on the directory too. The first time unixSync()
5693   ** is called the directory file descriptor will be fsync()ed and close()d.
5694   */
5695   int syncDir = (isCreate && (
5696         eType==SQLITE_OPEN_MASTER_JOURNAL
5697      || eType==SQLITE_OPEN_MAIN_JOURNAL
5698      || eType==SQLITE_OPEN_WAL
5699   ));
5700 
5701   /* If argument zPath is a NULL pointer, this function is required to open
5702   ** a temporary file. Use this buffer to store the file name in.
5703   */
5704   char zTmpname[MAX_PATHNAME+2];
5705   const char *zName = zPath;
5706 
5707   /* Check the following statements are true:
5708   **
5709   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
5710   **   (b) if CREATE is set, then READWRITE must also be set, and
5711   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
5712   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
5713   */
5714   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
5715   assert(isCreate==0 || isReadWrite);
5716   assert(isExclusive==0 || isCreate);
5717   assert(isDelete==0 || isCreate);
5718 
5719   /* The main DB, main journal, WAL file and master journal are never
5720   ** automatically deleted. Nor are they ever temporary files.  */
5721   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
5722   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
5723   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
5724   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
5725 
5726   /* Assert that the upper layer has set one of the "file-type" flags. */
5727   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
5728        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
5729        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
5730        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
5731   );
5732 
5733   /* Detect a pid change and reset the PRNG.  There is a race condition
5734   ** here such that two or more threads all trying to open databases at
5735   ** the same instant might all reset the PRNG.  But multiple resets
5736   ** are harmless.
5737   */
5738   if( randomnessPid!=osGetpid(0) ){
5739     randomnessPid = osGetpid(0);
5740     sqlite3_randomness(0,0);
5741   }
5742 
5743   memset(p, 0, sizeof(unixFile));
5744 
5745   if( eType==SQLITE_OPEN_MAIN_DB ){
5746     UnixUnusedFd *pUnused;
5747     pUnused = findReusableFd(zName, flags);
5748     if( pUnused ){
5749       fd = pUnused->fd;
5750     }else{
5751       pUnused = sqlite3_malloc64(sizeof(*pUnused));
5752       if( !pUnused ){
5753         return SQLITE_NOMEM_BKPT;
5754       }
5755     }
5756     p->pUnused = pUnused;
5757 
5758     /* Database filenames are double-zero terminated if they are not
5759     ** URIs with parameters.  Hence, they can always be passed into
5760     ** sqlite3_uri_parameter(). */
5761     assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
5762 
5763   }else if( !zName ){
5764     /* If zName is NULL, the upper layer is requesting a temp file. */
5765     assert(isDelete && !syncDir);
5766     rc = unixGetTempname(pVfs->mxPathname, zTmpname);
5767     if( rc!=SQLITE_OK ){
5768       return rc;
5769     }
5770     zName = zTmpname;
5771 
5772     /* Generated temporary filenames are always double-zero terminated
5773     ** for use by sqlite3_uri_parameter(). */
5774     assert( zName[strlen(zName)+1]==0 );
5775   }
5776 
5777   /* Determine the value of the flags parameter passed to POSIX function
5778   ** open(). These must be calculated even if open() is not called, as
5779   ** they may be stored as part of the file handle and used by the
5780   ** 'conch file' locking functions later on.  */
5781   if( isReadonly )  openFlags |= O_RDONLY;
5782   if( isReadWrite ) openFlags |= O_RDWR;
5783   if( isCreate )    openFlags |= O_CREAT;
5784   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
5785   openFlags |= (O_LARGEFILE|O_BINARY);
5786 
5787   if( fd<0 ){
5788     mode_t openMode;              /* Permissions to create file with */
5789     uid_t uid;                    /* Userid for the file */
5790     gid_t gid;                    /* Groupid for the file */
5791     rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
5792     if( rc!=SQLITE_OK ){
5793       assert( !p->pUnused );
5794       assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
5795       return rc;
5796     }
5797     fd = robust_open(zName, openFlags, openMode);
5798     OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
5799     assert( !isExclusive || (openFlags & O_CREAT)!=0 );
5800     if( fd<0 && errno!=EISDIR && isReadWrite ){
5801       /* Failed to open the file for read/write access. Try read-only. */
5802       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
5803       openFlags &= ~(O_RDWR|O_CREAT);
5804       flags |= SQLITE_OPEN_READONLY;
5805       openFlags |= O_RDONLY;
5806       isReadonly = 1;
5807       fd = robust_open(zName, openFlags, openMode);
5808     }
5809     if( fd<0 ){
5810       rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
5811       goto open_finished;
5812     }
5813 
5814     /* If this process is running as root and if creating a new rollback
5815     ** journal or WAL file, set the ownership of the journal or WAL to be
5816     ** the same as the original database.
5817     */
5818     if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
5819       robustFchown(fd, uid, gid);
5820     }
5821   }
5822   assert( fd>=0 );
5823   if( pOutFlags ){
5824     *pOutFlags = flags;
5825   }
5826 
5827   if( p->pUnused ){
5828     p->pUnused->fd = fd;
5829     p->pUnused->flags = flags;
5830   }
5831 
5832   if( isDelete ){
5833 #if OS_VXWORKS
5834     zPath = zName;
5835 #elif defined(SQLITE_UNLINK_AFTER_CLOSE)
5836     zPath = sqlite3_mprintf("%s", zName);
5837     if( zPath==0 ){
5838       robust_close(p, fd, __LINE__);
5839       return SQLITE_NOMEM_BKPT;
5840     }
5841 #else
5842     osUnlink(zName);
5843 #endif
5844   }
5845 #if SQLITE_ENABLE_LOCKING_STYLE
5846   else{
5847     p->openFlags = openFlags;
5848   }
5849 #endif
5850 
5851 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
5852   if( fstatfs(fd, &fsInfo) == -1 ){
5853     storeLastErrno(p, errno);
5854     robust_close(p, fd, __LINE__);
5855     return SQLITE_IOERR_ACCESS;
5856   }
5857   if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
5858     ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
5859   }
5860   if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
5861     ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
5862   }
5863 #endif
5864 
5865   /* Set up appropriate ctrlFlags */
5866   if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
5867   if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
5868   noLock = eType!=SQLITE_OPEN_MAIN_DB;
5869   if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
5870   if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
5871   if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
5872 
5873 #if SQLITE_ENABLE_LOCKING_STYLE
5874 #if SQLITE_PREFER_PROXY_LOCKING
5875   isAutoProxy = 1;
5876 #endif
5877   if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
5878     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
5879     int useProxy = 0;
5880 
5881     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
5882     ** never use proxy, NULL means use proxy for non-local files only.  */
5883     if( envforce!=NULL ){
5884       useProxy = atoi(envforce)>0;
5885     }else{
5886       useProxy = !(fsInfo.f_flags&MNT_LOCAL);
5887     }
5888     if( useProxy ){
5889       rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
5890       if( rc==SQLITE_OK ){
5891         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
5892         if( rc!=SQLITE_OK ){
5893           /* Use unixClose to clean up the resources added in fillInUnixFile
5894           ** and clear all the structure's references.  Specifically,
5895           ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
5896           */
5897           unixClose(pFile);
5898           return rc;
5899         }
5900       }
5901       goto open_finished;
5902     }
5903   }
5904 #endif
5905 
5906   rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
5907 
5908 open_finished:
5909   if( rc!=SQLITE_OK ){
5910     sqlite3_free(p->pUnused);
5911   }
5912   return rc;
5913 }
5914 
5915 
5916 /*
5917 ** Delete the file at zPath. If the dirSync argument is true, fsync()
5918 ** the directory after deleting the file.
5919 */
unixDelete(sqlite3_vfs * NotUsed,const char * zPath,int dirSync)5920 static int unixDelete(
5921   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
5922   const char *zPath,        /* Name of file to be deleted */
5923   int dirSync               /* If true, fsync() directory after deleting file */
5924 ){
5925   int rc = SQLITE_OK;
5926   UNUSED_PARAMETER(NotUsed);
5927   SimulateIOError(return SQLITE_IOERR_DELETE);
5928   if( osUnlink(zPath)==(-1) ){
5929     if( errno==ENOENT
5930 #if OS_VXWORKS
5931         || osAccess(zPath,0)!=0
5932 #endif
5933     ){
5934       rc = SQLITE_IOERR_DELETE_NOENT;
5935     }else{
5936       rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
5937     }
5938     return rc;
5939   }
5940 #ifndef SQLITE_DISABLE_DIRSYNC
5941   if( (dirSync & 1)!=0 ){
5942     int fd;
5943     rc = osOpenDirectory(zPath, &fd);
5944     if( rc==SQLITE_OK ){
5945       if( full_fsync(fd,0,0) ){
5946         rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
5947       }
5948       robust_close(0, fd, __LINE__);
5949     }else{
5950       assert( rc==SQLITE_CANTOPEN );
5951       rc = SQLITE_OK;
5952     }
5953   }
5954 #endif
5955   return rc;
5956 }
5957 
5958 /*
5959 ** Test the existence of or access permissions of file zPath. The
5960 ** test performed depends on the value of flags:
5961 **
5962 **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
5963 **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
5964 **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
5965 **
5966 ** Otherwise return 0.
5967 */
unixAccess(sqlite3_vfs * NotUsed,const char * zPath,int flags,int * pResOut)5968 static int unixAccess(
5969   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
5970   const char *zPath,      /* Path of the file to examine */
5971   int flags,              /* What do we want to learn about the zPath file? */
5972   int *pResOut            /* Write result boolean here */
5973 ){
5974   UNUSED_PARAMETER(NotUsed);
5975   SimulateIOError( return SQLITE_IOERR_ACCESS; );
5976   assert( pResOut!=0 );
5977 
5978   /* The spec says there are three possible values for flags.  But only
5979   ** two of them are actually used */
5980   assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
5981 
5982   if( flags==SQLITE_ACCESS_EXISTS ){
5983     struct stat buf;
5984     *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0);
5985   }else{
5986     *pResOut = osAccess(zPath, W_OK|R_OK)==0;
5987   }
5988   return SQLITE_OK;
5989 }
5990 
5991 /*
5992 **
5993 */
mkFullPathname(const char * zPath,char * zOut,int nOut)5994 static int mkFullPathname(
5995   const char *zPath,              /* Input path */
5996   char *zOut,                     /* Output buffer */
5997   int nOut                        /* Allocated size of buffer zOut */
5998 ){
5999   int nPath = sqlite3Strlen30(zPath);
6000   int iOff = 0;
6001   if( zPath[0]!='/' ){
6002     if( osGetcwd(zOut, nOut-2)==0 ){
6003       return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
6004     }
6005     iOff = sqlite3Strlen30(zOut);
6006     zOut[iOff++] = '/';
6007   }
6008   if( (iOff+nPath+1)>nOut ){
6009     /* SQLite assumes that xFullPathname() nul-terminates the output buffer
6010     ** even if it returns an error.  */
6011     zOut[iOff] = '\0';
6012     return SQLITE_CANTOPEN_BKPT;
6013   }
6014   sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
6015   return SQLITE_OK;
6016 }
6017 
6018 /*
6019 ** Turn a relative pathname into a full pathname. The relative path
6020 ** is stored as a nul-terminated string in the buffer pointed to by
6021 ** zPath.
6022 **
6023 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
6024 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
6025 ** this buffer before returning.
6026 */
unixFullPathname(sqlite3_vfs * pVfs,const char * zPath,int nOut,char * zOut)6027 static int unixFullPathname(
6028   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
6029   const char *zPath,            /* Possibly relative input path */
6030   int nOut,                     /* Size of output buffer in bytes */
6031   char *zOut                    /* Output buffer */
6032 ){
6033 #if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
6034   return mkFullPathname(zPath, zOut, nOut);
6035 #else
6036   int rc = SQLITE_OK;
6037   int nByte;
6038   int nLink = 1;                /* Number of symbolic links followed so far */
6039   const char *zIn = zPath;      /* Input path for each iteration of loop */
6040   char *zDel = 0;
6041 
6042   assert( pVfs->mxPathname==MAX_PATHNAME );
6043   UNUSED_PARAMETER(pVfs);
6044 
6045   /* It's odd to simulate an io-error here, but really this is just
6046   ** using the io-error infrastructure to test that SQLite handles this
6047   ** function failing. This function could fail if, for example, the
6048   ** current working directory has been unlinked.
6049   */
6050   SimulateIOError( return SQLITE_ERROR );
6051 
6052   do {
6053 
6054     /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
6055     ** link, or false otherwise.  */
6056     int bLink = 0;
6057     struct stat buf;
6058     if( osLstat(zIn, &buf)!=0 ){
6059       if( errno!=ENOENT ){
6060         rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
6061       }
6062     }else{
6063       bLink = S_ISLNK(buf.st_mode);
6064     }
6065 
6066     if( bLink ){
6067       if( zDel==0 ){
6068         zDel = sqlite3_malloc(nOut);
6069         if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
6070       }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
6071         rc = SQLITE_CANTOPEN_BKPT;
6072       }
6073 
6074       if( rc==SQLITE_OK ){
6075         nByte = osReadlink(zIn, zDel, nOut-1);
6076         if( nByte<0 ){
6077           rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
6078         }else{
6079           if( zDel[0]!='/' ){
6080             int n;
6081             for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
6082             if( nByte+n+1>nOut ){
6083               rc = SQLITE_CANTOPEN_BKPT;
6084             }else{
6085               memmove(&zDel[n], zDel, nByte+1);
6086               memcpy(zDel, zIn, n);
6087               nByte += n;
6088             }
6089           }
6090           zDel[nByte] = '\0';
6091         }
6092       }
6093 
6094       zIn = zDel;
6095     }
6096 
6097     assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
6098     if( rc==SQLITE_OK && zIn!=zOut ){
6099       rc = mkFullPathname(zIn, zOut, nOut);
6100     }
6101     if( bLink==0 ) break;
6102     zIn = zOut;
6103   }while( rc==SQLITE_OK );
6104 
6105   sqlite3_free(zDel);
6106   return rc;
6107 #endif   /* HAVE_READLINK && HAVE_LSTAT */
6108 }
6109 
6110 
6111 #ifndef SQLITE_OMIT_LOAD_EXTENSION
6112 /*
6113 ** Interfaces for opening a shared library, finding entry points
6114 ** within the shared library, and closing the shared library.
6115 */
6116 #include <dlfcn.h>
unixDlOpen(sqlite3_vfs * NotUsed,const char * zFilename)6117 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
6118   UNUSED_PARAMETER(NotUsed);
6119   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
6120 }
6121 
6122 /*
6123 ** SQLite calls this function immediately after a call to unixDlSym() or
6124 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
6125 ** message is available, it is written to zBufOut. If no error message
6126 ** is available, zBufOut is left unmodified and SQLite uses a default
6127 ** error message.
6128 */
unixDlError(sqlite3_vfs * NotUsed,int nBuf,char * zBufOut)6129 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
6130   const char *zErr;
6131   UNUSED_PARAMETER(NotUsed);
6132   unixEnterMutex();
6133   zErr = dlerror();
6134   if( zErr ){
6135     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
6136   }
6137   unixLeaveMutex();
6138 }
unixDlSym(sqlite3_vfs * NotUsed,void * p,const char * zSym)6139 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
6140   /*
6141   ** GCC with -pedantic-errors says that C90 does not allow a void* to be
6142   ** cast into a pointer to a function.  And yet the library dlsym() routine
6143   ** returns a void* which is really a pointer to a function.  So how do we
6144   ** use dlsym() with -pedantic-errors?
6145   **
6146   ** Variable x below is defined to be a pointer to a function taking
6147   ** parameters void* and const char* and returning a pointer to a function.
6148   ** We initialize x by assigning it a pointer to the dlsym() function.
6149   ** (That assignment requires a cast.)  Then we call the function that
6150   ** x points to.
6151   **
6152   ** This work-around is unlikely to work correctly on any system where
6153   ** you really cannot cast a function pointer into void*.  But then, on the
6154   ** other hand, dlsym() will not work on such a system either, so we have
6155   ** not really lost anything.
6156   */
6157   void (*(*x)(void*,const char*))(void);
6158   UNUSED_PARAMETER(NotUsed);
6159   x = (void(*(*)(void*,const char*))(void))dlsym;
6160   return (*x)(p, zSym);
6161 }
unixDlClose(sqlite3_vfs * NotUsed,void * pHandle)6162 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
6163   UNUSED_PARAMETER(NotUsed);
6164   dlclose(pHandle);
6165 }
6166 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
6167   #define unixDlOpen  0
6168   #define unixDlError 0
6169   #define unixDlSym   0
6170   #define unixDlClose 0
6171 #endif
6172 
6173 /*
6174 ** Write nBuf bytes of random data to the supplied buffer zBuf.
6175 */
unixRandomness(sqlite3_vfs * NotUsed,int nBuf,char * zBuf)6176 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
6177   UNUSED_PARAMETER(NotUsed);
6178   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
6179 
6180   /* We have to initialize zBuf to prevent valgrind from reporting
6181   ** errors.  The reports issued by valgrind are incorrect - we would
6182   ** prefer that the randomness be increased by making use of the
6183   ** uninitialized space in zBuf - but valgrind errors tend to worry
6184   ** some users.  Rather than argue, it seems easier just to initialize
6185   ** the whole array and silence valgrind, even if that means less randomness
6186   ** in the random seed.
6187   **
6188   ** When testing, initializing zBuf[] to zero is all we do.  That means
6189   ** that we always use the same random number sequence.  This makes the
6190   ** tests repeatable.
6191   */
6192   memset(zBuf, 0, nBuf);
6193   randomnessPid = osGetpid(0);
6194 #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
6195   {
6196     int fd, got;
6197     fd = robust_open("/dev/urandom", O_RDONLY, 0);
6198     if( fd<0 ){
6199       time_t t;
6200       time(&t);
6201       memcpy(zBuf, &t, sizeof(t));
6202       memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
6203       assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
6204       nBuf = sizeof(t) + sizeof(randomnessPid);
6205     }else{
6206       do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
6207       robust_close(0, fd, __LINE__);
6208     }
6209   }
6210 #endif
6211   return nBuf;
6212 }
6213 
6214 
6215 /*
6216 ** Sleep for a little while.  Return the amount of time slept.
6217 ** The argument is the number of microseconds we want to sleep.
6218 ** The return value is the number of microseconds of sleep actually
6219 ** requested from the underlying operating system, a number which
6220 ** might be greater than or equal to the argument, but not less
6221 ** than the argument.
6222 */
unixSleep(sqlite3_vfs * NotUsed,int microseconds)6223 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
6224 #if OS_VXWORKS
6225   struct timespec sp;
6226 
6227   sp.tv_sec = microseconds / 1000000;
6228   sp.tv_nsec = (microseconds % 1000000) * 1000;
6229   nanosleep(&sp, NULL);
6230   UNUSED_PARAMETER(NotUsed);
6231   return microseconds;
6232 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
6233   usleep(microseconds);
6234   UNUSED_PARAMETER(NotUsed);
6235   return microseconds;
6236 #else
6237   int seconds = (microseconds+999999)/1000000;
6238   sleep(seconds);
6239   UNUSED_PARAMETER(NotUsed);
6240   return seconds*1000000;
6241 #endif
6242 }
6243 
6244 /*
6245 ** The following variable, if set to a non-zero value, is interpreted as
6246 ** the number of seconds since 1970 and is used to set the result of
6247 ** sqlite3OsCurrentTime() during testing.
6248 */
6249 #ifdef SQLITE_TEST
6250 int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
6251 #endif
6252 
6253 /*
6254 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
6255 ** the current time and date as a Julian Day number times 86_400_000.  In
6256 ** other words, write into *piNow the number of milliseconds since the Julian
6257 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
6258 ** proleptic Gregorian calendar.
6259 **
6260 ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
6261 ** cannot be found.
6262 */
unixCurrentTimeInt64(sqlite3_vfs * NotUsed,sqlite3_int64 * piNow)6263 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
6264   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
6265   int rc = SQLITE_OK;
6266 #if defined(NO_GETTOD)
6267   time_t t;
6268   time(&t);
6269   *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
6270 #elif OS_VXWORKS
6271   struct timespec sNow;
6272   clock_gettime(CLOCK_REALTIME, &sNow);
6273   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
6274 #else
6275   struct timeval sNow;
6276   (void)gettimeofday(&sNow, 0);  /* Cannot fail given valid arguments */
6277   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
6278 #endif
6279 
6280 #ifdef SQLITE_TEST
6281   if( sqlite3_current_time ){
6282     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
6283   }
6284 #endif
6285   UNUSED_PARAMETER(NotUsed);
6286   return rc;
6287 }
6288 
6289 #ifndef SQLITE_OMIT_DEPRECATED
6290 /*
6291 ** Find the current time (in Universal Coordinated Time).  Write the
6292 ** current time and date as a Julian Day number into *prNow and
6293 ** return 0.  Return 1 if the time and date cannot be found.
6294 */
unixCurrentTime(sqlite3_vfs * NotUsed,double * prNow)6295 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
6296   sqlite3_int64 i = 0;
6297   int rc;
6298   UNUSED_PARAMETER(NotUsed);
6299   rc = unixCurrentTimeInt64(0, &i);
6300   *prNow = i/86400000.0;
6301   return rc;
6302 }
6303 #else
6304 # define unixCurrentTime 0
6305 #endif
6306 
6307 /*
6308 ** The xGetLastError() method is designed to return a better
6309 ** low-level error message when operating-system problems come up
6310 ** during SQLite operation.  Only the integer return code is currently
6311 ** used.
6312 */
unixGetLastError(sqlite3_vfs * NotUsed,int NotUsed2,char * NotUsed3)6313 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
6314   UNUSED_PARAMETER(NotUsed);
6315   UNUSED_PARAMETER(NotUsed2);
6316   UNUSED_PARAMETER(NotUsed3);
6317   return errno;
6318 }
6319 
6320 
6321 /*
6322 ************************ End of sqlite3_vfs methods ***************************
6323 ******************************************************************************/
6324 
6325 /******************************************************************************
6326 ************************** Begin Proxy Locking ********************************
6327 **
6328 ** Proxy locking is a "uber-locking-method" in this sense:  It uses the
6329 ** other locking methods on secondary lock files.  Proxy locking is a
6330 ** meta-layer over top of the primitive locking implemented above.  For
6331 ** this reason, the division that implements of proxy locking is deferred
6332 ** until late in the file (here) after all of the other I/O methods have
6333 ** been defined - so that the primitive locking methods are available
6334 ** as services to help with the implementation of proxy locking.
6335 **
6336 ****
6337 **
6338 ** The default locking schemes in SQLite use byte-range locks on the
6339 ** database file to coordinate safe, concurrent access by multiple readers
6340 ** and writers [http://sqlite.org/lockingv3.html].  The five file locking
6341 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
6342 ** as POSIX read & write locks over fixed set of locations (via fsctl),
6343 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
6344 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
6345 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
6346 ** address in the shared range is taken for a SHARED lock, the entire
6347 ** shared range is taken for an EXCLUSIVE lock):
6348 **
6349 **      PENDING_BYTE        0x40000000
6350 **      RESERVED_BYTE       0x40000001
6351 **      SHARED_RANGE        0x40000002 -> 0x40000200
6352 **
6353 ** This works well on the local file system, but shows a nearly 100x
6354 ** slowdown in read performance on AFP because the AFP client disables
6355 ** the read cache when byte-range locks are present.  Enabling the read
6356 ** cache exposes a cache coherency problem that is present on all OS X
6357 ** supported network file systems.  NFS and AFP both observe the
6358 ** close-to-open semantics for ensuring cache coherency
6359 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
6360 ** address the requirements for concurrent database access by multiple
6361 ** readers and writers
6362 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
6363 **
6364 ** To address the performance and cache coherency issues, proxy file locking
6365 ** changes the way database access is controlled by limiting access to a
6366 ** single host at a time and moving file locks off of the database file
6367 ** and onto a proxy file on the local file system.
6368 **
6369 **
6370 ** Using proxy locks
6371 ** -----------------
6372 **
6373 ** C APIs
6374 **
6375 **  sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
6376 **                       <proxy_path> | ":auto:");
6377 **  sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
6378 **                       &<proxy_path>);
6379 **
6380 **
6381 ** SQL pragmas
6382 **
6383 **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
6384 **  PRAGMA [database.]lock_proxy_file
6385 **
6386 ** Specifying ":auto:" means that if there is a conch file with a matching
6387 ** host ID in it, the proxy path in the conch file will be used, otherwise
6388 ** a proxy path based on the user's temp dir
6389 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
6390 ** actual proxy file name is generated from the name and path of the
6391 ** database file.  For example:
6392 **
6393 **       For database path "/Users/me/foo.db"
6394 **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
6395 **
6396 ** Once a lock proxy is configured for a database connection, it can not
6397 ** be removed, however it may be switched to a different proxy path via
6398 ** the above APIs (assuming the conch file is not being held by another
6399 ** connection or process).
6400 **
6401 **
6402 ** How proxy locking works
6403 ** -----------------------
6404 **
6405 ** Proxy file locking relies primarily on two new supporting files:
6406 **
6407 **   *  conch file to limit access to the database file to a single host
6408 **      at a time
6409 **
6410 **   *  proxy file to act as a proxy for the advisory locks normally
6411 **      taken on the database
6412 **
6413 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
6414 ** by taking an sqlite-style shared lock on the conch file, reading the
6415 ** contents and comparing the host's unique host ID (see below) and lock
6416 ** proxy path against the values stored in the conch.  The conch file is
6417 ** stored in the same directory as the database file and the file name
6418 ** is patterned after the database file name as ".<databasename>-conch".
6419 ** If the conch file does not exist, or its contents do not match the
6420 ** host ID and/or proxy path, then the lock is escalated to an exclusive
6421 ** lock and the conch file contents is updated with the host ID and proxy
6422 ** path and the lock is downgraded to a shared lock again.  If the conch
6423 ** is held by another process (with a shared lock), the exclusive lock
6424 ** will fail and SQLITE_BUSY is returned.
6425 **
6426 ** The proxy file - a single-byte file used for all advisory file locks
6427 ** normally taken on the database file.   This allows for safe sharing
6428 ** of the database file for multiple readers and writers on the same
6429 ** host (the conch ensures that they all use the same local lock file).
6430 **
6431 ** Requesting the lock proxy does not immediately take the conch, it is
6432 ** only taken when the first request to lock database file is made.
6433 ** This matches the semantics of the traditional locking behavior, where
6434 ** opening a connection to a database file does not take a lock on it.
6435 ** The shared lock and an open file descriptor are maintained until
6436 ** the connection to the database is closed.
6437 **
6438 ** The proxy file and the lock file are never deleted so they only need
6439 ** to be created the first time they are used.
6440 **
6441 ** Configuration options
6442 ** ---------------------
6443 **
6444 **  SQLITE_PREFER_PROXY_LOCKING
6445 **
6446 **       Database files accessed on non-local file systems are
6447 **       automatically configured for proxy locking, lock files are
6448 **       named automatically using the same logic as
6449 **       PRAGMA lock_proxy_file=":auto:"
6450 **
6451 **  SQLITE_PROXY_DEBUG
6452 **
6453 **       Enables the logging of error messages during host id file
6454 **       retrieval and creation
6455 **
6456 **  LOCKPROXYDIR
6457 **
6458 **       Overrides the default directory used for lock proxy files that
6459 **       are named automatically via the ":auto:" setting
6460 **
6461 **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
6462 **
6463 **       Permissions to use when creating a directory for storing the
6464 **       lock proxy files, only used when LOCKPROXYDIR is not set.
6465 **
6466 **
6467 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
6468 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
6469 ** force proxy locking to be used for every database file opened, and 0
6470 ** will force automatic proxy locking to be disabled for all database
6471 ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
6472 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
6473 */
6474 
6475 /*
6476 ** Proxy locking is only available on MacOSX
6477 */
6478 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
6479 
6480 /*
6481 ** The proxyLockingContext has the path and file structures for the remote
6482 ** and local proxy files in it
6483 */
6484 typedef struct proxyLockingContext proxyLockingContext;
6485 struct proxyLockingContext {
6486   unixFile *conchFile;         /* Open conch file */
6487   char *conchFilePath;         /* Name of the conch file */
6488   unixFile *lockProxy;         /* Open proxy lock file */
6489   char *lockProxyPath;         /* Name of the proxy lock file */
6490   char *dbPath;                /* Name of the open file */
6491   int conchHeld;               /* 1 if the conch is held, -1 if lockless */
6492   int nFails;                  /* Number of conch taking failures */
6493   void *oldLockingContext;     /* Original lockingcontext to restore on close */
6494   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
6495 };
6496 
6497 /*
6498 ** The proxy lock file path for the database at dbPath is written into lPath,
6499 ** which must point to valid, writable memory large enough for a maxLen length
6500 ** file path.
6501 */
proxyGetLockPath(const char * dbPath,char * lPath,size_t maxLen)6502 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
6503   int len;
6504   int dbLen;
6505   int i;
6506 
6507 #ifdef LOCKPROXYDIR
6508   len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
6509 #else
6510 # ifdef _CS_DARWIN_USER_TEMP_DIR
6511   {
6512     if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
6513       OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
6514                lPath, errno, osGetpid(0)));
6515       return SQLITE_IOERR_LOCK;
6516     }
6517     len = strlcat(lPath, "sqliteplocks", maxLen);
6518   }
6519 # else
6520   len = strlcpy(lPath, "/tmp/", maxLen);
6521 # endif
6522 #endif
6523 
6524   if( lPath[len-1]!='/' ){
6525     len = strlcat(lPath, "/", maxLen);
6526   }
6527 
6528   /* transform the db path to a unique cache name */
6529   dbLen = (int)strlen(dbPath);
6530   for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
6531     char c = dbPath[i];
6532     lPath[i+len] = (c=='/')?'_':c;
6533   }
6534   lPath[i+len]='\0';
6535   strlcat(lPath, ":auto:", maxLen);
6536   OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
6537   return SQLITE_OK;
6538 }
6539 
6540 /*
6541  ** Creates the lock file and any missing directories in lockPath
6542  */
proxyCreateLockPath(const char * lockPath)6543 static int proxyCreateLockPath(const char *lockPath){
6544   int i, len;
6545   char buf[MAXPATHLEN];
6546   int start = 0;
6547 
6548   assert(lockPath!=NULL);
6549   /* try to create all the intermediate directories */
6550   len = (int)strlen(lockPath);
6551   buf[0] = lockPath[0];
6552   for( i=1; i<len; i++ ){
6553     if( lockPath[i] == '/' && (i - start > 0) ){
6554       /* only mkdir if leaf dir != "." or "/" or ".." */
6555       if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
6556          || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
6557         buf[i]='\0';
6558         if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
6559           int err=errno;
6560           if( err!=EEXIST ) {
6561             OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
6562                      "'%s' proxy lock path=%s pid=%d\n",
6563                      buf, strerror(err), lockPath, osGetpid(0)));
6564             return err;
6565           }
6566         }
6567       }
6568       start=i+1;
6569     }
6570     buf[i] = lockPath[i];
6571   }
6572   OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
6573   return 0;
6574 }
6575 
6576 /*
6577 ** Create a new VFS file descriptor (stored in memory obtained from
6578 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
6579 **
6580 ** The caller is responsible not only for closing the file descriptor
6581 ** but also for freeing the memory associated with the file descriptor.
6582 */
proxyCreateUnixFile(const char * path,unixFile ** ppFile,int islockfile)6583 static int proxyCreateUnixFile(
6584     const char *path,        /* path for the new unixFile */
6585     unixFile **ppFile,       /* unixFile created and returned by ref */
6586     int islockfile           /* if non zero missing dirs will be created */
6587 ) {
6588   int fd = -1;
6589   unixFile *pNew;
6590   int rc = SQLITE_OK;
6591   int openFlags = O_RDWR | O_CREAT;
6592   sqlite3_vfs dummyVfs;
6593   int terrno = 0;
6594   UnixUnusedFd *pUnused = NULL;
6595 
6596   /* 1. first try to open/create the file
6597   ** 2. if that fails, and this is a lock file (not-conch), try creating
6598   ** the parent directories and then try again.
6599   ** 3. if that fails, try to open the file read-only
6600   ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
6601   */
6602   pUnused = findReusableFd(path, openFlags);
6603   if( pUnused ){
6604     fd = pUnused->fd;
6605   }else{
6606     pUnused = sqlite3_malloc64(sizeof(*pUnused));
6607     if( !pUnused ){
6608       return SQLITE_NOMEM_BKPT;
6609     }
6610   }
6611   if( fd<0 ){
6612     fd = robust_open(path, openFlags, 0);
6613     terrno = errno;
6614     if( fd<0 && errno==ENOENT && islockfile ){
6615       if( proxyCreateLockPath(path) == SQLITE_OK ){
6616         fd = robust_open(path, openFlags, 0);
6617       }
6618     }
6619   }
6620   if( fd<0 ){
6621     openFlags = O_RDONLY;
6622     fd = robust_open(path, openFlags, 0);
6623     terrno = errno;
6624   }
6625   if( fd<0 ){
6626     if( islockfile ){
6627       return SQLITE_BUSY;
6628     }
6629     switch (terrno) {
6630       case EACCES:
6631         return SQLITE_PERM;
6632       case EIO:
6633         return SQLITE_IOERR_LOCK; /* even though it is the conch */
6634       default:
6635         return SQLITE_CANTOPEN_BKPT;
6636     }
6637   }
6638 
6639   pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
6640   if( pNew==NULL ){
6641     rc = SQLITE_NOMEM_BKPT;
6642     goto end_create_proxy;
6643   }
6644   memset(pNew, 0, sizeof(unixFile));
6645   pNew->openFlags = openFlags;
6646   memset(&dummyVfs, 0, sizeof(dummyVfs));
6647   dummyVfs.pAppData = (void*)&autolockIoFinder;
6648   dummyVfs.zName = "dummy";
6649   pUnused->fd = fd;
6650   pUnused->flags = openFlags;
6651   pNew->pUnused = pUnused;
6652 
6653   rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
6654   if( rc==SQLITE_OK ){
6655     *ppFile = pNew;
6656     return SQLITE_OK;
6657   }
6658 end_create_proxy:
6659   robust_close(pNew, fd, __LINE__);
6660   sqlite3_free(pNew);
6661   sqlite3_free(pUnused);
6662   return rc;
6663 }
6664 
6665 #ifdef SQLITE_TEST
6666 /* simulate multiple hosts by creating unique hostid file paths */
6667 int sqlite3_hostid_num = 0;
6668 #endif
6669 
6670 #define PROXY_HOSTIDLEN    16  /* conch file host id length */
6671 
6672 #ifdef HAVE_GETHOSTUUID
6673 /* Not always defined in the headers as it ought to be */
6674 extern int gethostuuid(uuid_t id, const struct timespec *wait);
6675 #endif
6676 
6677 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
6678 ** bytes of writable memory.
6679 */
proxyGetHostID(unsigned char * pHostID,int * pError)6680 static int proxyGetHostID(unsigned char *pHostID, int *pError){
6681   assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
6682   memset(pHostID, 0, PROXY_HOSTIDLEN);
6683 #ifdef HAVE_GETHOSTUUID
6684   {
6685     struct timespec timeout = {1, 0}; /* 1 sec timeout */
6686     if( gethostuuid(pHostID, &timeout) ){
6687       int err = errno;
6688       if( pError ){
6689         *pError = err;
6690       }
6691       return SQLITE_IOERR;
6692     }
6693   }
6694 #else
6695   UNUSED_PARAMETER(pError);
6696 #endif
6697 #ifdef SQLITE_TEST
6698   /* simulate multiple hosts by creating unique hostid file paths */
6699   if( sqlite3_hostid_num != 0){
6700     pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
6701   }
6702 #endif
6703 
6704   return SQLITE_OK;
6705 }
6706 
6707 /* The conch file contains the header, host id and lock file path
6708  */
6709 #define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
6710 #define PROXY_HEADERLEN    1   /* conch file header length */
6711 #define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
6712 #define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
6713 
6714 /*
6715 ** Takes an open conch file, copies the contents to a new path and then moves
6716 ** it back.  The newly created file's file descriptor is assigned to the
6717 ** conch file structure and finally the original conch file descriptor is
6718 ** closed.  Returns zero if successful.
6719 */
proxyBreakConchLock(unixFile * pFile,uuid_t myHostID)6720 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
6721   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
6722   unixFile *conchFile = pCtx->conchFile;
6723   char tPath[MAXPATHLEN];
6724   char buf[PROXY_MAXCONCHLEN];
6725   char *cPath = pCtx->conchFilePath;
6726   size_t readLen = 0;
6727   size_t pathLen = 0;
6728   char errmsg[64] = "";
6729   int fd = -1;
6730   int rc = -1;
6731   UNUSED_PARAMETER(myHostID);
6732 
6733   /* create a new path by replace the trailing '-conch' with '-break' */
6734   pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
6735   if( pathLen>MAXPATHLEN || pathLen<6 ||
6736      (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
6737     sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
6738     goto end_breaklock;
6739   }
6740   /* read the conch content */
6741   readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
6742   if( readLen<PROXY_PATHINDEX ){
6743     sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
6744     goto end_breaklock;
6745   }
6746   /* write it out to the temporary break file */
6747   fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
6748   if( fd<0 ){
6749     sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
6750     goto end_breaklock;
6751   }
6752   if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
6753     sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
6754     goto end_breaklock;
6755   }
6756   if( rename(tPath, cPath) ){
6757     sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
6758     goto end_breaklock;
6759   }
6760   rc = 0;
6761   fprintf(stderr, "broke stale lock on %s\n", cPath);
6762   robust_close(pFile, conchFile->h, __LINE__);
6763   conchFile->h = fd;
6764   conchFile->openFlags = O_RDWR | O_CREAT;
6765 
6766 end_breaklock:
6767   if( rc ){
6768     if( fd>=0 ){
6769       osUnlink(tPath);
6770       robust_close(pFile, fd, __LINE__);
6771     }
6772     fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
6773   }
6774   return rc;
6775 }
6776 
6777 /* Take the requested lock on the conch file and break a stale lock if the
6778 ** host id matches.
6779 */
proxyConchLock(unixFile * pFile,uuid_t myHostID,int lockType)6780 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
6781   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
6782   unixFile *conchFile = pCtx->conchFile;
6783   int rc = SQLITE_OK;
6784   int nTries = 0;
6785   struct timespec conchModTime;
6786 
6787   memset(&conchModTime, 0, sizeof(conchModTime));
6788   do {
6789     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
6790     nTries ++;
6791     if( rc==SQLITE_BUSY ){
6792       /* If the lock failed (busy):
6793        * 1st try: get the mod time of the conch, wait 0.5s and try again.
6794        * 2nd try: fail if the mod time changed or host id is different, wait
6795        *           10 sec and try again
6796        * 3rd try: break the lock unless the mod time has changed.
6797        */
6798       struct stat buf;
6799       if( osFstat(conchFile->h, &buf) ){
6800         storeLastErrno(pFile, errno);
6801         return SQLITE_IOERR_LOCK;
6802       }
6803 
6804       if( nTries==1 ){
6805         conchModTime = buf.st_mtimespec;
6806         usleep(500000); /* wait 0.5 sec and try the lock again*/
6807         continue;
6808       }
6809 
6810       assert( nTries>1 );
6811       if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
6812          conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
6813         return SQLITE_BUSY;
6814       }
6815 
6816       if( nTries==2 ){
6817         char tBuf[PROXY_MAXCONCHLEN];
6818         int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
6819         if( len<0 ){
6820           storeLastErrno(pFile, errno);
6821           return SQLITE_IOERR_LOCK;
6822         }
6823         if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
6824           /* don't break the lock if the host id doesn't match */
6825           if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
6826             return SQLITE_BUSY;
6827           }
6828         }else{
6829           /* don't break the lock on short read or a version mismatch */
6830           return SQLITE_BUSY;
6831         }
6832         usleep(10000000); /* wait 10 sec and try the lock again */
6833         continue;
6834       }
6835 
6836       assert( nTries==3 );
6837       if( 0==proxyBreakConchLock(pFile, myHostID) ){
6838         rc = SQLITE_OK;
6839         if( lockType==EXCLUSIVE_LOCK ){
6840           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
6841         }
6842         if( !rc ){
6843           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
6844         }
6845       }
6846     }
6847   } while( rc==SQLITE_BUSY && nTries<3 );
6848 
6849   return rc;
6850 }
6851 
6852 /* Takes the conch by taking a shared lock and read the contents conch, if
6853 ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL
6854 ** lockPath means that the lockPath in the conch file will be used if the
6855 ** host IDs match, or a new lock path will be generated automatically
6856 ** and written to the conch file.
6857 */
proxyTakeConch(unixFile * pFile)6858 static int proxyTakeConch(unixFile *pFile){
6859   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
6860 
6861   if( pCtx->conchHeld!=0 ){
6862     return SQLITE_OK;
6863   }else{
6864     unixFile *conchFile = pCtx->conchFile;
6865     uuid_t myHostID;
6866     int pError = 0;
6867     char readBuf[PROXY_MAXCONCHLEN];
6868     char lockPath[MAXPATHLEN];
6869     char *tempLockPath = NULL;
6870     int rc = SQLITE_OK;
6871     int createConch = 0;
6872     int hostIdMatch = 0;
6873     int readLen = 0;
6874     int tryOldLockPath = 0;
6875     int forceNewLockPath = 0;
6876 
6877     OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
6878              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
6879              osGetpid(0)));
6880 
6881     rc = proxyGetHostID(myHostID, &pError);
6882     if( (rc&0xff)==SQLITE_IOERR ){
6883       storeLastErrno(pFile, pError);
6884       goto end_takeconch;
6885     }
6886     rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
6887     if( rc!=SQLITE_OK ){
6888       goto end_takeconch;
6889     }
6890     /* read the existing conch file */
6891     readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
6892     if( readLen<0 ){
6893       /* I/O error: lastErrno set by seekAndRead */
6894       storeLastErrno(pFile, conchFile->lastErrno);
6895       rc = SQLITE_IOERR_READ;
6896       goto end_takeconch;
6897     }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
6898              readBuf[0]!=(char)PROXY_CONCHVERSION ){
6899       /* a short read or version format mismatch means we need to create a new
6900       ** conch file.
6901       */
6902       createConch = 1;
6903     }
6904     /* if the host id matches and the lock path already exists in the conch
6905     ** we'll try to use the path there, if we can't open that path, we'll
6906     ** retry with a new auto-generated path
6907     */
6908     do { /* in case we need to try again for an :auto: named lock file */
6909 
6910       if( !createConch && !forceNewLockPath ){
6911         hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
6912                                   PROXY_HOSTIDLEN);
6913         /* if the conch has data compare the contents */
6914         if( !pCtx->lockProxyPath ){
6915           /* for auto-named local lock file, just check the host ID and we'll
6916            ** use the local lock file path that's already in there
6917            */
6918           if( hostIdMatch ){
6919             size_t pathLen = (readLen - PROXY_PATHINDEX);
6920 
6921             if( pathLen>=MAXPATHLEN ){
6922               pathLen=MAXPATHLEN-1;
6923             }
6924             memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
6925             lockPath[pathLen] = 0;
6926             tempLockPath = lockPath;
6927             tryOldLockPath = 1;
6928             /* create a copy of the lock path if the conch is taken */
6929             goto end_takeconch;
6930           }
6931         }else if( hostIdMatch
6932                && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
6933                            readLen-PROXY_PATHINDEX)
6934         ){
6935           /* conch host and lock path match */
6936           goto end_takeconch;
6937         }
6938       }
6939 
6940       /* if the conch isn't writable and doesn't match, we can't take it */
6941       if( (conchFile->openFlags&O_RDWR) == 0 ){
6942         rc = SQLITE_BUSY;
6943         goto end_takeconch;
6944       }
6945 
6946       /* either the conch didn't match or we need to create a new one */
6947       if( !pCtx->lockProxyPath ){
6948         proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
6949         tempLockPath = lockPath;
6950         /* create a copy of the lock path _only_ if the conch is taken */
6951       }
6952 
6953       /* update conch with host and path (this will fail if other process
6954       ** has a shared lock already), if the host id matches, use the big
6955       ** stick.
6956       */
6957       futimes(conchFile->h, NULL);
6958       if( hostIdMatch && !createConch ){
6959         if( conchFile->pInode && conchFile->pInode->nShared>1 ){
6960           /* We are trying for an exclusive lock but another thread in this
6961            ** same process is still holding a shared lock. */
6962           rc = SQLITE_BUSY;
6963         } else {
6964           rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
6965         }
6966       }else{
6967         rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
6968       }
6969       if( rc==SQLITE_OK ){
6970         char writeBuffer[PROXY_MAXCONCHLEN];
6971         int writeSize = 0;
6972 
6973         writeBuffer[0] = (char)PROXY_CONCHVERSION;
6974         memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
6975         if( pCtx->lockProxyPath!=NULL ){
6976           strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
6977                   MAXPATHLEN);
6978         }else{
6979           strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
6980         }
6981         writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
6982         robust_ftruncate(conchFile->h, writeSize);
6983         rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
6984         full_fsync(conchFile->h,0,0);
6985         /* If we created a new conch file (not just updated the contents of a
6986          ** valid conch file), try to match the permissions of the database
6987          */
6988         if( rc==SQLITE_OK && createConch ){
6989           struct stat buf;
6990           int err = osFstat(pFile->h, &buf);
6991           if( err==0 ){
6992             mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
6993                                         S_IROTH|S_IWOTH);
6994             /* try to match the database file R/W permissions, ignore failure */
6995 #ifndef SQLITE_PROXY_DEBUG
6996             osFchmod(conchFile->h, cmode);
6997 #else
6998             do{
6999               rc = osFchmod(conchFile->h, cmode);
7000             }while( rc==(-1) && errno==EINTR );
7001             if( rc!=0 ){
7002               int code = errno;
7003               fprintf(stderr, "fchmod %o FAILED with %d %s\n",
7004                       cmode, code, strerror(code));
7005             } else {
7006               fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
7007             }
7008           }else{
7009             int code = errno;
7010             fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
7011                     err, code, strerror(code));
7012 #endif
7013           }
7014         }
7015       }
7016       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
7017 
7018     end_takeconch:
7019       OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
7020       if( rc==SQLITE_OK && pFile->openFlags ){
7021         int fd;
7022         if( pFile->h>=0 ){
7023           robust_close(pFile, pFile->h, __LINE__);
7024         }
7025         pFile->h = -1;
7026         fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
7027         OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
7028         if( fd>=0 ){
7029           pFile->h = fd;
7030         }else{
7031           rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
7032            during locking */
7033         }
7034       }
7035       if( rc==SQLITE_OK && !pCtx->lockProxy ){
7036         char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
7037         rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
7038         if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
7039           /* we couldn't create the proxy lock file with the old lock file path
7040            ** so try again via auto-naming
7041            */
7042           forceNewLockPath = 1;
7043           tryOldLockPath = 0;
7044           continue; /* go back to the do {} while start point, try again */
7045         }
7046       }
7047       if( rc==SQLITE_OK ){
7048         /* Need to make a copy of path if we extracted the value
7049          ** from the conch file or the path was allocated on the stack
7050          */
7051         if( tempLockPath ){
7052           pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
7053           if( !pCtx->lockProxyPath ){
7054             rc = SQLITE_NOMEM_BKPT;
7055           }
7056         }
7057       }
7058       if( rc==SQLITE_OK ){
7059         pCtx->conchHeld = 1;
7060 
7061         if( pCtx->lockProxy->pMethod == &afpIoMethods ){
7062           afpLockingContext *afpCtx;
7063           afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
7064           afpCtx->dbPath = pCtx->lockProxyPath;
7065         }
7066       } else {
7067         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
7068       }
7069       OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
7070                rc==SQLITE_OK?"ok":"failed"));
7071       return rc;
7072     } while (1); /* in case we need to retry the :auto: lock file -
7073                  ** we should never get here except via the 'continue' call. */
7074   }
7075 }
7076 
7077 /*
7078 ** If pFile holds a lock on a conch file, then release that lock.
7079 */
proxyReleaseConch(unixFile * pFile)7080 static int proxyReleaseConch(unixFile *pFile){
7081   int rc = SQLITE_OK;         /* Subroutine return code */
7082   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
7083   unixFile *conchFile;        /* Name of the conch file */
7084 
7085   pCtx = (proxyLockingContext *)pFile->lockingContext;
7086   conchFile = pCtx->conchFile;
7087   OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
7088            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
7089            osGetpid(0)));
7090   if( pCtx->conchHeld>0 ){
7091     rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
7092   }
7093   pCtx->conchHeld = 0;
7094   OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
7095            (rc==SQLITE_OK ? "ok" : "failed")));
7096   return rc;
7097 }
7098 
7099 /*
7100 ** Given the name of a database file, compute the name of its conch file.
7101 ** Store the conch filename in memory obtained from sqlite3_malloc64().
7102 ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
7103 ** or SQLITE_NOMEM if unable to obtain memory.
7104 **
7105 ** The caller is responsible for ensuring that the allocated memory
7106 ** space is eventually freed.
7107 **
7108 ** *pConchPath is set to NULL if a memory allocation error occurs.
7109 */
proxyCreateConchPathname(char * dbPath,char ** pConchPath)7110 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
7111   int i;                        /* Loop counter */
7112   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
7113   char *conchPath;              /* buffer in which to construct conch name */
7114 
7115   /* Allocate space for the conch filename and initialize the name to
7116   ** the name of the original database file. */
7117   *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
7118   if( conchPath==0 ){
7119     return SQLITE_NOMEM_BKPT;
7120   }
7121   memcpy(conchPath, dbPath, len+1);
7122 
7123   /* now insert a "." before the last / character */
7124   for( i=(len-1); i>=0; i-- ){
7125     if( conchPath[i]=='/' ){
7126       i++;
7127       break;
7128     }
7129   }
7130   conchPath[i]='.';
7131   while ( i<len ){
7132     conchPath[i+1]=dbPath[i];
7133     i++;
7134   }
7135 
7136   /* append the "-conch" suffix to the file */
7137   memcpy(&conchPath[i+1], "-conch", 7);
7138   assert( (int)strlen(conchPath) == len+7 );
7139 
7140   return SQLITE_OK;
7141 }
7142 
7143 
7144 /* Takes a fully configured proxy locking-style unix file and switches
7145 ** the local lock file path
7146 */
switchLockProxyPath(unixFile * pFile,const char * path)7147 static int switchLockProxyPath(unixFile *pFile, const char *path) {
7148   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
7149   char *oldPath = pCtx->lockProxyPath;
7150   int rc = SQLITE_OK;
7151 
7152   if( pFile->eFileLock!=NO_LOCK ){
7153     return SQLITE_BUSY;
7154   }
7155 
7156   /* nothing to do if the path is NULL, :auto: or matches the existing path */
7157   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
7158     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
7159     return SQLITE_OK;
7160   }else{
7161     unixFile *lockProxy = pCtx->lockProxy;
7162     pCtx->lockProxy=NULL;
7163     pCtx->conchHeld = 0;
7164     if( lockProxy!=NULL ){
7165       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
7166       if( rc ) return rc;
7167       sqlite3_free(lockProxy);
7168     }
7169     sqlite3_free(oldPath);
7170     pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
7171   }
7172 
7173   return rc;
7174 }
7175 
7176 /*
7177 ** pFile is a file that has been opened by a prior xOpen call.  dbPath
7178 ** is a string buffer at least MAXPATHLEN+1 characters in size.
7179 **
7180 ** This routine find the filename associated with pFile and writes it
7181 ** int dbPath.
7182 */
proxyGetDbPathForUnixFile(unixFile * pFile,char * dbPath)7183 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
7184 #if defined(__APPLE__)
7185   if( pFile->pMethod == &afpIoMethods ){
7186     /* afp style keeps a reference to the db path in the filePath field
7187     ** of the struct */
7188     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
7189     strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
7190             MAXPATHLEN);
7191   } else
7192 #endif
7193   if( pFile->pMethod == &dotlockIoMethods ){
7194     /* dot lock style uses the locking context to store the dot lock
7195     ** file path */
7196     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
7197     memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
7198   }else{
7199     /* all other styles use the locking context to store the db file path */
7200     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
7201     strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
7202   }
7203   return SQLITE_OK;
7204 }
7205 
7206 /*
7207 ** Takes an already filled in unix file and alters it so all file locking
7208 ** will be performed on the local proxy lock file.  The following fields
7209 ** are preserved in the locking context so that they can be restored and
7210 ** the unix structure properly cleaned up at close time:
7211 **  ->lockingContext
7212 **  ->pMethod
7213 */
proxyTransformUnixFile(unixFile * pFile,const char * path)7214 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
7215   proxyLockingContext *pCtx;
7216   char dbPath[MAXPATHLEN+1];       /* Name of the database file */
7217   char *lockPath=NULL;
7218   int rc = SQLITE_OK;
7219 
7220   if( pFile->eFileLock!=NO_LOCK ){
7221     return SQLITE_BUSY;
7222   }
7223   proxyGetDbPathForUnixFile(pFile, dbPath);
7224   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
7225     lockPath=NULL;
7226   }else{
7227     lockPath=(char *)path;
7228   }
7229 
7230   OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
7231            (lockPath ? lockPath : ":auto:"), osGetpid(0)));
7232 
7233   pCtx = sqlite3_malloc64( sizeof(*pCtx) );
7234   if( pCtx==0 ){
7235     return SQLITE_NOMEM_BKPT;
7236   }
7237   memset(pCtx, 0, sizeof(*pCtx));
7238 
7239   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
7240   if( rc==SQLITE_OK ){
7241     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
7242     if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
7243       /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
7244       ** (c) the file system is read-only, then enable no-locking access.
7245       ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
7246       ** that openFlags will have only one of O_RDONLY or O_RDWR.
7247       */
7248       struct statfs fsInfo;
7249       struct stat conchInfo;
7250       int goLockless = 0;
7251 
7252       if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
7253         int err = errno;
7254         if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
7255           goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
7256         }
7257       }
7258       if( goLockless ){
7259         pCtx->conchHeld = -1; /* read only FS/ lockless */
7260         rc = SQLITE_OK;
7261       }
7262     }
7263   }
7264   if( rc==SQLITE_OK && lockPath ){
7265     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
7266   }
7267 
7268   if( rc==SQLITE_OK ){
7269     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
7270     if( pCtx->dbPath==NULL ){
7271       rc = SQLITE_NOMEM_BKPT;
7272     }
7273   }
7274   if( rc==SQLITE_OK ){
7275     /* all memory is allocated, proxys are created and assigned,
7276     ** switch the locking context and pMethod then return.
7277     */
7278     pCtx->oldLockingContext = pFile->lockingContext;
7279     pFile->lockingContext = pCtx;
7280     pCtx->pOldMethod = pFile->pMethod;
7281     pFile->pMethod = &proxyIoMethods;
7282   }else{
7283     if( pCtx->conchFile ){
7284       pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
7285       sqlite3_free(pCtx->conchFile);
7286     }
7287     sqlite3DbFree(0, pCtx->lockProxyPath);
7288     sqlite3_free(pCtx->conchFilePath);
7289     sqlite3_free(pCtx);
7290   }
7291   OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
7292            (rc==SQLITE_OK ? "ok" : "failed")));
7293   return rc;
7294 }
7295 
7296 
7297 /*
7298 ** This routine handles sqlite3_file_control() calls that are specific
7299 ** to proxy locking.
7300 */
proxyFileControl(sqlite3_file * id,int op,void * pArg)7301 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
7302   switch( op ){
7303     case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
7304       unixFile *pFile = (unixFile*)id;
7305       if( pFile->pMethod == &proxyIoMethods ){
7306         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
7307         proxyTakeConch(pFile);
7308         if( pCtx->lockProxyPath ){
7309           *(const char **)pArg = pCtx->lockProxyPath;
7310         }else{
7311           *(const char **)pArg = ":auto: (not held)";
7312         }
7313       } else {
7314         *(const char **)pArg = NULL;
7315       }
7316       return SQLITE_OK;
7317     }
7318     case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
7319       unixFile *pFile = (unixFile*)id;
7320       int rc = SQLITE_OK;
7321       int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
7322       if( pArg==NULL || (const char *)pArg==0 ){
7323         if( isProxyStyle ){
7324           /* turn off proxy locking - not supported.  If support is added for
7325           ** switching proxy locking mode off then it will need to fail if
7326           ** the journal mode is WAL mode.
7327           */
7328           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
7329         }else{
7330           /* turn off proxy locking - already off - NOOP */
7331           rc = SQLITE_OK;
7332         }
7333       }else{
7334         const char *proxyPath = (const char *)pArg;
7335         if( isProxyStyle ){
7336           proxyLockingContext *pCtx =
7337             (proxyLockingContext*)pFile->lockingContext;
7338           if( !strcmp(pArg, ":auto:")
7339            || (pCtx->lockProxyPath &&
7340                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
7341           ){
7342             rc = SQLITE_OK;
7343           }else{
7344             rc = switchLockProxyPath(pFile, proxyPath);
7345           }
7346         }else{
7347           /* turn on proxy file locking */
7348           rc = proxyTransformUnixFile(pFile, proxyPath);
7349         }
7350       }
7351       return rc;
7352     }
7353     default: {
7354       assert( 0 );  /* The call assures that only valid opcodes are sent */
7355     }
7356   }
7357   /*NOTREACHED*/
7358   return SQLITE_ERROR;
7359 }
7360 
7361 /*
7362 ** Within this division (the proxying locking implementation) the procedures
7363 ** above this point are all utilities.  The lock-related methods of the
7364 ** proxy-locking sqlite3_io_method object follow.
7365 */
7366 
7367 
7368 /*
7369 ** This routine checks if there is a RESERVED lock held on the specified
7370 ** file by this or any other process. If such a lock is held, set *pResOut
7371 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
7372 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
7373 */
proxyCheckReservedLock(sqlite3_file * id,int * pResOut)7374 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
7375   unixFile *pFile = (unixFile*)id;
7376   int rc = proxyTakeConch(pFile);
7377   if( rc==SQLITE_OK ){
7378     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
7379     if( pCtx->conchHeld>0 ){
7380       unixFile *proxy = pCtx->lockProxy;
7381       return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
7382     }else{ /* conchHeld < 0 is lockless */
7383       pResOut=0;
7384     }
7385   }
7386   return rc;
7387 }
7388 
7389 /*
7390 ** Lock the file with the lock specified by parameter eFileLock - one
7391 ** of the following:
7392 **
7393 **     (1) SHARED_LOCK
7394 **     (2) RESERVED_LOCK
7395 **     (3) PENDING_LOCK
7396 **     (4) EXCLUSIVE_LOCK
7397 **
7398 ** Sometimes when requesting one lock state, additional lock states
7399 ** are inserted in between.  The locking might fail on one of the later
7400 ** transitions leaving the lock state different from what it started but
7401 ** still short of its goal.  The following chart shows the allowed
7402 ** transitions and the inserted intermediate states:
7403 **
7404 **    UNLOCKED -> SHARED
7405 **    SHARED -> RESERVED
7406 **    SHARED -> (PENDING) -> EXCLUSIVE
7407 **    RESERVED -> (PENDING) -> EXCLUSIVE
7408 **    PENDING -> EXCLUSIVE
7409 **
7410 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
7411 ** routine to lower a locking level.
7412 */
proxyLock(sqlite3_file * id,int eFileLock)7413 static int proxyLock(sqlite3_file *id, int eFileLock) {
7414   unixFile *pFile = (unixFile*)id;
7415   int rc = proxyTakeConch(pFile);
7416   if( rc==SQLITE_OK ){
7417     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
7418     if( pCtx->conchHeld>0 ){
7419       unixFile *proxy = pCtx->lockProxy;
7420       rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
7421       pFile->eFileLock = proxy->eFileLock;
7422     }else{
7423       /* conchHeld < 0 is lockless */
7424     }
7425   }
7426   return rc;
7427 }
7428 
7429 
7430 /*
7431 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
7432 ** must be either NO_LOCK or SHARED_LOCK.
7433 **
7434 ** If the locking level of the file descriptor is already at or below
7435 ** the requested locking level, this routine is a no-op.
7436 */
proxyUnlock(sqlite3_file * id,int eFileLock)7437 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
7438   unixFile *pFile = (unixFile*)id;
7439   int rc = proxyTakeConch(pFile);
7440   if( rc==SQLITE_OK ){
7441     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
7442     if( pCtx->conchHeld>0 ){
7443       unixFile *proxy = pCtx->lockProxy;
7444       rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
7445       pFile->eFileLock = proxy->eFileLock;
7446     }else{
7447       /* conchHeld < 0 is lockless */
7448     }
7449   }
7450   return rc;
7451 }
7452 
7453 /*
7454 ** Close a file that uses proxy locks.
7455 */
proxyClose(sqlite3_file * id)7456 static int proxyClose(sqlite3_file *id) {
7457   if( ALWAYS(id) ){
7458     unixFile *pFile = (unixFile*)id;
7459     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
7460     unixFile *lockProxy = pCtx->lockProxy;
7461     unixFile *conchFile = pCtx->conchFile;
7462     int rc = SQLITE_OK;
7463 
7464     if( lockProxy ){
7465       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
7466       if( rc ) return rc;
7467       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
7468       if( rc ) return rc;
7469       sqlite3_free(lockProxy);
7470       pCtx->lockProxy = 0;
7471     }
7472     if( conchFile ){
7473       if( pCtx->conchHeld ){
7474         rc = proxyReleaseConch(pFile);
7475         if( rc ) return rc;
7476       }
7477       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
7478       if( rc ) return rc;
7479       sqlite3_free(conchFile);
7480     }
7481     sqlite3DbFree(0, pCtx->lockProxyPath);
7482     sqlite3_free(pCtx->conchFilePath);
7483     sqlite3DbFree(0, pCtx->dbPath);
7484     /* restore the original locking context and pMethod then close it */
7485     pFile->lockingContext = pCtx->oldLockingContext;
7486     pFile->pMethod = pCtx->pOldMethod;
7487     sqlite3_free(pCtx);
7488     return pFile->pMethod->xClose(id);
7489   }
7490   return SQLITE_OK;
7491 }
7492 
7493 
7494 
7495 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
7496 /*
7497 ** The proxy locking style is intended for use with AFP filesystems.
7498 ** And since AFP is only supported on MacOSX, the proxy locking is also
7499 ** restricted to MacOSX.
7500 **
7501 **
7502 ******************* End of the proxy lock implementation **********************
7503 ******************************************************************************/
7504 
7505 /*
7506 ** Initialize the operating system interface.
7507 **
7508 ** This routine registers all VFS implementations for unix-like operating
7509 ** systems.  This routine, and the sqlite3_os_end() routine that follows,
7510 ** should be the only routines in this file that are visible from other
7511 ** files.
7512 **
7513 ** This routine is called once during SQLite initialization and by a
7514 ** single thread.  The memory allocation and mutex subsystems have not
7515 ** necessarily been initialized when this routine is called, and so they
7516 ** should not be used.
7517 */
sqlite3_os_init(void)7518 int sqlite3_os_init(void){
7519   /*
7520   ** The following macro defines an initializer for an sqlite3_vfs object.
7521   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
7522   ** to the "finder" function.  (pAppData is a pointer to a pointer because
7523   ** silly C90 rules prohibit a void* from being cast to a function pointer
7524   ** and so we have to go through the intermediate pointer to avoid problems
7525   ** when compiling with -pedantic-errors on GCC.)
7526   **
7527   ** The FINDER parameter to this macro is the name of the pointer to the
7528   ** finder-function.  The finder-function returns a pointer to the
7529   ** sqlite_io_methods object that implements the desired locking
7530   ** behaviors.  See the division above that contains the IOMETHODS
7531   ** macro for addition information on finder-functions.
7532   **
7533   ** Most finders simply return a pointer to a fixed sqlite3_io_methods
7534   ** object.  But the "autolockIoFinder" available on MacOSX does a little
7535   ** more than that; it looks at the filesystem type that hosts the
7536   ** database file and tries to choose an locking method appropriate for
7537   ** that filesystem time.
7538   */
7539   #define UNIXVFS(VFSNAME, FINDER) {                        \
7540     3,                    /* iVersion */                    \
7541     sizeof(unixFile),     /* szOsFile */                    \
7542     MAX_PATHNAME,         /* mxPathname */                  \
7543     0,                    /* pNext */                       \
7544     VFSNAME,              /* zName */                       \
7545     (void*)&FINDER,       /* pAppData */                    \
7546     unixOpen,             /* xOpen */                       \
7547     unixDelete,           /* xDelete */                     \
7548     unixAccess,           /* xAccess */                     \
7549     unixFullPathname,     /* xFullPathname */               \
7550     unixDlOpen,           /* xDlOpen */                     \
7551     unixDlError,          /* xDlError */                    \
7552     unixDlSym,            /* xDlSym */                      \
7553     unixDlClose,          /* xDlClose */                    \
7554     unixRandomness,       /* xRandomness */                 \
7555     unixSleep,            /* xSleep */                      \
7556     unixCurrentTime,      /* xCurrentTime */                \
7557     unixGetLastError,     /* xGetLastError */               \
7558     unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
7559     unixSetSystemCall,    /* xSetSystemCall */              \
7560     unixGetSystemCall,    /* xGetSystemCall */              \
7561     unixNextSystemCall,   /* xNextSystemCall */             \
7562   }
7563 
7564   /*
7565   ** All default VFSes for unix are contained in the following array.
7566   **
7567   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
7568   ** by the SQLite core when the VFS is registered.  So the following
7569   ** array cannot be const.
7570   */
7571   static sqlite3_vfs aVfs[] = {
7572 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
7573     UNIXVFS("unix",          autolockIoFinder ),
7574 #elif OS_VXWORKS
7575     UNIXVFS("unix",          vxworksIoFinder ),
7576 #else
7577     UNIXVFS("unix",          posixIoFinder ),
7578 #endif
7579     UNIXVFS("unix-none",     nolockIoFinder ),
7580     UNIXVFS("unix-dotfile",  dotlockIoFinder ),
7581     UNIXVFS("unix-excl",     posixIoFinder ),
7582 #if OS_VXWORKS
7583     UNIXVFS("unix-namedsem", semIoFinder ),
7584 #endif
7585 #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
7586     UNIXVFS("unix-posix",    posixIoFinder ),
7587 #endif
7588 #if SQLITE_ENABLE_LOCKING_STYLE
7589     UNIXVFS("unix-flock",    flockIoFinder ),
7590 #endif
7591 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
7592     UNIXVFS("unix-afp",      afpIoFinder ),
7593     UNIXVFS("unix-nfs",      nfsIoFinder ),
7594     UNIXVFS("unix-proxy",    proxyIoFinder ),
7595 #endif
7596   };
7597   unsigned int i;          /* Loop counter */
7598 
7599   /* Double-check that the aSyscall[] array has been constructed
7600   ** correctly.  See ticket [bb3a86e890c8e96ab] */
7601   assert( ArraySize(aSyscall)==28 );
7602 
7603   /* Register all VFSes defined in the aVfs[] array */
7604   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
7605     sqlite3_vfs_register(&aVfs[i], i==0);
7606   }
7607   return SQLITE_OK;
7608 }
7609 
7610 /*
7611 ** Shutdown the operating system interface.
7612 **
7613 ** Some operating systems might need to do some cleanup in this routine,
7614 ** to release dynamically allocated objects.  But not on unix.
7615 ** This routine is a no-op for unix.
7616 */
sqlite3_os_end(void)7617 int sqlite3_os_end(void){
7618   return SQLITE_OK;
7619 }
7620 
7621 #endif /* SQLITE_OS_UNIX */
7622