Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fopen does not return

Tags:

c

linux

fopen

I used 'fopen' in a C program to open a file in readonly mode (r). But in my case I observed that fopen call does not return. It does not return NULL or valid pointer - execution gets blocked at fopen call. The patch of file is absolutely correct (I have already verified that) and there is no permission related issues. Can anybody please tell what could be the reason for this kind if behavior. Any kind of help is really appreciable. Is there anything related to gcc or glibc?

EDIT

Here is the sample code

printf("%s %d\n",__FUNCTION__,__LINE__);
if ((fp = fopen(argv[1], "r")) == NULL) {
   printf("%s %d\n",__FUNCTION__,__LINE__);
   return;
}
printf("%s %d\n",__FUNCTION__,__LINE__);

When I run this code, I only get the first print (before calling fopen) and after that program just halts. So fopen does not complete it's operation. The file is a simple configuration file with '.conf' extension and this file can be opened by all other means like vi, cat etc. There should not be any NFS related issue. Filesystem is ext3.

Thanks in advance, Souvik

like image 296
Souvik Avatar asked Dec 20 '10 14:12

Souvik


People also ask

What happens when you open a file with fopen?

If opened successfully, fopen () loads it into memory and sets up a pointer which points to the first character in it. Returns NULL, if unable to open the file. “w+” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file.

What is the difference between “R” and fopen ()?

“r” – Searches file. Opens the file for reading only. If the file is opened successfully fopen () loads it into memory and sets up a pointer which points to the first character in it. If the file cannot be opened fopen () returns NULL.

How does the fopen () function work?

The fopen () function opens the file whose name is the string pointed to by pathname and associates a stream with it. The argument mode points to a string beginning with one of the following sequences (possibly followed by additional characters, as described below): r Open text file for reading.

Is fopen() required to set errno when returning null?

If a function sets errno on error, it will do so - doesn't matter what the previous value was. But fopen () is not required to set errno every time it returns NULL. Ah, thanks! I think I see what I was missing. to set errno to any value it wants (except zero). as before the call. Please correct me if that's not right. That's *almost* right.


1 Answers

Here's a few reasons:

  • You've corrupted memory somewhere, and all bets are off as to what's happening (run your program through valgrind)
  • You're calling this code inside a signal handler, fopen() is not signal async safe, so really anything could happen (a deadlock due to the FILE* internal mutex is common though)
  • The file is a fifo , in which cases opening the file will block until someone opens the file at the other end(read/writing)
  • The file is on a stale NFS mount.
  • The file is a character/block special file with semantics that open blocks until something interesting happens,
like image 175
nos Avatar answered Sep 24 '22 18:09

nos