title: C - Redirecting stdout after forking of child from parent
body: <p>I'm writing a program to execute another program as a forked process and redirect it's output to a file or /dev/null on demand. </p>
  
  <p>Currently I have forked and executed the external program using execvp().
  Then redirected the stdout from a thread created before forking as the forked process will inherit parents file descriptor table allowing me to redirect after foking.</p>
  
  <p>But, I can initially redirect stdout to a desired file and both parents and child's stdouts are being redirected. However if I try to redirect it again to another file, only parents stdout is redirected, child's stdout stays the same.</p>
  
  <p>Here's the code without all the error checking bits.</p>
  
  <pre><code>struct params {
      const char *p;
      int fd;
      int wait;
  };
  
  #define EXIT_NOEXEC 126
  #define EXIT_NOTFOUND   127
  #define EXIT_MISC   127
  
  static void dofile(struct params* st);
  void dupit(const char *p, struct params* st);
  void* reload_config(void* para);
  
  int
  main(int argc, char *argv[]) {
      int exit_status, prog_status;
      struct params init;
      pid_t prog_pid;
  
  
      dofile(&amp;init);
  
      prog_pid = fork();
      if (prog_pid == 0) {
          execvp(*argv, argv);
          exit_status = (errno == ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC;
          err(exit_status, "%s", argv[0]);
          exit(EXIT_FAILURE);
      } else {
          while (wait(&amp;prog_status) != prog_pid);
          return prog_status;
      }
  }
  
  static void dofile(struct params* st) {
      const char *p
      p = out.txt;
      dupit(p, st);
  
  }
  
  
  void dupit(const char *p, struct params* st) {
      pthread_t tid;
      st-&gt;wait = 0;
      int err = pthread_create(&amp;(tid), NULL, &amp;reload_config, st);
      if (err != 0) {
          printf("\ncan't create thread :[%s]", strerror(err));
          exit(1);
      } else {
          while (st-&gt;wait == 0) {
              sleep(1)
          }
      }
  }
  
  void* reload_config(void* para) {
      struct params *passed = (struct params *) para;
      int pre_config = 3; 
      int cur_config = 1; 
      int saved_stdout = dup(STDOUT_FILENO);
      char infile[5];
      int devNull = open("/dev/null", O_WRONLY);
      int file = open("out.txt", O_WRONLY);
      FILE *config;
      config = fopen("config.txt", "r");
      if (access("config.txt", F_OK) != -1) {
          while (1) {
              fgets(infile, 5, config);
              fclose(config);
              cur_config = infile[0] - '0';
              printf("output from thread, current config = %d\n", cur_config);
              if (pre_config != cur_config) {
                  if (cur_config == 1) {
                      if (dup2(file, STDOUT_FILENO) == -1) {
                          err(EXIT_MISC, NULL);
                      }
                  } else {
                      dup2(devNull, STDOUT_FILENO);
                  }
                  pre_config = cur_config;
              }
              if (passed-&gt;wait==0) {
                  passed-&gt;wait = 1;
              }
              sleep(1);
          }
      } else {
          if (dup2(passed-&gt;fd, STDOUT_FILENO) == -1) {
              err(EXIT_MISC, NULL);
          }
      }
  }
  </code></pre>
  
  <p>Well, I changed the code a bit so you guys will understand, so some parts will make no sense. But you get the basic idea.</p>
  
  <p>How can I redirect child's stdout as I wish after forking.</p>