[libc-commits] [libc] [libc] Port remaining socket functions to syscall_wrappers (PR #198463)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Tue May 19 06:53:48 PDT 2026


https://github.com/labath updated https://github.com/llvm/llvm-project/pull/198463

>From c62030a9d4c18b7c6edea9ccb1974eb60606b34a Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Tue, 19 May 2026 07:18:28 +0000
Subject: [PATCH 1/2] [libc] Port remaining socket functions to
 syscall_wrappers

While in there:
- fix file headers to conform to latest standards
- add missing restrict qualifier to recvfrom

Assisted by Gemini.
---
 libc/include/sys/socket.yaml                  |   2 +-
 .../linux/syscall_wrappers/CMakeLists.txt     | 122 ++++++++++++++++++
 .../OSUtil/linux/syscall_wrappers/bind.h      |  36 ++++++
 .../OSUtil/linux/syscall_wrappers/recv.h      |  42 ++++++
 .../OSUtil/linux/syscall_wrappers/recvfrom.h  |  39 ++++++
 .../OSUtil/linux/syscall_wrappers/recvmsg.h   |  35 +++++
 .../OSUtil/linux/syscall_wrappers/send.h      |  42 ++++++
 .../OSUtil/linux/syscall_wrappers/sendmsg.h   |  36 ++++++
 .../OSUtil/linux/syscall_wrappers/sendto.h    |  39 ++++++
 .../OSUtil/linux/syscall_wrappers/socket.h    |  33 +++++
 .../linux/syscall_wrappers/socketpair.h       |  34 +++++
 libc/src/sys/socket/linux/CMakeLists.txt      |  48 +++----
 libc/src/sys/socket/linux/bind.cpp            |  25 ++--
 libc/src/sys/socket/linux/recv.cpp            |  32 ++---
 libc/src/sys/socket/linux/recvfrom.cpp        |  24 ++--
 libc/src/sys/socket/linux/recvmsg.cpp         |  21 +--
 libc/src/sys/socket/linux/send.cpp            |  30 ++---
 libc/src/sys/socket/linux/sendmsg.cpp         |  21 +--
 libc/src/sys/socket/linux/sendto.cpp          |  23 ++--
 libc/src/sys/socket/linux/socket.cpp          |  23 ++--
 libc/src/sys/socket/linux/socketpair.cpp      |  21 +--
 libc/src/sys/socket/recvfrom.h                |   2 +-
 22 files changed, 591 insertions(+), 139 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/bind.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/recv.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/recvfrom.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/recvmsg.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/send.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/sendmsg.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/sendto.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/socket.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/socketpair.h

diff --git a/libc/include/sys/socket.yaml b/libc/include/sys/socket.yaml
index e488bb9e43353..e5e9c9d2df183 100644
--- a/libc/include/sys/socket.yaml
+++ b/libc/include/sys/socket.yaml
@@ -99,7 +99,7 @@ functions:
     return_type: ssize_t
     arguments:
       - type: int
-      - type: void *
+      - type: void *__restrict
       - type: size_t
       - type: int
       - type: struct sockaddr *__restrict
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 4126798abf233..b90d2d5ceebf8 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -66,6 +66,20 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  bind
+  HDRS
+    bind.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.hdr.types.socklen_t
+    libc.hdr.types.struct_sockaddr
+    libc.include.sys_syscall
+)
+
 add_header_library(
   connect
   HDRS
@@ -146,6 +160,90 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  recv
+  HDRS
+    recv.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.hdr.types.ssize_t
+    libc.include.sys_syscall
+)
+
+add_header_library(
+  recvfrom
+  HDRS
+    recvfrom.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.hdr.types.socklen_t
+    libc.hdr.types.ssize_t
+    libc.hdr.types.struct_sockaddr
+    libc.include.sys_syscall
+)
+
+add_header_library(
+  recvmsg
+  HDRS
+    recvmsg.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.hdr.types.ssize_t
+    libc.hdr.types.struct_msghdr
+    libc.include.sys_syscall
+)
+
+add_header_library(
+  send
+  HDRS
+    send.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.hdr.types.ssize_t
+    libc.include.sys_syscall
+)
+
+add_header_library(
+  sendmsg
+  HDRS
+    sendmsg.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.hdr.types.ssize_t
+    libc.hdr.types.struct_msghdr
+    libc.include.sys_syscall
+)
+
+add_header_library(
+  sendto
+  HDRS
+    sendto.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.hdr.types.socklen_t
+    libc.hdr.types.ssize_t
+    libc.hdr.types.struct_sockaddr
+    libc.include.sys_syscall
+)
+
 add_header_library(
   setsockopt
   HDRS
@@ -171,6 +269,30 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  socket
+  HDRS
+    socket.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(
+  socketpair
+  HDRS
+    socketpair.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/bind.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/bind.h
new file mode 100644
index 0000000000000..08effbe6d4b5e
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/bind.h
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 bind.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_BIND_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_BIND_H
+
+#include "hdr/types/socklen_t.h"
+#include "hdr/types/struct_sockaddr.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> bind(int socket, const struct sockaddr *address,
+                              socklen_t address_len) {
+  return syscall_checked<int>(SYS_bind, socket, address, address_len);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_BIND_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/recv.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/recv.h
new file mode 100644
index 0000000000000..6aa580ba16cae
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/recv.h
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 recv.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECV_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECV_H
+
+#include "hdr/types/ssize_t.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<ssize_t> recv(int sockfd, void *buf, size_t len,
+                                  int flags) {
+#ifdef SYS_recv
+  return syscall_checked<ssize_t>(SYS_recv, sockfd, buf, len, flags);
+#elif defined(SYS_recvfrom)
+  return syscall_checked<ssize_t>(SYS_recvfrom, sockfd, buf, len, flags,
+                                  nullptr, nullptr);
+#else
+#error "recv and recvfrom syscalls unavailable for this platform."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECV_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/recvfrom.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/recvfrom.h
new file mode 100644
index 0000000000000..64ea2b26f0f64
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/recvfrom.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 recvfrom.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECVFROM_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECVFROM_H
+
+#include "hdr/types/socklen_t.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/types/struct_sockaddr.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<ssize_t> recvfrom(int sockfd, void *buf, size_t len,
+                                      int flags, sockaddr *__restrict src_addr,
+                                      socklen_t *__restrict addrlen) {
+  return syscall_checked<ssize_t>(SYS_recvfrom, sockfd, buf, len, flags,
+                                  src_addr, addrlen);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECVFROM_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/recvmsg.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/recvmsg.h
new file mode 100644
index 0000000000000..237df61c0b21a
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/recvmsg.h
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 recvmsg.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECVMSG_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECVMSG_H
+
+#include "hdr/types/ssize_t.h"
+#include "hdr/types/struct_msghdr.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<ssize_t> recvmsg(int sockfd, msghdr *msg, int flags) {
+  return syscall_checked<ssize_t>(SYS_recvmsg, sockfd, msg, flags);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECVMSG_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/send.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/send.h
new file mode 100644
index 0000000000000..0e4d831e8abc6
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/send.h
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 send.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEND_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEND_H
+
+#include "hdr/types/ssize_t.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<ssize_t> send(int sockfd, const void *buf, size_t len,
+                                  int flags) {
+#ifdef SYS_send
+  return syscall_checked<ssize_t>(SYS_send, sockfd, buf, len, flags);
+#elif defined(SYS_sendto)
+  return syscall_checked<ssize_t>(SYS_sendto, sockfd, buf, len, flags, nullptr,
+                                  0);
+#else
+#error "send or sendto syscalls unavailable for this platform."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEND_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/sendmsg.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/sendmsg.h
new file mode 100644
index 0000000000000..545b8e3853edb
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/sendmsg.h
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 sendmsg.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SENDMSG_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SENDMSG_H
+
+#include "hdr/types/ssize_t.h"
+#include "hdr/types/struct_msghdr.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<ssize_t> sendmsg(int sockfd, const struct msghdr *msg,
+                                     int flags) {
+  return syscall_checked<ssize_t>(SYS_sendmsg, sockfd, msg, flags);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SENDMSG_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/sendto.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/sendto.h
new file mode 100644
index 0000000000000..4a36cc6d1ea18
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/sendto.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 sendto.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SENDTO_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SENDTO_H
+
+#include "hdr/types/socklen_t.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/types/struct_sockaddr.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<ssize_t> sendto(int sockfd, const void *buf, size_t len,
+                                    int flags, const struct sockaddr *dest_addr,
+                                    socklen_t addrlen) {
+  return syscall_checked<ssize_t>(SYS_sendto, sockfd, buf, len, flags,
+                                  dest_addr, addrlen);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SENDTO_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/socket.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/socket.h
new file mode 100644
index 0000000000000..e6c746060a4bf
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/socket.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 socket.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SOCKET_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SOCKET_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> socket(int domain, int type, int protocol) {
+  return syscall_checked<int>(SYS_socket, domain, type, protocol);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SOCKET_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/socketpair.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/socketpair.h
new file mode 100644
index 0000000000000..c8b08ba57161d
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/socketpair.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 socketpair.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SOCKETPAIR_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SOCKETPAIR_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> socketpair(int domain, int type, int protocol,
+                                    int sv[2]) {
+  return syscall_checked<int>(SYS_socketpair, domain, type, protocol, sv);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SOCKETPAIR_H
diff --git a/libc/src/sys/socket/linux/CMakeLists.txt b/libc/src/sys/socket/linux/CMakeLists.txt
index c86991a3e42ea..a04003787e4fe 100644
--- a/libc/src/sys/socket/linux/CMakeLists.txt
+++ b/libc/src/sys/socket/linux/CMakeLists.txt
@@ -5,9 +5,9 @@ add_entrypoint_object(
   HDRS
     ../socket.h
   DEPENDS
-    libc.include.sys_syscall
     libc.include.sys_socket
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socket
     libc.src.errno.errno
 )
 
@@ -18,9 +18,11 @@ add_entrypoint_object(
   HDRS
     ../bind.h
   DEPENDS
-    libc.include.sys_syscall
     libc.include.sys_socket
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.types.struct_sockaddr
+    libc.hdr.types.socklen_t
+    libc.src.__support.common
+    libc.src.__support.OSUtil.linux.syscall_wrappers.bind
     libc.src.errno.errno
 )
 
@@ -129,10 +131,10 @@ add_entrypoint_object(
   HDRS
     ../socketpair.h
   DEPENDS
-    libc.include.sys_syscall
     libc.include.sys_socket
     libc.src.__support.macros.sanitizer
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.OSUtil.linux.syscall_wrappers.socketpair
     libc.src.errno.errno
 )
 
@@ -143,10 +145,9 @@ add_entrypoint_object(
   HDRS
     ../send.h
   DEPENDS
-    libc.include.sys_syscall
-    libc.hdr.types.struct_sockaddr
-    libc.hdr.types.socklen_t
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.types.ssize_t
+    libc.src.__support.common
+    libc.src.__support.OSUtil.linux.syscall_wrappers.send
     libc.src.errno.errno
 )
 
@@ -157,10 +158,11 @@ add_entrypoint_object(
   HDRS
     ../sendto.h
   DEPENDS
-    libc.include.sys_syscall
     libc.hdr.types.struct_sockaddr
     libc.hdr.types.socklen_t
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.types.ssize_t
+    libc.src.__support.common
+    libc.src.__support.OSUtil.linux.syscall_wrappers.sendto
     libc.src.errno.errno
 )
 
@@ -185,9 +187,10 @@ add_entrypoint_object(
   HDRS
     ../sendmsg.h
   DEPENDS
-    libc.include.sys_syscall
     libc.hdr.types.struct_msghdr
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.types.ssize_t
+    libc.src.__support.common
+    libc.src.__support.OSUtil.linux.syscall_wrappers.sendmsg
     libc.src.errno.errno
 )
 add_entrypoint_object(
@@ -197,11 +200,10 @@ add_entrypoint_object(
   HDRS
     ../recv.h
   DEPENDS
-    libc.include.sys_syscall
-    libc.hdr.types.struct_sockaddr
-    libc.hdr.types.socklen_t
     libc.src.__support.macros.sanitizer
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.types.ssize_t
+    libc.src.__support.common
+    libc.src.__support.OSUtil.linux.syscall_wrappers.recv
     libc.src.errno.errno
 )
 
@@ -212,11 +214,12 @@ add_entrypoint_object(
   HDRS
     ../recvfrom.h
   DEPENDS
-    libc.include.sys_syscall
     libc.hdr.types.struct_sockaddr
     libc.hdr.types.socklen_t
     libc.src.__support.macros.sanitizer
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.types.ssize_t
+    libc.src.__support.common
+    libc.src.__support.OSUtil.linux.syscall_wrappers.recvfrom
     libc.src.errno.errno
 )
 
@@ -227,10 +230,11 @@ add_entrypoint_object(
   HDRS
     ../recvmsg.h
   DEPENDS
-    libc.include.sys_syscall
     libc.hdr.types.struct_msghdr
     libc.src.__support.macros.sanitizer
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.types.ssize_t
+    libc.src.__support.common
+    libc.src.__support.OSUtil.linux.syscall_wrappers.recvmsg
     libc.src.errno.errno
 )
 
diff --git a/libc/src/sys/socket/linux/bind.cpp b/libc/src/sys/socket/linux/bind.cpp
index 1b0a868f6b127..5ade7e97a506d 100644
--- a/libc/src/sys/socket/linux/bind.cpp
+++ b/libc/src/sys/socket/linux/bind.cpp
@@ -1,32 +1,33 @@
-//===-- Linux implementation of bind --------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 bind.
+///
+//===----------------------------------------------------------------------===//
 #include "src/sys/socket/bind.h"
-
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "hdr/types/socklen_t.h"
+#include "hdr/types/struct_sockaddr.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/bind.h"
 #include "src/__support/common.h"
-
 #include "src/__support/libc_errno.h"
-#include "src/__support/macros/config.h"
-
-#include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, bind,
                    (int socket, const struct sockaddr *address,
                     socklen_t address_len)) {
-  int ret = syscall_impl<int>(SYS_bind, socket, address, address_len);
-  if (ret < 0) {
-    libc_errno = -ret;
+  auto result = linux_syscalls::bind(socket, address, address_len);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/socket/linux/recv.cpp b/libc/src/sys/socket/linux/recv.cpp
index b7b208d454ffe..83b0996dc738b 100644
--- a/libc/src/sys/socket/linux/recv.cpp
+++ b/libc/src/sys/socket/linux/recv.cpp
@@ -1,19 +1,18 @@
-//===-- Linux implementation of recv --------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 recv.
+///
+//===----------------------------------------------------------------------===//
 #include "src/sys/socket/recv.h"
-
-#include <sys/syscall.h> // For syscall numbers.
-
-#include "hdr/types/socklen_t.h"
 #include "hdr/types/ssize_t.h"
-#include "hdr/types/struct_sockaddr.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/recv.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/sanitizer.h"
@@ -22,22 +21,15 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(ssize_t, recv,
                    (int sockfd, void *buf, size_t len, int flags)) {
-#ifdef SYS_recv
-  ssize_t ret = syscall_impl<ssize_t>(SYS_recv, sockfd, buf, len, flags);
-#elif defined(SYS_recvfrom)
-  ssize_t ret = syscall_impl<ssize_t>(SYS_recvfrom, sockfd, buf, len, flags,
-                                      nullptr, nullptr);
-#else
-#error "recv or recvfrom syscalls unavailable for this platform."
-#endif
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result = linux_syscalls::recv(sockfd, buf, len, flags);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
 
-  MSAN_UNPOISON(buf, ret);
+  MSAN_UNPOISON(buf, result.value());
 
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/socket/linux/recvfrom.cpp b/libc/src/sys/socket/linux/recvfrom.cpp
index ff4d5494fbeff..97f1c391711e7 100644
--- a/libc/src/sys/socket/linux/recvfrom.cpp
+++ b/libc/src/sys/socket/linux/recvfrom.cpp
@@ -1,19 +1,20 @@
-//===-- Linux implementation of recvfrom ----------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 recvfrom.
+///
+//===----------------------------------------------------------------------===//
 #include "src/sys/socket/recvfrom.h"
-
-#include <sys/syscall.h> // For syscall numbers.
-
 #include "hdr/types/socklen_t.h"
 #include "hdr/types/ssize_t.h"
 #include "hdr/types/struct_sockaddr.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/recvfrom.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/sanitizer.h"
@@ -21,7 +22,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(ssize_t, recvfrom,
-                   (int sockfd, void *buf, size_t len, int flags,
+                   (int sockfd, void *__restrict buf, size_t len, int flags,
                     sockaddr *__restrict src_addr,
                     socklen_t *__restrict addrlen)) {
   // addrlen is a value-result argument. If it's not null, it passes the max
@@ -33,13 +34,14 @@ LLVM_LIBC_FUNCTION(ssize_t, recvfrom,
     srcaddr_sz = *addrlen;
   (void)srcaddr_sz; // prevent "set but not used" warning
 
-  ssize_t ret = syscall_impl<ssize_t>(SYS_recvfrom, sockfd, buf, len, flags,
-                                      src_addr, addrlen);
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result =
+      linux_syscalls::recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
 
+  ssize_t ret = result.value();
   MSAN_UNPOISON(buf, ret);
 
   if (src_addr) {
diff --git a/libc/src/sys/socket/linux/recvmsg.cpp b/libc/src/sys/socket/linux/recvmsg.cpp
index e7650c508115c..e2361e2d40245 100644
--- a/libc/src/sys/socket/linux/recvmsg.cpp
+++ b/libc/src/sys/socket/linux/recvmsg.cpp
@@ -1,18 +1,19 @@
-//===-- Linux implementation of recvmsg -----------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 recvmsg.
+///
+//===----------------------------------------------------------------------===//
 #include "src/sys/socket/recvmsg.h"
-
-#include <sys/syscall.h> // For syscall numbers.
-
 #include "hdr/types/ssize_t.h"
 #include "hdr/types/struct_msghdr.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/recvmsg.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/sanitizer.h"
@@ -20,9 +21,9 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(ssize_t, recvmsg, (int sockfd, msghdr *msg, int flags)) {
-  ssize_t ret = syscall_impl<ssize_t>(SYS_recvmsg, sockfd, msg, flags);
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result = linux_syscalls::recvmsg(sockfd, msg, flags);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
 
@@ -35,7 +36,7 @@ LLVM_LIBC_FUNCTION(ssize_t, recvmsg, (int sockfd, msghdr *msg, int flags)) {
   }
   MSAN_UNPOISON(msg->msg_control, msg->msg_controllen);
 
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/socket/linux/send.cpp b/libc/src/sys/socket/linux/send.cpp
index 7e63e9e716433..0832d4fea248f 100644
--- a/libc/src/sys/socket/linux/send.cpp
+++ b/libc/src/sys/socket/linux/send.cpp
@@ -1,19 +1,18 @@
-//===-- Linux implementation of send --------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 send.
+///
+//===----------------------------------------------------------------------===//
 #include "src/sys/socket/send.h"
-
-#include <sys/syscall.h> // For syscall numbers.
-
-#include "hdr/types/socklen_t.h"
 #include "hdr/types/ssize_t.h"
-#include "hdr/types/struct_sockaddr.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/send.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 
@@ -21,19 +20,12 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(ssize_t, send,
                    (int sockfd, const void *buf, size_t len, int flags)) {
-#ifdef SYS_send
-  ssize_t ret = syscall_impl<ssize_t>(SYS_send, sockfd, buf, len, flags);
-#elif defined(SYS_sendto)
-  ssize_t ret =
-      syscall_impl<ssize_t>(SYS_sendto, sockfd, buf, len, flags, nullptr, 0);
-#else
-#error "send or sendto syscalls unavailable for this platform."
-#endif
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result = linux_syscalls::send(sockfd, buf, len, flags);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/socket/linux/sendmsg.cpp b/libc/src/sys/socket/linux/sendmsg.cpp
index b4bbd7f78d433..5270225dab8b0 100644
--- a/libc/src/sys/socket/linux/sendmsg.cpp
+++ b/libc/src/sys/socket/linux/sendmsg.cpp
@@ -1,18 +1,19 @@
-//===-- Linux implementation of sendmsg -----------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 sendmsg.
+///
+//===----------------------------------------------------------------------===//
 #include "src/sys/socket/sendmsg.h"
-
-#include <sys/syscall.h> // For syscall numbers.
-
 #include "hdr/types/ssize_t.h"
 #include "hdr/types/struct_msghdr.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/sendmsg.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 
@@ -20,12 +21,12 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(ssize_t, sendmsg,
                    (int sockfd, const struct msghdr *msg, int flags)) {
-  ssize_t ret = syscall_impl<ssize_t>(SYS_sendmsg, sockfd, msg, flags);
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result = linux_syscalls::sendmsg(sockfd, msg, flags);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/socket/linux/sendto.cpp b/libc/src/sys/socket/linux/sendto.cpp
index 48450e22e9f48..db8bef9537c2c 100644
--- a/libc/src/sys/socket/linux/sendto.cpp
+++ b/libc/src/sys/socket/linux/sendto.cpp
@@ -1,19 +1,20 @@
-//===-- Linux implementation of sendto ------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 sendto.
+///
+//===----------------------------------------------------------------------===//
 #include "src/sys/socket/sendto.h"
-
-#include <sys/syscall.h> // For syscall numbers.
-
 #include "hdr/types/socklen_t.h"
 #include "hdr/types/ssize_t.h"
 #include "hdr/types/struct_sockaddr.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/sendto.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 
@@ -22,13 +23,13 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(ssize_t, sendto,
                    (int sockfd, const void *buf, size_t len, int flags,
                     const struct sockaddr *dest_addr, socklen_t addrlen)) {
-  ssize_t ret = syscall_impl<ssize_t>(SYS_sendto, sockfd, buf, len, flags,
-                                      dest_addr, addrlen);
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result =
+      linux_syscalls::sendto(sockfd, buf, len, flags, dest_addr, addrlen);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/socket/linux/socket.cpp b/libc/src/sys/socket/linux/socket.cpp
index a2da75a0d7be0..20c470e45d3c1 100644
--- a/libc/src/sys/socket/linux/socket.cpp
+++ b/libc/src/sys/socket/linux/socket.cpp
@@ -1,30 +1,29 @@
-//===-- Linux implementation of socket ------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 socket.
+///
+//===----------------------------------------------------------------------===//
 #include "src/sys/socket/socket.h"
-
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/socket.h"
 #include "src/__support/common.h"
-
 #include "src/__support/libc_errno.h"
-#include "src/__support/macros/config.h"
-
-#include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, socket, (int domain, int type, int protocol)) {
-  int ret = syscall_impl<int>(SYS_socket, domain, type, protocol);
-  if (ret < 0) {
-    libc_errno = -ret;
+  auto result = linux_syscalls::socket(domain, type, protocol);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/socket/linux/socketpair.cpp b/libc/src/sys/socket/linux/socketpair.cpp
index a17850a3468fa..49445e07fd6e2 100644
--- a/libc/src/sys/socket/linux/socketpair.cpp
+++ b/libc/src/sys/socket/linux/socketpair.cpp
@@ -1,33 +1,34 @@
-//===-- Linux implementation of socketpair --------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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 socketpair.
+///
+//===----------------------------------------------------------------------===//
 #include "src/sys/socket/socketpair.h"
-
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/socketpair.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
-#include "src/__support/macros/config.h"
 #include "src/__support/macros/sanitizer.h"
-#include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, socketpair,
                    (int domain, int type, int protocol, int sv[2])) {
-  int ret = syscall_impl<int>(SYS_socketpair, domain, type, protocol, sv);
-  if (ret < 0) {
-    libc_errno = -ret;
+  auto result = linux_syscalls::socketpair(domain, type, protocol, sv);
+  if (!result.has_value()) {
+    libc_errno = result.error();
     return -1;
   }
 
   MSAN_UNPOISON(sv, sizeof(int) * 2);
 
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/socket/recvfrom.h b/libc/src/sys/socket/recvfrom.h
index 5c12410dd4ea4..69c2438b70287 100644
--- a/libc/src/sys/socket/recvfrom.h
+++ b/libc/src/sys/socket/recvfrom.h
@@ -17,7 +17,7 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
+ssize_t recvfrom(int sockfd, void *__restrict buf, size_t len, int flags,
                  sockaddr *__restrict src_addr, socklen_t *__restrict addrlen);
 
 } // namespace LIBC_NAMESPACE_DECL

>From c5ce51c83821ad5fc70224e8a93d8a26282ab69b Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Tue, 19 May 2026 13:37:18 +0000
Subject: [PATCH 2/2] rm send recv endpoint

---
 .../linux/syscall_wrappers/CMakeLists.txt     | 26 ------------
 .../OSUtil/linux/syscall_wrappers/recv.h      | 42 -------------------
 .../OSUtil/linux/syscall_wrappers/send.h      | 42 -------------------
 libc/src/sys/socket/linux/CMakeLists.txt      |  4 +-
 libc/src/sys/socket/linux/recv.cpp            |  5 ++-
 libc/src/sys/socket/linux/send.cpp            |  4 +-
 6 files changed, 7 insertions(+), 116 deletions(-)
 delete mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/recv.h
 delete mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/send.h

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index b90d2d5ceebf8..dc1702e1e4539 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -160,19 +160,6 @@ add_header_library(
     libc.include.sys_syscall
 )
 
-add_header_library(
-  recv
-  HDRS
-    recv.h
-  DEPENDS
-    libc.src.__support.OSUtil.osutil
-    libc.src.__support.common
-    libc.src.__support.error_or
-    libc.src.__support.macros.config
-    libc.hdr.types.ssize_t
-    libc.include.sys_syscall
-)
-
 add_header_library(
   recvfrom
   HDRS
@@ -202,19 +189,6 @@ add_header_library(
     libc.include.sys_syscall
 )
 
-add_header_library(
-  send
-  HDRS
-    send.h
-  DEPENDS
-    libc.src.__support.OSUtil.osutil
-    libc.src.__support.common
-    libc.src.__support.error_or
-    libc.src.__support.macros.config
-    libc.hdr.types.ssize_t
-    libc.include.sys_syscall
-)
-
 add_header_library(
   sendmsg
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/recv.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/recv.h
deleted file mode 100644
index 6aa580ba16cae..0000000000000
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/recv.h
+++ /dev/null
@@ -1,42 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 recv.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECV_H
-#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECV_H
-
-#include "hdr/types/ssize_t.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<ssize_t> recv(int sockfd, void *buf, size_t len,
-                                  int flags) {
-#ifdef SYS_recv
-  return syscall_checked<ssize_t>(SYS_recv, sockfd, buf, len, flags);
-#elif defined(SYS_recvfrom)
-  return syscall_checked<ssize_t>(SYS_recvfrom, sockfd, buf, len, flags,
-                                  nullptr, nullptr);
-#else
-#error "recv and recvfrom syscalls unavailable for this platform."
-#endif
-}
-
-} // namespace linux_syscalls
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RECV_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/send.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/send.h
deleted file mode 100644
index 0e4d831e8abc6..0000000000000
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/send.h
+++ /dev/null
@@ -1,42 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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 send.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEND_H
-#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEND_H
-
-#include "hdr/types/ssize_t.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<ssize_t> send(int sockfd, const void *buf, size_t len,
-                                  int flags) {
-#ifdef SYS_send
-  return syscall_checked<ssize_t>(SYS_send, sockfd, buf, len, flags);
-#elif defined(SYS_sendto)
-  return syscall_checked<ssize_t>(SYS_sendto, sockfd, buf, len, flags, nullptr,
-                                  0);
-#else
-#error "send or sendto syscalls unavailable for this platform."
-#endif
-}
-
-} // namespace linux_syscalls
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SEND_H
diff --git a/libc/src/sys/socket/linux/CMakeLists.txt b/libc/src/sys/socket/linux/CMakeLists.txt
index a04003787e4fe..4c7352dbd0200 100644
--- a/libc/src/sys/socket/linux/CMakeLists.txt
+++ b/libc/src/sys/socket/linux/CMakeLists.txt
@@ -147,7 +147,7 @@ add_entrypoint_object(
   DEPENDS
     libc.hdr.types.ssize_t
     libc.src.__support.common
-    libc.src.__support.OSUtil.linux.syscall_wrappers.send
+    libc.src.__support.OSUtil.linux.syscall_wrappers.sendto
     libc.src.errno.errno
 )
 
@@ -203,7 +203,7 @@ add_entrypoint_object(
     libc.src.__support.macros.sanitizer
     libc.hdr.types.ssize_t
     libc.src.__support.common
-    libc.src.__support.OSUtil.linux.syscall_wrappers.recv
+    libc.src.__support.OSUtil.linux.syscall_wrappers.recvfrom
     libc.src.errno.errno
 )
 
diff --git a/libc/src/sys/socket/linux/recv.cpp b/libc/src/sys/socket/linux/recv.cpp
index 83b0996dc738b..3c2dfdce7fbab 100644
--- a/libc/src/sys/socket/linux/recv.cpp
+++ b/libc/src/sys/socket/linux/recv.cpp
@@ -12,7 +12,7 @@
 //===----------------------------------------------------------------------===//
 #include "src/sys/socket/recv.h"
 #include "hdr/types/ssize_t.h"
-#include "src/__support/OSUtil/linux/syscall_wrappers/recv.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/recvfrom.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/sanitizer.h"
@@ -21,7 +21,8 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(ssize_t, recv,
                    (int sockfd, void *buf, size_t len, int flags)) {
-  auto result = linux_syscalls::recv(sockfd, buf, len, flags);
+  auto result =
+      linux_syscalls::recvfrom(sockfd, buf, len, flags, nullptr, nullptr);
   if (!result.has_value()) {
     libc_errno = result.error();
     return -1;
diff --git a/libc/src/sys/socket/linux/send.cpp b/libc/src/sys/socket/linux/send.cpp
index 0832d4fea248f..a3b7a20e3b8c4 100644
--- a/libc/src/sys/socket/linux/send.cpp
+++ b/libc/src/sys/socket/linux/send.cpp
@@ -12,7 +12,7 @@
 //===----------------------------------------------------------------------===//
 #include "src/sys/socket/send.h"
 #include "hdr/types/ssize_t.h"
-#include "src/__support/OSUtil/linux/syscall_wrappers/send.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/sendto.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(ssize_t, send,
                    (int sockfd, const void *buf, size_t len, int flags)) {
-  auto result = linux_syscalls::send(sockfd, buf, len, flags);
+  auto result = linux_syscalls::sendto(sockfd, buf, len, flags, nullptr, 0);
   if (!result.has_value()) {
     libc_errno = result.error();
     return -1;



More information about the libc-commits mailing list