unix - Can 4 file descriptors point to the same file table entry -
unix - Can 4 file descriptors point to the same file table entry -
the next sequence of code has been observed in various programs:
dup2(fd, 0); dup2(fd, 1); dup2(fd, 2); if(fd > 2) close(fd); i not understand why if test needed. suppose fd 3, have 4 file descriptor 0, 1,2,3 point same entry in file table. problem?
i not understand why if test needed. suppose fd 3, have 4 file descriptor 0, 1,2,3 point same entry in file table. problem?
suppose fd 0, 1, or 2. without test, 1 of stdin, stdout, or stderr closed. code trying ensure in/out/err mapped known file — redirect /dev/null opened o_rdwr — later reads/writes aren't frustrated (ebadf) or, worse, sent "wrong" fd.
or suppose close omitted entirely. if fd 3 or greater, file descriptor may leak (be inherited by) kid processes, if in/out/err dup'd away before exec, granting access resource don't want kid have.
unix
Comments
Post a Comment