corrections on 03 and 08, 09 done

This commit is contained in:
2025-07-11 19:35:43 +02:00
parent fb12152966
commit 2e4d67d510
5 changed files with 115 additions and 18 deletions

9
09/Makefile Normal file
View File

@ -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

44
09/main.c Normal file
View File

@ -0,0 +1,44 @@
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/sched.h>
#include <linux/nsproxy.h>
#include <linux/mount.h>
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);

41
09/rw.c Normal file
View File

@ -0,0 +1,41 @@
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/nsproxy.h>
#include <linux/sched.h>
#include <linux/fs.h>
#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;
}