简体   繁体   English

在 linux 中链接信号处理程序的正确方法?

[英]Proper way to chain signal handlers in linux?

I've got a couple signal handlers I'm using to exit my program cleanly, and I'd like to play nicely with whatever else has hooked them by chaining the signal handlers calls.我有几个信号处理程序用来干净地退出我的程序,我想通过链接信号处理程序调用来很好地处理其他任何钩住它们的东西。 I'm using sigaction per the man page for signal saying it's not preferred anymore.我根据手册页使用 sigaction 来表示它不再是首选的信号。

Looking at the sigaction struct, there's signal masks and such that are specified, along with several flags.查看 sigaction 结构,有指定的信号掩码等,以及几个标志。 What's the "right" way to call the currently installed handler so that all those options are taken into account?调用当前安装的处理程序以便考虑所有这些选项的“正确”方法是什么?

The answer is "it depends": on what the signal handlers do :答案是“这取决于”:取决于信号处理程序的作用

  • The first reaction from many will be that a signal handler will be used to handle a SIGINT , etc., and terminate a program cleanly.许多人的第一反应是将使用信号处理程序来处理SIGINT等,并干净地终止程序。
  • On the other hand, there are (more or less) benign signals such as SIGWINCH (which you would like to not stop your program).在另一方面,也有(或多或少)良性的信号,例如SIGWINCH (你想中断程序)。

In "terminate a program cleanly", there may not be room for other programs to do something useful.在“干净地终止程序”中,其他程序可能没有空间做一些有用的事情。 For instance, the proposed chained-handler may close files that you rely upon.例如,建议的链式处理程序可能会关闭您依赖的文件。 So you have to start with a good knowledge of what the other handlers do.因此,您必须首先了解其他处理程序的工作。

If the signal handler is in the latter class, simply updating a variable which the application can test, then the signal handler function is just another function.如果信号处理程序在后一个类中,只需更新应用程序可以测试的变量,那么信号处理程序函数只是另一个函数。 When you call signal to associate a signal with a function, that returns the previous handler (which may be one of the magic values, eg,. SIG_DFL , SIG_IGN ).当您调用signal将信号与函数关联时,它会返回前一个处理程序(可能是魔术值之一,例如SIG_DFLSIG_IGN )。 If you save that, it's possible to check if it is none of those, and (presumably) a genuine function.如果您保存它,则可以检查它是否不是这些,并且(大概)是真正的功能。 Calling that function would continue execution as if it were part of your current signal handler.调用函数将继续执行,就好像它是当前信号处理程序的一部分一样。

It is not a new idea (I used it in the late 1990s), and is discussed occasionally:这不是一个新想法(我在 1990 年代后期使用过),偶尔会被讨论:

And of course:当然:

Well, the proper answer IMO is "Don't do that".好吧,IMO 的正确答案是“不要那样做”。 I think you should reconsider if you plan to chain signal handlers.我认为您应该重新考虑是否计划链接信号处理程序。

Basically, if you have something so critical it has to be cleaned up even if a fatal signal arrives, then you should do that part quickly and reset the signal handlers before letting any other code run.基本上,如果您有一些非常重要的事情,即使致命信号到达也必须清理,那么您应该快速完成该部分并在让任何其他代码运行之前重置信号处理程序。

Signals such as SIGTERM and SIGQUIT should be handled by events that terminate your program in the normal fashion. SIGTERM 和 SIGQUIT 等信号应由以正常方式终止程序的事件处理。 Typically your signal handler writes on a pipe to message the normal event loop in the application, or sets a global variable if you don't have an event loop.通常,您的信号处理程序写入管道以向应用程序中的正常事件循环发送消息,或者如果您没有事件循环,则设置一个全局变量。

Perhaps you know this, but please also make sure to read the list of functions that are safe to call from a signal handler.也许您知道这一点,但也请务必阅读可以安全地从信号处理程序调用的函数列表。 It should be in the man page.它应该在手册页中。 Anything except for that list is not safe.除了那个列表之外的任何东西都是不安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM