Description of problem: nis+-based getgrent mistakenly sets errno when there are no more entries This is the root cause of a bug reported against coreutils: http://savannah.gnu.org/bugs/?22505 Version-Release number of selected component (if applicable): glibc-2.7.90-9.x86_64 (but also affects at least RHEL4.6 and FC5) How reproducible: every time Here's code to demonstrate the bug: cat <<\EOF > getgrent-test.c #include <sys/types.h> #include <grp.h> #include <errno.h> #include <stdio.h> static int count_groups (void) { int count = 0; setgrent (); while (1) { errno = 0; struct group *grp = getgrent (); if (!grp) break; ++count; } if (errno != 0) { perror ("getgrent failed"); count = -1; } int saved_errno = errno; endgrent (); errno = saved_errno; return count; } int main () { return count_groups () < 0; } EOF gcc -O -W -Wall getgrent-test.c && ./a.out && echo ok ====================================== When I run that on a rawhide system on which /etc/nsswitch.conf has this: group: files I get an exit status of 0, as expected. However, when I change nsswitch.conf, appending " nisplus" (and I haven't configured NIS+ at all): group: files nisplus and run ./a.out again, I get this output: getgrent failed: No such file or directory [Exit 1] Additional info:
Created attachment 298380 [details] bugfix patch
The patch I posted minutes ago is worth applying, but was not relevant to the getgrent bug. Sorry about the mix-up. Here's the patch that is likely to actually solve the problem. 2008-03-18 Jim Meyering <meyering> nis+/getgrent: don't set errno when there are no more entries * nis/nis_call.c (__libc_lock_define_initialized): Don't let ENOENT from failure to stat "/var/nis/NIS_COLD_START" affect leak out to confuse the getgrent caller, who must be able to distinguish a return value of NULL indicating no more values and a return value of NULL indicating an error occurred.
Created attachment 298395 [details] the proper bugfix
Fixed upstream with a patch different from the one attached here.
Should be in glibc-2.7.90-12 and above.