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 & 0x87f7f, 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    return pVfs->xRandomness(pVfs, nByte, zBufOut);
000262  }
000263  int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
000264    return pVfs->xSleep(pVfs, nMicro);
000265  }
000266  int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
000267    return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
000268  }
000269  int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
000270    int rc;
000271    /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
000272    ** method to get the current date and time if that method is available
000273    ** (if iVersion is 2 or greater and the function pointer is not NULL) and
000274    ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
000275    ** unavailable.
000276    */
000277    if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
000278      rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
000279    }else{
000280      double r;
000281      rc = pVfs->xCurrentTime(pVfs, &r);
000282      *pTimeOut = (sqlite3_int64)(r*86400000.0);
000283    }
000284    return rc;
000285  }
000286  
000287  int sqlite3OsOpenMalloc(
000288    sqlite3_vfs *pVfs,
000289    const char *zFile,
000290    sqlite3_file **ppFile,
000291    int flags,
000292    int *pOutFlags
000293  ){
000294    int rc;
000295    sqlite3_file *pFile;
000296    pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
000297    if( pFile ){
000298      rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
000299      if( rc!=SQLITE_OK ){
000300        sqlite3_free(pFile);
000301      }else{
000302        *ppFile = pFile;
000303      }
000304    }else{
000305      rc = SQLITE_NOMEM_BKPT;
000306    }
000307    return rc;
000308  }
000309  void sqlite3OsCloseFree(sqlite3_file *pFile){
000310    assert( pFile );
000311    sqlite3OsClose(pFile);
000312    sqlite3_free(pFile);
000313  }
000314  
000315  /*
000316  ** This function is a wrapper around the OS specific implementation of
000317  ** sqlite3_os_init(). The purpose of the wrapper is to provide the
000318  ** ability to simulate a malloc failure, so that the handling of an
000319  ** error in sqlite3_os_init() by the upper layers can be tested.
000320  */
000321  int sqlite3OsInit(void){
000322    void *p = sqlite3_malloc(10);
000323    if( p==0 ) return SQLITE_NOMEM_BKPT;
000324    sqlite3_free(p);
000325    return sqlite3_os_init();
000326  }
000327  
000328  /*
000329  ** The list of all registered VFS implementations.
000330  */
000331  static sqlite3_vfs * SQLITE_WSD vfsList = 0;
000332  #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
000333  
000334  /*
000335  ** Locate a VFS by name.  If no name is given, simply return the
000336  ** first VFS on the list.
000337  */
000338  sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
000339    sqlite3_vfs *pVfs = 0;
000340  #if SQLITE_THREADSAFE
000341    sqlite3_mutex *mutex;
000342  #endif
000343  #ifndef SQLITE_OMIT_AUTOINIT
000344    int rc = sqlite3_initialize();
000345    if( rc ) return 0;
000346  #endif
000347  #if SQLITE_THREADSAFE
000348    mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
000349  #endif
000350    sqlite3_mutex_enter(mutex);
000351    for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
000352      if( zVfs==0 ) break;
000353      if( strcmp(zVfs, pVfs->zName)==0 ) break;
000354    }
000355    sqlite3_mutex_leave(mutex);
000356    return pVfs;
000357  }
000358  
000359  /*
000360  ** Unlink a VFS from the linked list
000361  */
000362  static void vfsUnlink(sqlite3_vfs *pVfs){
000363    assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
000364    if( pVfs==0 ){
000365      /* No-op */
000366    }else if( vfsList==pVfs ){
000367      vfsList = pVfs->pNext;
000368    }else if( vfsList ){
000369      sqlite3_vfs *p = vfsList;
000370      while( p->pNext && p->pNext!=pVfs ){
000371        p = p->pNext;
000372      }
000373      if( p->pNext==pVfs ){
000374        p->pNext = pVfs->pNext;
000375      }
000376    }
000377  }
000378  
000379  /*
000380  ** Register a VFS with the system.  It is harmless to register the same
000381  ** VFS multiple times.  The new VFS becomes the default if makeDflt is
000382  ** true.
000383  */
000384  int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
000385    MUTEX_LOGIC(sqlite3_mutex *mutex;)
000386  #ifndef SQLITE_OMIT_AUTOINIT
000387    int rc = sqlite3_initialize();
000388    if( rc ) return rc;
000389  #endif
000390  #ifdef SQLITE_ENABLE_API_ARMOR
000391    if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
000392  #endif
000393  
000394    MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
000395    sqlite3_mutex_enter(mutex);
000396    vfsUnlink(pVfs);
000397    if( makeDflt || vfsList==0 ){
000398      pVfs->pNext = vfsList;
000399      vfsList = pVfs;
000400    }else{
000401      pVfs->pNext = vfsList->pNext;
000402      vfsList->pNext = pVfs;
000403    }
000404    assert(vfsList);
000405    sqlite3_mutex_leave(mutex);
000406    return SQLITE_OK;
000407  }
000408  
000409  /*
000410  ** Unregister a VFS so that it is no longer accessible.
000411  */
000412  int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
000413  #if SQLITE_THREADSAFE
000414    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
000415  #endif
000416    sqlite3_mutex_enter(mutex);
000417    vfsUnlink(pVfs);
000418    sqlite3_mutex_leave(mutex);
000419    return SQLITE_OK;
000420  }