如何拦截Linux的信号? (C语言) [英] how to intercept linux signals ? (in C)
本文介绍了如何拦截Linux的信号? (C语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从任何二进制拦截和跟踪信号,诸如strace确实是在linux下。
我不需要像真正的strace的一个如此冗长的输出。
我只是想知道它是如何工作的,我怎么可以拦截信号,我怎么可以跟踪它们。
在此先感谢:)
I need to intercept and trace signals from any binaries, like strace does it under linux. I don't need a so verbose output like the real one strace. I just want to know how it works, how can I intercept signal and how can I trace them. Thanks in advance :)
推荐答案
strace的使用 ptrace的()
系统调用追踪,这也让您能够拦截(也可能是操纵)发送到该进程的信号。
strace uses the ptrace()
system call for tracing, which also allows you to intercept (and possibly manipulate) signals sent to the process.
下面是一个小例子:
#include <sys/ptrace.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
/* simple example, child is traced, uses alarm which causes a signal to be
* set up */
pid_t child;
child = fork();
if (child == 0)
{
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
alarm(3);
while(1)
{
}
exit(0);
}
/* parent */
while(1)
{
int wstatus;
int signum;
wait(&wstatus);
if (WIFEXITED(wstatus) || WIFSIGNALED(wstatus))
break;
signum = WSTOPSIG(wstatus);
printf("child stopped with signal %d\n", signum);
/* resume execution */
ptrace(PTRACE_CONT, child, NULL, signum);
}
return 0;
}
这篇关于如何拦截Linux的信号? (C语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文