os.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. ** 2005 November 29
  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 OS interface code that is common to all
  14. ** architectures.
  15. */
  16. #define _SQLITE_OS_C_ 1
  17. #include "sqliteInt.h"
  18. #undef _SQLITE_OS_C_
  19. /*
  20. ** The default SQLite sqlite3_vfs implementations do not allocate
  21. ** memory (actually, os_unix.c allocates a small amount of memory
  22. ** from within OsOpen()), but some third-party implementations may.
  23. ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
  24. ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
  25. **
  26. ** The following functions are instrumented for malloc() failure
  27. ** testing:
  28. **
  29. ** sqlite3OsRead()
  30. ** sqlite3OsWrite()
  31. ** sqlite3OsSync()
  32. ** sqlite3OsFileSize()
  33. ** sqlite3OsLock()
  34. ** sqlite3OsCheckReservedLock()
  35. ** sqlite3OsFileControl()
  36. ** sqlite3OsShmMap()
  37. ** sqlite3OsOpen()
  38. ** sqlite3OsDelete()
  39. ** sqlite3OsAccess()
  40. ** sqlite3OsFullPathname()
  41. **
  42. */
  43. #if defined(SQLITE_TEST)
  44. int sqlite3_memdebug_vfs_oom_test = 1;
  45. #define DO_OS_MALLOC_TEST(x) \
  46. if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) { \
  47. void *pTstAlloc = sqlite3Malloc(10); \
  48. if (!pTstAlloc) return SQLITE_IOERR_NOMEM; \
  49. sqlite3_free(pTstAlloc); \
  50. }
  51. #else
  52. #define DO_OS_MALLOC_TEST(x)
  53. #endif
  54. /*
  55. ** The following routines are convenience wrappers around methods
  56. ** of the sqlite3_file object. This is mostly just syntactic sugar. All
  57. ** of this would be completely automatic if SQLite were coded using
  58. ** C++ instead of plain old C.
  59. */
  60. int sqlite3OsClose(sqlite3_file *pId){
  61. int rc = SQLITE_OK;
  62. if( pId->pMethods ){
  63. rc = pId->pMethods->xClose(pId);
  64. pId->pMethods = 0;
  65. }
  66. return rc;
  67. }
  68. int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
  69. DO_OS_MALLOC_TEST(id);
  70. return id->pMethods->xRead(id, pBuf, amt, offset);
  71. }
  72. int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
  73. DO_OS_MALLOC_TEST(id);
  74. return id->pMethods->xWrite(id, pBuf, amt, offset);
  75. }
  76. int sqlite3OsTruncate(sqlite3_file *id, i64 size){
  77. return id->pMethods->xTruncate(id, size);
  78. }
  79. int sqlite3OsSync(sqlite3_file *id, int flags){
  80. DO_OS_MALLOC_TEST(id);
  81. return id->pMethods->xSync(id, flags);
  82. }
  83. int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
  84. DO_OS_MALLOC_TEST(id);
  85. return id->pMethods->xFileSize(id, pSize);
  86. }
  87. int sqlite3OsLock(sqlite3_file *id, int lockType){
  88. DO_OS_MALLOC_TEST(id);
  89. return id->pMethods->xLock(id, lockType);
  90. }
  91. int sqlite3OsUnlock(sqlite3_file *id, int lockType){
  92. return id->pMethods->xUnlock(id, lockType);
  93. }
  94. int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
  95. DO_OS_MALLOC_TEST(id);
  96. return id->pMethods->xCheckReservedLock(id, pResOut);
  97. }
  98. /*
  99. ** Use sqlite3OsFileControl() when we are doing something that might fail
  100. ** and we need to know about the failures. Use sqlite3OsFileControlHint()
  101. ** when simply tossing information over the wall to the VFS and we do not
  102. ** really care if the VFS receives and understands the information since it
  103. ** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
  104. ** routine has no return value since the return value would be meaningless.
  105. */
  106. int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
  107. DO_OS_MALLOC_TEST(id);
  108. return id->pMethods->xFileControl(id, op, pArg);
  109. }
  110. void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
  111. (void)id->pMethods->xFileControl(id, op, pArg);
  112. }
  113. int sqlite3OsSectorSize(sqlite3_file *id){
  114. int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
  115. return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
  116. }
  117. int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
  118. return id->pMethods->xDeviceCharacteristics(id);
  119. }
  120. int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
  121. return id->pMethods->xShmLock(id, offset, n, flags);
  122. }
  123. void sqlite3OsShmBarrier(sqlite3_file *id){
  124. id->pMethods->xShmBarrier(id);
  125. }
  126. int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
  127. return id->pMethods->xShmUnmap(id, deleteFlag);
  128. }
  129. int sqlite3OsShmMap(
  130. sqlite3_file *id, /* Database file handle */
  131. int iPage,
  132. int pgsz,
  133. int bExtend, /* True to extend file if necessary */
  134. void volatile **pp /* OUT: Pointer to mapping */
  135. ){
  136. DO_OS_MALLOC_TEST(id);
  137. return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
  138. }
  139. #if SQLITE_MAX_MMAP_SIZE>0
  140. /* The real implementation of xFetch and xUnfetch */
  141. int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
  142. DO_OS_MALLOC_TEST(id);
  143. return id->pMethods->xFetch(id, iOff, iAmt, pp);
  144. }
  145. int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
  146. return id->pMethods->xUnfetch(id, iOff, p);
  147. }
  148. #else
  149. /* No-op stubs to use when memory-mapped I/O is disabled */
  150. int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
  151. *pp = 0;
  152. return SQLITE_OK;
  153. }
  154. int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
  155. return SQLITE_OK;
  156. }
  157. #endif
  158. /*
  159. ** The next group of routines are convenience wrappers around the
  160. ** VFS methods.
  161. */
  162. int sqlite3OsOpen(
  163. sqlite3_vfs *pVfs,
  164. const char *zPath,
  165. sqlite3_file *pFile,
  166. int flags,
  167. int *pFlagsOut
  168. ){
  169. int rc;
  170. DO_OS_MALLOC_TEST(0);
  171. /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
  172. ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
  173. ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
  174. ** reaching the VFS. */
  175. rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
  176. assert( rc==SQLITE_OK || pFile->pMethods==0 );
  177. return rc;
  178. }
  179. int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
  180. DO_OS_MALLOC_TEST(0);
  181. assert( dirSync==0 || dirSync==1 );
  182. return pVfs->xDelete(pVfs, zPath, dirSync);
  183. }
  184. int sqlite3OsAccess(
  185. sqlite3_vfs *pVfs,
  186. const char *zPath,
  187. int flags,
  188. int *pResOut
  189. ){
  190. DO_OS_MALLOC_TEST(0);
  191. return pVfs->xAccess(pVfs, zPath, flags, pResOut);
  192. }
  193. int sqlite3OsFullPathname(
  194. sqlite3_vfs *pVfs,
  195. const char *zPath,
  196. int nPathOut,
  197. char *zPathOut
  198. ){
  199. DO_OS_MALLOC_TEST(0);
  200. zPathOut[0] = 0;
  201. return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
  202. }
  203. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  204. void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
  205. return pVfs->xDlOpen(pVfs, zPath);
  206. }
  207. void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
  208. pVfs->xDlError(pVfs, nByte, zBufOut);
  209. }
  210. void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
  211. return pVfs->xDlSym(pVfs, pHdle, zSym);
  212. }
  213. void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
  214. pVfs->xDlClose(pVfs, pHandle);
  215. }
  216. #endif /* SQLITE_OMIT_LOAD_EXTENSION */
  217. int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
  218. return pVfs->xRandomness(pVfs, nByte, zBufOut);
  219. }
  220. int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
  221. return pVfs->xSleep(pVfs, nMicro);
  222. }
  223. int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
  224. int rc;
  225. /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
  226. ** method to get the current date and time if that method is available
  227. ** (if iVersion is 2 or greater and the function pointer is not NULL) and
  228. ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
  229. ** unavailable.
  230. */
  231. if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
  232. rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
  233. }else{
  234. double r;
  235. rc = pVfs->xCurrentTime(pVfs, &r);
  236. *pTimeOut = (sqlite3_int64)(r*86400000.0);
  237. }
  238. return rc;
  239. }
  240. int sqlite3OsOpenMalloc(
  241. sqlite3_vfs *pVfs,
  242. const char *zFile,
  243. sqlite3_file **ppFile,
  244. int flags,
  245. int *pOutFlags
  246. ){
  247. int rc = SQLITE_NOMEM;
  248. sqlite3_file *pFile;
  249. pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
  250. if( pFile ){
  251. rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
  252. if( rc!=SQLITE_OK ){
  253. sqlite3_free(pFile);
  254. }else{
  255. *ppFile = pFile;
  256. }
  257. }
  258. return rc;
  259. }
  260. int sqlite3OsCloseFree(sqlite3_file *pFile){
  261. int rc = SQLITE_OK;
  262. assert( pFile );
  263. rc = sqlite3OsClose(pFile);
  264. sqlite3_free(pFile);
  265. return rc;
  266. }
  267. /*
  268. ** This function is a wrapper around the OS specific implementation of
  269. ** sqlite3_os_init(). The purpose of the wrapper is to provide the
  270. ** ability to simulate a malloc failure, so that the handling of an
  271. ** error in sqlite3_os_init() by the upper layers can be tested.
  272. */
  273. int sqlite3OsInit(void){
  274. void *p = sqlite3_malloc(10);
  275. if( p==0 ) return SQLITE_NOMEM;
  276. sqlite3_free(p);
  277. return sqlite3_os_init();
  278. }
  279. /*
  280. ** The list of all registered VFS implementations.
  281. */
  282. static sqlite3_vfs * SQLITE_WSD vfsList = 0;
  283. #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
  284. /*
  285. ** Locate a VFS by name. If no name is given, simply return the
  286. ** first VFS on the list.
  287. */
  288. sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
  289. sqlite3_vfs *pVfs = 0;
  290. #if SQLITE_THREADSAFE
  291. sqlite3_mutex *mutex;
  292. #endif
  293. #ifndef SQLITE_OMIT_AUTOINIT
  294. int rc = sqlite3_initialize();
  295. if( rc ) return 0;
  296. #endif
  297. #if SQLITE_THREADSAFE
  298. mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  299. #endif
  300. sqlite3_mutex_enter(mutex);
  301. for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
  302. if( zVfs==0 ) break;
  303. if( strcmp(zVfs, pVfs->zName)==0 ) break;
  304. }
  305. sqlite3_mutex_leave(mutex);
  306. return pVfs;
  307. }
  308. /*
  309. ** Unlink a VFS from the linked list
  310. */
  311. static void vfsUnlink(sqlite3_vfs *pVfs){
  312. assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
  313. if( pVfs==0 ){
  314. /* No-op */
  315. }else if( vfsList==pVfs ){
  316. vfsList = pVfs->pNext;
  317. }else if( vfsList ){
  318. sqlite3_vfs *p = vfsList;
  319. while( p->pNext && p->pNext!=pVfs ){
  320. p = p->pNext;
  321. }
  322. if( p->pNext==pVfs ){
  323. p->pNext = pVfs->pNext;
  324. }
  325. }
  326. }
  327. /*
  328. ** Register a VFS with the system. It is harmless to register the same
  329. ** VFS multiple times. The new VFS becomes the default if makeDflt is
  330. ** true.
  331. */
  332. int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
  333. MUTEX_LOGIC(sqlite3_mutex *mutex;)
  334. #ifndef SQLITE_OMIT_AUTOINIT
  335. int rc = sqlite3_initialize();
  336. if( rc ) return rc;
  337. #endif
  338. MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
  339. sqlite3_mutex_enter(mutex);
  340. vfsUnlink(pVfs);
  341. if( makeDflt || vfsList==0 ){
  342. pVfs->pNext = vfsList;
  343. vfsList = pVfs;
  344. }else{
  345. pVfs->pNext = vfsList->pNext;
  346. vfsList->pNext = pVfs;
  347. }
  348. assert(vfsList);
  349. sqlite3_mutex_leave(mutex);
  350. return SQLITE_OK;
  351. }
  352. /*
  353. ** Unregister a VFS so that it is no longer accessible.
  354. */
  355. int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
  356. #if SQLITE_THREADSAFE
  357. sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  358. #endif
  359. sqlite3_mutex_enter(mutex);
  360. vfsUnlink(pVfs);
  361. sqlite3_mutex_leave(mutex);
  362. return SQLITE_OK;
  363. }