add assignement 00 to 08
This commit is contained in:
9
05/Makefile
Normal file
9
05/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
obj-m += fortytwo.o
|
||||
fortytwo-y = main.o rw.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
|
6
05/boot.log
Normal file
6
05/boot.log
Normal file
@ -0,0 +1,6 @@
|
||||
[10006.564959] creating fortytwo misc device
|
||||
[10011.739012] someone read from the file
|
||||
[10011.740989] someone read from the file
|
||||
[10026.526506] someone wrote the wrong string
|
||||
[10031.246367] someone wrote the right string
|
||||
[10068.207415] removing forytwo misc device
|
42
05/main.c
Normal file
42
05/main.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <linux/init.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/miscdevice.h>
|
||||
#include <linux/uaccess.h>
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_DESCRIPTION("hello world module");
|
||||
|
||||
#define FILENAME "fortytwo"
|
||||
|
||||
ssize_t dev_read(struct file *f, char __user *buf, size_t len, loff_t *off);
|
||||
ssize_t dev_write(struct file *f, const char __user *buf, size_t len, loff_t *off);
|
||||
|
||||
static const struct file_operations fops = {
|
||||
.owner = THIS_MODULE,
|
||||
.read = dev_read,
|
||||
.write = dev_write,
|
||||
};
|
||||
|
||||
static struct miscdevice dev = {
|
||||
.minor = MISC_DYNAMIC_MINOR,
|
||||
.name = FILENAME,
|
||||
.fops = &fops,
|
||||
.mode = 0666,
|
||||
};
|
||||
|
||||
static int __init fortytwo_init(void)
|
||||
{
|
||||
pr_info("creating fortytwo misc device\n");
|
||||
return misc_register(&dev);
|
||||
}
|
||||
|
||||
static void __exit fortytwo_exit(void)
|
||||
{
|
||||
pr_info("removing forytwo misc device\n");
|
||||
misc_deregister(&dev);
|
||||
}
|
||||
|
||||
module_init(fortytwo_init);
|
||||
module_exit(fortytwo_exit);
|
||||
|
35
05/rw.c
Normal file
35
05/rw.c
Normal file
@ -0,0 +1,35 @@
|
||||
#include <linux/init.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/miscdevice.h>
|
||||
#include <linux/uaccess.h>
|
||||
|
||||
#define VALUE "tomoron"
|
||||
|
||||
ssize_t dev_read(struct file *f, char __user *buf, size_t len, loff_t *off);
|
||||
ssize_t dev_write(struct file *f, const char __user *buf, size_t len, loff_t *off);
|
||||
|
||||
ssize_t dev_read(struct file *f, char __user *buf, size_t len, loff_t *off) {
|
||||
char msg[] = VALUE;
|
||||
pr_info("someone read from the file\n");
|
||||
return simple_read_from_buffer(buf, len, off, msg, sizeof(msg) - 1);
|
||||
}
|
||||
|
||||
|
||||
ssize_t dev_write(struct file *f, const char __user *buf, size_t len, loff_t *off) {
|
||||
char kbuf[64];
|
||||
if (len > sizeof(kbuf) - 1)
|
||||
return -EINVAL;
|
||||
if (copy_from_user(kbuf, buf, len))
|
||||
return -EINVAL;
|
||||
|
||||
kbuf[len] = '\0';
|
||||
|
||||
if (strcmp(kbuf, VALUE) == 0)
|
||||
{
|
||||
pr_info("someone wrote the right string\n");
|
||||
return len;
|
||||
}
|
||||
pr_info("someone wrote the wrong string\n");
|
||||
return -EINVAL;
|
||||
}
|
Reference in New Issue
Block a user