poll sketch

This commit is contained in:
Ciro Santilli
2017-05-24 09:57:49 +01:00
parent 6c028f9398
commit 3eaac95c0f
7 changed files with 126 additions and 3 deletions

View File

@@ -30,6 +30,11 @@ Each module comes from a C file under `kernel_module/`. For module usage see:
head kernel_module/*.c
Good bets inside guest are:
/modulename.sh
/modulename.out
After the first build, you can also run just:
./runqemu
@@ -221,6 +226,7 @@ ARM TODOs:
1. [debugfs](kernel_module/debugfs.c)
1. [panic](kernel_module/panic.c)
1. [fops](kernel_module/fops.c)
1. [poll](poll.c)
1. Asynchronous
1. [workqueue](kernel_module/workqueue.c)
1. [sleep](kernel_module/sleep.c)

View File

@@ -14,7 +14,7 @@ Requires `CONFIG_DEBUG_FS=y`.
MODULE_LICENSE("GPL");
static struct dentry *dir = 0;
static struct dentry *dir;
static u32 value = 42;
int init_module(void)

View File

@@ -5,6 +5,8 @@ Usage:
/fops.sh
The buffer can be written and read from. If data overflows, data is thrown away.
No, there ain't no official docs:
http://stackoverflow.com/questions/15213932/what-are-the-struct-file-operations-arguments
@@ -25,7 +27,7 @@ Here we use debugfs.
MODULE_LICENSE("GPL");
static struct dentry *dir = 0;
static struct dentry *dir;
static char data[] = {'a', 'b', 'c', 'd'};

75
kernel_module/poll.c Normal file
View File

@@ -0,0 +1,75 @@
/*
Basic poll file_operation example.
Waits for a second, give jiffies to user, wait for a second...
usleep_range
*/
#include <asm/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/debugfs.h>
#include <linux/delay.h> /* usleep_range */
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h>
#include <linux/jiffies.h>
#include <linux/kernel.h> /* min */
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/printk.h> /* printk */
MODULE_LICENSE("GPL");
static struct dentry *dir;
static struct task_struct *kthread;
static wait_queue_head_t waitqueue;
static ssize_t read(struct file *file, char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
char s[1024];
ret = snprintf(s, sizeof(s), "%llu", (unsigned long long)jiffies);
if (copy_to_user(buf, s, ret)) {
ret = -EFAULT;
}
return ret;
}
unsigned int poll(struct file *filp, struct poll_table_struct *wait)
{
/*TODO*/
/*wait_event_interruptible(waitqueue, (dev->rp != dev->wp));*/
pr_info("poll_wait before\n");
poll_wait(filp, &waitqueue, wait);
pr_info("poll_wait after\n");
return POLLIN;
}
static int kthread_func(void *data)
{
while (!kthread_should_stop()) {
usleep_range(1000000, 1000001);
wake_up_interruptible(&waitqueue);
}
return 0;
}
static const struct file_operations fops = {
.read = read,
.poll = poll
};
int init_module(void)
{
dir = debugfs_create_dir("kernel_module_cheat_poll", 0);
debugfs_create_file("f", 0666, dir, NULL, &fops);
init_waitqueue_head(&waitqueue);
kthread = kthread_create(kthread_func, NULL, "mykthread");
return 0;
}
void cleanup_module(void)
{
debugfs_remove_recursive(dir);
}

37
kernel_module/test/poll.c Normal file
View File

@@ -0,0 +1,37 @@
#define _XOPEN_SOURCE 700
#include <fcntl.h> /* creat, O_CREAT */
#include <poll.h> /* poll */
#include <stdio.h> /* printf, puts, snprintf */
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
#include <unistd.h> /* read */
int main(int argc, char **argv) {
char buf[1024];
int fd, n;
short revents;
struct pollfd pfd;
fd = open(argv[1], O_RDONLY | O_NONBLOCK);
pfd.fd = fd;
pfd.events = POLLIN;
while (1) {
puts("loop");
poll(&pfd, 1, -1);
revents = pfd.revents;
if (revents & POLLIN) {
n = read(pfd.fd, buf, sizeof(buf));
printf("POLLIN n=%d buf=%.*s\n", n, n, buf);
}
if (revents & POLLHUP) {
printf("POLLHUP\n");
close(pfd.fd);
break;
}
if (revents & POLLNVAL) {
printf("POLLNVAL\n");
}
if (revents & POLLERR) {
printf("POLLERR\n");
}
}
}

3
rootfs_overlay/poll.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/sh
insmod /poll.ko
/poll.out /sys/kernel/debug/kernel_module_cheat_poll/f

2
run
View File

@@ -2,7 +2,7 @@
set -e
cd buildroot
arch='x86_64'
while getopts a: OPT; do
while getopts a: OPT > /dev/null 2>&1; do
case "$OPT" in
a)
arch=$OPTARG