nginx为什么子进程打不开父进程创建的文件

问题描述 投票:0回答:0

我正在尝试在

nginx
中编写一个插件,我应该在
main process
中创建一个文件然后
child process
将读取这个文件。我已经在
0766
中设置了文件
main process
,但是当我尝试在
open
中打开文件时,
statvfs
child process
失败了,
errno
是13,这意味着
Permission denied
.

parent
child
进程之间有什么特殊设置吗?我该如何解决?

我的代码是这样的:

// in nginx main process I create the file
u_char* shared_addr = NULL; 
int map_my_file(const char* path)
{
    extern int errno;
    umask(0);
    int fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0766);
    if (fd < 0)
    {
        ngx_log_stderr(0, "open file failed %s, %d", path, errno);
    }
    if (ftruncate(fd, 1024 *1024) < 0)
    {
        printf("ftruncate %d failed", fd);
    }
    shared_addr = (u_char*)mmap(NULL, 1024 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    close(fd);
}

// in nginx child process, I try to open the file 
    int cfd = 0;
    if ((cfd = open("./t.log", O_RDWR)) >= 0)
    {
        printf("child open success\n");
    } else {
        printf("failed %d\n", errno);
    }
// I got `failed 13`, means `Permission denied`

如果我把这些

create
open
代码单独拿出来放在一个示例
parent-child
流程案例中,它可以正常工作。但它不能在
nginx
工作,总是告诉我
13
.

linux nginx filesystems fork permission-denied
© www.soinside.com 2019 - 2024. All rights reserved.