[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: oddness from getgrnam()



> 
> I was testing a program and to see how it handled an invalid group I did:
> 
> gr = getgrnam("bob");
> 
> Now obviously this failed.  However the string from perror() states:
> 
> "Could not find file or directory"
> 
> Why is this?  Seems like a counter intuitive error.

I think the error is not set by getgrnam().  If I run

------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <grp.h>

int main(void)
{
  struct group* gr;
  perror("before");
  gr = getgrnam("tgakem");
  perror("after");

  return 0;
}
------------------------------------------------------------------------

it always prints

before: Success
after: No such file or directory

It makes no difference if "tgakem" exists (which it happens to do) or
if it doesn't.  My guess is that getgrnam tries to look for group
descriptions in several files, of which at least one does not exist.

If I run the above program with strace (strace ./prg) and grep for
`open', I get

open("/etc/ld.so.preload", O_RDONLY)    = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
open("/lib/libc.so.6", O_RDONLY)        = 3
open("/etc/nsswitch.conf", O_RDONLY)    = 3
open("/etc/ld.so.cache", O_RDONLY)      = 3
open("/lib/libnss_compat.so.1", O_RDONLY) = 3
open("/lib/libnsl.so.1", O_RDONLY)      = 3
open("/lib/libnss_files.so.1", O_RDONLY) = 3
open("/etc/group", O_RDONLY)            = 3

As you can see the first file could not be opened.  I checked and it
indeed does not exist on my system, so that will have triggered the
error message.  The getgrnam function returns NULL on failure, and
apparently does not manipulate errno.

HTH,
Eric

-- 
 E.L. Meijer (tgakem@chem.tue.nl)
 Eindhoven Univ. of Technology
 Lab. for Catalysis and Inorg. Chem. (SKA)


Reply to: