mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
anonymous inode: move doc to readme
This commit is contained in:
@@ -19,7 +19,6 @@
|
||||
... link:dep.c[]
|
||||
... link:dep2.c[]
|
||||
. Pseudo filesystems
|
||||
.. link:anonymous_inode.c[]
|
||||
.. link:ioctl.c[]
|
||||
.. link:mmap.c[]
|
||||
.. link:poll.c[]
|
||||
|
||||
@@ -1,20 +1,9 @@
|
||||
/*
|
||||
https://stackoverflow.com/questions/4508998/what-is-anonymous-inode
|
||||
|
||||
anon_inode_getfd example:
|
||||
|
||||
- get an anonymous inode via ioctl from a debugfs entry
|
||||
- read jiffies from that inode
|
||||
|
||||
This method allows getting multiple file descriptors from a single filesystem,
|
||||
which reduces namespace pollution.
|
||||
*/
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#anonymous-inode */
|
||||
|
||||
#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 */
|
||||
@@ -23,16 +12,18 @@ which reduces namespace pollution.
|
||||
#include "anonymous_inode.h"
|
||||
|
||||
static struct dentry *debugfs_file;
|
||||
static u32 myval = 1;
|
||||
|
||||
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
|
||||
{
|
||||
char kbuf[1024];
|
||||
char kbuf[8];
|
||||
size_t ret;
|
||||
|
||||
ret = snprintf(kbuf, sizeof(kbuf), "%llu", (unsigned long long)jiffies);
|
||||
ret = snprintf(kbuf, sizeof(kbuf), "%x", myval);
|
||||
if (copy_to_user(buf, kbuf, ret)) {
|
||||
ret = -EFAULT;
|
||||
}
|
||||
myval <<= 4;
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -47,7 +38,7 @@ static long unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long ar
|
||||
switch (cmd) {
|
||||
case LKMC_ANONYMOUS_INODE_GET_FD:
|
||||
fd = anon_inode_getfd(
|
||||
"random",
|
||||
"todo_what_is_this_for",
|
||||
&fops_anon,
|
||||
NULL,
|
||||
O_RDONLY | O_CLOEXEC
|
||||
|
||||
@@ -14,10 +14,15 @@ int main(int argc, char **argv)
|
||||
{
|
||||
char buf[1024];
|
||||
int fd_ioctl, fd_ioctl_anon, ret;
|
||||
size_t i, nreads;
|
||||
|
||||
if (argc < 2) {
|
||||
puts("Usage: ./prog <ioctl-file>");
|
||||
puts("Usage: ./prog <ioctl-file> [<nreads>]");
|
||||
return EXIT_FAILURE;
|
||||
} else if (argc > 2) {
|
||||
nreads = strtol(argv[2], NULL, 10);
|
||||
} else {
|
||||
nreads = 3;
|
||||
}
|
||||
fd_ioctl = open(argv[1], O_RDONLY);
|
||||
if (fd_ioctl == -1) {
|
||||
@@ -29,11 +34,10 @@ int main(int argc, char **argv)
|
||||
perror("ioctl");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
ret = read(fd_ioctl_anon, buf, sizeof(buf));
|
||||
printf("%.*s\n", ret, buf);
|
||||
sleep(1);
|
||||
ret = read(fd_ioctl_anon, buf, sizeof(buf));
|
||||
printf("%.*s\n", ret, buf);
|
||||
for (i = 0; i < nreads; ++i) {
|
||||
ret = read(fd_ioctl_anon, buf, sizeof(buf));
|
||||
printf("%.*s\n", ret, buf);
|
||||
}
|
||||
close(fd_ioctl_anon);
|
||||
close(fd_ioctl);
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
Reference in New Issue
Block a user