[libc-commits] [libc] bafbd54 - [libc] Add "struct linger" (#192606)
via libc-commits
libc-commits at lists.llvm.org
Mon Apr 20 01:36:45 PDT 2026
Author: Pavel Labath
Date: 2026-04-20T08:36:40Z
New Revision: bafbd54a904ffd69f36e6d9ffcd28e6c1431db97
URL: https://github.com/llvm/llvm-project/commit/bafbd54a904ffd69f36e6d9ffcd28e6c1431db97
DIFF: https://github.com/llvm/llvm-project/commit/bafbd54a904ffd69f36e6d9ffcd28e6c1431db97.diff
LOG: [libc] Add "struct linger" (#192606)
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.
Added:
libc/hdr/types/struct_linger.h
libc/include/llvm-libc-types/struct_linger.h
Modified:
libc/hdr/types/CMakeLists.txt
libc/include/CMakeLists.txt
libc/include/llvm-libc-types/CMakeLists.txt
libc/include/sys/socket.yaml
libc/test/src/sys/socket/linux/CMakeLists.txt
libc/test/src/sys/socket/linux/socketopt_test.cpp
Removed:
################################################################################
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 7395f70c0d2aa..3a88f781b4de0 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 74d31025f9f4d..0b8b17ee127be 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 021e0a29555cf..e05e3fbc2d76f 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -197,6 +197,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));
}
More information about the libc-commits
mailing list