[libc-commits] [libc] [libc] add ioctl (PR #141393)

W. Turner Abney via libc-commits libc-commits at lists.llvm.org
Fri May 30 11:43:26 PDT 2025


https://github.com/cowtoolz updated https://github.com/llvm/llvm-project/pull/141393

>From 5fad00a206c39f56352e08dfaef1522fc46049f4 Mon Sep 17 00:00:00 2001
From: weebney <weebney at gmail.com>
Date: Sat, 24 May 2025 22:59:58 -0400
Subject: [PATCH 01/15] [libc] add ioctl

---
 libc/config/linux/aarch64/entrypoints.txt    |  3 ++
 libc/config/linux/arm/entrypoints.txt        |  3 ++
 libc/config/linux/riscv/entrypoints.txt      |  3 ++
 libc/config/linux/x86_64/entrypoints.txt     |  3 ++
 libc/src/sys/CMakeLists.txt                  |  1 +
 libc/src/sys/ioctl/CMakeLists.txt            | 10 +++++
 libc/src/sys/ioctl/ioctl.h                   | 21 ++++++++++
 libc/src/sys/ioctl/linux/CMakeLists.txt      | 12 ++++++
 libc/src/sys/ioctl/linux/ioctl.cpp           | 40 ++++++++++++++++++++
 libc/test/src/sys/CMakeLists.txt             |  1 +
 libc/test/src/sys/ioctl/CMakeLists.txt       |  3 ++
 libc/test/src/sys/ioctl/linux/CMakeLists.txt | 14 +++++++
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp | 34 +++++++++++++++++
 13 files changed, 148 insertions(+)
 create mode 100644 libc/src/sys/ioctl/CMakeLists.txt
 create mode 100644 libc/src/sys/ioctl/ioctl.h
 create mode 100644 libc/src/sys/ioctl/linux/CMakeLists.txt
 create mode 100644 libc/src/sys/ioctl/linux/ioctl.cpp
 create mode 100644 libc/test/src/sys/ioctl/CMakeLists.txt
 create mode 100644 libc/test/src/sys/ioctl/linux/CMakeLists.txt
 create mode 100644 libc/test/src/sys/ioctl/linux/ioctl_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 520046f768b5d..fcf1278eae723 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -245,6 +245,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # https://github.com/llvm/llvm-project/issues/80060
     # libc.src.sys.epoll.epoll_pwait2
 
+    # sys/ioctl.h entrypoints
+    libc.src.sys.ioctl.ioctl
+
     # sys/mman.h entrypoints
     libc.src.sys.mman.madvise
     libc.src.sys.mman.mincore
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 7432a7e912e81..1161ae260be2e 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -172,6 +172,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdlib.free
     libc.src.stdlib.malloc
 
+    # sys/ioctl.h entrypoints
+    libc.src.sys.ioctl.ioctl
+
     # sys/mman.h entrypoints
     libc.src.sys.mman.mmap
     libc.src.sys.mman.munmap
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 0b645a2d2fb8b..050fc2672a57e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -246,6 +246,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # https://github.com/llvm/llvm-project/issues/80060
     # libc.src.sys.epoll.epoll_pwait2
 
+    # sys/ioctl.h entrypoints
+    libc.src.sys.ioctl.ioctl
+
     # sys/mman.h entrypoints
     libc.src.sys.mman.madvise
     libc.src.sys.mman.mincore
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9f447dd0d35d2..07ebf51f70774 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -246,6 +246,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # https://github.com/llvm/llvm-project/issues/80060
     # libc.src.sys.epoll.epoll_pwait2
 
+    # sys/ioctl.h entrypoints
+    libc.src.sys.ioctl.ioctl
+
     # sys/mman.h entrypoints
     libc.src.sys.mman.madvise
     libc.src.sys.mman.mincore
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 9a73b80d35d2f..0fa11e9eee696 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -13,3 +13,4 @@ add_subdirectory(utsname)
 add_subdirectory(wait)
 add_subdirectory(prctl)
 add_subdirectory(uio)
+add_subdirectory(ioctl)
diff --git a/libc/src/sys/ioctl/CMakeLists.txt b/libc/src/sys/ioctl/CMakeLists.txt
new file mode 100644
index 0000000000000..099a1b96389fc
--- /dev/null
+++ b/libc/src/sys/ioctl/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  ioctl
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.ioctl
+)
diff --git a/libc/src/sys/ioctl/ioctl.h b/libc/src/sys/ioctl/ioctl.h
new file mode 100644
index 0000000000000..91d6bb7f7abe7
--- /dev/null
+++ b/libc/src/sys/ioctl/ioctl.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for ioctl ---------------------------*-C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_H
+#define LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_H
+
+#include "src/__support/macros/config.h"
+#include <sys/ioctl.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+int ioctl(int fd, unsigned long request, ...);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_H
diff --git a/libc/src/sys/ioctl/linux/CMakeLists.txt b/libc/src/sys/ioctl/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..876f35aaee66c
--- /dev/null
+++ b/libc/src/sys/ioctl/linux/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_entrypoint_object(
+  ioctl
+  SRCS
+    ioctl.cpp
+  HDRS
+    ../ioctl.h
+  DEPENDS
+    libc.include.sys_ioctl
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
diff --git a/libc/src/sys/ioctl/linux/ioctl.cpp b/libc/src/sys/ioctl/linux/ioctl.cpp
new file mode 100644
index 0000000000000..898385027105c
--- /dev/null
+++ b/libc/src/sys/ioctl/linux/ioctl.cpp
@@ -0,0 +1,40 @@
+//===---------- Linux implementation of the ioctl function ----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/ioctl/ioctl.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
+#include <stdarg.h>
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, ioctl, (int fd, unsigned long request, ...)) {
+  va_list vargs;
+  va_start(vargs, request);
+  void *data_pointer = va_arg(vargs, void *);
+  int ret =
+      LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, request, data_pointer);
+  va_end(vargs);
+
+  // From `man ioctl`:
+  // "Usually, on success zero is returned.  A few ioctl() operations
+  // use the return value as an output parameter and return a
+  // nonnegative value on success.  On error, -1 is returned, and errno
+  // is set to indicate the error."
+  if (ret < 0) {
+    libc_errno = -ret;
+    return -1;
+  }
+
+  return ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 224cc7905ad31..13bf91eef04be 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -13,3 +13,4 @@ add_subdirectory(auxv)
 add_subdirectory(epoll)
 add_subdirectory(uio)
 add_subdirectory(time)
+add_subdirectory(ioctl)
diff --git a/libc/test/src/sys/ioctl/CMakeLists.txt b/libc/test/src/sys/ioctl/CMakeLists.txt
new file mode 100644
index 0000000000000..b4bbe81c92ff2
--- /dev/null
+++ b/libc/test/src/sys/ioctl/CMakeLists.txt
@@ -0,0 +1,3 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/sys/ioctl/linux/CMakeLists.txt b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..1466453fe08aa
--- /dev/null
+++ b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
@@ -0,0 +1,14 @@
+add_custom_target(libc_sys_ioctl_unittests)
+
+add_libc_unittest(
+  ioctl_test
+  SUITE
+    libc_sys_ioctl_unittests
+  SRCS
+    ioctl_test.cpp
+  DEPENDS
+    libc.include.sys_ioctl
+    libc.include.sys_filio
+    libc.src.sys.ioctl.ioctl
+    libc.src.errno.errno
+)
diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
new file mode 100644
index 0000000000000..1752db8a57ce7
--- /dev/null
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -0,0 +1,34 @@
+//===-- Unittests for ioctl -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/errno/libc_errno.h"
+#include "src/sys/ioctl/ioctl.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include <sys/filio.h>
+#include <sys/ioctl.h>
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST(LlvmLibcSysIoctlTest, StdinFIONREAD) {
+  LIBC_NAMESPACE::libc_errno = 0;
+
+  // FIONREAD reports the number of readable bytes for fd
+  int bytes;
+  int ret = LIBC_NAMESPACE::ioctl(0, FIONREAD, &bytes);
+  ASSERT_ERRNO_SUCCESS();
+}
+
+TEST(LlvmLibcSysIoctlTest, InvalidCommandENOTTY) {
+  LIBC_NAMESPACE::libc_errno = 0;
+
+  // 0xDEADBEEF is just a random nonexistent command;
+  // calling this should always fail with ENOTTY
+  int ret = LIBC_NAMESPACE::ioctl(3, 0xDEADBEEF, NULL);
+  ASSERT_TRUE(ret == -1 && errno == ENOTTY);
+}

>From 51499ce0b91ab014ad6cc7616c02914fcaf4ddfd Mon Sep 17 00:00:00 2001
From: weebney <weebney at gmail.com>
Date: Sun, 25 May 2025 11:08:10 -0400
Subject: [PATCH 02/15] fix ioctl linux interior return value documentation

---
 libc/src/sys/ioctl/linux/ioctl.cpp | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/libc/src/sys/ioctl/linux/ioctl.cpp b/libc/src/sys/ioctl/linux/ioctl.cpp
index 898385027105c..408d2735eec56 100644
--- a/libc/src/sys/ioctl/linux/ioctl.cpp
+++ b/libc/src/sys/ioctl/linux/ioctl.cpp
@@ -24,17 +24,14 @@ LLVM_LIBC_FUNCTION(int, ioctl, (int fd, unsigned long request, ...)) {
       LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, request, data_pointer);
   va_end(vargs);
 
-  // From `man ioctl`:
-  // "Usually, on success zero is returned.  A few ioctl() operations
-  // use the return value as an output parameter and return a
-  // nonnegative value on success.  On error, -1 is returned, and errno
-  // is set to indicate the error."
-  if (ret < 0) {
-    libc_errno = -ret;
-    return -1;
+  // Some ioctls can be expected to return positive values
+  if (ret >= 0) {
+    return ret;
   }
 
-  return ret;
+  // If there is an error, errno is set and -1 is returned.
+  libc_errno = -ret;
+  return -1;
 }
 
 } // namespace LIBC_NAMESPACE_DECL

>From e7494558e5e223eb993e63c0ec4fc36b4e43b521 Mon Sep 17 00:00:00 2001
From: "W. Turner Abney" <weebney at gmail.com>
Date: Sun, 25 May 2025 12:23:54 -0400
Subject: [PATCH 03/15] Update libc/src/sys/ioctl/linux/ioctl.cpp

Co-authored-by: Joseph Huber <huberjn at outlook.com>
---
 libc/src/sys/ioctl/linux/ioctl.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/src/sys/ioctl/linux/ioctl.cpp b/libc/src/sys/ioctl/linux/ioctl.cpp
index 408d2735eec56..f03fea21c75bd 100644
--- a/libc/src/sys/ioctl/linux/ioctl.cpp
+++ b/libc/src/sys/ioctl/linux/ioctl.cpp
@@ -25,9 +25,8 @@ LLVM_LIBC_FUNCTION(int, ioctl, (int fd, unsigned long request, ...)) {
   va_end(vargs);
 
   // Some ioctls can be expected to return positive values
-  if (ret >= 0) {
+  if (ret >= 0)
     return ret;
-  }
 
   // If there is an error, errno is set and -1 is returned.
   libc_errno = -ret;

>From 25fbc06865e1df71d4156fb4372e1477d259514b Mon Sep 17 00:00:00 2001
From: weebney <weebney at gmail.com>
Date: Sun, 25 May 2025 11:44:21 -0400
Subject: [PATCH 04/15] replace ioctl syscalls with internal ioctl

---
 libc/src/termios/linux/tcdrain.cpp     |  9 ++-------
 libc/src/termios/linux/tcflow.cpp      |  9 ++-------
 libc/src/termios/linux/tcflush.cpp     | 10 ++--------
 libc/src/termios/linux/tcgetattr.cpp   |  9 ++++-----
 libc/src/termios/linux/tcgetsid.cpp    |  9 ++++-----
 libc/src/termios/linux/tcsendbreak.cpp |  9 ++-------
 libc/src/termios/linux/tcsetattr.cpp   |  9 ++-------
 libc/src/unistd/linux/isatty.cpp       |  4 +---
 8 files changed, 19 insertions(+), 49 deletions(-)

diff --git a/libc/src/termios/linux/tcdrain.cpp b/libc/src/termios/linux/tcdrain.cpp
index 116e3f0e0cbc5..66557ffe0cce0 100644
--- a/libc/src/termios/linux/tcdrain.cpp
+++ b/libc/src/termios/linux/tcdrain.cpp
@@ -13,19 +13,14 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, tcdrain, (int fd)) {
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCSBRK, 1);
-  if (ret < 0) {
-    libc_errno = -ret;
-    return -1;
-  }
-  return 0;
+  return LIBC_NAMESPACE::ioctl(fd, TCSBRK, 1);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/linux/tcflow.cpp b/libc/src/termios/linux/tcflow.cpp
index d229230b5d138..f353923acb0e6 100644
--- a/libc/src/termios/linux/tcflow.cpp
+++ b/libc/src/termios/linux/tcflow.cpp
@@ -13,19 +13,14 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, tcflow, (int fd, int action)) {
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCXONC, action);
-  if (ret < 0) {
-    libc_errno = -ret;
-    return -1;
-  }
-  return 0;
+  return LIBC_NAMESPACE::ioctl(fd, TCXONC, action);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/linux/tcflush.cpp b/libc/src/termios/linux/tcflush.cpp
index 028a5414b1960..174af2fc36081 100644
--- a/libc/src/termios/linux/tcflush.cpp
+++ b/libc/src/termios/linux/tcflush.cpp
@@ -13,20 +13,14 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, tcflush, (int fd, int queue_selector)) {
-  int ret =
-      LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCFLSH, queue_selector);
-  if (ret < 0) {
-    libc_errno = -ret;
-    return -1;
-  }
-  return 0;
+  return LIBC_NAMESPACE::ioctl(fd, TCFLSH, queue_selector);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/linux/tcgetattr.cpp b/libc/src/termios/linux/tcgetattr.cpp
index 63c096ff88eba..f8806f6207e7e 100644
--- a/libc/src/termios/linux/tcgetattr.cpp
+++ b/libc/src/termios/linux/tcgetattr.cpp
@@ -14,7 +14,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
@@ -22,11 +22,10 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, tcgetattr, (int fd, struct termios *t)) {
   LIBC_NAMESPACE::kernel_termios kt;
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCGETS, &kt);
-  if (ret < 0) {
-    libc_errno = -ret;
+  int ret = LIBC_NAMESPACE::ioctl(fd, TCGETS, &kt);
+  if (ret < 0)
     return -1;
-  }
+
   t->c_iflag = kt.c_iflag;
   t->c_oflag = kt.c_oflag;
   t->c_cflag = kt.c_cflag;
diff --git a/libc/src/termios/linux/tcgetsid.cpp b/libc/src/termios/linux/tcgetsid.cpp
index c283d0e4fda9a..3c18e285e496f 100644
--- a/libc/src/termios/linux/tcgetsid.cpp
+++ b/libc/src/termios/linux/tcgetsid.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
@@ -21,11 +21,10 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(pid_t, tcgetsid, (int fd)) {
   pid_t sid;
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TIOCGSID, &sid);
-  if (ret < 0) {
-    libc_errno = -ret;
+  int ret = LIBC_NAMESPACE::ioctl(fd, TIOCGSID, &sid);
+  if (ret < 0) 
     return -1;
-  }
+    
   return sid;
 }
 
diff --git a/libc/src/termios/linux/tcsendbreak.cpp b/libc/src/termios/linux/tcsendbreak.cpp
index 30bc91cf3de0a..7e8fbc82dadae 100644
--- a/libc/src/termios/linux/tcsendbreak.cpp
+++ b/libc/src/termios/linux/tcsendbreak.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
@@ -23,12 +23,7 @@ LLVM_LIBC_FUNCTION(pid_t, tcsendbreak, (int fd, int /* unused duration */)) {
   // POSIX leaves the behavior for non-zero duration implementation dependent.
   // Which means that the behavior can be the same as it is when duration is
   // zero. So, we just pass zero to the syscall.
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCSBRK, 0);
-  if (ret < 0) {
-    libc_errno = -ret;
-    return -1;
-  }
-  return 0;
+  return LIBC_NAMESPACE::ioctl(SYS_ioctl, fd, TCSBRK, 0);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/linux/tcsetattr.cpp b/libc/src/termios/linux/tcsetattr.cpp
index 8aa1e5c57b34e..17ced392a301a 100644
--- a/libc/src/termios/linux/tcsetattr.cpp
+++ b/libc/src/termios/linux/tcsetattr.cpp
@@ -14,7 +14,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
@@ -52,12 +52,7 @@ LLVM_LIBC_FUNCTION(int, tcsetattr,
       kt.c_cc[i] = 0;
   }
 
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, cmd, &kt);
-  if (ret < 0) {
-    libc_errno = -ret;
-    return -1;
-  }
-  return 0;
+  return LIBC_NAMESPACE::ioctl(fd, cmd, &kt);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/linux/isatty.cpp b/libc/src/unistd/linux/isatty.cpp
index e6ea22a714c78..32792fa729e0c 100644
--- a/libc/src/unistd/linux/isatty.cpp
+++ b/libc/src/unistd/linux/isatty.cpp
@@ -23,12 +23,10 @@ LLVM_LIBC_FUNCTION(int, isatty, (int fd)) {
   int line_d_val = INIT_VAL;
   // This gets the line dicipline of the terminal. When called on something that
   // isn't a terminal it doesn't change line_d_val and returns -1.
-  int result =
-      LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TIOCGETD, &line_d_val);
+  int result = LIBC_NAMESPACE::ioctl(fd, TIOCGETD, &line_d_val);
   if (result == 0)
     return 1;
 
-  libc_errno = -result;
   return 0;
 }
 

>From 80838139da4f3fb353f0447428d636bc5528cf29 Mon Sep 17 00:00:00 2001
From: weebney <weebney at gmail.com>
Date: Sun, 25 May 2025 12:51:29 -0400
Subject: [PATCH 05/15] Revert "replace ioctl syscalls with internal ioctl"

This reverts commit 25fbc06865e1df71d4156fb4372e1477d259514b.
---
 libc/src/termios/linux/tcdrain.cpp     |  9 +++++++--
 libc/src/termios/linux/tcflow.cpp      |  9 +++++++--
 libc/src/termios/linux/tcflush.cpp     | 10 ++++++++--
 libc/src/termios/linux/tcgetattr.cpp   |  9 +++++----
 libc/src/termios/linux/tcgetsid.cpp    |  9 +++++----
 libc/src/termios/linux/tcsendbreak.cpp |  9 +++++++--
 libc/src/termios/linux/tcsetattr.cpp   |  9 +++++++--
 libc/src/unistd/linux/isatty.cpp       |  4 +++-
 8 files changed, 49 insertions(+), 19 deletions(-)

diff --git a/libc/src/termios/linux/tcdrain.cpp b/libc/src/termios/linux/tcdrain.cpp
index 66557ffe0cce0..116e3f0e0cbc5 100644
--- a/libc/src/termios/linux/tcdrain.cpp
+++ b/libc/src/termios/linux/tcdrain.cpp
@@ -13,14 +13,19 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, tcdrain, (int fd)) {
-  return LIBC_NAMESPACE::ioctl(fd, TCSBRK, 1);
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCSBRK, 1);
+  if (ret < 0) {
+    libc_errno = -ret;
+    return -1;
+  }
+  return 0;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/linux/tcflow.cpp b/libc/src/termios/linux/tcflow.cpp
index f353923acb0e6..d229230b5d138 100644
--- a/libc/src/termios/linux/tcflow.cpp
+++ b/libc/src/termios/linux/tcflow.cpp
@@ -13,14 +13,19 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, tcflow, (int fd, int action)) {
-  return LIBC_NAMESPACE::ioctl(fd, TCXONC, action);
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCXONC, action);
+  if (ret < 0) {
+    libc_errno = -ret;
+    return -1;
+  }
+  return 0;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/linux/tcflush.cpp b/libc/src/termios/linux/tcflush.cpp
index 174af2fc36081..028a5414b1960 100644
--- a/libc/src/termios/linux/tcflush.cpp
+++ b/libc/src/termios/linux/tcflush.cpp
@@ -13,14 +13,20 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, tcflush, (int fd, int queue_selector)) {
-  return LIBC_NAMESPACE::ioctl(fd, TCFLSH, queue_selector);
+  int ret =
+      LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCFLSH, queue_selector);
+  if (ret < 0) {
+    libc_errno = -ret;
+    return -1;
+  }
+  return 0;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/linux/tcgetattr.cpp b/libc/src/termios/linux/tcgetattr.cpp
index f8806f6207e7e..63c096ff88eba 100644
--- a/libc/src/termios/linux/tcgetattr.cpp
+++ b/libc/src/termios/linux/tcgetattr.cpp
@@ -14,7 +14,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
@@ -22,10 +22,11 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, tcgetattr, (int fd, struct termios *t)) {
   LIBC_NAMESPACE::kernel_termios kt;
-  int ret = LIBC_NAMESPACE::ioctl(fd, TCGETS, &kt);
-  if (ret < 0)
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCGETS, &kt);
+  if (ret < 0) {
+    libc_errno = -ret;
     return -1;
-
+  }
   t->c_iflag = kt.c_iflag;
   t->c_oflag = kt.c_oflag;
   t->c_cflag = kt.c_cflag;
diff --git a/libc/src/termios/linux/tcgetsid.cpp b/libc/src/termios/linux/tcgetsid.cpp
index 3c18e285e496f..c283d0e4fda9a 100644
--- a/libc/src/termios/linux/tcgetsid.cpp
+++ b/libc/src/termios/linux/tcgetsid.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
@@ -21,10 +21,11 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(pid_t, tcgetsid, (int fd)) {
   pid_t sid;
-  int ret = LIBC_NAMESPACE::ioctl(fd, TIOCGSID, &sid);
-  if (ret < 0) 
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TIOCGSID, &sid);
+  if (ret < 0) {
+    libc_errno = -ret;
     return -1;
-    
+  }
   return sid;
 }
 
diff --git a/libc/src/termios/linux/tcsendbreak.cpp b/libc/src/termios/linux/tcsendbreak.cpp
index 7e8fbc82dadae..30bc91cf3de0a 100644
--- a/libc/src/termios/linux/tcsendbreak.cpp
+++ b/libc/src/termios/linux/tcsendbreak.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
@@ -23,7 +23,12 @@ LLVM_LIBC_FUNCTION(pid_t, tcsendbreak, (int fd, int /* unused duration */)) {
   // POSIX leaves the behavior for non-zero duration implementation dependent.
   // Which means that the behavior can be the same as it is when duration is
   // zero. So, we just pass zero to the syscall.
-  return LIBC_NAMESPACE::ioctl(SYS_ioctl, fd, TCSBRK, 0);
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCSBRK, 0);
+  if (ret < 0) {
+    libc_errno = -ret;
+    return -1;
+  }
+  return 0;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/termios/linux/tcsetattr.cpp b/libc/src/termios/linux/tcsetattr.cpp
index 17ced392a301a..8aa1e5c57b34e 100644
--- a/libc/src/termios/linux/tcsetattr.cpp
+++ b/libc/src/termios/linux/tcsetattr.cpp
@@ -14,7 +14,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <asm/ioctls.h>  // Safe to include without the risk of name pollution.
+#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
 #include <sys/syscall.h> // For syscall numbers
 #include <termios.h>
 
@@ -52,7 +52,12 @@ LLVM_LIBC_FUNCTION(int, tcsetattr,
       kt.c_cc[i] = 0;
   }
 
-  return LIBC_NAMESPACE::ioctl(fd, cmd, &kt);
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, cmd, &kt);
+  if (ret < 0) {
+    libc_errno = -ret;
+    return -1;
+  }
+  return 0;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/linux/isatty.cpp b/libc/src/unistd/linux/isatty.cpp
index 32792fa729e0c..e6ea22a714c78 100644
--- a/libc/src/unistd/linux/isatty.cpp
+++ b/libc/src/unistd/linux/isatty.cpp
@@ -23,10 +23,12 @@ LLVM_LIBC_FUNCTION(int, isatty, (int fd)) {
   int line_d_val = INIT_VAL;
   // This gets the line dicipline of the terminal. When called on something that
   // isn't a terminal it doesn't change line_d_val and returns -1.
-  int result = LIBC_NAMESPACE::ioctl(fd, TIOCGETD, &line_d_val);
+  int result =
+      LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TIOCGETD, &line_d_val);
   if (result == 0)
     return 1;
 
+  libc_errno = -result;
   return 0;
 }
 

>From b5b86c211f3df0f50c7df81eeaae1a86e3588c1d Mon Sep 17 00:00:00 2001
From: "W. Turner Abney" <weebney at gmail.com>
Date: Tue, 27 May 2025 19:47:47 -0400
Subject: [PATCH 06/15] Update libc/test/src/sys/ioctl/linux/ioctl_test.cpp

Co-authored-by: Michael Jones <michaelrj at google.com>
---
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
index 1752db8a57ce7..43e3a8bd66a74 100644
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -30,5 +30,6 @@ TEST(LlvmLibcSysIoctlTest, InvalidCommandENOTTY) {
   // 0xDEADBEEF is just a random nonexistent command;
   // calling this should always fail with ENOTTY
   int ret = LIBC_NAMESPACE::ioctl(3, 0xDEADBEEF, NULL);
-  ASSERT_TRUE(ret == -1 && errno == ENOTTY);
+  ASSERT_EQ(ret, -1);
+  ASSERT_ERRNO_EQ(ENOTTY);
 }

>From 759e2f2bffd75b9c597f8f583d7f4000daef53c0 Mon Sep 17 00:00:00 2001
From: weebney <weebney at gmail.com>
Date: Tue, 27 May 2025 20:07:54 -0400
Subject: [PATCH 07/15] fix bad includes and missing macro for ioctl test

---
 libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h | 1 +
 libc/src/sys/ioctl/ioctl.h                             | 1 -
 libc/test/src/sys/ioctl/linux/CMakeLists.txt           | 1 -
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp           | 1 -
 4 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h b/libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h
index 5eb779aeeca56..41226080084c3 100644
--- a/libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-ioctl-macros.h
@@ -15,5 +15,6 @@
 // around the definitions of macros like _IO, _IOR, _IOW, and _IOWR that I don't
 // think is worth digging into right now.
 #define TIOCGETD 0x5424
+#define FIONREAD 0x541B
 
 #endif // LLVM_LIBC_MACROS_LINUX_SYS_IOCTL_MACROS_H
diff --git a/libc/src/sys/ioctl/ioctl.h b/libc/src/sys/ioctl/ioctl.h
index 91d6bb7f7abe7..62323ba7dd4dc 100644
--- a/libc/src/sys/ioctl/ioctl.h
+++ b/libc/src/sys/ioctl/ioctl.h
@@ -10,7 +10,6 @@
 #define LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_H
 
 #include "src/__support/macros/config.h"
-#include <sys/ioctl.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/test/src/sys/ioctl/linux/CMakeLists.txt b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
index 1466453fe08aa..db1acf98c3bfe 100644
--- a/libc/test/src/sys/ioctl/linux/CMakeLists.txt
+++ b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
@@ -8,7 +8,6 @@ add_libc_unittest(
     ioctl_test.cpp
   DEPENDS
     libc.include.sys_ioctl
-    libc.include.sys_filio
     libc.src.sys.ioctl.ioctl
     libc.src.errno.errno
 )
diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
index 43e3a8bd66a74..6b4af375a6a42 100644
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -9,7 +9,6 @@
 #include "src/errno/libc_errno.h"
 #include "src/sys/ioctl/ioctl.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
-#include <sys/filio.h>
 #include <sys/ioctl.h>
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;

>From 98ef42e019b3474e8e48848388c3d74301e9cfaa Mon Sep 17 00:00:00 2001
From: weebney <weebney at gmail.com>
Date: Thu, 29 May 2025 15:06:18 -0400
Subject: [PATCH 08/15] various improvements to ioctl tests

---
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp  | 63 ++++++++++++++++---
 .../sys/ioctl/linux/testdata/CMakeLists.txt   |  1 +
 2 files changed, 56 insertions(+), 8 deletions(-)
 create mode 100644 libc/test/src/sys/ioctl/linux/testdata/CMakeLists.txt

diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
index 6b4af375a6a42..dc81b2d4a4dab 100644
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -5,30 +5,77 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-
 #include "src/errno/libc_errno.h"
+#include "src/fcntl/open.h"
 #include "src/sys/ioctl/ioctl.h"
+#include "src/unistd/close.h"
+#include "src/unistd/read.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
-#include <sys/ioctl.h>
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
 
-TEST(LlvmLibcSysIoctlTest, StdinFIONREAD) {
+TEST(LlvmLibcSysIoctlTest, NullAndTestFileFIONREAD) {
   LIBC_NAMESPACE::libc_errno = 0;
 
-  // FIONREAD reports the number of readable bytes for fd
-  int bytes;
-  int ret = LIBC_NAMESPACE::ioctl(0, FIONREAD, &bytes);
+  // FIONREAD reports the number of available bytes to read for the passed fd
+  int dev_zero_fd = LIBC_NAMESPACE::open("/dev/zero", O_RDONLY);
+  ASSERT_GT(dev_zero_fd, 0);
+  ASSERT_ERRNO_SUCCESS();
+
+  // For /dev/zero, this is always 0
+  int dev_zero_n = -1;
+  int ret = LIBC_NAMESPACE::ioctl(dev_zero_fd, FIONREAD, &dev_zero_n);
+  ASSERT_GT(ret, -1);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_EQ(dev_zero_n, 0);
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(dev_zero_fd), Succeeds(0));
+
+  // Now, with a file known to have a non-zero size
+  constexpr const char TEST_MSG[] = "ioctl test";
+  constexpr ssize_t TEST_MSG_SIZE = sizeof(TEST_MSG) - 1;
+  constexpr const char *TEST_FILE = "testdata/ioctl.test";
+  int test_file_fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
+  ASSERT_GT(test_file_fd, 0);
+  ASSERT_ERRNO_SUCCESS();
+
+  // This reports the full size of the file, as we haven't read anything yet
+  int test_file_n = -1;
+  ret = LIBC_NAMESPACE::ioctl(test_file_fd, FIONREAD, &test_file_n);
+  ASSERT_GT(ret, -1);
   ASSERT_ERRNO_SUCCESS();
+  ASSERT_EQ(test_file_n, TEST_MSG_SIZE);
+
+  // But if we read some bytes...
+  constexpr int READ_COUNT = 5;
+  char buffer[READ_COUNT];
+  ASSERT_THAT(LIBC_NAMESPACE::read(test_file_fd, buffer, READ_COUNT),
+              Succeeds(READ_COUNT));
+
+  // ... n should have decreased by the number of bytes we've read
+  int test_file_n_after_reading = -1;
+  ret =
+      LIBC_NAMESPACE::ioctl(test_file_fd, FIONREAD, &test_file_n_after_reading);
+  ASSERT_GT(ret, -1);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_EQ(test_file_n - READ_COUNT, test_file_n_after_reading);
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(test_file_fd), Succeeds(0));
 }
 
-TEST(LlvmLibcSysIoctlTest, InvalidCommandENOTTY) {
+TEST(LlvmLibcSysIoctlTest, InvalidIoctlCommand) {
   LIBC_NAMESPACE::libc_errno = 0;
 
+  int fd = LIBC_NAMESPACE::open("/dev/zero", O_RDONLY);
+  ASSERT_GT(fd, 0);
+  ASSERT_ERRNO_SUCCESS();
+
   // 0xDEADBEEF is just a random nonexistent command;
   // calling this should always fail with ENOTTY
-  int ret = LIBC_NAMESPACE::ioctl(3, 0xDEADBEEF, NULL);
+  int ret = LIBC_NAMESPACE::ioctl(fd, 0xDEADBEEF, NULL);
   ASSERT_EQ(ret, -1);
   ASSERT_ERRNO_EQ(ENOTTY);
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
 }
diff --git a/libc/test/src/sys/ioctl/linux/testdata/CMakeLists.txt b/libc/test/src/sys/ioctl/linux/testdata/CMakeLists.txt
new file mode 100644
index 0000000000000..6e56ad91d0995
--- /dev/null
+++ b/libc/test/src/sys/ioctl/linux/testdata/CMakeLists.txt
@@ -0,0 +1 @@
+file(GENERATE OUTPUT ioctl.test CONTENT "ioctl test")

>From 3cfd2f84efbfc524e58a3bd59a05a38c1a179c3e Mon Sep 17 00:00:00 2001
From: weebney <weebney at gmail.com>
Date: Thu, 29 May 2025 18:28:29 -0400
Subject: [PATCH 09/15] fix missing include

---
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
index dc81b2d4a4dab..a562730872441 100644
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -12,6 +12,8 @@
 #include "src/unistd/read.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 
+#include <sys/ioctl.h>
+
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
 

>From aed221e3826f0b5b18b14e52e0dc52472bf2e50c Mon Sep 17 00:00:00 2001
From: weebney <weebney at gmail.com>
Date: Thu, 29 May 2025 18:29:48 -0400
Subject: [PATCH 10/15] fix incorrect type for assertion param

---
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
index a562730872441..be2a80340d275 100644
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -36,7 +36,7 @@ TEST(LlvmLibcSysIoctlTest, NullAndTestFileFIONREAD) {
 
   // Now, with a file known to have a non-zero size
   constexpr const char TEST_MSG[] = "ioctl test";
-  constexpr ssize_t TEST_MSG_SIZE = sizeof(TEST_MSG) - 1;
+  constexpr int TEST_MSG_SIZE = sizeof(TEST_MSG) - 1;
   constexpr const char *TEST_FILE = "testdata/ioctl.test";
   int test_file_fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
   ASSERT_GT(test_file_fd, 0);

>From d62854169836ef3ec5df59425882408b42a6d765 Mon Sep 17 00:00:00 2001
From: weebney <weebney at gmail.com>
Date: Thu, 29 May 2025 22:55:03 -0400
Subject: [PATCH 11/15] add missing test dependencies to cmakelists

---
 libc/test/src/sys/ioctl/linux/CMakeLists.txt | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libc/test/src/sys/ioctl/linux/CMakeLists.txt b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
index db1acf98c3bfe..8ab9a80d1907d 100644
--- a/libc/test/src/sys/ioctl/linux/CMakeLists.txt
+++ b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
@@ -10,4 +10,7 @@ add_libc_unittest(
     libc.include.sys_ioctl
     libc.src.sys.ioctl.ioctl
     libc.src.errno.errno
+    libc.src.fcntl.open
+    libc.src.unistd.close
+    libc.src.unistd.read
 )

>From 924ed4cdbda1151bfb10c5117777c5af5e96e0c7 Mon Sep 17 00:00:00 2001
From: William <weebney at gmail.com>
Date: Fri, 30 May 2025 10:32:30 -0400
Subject: [PATCH 12/15] fix inappropriate ioctl for device in ioctl tests

---
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp | 22 ++++----------------
 1 file changed, 4 insertions(+), 18 deletions(-)

diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
index be2a80340d275..f706fefa45365 100644
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -17,24 +17,9 @@
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
 
-TEST(LlvmLibcSysIoctlTest, NullAndTestFileFIONREAD) {
+TEST(LlvmLibcSysIoctlTest, TestFileFIONREAD) {
   LIBC_NAMESPACE::libc_errno = 0;
 
-  // FIONREAD reports the number of available bytes to read for the passed fd
-  int dev_zero_fd = LIBC_NAMESPACE::open("/dev/zero", O_RDONLY);
-  ASSERT_GT(dev_zero_fd, 0);
-  ASSERT_ERRNO_SUCCESS();
-
-  // For /dev/zero, this is always 0
-  int dev_zero_n = -1;
-  int ret = LIBC_NAMESPACE::ioctl(dev_zero_fd, FIONREAD, &dev_zero_n);
-  ASSERT_GT(ret, -1);
-  ASSERT_ERRNO_SUCCESS();
-  ASSERT_EQ(dev_zero_n, 0);
-
-  ASSERT_THAT(LIBC_NAMESPACE::close(dev_zero_fd), Succeeds(0));
-
-  // Now, with a file known to have a non-zero size
   constexpr const char TEST_MSG[] = "ioctl test";
   constexpr int TEST_MSG_SIZE = sizeof(TEST_MSG) - 1;
   constexpr const char *TEST_FILE = "testdata/ioctl.test";
@@ -42,9 +27,10 @@ TEST(LlvmLibcSysIoctlTest, NullAndTestFileFIONREAD) {
   ASSERT_GT(test_file_fd, 0);
   ASSERT_ERRNO_SUCCESS();
 
-  // This reports the full size of the file, as we haven't read anything yet
+  // FIONREAD reports the number of available bytes to read for the passed fd
+  // This will report the full size of the file, as we haven't read anything yet
   int test_file_n = -1;
-  ret = LIBC_NAMESPACE::ioctl(test_file_fd, FIONREAD, &test_file_n);
+  int ret = LIBC_NAMESPACE::ioctl(test_file_fd, FIONREAD, &test_file_n);
   ASSERT_GT(ret, -1);
   ASSERT_ERRNO_SUCCESS();
   ASSERT_EQ(test_file_n, TEST_MSG_SIZE);

>From e46974b45537f3b6e7f3b7d961367a0e92a92f86 Mon Sep 17 00:00:00 2001
From: William <weebney at gmail.com>
Date: Fri, 30 May 2025 11:00:52 -0400
Subject: [PATCH 13/15] test cleanup

---
 libc/test/src/sys/ioctl/linux/CMakeLists.txt |  2 ++
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp | 27 ++++++++++----------
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/libc/test/src/sys/ioctl/linux/CMakeLists.txt b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
index 8ab9a80d1907d..0474a1ec01a41 100644
--- a/libc/test/src/sys/ioctl/linux/CMakeLists.txt
+++ b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
@@ -1,5 +1,7 @@
 add_custom_target(libc_sys_ioctl_unittests)
 
+add_subdirectory(testdata)
+
 add_libc_unittest(
   ioctl_test
   SUITE
diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
index f706fefa45365..df64396be13d1 100644
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -23,33 +23,32 @@ TEST(LlvmLibcSysIoctlTest, TestFileFIONREAD) {
   constexpr const char TEST_MSG[] = "ioctl test";
   constexpr int TEST_MSG_SIZE = sizeof(TEST_MSG) - 1;
   constexpr const char *TEST_FILE = "testdata/ioctl.test";
-  int test_file_fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
-  ASSERT_GT(test_file_fd, 0);
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
   ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
 
   // FIONREAD reports the number of available bytes to read for the passed fd
   // This will report the full size of the file, as we haven't read anything yet
-  int test_file_n = -1;
-  int ret = LIBC_NAMESPACE::ioctl(test_file_fd, FIONREAD, &test_file_n);
-  ASSERT_GT(ret, -1);
+  int n = -1;
+  int ret = LIBC_NAMESPACE::ioctl(fd, FIONREAD, &n);
   ASSERT_ERRNO_SUCCESS();
-  ASSERT_EQ(test_file_n, TEST_MSG_SIZE);
+  ASSERT_GT(ret, -1);
+  ASSERT_EQ(n, TEST_MSG_SIZE);
 
   // But if we read some bytes...
   constexpr int READ_COUNT = 5;
-  char buffer[READ_COUNT];
-  ASSERT_THAT(LIBC_NAMESPACE::read(test_file_fd, buffer, READ_COUNT),
+  char read_buffer[READ_COUNT];
+  ASSERT_THAT((int)LIBC_NAMESPACE::read(fd, read_buffer, READ_COUNT),
               Succeeds(READ_COUNT));
 
   // ... n should have decreased by the number of bytes we've read
-  int test_file_n_after_reading = -1;
-  ret =
-      LIBC_NAMESPACE::ioctl(test_file_fd, FIONREAD, &test_file_n_after_reading);
-  ASSERT_GT(ret, -1);
+  int n_after_reading = -1;
+  ret = LIBC_NAMESPACE::ioctl(fd, FIONREAD, &n_after_reading);
   ASSERT_ERRNO_SUCCESS();
-  ASSERT_EQ(test_file_n - READ_COUNT, test_file_n_after_reading);
+  ASSERT_GT(ret, -1);
+  ASSERT_EQ(n - READ_COUNT, n_after_reading);
 
-  ASSERT_THAT(LIBC_NAMESPACE::close(test_file_fd), Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
 }
 
 TEST(LlvmLibcSysIoctlTest, InvalidIoctlCommand) {

>From c0fed59449ef5321838303de0035d1be2a072f2b Mon Sep 17 00:00:00 2001
From: William <weebney at gmail.com>
Date: Fri, 30 May 2025 13:05:56 -0400
Subject: [PATCH 14/15] add ioctl wrapper header

---
 libc/hdr/sys_ioctl_macros.h                  | 22 ++++++++++++++++++++
 libc/test/src/sys/ioctl/linux/CMakeLists.txt |  2 +-
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp |  3 ++-
 3 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 libc/hdr/sys_ioctl_macros.h

diff --git a/libc/hdr/sys_ioctl_macros.h b/libc/hdr/sys_ioctl_macros.h
new file mode 100644
index 0000000000000..935d436273465
--- /dev/null
+++ b/libc/hdr/sys_ioctl_macros.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from sys/ioctl.h -----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_SYS_IOCTL_MACROS_H
+#define LLVM_LIBC_HDR_SYS_IOCTL_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/sys-ioctl-macros.h"
+
+#else // Overlay mode
+
+#include <sys/ioctl.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SYS_IOCTL_MACROS_H
diff --git a/libc/test/src/sys/ioctl/linux/CMakeLists.txt b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
index 0474a1ec01a41..fd32568276430 100644
--- a/libc/test/src/sys/ioctl/linux/CMakeLists.txt
+++ b/libc/test/src/sys/ioctl/linux/CMakeLists.txt
@@ -9,7 +9,7 @@ add_libc_unittest(
   SRCS
     ioctl_test.cpp
   DEPENDS
-    libc.include.sys_ioctl
+    libc.hdr.ioctl_macros
     libc.src.sys.ioctl.ioctl
     libc.src.errno.errno
     libc.src.fcntl.open
diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
index df64396be13d1..ba7f7bff76e91 100644
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -5,6 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+
 #include "src/errno/libc_errno.h"
 #include "src/fcntl/open.h"
 #include "src/sys/ioctl/ioctl.h"
@@ -12,7 +13,7 @@
 #include "src/unistd/read.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 
-#include <sys/ioctl.h>
+#include "hdr/sys_ioctl_macros.h"
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;

>From 3a71160d3b0391daa3419e09dd13b0aafbaf0f4f Mon Sep 17 00:00:00 2001
From: William <weebney at gmail.com>
Date: Fri, 30 May 2025 13:05:56 -0400
Subject: [PATCH 15/15] add ioctl wrapper header

---
 libc/hdr/CMakeLists.txt | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 209fcb965242f..1e9f59621a8e5 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -126,6 +126,15 @@ add_proxy_header_library(
     libc.include.llvm-libc-macros.sys_epoll_macros
 )
 
+add_proxy_header_library(
+  sys_ioctl_macros
+  HDRS
+    sys_ioctl_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.sys_ioctl
+    libc.include.llvm-libc-macros.sys_ioctl_macros
+)
+
 add_proxy_header_library(
   sys_stat_macros
   HDRS



More information about the libc-commits mailing list