Lseek / write suddenly returns -1 with errno = 9 (Bad file descriptor)

My application uses lseek()to search for the desired position for recording data. File successfully opened by open(), and my application could be used lseek()and write()a lot of times.

At some point in time, for some users it is not easy to play, it lseek()returns -1 s errnoout of 9. The file does not close before this, and the file descriptor (int) is not reset.

After that another file is created; open()again in order and lseek()and write()work again.

To make this even worse, this user again tried the full sequence, and everything was fine.

So my question is: can the OS close the file for me for some reason? What could be the reason for this? Is there a file indexer or file scanner?

What is the best way to solve this problem? is this pseudo code the best solution? (not paying attention to the code layout, it will create functions for it)

int fd=open(...);
if (fd>-1) {
  long result = lseek(fd,....);
  if (result == -1 && errno==9) {
      close(fd..); //make sure we try to close nicely
      fd=open(...);

      result = lseek(fd,....);
  }
}

Does anyone experience something similar?

Summary: file searching and writing work fine for a given fd and unexpectedly returns errno = 9 for no reason.

+5
source share
4 answers

I don’t know what type of tuning you have, but in the following scenario I can create such an effect (or another similar to it). I did not check this to check, so please take it with salt.

/, , (, NFS), , , //. , , . , EBADF.

, .

+1

, : - ? ? -?

, .

? - ? ( , )

, - .

- ?

, fds , EBADF , , :

  • - - 'int fd;' .
  • , , - - if(fd = foo[i].fd), if(fd == foo[i].fd)
  • Raceconditions , , - .

, "strace", , .

+7

( Unix- ). , , - , ( C Unix API , , , ).

- , , , .

(.. printf() ), . , Valgrind.

( , , , CPU, , . , , , Mips).

+2

, , ( ..) .

, . , , , .

  • Check your assumptions. Is set errnoto 0 before calling? Is fd really at the time of the call? (I know that you said it was, but you noted )?
  • What is the result of working puts( strerror( 9 ) );on your platform?
0
source

All Articles