[libc-commits] [libc] [llvm] [libc] Add functions to send/recv messages (PR #106467)

via libc-commits libc-commits at lists.llvm.org
Wed Sep 18 11:26:53 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Michael Jones (michaelrj-google)

<details>
<summary>Changes</summary>

This patch adds the necessary functions to send and receive messages
over a socket. Those functions are: recv, recvfrom, recvmsg, send,
sendto, sendmsg, and socketpair for testing.

---

Patch is 57.97 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/106467.diff


37 Files Affected:

- (modified) libc/config/linux/api.td (+4) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+7-1) 
- (modified) libc/hdr/types/CMakeLists.txt (+27) 
- (added) libc/hdr/types/socklen_t.h (+21) 
- (added) libc/hdr/types/ssize_t.h (+23) 
- (added) libc/hdr/types/struct_msghdr.h (+21) 
- (added) libc/hdr/types/struct_sockaddr.h (+21) 
- (modified) libc/include/CMakeLists.txt (+2) 
- (modified) libc/include/llvm-libc-types/CMakeLists.txt (+4-2) 
- (added) libc/include/llvm-libc-types/struct_iovec.h (+19) 
- (added) libc/include/llvm-libc-types/struct_msghdr.h (+26) 
- (modified) libc/newhdrgen/yaml/sys/socket.yaml (+92) 
- (modified) libc/spec/posix.td (+56) 
- (modified) libc/src/sys/socket/CMakeLists.txt (+44-2) 
- (modified) libc/src/sys/socket/linux/CMakeLists.txt (+84-3) 
- (added) libc/src/sys/socket/linux/recv.cpp (+47) 
- (added) libc/src/sys/socket/linux/recvfrom.cpp (+49) 
- (added) libc/src/sys/socket/linux/recvmsg.cpp (+42) 
- (added) libc/src/sys/socket/linux/send.cpp (+46) 
- (added) libc/src/sys/socket/linux/sendmsg.cpp (+42) 
- (added) libc/src/sys/socket/linux/sendto.cpp (+49) 
- (renamed) libc/src/sys/socket/linux/socketpair.cpp (+11-12) 
- (renamed) libc/src/sys/socket/recv.h (+5-5) 
- (added) libc/src/sys/socket/recvfrom.h (+25) 
- (added) libc/src/sys/socket/recvmsg.h (+22) 
- (added) libc/src/sys/socket/send.h (+21) 
- (added) libc/src/sys/socket/sendmsg.h (+22) 
- (added) libc/src/sys/socket/sendto.h (+25) 
- (added) libc/src/sys/socket/socketpair.h (+20) 
- (modified) libc/test/src/sys/socket/linux/CMakeLists.txt (+47-5) 
- (removed) libc/test/src/sys/socket/linux/bind_test.cpp (-54) 
- (added) libc/test/src/sys/socket/linux/send_recv_test.cpp (+73) 
- (added) libc/test/src/sys/socket/linux/sendmsg_recvmsg_test.cpp (+125) 
- (added) libc/test/src/sys/socket/linux/sendto_recvfrom_test.cpp (+75) 
- (added) libc/test/src/sys/socket/linux/socketpair_test.cpp (+37) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+126) 
- (added) utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel (+62) 


``````````diff
diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 6a7c64296bf922..46523b4ca4b8df 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -193,6 +193,10 @@ def SysSocketAPI : PublicAPI<"sys/socket.h"> {
     "socklen_t",
     "struct sockaddr",
     "struct sockaddr_un",
+    "struct msghdr",
+    "struct iovec",
+    "size_t",
+    "ssize_t",
   ];
 }
 
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 2a38db5bcdad8c..86fd33136832c7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1041,8 +1041,14 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.sys.select.select
 
     # sys/socket.h entrypoints
-    libc.src.sys.socket.bind
     libc.src.sys.socket.socket
+    libc.src.sys.socket.socketpair
+    libc.src.sys.socket.send
+    libc.src.sys.socket.sendto
+    libc.src.sys.socket.sendmsg
+    libc.src.sys.socket.recv
+    libc.src.sys.socket.recvfrom
+    libc.src.sys.socket.recvmsg
   )
 endif()
 
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 12641c4d93ffe8..2259ca02f2db69 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -199,3 +199,30 @@ add_proxy_header_library(
     libc.include.setjmp
 )
 
+
+add_proxy_header_library(
+  struct_msghdr
+  HDRS
+    struct_msghdr.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.struct_msghdr
+    libc.include.sys_socket
+)
+
+add_proxy_header_library(
+  struct_sockaddr
+  HDRS
+    struct_sockaddr.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.struct_sockaddr
+    libc.include.sys_socket
+)
+
+add_proxy_header_library(
+  socklen_t
+  HDRS
+    socklen_t.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.socklen_t
+    libc.include.sys_socket
+)
diff --git a/libc/hdr/types/socklen_t.h b/libc/hdr/types/socklen_t.h
new file mode 100644
index 00000000000000..79a6b9c7deadf0
--- /dev/null
+++ b/libc/hdr/types/socklen_t.h
@@ -0,0 +1,21 @@
+//===-- Proxy for socklen_t -----------------------------------------------===//
+//
+// 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_TYPES_SOCKLEN_T_H
+#define LLVM_LIBC_HDR_TYPES_SOCKLEN_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/socklen_t.h"
+
+#else
+
+#include <signal.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_SOCKLEN_T_H
diff --git a/libc/hdr/types/ssize_t.h b/libc/hdr/types/ssize_t.h
new file mode 100644
index 00000000000000..4d2000780ee11f
--- /dev/null
+++ b/libc/hdr/types/ssize_t.h
@@ -0,0 +1,23 @@
+//===-- Proxy for ssize_t -------------------------------------------------===//
+//
+// 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_TYPES_SSIZE_T_H
+#define LLVM_LIBC_HDR_TYPES_SSIZE_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/ssize_t.h"
+
+#else
+
+#define __need_ssize_t
+#include <stddef.h>
+#undef __need_ssize_t
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_SSIZE_T_H
diff --git a/libc/hdr/types/struct_msghdr.h b/libc/hdr/types/struct_msghdr.h
new file mode 100644
index 00000000000000..6a36af791e2147
--- /dev/null
+++ b/libc/hdr/types/struct_msghdr.h
@@ -0,0 +1,21 @@
+//===-- Proxy for struct msghdr  ------------------------------------------===//
+//
+// 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_TYPES_STRUCT_MSGHDR_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_MSGHDR_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_msghdr.h"
+
+#else
+
+#include <sys/socket.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_MSGHDR_H
diff --git a/libc/hdr/types/struct_sockaddr.h b/libc/hdr/types/struct_sockaddr.h
new file mode 100644
index 00000000000000..0fc31d53748540
--- /dev/null
+++ b/libc/hdr/types/struct_sockaddr.h
@@ -0,0 +1,21 @@
+//===-- Proxy for struct sockaddr  ----------------------------------------===//
+//
+// 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_TYPES_STRUCT_SOCKADDR_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_SOCKADDR_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_sockaddr.h"
+
+#else
+
+#include <sys/socket.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_SOCKADDR_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index dfa5063889e8ab..16c2ac5124c843 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -593,6 +593,8 @@ add_header_macro(
     .llvm-libc-macros.sys_socket_macros
     .llvm-libc-types.sa_family_t
     .llvm-libc-types.socklen_t
+    .llvm-libc-types.struct_iovec
+    .llvm-libc-types.struct_msghdr
     .llvm-libc-types.struct_sockaddr
     .llvm-libc-types.struct_sockaddr_un
 )
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 583b84ccaae67c..a4cf4631c8470e 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -117,8 +117,10 @@ add_header(
 add_header(wint_t HDR wint_t.h)
 add_header(sa_family_t HDR sa_family_t.h)
 add_header(socklen_t HDR socklen_t.h)
-add_header(struct_sockaddr_un HDR struct_sockaddr_un.h)
-add_header(struct_sockaddr HDR struct_sockaddr.h)
+add_header(struct_sockaddr_un HDR struct_sockaddr_un.h DEPENDS .sa_family_t)
+add_header(struct_sockaddr HDR struct_sockaddr.h DEPENDS .sa_family_t)
+add_header(struct_iovec HDR struct_iovec.h DEPENDS .size_t)
+add_header(struct_msghdr HDR struct_msghdr.h DEPENDS .size_t .socklen_t .struct_iovec)
 add_header(rpc_opcodes_t HDR rpc_opcodes_t.h)
 add_header(ACTION HDR ACTION.h)
 add_header(ENTRY HDR ENTRY.h)
diff --git a/libc/include/llvm-libc-types/struct_iovec.h b/libc/include/llvm-libc-types/struct_iovec.h
new file mode 100644
index 00000000000000..4f889d3d038a11
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_iovec.h
@@ -0,0 +1,19 @@
+//===-- Definition of struct iovec ----------------------------------------===//
+//
+// 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_TYPES_STRUCT_IOVEC_H__
+#define __LLVM_LIBC_TYPES_STRUCT_IOVEC_H__
+
+#include "llvm-libc-types/size_t.h"
+
+struct iovec {
+  void *iov_base;
+  size_t iov_len;
+};
+
+#endif // __LLVM_LIBC_TYPES_STRUCT_IOVEC_H__
diff --git a/libc/include/llvm-libc-types/struct_msghdr.h b/libc/include/llvm-libc-types/struct_msghdr.h
new file mode 100644
index 00000000000000..afd37263fd22c1
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_msghdr.h
@@ -0,0 +1,26 @@
+//===-- Definition of struct msghdr ---------------------------------------===//
+//
+// 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_TYPES_STRUCT_MSGHDR_H__
+#define __LLVM_LIBC_TYPES_STRUCT_MSGHDR_H__
+
+#include "llvm-libc-types/size_t.h"
+#include "llvm-libc-types/socklen_t.h"
+#include "llvm-libc-types/struct_iovec.h"
+
+struct msghdr {
+  void *msg_name;        /* Optional address */
+  socklen_t msg_namelen; /* Size of address */
+  struct iovec *msg_iov; /* Scatter/gather array */
+  size_t msg_iovlen;     /* # elements in msg_iov */
+  void *msg_control;     /* Ancillary data, see below */
+  size_t msg_controllen; /* Ancillary data buffer len */
+  int msg_flags;         /* Flags (unused) */
+};
+
+#endif // __LLVM_LIBC_TYPES_STRUCT_MSGHDR_H__
diff --git a/libc/newhdrgen/yaml/sys/socket.yaml b/libc/newhdrgen/yaml/sys/socket.yaml
index 3b8bf4cecfe5aa..accb4b14099e82 100644
--- a/libc/newhdrgen/yaml/sys/socket.yaml
+++ b/libc/newhdrgen/yaml/sys/socket.yaml
@@ -5,9 +5,21 @@ types:
   - type_name: struct_sockaddr
   - type_name: socklen_t
   - type_name: sa_family_t
+  - type_name: struct_msghdr
+  - type_name: struct_iovec
+  - type_name: size_t
+  - type_name: ssize_t
 enums: []
 objects: []
 functions:
+  - name: accept
+    standards:
+      - POSIX
+    return_type: int
+    arguments:
+      - type: int
+      - type: sockaddr *__restrict
+      - type: socklen_t *__restrict
   - name: bind
     standards:
       - POSIX
@@ -16,6 +28,77 @@ functions:
       - type: int
       - type: const struct sockaddr *
       - type: socklen_t
+  - name: connect
+    standards:
+      - POSIX
+    return_type: int
+    arguments:
+      - type: int
+      - type: const struct sockaddr *
+      - type: socklen_t
+  - name: listen
+    standards:
+      - POSIX
+    return_type: int
+    arguments:
+      - type: int
+      - type: int
+  - name: recv
+    standards:
+      - POSIX
+    return_type: ssize_t
+    arguments:
+      - type: int
+      - type: const void *
+      - type: size_t
+      - type: int
+  - name: recvfrom
+    standards:
+      - POSIX
+    return_type: ssize_t
+    arguments:
+      - type: int
+      - type: const void*
+      - type: size_t
+      - type: int
+      - type: const struct sockaddr *
+      - type: socklen_t
+  - name: recvmsg
+    standards:
+      - POSIX
+    return_type: ssize_t
+    arguments:
+      - type: int
+      - type: const struct msghdr *
+      - type: int
+  - name: send
+    standards:
+      - POSIX
+    return_type: ssize_t
+    arguments:
+      - type: int
+      - type: const void*
+      - type: size_t
+      - type: int
+  - name: sendmsg
+    standards:
+      - POSIX
+    return_type: ssize_t
+    arguments:
+      - type: int
+      - type: const struct msghdr *
+      - type: int
+  - name: sendto
+    standards:
+      - POSIX
+    return_type: ssize_t
+    arguments:
+      - type: int
+      - type: const void *
+      - type: size_t
+      - type: int
+      - type: const struct sockaddr *
+      - type: socklen_t
   - name: socket
     standards:
       - POSIX
@@ -24,3 +107,12 @@ functions:
       - type: int
       - type: int
       - type: int
+  - name: socketpair
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: int
+      - type: int
+      - type: int
+      - type: int*
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index 085f2ec34ab346..beede79a38ec24 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -80,11 +80,20 @@ def GetoptArgvT : NamedType<"__getoptargv_t">;
 
 def SAFamilyType : NamedType<"sa_family_t">;
 def SocklenType : NamedType<"socklen_t">;
+def SocklenPtr : PtrType<SocklenType>;
 
 def StructSockAddr : NamedType<"struct sockaddr">;
 def StructSockAddrPtr : PtrType<StructSockAddr>;
 def ConstStructSockAddrPtr : ConstType<StructSockAddrPtr>;
 
+def StructMsghdr : NamedType<"struct msghdr">;
+def StructMsghdrPtr : PtrType<StructMsghdr>;
+def ConstStructMsghdrPtr : ConstType<StructMsghdrPtr>;
+
+def StructIovec : NamedType<"struct iovec">;
+def StructIovecPtr : PtrType<StructIovec>;
+def ConstStructIovecPtr : ConstType<StructIovecPtr>;
+
 def StructSockAddrUn : NamedType<"struct sockaddr_un">;
 
 def StructStatvfs : NamedType<"struct statvfs">;
@@ -1711,10 +1720,14 @@ def POSIX : StandardSpec<"POSIX"> {
         Macro<"SOCK_PACKET">,
       ], // Macros
       [
+        SizeTType,
+        SSizeTType,
         SAFamilyType,
         StructSockAddr,
         StructSockAddrUn,
         SocklenType,
+        StructIovec,
+        StructMsghdr,
       ], // Types
       [], // Enumerations
       [
@@ -1723,11 +1736,54 @@ def POSIX : StandardSpec<"POSIX"> {
           RetValSpec<IntType>,
           [ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<IntType>]
         >,
+        FunctionSpec<
+          "socketpair",
+          RetValSpec<IntType>,
+          [ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<IntPtr>]
+        >,
         FunctionSpec<
           "bind",
           RetValSpec<IntType>,
           [ArgSpec<IntType>, ArgSpec<ConstStructSockAddrPtr>, ArgSpec<SocklenType>]
         >,
+        FunctionSpec<
+          "send",
+          RetValSpec<SSizeTType>,
+          [ArgSpec<IntType>, ArgSpec<ConstVoidPtr>, ArgSpec<SizeTType>, ArgSpec<IntType>]
+        >,
+        FunctionSpec<
+          "sendto",
+          RetValSpec<SSizeTType>,
+          [
+            ArgSpec<IntType>, ArgSpec<ConstVoidPtr>, ArgSpec<SizeTType>,
+            ArgSpec<IntType>, ArgSpec<ConstStructSockAddrPtr>,
+            ArgSpec<SocklenType>
+          ]
+        >,
+        FunctionSpec<
+          "sendmsg",
+          RetValSpec<SSizeTType>,
+          [ArgSpec<IntType>, ArgSpec<ConstStructMsghdrPtr>, ArgSpec<IntType>]
+        >,
+        FunctionSpec<
+          "recv",
+          RetValSpec<SSizeTType>,
+          [ArgSpec<IntType>, ArgSpec<VoidPtr>, ArgSpec<SizeTType>, ArgSpec<IntType>]
+        >,
+        FunctionSpec<
+          "recvfrom",
+          RetValSpec<SSizeTType>,
+          [
+            ArgSpec<IntType>, ArgSpec<VoidPtr>, ArgSpec<SizeTType>,
+            ArgSpec<IntType>, ArgSpec<StructSockAddrPtr>,
+            ArgSpec<SocklenPtr>
+          ]
+        >,
+        FunctionSpec<
+          "recvmsg",
+          RetValSpec<SSizeTType>,
+          [ArgSpec<IntType>, ArgSpec<StructMsghdrPtr>, ArgSpec<IntType>]
+        >,
       ]  // Functions
   >;
 
diff --git a/libc/src/sys/socket/CMakeLists.txt b/libc/src/sys/socket/CMakeLists.txt
index e0bc48735a0314..e283c12abef378 100644
--- a/libc/src/sys/socket/CMakeLists.txt
+++ b/libc/src/sys/socket/CMakeLists.txt
@@ -10,8 +10,50 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
-  bind
+  socketpair
   ALIAS
   DEPENDS
-    .${LIBC_TARGET_OS}.bind
+    .${LIBC_TARGET_OS}.socketpair
+)
+
+add_entrypoint_object(
+  send
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.send
+)
+
+add_entrypoint_object(
+  sendto
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sendto
+)
+
+add_entrypoint_object(
+  sendmsg
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sendmsg
+)
+
+add_entrypoint_object(
+  recv
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.recv
+)
+
+add_entrypoint_object(
+  recvfrom
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.recvfrom
+)
+
+add_entrypoint_object(
+  recvmsg
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.recvmsg
 )
diff --git a/libc/src/sys/socket/linux/CMakeLists.txt b/libc/src/sys/socket/linux/CMakeLists.txt
index fc9febdec2cc3c..5ced5c5e310d6e 100644
--- a/libc/src/sys/socket/linux/CMakeLists.txt
+++ b/libc/src/sys/socket/linux/CMakeLists.txt
@@ -12,14 +12,95 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
-  bind
+  socketpair
   SRCS
-    bind.cpp
+    socketpair.cpp
   HDRS
-    ../bind.h
+    ../socketpair.h
   DEPENDS
     libc.include.sys_syscall
     libc.include.sys_socket
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
 )
+
+add_entrypoint_object(
+  send
+  SRCS
+    send.cpp
+  HDRS
+    ../send.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.hdr.types.struct_sockaddr
+    libc.hdr.types.socklen_t
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sendto
+  SRCS
+    sendto.cpp
+  HDRS
+    ../sendto.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.hdr.types.struct_sockaddr
+    libc.hdr.types.socklen_t
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sendmsg
+  SRCS
+    sendmsg.cpp
+  HDRS
+    ../sendmsg.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.hdr.types.struct_msghdr
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+add_entrypoint_object(
+  recv
+  SRCS
+    recv.cpp
+  HDRS
+    ../recv.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.hdr.types.struct_sockaddr
+    libc.hdr.types.socklen_t
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  recvfrom
+  SRCS
+    recvfrom.cpp
+  HDRS
+    ../recvfrom.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.hdr.types.struct_sockaddr
+    libc.hdr.types.socklen_t
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  recvmsg
+  SRCS
+    recvmsg.cpp
+  HDRS
+    ../recvmsg.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.hdr.types.struct_msghdr
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
diff --git a/libc/src/sys/socket/linux/recv.cpp b/libc/src/sys/socket/linux/recv.cpp
new file mode 100644
index 00000000000000..96acf449dc4bfd
--- /dev/null
+++ b/libc/src/sys/socket/linux/recv.cpp
@@ -0,0 +1,47 @@
+//===-- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/socket/recv.h"
+
+#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/common.h"
+#include "src/errno/libc_errno.h"
+#include <linux/net.h>   // For SYS_SOCKET socketcall number.
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(ssize_t, recv,
+                   (int sockfd, const void *buf, size_t len, int flags)) {
+#ifdef SYS_recv
+  ssize_t ret =
+      LIBC_NAMESPACE::syscall_impl<int>(SYS_recv, sockfd, buf, len, flags);
+#elif defined(SYS_recvfrom)
+  ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_recvfrom, sockfd,
+                                                  reinterpret_cast<long>(buf),
+                                                  len, flags, nullptr, 0);
+#elif defined(SYS_socketcall)
+  unsigned long sockcall_args[4] = {
+      static_cast<unsigned long>(sockfd), reinterpret_cast<unsigned long>(buf),
+      static_cast<unsigned long>(len), static_cast<unsigned long>(flags)};
+  ssize_t ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_RECV,
+                                                  sockcall_args);
+#else
+#error "socket and socketcall syscalls unavailable for this platform."
+#endif
+  if (ret < 0) {
+    libc_errno = static_cast<int>(-ret);
+    return -1;
+  }
+  return ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/socket/linux/recvfrom.cpp b/libc/src/sys/socket/linux/recvfrom.cpp
new file mode 100644
index 00000000000000..17489a99c922dc
--- /dev/null
+++ b/libc/src/sys/socket/linux/recvfrom.cpp
@@ -0,0 +1,49 @@
+//===-- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/socket/recvfrom.h"
+
+#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/common.h"
+#include "src/errno/libc_errno.h"
+#include <linux/net.h>   // For SYS_SOCK...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/106467


More information about the libc-commits mailing list