9
votes

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

4
Post the code. We can't tell without that. Include the parameters (with values) you're passing to fopen. - The Archetypal Paul
Can you open that file using some other means like cat? One more possible reason is that the file is on NFS. - Sergei Tachenov
@Time Machine: Erm... NULL is an alias for a null pointer. No, the two are not different. - DevSolar
Replace printf() with fprintf(stderr,) followed by fflush(stderr), as there may be buffering issues. Just to make sure that it really hangs on that line. It is also worth printing argv[1] before fopen(). And lastly, did you try to run the whole thing in a debugger? - Sergei Tachenov
What does running your program with strace show? - R.. GitHub STOP HELPING ICE

4 Answers

7
votes

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,
2
votes

So what? fopen is allowed to block until the file has been opened, or until it has been determined that access is denied. If you have a slow storage device, it is absolutely correct to wait until that becomes available. But that is an operating system issue then, not C's.

1
votes

I notice you don't close the file if you open it successfully.

Is it possible you that you have run it before and killed it, and now you have a process out there which has the file open, and locked?

If so, then maybe fopen is waiting for the lock to be released.

-1
votes

Is it possible that you've redefined a symbol in the reserved namespace: either something beginning with two underscores, an underscore and a capital letter, or any of the standard C library functions? If so, that results in undefined behavior, and it's possible that fopen somehow ends up calling part of your code instead of the correct code in the standard library.

This question has a major "missing information" smell to it. I seriously doubt the code snippet in the question has the behavior OP has described when it appears by itself in main, and I wonder if OP hasn't done some bogus stuff he's not telling us about...