linux - 为什么 proc_create 模式参数 0 对应于 0444

标签 linux permissions kernel procfs

我是内核编程的新手,正在尝试使用 procfs。我用谷歌搜索了几个例子,他们都使用:

 proc_create("hello",0,NULL,&proc_fops);

第二个参数是文件模式/权限。根据 proc_fs.h:

static inline struct proc_dir_entry *proc_create(
         const char *name, umode_t mode, struct proc_dir_entry *parent,
         const struct file_operations *proc_fops)

模式是 umode_t 类型,据我所知它被解析为 unsigned short int。

在至少 3 个示例中,附带的注释和措辞基本上表示同一件事,即“您在那里看到的值零表示您希望对 proc 文件的权限,其中零表示文件权限的默认值 0444。换句话说,您可以只使用 tt 他数值 0444(对于八进制),或 0400 用于更严格的访问,等等。但是对于典型的可读 proc 文件使用零是相当正常的。”

我无法理解这意味着什么。我了解文件权限、chmod u+x、八进制数,而且我想我了解 umask。

我只是不清楚 0 如何映射到 0444,以及如果我将 1 放在那里而不是 0 会发生什么。

最佳答案

答案似乎只有一点点。 include/linux/proc_fs.h 说:

static inline struct proc_dir_entry *proc_create(
        const char *name, umode_t mode, struct proc_dir_entry *parent,
        const struct file_operations *proc_fops)
{
           return proc_create_data(name, mode, parent, proc_fops, NULL);
}

proc_create_datafs/proc/generic.c 中定义。它就在那里:

struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
                    struct proc_dir_entry *parent,
                    const struct file_operations *proc_fops,
                    void *data)
{
    ...

    if ((mode & S_IALLUGO) == 0)
        mode |= S_IRUGO;

    ...
}

如果模式阻止所有人做任何事情,请将模式设置为 00444。

为了完整性,来自include/linux/stat.h:

#define S_IRWXUGO   (S_IRWXU|S_IRWXG|S_IRWXO)
#define S_IALLUGO   (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
#define S_IRUGO     (S_IRUSR|S_IRGRP|S_IROTH)

来自include/uapi/linux/stat.h:

#define S_ISUID  0004000
#define S_ISGID  0002000
#define S_ISVTX  0001000

#define S_IRWXU 00700
#define S_IRUSR 00400

#define S_IRWXG 00070
#define S_IRGRP 00040

#define S_IRWXO 00007
#define S_IROTH 00004

关于linux - 为什么 proc_create 模式参数 0 对应于 0444,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28664971/

相关文章:

android - 打开/关闭 WiFi

linux - "Linux aio"和 "Linux native aio"是一回事吗?

linux - 如何在 Linux 中跟踪系统调用?

linux - r-base 无法找到包

python - 从多列中过滤和替换

Android:通过 AOSP 源代码创建新的系统权限。

php - 登录系统php的管理员权限

python - Unix(Linux、FreeBSD)上如何获取系统库路径

regex - 如何使用正则表达式在 Unix 中搜索文件?

c - 如何在内核中获取字符设备的节点名或路径