Unix Technical Forum

getpwnam problem

This is a discussion on getpwnam problem within the HP-UX Operating System forums, part of the Unix Operating Systems category; --> Hi there, I have a program that call getpwnam to get the user info, but sometimes the call returned ...


Go Back   Unix Technical Forum > Unix Operating Systems > HP-UX Operating System

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-16-2008, 06:15 PM
ultra
 
Posts: n/a
Default getpwnam problem

Hi there,
I have a program that call getpwnam to get the user info, but sometimes
the call returned NULL with errno EINVAL. I always pass the same user to the
call. Anyone know what is the reason? I'm running HP11.00 and NIS.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-16-2008, 06:15 PM
Aaron Isotton
 
Posts: n/a
Default Re: getpwnam problem

ultra wrote:
> Hi there,
> I have a program that call getpwnam to get the user info, but sometimes
> the call returned NULL with errno EINVAL. I always pass the same user to the
> call. Anyone know what is the reason? I'm running HP11.00 and NIS.
>
>


getpwnam() returns NULL if:

a) the user doesn't exits
b) an error occurred

When an error occurred, errno is set appropriately. When the user
doesn't exist, errno is NOT set. Thus you want to use some code as follows:

struct passwd *pw;

/* we reset errno so we can tell whether an error happens in getpwnam()
* or not */
errno = 0;
pw = getpwnam("some_user");
if (!pw) {
if (errno) fprintf(stderr, "error: %s\n", strerror(errno));
else fprintf(stderr, "no such user\n");
}

Greetings,
Aaron
--
Aaron Isotton | http://www.isotton.com/
You know it's Monday when you wake up and it's Tuesday. -- Garfield
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-16-2008, 06:15 PM
ultra
 
Posts: n/a
Default Re: getpwnam problem

>
> getpwnam() returns NULL if:
>
> a) the user doesn't exits
> b) an error occurred
>
> When an error occurred, errno is set appropriately. When the user
> doesn't exist, errno is NOT set. Thus you want to use some code as

follows:
>
> struct passwd *pw;
>
> /* we reset errno so we can tell whether an error happens in getpwnam()
> * or not */
> errno = 0;
> pw = getpwnam("some_user");
> if (!pw) {
> if (errno) fprintf(stderr, "error: %s\n", strerror(errno));
> else fprintf(stderr, "no such user\n");
> }
>
> Greetings,
> Aaron
> --
> Aaron Isotton | http://www.isotton.com/
> You know it's Monday when you wake up and it's Tuesday. -- Garfield


But the problem is I always pass the same user(exist in the system) to
getpwnam, sometimes it works but sometimes it fails with NULL. Any idea?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-16-2008, 06:15 PM
Jim Hollenback
 
Posts: n/a
Default Re: getpwnam problem

ultra (ultraman@rogers_NOSPAM.com) wrote:
: >
: > getpwnam() returns NULL if:
: >
: > a) the user doesn't exits
: > b) an error occurred
: >
: > When an error occurred, errno is set appropriately. When the user
: > doesn't exist, errno is NOT set. Thus you want to use some code as
: follows:
: >
: > struct passwd *pw;
: >
: > /* we reset errno so we can tell whether an error happens in getpwnam()
: > * or not */
: > errno = 0;
: > pw = getpwnam("some_user");
: > if (!pw) {
: > if (errno) fprintf(stderr, "error: %s\n", strerror(errno));
: > else fprintf(stderr, "no such user\n");
: > }
: >

: But the problem is I always pass the same user(exist in the system) to
: getpwnam, sometimes it works but sometimes it fails with NULL. Any idea?

what is the errno when it fails?

--
Jim Hollenback
jholly@cup.hp.com
my opinion.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-16-2008, 06:15 PM
Dragan Cvetkovic
 
Posts: n/a
Default Re: getpwnam problem

jholly@cup.hp.com (Jim Hollenback) writes:

> ultra (ultraman@rogers_NOSPAM.com) wrote:
> : But the problem is I always pass the same user(exist in the system) to
> : getpwnam, sometimes it works but sometimes it fails with NULL. Any idea?
>
> what is the errno when it fails?


Apparently EINVAL. Here is the original message:

> I have a program that call getpwnam to get the user info, but sometimes
> the call returned NULL with errno EINVAL. I always pass the same user to the
> call. Anyone know what is the reason? I'm running HP11.00 and NIS.
>


--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-16-2008, 06:15 PM
ultra
 
Posts: n/a
Default Re: getpwnam problem

> what is the errno when it fails?
>
> --
> Jim Hollenback
> jholly@cup.hp.com
> my opinion.


it didn't set the errno when it failed...


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-16-2008, 06:15 PM
Dragan Cvetkovic
 
Posts: n/a
Default Re: getpwnam problem

"ultra" <ultraman@rogers_NOSPAM.com> writes:

>> what is the errno when it fails?

>
> it didn't set the errno when it failed...
>
>


Huh? What do you mean it didn't set the errno? Here is your original
message:

> I have a program that call getpwnam to get the user info, but sometimes
> the call returned NULL with errno EINVAL.





--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-16-2008, 06:15 PM
ultra
 
Posts: n/a
Default Re: getpwnam problem

ya.. I didn't errno=0 before the call at the begining. Once I set errno=0
before the call, it is still errno=0 after the call fail...

"Dragan Cvetkovic" <me@privacy.net> wrote in message
news:lmu0w1qjtq.fsf@privacy.net...
> "ultra" <ultraman@rogers_NOSPAM.com> writes:
>
> >> what is the errno when it fails?

> >
> > it didn't set the errno when it failed...
> >
> >

>
> Huh? What do you mean it didn't set the errno? Here is your original
> message:
>
> > I have a program that call getpwnam to get the user info, but sometimes
> > the call returned NULL with errno EINVAL.

>
>
>
>
> --
> Dragan Cvetkovic,
>
> To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer
>
> !!! Sender/From address is bogus. Use reply-to one !!!



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-16-2008, 06:15 PM
Chuck Dillon
 
Posts: n/a
Default Re: getpwnam problem

ultra wrote:
> Hi there,
> I have a program that call getpwnam to get the user info, but sometimes
> the call returned NULL with errno EINVAL. I always pass the same user to the
> call. Anyone know what is the reason? I'm running HP11.00 and NIS.
>
>


Is the program threaded? Try getpwnam_r?

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-16-2008, 06:15 PM
Aaron Isotton
 
Posts: n/a
Default Re: getpwnam problem

ultra wrote:
> ya.. I didn't errno=0 before the call at the begining. Once I set errno=0
> before the call, it is still errno=0 after the call fail...


In this case, the system thinks that the user doesn't exist. Maybe
there's some problem with your NIS configuration.

Greetings,
Aaron
--
Aaron Isotton | http://www.isotton.com/
You know it's Monday when you wake up and it's Tuesday. -- Garfield
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:09 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com