4

should we use perror inside signal handler because they have a user space buffer like printf and other stdio functions?

Majid Azimi
  • 5,575
  • 13
  • 64
  • 113
  • 2
    `perror` is not considered safe to call inside a signal handler. A list of safe function can be found [here](http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04). – Some programmer dude Aug 08 '12 at 09:06

2 Answers2

4

perror is not listed in the table of async-signal-safe functions (e.g., section 2.4.3 Signal Actions) so it is not safe to call from a signal handler that may be invoked in response to a signal interrupting a non-async-signal-safe function.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
2

perror() is not async-signal-safe, it may break things when it is call inside signal handler, but if an error has already happen, you may have to call it or some other log utils(also probably not aysnc-signal-safe) to report the error. It is likely that the output log message is not be messed up. Even it is messed up, you can also get some information, it is better than nothing.

You can also write your own re-entrancy safe log utils. (re-entrancy ringbuffer, rare write() syscall)

Lai Jiangshan
  • 1,420
  • 1
  • 13
  • 23