aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/lock.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-11-03 17:43:40 +0100
committerEelco Dolstra <edolstra@gmail.com>2022-11-03 17:43:40 +0100
commitb95faccf03e5213b6087626ab8d46e0704aad6b5 (patch)
treef9d560f2004f951d8efbf1024292a88366d8efd7 /src/libstore/lock.cc
parent47dec825c5daeeb9d615eb4d1eead3dbaa06c7c9 (diff)
parentdd1970c233a82328445b69e903574e14115ee933 (diff)
Merge remote-tracking branch 'origin/master' into auto-uid-allocation
Diffstat (limited to 'src/libstore/lock.cc')
-rw-r--r--src/libstore/lock.cc25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/libstore/lock.cc b/src/libstore/lock.cc
index 02ccb1186..cc3977496 100644
--- a/src/libstore/lock.cc
+++ b/src/libstore/lock.cc
@@ -80,13 +80,30 @@ struct SimpleUserLock : UserLock
/* Get the list of supplementary groups of this build
user. This is usually either empty or contains a
group such as "kvm". */
- lock->supplementaryGIDs.resize(10);
- int ngroups = lock->supplementaryGIDs.size();
- int err = getgrouplist(pw->pw_name, pw->pw_gid,
- lock->supplementaryGIDs.data(), &ngroups);
+ int ngroups = 32; // arbitrary initial guess
+ lock->supplementaryGIDs.resize(ngroups);
+
+ int err = getgrouplist(
+ pw->pw_name, pw->pw_gid,
+ lock->supplementaryGIDs.data(),
+ &ngroups);
+
+ /* Our initial size of 32 wasn't sufficient, the
+ correct size has been stored in ngroups, so we try
+ again. */
+ if (err == -1) {
+ lock->supplementaryGIDs.resize(ngroups);
+ err = getgrouplist(
+ pw->pw_name, pw->pw_gid,
+ lock->supplementaryGIDs.data(),
+ &ngroups);
+ }
+
+ // If it failed once more, then something must be broken.
if (err == -1)
throw Error("failed to get list of supplementary groups for '%s'", pw->pw_name);
+ // Finally, trim back the GID list to its real size.
lock->supplementaryGIDs.resize(ngroups);
#endif