如果我尝试在信号处理程序中调用开放系统,则进行代码转储

时间:2013-04-02 10:22:54

标签: c linux linux-kernel signals

我收到信号时需要模拟系统调用。我试图调用一个开放的系统调用,但是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;

}

2 个答案:

答案 0 :(得分:2)

  • 首先:你不应该在信号处理程序中使用printf()。 printf()不是信号安全的。

  • 第二:你对open()的调用毫无意义。你正在泄漏filedescriptor,在关闭之前你甚至没有使用

[张贴作为答案,因为我无法发表评论]

答案 1 :(得分:0)

您通过调用信号处理程序中的不安全函数来调用未定义的行为,除非您的信号没有中断不安全的函数。