c - Call to fdopendir() corrupts file descriptor -
i stumbled upon problem in program working on. following reproduces issue:
#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <sys/types.h> #include <dirent.h> #include <fcntl.h> #include <sys/stat.h> int main(int argc, char *argv[]) { int fd, ret_fd; dir *dirp; fd = open("./", o_rdonly); #if 1 if ((dirp = fdopendir(fd)) == null) { perror("dirp"); return 1; } closedir(dirp); #endif ret_fd = openat(fd, "makefile", o_rdonly); if (ret_fd == -1) { perror("ret_fd"); return 1; } return 0; }
basically, call openat()
, has been preceeded fdopendir()
, fails with: bad file descriptor
. however, not happen if fdopendir()
omitted.
i know fdopendir()
makes internal use of file descriptor, shouldn't revert changes after calling closedir()
?
what can prevent openat()
failing in case?
the posix description of fdopendir()
says:
upon calling closedir() file descriptor shall closed.
so descriptor closed time call openat()
.
and a typical linux man page fdopendir():
after successful call fdopendir(), fd used internally implementation, , should not otherwise used application.
Comments
Post a Comment