我在谷歌上搜索了“匿名inode”,似乎与epoll有关...但它到底是什么?
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.
O_EXCL
并将模式设置为 0600
而不是 0666
... 因为现在至少存在两个主要的潜在漏洞(符号链接漏洞和竞争条件窗口,如果您的 umask
不够严格,另一个用户可能会打开文件)。 - R.. GitHub STOP HELPING ICEtimerfd
的东西也使用匿名 inode。 - Viktor DahlO_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;
}
编译并运行:
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_operations
和inode
的fd
,当该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