interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / src / test_syscall.c

764 lines · 21504 bytes Raw Blame History
1/*
2** 2011 March 28
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** The code in this file implements a Tcl interface used to test error
14** handling in the os_unix.c module. Wrapper functions that support fault
15** injection are registered as the low-level OS functions using the
16** xSetSystemCall() method of the VFS. The Tcl interface is as follows:
17**
18**
19** test_syscall install LIST
20** Install wrapper functions for all system calls in argument LIST.
21** LIST must be a list consisting of zero or more of the following
22** literal values:
23**
24** open close access getcwd stat fstat
25** ftruncate fcntl read pread pread64 write
26** pwrite pwrite64 fchmod fallocate mmap
27**
28** test_syscall uninstall
29** Uninstall all wrapper functions.
30**
31** test_syscall fault ?COUNT PERSIST?
32** If [test_syscall fault] is invoked without the two arguments, fault
33** injection is disabled. Otherwise, fault injection is configured to
34** cause a failure on the COUNT'th next call to a system call with a
35** wrapper function installed. A COUNT value of 1 means fail the next
36** system call.
37**
38** Argument PERSIST is interpreted as a boolean. If true, the all
39** system calls following the initial failure also fail. Otherwise, only
40** the single transient failure is injected.
41**
42** test_syscall errno CALL ERRNO
43** Set the value that the global "errno" is set to following a fault
44** in call CALL. Argument CALL must be one of the system call names
45** listed above (under [test_syscall install]). ERRNO is a symbolic
46** name (i.e. "EACCES"). Not all errno codes are supported. Add extra
47** to the aErrno table in function test_syscall_errno() below as
48** required.
49**
50** test_syscall reset ?SYSTEM-CALL?
51** With no argument, this is an alias for the [uninstall] command. However,
52** this command uses a VFS call of the form:
53**
54** xSetSystemCall(pVfs, 0, 0);
55**
56** To restore the default system calls. The [uninstall] command restores
57** each system call individually by calling (i.e.):
58**
59** xSetSystemCall(pVfs, "open", 0);
60**
61** With an argument, this command attempts to reset the system call named
62** by the parameter using the same method as [uninstall].
63**
64** test_syscall exists SYSTEM-CALL
65** Return true if the named system call exists. Or false otherwise.
66**
67** test_syscall list
68** Return a list of all system calls. The list is constructed using
69** the xNextSystemCall() VFS method.
70**
71** test_syscall pagesize PGSZ
72** If PGSZ is a power of two greater than 256, install a wrapper around
73** OS function getpagesize() that reports the system page size as PGSZ.
74** Or, if PGSZ is less than zero, remove any wrapper already installed.
75*/
76
77#include "sqliteInt.h"
78#include "sqlite3.h"
79#include "tclsqlite.h"
80#include <stdlib.h>
81#include <string.h>
82#include <assert.h>
83
84#if SQLITE_OS_UNIX
85
86/* From main.c */
87extern const char *sqlite3ErrName(int);
88
89#include <sys/mman.h>
90#include <sys/types.h>
91#include <errno.h>
92
93static struct TestSyscallGlobal {
94 int bPersist; /* 1 for persistent errors, 0 for transient */
95 int nCount; /* Fail after this many more calls */
96 int nFail; /* Number of failures that have occurred */
97 int pgsz;
98 sqlite3_syscall_ptr orig_getpagesize;
99} gSyscall = { 0, 0, 0, 0, 0 };
100
101static int ts_open(const char *, int, int);
102static int ts_close(int fd);
103static int ts_access(const char *zPath, int mode);
104static char *ts_getcwd(char *zPath, size_t nPath);
105static int ts_stat(const char *zPath, struct stat *p);
106static int ts_fstat(int fd, struct stat *p);
107static int ts_ftruncate(int fd, off_t n);
108static int ts_fcntl(int fd, int cmd, ... );
109static ssize_t ts_read(int fd, void *aBuf, size_t nBuf);
110static ssize_t ts_pread(int fd, void *aBuf, size_t nBuf, off_t off);
111/* Note: pread64() and pwrite64() actually use off64_t as the type on their
112** last parameter. But that datatype is not defined on many systems
113** (ex: Mac, OpenBSD). So substitute a likely equivalent: sqlite3_uint64 */
114static ssize_t ts_pread64(int fd, void *aBuf, size_t nBuf, sqlite3_uint64 off);
115static ssize_t ts_write(int fd, const void *aBuf, size_t nBuf);
116static ssize_t ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off);
117static ssize_t ts_pwrite64(int fd, const void *aBuf, size_t nBuf, sqlite3_uint64 off);
118static int ts_fchmod(int fd, mode_t mode);
119static int ts_fallocate(int fd, off_t off, off_t len);
120static void *ts_mmap(void *, size_t, int, int, int, off_t);
121static void *ts_mremap(void*, size_t, size_t, int, ...);
122
123struct TestSyscallArray {
124 const char *zName;
125 sqlite3_syscall_ptr xTest;
126 sqlite3_syscall_ptr xOrig;
127 int default_errno; /* Default value for errno following errors */
128 int custom_errno; /* Current value for errno if error */
129} aSyscall[] = {
130 /* 0 */ { "open", (sqlite3_syscall_ptr)ts_open, 0, EACCES, 0 },
131 /* 1 */ { "close", (sqlite3_syscall_ptr)ts_close, 0, 0, 0 },
132 /* 2 */ { "access", (sqlite3_syscall_ptr)ts_access, 0, 0, 0 },
133 /* 3 */ { "getcwd", (sqlite3_syscall_ptr)ts_getcwd, 0, 0, 0 },
134 /* 4 */ { "stat", (sqlite3_syscall_ptr)ts_stat, 0, 0, 0 },
135 /* 5 */ { "fstat", (sqlite3_syscall_ptr)ts_fstat, 0, 0, 0 },
136 /* 6 */ { "ftruncate", (sqlite3_syscall_ptr)ts_ftruncate, 0, EIO, 0 },
137 /* 7 */ { "fcntl", (sqlite3_syscall_ptr)ts_fcntl, 0, EACCES, 0 },
138 /* 8 */ { "read", (sqlite3_syscall_ptr)ts_read, 0, 0, 0 },
139 /* 9 */ { "pread", (sqlite3_syscall_ptr)ts_pread, 0, 0, 0 },
140 /* 10 */ { "pread64", (sqlite3_syscall_ptr)ts_pread64, 0, 0, 0 },
141 /* 11 */ { "write", (sqlite3_syscall_ptr)ts_write, 0, 0, 0 },
142 /* 12 */ { "pwrite", (sqlite3_syscall_ptr)ts_pwrite, 0, 0, 0 },
143 /* 13 */ { "pwrite64", (sqlite3_syscall_ptr)ts_pwrite64, 0, 0, 0 },
144 /* 14 */ { "fchmod", (sqlite3_syscall_ptr)ts_fchmod, 0, 0, 0 },
145 /* 15 */ { "fallocate", (sqlite3_syscall_ptr)ts_fallocate, 0, 0, 0 },
146 /* 16 */ { "mmap", (sqlite3_syscall_ptr)ts_mmap, 0, 0, 0 },
147 /* 17 */ { "mremap", (sqlite3_syscall_ptr)ts_mremap, 0, 0, 0 },
148 { 0, 0, 0, 0, 0 }
149};
150
151#define orig_open ((int(*)(const char *, int, int))aSyscall[0].xOrig)
152#define orig_close ((int(*)(int))aSyscall[1].xOrig)
153#define orig_access ((int(*)(const char*,int))aSyscall[2].xOrig)
154#define orig_getcwd ((char*(*)(char*,size_t))aSyscall[3].xOrig)
155#define orig_stat ((int(*)(const char*,struct stat*))aSyscall[4].xOrig)
156#define orig_fstat ((int(*)(int,struct stat*))aSyscall[5].xOrig)
157#define orig_ftruncate ((int(*)(int,off_t))aSyscall[6].xOrig)
158#define orig_fcntl ((int(*)(int,int,...))aSyscall[7].xOrig)
159#define orig_read ((ssize_t(*)(int,void*,size_t))aSyscall[8].xOrig)
160#define orig_pread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].xOrig)
161#define orig_pread64 ((ssize_t(*)(int,void*,size_t,sqlite3_uint64))aSyscall[10].xOrig)
162#define orig_write ((ssize_t(*)(int,const void*,size_t))aSyscall[11].xOrig)
163#define orig_pwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
164 aSyscall[12].xOrig)
165#define orig_pwrite64 ((ssize_t(*)(int,const void*,size_t,sqlite3_uint64))\
166 aSyscall[13].xOrig)
167#define orig_fchmod ((int(*)(int,mode_t))aSyscall[14].xOrig)
168#define orig_fallocate ((int(*)(int,off_t,off_t))aSyscall[15].xOrig)
169#define orig_mmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[16].xOrig)
170#define orig_mremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[17].xOrig)
171
172/*
173** This function is called exactly once from within each invocation of a
174** system call wrapper in this file. It returns 1 if the function should
175** fail, or 0 if it should succeed.
176*/
177static int tsIsFail(void){
178 gSyscall.nCount--;
179 if( gSyscall.nCount==0 || (gSyscall.nFail && gSyscall.bPersist) ){
180 gSyscall.nFail++;
181 return 1;
182 }
183 return 0;
184}
185
186/*
187** Return the current error-number value for function zFunc. zFunc must be
188** the name of a system call in the aSyscall[] table.
189**
190** Usually, the current error-number is the value that errno should be set
191** to if the named system call fails. The exception is "fallocate". See
192** comments above the implementation of ts_fallocate() for details.
193*/
194static int tsErrno(const char *zFunc){
195 int i;
196 size_t nFunc = strlen(zFunc);
197 for(i=0; aSyscall[i].zName; i++){
198 if( strlen(aSyscall[i].zName)!=nFunc ) continue;
199 if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
200 return aSyscall[i].custom_errno;
201 }
202
203 assert(0);
204 return 0;
205}
206
207/*
208** A wrapper around tsIsFail(). If tsIsFail() returns non-zero, set the
209** value of errno before returning.
210*/
211static int tsIsFailErrno(const char *zFunc){
212 if( tsIsFail() ){
213 errno = tsErrno(zFunc);
214 return 1;
215 }
216 return 0;
217}
218
219/*
220** A wrapper around open().
221*/
222static int ts_open(const char *zFile, int flags, int mode){
223 if( tsIsFailErrno("open") ){
224 return -1;
225 }
226 return orig_open(zFile, flags, mode);
227}
228
229/*
230** A wrapper around close().
231*/
232static int ts_close(int fd){
233 if( tsIsFail() ){
234 /* Even if simulating an error, close the original file-descriptor.
235 ** This is to stop the test process from running out of file-descriptors
236 ** when running a long test. If a call to close() appears to fail, SQLite
237 ** never attempts to use the file-descriptor afterwards (or even to close
238 ** it a second time). */
239 orig_close(fd);
240 return -1;
241 }
242 return orig_close(fd);
243}
244
245/*
246** A wrapper around access().
247*/
248static int ts_access(const char *zPath, int mode){
249 if( tsIsFail() ){
250 return -1;
251 }
252 return orig_access(zPath, mode);
253}
254
255/*
256** A wrapper around getcwd().
257*/
258static char *ts_getcwd(char *zPath, size_t nPath){
259 if( tsIsFail() ){
260 return NULL;
261 }
262 return orig_getcwd(zPath, nPath);
263}
264
265/*
266** A wrapper around stat().
267*/
268static int ts_stat(const char *zPath, struct stat *p){
269 if( tsIsFail() ){
270 return -1;
271 }
272 return orig_stat(zPath, p);
273}
274
275/*
276** A wrapper around fstat().
277*/
278static int ts_fstat(int fd, struct stat *p){
279 if( tsIsFailErrno("fstat") ){
280 return -1;
281 }
282 return orig_fstat(fd, p);
283}
284
285/*
286** A wrapper around ftruncate().
287*/
288static int ts_ftruncate(int fd, off_t n){
289 if( tsIsFailErrno("ftruncate") ){
290 return -1;
291 }
292 return orig_ftruncate(fd, n);
293}
294
295/*
296** A wrapper around fcntl().
297*/
298static int ts_fcntl(int fd, int cmd, ... ){
299 va_list ap;
300 void *pArg;
301 if( tsIsFailErrno("fcntl") ){
302 return -1;
303 }
304 va_start(ap, cmd);
305 pArg = va_arg(ap, void *);
306 return orig_fcntl(fd, cmd, pArg);
307}
308
309/*
310** A wrapper around read().
311*/
312static ssize_t ts_read(int fd, void *aBuf, size_t nBuf){
313 if( tsIsFailErrno("read") ){
314 return -1;
315 }
316 return orig_read(fd, aBuf, nBuf);
317}
318
319/*
320** A wrapper around pread().
321*/
322static ssize_t ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){
323 if( tsIsFailErrno("pread") ){
324 return -1;
325 }
326 return orig_pread(fd, aBuf, nBuf, off);
327}
328
329/*
330** A wrapper around pread64().
331*/
332static ssize_t ts_pread64(int fd, void *aBuf, size_t nBuf, sqlite3_uint64 off){
333 if( tsIsFailErrno("pread64") ){
334 return -1;
335 }
336 return orig_pread64(fd, aBuf, nBuf, off);
337}
338
339/*
340** A wrapper around write().
341*/
342static ssize_t ts_write(int fd, const void *aBuf, size_t nBuf){
343 if( tsIsFailErrno("write") ){
344 if( tsErrno("write")==EINTR ) orig_write(fd, aBuf, nBuf/2);
345 return -1;
346 }
347 return orig_write(fd, aBuf, nBuf);
348}
349
350/*
351** A wrapper around pwrite().
352*/
353static ssize_t ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){
354 if( tsIsFailErrno("pwrite") ){
355 return -1;
356 }
357 return orig_pwrite(fd, aBuf, nBuf, off);
358}
359
360/*
361** A wrapper around pwrite64().
362*/
363static ssize_t ts_pwrite64(int fd, const void *aBuf, size_t nBuf, sqlite3_uint64 off){
364 if( tsIsFailErrno("pwrite64") ){
365 return -1;
366 }
367 return orig_pwrite64(fd, aBuf, nBuf, off);
368}
369
370/*
371** A wrapper around fchmod().
372*/
373static int ts_fchmod(int fd, mode_t mode){
374 if( tsIsFail() ){
375 return -1;
376 }
377 return orig_fchmod(fd, mode);
378}
379
380/*
381** A wrapper around fallocate().
382**
383** SQLite assumes that the fallocate() function is compatible with
384** posix_fallocate(). According to the Linux man page (2009-09-30):
385**
386** posix_fallocate() returns zero on success, or an error number on
387** failure. Note that errno is not set.
388*/
389static int ts_fallocate(int fd, off_t off, off_t len){
390 if( tsIsFail() ){
391 return tsErrno("fallocate");
392 }
393 return orig_fallocate(fd, off, len);
394}
395
396static void *ts_mmap(
397 void *pAddr,
398 size_t nByte,
399 int prot,
400 int flags,
401 int fd,
402 off_t iOff
403){
404 if( tsIsFailErrno("mmap") ){
405 return MAP_FAILED;
406 }
407 return orig_mmap(pAddr, nByte, prot, flags, fd, iOff);
408}
409
410static void *ts_mremap(void *a, size_t b, size_t c, int d, ...){
411 if( tsIsFailErrno("mremap") ){
412 return MAP_FAILED;
413 }
414 /* mremap() may be called with 4 or 5 arguments (when MREMAP_FIXED) is
415 ** set. But SQLite only ever calls the 4 argument form, and attempting to
416 ** read another argument using va_arg(void*) when one has not been specified
417 ** causes problems for Fil-C. So assume the 4 argument form here. */
418#ifdef MREMAP_FIXED
419 assert( (d & MREMAP_FIXED)==0 );
420#endif
421 return orig_mremap(a, b, c, d);
422}
423
424static int SQLITE_TCLAPI test_syscall_install(
425 void * clientData,
426 Tcl_Interp *interp,
427 int objc,
428 Tcl_Obj *CONST objv[]
429){
430 sqlite3_vfs *pVfs;
431 Tcl_Size nElem;
432 int i;
433 Tcl_Obj **apElem;
434
435 if( objc!=3 ){
436 Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL-LIST");
437 return TCL_ERROR;
438 }
439 if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){
440 return TCL_ERROR;
441 }
442 pVfs = sqlite3_vfs_find(0);
443
444 for(i=0; i<(int)nElem; i++){
445 int iCall;
446 int rc = Tcl_GetIndexFromObjStruct(interp,
447 apElem[i], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
448 );
449 if( rc ) return rc;
450 if( aSyscall[iCall].xOrig==0 ){
451 aSyscall[iCall].xOrig = pVfs->xGetSystemCall(pVfs, aSyscall[iCall].zName);
452 pVfs->xSetSystemCall(pVfs, aSyscall[iCall].zName, aSyscall[iCall].xTest);
453 }
454 aSyscall[iCall].custom_errno = aSyscall[iCall].default_errno;
455 }
456
457 return TCL_OK;
458}
459
460static int SQLITE_TCLAPI test_syscall_uninstall(
461 void * clientData,
462 Tcl_Interp *interp,
463 int objc,
464 Tcl_Obj *CONST objv[]
465){
466 sqlite3_vfs *pVfs;
467 int i;
468
469 if( objc!=2 ){
470 Tcl_WrongNumArgs(interp, 2, objv, "");
471 return TCL_ERROR;
472 }
473
474 pVfs = sqlite3_vfs_find(0);
475 for(i=0; aSyscall[i].zName; i++){
476 if( aSyscall[i].xOrig ){
477 pVfs->xSetSystemCall(pVfs, aSyscall[i].zName, 0);
478 aSyscall[i].xOrig = 0;
479 }
480 }
481 return TCL_OK;
482}
483
484static int SQLITE_TCLAPI test_syscall_reset(
485 void * clientData,
486 Tcl_Interp *interp,
487 int objc,
488 Tcl_Obj *CONST objv[]
489){
490 sqlite3_vfs *pVfs;
491 int i;
492 int rc;
493
494 if( objc!=2 && objc!=3 ){
495 Tcl_WrongNumArgs(interp, 2, objv, "");
496 return TCL_ERROR;
497 }
498
499 pVfs = sqlite3_vfs_find(0);
500 if( objc==2 ){
501 rc = pVfs->xSetSystemCall(pVfs, 0, 0);
502 for(i=0; aSyscall[i].zName; i++) aSyscall[i].xOrig = 0;
503 }else{
504 Tcl_Size nFunc;
505 char *zFunc = Tcl_GetStringFromObj(objv[2], &nFunc);
506 rc = pVfs->xSetSystemCall(pVfs, Tcl_GetString(objv[2]), 0);
507 for(i=0; rc==SQLITE_OK && aSyscall[i].zName; i++){
508 if( strlen(aSyscall[i].zName)!=nFunc ) continue;
509 if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
510 aSyscall[i].xOrig = 0;
511 }
512 }
513 if( rc!=SQLITE_OK ){
514 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
515 return TCL_ERROR;
516 }
517
518 Tcl_ResetResult(interp);
519 return TCL_OK;
520}
521
522static int SQLITE_TCLAPI test_syscall_exists(
523 void * clientData,
524 Tcl_Interp *interp,
525 int objc,
526 Tcl_Obj *CONST objv[]
527){
528 sqlite3_vfs *pVfs;
529 sqlite3_syscall_ptr x;
530
531 if( objc!=3 ){
532 Tcl_WrongNumArgs(interp, 2, objv, "");
533 return TCL_ERROR;
534 }
535
536 pVfs = sqlite3_vfs_find(0);
537 x = pVfs->xGetSystemCall(pVfs, Tcl_GetString(objv[2]));
538
539 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(x!=0));
540 return TCL_OK;
541}
542
543static int SQLITE_TCLAPI test_syscall_fault(
544 void * clientData,
545 Tcl_Interp *interp,
546 int objc,
547 Tcl_Obj *CONST objv[]
548){
549 int nCount = 0;
550 int bPersist = 0;
551
552 if( objc!=2 && objc!=4 ){
553 Tcl_WrongNumArgs(interp, 2, objv, "?COUNT PERSIST?");
554 return TCL_ERROR;
555 }
556
557 if( objc==4 ){
558 if( Tcl_GetIntFromObj(interp, objv[2], &nCount)
559 || Tcl_GetBooleanFromObj(interp, objv[3], &bPersist)
560 ){
561 return TCL_ERROR;
562 }
563 }
564
565 Tcl_SetObjResult(interp, Tcl_NewIntObj(gSyscall.nFail));
566 gSyscall.nCount = nCount;
567 gSyscall.bPersist = bPersist;
568 gSyscall.nFail = 0;
569 return TCL_OK;
570}
571
572static int SQLITE_TCLAPI test_syscall_errno(
573 void * clientData,
574 Tcl_Interp *interp,
575 int objc,
576 Tcl_Obj *CONST objv[]
577){
578 int iCall;
579 int iErrno;
580 int rc;
581
582 struct Errno {
583 const char *z;
584 int i;
585 } aErrno[] = {
586 { "EACCES", EACCES },
587 { "EINTR", EINTR },
588 { "EIO", EIO },
589 { "EOVERFLOW", EOVERFLOW },
590 { "ENOMEM", ENOMEM },
591 { "EAGAIN", EAGAIN },
592 { "ETIMEDOUT", ETIMEDOUT },
593 { "EBUSY", EBUSY },
594 { "EPERM", EPERM },
595 { "EDEADLK", EDEADLK },
596 { "ENOLCK", ENOLCK },
597 { 0, 0 }
598 };
599
600 if( objc!=4 ){
601 Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL ERRNO");
602 return TCL_ERROR;
603 }
604
605 rc = Tcl_GetIndexFromObjStruct(interp,
606 objv[2], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
607 );
608 if( rc!=TCL_OK ) return rc;
609 rc = Tcl_GetIndexFromObjStruct(interp,
610 objv[3], aErrno, sizeof(aErrno[0]), "errno", 0, &iErrno
611 );
612 if( rc!=TCL_OK ) return rc;
613
614 aSyscall[iCall].custom_errno = aErrno[iErrno].i;
615 return TCL_OK;
616}
617
618static int SQLITE_TCLAPI test_syscall_list(
619 void * clientData,
620 Tcl_Interp *interp,
621 int objc,
622 Tcl_Obj *CONST objv[]
623){
624 const char *zSys;
625 sqlite3_vfs *pVfs;
626 Tcl_Obj *pList;
627
628 if( objc!=2 ){
629 Tcl_WrongNumArgs(interp, 2, objv, "");
630 return TCL_ERROR;
631 }
632
633 pVfs = sqlite3_vfs_find(0);
634 pList = Tcl_NewObj();
635 Tcl_IncrRefCount(pList);
636 for(zSys = pVfs->xNextSystemCall(pVfs, 0);
637 zSys!=0;
638 zSys = pVfs->xNextSystemCall(pVfs, zSys)
639 ){
640 Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(zSys, -1));
641 }
642
643 Tcl_SetObjResult(interp, pList);
644 Tcl_DecrRefCount(pList);
645 return TCL_OK;
646}
647
648static int SQLITE_TCLAPI test_syscall_defaultvfs(
649 void * clientData,
650 Tcl_Interp *interp,
651 int objc,
652 Tcl_Obj *CONST objv[]
653){
654 sqlite3_vfs *pVfs;
655
656 if( objc!=2 ){
657 Tcl_WrongNumArgs(interp, 2, objv, "");
658 return TCL_ERROR;
659 }
660
661 pVfs = sqlite3_vfs_find(0);
662 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVfs->zName, -1));
663 return TCL_OK;
664}
665
666static int ts_getpagesize(void){
667 return gSyscall.pgsz;
668}
669
670static int SQLITE_TCLAPI test_syscall_pagesize(
671 void * clientData,
672 Tcl_Interp *interp,
673 int objc,
674 Tcl_Obj *CONST objv[]
675){
676 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
677 int pgsz;
678 if( objc!=3 ){
679 Tcl_WrongNumArgs(interp, 2, objv, "PGSZ");
680 return TCL_ERROR;
681 }
682 if( Tcl_GetIntFromObj(interp, objv[2], &pgsz) ){
683 return TCL_ERROR;
684 }
685
686 if( pgsz<0 ){
687 if( gSyscall.orig_getpagesize ){
688 pVfs->xSetSystemCall(pVfs, "getpagesize", gSyscall.orig_getpagesize);
689 }
690 }else{
691 if( pgsz<512 || (pgsz & (pgsz-1)) ){
692 Tcl_AppendResult(interp, "pgsz out of range", NULL);
693 return TCL_ERROR;
694 }
695 gSyscall.orig_getpagesize = pVfs->xGetSystemCall(pVfs, "getpagesize");
696 gSyscall.pgsz = pgsz;
697 pVfs->xSetSystemCall(
698 pVfs, "getpagesize", (sqlite3_syscall_ptr)ts_getpagesize
699 );
700 }
701
702 return TCL_OK;
703}
704
705static int SQLITE_TCLAPI test_syscall(
706 void * clientData,
707 Tcl_Interp *interp,
708 int objc,
709 Tcl_Obj *CONST objv[]
710){
711 struct SyscallCmd {
712 const char *zName;
713 Tcl_ObjCmdProc *xCmd;
714 } aCmd[] = {
715 { "fault", test_syscall_fault },
716 { "install", test_syscall_install },
717 { "uninstall", test_syscall_uninstall },
718 { "reset", test_syscall_reset },
719 { "errno", test_syscall_errno },
720 { "exists", test_syscall_exists },
721 { "list", test_syscall_list },
722 { "defaultvfs", test_syscall_defaultvfs },
723 { "pagesize", test_syscall_pagesize },
724 { 0, 0 }
725 };
726 int iCmd;
727 int rc;
728 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
729
730 if( objc<2 ){
731 Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ...");
732 return TCL_ERROR;
733 }
734 if( pVfs->iVersion<3 || pVfs->xSetSystemCall==0 ){
735 Tcl_AppendResult(interp, "VFS does not support xSetSystemCall", NULL);
736 rc = TCL_ERROR;
737 }else{
738 rc = Tcl_GetIndexFromObjStruct(interp,
739 objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iCmd
740 );
741 }
742 if( rc!=TCL_OK ) return rc;
743 return aCmd[iCmd].xCmd(clientData, interp, objc, objv);
744}
745
746int SqlitetestSyscall_Init(Tcl_Interp *interp){
747 struct SyscallCmd {
748 const char *zName;
749 Tcl_ObjCmdProc *xCmd;
750 } aCmd[] = {
751 { "test_syscall", test_syscall},
752 };
753 int i;
754
755 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
756 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xCmd, 0, 0);
757 }
758 return TCL_OK;
759}
760#else
761int SqlitetestSyscall_Init(Tcl_Interp *interp){
762 return TCL_OK;
763}
764#endif