title: Using setuid() after fork()
body: <p>I have a server which is handling requests of different users. After connecting to a client I fork a child process and use <code>setuid()</code> to switch to the client user. As it looks like it sets also the uid for the parent. </p>
  
  <p>How can I set the uid only for the child process?</p>
  
  <hr>
  
  <p><strong>[EDIT]</strong> I put everything in a small example together and it's working fine. Thanks for the hint with <code>fork()</code>;</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int main() {
    int uid[] = { 11942, 11943 };
    char *file[] = { "user1", "user2" };
  
    int i = 0;
    for (i = 0; i &lt; 2; i++) {
      printf("parent, i = %i\n", i);
      pid_t child = fork();
      if (child == 0) {
        printf("child, uid = %d\n", uid[i]);
        if (setuid(uid[i]) == -1) {
          perror("setuid(11942)\n");
        }
  
        FILE *f = fopen(file[i], "w");
        fclose(f);
  
        return 0;
      }
    }
  
    return 0;
  }
  </code></pre>