This source file includes following definitions.
- tsIsFail
- tsErrno
- tsIsFailErrno
- ts_open
- ts_close
- ts_access
- ts_getcwd
- ts_stat
- ts_fstat
- ts_ftruncate
- ts_fcntl
- ts_read
- ts_pread
- ts_pread64
- ts_write
- ts_pwrite
- ts_pwrite64
- ts_fchmod
- ts_fallocate
- test_syscall_install
- test_syscall_uninstall
- test_syscall_reset
- test_syscall_exists
- test_syscall_fault
- test_syscall_errno
- test_syscall_list
- test_syscall_defaultvfs
- test_syscall
- SqlitetestSyscall_Init
- SqlitetestSyscall_Init
#include "sqlite3.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "sqliteInt.h"
#if SQLITE_OS_UNIX
extern const char *sqlite3TestErrorName(int);
#include <sys/types.h>
#include <errno.h>
static struct TestSyscallGlobal {
int bPersist;
int nCount;
int nFail;
} gSyscall = { 0, 0 };
static int ts_open(const char *, int, int);
static int ts_close(int fd);
static int ts_access(const char *zPath, int mode);
static char *ts_getcwd(char *zPath, size_t nPath);
static int ts_stat(const char *zPath, struct stat *p);
static int ts_fstat(int fd, struct stat *p);
static int ts_ftruncate(int fd, off_t n);
static int ts_fcntl(int fd, int cmd, ... );
static int ts_read(int fd, void *aBuf, size_t nBuf);
static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off);
static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off);
static int ts_write(int fd, const void *aBuf, size_t nBuf);
static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off);
static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off);
static int ts_fchmod(int fd, mode_t mode);
static int ts_fallocate(int fd, off_t off, off_t len);
struct TestSyscallArray {
const char *zName;
sqlite3_syscall_ptr xTest;
sqlite3_syscall_ptr xOrig;
int default_errno;
int custom_errno;
} aSyscall[] = {
{ "open", (sqlite3_syscall_ptr)ts_open, 0, EACCES, 0 },
{ "close", (sqlite3_syscall_ptr)ts_close, 0, 0, 0 },
{ "access", (sqlite3_syscall_ptr)ts_access, 0, 0, 0 },
{ "getcwd", (sqlite3_syscall_ptr)ts_getcwd, 0, 0, 0 },
{ "stat", (sqlite3_syscall_ptr)ts_stat, 0, 0, 0 },
{ "fstat", (sqlite3_syscall_ptr)ts_fstat, 0, 0, 0 },
{ "ftruncate", (sqlite3_syscall_ptr)ts_ftruncate, 0, EIO, 0 },
{ "fcntl", (sqlite3_syscall_ptr)ts_fcntl, 0, EACCES, 0 },
{ "read", (sqlite3_syscall_ptr)ts_read, 0, 0, 0 },
{ "pread", (sqlite3_syscall_ptr)ts_pread, 0, 0, 0 },
{ "pread64", (sqlite3_syscall_ptr)ts_pread64, 0, 0, 0 },
{ "write", (sqlite3_syscall_ptr)ts_write, 0, 0, 0 },
{ "pwrite", (sqlite3_syscall_ptr)ts_pwrite, 0, 0, 0 },
{ "pwrite64", (sqlite3_syscall_ptr)ts_pwrite64, 0, 0, 0 },
{ "fchmod", (sqlite3_syscall_ptr)ts_fchmod, 0, 0, 0 },
{ "fallocate", (sqlite3_syscall_ptr)ts_fallocate, 0, 0, 0 },
{ 0, 0, 0, 0, 0 }
};
#define orig_open ((int(*)(const char *, int, int))aSyscall[0].xOrig)
#define orig_close ((int(*)(int))aSyscall[1].xOrig)
#define orig_access ((int(*)(const char*,int))aSyscall[2].xOrig)
#define orig_getcwd ((char*(*)(char*,size_t))aSyscall[3].xOrig)
#define orig_stat ((int(*)(const char*,struct stat*))aSyscall[4].xOrig)
#define orig_fstat ((int(*)(int,struct stat*))aSyscall[5].xOrig)
#define orig_ftruncate ((int(*)(int,off_t))aSyscall[6].xOrig)
#define orig_fcntl ((int(*)(int,int,...))aSyscall[7].xOrig)
#define orig_read ((ssize_t(*)(int,void*,size_t))aSyscall[8].xOrig)
#define orig_pread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].xOrig)
#define orig_pread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].xOrig)
#define orig_write ((ssize_t(*)(int,const void*,size_t))aSyscall[11].xOrig)
#define orig_pwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
aSyscall[12].xOrig)
#define orig_pwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\
aSyscall[13].xOrig)
#define orig_fchmod ((int(*)(int,mode_t))aSyscall[14].xOrig)
#define orig_fallocate ((int(*)(int,off_t,off_t))aSyscall[15].xOrig)
static int tsIsFail(void){
gSyscall.nCount--;
if( gSyscall.nCount==0 || (gSyscall.nFail && gSyscall.bPersist) ){
gSyscall.nFail++;
return 1;
}
return 0;
}
static int tsErrno(const char *zFunc){
int i;
int nFunc = strlen(zFunc);
for(i=0; aSyscall[i].zName; i++){
if( strlen(aSyscall[i].zName)!=nFunc ) continue;
if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
return aSyscall[i].custom_errno;
}
assert(0);
return 0;
}
static int tsIsFailErrno(const char *zFunc){
if( tsIsFail() ){
errno = tsErrno(zFunc);
return 1;
}
return 0;
}
static int ts_open(const char *zFile, int flags, int mode){
if( tsIsFailErrno("open") ){
return -1;
}
return orig_open(zFile, flags, mode);
}
static int ts_close(int fd){
if( tsIsFail() ){
orig_close(fd);
return -1;
}
return orig_close(fd);
}
static int ts_access(const char *zPath, int mode){
if( tsIsFail() ){
return -1;
}
return orig_access(zPath, mode);
}
static char *ts_getcwd(char *zPath, size_t nPath){
if( tsIsFail() ){
return NULL;
}
return orig_getcwd(zPath, nPath);
}
static int ts_stat(const char *zPath, struct stat *p){
if( tsIsFail() ){
return -1;
}
return orig_stat(zPath, p);
}
static int ts_fstat(int fd, struct stat *p){
if( tsIsFailErrno("fstat") ){
return -1;
}
return orig_fstat(fd, p);
}
static int ts_ftruncate(int fd, off_t n){
if( tsIsFailErrno("ftruncate") ){
return -1;
}
return orig_ftruncate(fd, n);
}
static int ts_fcntl(int fd, int cmd, ... ){
va_list ap;
void *pArg;
if( tsIsFailErrno("fcntl") ){
return -1;
}
va_start(ap, cmd);
pArg = va_arg(ap, void *);
return orig_fcntl(fd, cmd, pArg);
}
static int ts_read(int fd, void *aBuf, size_t nBuf){
if( tsIsFailErrno("read") ){
return -1;
}
return orig_read(fd, aBuf, nBuf);
}
static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){
if( tsIsFailErrno("pread") ){
return -1;
}
return orig_pread(fd, aBuf, nBuf, off);
}
static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off){
if( tsIsFailErrno("pread64") ){
return -1;
}
return orig_pread64(fd, aBuf, nBuf, off);
}
static int ts_write(int fd, const void *aBuf, size_t nBuf){
if( tsIsFailErrno("write") ){
return -1;
}
return orig_write(fd, aBuf, nBuf);
}
static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){
if( tsIsFailErrno("pwrite") ){
return -1;
}
return orig_pwrite(fd, aBuf, nBuf, off);
}
static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off){
if( tsIsFailErrno("pwrite64") ){
return -1;
}
return orig_pwrite64(fd, aBuf, nBuf, off);
}
static int ts_fchmod(int fd, mode_t mode){
if( tsIsFail() ){
return -1;
}
return orig_fchmod(fd, mode);
}
static int ts_fallocate(int fd, off_t off, off_t len){
if( tsIsFail() ){
return tsErrno("fallocate");
}
return orig_fallocate(fd, off, len);
}
static int test_syscall_install(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3_vfs *pVfs;
int nElem;
int i;
Tcl_Obj **apElem;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL-LIST");
return TCL_ERROR;
}
if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){
return TCL_ERROR;
}
pVfs = sqlite3_vfs_find(0);
for(i=0; i<nElem; i++){
int iCall;
int rc = Tcl_GetIndexFromObjStruct(interp,
apElem[i], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
);
if( rc ) return rc;
if( aSyscall[iCall].xOrig==0 ){
aSyscall[iCall].xOrig = pVfs->xGetSystemCall(pVfs, aSyscall[iCall].zName);
pVfs->xSetSystemCall(pVfs, aSyscall[iCall].zName, aSyscall[iCall].xTest);
}
aSyscall[iCall].custom_errno = aSyscall[iCall].default_errno;
}
return TCL_OK;
}
static int test_syscall_uninstall(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3_vfs *pVfs;
int i;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 2, objv, "");
return TCL_ERROR;
}
pVfs = sqlite3_vfs_find(0);
for(i=0; aSyscall[i].zName; i++){
if( aSyscall[i].xOrig ){
pVfs->xSetSystemCall(pVfs, aSyscall[i].zName, 0);
aSyscall[i].xOrig = 0;
}
}
return TCL_OK;
}
static int test_syscall_reset(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3_vfs *pVfs;
int i;
int rc;
if( objc!=2 && objc!=3 ){
Tcl_WrongNumArgs(interp, 2, objv, "");
return TCL_ERROR;
}
pVfs = sqlite3_vfs_find(0);
if( objc==2 ){
rc = pVfs->xSetSystemCall(pVfs, 0, 0);
for(i=0; aSyscall[i].zName; i++) aSyscall[i].xOrig = 0;
}else{
int nFunc;
char *zFunc = Tcl_GetStringFromObj(objv[2], &nFunc);
rc = pVfs->xSetSystemCall(pVfs, Tcl_GetString(objv[2]), 0);
for(i=0; rc==SQLITE_OK && aSyscall[i].zName; i++){
if( strlen(aSyscall[i].zName)!=nFunc ) continue;
if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
aSyscall[i].xOrig = 0;
}
}
if( rc!=SQLITE_OK ){
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3TestErrorName(rc), -1));
return TCL_ERROR;
}
Tcl_ResetResult(interp);
return TCL_OK;
}
static int test_syscall_exists(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3_vfs *pVfs;
sqlite3_syscall_ptr x;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 2, objv, "");
return TCL_ERROR;
}
pVfs = sqlite3_vfs_find(0);
x = pVfs->xGetSystemCall(pVfs, Tcl_GetString(objv[2]));
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(x!=0));
return TCL_OK;
}
static int test_syscall_fault(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int nCount = 0;
int bPersist = 0;
if( objc!=2 && objc!=4 ){
Tcl_WrongNumArgs(interp, 2, objv, "?COUNT PERSIST?");
return TCL_ERROR;
}
if( objc==4 ){
if( Tcl_GetIntFromObj(interp, objv[2], &nCount)
|| Tcl_GetBooleanFromObj(interp, objv[3], &bPersist)
){
return TCL_ERROR;
}
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(gSyscall.nFail));
gSyscall.nCount = nCount;
gSyscall.bPersist = bPersist;
gSyscall.nFail = 0;
return TCL_OK;
}
static int test_syscall_errno(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int iCall;
int iErrno;
int rc;
struct Errno {
const char *z;
int i;
} aErrno[] = {
{ "EACCES", EACCES },
{ "EINTR", EINTR },
{ "EIO", EIO },
{ "EOVERFLOW", EOVERFLOW },
{ "ENOMEM", ENOMEM },
{ "EAGAIN", EAGAIN },
{ "ETIMEDOUT", ETIMEDOUT },
{ "EBUSY", EBUSY },
{ "EPERM", EPERM },
{ "EDEADLK", EDEADLK },
{ "ENOLCK", ENOLCK },
{ 0, 0 }
};
if( objc!=4 ){
Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL ERRNO");
return TCL_ERROR;
}
rc = Tcl_GetIndexFromObjStruct(interp,
objv[2], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
);
if( rc!=TCL_OK ) return rc;
rc = Tcl_GetIndexFromObjStruct(interp,
objv[3], aErrno, sizeof(aErrno[0]), "errno", 0, &iErrno
);
if( rc!=TCL_OK ) return rc;
aSyscall[iCall].custom_errno = aErrno[iErrno].i;
return TCL_OK;
}
static int test_syscall_list(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
const char *zSys;
sqlite3_vfs *pVfs;
Tcl_Obj *pList;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 2, objv, "");
return TCL_ERROR;
}
pVfs = sqlite3_vfs_find(0);
pList = Tcl_NewObj();
Tcl_IncrRefCount(pList);
for(zSys = pVfs->xNextSystemCall(pVfs, 0);
zSys!=0;
zSys = pVfs->xNextSystemCall(pVfs, zSys)
){
Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(zSys, -1));
}
Tcl_SetObjResult(interp, pList);
Tcl_DecrRefCount(pList);
return TCL_OK;
}
static int test_syscall_defaultvfs(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3_vfs *pVfs;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 2, objv, "");
return TCL_ERROR;
}
pVfs = sqlite3_vfs_find(0);
Tcl_SetObjResult(interp, Tcl_NewStringObj(pVfs->zName, -1));
return TCL_OK;
}
static int test_syscall(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
struct SyscallCmd {
const char *zName;
Tcl_ObjCmdProc *xCmd;
} aCmd[] = {
{ "fault", test_syscall_fault },
{ "install", test_syscall_install },
{ "uninstall", test_syscall_uninstall },
{ "reset", test_syscall_reset },
{ "errno", test_syscall_errno },
{ "exists", test_syscall_exists },
{ "list", test_syscall_list },
{ "defaultvfs", test_syscall_defaultvfs },
{ 0, 0 }
};
int iCmd;
int rc;
if( objc<2 ){
Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ...");
return TCL_ERROR;
}
rc = Tcl_GetIndexFromObjStruct(interp,
objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iCmd
);
if( rc!=TCL_OK ) return rc;
return aCmd[iCmd].xCmd(clientData, interp, objc, objv);
}
int SqlitetestSyscall_Init(Tcl_Interp *interp){
struct SyscallCmd {
const char *zName;
Tcl_ObjCmdProc *xCmd;
} aCmd[] = {
{ "test_syscall", test_syscall},
};
int i;
for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xCmd, 0, 0);
}
return TCL_OK;
}
#else
int SqlitetestSyscall_Init(Tcl_Interp *interp){
return TCL_OK;
}
#endif