[libc-commits] [libc] [libc] Migrate socket syscall wrappers to syscall_checked (PR #198241)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Mon May 18 23:17:30 PDT 2026
https://github.com/labath updated https://github.com/llvm/llvm-project/pull/198241
>From e15ba441fc2250b985da25f32a2299f6515acd13 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Mon, 18 May 2026 06:38:07 +0000
Subject: [PATCH] [libc] Migrate socket syscall wrappers to syscall_checked
Also update the file headers while I'm at it. Move includes into a
single block so that clang-format can enforce a consistent ordering. Fix
a couple of discrepancies in the cmake file.
Assisted by Gemini.
---
.../linux/syscall_wrappers/CMakeLists.txt | 25 +++++++++++++------
.../OSUtil/linux/syscall_wrappers/accept.h | 22 ++++++++--------
.../OSUtil/linux/syscall_wrappers/accept4.h | 19 +++++++-------
.../OSUtil/linux/syscall_wrappers/connect.h | 19 +++++++-------
.../linux/syscall_wrappers/getpeername.h | 7 ++----
.../linux/syscall_wrappers/getsockname.h | 7 ++----
.../linux/syscall_wrappers/getsockopt.h | 19 +++++++-------
.../OSUtil/linux/syscall_wrappers/listen.h | 15 +++++------
.../linux/syscall_wrappers/setsockopt.h | 19 +++++++-------
.../OSUtil/linux/syscall_wrappers/shutdown.h | 15 +++++------
10 files changed, 89 insertions(+), 78 deletions(-)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 1009023a109e3..4126798abf233 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -42,7 +42,7 @@ add_header_library(
HDRS
accept.h
DEPENDS
- libc.src.__support.OSUtil.linux.syscall_wrappers.accept
+ libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.__support.error_or
libc.src.__support.libc_errno
@@ -57,9 +57,9 @@ add_header_library(
HDRS
accept4.h
DEPENDS
+ libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.__support.error_or
- libc.src.__support.libc_errno
libc.src.__support.macros.config
libc.hdr.types.socklen_t
libc.hdr.types.struct_sockaddr
@@ -71,10 +71,9 @@ add_header_library(
HDRS
connect.h
DEPENDS
- libc.src.__support.OSUtil.linux.syscall_wrappers.connect
+ libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.__support.error_or
- libc.src.__support.libc_errno
libc.src.__support.macros.config
libc.hdr.types.socklen_t
libc.hdr.types.struct_sockaddr
@@ -86,9 +85,9 @@ add_header_library(
HDRS
getsockopt.h
DEPENDS
+ libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.__support.error_or
- libc.src.__support.libc_errno
libc.src.__support.macros.config
libc.hdr.types.socklen_t
libc.include.sys_syscall
@@ -127,9 +126,9 @@ add_header_library(
HDRS
listen.h
DEPENDS
+ libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.__support.error_or
- libc.src.__support.libc_errno
libc.src.__support.macros.config
libc.include.sys_syscall
)
@@ -152,14 +151,26 @@ add_header_library(
HDRS
setsockopt.h
DEPENDS
+ libc.src.__support.OSUtil.osutil
libc.src.__support.common
libc.src.__support.error_or
- libc.src.__support.libc_errno
libc.src.__support.macros.config
libc.hdr.types.socklen_t
libc.include.sys_syscall
)
+add_header_library(
+ shutdown
+ HDRS
+ shutdown.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
+
add_header_library(
raise
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/accept.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/accept.h
index b25f0fac1f071..023959ce10312 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/accept.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/accept.h
@@ -1,21 +1,25 @@
-//===-- Implementation header for accept ------------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for accept.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_ACCEPT_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_ACCEPT_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "hdr/types/socklen_t.h"
+#include "hdr/types/struct_sockaddr.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
-
-#include "hdr/types/socklen_t.h"
-#include "hdr/types/struct_sockaddr.h"
#include <sys/syscall.h> // For syscall numbers
namespace LIBC_NAMESPACE_DECL {
@@ -24,16 +28,12 @@ namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> accept(int sockfd, struct sockaddr *addr,
socklen_t *addrlen) {
#if defined(SYS_accept)
- int ret = syscall_impl<int>(SYS_accept, sockfd, addr, addrlen);
+ return syscall_checked<int>(SYS_accept, sockfd, addr, addrlen);
#elif defined(SYS_accept4)
- int ret = syscall_impl<int>(SYS_accept4, sockfd, addr, addrlen, 0);
+ return syscall_checked<int>(SYS_accept4, sockfd, addr, addrlen, 0);
#else
#error "accept and accept4 syscalls unavailable for this platform."
#endif
-
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/accept4.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/accept4.h
index 69c316fb72766..bde37d3626895 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/accept4.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/accept4.h
@@ -1,21 +1,25 @@
-//===-- Implementation header for accept4 -----------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for accept4.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_ACCEPT4_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_ACCEPT4_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "hdr/types/socklen_t.h"
+#include "hdr/types/struct_sockaddr.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
-
-#include "hdr/types/socklen_t.h"
-#include "hdr/types/struct_sockaddr.h"
#include <sys/syscall.h> // For syscall numbers
namespace LIBC_NAMESPACE_DECL {
@@ -23,10 +27,7 @@ namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> accept4(int sockfd, struct sockaddr *addr,
socklen_t *addrlen, int flags) {
- int ret = syscall_impl<int>(SYS_accept4, sockfd, addr, addrlen, flags);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<int>(SYS_accept4, sockfd, addr, addrlen, flags);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/connect.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/connect.h
index 9ddc4674e4d9f..51b07425c6c9d 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/connect.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/connect.h
@@ -1,21 +1,25 @@
-//===-- Implementation header for connect -----------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for connect.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CONNECT_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CONNECT_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "hdr/types/socklen_t.h"
+#include "hdr/types/struct_sockaddr.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
-
-#include "hdr/types/socklen_t.h"
-#include "hdr/types/struct_sockaddr.h"
#include <sys/syscall.h> // For syscall numbers
namespace LIBC_NAMESPACE_DECL {
@@ -23,10 +27,7 @@ namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen) {
- int ret = syscall_impl<int>(SYS_connect, sockfd, addr, addrlen);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<int>(SYS_connect, sockfd, addr, addrlen);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getpeername.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getpeername.h
index 20c51d51da946..56521d492a777 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/getpeername.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getpeername.h
@@ -16,7 +16,7 @@
#include "hdr/types/socklen_t.h"
#include "hdr/types/struct_sockaddr.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
@@ -27,10 +27,7 @@ namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> getpeername(int sockfd, struct sockaddr *addr,
socklen_t *addrlen) {
- int ret = syscall_impl<int>(SYS_getpeername, sockfd, addr, addrlen);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<int>(SYS_getpeername, sockfd, addr, addrlen);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockname.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockname.h
index 6069fc58c5816..1cd2f978e41cd 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockname.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockname.h
@@ -16,7 +16,7 @@
#include "hdr/types/socklen_t.h"
#include "hdr/types/struct_sockaddr.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
@@ -27,10 +27,7 @@ namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> getsockname(int sockfd, struct sockaddr *addr,
socklen_t *addrlen) {
- int ret = syscall_impl<int>(SYS_getsockname, sockfd, addr, addrlen);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<int>(SYS_getsockname, sockfd, addr, addrlen);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockopt.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockopt.h
index 9a0c0570ad2b7..c74d053666baa 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockopt.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getsockopt.h
@@ -1,20 +1,24 @@
-//===-- Implementation header for getsockopt --------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for getsockopt.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETSOCKOPT_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETSOCKOPT_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "hdr/types/socklen_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
-
-#include "hdr/types/socklen_t.h"
#include <sys/syscall.h> // For syscall numbers
namespace LIBC_NAMESPACE_DECL {
@@ -22,11 +26,8 @@ namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> getsockopt(int sockfd, int level, int optname,
void *optval, socklen_t *optlen) {
- int ret =
- syscall_impl<int>(SYS_getsockopt, sockfd, level, optname, optval, optlen);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<int>(SYS_getsockopt, sockfd, level, optname, optval,
+ optlen);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/listen.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/listen.h
index e764c6b108432..345abb1ac5d86 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/listen.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/listen.h
@@ -1,29 +1,30 @@
-//===-- Implementation header for listen ------------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for listen.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_LISTEN_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_LISTEN_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // 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> listen(int sockfd, int backlog) {
- int ret = syscall_impl<int>(SYS_listen, sockfd, backlog);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<int>(SYS_listen, sockfd, backlog);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h
index f1cfa8cd8e562..1825a60dc4149 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h
@@ -1,20 +1,24 @@
-//===-- Implementation header for setsockopt --------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for setsockopt.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SETSOCKOPT_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SETSOCKOPT_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "hdr/types/socklen_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.h"
-
-#include "hdr/types/socklen_t.h"
#include <sys/syscall.h> // For syscall numbers
namespace LIBC_NAMESPACE_DECL {
@@ -22,11 +26,8 @@ namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> setsockopt(int sockfd, int level, int optname,
const void *optval, socklen_t optlen) {
- int ret =
- syscall_impl<int>(SYS_setsockopt, sockfd, level, optname, optval, optlen);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<int>(SYS_setsockopt, sockfd, level, optname, optval,
+ optlen);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/shutdown.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/shutdown.h
index 156905a408f1a..835622abef62b 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/shutdown.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/shutdown.h
@@ -1,29 +1,30 @@
-//===-- Implementation header for shutdown ----------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for shutdown.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SHUTDOWN_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SHUTDOWN_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // 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> shutdown(int sockfd, int how) {
- int ret = syscall_impl<int>(SYS_shutdown, sockfd, how);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<int>(SYS_shutdown, sockfd, how);
}
} // namespace linux_syscalls
More information about the libc-commits
mailing list