LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-16-2010, 04:41 PM   #1
S_interactive
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();
open_many_files();
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
close_many_files();
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.
setpwent();
endpwent();
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, 05:00 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
moved to programming
 
Old 02-18-2010, 08:03 PM   #3
ta0kira
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, 07:27 AM   #4
S_interactive
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.
 
  


Reply



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 03:30 PM
"GConf error" when open files under root account helai Debian 1 03-15-2009 03:54 PM
F8 freezes during boot - "getpwnam failed for roota" Magmagal Fedora 6 05-18-2008 08:23 PM
xmame problems "ERROR: required files are missing" GT_Onizuka Linux - Games 14 09-19-2007 02:10 AM
"X-MS" cant open because "x-Multimedia System" cant access files at "smb&qu ponchy5 Linux - Networking 0 03-29-2004 11:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 AM.

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