The errno race

Consider this situation:

  1. A process sets up a signal handler for several signals:
    • Let's say the signal handler for SIGUSR1 is called handle_sigusr.
  2. Now the process is running a part of its code, a function foo:
    • foo issues a system call, say the open(2)
    • The system call fails returning -1
      • errno gets set to the positive integer 13 reflecting the error permission denied (errno macro EACCES).
    • The system call's error-handling code calls perror(3) to emit the English error message.

All this seems innocent enough, yes. However, now let's consider signals in the mix; check out the following scenario:

  • <...>
    • foo issues a system call, say the open(2).
    • The system call fails returning -1.
      • errno gets set to the positive integer ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.