aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2024-02-14 17:10:54 -0800
committerElliott Hughes <enh@google.com>2024-02-14 17:19:14 -0800
commit6a458848514a1816c95f11c6baa30dfdfcbf2d34 (patch)
treeb7f5f413315b8df369d3657a243e3ae59830f549 /tests
parent469568b94ab7f6b36c0b753f029c7e3d04b6f40d (diff)
downloadbionic-6a458848514a1816c95f11c6baa30dfdfcbf2d34.tar.gz
Simplify our trivial initgroups(), and add a test.
initgroups() is just a call to getgrouplist() followed by a call to setgroups(). The tricky part is memory allocation. OpenBSD allocates an NGROUPS_MAX-sized array of gid_t on the stack. FreeBSD allocates a sysconf(_SC_NGROUPS_MAX)-sized array of gid_t on the heap. bionic had a mix where it would try a 2-element stack array but fall back to a heap allocation, which sounds reasonable if you want to avoid a 256KiB (64Ki*4 bytes) allocation on either stack or heap. But that constant 2? That's weird in two ways... It's really small (musl has an NGROUPS_MAX of 32 unlike the Linux kernel's 64Ki, but 32 is still a lot larger than 2), but at the same time it's too big --- bionic's getgrouplist() always returns a single element. So although the FreeBSD "what the hell, let's just allocate 256KiB on the heap" implementation would have been fine, there's really no point, and anyone who's trying to understand initgroups() on Android really needs to read getgroupslist() anyway, so let's just have the most trivial implementation -- a single-element array -- and let's have it right next to getgroupslist() in the same file as all the other <grp.h> functions. Also add a trivial smoke test. You mostly won't have permission to do anything interesting with initgroups(), and it's basically unused save for privilege dropping tcpdump and strace, but we may as well make an effort. (I tested tcpdump before and after too.) Test: treehugger Change-Id: I67fe02e309ed1dbefc490c01733738363ca606be
Diffstat (limited to 'tests')
-rw-r--r--tests/grp_pwd_test.cpp5
1 files changed, 5 insertions, 0 deletions
diff --git a/tests/grp_pwd_test.cpp b/tests/grp_pwd_test.cpp
index d3acf03d1..16b8d5ac5 100644
--- a/tests/grp_pwd_test.cpp
+++ b/tests/grp_pwd_test.cpp
@@ -851,6 +851,11 @@ TEST(grp, getgrouplist) {
#endif
}
+TEST(grp, initgroups) {
+ if (getuid() != 0) GTEST_SKIP() << "test requires root";
+ ASSERT_EQ(0, initgroups("root", 0));
+}
+
#if defined(__BIONIC__)
static void TestAidNamePrefix(const std::string& file_path) {
std::string file_contents;