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