在程序集中进行Linux sigaction系统调用时出现问题
我正在学习Linux系统调用,调用系统调用13时遇到问题: rt_sigaction(int sig,const struct sigaction\u用户* act,结构SIGATION(用户*oact,大小sigsetsize) 在64位Linux上进行汇编。我已经为这个函数使用了C包装器(sigaction()),它工作得很好,但是当我尝试使用下面的代码进行直接系统调用时失败了。我用这种方法进行了其他直接系统调用,只有这个系统调用失败 我怀疑指向sigint_handler()函数的指针被错误地传递到内核,但我不确定 我不能在我的项目中使用C系统库,所以让这个特定的系统调用与程序集调用一起工作是我的目标 我正在运行的C代码:在程序集中进行Linux sigaction系统调用时出现问题,c,linux,assembly,signals,system-calls,C,Linux,Assembly,Signals,System Calls,我正在学习Linux系统调用,调用系统调用13时遇到问题: rt_sigaction(int sig,const struct sigaction\u用户* act,结构SIGATION(用户*oact,大小sigsetsize) 在64位Linux上进行汇编。我已经为这个函数使用了C包装器(sigaction()),它工作得很好,但是当我尝试使用下面的代码进行直接系统调用时失败了。我用这种方法进行了其他直接系统调用,只有这个系统调用失败 我怀疑指向sigint_handler()函数的指针被错
#define _POSIX_SOURCE
#include <stddef.h>
#include <stdint.h>
#include <linux/signal.h>
#include <linux/unistd.h>
#include <string.h>
#include <syscall.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
// SYSCALL_4 simply injects the assembly to make the syscall directly
#define SYSCALL_4(sys_num, a1,a2,a3,a4) \ ({ \
int64_t sys_ret; \
__asm __volatile ( \
"movq %1, %%rax\n\t"\
"movq %2, %%rdi\n\t"\
"movq %3, %%rsi\n\t"\
"movq %4, %%rdx\n\t"\
"movq %5, %%r10\n\t"\
"syscall\n\t" \
"movq %%rax, %0\n\t"\
: "=g" (sys_ret)\
: "g"(sys_num), "g" (a1) , "g" (a2), "g" (a3), "g" (a4)\
: "cc", "memory", "%rax", "%rdi", "%rsi", "%rdx", "%rcx", "%r11", "%r10"\ );\
sys_ret; \ })
struct kernel_sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
void sigint_handler(int signo) {
printf("In SIGINT handler ...\n");
}
int main(void) {
struct kernel_sigaction sig;
int64_t ret = SYSCALL_4(13, SIGINT, 0, &sig, sizeof(sigset_t)); //__NR_rt_sigaction
if (ret < 0)
{
fprintf(stderr,"Unable to install SIGINT handler\n");
errno = -ret;
perror(0);
exit(1);
}
sig.sa_handler = sigint_handler;
ret = SYSCALL_4(13, SIGINT, &sig, 0, sizeof(sigset_t)); //__NR_rt_sigaction
if (ret < 0) //doesnt fail here
{
fprintf(stderr,"Unable to install SIGINT handler\n");
errno = -ret;
perror(0);
exit(1);
}
printf("Sleeping for 30 seconds ...\n");
sleep(30); //seems to fail here
printf("Waking up ...\n"); //supposed to print after SIGINT handler
exit(0);
}
参见
struct kernel\u sigaction
也是错误的,它是union{sa\u handler
,sa\u sigaction
}参见struct kernel\u sigaction
也是错误的,它是union{sa\u handler
,sa\u sigaction
}
> gcc -Wall -march=native -std=c11 -O0 P1c_inline.c -o P1c_inline.out
> ./P1c_inline.out
Sleeping for 30 seconds ...
^CSegmentation fault (core dumped)