当我收到信号时,我需要模拟系统调用。我试图调用一个开放的系统调用,但 reuslt 是一个核心转储,我不明白为什么。
我正在使用的代码是这样的:
static void emulator(int nr, siginfo_t *siginfo, void *void_context)
{
ucontext_t *ctx = (ucontext_t *)(void_context);
int syscall_n;
char *buf;
ssize_t bytes;
size_t len;
long long int syscall_addr;
long int arg0=0,arg1=0,arg2=0,arg3=0,arg4=0,arg5=0, ret=0;
if (siginfo->si_code != SYS_SECCOMP)
return;
if (!ctx)
return;
syscall_n = ctx->uc_mcontext.gregs[REG_SYSCALL];
arg0 = ctx->uc_mcontext.gregs[REG_ARG0];
arg1 = ctx->uc_mcontext.gregs[REG_ARG1];
arg2 = ctx->uc_mcontext.gregs[REG_ARG2];
arg3 = ctx->uc_mcontext.gregs[REG_ARG3];
arg4 = ctx->uc_mcontext.gregs[REG_ARG4];
arg5 = ctx->uc_mcontext.gregs[REG_ARG5];
syscall_addr= (long int)siginfo->si_call_addr;
printf("Arg0 = %d \n", arg0);
printf("Arg1 = %d \n", arg1);
printf("Arg2 = %d \n", arg2);
printf("Arg3 = %d \n", arg3);
printf("Arg4 = %d \n", arg4);
printf("Arg5 = %d \n", arg5);
//ret=syscall(syscall_n, arg0,arg1,arg2,arg3,arg4,arg5);
//ret=syscall(SYS_open,"testfile.txt", 2);
open("test.p", O_RDWR);
getpid();
ctx->uc_mcontext.gregs[REG_RESULT]=ret;
return;
}
static int install_emulator(void)
{
struct sigaction act;
sigset_t mask;
memset(&act, 0, sizeof(act));
sigemptyset(&mask);
sigaddset(&mask, SIGSYS);
act.sa_sigaction = &emulator;
/*specify to use sigaction as handler*/
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGSYS, &act, NULL) < 0) {
perror("sigaction");
return -1;
}
if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) {
perror("sigprocmask");
return -1;
}
return 0;
}