Skip to content

Commit 237b278

Browse files
committed
kstrto: move doc to README
1 parent 6c24127 commit 237b278

File tree

5 files changed

+60
-24
lines changed

5 files changed

+60
-24
lines changed

README.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4076,6 +4076,30 @@ The module also shows which handlers are registered for each IRQ, as we have obs
40764076

40774077
When in text mode, we can also observe interrupt line 4 with handler `ttyS0` increase continuously as IO goes through the UART.
40784078

4079+
=== Linux kernel utility functions
4080+
4081+
==== kstrto
4082+
4083+
Convert a string to an integer:
4084+
4085+
....
4086+
/kstrto.sh
4087+
echo $?
4088+
....
4089+
4090+
Outcome: the test passes:
4091+
4092+
....
4093+
0
4094+
....
4095+
4096+
Sources:
4097+
4098+
* link:kernel_module/kstrto.c[]
4099+
* link:rootfs_overlay/kstrto.sh[]
4100+
4101+
Bibliography: https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658
4102+
40794103
=== Linux kernel tracing
40804104

40814105
Good overviews:

kernel_module/README.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ Our kernel modules!
1212
. Misc
1313
.. link:virt_to_phys.c[]
1414
.. link:netlink.c[]
15-
. Utilities
16-
.. link:kstrto.c[]
1715
. Hardening
1816
.. link:strlen_overflow.c[]
1917
. Tracing

kernel_module/kstrto.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1-
/*
2-
https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658
3-
4-
Usage:
5-
6-
/kstrto.sh
7-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kstrto */
82

93
#include <linux/debugfs.h>
104
#include <linux/kernel.h>
115
#include <linux/module.h>
12-
#include <uapi/linux/stat.h> /* S_IRUSR */
6+
#include <linux/seq_file.h>
7+
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
8+
#include <uapi/linux/stat.h> /* S_IWUSR */
139

1410
static struct dentry *toplevel_file;
11+
static char read_buf[1024];
12+
13+
static int show(struct seq_file *m, void *v)
14+
{
15+
seq_printf(m, read_buf);
16+
return 0;
17+
}
18+
19+
static int open(struct inode *inode, struct file *file)
20+
{
21+
return single_open(file, show, NULL);
22+
}
1523

1624
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
1725
{
18-
int ret;
19-
unsigned long long res;
20-
ret = kstrtoull_from_user(buf, len, 10, &res);
21-
if (ret) {
26+
ssize_t ret;
27+
int kstrto_return;
28+
unsigned long long kstrto_result;
29+
kstrto_return = kstrtoull_from_user(buf, len, 10, &kstrto_result);
30+
if (kstrto_return) {
2231
/* Negative error code. */
23-
pr_info("ko = %d\n", ret);
24-
return ret;
32+
ret = kstrto_return;
2533
} else {
26-
pr_info("ok = %llu\n", res);
27-
*off= len;
28-
return len;
34+
ret = len;
2935
}
36+
snprintf(read_buf, sizeof(read_buf), "%llu", kstrto_result + 1);
37+
return ret;
3038
}
3139

3240
static const struct file_operations fops = {
41+
.llseek = seq_lseek,
42+
.open = open,
3343
.owner = THIS_MODULE,
44+
.read = seq_read,
45+
.release = single_release,
3446
.write = write,
3547
};
3648

rootfs_overlay/kstrto.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/sh
2+
set -e
3+
f=/sys/kernel/debug/lkmc_kstrto
24
insmod /kstrto.ko
3-
cd /sys/kernel/debug
4-
echo 1234 > lkmc_kstrto
5-
# 1234
6-
echo foobar > lkmc_kstrto
7-
# -22
5+
printf 123 > "$f"
6+
[ "$(cat "$f")" = 124 ]
7+
echo foobar > "$f" && exit 1
8+
rmmod kstrto

rootfs_overlay/test_all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ for test in \
88
/fops.sh \
99
/init_module.sh \
1010
/ioctl.sh \
11+
/kstrto.sh \
1112
/mmap.sh \
1213
/params.sh \
1314
/procfs.sh \

0 commit comments

Comments
 (0)