我正在尝试在
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
.