是否可以使任意程序忽略信号? [英] Is it possible to make an arbitrary program ignore signals?

查看:117
本文介绍了是否可以使任意程序忽略信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特别是在Mac OS X上,是否可以通过DYLD_INSERT_LIBRARIES使程序忽略SIGTERM,从而对任何或大多数程序都起作用?

Specifically on Mac OS X, is it possible to make a program ignore SIGTERM via DYLD_INSERT_LIBRARIES, in a way which works for any or most programs?

我尝试编译并插入此代码:

I tried compiling and inserting this:

#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signo)
{
    if (signo == SIGTERM)
        printf("received SIGTERM\n");
}

int main(void)
{
    signal(SIGTERM, sig_handler);
    return 0;
}

但是,

DYLD_INSERT_LIBRARIES=libignore.dylib sleep 60

能够被杀死-15点而没有问题.

was able to be kill -15'd without issue.

推荐答案

您可以创建一个可执行文件,将SIGTERM的操作设置为SIG_IGN,然后将

You can create an executable that sets the action for SIGTERM to SIG_IGN and then execvp() the program you would like to run with that signal ignored:

在调用过程映像中设置为默认操作(SIG_DFL)的信号应在新过程映像中设置为默认操作.除SIGCHLD之外,应将调用过程映像设置为忽略的信号(SIG_IGN)设置为新过程映像忽略.设置为由调用过程映像捕获的信号应设置为新过程映像中的默认操作

Signals set to the default action (SIG_DFL) in the calling process image shall be set to the default action in the new process image. Except for SIGCHLD, signals set to be ignored (SIG_IGN) by the calling process image shall be set to be ignored by the new process image. Signals set to be caught by the calling process image shall be set to the default action in the new process image

即使使用bash,您也可以这样做:

You can do that even with bash:

#!/bin/bash
# no-sigterm.sh
trap "" SIGTERM 
exec "$@"

然后:

no-sigterm.sh sleep 60

这篇关于是否可以使任意程序忽略信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆