title: What's so special about file descriptor 3 on linux?
body: <p>I'm working on a server application that's going to work on Linux and Mac OS X. It goes like this:</p>
  
  <ul>
  <li>start main application</li>
  <li>fork of the controller process</li>
  <li>call lock_down() in the controller process</li>
  <li>terminate main application</li>
  <li>the controller process then forks again, creating a worker process</li>
  <li>eventually the controller keeps forking more worker processes</li>
  </ul>
  
  <p>I can log using several of methods (e.g. syslog or a file) but right now I'm pondering about syslog. The "funny" thing is that no syslog output is ever seen in the controller process unless I include the #ifdef section below. </p>
  
  <p>The worker processes logs flawlessly in Mac OS X and linux with or without the ifdef'ed section below. The controller also logs flawlessly in Mac OS X without the #ifdef'ed section, but on linux the ifdef is needed if I want to see any output into syslog (or the log file for that matter) from the controller process.</p>
  
  <p>So, why is that?</p>
  
  <pre><code>static int
  lock_down(void)
  {
      struct rlimit rl;
      unsigned int n;
      int fd0;
      int fd1;
      int fd2;
  
      // Reset file mode mask
      umask(0);
  
      // change the working directory
      if ((chdir("/")) &lt; 0)
          return EXIT_FAILURE;
  
      // close any and all open file descriptors
      if (getrlimit(RLIMIT_NOFILE, &amp;rl))
          return EXIT_FAILURE;
      if (RLIM_INFINITY == rl.rlim_max)
          rl.rlim_max = 1024;
  
      for (n = 0; n &lt; rl.rlim_max; n++) {
  #ifdef __linux__        
          if (3 == n) // deep magic...
              continue;
  #endif
          if (close(n) &amp;&amp; (EBADF != errno))
              return EXIT_FAILURE;
      }
  
      // attach file descriptors 0, 1 and 2 to /dev/null
      fd0 = open("/dev/null", O_RDWR);
      fd1 = dup2(fd0, 1);
      fd2 = dup2(fd0, 2);
      if (0 != fd0)
          return EXIT_FAILURE;
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>camh was close, but using closelog() was the idea that did the trick so the honor goes to jilles. Something else, aside from closing a file descriptor from under syslogs feet must go on though. To make the code work I added a call to closelog() just before the loop:</p>
  
  <pre><code>closelog();
  for (n = 0; n &lt; rl.rlim_max; n++) {
      if (close(n) &amp;&amp; (EBADF != errno))
          return EXIT_FAILURE;
  }
  </code></pre>
  
  <p>I was relying on a verbatim understanding of the manual page, saying:</p>
  
  <blockquote>
    <p>The use of openlog() is optional; it will automatically be called by syslog() if necessary...</p>
  </blockquote>
  
  <p>I interpreted this as saying that syslog would detect if the file descriptor was closed under it. Apparently it did not. An explicit closelog() on linux was needed to tell syslog that the descriptor was closed. </p>
  
  <p>One more thing that still perplexes me is that not using closelog() prevented the first forked process (the controller) from even opening and using a log file. The following forked processes could use syslog or a log file with no problems. Maybe there are some caching effect in the filesystem that make the first forked process having an unreliable "idea" of which file descriptors are available, while the next set of forked process are sufficiently delayed to not be affected by this? </p>