Compare commits

...

2 Commits

Author SHA1 Message Date
Mike Yuan 1e7460c17b
Merge 2890403e21 into 52b0351a15 2024-11-20 17:34:52 +09:00
Mike Yuan 2890403e21
basic/user-util: modernize getgroups_alloc() a bit 2024-11-19 00:36:45 +01:00
2 changed files with 18 additions and 23 deletions

View File

@ -526,21 +526,23 @@ int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t
return (int)nresult;
}
int getgroups_alloc(gid_t** gids) {
gid_t *allocated;
_cleanup_free_ gid_t *p = NULL;
int getgroups_alloc(gid_t **ret) {
int ngroups = 8;
unsigned attempt = 0;
allocated = new(gid_t, ngroups);
if (!allocated)
return -ENOMEM;
p = allocated;
assert(ret);
for (unsigned attempt = 0;;) {
_cleanup_free_ gid_t *p = NULL;
p = new(gid_t, ngroups);
if (!p)
return -ENOMEM;
for (;;) {
ngroups = getgroups(ngroups, p);
if (ngroups >= 0)
break;
if (ngroups >= 0) {
*ret = TAKE_PTR(p);
return ngroups;
}
if (errno != EINVAL)
return -errno;
@ -553,18 +555,11 @@ int getgroups_alloc(gid_t** gids) {
ngroups = getgroups(0, NULL);
if (ngroups < 0)
return -errno;
if (ngroups == 0)
return false;
free(allocated);
p = allocated = new(gid_t, ngroups);
if (!allocated)
return -ENOMEM;
if (ngroups == 0) {
*ret = NULL;
return 0;
}
}
*gids = TAKE_PTR(p);
return ngroups;
}
int get_home_dir(char **ret) {

View File

@ -64,7 +64,7 @@ int in_gid(gid_t gid);
int in_group(const char *name);
int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t size2, gid_t **result);
int getgroups_alloc(gid_t** gids);
int getgroups_alloc(gid_t **ret);
int get_home_dir(char **ret);
int get_shell(char **ret);