Compare commits

...

2 Commits

Author SHA1 Message Date
Mike Yuan ba56e9c55b
Merge 2890403e21 into 69af4849aa 2024-11-20 20:39:52 +01: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; return (int)nresult;
} }
int getgroups_alloc(gid_t** gids) { int getgroups_alloc(gid_t **ret) {
gid_t *allocated;
_cleanup_free_ gid_t *p = NULL;
int ngroups = 8; int ngroups = 8;
unsigned attempt = 0;
allocated = new(gid_t, ngroups); assert(ret);
if (!allocated)
for (unsigned attempt = 0;;) {
_cleanup_free_ gid_t *p = NULL;
p = new(gid_t, ngroups);
if (!p)
return -ENOMEM; return -ENOMEM;
p = allocated;
for (;;) {
ngroups = getgroups(ngroups, p); ngroups = getgroups(ngroups, p);
if (ngroups >= 0) if (ngroups >= 0) {
break; *ret = TAKE_PTR(p);
return ngroups;
}
if (errno != EINVAL) if (errno != EINVAL)
return -errno; return -errno;
@ -553,18 +555,11 @@ int getgroups_alloc(gid_t** gids) {
ngroups = getgroups(0, NULL); ngroups = getgroups(0, NULL);
if (ngroups < 0) if (ngroups < 0)
return -errno; return -errno;
if (ngroups == 0) if (ngroups == 0) {
return false; *ret = NULL;
return 0;
free(allocated); }
p = allocated = new(gid_t, ngroups);
if (!allocated)
return -ENOMEM;
} }
*gids = TAKE_PTR(p);
return ngroups;
} }
int get_home_dir(char **ret) { 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 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 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_home_dir(char **ret);
int get_shell(char **ret); int get_shell(char **ret);