From: Alejandro Colomar Date: Tue, 19 Nov 2024 00:20:56 +0000 (+0100) Subject: lib/shadow/grp/: agetgroups(): Add function X-Git-Tag: 4.17.3~31 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=05322ed89a1c46f461dffe11395d01fe950f6470;p=thirdparty%2Fshadow.git lib/shadow/grp/: agetgroups(): Add function This encapsulates the logic for calling getgroups(3), which requires two calls plus a malloc(3) call to do it correctly. Reviewed-by: Serge Hallyn Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 95db1053c..dc67df9e8 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -173,6 +173,8 @@ libshadow_la_SOURCES = \ sgroupio.c \ sgroupio.h\ shadow.c \ + shadow/grp/agetgroups.c \ + shadow/grp/agetgroups.h \ shadowio.c \ shadowio.h \ shadowlog.c \ diff --git a/lib/shadow/grp/agetgroups.c b/lib/shadow/grp/agetgroups.c new file mode 100644 index 000000000..9bc47cc61 --- /dev/null +++ b/lib/shadow/grp/agetgroups.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "shadow/grp/agetgroups.h" + +#include +#include + + +extern inline gid_t *agetgroups(size_t *ngids); diff --git a/lib/shadow/grp/agetgroups.h b/lib/shadow/grp/agetgroups.h new file mode 100644 index 000000000..431775565 --- /dev/null +++ b/lib/shadow/grp/agetgroups.h @@ -0,0 +1,52 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_SHADOW_GRP_AGETGROUPS_H_ +#define SHADOW_INCLUDE_LIB_SHADOW_GRP_AGETGROUPS_H_ + + +#include + +#include +#include +#include +#include + +#include "alloc/malloc.h" +#include "attr.h" + + +ATTR_ACCESS(write_only, 1) +ATTR_MALLOC(free) +inline gid_t *agetgroups(size_t *ngids); + + +// Like getgroups(3), but allocate the buffer. +// *ngids is used to return the number of elements in the allocated array. +inline gid_t * +agetgroups(size_t *ngids) +{ + int n; + gid_t *gids; + + n = getgroups(0, NULL); + if (n == -1) + return NULL; + + gids = MALLOC(n, gid_t); + if (gids == NULL) + return NULL; + + n = getgroups(n, gids); + if (n == -1) { + free(gids); + return NULL; + } + + *ngids = n; + return gids; +} + + +#endif // include guard