42_little-penguin/ass05/main.c
2024-08-07 12:33:08 +02:00

75 lines
1.4 KiB
C

#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)