Specification of file descriptors

后端 未结 2 538
别那么骄傲
别那么骄傲 2021-01-26 14:23

I am trying to understand flags and modes of file descriptors.

The man page for

fcntl - manipulate file descriptor

int fcntl(int fd, int cmd);
<         


        
相关标签:
2条回答
  • 2021-01-26 15:09

    File descriptors can be duplicated. For example, when a process forks, it gets its own set of FDs that the parent doesn't affect, and the dup syscall can be used to explicitly duplicate individual FDs.

    When file descriptors get duplicated, every descriptor has its own set of file descriptor flags, but they'll all share the same file status flags. For example, consider this code:

    int fdA = open('/tmp/somefile', O_WRONLY);
    int fdB = dup(fdA);
    fcntl(fdA, F_SETFD, FD_CLOEXEC);
    fcntl(fdA, F_SETFL, O_APPEND);
    

    After running it, fdA will be close-on-exec and in append mode, and fdB will be in append mode but not close-on-exec. This is because close-on-exec is a file descriptor flag and append mode is a file status flag.

    The file access mode and file creation flags are passed along with the file status flags when they're supported.

    The third parameter to open, also confusingly called mode, is unrelated to everything else discussed so far. If the file is created by the call to open, then that mode is used as the permissions for the new file. Otherwise, it has no effect.

    • FD_CLOEXEC - file descriptor flag
    • O_RDONLY - file access mode
    • O_WRONLY - file access mode
    • O_RDWR - file access mode
    • O_CLOEXEC - file creation flag
    • O_CREAT - file creation flag
    • O_DIRECTORY - file creation flag
    • O_EXCL - file creation flag
    • O_NOCTTY - file creation flag
    • O_NOFOLLOW - file creation flag
    • O_TMPFILE - file creation flag
    • O_TRUNC - file creation flag

    The rest of the flags you listed are file status flags.

    And one final note: O_CLOEXEC is only relevant for a new FD. For existing FDs, you'll only ever use FD_CLOEXEC.

    0 讨论(0)
  • 2021-01-26 15:25

    I will summarize the description by Joseph Sible-Reinstate Monica and add a few remarks on possibly confusing wording across man pages, likely the cause of the OP.

    As per headings in http://man7.org/linux/man-pages/man2/fcntl.2.html (as cited in the OP) Flags = File descriptor flags + File status flags.
    Remark 1: this usage of File status flags is not consistent with the rest of available information, so it should rather be called something like

    Flags = File descriptor flags + Non-FD flags.

    The distinction between these two groups of flags is given by Joseph Sible-Reinstate Monica.

    As per http://man7.org/linux/man-pages/man2/open.2.html,

    Non-FD Flags = Access mode + File creation flags + File status flags

    Note that:

    1. The man page does not use the name Non-FD Flags. It simply calls this flags, as the name of the argument in the prototypes listed. But this should not be taken as if, conceptually, these flags encompass all flags since File descriptor flags are not included.

    2. "The distinction between these two groups of flags is that the file creation flags affect the semantics of the open operation itself, while the file status flags affect the semantics of subsequent I/O operations." [ref]

    3. This is the most common usage of File status flags.

    This is the basic classification of "entities".
    Remark 2: I use quotes since the general use of flags is quite misleading. Access mode are not flags in the usual sense, and this is clarified in How to make sense of O_RDONLY = 0?.

    Remark 3: GNU uses a different naming, adding to the confusion. The translation POSIX.1 <-> GNU is shown below. Usage of File Status Flags in GNU may be especially confusing.

    POSIX.1               GNU
    Non-FD Flags*         File Status Flags
    Access modes          Access mode
    File creation flags   Open-time Flags
    File status flags     Operating Modes
    

    As for the listings enumerating each category, they are given by Joseph Sible-Reinstate Monica. GNU also has its own Access modes, File creation flags (Open-time Flags) and File status flags (Operating Modes).

    0 讨论(0)
提交回复
热议问题