mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-27 20:14:27 +01:00
anonymous inode: move doc to readme
This commit is contained in:
33
README.adoc
33
README.adoc
@@ -3101,6 +3101,37 @@ Sources:
|
|||||||
|
|
||||||
Bibliography: https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867
|
Bibliography: https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867
|
||||||
|
|
||||||
|
==== Anonymous inode
|
||||||
|
|
||||||
|
In guest:
|
||||||
|
|
||||||
|
....
|
||||||
|
/anonymous_inode.sh
|
||||||
|
echo $?
|
||||||
|
....
|
||||||
|
|
||||||
|
Outcome: the test passes:
|
||||||
|
|
||||||
|
....
|
||||||
|
0
|
||||||
|
....
|
||||||
|
|
||||||
|
Sources:
|
||||||
|
|
||||||
|
* link:kernel_module/anonymous_inode.c[]
|
||||||
|
* link:kernel_module/anonymous_inode.h[]
|
||||||
|
* link:kernel_module/user/anonymous_inode.c[]
|
||||||
|
* link:rootfs_overlay/anonymous_inode.sh[]
|
||||||
|
|
||||||
|
This example:
|
||||||
|
|
||||||
|
* gets an anonymous inode via `ioctl` from a debugfs entry `anon_inode_getfd` from a debugfs file
|
||||||
|
* read jiffies from that inode
|
||||||
|
|
||||||
|
Anonymous inodes allow getting multiple file descriptors from a single filesystem entry, which reduces namespace pollution compared to creating multiple device files.
|
||||||
|
|
||||||
|
Bibliography: https://stackoverflow.com/questions/4508998/what-is-an-anonymous-inode-in-linux
|
||||||
|
|
||||||
=== Kernel panic and oops
|
=== Kernel panic and oops
|
||||||
|
|
||||||
To test out kernel panics and oops in controlled circumstances, try out the modules:
|
To test out kernel panics and oops in controlled circumstances, try out the modules:
|
||||||
@@ -7734,7 +7765,7 @@ We are slowly automating testable guest tests with:
|
|||||||
./run -F '/test_all.sh;/poweroff.out' | grep lkmc_test
|
./run -F '/test_all.sh;/poweroff.out' | grep lkmc_test
|
||||||
....
|
....
|
||||||
|
|
||||||
which outputs to stdout `lkmc_test_pass` or `lkmc_test_fail` to stdout, which we can grep from host to automate testing.
|
which outputs `lkmc_test_pass` or `lkmc_test_fail`.
|
||||||
|
|
||||||
Source: link:rootfs_overlay/test_all.sh[].
|
Source: link:rootfs_overlay/test_all.sh[].
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
... link:dep.c[]
|
... link:dep.c[]
|
||||||
... link:dep2.c[]
|
... link:dep2.c[]
|
||||||
. Pseudo filesystems
|
. Pseudo filesystems
|
||||||
.. link:anonymous_inode.c[]
|
|
||||||
.. link:ioctl.c[]
|
.. link:ioctl.c[]
|
||||||
.. link:mmap.c[]
|
.. link:mmap.c[]
|
||||||
.. link:poll.c[]
|
.. link:poll.c[]
|
||||||
|
|||||||
@@ -1,20 +1,9 @@
|
|||||||
/*
|
/* https://github.com/cirosantilli/linux-kernel-module-cheat#anonymous-inode */
|
||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <linux/anon_inodes.h>
|
#include <linux/anon_inodes.h>
|
||||||
#include <linux/debugfs.h>
|
#include <linux/debugfs.h>
|
||||||
#include <linux/errno.h> /* EFAULT */
|
#include <linux/errno.h> /* EFAULT */
|
||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
#include <linux/jiffies.h>
|
|
||||||
#include <linux/kernel.h> /* min */
|
#include <linux/kernel.h> /* min */
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
#include <linux/printk.h> /* printk */
|
#include <linux/printk.h> /* printk */
|
||||||
@@ -23,16 +12,18 @@ which reduces namespace pollution.
|
|||||||
#include "anonymous_inode.h"
|
#include "anonymous_inode.h"
|
||||||
|
|
||||||
static struct dentry *debugfs_file;
|
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)
|
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;
|
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)) {
|
if (copy_to_user(buf, kbuf, ret)) {
|
||||||
ret = -EFAULT;
|
ret = -EFAULT;
|
||||||
}
|
}
|
||||||
|
myval <<= 4;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +38,7 @@ static long unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long ar
|
|||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case LKMC_ANONYMOUS_INODE_GET_FD:
|
case LKMC_ANONYMOUS_INODE_GET_FD:
|
||||||
fd = anon_inode_getfd(
|
fd = anon_inode_getfd(
|
||||||
"random",
|
"todo_what_is_this_for",
|
||||||
&fops_anon,
|
&fops_anon,
|
||||||
NULL,
|
NULL,
|
||||||
O_RDONLY | O_CLOEXEC
|
O_RDONLY | O_CLOEXEC
|
||||||
|
|||||||
@@ -14,10 +14,15 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
int fd_ioctl, fd_ioctl_anon, ret;
|
int fd_ioctl, fd_ioctl_anon, ret;
|
||||||
|
size_t i, nreads;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
puts("Usage: ./prog <ioctl-file>");
|
puts("Usage: ./prog <ioctl-file> [<nreads>]");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
} else if (argc > 2) {
|
||||||
|
nreads = strtol(argv[2], NULL, 10);
|
||||||
|
} else {
|
||||||
|
nreads = 3;
|
||||||
}
|
}
|
||||||
fd_ioctl = open(argv[1], O_RDONLY);
|
fd_ioctl = open(argv[1], O_RDONLY);
|
||||||
if (fd_ioctl == -1) {
|
if (fd_ioctl == -1) {
|
||||||
@@ -29,11 +34,10 @@ int main(int argc, char **argv)
|
|||||||
perror("ioctl");
|
perror("ioctl");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
ret = read(fd_ioctl_anon, buf, sizeof(buf));
|
for (i = 0; i < nreads; ++i) {
|
||||||
printf("%.*s\n", ret, buf);
|
ret = read(fd_ioctl_anon, buf, sizeof(buf));
|
||||||
sleep(1);
|
printf("%.*s\n", ret, buf);
|
||||||
ret = read(fd_ioctl_anon, buf, sizeof(buf));
|
}
|
||||||
printf("%.*s\n", ret, buf);
|
|
||||||
close(fd_ioctl_anon);
|
close(fd_ioctl_anon);
|
||||||
close(fd_ioctl);
|
close(fd_ioctl);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -e
|
set -e
|
||||||
insmod /anonymous_inode.ko
|
insmod /anonymous_inode.ko
|
||||||
/anonymous_inode.out /sys/kernel/debug/lkmc_anonymous_inode
|
[ "$(/anonymous_inode.out /sys/kernel/debug/lkmc_anonymous_inode 3)" = "$(printf '1\n10\n100')" ]
|
||||||
rmmod anonymous_inode
|
rmmod anonymous_inode
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
for test in \
|
for test in \
|
||||||
|
/anonymous_inode.sh \
|
||||||
/character_device.sh \
|
/character_device.sh \
|
||||||
/character_device_create.sh \
|
/character_device_create.sh \
|
||||||
/debugfs.sh \
|
/debugfs.sh \
|
||||||
|
|||||||
Reference in New Issue
Block a user