2

I have a server which is handling requests of different users. After connecting to a client I fork a child process and use setuid() to switch to the client user. As it looks like it sets also the uid for the parent.

How can I set the uid only for the child process?


[EDIT] I put everything in a small example together and it's working fine. Thanks for the hint with fork();

#include <stdio.h>
#include <unistd.h>

int main() {
  int uid[] = { 11942, 11943 };
  char *file[] = { "user1", "user2" };

  int i = 0;
  for (i = 0; i < 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;
}
multiholle
  • 3,050
  • 8
  • 41
  • 60

0 Answers0