Skip to contentSkip to conversation

man/man2/open.2: Fix doc for O_CREAT | O_DIRECTORY

PATCH In review Paul Eggert 2 participants 2 messages May 30, 2025 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 man/man2/open.2: Fix doc for O_CREAT | O_DIRECTORY

Although Linux kernels before 5.7 would create a regular file when opening a nonexistent file with O_CREAT | O_DIRECTORY, this behavior was changed to something deeply buggy in 5.7, and when the bug was fixed in 6.4 this combination of open flags became invalid.

Adjust the documentation to match the 6.4+ behavior, which makes more sense anyway.

---
 man/man2/open.2 | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
man/man2/open.2 +10 -14
index 948dcd62e..c47b2ad58 100644
@@ -1068,6 +1068,16 @@ was specified.
 .TP
 .B EINVAL
 .B O_CREAT
+and
+.B O_DIRECTORY
+were both specified in
+.I flags ,
+and the Linux kernel version is 6.4 or later.
+(Earlier kernels were inconsistent in this area,
+and POSIX does not specify the behavior.)
+.TP
+.B EINVAL
+.B O_CREAT
 was specified in
 .I flags
 and the final component ("basename") of the new file's
@@ -1882,20 +1892,6 @@ and
 when trying to determine whether the kernel supports
 .B O_TMPFILE
 functionality.
-.P
-When both
-.B O_CREAT
-and
-.B O_DIRECTORY
-are specified in
-.I flags
-and the file specified by
-.I path
-does not exist,
-.BR open ()
-will create a regular file (i.e.,
-.B O_DIRECTORY
-is ignored).
 .SH SEE ALSO
 .BR chmod (2),
 .BR chown (2),
Signature · 1 line
2.49.0
#1 lore raw

Hi Paul,

Quoted 9 lines · Paul Eggert

Although Linux kernels before 5.7 would create a regular file when opening a nonexistent file with O_CREAT | O_DIRECTORY, this behavior was changed to something deeply buggy in 5.7, and when the bug was fixed in 6.4 this combination of open flags became invalid.

Adjust the documentation to match the 6.4+ behavior, which makes more sense anyway.

Thanks! I have verified this with the following program:

	alx@devuan:~/tmp$ cat open.c 
	#include <fcntl.h>
	#include <stdio.h>
	int
	main(void)
	{
		if (open("/home/alx/tmp/nonexistent",
			 O_WRONLY | O_CREAT | O_DIRECTORY, 0644) == -1)
		{
			perror("open");
		}
	}
	alx@devuan:~/tmp$ gcc -Wall -Wextra open.c 
	alx@devuan:~/tmp$ ./a.out 
	open: Invalid argument

I've applied the patch.
<https://www.alejandro-colomar.es/src/alx/linux/man-pages/man-pages.git/commit/?h=contrib&id=4dc50bb57870012426c7e5a44eb573bec3dc4cac>

Have a lovely day!
Alex

Quoted 49 lines
---
 man/man2/open.2 | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
man/man2/open.2 +10 -14
index 948dcd62e..c47b2ad58 100644
@@ -1068,6 +1068,16 @@ was specified.
 .TP
 .B EINVAL
 .B O_CREAT
+and
+.B O_DIRECTORY
+were both specified in
+.I flags ,
+and the Linux kernel version is 6.4 or later.
+(Earlier kernels were inconsistent in this area,
+and POSIX does not specify the behavior.)
+.TP
+.B EINVAL
+.B O_CREAT
 was specified in
 .I flags
 and the final component ("basename") of the new file's
@@ -1882,20 +1892,6 @@ and
 when trying to determine whether the kernel supports
 .B O_TMPFILE
 functionality.
-.P
-When both
-.B O_CREAT
-and
-.B O_DIRECTORY
-are specified in
-.I flags
-and the file specified by
-.I path
-does not exist,
-.BR open ()
-will create a regular file (i.e.,
-.B O_DIRECTORY
-is ignored).
 .SH SEE ALSO
 .BR chmod (2),
 .BR chown (2),
Signature · 1 line
2.49.0
Signature · 1 line
<https://www.alejandro-colomar.es/>