Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-16-2010, 05:41 PM   #1
LQ Newbie
Registered: Feb 2010
Posts: 2

Rep: Reputation: 0
problems recovering from getpwnam error related to "too many open files"

I was trying to see if our code could recover from failures on getpwnam calls, if we did retries after failures. So the easiest error to simulate was to open too many files before the getpwnam call: I opened almost as many files as are allowed - such that the next open of the password file on a subsequent getpwnam call will fail. And indeed I was able to get the expected error on getpwnam.

The problem is that I was unable to recover from this error, even after closing all those open files and trying getpwnam again. It continued to return a password structure of 0 and didn't reset errno. It *would* work fine, however, if I had done a "setpwent/endpwent" duo BEFORE ever opening the many files. (Which isn't a satisfactory solution since we don't really want to issue this set of calls in every program that might get errors on getpwnam.)

The program is able to recover successfully on a Solaris system, although that may just be coincidental and due to other environmental factors I'm not aware of. My testing was mostly done on RedHat systems like this, altho I tried a few more recent versions as well:

$ uname -a
Linux <hostname> 2.6.9-11.EL #1 Fri May 20 18:15:25 EDT 2005 x86_64 x86_64 x86_64 GNU/Linux

I'd be happy to include the entire program except that there is additional setup needed for opening the files anyway (you have to figure out how many files you are allowed to open on your system), so I'll just show the main program I used, without the subroutines to open and close the many files:

#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
int main ()
const char name[] = "foo";
struct passwd *uinfo;

// Uncomment the next two lines to make the program work properly
// setpwent();
// endpwent();
printf ("going to try getpwnam with name value:%s\n", name);
errno = 0;
// The following rewind should not be necessary and in fact doesn't
// help if you uncomment it
// setpwent();
uinfo = getpwnam(name);
if (uinfo) {
printf ("Unexpected success, dir is %s\n", uinfo->pw_dir);
} else {
printf ("Expected failure, errno is %d\n", errno);
// close all the files and try again
printf ("going for a retry\n");
// The following setpwent seems like a good idea, but doesn't help.
// Addition of endpwent() doesn't help either.
errno = 0;
uinfo = getpwnam(name);
if (uinfo) {
printf ("Expected success, dir is %s\n", uinfo->pw_dir);
return 1;
} else {
printf ("Unexpected failure, errno is %d\n", errno);
return 0;

Output on RH Linux (unexpected):

$ ./gpw
many files opened
going to try getpwnam with name value:foo
Expected failure, errno is 24
all files closed
going for a retry
Unexpected failure, errno is 0

Output on Solaris, and on RH Linux once you uncomment the initial
'setpwent and endpwent' calls (expected output):

$ ./gpw
many files opened
going to try getpwnam with name value:foo
failure, errno is 24
all files closed
going for a retry
Expected success, dir is /home/foo


(Naturally, "foo" can be any valid username on the system.)

Thank you for any insight into this behavior!
Old 02-18-2010, 06:00 PM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
moved to programming
Old 02-18-2010, 09:03 PM   #3
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
How do you open and close the files (fopen, open, dup, etc.)? Have you tried opening a file after you call close_many_files()?
Kevin Barry
Old 02-19-2010, 08:27 AM   #4
LQ Newbie
Registered: Feb 2010
Posts: 2

Original Poster
Rep: Reputation: 0
I used "fopen" to open the files ("fclose" to close them), and yes, I am able to open at least one file again after closing all the files. I still get the subsequent error from getpwnam in that case as well.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
script using "/usr/bin/cat error" produces "cannot open" in cron Dcrusoe Programming 6 07-22-2009 04:30 PM
"GConf error" when open files under root account helai Debian 1 03-15-2009 04:54 PM
F8 freezes during boot - "getpwnam failed for roota" Magmagal Fedora 6 05-18-2008 09:23 PM
xmame problems "ERROR: required files are missing" GT_Onizuka Linux - Games 14 09-19-2007 03:10 AM
"X-MS" cant open because "x-Multimedia System" cant access files at "smb&qu ponchy5 Linux - Networking 0 03-30-2004 12:18 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:59 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration