The procps manpage states that the flags field in files in /proc/PID/fdinfo/ is an octal number indicating the file access mode and file status flags.
The open manpage gives explanations of various flags (O_APPEND, O_ASYNC, etc.) but no corresponding octal values.
Are these octal values listed anywhere, or do I have to search through the linux source code? (and if so then where?).
Asked
Active
Viewed 1,745 times
4
Ben
- 315
- 2
- 9
1 Answers
2
I’m not aware of documentation listing the values of the flag constants, but you don’t need to search through source code or even header files, you can ask the compiler:
echo O_APPEND | gcc -include fcntl.h -E -
Going over all the flags in open(2) on x86-64 Linux:
$ for flag in APPEND ASYNC CLOEXEC CREAT DIRECT DIRECTORY DSYNC EXCL LARGEFILE NOATIME NOCTTY NOFOLLOW NONBLOCK PATH SYNC TMPFILE TRUNC; do printf '%s: ' O_$flag; echo O_$flag | gcc -D_GNU_SOURCE -include fcntl.h -E - | tail -n 1; done
| Flag | Value |
|---|---|
O_APPEND |
02000 |
O_ASYNC |
020000 |
O_CLOEXEC |
02000000 |
O_CREAT |
0100 |
O_DIRECT |
040000 |
O_DIRECTORY |
0200000 |
O_DSYNC |
010000 |
O_EXCL |
0200 |
O_LARGEFILE |
0 |
O_NOATIME |
01000000 |
O_NOCTTY |
0400 |
O_NOFOLLOW |
0400000 |
O_NONBLOCK |
04000 |
O_PATH |
010000000 |
O_SYNC |
04010000 |
O_TMPFILE |
(020000000 | 0200000) |
O_TRUNC |
01000 |
(Some of these are architecture-specific; for example O_LARGEFILE is 0100000 on i386.)
Stephen Kitt
- 411,918
- 54
- 1,065
- 1,164
-
Thanks, that's very helpful.. however I still can't understand how to interpret the fdinfo flags; for example the fdinfo for the 0 symlink (i.e. STDIN) has flags of 02, which doesn't correspond to any of the flags you listed, and its access permissions are 700 for the symlink, and 620 for the pty slave it links to. What's going on? – Ben Mar 19 '21 at 22:44
-
OK, so I found the O_RDONLY (00), O_WRONLY (01) & O_RDWR (02) flags. However I have a file descriptor pointing to /dev/ptmx with flags = 0100002?! – Ben Mar 20 '21 at 01:45
-
OK, after some more googling I discovered that you can also use `lsof +fg
` to get a list of the fdinfo flags codes, with descriptions listed in the `lsof` manpage. From this I figured out that 0100002 = O_LARGEFILE|O_RDRW, which means that O_LARGEFILE = 0100000 even though your one-liner printed it as 0 on my x86_64 system, strange. – Ben Mar 20 '21 at 01:55