简体   繁体   English

守护进程无法找到文件(通过相对路径指定)

[英]Daemon process unable to find file (specified via relative path)

I have a daemon process that spawns several threads one of which handles HTTP requests. 我有一个守护进程,它产生几个线程,其中一个线程处理HTTP请求。 The handler is intended to return a file located in 处理程序旨在返回位于的文件

resources/html/index.html

I have the following code: 我有以下代码:

void * read_file_ex(char *file_name, int32_t *data_len) {
   FILE *fp;
   fp = fopen(file_name, "r");
   ... // more code to fetch file contents
   fclose(fp);
}

void * read_file(char *file_name){
   return read_file_ex(file_name, NULL);
}

And in the thread, I call: 在线程中,我打电话给:

read_file("resources/html/index.html");    

The code crashes with a "Segmentation Fault" error when a request is made for that file. 在为该文件发出请求时,代码崩溃并出现“Segmentation Fault”错误。

When I use GDB to break at fopen, I notice that a NULL is returned and errno is set to 2 (File not found). 当我使用GDB在fopen中断时,我注意到返回NULL并且errno设置为2(找不到文件)。

Also, when I change the code to use the absolute path of the file: 此外,当我更改代码以使用文件的绝对路径时:

/usr/sbin/app/resources/html/index.html

then `fopen()' is able to find the index file and everything works fine. 然后`fopen()'能够找到索引文件,一切正常。

Another thing to mention is that this error happens when run on Debian Linux but not on Ubuntu 12.04 which makes my question look even dumber. 另外需要提一下的是,在Debian Linux上运行时会出现此错误,而在Ubuntu 12.04上运行则不会发生这种错误,这使我的问题看起来更加笨拙。

I forgot to add that I am running the program from the same folder that contains the `resources' folder. 我忘了添加我从包含`resources'文件夹的同一文件夹运行程序。

If the current directory of the process is not /usr/sbin/app (and it seems a bit unlikely that the current directory would be /usr/bin/app ), then the relative pathname won't work. 如果进程的当前目录不是/usr/sbin/app (并且当前目录似乎不太可能是/usr/bin/app ),那么相对路径名将不起作用。 You should always check the return result from fopen() before attempting to use it. 在尝试使用fopen()之前,应始终检查fopen()的返回结果。 There are endless reasons why an open operation can fail even if you're in the correct directory, let alone when there's a chance that you aren't. 即使你在正确的目录中,开放式操作失败也有无穷无尽的原因,更不用说当你有可能没有时。

Note that if your process uses functions like daemon() , or is run via a daemonize program, the current directory can be changed to / even if you expected it to be somewhere else. 请注意,如果您的进程使用daemon()等函数,或者通过daemonize程序运行,则可以将当前目录更改为/即使您希望它位于其他位置。

If you need to check the current directory of the process (a process has a single current directory common to all threads), you can use the getcwd() to get the current working directory. 如果需要检查进程的当前目录(进程具有所有线程共用的单个当前目录),则可以使用getcwd()获取当前工作目录。

If you need to change directory (again) after daemonizing your process, you can use chdir() to do so. 如果您需要在守护进程后再次更改目录,则可以使用chdir()来执行此操作。 There's also fchdir() which can be used to change back to a directory if you have an open file descriptor for the directory. 还有fchdir() ,如果你有一个目录的打开文件描述符,它可以用来改回目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM