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 flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
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    if( id->pMethods==0 ) return SQLITE_NOTFOUND;
000129  #ifdef SQLITE_TEST
000130    if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
000131     && op!=SQLITE_FCNTL_LOCK_TIMEOUT
000132    ){
000133      /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
000134      ** is using a regular VFS, it is called after the corresponding
000135      ** transaction has been committed. Injecting a fault at this point
000136      ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
000137      ** but the transaction is committed anyway.
000138      **
000139      ** The core must call OsFileControl() though, not OsFileControlHint(),
000140      ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
000141      ** means the commit really has failed and an error should be returned
000142      ** to the user.  */
000143      DO_OS_MALLOC_TEST(id);
000144    }
000145  #endif
000146    return id->pMethods->xFileControl(id, op, pArg);
000147  }
000148  void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
000149    if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
000150  }
000151  
000152  int sqlite3OsSectorSize(sqlite3_file *id){
000153    int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
000154    return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
000155  }
000156  int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
000157    return id->pMethods->xDeviceCharacteristics(id);
000158  }
000159  #ifndef SQLITE_OMIT_WAL
000160  int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
000161    return id->pMethods->xShmLock(id, offset, n, flags);
000162  }
000163  void sqlite3OsShmBarrier(sqlite3_file *id){
000164    id->pMethods->xShmBarrier(id);
000165  }
000166  int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
000167    return id->pMethods->xShmUnmap(id, deleteFlag);
000168  }
000169  int sqlite3OsShmMap(
000170    sqlite3_file *id,               /* Database file handle */
000171    int iPage,
000172    int pgsz,
000173    int bExtend,                    /* True to extend file if necessary */
000174    void volatile **pp              /* OUT: Pointer to mapping */
000175  ){
000176    DO_OS_MALLOC_TEST(id);
000177    return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
000178  }
000179  #endif /* SQLITE_OMIT_WAL */
000180  
000181  #if SQLITE_MAX_MMAP_SIZE>0
000182  /* The real implementation of xFetch and xUnfetch */
000183  int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
000184    DO_OS_MALLOC_TEST(id);
000185    return id->pMethods->xFetch(id, iOff, iAmt, pp);
000186  }
000187  int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
000188    return id->pMethods->xUnfetch(id, iOff, p);
000189  }
000190  #else
000191  /* No-op stubs to use when memory-mapped I/O is disabled */
000192  int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
000193    *pp = 0;
000194    return SQLITE_OK;
000195  }
000196  int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
000197    return SQLITE_OK;
000198  }
000199  #endif
000200  
000201  /*
000202  ** The next group of routines are convenience wrappers around the
000203  ** VFS methods.
000204  */
000205  int sqlite3OsOpen(
000206    sqlite3_vfs *pVfs,
000207    const char *zPath,
000208    sqlite3_file *pFile,
000209    int flags,
000210    int *pFlagsOut
000211  ){
000212    int rc;
000213    DO_OS_MALLOC_TEST(0);
000214    /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
000215    ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
000216    ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
000217    ** reaching the VFS. */
000218    rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
000219    assert( rc==SQLITE_OK || pFile->pMethods==0 );
000220    return rc;
000221  }
000222  int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
000223    DO_OS_MALLOC_TEST(0);
000224    assert( dirSync==0 || dirSync==1 );
000225    return pVfs->xDelete(pVfs, zPath, dirSync);
000226  }
000227  int sqlite3OsAccess(
000228    sqlite3_vfs *pVfs,
000229    const char *zPath,
000230    int flags,
000231    int *pResOut
000232  ){
000233    DO_OS_MALLOC_TEST(0);
000234    return pVfs->xAccess(pVfs, zPath, flags, pResOut);
000235  }
000236  int sqlite3OsFullPathname(
000237    sqlite3_vfs *pVfs,
000238    const char *zPath,
000239    int nPathOut,
000240    char *zPathOut
000241  ){
000242    DO_OS_MALLOC_TEST(0);
000243    zPathOut[0] = 0;
000244    return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
000245  }
000246  #ifndef SQLITE_OMIT_LOAD_EXTENSION
000247  void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
000248    return pVfs->xDlOpen(pVfs, zPath);
000249  }
000250  void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
000251    pVfs->xDlError(pVfs, nByte, zBufOut);
000252  }
000253  void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
000254    return pVfs->xDlSym(pVfs, pHdle, zSym);
000255  }
000256  void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
000257    pVfs->xDlClose(pVfs, pHandle);
000258  }
000259  #endif /* SQLITE_OMIT_LOAD_EXTENSION */
000260  int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
000261    if( sqlite3Config.iPrngSeed ){
000262      memset(zBufOut, 0, nByte);
000263      if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
000264      memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte);
000265      return SQLITE_OK;
000266    }else{
000267      return pVfs->xRandomness(pVfs, nByte, zBufOut);
000268    }
000269    
000270  }
000271  int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
000272    return pVfs->xSleep(pVfs, nMicro);
000273  }
000274  int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
000275    return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
000276  }
000277  int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
000278    int rc;
000279    /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
000280    ** method to get the current date and time if that method is available
000281    ** (if iVersion is 2 or greater and the function pointer is not NULL) and
000282    ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
000283    ** unavailable.
000284    */
000285    if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
000286      rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
000287    }else{
000288      double r;
000289      rc = pVfs->xCurrentTime(pVfs, &r);
000290      *pTimeOut = (sqlite3_int64)(r*86400000.0);
000291    }
000292    return rc;
000293  }
000294  
000295  int sqlite3OsOpenMalloc(
000296    sqlite3_vfs *pVfs,
000297    const char *zFile,
000298    sqlite3_file **ppFile,
000299    int flags,
000300    int *pOutFlags
000301  ){
000302    int rc;
000303    sqlite3_file *pFile;
000304    pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
000305    if( pFile ){
000306      rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
000307      if( rc!=SQLITE_OK ){
000308        sqlite3_free(pFile);
000309      }else{
000310        *ppFile = pFile;
000311      }
000312    }else{
000313      rc = SQLITE_NOMEM_BKPT;
000314    }
000315    return rc;
000316  }
000317  void sqlite3OsCloseFree(sqlite3_file *pFile){
000318    assert( pFile );
000319    sqlite3OsClose(pFile);
000320    sqlite3_free(pFile);
000321  }
000322  
000323  /*
000324  ** This function is a wrapper around the OS specific implementation of
000325  ** sqlite3_os_init(). The purpose of the wrapper is to provide the
000326  ** ability to simulate a malloc failure, so that the handling of an
000327  ** error in sqlite3_os_init() by the upper layers can be tested.
000328  */
000329  int sqlite3OsInit(void){
000330    void *p = sqlite3_malloc(10);
000331    if( p==0 ) return SQLITE_NOMEM_BKPT;
000332    sqlite3_free(p);
000333    return sqlite3_os_init();
000334  }
000335  
000336  /*
000337  ** The list of all registered VFS implementations.
000338  */
000339  static sqlite3_vfs * SQLITE_WSD vfsList = 0;
000340  #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
000341  
000342  /*
000343  ** Locate a VFS by name.  If no name is given, simply return the
000344  ** first VFS on the list.
000345  */
000346  sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
000347    sqlite3_vfs *pVfs = 0;
000348  #if SQLITE_THREADSAFE
000349    sqlite3_mutex *mutex;
000350  #endif
000351  #ifndef SQLITE_OMIT_AUTOINIT
000352    int rc = sqlite3_initialize();
000353    if( rc ) return 0;
000354  #endif
000355  #if SQLITE_THREADSAFE
000356    mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
000357  #endif
000358    sqlite3_mutex_enter(mutex);
000359    for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
000360      if( zVfs==0 ) break;
000361      if( strcmp(zVfs, pVfs->zName)==0 ) break;
000362    }
000363    sqlite3_mutex_leave(mutex);
000364    return pVfs;
000365  }
000366  
000367  /*
000368  ** Unlink a VFS from the linked list
000369  */
000370  static void vfsUnlink(sqlite3_vfs *pVfs){
000371    assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
000372    if( pVfs==0 ){
000373      /* No-op */
000374    }else if( vfsList==pVfs ){
000375      vfsList = pVfs->pNext;
000376    }else if( vfsList ){
000377      sqlite3_vfs *p = vfsList;
000378      while( p->pNext && p->pNext!=pVfs ){
000379        p = p->pNext;
000380      }
000381      if( p->pNext==pVfs ){
000382        p->pNext = pVfs->pNext;
000383      }
000384    }
000385  }
000386  
000387  /*
000388  ** Register a VFS with the system.  It is harmless to register the same
000389  ** VFS multiple times.  The new VFS becomes the default if makeDflt is
000390  ** true.
000391  */
000392  int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
000393    MUTEX_LOGIC(sqlite3_mutex *mutex;)
000394  #ifndef SQLITE_OMIT_AUTOINIT
000395    int rc = sqlite3_initialize();
000396    if( rc ) return rc;
000397  #endif
000398  #ifdef SQLITE_ENABLE_API_ARMOR
000399    if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
000400  #endif
000401  
000402    MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
000403    sqlite3_mutex_enter(mutex);
000404    vfsUnlink(pVfs);
000405    if( makeDflt || vfsList==0 ){
000406      pVfs->pNext = vfsList;
000407      vfsList = pVfs;
000408    }else{
000409      pVfs->pNext = vfsList->pNext;
000410      vfsList->pNext = pVfs;
000411    }
000412    assert(vfsList);
000413    sqlite3_mutex_leave(mutex);
000414    return SQLITE_OK;
000415  }
000416  
000417  /*
000418  ** Unregister a VFS so that it is no longer accessible.
000419  */
000420  int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
000421    MUTEX_LOGIC(sqlite3_mutex *mutex;)
000422  #ifndef SQLITE_OMIT_AUTOINIT
000423    int rc = sqlite3_initialize();
000424    if( rc ) return rc;
000425  #endif
000426    MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
000427    sqlite3_mutex_enter(mutex);
000428    vfsUnlink(pVfs);
000429    sqlite3_mutex_leave(mutex);
000430    return SQLITE_OK;
000431  }