mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
kstrto: move doc to README
This commit is contained in:
24
README.adoc
24
README.adoc
@@ -4076,6 +4076,30 @@ The module also shows which handlers are registered for each IRQ, as we have obs
|
||||
|
||||
When in text mode, we can also observe interrupt line 4 with handler `ttyS0` increase continuously as IO goes through the UART.
|
||||
|
||||
=== Linux kernel utility functions
|
||||
|
||||
==== kstrto
|
||||
|
||||
Convert a string to an integer:
|
||||
|
||||
....
|
||||
/kstrto.sh
|
||||
echo $?
|
||||
....
|
||||
|
||||
Outcome: the test passes:
|
||||
|
||||
....
|
||||
0
|
||||
....
|
||||
|
||||
Sources:
|
||||
|
||||
* link:kernel_module/kstrto.c[]
|
||||
* link:rootfs_overlay/kstrto.sh[]
|
||||
|
||||
Bibliography: https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658
|
||||
|
||||
=== Linux kernel tracing
|
||||
|
||||
Good overviews:
|
||||
|
||||
@@ -12,8 +12,6 @@ Our kernel modules!
|
||||
. Misc
|
||||
.. link:virt_to_phys.c[]
|
||||
.. link:netlink.c[]
|
||||
. Utilities
|
||||
.. link:kstrto.c[]
|
||||
. Hardening
|
||||
.. link:strlen_overflow.c[]
|
||||
. Tracing
|
||||
|
||||
@@ -1,36 +1,48 @@
|
||||
/*
|
||||
https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658
|
||||
|
||||
Usage:
|
||||
|
||||
/kstrto.sh
|
||||
*/
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kstrto */
|
||||
|
||||
#include <linux/debugfs.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <uapi/linux/stat.h> /* S_IRUSR */
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
|
||||
#include <uapi/linux/stat.h> /* S_IWUSR */
|
||||
|
||||
static struct dentry *toplevel_file;
|
||||
static char read_buf[1024];
|
||||
|
||||
static int show(struct seq_file *m, void *v)
|
||||
{
|
||||
seq_printf(m, read_buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int open(struct inode *inode, struct file *file)
|
||||
{
|
||||
return single_open(file, show, NULL);
|
||||
}
|
||||
|
||||
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) {
|
||||
ssize_t ret;
|
||||
int kstrto_return;
|
||||
unsigned long long kstrto_result;
|
||||
kstrto_return = kstrtoull_from_user(buf, len, 10, &kstrto_result);
|
||||
if (kstrto_return) {
|
||||
/* Negative error code. */
|
||||
pr_info("ko = %d\n", ret);
|
||||
return ret;
|
||||
ret = kstrto_return;
|
||||
} else {
|
||||
pr_info("ok = %llu\n", res);
|
||||
*off= len;
|
||||
return len;
|
||||
ret = len;
|
||||
}
|
||||
snprintf(read_buf, sizeof(read_buf), "%llu", kstrto_result + 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const struct file_operations fops = {
|
||||
.llseek = seq_lseek,
|
||||
.open = open,
|
||||
.owner = THIS_MODULE,
|
||||
.read = seq_read,
|
||||
.release = single_release,
|
||||
.write = write,
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
f=/sys/kernel/debug/lkmc_kstrto
|
||||
insmod /kstrto.ko
|
||||
cd /sys/kernel/debug
|
||||
echo 1234 > lkmc_kstrto
|
||||
# 1234
|
||||
echo foobar > lkmc_kstrto
|
||||
# -22
|
||||
printf 123 > "$f"
|
||||
[ "$(cat "$f")" = 124 ]
|
||||
echo foobar > "$f" && exit 1
|
||||
rmmod kstrto
|
||||
|
||||
@@ -8,6 +8,7 @@ for test in \
|
||||
/fops.sh \
|
||||
/init_module.sh \
|
||||
/ioctl.sh \
|
||||
/kstrto.sh \
|
||||
/mmap.sh \
|
||||
/params.sh \
|
||||
/procfs.sh \
|
||||
|
||||
Reference in New Issue
Block a user