08 done
This commit is contained in:
76
08/main.c
Normal file
76
08/main.c
Normal file
@ -0,0 +1,76 @@
|
||||
#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];
|
||||
DEFINE_MUTEX(rw_lock);
|
||||
|
||||
static int __init myfd_init(void)
|
||||
{
|
||||
mutex_init(&rw_lock);
|
||||
return misc_register(&myfd_device);
|
||||
}
|
||||
|
||||
static void __exit myfd_cleanup(void)
|
||||
{
|
||||
misc_deregister(&myfd_device);
|
||||
}
|
||||
|
||||
ssize_t myfd_read(struct file *fp, char __user *user, size_t size, loff_t *offs)
|
||||
{
|
||||
ssize_t retval;
|
||||
size_t len;
|
||||
char *tmp;
|
||||
|
||||
mutex_lock(&rw_lock);
|
||||
len = strlen(str);
|
||||
tmp = kmalloc(sizeof(char) * PAGE_SIZE, GFP_KERNEL);
|
||||
for (size_t i = 0,j = len - 1; i < len; i++, j--) {
|
||||
tmp[i] = str[j];
|
||||
}
|
||||
tmp[len] = 0;
|
||||
retval = simple_read_from_buffer(user, size, offs, tmp, len);
|
||||
mutex_unlock(&rw_lock);
|
||||
return retval;
|
||||
}
|
||||
|
||||
ssize_t myfd_write(struct file *fp, const char __user *user, size_t size,
|
||||
loff_t *offs)
|
||||
{
|
||||
ssize_t res;
|
||||
|
||||
if(size >= (PAGE_SIZE - 1))
|
||||
return -EINVAL;
|
||||
mutex_lock(&rw_lock);
|
||||
res = simple_write_to_buffer(str, size, offs, user, size);
|
||||
str[res] = 0x0;
|
||||
mutex_unlock(&rw_lock);
|
||||
return res;
|
||||
}
|
||||
|
||||
module_init(myfd_init);
|
||||
module_exit(myfd_cleanup);
|
Reference in New Issue
Block a user