test-mkostemp-O_CLOEXEC.c 829 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <sys/wait.h>
  9. #include <errno.h>
  10. int main(int argc, char *argv[]) {
  11. int fd, status;
  12. char buff[5];
  13. char template[] = "/tmp/test-mkostemp.XXXXXX";
  14. fd = mkostemp(template, O_CLOEXEC);
  15. unlink(template);
  16. snprintf(buff, 5, "%d", fd);
  17. if(!fork())
  18. if(execl("./test-mkostemp-child", "test-mkostemp-child", buff, NULL) == -1)
  19. exit(EXIT_FAILURE);
  20. wait(&status);
  21. memset(buff, 0, 5);
  22. lseek(fd, 0, SEEK_SET);
  23. errno = 0;
  24. if(read(fd, buff, 5) == -1)
  25. exit(EXIT_FAILURE);
  26. if(!strncmp(buff, "test", 5))
  27. exit(EXIT_FAILURE);
  28. else
  29. exit(EXIT_SUCCESS);
  30. close(fd);
  31. exit(EXIT_SUCCESS);
  32. }