LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   problems recovering from getpwnam error related to "too many open files" (http://www.linuxquestions.org/questions/programming-9/problems-recovering-from-getpwnam-error-related-to-too-many-open-files-789557/)

S_interactive 02-16-2010 04:41 PM

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!

pixellany 02-18-2010 05:00 PM

moved to programming

ta0kira 02-18-2010 08:03 PM

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

S_interactive 02-19-2010 07:27 AM

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.


All times are GMT -5. The time now is 06:49 PM.