mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-29 04:54:27 +01:00
Create debugfs files directly on debugfs root instead of a subdirectory
This commit is contained in:
@@ -24,7 +24,7 @@ which reduces namespace pollution.
|
|||||||
|
|
||||||
MODULE_LICENSE("GPL");
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
static struct dentry *dir;
|
static struct dentry *debugfs_file;
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -72,14 +72,13 @@ static const struct file_operations fops_ioctl = {
|
|||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
dir = debugfs_create_dir("lkmc_anonymous_inode", 0);
|
debugfs_file = debugfs_create_file("lkmc_anonymous_inode", 0, NULL, NULL, &fops_ioctl);
|
||||||
debugfs_create_file("f", 0, dir, NULL, &fops_ioctl);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
{
|
{
|
||||||
debugfs_remove_recursive(dir);
|
debugfs_remove(debugfs_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(myinit)
|
module_init(myinit)
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ Here we use debugfs.
|
|||||||
|
|
||||||
MODULE_LICENSE("GPL");
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
static struct dentry *dir;
|
static struct dentry *debugfs_file;
|
||||||
static char data[] = {'a', 'b', 'c', 'd'};
|
static char data[] = {'a', 'b', 'c', 'd'};
|
||||||
|
|
||||||
static int open(struct inode *inode, struct file *filp)
|
static int open(struct inode *inode, struct file *filp)
|
||||||
@@ -143,14 +143,13 @@ static const struct file_operations fops = {
|
|||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
dir = debugfs_create_dir("lkmc_fops", 0);
|
debugfs_file = debugfs_create_file("lkmc_fops", S_IRUSR | S_IWUSR, NULL, NULL, &fops);
|
||||||
debugfs_create_file("f", S_IRUSR | S_IWUSR, dir, NULL, &fops);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
{
|
{
|
||||||
debugfs_remove_recursive(dir);
|
debugfs_remove_recursive(debugfs_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(myinit)
|
module_init(myinit)
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ Documentation/ioctl/ioctl-number.txt has some info:
|
|||||||
|
|
||||||
MODULE_LICENSE("GPL");
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
static struct dentry *dir;
|
static struct dentry *debugfs_file;
|
||||||
|
|
||||||
static long unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long argp)
|
static long unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long argp)
|
||||||
{
|
{
|
||||||
@@ -76,17 +76,16 @@ static const struct file_operations fops = {
|
|||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
dir = debugfs_create_dir("lkmc_ioctl", 0);
|
|
||||||
/* ioctl permissions are not automatically restricted by rwx as for read / write,
|
/* ioctl permissions are not automatically restricted by rwx as for read / write,
|
||||||
* but we could of course implement that ourselves:
|
* but we could of course implement that ourselves:
|
||||||
* https://stackoverflow.com/questions/29891803/user-permission-check-on-ioctl-command */
|
* https://stackoverflow.com/questions/29891803/user-permission-check-on-ioctl-command */
|
||||||
debugfs_create_file("f", 0, dir, NULL, &fops);
|
debugfs_file = debugfs_create_file("lkmc_ioctl", 0, NULL, NULL, &fops);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
{
|
{
|
||||||
debugfs_remove_recursive(dir);
|
debugfs_remove(debugfs_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(myinit)
|
module_init(myinit)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ MODULE_LICENSE("GPL");
|
|||||||
|
|
||||||
static char readbuf[1024];
|
static char readbuf[1024];
|
||||||
static size_t readbuflen;
|
static size_t readbuflen;
|
||||||
static struct dentry *dir;
|
static struct dentry *debugfs_file;
|
||||||
static struct task_struct *kthread;
|
static struct task_struct *kthread;
|
||||||
static wait_queue_head_t waitqueue;
|
static wait_queue_head_t waitqueue;
|
||||||
|
|
||||||
@@ -74,8 +74,8 @@ static const struct file_operations fops = {
|
|||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
dir = debugfs_create_dir("lkmc_poll", 0);
|
debugfs_file = debugfs_create_file(
|
||||||
debugfs_create_file("f", S_IRUSR | S_IWUSR, dir, NULL, &fops);
|
"lkmc_poll", S_IRUSR | S_IWUSR, NULL, NULL, &fops);
|
||||||
init_waitqueue_head(&waitqueue);
|
init_waitqueue_head(&waitqueue);
|
||||||
kthread = kthread_create(kthread_func, NULL, "mykthread");
|
kthread = kthread_create(kthread_func, NULL, "mykthread");
|
||||||
wake_up_process(kthread);
|
wake_up_process(kthread);
|
||||||
@@ -85,7 +85,7 @@ static int myinit(void)
|
|||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
{
|
{
|
||||||
kthread_stop(kthread);
|
kthread_stop(kthread);
|
||||||
debugfs_remove_recursive(dir);
|
debugfs_remove(debugfs_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(myinit)
|
module_init(myinit)
|
||||||
|
|||||||
@@ -44,11 +44,7 @@ static int myinit(void)
|
|||||||
{
|
{
|
||||||
debugfs_file = debugfs_create_file(
|
debugfs_file = debugfs_create_file(
|
||||||
"lkmc_seq_file_single", S_IRUSR, NULL, NULL, &fops);
|
"lkmc_seq_file_single", S_IRUSR, NULL, NULL, &fops);
|
||||||
if (debugfs_file) {
|
return 0;
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -e
|
set -e
|
||||||
insmod /anonymous_inode.ko
|
insmod /anonymous_inode.ko
|
||||||
cd /sys/kernel/debug/lkmc_anonymous_inode/
|
/anonymous_inode.out /sys/kernel/debug/lkmc_anonymous_inode
|
||||||
/anonymous_inode.out f
|
|
||||||
rmmod anonymous_inode
|
rmmod anonymous_inode
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -e
|
set -e
|
||||||
insmod /ioctl.ko
|
insmod /ioctl.ko
|
||||||
cd /sys/kernel/debug/lkmc_ioctl/
|
/ioctl.out /sys/kernel/debug/lkmc_ioctl
|
||||||
/ioctl.out f
|
|
||||||
rmmod ioctl
|
rmmod ioctl
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
insmod /poll.ko
|
insmod /poll.ko
|
||||||
/poll.out /sys/kernel/debug/lkmc_poll/f
|
/poll.out /sys/kernel/debug/lkmc_poll
|
||||||
|
|||||||
@@ -25,3 +25,5 @@ dd if='lkmc_seq_file' bs=1 count=2 skip=2 status=none
|
|||||||
dd if='lkmc_seq_file' bs=4 count=1 skip=0 status=none
|
dd if='lkmc_seq_file' bs=4 count=1 skip=0 status=none
|
||||||
# => 0
|
# => 0
|
||||||
# => 1
|
# => 1
|
||||||
|
|
||||||
|
rmmod seq_file
|
||||||
|
|||||||
@@ -8,3 +8,5 @@ cat 'lkmc_seq_file_single'
|
|||||||
dd if='lkmc_seq_file_single' bs=1 count=3 skip=1
|
dd if='lkmc_seq_file_single' bs=1 count=3 skip=1
|
||||||
# => b
|
# => b
|
||||||
# => c
|
# => c
|
||||||
|
|
||||||
|
rmmod seq_file_single
|
||||||
|
|||||||
Reference in New Issue
Block a user