title: Why does closing file descriptors after fork affect the child process?
body: <p>I want to run programs in linux by a button click an therefore I wrote a function <code>execute</code>: </p>
  
  <pre><code>void execute(const char* program_call, const char* param )
  {
      pid_t child = vfork();
  
      if(child == 0) // child process
      {
          int child_pid = getpid();
  
          char *args[2]; // arguments for exec
          args[0] = (char*)program_call; // first argument is program_call
          args[1] = (char*)param;
  
          // close all opened file descriptors:
          const char* prefix = "/proc/";
          const char* suffix = "/fd/";
          char child_proc_dir[16]; 
          sprintf(child_proc_dir,"%s%d%s",prefix,child_pid, suffix);
  
          DIR *dir;
          struct dirent *ent;
  
          if ((dir = opendir (child_proc_dir)) != NULL) {
              // get files and directories within directory
              while ((ent = readdir (dir)) != NULL) {
                  // convert file name to int
                  char* end;
                  int fd = strtol(ent-&gt;d_name, &amp;end, 32);
                  if (!*end) // valid file descriptor
                  {
                      close(fd); // close file descriptor
                      // or set the flag FD_CLOEXEC
                      //fcntl( fd, F_SETFD, FD_CLOEXEC );
                  }
              }
              closedir (dir);
          } 
          else 
          {
              cerr&lt;&lt; "can not open directory: " &lt;&lt; child_proc_dir &lt;&lt;endl;
          }
          // replace the child process with exec*-function
              execv(program_call,args);
              _exit(2);
          }
      else if (child == -1) // fork error
      {
          if (errno == EAGAIN)
          {
              cerr&lt;&lt;“To much processes"&lt;&lt;endl;
          }
          else if (errno == ENOMEM)
          {
              cerr&lt;&lt;“Not enough space available."&lt;&lt;endl;
          }
      }
      else // parent process
      {
          usleep(50); // give some time 
          if ( errno == EACCES)
          {
              cerr&lt;&lt;“Permission denied or process file not executable."&lt;&lt;endl;
          }
          else if ( errno == ENOENT)
          {
              cerr&lt;&lt;"\n Invalid path or file."&lt;&lt;endl;
          }
          int child_status;
          if ( waitpid(child, &amp;child_status, WNOHANG | WUNTRACED) &lt; 0) // waitpid failed
          {
              cerr&lt;&lt;"Error - Execution failed"&lt;&lt;endl;
          }
          else if ( WIFEXITED( child_status ) &amp;&amp;  WEXITSTATUS( child_status ) != 0)   
          {
              cerr&lt;&lt;“Child process error - Execution failed"&lt;&lt;endl;
          }
      }
  }
  </code></pre>
  
  <p>There are two problems: </p>
  
  <ol>
  <li><p>Closing the file descriptors causes some problems, for example Thunderbird crashes or VLC runs without sound. More exactly: closing of <code>stdout(1)</code> and <code>stderr(2)</code> causes these problems. As I understand, closing file descriptor before exec only prevents them from been duplicated (there is no need to send informations from child process to parent process). Why does this affect the child process? Replacing <code>close()</code> by setting the flag <code>FD_CLOEXEC</code> doesn't change anything. Also setting the <code>FD_CLOEXEC</code> flag before fork doesn't solve the problem. Is there a better way to prevent inheritance of file descriptors?</p></li>
  <li><p>The return value of waitpid is often 0, even if the program call fails, I think because there are two (asynchrone) processes. <code>usleep(50)</code> solves this problem for my needs, but I hope there are better solutions for this problem. </p></li>
  </ol>
  
  <p>I'm using vfork, but the same problems occur by using fork. </p>