08 done
This commit is contained in:
8
08/Makefile
Normal file
8
08/Makefile
Normal 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
|
@ -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;
|
||||
mutex_init(&rw_lock);
|
||||
return misc_register(&myfd_device);
|
||||
}
|
||||
|
||||
retval = misc_register(&myfd_device);
|
||||
return 1;
|
||||
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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user