我的计划如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
int main()
{
struct sigaction new_sa;
struct sigaction old_sa;
sigfillset(&new_sa.sa_mask);
new_sa.sa_handler = SIG_IGN;
new_sa.sa_flags = 0;
int input;
if (sigaction(SIGTERM, &new_sa, &old_sa) == 0 && old_sa.sa_handler != SIG_IGN) {
new_sa.sa_handler = NULL;
sigaction(SIGTERM, &new_sa, 0);
}
printf("Pgm is running\n");
while (1) {
printf("Enter input\n");
scanf("%d", &input);
if (!input) {
/* I actually call a libraries API which calls exit()
* Just calling exit here for simpilicity */
exit(1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想处理/忽略退出系统调用生成的SIGTERM.可能吗?我没办法避免调用exit,因为它实际上是一个试图退出我想要避免的程序的库调用.
查看退出的glibc 源代码,看起来这是不可能的。但如果您使用另一个 C std 库,则可能是这样。
你可以在_exit处做一些事情,但你无法阻止它。
[编辑]
由于这个问题的原因,这里下面的所有内容显然不适用于退出。
如果您使用 gnu ld ,则可能可以覆盖__run_exit_handlers,或者exit就此而言,使用 gnu ld--wrap选项,但我还没有尝试过。
如果您可以使用 gnu ld 您可以这样做--wrap exit,然后__wrap_exit()在您的代码中实现。如果您想在此之后致电,exit您可以通过 访问它__real_exit()。这是一个 gnu ld 功能,我不确定它是否普遍可用。
| 归档时间: |
|
| 查看次数: |
127 次 |
| 最近记录: |