Call it regardless of having added any groups. If the group list is the
same that getgroups(3) gave us, setgroups(3) will be a no-op, and it
simplifies the surrounding code, by removing the 'added' variable, and
allowing to call lsearch(3) instead of lfind(3).
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
{
GETGROUPS_T *grouplist;
int ngroups;
- bool added;
char *g, *p;
char buf[1024];
FILE *shadow_logfd = log_get_logfd();
if (ngroups == -1)
goto free_gids;
- added = false;
p = buf;
while (NULL != (g = strsep(&p, ",:"))) {
struct group *grp;
grouplist[ngroups] = grp->gr_gid;
ngroups++;
- added = true;
}
- if (added) {
- if (setgroups(ngroups, grouplist) == -1)
- goto free_gids;
- }
+ if (setgroups(ngroups, grouplist) == -1)
+ goto free_gids;
free (grouplist);
return 0;
(void) fputs (_("too many groups\n"), stderr);
} else {
grouplist[ngroups++] = gid;
- if (setgroups (ngroups, grouplist) != 0) {
- perror ("setgroups");
- }
}
}
+ if (setgroups(ngroups, grouplist) == -1)
+ perror("setgroups");
+
/*
* Close all files before changing the user/group IDs.
*