@@ -43,15 +43,18 @@ get_current_uid()
43
43
const uid_t ThreadScope::orig_uid = getuid();
44
44
const gid_t ThreadScope::orig_gid = getgid();
45
45
const std::vector<gid_t > ThreadScope::orig_groups = get_process_groups();
46
+ int ThreadScope::passwd_buf_size = -1 ;
46
47
47
48
static int
48
49
get_supplemental_groups_by_uid (uid_t uid, std::vector<gid_t >& groups)
49
50
{
50
- // getpwuid will only indicate if an error happened by setting errno. set it to 0, so will know if there is a change
51
- errno = 0 ;
52
- struct passwd * pw = getpwuid (uid);
53
- if (pw == NULL ) {
54
- if (errno == 0 ) {
51
+ std::unique_ptr<char []> buf (new char [ThreadScope::passwd_buf_size]);
52
+ struct passwd pwd;
53
+ struct passwd *getpwnam_res;
54
+
55
+ const int res = getpwuid_r (uid, &pwd, buf.get (), ThreadScope::passwd_buf_size, &getpwnam_res);
56
+ if (getpwnam_res == NULL ) {
57
+ if (res == 0 ) {
55
58
// LOG("get_supplemental_groups_by_uid: no record for uid " << uid);
56
59
} else {
57
60
LOG (" WARNING: get_supplemental_groups_by_uid: getpwuid failed: " << strerror (errno));
@@ -61,7 +64,7 @@ get_supplemental_groups_by_uid(uid_t uid, std::vector<gid_t>& groups)
61
64
int ngroups = NGROUPS_MAX;
62
65
// for some reason on mac getgrouplist accepts an array of int instead of gid_t. so need to create a vector of int and then insert it into groups
63
66
std::vector<int > tmp_groups (ngroups);
64
- if (getgrouplist (pw-> pw_name , pw-> pw_gid , &tmp_groups[0 ], &ngroups) < 0 ) {
67
+ if (getgrouplist (pwd. pw_name , pwd. pw_gid , &tmp_groups[0 ], &ngroups) < 0 ) {
65
68
LOG (" get_supplemental_groups_by_uid: getgrouplist failed: ngroups too small " << ngroups);
66
69
return -1 ;
67
70
}
0 commit comments