mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] use openat syscall in open() to avoid the spurious fcntl call in modern kernels to apply O_CLOEXEC
@ 2020-12-29 22:55 Érico Nogueira
  0 siblings, 0 replies; only message in thread
From: Érico Nogueira @ 2020-12-29 22:55 UTC (permalink / raw)
  To: musl; +Cc: Érico Rolim

From: Érico Rolim <ericonr@disroot.org>

openat appears to have been introduced after the O_CLOEXEC flag was
added to the kernel, so its presence can be used as a proxy for the
O_CLOEXEC flag definitely taking effect
---

The commit message is unsure about the timeline between O_CLOEXEC and
openat, because I couldn't find any definitive sources. It's certainly
the assumption in the current openat() implementation, and glibc seems
to use openat for open() since the first commit I could find
(a63c7fa1856d6d4ef6573111e5700ac01b0bf6b2, from 2011). bionic also
always uses openat, but I didn't look through its history.

I believe this will generate a tiny amount of dead code in archs which
don't have SYS_open (so __sys_open_cp already uses SYS_openat), since
they should never return ENOSYS, and if they do (funky seccomp filters?)
the second attempt will be useless, since it will use the exact same
values. The block could be guarded by an ifdef, but I'm not sure it's
desired.

 src/fcntl/open.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/fcntl/open.c b/src/fcntl/open.c
index 1d817a2d..75c4b919 100644
--- a/src/fcntl/open.c
+++ b/src/fcntl/open.c
@@ -13,9 +13,12 @@ int open(const char *filename, int flags, ...)
 		va_end(ap);
 	}
 
-	int fd = __sys_open_cp(filename, flags, mode);
-	if (fd>=0 && (flags & O_CLOEXEC))
-		__syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
+	int fd = __syscall(SYS_openat, AT_FDCWD, filename, flags|O_LARGEFILE, mode);
+	if (fd==-ENOSYS) {
+		fd = __sys_open_cp(filename, flags, mode);
+		if (fd>=0 && (flags & O_CLOEXEC))
+			__syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
+	}
 
 	return __syscall_ret(fd);
 }
-- 
2.29.2


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-12-29 22:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-29 22:55 [musl] [PATCH] use openat syscall in open() to avoid the spurious fcntl call in modern kernels to apply O_CLOEXEC Érico Nogueira

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).