[libc-commits] [libc] 8fdbba5 - [libc][unistd] Support _SC_GET(GR|PW)_R_SIZE_MAX in sysconf. (#222135)
via libc-commits
libc-commits at lists.llvm.org
Tue Sep 8 13:58:25 PDT 2026
Author: Alexey Samsonov
Date: 2026-09-08T13:58:20-07:00
New Revision: 8fdbba5073a6fe2ae16be0117f8f473eea27dc2a
URL: https://github.com/llvm/llvm-project/commit/8fdbba5073a6fe2ae16be0117f8f473eea27dc2a
DIFF: https://github.com/llvm/llvm-project/commit/8fdbba5073a6fe2ae16be0117f8f473eea27dc2a.diff
LOG: [libc][unistd] Support _SC_GET(GR|PW)_R_SIZE_MAX in sysconf. (#222135)
* Add support for POSIX-specified `_SC_GETGR_R_SIZE_MAX` and
`_SC_GETPW_R_SIZE_MAX`
constants used as arguments to `sysconf` function;
* Return -1 for these values - they provide "initial size" for
user-supplied buffers passed to `getgr*` and `getpw*`
functions, but there's no real maximum limit imposed by the LLVM-libc
implementation, and user might provide a
size they can, and rely on `ERANGE` return values if the chosen size
ended up being too small for pwd/group entries.
* Add unit test coverage, and update the unit test to use proxy headers
instead of system-provided `<unistd.h>`.
Also, properly test errno values returned by `sysconf`.
This fixes one of the remaining blockers for building Clang against
LLVM-libc (https://github.com/llvm/llvm-project/issues/97191)
Added:
Modified:
libc/include/llvm-libc-macros/linux/unistd-macros.h
libc/include/unistd.yaml
libc/src/unistd/linux/sysconf.cpp
libc/test/src/unistd/CMakeLists.txt
libc/test/src/unistd/sysconf_test.cpp
Removed:
################################################################################
diff --git a/libc/include/llvm-libc-macros/linux/unistd-macros.h b/libc/include/llvm-libc-macros/linux/unistd-macros.h
index 49409f83711cc..677d4a1c68ddd 100644
--- a/libc/include/llvm-libc-macros/linux/unistd-macros.h
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -26,6 +26,8 @@
#define _SC_PAGESIZE 30
#define _SC_PAGE_SIZE _SC_PAGESIZE
#define _SC_THREADS 67
+#define _SC_GETGR_R_SIZE_MAX 69
+#define _SC_GETPW_R_SIZE_MAX 70
#define _SC_NPROCESSORS_CONF 83
#define _SC_NPROCESSORS_ONLN 84
#define _SC_PHYS_PAGES 85
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 14c70a6757268..b3c6a0b15a261 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -26,6 +26,10 @@ macros:
macro_header: unistd-macros.h
- macro_name: _SC_THREADS
macro_header: unistd-macros.h
+ - macro_name: _SC_GETGR_R_SIZE_MAX
+ macro_header: unistd-macros.h
+ - macro_name: _SC_GETPW_R_SIZE_MAX
+ macro_header: unistd-macros.h
- macro_name: _SC_NPROCESSORS_CONF
macro_header: unistd-macros.h
- macro_name: _SC_NPROCESSORS_ONLN
diff --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
index 0e55a9e4db335..a4e9afc8d38f0 100644
--- a/libc/src/unistd/linux/sysconf.cpp
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -140,6 +140,14 @@ LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
return get_nprocessors_onln();
case _SC_THREADS:
return _POSIX_THREADS;
+ case _SC_GETGR_R_SIZE_MAX:
+ // No recommended buffer size for getgrgid_r/getgrnam_r, as they work
+ // with any user-supplied buffer.
+ return -1;
+ case _SC_GETPW_R_SIZE_MAX:
+ // No recommended buffer size for getpwuid_r/getpwnam_r, as they work
+ // with any user-supplied buffer.
+ return -1;
case _SC_OPEN_MAX:
return get_open_max();
case _SC_PHYS_PAGES:
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 50e0b937e8447..17960278bd3f7 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -832,8 +832,11 @@ add_libc_test(
SRCS
sysconf_test.cpp
DEPENDS
- libc.include.unistd
+ libc.hdr.errno_macros
+ libc.hdr.unistd_macros
libc.src.unistd.sysconf
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
)
add_libc_test(
diff --git a/libc/test/src/unistd/sysconf_test.cpp b/libc/test/src/unistd/sysconf_test.cpp
index 0fc2bcc5b3815..57a7e4955a45c 100644
--- a/libc/test/src/unistd/sysconf_test.cpp
+++ b/libc/test/src/unistd/sysconf_test.cpp
@@ -11,50 +11,60 @@
///
//===----------------------------------------------------------------------===//
+#include "hdr/errno_macros.h"
+#include "hdr/unistd_macros.h"
#include "src/unistd/sysconf.h"
-#include "test/UnitTest/Test.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
-#include <unistd.h>
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcSysconfTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
-TEST(LlvmLibcSysconfTest, PagesizeTest) {
+TEST_F(LlvmLibcSysconfTest, PagesizeTest) {
long pagesize = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
ASSERT_GT(pagesize, 0L);
}
-TEST(LlvmLibcSysconfTest, NprocessorsConfTest) {
+TEST_F(LlvmLibcSysconfTest, NprocessorsConfTest) {
long sysconf_count = LIBC_NAMESPACE::sysconf(_SC_NPROCESSORS_CONF);
ASSERT_GT(sysconf_count, 0L);
}
-TEST(LlvmLibcSysconfTest, NprocessorsOnlnTest) {
+TEST_F(LlvmLibcSysconfTest, NprocessorsOnlnTest) {
long sysconf_count = LIBC_NAMESPACE::sysconf(_SC_NPROCESSORS_ONLN);
ASSERT_GT(sysconf_count, 0L);
}
-TEST(LlvmLibcSysconfTest, ThreadsTest) {
+TEST_F(LlvmLibcSysconfTest, ThreadsTest) {
long threads = LIBC_NAMESPACE::sysconf(_SC_THREADS);
ASSERT_EQ(threads, _POSIX_THREADS);
}
-TEST(LlvmLibcSysconfTest, ArgMaxTest) {
+TEST_F(LlvmLibcSysconfTest, ArgMaxTest) {
long arg_max = LIBC_NAMESPACE::sysconf(_SC_ARG_MAX);
ASSERT_GT(arg_max, 0L);
ASSERT_GE(arg_max, 131072L);
}
-TEST(LlvmLibcSysconfTest, OpenMaxTest) {
+TEST_F(LlvmLibcSysconfTest, OpenMaxTest) {
long open_max = LIBC_NAMESPACE::sysconf(_SC_OPEN_MAX);
if (open_max == -1)
return;
ASSERT_GT(open_max, 0L);
}
-TEST(LlvmLibcSysconfTest, PhysPagesTest) {
+TEST_F(LlvmLibcSysconfTest, PhysPagesTest) {
long phys_pages = LIBC_NAMESPACE::sysconf(_SC_PHYS_PAGES);
ASSERT_GT(phys_pages, 0L);
}
-TEST(LlvmLibcSysconfTest, ClkTckTest) {
- long clk_tck = LIBC_NAMESPACE::sysconf(_SC_CLK_TCK);
- ASSERT_EQ(clk_tck, 100L);
+TEST_F(LlvmLibcSysconfTest, KnownConstantValuesTest) {
+ EXPECT_EQ(LIBC_NAMESPACE::sysconf(_SC_CLK_TCK), 100L);
+ EXPECT_EQ(LIBC_NAMESPACE::sysconf(_SC_GETGR_R_SIZE_MAX), -1L);
+ EXPECT_EQ(LIBC_NAMESPACE::sysconf(_SC_GETPW_R_SIZE_MAX), -1L);
+}
+
+TEST_F(LlvmLibcSysconfTest, InvalidNameTest) {
+ EXPECT_THAT(LIBC_NAMESPACE::sysconf(100000), Fails(EINVAL, -1L));
+ EXPECT_THAT(LIBC_NAMESPACE::sysconf(0x7fffffff), Fails(EINVAL, -1L));
}
More information about the libc-commits
mailing list