在Linux中,什么是匿名inode?

25

我在谷歌上搜索了“匿名inode”,似乎与epoll有关...但它到底是什么?


1
请参见已接受的答案:https://dev59.com/UHM_5IYBdhLWcg3wZSE6 - Anders
2个回答

25
在某些情况下,匿名inode是没有附加目录条目的inode。创建这样的inode最简单的方法如下:
int fd = open( "/tmp/file", O_CREAT | O_RDWR, 0666 );
unlink( "/tmp/file" );
// Note that the descriptor fd now points to an inode that has no filesystem entry; you
// can still write to it, fstat() it, etc. but you can't find it in the filesystem.

简而言之,匿名inode是一个不代表文件而是内存块的inode? - mrkschan
11
不,这是磁盘上的数据,但是这些数据已经没有文件系统中的条目可以打开了--只有在最后一个文件描述符关闭之前才能使用这些数据。当你需要一个临时文件,却不希望任何人都能够打开它时,就会出现这种情况;在UNIX系统中,许多mktemp()调用的应用程序实际上是这样,即你不希望其他进程能够打开该文件,而只需要一个临时文件来完成某些任务。 - user335938
1
对于这个例子,您几乎肯定想要添加 O_EXCL 并将模式设置为 0600 而不是 0666... 因为现在至少存在两个主要的潜在漏洞(符号链接漏洞和竞争条件窗口,如果您的 umask 不够严格,另一个用户可能会打开文件)。 - R.. GitHub STOP HELPING ICE
2
有没有办法重新链接目录条目? - nroose
2
不行,这样的文件无法重新链接。 - djmitche
1
类似 timerfd 的东西也使用匿名 inode。 - Viktor Dahl

11

O_TMPFILE打开

这是匿名inode的一个很好的定义:在给定目录中创建一个没有名称的inode,它在使用ls命令时不会出现。

然后,当您关闭文件描述符时,文件将被删除。

这是在Linux 3.11中添加的。

main.c

#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(void) {
    char buf[]  = { 'a', 'b', 'c', 'd' };
    char buf2[]  = { 'e', 'f', 'g', 'h' };
    int f, ret;
    size_t off;

    /* write */
    f = open(".", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
    ret = write(f, buf, sizeof(buf));

    /* Interactivelly check if anything changed on directory. It hasn't. */
    /*puts("hit enter to continue");*/
    /*getchar();*/

    /* read */
    lseek(f, 0, SEEK_SET);
    off = 0;
    while ((ret = read(f, buf2 + off, sizeof(buf) - off))) {
        off += ret;
    }
    close(f);
    assert(!memcmp(buf, buf2, sizeof(buf)));

    return EXIT_SUCCESS;
}

GitHub上游

编译并运行:

gcc -o main.out -std=c99 -Wall -Wextra -pedantic  main.c
./main.out

因此,这基本上是实现临时文件的最佳方法:如何在C ++中创建临时文本文件?,因为它可以:

  • 总是立即找到一个可用的名称,而不需要我们循环遍历文件名(不需要名称!)
  • 并且具有自动删除功能

将其与手动更多的目录方法进行比较,例如此处所示的方法

在Ubuntu 17.04,Linux 4.10,glibc 2.24中进行过测试。

如何在/proc/PID/fd中查看 O_TMPFILE

我们可以使用以下命令来快速检查:

#define _GNU_SOURCE
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(void) {
    int f = open(".", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
    struct stat s;
    fstat(f, &s);
    printf("pid %jd\n", (intmax_t)getpid());
    printf("inode %jd\n", (intmax_t)s.st_ino);
    getchar();
}

然后给出一个示例输出:

pid 15952     
inode 44698689

我们做:

ls -l /proc/15952/fd

并且包含:

3 -> '/home/ciro/test/#44698689 (deleted)'

这段内容告诉我们如何查找给定进程的临时文件并查看它们的inode和inode父目录。

anon_inode_getfd是Linux内核函数

如果你在处理内核模块,这可能是它的定义。

你可以这样调用它:

fd = anon_inode_getfd("random", &fops_anon, NULL, O_RDONLY | O_CLOEXEC);
并将fd返回给用户,例如通过ioctl

现在,用户拥有一个关联了任意file_operationsinodefd,当该fd关闭时,所有内容都会被释放。

如果您想要多个read系统调用,但不想创建多个设备文件,从而进一步污染/dev,此方法非常有用:您只需创建额外的ioctl即可。

使用QEMU Buildroot的最小可运行示例:

#include <asm/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/anon_inodes.h>
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h>
#include <linux/jiffies.h>
#include <linux/kernel.h> /* min */
#include <linux/module.h>
#include <linux/printk.h> /* printk */

#include "anonymous_inode.h"

MODULE_LICENSE("GPL");

static struct dentry *dir;

static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
    char kbuf[1024];
    size_t ret;

    ret = snprintf(kbuf, sizeof(kbuf), "%llu", (unsigned long long)jiffies);
    if (copy_to_user(buf, kbuf, ret)) {
        ret = -EFAULT;
    }
    return ret;
}

static const struct file_operations fops_anon = {
    .read = read,
};

static long unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long argp)
{
    int fd;

    switch (cmd) {
        case LKMC_ANONYMOUS_INODE_GET_FD:
            fd = anon_inode_getfd(
                "random",
                &fops_anon,
                NULL,
                O_RDONLY | O_CLOEXEC
            );
            if (copy_to_user((void __user *)argp, &fd, sizeof(fd))) {
                return -EFAULT;
            }
        break;
        default:
            return -EINVAL;
        break;
    }
    return 0;
}

static const struct file_operations fops_ioctl = {
    .owner = THIS_MODULE,
    .unlocked_ioctl = unlocked_ioctl
};

static int myinit(void)
{
    dir = debugfs_create_dir("lkmc_anonymous_inode", 0);
    debugfs_create_file("f", 0, dir, NULL, &fops_ioctl);
    return 0;
}

static void myexit(void)
{
    debugfs_remove_recursive(dir);
}

module_init(myinit)
module_exit(myexit)

/proc/PID/fd 中的 anon_inode

它们是管道和套接字,请参阅:https://unix.stackexchange.com/questions/463548/what-is-anon-inode-in-the-output-of-ls-l


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接