mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Érico Nogueira" <ericonr@disroot.org>
To: musl@lists.openwall.com
Cc: "Érico Rolim" <ericonr@disroot.org>
Subject: [musl] [PATCH] use openat syscall in open() to avoid the spurious fcntl call in modern kernels to apply O_CLOEXEC
Date: Tue, 29 Dec 2020 19:55:18 -0300	[thread overview]
Message-ID: <20201229225518.3677-1-ericonr@disroot.org> (raw)

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


                 reply	other threads:[~2020-12-29 22:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201229225518.3677-1-ericonr@disroot.org \
    --to=ericonr@disroot.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).