This is the mail archive of the
mailing list for the Cygwin project.
Why does CYGWIN implementation of getgrouplist() verify the supplied user or gid?
- From: "Lavrentiev, Anton (NIH/NLM/NCBI) [C]" <lavr at ncbi dot nlm dot nih dot gov>
- To: "cygwin at cygwin dot com" <cygwin at cygwin dot com>
- Date: Mon, 13 May 2013 20:51:35 +0000
- Subject: Why does CYGWIN implementation of getgrouplist() verify the supplied user or gid?
Per a hint I've got earlier today from this list (and which I appreciate!),
I've tried to use getgrouplist() to obtain group information about a user.
There's a problem with that.
I notice that the actual behavior of this call deviates from
what's documented: nowhere in the documentation that I found,
it states that the call fails for non-existent user and / or group ID
provided in the parameters; but only if there is insufficient space
to store the resultant group IDs (one place specifically mentions that
"if user does not refer to a valid user on the system, getgrouplist()
shall return 0, and set the value referenced by ngroups to 0").
I checked the attached test case on Linux, Free BSD and, oh well,
Mac OS X, and the results are pretty consistent:
for non-existent user, the call does _not_ fail (i.e. does not return -1)
but results in the list of GIDs containing only the GID passed in the
parameters, regardless whether or not that GID is valid (i.e. of an
If the user does exist, the provided GID gets added to the list, if it
does not happen to be there already (again, the GID is not checked
On Cygwin, the call always fails with return code -1 for nonexistent
user name and / or GID.
Note that errnos are not particularly documented for this call, so the
only reason for it to fail, is for insufficient buffer (otherwise, the
failure reasons cannot be distinguished)! To work around this
behavior I have to bulk the code with a number of '#ifdef CYGWIN'.
Why it's being different on CYGWIN?
#define _BSD_SOURCE 1
#define GROUP (-1L)
#define GROUP 0
// Use your login name as a command-line parameter
int main(int argc, char* argv)
static gid_t groups[NGROUPS_MAX + 1];
int ngroups = sizeof(groups) / sizeof(groups);
int rv = getgrouplist(argv, (gid_t) GROUP, groups, &ngroups);
if (rv < 0)
printf("getgrouplist(): %m, %d\n", ngroups);
else while (ngroups)
printf(" %u\n", (unsigned int) groups[--ngroups]);
gcc -Wall getgrouplist.c -o getgrouplist
Problem reports: http://cygwin.com/problems.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple