summaryrefslogtreecommitdiffstats
path: root/lib/libc/gen
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/gen')
-rw-r--r--lib/libc/gen/initgroups.37
-rw-r--r--lib/libc/gen/initgroups.c21
2 files changed, 22 insertions, 6 deletions
diff --git a/lib/libc/gen/initgroups.3 b/lib/libc/gen/initgroups.3
index 2fe2dd9..3ed3ca7 100644
--- a/lib/libc/gen/initgroups.3
+++ b/lib/libc/gen/initgroups.3
@@ -65,6 +65,13 @@ function may fail and set
.Va errno
for any of the errors specified for the library function
.Xr setgroups 2 .
+It may also return:
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+The
+.Fn initgroups
+function was unable to allocate temporary storage.
+.El
.Sh SEE ALSO
.Xr setgroups 2 ,
.Xr getgrouplist 3
diff --git a/lib/libc/gen/initgroups.c b/lib/libc/gen/initgroups.c
index 299ae50..aacaf7e 100644
--- a/lib/libc/gen/initgroups.c
+++ b/lib/libc/gen/initgroups.c
@@ -35,10 +35,12 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
-#include <stdio.h>
#include "namespace.h"
#include <err.h>
#include "un-namespace.h"
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <unistd.h>
int
@@ -46,14 +48,21 @@ initgroups(uname, agroup)
const char *uname;
gid_t agroup;
{
- int ngroups;
+ int ngroups, ret;
+ long ngroups_max;
+ gid_t *groups;
+
/*
- * Provide space for one group more than NGROUPS to allow
+ * Provide space for one group more than possible to allow
* setgroups to fail and set errno.
*/
- gid_t groups[NGROUPS + 1];
+ ngroups_max = sysconf(_SC_NGROUPS_MAX) + 2;
+ if ((groups = malloc(sizeof(*groups) * ngroups_max)) == NULL)
+ return (ENOMEM);
- ngroups = NGROUPS + 1;
+ ngroups = (int)ngroups_max;
getgrouplist(uname, agroup, groups, &ngroups);
- return (setgroups(ngroups, groups));
+ ret = setgroups(ngroups, groups);
+ free(groups);
+ return (ret);
}
OpenPOWER on IntegriCloud