kstrto: move doc to README

This commit is contained in:
Ciro Santilli
2018-07-05 02:01:17 +01:00
parent 6c241279a6
commit 237b27869e
5 changed files with 60 additions and 24 deletions

View File

@@ -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

View File

@@ -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,
};