[libc-commits] [libc] [libc] prefer *at syscalls in sys/stat wrappers (PR #201051)

Kiriti Ponduri via libc-commits libc-commits at lists.llvm.org
Tue Jun 2 00:58:03 PDT 2026


https://github.com/udaykiriti created https://github.com/llvm/llvm-project/pull/201051

        - These changes flips the #ifdef order to prefer the *at syscalls over normal ones.
	- In modern architectures, *at system calls are preferred over normal system calls cuz of safety issues.
	- So by checking for ""*at"" system calls first, we ensure better compatibility with modern systems.
	- After then normal syscalls moved else or elif for support to older ones.
	- From merged pr(#195792) .
	- issue(#195620).

>From 93410608c3c41ce6078b456f49949aab6436f523 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Tue, 2 Jun 2026 13:24:55 +0530
Subject: [PATCH] [libc] prefer *at syscalls in sys/stat wrappers

	- These changes flips the #ifdef order to prefer the *at syscalls over normal ones.
	- In modern architectures, *at system calls are preferred over normal system calls cuz of safety issues.
	- So by checking for ""*at"" system calls first, we ensure better compatibility with modern systems.
	- After then normal syscalls moved else or elif for support to older ones.

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/src/__support/File/linux/dir.cpp  | 8 ++++----
 libc/src/__support/File/linux/file.cpp | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/libc/src/__support/File/linux/dir.cpp b/libc/src/__support/File/linux/dir.cpp
index 5fe44fa8297b6..03203ae208de4 100644
--- a/libc/src/__support/File/linux/dir.cpp
+++ b/libc/src/__support/File/linux/dir.cpp
@@ -13,17 +13,17 @@
 #include "src/__support/macros/config.h"
 
 #include "hdr/fcntl_macros.h" // For open flags
-#include <sys/syscall.h> // For syscall numbers
+#include <sys/syscall.h>      // For syscall numbers
 
 namespace LIBC_NAMESPACE_DECL {
 
 ErrorOr<int> platform_opendir(const char *name) {
   int open_flags = O_RDONLY | O_DIRECTORY | O_CLOEXEC;
-#ifdef SYS_open
-  int fd = LIBC_NAMESPACE::syscall_impl<int>(SYS_open, name, open_flags);
-#elif defined(SYS_openat)
+#ifdef SYS_openat
   int fd =
       LIBC_NAMESPACE::syscall_impl<int>(SYS_openat, AT_FDCWD, name, open_flags);
+#elif defined(SYS_open)
+  int fd = LIBC_NAMESPACE::syscall_impl<int>(SYS_open, name, open_flags);
 #else
 #error                                                                         \
     "SYS_open and SYS_openat syscalls not available to perform an open operation."
diff --git a/libc/src/__support/File/linux/file.cpp b/libc/src/__support/File/linux/file.cpp
index 2bef96a102a0c..a044c11393bd2 100644
--- a/libc/src/__support/File/linux/file.cpp
+++ b/libc/src/__support/File/linux/file.cpp
@@ -92,12 +92,12 @@ ErrorOr<File *> openfile(const char *path, const char *mode) {
   constexpr long OPEN_MODE =
       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
 
-#ifdef SYS_open
-  int fd =
-      LIBC_NAMESPACE::syscall_impl<int>(SYS_open, path, open_flags, OPEN_MODE);
-#elif defined(SYS_openat)
+#ifdef SYS_openat
   int fd = LIBC_NAMESPACE::syscall_impl<int>(SYS_openat, AT_FDCWD, path,
                                              open_flags, OPEN_MODE);
+#elif defined(SYS_open)
+  int fd =
+      LIBC_NAMESPACE::syscall_impl<int>(SYS_open, path, open_flags, OPEN_MODE);
 #else
 #error "open and openat syscalls not available."
 #endif



More information about the libc-commits mailing list