C/Code snippet
[C] SA_SIGINFO 예제
2022. 5. 9. 23:54반응형
Listing 3. Using the SA_SIGINFO Option
#include <stdio.h>
#include <signal.h>
#include <execinfo.h>
/* get REG_EIP from ucontext.h */
#define __USE_GNU
#include <ucontext.h>
void bt_sighandler(int sig, siginfo_t *info,
void *secret) {
void *trace[16];
char **messages = (char **)NULL;
int i, trace_size = 0;
ucontext_t *uc = (ucontext_t *)secret;
/* Do something useful with siginfo_t */
if (sig == SIGSEGV)
printf("Got signal %d, faulty address is %p, "
"from %p\n", sig, info->si_addr,
uc->uc_mcontext.gregs[REG_EIP]);
else
printf("Got signal %d#92;n", sig);
trace_size = backtrace(trace, 16);
/* overwrite sigaction with caller's address */
trace[1] = (void *) uc->uc_mcontext.gregs[REG_EIP];
messages = backtrace_symbols(trace, trace_size);
/* skip first stack frame (points here) */
printf("[bt] Execution path:#92;n");
for (i=1; i<trace_size; ++i)
printf("[bt] %s#92;n", messages[i]);
exit(0);
}
int func_a(int a, char b) {
char *p = (char *)0xdeadbeef;
a = a + b;
*p = 10; /* CRASH here!! */
return 2*a;
}
int func_b() {
int res, a = 5;
res = 5 + func_a(a, 't');
return res;
}
int main() {
/* Install our signal handler */
struct sigaction sa;
sa.sa_sigaction = (void *)bt_sighandler;
sigemptyset (&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
/* ... add any other signal here */
/* Do something */
printf("%d#92;n", func_b());
}
You set up your signal handler with sigaction using the SA_SIGINFO flag. Your handler will accept a parameter of siginfo_t. Within the siginfo_t struct is the field si_pid. This is the process id of the sending process. Match that against the child's ppid().
https://stackoverflow.com/questions/24617673/finding-the-process-which-sent-signal-in-c
출처/참고자료
https://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/063/6391/6391l3.html
반응형
'C > Code snippet' 카테고리의 다른 글
[Linux/C] strcat 사용시 주의사항 (0) | 2022.05.14 |
---|---|
[C] C언어 문자열 나누기 (strtok 사용) (0) | 2022.05.10 |
[C] sleep 함수로 2초마다 printf 찍게 하기 (리눅스 C) (0) | 2022.05.05 |
[C] 파일 읽기 간단한 예제 (0) | 2022.05.02 |
[C] argc와 argv의 관계 (0) | 2022.04.30 |