Operating System - Linux
1748242 Members
4052 Online
108760 Solutions
New Discussion юеВ

Re: Intermittent problem with BAD FILE NUMBER errno 9

 
David DiBiase
Frequent Advisor

Intermittent problem with BAD FILE NUMBER errno 9

What type of conditions can cause an errno 9, "BAD FILE NUMBER" to occur when you try to write (that's a man 2 write) to a file that you created with the open call with a status of O_APPEND and a mode of 0666.
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: Intermittent problem with BAD FILE NUMBER errno 9

Hi David:

Well. errno=9 is EBADF and that will be returned for an invalid file descriptor; viz. The 'fildes' argument to the write() is not a valid file descriptor open for writing.

...JRF...
Marvin Strong
Honored Contributor

Re: Intermittent problem with BAD FILE NUMBER errno 9

9 EBADF Bad file number
Either a file descriptor refers to no open file, or a read (resp. write)
request is made to a file that is open only for writing (resp. reading).

If the file is open is it growing beyond your ulimit? Just a guess.
Stephen Keane
Honored Contributor

Re: Intermittent problem with BAD FILE NUMBER errno 9

You did check that the open() call returned a valid file descriptor before using it in the write() call, didn't you?
David DiBiase
Frequent Advisor

Re: Intermittent problem with BAD FILE NUMBER errno 9

All your responses have given me lots of ideas - thanks. Below is the function i am using. In test - works fine. My problem is intermittent failures in production:

Here's the partial
code: Please note that error_file is declared an extern in the main program and is set to a 1.
===============================================================

#include
#include
#include
#include
#include
#include

extern int debug;
char ErrorFile[70];
extern char *pgm_name;
int error_file;

void
error_proc(const char *in_mess)
{
int mess_sz;
int err_fd;
static int the_top = 1;

if (debug)
(void)fprintf(stderr, "%s: in_mess = %s\n", pgm_name, in_mess);
if (error_file == 1)
{
mess_sz = strlen(in_mess);
if (the_top)
{
if ((err_fd = open(ErrorFile, O_WRONLY | O_CREAT, 0666)) < 0)
{
(void)fprintf(stderr, "%s: Error in opening the Error File %s errno = %d\n", pgm_name, ErrorFile, errno);
return;
}
the_top = 0;
}
else
{
if ((err_fd = open(ErrorFile, O_APPEND, 0666)) < 0)
{
(void)fprintf(stderr, "%s: Error in opening the Error File %s errno = %d\n", pgm_name, ErrorFile, errno);
return;
}
}
if (write(err_fd, in_mess, mess_sz) != mess_sz)
{
(void)fprintf(stderr, "%s: Error in writing to the Error File %s errno = %d\n", pgm_name, ErrorFile, errno);
(void)close(err_fd);
return;
}
(void)close(err_fd);
}
else
(void)fprintf(stderr, "%s: %s\n", pgm_name, in_mess);
return;
}
susan gregory_1
Valued Contributor

Re: Intermittent problem with BAD FILE NUMBER errno 9

Are you sure that the write is failing? You seem to be checking for a return write value not equal to the size of the max buffer size:

if (write(err_fd, in_mess, mess_sz) != mess_sz)

and could it be returning a value greater than -1 but less than mess_sz, like mess_sz -1?
Can you change the test to be != -1 and then print errno? I think that the value of errno is undefined unless the write returns a -1 to signify failure. If the write is successful, you shouldn't be checking errno.....
Torsten.
Acclaimed Contributor

Re: Intermittent problem with BAD FILE NUMBER errno 9

Hi David,

for my own private programming I use perror instead of reading errno.

e.g.:

if ((sf=open(argv[1],O_RDONLY)) == -1)
{
perror("open ");
return EXIT_FAILURE;
}

prints the systems error message.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Stephen Keane
Honored Contributor

Re: Intermittent problem with BAD FILE NUMBER errno 9

You should ideally check for three conditions when doing a write.

1) All the characters you asked it to write were written in one go. write() will return the number of characters you asked it to write.

2) Some (but not all) of the characters you asked it to write were written. i.e. a partial write. this can happen if the write() was interrupted by say a signal. It isn't an error, but you haven't written all the data, you must write the remaining data in subsequent write() operation(s). write() will return the actial number of characters written, which will be less than what you asked it to write.

3) An error occurred. write() will return -1 and set errno to indicate the error.

Torsten.
Acclaimed Contributor

Re: Intermittent problem with BAD FILE NUMBER errno 9

Hi David,

according to the posting above you may have a look at this "well known" procedure.
Maybe this is helpful in this case
________________________________

int writen (int fd,char * ptr, int nbytes)
{
int nleft,nwritten;

nleft=nbytes;
while (nleft > 0)
{ nwritten=write(fd,ptr,nleft);
if (nwritten <= 0)
return (nwritten); /* error or EOF */
nleft-=nwritten;
ptr+=nwritten;
}
return (nbytes-nleft);
}

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!