From 2e4d67d510409204c3392fceede8f7a637f6ae58 Mon Sep 17 00:00:00 2001 From: tomoron Date: Fri, 11 Jul 2025 19:35:43 +0200 Subject: [PATCH] corrections on 03 and 08, 09 done --- 03/main.c | 17 +++++++++-------- 08/main.c | 22 ++++++++++++---------- 09/Makefile | 9 +++++++++ 09/main.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 09/rw.c | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 09/Makefile create mode 100644 09/main.c create mode 100644 09/rw.c diff --git a/03/main.c b/03/main.c index 6193381..164b17d 100644 --- a/03/main.c +++ b/03/main.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include @@ -7,21 +8,21 @@ MODULE_LICENSE("GPL"); MODULE_AUTHOR("42"); MODULE_DESCRIPTION("A module that does work"); - -static int do_work(int *nbLoop) +static int do_work(int *nb_loop) { int x; - for (x = 0; x < *nbLoop; ++x) { - udelay(10); - } - if (*nbLoop > 10) - pr_info("do_work(): slept a long time\n"); - return(x * *nbLoop); + + for (x = 0; x < *nb_loop; ++x) + usleep_range(10); + if (*nb_loop > 10) + pr_info("%s: slept a long time\n", __func__); + return(x * *nb_loop); } static int __init module_example_42_init(void) { int x = 10; + x = do_work(&x); return x; } diff --git a/08/main.c b/08/main.c index d90bbf2..450644c 100644 --- a/08/main.c +++ b/08/main.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include @@ -9,12 +10,12 @@ MODULE_LICENSE("GPL"); MODULE_AUTHOR("Louis Solofrizzo "); MODULE_DESCRIPTION("Useless module"); -static ssize_t myfd_read (struct file *fp, char __user *user, -size_t size, loff_t *offs); +static ssize_t myfd_read(struct file *fp, char __user *user, + size_t size, loff_t *offs); static ssize_t myfd_write(struct file *fp, const char __user *user, -size_t size, loff_t *offs); + size_t size, loff_t *offs); -static struct file_operations myfd_fops = { +static const struct file_operations myfd_fops = { .owner = THIS_MODULE, .read = &myfd_read, .write = &myfd_write @@ -27,7 +28,8 @@ static struct miscdevice myfd_device = { }; char str[PAGE_SIZE]; -DEFINE_MUTEX(rw_lock); + +DEFINE_MUTEX(rw_lock); //lock read/write operations of the /dev/reverse file static int __init myfd_init(void) { @@ -45,25 +47,25 @@ ssize_t myfd_read(struct file *fp, char __user *user, size_t size, loff_t *offs) ssize_t retval; size_t len; char *tmp; - + mutex_lock(&rw_lock); len = strlen(str); tmp = kmalloc(sizeof(char) * PAGE_SIZE, GFP_KERNEL); - for (size_t i = 0,j = len - 1; i < len; i++, j--) { + for (size_t i = 0, j = len - 1; i < len; i++, j--) tmp[i] = str[j]; - } tmp[len] = 0; retval = simple_read_from_buffer(user, size, offs, tmp, len); mutex_unlock(&rw_lock); + kfree(tmp); return retval; } ssize_t myfd_write(struct file *fp, const char __user *user, size_t size, - loff_t *offs) + loff_t *offs) { ssize_t res; - if(size >= (PAGE_SIZE - 1)) + if (size >= (PAGE_SIZE - 1)) return -EINVAL; mutex_lock(&rw_lock); res = simple_write_to_buffer(str, size, offs, user, size); diff --git a/09/Makefile b/09/Makefile new file mode 100644 index 0000000..ba8d657 --- /dev/null +++ b/09/Makefile @@ -0,0 +1,9 @@ +obj-m += mounts.o +mounts-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 diff --git a/09/main.c b/09/main.c new file mode 100644 index 0000000..82edcd5 --- /dev/null +++ b/09/main.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +#include +#include +#include + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("create /proc/mymounts file that lists mounts"); + +struct proc_dir_entry *proc_file; + +int list_mounts(struct seq_file *m, void *v); + +static int list_mounts_open(struct inode *inode, struct file *file) +{ + return single_open(file, list_mounts, NULL); +} + +static const struct proc_ops proc_fops = { + .proc_open = list_mounts_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = single_release, +}; + +static int __init mounts_init(void) +{ + proc_file = proc_create("mymounts", 0444, 0, &proc_fops); + if(IS_ERR(proc_file)) return PTR_ERR(proc_file); + return 0; +} + +static void __exit mounts_exit(void) +{ + proc_remove(proc_file); +} + +module_init(mounts_init); +module_exit(mounts_exit); + diff --git a/09/rw.c b/09/rw.c new file mode 100644 index 0000000..fca14b5 --- /dev/null +++ b/09/rw.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include <../fs/mount.h> + +int list_mounts(struct seq_file *m, void *v); + +int list_mounts(struct seq_file *m, void *v) +{ + struct rb_node *cur; + struct mount *mnt; + char *buf; + char *path; + + cur = current->nsproxy->mnt_ns->mnt_first_node; + + buf = kmalloc(sizeof(char) * PATH_MAX, GFP_KERNEL); + while(cur) + { + mnt = rb_entry(cur, struct mount, mnt_node); + + struct path p = { + .mnt = &mnt->mnt, + .dentry = mnt->mnt.mnt_root, + }; + + path = d_path(&p, buf, PATH_MAX); + if(IS_ERR(path)) + path = 0; + seq_printf(m, "%-10s %s\n", mnt->mnt_devname, path); + cur = rb_next(cur); + } + kfree(buf); + return 0; +}