test-mkostemp-O_CLOEXEC.c 918 B

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