Reputation: 85
I'm writing a linux kernel module for a class assignment and I've run into a bit of a puzzling issue.
First, the module creates a folder under /proc/
, and creates a file under that folder, /proc/folder/file
.
The implementation is basically like this:
static struct proc_dir_entry* parent;
static struct proc_dir_entry* file;
// ...
int __init mod_init(void) {
parent = proc_mkdir("folder", NULL);
if(parent == NULL) {
return -ENOMEM;
}
file = proc_create("file", 0666, parent, &fops);
if(file == NULL) {
return -ENOMEM;
}
pr_info("Module insertion successful.\n");
return 0;
}
Now there are several issues the module runs into here. First of all, if the files/folders already exist, it returns -ENOMEM and states that the module couldn't be inserted after failing to allocate memory. I'd rather be able to differentiate it between actually being unable to create a pointer to the procfile rather than being unable to overwrite it, and I'm wondering how wto do that.
Next, I remove the module and try deleting the files and folders I made:
void __exit mod_exit(void) {
proc_remove(file);
proc_remove(folder);
}
// (And then I bind the insert/exit functions)
Now (assuming the prior code worked properly), this function works without error. However, it only deletes the child file, it doesn't delete the folder, which goes against the intended function, so /proc/
still contains /parent
TL;DR:
Most of the other code is pretty arbitrary boilerplate stuff that already works as intended, and is mostly specific to my assignment. I just can't figure out how to get this working, since none of the documentation I can find online covers this particular problem (or google just isn't working.)
EDIT:
I forgot to mention, using proc_create with parent
as the parent entry also doesn't work, since proc_mkdir returned NULL. Is there a way to simply get a pointer to the existing folder?
I'm using kernel 6.8.0-48-generic
, on Ubuntu 20.04 LTS.
Upvotes: 0
Views: 62
Reputation: 85
Alright, it turns out there's a separate function proc_remove_subtree()
that's specialized in removing /proc directories and their children, so that solved both of those problems. I still don't have a way to distinguish failed files and existing files, but this eliminates the need to check for that as they're guaranteed to be deleted after removing the module.
Upvotes: 1