This commit is contained in:
2025-07-10 18:48:31 +02:00
parent 85301ea6e0
commit fb12152966
2 changed files with 33 additions and 11 deletions

8
08/Makefile Normal file
View File

@ -0,0 +1,8 @@
obj-m += main.o
PWD := $(CURDIR)
all:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

View File

@ -1,4 +1,3 @@
Little Penguin Linux Kernel Development
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
@ -28,24 +27,35 @@ static struct miscdevice myfd_device = {
};
char str[PAGE_SIZE];
DEFINE_MUTEX(rw_lock);
static int __init myfd_init(void)
{
int retval;
retval = misc_register(&myfd_device);
return 1;
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);
for (size_t i = 0; i < len / 2; i++) {
tmp[i] = str[t];
tmp = kmalloc(sizeof(char) * PAGE_SIZE, GFP_KERNEL);
for (size_t i = 0,j = len - 1; i < len; i++, j--) {
tmp[i] = str[j];
}
return simple_read_from_buffer(user, size, offs, tmp, i);
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,
@ -53,8 +63,12 @@ ssize_t myfd_write(struct file *fp, const char __user *user, size_t size,
{
ssize_t res;
res = simple_write_to_buffer(str, size, offs, user, size) + 1;
str[size + 1] = 0x0;
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;
}