finished code

This commit is contained in:
paul-loedige 2022-10-28 16:04:51 +09:00
parent 1cf29b71ed
commit 38609c46ec
6 changed files with 98 additions and 16 deletions

View File

View File

@ -1,4 +1,4 @@
obj-m += module.o obj-m += assignment.o
PWD := $(CURDIR) PWD := $(CURDIR)

View File

@ -24,7 +24,8 @@
static int device_open(struct inode *, struct file *); static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *); static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char __user *, size_t, loff_t *); static ssize_t device_read(struct file *, char __user *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char __user *, size_t, loff_t *); static ssize_t device_write(struct file *, const char __user *, size_t,
loff_t *);
#define SUCCESS 0 #define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */ #define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
@ -74,6 +75,8 @@ static int __init chardev_init(void)
static void __exit chardev_exit(void) static void __exit chardev_exit(void)
{ {
pr_info("Goodbye");
device_destroy(cls, MKDEV(major, 0)); device_destroy(cls, MKDEV(major, 0));
class_destroy(cls); class_destroy(cls);
@ -125,8 +128,7 @@ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
int bytes_read = 0; int bytes_read = 0;
const char *msg_ptr = msg; const char *msg_ptr = msg;
if (!*(msg_ptr + *offset)) if (!*(msg_ptr + *offset)) { /* we are at the end of message */
{ /* we are at the end of message */
*offset = 0; /* reset the offset */ *offset = 0; /* reset the offset */
return 0; /* signify end of file */ return 0; /* signify end of file */
} }
@ -134,8 +136,7 @@ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
msg_ptr += *offset; msg_ptr += *offset;
/* Actually put the data into the buffer */ /* Actually put the data into the buffer */
while (length && *msg_ptr) while (length && *msg_ptr) {
{
/* The buffer is in the user data segment, not the kernel /* The buffer is in the user data segment, not the kernel
* segment so "*" assignment won't work. We have to use * segment so "*" assignment won't work. We have to use
* put_user which copies data from the kernel data segment to * put_user which copies data from the kernel data segment to
@ -147,6 +148,7 @@ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
} }
*offset += bytes_read; *offset += bytes_read;
/* Most read functions return the number of bytes put into the buffer. */ /* Most read functions return the number of bytes put into the buffer. */
return bytes_read; return bytes_read;
} }

View File

@ -10,6 +10,7 @@ Deadline of homework: Oct 30, 2022 (in case that you cannot finish in the class)
## Prerequisits ## Prerequisits
- requires the correct linux headers to be installed - requires the correct linux headers to be installed
- root access is needed - root access is needed
- the Code file is NOT named module.c
## how to run the kernel module ## how to run the kernel module
- `make` to build the module - `make` to build the module

67
output.txt Normal file
View File

@ -0,0 +1,67 @@
Character devices:
1 mem
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/ptmx
7 vcs
10 misc
13 input
29 fb
116 alsa
128 ptm
136 pts
180 usb
188 ttyUSB
189 usb_device
202 cpu/msr
203 cpu/cpuid
226 drm
235 chardev
236 binder
237 hidraw
238 wwan_port
239 nvme-generic
240 nvme
241 aux
242 bsg
243 watchdog
244 remoteproc
245 ptp
246 pps
247 cec
248 lirc
249 rtc
250 dma_heap
251 dax
252 dimmctl
253 ndctl
254 gpiochip
Block devices:
8 sd
65 sd
66 sd
67 sd
68 sd
69 sd
70 sd
71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
259 blkext
==================================================
I already told you 0 times Hello world!
I already told you 1 times Hello world!
==================================================
[Fri Oct 28 15:47:13 2022] I was assigned major number 235.
[Fri Oct 28 15:47:13 2022] Device created on /dev/chardev
[Fri Oct 28 15:47:13 2022] Goodbye

12
run.sh Normal file
View File

@ -0,0 +1,12 @@
cd ./Code
make
sudo insmod assignment.ko
cat /proc/devices > ../output.txt
echo "==================================================" >> ../output.txt
sudo cat /dev/chardev >> ../output.txt
sudo cat /dev/chardev >> ../output.txt
echo "==================================================" >> ../output.txt
sudo rmmod assignment
sleep 5
sudo dmesg -T -l info | tail -3 >> ../output.txt
echo "Done"