init
This commit is contained in:
7
ass08/Makefile
Normal file
7
ass08/Makefile
Normal file
@ -0,0 +1,7 @@
|
||||
obj-m += main.o
|
||||
|
||||
all:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
|
||||
|
||||
clean:
|
||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
|
63
ass08/main.c
Normal file
63
ass08/main.c
Normal file
@ -0,0 +1,63 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/miscdevice.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/slab.h>
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Louis Solofrizzo <louis@ne02ptzero.me>");
|
||||
MODULE_DESCRIPTION("Useless module");
|
||||
|
||||
static ssize_t myfd_read(struct file *fp, char __user *user, size_t size, loff_t *offs);
|
||||
static ssize_t myfd_write(struct file *fp, const char __user *user,
|
||||
size_t size, loff_t *offs);
|
||||
|
||||
static struct file_operations myfd_fops = {
|
||||
.owner = THIS_MODULE,
|
||||
.read = &myfd_read,
|
||||
.write = &myfd_write
|
||||
};
|
||||
|
||||
static struct miscdevice myfd_device = {
|
||||
.minor = MISC_DYNAMIC_MINOR,
|
||||
.name = "reverse",
|
||||
.fops = &myfd_fops
|
||||
};
|
||||
|
||||
char str[PAGE_SIZE];
|
||||
|
||||
static int __init myfd_init(void)
|
||||
{
|
||||
str[0] = 0;
|
||||
misc_register(&myfd_device);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit myfd_cleanup(void)
|
||||
{
|
||||
misc_deregister(&myfd_device);
|
||||
}
|
||||
|
||||
ssize_t myfd_read(struct file *fp, char __user *buf, size_t size, loff_t *offs)
|
||||
{
|
||||
size_t str_len;
|
||||
char tmp[PAGE_SIZE + 1];
|
||||
|
||||
str_len = strlen(str);
|
||||
for (size_t i = 0; i != str_len; i++)
|
||||
tmp[i] = str[str_len - 1 - i];
|
||||
return simple_read_from_buffer(buf, size, offs, tmp, str_len);
|
||||
}
|
||||
|
||||
ssize_t myfd_write(struct file *fp, const char __user *buf, size_t size, loff_t *offs)
|
||||
{
|
||||
ssize_t res;
|
||||
|
||||
res = simple_write_to_buffer(str, sizeof(str), offs, buf, PAGE_SIZE) + 1;
|
||||
str[size] = 0x0;
|
||||
return res;
|
||||
}
|
||||
|
||||
module_init(myfd_init);
|
||||
module_exit(myfd_cleanup)
|
Reference in New Issue
Block a user