I’m trying to get all the mount points in a kernel module. Below is what I’ve come up with. It segfaults because of the strcat. Is this the correct way to get the mount points? Will this work? if so how do i fix the segfault? If not, how does one go about getting the mount points in a linux kernel module?
I’ve tried cycle the whole namespace looking for mountpoint roots that match but its from 2003 and the kernel has changed so much so its basically useless. Also tried get filesystem mount point in kernel module but again its from 2012 so it to is outdated.
static int __init misc_init(void) { struct path path; struct dentry *thedentry; struct dentry *curdentry; kern_path("/", LOOKUP_FOLLOW, &path); thedentry = path.dentry; list_for_each_entry(curdentry, &thedentry->d_subdirs, d_child) { kern_path(strncat("/", curdentry->d_name.name, strlen(curdentry->d_name.name)), LOOKUP_FOLLOW, &path); if (path_is_mountpoint(&path)) { printk("%s: is a mountpoint", curdentry->d_name.name); } else printk("%s: is not a mountpoint", curdentry->d_name.name); } return 0; }
Advertisement
Answer
There are flags for it in the dentry struct. d_flags. and there is a flag DCACHED_MOUNTED. Get the current pointer. The fs_struct in there. then the root. this gives you the root of the current filesystem. from there loop though all the subdirs and if d_flags & DCACHE_MOUNTED passes then it is a mount point.
ssize_t read_proc(struct file *filp, char *buf, size_t len, loff_t *offp ) { struct dentry *curdentry; list_for_each_entry(curdentry, ¤t->fs->root.mnt->mnt_root->d_subdirs, d_child) { if ( curdentry->d_flags & DCACHE_MOUNTED) printk("%s is mounted", curdentry->d_name.name); } return 0; }