Compare commits

...

1 Commits

Author SHA1 Message Date
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

@ -528,21 +528,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)
return -ENOMEM; for (unsigned attempt = 0;;) {
p = allocated; _cleanup_free_ gid_t *p = NULL;
p = new(gid_t, ngroups);
if (!p)
return -ENOMEM;
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;
@ -555,18 +557,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

@ -52,7 +52,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);