ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi ..
I wrote the kernel module and I want to read regular file for get the context in that file to do something and I have a problem, How to read file from kernel module ??
Please help me for suggetion , I'm newbie.
Thanks you.
I know very little about programming kernel modules, but isn't it possible to just call open(2) and read(2). They are system calls, and as such in the kernel, after all.
it doesnt make sense to call open from the kernel because open returns a file descriptor which is specific to a process but the kernel isnt a process. eg every process has file descriptor STDOUT_FILENO which is 1, but they go to different places this can only work because they are in different processes, the kernel isnt a process and therefore it cant have file descriptors. (i dont think ive explained this very well if someone can explain better please do)
im sure its possible to concoct some method of opening a file from inside the kernel but i wouldnt reccommend it as it will most likely cause security problems/race conditions/etc...
my suggestion is to use the proc interface or run a user mode daemon to talk to your module.
If the function in your module is called from a system call, then any file descriptors will be in the context of the process that made the system call. For instance, if it is in your init function, then it will be in the context of the insmod process. As long as you read the file and close it before the function returns, you should be all right. You should probably make a module option to specify the file to read. What is the file? Config, data, etc.?
If it doesn't ever change, try converting the file to an array in a header. That way, it will be built right into the module itself. Almost all of the raw data in the kernel is done this way. It shouldn't be too difficult to create a program to convert a binary to a char array.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.