[libc-commits] [libc] [libc] implement klogctl (PR #244040)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Thu Nov 19 10:49:03 PST 2026
https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/244040
>From 0b89cb280f7ec98dedfb49fb941e15d1428599b4 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 18 Nov 2026 23:16:21 +0000
Subject: [PATCH] [libc] implement klogctl
This is a syscall wrapper that reads from the kernel's log buffer.
It's linux specific.
Assisted-by: Automated tooling, human reviewed.
---
libc/config/linux/aarch64/entrypoints.txt | 3 ++
libc/config/linux/aarch64/headers.txt | 1 +
libc/config/linux/arm/entrypoints.txt | 3 ++
libc/config/linux/arm/headers.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 3 ++
libc/config/linux/riscv/headers.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 3 ++
libc/config/linux/x86_64/headers.txt | 1 +
libc/include/CMakeLists.txt | 8 ++++
libc/include/sys/klog.yaml | 16 +++++++
.../linux/syscall_wrappers/CMakeLists.txt | 12 +++++
.../OSUtil/linux/syscall_wrappers/syslog.h | 33 +++++++++++++
libc/src/sys/CMakeLists.txt | 1 +
libc/src/sys/klog/CMakeLists.txt | 10 ++++
libc/src/sys/klog/klogctl.h | 25 ++++++++++
libc/src/sys/klog/linux/CMakeLists.txt | 13 ++++++
libc/src/sys/klog/linux/klogctl.cpp | 33 +++++++++++++
libc/test/src/sys/CMakeLists.txt | 1 +
libc/test/src/sys/klog/CMakeLists.txt | 14 ++++++
libc/test/src/sys/klog/klogctl_test.cpp | 46 +++++++++++++++++++
20 files changed, 228 insertions(+)
create mode 100644 libc/include/sys/klog.yaml
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/syslog.h
create mode 100644 libc/src/sys/klog/CMakeLists.txt
create mode 100644 libc/src/sys/klog/klogctl.h
create mode 100644 libc/src/sys/klog/linux/CMakeLists.txt
create mode 100644 libc/src/sys/klog/linux/klogctl.cpp
create mode 100644 libc/test/src/sys/klog/CMakeLists.txt
create mode 100644 libc/test/src/sys/klog/klogctl_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index a8a6bed94f3762..7e56cc6217d1d6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -314,6 +314,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl
+ # sys/klog.h entrypoints
+ libc.src.sys.klog.klogctl
+
# sys/mman.h entrypoints
libc.src.sys.mman.madvise
libc.src.sys.mman.memfd_create
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 53ced730e8ff8d..1826e6d90e4b2f 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -60,6 +60,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_file
libc.include.sys_inotify
libc.include.sys_ioctl
+ libc.include.sys_klog
libc.include.sys_mman
libc.include.sys_mount
libc.include.sys_param
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 51a6d29052db4d..6bc1c32fddb782 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -229,6 +229,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl
+ # sys/klog.h entrypoints
+ libc.src.sys.klog.klogctl
+
# sys/mman.h entrypoints
libc.src.sys.mman.memfd_create
libc.src.sys.mman.mmap
diff --git a/libc/config/linux/arm/headers.txt b/libc/config/linux/arm/headers.txt
index bf52235cc1fab0..966147bd2e6c16 100644
--- a/libc/config/linux/arm/headers.txt
+++ b/libc/config/linux/arm/headers.txt
@@ -29,6 +29,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.wctype
libc.include.sys_eventfd
libc.include.sys_inotify
+ libc.include.sys_klog
libc.include.sys_mount
libc.include.sys_param
libc.include.sys_personality
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 940df321e93a07..288807fb0ee307 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -347,6 +347,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/ipc.h entrypoints
libc.src.sys.ipc.ftok
+ # sys/klog.h entrypoints
+ libc.src.sys.klog.klogctl
+
# sys/mman.h entrypoints
libc.src.sys.mman.madvise
libc.src.sys.mman.memfd_create
diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index 4fd6f5e591a6bb..d7ee7dbc4fc050 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -62,6 +62,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_inotify
libc.include.sys_ioctl
libc.include.sys_ipc
+ libc.include.sys_klog
libc.include.sys_mman
libc.include.sys_mount
libc.include.sys_param
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b2552365e8a551..341091305bdeb0 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -347,6 +347,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/ipc.h entrypoints
libc.src.sys.ipc.ftok
+ # sys/klog.h entrypoints
+ libc.src.sys.klog.klogctl
+
# sys/mman.h entrypoints
libc.src.sys.mman.madvise
libc.src.sys.mman.memfd_create
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index cd882ec8e36039..b223fec01245a4 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -62,6 +62,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_inotify
libc.include.sys_ioctl
libc.include.sys_ipc
+ libc.include.sys_klog
libc.include.sys_mman
libc.include.sys_mount
libc.include.sys_param
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 6daaff9f87ea77..bda58c870ca0c7 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -817,6 +817,14 @@ add_header_macro(
.llvm-libc-types.struct_ipc_perm
)
+add_header_macro(
+ sys_klog
+ ../libc/include/sys/klog.yaml
+ sys/klog.h
+ DEPENDS
+ .llvm_libc_common_h
+)
+
add_header_macro(
sys_mman
../libc/include/sys/mman.yaml
diff --git a/libc/include/sys/klog.yaml b/libc/include/sys/klog.yaml
new file mode 100644
index 00000000000000..2baccf468347f1
--- /dev/null
+++ b/libc/include/sys/klog.yaml
@@ -0,0 +1,16 @@
+header: sys/klog.h
+standards:
+ - linux
+macros: []
+types: []
+enums: []
+objects: []
+functions:
+ - name: klogctl
+ standards:
+ - linux
+ return_type: int
+ arguments:
+ - type: int
+ - type: char *
+ - type: int
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 724d2d0b43cecc..e54a2920fe53f8 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -2791,3 +2791,15 @@ add_header_library(
libc.src.__support.error_or
libc.src.__support.macros.config
)
+
+add_header_library(
+ syslog
+ HDRS
+ syslog.h
+ DEPENDS
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/syslog.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/syslog.h
new file mode 100644
index 00000000000000..adab82c317d523
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/syslog.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for syslog.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SYSLOG_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SYSLOG_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> syslog(int type, char *bufp, int len) {
+ return syscall_checked<int>(SYS_syslog, type, bufp, len);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SYSLOG_H
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index d95b380a4f9ea1..6ddca0c08f2b30 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -5,6 +5,7 @@ add_subdirectory(eventfd)
add_subdirectory(file)
add_subdirectory(inotify)
add_subdirectory(ipc)
+add_subdirectory(klog)
add_subdirectory(mman)
add_subdirectory(mount)
add_subdirectory(personality)
diff --git a/libc/src/sys/klog/CMakeLists.txt b/libc/src/sys/klog/CMakeLists.txt
new file mode 100644
index 00000000000000..442f69e44f3f89
--- /dev/null
+++ b/libc/src/sys/klog/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(
+ klogctl
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.klogctl
+)
diff --git a/libc/src/sys/klog/klogctl.h b/libc/src/sys/klog/klogctl.h
new file mode 100644
index 00000000000000..3ac55fa328e686
--- /dev/null
+++ b/libc/src/sys/klog/klogctl.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for klogctl.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_KLOG_KLOGCTL_H
+#define LLVM_LIBC_SRC_SYS_KLOG_KLOGCTL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int klogctl(int type, char *bufp, int len);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_KLOG_KLOGCTL_H
diff --git a/libc/src/sys/klog/linux/CMakeLists.txt b/libc/src/sys/klog/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..d8ce4a1a2d9db5
--- /dev/null
+++ b/libc/src/sys/klog/linux/CMakeLists.txt
@@ -0,0 +1,13 @@
+add_entrypoint_object(
+ klogctl
+ SRCS
+ klogctl.cpp
+ HDRS
+ ../klogctl.h
+ DEPENDS
+ libc.src.__support.OSUtil.linux.syscall_wrappers.syslog
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+)
diff --git a/libc/src/sys/klog/linux/klogctl.cpp b/libc/src/sys/klog/linux/klogctl.cpp
new file mode 100644
index 00000000000000..6c724637387ad3
--- /dev/null
+++ b/libc/src/sys/klog/linux/klogctl.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of klogctl.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/klog/klogctl.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/syslog.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, klogctl, (int type, char *bufp, int len)) {
+ ErrorOr<int> result = linux_syscalls::syslog(type, bufp, len);
+ if (!result.has_value()) {
+ libc_errno = result.error();
+ return -1;
+ }
+ return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index ee56e464cf9a81..fe31356a1d3691 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -19,6 +19,7 @@ add_subdirectory(eventfd)
add_subdirectory(file)
add_subdirectory(inotify)
add_subdirectory(ipc)
+add_subdirectory(klog)
add_subdirectory(uio)
add_subdirectory(time)
add_subdirectory(timerfd)
diff --git a/libc/test/src/sys/klog/CMakeLists.txt b/libc/test/src/sys/klog/CMakeLists.txt
new file mode 100644
index 00000000000000..4e3e6cdddf8f30
--- /dev/null
+++ b/libc/test/src/sys/klog/CMakeLists.txt
@@ -0,0 +1,14 @@
+add_libc_testsuite(libc-sys-klog-tests)
+
+add_libc_test(
+ klogctl_test
+ SUITE
+ libc-sys-klog-tests
+ SRCS
+ klogctl_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.__support.libc_errno
+ libc.src.sys.klog.klogctl
+ libc.test.UnitTest.ErrnoCheckingTest
+)
diff --git a/libc/test/src/sys/klog/klogctl_test.cpp b/libc/test/src/sys/klog/klogctl_test.cpp
new file mode 100644
index 00000000000000..f8ee0c520b6939
--- /dev/null
+++ b/libc/test/src/sys/klog/klogctl_test.cpp
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for klogctl.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "src/__support/libc_errno.h"
+#include "src/sys/klog/klogctl.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcKlogctlTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcKlogctlTest, SizeBuffer) {
+ constexpr int SYSLOG_ACTION_SIZE_BUFFER = 10;
+ int ret = LIBC_NAMESPACE::klogctl(SYSLOG_ACTION_SIZE_BUFFER, nullptr, 0);
+ if (ret >= 0) {
+ ASSERT_ERRNO_SUCCESS();
+ } else {
+ ASSERT_EQ(ret, -1);
+ if (libc_errno == ENOSYS)
+ ASSERT_ERRNO_EQ(ENOSYS);
+ else
+ ASSERT_ERRNO_EQ(EPERM);
+ }
+}
+
+TEST_F(LlvmLibcKlogctlTest, InvalidAction) {
+ constexpr int INVALID_SYSLOG_ACTION = -1;
+ int ret = LIBC_NAMESPACE::klogctl(INVALID_SYSLOG_ACTION, nullptr, 0);
+ ASSERT_EQ(ret, -1);
+ if (libc_errno == EPERM)
+ ASSERT_ERRNO_EQ(EPERM);
+ else if (libc_errno == ENOSYS)
+ ASSERT_ERRNO_EQ(ENOSYS);
+ else
+ ASSERT_ERRNO_EQ(EINVAL);
+}
More information about the libc-commits
mailing list