Subject: useless calls of setpwent()/setgrent() in stat
From: Fridolín Pokorný <[email-masked]>
To: [email-masked]
Date: Fri, 24 May 2013 15:32:56 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi,

I am investigating an issue with stat. stat calls setpwent() (when
resolving a username) and setgrent() (when resolving a group name). This
has a bad impact when a LDAP is used and a database is quite large. It
forces to download the whole database. According to RFC 2307,
getpwuid()/getgrgid() are handled correctly with a search pattern to get
an username/group name and there is no need to call
setpwent()/setgrent(). My tests have approved this.

Even when the LDAP is not used, I don't see the point of using
setpwent() and setgrent() calls. On local system with /etc/passwd in
use, these calls does nothing in fact. For example setpwent() should be
used with functions getpwent()/endpwent(), on the other hand, setgrent()
is designed to be used with getgrent()/endgrent() functions. I cannot
find anything why are these functions used with getpwuid()/getgrgid().
Is there any non-documented behavior? For example nscd is using its own
cache and these calls are, again, useless.

I cannot determinate why these functions are called. They request
additional computation which is not used. In my opinion, these spare
calls should be removed.

If these calls are useful, why there are no endpwent()/endgrent() calls?

Thanks for your response!

Best regards,
Fridolin Pokorny.

---coreutils_old/src/stat.c	2013-05-24 08:14:35.922027283 +0200
+++ coreutils/src/stat.c	2013-05-24 08:14:56.076101484 +0200
@@ -965,7 +965,6 @@ print_stat (char *pformat, size_t prefix
       out_uint (pformat, prefix_len, statbuf->st_uid);
       break;
     case 'U':
-      setpwent ();
       pw_ent = getpwuid (statbuf->st_uid);
       out_string (pformat, prefix_len,
                   pw_ent ? pw_ent->pw_name : "UNKNOWN");
@@ -974,7 +973,6 @@ print_stat (char *pformat, size_t prefix
       out_uint (pformat, prefix_len, statbuf->st_gid);
       break;
     case 'G':
-      setgrent ();
       gw_ent = getgrgid (statbuf->st_gid);
       out_string (pformat, prefix_len,
                   gw_ent ? gw_ent->gr_name : "UNKNOWN");