1 #include <sys/types.h> 2 #include <sys/wait.h> 3 #include <errno.h> 4 #include <fcntl.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <unistd.h> 8 9 int 10 main(int argc, char *argv[]) 11 { 12 int fd, fd_parent; 13 char buf[1]; 14 15 if (argc != 2) { 16 return 1; 17 } 18 19 fd_parent = atoi(argv[1]); 20 21 /* Writing to fd_parent should fail as it has to be closed at this 22 * point */ 23 if (write(fd_parent, buf, sizeof(buf)) != -1) { 24 return 2; 25 } 26 if (errno != EBADF) { 27 return 3; 28 } 29 30 /* If we open a new file, the fd we obtain should be identical to 31 * fd_parent */ 32 fd = open("open_identical_fd", O_CREAT|O_RDWR, 0660); 33 if (fd != fd_parent) { 34 return 4; 35 } 36 37 return 0; 38 } 39 40