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