为什么我能够创建模式为 000 的文件并使用开放系统调用在 C 中写入它?

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

根据我的理解,000 权限意味着除了 root 用户之外,没有人能够读取或写入该文件。为什么下面这段代码有效?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

int main(void)
{
    char *message = "Hello world\n";

    int fd = open("greeting.txt", O_CREAT | O_WRONLY, 0000);
    write(fd, message, strlen(message));
    close(fd);
}

此外,当我使用

sem_open(sem_name, O_CREAT, 0000, 1);
创建命名信号量时,即使文件权限设置为 000,父进程和所有子进程都能够修改它。

我对低级 C 系统调用和信号量的理解是有限的,因此需要一个全面的解释。

c linux system-calls semaphore file-permissions
1个回答
0
投票

来自 man open https://man7.org/linux/man-pages/man2/open.2.html :

          Note that mode applies only to future accesses of the
          newly created file; the open() call that creates a read-
          only file may well return a read/write file descriptor.
© www.soinside.com 2019 - 2024. All rights reserved.