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