diff --git a/.gitignore b/Code/.gitignore similarity index 100% rename from .gitignore rename to Code/.gitignore diff --git a/Makefile b/Code/Makefile similarity index 87% rename from Makefile rename to Code/Makefile index d8bb814..7eeac20 100644 --- a/Makefile +++ b/Code/Makefile @@ -1,4 +1,4 @@ -obj-m += module.o +obj-m += assignment.o PWD := $(CURDIR) diff --git a/module.c b/Code/assignment.c similarity index 88% rename from module.c rename to Code/assignment.c index bef32a1..b49409b 100644 --- a/module.c +++ b/Code/assignment.c @@ -20,14 +20,15 @@ #include #include -/* Prototypes - this would normally go in a .h file */ +/* Prototypes - this would normally go in a .h file */ static int device_open(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_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 DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */ +#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */ #define BUF_LEN 80 /* Max length of the message from the device */ /* Global variables are declared as static, so are global within the file. */ @@ -74,6 +75,8 @@ static int __init chardev_init(void) static void __exit chardev_exit(void) { + pr_info("Goodbye"); + device_destroy(cls, MKDEV(major, 0)); class_destroy(cls); @@ -84,8 +87,8 @@ static void __exit chardev_exit(void) /* Methods */ /* Called when a process tries to open the device file, like -* "sudo cat /dev/chardev" -*/ + * "sudo cat /dev/chardev" + */ static int device_open(struct inode *inode, struct file *file) { static int counter = 0; @@ -114,30 +117,28 @@ static int device_release(struct inode *inode, struct file *file) } /* Called when a process, which already opened the dev file, attempts to -* read from it. -*/ -static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ + * read from it. + */ +static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ char __user *buffer, /* buffer to fill with data */ - size_t length, /* length of the buffer */ + size_t length, /* length of the buffer */ loff_t *offset) { /* Number of bytes actually written to the buffer */ int bytes_read = 0; const char *msg_ptr = msg; - if (!*(msg_ptr + *offset)) - { /* we are at the end of message */ + if (!*(msg_ptr + *offset)) { /* we are at the end of message */ *offset = 0; /* reset the offset */ - return 0; /* signify end of file */ + return 0; /* signify end of file */ } msg_ptr += *offset; /* 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 - * 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 * the user data segment. */ @@ -147,6 +148,7 @@ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ } *offset += bytes_read; + /* Most read functions return the number of bytes put into the buffer. */ return bytes_read; } diff --git a/README.md b/README.md index 6582cac..32964c7 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Deadline of homework: Oct 30, 2022 (in case that you cannot finish in the class) ## Prerequisits - requires the correct linux headers to be installed - root access is needed + - the Code file is NOT named module.c ## how to run the kernel module - `make` to build the module diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..aac3e8e --- /dev/null +++ b/output.txt @@ -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 diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..41a224b --- /dev/null +++ b/run.sh @@ -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" \ No newline at end of file