init
This commit is contained in:
7
ass05/Makefile
Normal file
7
ass05/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
|
74
ass05/main.c
Normal file
74
ass05/main.c
Normal file
@ -0,0 +1,74 @@
|
||||
#include <linux/miscdevice.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/init.h>
|
||||
|
||||
#define STUDENT_LOGIN "cchauvet"
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
static ssize_t misc_write(struct file *file, const char __user *buf,
|
||||
size_t len, loff_t *ppos)
|
||||
{
|
||||
char data[10];
|
||||
size_t len_red = 10 > len ? len : 10;
|
||||
|
||||
if (copy_from_user(data, buf, len_red))
|
||||
return -EFAULT;
|
||||
|
||||
if (strcmp(data, STUDENT_LOGIN))
|
||||
return -EINVAL;
|
||||
|
||||
return len_red;
|
||||
}
|
||||
|
||||
static ssize_t misc_read(struct file *file, char __user *buf,
|
||||
size_t count, loff_t *f_pos)
|
||||
{
|
||||
char data[] = STUDENT_LOGIN;
|
||||
size_t len = strlen(data);
|
||||
|
||||
if(*f_pos > 0)
|
||||
return 0;
|
||||
|
||||
if (copy_to_user(buf, data, len))
|
||||
return -EFAULT;
|
||||
|
||||
*f_pos += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
const struct file_operations fops = {
|
||||
.write = misc_write,
|
||||
.read = misc_read,
|
||||
};
|
||||
|
||||
static struct miscdevice misc_device = {
|
||||
.name = "fortytwo",
|
||||
.minor = MISC_DYNAMIC_MINOR,
|
||||
.fops = &fops,
|
||||
};
|
||||
|
||||
static int __init module_start(void)
|
||||
{
|
||||
int error = misc_register(&misc_device);
|
||||
|
||||
if (error) {
|
||||
pr_err("misc_register failed!!!\n");
|
||||
return error;
|
||||
}
|
||||
|
||||
pr_info("misc_register init done!!!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit module_end(void)
|
||||
{
|
||||
misc_deregister(&misc_device);
|
||||
pr_info("misc_register exit done!!!\n");
|
||||
}
|
||||
|
||||
module_init(module_start)
|
||||
module_exit(module_end)
|
||||
|
Reference in New Issue
Block a user