gpt4 book ai didi

c - 在 C mmap 中写入文件 : Permission denied. Linux

转载 作者:太空狗 更新时间:2023-10-29 12:01:40 25 4
gpt4 key购买 nike

这是我的代码:

   #include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main()
{
int fd=open("/home/victor/hello",O_WRONLY);

if(fd<0)
{
perror("Open");
exit(EXIT_FAILURE);
}

struct stat sbuf;

if(fstat(fd, &sbuf)==-1){
perror("stat");
close(fd);
exit(EXIT_FAILURE);
}

void* file_memory= mmap(NULL, sbuf.st_size, PROT_WRITE, MAP_SHARED,fd,0);
if (file_memory == MAP_FAILED ) {
perror("Error mmapping the file");
close(fd);
exit(EXIT_FAILURE);
}

return 0;
}

我也试过

    int fd=open("/home/victor/hello",O_WRONLY|0777);

但还是一样的错误:

映射文件时出错:权限被拒绝

执行 ls -l | grep hola -rwxrwxrwx 1 victor victor 10 月 24 日 01:47 你好

怎么了?

最佳答案

来自 glibc 手册,正如上面的 R..Iwillnotexist Idonotexist 所指出的:

Note that most hardware designs cannot support write permission without read permission, and many do not distinguish read and execute permission. Thus, you may receive wider permissions than you ask for, and mappings of write-only files may be denied even if you do not use PROT_READ.

http://www.gnu.org/software/libc/manual/html_node/Memory_002dmapped-I_002fO.html

关于c - 在 C mmap 中写入文件 : Permission denied. Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33314745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com