Skip to content

Commit 923f655

Browse files
committed
anonymous inode: move doc to readme
1 parent 662d4f2 commit 923f655

File tree

6 files changed

+50
-24
lines changed

6 files changed

+50
-24
lines changed

README.adoc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3101,6 +3101,37 @@ Sources:
31013101

31023102
Bibliography: https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867
31033103

3104+
==== Anonymous inode
3105+
3106+
In guest:
3107+
3108+
....
3109+
/anonymous_inode.sh
3110+
echo $?
3111+
....
3112+
3113+
Outcome: the test passes:
3114+
3115+
....
3116+
0
3117+
....
3118+
3119+
Sources:
3120+
3121+
* link:kernel_module/anonymous_inode.c[]
3122+
* link:kernel_module/anonymous_inode.h[]
3123+
* link:kernel_module/user/anonymous_inode.c[]
3124+
* link:rootfs_overlay/anonymous_inode.sh[]
3125+
3126+
This example:
3127+
3128+
* gets an anonymous inode via `ioctl` from a debugfs entry `anon_inode_getfd` from a debugfs file
3129+
* read jiffies from that inode
3130+
3131+
Anonymous inodes allow getting multiple file descriptors from a single filesystem entry, which reduces namespace pollution compared to creating multiple device files.
3132+
3133+
Bibliography: https://stackoverflow.com/questions/4508998/what-is-an-anonymous-inode-in-linux
3134+
31043135
=== Kernel panic and oops
31053136

31063137
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:
77347765
./run -F '/test_all.sh;/poweroff.out' | grep lkmc_test
77357766
....
77367767

7737-
which outputs to stdout `lkmc_test_pass` or `lkmc_test_fail` to stdout, which we can grep from host to automate testing.
7768+
which outputs `lkmc_test_pass` or `lkmc_test_fail`.
77387769

77397770
Source: link:rootfs_overlay/test_all.sh[].
77407771

kernel_module/README.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
... link:dep.c[]
2020
... link:dep2.c[]
2121
. Pseudo filesystems
22-
.. link:anonymous_inode.c[]
2322
.. link:ioctl.c[]
2423
.. link:mmap.c[]
2524
.. link:poll.c[]

kernel_module/anonymous_inode.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
/*
2-
https://stackoverflow.com/questions/4508998/what-is-anonymous-inode
3-
4-
anon_inode_getfd example:
5-
6-
- get an anonymous inode via ioctl from a debugfs entry
7-
- read jiffies from that inode
8-
9-
This method allows getting multiple file descriptors from a single filesystem,
10-
which reduces namespace pollution.
11-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#anonymous-inode */
122

133
#include <linux/anon_inodes.h>
144
#include <linux/debugfs.h>
155
#include <linux/errno.h> /* EFAULT */
166
#include <linux/fs.h>
17-
#include <linux/jiffies.h>
187
#include <linux/kernel.h> /* min */
198
#include <linux/module.h>
209
#include <linux/printk.h> /* printk */
@@ -23,16 +12,18 @@ which reduces namespace pollution.
2312
#include "anonymous_inode.h"
2413

2514
static struct dentry *debugfs_file;
15+
static u32 myval = 1;
2616

2717
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
2818
{
29-
char kbuf[1024];
19+
char kbuf[8];
3020
size_t ret;
3121

32-
ret = snprintf(kbuf, sizeof(kbuf), "%llu", (unsigned long long)jiffies);
22+
ret = snprintf(kbuf, sizeof(kbuf), "%x", myval);
3323
if (copy_to_user(buf, kbuf, ret)) {
3424
ret = -EFAULT;
3525
}
26+
myval <<= 4;
3627
return ret;
3728
}
3829

@@ -47,7 +38,7 @@ static long unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long ar
4738
switch (cmd) {
4839
case LKMC_ANONYMOUS_INODE_GET_FD:
4940
fd = anon_inode_getfd(
50-
"random",
41+
"todo_what_is_this_for",
5142
&fops_anon,
5243
NULL,
5344
O_RDONLY | O_CLOEXEC

kernel_module/user/anonymous_inode.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ int main(int argc, char **argv)
1414
{
1515
char buf[1024];
1616
int fd_ioctl, fd_ioctl_anon, ret;
17+
size_t i, nreads;
1718

1819
if (argc < 2) {
19-
puts("Usage: ./prog <ioctl-file>");
20+
puts("Usage: ./prog <ioctl-file> [<nreads>]");
2021
return EXIT_FAILURE;
22+
} else if (argc > 2) {
23+
nreads = strtol(argv[2], NULL, 10);
24+
} else {
25+
nreads = 3;
2126
}
2227
fd_ioctl = open(argv[1], O_RDONLY);
2328
if (fd_ioctl == -1) {
@@ -29,11 +34,10 @@ int main(int argc, char **argv)
2934
perror("ioctl");
3035
return EXIT_FAILURE;
3136
}
32-
ret = read(fd_ioctl_anon, buf, sizeof(buf));
33-
printf("%.*s\n", ret, buf);
34-
sleep(1);
35-
ret = read(fd_ioctl_anon, buf, sizeof(buf));
36-
printf("%.*s\n", ret, buf);
37+
for (i = 0; i < nreads; ++i) {
38+
ret = read(fd_ioctl_anon, buf, sizeof(buf));
39+
printf("%.*s\n", ret, buf);
40+
}
3741
close(fd_ioctl_anon);
3842
close(fd_ioctl);
3943
return EXIT_SUCCESS;

rootfs_overlay/anonymous_inode.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22
set -e
33
insmod /anonymous_inode.ko
4-
/anonymous_inode.out /sys/kernel/debug/lkmc_anonymous_inode
4+
[ "$(/anonymous_inode.out /sys/kernel/debug/lkmc_anonymous_inode 3)" = "$(printf '1\n10\n100')" ]
55
rmmod anonymous_inode

rootfs_overlay/test_all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/sh
22
for test in \
3+
/anonymous_inode.sh \
34
/character_device.sh \
45
/character_device_create.sh \
56
/debugfs.sh \

0 commit comments

Comments
 (0)