[libc-commits] [libc] [libc] Add "struct linger" (PR #192606)
via libc-commits
libc-commits at lists.llvm.org
Fri Apr 17 00:34:55 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Pavel Labath (labath)
<details>
<summary>Changes</summary>
Add a simple test to get/set the socket option. I didn't try to test the actual lingering behavior. That sounds complicated and I'm not sure if it's even doable on a loopback connection.
---
Full diff: https://github.com/llvm/llvm-project/pull/192606.diff
8 Files Affected:
- (modified) libc/hdr/types/CMakeLists.txt (+9)
- (added) libc/hdr/types/struct_linger.h (+22)
- (modified) libc/include/CMakeLists.txt (+1)
- (modified) libc/include/llvm-libc-types/CMakeLists.txt (+1)
- (added) libc/include/llvm-libc-types/struct_linger.h (+17)
- (modified) libc/include/sys/socket.yaml (+1)
- (modified) libc/test/src/sys/socket/linux/CMakeLists.txt (+1)
- (modified) libc/test/src/sys/socket/linux/socketopt_test.cpp (+19)
``````````diff
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 7e1a8974fa486..374b97fd27493 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -385,6 +385,15 @@ add_proxy_header_library(
libc.include.setjmp
)
+add_proxy_header_library(
+ struct_linger
+ HDRS
+ struct_linger.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_linger
+ libc.include.sys_socket
+)
+
add_proxy_header_library(
struct_msghdr
HDRS
diff --git a/libc/hdr/types/struct_linger.h b/libc/hdr/types/struct_linger.h
new file mode 100644
index 0000000000000..6343e7dda2bdc
--- /dev/null
+++ b/libc/hdr/types/struct_linger.h
@@ -0,0 +1,22 @@
+//===-- Proxy for struct linger -------------------------------------------===//
+//
+// 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_LINGER_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_LINGER_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_linger.h"
+
+#else
+
+#include <sys/socket.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_LINGER_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index ab1d4967d4183..38cd3ff7f20eb 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -738,6 +738,7 @@ add_header_macro(
.llvm-libc-types.sa_family_t
.llvm-libc-types.socklen_t
.llvm-libc-types.struct_iovec
+ .llvm-libc-types.struct_linger
.llvm-libc-types.struct_msghdr
.llvm-libc-types.struct_sockaddr
)
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index e40f6e194ab3a..602350d42b5d9 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -196,6 +196,7 @@ add_header(socklen_t HDR socklen_t.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_linger HDR struct_linger.h)
add_header(struct_msghdr HDR struct_msghdr.h DEPENDS .size_t .socklen_t .struct_iovec)
add_header(ACTION HDR ACTION.h)
add_header(ENTRY HDR ENTRY.h)
diff --git a/libc/include/llvm-libc-types/struct_linger.h b/libc/include/llvm-libc-types/struct_linger.h
new file mode 100644
index 0000000000000..4fc19ff612998
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_linger.h
@@ -0,0 +1,17 @@
+//===-- Definition of struct linger ---------------------------------------===//
+//
+// 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_LINGER_H
+#define LLVM_LIBC_TYPES_STRUCT_LINGER_H
+
+struct linger {
+ int l_onoff; // Nonzero means "on"
+ int l_linger; // Number of seconds to linger
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_LINGER_H
diff --git a/libc/include/sys/socket.yaml b/libc/include/sys/socket.yaml
index 934a5ed400eeb..28a81328a3559 100644
--- a/libc/include/sys/socket.yaml
+++ b/libc/include/sys/socket.yaml
@@ -10,6 +10,7 @@ types:
- type_name: sa_family_t
- type_name: struct_msghdr
- type_name: struct_iovec
+ - type_name: struct_linger
- type_name: size_t
- type_name: ssize_t
enums: []
diff --git a/libc/test/src/sys/socket/linux/CMakeLists.txt b/libc/test/src/sys/socket/linux/CMakeLists.txt
index c8a08b0557ce9..af0a46e9a06bb 100644
--- a/libc/test/src/sys/socket/linux/CMakeLists.txt
+++ b/libc/test/src/sys/socket/linux/CMakeLists.txt
@@ -89,6 +89,7 @@ add_libc_unittest(
libc.include.sys_socket
libc.hdr.sys_socket_macros
libc.hdr.types.socklen_t
+ libc.hdr.types.struct_linger
libc.src.errno.errno
libc.src.sys.socket.getsockopt
libc.src.sys.socket.setsockopt
diff --git a/libc/test/src/sys/socket/linux/socketopt_test.cpp b/libc/test/src/sys/socket/linux/socketopt_test.cpp
index 67feef4670b0a..e069a7bd35fa6 100644
--- a/libc/test/src/sys/socket/linux/socketopt_test.cpp
+++ b/libc/test/src/sys/socket/linux/socketopt_test.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "hdr/sys_socket_macros.h"
+#include "hdr/types/struct_linger.h"
#include "src/sys/socket/getsockopt.h"
#include "src/sys/socket/setsockopt.h"
#include "src/sys/socket/socket.h"
@@ -55,6 +56,24 @@ TEST_F(LlvmLibcSocketOptTest, BasicSocketOpt) {
LIBC_NAMESPACE::setsockopt(sock, SOL_SOCKET, SO_TYPE, &optval, optlen),
Fails(ENOPROTOOPT));
+ // Test SO_LINGER (uses a struct)
+ struct linger lin;
+ lin.l_onoff = 1;
+ lin.l_linger = 5;
+ optlen = sizeof(lin);
+ ASSERT_THAT(
+ LIBC_NAMESPACE::setsockopt(sock, SOL_SOCKET, SO_LINGER, &lin, optlen),
+ Succeeds(0));
+
+ lin = {};
+ optlen = sizeof(lin);
+ ASSERT_THAT(
+ LIBC_NAMESPACE::getsockopt(sock, SOL_SOCKET, SO_LINGER, &lin, &optlen),
+ Succeeds(0));
+ ASSERT_EQ(lin.l_onoff, 1);
+ ASSERT_EQ(lin.l_linger, 5);
+ ASSERT_EQ(optlen, static_cast<socklen_t>(sizeof(lin)));
+
ASSERT_THAT(LIBC_NAMESPACE::close(sock), Succeeds(0));
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/192606
More information about the libc-commits
mailing list