确定函数是否异步信号安全(可以在信号处理程序中调用) [英] Determine if a function is async-signal-safe (can be called inside a signal handler)

查看:365
本文介绍了确定函数是否异步信号安全(可以在信号处理程序中调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:

  1. 如果您无权访问某个函数的实现,是否有办法确定该函数是否异步信号安全?
  2. 如果没有,是否有一种方法可以测试函数是否足够异步信号安全以从信号处理程序中调用?

如果您阅读signal()或sigaction()的手册页,则会获得异步信号安全函数(可以在信号处理程序中安全调用的函数)的列表.但是,我认为此列表并不详尽.例如,下面的页面 http://linux.die.net/man/7/signal 异步信号安全功能标头,内容为:

If you reads the man pages of signal() or sigaction(), you get a list of async-signal-safe functions (functions that can be safely called inside a signal handler). However, I believe that this list is not exhaustive. For example, the following page http://linux.die.net/man/7/signal, under the Async-signal-safe functions header, reads:

POSIX.1-2004(也称为POSIX.1-2001技术更正2)要求实现以确保可以在信号处理程序中安全地调用以下功能:

POSIX.1-2004 (also known as POSIX.1-2001 Technical Corrigendum 2) requires an implementation to guarantee that the following functions can be safely called inside a signal handler:

然后继续列出上面的手册页中列出的常规异步信号安全功能.在我阅读该书时,它说的是需要",而不是这些是唯一的".

And then it proceeds to list the normal async-signal-safe functions listed in the man pages above. As I read it, it says "it requires", not "these are the only ones".

例如,此站点表示back_trace_symbols_fd()是异步信号安全的.该函数从dladdr()获取数据,并且不像back_trace_symbols()那样使用malloc(),因此看起来很安全.另外,我进行了一些测试,并且dladdr()的输出结构包含char *变量,但这些变量在运行时未分配.它们指向的char字符串甚至在调用dladdr()之前就在运行时存在.

For example, this site says that back_trace_symbols_fd() is async-signal safe. That function obtains is data from dladdr() and it doesn't use malloc() like back_trace_symbols(), so it looks like it may be safe. Also, I did some testing, and the output struct of dladdr() contains char* variables, but these are NOT malloc'ed at runtime. The char string they point to exists at run-time even before dladdr() is called.

任何能将我指向正确方向的想法或想法都会受到赞赏.

Any thoughts or ideas that can point me in the right direction are appreciated.

推荐答案

如果您无权访问该函数的实现,则可以查看手册页.如果手册页没有说它是异步安全的,并且POSIX标准没有说它是异步安全的,那么唯一安全的结论是它不是异步安全的"(加上不要使用它") ).

If you don't have access to the function's implementation, you can look at the manual page. If the manual page doesn't say it is async-safe, and the POSIX standard doesn't say it is async-safe, the only safe conclusion is "it is not async-safe" (coupled with "do not use it").

没有100%可靠的方法来测试功能是否异步安全.请记住,测试只能显示错误的存在,而不能显示错误的存在(Dijkstra).您没有设法使功能陷入被测行为不当的单纯事实可能仅表示您的测试不充分(但请放心,您负担不起的重要客户会立即并意外地设计出破坏性的有效方法)测试,证明在您以错误的假设发布代码后,该功能几乎不是异步安全的.

There is no 100% reliable way to test whether a function is async-safe. Remember, testing can only show the presence of bugs, not their absence (Dijkstra). The mere fact that you don't manage to tickle the function into misbehaving under test may simply mean that your testing is not adequate (but rest assured, the important customer who you can't afford to offend will immediately and accidentally devise a devastatingly effective test that demonstrates that the function is not async-safe almost as soon as you release the code with the faulty assumption).

这篇关于确定函数是否异步信号安全(可以在信号处理程序中调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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