mailing list of musl libc
 help / color / mirror / 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

mailing list of musl libc

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/musl

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 musl musl/ http://inbox.vuxu.org/musl \
		musl@inbox.vuxu.org
	public-inbox-index musl

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git