Is it possible to make an arbitrary program ignore signals?

孤人 提交于 2019-12-04 19:45:48

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:

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

You can do that even with bash:

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

And then:

no-sigterm.sh sleep 60

It is possible, but not the way you are doing it. Specifically:

  • You need to compile as a library. That means: - no main() , as in your sample. - an __attribute(constructor) for the entry point. And setting the sig handler there

  • The program you're injecting into must itself not set a signal handler after your injected library does (i.e. no call to signal() in the library. If it does, it will override your handler. Of course, you can get around this concern if you also provide a mock signal implementation that does nothing via function interposing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!