mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
kstrto
This commit is contained in:
@@ -1239,6 +1239,8 @@ although `-E` is smarter:
|
|||||||
|
|
||||||
so you should almost always use it, unless you are really counting each cycle ;-)
|
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:
|
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?
|
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
|
=== 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:
|
If you rely on something that BusyBox' init set up for you like networking, you could do:
|
||||||
|
|||||||
@@ -36,6 +36,8 @@
|
|||||||
.. link:character_device.c[]
|
.. link:character_device.c[]
|
||||||
.. link:character_device_create.c[]
|
.. link:character_device_create.c[]
|
||||||
.. link:virt_to_phys.c[]
|
.. link:virt_to_phys.c[]
|
||||||
|
. Utilities
|
||||||
|
.. kstrto
|
||||||
. Hardware device drivers
|
. Hardware device drivers
|
||||||
.. link:pci_min.c[]
|
.. link:pci_min.c[]
|
||||||
.. link:pci.c[]
|
.. link:pci.c[]
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ Only the more basic fops can be implemented in debugfs, e.g. mmap is never calle
|
|||||||
static struct dentry *dir, *toplevel_file;
|
static struct dentry *dir, *toplevel_file;
|
||||||
static u32 value = 42;
|
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)
|
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|||||||
53
kernel_module/kstrto.c
Normal file
53
kernel_module/kstrto.c
Normal 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");
|
||||||
7
rootfs_overlay/kstrto.sh
Executable file
7
rootfs_overlay/kstrto.sh
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
insmod /kstrto.ko
|
||||||
|
cd /sys/kernel/debug
|
||||||
|
echo 1234 > lkmc_kstrto
|
||||||
|
# 1234
|
||||||
|
echo foobar > lkmc_kstrto
|
||||||
|
# -22
|
||||||
Reference in New Issue
Block a user