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);
<
File descriptors can be duplicated. For example, when a process fork
s, 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 flagO_RDONLY
- file access modeO_WRONLY
- file access modeO_RDWR
- file access modeO_CLOEXEC
- file creation flagO_CREAT
- file creation flagO_DIRECTORY
- file creation flagO_EXCL
- file creation flagO_NOCTTY
- file creation flagO_NOFOLLOW
- file creation flagO_TMPFILE
- file creation flagO_TRUNC
- file creation flagThe 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
.
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:
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.
"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]
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).