Home > Net >  Create proc entry in existing dir not possible?
Create proc entry in existing dir not possible?

Time:04-02

Why it is not possible to create a proc_create_data entry in an existing folder like "/proc/foo/"??

this one is fine:

parent = proc_mkdir("foo/subfolder", NULL);
entry = proc_create_data("myentry", 0444, parent, &fops, NULL);

result: /proc/foo/subfolder/myentry

so folders could be created but not entries?

like /proc/foo/myentry

CodePudding user response:

/proc/ is a virtual file system, giving you a window into some of the things the kernel is doing in real time. The directories you see inside /proc/ are programs (e.g. kernel modules) running in the kernel. The files inside them technically don't exist. When you open them, all it does is fetch information from the module about that particular variable, dataset, etc.

You cannot create files in /proc/ because the act of creating a file is to write it to a block device (e.g. hard drive, solid state drive), which a directory will mounted to (e.g. /username/home/someDir will be mounted to your hard drive). If you attempted to create a file in /proc/ it wouldn't work because it's not a file system mounted to a drive, it's just a representation of things going on in the kernel. /proc/ just looks like other directories for the sake of accessibility/readability but is not meant for regular directory or file creation.

If you wanted to create something in /proc/, you would need to write your own kernel module and load it into the kernel with insmod or modprobe. The module name and the details would then exist inside /proc/.

CodePudding user response:

The Linux kernel expects modules to follow a simple principle: Different modules should create different directories under /proc and use these directories for their needs.

Denying this principle could lead to situation when one module could accidentially harm another module, developed by another author.

Linux kernel API is developed in a way when it tries to enforce a "correct usage". E.g. note that the parent parameter has a type struct proc_dir_entry*. Because object of this type is returned only on creation of the directory, and no "search" function is provided to the user, only the creator of the directory could create entries under that directory.

Combination of parent=NULL and relative path in the name parameter allows to "cheat" the above restriction. But you are not intended to do that.

Remember: Linux kernel doesn't physically separate resources of different modules not because it is really safe, but because the modules are trusted to not act wrongly.

  • Related