Merge branch 'master' of github.com:cirosantilli/linux-kernel-module-cheat

This commit is contained in:
Ciro Santilli
2018-04-13 13:41:12 +01:00
9 changed files with 149 additions and 35 deletions

View File

@@ -1239,6 +1239,8 @@ although `-E` is smarter:
so you should almost always use it, unless you are really counting each cycle ;-)
This method prevents the BusyBox' init from launching a shell, so you cannot interact with the system afterwards. If you also want that, use: <<init-busybox>>.
If the script is large, you can add it to a gitignored file and pass that to `-E` as in:
....
@@ -1286,6 +1288,7 @@ But this fails when we are `init` itself!
but why not just use your super simple and effective `/poweroff.out` and be done with it?
[[init-busybox]]
=== Run command at the end of BusyBox init
If you rely on something that BusyBox' init set up for you like networking, you could do:

View File

@@ -9,7 +9,7 @@
.. link:params.c[]
.. link:vermagic.c[]
.. link:vermagic_fail.c[]
.. link:module_version.c[]
.. link:module_info.c[]
. Pseudo filesystems
.. link:debugfs.c[]
.. link:procfs.c[]
@@ -36,6 +36,8 @@
.. link:character_device.c[]
.. link:character_device_create.c[]
.. link:virt_to_phys.c[]
. Utilities
.. kstrto
. Hardware device drivers
.. link:pci_min.c[]
.. link:pci.c[]

View File

@@ -18,20 +18,51 @@ Only the more basic fops can be implemented in debugfs, e.g. mmap is never calle
#include <linux/module.h>
#include <uapi/linux/stat.h> /* S_IRUSR */
static struct dentry *dir;
static struct dentry *dir, *toplevel_file;
static u32 value = 42;
/* This basically re-implents the write operation of debugfs_create_u32,
* it is just an excuse to illustrate a fop. */
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
int ret;
unsigned long long res;
/* https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel */
ret = kstrtoull_from_user(buf, len, 10, &res);
if (ret) {
/* Negative error code. */
return ret;
} else {
value = res;
*off= len;
return len;
}
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.write = write,
};
static int myinit(void)
{
struct dentry *file;
dir = debugfs_create_dir("lkmc_debugfs", 0);
if (!dir) {
printk(KERN_ALERT "debugfs_create_dir failed");
pr_alert("debugfs_create_dir failed");
return -1;
}
file = debugfs_create_u32("myfile", S_IRUSR, dir, &value);
file = debugfs_create_u32("myfile", S_IRUSR | S_IWUSR, dir, &value);
if (!file) {
printk(KERN_ALERT "debugfs_create_u32 failed");
pr_alert("debugfs_create_u32 failed");
return -1;
}
/* Created on the toplevel of the debugfs mount,
* and with explicit fops instead of a fixed integer value. */
toplevel_file = debugfs_create_file(
"lkmc_debugfs_file", S_IWUSR, NULL, NULL, &fops);
if (!toplevel_file) {
return -1;
}
return 0;
@@ -40,6 +71,7 @@ static int myinit(void)
static void myexit(void)
{
debugfs_remove_recursive(dir);
debugfs_remove(toplevel_file);
}
module_init(myinit)

View File

@@ -14,6 +14,9 @@ Hello world module.
static int myinit(void)
{
pr_info("hello init\n");
/* 0 for success, any negative value means failure,
* E* consts if you want to specify failure cause.
* https://www.linux.com/learn/kernel-newbie-corner-loadable-kernel-modules-coming-and-going */
return 0;
}

53
kernel_module/kstrto.c Normal file
View File

@@ -0,0 +1,53 @@
/*
https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658
Usage:
/kstrto.sh
*/
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <uapi/linux/stat.h> /* S_IRUSR */
static struct dentry *toplevel_file;
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
int ret;
unsigned long long res;
ret = kstrtoull_from_user(buf, len, 10, &res);
if (ret) {
/* Negative error code. */
pr_info("ko = %d\n", ret);
return ret;
} else {
pr_info("ok = %llu\n", res);
*off= len;
return len;
}
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.write = write,
};
static int myinit(void)
{
toplevel_file = debugfs_create_file("lkmc_kstrto", S_IWUSR, NULL, NULL, &fops);
if (!toplevel_file) {
return -1;
}
return 0;
}
static void myexit(void)
{
debugfs_remove(toplevel_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,32 @@
/*
- https://stackoverflow.com/questions/4839024/how-to-find-the-version-of-a-compiled-kernel-module/42556565#42556565
- https://stackoverflow.com/questions/19467150/significance-of-this-module-in-linux-driver/49812248#49812248
Usage:
insmod /module_info.ko
# dmesg => name = module_info
# dmesg => version = 1.0
cat /sys/module/module_info/version
# => module_info
modinfo /module_info.ko | grep -E '^version:'
# => version: 1.0
*/
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
/* Set by default based on the module file name. */
pr_info("name = %s\n", THIS_MODULE->name);
pr_info("version = %s\n", THIS_MODULE->version);
return 0;
}
static void myexit(void) {}
module_init(myinit)
module_exit(myexit)
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");

View File

@@ -1,28 +0,0 @@
/*
https://stackoverflow.com/questions/4839024/how-to-find-the-version-of-a-compiled-kernel-module/42556565#42556565
insmod /module_version.ko
cat /sys/modules/module_version/version
# => 1.0
cat /sys/module/module_version/srcversion
# => AB0F06618BC3A36B687CDC5
modinfo /module_version.ko | grep -E '^(src|)version'
# => version: 1.0
# => srcversion: AB0F06618BC3A36B687CDC5
*/
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
pr_info(__FILE__ "\n");
return 0;
}
static void myexit(void) {}
module_init(myinit)
module_exit(myexit)
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");

View File

@@ -1,12 +1,22 @@
#!/bin/sh
set -ex
mkdir -p /debugfs
d=/debugfs
mkdir -p "$d"
# We also added a fstab entry that mounts this under /sys/kernel/debug autmoatically.
# That is the most common place to mount it.
# The /sys/kernel/debug directory gets created automatically when debugfs is
# compiled into the kernel, but it does not get mounted automatically.
mount -t debugfs none /debugfs
insmod /debugfs.ko
cd /debugfs/lkmc_debugfs
cd "${d}/lkmc_debugfs"
cat myfile
# => 42
echo 13 > myfile
cat myfile
# => 13
echo 666 > "${d}/lkmc_debugfs_file"
cat myfile
# => 666

7
rootfs_overlay/kstrto.sh Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/sh
insmod /kstrto.ko
cd /sys/kernel/debug
echo 1234 > lkmc_kstrto
# 1234
echo foobar > lkmc_kstrto
# -22