Updated: April 19, 2023 |
Determine the group access list for a user
#include <unistd.h> int getgrouplist( const char *name, gid_t basegid, gid_t *groups, int *ngroups );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The getgrouplist() function reads the group file and determines the group access list for the user specified in name, automatically adding basegid.
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <limits.h> int main() { int ngroups, i; gid_t *groups; ngroups = sysconf( _SC_NGROUPS_MAX ); groups = (gid_t *) malloc ( ngroups * sizeof (gid_t) ); if (groups == NULL) { perror ("malloc()"); return EXIT_FAILURE; } if ( getgrouplist( getlogin(), getegid(), groups, &ngroups) == -1) { printf ("Groups array is too small: %d\n", ngroups); } printf ("%s belongs to these groups: %d", getlogin(), getegid()); for (i=0; i < ngroups; i++) { printf (", %d", groups[i]); } printf ("\n"); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
The getgrouplist() function uses the routines based on getgrent(). If the invoking program uses any of these routines, the group structure will be overwritten in the call to getgrouplist().