000001  /*
000002  ** 2005 November 29
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  ******************************************************************************
000012  **
000013  ** This file contains OS interface code that is common to all
000014  ** architectures.
000015  */
000016  #include "sqliteInt.h"
000017  
000018  /*
000019  ** If we compile with the SQLITE_TEST macro set, then the following block
000020  ** of code will give us the ability to simulate a disk I/O error.  This
000021  ** is used for testing the I/O recovery logic.
000022  */
000023  #if defined(SQLITE_TEST)
000024  int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
000025  int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
000026  int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
000027  int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
000028  int sqlite3_io_error_benign = 0;         /* True if errors are benign */
000029  int sqlite3_diskfull_pending = 0;
000030  int sqlite3_diskfull = 0;
000031  #endif /* defined(SQLITE_TEST) */
000032  
000033  /*
000034  ** When testing, also keep a count of the number of open files.
000035  */
000036  #if defined(SQLITE_TEST)
000037  int sqlite3_open_file_count = 0;
000038  #endif /* defined(SQLITE_TEST) */
000039  
000040  /*
000041  ** The default SQLite sqlite3_vfs implementations do not allocate
000042  ** memory (actually, os_unix.c allocates a small amount of memory
000043  ** from within OsOpen()), but some third-party implementations may.
000044  ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
000045  ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
000046  **
000047  ** The following functions are instrumented for malloc() failure
000048  ** testing:
000049  **
000050  **     sqlite3OsRead()
000051  **     sqlite3OsWrite()
000052  **     sqlite3OsSync()
000053  **     sqlite3OsFileSize()
000054  **     sqlite3OsLock()
000055  **     sqlite3OsCheckReservedLock()
000056  **     sqlite3OsFileControl()
000057  **     sqlite3OsShmMap()
000058  **     sqlite3OsOpen()
000059  **     sqlite3OsDelete()
000060  **     sqlite3OsAccess()
000061  **     sqlite3OsFullPathname()
000062  **
000063  */
000064  #if defined(SQLITE_TEST)
000065  int sqlite3_memdebug_vfs_oom_test = 1;
000066    #define DO_OS_MALLOC_TEST(x)                                       \
000067    if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
000068      void *pTstAlloc = sqlite3Malloc(10);                             \
000069      if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT;                  \
000070      sqlite3_free(pTstAlloc);                                         \
000071    }
000072  #else
000073    #define DO_OS_MALLOC_TEST(x)
000074  #endif
000075  
000076  /*
000077  ** The following routines are convenience wrappers around methods
000078  ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
000079  ** of this would be completely automatic if SQLite were coded using
000080  ** C++ instead of plain old C.
000081  */
000082  void sqlite3OsClose(sqlite3_file *pId){
000083    if( pId->pMethods ){
000084      pId->pMethods->xClose(pId);
000085      pId->pMethods = 0;
000086    }
000087  }
000088  int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
000089    DO_OS_MALLOC_TEST(id);
000090    return id->pMethods->xRead(id, pBuf, amt, offset);
000091  }
000092  int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
000093    DO_OS_MALLOC_TEST(id);
000094    return id->pMethods->xWrite(id, pBuf, amt, offset);
000095  }
000096  int sqlite3OsTruncate(sqlite3_file *id, i64 size){
000097    return id->pMethods->xTruncate(id, size);
000098  }
000099  int sqlite3OsSync(sqlite3_file *id, int flags){
000100    DO_OS_MALLOC_TEST(id);
000101    return id->pMethods->xSync(id, flags);
000102  }
000103  int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
000104    DO_OS_MALLOC_TEST(id);
000105    return id->pMethods->xFileSize(id, pSize);
000106  }
000107  int sqlite3OsLock(sqlite3_file *id, int lockType){
000108    DO_OS_MALLOC_TEST(id);
000109    return id->pMethods->xLock(id, lockType);
000110  }
000111  int sqlite3OsUnlock(sqlite3_file *id, int lockType){
000112    return id->pMethods->xUnlock(id, lockType);
000113  }
000114  int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
000115    DO_OS_MALLOC_TEST(id);
000116    return id->pMethods->xCheckReservedLock(id, pResOut);
000117  }
000118  
000119  /*
000120  ** Use sqlite3OsFileControl() when we are doing something that might fail
000121  ** and we need to know about the failures.  Use sqlite3OsFileControlHint()
000122  ** when simply tossing information over the wall to the VFS and we do not
000123  ** really care if the VFS receives and understands the information since it
000124  ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
000125  ** routine has no return value since the return value would be meaningless.
000126  */
000127  int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
000128  #ifdef SQLITE_TEST
000129    if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
000130      /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
000131      ** is using a regular VFS, it is called after the corresponding
000132      ** transaction has been committed. Injecting a fault at this point
000133      ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
000134      ** but the transaction is committed anyway.
000135      **
000136      ** The core must call OsFileControl() though, not OsFileControlHint(),
000137      ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
000138      ** means the commit really has failed and an error should be returned
000139      ** to the user.  */
000140      DO_OS_MALLOC_TEST(id);
000141    }
000142  #endif
000143    return id->pMethods->xFileControl(id, op, pArg);
000144  }
000145  void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
000146    (void)id->pMethods->xFileControl(id, op, pArg);
000147  }
000148  
000149  int sqlite3OsSectorSize(sqlite3_file *id){
000150    int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
000151    return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
000152  }
000153  int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
000154    return id->pMethods->xDeviceCharacteristics(id);
000155  }
000156  int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
000157    return id->pMethods->xShmLock(id, offset, n, flags);
000158  }
000159  void sqlite3OsShmBarrier(sqlite3_file *id){
000160    id->pMethods->xShmBarrier(id);
000161  }
000162  int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
000163    return id->pMethods->xShmUnmap(id, deleteFlag);
000164  }
000165  int sqlite3OsShmMap(
000166    sqlite3_file *id,               /* Database file handle */
000167    int iPage,
000168    int pgsz,
000169    int bExtend,                    /* True to extend file if necessary */
000170    void volatile **pp              /* OUT: Pointer to mapping */
000171  ){
000172    DO_OS_MALLOC_TEST(id);
000173    return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
000174  }
000175  
000176  #if SQLITE_MAX_MMAP_SIZE>0
000177  /* The real implementation of xFetch and xUnfetch */
000178  int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
000179    DO_OS_MALLOC_TEST(id);
000180    return id->pMethods->xFetch(id, iOff, iAmt, pp);
000181  }
000182  int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
000183    return id->pMethods->xUnfetch(id, iOff, p);
000184  }
000185  #else
000186  /* No-op stubs to use when memory-mapped I/O is disabled */
000187  int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
000188    *pp = 0;
000189    return SQLITE_OK;
000190  }
000191  int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
000192    return SQLITE_OK;
000193  }
000194  #endif
000195  
000196  /*
000197  ** The next group of routines are convenience wrappers around the
000198  ** VFS methods.
000199  */
000200  int sqlite3OsOpen(
000201    sqlite3_vfs *pVfs,
000202    const char *zPath,
000203    sqlite3_file *pFile,
000204    int flags,
000205    int *pFlagsOut
000206  ){
000207    int rc;
000208    DO_OS_MALLOC_TEST(0);
000209    /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
000210    ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
000211    ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
000212    ** reaching the VFS. */
000213    rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
000214    assert( rc==SQLITE_OK || pFile->pMethods==0 );
000215    return rc;
000216  }
000217  int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
000218    DO_OS_MALLOC_TEST(0);
000219    assert( dirSync==0 || dirSync==1 );
000220    return pVfs->xDelete(pVfs, zPath, dirSync);
000221  }
000222  int sqlite3OsAccess(
000223    sqlite3_vfs *pVfs,
000224    const char *zPath,
000225    int flags,
000226    int *pResOut
000227  ){
000228    DO_OS_MALLOC_TEST(0);
000229    return pVfs->xAccess(pVfs, zPath, flags, pResOut);
000230  }
000231  int sqlite3OsFullPathname(
000232    sqlite3_vfs *pVfs,
000233    const char *zPath,
000234    int nPathOut,
000235    char *zPathOut
000236  ){
000237    DO_OS_MALLOC_TEST(0);
000238    zPathOut[0] = 0;
000239    return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
000240  }
000241  #ifndef SQLITE_OMIT_LOAD_EXTENSION
000242  void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
000243    return pVfs->xDlOpen(pVfs, zPath);
000244  }
000245  void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
000246    pVfs->xDlError(pVfs, nByte, zBufOut);
000247  }
000248  void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
000249    return pVfs->xDlSym(pVfs, pHdle, zSym);
000250  }
000251  void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
000252    pVfs->xDlClose(pVfs, pHandle);
000253  }
000254  #endif /* SQLITE_OMIT_LOAD_EXTENSION */
000255  int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
000256    return pVfs->xRandomness(pVfs, nByte, zBufOut);
000257  }
000258  int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
000259    return pVfs->xSleep(pVfs, nMicro);
000260  }
000261  int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
000262    return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
000263  }
000264  int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
000265    int rc;
000266    /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
000267    ** method to get the current date and time if that method is available
000268    ** (if iVersion is 2 or greater and the function pointer is not NULL) and
000269    ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
000270    ** unavailable.
000271    */
000272    if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
000273      rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
000274    }else{
000275      double r;
000276      rc = pVfs->xCurrentTime(pVfs, &r);
000277      *pTimeOut = (sqlite3_int64)(r*86400000.0);
000278    }
000279    return rc;
000280  }
000281  
000282  int sqlite3OsOpenMalloc(
000283    sqlite3_vfs *pVfs,
000284    const char *zFile,
000285    sqlite3_file **ppFile,
000286    int flags,
000287    int *pOutFlags
000288  ){
000289    int rc;
000290    sqlite3_file *pFile;
000291    pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
000292    if( pFile ){
000293      rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
000294      if( rc!=SQLITE_OK ){
000295        sqlite3_free(pFile);
000296      }else{
000297        *ppFile = pFile;
000298      }
000299    }else{
000300      rc = SQLITE_NOMEM_BKPT;
000301    }
000302    return rc;
000303  }
000304  void sqlite3OsCloseFree(sqlite3_file *pFile){
000305    assert( pFile );
000306    sqlite3OsClose(pFile);
000307    sqlite3_free(pFile);
000308  }
000309  
000310  /*
000311  ** This function is a wrapper around the OS specific implementation of
000312  ** sqlite3_os_init(). The purpose of the wrapper is to provide the
000313  ** ability to simulate a malloc failure, so that the handling of an
000314  ** error in sqlite3_os_init() by the upper layers can be tested.
000315  */
000316  int sqlite3OsInit(void){
000317    void *p = sqlite3_malloc(10);
000318    if( p==0 ) return SQLITE_NOMEM_BKPT;
000319    sqlite3_free(p);
000320    return sqlite3_os_init();
000321  }
000322  
000323  /*
000324  ** The list of all registered VFS implementations.
000325  */
000326  static sqlite3_vfs * SQLITE_WSD vfsList = 0;
000327  #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
000328  
000329  /*
000330  ** Locate a VFS by name.  If no name is given, simply return the
000331  ** first VFS on the list.
000332  */
000333  sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
000334    sqlite3_vfs *pVfs = 0;
000335  #if SQLITE_THREADSAFE
000336    sqlite3_mutex *mutex;
000337  #endif
000338  #ifndef SQLITE_OMIT_AUTOINIT
000339    int rc = sqlite3_initialize();
000340    if( rc ) return 0;
000341  #endif
000342  #if SQLITE_THREADSAFE
000343    mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
000344  #endif
000345    sqlite3_mutex_enter(mutex);
000346    for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
000347      if( zVfs==0 ) break;
000348      if( strcmp(zVfs, pVfs->zName)==0 ) break;
000349    }
000350    sqlite3_mutex_leave(mutex);
000351    return pVfs;
000352  }
000353  
000354  /*
000355  ** Unlink a VFS from the linked list
000356  */
000357  static void vfsUnlink(sqlite3_vfs *pVfs){
000358    assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
000359    if( pVfs==0 ){
000360      /* No-op */
000361    }else if( vfsList==pVfs ){
000362      vfsList = pVfs->pNext;
000363    }else if( vfsList ){
000364      sqlite3_vfs *p = vfsList;
000365      while( p->pNext && p->pNext!=pVfs ){
000366        p = p->pNext;
000367      }
000368      if( p->pNext==pVfs ){
000369        p->pNext = pVfs->pNext;
000370      }
000371    }
000372  }
000373  
000374  /*
000375  ** Register a VFS with the system.  It is harmless to register the same
000376  ** VFS multiple times.  The new VFS becomes the default if makeDflt is
000377  ** true.
000378  */
000379  int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
000380    MUTEX_LOGIC(sqlite3_mutex *mutex;)
000381  #ifndef SQLITE_OMIT_AUTOINIT
000382    int rc = sqlite3_initialize();
000383    if( rc ) return rc;
000384  #endif
000385  #ifdef SQLITE_ENABLE_API_ARMOR
000386    if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
000387  #endif
000388  
000389    MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
000390    sqlite3_mutex_enter(mutex);
000391    vfsUnlink(pVfs);
000392    if( makeDflt || vfsList==0 ){
000393      pVfs->pNext = vfsList;
000394      vfsList = pVfs;
000395    }else{
000396      pVfs->pNext = vfsList->pNext;
000397      vfsList->pNext = pVfs;
000398    }
000399    assert(vfsList);
000400    sqlite3_mutex_leave(mutex);
000401    return SQLITE_OK;
000402  }
000403  
000404  /*
000405  ** Unregister a VFS so that it is no longer accessible.
000406  */
000407  int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
000408  #if SQLITE_THREADSAFE
000409    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
000410  #endif
000411    sqlite3_mutex_enter(mutex);
000412    vfsUnlink(pVfs);
000413    sqlite3_mutex_leave(mutex);
000414    return SQLITE_OK;
000415  }