c - Daemon process unable to find file (specified via relative path) -
I have a daemon process that spans multiple threads, one of which handles HTTP requests handler
Intended to return a file located in resources / html / index.html
I have the following code:
zero * Read_file_ex (char * file_name, int32_t * data_len) {FILE * fp; Fp = fopen (file_name, "r"); ... fclose more code to fetch file contents; } Zero * read_file (char * file_name) {read_file_ex return (file_name, NULL); }
In another thread, I call:
read_file ("resources / html / index.html");
When a request is made for that file, the code crashes with a "segmentation fault" error.
When I use GDB to break FOPE, I think NULL is returned and set to errno
2 (file not found).
In addition, when I change the code to use the full path of the file:
/usr/sbin/app/resources/html/index.html
Then the `fopen () 'index is able to find the file and everything works fine.
One more thing is that this error occurs when Debian runs on Linux but Ubuntu is not on 12.04, which is also facing my question dumb.
I forgot to add that I included this program in the same folder which contains the 'Resources' folder.
If the current directory of the process is not / usr / sbin / app
( And it is unlikely that the current directory will be / usr / bin / app
, then the name of the relative path will not be named. You should check the return result from fopen ()
before trying to use it Always . There are countless reasons that open operations may fail, even if you are in the right directory, when alone, there are times when you are not alone.
Note that if your process functions, or run through a program, the current directory can be changed to /
, even if you expect to be somewhere else. Are doing so.
If you need to check the existing directory of the process (A process is the same existing directory for all existing threads), you can use it to get the current working directory.
If you need to change the directory (again) after dumping your process, then you can use it to do this. It can also be used to switch back to a directory if you have an open file descriptor for the directory.
Comments
Post a Comment