failed fops attempt

This commit is contained in:
Ciro Santilli
2017-06-08 07:49:14 +01:00
parent 3893ea9272
commit bf538cd3a6
5 changed files with 88 additions and 21 deletions

View File

@@ -319,6 +319,7 @@ But TODO I don't think you can see where you are in the kernel source code and l
1. [panic](kernel_module/panic.c) 1. [panic](kernel_module/panic.c)
1. [params](kernel_module/params.c) 1. [params](kernel_module/params.c)
1. [fops](kernel_module/fops.c) 1. [fops](kernel_module/fops.c)
1. [ioctl](ioctl.c)
1. [poll](poll.c) 1. [poll](poll.c)
1. Asynchronous 1. Asynchronous
1. [workqueue](kernel_module/workqueue.c) 1. [workqueue](kernel_module/workqueue.c)

View File

@@ -28,10 +28,9 @@ Here we use debugfs.
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
static struct dentry *dir; static struct dentry *dir;
static char data[] = {'a', 'b', 'c', 'd'}; static char data[] = {'a', 'b', 'c', 'd'};
static int fop_open(struct inode *inode, struct file *file) static int open(struct inode *inode, struct file *filp)
{ {
printk(KERN_INFO "open\n"); printk(KERN_INFO "open\n");
return 0; return 0;
@@ -42,7 +41,7 @@ static int fop_open(struct inode *inode, struct file *file)
* Then when userland reads the same file descriptor again, * Then when userland reads the same file descriptor again,
* we start from that point instead. * we start from that point instead.
* */ * */
static ssize_t fop_read(struct file *file, char __user *buf, size_t len, loff_t *off) static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{ {
ssize_t ret; ssize_t ret;
printk(KERN_INFO "read\n"); printk(KERN_INFO "read\n");
@@ -67,7 +66,7 @@ static ssize_t fop_read(struct file *file, char __user *buf, size_t len, loff_t
* we must return ENOSPC if the user tries to write more * we must return ENOSPC if the user tries to write more
* than the size of our buffer. Otherwise, Bash > just * than the size of our buffer. Otherwise, Bash > just
* keeps trying to write to it infinitely. */ * keeps trying to write to it infinitely. */
static ssize_t fop_write(struct file *file, 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)
{ {
ssize_t ret; ssize_t ret;
printk(KERN_INFO "write\n"); printk(KERN_INFO "write\n");
@@ -96,13 +95,13 @@ static ssize_t fop_write(struct file *file, const char __user *buf, size_t len,
Called on the last close: Called on the last close:
http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called-release-in-struct-file-operations-in-the-l http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called-release-in-struct-file-operations-in-the-l
*/ */
static int fop_release (struct inode *inode, struct file *file) static int release (struct inode *inode, struct file *filp)
{ {
printk(KERN_INFO "release\n"); printk(KERN_INFO "release\n");
return 0; return 0;
} }
static loff_t fop_llseek(struct file *filp, loff_t off, int whence) static loff_t llseek(struct file *filp, loff_t off, int whence)
{ {
loff_t newpos; loff_t newpos;
printk(KERN_INFO "llseek\n"); printk(KERN_INFO "llseek\n");
@@ -129,26 +128,17 @@ static loff_t fop_llseek(struct file *filp, loff_t off, int whence)
} }
static const struct file_operations fops = { static const struct file_operations fops = {
.llseek = fop_llseek, .llseek = llseek,
.open = fop_open, .open = open,
.read = fop_read, .read = read,
.release = fop_release, .release = release,
.write = fop_write, .write = write,
}; };
static int myinit(void) static int myinit(void)
{ {
struct dentry *file;
dir = debugfs_create_dir("lkmc_fops", 0); dir = debugfs_create_dir("lkmc_fops", 0);
if (!dir) { debugfs_create_file("f", 0666, dir, NULL, &fops);
printk(KERN_ALERT "debugfs_create_dir failed");
return -1;
}
file = debugfs_create_file("f", 0666, dir, NULL, &fops);
if (!file) {
printk(KERN_ALERT "debugfs_create_file failed");
return -1;
}
return 0; return 0;
} }

36
kernel_module/ioctl.c Normal file
View File

@@ -0,0 +1,36 @@
/*
Give integer and a pointer to the kernel, and get one positive integer out.
*/
#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/printk.h> /* printk */
MODULE_LICENSE("GPL");
static struct dentry *dir;
static long unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
pr_info("cmd = %u\n", cmd);
return cmd + 1;
}
static const struct file_operations fops = {
.unlocked_ioctl = unlocked_ioctl
};
static int myinit(void)
{
dir = debugfs_create_dir("lkmc_ioctl", 0);
debugfs_create_file("f", 0666, dir, NULL, &fops);
return 0;
}
static void myexit(void)
{
debugfs_remove_recursive(dir);
}
module_init(myinit)
module_exit(myexit)

View File

@@ -0,0 +1,33 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char **argv)
{
int fd, ret;
if (argc < 4) {
puts("Usage: ./prog <ioctl-file> <cmd> <arg>");
return EXIT_FAILURE;
}
fd = open(argv[1], O_RDONLY);
if (fd == -1) {
perror("open");
return EXIT_FAILURE;
}
ret = ioctl(fd, strtoul(argv[2], NULL, 10), argv[3]);
if (ret == -1) {
perror("ioctl");
return EXIT_FAILURE;
}
printf("ret = %d\n", ret);
printf("errno = %d\n", errno);
close(fd);
return EXIT_SUCCESS;
}

7
rootfs_overlay/ioctl.sh Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/sh
set -e
insmod /ioctl.ko
cd /sys/kernel/debug/lkmc_ioctl/
/ioctl.out f 2 1
#/ioctl.out f 1 0
rmmod ioctl